function prueba()
	leer2("<message from='oscar@jabber.es/trabajo' to='iņaki@jabber.es'><body>Hola!XDkpasa con linux y con mogu</body></message><presence from='oscar@jabber.es/trabajo' to='jabber.es' type='available'><status>Estoy aburrido, que alguien me hable</status><priority>10</priority><show>chat</show></presence><hola></stream:stream><hola>")
end
-- function prueba2()
-- 	leer2("<message from='oscar@jabber.es/trabajo' to='iņaki@jabber.es'><body></body></message><presence from='oscar@jabber.es/trabajo' to='jabber.es' type='unavailable'><status>Estoy aburrido, que alguien me hable</status><priority>10</priority><show>chat</show></presence><hola>")
-- end
function prueba2()
	send_var("variable1","valor1","davgav@jabber.org.au")
end
-- cargamos el fichero de configuracion
function configuracion()
	local i=1
	local j=1
-- 	opciones por defecto
	options = { wlan_id=0, server="jabber.org", port=5222, debug=0, sound="./sounds/type.wav", back="./fondo.png", lang="./langs/spanish.lan", show_nicks=true}
	contactos={}
	shorts={}
-- 	cargando fichero de configuracion
	file = io.open("config.txt", "r")
	if file then
		for line in file:lines() do
			equal = string.find(line, "=")
			if equal then
				key = string.sub(line, 1, equal - 1)
				value = string.sub(line, equal + 1)
				if (key == "server") then
					options[key] = value
				elseif (key == "user") then
					options[key] = value
				elseif (key == "back") then
					options[key] = value
				elseif (key == "lang") then
					options[key] = value
				elseif (key == "sound") then
					options[key] = value
				elseif (key == "password") then
					options[key] = value
				elseif (key == "usuariomsn") then
					options[key] = value
				elseif (key == "passwordmsn") then
					options[key] = value
				elseif (key == "pasarelamsn") then
					options[key] = value
				elseif (key == "groupnick") then
					options[key] = value
				elseif (key == "contact") then
					contactos[i] = {}
					contactos[i].id = value
					table.setn(contactos, i)
					i = i+1
				elseif (key == "groupchat") then
					contactos[i] = {}
					contactos[i].id = value
					contactos[i].tipo = "groupchat"
					contactos[i].resource = resource
					resource = nil
					table.setn(contactos, i)
					i = i+1
				elseif (key == "shortcut") then
					shorts[j] = value
					table.setn(shorts, j)
					j = j+1
				elseif (key == "show_nicks") then
					if value=="true" then
						options[key] = true
					else
						options[key] = false
					end
				else
					options[key] = tonumber(value)
				end
			end
		end
		file:close()
	else
		opcion=0
		while (opcion==0) do
			opcion=pintar_menu({textos.enter_user,textos.new_user})
			if (opcion==1) then cambiar_usuario() end
			if (opcion==2) then crear_cuenta() end
		end
	end
end

function leer2(buff)
	local temp
	if (buff~=buffer_ant) then
------buscamos mensaje
		local i=string.find (buff, "<message", 1,true)
		if (i) then
			j=string.find (buff, "</message>", i,true)
		end
		if (i and j) then
			leer(string.sub(buff, i, j+9))
			temp=string.sub(buff, 1, i-1) .. string.sub(buff, j+10, string.len(buff))
			buff=temp
		end
		i=nil
		j=nil
------buscamos presencia
		local i=string.find (buff, "<presence", 1,true)
		if (i) then
			j=string.find (buff, "</presence>", i,true)
			if (j==nil) then
				j=string.find (buff, ">", i,true)
				if (j) then
					leer(string.sub(buff, i, j))
					temp=string.sub(buff, 1, i-1) .. string.sub(buff, j+1, string.len(buff))
					buff=temp
				end
			else
				leer(string.sub(buff, i, j+10))
				temp=string.sub(buff, 1, i-1) .. string.sub(buff, j+11, string.len(buff))
				buff=temp
			end
		end
		i=nil
		j=nil
--------------------iq-------------------------------------------------
		local i=string.find (buff, "<iq", 1,true)
		if (i) then
			j=string.find (buff, "</iq>", i,true)
		end
		if (i and j) then
			leer(string.sub(buff, i, j+4))
			temp=string.sub(buff, 1, i-1) .. string.sub(buff, j+5, string.len(buff))
			buff=temp
		end
		i=nil
		j=nil
------buscamos si el servidor desconecta
		local i=string.find (buff, "</stream:stream>", 1,true)
		if (i~=nil) then
			graphicsPrintln(textos.disconnected,green)
--		temp=string.sub(buff, 1, i-1) .. string.sub(buff, i+16, string.len(buff))
			buff=""
		end
		i=nil
		j=nil
		buffer_ant=buff
		cuenta=0
	else
		if (cuenta>3) then
			if (options.debug==1) then
				graphicsPrintln(buff)
			end
			buff=""
			buffer_ant=""
			cuenta=0
		else
			cuenta=cuenta+1
		end
	end
--	graphicsPrintln(buff)
	return buff
end

function leer(xml)
	if (options.debug==1) then
		graphicsPrintln(xml,red)
	end
	local i=1
	local j=1
	local texto=""
	local encontrado=false
	if string.lower(string.sub(xml, i, i+7))== "<message" then
		fecha = os.date("*t", os.time())
		i=string.find (xml, "from=", 1,true) + 6
		j=string.find (xml, "'", i,true)
		if (j==nil) then
			j=string.find (xml, '"', i,true)
		end
		local contacto=string.sub(xml, i, j-1)
		i=string.find (xml, "<body>", 1,true) + 6
		j=string.find (xml, "</body>", i,true)
		texto=tostring(string.sub(xml, i, j-1))
		k=string.find (texto, "###var###", 1,true)
		if (k~=nil) then
			l=string.find (texto, "###value###", k,true)
			variable=string.sub(tostring(texto), k + 9, l-1)
			valor=string.sub(tostring(texto), l+11, string.len(texto))
			if (options.debug==1) then
				graphicsPrintln(variable, verde)
				graphicsPrintln(valor, verde)
			end
			variables[variable]=valor
		else
			graphicsPrintln(contacto .. " " .. gethour(), rojo)
			graphicsPrintln(eiconos(texto), negro, false, iconos)
			if (running_plugin~=true) then
				Sound.load(options.sound):play()
			end
		end
		i=string.len(xml)

	elseif string.lower(string.sub(xml, i, i+2))== "<iq" then
