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