]> jfr.im git - erebus.git/blobdiff - modules/control.py
add exit alias for !die
[erebus.git] / modules / control.py
index c03b8f3445de66811eff3b9a69a9329ec05bef75..693770dd47821a2c9b34dce8d4fd51b370ef6c7e 100644 (file)
@@ -1,4 +1,5 @@
 # Erebus IRC bot - Author: Erebus Team
+# vim: fileencoding=utf-8
 # Various highly recommended "control" commands.
 # This file is released into the public domain; see http://unlicense.org/
 
@@ -6,8 +7,9 @@
 modinfo = {
        'author': 'Erebus Team',
        'license': 'public domain',
-       'compatible': [1],
+       'compatible': [0],
        'depends': [],
+       'softdeps': ['help'],
 }
 
 # preamble
@@ -17,40 +19,90 @@ modstart = lib.modstart
 modstop = lib.modstop
 
 # module code
-import sys
+import sys, os
 import ctlmod
+from collections import deque
 
 
-@lib.hook(needchan=False, glevel=lib.MANAGER)
+@lib.hook(('die','restart','exit'), needchan=False, glevel=lib.MANAGER)
+@lib.help(None, "stops the bot")
 def die(bot, user, chan, realtarget, *args):
-       for botitem in bot.parent.bots.itervalues():
-               for chan in botitem.chans:
-                       chan.fastmsg("Bot is restarting! %s" % ' '.join(args))
-               bot.conn.send("QUIT :Restarting.")
+       quitmsg = ' '.join(args)
+       for botitem in bot.parent.bots.values():
+               bot.conn.send("QUIT :Restarting. %s" % (quitmsg))
        sys.exit(0)
-       os._exit(0)
 
 @lib.hook(needchan=False, glevel=lib.MANAGER)
-@lib.argsEQ(1)
+@lib.help("[-autoload] <mod>", "loads a module, optionally marking it for autoloading")
+@lib.argsGE(1)
 def modload(bot, user, chan, realtarget, *args):
-       okay = ctlmod.load(bot.parent, args[0])
+       module = None
+       autoload = False
+       for arg in args:
+               if arg[0] != "-":
+                       if module is not None:
+                               bot.msg(user, "Wrong number of arguments.")
+                               return
+                       module = arg
+               elif arg == "-autoload":
+                       autoload = True
+               else:
+                       bot.msg(user, "Bad option %s" % (arg))
+
+       if autoload:
+               bot.parent.cfg.set('autoloads', module, 1)
+               bot.msg(user, "Marked %s for autoloading." % (module))
+       okay = ctlmod.load(bot.parent, module)
        if okay:
-               bot.msg(user, "Loaded %s" % (args[0]))
+               bot.msg(user, "Loaded %s" % (module))
        else:
-               bot.msg(user, "Error loading %s: %r" % (args[0], okay))
+               bot.msg(user, "Error loading %s: %r" % (module, okay))
 
 @lib.hook(needchan=False, glevel=lib.MANAGER)
-@lib.argsEQ(1)
+@lib.help("[-force] [-autoload] <mod>", "unloads a module", "will refuse to unload a module which is depended on by others unless you specify FORCE.", "optionally removes from autoloads")
+@lib.argsGE(1)
 def modunload(bot, user, chan, realtarget, *args):
-       okay = ctlmod.unload(bot.parent, args[0])
+       module = None
+       autoload = False
+       force = False
+       for arg in args:
+               if arg[0] != "-":
+                       if module is not None:
+                               bot.msg(user, "Incorrect syntax - can only name one module")
+                               return
+                       module = arg
+               elif arg.lower() == "-autoload":
+                       autoload = True
+               elif arg.lower() == "-force":
+                       force = True
+               else:
+                       bot.msg(user, "Bad option %s" % (arg))
+
+       if autoload:
+               bot.parent.cfg.delete('autoloads', module)
+               bot.msg(user, "Unmarked %s for autoloading." % (module))
+       if not ctlmod.isloaded(module):
+               bot.msg(user, "%s is not loaded" % (module))
+               return
+       if len(ctlmod.dependents[module]) > 0:
+               if not force:
+                       bot.msg(user, "That module has dependents! Say MODUNLOAD -force %s to unload it and any dependents." % (module))
+                       return
+
+       okay = ctlmod.unload(bot.parent, module)
        if okay:
-               bot.msg(user, "Unloaded %s" % (args[0]))
+               bot.msg(user, "Unloaded %s" % (module))
        else:
-               bot.msg(user, "Error unloading %s: %r" % (args[0], okay))
+               bot.msg(user, "Error unloading %s: %r" % (module, okay))
 
 @lib.hook(needchan=False, glevel=lib.MANAGER)
