]>
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
25 # (channel) access levels
31 PUBLIC
= 0 #anyone (use glevel to control auth-needed)
34 clevs
= [None, 'Friend', 'Voice', 'Op', 'Master', 'Owner', None]
37 WRONGARGS
= "Wrong number of arguments."
39 def __init__(self
, name
):
46 self
.name
= (name
.split("."))[-1]
48 def modstart(self
, parent
):
49 #modstart can return a few things...
50 # None: unspecified success
51 # False: unspecified error
52 # modlib.error (or anything else False-y): specified error
53 # True: unspecified success
54 # non-empty string (or anything else True-y): specified success
55 #"specified" values will be printed. unspecified values will result in "OK" or "failed"
57 for cmd
, func
in self
.hooks
.iteritems():
58 self
.parent
.hook(cmd
, func
)
59 self
.parent
.hook("%s.%s" % (self
.name
, cmd
), func
)
60 for num
, func
in self
.numhooks
.iteritems():
61 self
.parent
.hooknum(num
, func
)
62 for chan
, func
in self
.chanhooks
.iteritems():
63 self
.parent
.hookchan(chan
, func
)
65 for func
, args
, kwargs
in self
.helps
:
67 self
.mod('help').reghelp(func
, *args
, **kwargs
)
71 def modstop(self
, parent
):
72 for cmd
, func
in self
.hooks
.iteritems():
73 parent
.unhook(cmd
, func
)
74 parent
.unhook("%s.%s" % (self
.name
, cmd
), func
)
75 for num
, func
in self
.numhooks
.iteritems():
76 parent
.unhooknum(num
, func
)
77 for chan
, func
in self
.chanhooks
.iteritems():
78 parent
.unhookchan(chan
, func
)
80 for func
, args
, kwargs
in self
.helps
:
82 self
.mod('help').dereghelp(func
, *args
, **kwargs
)
87 def hooknum(self
, num
):
89 self
.numhooks
[str(num
)] = func
90 if self
.parent
is not None:
91 self
.parent
.hooknum(str(num
), func
)
95 def hookchan(self
, chan
, glevel
=ANYONE
, clevel
=PUBLIC
):
97 self
.chanhooks
[chan
] = func
98 if self
.parent
is not None:
99 self
.parent
.hookchan(chan
, func
)
103 def hook(self
, cmd
=None, needchan
=True, glevel
=ANYONE
, clevel
=PUBLIC
, wantchan
=None):
104 if wantchan
is None: wantchan
= needchan
105 _cmd
= cmd
#save this since it gets wiped out...
107 cmd
= _cmd
#...and restore it
109 cmd
= func
.__name
__ # default to function name
110 if isinstance(cmd
, basestring
):
113 func
.needchan
= needchan
114 func
.wantchan
= wantchan
115 func
.reqglevel
= glevel
116 func
.reqclevel
= clevel
118 func
.module
= func
.__module
__.split('.')[1]
122 if self
.parent
is not None:
123 self
.parent
.hook(c
, func
)
124 self
.parent
.hook("%s.%s" % (self
.name
, c
), func
)
128 def mod(self
, modname
):
129 if self
.parent
is not None:
130 return self
.parent
.module(modname
)
132 return error('unknown parent')
134 def argsEQ(self
, num
):
136 def checkargs(bot
, user
, chan
, realtarget
, *args
):
138 return func(bot
, user
, chan
, realtarget
, *args
)
140 bot
.msg(user
, self
.WRONGARGS
)
141 checkargs
.__name
__ = func
.__name
__
142 checkargs
.__module
__ = func
.__module
__
146 def argsGE(self
, num
):
148 def checkargs(bot
, user
, chan
, realtarget
, *args
):
150 return func(bot
, user
, chan
, realtarget
, *args
)
152 bot
.msg(user
, self
.WRONGARGS
)
153 checkargs
.__name
__ = func
.__name
__
154 checkargs
.__module
__ = func
.__module
__
158 def help(self
, *args
, **kwargs
):
159 """help(syntax, shorthelp, longhelp?, more lines longhelp?, cmd=...?)
161 help("<user> <pass>", "login")
162 ^ Help will only be one line. Command name determined based on function name.
163 help("<user> <level>", "add a user", cmd=("adduser", "useradd"))
164 ^ Help will be listed under ADDUSER; USERADD will say "alias for adduser"
165 help(None, "do stuff", "This command is really complicated.")
166 ^ Command takes no args. Short description (in overall HELP listing) is "do stuff".
167 Long description (HELP <command>) will say "<command> - do stuff", newline, "This command is really complicated."
170 if self
.parent
is not None:
172 self
.mod('help').reghelp(func
, *args
, **kwargs
)
175 self
.helps
.append((func
,args
,kwargs
))