From 884fb902e9936993e13fe87f46fcd0e1fc052499 Mon Sep 17 00:00:00 2001 From: lassulus Date: Mon, 16 Dec 2013 18:19:49 +0100 Subject: Reaktor/IRC: use command patterns --- Reaktor/IRC/asybot.py | 87 +++++++++++++++++++-------------------------- Reaktor/TODO | 9 +++++ Reaktor/commands/badcommand | 1 + Reaktor/commands/caps | 1 + Reaktor/commands/retard | 1 - Reaktor/commands/say | 2 ++ Reaktor/config.json | 15 ++++---- 7 files changed, 58 insertions(+), 58 deletions(-) create mode 100644 Reaktor/TODO create mode 100755 Reaktor/commands/badcommand delete mode 100755 Reaktor/commands/retard create mode 100755 Reaktor/commands/say diff --git a/Reaktor/IRC/asybot.py b/Reaktor/IRC/asybot.py index 9a183413..ceebe844 100755 --- a/Reaktor/IRC/asybot.py +++ b/Reaktor/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() diff --git a/Reaktor/TODO b/Reaktor/TODO new file mode 100644 index 00000000..ba9c4b6e --- /dev/null +++ b/Reaktor/TODO @@ -0,0 +1,9 @@ +{ "pattern": "^(?:asybot|\\*):.*", "argv": [ "commands/say", "{{from.nickname}}: you are made of stupid" ], only_match: true } + +getconf: check syntax and semantics on load +getconf: reload inotify + +apropros caps: commands need access to config + +commands need access to from (eg as env var): + _from = prefix.split('!', 1)[0] diff --git a/Reaktor/commands/badcommand b/Reaktor/commands/badcommand new file mode 100755 index 00000000..c59b4d1c --- /dev/null +++ b/Reaktor/commands/badcommand @@ -0,0 +1 @@ +#? //retard diff --git a/Reaktor/commands/caps b/Reaktor/commands/caps index bc3d7ba2..caa1fe06 100755 --- a/Reaktor/commands/caps +++ b/Reaktor/commands/caps @@ -1,4 +1,5 @@ #! /bin/sh +exec echo 'TODO: need access to config.json' set -euf cd public_commands echo `ls` diff --git a/Reaktor/commands/retard b/Reaktor/commands/retard deleted file mode 100755 index c59b4d1c..00000000 --- a/Reaktor/commands/retard +++ /dev/null @@ -1 +0,0 @@ -#? //retard diff --git a/Reaktor/commands/say b/Reaktor/commands/say new file mode 100755 index 00000000..8b83c056 --- /dev/null +++ b/Reaktor/commands/say @@ -0,0 +1,2 @@ +#!/bin/sh +printf '%s\n' "$*" diff --git a/Reaktor/config.json b/Reaktor/config.json index 0b032c60..7b84c55e 100644 --- a/Reaktor/config.json +++ b/Reaktor/config.json @@ -14,13 +14,14 @@ "#krebs" ], "commands": [ - { "pattern": "^{main.name}:\\s*caps\\s*$", "argv": [ "command/caps" ] }, - { "pattern": "^{main.name}:\\s*hello\\s*$", "argv": [ "command/hello" ] }, - { "pattern": "^{main.name}:\\s*reload\\s*$", "argv": [ "command/reload" ] }, - { "pattern": "^{main.name}:\\s*retard\\s*$", "argv": [ "command/retard" ] }, - { "pattern": "^{main.name}:\\s*rev\\s*$", "argv": [ "command/rev" ] }, - { "pattern": "^{main.name}:\\s*uptime\\s*$", "argv": [ "command/uptime" ] }, - { "pattern": "{main.name}", "argv": [ "command/say", "I'm famous" ] } + { "pattern": "^(?:asybot|\\*):\\s*caps\\s*$", "argv": [ "commands/caps" ] }, + { "pattern": "^(?:asybot|\\*):\\s*hello\\s*$", "argv": [ "commands/hello" ] }, + { "pattern": "^(?:asybot|\\*):\\s*reload\\s*$", "argv": [ "commands/reload" ] }, + { "pattern": "^(?:asybot|\\*):\\s*badcommand\\s*$", "argv": [ "commands/badcommand" ] }, + { "pattern": "^(?:asybot|\\*):\\s*rev\\s*$", "argv": [ "commands/rev" ] }, + { "pattern": "^(?:asybot|\\*):\\s*uptime\\s*$", "argv": [ "commands/uptime" ] }, + { "pattern": "^(?:asybot|\\*):\\s*nocommand\\s*$", "argv": [ "commands/nocommand" ] }, + { "pattern": "^.*\\basybot(?:\\b[^:].*)?$", "argv": [ "commands/say", "I'm famous" ] } ] } } -- cgit v1.2.3