]> jfr.im git - erebus.git/blame - modlib.py
urls: .strip title
[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
43151ead
JR
32 AUTHED = 0 # Users which have are known to be authed
33 ANYONE = -1 # non-authed users have glevel set to -1
34 IGNORED = -2 # The default reqglevel is ANYONE, so any commands will be ignored from IGNORED users unless the command reglevel=-2
931c88a4 35
839d2b35 36 # (channel) access levels
a290635a 37 COWNER = 5
69071d33 38 MASTER = 4
39 OP = 3
40 VOICE = 2
41 KNOWN = 1
43151ead
JR
42 PUBLIC = 0 # Anyone (use glevel to control whether auth is needed)
43 BANNED = -1 # The default reqclevel is PUBLIC, so any commands which needchan will be ignored from BANNED users unless the command reqclevel=-1
f164fd1c 44 # [ 0 1 2 3 4 5 -1]
45 clevs = [None, 'Friend', 'Voice', 'Op', 'Master', 'Owner', None]
839d2b35 46
d431e543 47 # messages
48 WRONGARGS = "Wrong number of arguments."
49
6c70d82c 50 def __init__(self, name):
db50981b 51 self.hooks = {}
61b67f0f 52 self.numhooks = {}
2a1a69a6 53 self.chanhooks = {}
0f8352dd 54 self.helps = []
db50981b 55 self.parent = None
56
a8553c45 57 self.name = (name.split("."))[-1]
6c70d82c 58
59 def modstart(self, parent):
a62d0d18 60 #modstart can return a few things...
61 # None: unspecified success
62 # False: unspecified error
63 # modlib.error (or anything else False-y): specified error
64 # True: unspecified success
65 # non-empty string (or anything else True-y): specified success
66 #"specified" values will be printed. unspecified values will result in "OK" or "failed"
6c70d82c 67 self.parent = parent
a28e2ae9 68 for cmd, func in self.hooks.items():
6c70d82c 69 self.parent.hook(cmd, func)
a290635a 70 self.parent.hook("%s.%s" % (self.name, cmd), func)
a28e2ae9 71 for num, func in self.numhooks.items():
61b67f0f 72 self.parent.hooknum(num, func)
a28e2ae9 73 for chan, func in self.chanhooks.items():
2a1a69a6 74 self.parent.hookchan(chan, func)
0f8352dd 75
76 for func, args, kwargs in self.helps:
77 try:
78 self.mod('help').reghelp(func, *args, **kwargs)
79 except:
80 pass
d431e543 81 return True
db50981b 82 def modstop(self, parent):
a28e2ae9 83 for cmd, func in self.hooks.items():
1bdecfcd 84 parent.unhook(cmd, func)
85 parent.unhook("%s.%s" % (self.name, cmd), func)
a28e2ae9 86 for num, func in self.numhooks.items():
1bdecfcd 87 parent.unhooknum(num, func)
a28e2ae9 88 for chan, func in self.chanhooks.items():
1bdecfcd 89 parent.unhookchan(chan, func)
0f8352dd 90
91 for func, args, kwargs in self.helps:
92 try:
93 self.mod('help').dereghelp(func, *args, **kwargs)
94 except:
95 pass
d431e543 96 return True
6c70d82c 97
61b67f0f 98 def hooknum(self, num):
99 def realhook(func):
36411de9 100 self.numhooks[str(num)] = func
61b67f0f 101 if self.parent is not None:
36411de9 102 self.parent.hooknum(str(num), func)
61b67f0f 103 return func
104 return realhook
105
2a1a69a6 106 def hookchan(self, chan, glevel=ANYONE, clevel=PUBLIC):
107 def realhook(func):
108 self.chanhooks[chan] = func
109 if self.parent is not None:
110 self.parent.hookchan(chan, func)
111 return func
112 return realhook
113
827ec8f0 114 def hook(self, cmd=None, needchan=True, glevel=ANYONE, clevel=PUBLIC, wantchan=None):
115 if wantchan is None: wantchan = needchan
3a8b7b5f 116 _cmd = cmd #save this since it gets wiped out...
6c70d82c 117 def realhook(func):
3a8b7b5f 118 cmd = _cmd #...and restore it
119 if cmd is None:
120 cmd = func.__name__ # default to function name
a28e2ae9 121 if isinstance(cmd, stringbase):
0f8352dd 122 cmd = (cmd,)
3a8b7b5f 123
68dff4aa
JR
124 if clevel > self.PUBLIC and not needchan:
125 raise Exception('clevel must be left at default if needchan is False')
126
839d2b35 127 func.needchan = needchan
827ec8f0 128 func.wantchan = wantchan
839d2b35 129 func.reqglevel = glevel
130 func.reqclevel = clevel
0f8352dd 131 func.cmd = cmd
179c06a9 132 func.module = func.__module__.split('.')[1]
839d2b35 133
9ea2be43 134 for c in cmd:
135 self.hooks[c] = func
136 if self.parent is not None:
137 self.parent.hook(c, func)
a290635a 138 self.parent.hook("%s.%s" % (self.name, c), func)
6c70d82c 139 return func
140 return realhook
d431e543 141
36411de9 142 def mod(self, modname):
143 if self.parent is not None:
144 return self.parent.module(modname)
145 else:
146 return error('unknown parent')
147
d431e543 148 def argsEQ(self, num):
149 def realhook(func):
4f8abd95 150 @wraps(func)
d431e543 151 def checkargs(bot, user, chan, realtarget, *args):
152 if len(args) == num:
153 return func(bot, user, chan, realtarget, *args)
154 else:
155 bot.msg(user, self.WRONGARGS)
156 return checkargs
157 return realhook
158
159 def argsGE(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
b670c2f4 169
984fc310 170 def help(self, *args, **kwargs):
0f8352dd 171 """help(syntax, shorthelp, longhelp?, more lines longhelp?, cmd=...?)
b670c2f4 172 Example:
173 help("<user> <pass>", "login")
174 ^ Help will only be one line. Command name determined based on function name.
175 help("<user> <level>", "add a user", cmd=("adduser", "useradd"))
176 ^ Help will be listed under ADDUSER; USERADD will say "alias for adduser"
177 help(None, "do stuff", "This command is really complicated.")
178 ^ Command takes no args. Short description (in overall HELP listing) is "do stuff".
179 Long description (HELP <command>) will say "<command> - do stuff", newline, "This command is really complicated."
180 """
0f8352dd 181 def realhook(func):
182 if self.parent is not None:
183 try:
184 self.mod('help').reghelp(func, *args, **kwargs)
185 except:
186 pass
71ef8273 187 self.helps.append((func, args, kwargs))
0f8352dd 188 return func
189 return realhook