]>
jfr.im git - erebus.git/blob - modules/control.py
1 # Erebus IRC bot - Author: Erebus Team
2 # vim: fileencoding=utf-8
3 # Various highly recommended "control" commands.
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 modstart
= lib
. modstart
24 from collections
import deque
27 @lib.hook (( 'die' , 'restart' ), needchan
= False , glevel
= lib
. MANAGER
)
28 @lib.help ( None , "stops the bot" )
29 def die ( bot
, user
, chan
, realtarget
, * args
):
30 quitmsg
= ' ' . join ( args
)
31 for botitem
in bot
. parent
. bots
. values ():
32 bot
. conn
. send ( "QUIT :Restarting. %s " % ( quitmsg
))
35 @lib.hook ( needchan
= False , glevel
= lib
. MANAGER
)
36 @lib.help ( "[-autoload] <mod>" , "loads a module, optionally marking it for autoloading" )
38 def modload ( bot
, user
, chan
, realtarget
, * args
):
43 if module
is not None :
44 bot
. msg ( user
, "Wrong number of arguments." )
47 elif arg
== "-autoload" :
50 bot
. msg ( user
, "Bad option %s " % ( arg
))
53 bot
. parent
. cfg
. set ( 'autoloads' , module
, 1 )
54 bot
. msg ( user
, "Marked %s for autoloading." % ( module
))
55 okay
= ctlmod
. load ( bot
. parent
, module
)
57 bot
. msg ( user
, "Loaded %s " % ( module
))
59 bot
. msg ( user
, "Error loading %s : %r " % ( module
, okay
))
61 @lib.hook ( needchan
= False , glevel
= lib
. MANAGER
)
62 @lib.help ( "[-force] [-autoload] <mod>" , "unloads a module" , "will refuse to unload a module which is depended on by others unless you specify FORCE." , "optionally removes from autoloads" )
64 def modunload ( bot
, user
, chan
, realtarget
, * args
):
70 if module
is not None :
71 bot
. msg ( user
, "Incorrect syntax - can only name one module" )
74 elif arg
. lower () == "-autoload" :
76 elif arg
. lower () == "-force" :
79 bot
. msg ( user
, "Bad option %s " % ( arg
))
82 bot
. parent
. cfg
. delete ( 'autoloads' , module
)
83 bot
. msg ( user
, "Unmarked %s for autoloading." % ( module
))
84 if not ctlmod
. isloaded ( module
):
85 bot
. msg ( user
, " %s is not loaded" % ( module
))
87 if len ( ctlmod
. dependents
[ module
]) > 0 :
89 bot
. msg ( user
, "That module has dependents! Say MODUNLOAD -force %s to unload it and any dependents." % ( module
))
92 okay
= ctlmod
. unload ( bot
. parent
, module
)
94 bot
. msg ( user
, "Unloaded %s " % ( module
))
96 bot
. msg ( user
, "Error unloading %s : %r " % ( module
, okay
))
98 @lib.hook ( needchan
= False , glevel
= lib
. MANAGER
)
99 @lib.help ( "<mod>" , "reloads a module" )
101 def modreload ( bot
, user
, chan
, realtarget
, * args
):
102 if not ctlmod
. isloaded ( args
[ 0 ]):
103 bot
. msg ( user
, " %s is not loaded" % ( args
[ 0 ]))
106 okay
= ctlmod
. reloadmod ( bot
. parent
, args
[ 0 ])
108 bot
. msg ( user
, "Reloaded %s " % ( args
[ 0 ]))
110 bot
. msg ( user
, "Error occurred: %r " % ( okay
))
112 @lib.hook ( needchan
= False , glevel
= lib
. STAFF
)
113 @lib.help ( None , "list loaded modules" )
115 def modlist ( bot
, user
, chan
, realtarget
, * args
):
116 mods
= ctlmod
. modules
117 for modname
, mod
in sorted ( mods
. items ()):
118 bot
. msg ( user
, "- %s ( %s ) [ %s ]" % (( modname
, mod
.__ file
__ , ', ' . join ( ctlmod
. dependents
[ modname
]))))
119 bot
. msg ( user
, "Done." )
121 def _whois ( user
, chan
, showglevel
= True , showclevel
= True ):
122 if not user
. isauthed ():
125 fillers
= {'auth': user.auth}
128 if showglevel
and user
. glevel
>= 1 :
129 fillers
[ 'glevel' ] = user
. glevel
130 fmt
+= " (global access: %(glevel)s )"
131 elif user
. glevel
>= 1 :
133 elif user
. glevel
<= - 2 :
136 fmt
+= " (not staff)"
138 if ( showclevel
or showglevel
) and chan
is not None :
139 clev
= chan
. levelof ( user
. auth
)
141 fillers
[ 'clevel' ] = ( lib
. clevs
[ clev
] if clev
< len ( lib
. clevs
) else clev
)
142 fmt
+= " (channel access: %(clevel)s )"
144 fmt
+= " (not a channel user)"
147 @lib.hook ( needchan
= False , wantchan
= True )
148 @lib.help ( "<user|#auth>" , "shows who someone is" )
150 def whois ( bot
, user
, chan
, realtarget
, * args
):
152 if name
. startswith ( "#" ):
153 target
= bot
. parent
. User ( name
, name
[ 1 :])
155 target
= bot
. parent
. user ( name
, create
= False )
157 return "I don't know %s ." % ( args
[ 0 ])
159 return " %s is %s " % ( args
[ 0 ], _whois ( target
, chan
, ( user
. glevel
>= 1 ), ( chan
is not None and chan
. levelof ( user
. auth
) >= 1 )))
161 @lib.hook ( needchan
= False , wantchan
= True )
162 @lib.help ( None , "shows who you are" )
163 def whoami ( bot
, user
, chan
, realtarget
, * args
):
164 return "You are %s " % ( _whois ( user
, chan
))
166 @lib.hook ( needchan
= False )
167 @lib.help ( None , "tries to read your auth and access level again" )
168 def auth ( bot
, user
, chan
, realtarget
, * args
):
169 bot
. msg ( user
, "Okay, give me a second." )
170 bot
. conn
. send ( "WHO %s n %% ant,2" % ( user
))
172 @lib.hook ( needchan
= False , glevel
= 1 )
173 @lib.help ( None , "displays length of each msgqueue" )
174 def qstat ( bot
, user
, chan
, realtarget
, * args
):
175 bot
. fastmsg ( user
, "Regular: %d -- Slow: %d " % ( len ( bot
. msgqueue
), len ( bot
. slowmsgqueue
)))
177 @lib.hook (( 'qclear' , 'cq' , 'clearq' , 'clearqueue' ), needchan
= False , glevel
= lib
. ADMIN
)
178 @lib.help ( "[regular|slow]" , "clears both or a specific msgqueue" )
179 def qclear ( bot
, user
, chan
, realtarget
, * args
):
181 bot
. msgqueue
= deque ()
182 bot
. slowmsgqueue
= deque ()
183 bot
. fastmsg ( user
, "Cleared both msgqueues." )
185 if args
[ 0 ] == 'regular' :
186 bot
. msgqueue
= deque ()
187 elif args
[ 0 ] == 'slow' :
188 bot
. slowmsgqueue
= deque ()
190 bot
. fastmsg ( user
, "Syntax: QCLEAR [regular|slow]" )
191 return #short-circuit
192 bot
. fastmsg ( user
, "Cleared that msgqueue." )