Comparison

core/stanza_router.lua @ 209:e9de0803676d

Part one of internal component support
author Matthew Wild <mwild1@gmail.com>
date Tue, 04 Nov 2008 18:15:56 +0000
parent 207:90c387884234
child 210:e679487e2fa4
comparison
equal deleted inserted replaced
208:23245a92b275 209:e9de0803676d
16 16
17 local s2s_verify_dialback = require "core.s2smanager".verify_dialback; 17 local s2s_verify_dialback = require "core.s2smanager".verify_dialback;
18 local s2s_make_authenticated = require "core.s2smanager".make_authenticated; 18 local s2s_make_authenticated = require "core.s2smanager".make_authenticated;
19 19
20 local modules_handle_stanza = require "core.modulemanager".handle_stanza; 20 local modules_handle_stanza = require "core.modulemanager".handle_stanza;
21 local component_handle_stanza = require "core.componentmanager".handle_stanza;
21 22
22 local format = string.format; 23 local format = string.format;
23 local tostring = tostring; 24 local tostring = tostring;
24 local t_concat = table.concat; 25 local t_concat = table.concat;
25 local t_insert = table.insert; 26 local t_insert = table.insert;
29 local jid_split = require "util.jid".split; 30 local jid_split = require "util.jid".split;
30 local print = print; 31 local print = print;
31 32
32 function core_process_stanza(origin, stanza) 33 function core_process_stanza(origin, stanza)
33 log("debug", "Received["..origin.type.."]: "..tostring(stanza)) 34 log("debug", "Received["..origin.type.."]: "..tostring(stanza))
35
34 -- TODO verify validity of stanza (as well as JID validity) 36 -- TODO verify validity of stanza (as well as JID validity)
35 if stanza.name == "iq" and not(#stanza.tags == 1 and stanza.tags[1].attr.xmlns) then 37 if stanza.name == "iq" and not(#stanza.tags == 1 and stanza.tags[1].attr.xmlns) then
36 if stanza.attr.type == "set" or stanza.attr.type == "get" then 38 if stanza.attr.type == "set" or stanza.attr.type == "get" then
37 error("Invalid IQ"); 39 error("Invalid IQ");
38 elseif #stanza.tags > 1 and not(stanza.attr.type == "error" or stanza.attr.type == "result") then 40 elseif #stanza.tags > 1 and not(stanza.attr.type == "error" or stanza.attr.type == "result") then
44 and not(stanza.name == "iq" and stanza.tags[1].name == "bind" 46 and not(stanza.name == "iq" and stanza.tags[1].name == "bind"
45 and stanza.tags[1].attr.xmlns == "urn:ietf:params:xml:ns:xmpp-bind") then 47 and stanza.tags[1].attr.xmlns == "urn:ietf:params:xml:ns:xmpp-bind") then
46 error("Client MUST bind resource after auth"); 48 error("Client MUST bind resource after auth");
47 end 49 end
48 50
49 local to = stanza.attr.to;
50 -- TODO also, stazas should be returned to their original state before the function ends 51 -- TODO also, stazas should be returned to their original state before the function ends
51 if origin.type == "c2s" then 52 if origin.type == "c2s" then
52 stanza.attr.from = origin.full_jid; -- quick fix to prevent impersonation (FIXME this would be incorrect when the origin is not c2s) 53 stanza.attr.from = origin.full_jid;
53 end 54 end
54 55 local to = stanza.attr.to;
56 local node, host, resource = jid_split(to);
57 local to_bare = node and (node.."@"..host) or host; -- bare JID
58 local from = stanza.attr.from;
59 local from_node, from_host, from_resource = jid_split(from);
60 local from_bare = from_node and (from_node.."@"..from_host) or from_host; -- bare JID
61
62 if origin.type == "s2sin" then
63 if origin.from_host ~= from_host then -- remote server trying to impersonate some other server?
64 log("warn", "Received a stanza claiming to be from %s, over a conn authed for %s!", from, origin.from_host);
65 return; -- FIXME what should we do here? does this work with subdomains?
66 end
67 end
68 --[[if to and not(hosts[to]) and not(hosts[to_bare]) and (hosts[host] and hosts[host].type ~= "local") then -- not for us?
69 log("warn", "stanza recieved for a non-local server");
70 return; -- FIXME what should we do here?
71 end]] -- FIXME
72
73 -- FIXME do stanzas not of jabber:client get handled by components?
55 if not to then 74 if not to then
56 core_handle_stanza(origin, stanza); 75 core_handle_stanza(origin, stanza);
76 elseif hosts[to] and hosts[to].type == "local" then -- directed at a local server
77 core_handle_stanza(origin, stanza);
78 elseif hosts[to_bare] and hosts[to_bare].type == "component" then -- hack to allow components to handle node@server
79 component_handle_stanza(origin, stanza);
80 elseif hosts[to] and hosts[to].type == "component" then -- hack to allow components to handle node@server/resource and server/resource
81 component_handle_stanza(origin, stanza);
82 elseif hosts[host].type == "component" then -- directed at a component
83 component_handle_stanza(origin, stanza);
57 elseif origin.type == "c2s" and stanza.name == "presence" and stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then 84 elseif origin.type == "c2s" and stanza.name == "presence" and stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then
58 local node, host = jid_split(stanza.attr.to);
59 local to_bare = node and (node.."@"..host) or host; -- bare JID
60 local from_node, from_host = jid_split(stanza.attr.from);
61 local from_bare = from_node and (from_node.."@"..from_host) or from_host; -- bare JID
62 handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare); 85 handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare);
63 elseif hosts[to] and hosts[to].type == "local" then 86 elseif stanza.name == "iq" and not resource then -- directed at bare JID
64 core_handle_stanza(origin, stanza);
65 elseif stanza.name == "iq" and not select(3, jid_split(to)) then
66 core_handle_stanza(origin, stanza); 87 core_handle_stanza(origin, stanza);
67 elseif stanza.attr.xmlns and stanza.attr.xmlns ~= "jabber:client" and stanza.attr.xmlns ~= "jabber:server" then 88 elseif stanza.attr.xmlns and stanza.attr.xmlns ~= "jabber:client" and stanza.attr.xmlns ~= "jabber:server" then
68 modules_handle_stanza(origin, stanza); 89 modules_handle_stanza(origin, stanza);
69 elseif origin.type == "c2s" or origin.type == "s2sin" then 90 elseif origin.type == "c2s" or origin.type == "s2sin" then
70 core_route_stanza(origin, stanza); 91 core_route_stanza(origin, stanza);
92 else
93 log("warn", "stanza not processed");
71 end 94 end
72 end 95 end
73 96
74 -- This function handles stanzas which are not routed any further, 97 -- This function handles stanzas which are not routed any further,
75 -- that is, they are handled by this server 98 -- that is, they are handled by this server
142 end 165 end
143 stanza.attr.to = nil; -- reset it 166 stanza.attr.to = nil; -- reset it
144 else 167 else
145 -- TODO error, bad type 168 -- TODO error, bad type
146 end 169 end
147 end 170 end -- TODO handle other stanzas
148 else 171 else
149 log("warn", "Unhandled origin: %s", origin.type); 172 log("warn", "Unhandled origin: %s", origin.type); -- FIXME reply with error
150 end 173 end
151 end 174 end
152 175
153 function send_presence_of_available_resources(user, host, jid, recipient_session) 176 function send_presence_of_available_resources(user, host, jid, recipient_session)
154 local h = hosts[host]; 177 local h = hosts[host];