Added Alias in the new plugin format.

This commit is contained in:
Jeremy Fincher 2005-02-02 21:48:04 +00:00
parent 7ed4967877
commit 75d45b9305
6 changed files with 571 additions and 0 deletions

1
plugins/Alias/README.txt Normal file
View File

@ -0,0 +1 @@
Insert a description of your plugin here, with any notes, etc. about using it.

61
plugins/Alias/__init__.py Normal file
View File

@ -0,0 +1,61 @@
###
# Copyright (c) 2005, 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.
###
"""
Allows aliases for other commands.
"""
import supybot
import supybot.world as world
# Use this for the version of this plugin. You may wish to put a CVS keyword
# in here if you're keeping the plugin in CVS or some similar system.
__version__ = ""
__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.
# Add more reloads here if you add third-party modules and want them to be
# reloaded when this plugin is reloaded. Don't forget to import them as well!
from plugin import findBiggestDollar, AliasError, findAliasCommand # tests
if world.testing:
import test
Class = plugin.Class
configure = config.configure
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:

44
plugins/Alias/config.py Normal file
View File

@ -0,0 +1,44 @@
###
# Copyright (c) 2005, 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('Alias', True)
Alias = conf.registerPlugin('Alias')
conf.registerGroup(Alias, 'aliases')
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78

314
plugins/Alias/plugin.py Normal file
View File

