]>
Commit | Line | Data |
---|---|---|
0f8352dd | 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], # compatible module API versions | |
10 | 'depends': [], # other modules required to work properly? | |
11 | } | |
12 | ||
13 | # preamble | |
14 | import modlib | |
15 | lib = modlib.modlib(__name__) | |
16 | modstart = lib.modstart | |
17 | modstop = lib.modstop | |
18 | ||
19 | # module code | |
20 | helps = {} | |
21 | cmds = {} | |
22 | ||
23 | # ! this is part of this module's API, called from modlib.help() | |
24 | # this function only handles the command name and aliases - the rest is passed directly to _reghelp() | |
25 | def reghelp(func, *args, **kwargs): | |
26 | syntax = None | |
27 | shorthelp = None | |
28 | longhelps = [] | |
29 | ||
30 | if len(args) > 0: | |
31 | syntax = args[0] | |
32 | if len(args) > 1: | |
33 | shorthelp = args[1] | |
34 | if len(args) > 2: | |
35 | longhelps = args[2:] | |
36 | ||
37 | if 'syntax' in kwargs: | |
38 | syntax = kwargs['syntax'] | |
39 | if 'shorthelp' in kwargs: | |
40 | shorthelp = kwargs['shorthelp'] | |
41 | if 'longhelps' in kwargs: | |
42 | longhelps = kwargs['longhelps'] | |
43 | ||
44 | if syntax is None: syntax = "" | |
45 | if shorthelp is None: shorthelp = "" | |
46 | ||
47 | func.syntax = syntax | |
48 | func.shorthelp = shorthelp | |
49 | func.longhelps = longhelps | |
50 | helps[func] = func | |
51 | for c in func.cmd: | |
52 | cmds[c] = func | |
53 | ||
54 | def dereghelp(func, *args, **kwargs): | |
55 | for c in func.cmd: | |
56 | del cmds[cmd] | |
57 | del helps[func] | |
58 | ||
7d0de55e | 59 | class HelpLine(object): |
60 | def __init__(self, cmd, syntax, shorthelp, level=0): | |
61 | self.cmd = cmd | |
62 | self.syntax = syntax | |
63 | self.shorthelp = shorthelp | |
64 | self.level = level | |
65 | ||
66 | def __cmp__(self, other): | |
67 | if self.level == other.level: | |
68 | return cmp(self.cmd, other.cmd) | |
69 | else: | |
70 | return cmp(self.level, other.level) | |
71 | ||
72 | ||
73 | def __str__(self): | |
74 | if self.level <= 0: | |
75 | return "%-40s - %-50s" % (self.cmd+' '+self.syntax, self.shorthelp) | |
76 | else: | |
77 | return "%-35s(%3s) - %-50s" % (self.cmd+' '+self.syntax, self.level, self.shorthelp) | |
78 | ||
0f8352dd | 79 | @lib.hook(needchan=False) |
80 | @lib.help('[<command>]', 'lists commands or describes a command') | |
5f03d045 | 81 | def help(bot, user, chan, realtarget, *args): #TODO add ordering - by access level, then alphabetic? |
82 | if len(args) == 0: # list commands | |
7d0de55e | 83 | lines = [] |
0f8352dd | 84 | for func in helps.itervalues(): |
85 | if user.glevel >= func.reqglevel: | |
86 | if func.reqglevel <= 0: | |
7d0de55e | 87 | lines.append(HelpLine(func.cmd[0], func.syntax, func.shorthelp)) |
88 | # bot.slowmsg(user, "%-40s - %-50s" % (func.cmd[0]+' '+func.syntax, func.shorthelp)) | |
0f8352dd | 89 | else: |
7d0de55e | 90 | lines.append(HelpLine(func.cmd[0], func.syntax, func.shorthelp, func.reqglevel)) |
91 | # bot.slowmsg(user, "%-40s - %-50s (%5s)" % (func.cmd[0]+' '+func.syntax, func.shorthelp, func.reqglevel)) | |
0f8352dd | 92 | if len(func.cmd) > 1: |
93 | for c in func.cmd[1:]: | |
7d0de55e | 94 | lines.append(HelpLine(c, "", "Alias of %s" % (func.cmd[0]), func.reqglevel)) |
95 | # bot.slowmsg(user, "%-40s - Alias of %s" % (c, func.cmd[0])) | |
96 | for line in sorted(lines): | |
97 | bot.slowmsg(user, str(line)) | |
5f03d045 | 98 | else: # help for a specific command/topic |
0f8352dd | 99 | cmd = str(' '.join(args)) |
100 | if cmd in cmds and user.glevel >= cmds[cmd].reqglevel: | |
101 | func = cmds[cmd] | |
102 | if func.reqglevel <= 0: | |
103 | bot.slowmsg(user, "%-40s - %-50s" % (func.cmd[0]+' '+func.syntax, func.shorthelp)) | |
104 | else: | |
105 | bot.slowmsg(user, "%-40s - %-50s (%5s)" % (func.cmd[0]+' '+func.syntax, func.shorthelp, func.reqglevel)) | |
106 | for line in func.longhelps: | |
107 | bot.slowmsg(user, " %s" % (line)) | |
108 | ||
109 | if len(func.cmd) > 1: | |
110 | bot.slowmsg(user, " Aliases: %s" % (' '.join(func.cmd[1:]))) | |
111 | else: | |
112 | bot.slowmsg(user, "No help found for %s" % (cmd)) |