mirror of
https://github.com/Mikaela/Limnoria.git
synced 2025-01-30 22:24:20 +01:00
parent
c50e27a278
commit
405f4ab8d9
@ -85,32 +85,64 @@ def findBiggestAt(alias):
|
|||||||
return 0
|
return 0
|
||||||
|
|
||||||
def makeNewAlias(name, alias):
|
def makeNewAlias(name, alias):
|
||||||
|
original = alias
|
||||||
if findAliasCommand(name, alias):
|
if findAliasCommand(name, alias):
|
||||||
raise RecursiveAlias
|
raise RecursiveAlias
|
||||||
biggestDollar = findBiggestDollar(alias)
|
biggestDollar = findBiggestDollar(original)
|
||||||
biggestAt = findBiggestAt(alias)
|
biggestAt = findBiggestAt(original)
|
||||||
wildcard = '$*' in alias
|
wildcard = '$*' in original
|
||||||
if biggestAt and wildcard:
|
if biggestAt and wildcard:
|
||||||
raise AliasError, 'Can\'t use $* and optional args (@1, etc.)'
|
raise AliasError, 'Can\'t mix $* and optional args (@1, etc.)'
|
||||||
|
if original.count('$*') > 1:
|
||||||
|
raise AliasError, 'There can be only one $* in an alias.'
|
||||||
def f(self, irc, msg, args):
|
def f(self, irc, msg, args):
|
||||||
alias_ = alias.replace('$nick', msg.nick)
|
alias = original.replace('$nick', msg.nick)
|
||||||
if '$channel' in alias:
|
if '$channel' in original:
|
||||||
channel = privmsgs.getChannel(msg, args)
|
channel = privmsgs.getChannel(msg, args)
|
||||||
alias_ = alias_.replace('$channel', channel)
|
alias = alias.replace('$channel', channel)
|
||||||
|
tokens = callbacks.tokenize(alias)
|
||||||
if not wildcard and biggestDollar or biggestAt:
|
if not wildcard and biggestDollar or biggestAt:
|
||||||
args = privmsgs.getArgs(args, needed=biggestDollar,
|
args = privmsgs.getArgs(args,
|
||||||
|
needed=biggestDollar,
|
||||||
optional=biggestAt)
|
optional=biggestAt)
|
||||||
# Gotta have a tuple.
|
# Gotta have a mutable sequence (for replace).
|
||||||
if biggestDollar + biggestAt == 1 and not wildcard:
|
if biggestDollar + biggestAt == 1: # We got a string, no tuple.
|
||||||
args = (args,)
|
args = [args]
|
||||||
def replace(m):
|
def regexpReplace(m):
|
||||||
idx = int(m.group(1))
|
idx = int(m.group(1))
|
||||||
return utils.dqrepr(args[idx-1])
|
return args[idx-1]
|
||||||
alias_ = dollarRe.sub(replace, alias_)
|
def replace(tokens, replacer):
|
||||||
args = args[biggestDollar:]
|
for (i, token) in enumerate(tokens):
|
||||||
alias_ = atRe.sub(replace, alias_)
|
if isinstance(token, list):
|
||||||
alias_ = alias_.replace('$*', ' '.join(map(utils.dqrepr, args)))
|
replace(token, replacer)
|
||||||
self.Proxy(irc.irc, msg, callbacks.tokenize(alias_))
|
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.irc, msg, tokens)
|
||||||
f = types.FunctionType(f.func_code, f.func_globals,
|
f = types.FunctionType(f.func_code, f.func_globals,
|
||||||
name, closure=f.func_closure)
|
name, closure=f.func_closure)
|
||||||
f.__doc__ ='<an alias, %s>\n\nAlias for %r' % \
|
f.__doc__ ='<an alias, %s>\n\nAlias for %r' % \
|
||||||
|
@ -134,6 +134,14 @@ class AliasTestCase(ChannelPluginTestCase, PluginDocumentation):
|
|||||||
self.assertResponse('myrepr foo', '"foo"')
|
self.assertResponse('myrepr foo', '"foo"')
|
||||||
self.assertResponse('myrepr ""', '""')
|
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')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|
||||||
|
Loading…
Reference in New Issue
Block a user