[327] | 1 | #!/usr/bin/env python
|
---|
| 2 | #
|
---|
| 3 | # Hack for Telex <> IRC bridge
|
---|
| 4 | #
|
---|
| 5 | # Reinders & Rick
|
---|
| 6 | import sys
|
---|
| 7 | import socket
|
---|
| 8 | import string
|
---|
| 9 | import serial
|
---|
| 10 | import time
|
---|
| 11 |
|
---|
| 12 | telex = serial.Serial(port='/dev/ttyU0', baudrate='9600',timeout=1)
|
---|
| 13 |
|
---|
| 14 | HOST="irc.freenode.net"
|
---|
| 15 | PORT=6667
|
---|
| 16 | NICK="telex106"
|
---|
| 17 | IDENT="telex106"
|
---|
| 18 | REALNAME="Telex Machine at OCC2011"
|
---|
| 19 | CHANNEL='#occ'
|
---|
| 20 |
|
---|
| 21 | s=socket.socket( )
|
---|
| 22 | s.settimeout(1)
|
---|
| 23 | s.connect((HOST, PORT))
|
---|
| 24 | s.send("NICK %s\r\n" % NICK)
|
---|
| 25 | s.send("USER %s %s bla :%s\r\n" % (IDENT, HOST, REALNAME))
|
---|
| 26 | s.send("JOIN %s\r\n" % CHANNEL)
|
---|
| 27 |
|
---|
| 28 | # Get rid of the annoying spam
|
---|
| 29 | show_output = False
|
---|
| 30 | telex_buffer = ''
|
---|
| 31 | irc_buffer = ''
|
---|
| 32 |
|
---|
| 33 | # Infinite Loop
|
---|
| 34 | while True:
|
---|
| 35 | irc_buffer = ''
|
---|
| 36 | try:
|
---|
| 37 | # Try reading IRC channel activity
|
---|
| 38 | while True:
|
---|
| 39 | c = s.recv(1)
|
---|
| 40 | irc_buffer = irc_buffer + c
|
---|
| 41 | if c == '\n':
|
---|
| 42 | break
|
---|
| 43 | if(irc_buffer[0:4]=="PING"):
|
---|
| 44 | s.send("PONG %s\r\n" % irc_buffer[1])
|
---|
| 45 | # Sanitizy and send to tty
|
---|
| 46 | if ':End of /NAMES list.' in irc_buffer:
|
---|
| 47 | show_output = True
|
---|
| 48 |
|
---|
| 49 | parts = irc_buffer.split()
|
---|
| 50 | line = parts[0].split('!')[0].lstrip(':') + " = " + ' '.join(parts[3:]).lstrip(':') + "\r\n"
|
---|
| 51 | print "IRC Received: " + irc_buffer,
|
---|
| 52 | if 'PRIVMSG' in irc_buffer and show_output:
|
---|
| 53 | if "BELL" in irc_buffer:
|
---|
| 54 | telex.write(chr(7))
|
---|
| 55 | telex.write("\x08\x08"+line)
|
---|
| 56 | except socket.timeout:
|
---|
| 57 | # Try reading from Serial and spit to IRC
|
---|
| 58 | while True:
|
---|
| 59 | c = telex.read(1)
|
---|
| 60 | # Need input to process
|
---|
| 61 | if not c:
|
---|
| 62 | break
|
---|
| 63 | print "Telex Input Character: '%s' (%s)" % (c,ord(c))
|
---|
| 64 | telex_buffer = telex_buffer + c
|
---|
| 65 | # Get a full line.
|
---|
| 66 | if c == '\n':
|
---|
| 67 | # First connect, make machine alive
|
---|
| 68 | if 'Telex Retrointerface 0.2' in telex_buffer:
|
---|
| 69 | print "Bringing machine alive"
|
---|
| 70 | telex.write(str(0x08))
|
---|
| 71 | else:
|
---|
| 72 | s.send("PRIVMSG %s : %s\r\n" % (CHANNEL, telex_buffer))
|
---|
| 73 | print "sending:"+ "PRIVMSG %s : %s\r\n" % (CHANNEL, telex_buffer)
|
---|
| 74 | telex_buffer = ''
|
---|
| 75 | continue
|
---|