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