]> jfr.im git - erebus.git/blame - modules/control.py
control - show ignored status in whois
[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)
4df64299 34
fb20be7c 35@lib.hook(needchan=False, glevel=lib.MANAGER)
5f03d045 36@lib.help("<mod>", "loads a module")
4df64299 37@lib.argsEQ(1)
fb20be7c 38def modload(bot, user, chan, realtarget, *args):
4df64299 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
fb20be7c 45@lib.hook(needchan=False, glevel=lib.MANAGER)
d3417a2c 46@lib.help("<mod> [FORCE]", "unloads a module", "will refuse to unload a module which is depended on by others", "unless you specify FORCE.")
b0910717 47@lib.argsGE(1)
fb20be7c 48def modunload(bot, user, chan, realtarget, *args):
68dff4aa
JR
49 if not ctlmod.isloaded(args[0]):
50 bot.msg(user, "%s is not loaded" % (args[0]))
51 return
b0910717 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
68dff4aa 56
4df64299 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
fb20be7c 63@lib.hook(needchan=False, glevel=lib.MANAGER)
5f03d045 64@lib.help("<mod>", "reloads a module")
4df64299 65@lib.argsEQ(1)
fb20be7c 66def modreload(bot, user, chan, realtarget, *args):
68dff4aa
JR
67 if not ctlmod.isloaded(args[0]):
68 bot.msg(user, "%s is not loaded" % (args[0]))
69 return
70
4df64299 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
fb20be7c 77@lib.hook(needchan=False, glevel=lib.STAFF)
5f03d045 78@lib.help(None, "list loaded modules")
4df64299 79@lib.argsEQ(0)
fb20be7c 80def modlist(bot, user, chan, realtarget, *args):
4df64299 81 mods = ctlmod.modules
958b8081 82 for modname, mod in sorted(mods.items()):
53fff692 83 bot.msg(user, "- %s (%s) [%s]" % ((modname, mod.__file__, ', '.join(ctlmod.dependents[modname]))))
4df64299 84 bot.msg(user, "Done.")
fb20be7c 85
72e5bb4e 86def _whois(user, chan, showglevel=True, showclevel=True):
fb20be7c 87 if not user.isauthed():
72e5bb4e 88 return "not authed."
fb20be7c 89
90 fillers = {'auth': user.auth}
72e5bb4e 91 fmt = "%(auth)s"
fb20be7c 92
72e5bb4e 93 if showglevel and user.glevel >= 1:
44aea37b
JR
94 fillers['glevel'] = user.glevel
95 fmt += " (global access: %(glevel)s)"
72e5bb4e 96 elif user.glevel >= 1:
97 fmt += " (staff)"
44aea37b
JR
98 elif user.glevel <= -2:
99 fmt += " (ignored)"
fb20be7c 100 else:
101 fmt += " (not staff)"
102
905f5930 103 if (showclevel or showglevel) and chan is not None:
68dff4aa
JR
104 clev = chan.levelof(user.auth)
105 if clev >= 1:
106 fillers['clevel'] = (lib.clevs[clev] if clev < len(lib.clevs) else clev)
72e5bb4e 107 fmt += " (channel access: %(clevel)s)"
108 else:
109 fmt += " (not a channel user)"
110 return fmt % fillers
111
827ec8f0 112@lib.hook(needchan=False, wantchan=True)
b4e3e62e 113@lib.help("<user|#auth>", "shows who someone is")
72e5bb4e 114@lib.argsEQ(1)
115def whois(bot, user, chan, realtarget, *args):
3fb492b9 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)
72e5bb4e 121 if target is None:
a5e7905a 122 return "I don't know %s." % (args[0])
fb20be7c 123 else:
a5e7905a 124 return "%s is %s" % (args[0], _whois(target, chan, (user.glevel >= 1), (chan is not None and chan.levelof(user.auth) >= 1)))
72e5bb4e 125
363febf1 126@lib.hook(needchan=False, wantchan=True)
5f03d045 127@lib.help(None, "shows who you are")
72e5bb4e 128def whoami(bot, user, chan, realtarget, *args):
a5e7905a 129 return "You are %s" % (_whois(user, chan))
32a54ded 130
b319b837 131@lib.hook(needchan=False)
132@lib.help(None, "tries to read your auth and access level again")
133def 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
32a54ded 137@lib.hook(needchan=False, glevel=1)
5f03d045 138@lib.help(None, "displays length of each msgqueue")
32a54ded 139def qstat(bot, user, chan, realtarget, *args):
140 bot.fastmsg(user, "Regular: %d -- Slow: %d" % (len(bot.msgqueue), len(bot.slowmsgqueue)))
141
74feb80e 142@lib.hook(('qclear','cq','clearq','clearqueue'), needchan=False, glevel=lib.ADMIN)
e6b60193 143@lib.help("[regular|slow]", "clears both or a specific msgqueue")
32a54ded 144def 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.")