diff options
-rwxr-xr-x | IRC/asybot.py | 82 | ||||
-rw-r--r-- | IRC/getconf.py | 20 | ||||
-rwxr-xr-x | IRC/index | 2 | ||||
-rw-r--r-- | IRC/translate_colors.py | 2 | ||||
-rw-r--r-- | config.json | 27 |
5 files changed, 76 insertions, 57 deletions
diff --git a/IRC/asybot.py b/IRC/asybot.py index 2cb533e..7fcc2ea 100755 --- a/IRC/asybot.py +++ b/IRC/asybot.py @@ -18,37 +18,42 @@ from sys import exit from re import split, search from textwrap import TextWrapper import logging,logging.handlers +from getconf import make_getconf +getconf = make_getconf('config.json') 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('main.debug') else logging.INFO) # s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g -- removes color codes class asybot(asychat): - def __init__(self, server, port, nickname, targets, **kwargs): + def __init__(self): asychat.__init__(self) - self.server = server - self.port = port - self.nickname = nickname - self.targets = targets - self.username = kwargs['username'] if 'username' in kwargs else nickname - self.hostname = kwargs['hostname'] if 'hostname' in kwargs else nickname - self.ircname = kwargs['ircname'] if 'ircname' in kwargs else nickname - self.realname = kwargs['realname'] if 'realname' in kwargs else nickname + self.server = getconf('irc.server') + self.port = getconf('irc.port') + self.channels = getconf('irc.channels') + self.realname = getconf('irc.nickname') + self.nickname = getconf('irc.nickname') + self.username = getconf('irc.nickname') + self.hostname = getconf('irc.nickname') + self.ircname = getconf('irc.nickname') self.data = '' - self.set_terminator('\r\n') + self.set_terminator('\r\n'.encode(encoding='UTF-8')) self.create_socket(AF_INET, SOCK_STREAM) self.connect((self.server, self.port)) self.wrapper = TextWrapper(subsequent_indent=" ",width=400) + log.info('=> irc://%s@%s:%s/%s' % (self.nickname, self.server, self.port, self.channels)) + # When we don't receive data for alarm_timeout seconds then issue a # PING every hammer_interval seconds until kill_timeout seconds have # passed without a message. Any incoming message will reset alarm. - self.alarm_timeout = 300 - self.hammer_interval = 10 + self.alarm_timeout = getconf('irc.alarm_timeout') + self.hammer_interval = getconf('irc.hammer_interval') self.kill_timeout = 360 signal(SIGALRM, lambda signum, frame: self.alarm_handler()) self.reset_alarm() @@ -69,7 +74,7 @@ class asybot(asychat): alarm(self.hammer_interval) def collect_incoming_data(self, data): - self.data += data + self.data += data.decode(encoding='UTF-8') def found_terminator(self): log.debug('<< %s' % self.data) @@ -100,18 +105,19 @@ class asybot(asychat): self.reset_alarm() def push(self, message): - log.debug('>> %s' % message) - asychat.push(self, message + self.get_terminator()) + msg = (message + self.get_terminator().decode(encoding='UTF-8')).encode(encoding='UTF-8') + log.debug('>> %s' % msg) + asychat.push(self, msg) def handle_connect(self): self.push('NICK %s' % self.nickname) self.push('USER %s %s %s :%s' % (self.username, self.hostname, self.server, self.realname)) - self.push('JOIN %s' % ','.join(self.targets)) + self.push('JOIN %s' % ','.join(self.channels)) def on_privmsg(self, prefix, command, params, rest): def PRIVMSG(text): - for line in self.wrapper.wrap(text): + for line in self.wrapper.wrap(text.decode(encoding='UTF-8')): msg = 'PRIVMSG %s :%s' % (','.join(params), line) log.info(msg) self.push(msg) @@ -125,7 +131,7 @@ class asybot(asychat): try: _, _handle, _command, _argument, _ = split( '^(\w+|\*):\s*(\w+)(?:\s+(.*))?$', rest) - except ValueError, error: + except (ValueError, Exception): if search(self.nickname, rest): PRIVMSG('I\'m so famous') return # ignore @@ -149,12 +155,12 @@ class asybot(asychat): args = shlex.split(_argument) try: p = popen([command] + args,bufsize=1, stdout=PIPE, stderr=PIPE, env=env) - except OSError, error: + except (OSError, Exception): ME('brain damaged') log.error('OSError@%s: %s' % (command, error)) return pid = p.pid - for line in iter(p.stdout.readline,""): + for line in iter(p.stdout.readline, ''.encode(encoding='UTF-8')): PRIVMSG(translate_colors(line)) log.debug('%s stdout: %s' % (pid, line)) p.wait() @@ -169,41 +175,7 @@ class asybot(asychat): else: if _handle != '*': PRIVMSG(_from + ': you are made of stupid') - -# retrieve the value of a [singleton] variable from a tinc.conf(5)-like file -def getconf1(x, path): - from re import findall - pattern = '(?:^|\n)\s*' + x + '\s*=\s*(.*\w)\s*(?:\n|$)' - y = findall(pattern, open(path, 'r').read()) - if len(y) < 1: - raise AttributeError("len(getconf1('%s', '%s') < 1)" % (x, path)) - if len(y) > 1: - y = ' '.join(y) - raise AttributeError("len(getconf1('%s', '%s') > 1)\n ====> %s" - % (x, path, y)) - return y[0] - if __name__ == "__main__": - from os import environ as env - - lol = logging.DEBUG if env.get('debug',False) else logging.INFO - logging.basicConfig(level=lol) - try: - name = getconf1('Name', '/etc/tinc/retiolum/tinc.conf') - hostname = '%s.retiolum' % name - except: - name = gethostname() - hostname = name - nick = str(env.get('nick', name)) - host = str(env.get('host', 'supernode')) - port = int(env.get('port', 6667)) - target = str(env.get('target', '#retiolum')) - log.info('=> irc://%s@%s:%s/%s' % (nick, host, port, target)) - - from getpass import getuser - asybot(host, port, nick, [target], username=getuser(), - ircname='//Reaktor running at %s' % hostname, - hostname=hostname) - + asybot() loop() diff --git a/IRC/getconf.py b/IRC/getconf.py new file mode 100644 index 0000000..5fdd1cd --- /dev/null +++ b/IRC/getconf.py @@ -0,0 +1,20 @@ +#getconf = make_getconf("dateiname.json") +#getconf(key) -> value +#oder error + +import json + +def make_getconf(filename): + def getconf(prop): + prop_split = prop.split('.') + string = '' + file = open(filename) + for line in file.readlines(): + string+=line + parsed = json.loads(string) + tmp = parsed + for pr in prop_split: + tmp = tmp[pr] + + return tmp + return getconf @@ -3,4 +3,4 @@ set -xeuf # cd //Reaktor cd $(dirname $(readlink -f $0))/.. -host=irc.freenode.net target='#krebs' python IRC/asybot.py "$@" +exec IRC/asybot.py "$@" diff --git a/IRC/translate_colors.py b/IRC/translate_colors.py index bd71661..3ea34be 100644 --- a/IRC/translate_colors.py +++ b/IRC/translate_colors.py @@ -23,7 +23,7 @@ COLOR_MAP = { } def translate_colors (line): for color,replace in COLOR_MAP.items(): - line = line.replace(color,replace) + line = line.replace(color.encode(encoding='UTF-8'),replace.encode(encoding='UTF-8')) return line if __name__ == "__main__": diff --git a/config.json b/config.json new file mode 100644 index 0000000..0b032c6 --- /dev/null +++ b/config.json @@ -0,0 +1,27 @@ +{ + "main": { + "debug": true, + "name": "asybot" + }, + "irc": { + "alarm_timeout": 300, + "hammer_interval": 10, + "kill_timeout": 360, + "nickname": "asybot", + "server": "irc.freenode.org", + "port": 6667, + "channels": [ + "#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" ] } + ] + } +} + |