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