]> jfr.im git - erebus.git/blame - modules/control.py
add nitterize module
[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:
94 fillers['glevel'] = user.glevel
95 fmt += " (global access: %(glevel)s)"
96 elif user.glevel >= 1:
97 fmt += " (staff)"
fb20be7c 98 else:
99 fmt += " (not staff)"
100
905f5930 101 if (showclevel or showglevel) and chan is not None:
68dff4aa
JR
102 clev = chan.levelof(user.auth)
103 if clev >= 1:
104 fillers['clevel'] = (lib.clevs[clev] if clev < len(lib.clevs) else clev)
72e5bb4e 105 fmt += " (channel access: %(clevel)s)"
106 else:
107 fmt += " (not a channel user)"
108 return fmt % fillers
109
827ec8f0 110@lib.hook(needchan=False, wantchan=True)
b4e3e62e 111@lib.help("<user|#auth>", "shows who someone is")
72e5bb4e 112@lib.argsEQ(1)
113def whois(bot, user, chan, realtarget, *args):
3fb492b9 114 name = args[0]
115 if name.startswith("#"):
116 target = bot.parent.User(name, name[1:])
117 else:
118 target = bot.parent.user(name, create=False)
72e5bb4e 119 if target is None:
a5e7905a 120 return "I don't know %s." % (args[0])
fb20be7c 121 else:
a5e7905a 122 return "%s is %s" % (args[0], _whois(target, chan, (user.glevel >= 1), (chan is not None and chan.levelof(user.auth) >= 1)))
72e5bb4e 123
363febf1 124@lib.hook(needchan=False, wantchan=True)
5f03d045 125@lib.help(None, "shows who you are")
72e5bb4e 126def whoami(bot, user, chan, realtarget, *args):
a5e7905a 127 return "You are %s" % (_whois(user, chan))
32a54ded 128
b319b837 129@lib.hook(needchan=False)
130@lib.help(None, "tries to read your auth and access level again")
131def auth(bot, user, chan, realtarget, *args):
132 bot.msg(user, "Okay, give me a second.")
133 bot.conn.send("WHO %s n%%ant,2" % (user))
134
32a54ded 135@lib.hook(needchan=False, glevel=1)
5f03d045 136@lib.help(None, "displays length of each msgqueue")
32a54ded 137def qstat(bot, user, chan, realtarget, *args):
138 bot.fastmsg(user, "Regular: %d -- Slow: %d" % (len(bot.msgqueue), len(bot.slowmsgqueue)))
139
74feb80e 140@lib.hook(('qclear','cq','clearq','clearqueue'), needchan=False, glevel=lib.ADMIN)
e6b60193 141@lib.help("[regular|slow]", "clears both or a specific msgqueue")
32a54ded 142def qclear(bot, user, chan, realtarget, *args):
143 if len(args) == 0:
144 bot.msgqueue = deque()
145 bot.slowmsgqueue = deque()
146 bot.fastmsg(user, "Cleared both msgqueues.")
147 else:
148 if args[0] == 'regular':
149 bot.msgqueue = deque()
150 elif args[0] == 'slow':
151 bot.slowmsgqueue = deque()
152 else:
153 bot.fastmsg(user, "Syntax: QCLEAR [regular|slow]")
154 return #short-circuit
155 bot.fastmsg(user, "Cleared that msgqueue.")