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 user_exists = require "core.usermanager".user_exists;
14 local jid_split = require "util.jid".split;
15 local print, debug = print, debug;
17 function core_process_stanza(origin, stanza)
18 log("debug", "Received: "..tostring(stanza))
19 -- TODO verify validity of stanza (as well as JID validity)
20 if stanza.name == "iq" and not(#stanza.tags == 1 and stanza.tags[1].attr.xmlns) then
21 if stanza.attr.type == "set" or stanza.attr.type == "get" then
23 elseif #stanza.tags > 1 or not(stanza.attr.type == "error" or stanza.attr.type == "result") then
28 if origin.type == "c2s" and not origin.full_jid
29 and not(stanza.name == "iq" and stanza.tags[1].name == "bind"
30 and stanza.tags[1].attr.xmlns == "urn:ietf:params:xml:ns:xmpp-bind") then
31 error("Client MUST bind resource after auth");
34 local to = stanza.attr.to;
35 stanza.attr.from = origin.full_jid; -- quick fix to prevent impersonation (FIXME this would be incorrect when the origin is not c2s)
36 -- TODO also, stazas should be returned to their original state before the function ends
38 -- TODO presence subscriptions
40 core_handle_stanza(origin, stanza);
41 elseif hosts[to] and hosts[to].type == "local" then
42 core_handle_stanza(origin, stanza);
43 elseif stanza.name == "iq" and not select(3, jid_split(to)) then
44 core_handle_stanza(origin, stanza);
45 elseif origin.type == "c2s" then
46 core_route_stanza(origin, stanza);
50 -- This function handles stanzas which are not routed any further,
51 -- that is, they are handled by this server
52 function core_handle_stanza(origin, stanza)
54 if origin.type == "c2s" or origin.type == "c2s_unauthed" then
55 local session = origin;
57 if stanza.name == "presence" and origin.roster then
58 if stanza.attr.type == nil or stanza.attr.type == "available" or stanza.attr.type == "unavailable" then
59 for jid in pairs(origin.roster) do -- broadcast to all interested contacts
60 local subscription = origin.roster[jid].subscription;
61 if subscription == "both" or subscription == "from" then
63 core_route_stanza(origin, stanza);
66 local node, host = jid_split(stanza.attr.from);
67 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast to all resources and from resources
69 if res.full_jid then -- to resource. FIXME is this check correct? Maybe it should be res.presence
70 stanza.attr.to = res.full_jid;
71 core_route_stanza(origin, stanza);
73 if res.presence then -- from all resources for which we have presence
74 res.presence.attr.to = origin.full_jid;
75 core_route_stanza(res, res.presence);
76 res.presence.attr.to = nil;
80 if not origin.presence then -- presence probes on initial presence
81 local probe = st.presence({from = origin.full_jid, type = "probe"});
82 for jid in pairs(origin.roster) do
83 local subscription = origin.roster[jid].subscription;
84 if subscription == "both" or subscription == "to" then
86 core_route_stanza(origin, probe);
90 origin.presence = stanza;
91 stanza.attr.to = nil; -- reset it
93 -- TODO error, bad type
96 log("debug", "Routing stanza to local");
97 handle_stanza(session, stanza);
102 function is_authorized_to_see_presence(origin, username, host)
103 local roster = datamanager.load(username, host, "roster") or {};
104 local item = roster[origin.username.."@"..origin.host];
105 return item and (item.subscription == "both" or item.subscription == "from");
108 function core_route_stanza(origin, stanza)
113 local to = stanza.attr.to;
114 local node, host, resource = jid_split(to);
116 if stanza.name == "presence" and (stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable") then resource = nil; end
118 local host_session = hosts[host]
119 if host_session and host_session.type == "local" then
121 local user = host_session.sessions[node];
123 local res = user.sessions[resource];
125 -- if we get here, resource was not specified or was unavailable
126 if stanza.name == "presence" then
127 if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then
128 if stanza.attr.type == "probe" then
129 if is_authorized_to_see_presence(origin, node, host) then
130 for k in pairs(user.sessions) do -- return presence for all resources
131 if user.sessions[k].presence then
132 local pres = user.sessions[k].presence;
133 pres.attr.to = origin.full_jid;
134 pres.attr.from = user.sessions[k].full_jid;
137 pres.attr.from = nil;
141 send(origin, st.presence({from=user.."@"..host, to=origin.username.."@"..origin.host, type="unsubscribed"}));
143 elseif stanza.attr.type == "subscribe" then
145 elseif stanza.attr.type == "unsubscribe" then
147 elseif stanza.attr.type == "subscribed" then
149 elseif stanza.attr.type == "unsubscribed" then
151 end -- discard any other type
152 else -- sender is available or unavailable
153 for k in pairs(user.sessions) do -- presence broadcast to all user resources
154 if user.sessions[k].full_jid then
155 stanza.attr.to = user.sessions[k].full_jid;
156 send(user.sessions[k], stanza);
160 elseif stanza.name == "message" then -- select a resource to recieve message
161 for k in pairs(user.sessions) do
162 if user.sessions[k].full_jid then
163 res = user.sessions[k];
167 -- TODO find resource with greatest priority
170 -- TODO send IQ error
173 -- User + resource is online...
174 stanza.attr.to = res.full_jid;
175 send(res, stanza); -- Yay \o/
179 if user_exists(node, host) then
180 if stanza.name == "presence" then
181 if stanza.attr.type == "probe" and is_authorized_to_see_presence(origin, node, host) then -- FIXME what to do for not c2s?
182 -- TODO send last recieved unavailable presence
184 -- TODO send unavailable presence
186 elseif stanza.name == "message" then
187 -- TODO send message error, or store offline messages
188 elseif stanza.name == "iq" then
189 -- TODO send IQ error
191 else -- user does not exist
192 -- TODO we would get here for nodeless JIDs too. Do something fun maybe? Echo service? Let plugins use xmpp:server/resource addresses?
193 if stanza.name == "presence" then
194 if stanza.attr.type == "probe" then
195 send(origin, st.presence({from = user.."@"..host, to = origin.username.."@"..origin.host, type = "unsubscribed"}));
199 send(origin, st.error_reply(stanza, "cancel", "service-unavailable"));
208 -- Need to establish the connection
211 stanza.attr.to = to; -- reset
214 function handle_stanza_toremote(stanza)
215 log("error", "Stanza bound for remote host, but s2s is not implemented");