-- <iq from="jabber.org.au" type="result" to="davgav@jabber.org.au/PSP" id="ab43a" >
-- <query xmlns="http://jabber.org/protocol/disco#items">
-- <item name="MSN Transport" jid="msn.jabber.org.au" />
-- <item name="Yahoo! IM Gateway" jid="yahoo.jabber.org.au" />
-- <item name="ICQ Transport" jid="icq.jabber.org.au" />
-- <item name="Public Chatrooms" jid="rooms.jabber.org.au" />
-- </query>
-- </iq>

		if (string.find(xml,"<query xmlns='jabber:iq:roster'>",1, true)) then
			j=1
			while j~=nil do 
				l=string.find(xml,"jid=",j,true)
				if l then
					l=l+5
					k=string.find(xml, "'", l, true)
					if (k==nil) then
						k=string.find(xml, '"', l, true)
					end
					if (l and k) then
					id=string.sub(xml,l,k-1)
					end
				end
				ll=string.find(xml,"name=",j,true)
				if ll then
					ll=ll+6
					kk=string.find(xml, "'", ll, true)
					if (kk==nil) then
						kk=string.find(xml, '"', ll, true)
					end
					if (ll and kk) then
					name=string.sub(xml,ll,kk-1)
					end
				end
				j=string.find(xml,"/>",j+1, true)
				if j then contacto=id end
				if ll and j then
					if ll<j then nickname=name end
				end
				encontrado=false
				for i=1,table.getn(contactos) do
					if (contactos[i].id==contacto) then
						if (nickname) then
							contactos[i].nick=nickname
							nickname=nil
						end
						encontrado=true
					end
				end
				if (encontrado==false) then
					i=table.getn(contactos)+1
					contactos[i]={}
					contactos[i].id = contacto
						if (nickname) then
							contactos[i].nick=nickname
							nickname=nil
						end
					table.setn(contactos,i)
				end
			end
		else
			j=1
			local n_service=1
			l=string.find(xml,"id=",j,true)
			if l then
				l=l+4
				k=string.find(xml, "'", l, true)
				if (k==nil) then
					k=string.find(xml, '"', l, true)
				end
				if (l and k) then
				id=string.sub(xml,l,k-1)
				end
			end
			l=nil k=nil
			while j<string.len(xml) do
				l=string.find(xml, "<item name=", j, true)
				if l then
					l=l+12
					k=string.find(xml, "'", l, true)
					if (k==nil) then
						k=string.find(xml, '"', l, true)
					end
					if (l and k) then
						server_service=string.sub(xml,l,k-1)
						ll=string.find(xml, "jid=", k, true)
						if ll then
							ll=ll+5
							kk=string.find(xml, "'", ll, true)
							if (kk==nil) then
								kk=string.find(xml, '"', ll, true)
							end
							if (ll and kk) then
								server_jid=string.sub(xml,ll,kk-1)
								services[server_service]=server_jid
								server_services[n_service]={}
								server_services[n_service].name=server_service
								server_services[n_service].jid=server_jid
								server_services[n_service].id=id
								n_service=n_service+1
							end
						end
						j=k
					end
				else
					j=j+1
				end
			end
		end
		received_iq=true
		i=string.len(xml)

	elseif string.lower(string.sub(xml, i, i+8))== "<presence" then
		l=string.find (xml, "<nickname>", 1, true)
		if (l~=nil) then
			l=l+10
			k=string.find (xml, "</nickname>", l, true)
			nickname=string.sub(xml,l,k-1)
		end
		i=string.find (xml, "type=", 1, true)
		if (i~=nil) then
			i=i+6
			j=string.find (xml, "'", i, true)
			if (j==nil) then
				j=string.find (xml, '"', i, true)
			end
			tipo=string.sub(xml, i, j-1)
		else
			tipo="otro"
		end
		if (tipo=="available") then
			i=string.find (xml, "from=", 1, true) + 6
			j=string.find (xml, "'", i, true)
			if (j==nil) then
				j=string.find (xml, '"', i, true)
			end
			local contacto=string.sub(xml, i, j-1)
			i=string.find (contacto, "/", 1, true)
			resource=string.sub(contacto, i+1, string.len(contacto))
			if (i~=nil) then contacto=string.sub(contacto, 1, i-1) end
			for i=1,table.getn(contactos) do
				if (contactos[i].id==contacto) then
					if(contactos[i].conectado~=true) then
						graphicsPrintln(contacto .. textos.online, green)
					end
					contactos[i].conectado=true
					contactos[i].resource=resource
					resource=nil
					if (nickname) then
						contactos[i].nick=nickname
						nickname=nil
					end
					encontrado=true
				end
			end
			if (encontrado==false) then
				i=table.getn(contactos)+1
				contactos[i]={}
				contactos[i].id = contacto
				contactos[i].conectado=true
				contactos[i].resource=resource
					if (nickname) then
						contactos[i].nick=nickname
						nickname=nil
					end
				resource=nil
				table.setn(contactos,i)
			end
		elseif (tipo=="unavailable") then
			i=string.find (xml, "from=", 1, true) + 6
			j=string.find (xml, "'", i, true)
			if (j==nil) then
				j=string.find (xml, '"', i, true)
			end
			local contacto=string.sub(xml, i, j-1)
			i=string.find (contacto, "/", 1, true)
			if (i~=nil) then
				resource=string.sub(contacto, i+1, string.len(contacto))
			end
			graphicsPrintln(contacto .. textos.offline, green)
			if (i~=nil) then contacto=string.sub(contacto, 1, i-1) end
			for i=1,table.getn(contactos) do
				if (contactos[i].id==contacto) then
					contactos[i].conectado=false
					contactos[i].resource=resource
					if (nickname) then
						contactos[i].nick=nickname
						nickname=nil
					end
					resource=nil
					encontrado=true
				end
			end
		else
			i=string.find (xml, "from=", 1, true) + 6
			j=string.find (xml, "'", i, true)
			if (j==nil) then
				j=string.find (xml, '"', i, true)
			end
			local contacto=string.sub(xml, i, j-1)
			i=string.find (contacto, "/", 1, true)
			if (i~=nil) then
				resource=string.sub(contacto, i+1, string.len(contacto))
			end
			if (i~=nil) then contacto=string.sub(contacto, 1, i-1) end
			for i=1,table.getn(contactos) do
				if (contactos[i].id==contacto) then
					if(contactos[i].conectado~=true) then
						graphicsPrintln(contacto .. textos.online, green)
					end
					contactos[i].conectado=true
					contactos[i].resource=resource
					if (nickname) then
						contactos[i].nick=nickname
						nickname=nil
					end
					resource=nil
					encontrado=true
				end
			end
			if (encontrado==false) then
				i=table.getn(contactos)+1
				contactos[i]={}
				contactos[i].conectado=true
				contactos[i].id = contacto
					if (nickname) then
						contactos[i].nick=nickname
						nickname=nil
					end
				contactos[i].resource=resource
				resource=nil
				table.setn(contactos,i)
			end
		end
		if (tipo=="subscribe") then
			i=string.find (xml, "from=", i, true) + 6
			j=string.find (xml, "'", i, true)
			if (j==nil) then
				j=string.find (xml, '"', i, true)
			end
			local contacto=string.sub(xml, i, j-1)
			graphicsPrintln(textos.authorization .. contacto, green, true)
			graphicsPrintln(textos.confirm, green, true)
			while true do
				if Controls.read():cross() then
					socket:send("<presence from='" .. options.user .. "' to='" .. contacto .. "' type='unsubscribed'/>")
					graphicsPrintln(textos.cancelled, green, true)
					break
				end
				if Controls.read():circle() then
					socket:send("<presence from='" .. options.user .. "' to='" .. contacto .. "' type='subscribed'/>")
					graphicsPrintln(contacto .. textos.authorized, green)
					System.sleep(300)
					break 
				end
			end
		end
		if (tipo=="subscribed") then
			i=string.find (xml, "from=", i, true) + 6
			j=string.find (xml, "'", i, true)
			if (j==nil) then
				j=string.find (xml, '"', i, true)
			end
			local contacto=string.sub(xml, i, j-1)
			graphicsPrintln(textos.authorized_by .. contacto .."!!!", green)
		end
		if (tipo=="unsubscribed") then
			i=string.find (xml, "from=", i, true) + 6
			j=string.find (xml, "'", i, true)
			if (j==nil) then
				j=string.find (xml, '"', i, true)
			end
			local contacto=string.sub(xml, i, j-1)
			graphicsPrintln(textos.unauthorized_by .. contacto .."!!!", green)
		end
	else
		graphicsPrintln(xml)
	end
