]> jfr.im git - erebus.git/blobdiff - erebus.py
modlib - reorder hook functions and code to be consistent
[erebus.git] / erebus.py
index 0377d92ded68ae4014a3546e917048ec4303bb2d..91e01bfad02c4e91e0ac47c924811315ef3882c4 100644 (file)
--- a/erebus.py
+++ b/erebus.py
@@ -18,13 +18,17 @@ class Erebus(object): #singleton to pass around
        numhandlers = {}
        msghandlers = {}
        chanhandlers = {}
+       exceptionhandlers = [] # list of (Exception_class, handler_function) tuples
        users = {}
        chans = {}
 
        class User(object):
                def __init__(self, nick, auth=None):
                        self.nick = nick
-                       self.auth = auth
+                       if auth is None:
+                               self.auth = None
+                       else:
+                               self.auth = auth.lower()
                        self.checklevel()
 
                        self.chans = []
@@ -59,6 +63,23 @@ class Erebus(object): #singleton to pass around
                                        self.glevel = 0
                        return self.glevel
 
+               def setlevel(self, level, savetodb=True):
+                       if savetodb:
+                               if level != 0:
+                                       c = main.query("REPLACE INTO users (auth, level) VALUES (%s, %s)", (self.auth, level))
+                               else:
+                                       c = main.query("DELETE FROM users WHERE auth = %s", (self.auth,))
+                                       if c == 0: # no rows affected
+                                               c = True # is fine
+                               if c:
+                                       self.glevel = level
+                                       return True
+                               else:
+                                       return False
+                       else:
+                               self.glevel = level
+                               return True
+
                def join(self, chan):
                        if chan not in self.chans: self.chans.append(chan)
                def part(self, chan):
@@ -117,6 +138,9 @@ class Erebus(object): #singleton to pass around
                                        return True
                                else:
                                        return False
+                       else:
+                               self.levels[auth] = level
+                               return True
 
                def userjoin(self, user, level=None):
                        if user not in self.users: self.users.append(user)
@@ -292,6 +316,15 @@ class Erebus(object): #singleton to pass around
        def getchanhook(self, chan):
                return self.chanhandlers[chan]
 
+       def hookexception(self, exc, handler):
+               self.exceptionhandlers.append((exc, handler))
+       def unhookexception(self, exc, handler):
+               self.exceptionhandlers.remove((exc, handler))
+       def hasexceptionhook(self, exc):
+               return any((True for x,h in self.exceptionhandlers if isinstance(exc, x)))
+       def getexceptionhook(self, exc):
+               return (h for x,h in self.exceptionhandlers if isinstance(exc, x))
+
 
 def dbsetup():
        main.db = None