]> jfr.im git - erebus.git/blame - modules/control.py
misc fixes
[erebus.git] / modules / control.py
CommitLineData
e2b30093 1# Erebus IRC bot - Author: Erebus Team
4477123d 2# vim: fileencoding=utf-8
4df64299 3# Various highly recommended "control" commands.
e2b30093 4# This file is released into the public domain; see http://unlicense.org/
5
6# module info
7modinfo = {
8 'author': 'Erebus Team',
9 'license': 'public domain',
fa93b933 10 'compatible': [0],
e2b30093 11 'depends': [],
a62d0d18 12 'softdeps': ['help'],
e2b30093 13}
14
15# preamble
16import modlib
17lib = modlib.modlib(__name__)
18modstart = lib.modstart
19modstop = lib.modstop
20
21# module code
d371d194 22import sys, os
e2b30093 23import ctlmod
32a54ded 24from collections import deque
e2b30093 25
26
f7725bee 27@lib.hook(('die','restart'), needchan=False, glevel=lib.MANAGER)
5f03d045 28@lib.help(None, "stops the bot")
fb20be7c 29def die(bot, user, chan, realtarget, *args):
889eeb24 30 quitmsg = ' '.join(args)
a28e2ae9 31 for botitem in bot.parent.bots.values():
889eeb24 32 bot.conn.send("QUIT :Restarting. %s" % (quitmsg))
e2b30093 33 sys.exit(0)
34 os._exit(0)
4df64299 35
fb20be7c 36@lib.hook(needchan=False, glevel=lib.MANAGER)
5f03d045 37@lib.help("<mod>", "loads a module")
4df64299 38@lib.argsEQ(1)
fb20be7c 39def modload(bot, user, chan, realtarget, *args):
4df64299 40 okay = ctlmod.load(bot.parent, args[0])
41 if okay:
42 bot.msg(user, "Loaded %s" % (args[0]))
43 else:
44 bot.msg(user, "Error loading %s: %r" % (args[0], okay))
45
fb20be7c 46@lib.hook(needchan=False, glevel=lib.MANAGER)
d3417a2c 47@lib.help("<mod> [FORCE]", "unloads a module", "will refuse to unload a module which is depended on by others", "unless you specify FORCE.")
b0910717 48@lib.argsGE(1)
fb20be7c 49def modunload(bot, user, chan, realtarget, *args):
68dff4aa
JR
50 if not ctlmod.isloaded(args[0]):
51 bot.msg(user, "%s is not loaded" % (args[0]))
52 return
b0910717 53 if len(ctlmod.dependents[args[0]]) > 0:
54 if len(args) == 1 or args[1].lower() != "force":
55 bot.msg(user, "That module has dependents! Say MODUNLOAD %s FORCE to unload it and any dependents." % (args[0]))
56 return
68dff4aa 57
4df64299 58 okay = ctlmod.unload(bot.parent, args[0])
59 if okay:
60 bot.msg(user, "Unloaded %s" % (args[0]))
61 else:
62 bot.msg(user, "Error unloading %s: %r" % (args[0], okay))
63
fb20be7c 64@lib.hook(needchan=False, glevel=lib.MANAGER)
5f03d045 65@lib.help("<mod>", "reloads a module")
4df64299 66@lib.argsEQ(1)
fb20be7c 67def modreload(bot, user, chan, realtarget, *args):
68dff4aa
JR
68 if not ctlmod.isloaded(args[0]):
69 bot.msg(user, "%s is not loaded" % (args[0]))
70 return
71
4df64299 72 okay = ctlmod.reloadmod(bot.parent, args[0])
73 if okay:
74 bot.msg(user, "Reloaded %s" % (args[0]))
75 else:
76 bot.msg(user, "Error occurred: %r" % (okay))
77
fb20be7c 78@lib.hook(needchan=False, glevel=lib.STAFF)
5f03d045 79@lib.help(None, "list loaded modules")
4df64299 80@lib.argsEQ(0)
fb20be7c 81def modlist(bot, user, chan, realtarget, *args):
4df64299 82 mods = ctlmod.modules
a28e2ae9 83 for modname, mod in mods.items():
53fff692 84 bot.msg(user, "- %s (%s) [%s]" % ((modname, mod.__file__, ', '.join(ctlmod.dependents[modname]))))
4df64299 85 bot.msg(user, "Done.")
fb20be7c 86
72e5bb4e 87def _whois(user, chan, showglevel=True, showclevel=True):
fb20be7c 88 if not user.isauthed():
72e5bb4e 89 return "not authed."
fb20be7c 90
91 fillers = {'auth': user.auth}
72e5bb4e 92 fmt = "%(auth)s"
fb20be7c 93
72e5bb4e 94 if showglevel and user.glevel >= 1:
95 fillers['glevel'] = user.glevel
96 fmt += " (global access: %(glevel)s)"
97 elif user.glevel >= 1:
98 fmt += " (staff)"
fb20be7c 99 else:
100 fmt += " (not staff)"
101
72e5bb4e 102 if showclevel and chan is not None:
68dff4aa
JR
103 clev = chan.levelof(user.auth)
104 if clev >= 1:
105 fillers['clevel'] = (lib.clevs[clev] if clev < len(lib.clevs) else clev)
72e5bb4e 106 fmt += " (channel access: %(clevel)s)"
107 else:
108 fmt += " (not a channel user)"
109 return fmt % fillers
110
827ec8f0 111@lib.hook(needchan=False, wantchan=True)
b4e3e62e 112@lib.help("<user|#auth>", "shows who someone is")
72e5bb4e 113@lib.argsEQ(1)
114def whois(bot, user, chan, realtarget, *args):
3fb492b9 115 name = args[0]
116 if name.startswith("#"):
117 target = bot.parent.User(name, name[1:])
118 else:
119 target = bot.parent.user(name, create=False)
72e5bb4e 120 if target is None:
a5e7905a 121 return "I don't know %s." % (args[0])
fb20be7c 122 else:
a5e7905a 123 return "%s is %s" % (args[0], _whois(target, chan, (user.glevel >= 1), (chan is not None and chan.levelof(user.auth) >= 1)))
72e5bb4e 124
363febf1 125@lib.hook(needchan=False, wantchan=True)
5f03d045 126@lib.help(None, "shows who you are")
72e5bb4e 127def whoami(bot, user, chan, realtarget, *args):
a5e7905a 128 return "You are %s" % (_whois(user, chan))
32a54ded 129
b319b837 130@lib.hook(needchan=False)
131@lib.help(None, "tries to read your auth and access level again")
132def auth(bot, user, chan, realtarget, *args):
133 bot.msg(user, "Okay, give me a second.")
134 bot.conn.send("WHO %s n%%ant,2" % (user))
135
32a54ded 136@lib.hook(needchan=False, glevel=1)
5f03d045 137@lib.help(None, "displays length of each msgqueue")
32a54ded 138def qstat(bot, user, chan, realtarget, *args):
139 bot.fastmsg(user, "Regular: %d -- Slow: %d" % (len(bot.msgqueue), len(bot.slowmsgqueue)))
140
74feb80e 141@lib.hook(('qclear','cq','clearq','clearqueue'), needchan=False, glevel=lib.ADMIN)
e6b60193 142@lib.help("[regular|slow]", "clears both or a specific msgqueue")
32a54ded 143def qclear(bot, user, chan, realtarget, *args):
144 if len(args) == 0:
145 bot.msgqueue = deque()
146 bot.slowmsgqueue = deque()
147 bot.fastmsg(user, "Cleared both msgqueues.")
148 else:
149 if args[0] == 'regular':
150 bot.msgqueue = deque()
151 elif args[0] == 'slow':
152 bot.slowmsgqueue = deque()
153 else:
154 bot.fastmsg(user, "Syntax: QCLEAR [regular|slow]")
155 return #short-circuit
156 bot.fastmsg(user, "Cleared that msgqueue.")
193806d5 157
158@lib.hook(needchan=False, wantchan=True, glevel=lib.ADMIN)
159@lib.help("<nick> <message>", "inject a line as though it came from <nick>", "note that this injects lines, not commands", "ex: INJECT DimeCadmium !WHOAMI")
160def inject(bot, user, chan, realtarget, *args):
161 targetuser = bot.parent.user(args[0], create=False)
162 if targetuser is None:
163 bot.msg(user, "User is unknown.")
164 return
165 if targetuser.glevel > user.glevel:
166 bot.msg(user, "That user has a higher access level than you.")
167 return
168
169 if chan is not None:
170 bot.parsemsg(bot.parent.user(args[0], create=False), str(chan), ' '.join(args[1:]))
171 else:
172 bot.parsemsg(bot.parent.user(args[0], create=False), str(bot), ' '.join(args[1:]))