end
l_contactos = Image.createEmpty(130, 180)
l_contactos:clear(transp)
function pintar_contactos(num_contacto)
	l_contactos = Image.createEmpty(130, 180)
	l_contactos:clear(transp)
	local i=1
	local color=blanco
	if (table.getn(contactos)==0) then
		nuevo_contacto()
	end
	for i=1,table.getn(contactos) do
		if (num_contacto==i) then
			color=azul
		elseif (contactos[i].tipo=="groupchat") then
			color=amarillo
		elseif (contactos[i].conectado) then
			color=green
		else
			color=negro
		end
		if (num_contacto>10) then
			j=num_contacto-10
		else
			j=0
		end
		if options.show_nicks==true and contactos[i].nick~=nil then
			l_contactos:print(17, ((i-j)*10), contactos[i].nick, color)
		else
			l_contactos:print(17, ((i-j)*10), contactos[i].id, color)
		end
		if (contactos[i].resource) then
			if (string.lower(contactos[i].resource)=="psp") then
				l_contactos:blit(1, ((i-j)*10), psp_icon)
			end
		end
	end
	screen:blit(340, 25, l_contactos)
end

function iq_get_items(igi_destino)
	local id=tostring(math.random(1,100000))
		bytesSent = socket:send("<iq type='get' to='" .. igi_destino .. "' id='" .. id .. "'><query xmlns='http://jabber.org/protocol/disco#items'/></iq>")
	while (received_iq~=true) do
		buffer = buffer .. socket:recv()
		if string.len(buffer) > 0 then 
			buffer=leer2(buffer)
		end
		if (Controls.read():start()) then received_iq=true end
	end
	received_iq=nil
	return id
