]> jfr.im git - erebus.git/blame - modlib.py
chanops - why is this here? its not used
[erebus.git] / modlib.py
CommitLineData
931c88a4 1# Erebus IRC bot - Author: John Runyon
4477123d 2# vim: fileencoding=utf-8
931c88a4 3# module helper functions, see modules/modtest.py for usage
4# This file is released into the public domain; see http://unlicense.org/
5
a28e2ae9 6import sys
4f8abd95 7from functools import wraps
a28e2ae9 8
9if sys.version_info.major < 3:
10 stringbase = basestring
11else:
12 stringbase = str
13
43151ead 14"""Used to return an error to the bot core."""
e4255e70 15class error(object):
16 def __init__(self, desc):
17 self.errormsg = desc
18 def __nonzero__(self):
19 return False #object will test to False
71ef8273 20 __bool__ = __nonzero__ #py3 compat
e4255e70 21 def __repr__(self):
22 return '<modlib.error %r>' % self.errormsg
23 def __str__(self):
e3878612 24 return str(self.errormsg)
e4255e70 25
6c70d82c 26class modlib(object):
839d2b35 27 # default (global) access levels
69071d33 28 OWNER = 100
29 MANAGER = 99
30 ADMIN = 75
31 STAFF = 50
25bf8fc5 32 KNOWN = 1
43151ead
JR
33 AUTHED = 0 # Users which have are known to be authed
34 ANYONE = -1 # non-authed users have glevel set to -1
35 IGNORED = -2 # The default reqglevel is ANYONE, so any commands will be ignored from IGNORED users unless the command reglevel=-2
25bf8fc5
JR
36 glevs = {
37 'OWNER': OWNER,
38 'MANAGER': MANAGER,
39 'ADMIN': ADMIN,
40 'STAFF': STAFF,
41 'KNOWN': KNOWN,
42 'AUTHED': AUTHED,
43 'ANYONE': ANYONE,
44 'IGNORED': IGNORED,
45 }
931c88a4 46
839d2b35 47 # (channel) access levels
a290635a 48 COWNER = 5
69071d33 49 MASTER = 4
50 OP = 3
51 VOICE = 2
25bf8fc5 52 #KNOWN = 1 is set above by glevels
43151ead
JR
53 PUBLIC = 0 # Anyone (use glevel to control whether auth is needed)
54 BANNED = -1 # The default reqclevel is PUBLIC, so any commands which needchan will be ignored from BANNED users unless the command reqclevel=-1
f164fd1c 55 # [ 0 1 2 3 4 5 -1]
56 clevs = [None, 'Friend', 'Voice', 'Op', 'Master', 'Owner', None]
839d2b35 57
d431e543 58 # messages
59 WRONGARGS = "Wrong number of arguments."
60
6c70d82c 61 def __init__(self, name):
db50981b 62 self.hooks = {}
61b67f0f 63 self.numhooks = {}
2a1a69a6 64 self.chanhooks = {}
0f8352dd 65 self.helps = []
db50981b 66 self.parent = None
67
a8553c45 68 self.name = (name.split("."))[-1]
6c70d82c 69
70 def modstart(self, parent):
a62d0d18 71 #modstart can return a few things...
72 # None: unspecified success
73 # False: unspecified error
74 # modlib.error (or anything else False-y): specified error
75 # True: unspecified success
76 # non-empty string (or anything else True-y): specified success
77 #"specified" values will be printed. unspecified values will result in "OK" or "failed"
6c70d82c 78 self.parent = parent
a28e2ae9 79 for cmd, func in self.hooks.items():
6c70d82c 80 self.parent.hook(cmd, func)
a290635a 81 self.parent.hook("%s.%s" % (self.name, cmd), func)
a28e2ae9 82 for num, func in self.numhooks.items():
61b67f0f 83 self.parent.hooknum(num, func)
a28e2ae9 84 for chan, func in self.chanhooks.items():
2a1a69a6 85 self.parent.hookchan(chan, func)
0f8352dd 86
87 for func, args, kwargs in self.helps:
88 try:
89 self.mod('help').reghelp(func, *args, **kwargs)
90 except:
91 pass
d431e543 92 return True
db50981b 93 def modstop(self, parent):
a28e2ae9 94 for cmd, func in self.hooks.items():
1bdecfcd 95 parent.unhook(cmd, func)
96 parent.unhook("%s.%s" % (self.name, cmd), func)
a28e2ae9 97 for num, func in self.numhooks.items():
1bdecfcd 98 parent.unhooknum(num, func)
a28e2ae9 99 for chan, func in self.chanhooks.items():
1bdecfcd 100 parent.unhookchan(chan, func)
0f8352dd 101
102 for func, args, kwargs in self.helps:
103 try:
104 self.mod('help').dereghelp(func, *args, **kwargs)
105 except:
106 pass
d431e543 107 return True
6c70d82c 108
61b67f0f 109 def hooknum(self, num):
110 def realhook(func):
36411de9 111 self.numhooks[str(num)] = func
61b67f0f 112 if self.parent is not None:
36411de9 113 self.parent.hooknum(str(num), func)
61b67f0f 114 return func
115 return realhook
116
2a1a69a6 117 def hookchan(self, chan, glevel=ANYONE, clevel=PUBLIC):
118 def realhook(func):
119 self.chanhooks[chan] = func
120 if self.parent is not None:
121 self.parent.hookchan(chan, func)
122 return func
123 return realhook
124
827ec8f0 125 def hook(self, cmd=None, needchan=True, glevel=ANYONE, clevel=PUBLIC, wantchan=None):
126 if wantchan is None: wantchan = needchan
3a8b7b5f 127 _cmd = cmd #save this since it gets wiped out...
6c70d82c 128 def realhook(func):
3a8b7b5f 129 cmd = _cmd #...and restore it
130 if cmd is None:
131 cmd = func.__name__ # default to function name
a28e2ae9 132 if isinstance(cmd, stringbase):
0f8352dd 133 cmd = (cmd,)
3a8b7b5f 134
68dff4aa
JR
135 if clevel > self.PUBLIC and not needchan:
136 raise Exception('clevel must be left at default if needchan is False')
137
839d2b35 138 func.needchan = needchan
827ec8f0 139 func.wantchan = wantchan
839d2b35 140 func.reqglevel = glevel
141 func.reqclevel = clevel
0f8352dd 142 func.cmd = cmd
179c06a9 143 func.module = func.__module__.split('.')[1]
839d2b35 144
9ea2be43 145 for c in cmd:
146 self.hooks[c] = func
147 if self.parent is not None:
148 self.parent.hook(c, func)
a290635a 149 self.parent.hook("%s.%s" % (self.name, c), func)
6c70d82c 150 return func
151 return realhook
d431e543 152
36411de9 153 def mod(self, modname):
154 if self.parent is not None:
155 return self.parent.module(modname)
156 else:
157 return error('unknown parent')
158
d431e543 159 def argsEQ(self, num):
160 def realhook(func):
4f8abd95 161 @wraps(func)
d431e543 162 def checkargs(bot, user, chan, realtarget, *args):
163 if len(args) == num:
164 return func(bot, user, chan, realtarget, *args)
165 else:
166 bot.msg(user, self.WRONGARGS)
167 return checkargs
168 return realhook
169
170 def argsGE(self, num):
171 def realhook(func):
4f8abd95 172 @wraps(func)
d431e543 173 def checkargs(bot, user, chan, realtarget, *args):
174 if len(args) >= num:
175 return func(bot, user, chan, realtarget, *args)
176 else:
177 bot.msg(user, self.WRONGARGS)
178 return checkargs
179 return realhook
b670c2f4 180
984fc310 181 def help(self, *args, **kwargs):
0f8352dd 182 """help(syntax, shorthelp, longhelp?, more lines longhelp?, cmd=...?)
b670c2f4 183 Example:
184 help("<user> <pass>", "login")
185 ^ Help will only be one line. Command name determined based on function name.
186 help("<user> <level>", "add a user", cmd=("adduser", "useradd"))
187 ^ Help will be listed under ADDUSER; USERADD will say "alias for adduser"
188 help(None, "do stuff", "This command is really complicated.")
189 ^ Command takes no args. Short description (in overall HELP listing) is "do stuff".
190 Long description (HELP <command>) will say "<command> - do stuff", newline, "This command is really complicated."
191 """
0f8352dd 192 def realhook(func):
193 if self.parent is not None:
194 try:
195 self.mod('help').reghelp(func, *args, **kwargs)
196 except:
197 pass
71ef8273 198 self.helps.append((func, args, kwargs))
0f8352dd 199 return func
200 return realhook