]>
jfr.im git - erebus.git/blob - 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/
7 if sys
.version_info
.major
< 3:
8 stringbase
= basestring
13 """Used to return an error to the bot core."""
14 def __init__(self
, desc
):
16 def __nonzero__(self
):
17 return False #object will test to False
18 __bool__
= __nonzero__
#py3 compat
20 return '<modlib.error %r>' % self
.errormsg
22 return str(self
.errormsg
)
25 # default (global) access levels
34 # (channel) access levels
40 PUBLIC
= 0 #anyone (use glevel to control auth-needed)
43 clevs
= [None, 'Friend', 'Voice', 'Op', 'Master', 'Owner', None]
46 WRONGARGS
= "Wrong number of arguments."
48 def __init__(self
, name
):
55 self
.name
= (name
.split("."))[-1]
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"
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
)
74 for func
, args
, kwargs
in self
.helps
:
76 self
.mod('help').reghelp(func
, *args
, **kwargs
)
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
)
89 for func
, args
, kwargs
in self
.helps
:
91 self
.mod('help').dereghelp(func
, *args
, **kwargs
)
96 def hooknum(self
, num
):
98 self
.numhooks
[str(num
)] = func
99 if self
.parent
is not None:
100 self
.parent
.hooknum(str(num
), func
)
104 def hookchan(self
, chan
, glevel
=ANYONE
, clevel
=PUBLIC
):
106 self
.chanhooks
[chan
] = func
107 if self
.parent
is not None:
108 self
.parent
.hookchan(chan
, func
)
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...
116 cmd
= _cmd
#...and restore it
118 cmd
= func
.__name
__ # default to function name
119 if isinstance(cmd
, stringbase
):
122 func
.needchan
= needchan
123 func
.wantchan
= wantchan
124 func
.reqglevel
= glevel
125 func
.reqclevel
= clevel
127 func
.module
= func
.__module
__.split('.')[1]
131 if self
.parent
is not None:
132 self
.parent
.hook(c
, func
)
133 self
.parent
.hook("%s.%s" % (self
.name
, c
), func
)
137 def mod(self
, modname
):
138 if self
.parent
is not None:
139 return self
.parent
.module(modname
)
141 return error('unknown parent')
143 def argsEQ(self
, num
):
145 def checkargs(bot
, user
, chan
, realtarget
, *args
):
147 return func(bot
, user
, chan
, realtarget
, *args
)
149 bot
.msg(user
, self
.WRONGARGS
)
150 checkargs
.__name
__ = func
.__name
__
151 checkargs
.__module
__ = func
.__module
__
155 def argsGE(self
, num
):
157 def checkargs(bot
, user
, chan
, realtarget
, *args
):
159 return func(bot
, user
, chan
, realtarget
, *args
)
161 bot
.msg(user
, self
.WRONGARGS
)
162 checkargs
.__name
__ = func
.__name
__
163 checkargs
.__module
__ = func
.__module
__
167 def help(self
, *args
, **kwargs
):
168 """help(syntax, shorthelp, longhelp?, more lines longhelp?, cmd=...?)
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."
179 if self
.parent
is not None:
181 self
.mod('help').reghelp(func
, *args
, **kwargs
)
184 self
.helps
.append((func
, args
, kwargs
))