end

function iq_get_roster()
	local id=tostring(math.random(1,100000))
		bytesSent = socket:send("<iq type='get' id='" .. id .. "' ><query xmlns='jabber:iq:roster'/></iq>")
	while (received_iq~=true) do
		buffer = buffer .. socket:recv()
		if string.len(buffer) > 0 then 
			buffer=leer2(buffer)
		end
		if (Controls.read():start()) then received_iq=true end
	end
	received_iq=nil
	return id
end

function menu()
	if (options.debug==1) then
		local opcion=pintar_menu({textos.new_cont, textos.del_cont, textos.reconnect, textos.ch_user, textos.music, textos.debug_off, textos.config, textos.plugin, textos.quit, "Prueba", "Prueba2", "captura", "Groupchat"})
		if (opcion==1) then nuevo_contacto() end
		if (opcion==2) then borrar_contacto() end
		if (opcion==3) then conectar() login() end
		if (opcion==4) then if (cambiar_usuario()) then conectar() login() end end
		if (opcion==5) then musica() end
		if (opcion==6) then options.debug=0 end
		if (opcion==7) then configurar() end
		if (opcion==8) then plugin() end
		if (opcion==9) then salir=true end
		if (opcion==10) then prueba() end
		if (opcion==11) then prueba2() end
		if (opcion==12) then captura=1 end
		if (opcion==13) then groups() end
	else
		local opcion=pintar_menu({textos.new_cont, textos.del_cont, textos.reconnect, textos.ch_user, textos.music, textos.debug_on, textos.config, textos.plugin, textos.quit})
		if (opcion==1) then nuevo_contacto() end
		if (opcion==2) then borrar_contacto() end
		if (opcion==3) then conectar() login() end
		if (opcion==4) then if (cambiar_usuario()) then conectar() login() end end
		if (opcion==5) then musica() end
		if (opcion==6) then options.debug=1 end
		if (opcion==7) then configurar() end
		if (opcion==8) then plugin() end
		if (opcion==9) then salir=true end
	end
