]> jfr.im git - erebus.git/blame - modlib.py
weather - dont fail when wunderground screws up time
[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
a28e2ae9 5import sys
6
7if sys.version_info.major < 3:
8 stringbase = basestring
9else:
10 stringbase = str
11
e4255e70 12class error(object):
9221e6f6 13 """Used to return an error to the bot core."""
e4255e70 14 def __init__(self, desc):
15 self.errormsg = desc
16 def __nonzero__(self):
17 return False #object will test to False
71ef8273 18 __bool__ = __nonzero__ #py3 compat
e4255e70 19 def __repr__(self):
20 return '<modlib.error %r>' % self.errormsg
21 def __str__(self):
e3878612 22 return str(self.errormsg)
e4255e70 23
6c70d82c 24class modlib(object):
839d2b35 25 # default (global) access levels
69071d33 26 OWNER = 100
27 MANAGER = 99
28 ADMIN = 75
29 STAFF = 50
30 AUTHED = 0
31 ANYONE = -1
8b8ff79e 32 IGNORED = -2
931c88a4 33
839d2b35 34 # (channel) access levels
a290635a 35 COWNER = 5
69071d33 36 MASTER = 4
37 OP = 3
38 VOICE = 2
39 KNOWN = 1
40 PUBLIC = 0 #anyone (use glevel to control auth-needed)
8b8ff79e 41 BANNED = -1
f164fd1c 42 # [ 0 1 2 3 4 5 -1]
43 clevs = [None, 'Friend', 'Voice', 'Op', 'Master', 'Owner', None]
839d2b35 44
d431e543 45 # messages
46 WRONGARGS = "Wrong number of arguments."
47
6c70d82c 48 def __init__(self, name):
db50981b 49 self.hooks = {}
61b67f0f 50 self.numhooks = {}
2a1a69a6 51 self.chanhooks = {}
0f8352dd 52 self.helps = []
db50981b 53 self.parent = None
54
a8553c45 55 self.name = (name.split("."))[-1]
6c70d82c 56
57 def modstart(self, parent):
a62d0d18 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"
6c70d82c 65 self.parent = parent
a28e2ae9 66 for cmd, func in self.hooks.items():
6c70d82c 67 self.parent.hook(cmd, func)
a290635a 68 self.parent.hook("%s.%s" % (self.name, cmd), func)
a28e2ae9 69 for num, func in self.numhooks.items():
61b67f0f 70 self.parent.hooknum(num, func)
a28e2ae9 71 for chan, func in self.chanhooks.items():
2a1a69a6 72 self.parent.hookchan(chan, func)
0f8352dd 73
74 for func, args, kwargs in self.helps:
75 try:
76 self.mod('help').reghelp(func, *args, **kwargs)
77 except:
78 pass
d431e543 79 return True
db50981b 80 def modstop(self, parent):
a28e2ae9 81 for cmd, func in self.hooks.items():
1bdecfcd 82 parent.unhook(cmd, func)
83 parent.unhook("%s.%s" % (self.name, cmd), func)
a28e2ae9 84 for num, func in self.numhooks.items():
1bdecfcd 85 parent.unhooknum(num, func)
a28e2ae9 86 for chan, func in self.chanhooks.items():
1bdecfcd 87 parent.unhookchan(chan, func)
0f8352dd 88
89 for func, args, kwargs in self.helps:
90 try:
91 self.mod('help').dereghelp(func, *args, **kwargs)
92 except:
93 pass
d431e543 94 return True
6c70d82c 95
61b67f0f 96 def hooknum(self, num):
97 def realhook(func):
36411de9 98 self.numhooks[str(num)] = func
61b67f0f 99 if self.parent is not None:
36411de9 100 self.parent.hooknum(str(num), func)
61b67f0f 101 return func
102 return realhook
103
2a1a69a6 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
827ec8f0 112 def hook(self, cmd=None, needchan=True, glevel=ANYONE, clevel=PUBLIC, wantchan=None):
113 if wantchan is None: wantchan = needchan
3a8b7b5f 114 _cmd = cmd #save this since it gets wiped out...
6c70d82c 115 def realhook(func):
3a8b7b5f 116 cmd = _cmd #...and restore it
117 if cmd is None:
118 cmd = func.__name__ # default to function name
a28e2ae9 119 if isinstance(cmd, stringbase):
0f8352dd 120 cmd = (cmd,)
3a8b7b5f 121
839d2b35 122 func.needchan = needchan
827ec8f0 123 func.wantchan = wantchan
839d2b35 124 func.reqglevel = glevel
125 func.reqclevel = clevel
0f8352dd 126 func.cmd = cmd
179c06a9 127 func.module = func.__module__.split('.')[1]
839d2b35 128
9ea2be43 129 for c in cmd:
130 self.hooks[c] = func
131 if self.parent is not None:
132 self.parent.hook(c, func)
a290635a 133 self.parent.hook("%s.%s" % (self.name, c), func)
6c70d82c 134 return func
135 return realhook
d431e543 136
36411de9 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
d431e543 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)
3a8b7b5f 150 checkargs.__name__ = func.__name__
11ef6fed 151 checkargs.__module__ = func.__module__
d431e543 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)
3a8b7b5f 162 checkargs.__name__ = func.__name__
11ef6fed 163 checkargs.__module__ = func.__module__
d431e543 164 return checkargs
165 return realhook
b670c2f4 166
984fc310 167 def help(self, *args, **kwargs):
0f8352dd 168 """help(syntax, shorthelp, longhelp?, more lines longhelp?, cmd=...?)
b670c2f4 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 """
0f8352dd 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
71ef8273 184 self.helps.append((func, args, kwargs))
0f8352dd 185 return func
186 return realhook