diff --git a/plugins/Utilities/README.txt b/plugins/Utilities/README.txt new file mode 100644 index 000000000..d60b47a97 --- /dev/null +++ b/plugins/Utilities/README.txt @@ -0,0 +1 @@ +Insert a description of your plugin here, with any notes, etc. about using it. diff --git a/plugins/Utilities/__init__.py b/plugins/Utilities/__init__.py new file mode 100644 index 000000000..78fda2844 --- /dev/null +++ b/plugins/Utilities/__init__.py @@ -0,0 +1,54 @@ +### +# Copyright (c) 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. +### + +""" +Various utility commands, mostly useful for manipulating nested commands. +""" + +import supybot +import supybot.world as world + +__author__ = supybot.authors.jemfinch + +# This is a dictionary mapping supybot.Author instances to lists of +# contributions. +__contributors__ = {} + +import config +import plugin +reload(plugin) # In case we're being reloaded. + +if world.testing: + import test + +Class = plugin.Class +configure = config.configure + + +# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: diff --git a/plugins/Utilities/config.py b/plugins/Utilities/config.py new file mode 100644 index 000000000..826fed294 --- /dev/null +++ b/plugins/Utilities/config.py @@ -0,0 +1,48 @@ +### +# Copyright (c) 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. +### + +import supybot.conf as conf +import supybot.registry as registry + +def configure(advanced): + # This will be called by supybot to configure this module. advanced is + # a bool that specifies whether the user identified himself as an advanced + # user or not. You should effect your configuration by manipulating the + # registry as appropriate. + from supybot.questions import expect, anything, something, yn + conf.registerPlugin('Utilities', True) + + +Utilities = conf.registerPlugin('Utilities') +# This is where your configuration variables (if any) should go. For example: +# conf.registerGlobalValue(Utilities, 'someConfigVariableName', +# registry.Boolean(False, """Help for someConfigVariableName.""")) + + +# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78 diff --git a/plugins/Utilities/plugin.py b/plugins/Utilities/plugin.py new file mode 100644 index 000000000..41dcaaee8 --- /dev/null +++ b/plugins/Utilities/plugin.py @@ -0,0 +1,155 @@ +### +# 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. +### + +import types +import random +import string + +import supybot.utils as utils +from supybot.commands import * +import supybot.ircmsgs as ircmsgs +import supybot.ircutils as ircutils +import supybot.callbacks as callbacks + +class Utilities(callbacks.Privmsg): + # Yes, I really do mean "requires no arguments" below. "takes no + # arguments" would probably lead people to think it was a useless command. + def ignore(self, irc, msg, args): + """requires no arguments + + Does nothing. Useful sometimes for sequencing commands when you don't + care about their non-error return values. + """ + pass + # Do be careful not to wrap this unless you do any('something'). + + def reply(self, irc, msg, args, text): + """ + + Replies with . Equivalent to the alias, 'echo $nick: $1'. + """ + irc.reply(text, prefixName=True) + reply = wrap(reply, ['text']) + + def success(self, irc, msg, args, text): + """[] + + Does nothing except to reply with a success message. This is useful + when you want to run multiple commands as nested commands, and don't + care about their output as long as they're successful. An error, of + course, will break out of this command. , if given, will be + appended to the end of the success message. + """ + irc.replySuccess(text) + success = wrap(success, [additional('text')]) + + def last(self, irc, msg, args): + """ [ ...] + + Returns the last argument given. Useful when you'd like multiple + nested commands to run, but only the output of the last one to be + returned. + """ + args = filter(None, args) + if args: + irc.reply(args[-1]) + else: + raise callbacks.ArgumentError + + def strlen(self, irc, msg, args): + """ + + Returns the length of . + """ + total = 0 + for arg in args: + total += len(arg) + total += len(args)-1 # spaces between the arguments. + irc.reply(str(total)) + + def echo(self, irc, msg, args, text): + """ + + Returns the arguments given it. Uses our standard substitute on the + string(s) given to it; $nick (or $who), $randomNick, $randomInt, + $botnick, $channel, $user, $host, $today, $now, and $randomDate are all + handled appropriately. + """ + text = ircutils.standardSubstitute(irc, msg, text) + irc.reply(text, prefixName=False) + echo = wrap(echo, ['text']) + + def shuffle(self, irc, msg, args, things): + """ [ ...] + + Shuffles the arguments given it. + """ + random.shuffle(things) + irc.reply(' '.join(things)) + shuffle = wrap(shuffle, [many('anything')]) + + def re(self, irc, msg, args, ff, text): + """ + + If is of the form m/regexp/flags, returns the portion of + that matches the regexp. If is of the form + s/regexp/replacement/flags, returns the result of applying such a + regexp to + """ + if isinstance(ff, (types.FunctionType, types.MethodType)): + f = ff + else: + f = lambda s: ff.search(s) and ff.search(s).group(0) or '' + if f('') and len(f(' ')) > len(f(''))+1: # Matches the empty string. + s = 'You probably don\'t want to match the empty string.' + irc.error(s) + else: + irc.reply(f(text)) + re = wrap(re, [('checkCapability', 'trusted'), + first('regexpMatcher', 'regexpReplacer'), + 'text']) + + def apply(self, irc, msg, args, command, rest): + """ + + Tokenizes and calls with the resulting arguments. + """ + args = [token and token or '""' for token in rest] + text = ' '.join(args) + commands = command.split() + commands = map(callbacks.canonicalName, commands) + tokens = callbacks.tokenize(text) + allTokens = commands + tokens + self.Proxy(irc, msg, allTokens) + apply = wrap(apply, ['something', many('anything')]) + + +Class = Utilities + +# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: diff --git a/plugins/Utilities/test.py b/plugins/Utilities/test.py new file mode 100644 index 000000000..bf74f3a92 --- /dev/null +++ b/plugins/Utilities/test.py @@ -0,0 +1,90 @@ +### +# 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 supybot.test import * + +class UtilitiesTestCase(PluginTestCase): + plugins = ('Utilities',) + def testIgnore(self): + self.assertNoResponse('utilities ignore foo bar baz', 1) + self.assertError('utilities ignore [re m/foo bar]') + + def testSuccess(self): + self.assertNotError('success 1') + self.assertError('success [re m/foo bar]') + + def testLast(self): + self.assertResponse('utilities last foo bar baz', 'baz') + + def testStrlen(self): + self.assertResponse('strlen %s' % ('s'*10), '10') + self.assertResponse('strlen a b', '3') + + def testEcho(self): + self.assertHelp('echo') + self.assertResponse('echo foo', 'foo') + + def testEchoDollarOneRepliesDollarOne(self): + self.assertResponse('echo $1', '$1') + + def testEchoStandardSubstitute(self): + self.assertNotRegexp('echo $nick', r'\$') + + def testRe(self): + self.assertResponse('re "m/system time/" foo bar system time baz', + 'system time') + self.assertResponse('re s/user/luser/g user user', 'luser luser') + self.assertResponse('re s/user/luser/ user user', 'luser user') + self.assertNotRegexp('re m/foo/ bar', 'has no attribute') + self.assertResponse('re m/a\S+y/ "the bot angryman is hairy"','angry') + + def testReNotEmptyString(self): + self.assertError('re s//foo/g blah') + + def testReWorksWithJustCaret(self): + self.assertResponse('re s/^/foo/ bar', 'foobar') + + def testReNoEscapingUnpackListOfWrongSize(self): + self.assertNotRegexp('re foo bar baz', 'unpack list of wrong size') + + def testReBug850931(self): + self.assertResponse('re s/\b(\w+)\b/\1./g foo bar baz', + 'foo. bar. baz.') + + def testNotOverlongRe(self): + self.assertError('re [strjoin "" s/./ [eval \'xxx\'*400]] blah blah') + + def testApply(self): + self.assertResponse('apply "utilities last" a', 'a') + self.assertResponse('apply "utilities last" a b', 'b') + + def testShuffle(self): + self.assertResponse('shuffle a', 'a') + +# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: diff --git a/setup.py b/setup.py index fb77c80b0..c3bba2fa3 100644 --- a/setup.py +++ b/setup.py @@ -91,6 +91,7 @@ plugins = [ 'Misc', 'Owner', 'User', + 'Utilities', ] packages = ['supybot',