]> jfr.im git - erebus.git/blame - modules/control.py
update module loading system
[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',
9 'compatible': [1],
10 'depends': [],
11}
12
13# preamble
14import modlib
15lib = modlib.modlib(__name__)
16modstart = lib.modstart
17modstop = lib.modstop
18
19# module code
d371d194 20import sys, os
e2b30093 21import ctlmod
32a54ded 22from collections import deque
e2b30093 23
24
f7725bee 25@lib.hook(('die','restart'), needchan=False, glevel=lib.MANAGER)
5f03d045 26@lib.help(None, "stops the bot")
fb20be7c 27def die(bot, user, chan, realtarget, *args):
574ce192 28 for botitem in bot.parent.bots.itervalues():
29 for chan in botitem.chans:
30 chan.fastmsg("Bot is restarting! %s" % ' '.join(args))
31 bot.conn.send("QUIT :Restarting.")
e2b30093 32 sys.exit(0)
33 os._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)
5f03d045 46@lib.help("<mod>", "unloads a module")
4df64299 47@lib.argsEQ(1)
fb20be7c 48def modunload(bot, user, chan, realtarget, *args):
4df64299 49 okay = ctlmod.unload(bot.parent, args[0])
50 if okay:
51 bot.msg(user, "Unloaded %s" % (args[0]))
52 else:
53 bot.msg(user, "Error unloading %s: %r" % (args[0], okay))
54
fb20be7c 55@lib.hook(needchan=False, glevel=lib.MANAGER)
5f03d045 56@lib.help("<mod>", "reloads a module")
4df64299 57@lib.argsEQ(1)
fb20be7c 58def modreload(bot, user, chan, realtarget, *args):
4df64299 59 okay = ctlmod.reloadmod(bot.parent, args[0])
60 if okay:
61 bot.msg(user, "Reloaded %s" % (args[0]))
62 else:
63 bot.msg(user, "Error occurred: %r" % (okay))
64
fb20be7c 65@lib.hook(needchan=False, glevel=lib.STAFF)
5f03d045 66@lib.help(None, "list loaded modules")
4df64299 67@lib.argsEQ(0)
fb20be7c 68def modlist(bot, user, chan, realtarget, *args):
4df64299 69 mods = ctlmod.modules
70 for mod in mods.itervalues():
83ed3882 71 bot.msg(user, "- %s (%s)" % ((mod.__name__.split(".", 1))[1], mod.__file__))
4df64299 72 bot.msg(user, "Done.")
fb20be7c 73
72e5bb4e 74def _whois(user, chan, showglevel=True, showclevel=True):
fb20be7c 75 if not user.isauthed():
72e5bb4e 76 return "not authed."
fb20be7c 77
78 fillers = {'auth': user.auth}
72e5bb4e 79 fmt = "%(auth)s"
fb20be7c 80
72e5bb4e 81 if showglevel and user.glevel >= 1:
82 fillers['glevel'] = user.glevel
83 fmt += " (global access: %(glevel)s)"
84 elif user.glevel >= 1:
85 fmt += " (staff)"
fb20be7c 86 else:
87 fmt += " (not staff)"
88
72e5bb4e 89 if showclevel and chan is not None:
90 if chan.levelof(user.auth) >= 1:
91 fillers['clevel'] = chan.levelof(user.auth)
92 fmt += " (channel access: %(clevel)s)"
93 else:
94 fmt += " (not a channel user)"
95 return fmt % fillers
96
97@lib.hook(needchan=False)
5f03d045 98@lib.help("<user>", "shows who someone is")
72e5bb4e 99@lib.argsEQ(1)
100def whois(bot, user, chan, realtarget, *args):
101 target = bot.parent.user(args[0], create=False)
102 if target is None:
103 bot.msg(user, "I don't know %s." % (args[0]))
fb20be7c 104 else:
72e5bb4e 105 bot.msg(user, "%s is %s" % (args[0], _whois(target, chan, (user.glevel >= 1), (chan is not None and chan.levelof(user.auth) >= 1))))
106
107@lib.hook(needchan=False)
5f03d045 108@lib.help(None, "shows who you are")
72e5bb4e 109def whoami(bot, user, chan, realtarget, *args):
110 bot.msg(user, "You are %s" % (_whois(user, chan)))
32a54ded 111
b319b837 112@lib.hook(needchan=False)
113@lib.help(None, "tries to read your auth and access level again")
114def auth(bot, user, chan, realtarget, *args):
115 bot.msg(user, "Okay, give me a second.")
116 bot.conn.send("WHO %s n%%ant,2" % (user))
117
32a54ded 118@lib.hook(needchan=False, glevel=1)
5f03d045 119@lib.help(None, "displays length of each msgqueue")
32a54ded 120def qstat(bot, user, chan, realtarget, *args):
121 bot.fastmsg(user, "Regular: %d -- Slow: %d" % (len(bot.msgqueue), len(bot.slowmsgqueue)))
122
123@lib.hook(needchan=False, glevel=lib.ADMIN)
5f03d045 124@lib.help("[regular|slow]", "clears both or a specific msgqueue")
32a54ded 125def qclear(bot, user, chan, realtarget, *args):
126 if len(args) == 0:
127 bot.msgqueue = deque()
128 bot.slowmsgqueue = deque()
129 bot.fastmsg(user, "Cleared both msgqueues.")
130 else:
131 if args[0] == 'regular':
132 bot.msgqueue = deque()
133 elif args[0] == 'slow':
134 bot.slowmsgqueue = deque()
135 else:
136 bot.fastmsg(user, "Syntax: QCLEAR [regular|slow]")
137 return #short-circuit
138 bot.fastmsg(user, "Cleared that msgqueue.")