#!/usr/bin/python

import socket,md5,string,time,os,sys,random,urllib

##### CONFIG :-) ##########
tumsoul_server = ('10.42.1.59', 4242)
user = 'login_x'
password = 'passw0rd'
location = urllib.quote("Tumsoul 0.3.3")
useragent = urllib.quote("tumsoul v0.3 [%d]"%os.getpid())
states = ["actif", "away"]
delays = [ 10 ] + [ 4242 ]*10 + [ 424 ]*10

def md5sum(str):
	chk = md5.new()
	chk.update(str)
	return chk.digest()

def hexify(str):
	return string.join(map(lambda c:"%02x"%ord(c),str),"")

def expect(file,str):
	reply = file.readline()[:-1]
	if reply[:len(str)]==str: return
	print "Invalid reply : '%s*' expected, '%s' received."%(str,reply)
	time.sleep(3)
	sys.exit()

s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect(tumsoul_server)
f=s.makefile()
cmd,code,hash,ip,port,timestamp=string.split(f.readline()[:-1])
sum=md5sum("%s-%s/%s%s"%(hash,ip,port,password))
authreply=hexify(sum)
s.send("auth_ag ext_user none none\n")
expect(f,"rep 002 --")
s.send("ext_user_log %s %s %s %s\n"%(user,authreply,location,useragent))
expect(f,"rep 002 --")
print "Authentication complete, proceeding..."
statechange_deadline = string.atoi(timestamp)
ping_deadline = string.atoi(timestamp)
ping_delay = 40
check_deadline = string.atoi(timestamp)
localtime = string.atoi(timestamp)
while 1:
	localtime += 1
	if localtime > ping_deadline:
		ping_deadline += ping_delay
		#print "Ping..."
		s.send("ping\n")
	if localtime > statechange_deadline:
		delay = random.choice(delays)
		newstate = random.choice(states)
		statechange_deadline+=delay
		#print "Newstate : %s, next change in %d seconds..."%(newstate,delay)
		s.send("state %s:%i\n"%(newstate,int(localtime)))
	time.sleep(1)



