X-Git-Url: https://jfr.im/git/erebus.git/blobdiff_plain/2a1a69a65b1d4483a90d5acc4ebc14a7589cd814..0bf186e88f4f1753b7bc8096eb99fb9ac58fd858:/erebus.py diff --git a/erebus.py b/erebus.py index 3091760..a4d6ca3 100644 --- a/erebus.py +++ b/erebus.py @@ -3,17 +3,18 @@ # Erebus IRC bot - Author: John Runyon # main startup code -#TODO: tons - -import os, sys, select, MySQLdb, MySQLdb.cursors, time +import os, sys, select, MySQLdb, MySQLdb.cursors, time, random import bot, config, ctlmod class Erebus(object): + APIVERSION = 1 + RELEASE = 0 + bots = {} fds = {} - mods = {} numhandlers = {} msghandlers = {} + chanhandlers = {} users = {} chans = {} @@ -25,6 +26,11 @@ class Erebus(object): self.chans = [] + def msg(self, *args, **kwargs): + main.randbot().msg(self, *args, **kwargs) + def fastmsg(self, *args, **kwargs): + main.randbot().fastmsg(self, *args, **kwargs) + def isauthed(self): return self.auth is not None @@ -52,6 +58,11 @@ class Erebus(object): self.chans.append(chan) def part(self, chan): self.chans.remove(chan) + def quit(self): + for chan in self.chans: + self.chans.remove(chan) + def nickchange(self, newnick): + self.nick = newnick def __str__(self): return self.nick def __repr__(self): return "" % (self.nick,self.glevel) @@ -74,7 +85,14 @@ class Erebus(object): row = c.fetchone() + def msg(self, *args, **kwargs): + self.bot.msg(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] @@ -112,8 +130,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() @@ -121,9 +140,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): @@ -138,18 +157,18 @@ class Erebus(object): def fd(self, fileno): #get Bot() by fd/fileno return self.fds[fileno] def randbot(self): #get Bot() randomly - for b in self.bots.itervalues(): return b #TODO + return self.bots[random.choice(self.bots.keys())] - def user(self, nick, justjoined=False): - nick = nick.lower() + def user(self, _nick, justjoined=False): + nick = _nick.lower() if nick in self.users: return self.users[nick] else: - user = self.User(nick) + user = self.User(_nick) self.users[nick] = user if justjoined: - self.randbot().conn.send("WHO %s %%ant,2" % (nick)) + self.randbot().conn.send("WHO %s n%%ant,2" % (nick)) return user def channel(self, name): #get Channel() by name @@ -174,6 +193,9 @@ class Erebus(object): if bot.conn.state == 0: bot.connect() + def module(self, name): + return ctlmod.modules[name] + #bind functions def hook(self, word, handler): try: @@ -203,9 +225,9 @@ class Erebus(object): def hookchan(self, chan, handler): try: - self.chanhandlers[word].append(handler) + self.chanhandlers[chan].append(handler) except: - self.chanhandlers[word] = [handler] + 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) @@ -217,11 +239,11 @@ class Erebus(object): 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])) + print "%05.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: super(self.__class__, self).execute(*args, **kwargs) except MySQLdb.MySQLError as e: - print "[SQL] [!] MySQL error! %r" % (e) + print "%05.3f [SQL] [!] MySQL error! %r" % (time.time() % 100000, e) dbsetup() return False return True @@ -235,20 +257,24 @@ def setup(): global cfg, main cfg = config.Config('bot.config') - main = Erebus(cfg.trigger) + + 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: - print "Loading %s" % (mod) ctlmod.load(main, mod) dbsetup() c = main.db.cursor() - if c.execute("SELECT nick, user, bind FROM bots WHERE active = 1"): + 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'], cfg.host, cfg.port, cfg.realname) + main.newbot(row['nick'], row['user'], row['bind'], row['authname'], row['authpass'], cfg.host, cfg.port, cfg.realname) main.connectall() def loop(): @@ -258,5 +284,9 @@ def loop(): main.fd(fileno).parse(line) if __name__ == '__main__': + try: os.rename('logfile', 'oldlog.%s' % (time.time())) + except: pass + sys.stdout = open('logfile', 'w') + sys.stderr = sys.stdout setup() while True: loop()