]> jfr.im git - erebus.git/blob - modules/control.py
control - use new flag-style options in error messages
[erebus.git] / 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/
5
6 # module info
7 modinfo = {
8 'author': 'Erebus Team',
9 'license': 'public domain',
10 'compatible': [0],
11 'depends': [],
12 'softdeps': ['help'],
13 }
14
15 # preamble
16 import modlib
17 lib = modlib.modlib(__name__)
18 modstart = lib.modstart
19 modstop = lib.modstop
20
21 # module code
22 import sys, os
23 import ctlmod
24 from collections import deque
25
26
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))
33 sys.exit(0)
34
35 @lib.hook(needchan=False, glevel=lib.MANAGER)
36 @lib.help("[-autoload] <mod>", "loads a module, optionally marking it for autoloading")
37 @lib.argsGE(1)
38 def modload(bot, user, chan, realtarget, *args):
39 module = None
40 autoload = False
41 for arg in args:
42 if arg[0] != "-":
43 if module is not None:
44 bot.msg(user, "Wrong number of arguments.")
45 return
46 module = arg
47 elif arg == "-autoload":
48 autoload = True
49 else:
50 bot.msg(user, "Bad option %s" % (arg))
51
52 if autoload:
53 bot.parent.cfg.set('autoloads', module, 1)
54 bot.msg(user, "Marked %s for autoloading." % (module))
55 okay = ctlmod.load(bot.parent, module)
56 if okay:
57 bot.msg(user, "Loaded %s" % (module))
58 else:
59 bot.msg(user, "Error loading %s: %r" % (module, okay))
60
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")
63 @lib.argsGE(1)
64 def modunload(bot, user, chan, realtarget, *args):
65 module = None
66 autoload = False
67 force = False
68 for arg in args:
69 if arg[0] != "-":
70 if module is not None:
71 bot.msg(user, "Incorrect syntax - can only name one module")
72 return
73 module = arg
74 elif arg.lower() == "-autoload":
75 autoload = True
76 elif arg.lower() == "-force":
77 force = True
78 else:
79 bot.msg(user, "Bad option %s" % (arg))
80
81 if autoload:
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))
86 return
87 if len(ctlmod.dependents[module]) > 0:
88 if not force:
89 bot.msg(user, "That module has dependents! Say MODUNLOAD -force %s to unload it and any dependents." % (module))
90 return
91
92 okay = ctlmod.unload(bot.parent, module)
93 if okay:
94 bot.msg(user, "Unloaded %s" % (module))
95 else:
96 bot.msg(user, "Error unloading %s: %r" % (module, okay))
97
98 @lib.hook(needchan=False, glevel=lib.MANAGER)
99 @lib.help("<mod>", "reloads a module")
100 @lib.argsEQ(1)
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]))
104 return
105
106 okay = ctlmod.reloadmod(bot.parent, args[0])
107 if okay:
108 bot.msg(user, "Reloaded %s" % (args[0]))
109 else:
110 bot.msg(user, "Error occurred: %r" % (okay))
111
112 @lib.hook(needchan=False, glevel=lib.STAFF)
113 @lib.help(None, "list loaded modules")
114 @lib.argsEQ(0)
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.")
120
121 def _whois(user, chan, showglevel=True, showclevel=True):
122 if not user.isauthed():
123 return "not authed."
124
125 fillers = {'auth': user.auth}
126 fmt = "%(auth)s"
127
128 if showglevel and user.glevel >= 1:
129 fillers['glevel'] = user.glevel
130 fmt += " (global access: %(glevel)s)"
131 elif user.glevel >= 1:
132 fmt += " (staff)"
133 elif user.glevel <= -2:
134 fmt += " (ignored)"
135 else:
136 fmt += " (not staff)"
137
138 if (showclevel or showglevel) and chan is not None:
139 clev = chan.levelof(user.auth)
140 if clev >= 1:
141 fillers['clevel'] = (lib.clevs[clev] if clev < len(lib.clevs) else clev)
142 fmt += " (channel access: %(clevel)s)"
143 else:
144 fmt += " (not a channel user)"
145 return fmt % fillers
146
147 @lib.hook(needchan=False, wantchan=True)
148 @lib.help("<user|#auth>", "shows who someone is")
149 @lib.argsEQ(1)
150 def whois(bot, user, chan, realtarget, *args):
151 name = args[0]
152 if name.startswith("#"):
153 target = bot.parent.User(name, name[1:])
154 else:
155 target = bot.parent.user(name, create=False)
156 if target is None:
157 return "I don't know %s." % (args[0])
158 else:
159 return "%s is %s" % (args[0], _whois(target, chan, (user.glevel >= 1), (chan is not None and chan.levelof(user.auth) >= 1)))
160
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))
165
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))
171
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)))
176
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):
180 if len(args) == 0:
181 bot.msgqueue = deque()
182 bot.slowmsgqueue = deque()
183 bot.fastmsg(user, "Cleared both msgqueues.")
184 else:
185 if args[0] == 'regular':
186 bot.msgqueue = deque()
187 elif args[0] == 'slow':
188 bot.slowmsgqueue = deque()
189 else:
190 bot.fastmsg(user, "Syntax: QCLEAR [regular|slow]")
191 return #short-circuit
192 bot.fastmsg(user, "Cleared that msgqueue.")