mirror of
https://github.com/Mikaela/Limnoria.git
synced 2025-01-26 20:14:20 +01:00
Reworked some callCommand stuff to centralize some code.
This commit is contained in:
parent
457de48be4
commit
5ace516fb1
@ -316,15 +316,16 @@ class IrcObjectProxy:
|
|||||||
self.finalEvaled = True
|
self.finalEvaled = True
|
||||||
originalName = self.args.pop(0)
|
originalName = self.args.pop(0)
|
||||||
name = canonicalName(originalName)
|
name = canonicalName(originalName)
|
||||||
callback = self.findCallback(name)
|
cb = self.findCallback(name)
|
||||||
try:
|
try:
|
||||||
if callback is not None:
|
if cb is not None:
|
||||||
anticap = ircdb.makeAntiCapability(name)
|
anticap = ircdb.makeAntiCapability(name)
|
||||||
#debug.printf('Checking for %s' % anticap)
|
#debug.printf('Checking for %s' % anticap)
|
||||||
if ircdb.checkCapability(self.msg.prefix, anticap):
|
if ircdb.checkCapability(self.msg.prefix, anticap):
|
||||||
#debug.printf('Being prevented with anticap')
|
#debug.printf('Being prevented with anticap')
|
||||||
debug.msg('Preventing %s from calling %s' % \
|
debug.msg('Preventing %s from calling %s' % \
|
||||||
(self.msg.nick, name), 'normal')
|
(self.msg.nick, name), 'normal')
|
||||||
|
self.error(self.msg, conf.replyNoCapability % name)
|
||||||
return
|
return
|
||||||
recipient = self.msg.args[0]
|
recipient = self.msg.args[0]
|
||||||
if ircutils.isChannel(recipient):
|
if ircutils.isChannel(recipient):
|
||||||
@ -335,8 +336,13 @@ class IrcObjectProxy:
|
|||||||
debug.msg('Preventing %s from calling %s' % \
|
debug.msg('Preventing %s from calling %s' % \
|
||||||
(self.msg.nick, name), 'normal')
|
(self.msg.nick, name), 'normal')
|
||||||
return
|
return
|
||||||
command = getattr(callback, name)
|
command = getattr(cb, name)
|
||||||
callback.callCommand(command, self, self.msg, self.args)
|
if cb.threaded:
|
||||||
|
t = CommandThread(cb.callCommand, command,
|
||||||
|
self, self.msg, self.args)
|
||||||
|
t.start()
|
||||||
|
else:
|
||||||
|
cb.callCommand(command, self, self.msg, self.args)
|
||||||
else:
|
else:
|
||||||
self.args.insert(0, originalName)
|
self.args.insert(0, originalName)
|
||||||
if not isinstance(self.irc, irclib.Irc):
|
if not isinstance(self.irc, irclib.Irc):
|
||||||
@ -384,7 +390,7 @@ class IrcObjectProxy:
|
|||||||
# " (more)" to the end, so that's 7 more characters.
|
# " (more)" to the end, so that's 7 more characters.
|
||||||
# 512 - 51 == 461.
|
# 512 - 51 == 461.
|
||||||
s = ircutils.safeArgument(s)
|
s = ircutils.safeArgument(s)
|
||||||
allowedLength = 463 - len(self.irc.prefix)
|
allowedLength = 459 - len(self.irc.prefix)
|
||||||
msgs = textwrap.wrap(s, allowedLength-30) # -30 is for "nick:"
|
msgs = textwrap.wrap(s, allowedLength-30) # -30 is for "nick:"
|
||||||
msgs.reverse()
|
msgs.reverse()
|
||||||
response = msgs.pop()
|
response = msgs.pop()
|
||||||
@ -425,7 +431,7 @@ class CommandThread(threading.Thread):
|
|||||||
"""Just does some extra logging and error-recovery for commands that need
|
"""Just does some extra logging and error-recovery for commands that need
|
||||||
to run in threads.
|
to run in threads.
|
||||||
"""
|
"""
|
||||||
def __init__(self, command, irc, msg, args):
|
def __init__(self, callCommand, command, irc, msg, args):
|
||||||
self.command = command
|
self.command = command
|
||||||
world.threadsSpawned += 1
|
world.threadsSpawned += 1
|
||||||
try:
|
try:
|
||||||
@ -437,8 +443,8 @@ class CommandThread(threading.Thread):
|
|||||||
except AttributeError:
|
except AttributeError:
|
||||||
self.className = '<unknown>'
|
self.className = '<unknown>'
|
||||||
name = '%s.%s with args %r' % (self.className, self.commandName, args)
|
name = '%s.%s with args %r' % (self.className, self.commandName, args)
|
||||||
threading.Thread.__init__(self, target=command, name=name,
|
threading.Thread.__init__(self, target=callCommand, name=name,
|
||||||
args=(irc, msg, args))
|
args=(command, irc, msg, args))
|
||||||
debug.msg('Spawning thread %s' % name, 'verbose')
|
debug.msg('Spawning thread %s' % name, 'verbose')
|
||||||
self.irc = irc
|
self.irc = irc
|
||||||
self.msg = msg
|
self.msg = msg
|
||||||
@ -446,11 +452,7 @@ class CommandThread(threading.Thread):
|
|||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
try:
|
try:
|
||||||
start = time.time()
|
|
||||||
threading.Thread.run(self)
|
threading.Thread.run(self)
|
||||||
elapsed = time.time() - start
|
|
||||||
debug.msg('%s.%s took %s seconds.' % \
|
|
||||||
(self.className, self.commandName, elapsed), 'verbose')
|
|
||||||
except (getopt.GetoptError, ArgumentError):
|
except (getopt.GetoptError, ArgumentError):
|
||||||
if hasattr(self.command, '__doc__'):
|
if hasattr(self.command, '__doc__'):
|
||||||
help = self.command.__doc__.splitlines()[0]
|
help = self.command.__doc__.splitlines()[0]
|
||||||
@ -548,11 +550,6 @@ class Privmsg(irclib.IrcCallback):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
def callCommand(self, f, irc, msg, args):
|
def callCommand(self, f, irc, msg, args):
|
||||||
if self.threaded:
|
|
||||||
thread = CommandThread(f, irc, msg, args)
|
|
||||||
thread.start()
|
|
||||||
#debug.printf('Spawned new thread: %s' % thread)
|
|
||||||
else:
|
|
||||||
# Exceptions aren't caught here because IrcObjectProxy.finalEval
|
# Exceptions aren't caught here because IrcObjectProxy.finalEval
|
||||||
# catches them and does The Right Thing.
|
# catches them and does The Right Thing.
|
||||||
start = time.time()
|
start = time.time()
|
||||||
|
@ -90,7 +90,7 @@ def thread(f):
|
|||||||
"""Makes sure a command spawns a thread when called."""
|
"""Makes sure a command spawns a thread when called."""
|
||||||
def newf(self, irc, msg, args):
|
def newf(self, irc, msg, args):
|
||||||
ff = new.instancemethod(f, self, self.__class__)
|
ff = new.instancemethod(f, self, self.__class__)
|
||||||
t = callbacks.CommandThread(ff, irc, msg, args)
|
t = callbacks.CommandThread(self.callCommand, ff, irc, msg, args)
|
||||||
t.start()
|
t.start()
|
||||||
newf.__doc__ = f.__doc__
|
newf.__doc__ = f.__doc__
|
||||||
return newf
|
return newf
|
||||||
@ -106,4 +106,5 @@ class CapabilityCheckingPrivmsg(callbacks.Privmsg):
|
|||||||
else:
|
else:
|
||||||
irc.error(msg, conf.replyNoCapability % self.capability)
|
irc.error(msg, conf.replyNoCapability % self.capability)
|
||||||
|
|
||||||
|
|
||||||
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|
||||||
|
Loading…
Reference in New Issue
Block a user