]> jfr.im git - erebus.git/blobdiff - erebus.py
urls - remove broken APIs
[erebus.git] / erebus.py
index 6bc5246f48aca235c3d27eacc4daeadd6541d729..0377d92ded68ae4014a3546e917048ec4303bb2d 100644 (file)
--- a/erebus.py
+++ b/erebus.py
@@ -1,13 +1,16 @@
 #!/usr/bin/python
+# vim: fileencoding=utf-8
 
 # Erebus IRC bot - Author: John Runyon
 # main startup code
 
-import os, sys, select, MySQLdb, MySQLdb.cursors, time, random
+from __future__ import print_function
+
+import os, sys, select, MySQLdb, MySQLdb.cursors, time, random, gc
 import bot, config, ctlmod
 
-class Erebus(object):
-       APIVERSION = 1
+class Erebus(object): #singleton to pass around
+       APIVERSION = 0
        RELEASE = 0
 
        bots = {}
@@ -26,6 +29,13 @@ class Erebus(object):
 
                        self.chans = []
 
+               def msg(self, *args, **kwargs):
+                       main.randbot().msg(self, *args, **kwargs)
+               def slowmsg(self, *args, **kwargs):
+                       main.randbot().slowmsg(self, *args, **kwargs)
+               def fastmsg(self, *args, **kwargs):
+                       main.randbot().fastmsg(self, *args, **kwargs)
+
                def isauthed(self):
                        return self.auth is not None
 
@@ -38,8 +48,8 @@ class Erebus(object):
                        if self.auth is None:
                                self.glevel = -1
                        else:
-                               c = main.db.cursor()
-                               if c.execute("SELECT level FROM users WHERE auth = %s", (self.auth,)):
+                               c = main.query("SELECT level FROM users WHERE auth = %s", (self.auth,))
+                               if c:
                                        row = c.fetchone()
                                        if row is not None:
                                                self.glevel = row['level']
@@ -50,15 +60,19 @@ class Erebus(object):
                        return self.glevel
 
                def join(self, chan):
-                       self.chans.append(chan)
+                       if chan not in self.chans: self.chans.append(chan)
                def part(self, chan):
-                       self.chans.remove(chan)
-               def quit(self):
-                       for chan in self.chans:
+                       try:
                                self.chans.remove(chan)
+                       except: pass
+                       return len(self.chans) == 0
+               def quit(self):
+                       pass
+               def nickchange(self, newnick):
+                       self.nick = newnick
 
                def __str__(self): return self.nick
-               def __repr__(self): return "<User %r (%d)>" % (self.nick,self.glevel)
+               def __repr__(self): return "<User %r (%d)>" % (self.nick, self.glevel)
 
        class Channel(object):
                def __init__(self, name, bot):
@@ -70,8 +84,8 @@ class Erebus(object):
                        self.voices = []
                        self.ops = []
 
-                       c = main.db.cursor()
-                       if c.execute("SELECT user, level FROM chusers WHERE chan = %s", (self.name,)):
+                       c = main.query("SELECT user, level FROM chusers WHERE chan = %s", (self.name,))
+                       if c:
                                row = c.fetchone()
                                while row is not None:
                                        self.levels[row['user']] = row['level']
@@ -79,7 +93,11 @@ class Erebus(object):
 
 
                def msg(self, *args, **kwargs):
-                       self.bot.msg(self.name, *args, **kwargs)
+                       self.bot.msg(self, *args, **kwargs)
+               def slowmsg(self, *args, **kwargs):
+                       self.bot.slowmsg(self, *args, **kwargs)
+               def fastmsg(self, *args, **kwargs):
+                       self.bot.fastmsg(self, *args, **kwargs)
 
                def levelof(self, auth):
                        if auth is None:
@@ -93,8 +111,8 @@ class Erebus(object):
                def setlevel(self, auth, level, savetodb=True):
                        auth = auth.lower()
                        if savetodb:
-                               c = main.db.cursor()
-                               if c.execute("REPLACE INTO chusers (chan, user, level) VALUES (%s, %s, %s)", (self.name, auth, level)):
+                               c = main.query("REPLACE INTO chusers (chan, user, level) VALUES (%s, %s, %s)", (self.name, auth, level))
+                               if c:
                                        self.levels[auth] = level
                                        return True
                                else:
