]> jfr.im git - erebus.git/commitdiff
Modules!
authorzonidjan <redacted>
Wed, 27 Nov 2013 07:14:52 +0000 (01:14 -0600)
committerzonidjan <redacted>
Wed, 27 Nov 2013 07:14:52 +0000 (01:14 -0600)
bot.py
config.py
ctlmod.py [new file with mode: 0644]
erebus.py
modlib.py
modules/eval.py [new file with mode: 0644]
modules/modtest.py

diff --git a/bot.py b/bot.py
index 50456b5d1cebaf9a6250d06b8f00c1bf690f3c04..513f25722284543c144f4a80c13c76085c3b0fad 100644 (file)
--- a/bot.py
+++ b/bot.py
@@ -56,24 +56,14 @@ class Bot(object):
        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))
index af470cb4a63f9f85e8fbf9ffb543d48f8120f8f8..d274509e39b1e9f5d0f68360f997e119e05ddb33 100644 (file)
--- a/config.py
+++ b/config.py
@@ -1,7 +1,6 @@
 import ConfigParser
 
 class Config(object):
-#      config = ConfigParser.SafeConfigParser()
        def __init__(self, filename, writeout=True):
                self.__dict__['config'] = ConfigParser.SafeConfigParser()
                self.__dict__['filename'] = filename
@@ -14,8 +13,8 @@ class Config(object):
        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:
diff --git a/ctlmod.py b/ctlmod.py
new file mode 100644 (file)
index 0000000..86943d1
--- /dev/null
+++ b/ctlmod.py
@@ -0,0 +1,45 @@
+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')
index b38820b6fe5cfe2d0020d6dd0995c856c2270173..421d3012becec4d0e6e4f809fc89691bd32c689f 100644 (file)
--- a/erebus.py
+++ b/erebus.py
@@ -3,7 +3,7 @@
 #TODO: tons
 
 import os, sys, select, MySQLdb, MySQLdb.cursors
-import bot, config
+import bot, config, ctlmod
 
 class Erebus(object):
        bots = {}
@@ -108,15 +108,26 @@ class Erebus(object):
        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")
index eb9923f0103ba277e1d9f1d292a5ae4814f84ceb..72c9fa2be988745e545a21c3c2051bab5bcd20d2 100644 (file)
--- a/modlib.py
+++ b/modlib.py
@@ -1,14 +1,17 @@
 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):
diff --git a/modules/eval.py b/modules/eval.py
new file mode 100644 (file)
index 0000000..04f08fa
--- /dev/null
@@ -0,0 +1,22 @@
+# 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.")
index ea2f4d9db28f3019cb0386b8111eb5935ab5440f..928a834ec5713c30fd48f63ed86a7c4e80a0f4ed 100644 (file)
@@ -2,6 +2,7 @@
 import modlib
 lib = modlib.modlib(__name__)
 modstart = lib.modstart
+modstop = lib.modstop
 
 #module code
 @lib.hook('test')