]> jfr.im git - erebus.git/blob - modlib.py
more py3 compat
[erebus.git] / modlib.py
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
5 import sys
6
7 if sys.version_info.major < 3:
8 stringbase = basestring
9 else:
10 stringbase = str
11
12 class error(object):
13 """Used to return an error to the bot core."""
14 def __init__(self, desc):
15 self.errormsg = desc
16 def __nonzero__(self):
17 return False #object will test to False
18 __bool__ = __nonzero__ #py3 compat
19 def __repr__(self):
20 return '<modlib.error %r>' % self.errormsg
21 def __str__(self):
22 return str(self.errormsg)
23
24 class modlib(object):
25 # default (global) access levels
26 OWNER = 100
27 MANAGER = 99
28 ADMIN = 75
29 STAFF = 50
30 AUTHED = 0
31 ANYONE = -1
32 IGNORED = -2
33
34 # (channel) access levels
35 COWNER = 5
36 MASTER = 4
37 OP = 3
38 VOICE = 2
39 KNOWN = 1
40 PUBLIC = 0 #anyone (use glevel to control auth-needed)
41 BANNED = -1
42 # [ 0 1 2 3 4 5 -1]
43 clevs = [None, 'Friend', 'Voice', 'Op', 'Master', 'Owner', None]
44
45 # messages
46 WRONGARGS = "Wrong number of arguments."
47
48 def __init__(self, name):
49 self.hooks = {}
50 self.numhooks = {}
51 self.chanhooks = {}
52 self.helps = []
53 self.parent = None
54
55 self.name = (name.split("."))[-1]
56
57 def modstart(self, parent):
58 #modstart can return a few things...
59 # None: unspecified success
60 # False: unspecified error
61 # modlib.error (or anything else False-y): specified error
62 # True: unspecified success
63 # non-empty string (or anything else True-y): specified success
64 #"specified" values will be printed. unspecified values will result in "OK" or "failed"
65 self.parent = parent
66 for cmd, func in self.hooks.items():
67 self.parent.hook(cmd, func)
68 self.parent.hook("%s.%s" % (self.name, cmd), func)
69 for num, func in self.numhooks.items():
70 self.parent.hooknum(num, func)
71 for chan, func in self.chanhooks.items():
72 self.parent.hookchan(chan, func)
73
74 for func, args, kwargs in self.helps:
75 try:
76 self.mod('help').reghelp(func, *args, **kwargs)
77 except:
78 pass
79 return True
80 def modstop(self, parent):
81 for cmd, func in self.hooks.items():
82 parent.unhook(cmd, func)
83 parent.unhook("%s.%s" % (self.name, cmd), func)
84 for num, func in self.numhooks.items():
85 parent.unhooknum(num, func)
86 for chan, func in self.chanhooks.items():
87 parent.unhookchan(chan, func)
88
89 for func, args, kwargs in self.helps:
90 try:
91 self.mod('help').dereghelp(func, *args, **kwargs)
92 except:
93 pass
94 return True
95
96 def hooknum(self, num):
97 def realhook(func):
98 self.numhooks[str(num)] = func
99 if self.parent is not None:
100 self.parent.hooknum(str(num), func)
101 return func
102 return realhook
103
104 def hookchan(self, chan, glevel=ANYONE, clevel=PUBLIC):
105 def realhook(func):
106 self.chanhooks[chan] = func
107 if self.parent is not None:
108 self.parent.hookchan(chan, func)
109 return func
110 return realhook
111
112 def hook(self, cmd=None, needchan=True, glevel=ANYONE, clevel=PUBLIC, wantchan=None):
113 if wantchan is None: wantchan = needchan
114 _cmd = cmd #save this since it gets wiped out...
115 def realhook(func):
116 cmd = _cmd #...and restore it
117 if cmd is None:
118 cmd = func.__name__ # default to function name
119 if isinstance(cmd, stringbase):
120 cmd = (cmd,)
121
122 func.needchan = needchan
123 func.wantchan = wantchan
124 func.reqglevel = glevel
125 func.reqclevel = clevel
126 func.cmd = cmd
127 func.module = func.__module__.split('.')[1]
128
129 for c in cmd:
130 self.hooks[c] = func
131 if self.parent is not None:
132 self.parent.hook(c, func)
133 self.parent.hook("%s.%s" % (self.name, c), func)
134 return func
135 return realhook
136
137 def mod(self, modname):
138 if self.parent is not None:
139 return self.parent.module(modname)
140 else:
141 return error('unknown parent')
142
143 def argsEQ(self, num):
144 def realhook(func):
145 def checkargs(bot, user, chan, realtarget, *args):
146 if len(args) == num:
147 return func(bot, user, chan, realtarget, *args)
148 else:
149 bot.msg(user, self.WRONGARGS)
150 checkargs.__name__ = func.__name__
151 checkargs.__module__ = func.__module__
152 return checkargs
153 return realhook
154
155 def argsGE(self, num):
156 def realhook(func):
157 def checkargs(bot, user, chan, realtarget, *args):
158 if len(args) >= num:
159 return func(bot, user, chan, realtarget, *args)
160 else:
161 bot.msg(user, self.WRONGARGS)
162 checkargs.__name__ = func.__name__
163 checkargs.__module__ = func.__module__
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