Thu, 23 Oct 2008 22:08:19 +0500
Added TODO
135 | 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, debug = print, debug; | |
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 and from resources | |
69 | if res ~= origin then | |
70 | if res.full_jid then -- to resource. FIXME is this check correct? Maybe it should be res.presence | |
71 | stanza.attr.to = res.full_jid; | |
72 | core_route_stanza(origin, stanza); | |
73 | end | |
74 | if res.presence then -- from all resources for which we have presence | |
75 | res.presence.attr.to = origin.full_jid; | |
76 | core_route_stanza(res, res.presence); | |
77 | res.presence.attr.to = nil; | |
78 | end | |
79 | end | |
80 | end | |
81 | if not origin.presence then -- presence probes on initial presence | |
82 | local probe = st.presence({from = origin.full_jid, type = "probe"}); | |
83 | for jid in pairs(origin.roster) do | |
84 | local subscription = origin.roster[jid].subscription; | |
85 | if subscription == "both" or subscription == "to" then | |
86 | probe.attr.to = jid; | |
87 | core_route_stanza(origin, probe); | |
88 | end | |
89 | end | |
137 | 90 | -- TODO resend subscription requests |
135 | 91 | end |
92 | origin.presence = stanza; | |
93 | stanza.attr.to = nil; -- reset it | |
94 | else | |
95 | -- TODO error, bad type | |
96 | end | |
97 | else | |
98 | log("debug", "Routing stanza to local"); | |
99 | handle_stanza(session, stanza); | |
100 | end | |
101 | end | |
102 | end | |
103 | ||
104 | function is_authorized_to_see_presence(origin, username, host) | |
105 | local roster = datamanager.load(username, host, "roster") or {}; | |
106 | local item = roster[origin.username.."@"..origin.host]; | |
107 | return item and (item.subscription == "both" or item.subscription == "from"); | |
108 | end | |
109 | ||
110 | function core_route_stanza(origin, stanza) | |
111 | -- Hooks | |
112 | --- ...later | |
113 | ||
114 | -- Deliver | |
115 | local to = stanza.attr.to; | |
116 | local node, host, resource = jid_split(to); | |
117 | ||
118 | if stanza.name == "presence" and (stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable") then resource = nil; end | |
119 | ||
120 | local host_session = hosts[host] | |
121 | if host_session and host_session.type == "local" then | |
122 | -- Local host | |
123 | local user = host_session.sessions[node]; | |
124 | if user then | |
125 | local res = user.sessions[resource]; | |
126 | if not res then | |
127 | -- if we get here, resource was not specified or was unavailable | |
128 | if stanza.name == "presence" then | |
129 | if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then | |
130 | if stanza.attr.type == "probe" then | |
131 | if is_authorized_to_see_presence(origin, node, host) then | |
132 | for k in pairs(user.sessions) do -- return presence for all resources | |
133 | if user.sessions[k].presence then | |
134 | local pres = user.sessions[k].presence; | |
135 | pres.attr.to = origin.full_jid; | |
136 | pres.attr.from = user.sessions[k].full_jid; | |
137 | send(origin, pres); | |
138 | pres.attr.to = nil; | |
139 | pres.attr.from = nil; | |
140 | end | |
141 | end | |
142 | else | |
143 | send(origin, st.presence({from=user.."@"..host, to=origin.username.."@"..origin.host, type="unsubscribed"})); | |
144 | end | |
145 | elseif stanza.attr.type == "subscribe" then | |
146 | -- TODO | |
147 | elseif stanza.attr.type == "unsubscribe" then | |
148 | -- TODO | |
149 | elseif stanza.attr.type == "subscribed" then | |
150 | -- TODO | |
151 | elseif stanza.attr.type == "unsubscribed" then | |
152 | -- TODO | |
153 | end -- discard any other type | |
154 | else -- sender is available or unavailable | |
155 | for k in pairs(user.sessions) do -- presence broadcast to all user resources | |
156 | if user.sessions[k].full_jid then | |
157 | stanza.attr.to = user.sessions[k].full_jid; | |
158 | send(user.sessions[k], stanza); | |
159 | end | |
160 | end | |
161 | end | |
162 | elseif stanza.name == "message" then -- select a resource to recieve message | |
163 | for k in pairs(user.sessions) do | |
164 | if user.sessions[k].full_jid then | |
165 | res = user.sessions[k]; | |
166 | break; | |
167 | end | |
168 | end | |
169 | -- TODO find resource with greatest priority | |
170 | send(res, stanza); | |
171 | else | |
172 | -- TODO send IQ error | |
173 | end | |
174 | else | |
175 | -- User + resource is online... | |
176 | stanza.attr.to = res.full_jid; | |
177 | send(res, stanza); -- Yay \o/ | |
178 | end | |
179 | else | |
180 | -- user not online | |
181 | if user_exists(node, host) then | |
182 | if stanza.name == "presence" then | |
183 | if stanza.attr.type == "probe" and is_authorized_to_see_presence(origin, node, host) then -- FIXME what to do for not c2s? | |
184 | -- TODO send last recieved unavailable presence | |
185 | else | |
186 | -- TODO send unavailable presence | |
187 | end | |
188 | elseif stanza.name == "message" then | |
189 | -- TODO send message error, or store offline messages | |
190 | elseif stanza.name == "iq" then | |
191 | -- TODO send IQ error | |
192 | end | |
193 | else -- user does not exist | |
194 | -- TODO we would get here for nodeless JIDs too. Do something fun maybe? Echo service? Let plugins use xmpp:server/resource addresses? | |
195 | if stanza.name == "presence" then | |
196 | if stanza.attr.type == "probe" then | |
197 | send(origin, st.presence({from = user.."@"..host, to = origin.username.."@"..origin.host, type = "unsubscribed"})); | |
198 | end | |
199 | -- else ignore | |
200 | else | |
201 | send(origin, st.error_reply(stanza, "cancel", "service-unavailable")); | |
202 | end | |
203 | end | |
204 | end | |
205 | else | |
206 | -- Remote host | |
207 | if host_session then | |
208 | -- Send to session | |
209 | else | |
210 | -- Need to establish the connection | |
211 | end | |
212 | end | |
213 | stanza.attr.to = to; -- reset | |
214 | end | |
215 | ||
216 | function handle_stanza_toremote(stanza) | |
217 | log("error", "Stanza bound for remote host, but s2s is not implemented"); | |
218 | end |