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