]> jfr.im git - erebus.git/commitdiff
Added !MODLOAD, !MODUNLOAD, !MODRELOAD
authorJohn Runyon <redacted>
Thu, 23 Jan 2014 20:47:38 +0000 (14:47 -0600)
committerJohn Runyon <redacted>
Thu, 23 Jan 2014 20:47:38 +0000 (14:47 -0600)
bot.py
ctlmod.py
erebus.py
modlib.py
modules/eval.py
modules/foo.py [moved from modules/modtest.py with 97% similarity]
modules/module.py [new file with mode: 0644]

diff --git a/bot.py b/bot.py
index 603b966dd1356c4f9a6c5937dc56a60f4ca8bddc..0f2c7e6e782541d35ed09c3438cb30fbb084c4fd 100644 (file)
--- a/bot.py
+++ b/bot.py
@@ -111,7 +111,9 @@ class Bot(object):
                                return
                        if user.glevel >= callback.reqglevel:
                                #TODO TODO TODO check reqclevel
-                               callback(self, user, chan, target, *pieces[1:])
+                               cbret = callback(self, user, chan, target, *pieces[1:])
+                               if cbret is NotImplemented:
+                                       self.msg(user, "Command not implemented.")
                                return
 
                self.msg(user, "No such command, or you don't have access.")
index 24dd236cfb1b3fccdca99e4e2a01efbd58398637..b6fe1a969c085a417821a0546c66dcc1e10a1836 100644 (file)
--- a/ctlmod.py
+++ b/ctlmod.py
@@ -8,7 +8,7 @@ modules = {}
 dependents = {}
 
 def isloaded(modname): return modname in modules
-def modhas(modname, attname): return getattr(self.modules[modname], attname, None) is not None
+def modhas(modname, attname): return getattr(modules[modname], attname, None) is not None
 
 def load(parent, modname):
        if not isloaded(modname):
@@ -45,19 +45,19 @@ def unload(parent, modname):
                        unload(parent, dependent)
                for dep in dependents[modname]:
                        dependents[dep].remove(modname)
-               self.modules[modname].modstop(parent)
+               return modules[modname].modstop(parent)
        else:
                return modlib.error('already unloaded')
 
 def reloadmod(parent, modname):
        if isloaded(modname):
-               if modhas(modname, 'modrestart'): self.modules[modname].modrestart(parent)
-               else: self.modules[modname].modstop(parent)
+               if modhas(modname, 'modrestart'): modules[modname].modrestart(parent)
+               else: modules[modname].modstop(parent)
 
-               reload(self.modules[modname])
+               reload(modules[modname])
 
-               if modhas(modname, 'modrestarted'): self.modules[modname].modrestarted(parent)
-               else: self.modules[modname].modstart(parent)
+               if modhas(modname, 'modrestarted'): modules[modname].modrestarted(parent)
+               else: modules[modname].modstart(parent)
 
        else:
                load(parent, modname)
index 7c7dc920e8fad4b2ddc9094e5bce273de4d261cb..c91e24cc49b0f1603eddaff8c1467b23a2d5d9e5 100644 (file)
--- a/erebus.py
+++ b/erebus.py
@@ -164,6 +164,7 @@ def setup():
 
        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)
 
        main.db = MySQLdb.connect(host=cfg.dbhost, user=cfg.dbuser, passwd=cfg.dbpass, db=cfg.dbname, cursorclass=MySQLdb.cursors.DictCursor)
index 743e36dbf334482b1560cb18b397140aa722d42a..717406a6109b46149d2ae8f61a54704650fb5307 100644 (file)
--- a/modlib.py
+++ b/modlib.py
@@ -15,8 +15,8 @@ class error(object):
 class modlib(object):
        # default (global) access levels
        MANAGER = 100
-       ADMIN = 90
-       STAFF = 80
+       ADMIN = 75
+       STAFF = 50
        AUTHED = 0
        ANYONE = -1
 
@@ -28,6 +28,9 @@ class modlib(object):
        KNOWN = -3
        PUBLIC = -2 #anyone (use glevel to control auth-needed)
 
+       # messages
+       WRONGARGS = "Wrong number of arguments."
+
        def __init__(self, name):
                self.hooks = {}
                self.parent = None
@@ -38,9 +41,11 @@ class modlib(object):
                self.parent = parent
                for cmd, func in self.hooks.iteritems():
                        self.parent.hook(cmd, func)
+               return True
        def modstop(self, parent):
                for cmd, func in self.hooks.iteritems():
