core/stanza_router.lua

changeset 119
b48a573608e8
parent 113
9026fdad1531
child 121
74e5919e4737
equal deleted inserted replaced
118:76ac96c53ee5 119:b48a573608e8
35 stanza.attr.from = origin.full_jid; -- quick fix to prevent impersonation (FIXME this would be incorrect when the origin is not c2s) 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 36 -- TODO also, stazas should be returned to their original state before the function ends
37 37
38 -- TODO presence subscriptions 38 -- TODO presence subscriptions
39 if not to then 39 if not to then
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);
47 end
48 end
49
50 function core_handle_stanza(origin, stanza)
51 -- Handlers
52 if origin.type == "c2s" or origin.type == "c2s_unauthed" then
53 local session = origin;
54
40 if stanza.name == "presence" and origin.roster then 55 if stanza.name == "presence" and origin.roster then
41 if stanza.attr.type == nil or stanza.attr.type == "available" or stanza.attr.type == "unavailable" then 56 if stanza.attr.type == nil or stanza.attr.type == "available" or stanza.attr.type == "unavailable" then
42 --stanza.attr.from = origin.full_jid;
43 for jid in pairs(origin.roster) do -- broadcast to all interested contacts 57 for jid in pairs(origin.roster) do -- broadcast to all interested contacts
44 local subscription = origin.roster[jid].subscription; 58 local subscription = origin.roster[jid].subscription;
45 if subscription == "both" or subscription == "from" then 59 if subscription == "both" or subscription == "from" then
46 stanza.attr.to = jid; 60 stanza.attr.to = jid;
47 core_route_stanza(origin, stanza); 61 core_route_stanza(origin, stanza);
68 stanza.attr.to = nil; -- reset it 82 stanza.attr.to = nil; -- reset it
69 else 83 else
70 -- TODO error, bad type 84 -- TODO error, bad type
71 end 85 end
72 else 86 else
73 core_handle_stanza(origin, stanza); 87 log("debug", "Routing stanza to local");
74 end 88 handle_stanza(session, stanza);
75 elseif hosts[to] and hosts[to].type == "local" then 89 end
76 core_handle_stanza(origin, stanza);
77 elseif stanza.name == "iq" and not select(3, jid_split(to)) then
78 core_handle_stanza(origin, stanza);
79 elseif origin.type == "c2s" then
80 core_route_stanza(origin, stanza);
81 end
82 end
83
84 function core_handle_stanza(origin, stanza)
85 -- Handlers
86 if origin.type == "c2s" or origin.type == "c2s_unauthed" then
87 local session = origin;
88
89 log("debug", "Routing stanza");
90 -- Stanza has no to attribute
91 --local to_node, to_host, to_resource = jid_split(stanza.attr.to);
92 --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
93
94 -- Stanza is to this server, or a user on this server
95 log("debug", "Routing stanza to local");
96 handle_stanza(session, stanza);
97 end 90 end
98 end 91 end
99 92
100 function is_authorized_to_see_presence(origin, username, host) 93 function is_authorized_to_see_presence(origin, username, host)
101 local roster = datamanager.load(username, host, "roster") or {}; 94 local roster = datamanager.load(username, host, "roster") or {};

mercurial