Add support for adding global or 'other-channel'-specific Akas.

This commit is contained in:
Valentin Lorentz 2013-07-23 21:10:26 +02:00
parent c2f039c60b
commit e9cbb1f60b
2 changed files with 28 additions and 13 deletions

View File

@ -101,10 +101,15 @@ if sqlalchemy:
return list_ return list_
def get_alias(self, channel, name): def get_alias(self, channel, name):
return self.get_db(channel).query(Alias.alias) \ try:
.filter(Alias.name == name).one()[0] return self.get_db(channel).query(Alias.alias) \
.filter(Alias.name == name).one()[0]
except sqlalchemy.orm.exc.NoResultFound:
return None
def add_aka(self, channel, name, alias): def add_aka(self, channel, name, alias):
if self.has_aka(channel, name):
raise AliasError(_('This Aka already exists.'))
if sys.version_info[0] < 3: if sys.version_info[0] < 3:
if isinstance(name, str): if isinstance(name, str):
name = name.decode('utf8') name = name.decode('utf8')
@ -178,7 +183,7 @@ class Aka(callbacks.Plugin):
def listCommands(self): def listCommands(self):
channel = dynamic.channel channel = dynamic.channel
return set(self._db.get_aka_list(channel) + return set(self._db.get_aka_list(channel) +
self._db.get_aka_list() + self._db.get_aka_list('global') +
self.__parent.listCommands()) self.__parent.listCommands())
def getCommandMethod(self, command=None, name=None): def getCommandMethod(self, command=None, name=None):
@ -246,23 +251,23 @@ class Aka(callbacks.Plugin):
return f return f
def _add_aka(self, channel, name, alias): def _add_aka(self, channel, name, alias):
if self.isCommandMethod(name): if self.__parent.isCommandMethod(name):
raise AliasError(_('You can\'t overwrite commands in ' raise AliasError(_('You can\'t overwrite commands in '
'this plugin.')) 'this plugin.'))
biggestDollar = findBiggestDollar(alias) biggestDollar = findBiggestDollar(alias)
biggestAt = findBiggestAt(alias) biggestAt = findBiggestAt(alias)
wildcard = '$*' in alias wildcard = '$*' in alias
if biggestAt and wildcard: if biggestAt and wildcard:
raise AliasError('Can\'t mix $* and optional args (@1, etc.)') raise AliasError(_('Can\'t mix $* and optional args (@1, etc.)'))
if alias.count('$*') > 1: if alias.count('$*') > 1:
raise AliasError('There can be only one $* in an alias.') raise AliasError(_('There can be only one $* in an alias.'))
self._db.add_aka(channel, name, alias) self._db.add_aka(channel, name, alias)
def _remove_aka(self, channel, name): def _remove_aka(self, channel, name):
self._db.remove_aka(channel, name) self._db.remove_aka(channel, name)
def add(self, irc, msg, args, channel, name, alias): def add(self, irc, msg, args, optlist, name, alias):
"""[<#channel|global>] <name> <command> """[--channel <#channel>] <name> <command>
Defines an alias <name> that executes <command>. The <command> Defines an alias <name> that executes <command>. The <command>
should be in the standard "command argument [nestedcommand argument]" should be in the standard "command argument [nestedcommand argument]"
@ -271,6 +276,13 @@ class Aka(callbacks.Plugin):
etc. can be used for optional arguments. $* simply means "all etc. can be used for optional arguments. $* simply means "all
remaining arguments," and cannot be combined with optional arguments. remaining arguments," and cannot be combined with optional arguments.
""" """
channel = 'global'
for (option, arg) in optlist:
if option == 'channel':
if not ircutils.isChannel(arg):
irc.error(_('%r is not a valid channel.') % arg,
Raise=True)
channel = arg
if ' ' not in alias: if ' ' not in alias:
# If it's a single word, they probably want $*. # If it's a single word, they probably want $*.
alias += ' $*' alias += ' $*'
@ -281,8 +293,9 @@ class Aka(callbacks.Plugin):
irc.replySuccess() irc.replySuccess()
except AliasError as e: except AliasError as e:
irc.error(str(e)) irc.error(str(e))
add = wrap(add, [first(('literal', 'global'), 'channel'), add = wrap(add, [getopts({
'commandName', 'text']) 'channel': 'somethingWithoutSpaces',
}),'commandName', 'text'])
def remove(self, irc, msg, args, channel, name): def remove(self, irc, msg, args, channel, name):
"""[<#channel|global>] <name> """[<#channel|global>] <name>

View File

@ -129,12 +129,14 @@ class AkaTestCase(ChannelPluginTestCase):
def testChannelPriority(self): def testChannelPriority(self):
self.assertNotError('aka add spam "echo foo"') self.assertNotError('aka add spam "echo foo"')
self.assertNotError('aka add --channel #channel spam "echo bar"') self.assertNotError('aka add --channel %s spam "echo bar"' %
self.channel)
self.assertResponse('spam', 'bar') self.assertResponse('spam', 'bar')
self.assertNotError('aka add --channel #channel egg "echo baz"') self.assertNotError('aka add --channel %s egg "echo baz"' %
self.channel)
self.assertNotError('aka add egg "echo qux"') self.assertNotError('aka add egg "echo qux"')
self.assertResponse('spam', 'baz') self.assertResponse('egg', 'baz')
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: # vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: