]>
jfr.im git - erebus.git/blob - modules/help.py
1 # Erebus IRC bot - Author: Erebus Team
3 # This file is released into the public domain; see http://unlicense.org/
7 'author': 'Erebus Team',
8 'license': 'public domain',
16 lib
= modlib
.modlib(__name__
)
17 modstart
= lib
.modstart
25 # ! this is part of this module's API, called from modlib.help()
26 def reghelp(func
, *args
, **kwargs
):
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']
45 if syntax
is None: syntax
= ""
46 if shorthelp
is None: shorthelp
= ""
49 func
.shorthelp
= shorthelp
50 func
.longhelps
= longhelps
55 def dereghelp(func
, *args
, **kwargs
):
60 class HelpLine(object):
61 def __init__(self
, cmd
, syntax
, shorthelp
, admin
, level
, module
):
64 self
.shorthelp
= shorthelp
69 def __cmp__(self
, other
):
70 if self
.level
== other
.level
:
71 return cmp(self
.cmd
, other
.cmd
)
73 return cmp(self
.level
, other
.level
)
78 return "%-35s(%3s) - %-10s - %-50s" % (self
.cmd
+' '+self
.syntax
, self
.level
, self
.module
, self
.shorthelp
)
80 return "%-40s - %-50s" % (self
.cmd
+' '+self
.syntax
, self
.shorthelp
)
82 def _mkhelp(level
, func
):
84 if level
>= func
.reqglevel
:
85 lines
.append(HelpLine(func
.cmd
[0], func
.syntax
, func
.shorthelp
, (level
> 0), func
.reqglevel
, func
.module
))
87 for c
in func
.cmd
[1:]:
88 lines
.append(HelpLine(c
, "", "Alias of %s" % (func
.cmd
[0]), (level
> 0), func
.reqglevel
, func
.module
))
91 def _genhelp(bot
, user
, chan
, realtarget
, *args
):
93 filepath
= bot
.parent
.cfg
.get('help', 'path', default
='./help/%d.txt')
94 for level
in range(-1, 101):
95 filename
= filepath
% (level
)
96 fo
= open(filename
, 'w')
98 for func
in helps
.itervalues():
99 lines
+= _mkhelp(level
, func
)
100 for line
in sorted(lines
):
101 fo
.write(str(line
)+"\n")
102 except Exception as e
:
106 @lib.hook(glevel
=1, needchan
=False)
107 @lib.help(None, "generates help file", "default path: ./help/<level>.txt", "config as: [help]", "path = ./help/%d.txt")
108 #TODO: use args... "[@<module>] [#<level>] [<file>]"
109 def genhelp(bot
, user
, chan
, realtarget
, *args
):
110 ret
= _genhelp(bot
, user
, chan
, realtarget
, *args
)
111 if not isinstance(ret
, BaseException
):
112 bot
.msg(user
, "Help written.")
114 bot
.msg(user
, "Failed writing help. %s" % (ret
))
116 @lib.hook(needchan
=False)
117 @lib.help("<command>", "describes a command")
119 def help(bot
, user
, chan
, realtarget
, *args
):
120 cmd
= str(' '.join(args
)).lower()
121 if cmd
in cmds
and user
.glevel
>= cmds
[cmd
].reqglevel
:
123 bot
.slowmsg(user
, str(HelpLine(func
.cmd
[0], func
.syntax
, func
.shorthelp
, (user
.glevel
> 0), func
.reqglevel
, func
.module
)))
124 for line
in func
.longhelps
:
125 bot
.slowmsg(user
, " %s" % (line
))
126 bot
.slowmsg(user
, "End of help for %s." % (func
.cmd
[0]))
127 if len(func
.cmd
) > 1:
128 bot
.slowmsg(user
, " Aliases: %s" % (' '.join(func
.cmd
[1:])))
130 bot
.slowmsg(user
, "No help found for %s" % (cmd
))
132 @lib.hook(needchan
=False)
133 @lib.help(None, "provides command list")
134 def showcommands(bot
, user
, chan
, realtarget
, *args
):
135 if bool(int(bot
.parent
.cfg
.get('help', 'autogen', default
=0))):
136 _genhelp(bot
, user
, chan
, realtarget
, *args
)
137 url
= bot
.parent
.cfg
.get('help', 'url', default
=None)
141 myip
= urllib2
.urlopen("https://api.ipify.org").read()
142 url
= "http://%s/help/%%d.txt (maybe)" % (myip
)
145 url
= url
% (user
.glevel
)
146 bot
.msg(user
, "Help is at: %s" % (url
))
148 bot
.msg(user
, "I don't know where help is. Sorry. Contact my owner.")
151 @lib.hook(needchan=False)
152 @lib.help('[@<module>|<command>]', 'lists commands or describes a command', 'with @<module>, lists all commands in <module>')
153 def help(bot, user, chan, realtarget, *args):
154 if len(args) == 0: # list commands
156 for func in helps.itervalues():
157 lines += _mkhelp(user, func)
158 for line in sorted(lines):
159 bot.slowmsg(user, str(line))
160 bot.slowmsg(user, "End of command listing.")
161 elif args[0][0] == "@":
163 mod = args[0][1:].lower()
164 for func in helps.itervalues():
165 if func.module == mod:
166 lines += _mkhelp(user, func)
167 for line in sorted(lines):
168 bot.slowmsg(user, str(line))
169 bot.slowmsg(user, "End of command listing.")
170 else: # help for a specific command/topic
171 cmd = str(' '.join(args)).lower()
172 if cmd in cmds and user.glevel >= cmds[cmd].reqglevel:
174 bot.slowmsg(user, str(HelpLine(func.cmd[0], func.syntax, func.shorthelp, (user.glevel > 0), func.reqglevel, func.module)))
175 for line in func.longhelps:
176 bot.slowmsg(user, " %s" % (line))
177 bot.slowmsg(user, "End of help for %s." % (func.cmd[0]))
179 if len(func.cmd) > 1:
180 bot.slowmsg(user, " Aliases: %s" % (' '.join(func.cmd[1:])))
182 bot.slowmsg(user, "No help found for %s" % (cmd))