diff options
author | tv <tv@xso> | 2011-09-06 13:38:20 +0200 |
---|---|---|
committer | tv <tv@xso> | 2011-09-06 13:38:20 +0200 |
commit | 1a43c8c04073f3d945f1b0b35b67beeeac75df5d (patch) | |
tree | 5686ca6876061d4d48b626d74962b805896009c6 |
//Synapse -> //Reaktor/IRC
-rwxr-xr-x | IRC/bot.py | 34 | ||||
-rwxr-xr-x | IRC/bot2.py | 103 | ||||
-rw-r--r-- | IRC/content | 1 | ||||
-rwxr-xr-x | IRC/index | 8 | ||||
-rwxr-xr-x | IRC/install | 27 |
5 files changed, 173 insertions, 0 deletions
diff --git a/IRC/bot.py b/IRC/bot.py new file mode 100755 index 0000000..af974f4 --- /dev/null +++ b/IRC/bot.py @@ -0,0 +1,34 @@ +#! /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/IRC/bot2.py b/IRC/bot2.py new file mode 100755 index 0000000..0583d32 --- /dev/null +++ b/IRC/bot2.py @@ -0,0 +1,103 @@ +#! /usr/bin/env python + +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: + PRIVMSG(self.target, 'I\'m so famous') + return # ignore + + if _handle == _nickname or _handle == 'ALL': + + from os.path import realpath, dirname, join + from subprocess import Popen as popen, PIPE + + public_commands = join(realpath(dirname(__file__)), 'public_commands') + command = join(public_commands, _command) + + if is_executable(command): + try: + p = popen([command], stdin=PIPE, stdout=PIPE, stderr=PIPE) + except OSError, error: + ME(self.target, 'I am made of stupid') + print('OSError@%s: %s' % (argv, 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, _from + ': ' + x) for x in stdout] + [PRIVMSG(_source, x) for x in stderr] + else: + ME(self.target, 'mimimi') + + else: + ME(self.target, 'believes that ' + _from + ' is 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) + +def main(): + host = str(env.get('host', 'irc.freenode.org')) + port = int(env.get('port', 6667)) + nick = str(env.get('nick', 'crabspasm')) + target = str(env.get('target', '#tincspasm')) + print('====> irc://%s@%s:%s/%s' % (nick, host, port, target)) + + client = IRCBot(target) + try: + client.connect(host, port, nick) + except ServerConnectionError, error: + print(error) + exit(1) + client.start() + +if __name__ == "__main__": + main() diff --git a/IRC/content b/IRC/content new file mode 100644 index 0000000..e029237 --- /dev/null +++ b/IRC/content @@ -0,0 +1 @@ +python-irclib-0.4.6/ircbot.py diff --git a/IRC/index b/IRC/index new file mode 100755 index 0000000..21129c7 --- /dev/null +++ b/IRC/index @@ -0,0 +1,8 @@ +#! /bin/sh +set -xeuf + +cd $(dirname $(readlink -f $0)) + +./install + +exec python2 bot2.py "$@" diff --git a/IRC/install b/IRC/install new file mode 100755 index 0000000..95e0519 --- /dev/null +++ b/IRC/install @@ -0,0 +1,27 @@ +#! /bin/sh +set -xeuf + +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 +} + + |