]> jfr.im git - erebus.git/blobdiff - erebus.py
remove a debugging print
[erebus.git] / erebus.py
index fc3aca08ef98abcbabc6f46856fe768cb5e6ea04..42a2f416cefef07c20fdcfc3935431a63934ec9a 100644 (file)
--- a/erebus.py
+++ b/erebus.py
@@ -1,4 +1,5 @@
 #!/usr/bin/python
+# vim: fileencoding=utf-8
 
 # Erebus IRC bot - Author: John Runyon
 # main startup code
@@ -23,7 +24,10 @@ class Erebus(object): #singleton to pass around
        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 = []
@@ -58,6 +62,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):
@@ -116,6 +137,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)
@@ -139,6 +163,8 @@ class Erebus(object): #singleton to pass around
                def __repr__(self): return "<Channel %r>" % (self.name)
 
        def __init__(self, cfg):
+               self.mustquit = None
+               self.starttime = time.time()
                self.cfg = cfg
                self.trigger = cfg.trigger
                if os.name == "posix":
@@ -149,11 +175,11 @@ class Erebus(object): #singleton to pass around
                        self.fdlist = []
 
        def query(self, *args, **kwargs):
-               if 'norecurse' in kwargs:
-                       norecurse = kwargs['norecurse']
-                       del kwargs['norecurse']
+               if 'noretry' in kwargs:
+                       noretry = kwargs['noretry']
+                       del kwargs['noretry']
                else:
-                       norecurse = False
+                       noretry = False
 
                self.log("[SQL]", "?", "query(%s, %s)" % (', '.join([repr(i) for i in args]), ', '.join([str(key)+"="+repr(kwargs[key]) for key in kwargs])))
                try:
@@ -165,12 +191,17 @@ class Erebus(object): #singleton to pass around
                                return res
                except MySQLdb.MySQLError as e:
                        self.log("[SQL]", "!", "MySQL error! %r" % (e))
-                       if not norecurse:
+                       if not noretry:
                                dbsetup()
-                               return self.query(*args, norecurse=True, **kwargs)
+                               return self.query(*args, noretry=True, **kwargs)
                        else:
                                raise e
 
+       def querycb(self, cb, *args, **kwargs):
+               def run_query():
+                       cb(self.query(*args, **kwargs))
+               threading.Thread(target=run_query).start()
+
        def newbot(self, nick, user, bind, authname, authpass, server, port, realname):
                if bind is None: bind = ''
                obj = bot.Bot(self, nick, user, bind, authname, authpass, server, port, realname)
@@ -216,10 +247,12 @@ class Erebus(object): #singleton to pass around
                return chan
 
        def poll(self):
+               timeout_seconds = 30
                if self.potype == "poll":
-                       return [fd for (fd, ev) in self.po.poll()]
+                       pollres = self.po.poll(timeout_seconds * 1000)
+                       return [fd for (fd, ev) in pollres]
                elif self.potype == "select":
-                       return select.select(self.fdlist, [], [])[0]
+                       return select.select(self.fdlist, [], [], timeout_seconds)[0]
 
        def connectall(self):
                for bot in self.bots.values():
@@ -235,6 +268,13 @@ class Erebus(object): #singleton to pass around
        def getuserbyauth(self, auth):
                return [u for u in self.users.values() if u.auth == auth.lower()]
 
+       def getdb(self):
+               """Get a DB object. The object must be returned to the pool after us, using returndb()."""
+               return self.dbs.pop()
+
+       def returndb(self, db):
+               self.dbs.append(db)
+
        #bind functions
        def hook(self, word, handler):
                try:
@@ -278,12 +318,15 @@ class Erebus(object): #singleton to pass around
 
 def dbsetup():
        main.db = None
+       main.dbs = []
+       for i in range(cfg.get('erebus', 'num_db_connections', 2)-1):
+               main.dbs.append(MySQLdb.connect(host=cfg.dbhost, user=cfg.dbuser, passwd=cfg.dbpass, db=cfg.dbname, cursorclass=MySQLdb.cursors.DictCursor))
        main.db = MySQLdb.connect(host=cfg.dbhost, user=cfg.dbuser, passwd=cfg.dbpass, db=cfg.dbname, cursorclass=MySQLdb.cursors.DictCursor)
 
 def setup():
        global cfg, main
 
-       cfg = config.setup('bot.config')
+       cfg = config.Config('bot.config')
 
        if cfg.getboolean('debug', 'gc'):
                gc.set_debug(gc.DEBUG_LEAK)
@@ -293,12 +336,12 @@ def setup():
        pidfile.close()
 
        main = Erebus(cfg)
+       dbsetup()
 
        autoloads = [mod for mod, yes in cfg.items('autoloads') if int(yes) == 1]
        for mod in autoloads:
                ctlmod.load(main, mod)
 
-       dbsetup()
        c = main.query("SELECT nick, user, bind, authname, authpass FROM bots WHERE active = 1")
        if c:
                rows = c.fetchall()
@@ -312,6 +355,9 @@ def loop():
        for fileno in poready:
                for line in main.fd(fileno).getdata():
                        main.fd(fileno).parse(line)
+       if main.mustquit is not None:
+               main.log('*', '!', 'Core exiting due to: %s' % (main.mustquit))
+               raise main.mustquit
 
 if __name__ == '__main__':
        try: os.rename('logfile', 'oldlogs/%s' % (time.time()))