]>
jfr.im git - erebus.git/blob - 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/
7 from functools
import wraps
9 if sys
.version_info
.major
< 3:
10 stringbase
= basestring
14 """Used to return an error to the bot core."""
16 def __init__(self
, desc
):
18 def __nonzero__(self
):
19 return False #object will test to False
20 __bool__
= __nonzero__
#py3 compat
22 return '<modlib.error %r>' % self
.errormsg
24 return str(self
.errormsg
)
27 # default (global) access levels
33 AUTHED
= 0 # Users which have are known to be authed
34 ANYONE
= -1 # non-authed users have glevel set to -1
35 IGNORED
= -2 # The default reqglevel is ANYONE, so any commands will be ignored from IGNORED users unless the command reglevel=-2
47 # (channel) access levels
52 #KNOWN = 1 is set above by glevels
53 PUBLIC
= 0 # Anyone (use glevel to control whether auth is needed)
54 BANNED
= -1 # The default reqclevel is PUBLIC, so any commands which needchan will be ignored from BANNED users unless the command reqclevel=-1
56 clevs
= [None, 'Friend', 'Voice', 'Op', 'Master', 'Owner', None]
59 WRONGARGS
= "Wrong number of arguments."
61 def __init__(self
, name
):
68 self
.name
= (name
.split("."))[-1]
70 def modstart(self
, parent
):
71 #modstart can return a few things...
72 # None: unspecified success
73 # False: unspecified error
74 # modlib.error (or anything else False-y): specified error
75 # True: unspecified success
76 # non-empty string (or anything else True-y): specified success
77 #"specified" values will be printed. unspecified values will result in "OK" or "failed"
79 for cmd
, func
in self
.hooks
.items():
80 self
.parent
.hook(cmd
, func
)
81 self
.parent
.hook("%s.%s" % (self
.name
, cmd
), func
)
82 for num
, func
in self
.numhooks
.items():
83 self
.parent
.hooknum(num
, func
)
84 for chan
, func
in self
.chanhooks
.items():
85 self
.parent
.hookchan(chan
, func
)
87 for func
, args
, kwargs
in self
.helps
:
89 self
.mod('help').reghelp(func
, *args
, **kwargs
)
93 def modstop(self
, parent
):
94 for cmd
, func
in self
.hooks
.items():
95 parent
.unhook(cmd
, func
)
96 parent
.unhook("%s.%s" % (self
.name
, cmd
), func
)
97 for num
, func
in self
.numhooks
.items():
98 parent
.unhooknum(num
, func
)
99 for chan
, func
in self
.chanhooks
.items():
100 parent
.unhookchan(chan
, func
)
102 for func
, args
, kwargs
in self
.helps
:
104 self
.mod('help').dereghelp(func
, *args
, **kwargs
)
109 def hooknum(self
, num
):
111 self
.numhooks
[str(num
)] = func
112 if self
.parent
is not None:
113 self
.parent
.hooknum(str(num
), func
)
117 def hookchan(self
, chan
, glevel
=ANYONE
, clevel
=PUBLIC
):
119 self
.chanhooks
[chan
] = func
120 if self
.parent
is not None:
121 self
.parent
.hookchan(chan
, func
)
125 def hook(self
, cmd
=None, needchan
=True, glevel
=ANYONE
, clevel
=PUBLIC
, wantchan
=None):
126 if wantchan
is None: wantchan
= needchan
127 _cmd
= cmd
#save this since it gets wiped out...
129 cmd
= _cmd
#...and restore it
131 cmd
= func
.__name
__ # default to function name
132 if isinstance(cmd
, stringbase
):
135 if clevel
> self
.PUBLIC
and not needchan
:
136 raise Exception('clevel must be left at default if needchan is False')
138 func
.needchan
= needchan
139 func
.wantchan
= wantchan
140 func
.reqglevel
= glevel
141 func
.reqclevel
= clevel
143 func
.module
= func
.__module
__.split('.')[1]
147 if self
.parent
is not None:
148 self
.parent
.hook(c
, func
)
149 self
.parent
.hook("%s.%s" % (self
.name
, c
), func
)
153 def mod(self
, modname
):
154 if self
.parent
is not None:
155 return self
.parent
.module(modname
)
157 return error('unknown parent')
159 def argsEQ(self
, num
):
162 def checkargs(bot
, user
, chan
, realtarget
, *args
):
164 return func(bot
, user
, chan
, realtarget
, *args
)
166 bot
.msg(user
, self
.WRONGARGS
)
170 def argsGE(self
, num
):
173 def checkargs(bot
, user
, chan
, realtarget
, *args
):
175 return func(bot
, user
, chan
, realtarget
, *args
)
177 bot
.msg(user
, self
.WRONGARGS
)
181 def help(self
, *args
, **kwargs
):
182 """help(syntax, shorthelp, longhelp?, more lines longhelp?, cmd=...?)
184 help("<user> <pass>", "login")
185 ^ Help will only be one line. Command name determined based on function name.
186 help("<user> <level>", "add a user", cmd=("adduser", "useradd"))
187 ^ Help will be listed under ADDUSER; USERADD will say "alias for adduser"
188 help(None, "do stuff", "This command is really complicated.")
189 ^ Command takes no args. Short description (in overall HELP listing) is "do stuff".
190 Long description (HELP <command>) will say "<command> - do stuff", newline, "This command is really complicated."
193 if self
.parent
is not None:
195 self
.mod('help').reghelp(func
, *args
, **kwargs
)
198 self
.helps
.append((func
, args
, kwargs
))