1 # Erebus IRC bot - Author: Erebus Team
2 # vim: fileencoding=utf-8
4 # This file is released into the public domain; see http://unlicense.org/
8 'author': 'Erebus Team',
9 'license': 'public domain',
17 lib
= modlib
.modlib(__name__
)
18 def modstart(parent
, *args
, **kwargs
):
19 if parent
.cfg
.getboolean('erebus', 'nofakelag'):
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
))
22 lib
.hook('help', needchan
=False)(lib
.help("<command>", "describes a command")(help))
23 return lib
.modstart(parent
, *args
, **kwargs
)
31 # ! this is part of this module's API, called from modlib.help()
32 def reghelp(func
, *args
, **kwargs
):
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']
51 if syntax
is None: syntax
= ""
52 if shorthelp
is None: shorthelp
= ""
55 func
.shorthelp
= shorthelp
56 func
.longhelps
= longhelps
61 def dereghelp(func
, *args
, **kwargs
):
66 class HelpLine(object):
67 def __init__(self
, cmd
, syntax
, shorthelp
, admin
, glevel
, module
, clevel
):
70 self
.shorthelp
= shorthelp
76 def __cmp__(self
, other
):
77 if self
.glevel
== other
.glevel
:
78 return cmp(self
.cmd
, other
.cmd
)
80 return cmp(self
.glevel
, other
.glevel
)
85 ret
= "%-25s(%3s) - %-10s - " % (self
.cmd
+' '+self
.syntax
, self
.glevel
, self
.module
)
87 ret
= "%-30s - " % (self
.cmd
+' '+self
.syntax
)
89 ret
+= "(%s) " % (lib
.clevs
[self
.clevel
])
90 ret
+= str(self
.shorthelp
)
93 def _mkhelp(level
, func
):
95 if level
>= func
.reqglevel
:
96 lines
.append(HelpLine(func
.cmd
[0], func
.syntax
, func
.shorthelp
, (level
> 0), func
.reqglevel
, func
.module
, func
.reqclevel
))
98 for c
in func
.cmd
[1:]:
99 lines
.append(HelpLine(c
, "", "Alias of %s" % (func
.cmd
[0]), (level
> 0), func
.reqglevel
, func
.module
, func
.reqclevel
))
102 def _genhelp(bot
, user
, chan
, realtarget
, *args
):
106 filepath
= bot
.parent
.cfg
.get('help', 'path', default
='./help/%(@)s%(#)d.txt')
108 if arg
.startswith("@"):
110 raise Exception('Module option must not contain "."')
112 elif arg
.startswith("#") and user
.glevel
>= lib
.ADMIN
:
113 minlevel
= maxlevel
= int(arg
[1:])
114 elif arg
.startswith("+"):
115 maxlevel
= int(arg
[1:])
116 elif arg
.startswith("-"):
117 minlevel
= int(arg
[1:])
118 elif arg
.startswith("./"):
120 raise Exception('Filename option must not contain "./" except as the first two characters')
122 filepath
= os
.path
.join('help', arg
[2:])
124 raise Exception('Unknown option given to GENHELP: %s' % (arg
))
125 for level
in range(minlevel
, maxlevel
+1):
126 filename
= filepath
% {'#': level, '+': maxlevel, '-': minlevel, '@': module}
127 fo
= open(filename
, 'w')
129 for func
in helps
.values():
130 if module
!= '' and func
.module
!= module
:
132 lines
+= _mkhelp(level
, func
)
133 for line
in sorted(lines
):
134 fo
.write(str(line
)+"\n")
138 @lib.hook(glevel
=1, needchan
=False)
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: ./help/<module><level>.txt, with module blank if not supplied", "filename can also contain %(@)s, %(#)s, %(+)s, %(-)s", "for module, current (single) level, max and min level, respectively")
140 def genhelp(bot
, user
, chan
, realtarget
, *args
):
142 _genhelp(bot
, user
, chan
, realtarget
, *args
)
143 except Exception as e
:
144 bot
.msg(user
, "Failed writing help. %s" % (e
))
146 bot
.msg(user
, "Help written.")
148 #@lib.hook(needchan=False)
149 #@lib.help("<command>", "describes a command")
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
:
155 bot
.slowmsg(user
, str(HelpLine(func
.cmd
[0], func
.syntax
, func
.shorthelp
, (user
.glevel
> 0), func
.reqglevel
, func
.module
, func
.reqclevel
)))
156 for line
in func
.longhelps
:
157 bot
.slowmsg(user
, " %s" % (line
))
158 if len(func
.cmd
) > 1:
159 bot
.slowmsg(user
, " Aliases: %s" % (' '.join(func
.cmd
[1:])))
161 bot
.slowmsg(user
, "No help found for %s" % (cmd
))
163 @lib.hook(needchan
=False)
164 @lib.help(None, "provides command list")
165 def showcommands(bot
, user
, chan
, realtarget
, *args
):
166 if bot
.parent
.cfg
.getboolean('help', 'autogen'):
168 _genhelp(bot
, user
, chan
, realtarget
, *args
)
171 url
= bot
.parent
.cfg
.get('help', 'url', default
=None)
175 myip
= urllib2
.urlopen("https://api.ipify.org").read()
176 url
= "http://%s/help/%%d.txt (maybe)" % (myip
)
179 url
= url
% (user
.glevel
)
180 bot
.msg(user
, "Help is at: %s" % (url
))
182 bot
.msg(user
, "I don't know where help is. Sorry. Contact my owner.")
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
):
187 if len(args
) == 0: # list commands
189 for func
in helps
.values():
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.")
194 elif args
[0].startswith("@"):
196 mod
= args
[0][1:].lower()
197 for func
in helps
.values():
198 if func
.module
== mod
:
199 lines
+= _mkhelp(user
, func
)
200 for line
in sorted(lines
):
201 bot
.slowmsg(user
, str(line
))
202 bot
.slowmsg(user
, "End of command listing.")
203 else: # help for a specific command/topic
204 cmd
= str(' '.join(args
)).lower()
205 if cmd
in cmds
and user
.glevel
>= cmds
[cmd
].reqglevel
:
207 bot
.slowmsg(user
, str(HelpLine(func
.cmd
[0], func
.syntax
, func
.shorthelp
, (user
.glevel
> 0), func
.reqglevel
, func
.module
, func
.reqclevel
)))
208 for line
in func
.longhelps
:
209 bot
.slowmsg(user
, " %s" % (line
))
210 bot
.slowmsg(user
, "End of help for %s." % (func
.cmd
[0]))
212 if len(func
.cmd
) > 1:
213 bot
.slowmsg(user
, " Aliases: %s" % (' '.join(func
.cmd
[1:])))
215 bot
.slowmsg(user
, "No help found for %s" % (cmd
))