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 +++++++++++++++++++++++------------------------------ TODO | 9 ++++++ commands/badcommand | 1 + commands/caps | 1 + commands/retard | 1 - commands/say | 2 ++ config.json | 15 ++++----- 7 files changed, 58 insertions(+), 58 deletions(-) create mode 100644 TODO create mode 100755 commands/badcommand delete mode 100755 commands/retard create mode 100755 commands/say 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() diff --git a/TODO b/TODO new file mode 100644 index 0000000..ba9c4b6 --- /dev/null +++ b/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/commands/badcommand b/commands/badcommand new file mode 100755 index 0000000..c59b4d1 --- /dev/null +++ b/commands/badcommand @@ -0,0 +1 @@ +#? //retard diff --git a/commands/caps b/commands/caps index bc3d7ba..caa1fe0 100755 --- a/commands/caps +++ b/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/commands/retard b/commands/retard deleted file mode 100755 index c59b4d1..0000000 --- a/commands/retard +++ /dev/null @@ -1 +0,0 @@ -#? //retard diff --git a/commands/say b/commands/say new file mode 100755 index 0000000..8b83c05 --- /dev/null +++ b/commands/say @@ -0,0 +1,2 @@ +#!/bin/sh +printf '%s\n' "$*" diff --git a/config.json b/config.json index 0b032c6..7b84c55 100644 --- a/config.json +++ b/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