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