Merge with 0.4
[prosody.git] / core / stanza_router.lua
1 -- Prosody IM v0.4
2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 Waqas Hussain
4 -- 
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9
10
11 local log = require "util.logger".init("stanzarouter")
12
13 local hosts = _G.hosts;
14
15 local st = require "util.stanza";
16 local send_s2s = require "core.s2smanager".send_to_host;
17 local user_exists = require "core.usermanager".user_exists;
18
19 local rostermanager = require "core.rostermanager";
20 local sessionmanager = require "core.sessionmanager";
21 local offlinemanager = require "core.offlinemanager";
22
23 local s2s_verify_dialback = require "core.s2smanager".verify_dialback;
24 local s2s_make_authenticated = require "core.s2smanager".make_authenticated;
25
26 local modules_handle_stanza = require "core.modulemanager".handle_stanza;
27 local component_handle_stanza = require "core.componentmanager".handle_stanza;
28
29 local handle_outbound_presence_subscriptions_and_probes = function()end;--require "core.presencemanager".handle_outbound_presence_subscriptions_and_probes;
30 local handle_inbound_presence_subscriptions_and_probes = function()end;--require "core.presencemanager".handle_inbound_presence_subscriptions_and_probes;
31 local handle_normal_presence = function()end;--require "core.presencemanager".handle_normal_presence;
32
33 local format = string.format;
34 local tostring = tostring;
35 local t_concat = table.concat;
36 local t_insert = table.insert;
37 local tonumber = tonumber;
38 local s_find = string.find;
39 local pairs = pairs;
40 local ipairs = ipairs;
41
42 local jid_split = require "util.jid".split;
43 local jid_prepped_split = require "util.jid".prepped_split;
44 local print = print;
45 local fire_event = require "core.eventmanager2".fire_event;
46
47 function core_process_stanza(origin, stanza)
48         (origin.log or log)("debug", "Received[%s]: %s", origin.type, stanza:top_tag())
49
50         -- Currently we guarantee every stanza to have an xmlns, should we keep this rule?
51         if not stanza.attr.xmlns then stanza.attr.xmlns = "jabber:client"; end
52         
53         -- TODO verify validity of stanza (as well as JID validity)
54         if stanza.attr.type == "error" and #stanza.tags == 0 then return; end -- TODO invalid stanza, log
55         if stanza.name == "iq" then
56                 if (stanza.attr.type == "set" or stanza.attr.type == "get") and #stanza.tags ~= 1 then
57                         origin.send(st.error_reply(stanza, "modify", "bad-request"));
58                         return;
59                 end
60         end
61
62         if origin.type == "c2s" and not origin.full_jid
63                 and not(stanza.name == "iq" and stanza.tags[1].name == "bind"
64                                 and stanza.tags[1].attr.xmlns == "urn:ietf:params:xml:ns:xmpp-bind") then
65                 error("Client MUST bind resource after auth");
66         end
67
68         -- TODO also, stanzas should be returned to their original state before the function ends
69         if origin.type == "c2s" then
70                 stanza.attr.from = origin.full_jid;
71         end
72         local to, xmlns = stanza.attr.to, stanza.attr.xmlns;
73         local from = stanza.attr.from;
74         local node, host, resource;
75         local from_node, from_host, from_resource;
76         local to_bare, from_bare;
77         if to then
78                 node, host, resource = jid_prepped_split(to);
79                 if not host then
80                         log("warn", "Received stanza with invalid destination JID: %s", to);
81                         origin.send(st.error_reply(stanza, "modify", "jid-malformed", "The destination address is invalid: "..to));
82                         return;
83                 end
84                 to_bare = node and (node.."@"..host) or host; -- bare JID
85                 if resource then to = to_bare.."/"..resource; else to = to_bare; end
86                 stanza.attr.to = to;
87         end
88         if from then
89                 -- We only stamp the 'from' on c2s stanzas, so we still need to check validity
90                 from_node, from_host, from_resource = jid_prepped_split(from);
91                 if not from_host then
92                         log("warn", "Received stanza with invalid source JID: %s", from);
93                         origin.send(st.error_reply(stanza, "modify", "jid-malformed", "The source address is invalid: "..from));
94                         return;
95                 end
96                 from_bare = from_node and (from_node.."@"..from_host) or from_host; -- bare JID
97                 if from_resource then from = from_bare.."/"..from_resource; else from = from_bare; end
98                 stanza.attr.from = from;
99         end
100
101         --[[if to and not(hosts[to]) and not(hosts[to_bare]) and (hosts[host] and hosts[host].type ~= "local") then -- not for us?
102                 log("warn", "stanza recieved for a non-local server");
103                 return; -- FIXME what should we do here?
104         end]] -- FIXME
105
106         if (origin.type == "s2sin" or origin.type == "c2s" or origin.type == "component") and xmlns == "jabber:client" then
107                 if origin.type == "s2sin" and not origin.dummy then
108                         local host_status = origin.hosts[from_host];
109                         if not host_status or not host_status.authed then -- remote server trying to impersonate some other server?
110                                 log("warn", "Received a stanza claiming to be from %s, over a stream authed for %s!", from_host, origin.from_host);
111                                 return; -- FIXME what should we do here? does this work with subdomains?
112                         end
113                 end
114                 core_post_stanza(origin, stanza);
115         else
116                 modules_handle_stanza(host or origin.host or origin.to_host, origin, stanza);
117         end
118 end
119
120 function core_post_stanza(origin, stanza)
121         local to = stanza.attr.to;
122         local node, host, resource = jid_split(to);
123         local to_bare = node and (node.."@"..host) or host; -- bare JID
124
125         local event_data = {origin=origin, stanza=stanza};
126         if host and fire_event(host.."/"..stanza.name, event_data) then
127                 -- event handled
128         elseif stanza.name == "presence" and origin.host and fire_event(origin.host.."/"..stanza.name, event_data) then
129                 -- event handled
130         elseif not to then
131                 modules_handle_stanza(host or origin.host or origin.to_host, origin, stanza);
132         elseif hosts[to] and hosts[to].type == "local" then -- directed at a local server
133                 modules_handle_stanza(host or origin.host or origin.to_host, origin, stanza);
134         elseif hosts[to_bare] and hosts[to_bare].type == "component" then -- hack to allow components to handle node@server
135                 component_handle_stanza(origin, stanza);
136         elseif hosts[host] and hosts[host].type == "component" then -- directed at a component
137                 component_handle_stanza(origin, stanza);
138         elseif hosts[host] and hosts[host].type == "local" and stanza.name == "iq" and not resource then -- directed at bare JID
139                 modules_handle_stanza(host or origin.host or origin.to_host, origin, stanza);
140         else
141                 core_route_stanza(origin, stanza);
142         end
143 end
144
145 function core_route_stanza(origin, stanza)
146         -- Hooks
147         --- ...later
148
149         -- Deliver
150         local to = stanza.attr.to;
151         local node, host, resource = jid_split(to);
152         local to_bare = node and (node.."@"..host) or host; -- bare JID
153         local from = stanza.attr.from;
154         local from_node, from_host, from_resource = jid_split(from);
155         local from_bare = from_node and (from_node.."@"..from_host) or from_host; -- bare JID
156
157         -- Auto-detect origin if not specified
158         origin = origin or hosts[from_host];
159         if not origin then return false; end
160         
161         if hosts[to_bare] and hosts[to_bare].type == "component" then -- hack to allow components to handle node@server
162                 return component_handle_stanza(origin, stanza);
163         elseif hosts[host] and hosts[host].type == "component" then -- directed at a component
164                 return component_handle_stanza(origin, stanza);
165         end
166
167         if stanza.name == "presence" and (stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" and stanza.attr.type ~= "error") then resource = nil; end
168
169         local host_session = hosts[host]
170         if host_session and host_session.type == "local" then
171                 -- Local host
172                 local user = host_session.sessions[node];
173                 if user then
174                         local res = user.sessions[resource];
175                         if res then -- resource is online...
176                                 res.send(stanza); -- Yay \o/
177                         else
178                                 -- if we get here, resource was not specified or was unavailable
179                                 if stanza.name == "presence" then
180                                         if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" and stanza.attr.type ~= "error" then
181                                                 handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);
182                                         elseif not resource then -- sender is available or unavailable or error
183                                                 for _, session in pairs(user.sessions) do -- presence broadcast to all user resources.
184                                                         if session.full_jid then -- FIXME should this be just for available resources? Do we need to check subscription?
185                                                                 stanza.attr.to = session.full_jid; -- reset at the end of function
186                                                                 session.send(stanza);
187                                                         end
188                                                 end
189                                         end
190                                 elseif stanza.name == "message" then -- select a resource to recieve message
191                                         stanza.attr.to = to_bare;
192                                         if stanza.attr.type == 'headline' then
193                                                 for _, session in pairs(user.sessions) do -- find resource with greatest priority
194                                                         if session.presence and session.priority >= 0 then
195                                                                 session.send(stanza);
196                                                         end
197                                                 end
198                                         elseif resource and stanza.attr.type == 'groupchat' then
199                                                 -- Groupchat message sent to offline resource
200                                                 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
201                                         else
202                                                 local priority = 0;
203                                                 local recipients = {};
204                                                 for _, session in pairs(user.sessions) do -- find resource with greatest priority
205                                                         if session.presence then
206                                                                 local p = session.priority;
207                                                                 if p > priority then
208                                                                         priority = p;
209                                                                         recipients = {session};
210                                                                 elseif p == priority then
211                                                                         t_insert(recipients, session);
212                                                                 end
213                                                         end
214                                                 end
215                                                 local count = 0;
216                                                 for _, session in ipairs(recipients) do
217                                                         session.send(stanza);
218                                                         count = count + 1;
219                                                 end
220                                                 if count == 0 and (stanza.attr.type == "chat" or stanza.attr.type == "normal" or not stanza.attr.type) then
221                                                         offlinemanager.store(node, host, stanza);
222                                                         -- TODO deal with storage errors
223                                                 end
224                                         end
225                                 elseif stanza.attr.type == "get" or stanza.attr.type == "set" then
226                                         origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
227                                 end
228                         end
229                 else
230                         -- user not online
231                         if user_exists(node, host) then
232                                 if stanza.name == "presence" then
233                                         if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" and stanza.attr.type ~= "error" then
234                                                 handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);
235                                         else
236                                                 -- TODO send unavailable presence or unsubscribed
237                                         end
238                                 elseif stanza.name == "message" then -- FIXME if full jid, then send out to resources with highest priority
239                                         stanza.attr.to = to_bare; -- TODO not in RFC, but seems obvious. Should discuss on the mailing list.
240                                         if stanza.attr.type == "chat" or stanza.attr.type == "normal" or not stanza.attr.type then
241                                                 offlinemanager.store(node, host, stanza);
242                                                 -- FIXME don't store messages with only chat state notifications
243                                         elseif stanza.attr.type == "groupchat" then
244                                                 local reply = st.error_reply(stanza, "cancel", "service-unavailable");
245                                                 reply.attr.from = to;
246                                                 origin.send(reply);
247                                         end
248                                         -- TODO allow configuration of offline storage
249                                         -- TODO send error if not storing offline
250                                 elseif stanza.name == "iq" and (stanza.attr.type == "get" or stanza.attr.type == "set") then
251                                         origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
252                                 end
253                         else -- user does not exist
254                                 -- TODO we would get here for nodeless JIDs too. Do something fun maybe? Echo service? Let plugins use xmpp:server/resource addresses?
255                                 if stanza.name == "presence" then
256                                         local t = stanza.attr.type;
257                                         if t == "subscribe" or t == "probe" then
258                                                 origin.send(st.presence({from = to_bare, to = from_bare, type = "unsubscribed"}));
259                                         end
260                                         -- else ignore
261                                 elseif stanza.attr.type ~= "error" and (stanza.name ~= "iq" or stanza.attr.type ~= "result") then
262                                         origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
263                                 end
264                         end
265                 end
266         elseif origin.type == "c2s" then
267                 -- Remote host
268                 local xmlns = stanza.attr.xmlns;
269                 --stanza.attr.xmlns = "jabber:server";
270                 stanza.attr.xmlns = nil;
271                 log("debug", "sending s2s stanza: %s", tostring(stanza));
272                 send_s2s(origin.host, host, stanza); -- TODO handle remote routing errors
273                 stanza.attr.xmlns = xmlns; -- reset
274         elseif origin.type == "component" or origin.type == "local" then
275                 -- Route via s2s for components and modules
276                 log("debug", "Routing outgoing stanza for %s to %s", origin.host, host);
277                 send_s2s(origin.host, host, stanza);
278         else
279                 log("warn", "received stanza from unhandled connection type: %s", origin.type);
280         end
281         stanza.attr.to = to; -- reset
282 end