-- jabber client
main_dir=System.currentDirectory()
dofile "./funciones.lua"
dofile "./fjabber.lua"
cuenta = 0
buffer = ""
buffer_ant = ""
y = 0
x = 0
variables={}
services={}
server_services={}

function enviar_mensaje(mensaje, destino, tipo)
	fecha = os.date("*t", os.time())
	tipo=tipo or "chat"
	graphicsPrintln(textos.msg_sfrom .. options.user .. "@" .. options.server, azul)
	graphicsPrintln(textos.msg_sto .. destino .. " " .. fecha.hour..":"..fecha.min..":"..fecha.sec, azul)
	
	graphicsPrintln(eiconos(mensaje), negro, false, iconos)
	socket:send("<presence/>")
	bytesSent = socket:send("<message to='"..destino.."' type='" .. tipo .. "'><body>" .. mensaje .. " </body></message>")
end
function send_var(name, value, target)
	socket:send("<presence/>")
	bytesSent = socket:send("<message to='" .. target .. "' type='chat'><body>###var###" .. name .. "###value###" .. value .. "</body></message>")
end

function conectar()
--select network
	chooseNetwork(options.wlan_id)

-- start server socket
	graphicsPrintln(textos.sock_op, green, true)
	serverSocket = Socket.createServerSocket(5222)

-- start connection and wait until it is connected
	graphicsPrintln("")
	graphicsPrintln(textos.sock_conn_to .. options.server, green, true)
	socket, error = Socket.connect(options.server, 5222)
	while not socket:isConnected() do System.sleep(100) if (Controls.read():start()) then break end end

	graphicsPrintln(textos.sock_connected .. options.server .. "(" .. tostring(socket) .. ")", green, true)
	System.sleep(1000)

	graphicsPrintln(textos.sock_connecting, green, true)
	bytesSent = socket:send("<?xml version='1.0'?>")
	bytesSent = socket:send("<stream:stream xmlns:stream='http://etherx.jabber.org/streams' xmlns='jabber:client' to='"..options.server.."'>")
	buffer=""
	while (buffer=="") do
		buffer=socket:recv()
		if Controls.read():start() then break end
		System.sleep(200)
	end
	if (options.debug==1) then
		graphicsPrintln(buffer,red,true)
	end
	return true
end
function login()
	graphicsPrintln(textos.sock_user, green, true)
	bytesSent = socket:send("<iq type='get' id='auth_1'><query xmlns='jabber:iq:auth'><username>" .. options.user .. "</username></query></iq>")

	buffer=""
	while (buffer=="") do
		buffer=socket:recv()
		if Controls.read():start() then break end
		System.sleep(200)
	end

	bytesSent = socket:send("<iq type='set' id='auth_1' to='" .. options.server .. "'><query xmlns='jabber:iq:auth'><username>" .. options.user .. "</username><password>" .. options.password .. "</password><resource>PSP</resource></query></iq>")

	buffer=""
	while (buffer=="") do
		buffer=socket:recv()
		if Controls.read():start() then break end
		System.sleep(200)
	end
	if (options.debug==1) then
		graphicsPrintln(buffer,red,true)
	end
	buffer=""
	System.sleep(2000)
	socket:send("<presence/>")
	iq_get_roster()
	return true
end
-------------------------------------------------------------------------
---------------------------------Aqui comienza---------------------------
------------------------------el scrip principal-------------------------
-------------------------------------------------------------------------

-- cargamos el fichero de configuracion
dofile "./jabber.lua"

conectar()
login()
-- inicializacion del teclado en pantalla
texto=""
-- buffer=""
salir=false
num_contacto=1
ant_controles=Controls.read()
while true do
	controles=Controls.read()
	buffer = buffer .. socket:recv()
	if string.len(buffer) > 0 then 
		buffer=leer2(buffer)
	end
	if (controles~=ant_controles) then
		ant_controles=controles
		if controles:square() then
			if (contactos[num_contacto].tipo=="groupchat") then
				enviar_mensaje(texto, contactos[num_contacto].id, "groupchat")
			else
				enviar_mensaje(texto, contactos[num_contacto].id)
			end
			texto=""
		end
		if controles:l() then
			num_contacto=num_contacto-1
			if (num_contacto<1) then num_contacto=table.getn(contactos) end
		end
		if controles:r() then
			num_contacto=num_contacto+1
			if (num_contacto>table.getn(contactos)) then num_contacto=1 end
		end
		if controles:start() then menu() end
		if (salir==true) then break end
	end
	screen:blit(0, 0, fondo)
	screen:blit(0, 0, offscreen)
	texto=teclado(texto, negro)
	screen:print(0, 210, textos.msg_to .. contactos[num_contacto].id .. ":", blanco)
	screen:print(0, 220, "->" .. texto .. "_", green)
	screen:print(282, 260, textos.msg_send, negro)
	pintar_contactos(num_contacto)
	if (captura) then
		captura=captura+1
		if (captura>4) then
			screen:save("./foto.png")
			captura=nil
		end
	end
	screen.waitVblankStart()
	screen.flip()
end
salir2()
