2 -- The code in this file should be self-explanatory, though the logic is horrible
\r
3 -- for more info on that, see doc/stanza_routing.txt, which attempts to condense
\r
4 -- the rules from the RFCs (mainly 3921)
\r
6 require "core.servermanager"
\r
8 local log = require "util.logger".init("stanzarouter")
\r
10 local st = require "util.stanza";
\r
11 local send = require "core.sessionmanager".send_to_session;
\r
12 -- local send_s2s = require "core.s2smanager".send_to_host;
\r
13 local user_exists = require "core.usermanager".user_exists;
\r
15 local jid_split = require "util.jid".split;
\r
16 local print, debug = print, debug;
\r
18 function core_process_stanza(origin, stanza)
\r
19 log("debug", "Received: "..tostring(stanza))
\r
20 -- TODO verify validity of stanza (as well as JID validity)
\r
21 if stanza.name == "iq" and not(#stanza.tags == 1 and stanza.tags[1].attr.xmlns) then
\r
22 if stanza.attr.type == "set" or stanza.attr.type == "get" then
\r
23 error("Invalid IQ");
\r
24 elseif #stanza.tags > 1 or not(stanza.attr.type == "error" or stanza.attr.type == "result") then
\r
25 error("Invalid IQ");
\r
29 if origin.type == "c2s" and not origin.full_jid
\r
30 and not(stanza.name == "iq" and stanza.tags[1].name == "bind"
\r
31 and stanza.tags[1].attr.xmlns == "urn:ietf:params:xml:ns:xmpp-bind") then
\r
32 error("Client MUST bind resource after auth");
\r
35 local to = stanza.attr.to;
\r
36 stanza.attr.from = origin.full_jid; -- quick fix to prevent impersonation (FIXME this would be incorrect when the origin is not c2s)
\r
37 -- TODO also, stazas should be returned to their original state before the function ends
\r
39 -- TODO presence subscriptions
\r
41 core_handle_stanza(origin, stanza);
\r
42 elseif hosts[to] and hosts[to].type == "local" then
\r
43 core_handle_stanza(origin, stanza);
\r
44 elseif stanza.name == "iq" and not select(3, jid_split(to)) then
\r
45 core_handle_stanza(origin, stanza);
\r
46 elseif origin.type == "c2s" then
\r
47 core_route_stanza(origin, stanza);
\r
51 -- This function handles stanzas which are not routed any further,
\r
52 -- that is, they are handled by this server
\r
53 function core_handle_stanza(origin, stanza)
\r
55 if origin.type == "c2s" or origin.type == "c2s_unauthed" then
\r
56 local session = origin;
\r
58 if stanza.name == "presence" and origin.roster then
\r
59 if stanza.attr.type == nil or stanza.attr.type == "available" or stanza.attr.type == "unavailable" then
\r
60 for jid in pairs(origin.roster) do -- broadcast to all interested contacts
\r
61 local subscription = origin.roster[jid].subscription;
\r
62 if subscription == "both" or subscription == "from" then
\r
63 stanza.attr.to = jid;
\r
64 core_route_stanza(origin, stanza);
\r
67 local node, host = jid_split(stanza.attr.from);
\r
68 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast to all resources and from resources
\r
69 if res ~= origin then
\r
70 if res.full_jid then -- to resource. FIXME is this check correct? Maybe it should be res.presence
\r
71 stanza.attr.to = res.full_jid;
\r
72 core_route_stanza(origin, stanza);
\r
74 if res.presence then -- from all resources for which we have presence
\r
75 res.presence.attr.to = origin.full_jid;
\r
76 core_route_stanza(res, res.presence);
\r
77 res.presence.attr.to = nil;
\r
81 if not origin.presence then -- presence probes on initial presence
\r
82 local probe = st.presence({from = origin.full_jid, type = "probe"});
\r
83 for jid in pairs(origin.roster) do
\r
84 local subscription = origin.roster[jid].subscription;
\r
85 if subscription == "both" or subscription == "to" then
\r
86 probe.attr.to = jid;
\r
87 core_route_stanza(origin, probe);
\r
91 origin.presence = stanza;
\r
92 stanza.attr.to = nil; -- reset it
\r
94 -- TODO error, bad type
\r
97 log("debug", "Routing stanza to local");
\r
98 handle_stanza(session, stanza);
\r
103 function is_authorized_to_see_presence(origin, username, host)
\r
104 local roster = datamanager.load(username, host, "roster") or {};
\r
105 local item = roster[origin.username.."@"..origin.host];
\r
106 return item and (item.subscription == "both" or item.subscription == "from");
\r
109 function core_route_stanza(origin, stanza)
\r
114 local to = stanza.attr.to;
\r
115 local node, host, resource = jid_split(to);
\r
117 if stanza.name == "presence" and (stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable") then resource = nil; end
\r
119 local host_session = hosts[host]
\r
120 if host_session and host_session.type == "local" then
\r
122 local user = host_session.sessions[node];
\r
124 local res = user.sessions[resource];
\r
126 -- if we get here, resource was not specified or was unavailable
\r
127 if stanza.name == "presence" then
\r
128 if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then
\r
129 if stanza.attr.type == "probe" then
\r
130 if is_authorized_to_see_presence(origin, node, host) then
\r
131 for k in pairs(user.sessions) do -- return presence for all resources
\r
132 if user.sessions[k].presence then
\r
133 local pres = user.sessions[k].presence;
\r
134 pres.attr.to = origin.full_jid;
\r
135 pres.attr.from = user.sessions[k].full_jid;
\r
136 send(origin, pres);
\r
137 pres.attr.to = nil;
\r
138 pres.attr.from = nil;
\r
142 send(origin, st.presence({from=user.."@"..host, to=origin.username.."@"..origin.host, type="unsubscribed"}));
\r
144 elseif stanza.attr.type == "subscribe" then
\r
146 elseif stanza.attr.type == "unsubscribe" then
\r
148 elseif stanza.attr.type == "subscribed" then
\r
150 elseif stanza.attr.type == "unsubscribed" then
\r
152 end -- discard any other type
\r
153 else -- sender is available or unavailable
\r
154 for k in pairs(user.sessions) do -- presence broadcast to all user resources
\r
155 if user.sessions[k].full_jid then
\r
156 stanza.attr.to = user.sessions[k].full_jid;
\r
157 send(user.sessions[k], stanza);
\r
161 elseif stanza.name == "message" then -- select a resource to recieve message
\r
162 for k in pairs(user.sessions) do
\r
163 if user.sessions[k].full_jid then
\r
164 res = user.sessions[k];
\r
168 -- TODO find resource with greatest priority
\r
171 -- TODO send IQ error
\r
174 -- User + resource is online...
\r
175 stanza.attr.to = res.full_jid;
\r
176 send(res, stanza); -- Yay \o/
\r
180 if user_exists(node, host) then
\r
181 if stanza.name == "presence" then
\r
182 if stanza.attr.type == "probe" and is_authorized_to_see_presence(origin, node, host) then -- FIXME what to do for not c2s?
\r
183 -- TODO send last recieved unavailable presence
\r
185 -- TODO send unavailable presence
\r
187 elseif stanza.name == "message" then
\r
188 -- TODO send message error, or store offline messages
\r
189 elseif stanza.name == "iq" then
\r
190 -- TODO send IQ error
\r
192 else -- user does not exist
\r
193 -- TODO we would get here for nodeless JIDs too. Do something fun maybe? Echo service? Let plugins use xmpp:server/resource addresses?
\r
194 if stanza.name == "presence" then
\r
195 if stanza.attr.type == "probe" then
\r
196 send(origin, st.presence({from = user.."@"..host, to = origin.username.."@"..origin.host, type = "unsubscribed"}));
\r
200 send(origin, st.error_reply(stanza, "cancel", "service-unavailable"));
\r
206 if host_session then
\r
209 -- Need to establish the connection
\r
212 stanza.attr.to = to; -- reset
\r
215 function handle_stanza_toremote(stanza)
\r
216 log("error", "Stanza bound for remote host, but s2s is not implemented");
\r