]> jfr.im git - erebus.git/blob - modules/help.py
added softdeps to modinfo - bumped APIVERSION
[erebus.git] / modules / help.py
1 # Erebus IRC bot - Author: Erebus Team
2 # help module
3 # This file is released into the public domain; see http://unlicense.org/
4
5 # module info
6 modinfo = {
7 'author': 'Erebus Team',
8 'license': 'public domain',
9 'compatible': [1,2],
10 'depends': [],
11 'softdeps': [],
12 }
13
14 # preamble
15 import modlib
16 lib = modlib.modlib(__name__)
17 modstart = lib.modstart
18 modstop = lib.modstop
19
20 # module code
21 helps = {}
22 cmds = {}
23
24 # ! this is part of this module's API, called from modlib.help()
25 # this function only handles the command name and aliases - the rest is passed directly to _reghelp()
26 def reghelp(func, *args, **kwargs):
27 syntax = None
28 shorthelp = None
29 longhelps = []
30
31 if len(args) > 0:
32 syntax = args[0]
33 if len(args) > 1:
34 shorthelp = args[1]
35 if len(args) > 2:
36 longhelps = args[2:]
37
38 if 'syntax' in kwargs:
39 syntax = kwargs['syntax']
40 if 'shorthelp' in kwargs:
41 shorthelp = kwargs['shorthelp']
42 if 'longhelps' in kwargs:
43 longhelps = kwargs['longhelps']
44
45 if syntax is None: syntax = ""
46 if shorthelp is None: shorthelp = ""
47
48 func.syntax = syntax
49 func.shorthelp = shorthelp
50 func.longhelps = longhelps
51 helps[func] = func
52 for c in func.cmd:
53 cmds[c] = func
54
55 def dereghelp(func, *args, **kwargs):
56 for c in func.cmd:
57 del cmds[cmd]
58 del helps[func]
59
60 class HelpLine(object):
61 def __init__(self, cmd, syntax, shorthelp, admin, level, module):
62 self.cmd = cmd
63 self.syntax = syntax
64 self.shorthelp = shorthelp
65 self.admin = admin
66 self.level = level
67 self.module = module
68
69 def __cmp__(self, other):
70 if self.level == other.level:
71 return cmp(self.cmd, other.cmd)
72 else:
73 return cmp(self.level, other.level)
74
75
76 def __str__(self):
77 if self.admin:
78 return "%-35s(%3s) - %-10s - %-50s" % (self.cmd+' '+self.syntax, self.level, self.module, self.shorthelp)
79 else:
80 return "%-40s - %-50s" % (self.cmd+' '+self.syntax, self.shorthelp)
81
82 @lib.hook(needchan=False)
83 @lib.help('[<command>]', 'lists commands or describes a command')
84 def help(bot, user, chan, realtarget, *args):
85 if len(args) == 0: # list commands
86 lines = []
87 for func in helps.itervalues():
88 if user.glevel >= func.reqglevel:
89 lines.append(HelpLine(func.cmd[0], func.syntax, func.shorthelp, (user.glevel > 0), func.reqglevel, func.__module__))
90 if len(func.cmd) > 1:
91 for c in func.cmd[1:]:
92 lines.append(HelpLine(c, "", "Alias of %s" % (func.cmd[0]), (user.glevel > 0), func.reqglevel, func.__module__))
93 for line in sorted(lines):
94 bot.slowmsg(user, str(line))
95 bot.slowmsg(user, "End of command listing.")
96 else: # help for a specific command/topic
97 cmd = str(' '.join(args))
98 if cmd in cmds and user.glevel >= cmds[cmd].reqglevel:
99 func = cmds[cmd]
100 bot.slowmsg(user, str(HelpLine(func.cmd[0], func.syntax, func.shorthelp, (user.glevel > 0), func.reqglevel, func.__module__)))
101 for line in func.longhelps:
102 bot.slowmsg(user, " %s" % (line))
103 bot.slowmsg(user, "End of help for %s." % (func.cmd[0]))
104
105 if len(func.cmd) > 1:
106 bot.slowmsg(user, " Aliases: %s" % (' '.join(func.cmd[1:])))
107 else:
108 bot.slowmsg(user, "No help found for %s" % (cmd))