#! /usr/bin/python import socket import thread host = "www.squawkrpg.net" port = 1084 user = "" # user id number users = {} # list of all users mode_names = ("text", "client number", "new connection", "lost connection", "client name", "end of names", "oink", "clear", "copy", "name change") mode_numbers = {"text":0, "client number":1, "new connection":2, "lost connection":3, "client name":4, "end of names":5, "oink":6, "clear":7, "copy":8, "name change":9} def string_to_int (text): if len (text) < 4: return -1 # HACK - "end of names" block too short n = ord (text[0]) n = n + ord (text[1]) * 256 n = n + ord (text[2]) * 65536 n = n + ord (text[3]) * 16777216 return n def decode (mess): global user #print repr (mess) # additional debugging info mode = string_to_int (mess[0:4]) clnt = string_to_int (mess[4:8]) size = string_to_int (mess[8:12]) text = mess[12:11+size] mode = mode_names [mode] if mode == "text": print users[clnt] + "> " + text elif mode == "client number": print "[connected]" user = clnt elif mode == "new connection": print "[" + text + " joined]" users [clnt] = text elif mode == "lost connection": print "[" + users [clnt] + " left]" del users [clnt] elif mode == "client name": print "[user: " + text + "]" users [clnt] = text elif mode == "end of names": print "" elif mode == "oink": print users[clnt] + " sent an OINK!" elif mode == "clear": # what does this do anyway? print repr (mess) # additional debugging info print (mode, clnt, size, text) # debugging information elif mode == "copy": # what does this do anyway? print repr (mess) # additional debugging info print (mode, clnt, size, text) # debugging information elif mode == "name change": print "[" + users [clnt] + " is now known as " + text + "]" users [clnt] = text else: # didn't recognize that message type! print repr (mess) # additional debugging info print (mode, clnt, size, text) # debugging information #print (mode, clnt, size, text) # debugging information # check for more messages at the end of this one! if len(mess) > 12 + size: decode (mess[12+size:-1]) def listen (sock): while 1: mess = sock.recv (1024) if not mess: break # is this necessary? decode (mess) def int_to_string (i): a = i % 256 b = (i / 256) % 256 c = (i / 65536) % 256 d = (i / 16777216) % 256 return chr(a) + chr(b) + chr(c) + chr(d) def send (mess, mode): mess = mess + "\0" size = int_to_string (len (mess)) mode = int_to_string (mode) clnt = int_to_string (user) sock.send (mode + clnt + size + mess) # program starts here sock = socket.socket (socket.AF_INET, socket.SOCK_STREAM) sock.connect ((host, port)) thread.start_new_thread (listen, (sock,)) while 1: mess = raw_input ("") if mess == "quit": break elif mess == "users": for i in users.values (): print i elif mess == "name": print "old name: " + users[user] mess = raw_input ("new name: ") if mess != "": users[user] = mess send (mess, mode_numbers["name change"]) elif mess == "oink": send ("", mode_numbers["oink"]) else: send (mess, mode_numbers["text"]) sock.close ()