]>
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/
6 def __init__(self
, desc
):
9 return False #object will test to False
11 return '<modlib.error %r>' % self
.errormsg
13 return str(self
.errormsg
)
16 # default (global) access levels
24 # (channel) access levels
30 PUBLIC
= 0 #anyone (use glevel to control auth-needed)
33 WRONGARGS
= "Wrong number of arguments."
35 def __init__(self
, name
):
42 self
.name
= (name
.split("."))[-1]
44 def modstart(self
, parent
):
45 #modstart can return a few things...
46 # None: unspecified success
47 # False: unspecified error
48 # modlib.error (or anything else False-y): specified error
49 # True: unspecified success
50 # non-empty string (or anything else True-y): specified success
51 #"specified" values will be printed. unspecified values will result in "OK" or "failed"
53 for cmd
, func
in self
.hooks
.iteritems():
54 self
.parent
.hook(cmd
, func
)
55 self
.parent
.hook("%s.%s" % (self
.name
, cmd
), func
)
56 for num
, func
in self
.numhooks
.iteritems():
57 self
.parent
.hooknum(num
, func
)
58 for chan
, func
in self
.chanhooks
.iteritems():
59 self
.parent
.hookchan(chan
, func
)
61 for func
, args
, kwargs
in self
.helps
:
63 self
.mod('help').reghelp(func
, *args
, **kwargs
)
67 def modstop(self
, parent
):
68 for cmd
, func
in self
.hooks
.iteritems():
69 parent
.unhook(cmd
, func
)
70 parent
.unhook("%s.%s" % (self
.name
, cmd
), func
)
71 for num
, func
in self
.numhooks
.iteritems():
72 parent
.unhooknum(num
, func
)
73 for chan
, func
in self
.chanhooks
.iteritems():
74 parent
.unhookchan(chan
, func
)
76 for func
, args
, kwargs
in self
.helps
:
78 self
.mod('help').dereghelp(func
, *args
, **kwargs
)
83 def hooknum(self
, num
):
85 self
.numhooks
[str(num
)] = func
86 if self
.parent
is not None:
87 self
.parent
.hooknum(str(num
), func
)
91 def hookchan(self
, chan
, glevel
=ANYONE
, clevel
=PUBLIC
):
93 self
.chanhooks
[chan
] = func
94 if self
.parent
is not None:
95 self
.parent
.hookchan(chan
, func
)
99 def hook(self
, cmd
=None, needchan
=True, glevel
=ANYONE
, clevel
=PUBLIC
):
100 _cmd
= cmd
#save this since it gets wiped out...
102 cmd
= _cmd
#...and restore it
104 cmd
= func
.__name
__ # default to function name
105 if isinstance(cmd
, basestring
):
108 func
.needchan
= needchan
109 func
.reqglevel
= glevel
110 func
.reqclevel
= clevel
112 func
.module
= func
.__module
__.split('.')[1]
116 if self
.parent
is not None:
117 self
.parent
.hook(c
, func
)
118 self
.parent
.hook("%s.%s" % (self
.name
, c
), func
)
122 def mod(self
, modname
):
123 if self
.parent
is not None:
124 return self
.parent
.module(modname
)
126 return error('unknown parent')
128 def argsEQ(self
, num
):
130 def checkargs(bot
, user
, chan
, realtarget
, *args
):
132 return func(bot
, user
, chan
, realtarget
, *args
)
134 bot
.msg(user
, self
.WRONGARGS
)
135 checkargs
.__name
__ = func
.__name
__
136 checkargs
.__module
__ = func
.__module
__
140 def argsGE(self
, num
):
142 def checkargs(bot
, user
, chan
, realtarget
, *args
):
144 return func(bot
, user
, chan
, realtarget
, *args
)
146 bot
.msg(user
, self
.WRONGARGS
)
147 checkargs
.__name
__ = func
.__name
__
148 checkargs
.__module
__ = func
.__module
__
152 def help(self
, *args
, **kwargs
):
153 """help(syntax, shorthelp, longhelp?, more lines longhelp?, cmd=...?)
155 help("<user> <pass>", "login")
156 ^ Help will only be one line. Command name determined based on function name.
157 help("<user> <level>", "add a user", cmd=("adduser", "useradd"))
158 ^ Help will be listed under ADDUSER; USERADD will say "alias for adduser"
159 help(None, "do stuff", "This command is really complicated.")
160 ^ Command takes no args. Short description (in overall HELP listing) is "do stuff".
161 Long description (HELP <command>) will say "<command> - do stuff", newline, "This command is really complicated."
164 if self
.parent
is not None:
166 self
.mod('help').reghelp(func
, *args
, **kwargs
)
169 self
.helps
.append((func
,args
,kwargs
))