]> jfr.im git - erebus.git/blame - modules/channel_admin.py
update comments
[erebus.git] / modules / channel_admin.py
CommitLineData
f6386fa7
JR
1# Erebus IRC bot - Author: Erebus Team
2# vim: fileencoding=utf-8
bac69af4 3# Channel-access management
f6386fa7
JR
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',
10 'compatible': [0], # compatible module API versions
11 'depends': [], # other modules required to work properly?
12 'softdeps': ['help'], # modules which are preferred but not required
13}
14# note: softdeps will be loaded before this module, IF not disabled in the configuration (autoload.module = 0) (and if it exists)
15# however, if it is disabled it will be silently ignored, and if it is unloaded at runtime it won't cause this one to unload.
16#
17# basically, softdeps are things this module will use if available, but does not require (no errors will occur if it's not loaded)
18# for example, @lib.help() will attempt to use the help module, but swallow errors if it is not loaded
19
20# preamble
21import modlib
22lib = modlib.modlib(__name__)
23modstart = lib.modstart
24modstop = lib.modstop
25
26# module code
27
28@lib.hook(clevel=lib.FRIEND)
29@lib.help(None, "rejoin a channel (if the bot has been kicked)")
30def rejoin(bot, user, chan, realtarget, *args):
4d971790 31 chan.bot.join(str(chan))
023239aa 32 bot.msg(user, "Rejoined %s." % (chan))
f6386fa7
JR
33
34
35def _resolve_user(s):
36 if s.startswith("#"):
37 return lib.parent.User(s, s[1:])
38 else:
39 return lib.parent.user(s, create=False)
40
41
42def _resolve_level(s):
43 s = s.lower()
44 levels = ['unknown'] + [x.lower() for x in lib.clevs if x is not None]
45 if s in levels:
46 return levels.index(s)
47 try:
48 return int(s)
49 except:
50 pass
51
52
53@lib.hook(('clevel','setlevel','chanlev'), clevel=lib.FRIEND)
54@lib.help('<nick|#auth> <level>', "adds or sets a user's channel access level", "Levels: Unknown, " + ', '.join([x for x in lib.clevs if x]))
55@lib.argsEQ(2)
56def clevel(bot, user, chan, realtarget, *args):
57 target = _resolve_user(args[0])
58 level = _resolve_level(args[1])
59 user_clevel = chan.levelof(user.auth)
60
61 if target is None:
62 return "User not found (try #auth)"
63 if target.auth is None:
64 return "User is not authed"
65 if level is None or level < 0 or level > lib.COWNER:
66 return "Level is unknown"
67
68 target_clevel = chan.levelof(target.auth)
69
70 if not (target == user and level == 0) and user_clevel != lib.COWNER:
71 if user_clevel < lib.MASTER:
72 return "I'm afraid I can't let you do that. You can only reset your own level to 0."
73 if user_clevel <= target_clevel:
74 return "I'm afraid I can't let you do that. Your current access level is not higher than theirs."
75 if user_clevel <= level:
76 return "I'm afraid I can't let you do that. Your current access level is not higher than you are trying to set."
77
2c58b913
JR
78 if chan.setlevel(target.auth, level):
79 return "Set #%s channel level to %s" % (target.auth, args[1])
80 else:
81 return "An error occurred!"
f6386fa7
JR
82
83
39b26b08 84@lib.hook(('forceclevel','fclevel'), glevel=lib.MANAGER)
f6386fa7
JR
85@lib.help('<level>', "sets your own clevel on a channel")
86@lib.argsEQ(1)
87def forceclevel(bot, user, chan, realtarget, *args):
88 target = user.auth
89 level = _resolve_level(args[0])
2c58b913
JR
90 if chan.setlevel(target, level):
91 return 'Your level on %s has been set to %d' % (chan, level)
92 else:
93 return "An error occurred!"