end
function groups()
	local opcion=pintar_menu({"Entrar", "Salir", "buscar sala", "Nuevo Groupchat"})
	if (options.groupnick==nil) then
		options.groupnick=input_text("Nick: ","[]=Enter")
	end
	if (opcion==1) then
		salas={}
		j=1
		for i=1, table.getn(contactos) do
			if (contactos[i].tipo == "groupchat") then
				salas[j]=contactos[i].id
				table.setn(salas, j)
				j=j+1
			end
		end
		local sala=pintar_menu(salas, "Entrar en sala...")
		bytesSent = socket:send("<presence to='" .. salas[sala] .. "/" .. options.groupnick .. "'/>")
	end
	if (opcion==2) then
		salas={}
		j=1
		for i=1, table.getn(contactos) do
			if (contactos[i].tipo == "groupchat") then
				salas[j]=contactos[i].id
				table.setn(salas, j)
				j=j+1
			end
		end
		local sala=pintar_menu(salas, "Salir de sala...")
		bytesSent = socket:send("<presence to='" .. salas[sala] .. "/" .. options.groupnick .. "' type='unavailable'/>")
	end
	if (opcion==3) then
		local servidor=input_text(textos.new_server)
		if servidor~="" then
			local id=iq_get_items(servidor)
-- 			local id=tostring(math.random(1,100000))
-- 			bytesSent = socket:send("<iq type='get' to='" .. servidor .. "' id='" .. id .."'><query xmlns='http://jabber.org/protocol/disco#items'/></iq>")
-- 			while (received_iq~=true) do
-- 				buffer = buffer .. socket:recv()
-- 				if string.len(buffer) > 0 then 
-- 					buffer=leer2(buffer)
-- 				end
-- 			end
-- 			received_iq=nil
			lista_salas=nil
			for i=1, table.getn(server_services) do
				if (server_services[i].id==id and server_services[i].name=="Public Chatrooms") then
					lista_salas=server_services[i].jid
				end
			end
			if (lista_salas~=nil) then
			local id=iq_get_items(lista_salas)
-- 			local id=tostring(math.random(1,100000))
-- 				bytesSent = socket:send("<iq type='get' to='" .. lista_salas .. "' id='" .. id .. "'><query xmlns='http://jabber.org/protocol/disco#items'/></iq>")
-- 				while (received_iq~=true) do
-- 					buffer = buffer .. socket:recv()
-- 					if string.len(buffer) > 0 then 
-- 						buffer=leer2(buffer)
-- 					end
-- 				end
-- 				received_iq=nil
				j=1
				salas_charla={}
				for i=1, table.getn(server_services) do
					if (server_services[i].id==id) then
						salas_charla[j]=server_services[i]
						table.setn(salas_charla, j)
						j=j+1
					end
				end
				for i=1, table.getn(salas_charla) do
						nombre_salas[i]=salas_charla[i].name
				end
				table.setn(nombre_salas, table.getn(salas_charla))
				local sala_escogida=pintar_menu(nombre_salas,"Escoje una sala")
				if sala_escogida~=0 then
					i=table.getn(contactos)+1
					contactos[i]={}
					contactos[i].conectado=true
					contactos[i].id = salas_charla[sala_escogida].jid
					contactos[i].nick = salas_charla[sala_escogida].name
					contactos[i].tipo = "groupchat"
					contactos[i].resource=resource
					resource=nil
					table.setn(contactos,i)
				end
			else
				graphicsPrintln("No se han encontrado salas",green)
			end
		end
	end
	if (opcion==4) then
		local group_id=input_text("GroupChat: ","[]=Enter")
		i=table.getn(contactos)+1
		contactos[i]={}
		contactos[i].conectado=true
		contactos[i].id = group_id
		contactos[i].tipo = "groupchat"
		contactos[i].resource=resource
		resource=nil
		table.setn(contactos,i)
	end
end

function plugin()
	dire,file=menu_dir("./add",{"ADD"}, false)
	if (file~="") then
		System.currentDirectory(dire)
		graphicsPrintln(dire .. "/" .. file,green)
		dofile (file)
		System.currentDirectory(main_dir)
	end
end