-                       self.parent.unhook(cmd, func)
+                       self.parent.unhook(cmd)
+               return True
 
        def hook(self, cmd, needchan=True, glevel=ANYONE, clevel=PUBLIC):
                def realhook(func):
@@ -53,3 +58,23 @@ class modlib(object):
                                self.parent.hook(cmd, func)
                        return func
                return realhook
+
+       def argsEQ(self, num):
+               def realhook(func):
+                       def checkargs(bot, user, chan, realtarget, *args):
+                               if len(args) == num:
+                                       return func(bot, user, chan, realtarget, *args)
+                               else:
+                                       bot.msg(user, self.WRONGARGS)
+                       return checkargs
+               return realhook
+
+       def argsGE(self, num):
+               def realhook(func):
+                       def checkargs(bot, user, chan, realtarget, *args):
+                               if len(args) >= num:
+                                       return func(bot, user, chan, realtarget, *args)
+                               else:
+                                       bot.msg(user, self.WRONGARGS)
+                       return checkargs
+               return realhook
index 5ac60fd9814a534c8e077130ef5f43fda20af894..159635fb3f99eb248c4f43eff1330af424e455ed 100644 (file)
@@ -20,6 +20,7 @@ import sys
 
 
 @lib.hook('eval', needchan=False, glevel=lib.MANAGER)
+@lib.argsGE(1)
 def cmd_eval(bot, user, chan, realtarget, *args):
        if chan is not None: replyto = chan
        else: replyto = user
@@ -30,6 +31,7 @@ def cmd_eval(bot, user, chan, realtarget, *args):
 
 
 @lib.hook('exec', needchan=False, glevel=lib.MANAGER)
+@lib.argsGE(1)
 def cmd_exec(bot, user, chan, realtarget, *args):
        if chan is not None: replyto = chan
        else: replyto = user
similarity index 97%
rename from modules/modtest.py
rename to modules/foo.py
index f2f96d238f735b3607ef7fef508f640493ad2a97..f5a69fe0760a4ddc612daa3212d6c84e51252893 100644 (file)
@@ -17,6 +17,8 @@ modstart = lib.modstart
 modstop = lib.modstop
 
 # module code
+import ctlmod
+
 @lib.hook('test')
 def cmd_test(bot, user, chan, realtarget, *args):
        bot.msg(chan, "You said: !test %s" % (' '.join([str(arg) for arg in args])))
diff --git a/modules/module.py b/modules/module.py
new file mode 100644 (file)
index 0000000..3d9b3e1
--- /dev/null
@@ -0,0 +1,49 @@
+# Erebus IRC bot - Author: John Runyon
+# simple module example
+# This file is released into the public domain; see http://unlicense.org/
+
+# module info
+modinfo = {
+       'author': 'John Runyon (DimeCadmium)',
+       'license': 'public domain',
+       'compatible': [1], # compatible module API versions
+       'depends': [], # other modules required to work properly?
+}
+
+# preamble
+import modlib
+lib = modlib.modlib(__name__)
+modstart = lib.modstart
+modstop = lib.modstop
+
+# module code
+import ctlmod
+
+@lib.hook('modload', needchan=False, glevel=lib.MANAGER)
+@lib.argsEQ(1)
+def cmd_modload(bot, user, chan, realtarget, *args):
+       okay = ctlmod.load(bot.parent, args[0])
+       if okay:
+               bot.msg(user, "Loaded %s" % (args[0]))
+       else:
+               bot.msg(user, "Error loading %s: %r" % (args[0], okay))
+
+@lib.hook('modunload', needchan=False, glevel=lib.MANAGER)
+@lib.argsEQ(1)
+def cmd_modunload(bot, user, chan, realtarget, *args):
+       okay = ctlmod.unload(bot.parent, args[0])
+       if okay:
+               bot.msg(user, "Unloaded %s" % (args[0]))
+       else:
+               bot.msg(user, "Error unloading %s: %r" % (args[0], okay))
+
+@lib.hook('modreload', needchan=False, glevel=lib.MANAGER)
+@lib.argsEQ(1)
+def cmd_modreload(bot, user, chan, realtarget, *args):
+       okay = ctlmod.reloadmod(bot.parent, args[0])
+       bot.msg(user, "Reloaded %s" % (args[0]))
+
+@lib.hook('modlist', needchan=False, glevel=lib.STAFF)
+@lib.argsEQ(0)
+def cmd_modlist(bot, user, chan, realtarget, *args):
+       return NotImplemented