core/stanza_router.lua

changeset 140
a0fc73d5f48c
parent 139
8f17ba74823c
child 141
37b3e9ed8918
equal deleted inserted replaced
139:8f17ba74823c 140:a0fc73d5f48c
1 <<<<<<< local
1 2
2 -- The code in this file should be self-explanatory, though the logic is horrible 3 -- 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 -- for more info on that, see doc/stanza_routing.txt, which attempts to condense
4 -- the rules from the RFCs (mainly 3921) 5 -- the rules from the RFCs (mainly 3921)
5 6
11 local send = require "core.sessionmanager".send_to_session; 12 local send = require "core.sessionmanager".send_to_session;
12 -- local send_s2s = require "core.s2smanager".send_to_host; 13 -- local send_s2s = require "core.s2smanager".send_to_host;
13 local user_exists = require "core.usermanager".user_exists; 14 local user_exists = require "core.usermanager".user_exists;
14 15
15 local jid_split = require "util.jid".split; 16 local jid_split = require "util.jid".split;
16 local print = print; 17 local print, debug = print, debug;
17 18
18 function core_process_stanza(origin, stanza) 19 function core_process_stanza(origin, stanza)
19 log("debug", "Received: "..tostring(stanza)) 20 log("debug", "Received: "..tostring(stanza))
20 -- TODO verify validity of stanza (as well as JID validity) 21 -- 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.name == "iq" and not(#stanza.tags == 1 and stanza.tags[1].attr.xmlns) then
62 if subscription == "both" or subscription == "from" then 63 if subscription == "both" or subscription == "from" then
63 stanza.attr.to = jid; 64 stanza.attr.to = jid;
64 core_route_stanza(origin, stanza); 65 core_route_stanza(origin, stanza);
65 end 66 end
66 end 67 end
68 local node, host = jid_split(stanza.attr.from);
69 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast to all resources and from resources
70 if res ~= origin then
71 if res.full_jid then -- to resource. FIXME is this check correct? Maybe it should be res.presence
72 stanza.attr.to = res.full_jid;
73 core_route_stanza(origin, stanza);
74 end
75 if res.presence then -- from all resources for which we have presence
76 res.presence.attr.to = origin.full_jid;
77 core_route_stanza(res, res.presence);
78 res.presence.attr.to = nil;
79 end
80 end
81 end
82 if not origin.presence then -- presence probes on initial presence
83 local probe = st.presence({from = origin.full_jid, type = "probe"});
84 for jid in pairs(origin.roster) do
85 local subscription = origin.roster[jid].subscription;
86 if subscription == "both" or subscription == "to" then
87 probe.attr.to = jid;
88 core_route_stanza(origin, probe);
89 end
90 end
91 -- TODO resend subscription requests
92 end
93 origin.presence = stanza;
94 stanza.attr.to = nil; -- reset it
95 else
96 -- TODO error, bad type
97 end
98 else
99 log("debug", "Routing stanza to local");
100 handle_stanza(session, stanza);
101 end
102 end
103 end
104
105 function is_authorized_to_see_presence(origin, username, host)
106 local roster = datamanager.load(username, host, "roster") or {};
107 local item = roster[origin.username.."@"..origin.host];
108 return item and (item.subscription == "both" or item.subscription == "from");
109 end
110
111 function core_route_stanza(origin, stanza)
112 -- Hooks
113 --- ...later
114
115 -- Deliver
116 local to = stanza.attr.to;
117 local node, host, resource = jid_split(to);
118
119 if stanza.name == "presence" and (stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable") then resource = nil; end
120
121 local host_session = hosts[host]
122 if host_session and host_session.type == "local" then
123 -- Local host
124 local user = host_session.sessions[node];
125 if user then
126 local res = user.sessions[resource];
127 if not res then
128 -- if we get here, resource was not specified or was unavailable
129 if stanza.name == "presence" then
130 if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then
131 if stanza.attr.type == "probe" then
132 if is_authorized_to_see_presence(origin, node, host) then
133 for k in pairs(user.sessions) do -- return presence for all resources
134 if user.sessions[k].presence then
135 local pres = user.sessions[k].presence;
136 pres.attr.to = origin.full_jid;
137 pres.attr.from = user.sessions[k].full_jid;
138 send(origin, pres);
139 pres.attr.to = nil;
140 pres.attr.from = nil;
141 end
142 end
143 else
144 send(origin, st.presence({from=user.."@"..host, to=origin.username.."@"..origin.host, type="unsubscribed"}));
145 end
146 elseif stanza.attr.type == "subscribe" then
147 -- TODO
148 elseif stanza.attr.type == "unsubscribe" then
149 -- TODO
150 elseif stanza.attr.type == "subscribed" then
151 -- TODO
152 elseif stanza.attr.type == "unsubscribed" then
153 -- TODO
154 end -- discard any other type
155 else -- sender is available or unavailable
156 for k in pairs(user.sessions) do -- presence broadcast to all user resources
157 if user.sessions[k].full_jid then
158 stanza.attr.to = user.sessions[k].full_jid;
159 send(user.sessions[k], stanza);
160 end
161 end
162 end
163 elseif stanza.name == "message" then -- select a resource to recieve message
164 for k in pairs(user.sessions) do
165 if user.sessions[k].full_jid then
166 res = user.sessions[k];
167 break;
168 end
169 end
170 -- TODO find resource with greatest priority
171 send(res, stanza);
172 else
173 -- TODO send IQ error
174 end
175 else
176 -- User + resource is online...
177 stanza.attr.to = res.full_jid;
178 send(res, stanza); -- Yay \o/
179 end
180 else
181 -- user not online
182 if user_exists(node, host) then
183 if stanza.name == "presence" then
184 if stanza.attr.type == "probe" and is_authorized_to_see_presence(origin, node, host) then -- FIXME what to do for not c2s?
185 -- TODO send last recieved unavailable presence
186 else
187 -- TODO send unavailable presence
188 end
189 elseif stanza.name == "message" then
190 -- TODO send message error, or store offline messages
191 elseif stanza.name == "iq" then
192 -- TODO send IQ error
193 end
194 else -- user does not exist
195 -- TODO we would get here for nodeless JIDs too. Do something fun maybe? Echo service? Let plugins use xmpp:server/resource addresses?
196 if stanza.name == "presence" then
197 if stanza.attr.type == "probe" then
198 send(origin, st.presence({from = user.."@"..host, to = origin.username.."@"..origin.host, type = "unsubscribed"}));
199 end
200 -- else ignore
201 else
202 send(origin, st.error_reply(stanza, "cancel", "service-unavailable"));
203 end
204 end
205 end
206 else
207 -- Remote host
208 if host_session then
209 -- Send to session
210 else
211 -- Need to establish the connection
212 end
213 end
214 stanza.attr.to = to; -- reset
215 end
216
217 function handle_stanza_toremote(stanza)
218 log("error", "Stanza bound for remote host, but s2s is not implemented");
219 end
220 =======
221
222 -- The code in this file should be self-explanatory, though the logic is horrible
223 -- for more info on that, see doc/stanza_routing.txt, which attempts to condense
224 -- the rules from the RFCs (mainly 3921)
225
226 require "core.servermanager"
227
228 local log = require "util.logger".init("stanzarouter")
229
230 local st = require "util.stanza";
231 local send = require "core.sessionmanager".send_to_session;
232 -- local send_s2s = require "core.s2smanager".send_to_host;
233 local user_exists = require "core.usermanager".user_exists;
234
235 local jid_split = require "util.jid".split;
236 local print = print;
237
238 function core_process_stanza(origin, stanza)
239 log("debug", "Received: "..tostring(stanza))
240 -- TODO verify validity of stanza (as well as JID validity)
241 if stanza.name == "iq" and not(#stanza.tags == 1 and stanza.tags[1].attr.xmlns) then
242 if stanza.attr.type == "set" or stanza.attr.type == "get" then
243 error("Invalid IQ");
244 elseif #stanza.tags > 1 or not(stanza.attr.type == "error" or stanza.attr.type == "result") then
245 error("Invalid IQ");
246 end
247 end
248
249 if origin.type == "c2s" and not origin.full_jid
250 and not(stanza.name == "iq" and stanza.tags[1].name == "bind"
251 and stanza.tags[1].attr.xmlns == "urn:ietf:params:xml:ns:xmpp-bind") then
252 error("Client MUST bind resource after auth");
253 end
254
255 local to = stanza.attr.to;
256 stanza.attr.from = origin.full_jid; -- quick fix to prevent impersonation (FIXME this would be incorrect when the origin is not c2s)
257 -- TODO also, stazas should be returned to their original state before the function ends
258
259 -- TODO presence subscriptions
260 if not to then
261 core_handle_stanza(origin, stanza);
262 elseif hosts[to] and hosts[to].type == "local" then
263 core_handle_stanza(origin, stanza);
264 elseif stanza.name == "iq" and not select(3, jid_split(to)) then
265 core_handle_stanza(origin, stanza);
266 elseif origin.type == "c2s" then
267 core_route_stanza(origin, stanza);
268 end
269 end
270
271 -- This function handles stanzas which are not routed any further,
272 -- that is, they are handled by this server
273 function core_handle_stanza(origin, stanza)
274 -- Handlers
275 if origin.type == "c2s" or origin.type == "c2s_unauthed" then
276 local session = origin;
277
278 if stanza.name == "presence" and origin.roster then
279 if stanza.attr.type == nil or stanza.attr.type == "available" or stanza.attr.type == "unavailable" then
280 for jid in pairs(origin.roster) do -- broadcast to all interested contacts
281 local subscription = origin.roster[jid].subscription;
282 if subscription == "both" or subscription == "from" then
283 stanza.attr.to = jid;
284 core_route_stanza(origin, stanza);
285 end
286 end
67 --[[local node, host = jid_split(stanza.attr.from); 287 --[[local node, host = jid_split(stanza.attr.from);
68 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast to all resources 288 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast to all resources
69 if res.full_jid then 289 if res.full_jid then
70 res = user.sessions[k]; 290 res = user.sessions[k];
71 break; 291 break;
91 handle_stanza(session, stanza); 311 handle_stanza(session, stanza);
92 end 312 end
93 end 313 end
94 end 314 end
95 315
316 -- TODO: Does this function belong here?
96 function is_authorized_to_see_presence(origin, username, host) 317 function is_authorized_to_see_presence(origin, username, host)
97 local roster = datamanager.load(username, host, "roster") or {}; 318 local roster = datamanager.load(username, host, "roster") or {};
98 local item = roster[origin.username.."@"..origin.host]; 319 local item = roster[origin.username.."@"..origin.host];
99 return item and (item.subscription == "both" or item.subscription == "from"); 320 return item and (item.subscription == "both" or item.subscription == "from");
100 end 321 end
184 end 405 end
185 end 406 end
186 end 407 end
187 else 408 else
188 -- Remote host 409 -- Remote host
189 if host_session then 410 send_s2s(origin.host, host, stanza);
190 -- Send to session
191 else
192 -- Need to establish the connection
193 end
194 end 411 end
195 stanza.attr.to = to; -- reset 412 stanza.attr.to = to; -- reset
196 end 413 end
197 414
198 function handle_stanza_toremote(stanza) 415 function handle_stanza_toremote(stanza)
199 log("error", "Stanza bound for remote host, but s2s is not implemented"); 416 log("error", "Stanza bound for remote host, but s2s is not implemented");
200 end 417 end
418 >>>>>>> other

mercurial