function configurar()
	local opcion=pintar_menu({textos.back, textos.sound, textos.sel_lang, "ver Nick/JabberId"})
	if (opcion==1) then
		dire,file=menu_dir(".",{"PNG","JPEG","JPG"})
		if (file~="") then
			graphicsPrintln(dire .. "/" .. file,green)
			options.back=dire .. "/" .. file
			fondo=Image.load(options.back)
		end
	end
	if (opcion==2) then
		dire,file=menu_dir("./sounds", {"WAV"}, false)
		if (file~="") then
			graphicsPrintln(dire .. "/" .. file,green)
			options.sound="./sounds/" .. file
		end
	end
	if (opcion==3) then
		dire,file=menu_dir("./langs", {"LAN"}, false)
		if (file~="") then
			graphicsPrintln(dire .. "/" .. file,green)
			options.lang="./langs/" .. file
			dofile (options.lang)
		end
	end
	if (opcion==4) then
		if options.show_nicks==true then
			options.show_nicks=false
		else
			options.show_nicks=true
		end
	end
	System.currentDirectory(main_dir)
end

function msn()
	local opcion=pintar_menu({"Conectar con MSN"})
	if (opcion==1) then
		socket:send("<presence to='" .. options.pasarelamsn .. "'><priority>5</priority></presence>")
	end
end
function nuevo_contacto()
	local contacto=input_text("JabberId: ","[]=Enter")
	if (contacto~="") then
		i=table.getn(contactos)+1
		contactos[i]={}
		contactos[i].id = contacto
		table.setn(contactos,i)
		socket:send("<presence type='subscribe' to='" .. contacto .. "'/>")
	else
		graphicsPrintln(textos.blank_cont,green)
	end
end

function borrar_contacto()
	local i=1
	local lista={}
	for i=1,table.getn(contactos) do
					lista[i]=contactos[i].id
					table.setn(lista,i)
	end
	local contacto=pintar_menu(lista)
	if (contacto>0) then
		local contactoid=contactos[contacto].id
		graphicsPrintln(textos.del_cont .. " " .. contactoid, green, true)
		graphicsPrintln(textos.confirm, green, true)
		while true do
			if Controls.read():cross() then graphicsPrintln(textos.cancelled, green, true)break end
			if Controls.read():circle() then
				num_contacto=1
				i=table.getn(contactos)
				table.remove(contactos,contacto)
				table.setn(contactos,i-1)
				graphicsPrintln(textos.contact .. contactoid .. textos.erased,green)
				System.sleep(300)
				break 
			end
		end
	end
end

function cambiar_usuario()
	local servidor=input_text(textos.new_server)
	local puerto=input_text(textos.new_port)
	if (puerto=="") then puerto="5222" end
	local user=input_text(textos.new_id)
	local password=input_text(textos.new_pass)
	graphicsPrintln("JabberId:"..user .. "@" .. servidor .. " Puerto:" .. puerto .. " Password:" .. password, green, true)
	graphicsPrintln(textos.confirm, green, true)
	while true do
		if Controls.read():cross() then graphicsPrintln(textos.cancelled, green, true) return false end
		if Controls.read():circle() then
			options.user=user
			options.server=servidor
			options.password=password
			options.port=puerto
			System.sleep(300)
			return true
		end
	end
end

-----CARGA LOS EMOTICONOS DEL ARCHIVO EMOTICONS.XML--------
ei_iconos1={}
ei_iconos2={}
ei_cont=1
file = io.open("./icons/emoticons.xml", "r")
if file then
	for line in file:lines() do
		if (string.sub(line,1,15)=="<emoticon file=") then
			ie_j=string.find(line,'>', 1,true)
			ei_icono2=string.sub(line,17,ie_j-2)
		else
			ie_i=string.find(line,'<string>', 1,true)
			ie_j=string.find(line,'</string>', 1,true)
			if (ie_i) then
				ei_icono1=string.sub(line,ie_i+8,ie_j-1)
				ei_iconos1[ei_cont]=ei_icono1
				ei_iconos2[ei_cont]=ei_icono2
				ei_cont=ei_cont+1
			end
		end
	end
	file:close()
