#!/usr/bin/env python # # Hack for Telex <> IRC bridge # # Reinders & Rick import sys import socket import string import serial import time telex = serial.Serial(port='/dev/ttyU0', baudrate='9600',timeout=1) HOST="irc.freenode.net" PORT=6667 NICK="telex106" IDENT="telex106" REALNAME="Telex Machine at OCC2011" CHANNEL='#occ' s=socket.socket( ) s.settimeout(1) s.connect((HOST, PORT)) s.send("NICK %s\r\n" % NICK) s.send("USER %s %s bla :%s\r\n" % (IDENT, HOST, REALNAME)) s.send("JOIN %s\r\n" % CHANNEL) # Get rid of the annoying spam show_output = False telex_buffer = '' irc_buffer = '' # Infinite Loop while True: irc_buffer = '' try: # Try reading IRC channel activity while True: c = s.recv(1) irc_buffer = irc_buffer + c if c == '\n': break if(irc_buffer[0:4]=="PING"): s.send("PONG %s\r\n" % irc_buffer[1]) # Sanitizy and send to tty if ':End of /NAMES list.' in irc_buffer: show_output = True parts = irc_buffer.split() line = parts[0].split('!')[0].lstrip(':') + " = " + ' '.join(parts[3:]).lstrip(':') + "\r\n" print "IRC Received: " + irc_buffer, if 'PRIVMSG' in irc_buffer and show_output: if "BELL" in irc_buffer: telex.write(chr(7)) telex.write("\x08\x08"+line) except socket.timeout: # Try reading from Serial and spit to IRC while True: c = telex.read(1) # Need input to process if not c: break print "Telex Input Character: '%s' (%s)" % (c,ord(c)) telex_buffer = telex_buffer + c # Get a full line. if c == '\n': # First connect, make machine alive if 'Telex Retrointerface 0.2' in telex_buffer: print "Bringing machine alive" telex.write(str(0x08)) else: s.send("PRIVMSG %s : %s\r\n" % (CHANNEL, telex_buffer)) print "sending:"+ "PRIVMSG %s : %s\r\n" % (CHANNEL, telex_buffer) telex_buffer = '' continue