#!/usr/bin/python # -*- coding: utf-8 -*- # Talisman module # macros.py # Initial Copyright © 2007 dimichxp # Modifications Copyright © 2007 Als # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. import random,string,re,os def shell_esc(s): for c in [';', '&', '|', '`', '$', '\\', '#']: s = s.replace(c, '') return s def xml_esc(s): s = s.replace('\'', ''') s = s.replace('>', '>') s = s.replace('<', '<') s = s.replace('&', '&') s = s.replace('\"', '"') return s def macro_get_rand(args, source): try: f=int(args[0]) t=int(args[1]) return str(random.randrange(f, t)) except: return '' def macro_shell_escape(args, source): return shell_esc(args[0]) def macro_xml_escape(args, source): return xml_esc(args[0]) def macro_context(args, source): arg = args[0] if arg == 'conf': return xml_esc(source[1]) elif arg == 'nick': return xml_esc(source[2]) elif arg == 'conf_jid': return xml_esc(source[0]) else: return '' def read_file(filename): fp = file(filename) data = fp.read() fp.close() return data def write_file(filename, data): fp = file(filename, 'w') fp.write(data) fp.close() class MacroCommands: commands={ 'rand': [2, macro_get_rand ], 'shell_escape': [1, macro_shell_escape], 'xml_escape': [1, macro_xml_escape ], 'context': [1, macro_context ] } def map_char(self, x, i): st=i['state'] if i['esc']: i['esc']=False ret=i['level'] elif x == '\\': i['esc']=True ret=0 elif x == '%': i['state']='cmd_p' ret=0 elif x == '(': if i['state'] == 'cmd_p': i['level']+=1 i['state'] = 'args' ret=0 elif x == ')': if i['state'] == 'args': i['state'] = 'null' ret=0 else: if i['state'] == 'args': ret = i['level'] else: i['state'] = 'null' ret = 0 return ret def get_map(self, inp): i={'level': 0, 'state': 'null', 'esc': False} return [self.map_char(x, i) for x in list(inp)] def parse_cmd(self, me): i = 0 m = self.get_map(me) args=[''] * max(m) while i