From 95877b2d5359e3e4138d653f93571fc76fc87d22 Mon Sep 17 00:00:00 2001 From: Jeremy Fincher Date: Fri, 10 Sep 2004 06:26:46 +0000 Subject: [PATCH] This should work somewhat. --- plugins/Observer.py | 38 ++++++++++++++++++++-------- test/test_Observer.py | 58 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 10 deletions(-) create mode 100644 test/test_Observer.py diff --git a/plugins/Observer.py b/plugins/Observer.py index d5c7a0b67..be83cbe87 100644 --- a/plugins/Observer.py +++ b/plugins/Observer.py @@ -64,37 +64,55 @@ class Probability(registry.Float): else: super(Probability, self).setValue(v) +class Observers(registry.SpaceSeparatedListOfStrings): + List = callbacks.CanonicalNameSet + + +class ActiveObservers(registry.SpaceSeparatedListOfStrings): + String = callbacks.canonicalName + conf.registerPlugin('Observer') # XXX These both need to be CanonicalStrings. conf.registerGlobalValue(conf.supybot.plugins.Observer, 'observers', - registry.SpaceSeparatedSetOfStrings([], """Determines what observers are - available.""", orderAlphabetically=True)) + Observers([], """Determines what observers are available.""", + orderAlphabetically=True)) conf.registerChannelValue(conf.supybot.plugins.Observer.observers, 'active', - registry.SpaceSeparatedListOfStrings([], """Determines what observers are + ActiveObservers([], """Determines what observers are active on a channel.""")) -def registerObserver(name, regexpString=None, - commandString=None, probability=1.0): +def registerObserver(name, regexpString='', + commandString='', probability=1.0): g = conf.registerGlobalValue(conf.supybot.plugins.Observer.observers, name, registry.Regexp(regexpString, """Determines what regexp must match for this observer to be executed.""")) - if regexpString is not None: + if regexpString: g.set(regexpString) # This is in case it's been registered. conf.registerGlobalValue(g, 'command', registry.String('', """Determines what command will be run when this observer is executed.""")) + if commandString: + g.command.setValue(commandString) conf.registerGlobalValue(g, 'probability', Probability(probability, """ Determines what the probability of executing this observer is if it matches.""")) + g.probability.setValue(probability) conf.supybot.plugins.Observer.observers().add(name) return g class Observer(callbacks.Privmsg): + commandCalled = False def _isValidObserverName(self, name): return name != 'active' and registry.isValidRegistryName(name) + def callCommand(self, *args, **kwargs): + self.commandCalled = True + super(Observer, self).callCommand(*args, **kwargs) + def doPrivmsg(self, irc, msg): + if self.commandCalled: + self.commandCalled = False + return channel = msg.args[0] Owner = irc.getCallback('Owner') observers = self.registryValue('observers') @@ -116,7 +134,7 @@ class Observer(callbacks.Privmsg): groups.insert(0, m.group(0)) for (i, group) in enumerate(groups): command = command.replace('$%s' % i, group) - tokens = callbacks.tokenize(text, channel=channel) + tokens = callbacks.tokenize(command, channel=channel) Owner.processTokens(irc, msg, tokens) def list(self, irc, msg, args): @@ -142,7 +160,7 @@ class Observer(callbacks.Privmsg): else: irc.reply('There were no relevant observers.') - def enable(self, irc, msg, args): + def enable(self, irc, msg, args, channel): """[] Enables the observer in . is only @@ -151,7 +169,7 @@ class Observer(callbacks.Privmsg): name = privmsgs.getArgs(args) if name not in self.registryValue('observers'): irc.error('There is no observer %s.' % name, Raise=True) - self.registryValue('observers.active', channel).add(name) + self.registryValue('observers.active', channel).append(name) irc.replySuccess() enable = privmsgs.checkChannelCapability(enable, 'op') @@ -165,7 +183,7 @@ class Observer(callbacks.Privmsg): try: self.registryValue('observers.active', channel).remove(name) irc.replySuccess() - except KeyError: + except (KeyError, ValueError): irc.error('The observer %s was not active on %s.' % (name,channel)) disable = privmsgs.checkChannelCapability(disable, 'op') diff --git a/test/test_Observer.py b/test/test_Observer.py new file mode 100644 index 000000000..dcced3ad3 --- /dev/null +++ b/test/test_Observer.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python + +### +# Copyright (c) 2002-2004, Jeremiah Fincher +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, +# this list of conditions, and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions, and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of the author of this software nor the name of +# contributors to this software may be used to endorse or promote products +# derived from this software without specific prior written consent. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +### + +from testsupport import * + +class ObserverTestCase(ChannelPluginTestCase): + plugins = ('Observer', 'Utilities') + config = {'reply.whenNotCommand': False} + def testAdd(self): + self.assertNotError('add foo m/foo/i echo I saw foo.') + self.assertNoResponse('blah blah blah', 1) + self.assertNotError('observer enable foo') + self.assertNoResponse('blah blah blah', 1) + self.assertResponse('I love to foo!', 'I saw foo.') + self.assertResponse('Foo you!', 'I saw foo.') + self.assertResponse('foobar', 'I saw foo.') + self.assertNotError('observer disable foo') + + def testGroups(self): + self.assertNotError('add digits m/(\d+)/ echo $1') + self.assertNoResponse('asdfkjaf', 1) + self.assertNotError('observer enable digits') + self.assertNoResponse('asdfkjaf', 1) + self.assertResponse('abc -- easy as 123', '123') + self.assertResponse('testing, 1 2 3' , '1') + self.assertNotError('observer disable digits') + + +# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: +