diff options
author | Alexander Sulfrian <alex@spline.inf.fu-berlin.de> | 2013-08-04 04:58:33 +0200 |
---|---|---|
committer | root <root@vm-monitoring.spline.inf.fu-berlin.de> | 2013-08-04 04:58:50 +0200 |
commit | f0680eb182c6583a0e27124825b8261fa87d48ab (patch) | |
tree | 5f7ba07a5d169703bb448887aa8f47a500056098 /socket_notify.py | |
download | socket-notify-f0680eb182c6583a0e27124825b8261fa87d48ab.tar.gz socket-notify-f0680eb182c6583a0e27124825b8261fa87d48ab.tar.bz2 socket-notify-f0680eb182c6583a0e27124825b8261fa87d48ab.zip |
initial commit
Diffstat (limited to 'socket_notify.py')
-rw-r--r-- | socket_notify.py | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/socket_notify.py b/socket_notify.py new file mode 100644 index 0000000..5b651a5 --- /dev/null +++ b/socket_notify.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python +""" +socket_notify.py - Phenny Socket Notification +Copyright 2013, Alexander Sulfrian <alex@spline.inf.fu-berlin.de> +Licensed under the Eiffel Forum License 2. + +http://inamidst.com/phenny/ +""" + +import asyncore, socket, stat, os + +SOCKET_PATH = '/var/run/phenny/socket' + +def setup(phenny): + dir = os.path.dirname(SOCKET_PATH) + if not os.path.exists(dir): + os.makedirs(dir) + Monitor(SOCKET_PATH, phenny) + +class Sender(asyncore.dispatcher): + def __init__(self, sock, phenny): + asyncore.dispatcher.__init__(self, sock) + self.phenny = phenny + + def handle_read(self): + data = self.recv(8192) + if data: + for line in data.split('\n'): + for chan in self.phenny.config.channels: + self.phenny.msg(chan, line) + else: + self.close() + +class Monitor(asyncore.dispatcher): + def __init__(self, path, phenny): + asyncore.dispatcher.__init__(self) + self.phenny = phenny + self.create_socket(socket.AF_UNIX, socket.SOCK_STREAM) + + try: + mode = os.stat(path).st_mode + if stat.S_ISSOCK(mode): + os.unlink(path) + else: + print("Path '%s' exists and is not a socket!" % path) + sys.exit(1) + except OSError: + pass + + self.bind(path) + self.listen(5) + + def handle_accept(self): + pair = self.accept() + if pair is not None: + sock, addr = pair + handler = Sender(sock, self.phenny) |