end

-------CAMBIA LOS EMOTICONOS POR SUS NOMBRES DE ARCHIVO-----------
function eiconos(ei_texto)
	local encontrado=false
	local i=1
	while i <= string.len(ei_texto) do
		for index, icono in ei_iconos1 do
			encontrado=string.find(string.lower(ei_texto), string.lower(icono), i,true)
			if (encontrado==i) then
				ei_texto=string.sub(ei_texto,1,encontrado-1) .. ei_iconos2[index] .. string.sub(ei_texto,encontrado + string.len(icono),string.len(ei_texto))
			end
		end
		i=i+1
	end
	return ei_texto
end

------------CREA UN NUEVO USURIO DE JABBER--------------
function crear_cuenta()
	local servidor=input_text(textos.new_server)
	local puerto=input_text(textos.new_port)
	if (puerto=="") then puerto="5222" end
	local user=input_text(textos.new_id)
	local password=input_text(textos.new_pass)
	graphicsPrintln("JabberId:"..user .. "@" .. servidor .. " Puerto:" .. puerto .. " Password:" .. password, green, true)
	graphicsPrintln(textos.confirm, green, true)
	while true do
		if Controls.read():cross() then graphicsPrintln(textos.cancelled, green, true)break end
		if Controls.read():circle() then
			options.user=user
			options.server=servidor
			options.password=password
			options.port=puerto
			conectar()
			socket:send("<iq type='set' id='reg_id'><query xmlns='jabber:iq:register'><username>" .. options.user .. "</username><password>" .. options.password .. "</password></query></iq>")
			buffer=""
			while (buffer=="") do
				buffer=socket:recv()
				System.sleep(200)
			end
			if (options.debug==1) then
				graphicsPrintln(buffer,red,true)
			end
			if (string.find(buffer, "type='result'", 1, true)) then
				graphicsPrintln(textos.reg_ok, green, true)
			else
				graphicsPrintln(textos.reg_err, green, true)
				graphicsPrintln(textos.reg_check, green, true)
			end
			System.sleep(300)
			while true do
				if Controls.read():circle() then break end
			end
--			salir=true
			break 
		end
	end
end
-----------GRABA LA CONFIGURACION AL SALIR---------------------------
function salir2()
	file = io.open("config.txt", "w")
	if file then
		file:write("server=" .. options.server .. "\n")
		file:write("port=" .. options.port .. "\n")
		file:write("user=" .. options.user .. "\n")
		file:write("password=" .. options.password .. "\n")
		if (options.groupnick~=nil) then
			file:write("groupnick=" .. options.groupnick .. "\n")
		end
		file:write("lang=" .. options.lang .. "\n")
		file:write("wlan_id=" .. options.wlan_id .. "\n")
		file:write("debug=" .. options.debug .. "\n")
		file:write("sound=" .. options.sound .. "\n")
		file:write("back=" .. options.back .. "\n")
		file:write("show_nicks=" .. tostring(options.show_nicks) .. "\n")
-- 		if (options.usuariomsn) then
-- 			file:write("usuariomsn=" .. options.usuariomsn .. "\n")
-- 			file:write("passwordmsn=" .. options.passwordmsn .. "\n")
-- 			file:write("pasarelamsn=" .. options.pasarelamsn .. "\n")
-- 		end
		for i=1,table.getn(contactos) do
			if (contactos[i].tipo=="groupchat") then
				file:write("groupchat=" .. contactos[i].id .. "\n")
			else
				file:write("contact=" .. contactos[i].id .. "\n")
			end
		end
		for i=1,table.getn(shorts) do
			file:write("shortcut=" .. shorts[i] .. "\n")
		end
		file:close()
	end
	socket:send("</stream:stream>")
	Wlan.term()
	os.exit(1)
end
