]>
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', | |
fa93b933 | 9 | 'compatible': [0], |
a62d0d18 | 10 | 'depends': [], |
11 | 'softdeps': [], | |
0f8352dd | 12 | } |
13 | ||
14 | # preamble | |
15 | import modlib | |
16 | lib = modlib.modlib(__name__) | |
3569ead3 | 17 | def modstart(parent, *args, **kwargs): |
18 | if parent.cfg.getboolean('erebus', 'nofakelag'): | |
b367d0c5 | 19 | lib.hook('help', needchan=False)(lib.help('[@<module>|<command>]', 'lists commands or describes a command', 'with @<module>, lists all commands in <module>')(help_nolag)) |
3569ead3 | 20 | else: |
21 | lib.hook(needchan=False)(lib.help("<command>", "describes a command")(help)) | |
22 | return lib.modstart(parent, *args, **kwargs) | |
0f8352dd | 23 | modstop = lib.modstop |
24 | ||
25 | # module code | |
bc68cb5e | 26 | import os.path |
0f8352dd | 27 | helps = {} |
28 | cmds = {} | |
29 | ||
30 | # ! this is part of this module's API, called from modlib.help() | |
0f8352dd | 31 | def reghelp(func, *args, **kwargs): |
32 | syntax = None | |
33 | shorthelp = None | |
34 | longhelps = [] | |
35 | ||
36 | if len(args) > 0: | |
37 | syntax = args[0] | |
38 | if len(args) > 1: | |
39 | shorthelp = args[1] | |
40 | if len(args) > 2: | |
41 | longhelps = args[2:] | |
42 | ||
43 | if 'syntax' in kwargs: | |
44 | syntax = kwargs['syntax'] | |
45 | if 'shorthelp' in kwargs: | |
46 | shorthelp = kwargs['shorthelp'] | |
47 | if 'longhelps' in kwargs: | |
48 | longhelps = kwargs['longhelps'] | |
49 | ||
50 | if syntax is None: syntax = "" | |
51 | if shorthelp is None: shorthelp = "" | |
52 | ||
53 | func.syntax = syntax | |
54 | func.shorthelp = shorthelp | |
55 | func.longhelps = longhelps | |
56 | helps[func] = func | |
57 | for c in func.cmd: | |
58 | cmds[c] = func | |
59 | ||
60 | def dereghelp(func, *args, **kwargs): | |
61 | for c in func.cmd: | |
bc68cb5e | 62 | del cmds[c] |
0f8352dd | 63 | del helps[func] |
64 | ||
7d0de55e | 65 | class HelpLine(object): |
d58c924f | 66 | def __init__(self, cmd, syntax, shorthelp, admin, glevel, module, clevel): |
7d0de55e | 67 | self.cmd = cmd |
68 | self.syntax = syntax | |
69 | self.shorthelp = shorthelp | |
58401d09 | 70 | self.admin = admin |
d58c924f | 71 | self.glevel = glevel |
58401d09 | 72 | self.module = module |
d58c924f | 73 | self.clevel = clevel |
7d0de55e | 74 | |
75 | def __cmp__(self, other): | |
d58c924f | 76 | if self.glevel == other.glevel: |
7d0de55e | 77 | return cmp(self.cmd, other.cmd) |
78 | else: | |
d58c924f | 79 | return cmp(self.glevel, other.glevel) |
7d0de55e | 80 | |
81 | ||
82 | def __str__(self): | |
58401d09 | 83 | if self.admin: |
e6b60193 | 84 | ret = "%-25s(%3s) - %-10s - " % (self.cmd+' '+self.syntax, self.glevel, self.module) |
7d0de55e | 85 | else: |
e6b60193 | 86 | ret = "%-30s - " % (self.cmd+' '+self.syntax) |
d58c924f | 87 | if self.clevel != 0: |
88 | ret += "(%s) " % (lib.clevs[self.clevel]) | |
89 | ret += str(self.shorthelp) | |
90 | return ret | |
7d0de55e | 91 | |
bc68cb5e | 92 | def _mkhelp(level, func): |
93 | lines = [] | |
94 | if level >= func.reqglevel: | |
d58c924f | 95 | lines.append(HelpLine(func.cmd[0], func.syntax, func.shorthelp, (level > 0), func.reqglevel, func.module, func.reqclevel)) |
bc68cb5e | 96 | if len(func.cmd) > 1: |
97 | for c in func.cmd[1:]: | |
d58c924f | 98 | lines.append(HelpLine(c, "", "Alias of %s" % (func.cmd[0]), (level > 0), func.reqglevel, func.module, func.reqclevel)) |
bc68cb5e | 99 | return lines |
100 | ||
101 | def _genhelp(bot, user, chan, realtarget, *args): | |
898cf6a5 | 102 | module = None |
103 | minlevel = -1 | |
104 | maxlevel = 100 | |
0d93d7b4 | 105 | filepath = bot.parent.cfg.get('help', 'path', default='./help/%d.txt') |
898cf6a5 | 106 | for arg in args: |
fd07173d | 107 | if arg.startswith("@"): |
898cf6a5 | 108 | module = arg[1:] |
fd07173d | 109 | elif arg.startswith("#") and user.glevel >= lib.ADMIN: |
898cf6a5 | 110 | minlevel = maxlevel = int(arg[1:]) |
111 | else: | |
112 | filepath = arg | |
113 | if minlevel != maxlevel: | |
114 | minlevel = maxlevel | |
898cf6a5 | 115 | for level in range(minlevel, maxlevel+1): |
116 | if '%d' in filepath: | |
117 | filename = filepath % (level) | |
118 | else: | |
119 | filename = filepath | |
0d93d7b4 | 120 | fo = open(filename, 'w') |
121 | lines = [] | |
122 | for func in helps.itervalues(): | |
898cf6a5 | 123 | if module is not None and func.module != module: |
124 | continue | |
0d93d7b4 | 125 | lines += _mkhelp(level, func) |
126 | for line in sorted(lines): | |
127 | fo.write(str(line)+"\n") | |
898cf6a5 | 128 | fo.close() |
bc68cb5e | 129 | return True |
130 | ||
131 | @lib.hook(glevel=1, needchan=False) | |
b0b9af44 | 132 | @lib.help("[@<module>] [#<level>] [file]", "generates help file", "arguments are all optional and may be specified in any order", "default file: ./help/<level>.txt", "config as: [help]", "path = ./help/%d.txt") |
bc68cb5e | 133 | def genhelp(bot, user, chan, realtarget, *args): |
0d93d7b4 | 134 | try: |
135 | _genhelp(bot, user, chan, realtarget, *args) | |
136 | except Exception as e: | |
137 | bot.msg(user, "Failed writing help. %s" % (e)) | |
138 | return | |
139 | bot.msg(user, "Help written.") | |
bc68cb5e | 140 | |
3569ead3 | 141 | #@lib.hook(needchan=False) |
142 | #@lib.help("<command>", "describes a command") | |
bc68cb5e | 143 | @lib.argsGE(1) |
144 | def help(bot, user, chan, realtarget, *args): | |
145 | cmd = str(' '.join(args)).lower() | |
146 | if cmd in cmds and user.glevel >= cmds[cmd].reqglevel: | |
147 | func = cmds[cmd] | |
d58c924f | 148 | bot.slowmsg(user, str(HelpLine(func.cmd[0], func.syntax, func.shorthelp, (user.glevel > 0), func.reqglevel, func.module, func.reqclevel))) |
bc68cb5e | 149 | for line in func.longhelps: |
150 | bot.slowmsg(user, " %s" % (line)) | |
bc68cb5e | 151 | if len(func.cmd) > 1: |
152 | bot.slowmsg(user, " Aliases: %s" % (' '.join(func.cmd[1:]))) | |
153 | else: | |
154 | bot.slowmsg(user, "No help found for %s" % (cmd)) | |
155 | ||
156 | @lib.hook(needchan=False) | |
157 | @lib.help(None, "provides command list") | |
158 | def showcommands(bot, user, chan, realtarget, *args): | |
dcc5bde3 | 159 | if bot.parent.cfg.getboolean('help', 'autogen'): |
0d93d7b4 | 160 | try: |
161 | _genhelp(bot, user, chan, realtarget, *args) | |
162 | except: pass | |
163 | ||
bc68cb5e | 164 | url = bot.parent.cfg.get('help', 'url', default=None) |
165 | if url is None: | |
166 | try: | |
167 | import urllib2 | |
168 | myip = urllib2.urlopen("https://api.ipify.org").read() | |
169 | url = "http://%s/help/%%d.txt (maybe)" % (myip) | |
170 | except: url = None | |
171 | if url is not None: | |
172 | url = url % (user.glevel) | |
173 | bot.msg(user, "Help is at: %s" % (url)) | |
174 | else: | |
175 | bot.msg(user, "I don't know where help is. Sorry. Contact my owner.") | |
176 | ||
3569ead3 | 177 | #@lib.hook(needchan=False) |
178 | #@lib.help('[@<module>|<command>]', 'lists commands or describes a command', 'with @<module>, lists all commands in <module>') | |
179 | def help_nolag(bot, user, chan, realtarget, *args): | |
5f03d045 | 180 | if len(args) == 0: # list commands |
7d0de55e | 181 | lines = [] |
0f8352dd | 182 | for func in helps.itervalues(): |
bc68cb5e | 183 | lines += _mkhelp(user, func) |
184 | for line in sorted(lines): | |
185 | bot.slowmsg(user, str(line)) | |
186 | bot.slowmsg(user, "End of command listing.") | |
fd07173d | 187 | elif args[0].startswith("@"): |
bc68cb5e | 188 | lines = [] |
189 | mod = args[0][1:].lower() | |
190 | for func in helps.itervalues(): | |
191 | if func.module == mod: | |
192 | lines += _mkhelp(user, func) | |
7d0de55e | 193 | for line in sorted(lines): |
194 | bot.slowmsg(user, str(line)) | |
954cae0b | 195 | bot.slowmsg(user, "End of command listing.") |
5f03d045 | 196 | else: # help for a specific command/topic |
bc68cb5e | 197 | cmd = str(' '.join(args)).lower() |
0f8352dd | 198 | if cmd in cmds and user.glevel >= cmds[cmd].reqglevel: |
199 | func = cmds[cmd] | |
d4cfb340 | 200 | bot.slowmsg(user, str(HelpLine(func.cmd[0], func.syntax, func.shorthelp, (user.glevel > 0), func.reqglevel, func.module, func.reqclevel))) |
0f8352dd | 201 | for line in func.longhelps: |
202 | bot.slowmsg(user, " %s" % (line)) | |
954cae0b | 203 | bot.slowmsg(user, "End of help for %s." % (func.cmd[0])) |
0f8352dd | 204 | |
205 | if len(func.cmd) > 1: | |
206 | bot.slowmsg(user, " Aliases: %s" % (' '.join(func.cmd[1:]))) | |
207 | else: | |
208 | bot.slowmsg(user, "No help found for %s" % (cmd)) |