@@ -122,6 +140,8 @@ class Erebus(object):
                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":
@@ -131,6 +151,34 @@ class Erebus(object):
                        self.potype = "select"
                        self.fdlist = []
 
+       def query(self, *args, **kwargs):
+               if 'noretry' in kwargs:
+                       noretry = kwargs['noretry']
+                       del kwargs['noretry']
+               else:
+                       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:
+                       curs = self.db.cursor()
+                       res = curs.execute(*args, **kwargs)
+                       if res:
+                               return curs
+                       else:
+                               return res
+               except MySQLdb.MySQLError as e:
+                       self.log("[SQL]", "!", "MySQL error! %r" % (e))
+                       if not noretry:
+                               dbsetup()
+                               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)
@@ -148,20 +196,22 @@ class Erebus(object):
        def fd(self, fileno): #get Bot() by fd/fileno
                return self.fds[fileno]
        def randbot(self): #get Bot() randomly
-               return self.bots[random.choice(self.bots.keys())]
+               return self.bots[random.choice(list(self.bots.keys()))]
 
-       def user(self, _nick, justjoined=False):
+       def user(self, _nick, justjoined=False, create=True):
                nick = _nick.lower()
                if nick in self.users:
                        return self.users[nick]
-               else:
+               elif create:
                        user = self.User(_nick)
                        self.users[nick] = user
 
                        if justjoined:
-                               self.randbot().conn.send("WHO %s n%%ant,2" % (nick))
+                               self.randbot().conn.send("WHO %s n%%ant,1" % (nick))
 
                        return user
+               else:
+                       return None
        def channel(self, name): #get Channel() by name
                if name.lower() in self.chans:
                        return self.chans[name.lower()]
@@ -174,19 +224,34 @@ class Erebus(object):
                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.itervalues():
+               for bot in self.bots.values():
                        if bot.conn.state == 0:
                                bot.connect()
 
        def module(self, name):
                return ctlmod.modules[name]
 
+       def log(self, source, level, message):
+               print("%09.3f %s [%s] %s" % (time.time() % 100000, source, level, message))
+
+       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:
@@ -228,36 +293,34 @@ class Erebus(object):
                return self.chanhandlers[chan]
 
 
-class MyCursor(MySQLdb.cursors.DictCursor):
-       def execute(self, *args, **kwargs):
-               print "[SQL] [#] MyCursor.execute(self, %s, %s)" % (', '.join([repr(i) for i in args]), ', '.join([str(key)+"="+repr(kwargs[key]) for key in kwargs]))
-               try:
-                       super(self.__class__, self).execute(*args, **kwargs)
-               except MySQLdb.MySQLError as e:
-                       print "[SQL] [!] MySQL error! %r" % (e)
-                       dbsetup()
-                       return False
-               return True
-
-
 def dbsetup():
        main.db = None
-       main.db = MySQLdb.connect(host=cfg.dbhost, user=cfg.dbuser, passwd=cfg.dbpass, db=cfg.dbname, cursorclass=MyCursor)
+       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.Config('bot.config')
+
+       if cfg.getboolean('debug', 'gc'):
+               gc.set_debug(gc.DEBUG_LEAK)
+
+       pidfile = open(cfg.pidfile, 'w')
+       pidfile.write(str(os.getpid()))
+       pidfile.close()
+
        main = Erebus(cfg)
+       dbsetup()
 
        autoloads = [mod for mod, yes in cfg.items('autoloads') if int(yes) == 1]
        for mod in autoloads:
-               print "Loading %s" % (mod)
                ctlmod.load(main, mod)
 
-       dbsetup()
-       c = main.db.cursor()
-       if c.execute("SELECT nick, user, bind, authname, authpass FROM bots WHERE active = 1"):
+       c = main.query("SELECT nick, user, bind, authname, authpass FROM bots WHERE active = 1")
+       if c:
                rows = c.fetchall()
                c.close()
                for row in rows:
@@ -269,7 +332,14 @@ 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()))
+       except: pass
+       sys.stdout = open('logfile', 'w', 1)
+       sys.stderr = sys.stdout
        setup()
        while True: loop()