diff options
Diffstat (limited to 'Reaktor/IRC/reaktor.py')
-rwxr-xr-x[-rw-r--r--] | Reaktor/IRC/reaktor.py | 80 |
1 files changed, 63 insertions, 17 deletions
diff --git a/Reaktor/IRC/reaktor.py b/Reaktor/IRC/reaktor.py index 990d47e5..f9f25e57 100644..100755 --- a/Reaktor/IRC/reaktor.py +++ b/Reaktor/IRC/reaktor.py @@ -1,3 +1,4 @@ +#!/usr/bin/env python3 import os from ircasy import asybot from asyncore import loop @@ -5,29 +6,53 @@ from translate_colors import translate_colors import shlex from re import split, search, match -config_filename = './config.py' +default_config = './config.py' from getconf import make_getconf -getconf = make_getconf(config_filename) +getconf = None import logging,logging.handlers log = logging.getLogger('asybot') -hdlr = logging.handlers.SysLogHandler(facility=logging.handlers.SysLogHandler.LOG_DAEMON) -formatter = logging.Formatter( '%(filename)s: %(levelname)s: %(message)s') -hdlr.setFormatter(formatter) -log.addHandler(hdlr) -logging.basicConfig(level = logging.DEBUG if getconf('debug') else logging.INFO) +#hdlr = logging.handlers.SysLogHandler(address='/dev/log', facility=logging.handlers.SysLogHandler.LOG_DAEMON) +#formatter = logging.Formatter( '%(filename)s: %(levelname)s: %(message)s') +#hdlr.setFormatter(formatter) +#log.addHandler(hdlr) + class Reaktor(asybot): - def __init__(self): + def __init__(self,config=default_config): + self.config = config + log.info("using config file %s"%(config)) asybot.__init__(self, getconf('irc_server'), getconf('irc_port'), getconf('irc_nickname'), getconf('irc_channels'), hammer_interval=getconf('irc_hammer_interval'), alarm_timeout=getconf('irc_alarm_timeout'), kill_timeout=getconf('irc_kill_timeout')) + def is_admin(self,prefix): + try: + with open(getconf('auth_file')) as f: + for line in f: + if line.strip() == prefix: + return True + except Exception as e: + log.info(e) + return False + + def on_join(self, prefix, command, params, rest): + for command in getconf('on_join', []): + self.execute_command(command, None, prefix, params) + def on_privmsg(self, prefix, command, params, rest): for command in getconf('commands'): y = match(command['pattern'], rest) if y: - self.execute_command(command, y, prefix, params) - break + if not self.is_admin(prefix): + self.PRIVMSG(params,'unauthorized!') + else: + return self.execute_command(command, y, prefix, params) + + for command in getconf('public_commands'): + y = match(command['pattern'], rest) + if y: + return self.execute_command(command, y, prefix, params) + def execute_command(self, command, match, prefix, target): from os.path import realpath, dirname, join @@ -37,16 +62,33 @@ class Reaktor(asybot): #TODO: allow only commands below ./commands/ exe = join(dirname(realpath(dirname(__file__))), command['argv'][0]) myargv = [exe] + command['argv'][1:] - if match.groupdict().get('args',None): - myargv += shlex.split(match.groupdict()['args']) + try: + if match and match.groupdict().get('args', None): + myargv += shlex.split(match.groupdict()['args']) + except: + log.info("cannot parse args!") - env = {} + cwd = getconf('workdir') + if not os.access(cwd,os.W_OK): + log.error("Workdir '%s' is not Writable! Falling back to root dir"%cwd) + cwd = "/" + + env = command.get('env', {}) + env['_prefix'] = prefix env['_from'] = prefix.split('!', 1)[0] - env['config_filename'] = os.path.abspath(config_filename) + + log.debug('self:' +self.nickname) + # when receiving /query, answer to the user, not to self + if self.nickname in target: + target.remove(self.nickname) + target.append(env['_from']) + log.debug('target:' +str(target)) + start = time() try: - p = popen(myargv, bufsize=1, stdout=PIPE, stderr=PIPE, env=env) - except (OSError, Exception) as error: + print(myargv) + p = popen(myargv, bufsize=1, stdout=PIPE, stderr=PIPE, env=env, cwd=cwd) + except Exception as error: self.ME(target, 'brain damaged') log.error('OSError@%s: %s' % (myargv, error)) return @@ -67,5 +109,9 @@ class Reaktor(asybot): self.ME(target, 'mimimi') if __name__ == "__main__": - Reaktor() + import sys + conf = sys.argv[1] if len(sys.argv) == 2 else default_config + getconf = make_getconf(conf) + logging.basicConfig(level = logging.DEBUG if getconf('debug') else logging.INFO) + Reaktor(conf) loop() |