Added ability to define regexp callbacks in a mostly-Privmsg environment.

This commit is contained in:
Jeremy Fincher 2003-08-11 03:34:54 +00:00
parent 60fa81c268
commit f203bca787

View File

@ -42,6 +42,7 @@ from fix import *
import re import re
import new import new
import sets
import time import time
import shlex import shlex
import inspect import inspect
@ -407,7 +408,7 @@ class Privmsg(irclib.IrcCallback):
if s: if s:
recipient = msg.args[0] recipient = msg.args[0]
if ircdb.checkIgnored(msg.prefix, recipient): if ircdb.checkIgnored(msg.prefix, recipient):
debug.printf('Privmsg.doPrivmsg: ignoring.') debug.printf('Privmsg.doPrivmsg: ignoringi %s.' % recipient)
return return
m = self._r.match(s) m = self._r.match(s)
if m and self.isCommand(canonicalName(m.group(1))): if m and self.isCommand(canonicalName(m.group(1))):
@ -474,6 +475,7 @@ class PrivmsgRegexp(Privmsg):
def doPrivmsg(self, irc, msg): def doPrivmsg(self, irc, msg):
if ircdb.checkIgnored(msg.prefix, msg.args[0]): if ircdb.checkIgnored(msg.prefix, msg.args[0]):
debug.msg('PrivmsgRegexp.doPrivmsg: ignoring %s' % msg.args[0])
return return
for (r, method) in self.res: for (r, method) in self.res:
m = r.search(msg.args[1]) m = r.search(msg.args[1])
@ -485,57 +487,40 @@ class PrivmsgRegexp(Privmsg):
self.callCommand(method, irc, msg, m) self.callCommand(method, irc, msg, m)
class Combine(Privmsg): class PrivmsgCommandAndRegexp(Privmsg):
classes = [] # Override in a subclass. flags = re.I
def __getattr__(self, attr): regexps = sets.Set()
for instance in self.instances: def __init__(self):
try: Privmsg.__init__(self)
return getattr(instance, attr) self.res = []
except AttributeError: for name in self.regexps:
pass method = getattr(self, name)
raise AttributeError, attr r = re.compile(method.__doc__, self.flags)
self.res.append((r, method))
def __init__(self, *args, **kwargs): def doPrivmsg(self, irc, msg):
self.instances = [] if ircdb.checkIgnored(msg.prefix, msg.args[0]):
for cls in self.classes: return
self.instances.append(cls(*args, **kwargs)) for (r, method) in self.res:
m = r.search(msg.args[1])
if m:
self.rateLimiter.put(msg)
msg = self.rateLimiter.get()
if msg:
self.callCommand(method, IrcObjectProxyRegexp(irc), msg, m)
s = addressed(irc.nick, msg)
if s:
m = self._r.match(s)
if m and self.isCommand(canonicalName(m.group(1))):
self.rateLimiter.put(msg)
msg = self.rateLimiter.get()
if msg:
args = tokenize(s)
self.Proxy(irc, msg, args)
def __call__(self, irc, msg):
for instance in self.instances:
instance.__call__(irc, msg)
def inFilter(self, irc, msg):
for instance in self.instances:
msg = instance.inFilter(irc, msg)
return msg
def outFilter(self, irc, msg):
for instance in self.instances:
msg = instance.outFilter(irc, msg)
return msg
def isCommand(self, *args, **kwargs):
for instance in self.instances:
if instance.isCommand(*args, **kwargs):
return True
return False
def callCommand(self, f, *args, **kwargs):
for instance in self.instances:
if instance.__class__ == f.im_class:
return instance.callCommand(f, *args, **kwargs)
assert False
def name(self):
return self.__class__.__name__
def reset(self):
for instance in self.instances:
instance.reset()
def die(self):
for instance in self.instances:
instance.die()
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: # vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: