#!/usr/local/bin/python
import socket
import string

def connecttoserv(s, hostname, nick, channel, key):
	s.send('NICK ' + nick + '\r\n')
	authed = False
	s.send('USER ' + nick.lower() + ' x ' + hostname + ' :Python Bot 1.0\r\n')
	authed = False
	readbuffer = ''
	while not authed:
		readbuffer = readbuffer + s.recv(2048)
		temp = string.split(readbuffer, "\n")
		readbuffer = temp.pop( )
		for line in temp:
			line = string.rstrip(line)
			line = string.split(line)
			if line[0] == 'PING':
				s.send('PONG ' + line[1] + '\r\n')
				authed = True
	if key == '':
		s.send('JOIN :' + channel + '\r\n')
	else:
		s.send('JOIN ' + channel + ' :' + key + '\r\n')
			

hostname = 'irc.multiplay.co.uk'
port = 6667
nick = 'PYBot'
chan = '#multiplay'
key = ''

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((hostname, port))
connecttoserv(s, hostname, nick, chan, key)
connected = True
while connected:
	data = s.recv(2048)
	data = string.rstrip(data)
	if not data:
		connected = False
	else:
		print data
