def parsemsg(self, user, chan, msg):
if msg[0] == '!': #TODO check config for trigger
msg = msg[1:]
-
else:
return
pieces = msg.split()
- cmd = pieces[0].upper()
-
- if cmd == "EVAL":
- try: ret = eval(' '.join(pieces[1:]))
- except: self.msg(chan, "Error: %s %s" % (sys.exc_info()[0], sys.exc_info()[1]))
- else: self.msg(chan, "Done: %r" % (ret))
-
- elif cmd == "EXEC":
- try: exec ' '.join(pieces[1:])
- except: self.msg(chan, "Error: %s %s" % (sys.exc_info()[0], sys.exc_info()[1]))
- else: self.msg(chan, "Done.")
+ cmd = pieces[0].lower()
- #TODO
+ if self.parent.hashook(cmd):
+ self.parent.gethook(cmd)(self, user, chan, *pieces[1:])
def msg(self, target, msg):
if isinstance(target, self.parent.User): self.conn.send("NOTICE %s :%s" % (target.nick, msg))
import ConfigParser
class Config(object):
-# config = ConfigParser.SafeConfigParser()
def __init__(self, filename, writeout=True):
self.__dict__['config'] = ConfigParser.SafeConfigParser()
self.__dict__['filename'] = filename
def __setattr__(self, key, value):
self.config.set('erebus', key, value)
- def items(self):
- return self.config.items('erebus')
+ def items(self, section='erebus'):
+ return self.config.items(section)
def write(self):
with open(self.filename, 'wb') as configfile:
--- /dev/null
+import sys
+
+modules = {}
+
+def isloaded(modname): return modname in modules
+def modhas(modname, attname): return getattr(self.modules[modname], attname, None) is not None
+
+def load(parent, modname):
+ if not isloaded(modname):
+ mod = __import__(modname)
+ modules[modname] = mod
+ ret = mod.modstart(parent)
+ if not ret:
+ del modules[modname]
+ return ret
+ else:
+ return -1
+
+def unload(parent, modname):
+ if isloaded(modname):
+ self.modules[modname].modstop(parent)
+ else:
+ return -1
+
+def reloadmod(parent, modname):
+ if isloaded(modname):
+ if modhas(modname, 'modrestart'): self.modules[modname].modrestart(parent)
+ else: self.modules[modname].modstop(parent)
+
+ reload(self.modules[modname])
+
+ if modhas(modname, 'modrestarted'): self.modules[modname].modrestarted(parent)
+ else: self.modules[modname].modstart(parent)
+
+ else:
+ load(parent, modname)
+
+def loadall(parent, modlist):
+ for m in modlist: load(parent, m)
+def unloadall(parent, modlist):
+ for m in modlist: unload(parent, m)
+def reloadall(parent, modlist):
+ for m in modlist: reloadmod(parent, m)
+
+sys.path.append('modules')
#TODO: tons
import os, sys, select, MySQLdb, MySQLdb.cursors
-import bot, config
+import bot, config, ctlmod
class Erebus(object):
bots = {}
def reloadmod(self, name): pass
#bind functions
- def bind(self, word, handler): pass
- def addbind(self, word, handler): pass
- def rmbind(self, word, handler): pass
- def getbind(self, word, handler): pass
-
-cfg = config.Config('bot.config')
-main = Erebus()
+ def hook(self, word, handler):
+ print "hooked %r to %r" % (word, handler)
+ self.msghandlers[word] = handler
+ def unhook(self, word):
+ del self.msghandlers[word]
+ def hashook(self, word):
+ return word in self.msghandlers
+ def gethook(self, word):
+ return self.msghandlers[word]
def setup():
+ global cfg, main
+
+ cfg = config.Config('bot.config')
+ main = Erebus()
+
+ 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)
c = main.db.cursor()
c.execute("SELECT nick, user, bind FROM bots WHERE active = 1")
class modlib(object):
- hooks = {}
- parent = None
-
def __init__(self, name):
+ self.hooks = {}
+ self.parent = None
+
self.name = name
def modstart(self, parent):
self.parent = parent
for cmd, func in self.hooks.iteritems():
self.parent.hook(cmd, func)
+ def modstop(self, parent):
+ for cmd, func in self.hooks.iteritems():
+ self.parent.unhook(cmd, func)
def hook(self, cmd):
def realhook(func):
--- /dev/null
+# preamble
+import modlib
+lib = modlib.modlib(__name__)
+modstart = lib.modstart
+modstop = lib.modstop
+
+#module code
+import sys
+
+
+@lib.hook('eval')
+def cmd_eval(bot, user, chan, *args):
+ try: ret = eval(' '.join(args))
+ except: bot.msg(chan, "Error (%s): %s" % (sys.exc_info()[0], sys.exc_info()[1]))
+ else: bot.msg(chan, "Done: %r" % (ret))
+
+
+@lib.hook('exec')
+def cmd_exec(bot, user, chan, *args):
+ try: exec ' '.join(args)
+ except: bot.msg(chan, "Error: %s %s" % (sys.exc_info()[0], sys.exc_info()[1]))
+ else: bot.msg(chan, "Done.")
import modlib
lib = modlib.modlib(__name__)
modstart = lib.modstart
+modstop = lib.modstop
#module code
@lib.hook('test')