diff options
author | lassulus <lassulus@googlemail.com> | 2013-12-16 18:19:49 +0100 |
---|---|---|
committer | lassulus <lassulus@googlemail.com> | 2013-12-16 18:19:49 +0100 |
commit | f09b4ea6c6a381b85363fd811b6e6526c76593ce (patch) | |
tree | 18dd4124924ad457641e07094474204ab88cfb0b | |
parent | 58ed45469cab2e25f2a2a7a98de22ac079763063 (diff) |
Reaktor/IRC: use command patterns
-rwxr-xr-x | IRC/asybot.py | 87 | ||||
-rw-r--r-- | TODO | 9 | ||||
-rwxr-xr-x | commands/badcommand (renamed from commands/retard) | 0 | ||||
-rwxr-xr-x | commands/caps | 1 | ||||
-rwxr-xr-x | commands/say | 2 | ||||
-rw-r--r-- | config.json | 15 |
6 files changed, 57 insertions, 57 deletions
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() @@ -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/retard b/commands/badcommand index c59b4d1..c59b4d1 100755 --- a/commands/retard +++ b/commands/badcommand 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/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" ] } ] } } |