2 -- The code in this file should be self-explanatory, though the logic is horrible
3 -- for more info on that, see doc/stanza_routing.txt, which attempts to condense
4 -- the rules from the RFCs (mainly 3921)
6 require "core.servermanager"
8 local log = require "util.logger".init("stanzarouter")
10 local st = require "util.stanza";
11 local send = require "core.sessionmanager".send_to_session;
12 -- local send_s2s = require "core.s2smanager".send_to_host;
13 local user_exists = require "core.usermanager".user_exists;
15 local jid_split = require "util.jid".split;
18 function core_process_stanza(origin, stanza)
19 log("debug", "Received: "..tostring(stanza))
20 -- TODO verify validity of stanza (as well as JID validity)
21 if stanza.name == "iq" and not(#stanza.tags == 1 and stanza.tags[1].attr.xmlns) then
22 if stanza.attr.type == "set" or stanza.attr.type == "get" then
24 elseif #stanza.tags > 1 or not(stanza.attr.type == "error" or stanza.attr.type == "result") then
29 if origin.type == "c2s" and not origin.full_jid
30 and not(stanza.name == "iq" and stanza.tags[1].name == "bind"
31 and stanza.tags[1].attr.xmlns == "urn:ietf:params:xml:ns:xmpp-bind") then
32 error("Client MUST bind resource after auth");
35 local to = stanza.attr.to;
36 stanza.attr.from = origin.full_jid; -- quick fix to prevent impersonation (FIXME this would be incorrect when the origin is not c2s)
37 -- TODO also, stazas should be returned to their original state before the function ends
39 -- TODO presence subscriptions
41 core_handle_stanza(origin, stanza);
42 elseif hosts[to] and hosts[to].type == "local" then
43 core_handle_stanza(origin, stanza);
44 elseif stanza.name == "iq" and not select(3, jid_split(to)) then
45 core_handle_stanza(origin, stanza);
46 elseif origin.type == "c2s" then
47 core_route_stanza(origin, stanza);
51 -- This function handles stanzas which are not routed any further,
52 -- that is, they are handled by this server
53 function core_handle_stanza(origin, stanza)
55 if origin.type == "c2s" or origin.type == "c2s_unauthed" then
56 local session = origin;
58 if stanza.name == "presence" and origin.roster then
59 if stanza.attr.type == nil or stanza.attr.type == "available" or stanza.attr.type == "unavailable" then
60 for jid in pairs(origin.roster) do -- broadcast to all interested contacts
61 local subscription = origin.roster[jid].subscription;
62 if subscription == "both" or subscription == "from" then
64 core_route_stanza(origin, stanza);
67 --[[local node, host = jid_split(stanza.attr.from);
68 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast to all resources
70 res = user.sessions[k];
74 if not origin.presence then -- presence probes on initial presence
75 local probe = st.presence({from = origin.full_jid, type = "probe"});
76 for jid in pairs(origin.roster) do
77 local subscription = origin.roster[jid].subscription;
78 if subscription == "both" or subscription == "to" then
80 core_route_stanza(origin, probe);
84 origin.presence = stanza;
85 stanza.attr.to = nil; -- reset it
87 -- TODO error, bad type
90 log("debug", "Routing stanza to local");
91 handle_stanza(session, stanza);
96 -- TODO: Does this function belong here?
97 function is_authorized_to_see_presence(origin, username, host)
98 local roster = datamanager.load(username, host, "roster") or {};
99 local item = roster[origin.username.."@"..origin.host];
100 return item and (item.subscription == "both" or item.subscription == "from");
103 function core_route_stanza(origin, stanza)
108 local to = stanza.attr.to;
109 local node, host, resource = jid_split(to);
111 if stanza.name == "presence" and stanza.attr.type == "probe" then resource = nil; end
113 local host_session = hosts[host]
114 if host_session and host_session.type == "local" then
116 local user = host_session.sessions[node];
118 local res = user.sessions[resource];
120 -- if we get here, resource was not specified or was unavailable
121 if stanza.name == "presence" then
122 if stanza.attr.type == "probe" then
123 if is_authorized_to_see_presence(origin, node, host) then
124 for k in pairs(user.sessions) do -- return presence for all resources
125 if user.sessions[k].presence then
126 local pres = user.sessions[k].presence;
127 pres.attr.to = origin.full_jid;
128 pres.attr.from = user.sessions[k].full_jid;
131 pres.attr.from = nil;
135 send(origin, st.presence({from = user.."@"..host, to = origin.username.."@"..origin.host, type = "unsubscribed"}));
138 for k in pairs(user.sessions) do -- presence broadcast to all user resources
139 if user.sessions[k].full_jid then
140 stanza.attr.to = user.sessions[k].full_jid;
141 send(user.sessions[k], stanza);
145 elseif stanza.name == "message" then -- select a resource to recieve message
146 for k in pairs(user.sessions) do
147 if user.sessions[k].full_jid then
148 res = user.sessions[k];
152 -- TODO find resource with greatest priority
155 -- TODO send IQ error
158 -- User + resource is online...
159 stanza.attr.to = res.full_jid;
160 send(res, stanza); -- Yay \o/
164 if user_exists(node, host) then
165 if stanza.name == "presence" then
166 if stanza.attr.type == "probe" and is_authorized_to_see_presence(origin, node, host) then -- FIXME what to do for not c2s?
167 -- TODO send last recieved unavailable presence
169 -- TODO send unavailable presence
171 elseif stanza.name == "message" then
172 -- TODO send message error, or store offline messages
173 elseif stanza.name == "iq" then
174 -- TODO send IQ error
176 else -- user does not exist
177 -- TODO we would get here for nodeless JIDs too. Do something fun maybe? Echo service? Let plugins use xmpp:server/resource addresses?
178 if stanza.name == "presence" then
179 if stanza.attr.type == "probe" then
180 send(origin, st.presence({from = user.."@"..host, to = origin.username.."@"..origin.host, type = "unsubscribed"}));
184 send(origin, st.error_reply(stanza, "cancel", "service-unavailable"));
190 send_s2s(origin.host, host, stanza);
192 stanza.attr.to = to; -- reset
195 function handle_stanza_toremote(stanza)
196 log("error", "Stanza bound for remote host, but s2s is not implemented");