]> jfr.im git - erebus.git/blob - modules/control.py
control - use new flags feature
[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','exit'), 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.flags('autoload')
38 @lib.argsEQ(1)
39 def modload(bot, user, chan, realtarget, flags, *args):
40 autoload = flags['autoload']
41 module = args[0]
42
43 if autoload:
44 bot.parent.cfg.set('autoloads', module, 1)
45 bot.msg(user, "Marked %s for autoloading." % (module))
46 okay = ctlmod.load(bot.parent, module)
47 if okay:
48 bot.msg(user, "Loaded %s" % (module))
49 else:
50 bot.msg(user, "Error loading %s: %r" % (module, okay))
51
52 @lib.hook(needchan=False, glevel=lib.MANAGER)
53 @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")
54 @lib.flags('force','autoload')
55 @lib.argsEQ(1)
56 def modunload(bot, user, chan, realtarget, flags, *args):
57 autoload = flags['autoload']
58 force = flags['force']
59 module = args[0]
60
61 if autoload:
62 bot.parent.cfg.delete('autoloads', module)
63 bot.msg(user, "Unmarked %s for autoloading." % (module))
64 if not ctlmod.isloaded(module):
65 bot.msg(user, "%s is not loaded" % (module))
66 return
67 if len(ctlmod.dependents[module]) > 0:
68 if not force:
69 bot.msg(user, "That module has dependents! Say MODUNLOAD -force %s to unload it and any dependents." % (module))
70 return
71
72 okay = ctlmod.unload(bot.parent, module)
73 if okay:
74 bot.msg(user, "Unloaded %s" % (module))
75 else:
76 bot.msg(user, "Error unloading %s: %r" % (module, okay))
77
78 @lib.hook(needchan=False, glevel=lib.MANAGER)
79 @lib.help("<mod>", "reloads a module")
80 @lib.argsEQ(1)
81 def modreload(bot, user, chan, realtarget, *args):
82 if not ctlmod.isloaded(args[0]):
83 bot.msg(user, "%s is not loaded" % (args[0]))
84 return
85
86 okay = ctlmod.reloadmod(bot.parent, args[0])
87 if okay:
88 bot.msg(user, "Reloaded %s" % (args[0]))
89 else:
90 bot.msg(user, "Error occurred: %r" % (okay))
91
92 @lib.hook(needchan=False, glevel=lib.STAFF)
93 @lib.help(None, "list loaded modules")
94 @lib.argsEQ(0)
95 def modlist(bot, user, chan, realtarget, *args):
96 mods = ctlmod.modules
97 for modname, mod in sorted(mods.items()):
98 bot.msg(user, "- %s (%s) [%s]" % ((modname, mod.__file__, ', '.join(ctlmod.dependents[modname]))))
99 bot.msg(user, "Done.")
100
101 def _whois(user, chan, showglevel=True, showclevel=True):
102 if not user.isauthed():
103 return "not authed."
104
105 fillers = {'auth': user.auth}
106 fmt = "%(auth)s"
107
108 if showglevel and user.glevel >= 1:
109 fillers['glevel'] = user.glevel
110 fmt += " (global access: %(glevel)s)"
111 elif user.glevel >= 1:
112 fmt += " (staff)"
113 elif user.glevel <= -2:
114 fmt += " (ignored)"
115 else:
116 fmt += " (not staff)"
117
118 if (showclevel or showglevel) and chan is not None:
119 clev = chan.levelof(user.auth)
120 if clev >= 1:
121 fillers['clevel'] = (lib.clevs[clev] if clev < len(lib.clevs) else clev)
122 fmt += " (channel access: %(clevel)s)"
123 else:
124 fmt += " (not a channel user)"
125 return fmt % fillers
126
127 @lib.hook(needchan=False, wantchan=True)
128 @lib.help("<user|#auth>", "shows who someone is")
129 @lib.argsEQ(1)
130 def whois(bot, user, chan, realtarget, *args):
131 name = args[0]
132 if name.startswith("#"):
133 target = bot.parent.User(name, name[1:])
134 else:
135 target = bot.parent.user(name, create=False)
136 if target is None:
137 return "I don't know %s." % (args[0])
138 else:
139 return "%s is %s" % (args[0], _whois(target, chan, (user.glevel >= 1), (chan is not None and chan.levelof(user.auth) >= 1)))
140
141 @lib.hook(needchan=False, wantchan=True)
142 @lib.help(None, "shows who you are")
143 def whoami(bot, user, chan, realtarget, *args):
144 return "You are %s" % (_whois(user, chan))
145
146 @lib.hook(needchan=False)
147 @lib.help(None, "tries to read your auth and access level again")
148 def auth(bot, user, chan, realtarget, *args):
149 bot.msg(user, "Okay, give me a second.")
150 bot.conn.send("WHO %s n%%ant,2" % (user))
151
152 @lib.hook(needchan=False, glevel=1)
153 @lib.help(None, "displays length of each msgqueue")
154 def qstat(bot, user, chan, realtarget, *args):
155 bot.fastmsg(user, "Regular: %d -- Slow: %d" % (len(bot.msgqueue), len(bot.slowmsgqueue)))
156
157 @lib.hook(('qclear','cq','clearq','clearqueue'), needchan=False, glevel=lib.ADMIN)
158 @lib.help("[regular|slow]", "clears both or a specific msgqueue")
159 def qclear(bot, user, chan, realtarget, *args):
160 if len(args) == 0:
161 bot.msgqueue = deque()
162 bot.slowmsgqueue = deque()
163 bot.fastmsg(user, "Cleared both msgqueues.")
164 else:
165 if args[0] == 'regular':
166 bot.msgqueue = deque()
167 elif args[0] == 'slow':
168 bot.slowmsgqueue = deque()
169 else:
170 bot.fastmsg(user, "Syntax: QCLEAR [regular|slow]")
171 return #short-circuit
172 bot.fastmsg(user, "Cleared that msgqueue.")