X-Git-Url: https://jfr.im/git/erebus.git/blobdiff_plain/87b8b5ef780c8a2d831050585a06137e3e7313c7..b367d0c5869793453f81614d320a65b169926794:/erebus.py diff --git a/erebus.py b/erebus.py index 4e1934b..90e1389 100644 --- a/erebus.py +++ b/erebus.py @@ -3,16 +3,18 @@ # Erebus IRC bot - Author: John Runyon # main startup code -#TODO: tons - -import os, sys, select, MySQLdb, MySQLdb.cursors +import os, sys, select, MySQLdb, MySQLdb.cursors, time, random, gc import bot, config, ctlmod -class Erebus(object): +class Erebus(object): #singleton to pass around + APIVERSION = 2 + RELEASE = 0 + bots = {} fds = {} - mods = {} + numhandlers = {} msghandlers = {} + chanhandlers = {} users = {} chans = {} @@ -24,12 +26,19 @@ 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 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,32 +46,75 @@ 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 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) + 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 "" % (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 msg(self, *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: + return 0 + 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) @@ -84,8 +136,9 @@ class Erebus(object): def __str__(self): return self.name def __repr__(self): return "" % (self.name) - def __init__(self, trigger): - self.trigger = trigger + def __init__(self, cfg): + self.cfg = cfg + self.trigger = cfg.trigger if os.name == "posix": self.potype = "poll" self.po = select.poll() @@ -93,9 +146,9 @@ class Erebus(object): self.potype = "select" self.fdlist = [] - def newbot(self, nick, user, bind, server, port, realname): + def newbot(self, nick, user, bind, authname, authpass, server, port, realname): if bind is None: bind = '' - obj = bot.Bot(self, nick, user, bind, server, port, realname) + obj = bot.Bot(self, nick, user, bind, authname, authpass, server, port, realname) self.bots[nick.lower()] = obj def newfd(self, obj, fileno): @@ -109,23 +162,31 @@ class Erebus(object): return self.bots[name.lower()] 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())] - def user(self, nick): - nick = nick.lower() + def user(self, _nick, justjoined=False, create=True): + nick = _nick.lower() if nick in self.users: return self.users[nick] - else: - user = self.User(nick) + elif create: + user = self.User(_nick) self.users[nick] = user + + if justjoined: + 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()] 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 @@ -140,33 +201,106 @@ class Erebus(object): 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.itervalues() if u.auth == auth.lower()] + #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): + if 'norecurse' in kwargs: + norecurse = kwargs['norecurse'] + del kwargs['norecurse'] + else: + norecurse = False + main.log("[SQL]", "?", "MyCursor.execute(self, %s, %s)" % (', '.join([repr(i) for i in args]), ', '.join([str(key)+"="+repr(kwargs[key]) for key in kwargs]))) +# print "%09.3f [SQL] [#] MyCursor.execute(self, %s, %s)" % (time.time() % 100000, ', '.join([repr(i) for i in args]), ', '.join([str(key)+"="+repr(kwargs[key]) for key in kwargs])) + try: + return super(self.__class__, self).execute(*args, **kwargs) + except MySQLdb.MySQLError as e: + main.log("[SQL]", "!", "MySQL error! %r" % (e)) +# print "%09.3f [SQL] [!] MySQL error! %r" % (time.time() % 100000, e) + if not norecurse: + dbsetup() + return self.execute(norecurse=True, *args, **kwargs) + 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 - cfg = config.Config('bot.config') - main = Erebus(cfg.trigger) + cfg = config.setup('bot.config') + + if int(cfg.get('debug', 'gc', default=0)) == 1: + gc.set_debug(gc.DEBUG_LEAK) + + pidfile = open(cfg.pidfile, 'w') + pidfile.write(str(os.getpid())) + pidfile.close() + + main = Erebus(cfg) autoloads = [mod for mod, yes in cfg.items('autoloads') if int(yes) == 1] for mod in autoloads: 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, authname, authpass FROM bots WHERE active = 1"): + rows = c.fetchall() + c.close() + for row in rows: + main.newbot(row['nick'], row['user'], row['bind'], row['authname'], row['authpass'], cfg.host, cfg.port, cfg.realname) main.connectall() def loop(): @@ -176,5 +310,9 @@ def loop(): main.fd(fileno).parse(line) 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()