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-10-05 14:56:56 +02:00
|
|
|
import plugins
|
2003-04-08 09:00:39 +02:00
|
|
|
|
2003-04-12 12:26:05 +02:00
|
|
|
import re
|
2003-09-29 07:41:03 +02:00
|
|
|
import new
|
2003-07-31 08:20:58 +02:00
|
|
|
import sets
|
2003-04-08 09:00:39 +02:00
|
|
|
|
|
|
|
import conf
|
|
|
|
import debug
|
2003-08-29 00:24:33 +02:00
|
|
|
import utils
|
2003-04-08 09:00:39 +02:00
|
|
|
import privmsgs
|
|
|
|
import callbacks
|
|
|
|
|
|
|
|
def configure(onStart, afterConnect, advanced):
|
|
|
|
# This will be called by setup.py to configure this module. onStart and
|
|
|
|
# afterConnect are both lists. Append to onStart the commands you would
|
|
|
|
# like to be run when the bot is started; append to afterConnect the
|
|
|
|
# commands you would like to be run when the bot has finished connecting.
|
|
|
|
from questions import expect, anything, something, yn
|
|
|
|
onStart.append('load Alias')
|
|
|
|
|
2003-08-29 00:24:33 +02:00
|
|
|
example = utils.wrapLines("""
|
|
|
|
<jemfinch> @list Alias
|
|
|
|
<supybot> jemfinch: advogato, alias, debplanet, devshed, freeze, gnome, googlebsd, googlelinux, googlemac, k5, kde, kt, lambda, linuxmag, lj, lwn, nyttech, osnews, pypi, python, slashdot, unalias, unfreeze
|
|
|
|
<jemfinch> (just pay attention to the freeze/unfreeze and alias/unalias)
|
|
|
|
<jemfinch> @alias rot26 "rot13 [rot13 $1]"
|
|
|
|
<supybot> jemfinch: The operation succeeded.
|
|
|
|
<jemfinch> @rot26 jemfinch
|
|
|
|
<supybot> jemfinch: jemfinch
|
|
|
|
<jemfinch> @unalias rot26
|
|
|
|
<supybot> jemfinch: The operation succeeded.
|
|
|
|
<jemfinch> @rot26 jemfinch
|
|
|
|
<jemfinch> (look, Ma! No rot26!)
|
|
|
|
<Cerlyn> ooohh
|
|
|
|
<jemfinch> @alias rot26 "rot13 [rot13 $1]"
|
|
|
|
<supybot> jemfinch: The operation succeeded.
|
|
|
|
<jemfinch> @freeze rot26
|
|
|
|
<supybot> jemfinch: The operation succeeded.
|
|
|
|
<jemfinch> (now's your queue :))
|
|
|
|
<Cerlyn> @unalias rot26
|
|
|
|
<supybot> Cerlyn: Error: That alias is frozen.
|
|
|
|
<Cerlyn> @unalias rot26nothere
|
|
|
|
<supybot> Cerlyn: Error: There is no such alias.
|
|
|
|
<Cerlyn> @unfreeze rot26
|
|
|
|
<supybot> Cerlyn: Error: You don't have the "admin" capability.
|
|
|
|
<jemfinch> @unfreeze rot26
|
|
|
|
<supybot> jemfinch: The operation succeeded.
|
|
|
|
<jemfinch> (now try to remove it :))
|
|
|
|
<Cerlyn> @unalias rot26
|
|
|
|
<supybot> Cerlyn: The operation succeeded.
|
|
|
|
<jemfinch> @rot26 blah blah blah
|
2003-09-06 23:58:42 +02:00
|
|
|
<jemfinch> (note that it did nothing)
|
2003-08-29 00:24:33 +02:00
|
|
|
<jemfinch> @help slashdot
|
2003-09-06 23:58:42 +02:00
|
|
|
<supybot> jemfinch: slashdot <an alias, 0 arguments> (for more help use the morehelp command)
|
2003-08-29 00:24:33 +02:00
|
|
|
<jemfinch> @morehelp slashdot
|
2003-09-06 23:58:42 +02:00
|
|
|
<supybot> jemfinch: Alias for 'rsstitles http://slashdot.org/slashdot.rss'
|
2003-08-29 00:24:33 +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)
|
|
|
|
r = re.compile(r'(?:(^|\[)\s*%s|\|\s*%s)' % (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):
|
|
|
|
if findAliasCommand(name, alias):
|
2003-04-08 09:00:39 +02:00
|
|
|
raise RecursiveAlias
|
2003-04-12 12:26:05 +02:00
|
|
|
biggestDollar = findBiggestDollar(alias)
|
2003-09-13 15:30:33 +02:00
|
|
|
biggestAt = findBiggestAt(alias)
|
2003-10-08 15:53:41 +02:00
|
|
|
wildcard = '$*' in alias
|
|
|
|
if biggestAt and wildcard:
|
|
|
|
raise AliasError, 'Can\'t use $* and optional args (@1, etc.)'
|
2003-04-08 09:00:39 +02:00
|
|
|
def f(self, irc, msg, args):
|
2003-10-09 07:00:56 +02:00
|
|
|
alias_ = alias.replace('$nick', msg.nick)
|
|
|
|
if '$channel' in alias:
|
2003-04-12 12:26:05 +02:00
|
|
|
channel = privmsgs.getChannel(msg, args)
|
2003-10-09 07:00:56 +02:00
|
|
|
alias_ = alias_.replace('$channel', channel)
|
2003-10-08 15:53:41 +02:00
|
|
|
if not wildcard and biggestDollar or biggestAt:
|
2003-09-13 15:30:33 +02:00
|
|
|
args = privmsgs.getArgs(args, needed=biggestDollar,
|
|
|
|
optional=biggestAt)
|
|
|
|
# Gotta have a tuple.
|
2003-10-08 15:53:41 +02:00
|
|
|
if biggestDollar + biggestAt == 1 and not wildcard:
|
2003-04-12 12:26:05 +02:00
|
|
|
args = (args,)
|
2003-10-08 15:53:41 +02:00
|
|
|
def replace(m):
|
|
|
|
idx = int(m.group(1))
|
|
|
|
return utils.dqrepr(args[idx-1])
|
|
|
|
alias_ = dollarRe.sub(replace, alias_)
|
|
|
|
args = args[biggestDollar:]
|
|
|
|
alias_ = atRe.sub(replace, alias_)
|
|
|
|
alias_ = alias_.replace('$*', ' '.join(map(utils.dqrepr, args)))
|
2003-09-02 21:51:04 +02:00
|
|
|
self.Proxy(irc.irc, msg, callbacks.tokenize(alias_))
|
2003-10-21 22:59:51 +02:00
|
|
|
f = new.function(f.func_code, f.func_globals, name, closure=f.func_closure)
|
2003-09-05 08:55:58 +02:00
|
|
|
f.__doc__ ='<an alias, %s>\n\nAlias for %r' % \
|
2003-09-05 08:49:14 +02:00
|
|
|
(utils.nItems(biggestDollar, 'argument'), alias)
|
2003-04-08 09:00:39 +02:00
|
|
|
return f
|
|
|
|
|
|
|
|
|
|
|
|
class Alias(callbacks.Privmsg):
|
|
|
|
def __init__(self):
|
|
|
|
callbacks.Privmsg.__init__(self)
|
2003-07-31 08:20:58 +02:00
|
|
|
self.frozen = sets.Set()
|
2003-08-20 18:26:23 +02:00
|
|
|
|
2003-04-08 09:00:39 +02:00
|
|
|
def freeze(self, irc, msg, args):
|
|
|
|
"""<alias>
|
|
|
|
|
|
|
|
'Freezes' an alias so that no one else can change it.
|
|
|
|
"""
|
|
|
|
name = privmsgs.getArgs(args)
|
|
|
|
name = callbacks.canonicalName(name)
|
|
|
|
if hasattr(self, name) and self.isCommand(name):
|
|
|
|
self.frozen.add(name)
|
|
|
|
irc.reply(msg, conf.replySuccess)
|
|
|
|
else:
|
|
|
|
irc.error(msg, 'There is no such alias.')
|
|
|
|
freeze = privmsgs.checkCapability(freeze, 'admin')
|
|
|
|
|
|
|
|
def unfreeze(self, irc, msg, args):
|
|
|
|
"""<alias>
|
|
|
|
|
|
|
|
'Unfreezes' an alias so that people can define new aliases over it.
|
|
|
|
"""
|
|
|
|
name = privmsgs.getArgs(args)
|
|
|
|
name = callbacks.canonicalName(name)
|
|
|
|
if hasattr(self, name) and self.isCommand(name):
|
|
|
|
self.frozen.discard(name)
|
|
|
|
irc.reply(msg, conf.replySuccess)
|
|
|
|
else:
|
|
|
|
irc.error(msg, 'There is no such alias.')
|
|
|
|
unfreeze = privmsgs.checkCapability(unfreeze, 'admin')
|
2003-08-20 18:26:23 +02:00
|
|
|
|
2003-10-08 00:19:08 +02:00
|
|
|
_invalidCharsRe = re.compile(r'[\[\]\s]')
|
2003-09-10 22:25:15 +02:00
|
|
|
def addAlias(self, irc, name, alias, freeze=False):
|
2003-10-08 00:19:08 +02:00
|
|
|
if self._invalidCharsRe.search(name):
|
|
|
|
raise AliasError, 'Names cannot contain spaces or square brackets.'
|
2003-10-08 03:26:10 +02:00
|
|
|
if conf.enablePipeSyntax and '|' in name:
|
|
|
|
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)
|
|
|
|
if [cb for cb in cbs if cb != self]:
|
2003-10-21 21:20:47 +02:00
|
|
|
s = 'A command with the name %r already exists.' % name
|
|
|
|
raise AliasError, s
|
2003-09-10 22:25:15 +02:00
|
|
|
if name in self.frozen:
|
2003-10-11 14:48:12 +02:00
|
|
|
raise AliasError, 'Alias %r is frozen.' % 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)
|
|
|
|
if freeze:
|
|
|
|
self.frozen.add(name)
|
|
|
|
|
2003-09-10 23:15:09 +02:00
|
|
|
def removeAlias(self, name, evenIfFrozen=False):
|
|
|
|
name = callbacks.canonicalName(name)
|
|
|
|
if hasattr(self, name) and self.isCommand(name):
|
|
|
|
if evenIfFrozen or name not in self.frozen:
|
|
|
|
delattr(self.__class__, name)
|
2003-09-13 16:46:03 +02:00
|
|
|
self.frozen.discard(name)
|
2003-09-10 23:15:09 +02:00
|
|
|
else:
|
|
|
|
raise AliasError, 'That alias is frozen.'
|
|
|
|
else:
|
|
|
|
raise AliasError, 'There is no such alias.'
|
|
|
|
|
2003-10-24 11:10:10 +02:00
|
|
|
def add(self, irc, msg, args):
|
2003-04-08 09:00:39 +02:00
|
|
|
"""<name> <alias commands>
|
|
|
|
|
|
|
|
Defines an alias <name> for the commands <commands>. The <commands>
|
|
|
|
should be in the standard [command argument [nestedcommand argument]]
|
|
|
|
format. Underscores can be used to represent arguments to the alias
|
|
|
|
itself; for instance ...
|
|
|
|
"""
|
2003-08-29 00:24:33 +02:00
|
|
|
(name, alias) = privmsgs.getArgs(args, needed=2)
|
2003-04-08 09:00:39 +02:00
|
|
|
try:
|
2003-09-10 22:25:15 +02:00
|
|
|
self.addAlias(irc, name, alias)
|
|
|
|
irc.reply(msg, conf.replySuccess)
|
|
|
|
except AliasError, e:
|
|
|
|
irc.error(msg, 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>
|
|
|
|
|
|
|
|
Removes the given alias, if unfrozen.
|
|
|
|
"""
|
|
|
|
name = privmsgs.getArgs(args)
|
2003-09-10 23:15:09 +02:00
|
|
|
try:
|
|
|
|
self.removeAlias(name)
|
|
|
|
irc.reply(msg, conf.replySuccess)
|
|
|
|
except AliasError, e:
|
|
|
|
irc.error(msg, str(e))
|
2003-04-08 09:00:39 +02:00
|
|
|
|
|
|
|
|
|
|
|
Class = Alias
|
|
|
|
|
|
|
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|