]> jfr.im git - erebus.git/blob - modlib.py
721fa2d9b375d8a96a92d9bb17ace6d866096d66
[erebus.git] / modlib.py
1 # Erebus IRC bot - Author: John Runyon
2 # vim: fileencoding=utf-8
3 # module helper functions, see modules/modtest.py for usage
4 # This file is released into the public domain; see http://unlicense.org/
5
6 import sys
7 from functools import wraps
8
9 if sys.version_info.major < 3:
10 stringbase = basestring
11 else:
12 stringbase = str
13
14 class error(object):
15 """Used to return an error to the bot core."""
16 def __init__(self, desc):
17 self.errormsg = desc
18 def __nonzero__(self):
19 return False #object will test to False
20 __bool__ = __nonzero__ #py3 compat
21 def __repr__(self):
22 return '<modlib.error %r>' % self.errormsg
23 def __str__(self):
24 return str(self.errormsg)
25
26 class modlib(object):
27 # default (global) access levels
28 OWNER = 100
29 MANAGER = 99
30 ADMIN = 75
31 STAFF = 50
32 AUTHED = 0
33 ANYONE = -1
34 IGNORED = -2
35
36 # (channel) access levels
37 COWNER = 5
38 MASTER = 4
39 OP = 3
40 VOICE = 2
41 KNOWN = 1
42 PUBLIC = 0 #anyone (use glevel to control auth-needed)
43 BANNED = -1
44 # [ 0 1 2 3 4 5 -1]
45 clevs = [None, 'Friend', 'Voice', 'Op', 'Master', 'Owner', None]
46
47 # messages
48 WRONGARGS = "Wrong number of arguments."
49
50 def __init__(self, name):
51 self.hooks = {}
52 self.numhooks = {}
53 self.chanhooks = {}
54 self.helps = []
55 self.parent = None
56
57 self.name = (name.split("."))[-1]
58
59 def modstart(self, parent):
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"
67 self.parent = parent
68 for cmd, func in self.hooks.items():
69 self.parent.hook(cmd, func)
70 self.parent.hook("%s.%s" % (self.name, cmd), func)
71 for num, func in self.numhooks.items():
72 self.parent.hooknum(num, func)
73 for chan, func in self.chanhooks.items():
74 self.parent.hookchan(chan, func)
75
76 for func, args, kwargs in self.helps:
77 try:
78 self.mod('help').reghelp(func, *args, **kwargs)
79 except:
80 pass
81 return True
82 def modstop(self, parent):
83 for cmd, func in self.hooks.items():
84 parent.unhook(cmd, func)
85 parent.unhook("%s.%s" % (self.name, cmd), func)
86 for num, func in self.numhooks.items():
87 parent.unhooknum(num, func)
88 for chan, func in self.chanhooks.items():
89 parent.unhookchan(chan, func)
90
91 for func, args, kwargs in self.helps:
92 try:
93 self.mod('help').dereghelp(func, *args, **kwargs)
94 except:
95 pass
96 return True
97
98 def hooknum(self, num):
99 def realhook(func):
100 self.numhooks[str(num)] = func
101 if self.parent is not None:
102 self.parent.hooknum(str(num), func)
103 return func
104 return realhook
105
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
114 def hook(self, cmd=None, needchan=True, glevel=ANYONE, clevel=PUBLIC, wantchan=None):
115 if wantchan is None: wantchan = needchan
116 _cmd = cmd #save this since it gets wiped out...
117 def realhook(func):
118 cmd = _cmd #...and restore it
119 if cmd is None:
120 cmd = func.__name__ # default to function name
121 if isinstance(cmd, stringbase):
122 cmd = (cmd,)
123
124 func.needchan = needchan
125 func.wantchan = wantchan
126 func.reqglevel = glevel
127 func.reqclevel = clevel
128 func.cmd = cmd
129 func.module = func.__module__.split('.')[1]
130
131 for c in cmd:
132 self.hooks[c] = func
133 if self.parent is not None:
134 self.parent.hook(c, func)
135 self.parent.hook("%s.%s" % (self.name, c), func)
136 return func
137 return realhook
138
139 def mod(self, modname):
140 if self.parent is not None:
141 return self.parent.module(modname)
142 else:
143 return error('unknown parent')
144
145 def argsEQ(self, num):
146 def realhook(func):
147 @wraps(func)
148 def checkargs(bot, user, chan, realtarget, *args):
149 if len(args) == num:
150 return func(bot, user, chan, realtarget, *args)
151 else:
152 bot.msg(user, self.WRONGARGS)
153 return checkargs
154 return realhook
155
156 def argsGE(self, num):
157 def realhook(func):
158 @wraps(func)
159 def checkargs(bot, user, chan, realtarget, *args):
160 if len(args) >= num:
161 return func(bot, user, chan, realtarget, *args)
162 else:
163 bot.msg(user, self.WRONGARGS)
164 return checkargs
165 return realhook
166
167 def help(self, *args, **kwargs):
168 """help(syntax, shorthelp, longhelp?, more lines longhelp?, cmd=...?)
169 Example:
170 help("<user> <pass>", "login")
171 ^ Help will only be one line. Command name determined based on function name.
172 help("<user> <level>", "add a user", cmd=("adduser", "useradd"))
173 ^ Help will be listed under ADDUSER; USERADD will say "alias for adduser"
174 help(None, "do stuff", "This command is really complicated.")
175 ^ Command takes no args. Short description (in overall HELP listing) is "do stuff".
176 Long description (HELP <command>) will say "<command> - do stuff", newline, "This command is really complicated."
177 """
178 def realhook(func):
179 if self.parent is not None:
180 try:
181 self.mod('help').reghelp(func, *args, **kwargs)
182 except:
183 pass
184 self.helps.append((func, args, kwargs))
185 return func
186 return realhook