From f09b4ea6c6a381b85363fd811b6e6526c76593ce Mon Sep 17 00:00:00 2001 From: lassulus Date: Mon, 16 Dec 2013 18:19:49 +0100 Subject: Reaktor/IRC: use command patterns --- IRC/asybot.py | 87 +++++++++++++++++++++++++---------------------------------- 1 file changed, 37 insertions(+), 50 deletions(-) (limited to 'IRC') diff --git a/IRC/asybot.py b/IRC/asybot.py index 9a18341..ceebe84 100755 --- a/IRC/asybot.py +++ b/IRC/asybot.py @@ -15,7 +15,7 @@ from datetime import datetime as date, timedelta import shlex from time import sleep from sys import exit -from re import split, search +from re import split, search, match from textwrap import TextWrapper import logging,logging.handlers from getconf import make_getconf @@ -85,7 +85,6 @@ class asybot(asychat): _, prefix, command, params, rest, _ = \ split('^(?::(\S+)\s)?(\S+)((?:\s[^:]\S*)*)(?:\s:(.*))?$', message) params = params.split(' ')[1:] - #print([prefix, command, params, rest]) if command == 'PING': self.push('PONG :%s' % rest) @@ -124,57 +123,45 @@ class asybot(asychat): sleep(1) def ME(text): - PRIVMSG('ACTION ' + text + '') + PRIVMSG(('ACTION ' + text + '').encode(encoding='UTF-8')) - _from = prefix.split('!', 1)[0] + for command in getconf('irc.commands'): + y = match(command['pattern'], rest) + if y: + self.execute_command(command, y, PRIVMSG, ME) + def execute_command(self, command, match, PRIVMSG, ME): + from os.path import realpath, dirname, join + from subprocess import Popen as popen, PIPE + from time import time + + #TODO: allow only commands below ./commands/ + exe = join(dirname(realpath(dirname(__file__))), command['argv'][0]) + myargv = [exe] + command['argv'][1:] + + env = {} + start = time() try: - _, _handle, _command, _argument, _ = split( - '^(\w+|\*):\s*(\w+)(?:\s+(.*))?$', rest) - except (ValueError, Exception): - if search(self.nickname, rest): - PRIVMSG('I\'m so famous'.encode(encoding='UTF-8')) - return # ignore - - if _handle == self.nickname or _handle == '*': - - from os.path import realpath, dirname, join - from subprocess import Popen as popen, PIPE - from time import time - Reaktor_dir = dirname(realpath(dirname(__file__))) - public_commands = join(Reaktor_dir, 'public_commands') - command = join(public_commands, _command) - - if is_executable(command): - - env = {} - args = [] - start = time() - if _argument != None: - env['argument'] = _argument - args = shlex.split(_argument) - try: - p = popen([command] + args,bufsize=1, stdout=PIPE, stderr=PIPE, env=env) - except (OSError, Exception): - ME('brain damaged') - log.error('OSError@%s: %s' % (command, error)) - return - pid = p.pid - for line in iter(p.stdout.readline, ''.encode(encoding='UTF-8')): - PRIVMSG(translate_colors(line)) - log.debug('%s stdout: %s' % (pid, line)) - p.wait() - elapsed = time() - start - code = p.returncode - log.info('command: %s -> %s in %d seconds' % (command, code,elapsed)) - [log.debug('%s stderr: %s' % (pid, x)) for x in p.stderr.readlines()] - - if code != 0: - ME('mimimi') - - else: - if _handle != '*': - PRIVMSG(_from + ': you are made of stupid') + p = popen(myargv, bufsize=1, stdout=PIPE, stderr=PIPE, env=env) + except (OSError, Exception) as error: + ME('brain damaged') + log.error('OSError@%s: %s' % (myargv, error)) + return + pid = p.pid + for line in iter(p.stdout.readline, ''.encode(encoding='UTF-8')): + try: + PRIVMSG(translate_colors(line)) + except Exception as error: + log.error('no send: %s' % error) + log.debug('%s stdout: %s' % (pid, line)) + p.wait() + elapsed = time() - start + code = p.returncode + log.info('command: %s -> %s in %d seconds' % (myargv, code, elapsed)) + [log.debug('%s stderr: %s' % (pid, x)) for x in p.stderr.readlines()] + + if code != 0: + ME('mimimi') if __name__ == "__main__": asybot() -- cgit v1.2.3