core/stanza_dispatch.lua
changeset 2 9bb397205f26
parent 1 b8787e859fd2
child 4 09c3845ed442
equal deleted inserted replaced
1:b8787e859fd2 2:9bb397205f26
    37 				require "core.usermanager"
    37 				require "core.usermanager"
    38 				if usermanager.validate_credentials(session.host, username, password) then
    38 				if usermanager.validate_credentials(session.host, username, password) then
    39 					-- Authentication successful!
    39 					-- Authentication successful!
    40 					session.username = username;
    40 					session.username = username;
    41 					session.resource = resource;
    41 					session.resource = resource;
       
    42 					session.full_jid = username.."@"..session.host.."/"..session.resource;
    42 					if not hosts[session.host].sessions[username] then
    43 					if not hosts[session.host].sessions[username] then
    43 						hosts[session.host].sessions[username] = { sessions = {} };
    44 						hosts[session.host].sessions[username] = { sessions = {} };
    44 					end
    45 					end
    45 					hosts[session.host].sessions[username].sessions[resource] = session;
    46 					hosts[session.host].sessions[username].sessions[resource] = session;
    46 					send(st.reply(stanza));
    47 					send(st.reply(stanza));
    95 						send(format("<iq type='error' id='%s'><error type='cancel'><service-unavailable/></error></iq>", stanza.attr.id));
    96 						send(format("<iq type='error' id='%s'><error type='cancel'><service-unavailable/></error></iq>", stanza.attr.id));
    96 						return;
    97 						return;
    97 					end
    98 					end
    98 				elseif stanza.name == "presence" then
    99 				elseif stanza.name == "presence" then
    99 					if session.roster then
   100 					if session.roster then
       
   101 						local initial_presence = not session.last_presence;
       
   102 						session.last_presence = stanza;
       
   103 						
   100 						-- Broadcast presence and probes
   104 						-- Broadcast presence and probes
   101 						local broadcast = st.presence({ from = session.username.."@"..session.host.."/"..session.resource });
   105 						local broadcast = st.presence({ from = session.full_jid, type = stanza.attr.type });
   102 						local probe = st.presence { from = broadcast.attr.from, type = "probe" };
   106 						--local probe = st.presence { from = broadcast.attr.from, type = "probe" };
   103 
   107 
   104 						for child in stanza:children() do
   108 						for child in stanza:childtags() do
   105 							broadcast:tag(child.name, child.attr);
   109 							broadcast:text(tostring(child));
   106 						end
   110 						end
   107 						for contact in pairs(session.roster) do
   111 						for contact_jid in pairs(session.roster) do
   108 							broadcast.attr.to = contact;
   112 							broadcast.attr.to = contact_jid;
   109 							send_to(contact, broadcast);
   113 							send_to(contact_jid, broadcast);
   110 							--local host = jid.host(contact);
   114 							if initial_presence then
   111 							--if hosts[host] and hosts[host].type == "local" then
   115 								print("Initital presence");
   112 								--local node, host = jid.split(contact);
   116 								local node, host = jid.split(contact_jid);
   113 								--if host[host].sessions[node]
   117 								if hosts[host] and hosts[host].type == "local" then
   114 								--local pres = st.presence { from = con
   118 									local contact = hosts[host].sessions[node]
   115 							--else
   119 									if contact then
   116 							--	probe.attr.to = contact;
   120 										local pres = st.presence { to = session.full_jid };
   117 							--	send_to(contact, probe);
   121 										for resource, contact_session in pairs(contact.sessions) do
   118 							--end
   122 											if contact_session.last_presence then
       
   123 												pres.tags = contact_session.last_presence.tags;
       
   124 												pres.attr.from = contact_session.full_jid;
       
   125 												send(pres);
       
   126 											end
       
   127 										end
       
   128 									end
       
   129 									--FIXME: Do we send unavailable if they are offline?
       
   130 								else
       
   131 									probe.attr.to = contact;
       
   132 									send_to(contact, probe);
       
   133 								end
       
   134 							end
   119 						end
   135 						end
   120 						
   136 						
   121 						-- Probe for our contacts' presence
   137 						-- Probe for our contacts' presence
   122 					end
   138 					end
   123 				end
   139 				end