2003-04-08 09:00:39 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
###
|
|
|
|
# Copyright (c) 2002, 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.
|
|
|
|
"""
|
|
|
|
|
2003-11-25 09:23:47 +01:00
|
|
|
__revision__ = "$Id$"
|
|
|
|
|
2003-10-05 14:56:56 +02:00
|
|
|
import plugins
|
2003-04-08 09:00:39 +02:00
|
|
|
|
2003-11-11 13:18:25 +01:00
|
|
|
import os
|
2003-04-12 12:26:05 +02:00
|
|
|
import re
|
2003-07-31 08:20:58 +02:00
|
|
|
import sets
|
2003-04-08 09:00:39 +02:00
|
|
|
|
|
|
|
import conf
|
2003-08-29 00:24:33 +02:00
|
|
|
import utils
|
2003-04-08 09:00:39 +02:00
|
|
|
import privmsgs
|
|
|
|
import callbacks
|
2003-11-11 13:18:25 +01:00
|
|
|
import structures
|
2003-04-08 09:00:39 +02:00
|
|
|
|
2003-09-10 22:25:15 +02:00
|
|
|
class AliasError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class RecursiveAlias(AliasError):
|
2003-04-08 09:00:39 +02:00
|
|
|
pass
|
|
|
|
|
2003-04-12 12:26:05 +02:00
|
|
|
def findAliasCommand(s, alias):
|
2003-09-10 10:15:44 +02:00
|
|
|
s = re.escape(s)
|
2003-11-04 18:39:32 +01:00
|
|
|
r = re.compile(r'(?:(^|\[)\s*\b%s\b|\|\s*\b%s\b)' % (s, s))
|
2003-04-12 12:26:05 +02:00
|
|
|
return bool(r.search(alias))
|
|
|
|
|
|
|
|
dollarRe = re.compile(r'\$(\d+)')
|
|
|
|
def findBiggestDollar(alias):
|
|
|
|
dollars = dollarRe.findall(alias)
|
2003-08-29 00:24:33 +02:00
|
|
|
dollars = map(int, dollars)
|
2003-04-12 12:26:05 +02:00
|
|
|
dollars.sort()
|
|
|
|
if dollars:
|
2003-08-29 00:24:33 +02:00
|
|
|
return dollars[-1]
|
2003-04-12 12:26:05 +02:00
|
|
|
else:
|
2003-09-13 15:30:33 +02:00
|
|
|
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
|
2003-08-20 18:26:23 +02:00
|
|
|
|
2003-04-12 12:26:05 +02:00
|
|
|
def makeNewAlias(name, alias):
|
2003-11-11 12:30:32 +01:00
|
|
|
original = alias
|
2003-04-12 12:26:05 +02:00
|
|
|
if findAliasCommand(name, alias):
|
2003-04-08 09:00:39 +02:00
|
|
|
raise RecursiveAlias
|
2003-11-11 12:30:32 +01:00
|
|
|
biggestDollar = findBiggestDollar(original)
|
|
|
|
biggestAt = findBiggestAt(original)
|
|
|
|
wildcard = '$*' in original
|
2003-10-08 15:53:41 +02:00
|
|
|
if biggestAt and wildcard:
|
2003-11-11 12:30:32 +01:00
|
|
|
raise AliasError, 'Can\'t mix $* and optional args (@1, etc.)'
|
|
|
|
if original.count('$*') > 1:
|
|
|
|
raise AliasError, 'There can be only one $* in an alias.'
|
2003-04-08 09:00:39 +02:00
|
|
|
def f(self, irc, msg, args):
|
2003-11-11 12:30:32 +01:00
|
|
|
alias = original.replace('$nick', msg.nick)
|
|
|
|
if '$channel' in original:
|
2003-04-12 12:26:05 +02:00
|
|
|
channel = privmsgs.getChannel(msg, args)
|
2003-11-11 12:30:32 +01:00
|
|
|
alias = alias.replace('$channel', channel)
|
|
|
|
tokens = callbacks.tokenize(alias)
|
2003-10-08 15:53:41 +02:00
|
|
|
if not wildcard and biggestDollar or biggestAt:
|
2003-11-11 12:30:32 +01:00
|
|
|
args = privmsgs.getArgs(args,
|
2003-11-11 14:20:06 +01:00
|
|
|
required=biggestDollar,
|
2003-09-13 15:30:33 +02:00
|
|
|
optional=biggestAt)
|
2003-11-11 12:30:32 +01:00
|
|
|
# Gotta have a mutable sequence (for replace).
|
|
|
|
if biggestDollar + biggestAt == 1: # We got a string, no tuple.
|
|
|
|
args = [args]
|
|
|
|
def regexpReplace(m):
|
2003-10-08 15:53:41 +02:00
|
|
|
idx = int(m.group(1))
|
2003-11-11 12:30:32 +01:00
|
|
|
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)
|
2003-11-12 03:18:22 +01:00
|
|
|
Owner = irc.getCallback('Owner')
|
|
|
|
Owner.disambiguate(irc, tokens)
|
2003-11-11 12:30:32 +01:00
|
|
|
self.Proxy(irc.irc, msg, tokens)
|
2004-01-16 17:39:00 +01:00
|
|
|
doc ='<an alias, %s>\n\nAlias for %r' % \
|
|
|
|
(utils.nItems('argument', biggestDollar), alias)
|
|
|
|
f = utils.changeFunctionName(f, name, doc)
|
2003-04-08 09:00:39 +02:00
|
|
|
return f
|
|
|
|
|
|
|
|
|
|
|
|
class Alias(callbacks.Privmsg):
|
|
|
|
def __init__(self):
|
|
|
|
callbacks.Privmsg.__init__(self)
|
2004-01-18 08:58:26 +01:00
|
|
|
filename = os.path.join(conf.supybot.directories.data(), 'Aliases.db')
|
2003-11-25 11:19:40 +01:00
|
|
|
# Schema: {name: [alias, locked]}
|
2003-11-11 13:18:25 +01:00
|
|
|
self.aliases = structures.PersistentDictionary(filename)
|
|
|
|
|
|
|
|
def __call__(self, irc, msg):
|
|
|
|
# Adding the aliases requires an Irc. So the first time we get called
|
|
|
|
# with an Irc, we add our aliases and then delete ourselves :)
|
2003-12-03 22:54:18 +01:00
|
|
|
for (name, (alias, locked)) in self.aliases.items():
|
2003-12-03 22:42:48 +01:00
|
|
|
try:
|
|
|
|
self.addAlias(irc, name, alias, locked)
|
|
|
|
except Exception, e:
|
|
|
|
self.log.exception('Exception when trying to add alias %s. '
|
|
|
|
'Removing from the Alias database.' % name)
|
|
|
|
del self.aliases[name]
|
2003-11-11 13:18:25 +01:00
|
|
|
del self.__class__.__call__
|
|
|
|
|
|
|
|
def die(self):
|
|
|
|
self.aliases.close()
|
2003-08-20 18:26:23 +02:00
|
|
|
|
2003-11-25 11:19:40 +01:00
|
|
|
def lock(self, irc, msg, args):
|
2003-04-08 09:00:39 +02:00
|
|
|
"""<alias>
|
|
|
|
|
2003-11-25 11:19:40 +01:00
|
|
|
Locks an alias so that no one else can change it.
|
2003-04-08 09:00:39 +02:00
|
|
|
"""
|
|
|
|
name = privmsgs.getArgs(args)
|
|
|
|
name = callbacks.canonicalName(name)
|
|
|
|
if hasattr(self, name) and self.isCommand(name):
|
2003-11-11 13:18:25 +01:00
|
|
|
self.aliases[name][1] = True
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.replySuccess()
|
2003-04-08 09:00:39 +02:00
|
|
|
else:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error('There is no such alias.')
|
2003-11-25 11:19:40 +01:00
|
|
|
lock = privmsgs.checkCapability(lock, 'admin')
|
2003-04-08 09:00:39 +02:00
|
|
|
|
2003-11-25 11:19:40 +01:00
|
|
|
def unlock(self, irc, msg, args):
|
2003-04-08 09:00:39 +02:00
|
|
|
"""<alias>
|
|
|
|
|
2003-11-25 11:19:40 +01:00
|
|
|
Unlocks an alias so that people can define new aliases over it.
|
2003-04-08 09:00:39 +02:00
|
|
|
"""
|
|
|
|
name = privmsgs.getArgs(args)
|
|
|
|
name = callbacks.canonicalName(name)
|
|
|
|
if hasattr(self, name) and self.isCommand(name):
|
2003-11-11 13:18:25 +01:00
|
|
|
self.aliases[name][1] = False
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.replySuccess()
|
2003-04-08 09:00:39 +02:00
|
|
|
else:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error('There is no such alias.')
|
2003-11-25 11:19:40 +01:00
|
|
|
unlock = privmsgs.checkCapability(unlock, 'admin')
|
2003-08-20 18:26:23 +02:00
|
|
|
|
2003-10-08 00:19:08 +02:00
|
|
|
_invalidCharsRe = re.compile(r'[\[\]\s]')
|
2003-11-25 11:19:40 +01:00
|
|
|
def addAlias(self, irc, name, alias, lock=False):
|
2003-10-08 00:19:08 +02:00
|
|
|
if self._invalidCharsRe.search(name):
|
|
|
|
raise AliasError, 'Names cannot contain spaces or square brackets.'
|
2004-01-18 08:58:26 +01:00
|
|
|
if conf.supybot.pipeSyntax() and '|' in name:
|
2003-10-08 03:26:10 +02:00
|
|
|
raise AliasError, 'Names cannot contain pipes.'
|
2003-09-10 22:25:15 +02:00
|
|
|
realName = callbacks.canonicalName(name)
|
|
|
|
if name != realName:
|
2003-10-21 21:20:47 +02:00
|
|
|
s = 'That name isn\'t valid. Try %r instead.' % realName
|
|
|
|
raise AliasError, s
|
2003-09-10 22:25:15 +02:00
|
|
|
name = realName
|
2003-10-20 12:25:13 +02:00
|
|
|
cbs = callbacks.findCallbackForCommand(irc, name)
|
2003-12-10 08:43:45 +01:00
|
|
|
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
|
2003-11-11 13:18:25 +01:00
|
|
|
if name in self.aliases:
|
2003-11-25 11:19:40 +01:00
|
|
|
(currentAlias, locked) = self.aliases[name]
|
|
|
|
if locked and currentAlias != alias:
|
|
|
|
raise AliasError, 'Alias %r is locked.' % name
|
2003-09-10 22:25:15 +02:00
|
|
|
try:
|
|
|
|
f = makeNewAlias(name, alias)
|
|
|
|
except RecursiveAlias:
|
|
|
|
raise AliasError, 'You can\'t define a recursive alias.'
|
|
|
|
setattr(self.__class__, name, f)
|
2003-11-25 11:19:40 +01:00
|
|
|
self.aliases[name] = [alias, lock]
|
2003-09-10 22:25:15 +02:00
|
|
|
|
2003-11-25 11:19:40 +01:00
|
|
|
def removeAlias(self, name, evenIfLocked=False):
|
2003-09-10 23:15:09 +02:00
|
|
|
name = callbacks.canonicalName(name)
|
|
|
|
if hasattr(self, name) and self.isCommand(name):
|
2003-11-25 11:19:40 +01:00
|
|
|
if evenIfLocked or not self.aliases[name][1]:
|
2003-09-10 23:15:09 +02:00
|
|
|
delattr(self.__class__, name)
|
2003-11-11 13:18:25 +01:00
|
|
|
del self.aliases[name]
|
2003-09-10 23:15:09 +02:00
|
|
|
else:
|
2003-11-25 11:19:40 +01:00
|
|
|
raise AliasError, 'That alias is locked.'
|
2003-09-10 23:15:09 +02:00
|
|
|
else:
|
|
|
|
raise AliasError, 'There is no such alias.'
|
|
|
|
|
2003-10-24 11:10:10 +02:00
|
|
|
def add(self, irc, msg, args):
|
2003-11-17 07:06:43 +01:00
|
|
|
"""<name> <alias>
|
2003-04-08 09:00:39 +02:00
|
|
|
|
2003-11-17 07:06:43 +01:00
|
|
|
Defines an alias <name> that executes <alias>. The <alias>
|
|
|
|
should be in the standard "command argument [nestedcommand argument]"
|
|
|
|
format. $[digit] (like $1, $2, etc.) can be used to represent
|
|
|
|
arguments to the alias; they'll be filled with the first, second, etc.
|
|
|
|
arguments. @1, @2 can be used for optional arguments. $* simply
|
|
|
|
means "all remaining arguments," and cannot be combined with optional
|
|
|
|
arguments.
|
2003-04-08 09:00:39 +02:00
|
|
|
"""
|
2003-11-11 14:20:06 +01:00
|
|
|
(name, alias) = privmsgs.getArgs(args, required=2)
|
2003-04-08 09:00:39 +02:00
|
|
|
try:
|
2003-09-10 22:25:15 +02:00
|
|
|
self.addAlias(irc, name, alias)
|
2003-12-03 22:45:02 +01:00
|
|
|
self.log.info('Adding alias %r for %r (from %s)' %
|
|
|
|
(name, alias, msg.prefix))
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.replySuccess()
|
2003-09-10 22:25:15 +02:00
|
|
|
except AliasError, e:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error(str(e))
|
2003-04-08 09:00:39 +02:00
|
|
|
|
2003-10-24 11:10:10 +02:00
|
|
|
def remove(self, irc, msg, args):
|
2003-04-08 09:00:39 +02:00
|
|
|
"""<name>
|
|
|
|
|
2003-11-25 11:19:40 +01:00
|
|
|
Removes the given alias, if unlocked.
|
2003-04-08 09:00:39 +02:00
|
|
|
"""
|
|
|
|
name = privmsgs.getArgs(args)
|
2003-09-10 23:15:09 +02:00
|
|
|
try:
|
|
|
|
self.removeAlias(name)
|
2003-12-03 22:45:02 +01:00
|
|
|
self.log.info('Removing alias %r (from %s)' % (name, msg.prefix))
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.replySuccess()
|
2003-09-10 23:15:09 +02:00
|
|
|
except AliasError, e:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error(str(e))
|
2003-04-08 09:00:39 +02:00
|
|
|
|
|
|
|
|
|
|
|
Class = Alias
|
|
|
|
|
|
|
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|