X-Git-Url: https://jfr.im/git/erebus.git/blobdiff_plain/4fa1118bf7ba25a74d1443c562b98c11675d2c7c..dcb94ddca201b098020742aa70b79d14d183b854:/erebus.py diff --git a/erebus.py b/erebus.py index d96ae0b..b237347 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,9 @@ class Erebus(object): self.chans = [] + def msg(self, *args, **kwargs): + main.randbot.msg(self, *args, **kwargs) + def isauthed(self): return self.auth is not None @@ -52,6 +56,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 +83,12 @@ class Erebus(object): row = c.fetchone() + def msg(self, *args, **kwargs): + self.bot.msg(self.name, *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 +126,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 +136,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 +153,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 +189,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: @@ -201,6 +219,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): @@ -223,20 +253,26 @@ def setup(): global cfg, main cfg = config.Config('bot.config') - main = Erebus(cfg.trigger) + 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) + print "Loading %s ... " % (mod), + modstatus = ctlmod.load(main, mod) + if not modstatus: + print str(modstatus) + elif modstatus == True: + print "OK" + else: + print modstatus 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():