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