6 local t_concat = table.concat;
7 local format = string.format;
9 function init_stanza_dispatcher(session)
10 local iq_handlers = {};
12 local session_log = session.log;
13 local log = function (type, msg) session_log(type, "stanza_dispatcher", msg); end
14 local send = session.send;
17 local _send_to = session.send_to;
18 send_to = function (...) _send_to(session, ...); end
21 iq_handlers["jabber:iq:auth"] =
23 local username = stanza.tags[1]:child_with_name("username");
24 local password = stanza.tags[1]:child_with_name("password");
25 local resource = stanza.tags[1]:child_with_name("resource");
26 if not (username and password and resource) then
27 local reply = st.reply(stanza);
28 send(reply:query("jabber:iq:auth")
31 :tag("resource"):up());
34 username, password, resource = t_concat(username), t_concat(password), t_concat(resource);
35 print(username, password, resource)
36 local reply = st.reply(stanza);
37 require "core.usermanager"
38 if usermanager.validate_credentials(session.host, username, password) then
39 -- Authentication successful!
40 session.username = username;
41 session.resource = resource;
42 if not hosts[session.host].sessions[username] then
43 hosts[session.host].sessions[username] = { sessions = {} };
45 hosts[session.host].sessions[username].sessions[resource] = session;
46 send(st.reply(stanza));
49 local reply = st.reply(stanza);
50 reply.attr.type = "error";
51 reply:tag("error", { code = "401", type = "auth" })
52 :tag("not-authorized", { xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas" });
60 iq_handlers["jabber:iq:roster"] =
62 if stanza.attr.type == "get" then
63 session.roster = session.roster or rostermanager.getroster(session.username, session.host);
64 if session.roster == false then
66 :tag("error", { type = "wait" })
67 :tag("internal-server-error", { xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas"}));
69 else session.roster = session.roster or {};
71 local roster = st.reply(stanza)
72 :query("jabber:iq:roster");
73 for jid in pairs(session.roster) do
74 roster:tag("item", { jid = jid, subscription = "none" }):up();
82 return function (stanza)
83 log("info", "--> "..tostring(stanza));
84 if (not stanza.attr.to) or (hosts[stanza.attr.to] and hosts[stanza.attr.to].type == "local") then
85 if stanza.name == "iq" then
86 if not stanza.tags[1] then log("warn", "<iq> without child is invalid"); return; end
87 if not stanza.attr.id then log("warn", "<iq> without id attribute is invalid"); end
88 local xmlns = (stanza.tags[1].attr and stanza.tags[1].attr.xmlns) or nil;
89 if not xmlns then log("warn", "Child of <iq> has no xmlns - invalid"); return; end
90 if (((not stanza.attr.to) or stanza.attr.to == session.host or stanza.attr.to:match("@[^/]+$")) and (stanza.attr.type == "get" or stanza.attr.type == "set")) then -- Stanza sent to us
91 if iq_handlers[xmlns] then
92 if iq_handlers[xmlns](stanza) then return; end;
94 log("warn", "Unhandled namespace: "..xmlns);
95 send(format("<iq type='error' id='%s'><error type='cancel'><service-unavailable/></error></iq>", stanza.attr.id));
98 elseif stanza.name == "presence" then
99 if session.roster then
100 -- Broadcast presence and probes
101 local broadcast = st.presence({ from = session.username.."@"..session.host.."/"..session.resource });
102 local probe = st.presence { from = broadcast.attr.from, type = "probe" };
104 for child in stanza:children() do
105 broadcast:tag(child.name, child.attr);
107 for contact in pairs(session.roster) do
108 broadcast.attr.to = contact;
109 send_to(contact, broadcast);
110 --local host = jid.host(contact);
111 --if hosts[host] and hosts[host].type == "local" then
112 --local node, host = jid.split(contact);
113 --if host[host].sessions[node]
114 --local pres = st.presence { from = con
116 -- probe.attr.to = contact;
117 -- send_to(contact, probe);
121 -- Probe for our contacts' presence
126 --if stanza.attr.to and ((not hosts[stanza.attr.to]) or hosts[stanza.attr.to].type ~= "local") then
127 -- Need to route stanza
128 stanza.attr.from = session.username.."@"..session.host;
129 session:send_to(stanza.attr.to, stanza);