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