@ -0,0 +1,314 @@
###
# 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 re
import supybot.conf as conf
import supybot.utils as utils
from supybot.commands import *
import supybot.ircutils as ircutils
import supybot.registry as registry
import supybot.callbacks as callbacks
# Copied from the old privmsgs.py.
def getChannel(msg, args=()):
"""Returns the channel the msg came over or the channel given in args.
If the channel was given in args, args is modified (the channel is
removed).
"""
if args and ircutils.isChannel(args[0]):
if conf.supybot.reply.requireChannelCommandsToBeSentInChannel():
if args[0] != msg.args[0]:
s = 'Channel commands must be sent in the channel to which ' \
'they apply; if this is not the behavior you desire, ' \
'ask the bot\'s administrator to change the registry ' \
'variable ' \
'supybot.reply.requireChannelCommandsToBeSentInChannel ' \
'to False.'
raise callbacks.Error, s
return args.pop(0)
elif ircutils.isChannel(msg.args[0]):
return msg.args[0]
else:
raise callbacks.Error, 'Command must be sent in a channel or ' \
'include a channel in its arguments.'
def getArgs(args, required=1, optional=0):
if len(args) < required:
raise callbacks.ArgumentError
if len(args) < required + optional:
ret = list(args) + ([''] * (required + optional - len(args)))
elif len(args) >= required + optional:
ret = list(args[:required + optional - 1])
ret.append(' '.join(args[required + optional - 1:]))
if len(ret) == 1:
return ret[0]
else:
return ret
class AliasError(Exception):
pass
class RecursiveAlias(AliasError):
pass
def findAliasCommand(s, alias):
s = re.escape(s)
r = re.compile(r'(?:(^|\[)\s*\b%s\b|\|\s*\b%s\b)' % (s, s))
return bool(r.search(alias))
dollarRe = re.compile(r'\$(\d+)')
def findBiggestDollar(alias):
dollars = dollarRe.findall(alias)
dollars = map(int, dollars)
dollars.sort()
if dollars:
return dollars[-1]
else:
return 0
atRe = re.compile(r'@(\d+)')
def findBiggestAt(alias):
ats = atRe.findall(alias)
ats = map(int, ats)
ats.sort()
if ats:
return ats[-1]
else:
return 0
def makeNewAlias(name, alias):
original = alias
if findAliasCommand(name, alias):
raise RecursiveAlias
biggestDollar = findBiggestDollar(original)
biggestAt = findBiggestAt(original)
wildcard = '$*' in original
if biggestAt and wildcard:
raise AliasError, 'Can\'t mix $* and optional args (@1, etc.)'
if original.count('$*') > 1:
raise AliasError, 'There can be only one $* in an alias.'
testTokens = callbacks.tokenize(original)
if testTokens and isinstance(testTokens[0], list):
raise AliasError, 'Commands may not be the result of nesting.'
def f(self, irc, msg, args):
alias = original.replace('$nick', msg.nick)
if '$channel' in original:
channel = getChannel(msg, args)
alias = alias.replace('$channel', channel)
tokens = callbacks.tokenize(alias)
if not wildcard and biggestDollar or biggestAt:
args = getArgs(args, required=biggestDollar, optional=biggestAt)
# Gotta have a mutable sequence (for replace).
if biggestDollar + biggestAt == 1: # We got a string, no tuple.
args = [args]
def regexpReplace(m):
idx = int(m.group(1))
return args[idx-1]
def replace(tokens, replacer):
for (i, token) in enumerate(tokens):
if isinstance(token, list):
replace(token, replacer)
else:
tokens[i] = replacer(token)
replace(tokens, lambda s: dollarRe.sub(regexpReplace, s))
if biggestAt:
assert not wildcard
args = args[biggestDollar:]
replace(tokens, lambda s: atRe.sub(regexpReplace, s))
if wildcard:
assert not biggestAt
# Gotta remove the things that have already been subbed in.
i = biggestDollar
while i:
args.pop(0)
i -= 1
def everythingReplace(tokens):
for (i, token) in enumerate(tokens):
if isinstance(token, list):
if everythingReplace(token):
return
if token == '$*':
tokens[i:i+1] = args
return True
elif '$*' in token:
tokens[i] = token.replace('$*', ' '.join(args))
return True
return False
everythingReplace(tokens)
self.Proxy(irc, msg, tokens)
doc =format('<an alias, %n>\n\nAlias for %q.',
(biggestDollar, 'argument'), alias)
f = utils.changeFunctionName(f, name, doc)
return f
class Alias(callbacks.Privmsg):
def __init__(self, irc):
self.__parent = super(Alias, self)
self.__parent.__init__(irc)
# Schema: {alias: [command, locked]}
self.aliases = {}
# XXX This should go. aliases should be a space separate list, etc.
group = conf.supybot.plugins.Alias.aliases
for (name, alias) in registry._cache.iteritems():
name = name.lower()
if name.startswith('supybot.plugins.alias.aliases.'):
name = name[len('supybot.plugins.alias.aliases.'):]
if '.' in name:
continue
conf.registerGlobalValue(group, name, registry.String('', ''))
conf.registerGlobalValue(group.get(name), 'locked',
registry.Boolean(False, ''))
for (name, value) in group.getValues(fullNames=False):
name = name.lower() # Just in case.
command = value()
locked = value.locked()
self.aliases[name] = [command, locked]
for (alias, (command, locked)) in self.aliases.items():
try:
self.addAlias(irc, alias, command, locked)
except Exception, e:
self.log.exception('Exception when trying to add alias %s. '
'Removing from the Alias database.', alias)
del self.aliases[alias]
def lock(self, irc, msg, args, name):
"""<alias>
Locks an alias so that no one else can change it.
"""
if hasattr(self, name) and self.isCommand(name):
self.aliases[name][1] = True
conf.supybot.plugins.Alias.aliases.get(name).locked.setValue(True)
irc.replySuccess()
else:
irc.error('There is no such alias.')
lock = wrap(lock, [('checkCapability', 'admin'), 'commandName'])
def unlock(self, irc, msg, args, name):
"""<alias>
Unlocks an alias so that people can define new aliases over it.
"""
if hasattr(self, name) and self.isCommand(name):
self.aliases[name][1] = False
conf.supybot.plugins.Alias.aliases.get(name).locked.setValue(False)
irc.replySuccess()
else:
irc.error('There is no such alias.')
unlock = wrap(unlock, [('checkCapability', 'admin'), 'commandName'])
_invalidCharsRe = re.compile(r'[\[\]\s]')
def addAlias(self, irc, name, alias, lock=False):
if self._invalidCharsRe.search(name):
raise AliasError, 'Names cannot contain spaces or square brackets.'
if '|' in name:
raise AliasError, 'Names cannot contain pipes.'
if irc.getCallback(name):
raise AliasError, 'Names cannot coincide with names of plugins.'
realName = callbacks.canonicalName(name)
if name != realName:
s = format('That name isn\'t valid. Try %q instead.', realName)
raise AliasError, s
name = realName
cbs = callbacks.findCallbackForCommand(irc, name)
if self in cbs:
if hasattr(self, realName) and realName not in self.aliases:
s = 'You can\'t overwrite commands in this plugin.'
raise AliasError, s
if name in self.aliases:
(currentAlias, locked) = self.aliases[name]
if locked and currentAlias != alias:
raise AliasError, format('Alias %q is locked.', name)
try:
f = makeNewAlias(name, alias)
except RecursiveAlias:
raise AliasError, 'You can\'t define a recursive alias.'
if name in self.aliases:
# We gotta remove it so its value gets updated.
conf.supybot.plugins.Alias.aliases.unregister(name)
conf.supybot.plugins.Alias.aliases.register(name,
registry.String(alias, ''))
conf.supybot.plugins.Alias.aliases.get(name).register('locked',
registry.Boolean(lock, ''))
setattr(self.__class__, name, f)
self.aliases[name] = [alias, lock]
def removeAlias(self, name, evenIfLocked=False):
name = callbacks.canonicalName(name)
if hasattr(self, name) and self.isCommand(name):
if evenIfLocked or not self.aliases[name][1]:
delattr(self.__class__, name)
del self.aliases[name]
conf.supybot.plugins.Alias.aliases.unregister(name)
else:
raise AliasError, 'That alias is locked.'
else:
raise AliasError, 'There is no such alias.'
def add(self, irc, msg, args, name, alias):
"""<name> <alias>
Defines an alias <name> that executes <alias>. The <alias>
should be in the standard "command argument [nestedcommand argument]"
arguments to the alias; they'll be filled with the first, second, etc.
arguments. $1, $2, etc. can be used for required arguments. @1, @2,
etc. can be used for optional arguments. $* simply means "all
remaining arguments," and cannot be combined with optional arguments.
"""
if ' ' not in alias:
# If it's a single word, they probably want $*.
alias += ' $*'
try:
self.addAlias(irc, name, alias)
self.log.info('Adding alias %q for %q (from %s)',
name, alias, msg.prefix)
irc.replySuccess()
except AliasError, e:
irc.error(str(e))
add = wrap(add, ['commandName', 'text'])
def remove(self, irc, msg, args, name):
"""<name>
Removes the given alias, if unlocked.
"""
try:
self.removeAlias(name)
self.log.info('Removing alias %q (from %s)', name, msg.prefix)
irc.replySuccess()
except AliasError, e:
irc.error(str(e))
remove = wrap(remove, ['commandName'])
Class = Alias
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:

