core/stanza_router.lua

changeset 71
fbb4ef1da82e
parent 68
ceb7a55676a4
child 72
e78db62beebc
equal deleted inserted replaced
70:a6c00467a3f8 71:fbb4ef1da82e
5 5
6 require "core.servermanager" 6 require "core.servermanager"
7 7
8 local log = require "util.logger".init("stanzarouter") 8 local log = require "util.logger".init("stanzarouter")
9 9
10 local st = require "util.stanza";
11 local send = require "core.sessionmanager".send_to_session;
12
10 require "util.jid" 13 require "util.jid"
11 local jid_split = jid.split; 14 local jid_split = jid.split;
12 15
13 function core_process_stanza(origin, stanza) 16 function core_process_stanza(origin, stanza)
14 log("debug", "Received: "..tostring(stanza)) 17 log("debug", "Received: "..tostring(stanza))
18 -- TODO verify validity of stanza
19
15 local to = stanza.attr.to; 20 local to = stanza.attr.to;
21 stanza.attr.from = origin.full_jid -- quick fix to prevent impersonation
16 22
17 if not to or (hosts[to] and hosts[to].type == "local") then 23 if not to or (hosts[to] and hosts[to].type == "local") then
18 core_handle_stanza(origin, stanza); 24 core_handle_stanza(origin, stanza);
19 elseif origin.type == "c2s" then 25 elseif origin.type == "c2s" then
20 core_route_stanza(origin, stanza); 26 core_route_stanza(origin, stanza);
21 end 27 end
22
23 end 28 end
24 29
25 function core_handle_stanza(origin, stanza) 30 function core_handle_stanza(origin, stanza)
26 -- Handlers 31 -- Handlers
27 if origin.type == "c2s" or origin.type == "c2s_unauthed" then 32 if origin.type == "c2s" or origin.type == "c2s_unauthed" then
34 --if not to_host then error("Invalid destination JID: "..string.format("{ %q, %q, %q } == %q", to_node or "", to_host or "", to_resource or "", stanza.attr.to or "nil")); end 39 --if not to_host then error("Invalid destination JID: "..string.format("{ %q, %q, %q } == %q", to_node or "", to_host or "", to_resource or "", stanza.attr.to or "nil")); end
35 40
36 -- Stanza is to this server, or a user on this server 41 -- Stanza is to this server, or a user on this server
37 log("debug", "Routing stanza to local"); 42 log("debug", "Routing stanza to local");
38 handle_stanza(session, stanza); 43 handle_stanza(session, stanza);
39 end 44 end
40 end 45 end
41 46
42 function core_route_stanza(origin, stanza) 47 function core_route_stanza(origin, stanza)
43 -- Hooks 48 -- Hooks
44 --- ...later 49 --- ...later
46 -- Deliver 51 -- Deliver
47 local node, host, resource = jid_split(stanza.attr.to); 52 local node, host, resource = jid_split(stanza.attr.to);
48 local host_session = hosts[host] 53 local host_session = hosts[host]
49 if host_session and host_session.type == "local" then 54 if host_session and host_session.type == "local" then
50 -- Local host 55 -- Local host
56 local user = host_session.sessions[node];
57 if user then
58 local res = nil;
59 if resource then
60 res = user.sessions[resource];
61 end
62 -- TODO do something about presence broadcast
63 if not res then
64 -- if we get here, resource was not specified or was unavailable
65 for k in pairs(user.sessions) do
66 res = user.sessions[k];
67 break;
68 end
69 -- TODO find resource with greatest priority
70 end
71 stanza.attr.to = res.full_jid;
72 send(res, stanza); -- Yay \o/
73 else
74 -- user not found
75 send(origin, st.error_reply(stanza, "cancel", "service-unavailable"));
76 end
51 else 77 else
52 -- Remote host 78 -- Remote host
53 if host_session then 79 if host_session then
54 -- Send to session 80 -- Send to session
55 else 81 else

mercurial