import sys
cfg = Config(sys.argv[1], False)
- for k, v in cfg.items():
- print k, '=', v
+ for s in cfg.config.sections():
+ for k, v in cfg.items(s):
+ print s+'.'+k, '=', v
+# for k, v in cfg.items():
+# print 'erebus.'+k, '=', v
self.hooks = {}
self.numhooks = {}
self.chanhooks = {}
+ self.helps = []
self.parent = None
self.name = name
self.parent.hooknum(num, func)
for chan, func in self.chanhooks.iteritems():
self.parent.hookchan(chan, func)
+
+ for func, args, kwargs in self.helps:
+ try:
+ self.mod('help').reghelp(func, *args, **kwargs)
+ except:
+ pass
return True
def modstop(self, parent):
for cmd, func in self.hooks.iteritems():
self.parent.unhooknum(num, func)
for chan, func in self.chanhooks.iteritems():
self.parent.unhookchan(chan, func)
+
+ for func, args, kwargs in self.helps:
+ try:
+ self.mod('help').dereghelp(func, *args, **kwargs)
+ except:
+ pass
return True
def hooknum(self, num):
cmd = _cmd #...and restore it
if cmd is None:
cmd = func.__name__ # default to function name
+ if isinstance(cmd, basestring):
+ cmd = (cmd,)
func.needchan = needchan
func.reqglevel = glevel
func.reqclevel = clevel
+ func.cmd = cmd
- if isinstance(cmd, basestring):
- cmd = (cmd,)
for c in cmd:
self.hooks[c] = func
if self.parent is not None:
return realhook
def help(self, *args, **kwargs):
- """help(syntax, shorthelp, longhelp, more lines longhelp, cmd=...?)
+ """help(syntax, shorthelp, longhelp?, more lines longhelp?, cmd=...?)
Example:
help("<user> <pass>", "login")
^ Help will only be one line. Command name determined based on function name.
^ Command takes no args. Short description (in overall HELP listing) is "do stuff".
Long description (HELP <command>) will say "<command> - do stuff", newline, "This command is really complicated."
"""
- try:
- self.mod('help').reghelp(*args, **kwargs)
- except:
- pass
+ def realhook(func):
+ if self.parent is not None:
+ try:
+ self.mod('help').reghelp(func, *args, **kwargs)
+ except:
+ pass
+ self.helps.append((func,args,kwargs))
+ return func
+ return realhook
bot.msg(replyto, "You said: %s" % (' '.join([str(arg) for arg in args])))
@lib.hook(('foo', 'bar'), needchan=False) #hooks !foo and !bar as aliases
-@lib.help(None, 'replies with nonsense.', cmd=('foo', 'bar'))
+@lib.help(None, 'replies with nonsense.', "it's a very non-sensical command", "more lines")
def foobar(bot, user, chan, realtarget, *args):
bot.msg(user, "Foo bar baz.")
--- /dev/null
+# Erebus IRC bot - Author: Erebus Team
+# help module
+# This file is released into the public domain; see http://unlicense.org/
+
+# module info
+modinfo = {
+ 'author': 'Erebus Team',
+ '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
+helps = {}
+cmds = {}
+
+# ! this is part of this module's API, called from modlib.help()
+# this function only handles the command name and aliases - the rest is passed directly to _reghelp()
+def reghelp(func, *args, **kwargs):
+ syntax = None
+ shorthelp = None
+ longhelps = []
+
+ if len(args) > 0:
+ syntax = args[0]
+ if len(args) > 1:
+ shorthelp = args[1]
+ if len(args) > 2:
+ longhelps = args[2:]
+
+ if 'syntax' in kwargs:
+ syntax = kwargs['syntax']
+ if 'shorthelp' in kwargs:
+ shorthelp = kwargs['shorthelp']
+ if 'longhelps' in kwargs:
+ longhelps = kwargs['longhelps']
+
+ if syntax is None: syntax = ""
+ if shorthelp is None: shorthelp = ""
+
+ func.syntax = syntax
+ func.shorthelp = shorthelp
+ func.longhelps = longhelps
+ helps[func] = func
+ for c in func.cmd:
+ cmds[c] = func
+
+def dereghelp(func, *args, **kwargs):
+ for c in func.cmd:
+ del cmds[cmd]
+ del helps[func]
+
+@lib.hook(needchan=False)
+@lib.help('[<command>]', 'lists commands or describes a command')
+def help(bot, user, chan, realtarget, *args):
+ if len(args) == 0:
+ for func in helps.itervalues():
+ if user.glevel >= func.reqglevel:
+ if func.reqglevel <= 0:
+ bot.slowmsg(user, "%-40s - %-50s" % (func.cmd[0]+' '+func.syntax, func.shorthelp))
+ else:
+ bot.slowmsg(user, "%-40s - %-50s (%5s)" % (func.cmd[0]+' '+func.syntax, func.shorthelp, func.reqglevel))
+ if len(func.cmd) > 1:
+ for c in func.cmd[1:]:
+ bot.slowmsg(user, "%-40s - Alias of %s" % (c, func.cmd[0]))
+ else:
+ cmd = str(' '.join(args))
+ if cmd in cmds and user.glevel >= cmds[cmd].reqglevel:
+ func = cmds[cmd]
+ if func.reqglevel <= 0:
+ bot.slowmsg(user, "%-40s - %-50s" % (func.cmd[0]+' '+func.syntax, func.shorthelp))
+ else:
+ bot.slowmsg(user, "%-40s - %-50s (%5s)" % (func.cmd[0]+' '+func.syntax, func.shorthelp, func.reqglevel))
+ for line in func.longhelps:
+ bot.slowmsg(user, " %s" % (line))
+
+ if len(func.cmd) > 1:
+ bot.slowmsg(user, " Aliases: %s" % (' '.join(func.cmd[1:])))
+ else:
+ bot.slowmsg(user, "No help found for %s" % (cmd))