diff options
-rwxr-xr-x | Reaktor/IRC/asybot.py | 184 | ||||
-rwxr-xr-x | Reaktor/IRC/bot.py | 34 | ||||
-rwxr-xr-x | Reaktor/IRC/bot2.py | 130 | ||||
-rw-r--r-- | Reaktor/IRC/content | 1 | ||||
-rwxr-xr-x | Reaktor/IRC/index | 4 | ||||
-rwxr-xr-x | Reaktor/IRC/install | 28 | ||||
-rw-r--r-- | Reaktor/README.md | 23 | ||||
-rwxr-xr-x | Reaktor/commands/reload | 2 | ||||
-rwxr-xr-x | Reaktor/index | 7 | ||||
-rwxr-xr-x | Reaktor/install | 6 | ||||
-rwxr-xr-x | retiolum/bin/get-tinc-dir | 9 |
11 files changed, 212 insertions, 216 deletions
diff --git a/Reaktor/IRC/asybot.py b/Reaktor/IRC/asybot.py new file mode 100755 index 00000000..1a84b9d4 --- /dev/null +++ b/Reaktor/IRC/asybot.py @@ -0,0 +1,184 @@ +#! /usr/bin/env python +# +# //Reaktor/IRC/asybot.py +# + +from __future__ import print_function + +def is_executable(x): + import os + return os.path.exists(x) and os.access(x, os.X_OK) + +from asynchat import async_chat as asychat +from asyncore import loop +from socket import AF_INET, SOCK_STREAM +from signal import SIGALRM, signal, alarm +from datetime import datetime as date, timedelta +from sys import exit +from re import split, search +class asybot(asychat): + def __init__(self, server, port, nickname, targets, **kwargs): + 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.data = '' + self.set_terminator('\r\n') + self.create_socket(AF_INET, SOCK_STREAM) + self.connect((self.server, self.port)) + + # 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.kill_timeout = 360 + signal(SIGALRM, lambda signum, frame: self.alarm_handler()) + self.reset_alarm() + + loop() + + def reset_alarm(self): + self.last_activity = date.now() + alarm(self.alarm_timeout) + + def alarm_handler(self): + delta = date.now() - self.last_activity + if delta > timedelta(seconds=self.kill_timeout): + print('No data for %s. Giving up...' % delta) + exit(2) + else: + print('No data for %s. PINGing server...' % delta) + self.push('PING :%s' % self.nickname) + alarm(self.hammer_interval) + + def collect_incoming_data(self, data): + self.data += data + + def found_terminator(self): + print('< %s' % self.data) + + message = self.data + self.data = '' + + _, 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) + + elif command == 'PRIVMSG': + self.on_privmsg(prefix, command, params, rest) + + elif command == '433': + # ERR_NICKNAMEINUSE, retry with another name + _, nickname, int, _ = split('^.*[^0-9]([0-9]+)$', self.nickname) \ + if search('[0-9]$', self.nickname) \ + else ['', self.nickname, 0, ''] + self.nickname = nickname + str(int + 1) + self.handle_connect() + + self.reset_alarm() + + def push(self, message): + print('> %s' % message) + asychat.push(self, message + self.get_terminator()) + + 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)) + + def on_privmsg(self, prefix, command, params, rest): + def PRIVMSG(text): + self.push('PRIVMSG %s :%s' % (','.join(params), text)) + + def ME(text): + PRIVMSG('ACTION ' + text + '') + + _from = prefix.split('!', 1)[0] + + try: + _, _handle, _command, _argument, _ = split( + '^(\w+|\*):\s*(\w+)(?:\s+(.*))?$', rest) + except ValueError, error: + if search(self.nickname, rest): + PRIVMSG('I\'m so famous') + return # ignore + + if _handle == self.nickname or _handle == '*': + + from os.path import realpath, dirname, join + from subprocess import Popen as popen, PIPE + + Reaktor_dir = dirname(realpath(dirname(__file__))) + public_commands = join(Reaktor_dir, 'public_commands') + command = join(public_commands, _command) + + if is_executable(command): + + env = {} + if _argument != None: + env['argument'] = _argument + + try: + p = popen([command], stdin=PIPE, stdout=PIPE, stderr=PIPE, env=env) + except OSError, error: + ME('is made of stupid') + print('OSError@%s: %s' % (command, error)) + return + + stdout, stderr = [ x[:len(x)-1] for x in + [ x.split('\n') for x in p.communicate()]] + code = p.returncode + pid = p.pid + + print('command: %s -> %s' % (command, code)) + [print('%s stdout: %s' % (pid, x)) for x in stdout] + [print('%s stderr: %s' % (pid, x)) for x in stderr] + + if code == 0: + [PRIVMSG(x) for x in stdout] + [PRIVMSG(x) for x in stderr] + else: + ME('mimimi') + + 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 + name = getconf1('Name', '/etc/tinc/retiolum/tinc.conf') + hostname = '%s.retiolum' % 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')) + print('====> 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) diff --git a/Reaktor/IRC/bot.py b/Reaktor/IRC/bot.py deleted file mode 100755 index af974f4e..00000000 --- a/Reaktor/IRC/bot.py +++ /dev/null @@ -1,34 +0,0 @@ -#! /usr/bin/env python2 - -from irclib import IRC, ServerConnectionError, is_channel -from sys import exit -from os import environ as env - -host = str(env.get('host', 'irc.freenode.org')) -port = int(env.get('port', 6667)) -nick = str(env.get('nick', 'crabspasm')) -channel = str(env.get('channel', '#tincspasm')) -print '====> irc://%s@%s:%s/%s' % (nick, host, port, channel) - -irc = IRC() -try: - client = irc.server().connect(host, port, nick) -except ServerConnectionError, error: - print error - exit - -def on_connect(connection, event): - connection.join(channel) - print 'Es passiert...' - -def on_join(connection, event): - connection.privmsg(channel, 'lol') - -def on_disconnect(connection, event): - exit - -client.add_global_handler('welcome', on_connect) -client.add_global_handler('join', on_join) -client.add_global_handler('disconnect', on_disconnect) - -irc.process_forever() diff --git a/Reaktor/IRC/bot2.py b/Reaktor/IRC/bot2.py deleted file mode 100755 index 326dd8f5..00000000 --- a/Reaktor/IRC/bot2.py +++ /dev/null @@ -1,130 +0,0 @@ -#! /usr/bin/env python -# -# //Reaktor/IRC/bot2.py -# - -from __future__ import print_function -from irclib import SimpleIRCClient, ServerConnectionError, is_channel -from sys import exit -from os import environ as env -import re - -class IRCBot(SimpleIRCClient): - def __init__(self, target): - SimpleIRCClient.__init__(self) - self.target = target - - def on_pubmsg(self, connection, event): - - def PRIVMSG(target, text): - self.connection.privmsg(target, text) - - def ME(target, text): - PRIVMSG(target, 'ACTION ' + text + '') - - def is_executable(x): - import os - return os.path.exists(x) and os.access(x, os.X_OK) - - _nickname = connection.get_nickname() - _source = event.source() - _from = _source.split('!', 1)[0] - _target = event.target() - - try: - _, _handle, _command, _argument, _ = re.split( - '^(\w+|\*):\s*(\w+)(?:\s+(.*))?$', event.arguments()[0]) - except ValueError, error: - if re.search(_nickname, event.arguments()[0]): - PRIVMSG(self.target, 'I\'m so famous') - return # ignore - - if _handle == _nickname or _handle == '*': - - from os.path import realpath, dirname, join - from subprocess import Popen as popen, PIPE - - Reaktor_dir = dirname(realpath(dirname(__file__))) - public_commands = join(Reaktor_dir, 'public_commands') - command = join(public_commands, _command) - - if is_executable(command): - - env = {} - if _argument != None: - env['argument'] = _argument - - try: - p = popen([command], stdin=PIPE, stdout=PIPE, stderr=PIPE, env=env) - except OSError, error: - ME(self.target, 'is made of stupid') - print('OSError@%s: %s' % (command, error)) - return - - stdout, stderr = [ x[:len(x)-1] for x in - [ x.split('\n') for x in p.communicate()]] - code = p.returncode - pid = p.pid - - print('command: %s -> %s' % (command, code)) - [print('%s stdout: %s' % (pid, x)) for x in stdout] - [print('%s stderr: %s' % (pid, x)) for x in stderr] - - if code == 0: - [PRIVMSG(self.target, x) for x in stdout] - [PRIVMSG(_source, x) for x in stderr] - else: - ME(self.target, 'mimimi') - - else: - if _handle != '*': - PRIVMSG(self.target, _from + ': you are made of stupid') - - def on_welcome(self, connection, event): - print('I\'m welcome! :D joining to %s now...' % (self.target)) - if is_channel(self.target): - connection.join(self.target) - else: - self.connection.privmsg(self.target, 'lol') - self.connection.quit('Pong timeout: 423 seconds') - - def on_join(self, connection, event): - print('Es passiert in %s' % (self.target)) - - def on_disconnect(self, connection, event): - # TODO reconnect - exit(0) - -# 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] - -def main(): - name = getconf1('Name', '/etc/tinc/retiolum/tinc.conf') - nick = str(env.get('nick', name)) - host = str(env.get('host', 'supernode')) - port = int(env.get('port', 6667)) - target = str(env.get('target', '#retiolum')) - print('====> irc://%s@%s:%s/%s' % (nick, host, port, target)) - - client = IRCBot(target) - try: - from getpass import getuser - client.connect(host, port, nick, username=getuser(), - ircname='//Reaktor running at %s.retiolum' % (name)) - except ServerConnectionError, error: - print(error) - exit(1) - client.start() - -if __name__ == "__main__": - main() diff --git a/Reaktor/IRC/content b/Reaktor/IRC/content deleted file mode 100644 index e0292376..00000000 --- a/Reaktor/IRC/content +++ /dev/null @@ -1 +0,0 @@ -python-irclib-0.4.6/ircbot.py diff --git a/Reaktor/IRC/index b/Reaktor/IRC/index index 68b6cf33..24982c76 100755 --- a/Reaktor/IRC/index +++ b/Reaktor/IRC/index @@ -4,6 +4,4 @@ set -xeuf # cd //Reaktor cd $(dirname $(readlink -f $0))/.. -IRC/install - -exec python IRC/bot2.py "$@" +exec python IRC/asybot.py "$@" diff --git a/Reaktor/IRC/install b/Reaktor/IRC/install deleted file mode 100755 index d5f7a8c1..00000000 --- a/Reaktor/IRC/install +++ /dev/null @@ -1,28 +0,0 @@ -#! /bin/sh -set -xeuf - -# cd //Reaktor/IRC -cd $(dirname $(readlink -f $0)) - -# install irclib.py -{ - PV=0.4.6 - PN=python-irclib - P=$PN-$PV - tarball=$P.tar.gz - URL=http://downloads.sourceforge.net/$PN/$tarball - SHA1SUM=c6271e44293ed51c21af0f44ce106667d3006e6f - - file=irclib.py - - if ! echo "$SHA1SUM $file" | sha1sum -c; then - temp=`mktemp` - trap "rm -f $temp" EXIT INT - - echo $P/$file > $temp - curl -LfsS $URL | tar --strip-components=1 -zxT $temp - fi - echo "$SHA1SUM $file" | sha1sum -c -} - - diff --git a/Reaktor/README.md b/Reaktor/README.md index 05af8ef5..a581c6bd 100644 --- a/Reaktor/README.md +++ b/Reaktor/README.md @@ -2,28 +2,25 @@ ## Quickstart - ## 1. prepare Reaktor - //Reaktor/install + ## 1. create a dedicated user + useradd reaktor - ## 2. create a dedicated user - useradd Reaktor + ## 2. marry Reaktor with /sbin/init - ## 3. marry Reaktor with /sbin/init - - ## 3a. /etc/inittab-like foo - echo 10:2345:respawn:/bin/su Reaktor -c /krebs/Reaktor/index >>/etc/inittab - # or 10:2345:respawn:/usr/bin/sudo -u Reaktor /krebs/Reaktor/index - # if Reaktor's shell is /bin/false or similar - # [check with e.g getent passwd Reaktor] + ## 2a. /etc/inittab-like foo + echo 10:2345:respawn:/bin/su reaktor -c /krebs/Reaktor/index >>/etc/inittab + # or 10:2345:respawn:/usr/bin/sudo -u reaktor /krebs/Reaktor/index + # if reaktor's shell is /bin/false or similar + # [check with e.g getent passwd reaktor] telinit q - ## 3b. upstart-like foo + ## 2b. upstart-like foo cat > /etc/init/Reaktor.conf <<EOF description "Krebs Reaktor" author "The Ministerium" stop on runlevel [016] respawn - exec /usr/bin/sudo -u Reaktor /krebs/Reaktor/index + exec /usr/bin/sudo -u reaktor /krebs/Reaktor/index EOF start Reaktor diff --git a/Reaktor/commands/reload b/Reaktor/commands/reload index 2b78b178..bfa1f041 100755 --- a/Reaktor/commands/reload +++ b/Reaktor/commands/reload @@ -1,2 +1,2 @@ #! /bin/sh -exec pkill -U Reaktor +exec pkill -U reaktor diff --git a/Reaktor/index b/Reaktor/index index ac647ca3..05827373 100755 --- a/Reaktor/index +++ b/Reaktor/index @@ -4,4 +4,11 @@ set -euf # cd //Reaktor cd $(dirname $(readlink -f $0)) +# redirect stdout [and stderr] to syslog +stdout=`mktemp -u` +mkfifo $stdout +trap "rm -vf $stdout" EXIT INT +exec 1<>$stdout 2>&1 +logger -t Reaktor -f $stdout & + exec IRC/index diff --git a/Reaktor/install b/Reaktor/install deleted file mode 100755 index 761bc437..00000000 --- a/Reaktor/install +++ /dev/null @@ -1,6 +0,0 @@ -#! /bin/sh -set -euf - -cd $(dirname $(readlink -f $0)) - -exec IRC/install diff --git a/retiolum/bin/get-tinc-dir b/retiolum/bin/get-tinc-dir new file mode 100755 index 00000000..3f48b2cd --- /dev/null +++ b/retiolum/bin/get-tinc-dir @@ -0,0 +1,9 @@ +#!/bin/ksh +case `uname -a` in + Linux*|CYGWIN*|MING*) echo "/etc/tinc" + ;; + SunOS*|Darwin*) echo "/usr/local/etc/tinc" + ;; + *) echo "/etc/tinc" + ;; +esac |