X-Git-Url: https://jfr.im/git/erebus.git/blobdiff_plain/8af0407d500b8e27e8f3628f1083be30b6d2cf75..9557ee54c1ff9e7866004937957eb57617627641:/erebus.py diff --git a/erebus.py b/erebus.py index 7c7dc92..66f6cd2 100644 --- a/erebus.py +++ b/erebus.py @@ -5,14 +5,16 @@ #TODO: tons -import os, sys, select, MySQLdb, MySQLdb.cursors +import os, sys, select, MySQLdb, MySQLdb.cursors, time import bot, config, ctlmod class Erebus(object): bots = {} fds = {} mods = {} + numhandlers = {} msghandlers = {} + chanhandlers = {} users = {} chans = {} @@ -28,8 +30,8 @@ class Erebus(object): return self.auth is not None def authed(self, auth): - if auth == '0': auth = None - self.auth = auth + if auth == '0': self.auth = None + else: self.auth = auth.lower() self.checklevel() def checklevel(self): @@ -37,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 @@ -54,15 +58,40 @@ class Erebus(object): def __repr__(self): return "" % (self.nick,self.glevel) class Channel(object): - def __init__(self, name, bot, levels={}): + def __init__(self, name, bot): self.name = name self.bot = bot - self.levels = levels + self.levels = {} self.users = [] self.voices = [] self.ops = [] + c = main.db.cursor() + 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): + auth = auth.lower() + if auth in self.levels: + return self.levels[auth] + else: + return 0 + + 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)): + 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) if level == 'op' and user not in self.ops: self.ops.append(user) @@ -130,8 +159,8 @@ class Erebus(object): else: return None - def newchannel(self, bot, name, levels={}): - chan = self.Channel(name.lower(), bot, levels) + def newchannel(self, bot, name): + chan = self.Channel(name.lower(), bot) self.chans[name.lower()] = chan return chan @@ -148,14 +177,61 @@ class Erebus(object): #bind functions def hook(self, word, handler): - self.msghandlers[word] = handler - def unhook(self, word): - del self.msghandlers[word] + try: + self.msghandlers[word].append(handler) + except: + self.msghandlers[word] = [handler] + def unhook(self, word, handler): + if word in self.msghandlers and handler in self.msghandlers[word]: + self.msghandlers[word].remove(handler) def hashook(self, word): - return word in self.msghandlers + return word in self.msghandlers and len(self.msghandlers[word]) != 0 def gethook(self, word): return self.msghandlers[word] + def hooknum(self, word, handler): + try: + self.numhandlers[word].append(handler) + except: + self.numhandlers[word] = [handler] + def unhooknum(self, word, handler): + if word in self.numhandlers and handler in self.numhandlers[word]: + self.numhandlers[word].remove(handler) + def hasnumhook(self, word): + return word in self.numhandlers and len(self.numhandlers[word]) != 0 + 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): + 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) + def setup(): global cfg, main @@ -164,15 +240,16 @@ def setup(): 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) - main.db = MySQLdb.connect(host=cfg.dbhost, user=cfg.dbuser, passwd=cfg.dbpass, db=cfg.dbname, cursorclass=MySQLdb.cursors.DictCursor) + 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():