150
plugins/Alias/test.py Normal file
View File

@ -0,0 +1,150 @@
###
# 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 *
import supybot.plugin as plugin
Alias = plugin.loadPluginModule('Alias')
class FunctionsTest(SupyTestCase):
def testFindAliasCommand(self):
s = 'command'
self.failIf(Alias.findAliasCommand(s, ''))
self.failIf(Alias.findAliasCommand(s, 'foo'))
self.failIf(Alias.findAliasCommand(s, 'foo bar [ baz]'))
self.failIf(Alias.findAliasCommand(s, 'foo bar [baz]'))
self.failUnless(Alias.findAliasCommand(s, s))
self.failUnless(Alias.findAliasCommand(s, ' %s' % s))
self.failUnless(Alias.findAliasCommand(s, '[%s]' % s))
self.failUnless(Alias.findAliasCommand(s, '[ %s]' % s))
self.failUnless(Alias.findAliasCommand(s, 'foo bar [%s]' % s))
self.failUnless(Alias.findAliasCommand(s, 'foo bar [ %s]' % s))
self.failUnless(Alias.findAliasCommand(s, 'foo | %s' % s))
self.failUnless(Alias.findAliasCommand(s, 'foo |%s' % s))
def testFindBiggestDollar(self):
self.assertEqual(Alias.findBiggestDollar(''), 0)
self.assertEqual(Alias.findBiggestDollar('foo'), 0)
self.assertEqual(Alias.findBiggestDollar('$0'), 0)
self.assertEqual(Alias.findBiggestDollar('$1'), 1)
self.assertEqual(Alias.findBiggestDollar('$2'), 2)
self.assertEqual(Alias.findBiggestDollar('$2 $10'), 10)
self.assertEqual(Alias.findBiggestDollar('$3'), 3)
self.assertEqual(Alias.findBiggestDollar('$3 $2 $1'), 3)
self.assertEqual(Alias.findBiggestDollar('foo bar $1'), 1)
self.assertEqual(Alias.findBiggestDollar('foo $2 $1'), 2)
self.assertEqual(Alias.findBiggestDollar('foo $0 $1'), 1)
self.assertEqual(Alias.findBiggestDollar('foo $1 $3'), 3)
self.assertEqual(Alias.findBiggestDollar('$10 bar $1'), 10)
class AliasTestCase(ChannelPluginTestCase):
plugins = ('Alias', 'Filter', 'Utilities', 'Format', 'Reply')
def testNoAliasWithNestedCommandName(self):
self.assertError('alias add foo "[bar] baz"')
def testDoesNotOverwriteCommands(self):
self.assertError('alias add alias "echo foo bar baz"')
self.assertError('alias add add "echo foo bar baz"')
self.assertError('alias add remove "echo foo bar baz"')
self.assertError('alias add lock "echo foo bar baz"')
self.assertError('alias add unlock "echo foo bar baz"')
def testAliasHelp(self):
self.assertNotError('alias add slashdot foo')
self.assertRegexp('help slashdot', "Alias for .*foo")
def testRemove(self):
self.assertNotError('alias add foo echo bar')
self.assertResponse('foo', 'bar')
self.assertNotError('alias remove foo')
self.assertError('foo')
def testDollars(self):
self.assertNotError('alias add rot26 "rot13 [rot13 $1]"')
self.assertResponse('rot26 foobar', 'foobar')
def testMoreDollars(self):
self.assertNotError('alias add rev "echo $3 $2 $1"')
self.assertResponse('rev foo bar baz', 'baz bar foo')
def testAllArgs(self):
self.assertNotError('alias add swap "echo $2 $1 $*"')
self.assertResponse('swap 1 2 3 4 5', '2 1 3 4 5')
self.assertError('alias add foo "echo $1 @1 $*"')
def testNoRecursion(self):
self.assertError('alias add rotinfinity "rot13 [rotinfinity $1]"')
self.assertNotError('alias add rotinfintynot "rot13 [rotinfinity $1]"')
self.assertNotError('alias add rotinfin "rot13 [rotinfinity $1]"')
def testNonCanonicalName(self):
self.assertError('alias add FOO foo')
self.assertError('alias add [] foo')
self.assertError('alias add "foo bar" foo')
self.assertError('alias add "foo|bar" foo')
def testChannel(self):
self.assertNotError('alias add channel echo $channel')
self.assertResponse('alias channel', self.channel)
def testNick(self):
self.assertNotError('alias add sendingnick "rot13 [rot13 $nick]"')
self.assertResponse('sendingnick', self.nick)
def testAddRemoveAlias(self):
cb = self.irc.getCallback('Alias')
cb.addAlias(self.irc, 'foobar', 'echo sbbone', lock=True)
self.assertResponse('foobar', 'sbbone')
self.assertRaises(Alias.AliasError, cb.removeAlias, 'foobar')
cb.removeAlias('foobar', evenIfLocked=True)
self.failIf('foobar' in cb.aliases)
self.assertError('foobar')
def testOptionalArgs(self):
self.assertNotError('alias add myrepr "repr @1"')
self.assertResponse('myrepr foo', '"foo"')
self.assertResponse('myrepr ""', '""')
def testNoExtraSpaces(self):
self.assertNotError('alias add foo "action takes $1\'s money"')
self.assertResponse('foo bar', '\x01ACTION takes bar\'s money\x01')
def testNoExtraQuotes(self):
self.assertNotError('alias add myre "echo s/$1/$2/g"')
self.assertResponse('myre foo bar', 's/foo/bar/g')
def testSimpleAliasWithoutArgsImpliesDollarStar(self):
self.assertNotError('alias add exo echo')
self.assertResponse('exo foo bar baz', 'foo bar baz')
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:

View File

@ -31,6 +31,7 @@
plugins = [
'Admin',
'Alias',
'AutoMode',
'Anonymous',
'Babelfish',