]>
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
, glevel
, module
, clevel
):
64 self
. shorthelp
= shorthelp
70 def __cmp__ ( self
, other
):
71 if self
. glevel
== other
. glevel
:
72 return cmp ( self
. cmd
, other
. cmd
)
74 return cmp ( self
. glevel
, other
. glevel
)
79 ret
= " %- 35s(%3s) - %- 10s - " % ( self
. cmd
+ ' ' + self
. syntax
, self
. glevel
, self
. module
)
81 ret
= " %- 40s - " % ( self
. cmd
+ ' ' + self
. syntax
)
83 ret
+= "( %s ) " % ( lib
. clevs
[ self
. clevel
])
84 ret
+= str ( self
. shorthelp
)
87 def _mkhelp ( level
, func
):
89 if level
>= func
. reqglevel
:
90 lines
. append ( HelpLine ( func
. cmd
[ 0 ], func
. syntax
, func
. shorthelp
, ( level
> 0 ), func
. reqglevel
, func
. module
, func
. reqclevel
))
92 for c
in func
. cmd
[ 1 :]:
93 lines
. append ( HelpLine ( c
, "" , "Alias of %s " % ( func
. cmd
[ 0 ]), ( level
> 0 ), func
. reqglevel
, func
. module
, func
. reqclevel
))
96 def _genhelp ( bot
, user
, chan
, realtarget
, * args
):
100 filepath
= bot
. parent
. cfg
. get ( 'help' , 'path' , default
= './help/ %d .txt' )
102 if arg
. startswith ( "@" ):
104 elif arg
. startswith ( "#" ) and user
. glevel
>= lib
. ADMIN
:
105 minlevel
= maxlevel
= int ( arg
[ 1 :])
108 if minlevel
!= maxlevel
:
110 for level
in range ( minlevel
, maxlevel
+ 1 ):
112 filename
= filepath
% ( level
)
115 fo
= open ( filename
, 'w' )
117 for func
in helps
. itervalues ():
118 if module
is not None and func
. module
!= module
:
120 lines
+= _mkhelp ( level
, func
)
121 for line
in sorted ( lines
):
122 fo
. write ( str ( line
)+ " \n " )
126 @lib.hook ( glevel
= 1 , needchan
= False )
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" )
128 def genhelp ( bot
, user
, chan
, realtarget
, * args
):
130 _genhelp ( bot
, user
, chan
, realtarget
, * args
)
131 except Exception as e
:
132 bot
. msg ( user
, "Failed writing help. %s " % ( e
))
134 bot
. msg ( user
, "Help written." )
136 @lib.hook ( needchan
= False )
137 @lib.help ( "<command>" , "describes a command" )
139 def help ( bot
, user
, chan
, realtarget
, * args
):
140 cmd
= str ( ' ' . join ( args
)). lower ()
141 if cmd
in cmds
and user
. glevel
>= cmds
[ cmd
]. reqglevel
:
143 bot
. slowmsg ( user
, str ( HelpLine ( func
. cmd
[ 0 ], func
. syntax
, func
. shorthelp
, ( user
. glevel
> 0 ), func
. reqglevel
, func
. module
, func
. reqclevel
)))
144 for line
in func
. longhelps
:
145 bot
. slowmsg ( user
, " %s " % ( line
))
146 if len ( func
. cmd
) > 1 :
147 bot
. slowmsg ( user
, " Aliases: %s " % ( ' ' . join ( func
. cmd
[ 1 :])))
149 bot
. slowmsg ( user
, "No help found for %s " % ( cmd
))
151 @lib.hook ( needchan
= False )
152 @lib.help ( None , "provides command list" )
153 def showcommands ( bot
, user
, chan
, realtarget
, * args
):
154 if bool ( int ( bot
. parent
. cfg
. get ( 'help' , 'autogen' , default
= 0 ))):
156 _genhelp ( bot
, user
, chan
, realtarget
, * args
)
159 url
= bot
. parent
. cfg
. get ( 'help' , 'url' , default
= None )
163 myip
= urllib2
. urlopen ( "https://api.ipify.org" ). read ()
164 url
= "http:// %s /help/ %%d .txt (maybe)" % ( myip
)
167 url
= url
% ( user
. glevel
)
168 bot
. msg ( user
, "Help is at: %s " % ( url
))
170 bot
. msg ( user
, "I don't know where help is. Sorry. Contact my owner." )
173 @lib.hook(needchan=False)
174 @lib.help('[@<module>|<command>]', 'lists commands or describes a command', 'with @<module>, lists all commands in <module>')
175 def help(bot, user, chan, realtarget, *args):
176 if len(args) == 0: # list commands
178 for func in helps.itervalues():
179 lines += _mkhelp(user, func)
180 for line in sorted(lines):
181 bot.slowmsg(user, str(line))
182 bot.slowmsg(user, "End of command listing.")
183 elif args[0].startswith("@"):
185 mod = args[0][1:].lower()
186 for func in helps.itervalues():
187 if func.module == mod:
188 lines += _mkhelp(user, func)
189 for line in sorted(lines):
190 bot.slowmsg(user, str(line))
191 bot.slowmsg(user, "End of command listing.")
192 else: # help for a specific command/topic
193 cmd = str(' '.join(args)).lower()
194 if cmd in cmds and user.glevel >= cmds[cmd].reqglevel:
196 bot.slowmsg(user, str(HelpLine(func.cmd[0], func.syntax, func.shorthelp, (user.glevel > 0), func.reqglevel, func.module)))
197 for line in func.longhelps:
198 bot.slowmsg(user, " %s " % (line))
199 bot.slowmsg(user, "End of help for %s ." % (func.cmd[0]))
201 if len(func.cmd) > 1:
202 bot.slowmsg(user, " Aliases: %s " % (' '.join(func.cmd[1:])))
204 bot.slowmsg(user, "No help found for %s " % (cmd))