+@lib.help("<mod>", "reloads a module")
 @lib.argsEQ(1)
 def modreload(bot, user, chan, realtarget, *args):
+       if not ctlmod.isloaded(args[0]):
+               bot.msg(user, "%s is not loaded" % (args[0]))
+               return
+
        okay = ctlmod.reloadmod(bot.parent, args[0])
        if okay:
                bot.msg(user, "Reloaded %s" % (args[0]))
@@ -58,31 +110,83 @@ def modreload(bot, user, chan, realtarget, *args):
                bot.msg(user, "Error occurred: %r" % (okay))
 
 @lib.hook(needchan=False, glevel=lib.STAFF)
+@lib.help(None, "list loaded modules")
 @lib.argsEQ(0)
 def modlist(bot, user, chan, realtarget, *args):
        mods = ctlmod.modules
-       for mod in mods.itervalues():
-               bot.msg(user, "- %s %r" % (mod.__name__, mod))
+       for modname, mod in sorted(mods.items()):
+               bot.msg(user, "- %s (%s) [%s]" % ((modname, mod.__file__, ', '.join(ctlmod.dependents[modname]))))
        bot.msg(user, "Done.")
 
-@lib.hook(cmd='whoami', needchan=False)
-def whoami(bot, user, chan, realtarget, *args):
+def _whois(user, chan, showglevel=True, showclevel=True):
        if not user.isauthed():
-               bot.msg(user, "You are not authed.")
-               return
+               return "not authed."
 
        fillers = {'auth': user.auth}
-       fmt = "You are %(auth)s"
+       fmt = "%(auth)s"
 
-       if user.glevel >= 1:
+       if showglevel and user.glevel >= 1:
                fillers['glevel'] = user.glevel
                fmt += " (global access: %(glevel)s)"
+       elif user.glevel >= 1:
+               fmt += " (staff)"
+       elif user.glevel <= -2:
+               fmt += " (ignored)"
        else:
                fmt += " (not staff)"
 
-       if chan is not None and chan.levelof(user.auth) >= 1:
-               fillers['clevel'] = chan.levelof(user.auth)
-               fmt += " (channel access: %(clevel)s)"
+       if (showclevel or showglevel) and chan is not None:
+               clev = chan.levelof(user.auth)
+               if clev >= 1:
+                       fillers['clevel'] = (lib.clevs[clev] if clev < len(lib.clevs) else clev)
+                       fmt += " (channel access: %(clevel)s)"
+               else:
+                       fmt += " (not a channel user)"
+       return fmt % fillers
+
+@lib.hook(needchan=False, wantchan=True)
+@lib.help("<user|#auth>", "shows who someone is")
+@lib.argsEQ(1)
+def whois(bot, user, chan, realtarget, *args):
+       name = args[0]
+       if name.startswith("#"):
+               target = bot.parent.User(name, name[1:])
+       else:
+               target = bot.parent.user(name, create=False)
+       if target is None:
+               return  "I don't know %s." % (args[0])
+       else:
+               return "%s is %s" % (args[0], _whois(target, chan, (user.glevel >= 1), (chan is not None and chan.levelof(user.auth) >= 1)))
+
+@lib.hook(needchan=False, wantchan=True)
+@lib.help(None, "shows who you are")
+def whoami(bot, user, chan, realtarget, *args):
+       return "You are %s" % (_whois(user, chan))
+
+@lib.hook(needchan=False)
+@lib.help(None, "tries to read your auth and access level again")
+def auth(bot, user, chan, realtarget, *args):
+       bot.msg(user, "Okay, give me a second.")
+       bot.conn.send("WHO %s n%%ant,2" % (user))
+
+@lib.hook(needchan=False, glevel=1)
+@lib.help(None, "displays length of each msgqueue")
+def qstat(bot, user, chan, realtarget, *args):
+       bot.fastmsg(user, "Regular: %d -- Slow: %d" % (len(bot.msgqueue), len(bot.slowmsgqueue)))
+
+@lib.hook(('qclear','cq','clearq','clearqueue'), needchan=False, glevel=lib.ADMIN)
+@lib.help("[regular|slow]", "clears both or a specific msgqueue")
+def qclear(bot, user, chan, realtarget, *args):
+       if len(args) == 0:
+               bot.msgqueue = deque()
+               bot.slowmsgqueue = deque()
+               bot.fastmsg(user, "Cleared both msgqueues.")
        else:
-               fmt += " (not a channel user)"
-       bot.msg(user, fmt % fillers)
+               if args[0] == 'regular':
+                       bot.msgqueue = deque()
+               elif args[0] == 'slow':
+                       bot.slowmsgqueue = deque()
+               else:
+                       bot.fastmsg(user, "Syntax: QCLEAR [regular|slow]")
+                       return #short-circuit
+               bot.fastmsg(user, "Cleared that msgqueue.")