mirror of
https://github.com/Mikaela/Limnoria.git
synced 2025-02-06 17:44:09 +01:00
Fixed makeNewAlias to allow and better argument handling.
This commit is contained in:
parent
4c67a96b49
commit
f6a286b311
@ -41,9 +41,7 @@ Commands include:
|
|||||||
|
|
||||||
from baseplugin import *
|
from baseplugin import *
|
||||||
|
|
||||||
import new
|
import re
|
||||||
import copy
|
|
||||||
import traceback
|
|
||||||
|
|
||||||
import conf
|
import conf
|
||||||
import debug
|
import debug
|
||||||
@ -61,54 +59,49 @@ def configure(onStart, afterConnect, advanced):
|
|||||||
class RecursiveAlias(Exception):
|
class RecursiveAlias(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def findString(s, args):
|
def findAliasCommand(s, alias):
|
||||||
n = 0
|
r = re.compile(r'(^|\[)\s*%s' % s)
|
||||||
for elt in args:
|
return bool(r.search(alias))
|
||||||
if type(elt) == list:
|
|
||||||
n += findString(s, elt)
|
|
||||||
elif elt == s:
|
|
||||||
n += 1
|
|
||||||
return n
|
|
||||||
|
|
||||||
def findDollars(args, soFar=None, L=None):
|
dollarRe = re.compile(r'\$(\d+)')
|
||||||
if soFar is None:
|
def findBiggestDollar(alias):
|
||||||
soFar = []
|
dollars = dollarRe.findall(alias)
|
||||||
if L is None:
|
dollars.sort()
|
||||||
L = []
|
if dollars:
|
||||||
for (i, elt) in enumerate(args):
|
return int(dollars[-1])
|
||||||
if type(elt) == list:
|
else:
|
||||||
nextSoFar = soFar[:]
|
return None
|
||||||
nextSoFar.append(i)
|
|
||||||
findDollars(elt, soFar=nextSoFar, L=L)
|
|
||||||
if len(elt) >= 2:
|
|
||||||
if elt[0] == '$' and elt[1:].isdigit():
|
|
||||||
mySoFar = soFar[:]
|
|
||||||
mySoFar.append(i)
|
|
||||||
L.append((mySoFar, elt))
|
|
||||||
return L
|
|
||||||
|
|
||||||
def replaceDollars(dollars, aliasArgs, realArgs):
|
def makeNewAlias(name, alias):
|
||||||
for (indexes, dollar) in dollars:
|
if findAliasCommand(name, alias):
|
||||||
L = aliasArgs
|
|
||||||
for i in indexes[:-1]:
|
|
||||||
L = L[i]
|
|
||||||
L[indexes[-1]] = realArgs[int(dollar[1:])-1]
|
|
||||||
|
|
||||||
def makeNewAlias(name, alias, aliasArgs):
|
|
||||||
if findString(name, aliasArgs):
|
|
||||||
raise RecursiveAlias
|
raise RecursiveAlias
|
||||||
dollars = findDollars(aliasArgs)
|
doChannel = bool(alias.find('$channel') != -1)
|
||||||
numDollars = len(dollars)
|
biggestDollar = findBiggestDollar(alias)
|
||||||
|
doDollars = bool(biggestDollar)
|
||||||
|
if biggestDollar is not None:
|
||||||
|
biggestDollar = int(biggestDollar)
|
||||||
def f(self, irc, msg, args):
|
def f(self, irc, msg, args):
|
||||||
#debug.printf('%s being called' % name)
|
alias_ = alias
|
||||||
realArgs = privmsgs.getArgs(args, needed=numDollars)
|
if doChannel:
|
||||||
myArgs = copy.deepcopy(aliasArgs)
|
channel = privmsgs.getChannel(msg, args)
|
||||||
if dollars:
|
alias_ = alias.replace('$channel', channel)
|
||||||
replaceDollars(dollars, myArgs, realArgs)
|
if doDollars:
|
||||||
else:
|
debug.printf(args)
|
||||||
myArgs.extend(args)
|
args = privmsgs.getArgs(args, needed=biggestDollar)
|
||||||
self.Proxy(irc, msg, myArgs)
|
if biggestDollar == 1:
|
||||||
f.__doc__ = '<an alias, arguments unknown>\n\nAlias for %r' % alias
|
args = (args,)
|
||||||
|
debug.printf(args)
|
||||||
|
def replace(m):
|
||||||
|
debug.printf(m.group(1))
|
||||||
|
idx = int(m.group(1))
|
||||||
|
debug.printf(args[idx-1])
|
||||||
|
return args[idx-1]
|
||||||
|
debug.printf(alias_)
|
||||||
|
alias_ = dollarRe.sub(replace, alias_)
|
||||||
|
debug.printf(alias_)
|
||||||
|
debug.printf(alias_)
|
||||||
|
self.Proxy(irc, msg, callbacks.tokenize(alias_))
|
||||||
|
f.__doc__ ='<an alias, %s arguments>\n\nAlias for %r'%(biggestDollar,alias)
|
||||||
#f = new.function(f.func_code, f.func_globals, name)
|
#f = new.function(f.func_code, f.func_globals, name)
|
||||||
return f
|
return f
|
||||||
|
|
||||||
@ -164,9 +157,8 @@ class Alias(callbacks.Privmsg):
|
|||||||
if name in self.frozen:
|
if name in self.frozen:
|
||||||
irc.error(msg, 'That alias is frozen.')
|
irc.error(msg, 'That alias is frozen.')
|
||||||
return
|
return
|
||||||
args = callbacks.tokenize(alias)
|
|
||||||
try:
|
try:
|
||||||
f = makeNewAlias(name, alias, args)
|
f = makeNewAlias(name, alias)
|
||||||
except RecursiveAlias:
|
except RecursiveAlias:
|
||||||
irc.error(msg, 'You can\'t define a recursive alias.')
|
irc.error(msg, 'You can\'t define a recursive alias.')
|
||||||
#debug.printf('setting attribute')
|
#debug.printf('setting attribute')
|
||||||
|
Loading…
Reference in New Issue
Block a user