source: misc/irc2telex.py@ 339

Last change on this file since 339 was 327, checked in by Rick van der Zwet, 13 years ago

Telex106 @ occ2011

File size: 1.9 KB
Line 
1#!/usr/bin/env python
2#
3# Hack for Telex <> IRC bridge
4#
5# Reinders & Rick
6import sys
7import socket
8import string
9import serial
10import time
11
12telex = serial.Serial(port='/dev/ttyU0', baudrate='9600',timeout=1)
13
14HOST="irc.freenode.net"
15PORT=6667
16NICK="telex106"
17IDENT="telex106"
18REALNAME="Telex Machine at OCC2011"
19CHANNEL='#occ'
20
21s=socket.socket( )
22s.settimeout(1)
23s.connect((HOST, PORT))
24s.send("NICK %s\r\n" % NICK)
25s.send("USER %s %s bla :%s\r\n" % (IDENT, HOST, REALNAME))
26s.send("JOIN %s\r\n" % CHANNEL)
27
28# Get rid of the annoying spam
29show_output = False
30telex_buffer = ''
31irc_buffer = ''
32
33# Infinite Loop
34while 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
Note: See TracBrowser for help on using the repository browser.