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