]> jfr.im git - erebus.git/blobdiff - erebus.py
err... yeah.
[erebus.git] / erebus.py
index d0d41a713a6222b1f17d489ce6ef97750e4aa55d..66f6cd2532a38695b7c178765e0a58b70dead2b7 100644 (file)
--- a/erebus.py
+++ b/erebus.py
@@ -5,7 +5,7 @@
 
 #TODO: tons
 
-import os, sys, select, MySQLdb, MySQLdb.cursors
+import os, sys, select, MySQLdb, MySQLdb.cursors, time
 import bot, config, ctlmod
 
 class Erebus(object):
@@ -14,6 +14,7 @@ class Erebus(object):
        mods = {}
        numhandlers = {}
        msghandlers = {}
+       chanhandlers = {}
        users = {}
        chans = {}
 
@@ -38,10 +39,12 @@ class Erebus(object):
                                self.glevel = -1
                        else:
                                c = main.db.cursor()
-                               c.execute("SELECT level FROM users WHERE auth = %s", (self.auth,))
-                               row = c.fetchone()
-                               if row is not None:
-                                       self.glevel = row['level']
+                               if c.execute("SELECT level FROM users WHERE auth = %s", (self.auth,)):
+                                       row = c.fetchone()
+                                       if row is not None:
+                                               self.glevel = row['level']
+                                       else:
+                                               self.glevel = 0
                                else:
                                        self.glevel = 0
                        return self.glevel
@@ -65,11 +68,11 @@ class Erebus(object):
                        self.ops = []
 
                        c = main.db.cursor()
-                       c.execute("SELECT user, level FROM chusers WHERE chan = %s", (self.name,))
-                       row = c.fetchone()
-                       while row is not None:
-                               self.levels[row['user']] = row['level']
+                       if c.execute("SELECT user, level FROM chusers WHERE chan = %s", (self.name,)):
                                row = c.fetchone()
+                               while row is not None:
+                                       self.levels[row['user']] = row['level']
+                                       row = c.fetchone()
 
 
                def levelof(self, auth):
@@ -83,8 +86,11 @@ class Erebus(object):
                        auth = auth.lower()
                        if savetodb:
                                c = main.db.cursor()
-                               c.execute("REPLACE INTO chusers (chan, user, level) VALUES (%s, %s, %s)", (self.name, auth, level))
-                       self.levels[auth] = level
+                               if c.execute("REPLACE INTO chusers (chan, user, level) VALUES (%s, %s, %s)", (self.name, auth, level)):
+                                       self.levels[auth] = level
+                                       return True
+                               else:
+                                       return False
 
                def userjoin(self, user, level=None):
                        if user not in self.users: self.users.append(user)
@@ -196,6 +202,18 @@ class Erebus(object):
        def getnumhook(self, word):
                return self.numhandlers[word]
 
+       def hookchan(self, chan, handler):
+               try:
+                       self.chanhandlers[chan].append(handler)
+               except:
+                       self.chanhandlers[chan] = [handler]
+       def unhookchan(self, chan, handler):
+               if chan in self.chanhandlers and handler in self.chanhandlers[chan]:
+                       self.chanhandlers[chan].remove(handler)
+       def haschanhook(self, chan):
+               return chan in self.chanhandlers and len(self.chanhandlers[chan]) != 0
+       def getchanhook(self, chan):
+               return self.chanhandlers[chan]
 
 
 class MyCursor(MySQLdb.cursors.DictCursor):
@@ -205,16 +223,13 @@ class MyCursor(MySQLdb.cursors.DictCursor):
                        super(self.__class__, self).execute(*args, **kwargs)
                except MySQLdb.MySQLError as e:
                        print "[SQL] [!] MySQL error! %r" % (e)
-                       try:
-                               dbsetup()
-                               super(self.__class__, self).execute(*args, **kwargs)
-                       except MySQLdb.MySQLError as e:
-                               print "[SQL] [!!!] DOUBLE MySQL error! %r" % (e)
-                               print "* [!!!] Giving up."
-                               sys.exit()
+                       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)
 
 def setup():
@@ -230,11 +245,11 @@ def setup():
 
        dbsetup()
        c = main.db.cursor()
-       c.execute("SELECT nick, user, bind FROM bots WHERE active = 1")
-       rows = c.fetchall()
-       c.close()
-       for row in rows:
-               main.newbot(row['nick'], row['user'], row['bind'], cfg.host, cfg.port, cfg.realname)
+       if c.execute("SELECT nick, user, bind FROM bots WHERE active = 1"):
+               rows = c.fetchall()
+               c.close()
+               for row in rows:
+                       main.newbot(row['nick'], row['user'], row['bind'], cfg.host, cfg.port, cfg.realname)
        main.connectall()
 
 def loop():