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 def modstart(parent
, *args
, **kwargs
):
18 if parent
.cfg
.getboolean('erebus', 'nofakelag'):
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
))
21 lib
.hook('help', needchan
=False)(lib
.help("<command>", "describes a command")(help))
22 return lib
.modstart(parent
, *args
, **kwargs
)
30 # ! this is part of this module's API, called from modlib.help()
31 def reghelp(func
, *args
, **kwargs
):
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']
50 if syntax
is None: syntax
= ""
51 if shorthelp
is None: shorthelp
= ""
54 func
.shorthelp
= shorthelp
55 func
.longhelps
= longhelps
60 def dereghelp(func
, *args
, **kwargs
):
65 class HelpLine(object):
66 def __init__(self
, cmd
, syntax
, shorthelp
, admin
, glevel
, module
, clevel
):
69 self
.shorthelp
= shorthelp
75 def __cmp__(self
, other
):
76 if self
.glevel
== other
.glevel
:
77 return cmp(self
.cmd
, other
.cmd
)
79 return cmp(self
.glevel
, other
.glevel
)
84 ret
= "%-25s(%3s) - %-10s - " % (self
.cmd
+' '+self
.syntax
, self
.glevel
, self
.module
)
86 ret
= "%-30s - " % (self
.cmd
+' '+self
.syntax
)
88 ret
+= "(%s) " % (lib
.clevs
[self
.clevel
])
89 ret
+= str(self
.shorthelp
)
92 def _mkhelp(level
, func
):
94 if level
>= func
.reqglevel
:
95 lines
.append(HelpLine(func
.cmd
[0], func
.syntax
, func
.shorthelp
, (level
> 0), func
.reqglevel
, func
.module
, func
.reqclevel
))
97 for c
in func
.cmd
[1:]:
98 lines
.append(HelpLine(c
, "", "Alias of %s" % (func
.cmd
[0]), (level
> 0), func
.reqglevel
, func
.module
, func
.reqclevel
))
101 def _genhelp(bot
, user
, chan
, realtarget
, *args
):
105 filepath
= bot
.parent
.cfg
.get('help', 'path', default
='./help/%(@)s%(#)d.txt')
107 if arg
.startswith("@"):
109 raise Exception('Module option must not contain "."')
111 elif arg
.startswith("#") and user
.glevel
>= lib
.ADMIN
:
112 minlevel
= maxlevel
= int(arg
[1:])
113 elif arg
.startswith("+"):
114 maxlevel
= int(arg
[1:])
115 elif arg
.startswith("-"):
116 minlevel
= int(arg
[1:])
117 elif arg
.startswith("./"):
119 raise Exception('Filename option must not contain "./" except as the first two characters')
121 filepath
= os
.path
.join('help', arg
[2:])
123 raise Exception('Unknown option given to GENHELP: %s' % (arg
))
124 for level
in range(minlevel
, maxlevel
+1):
125 filename
= filepath
% {'#': level, '+': maxlevel, '-': minlevel, '@': module}
126 fo
= open(filename
, 'w')
128 for func
in helps
.values():
129 if module
!= '' and func
.module
!= module
:
131 lines
+= _mkhelp(level
, func
)
132 for line
in sorted(lines
):
133 fo
.write(str(line
)+"\n")
137 @lib.hook(glevel
=1, needchan
=False)
138 @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")
139 def genhelp(bot
, user
, chan
, realtarget
, *args
):
141 _genhelp(bot
, user
, chan
, realtarget
, *args
)
142 except Exception as e
:
143 bot
.msg(user
, "Failed writing help. %s" % (e
))
145 bot
.msg(user
, "Help written.")
147 #@lib.hook(needchan=False)
148 #@lib.help("<command>", "describes a command")
150 def help(bot
, user
, chan
, realtarget
, *args
):
151 cmd
= str(' '.join(args
)).lower()
152 if cmd
in cmds
and user
.glevel
>= cmds
[cmd
].reqglevel
:
154 bot
.slowmsg(user
, str(HelpLine(func
.cmd
[0], func
.syntax
, func
.shorthelp
, (user
.glevel
> 0), func
.reqglevel
, func
.module
, func
.reqclevel
)))
155 for line
in func
.longhelps
:
156 bot
.slowmsg(user
, " %s" % (line
))
157 if len(func
.cmd
) > 1:
158 bot
.slowmsg(user
, " Aliases: %s" % (' '.join(func
.cmd
[1:])))
160 bot
.slowmsg(user
, "No help found for %s" % (cmd
))
162 @lib.hook(needchan
=False)
163 @lib.help(None, "provides command list")
164 def showcommands(bot
, user
, chan
, realtarget
, *args
):
165 if bot
.parent
.cfg
.getboolean('help', 'autogen'):
167 _genhelp(bot
, user
, chan
, realtarget
, *args
)
170 url
= bot
.parent
.cfg
.get('help', 'url', default
=None)
174 myip
= urllib2
.urlopen("https://api.ipify.org").read()
175 url
= "http://%s/help/%%d.txt (maybe)" % (myip
)
178 url
= url
% (user
.glevel
)
179 bot
.msg(user
, "Help is at: %s" % (url
))
181 bot
.msg(user
, "I don't know where help is. Sorry. Contact my owner.")
183 #@lib.hook(needchan=False)
184 #@lib.help('[@<module>|<command>]', 'lists commands or describes a command', 'with @<module>, lists all commands in <module>')
185 def help_nolag(bot
, user
, chan
, realtarget
, *args
):
186 if len(args
) == 0: # list commands
188 for func
in helps
.values():
189 lines
+= _mkhelp(user
, func
)
190 for line
in sorted(lines
):
191 bot
.slowmsg(user
, str(line
))
192 bot
.slowmsg(user
, "End of command listing.")
193 elif args
[0].startswith("@"):
195 mod
= args
[0][1:].lower()
196 for func
in helps
.values():
197 if func
.module
== mod
:
198 lines
+= _mkhelp(user
, func
)
199 for line
in sorted(lines
):
200 bot
.slowmsg(user
, str(line
))
201 bot
.slowmsg(user
, "End of command listing.")
202 else: # help for a specific command/topic
203 cmd
= str(' '.join(args
)).lower()
204 if cmd
in cmds
and user
.glevel
>= cmds
[cmd
].reqglevel
:
206 bot
.slowmsg(user
, str(HelpLine(func
.cmd
[0], func
.syntax
, func
.shorthelp
, (user
.glevel
> 0), func
.reqglevel
, func
.module
, func
.reqclevel
)))
207 for line
in func
.longhelps
:
208 bot
.slowmsg(user
, " %s" % (line
))
209 bot
.slowmsg(user
, "End of help for %s." % (func
.cmd
[0]))
211 if len(func
.cmd
) > 1:
212 bot
.slowmsg(user
, " Aliases: %s" % (' '.join(func
.cmd
[1:])))
214 bot
.slowmsg(user
, "No help found for %s" % (cmd
))