Removed .hgtags
[prosody.git] / core / stanza_router.lua
1
2 -- 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 -- the rules from the RFCs (mainly 3921)
5
6 require "core.servermanager"
7
8 local log = require "util.logger".init("stanzarouter")
9
10 local st = require "util.stanza";
11 local send = require "core.sessionmanager".send_to_session;
12 local user_exists = require "core.usermanager".user_exists;
13
14 local jid_split = require "util.jid".split;
15 local print, debug = print, debug;
16
17 function core_process_stanza(origin, stanza)
18         log("debug", "Received: "..tostring(stanza))
19         -- TODO verify validity of stanza (as well as JID validity)
20         if stanza.name == "iq" and not(#stanza.tags == 1 and stanza.tags[1].attr.xmlns) then
21                 if stanza.attr.type == "set" or stanza.attr.type == "get" then
22                         error("Invalid IQ");
23                 elseif #stanza.tags > 1 or not(stanza.attr.type == "error" or stanza.attr.type == "result") then
24                         error("Invalid IQ");
25                 end
26         end
27
28         if origin.type == "c2s" and not origin.full_jid
29                 and not(stanza.name == "iq" and stanza.tags[1].name == "bind"
30                                 and stanza.tags[1].attr.xmlns == "urn:ietf:params:xml:ns:xmpp-bind") then
31                 error("Client MUST bind resource after auth");
32         end
33
34         local to = stanza.attr.to;
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
37         
38         -- TODO presence subscriptions
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 -- This function handles stanzas which are not routed any further,
51 -- that is, they are handled by this server
52 function core_handle_stanza(origin, stanza)
53         -- Handlers
54         if origin.type == "c2s" or origin.type == "c2s_unauthed" then
55                 local session = origin;
56                 
57                 if stanza.name == "presence" and origin.roster then
58                         if stanza.attr.type == nil or stanza.attr.type == "available" or stanza.attr.type == "unavailable" then
59                                 for jid in pairs(origin.roster) do -- broadcast to all interested contacts
60                                         local subscription = origin.roster[jid].subscription;
61                                         if subscription == "both" or subscription == "from" then
62                                                 stanza.attr.to = jid;
63                                                 core_route_stanza(origin, stanza);
64                                         end
65                                 end
66                                 local node, host = jid_split(stanza.attr.from);
67                                 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast to all resources and from resources
68                                         if res ~= origin then
69                                                 if res.full_jid then -- to resource. FIXME is this check correct? Maybe it should be res.presence
70                                                         stanza.attr.to = res.full_jid;
71                                                         core_route_stanza(origin, stanza);
72                                                 end
73                                                 if res.presence then -- from all resources for which we have presence
74                                                         res.presence.attr.to = origin.full_jid;
75                                                         core_route_stanza(res, res.presence);
76                                                         res.presence.attr.to = nil;
77                                                 end
78                                         end
79                                 end
80                                 if not origin.presence then -- presence probes on initial presence
81                                         local probe = st.presence({from = origin.full_jid, type = "probe"});
82                                         for jid in pairs(origin.roster) do
83                                                 local subscription = origin.roster[jid].subscription;
84                                                 if subscription == "both" or subscription == "to" then
85                                                         probe.attr.to = jid;
86                                                         core_route_stanza(origin, probe);
87                                                 end
88                                         end
89                                 end
90                                 origin.presence = stanza;
91                                 stanza.attr.to = nil; -- reset it
92                         else
93                                 -- TODO error, bad type
94                         end
95                 else
96                         log("debug", "Routing stanza to local");
97                         handle_stanza(session, stanza);
98                 end
99         end
100 end
101
102 function is_authorized_to_see_presence(origin, username, host)
103         local roster = datamanager.load(username, host, "roster") or {};
104         local item = roster[origin.username.."@"..origin.host];
105         return item and (item.subscription == "both" or item.subscription == "from");
106 end
107
108 function core_route_stanza(origin, stanza)
109         -- Hooks
110         --- ...later
111         
112         -- Deliver
113         local to = stanza.attr.to;
114         local node, host, resource = jid_split(to);
115
116         if stanza.name == "presence" and (stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable") then resource = nil; end
117
118         local host_session = hosts[host]
119         if host_session and host_session.type == "local" then
120                 -- Local host
121                 local user = host_session.sessions[node];
122                 if user then
123                         local res = user.sessions[resource];
124                         if not res then
125                                 -- if we get here, resource was not specified or was unavailable
126                                 if stanza.name == "presence" then
127                                         if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then
128                                                 if stanza.attr.type == "probe" then
129                                                         if is_authorized_to_see_presence(origin, node, host) then
130                                                                 for k in pairs(user.sessions) do -- return presence for all resources
131                                                                         if user.sessions[k].presence then
132                                                                                 local pres = user.sessions[k].presence;
133                                                                                 pres.attr.to = origin.full_jid;
134                                                                                 pres.attr.from = user.sessions[k].full_jid;
135                                                                                 send(origin, pres);
136                                                                                 pres.attr.to = nil;
137                                                                                 pres.attr.from = nil;
138                                                                         end
139                                                                 end
140                                                         else
141                                                                 send(origin, st.presence({from=user.."@"..host, to=origin.username.."@"..origin.host, type="unsubscribed"}));
142                                                         end
143                                                 elseif stanza.attr.type == "subscribe" then
144                                                         -- TODO
145                                                 elseif stanza.attr.type == "unsubscribe" then
146                                                         -- TODO
147                                                 elseif stanza.attr.type == "subscribed" then
148                                                         -- TODO
149                                                 elseif stanza.attr.type == "unsubscribed" then
150                                                         -- TODO
151                                                 end -- discard any other type
152                                         else -- sender is available or unavailable
153                                                 for k in pairs(user.sessions) do -- presence broadcast to all user resources
154                                                         if user.sessions[k].full_jid then
155                                                                 stanza.attr.to = user.sessions[k].full_jid;
156                                                                 send(user.sessions[k], stanza);
157                                                         end
158                                                 end
159                                         end
160                                 elseif stanza.name == "message" then -- select a resource to recieve message
161                                         for k in pairs(user.sessions) do
162                                                 if user.sessions[k].full_jid then
163                                                         res = user.sessions[k];
164                                                         break;
165                                                 end
166                                         end
167                                         -- TODO find resource with greatest priority
168                                         send(res, stanza);
169                                 else
170                                         -- TODO send IQ error
171                                 end
172                         else
173                                 -- User + resource is online...
174                                 stanza.attr.to = res.full_jid;
175                                 send(res, stanza); -- Yay \o/
176                         end
177                 else
178                         -- user not online
179                         if user_exists(node, host) then
180                                 if stanza.name == "presence" then
181                                         if stanza.attr.type == "probe" and is_authorized_to_see_presence(origin, node, host) then -- FIXME what to do for not c2s?
182                                                 -- TODO send last recieved unavailable presence
183                                         else
184                                                 -- TODO send unavailable presence
185                                         end
186                                 elseif stanza.name == "message" then
187                                         -- TODO send message error, or store offline messages
188                                 elseif stanza.name == "iq" then
189                                         -- TODO send IQ error
190                                 end
191                         else -- user does not exist
192                                 -- TODO we would get here for nodeless JIDs too. Do something fun maybe? Echo service? Let plugins use xmpp:server/resource addresses?
193                                 if stanza.name == "presence" then
194                                         if stanza.attr.type == "probe" then
195                                                 send(origin, st.presence({from = user.."@"..host, to = origin.username.."@"..origin.host, type = "unsubscribed"}));
196                                         end
197                                         -- else ignore
198                                 else
199                                         send(origin, st.error_reply(stanza, "cancel", "service-unavailable"));
200                                 end
201                         end
202                 end
203         else
204                 -- Remote host
205                 if host_session then
206                         -- Send to session
207                 else
208                         -- Need to establish the connection
209                 end
210         end
211         stanza.attr.to = to; -- reset
212 end
213
214 function handle_stanza_toremote(stanza)
215         log("error", "Stanza bound for remote host, but s2s is not implemented");
216 end