Mercurial > prosody-hg
annotate core/stanza_router.lua @ 138:cb0dd442ca63
Making the best attempt out of a bad merge from waqas
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Thu, 23 Oct 2008 18:24:55 +0100 |
| parents | 9a05dfb35dd5 93f3c6b94c75 |
| children | 8f17ba74823c |
| rev | line source |
|---|---|
|
138
cb0dd442ca63
Making the best attempt out of a bad merge from waqas
Matthew Wild <mwild1@gmail.com>
diff
changeset
|
1 <<<<<<< local |
| 135 | 2 |
| 3 -- The code in this file should be self-explanatory, though the logic is horrible | |
| 4 -- for more info on that, see doc/stanza_routing.txt, which attempts to condense | |
| 5 -- the rules from the RFCs (mainly 3921) | |
| 6 | |
| 7 require "core.servermanager" | |
| 8 | |
| 9 local log = require "util.logger".init("stanzarouter") | |
| 10 | |
| 11 local st = require "util.stanza"; | |
| 12 local send = require "core.sessionmanager".send_to_session; | |
| 13 -- local send_s2s = require "core.s2smanager".send_to_host; | |
| 14 local user_exists = require "core.usermanager".user_exists; | |
| 15 | |
| 16 local jid_split = require "util.jid".split; | |
| 17 local print, debug = print, debug; | |
| 18 | |
| 19 function core_process_stanza(origin, stanza) | |
| 20 log("debug", "Received: "..tostring(stanza)) | |
| 21 -- TODO verify validity of stanza (as well as JID validity) | |
| 22 if stanza.name == "iq" and not(#stanza.tags == 1 and stanza.tags[1].attr.xmlns) then | |
| 23 if stanza.attr.type == "set" or stanza.attr.type == "get" then | |
| 24 error("Invalid IQ"); | |
| 25 elseif #stanza.tags > 1 or not(stanza.attr.type == "error" or stanza.attr.type == "result") then | |
| 26 error("Invalid IQ"); | |
| 27 end | |
| 28 end | |
| 29 | |
| 30 if origin.type == "c2s" and not origin.full_jid | |
| 31 and not(stanza.name == "iq" and stanza.tags[1].name == "bind" | |
| 32 and stanza.tags[1].attr.xmlns == "urn:ietf:params:xml:ns:xmpp-bind") then | |
| 33 error("Client MUST bind resource after auth"); | |
| 34 end | |
| 35 | |
| 36 local to = stanza.attr.to; | |
| 37 stanza.attr.from = origin.full_jid; -- quick fix to prevent impersonation (FIXME this would be incorrect when the origin is not c2s) | |
| 38 -- TODO also, stazas should be returned to their original state before the function ends | |
| 39 | |
| 40 -- TODO presence subscriptions | |
| 41 if not to then | |
| 42 core_handle_stanza(origin, stanza); | |
| 43 elseif hosts[to] and hosts[to].type == "local" then | |
| 44 core_handle_stanza(origin, stanza); | |
| 45 elseif stanza.name == "iq" and not select(3, jid_split(to)) then | |
| 46 core_handle_stanza(origin, stanza); | |
| 47 elseif origin.type == "c2s" then | |
| 48 core_route_stanza(origin, stanza); | |
| 49 end | |
| 50 end | |
| 51 | |
| 52 -- This function handles stanzas which are not routed any further, | |
| 53 -- that is, they are handled by this server | |
| 54 function core_handle_stanza(origin, stanza) | |
| 55 -- Handlers | |
| 56 if origin.type == "c2s" or origin.type == "c2s_unauthed" then | |
| 57 local session = origin; | |
| 58 | |
| 59 if stanza.name == "presence" and origin.roster then | |
| 60 if stanza.attr.type == nil or stanza.attr.type == "available" or stanza.attr.type == "unavailable" then | |
| 61 for jid in pairs(origin.roster) do -- broadcast to all interested contacts | |
| 62 local subscription = origin.roster[jid].subscription; | |
| 63 if subscription == "both" or subscription == "from" then | |
| 64 stanza.attr.to = jid; | |
| 65 core_route_stanza(origin, stanza); | |
| 66 end | |
| 67 end | |
| 68 local node, host = jid_split(stanza.attr.from); | |
| 69 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast to all resources and from resources | |
| 70 if res ~= origin then | |
| 71 if res.full_jid then -- to resource. FIXME is this check correct? Maybe it should be res.presence | |
| 72 stanza.attr.to = res.full_jid; | |
| 73 core_route_stanza(origin, stanza); | |
| 74 end | |
| 75 if res.presence then -- from all resources for which we have presence | |
| 76 res.presence.attr.to = origin.full_jid; | |
| 77 core_route_stanza(res, res.presence); | |
| 78 res.presence.attr.to = nil; | |
| 79 end | |
| 80 end | |
| 81 end | |
| 82 if not origin.presence then -- presence probes on initial presence | |
| 83 local probe = st.presence({from = origin.full_jid, type = "probe"}); | |
| 84 for jid in pairs(origin.roster) do | |
| 85 local subscription = origin.roster[jid].subscription; | |
| 86 if subscription == "both" or subscription == "to" then | |
| 87 probe.attr.to = jid; | |
| 88 core_route_stanza(origin, probe); | |
| 89 end | |
| 90 end | |
| 137 | 91 -- TODO resend subscription requests |
| 135 | 92 end |
| 93 origin.presence = stanza; | |
| 94 stanza.attr.to = nil; -- reset it | |
| 95 else | |
| 96 -- TODO error, bad type | |
| 97 end | |
| 98 else | |
| 99 log("debug", "Routing stanza to local"); | |
| 100 handle_stanza(session, stanza); | |
| 101 end | |
| 102 end | |
| 103 end | |
| 104 | |
| 105 function is_authorized_to_see_presence(origin, username, host) | |
| 106 local roster = datamanager.load(username, host, "roster") or {}; | |
| 107 local item = roster[origin.username.."@"..origin.host]; | |
| 108 return item and (item.subscription == "both" or item.subscription == "from"); | |
| 109 end | |
| 110 | |
| 111 function core_route_stanza(origin, stanza) | |
| 112 -- Hooks | |
| 113 --- ...later | |
| 114 | |
| 115 -- Deliver | |
| 116 local to = stanza.attr.to; | |
| 117 local node, host, resource = jid_split(to); | |
| 118 | |
| 119 if stanza.name == "presence" and (stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable") then resource = nil; end | |
| 120 | |
| 121 local host_session = hosts[host] | |
| 122 if host_session and host_session.type == "local" then | |
| 123 -- Local host | |
| 124 local user = host_session.sessions[node]; | |
| 125 if user then | |
| 126 local res = user.sessions[resource]; | |
| 127 if not res then | |
| 128 -- if we get here, resource was not specified or was unavailable | |
| 129 if stanza.name == "presence" then | |
| 130 if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then | |
| 131 if stanza.attr.type == "probe" then | |
| 132 if is_authorized_to_see_presence(origin, node, host) then | |
| 133 for k in pairs(user.sessions) do -- return presence for all resources | |
| 134 if user.sessions[k].presence then | |
| 135 local pres = user.sessions[k].presence; | |
| 136 pres.attr.to = origin.full_jid; | |
| 137 pres.attr.from = user.sessions[k].full_jid; | |
| 138 send(origin, pres); | |
| 139 pres.attr.to = nil; | |
| 140 pres.attr.from = nil; | |
| 141 end | |
| 142 end | |
| 143 else | |
| 144 send(origin, st.presence({from=user.."@"..host, to=origin.username.."@"..origin.host, type="unsubscribed"})); | |
| 145 end | |
| 146 elseif stanza.attr.type == "subscribe" then | |
| 147 -- TODO | |
| 148 elseif stanza.attr.type == "unsubscribe" then | |
| 149 -- TODO | |
| 150 elseif stanza.attr.type == "subscribed" then | |
| 151 -- TODO | |
| 152 elseif stanza.attr.type == "unsubscribed" then | |
| 153 -- TODO | |
| 154 end -- discard any other type | |
| 155 else -- sender is available or unavailable | |
| 156 for k in pairs(user.sessions) do -- presence broadcast to all user resources | |
| 157 if user.sessions[k].full_jid then | |
| 158 stanza.attr.to = user.sessions[k].full_jid; | |
| 159 send(user.sessions[k], stanza); | |
| 160 end | |
| 161 end | |
| 162 end | |
| 163 elseif stanza.name == "message" then -- select a resource to recieve message | |
| 164 for k in pairs(user.sessions) do | |
| 165 if user.sessions[k].full_jid then | |
| 166 res = user.sessions[k]; | |
| 167 break; | |
| 168 end | |
| 169 end | |
| 170 -- TODO find resource with greatest priority | |
| 171 send(res, stanza); | |
| 172 else | |
| 173 -- TODO send IQ error | |
| 174 end | |
| 175 else | |
| 176 -- User + resource is online... | |
| 177 stanza.attr.to = res.full_jid; | |
| 178 send(res, stanza); -- Yay \o/ | |
| 179 end | |
| 180 else | |
| 181 -- user not online | |
| 182 if user_exists(node, host) then | |
| 183 if stanza.name == "presence" then | |
| 184 if stanza.attr.type == "probe" and is_authorized_to_see_presence(origin, node, host) then -- FIXME what to do for not c2s? | |
| 185 -- TODO send last recieved unavailable presence | |
| 186 else | |
| 187 -- TODO send unavailable presence | |
| 188 end | |
| 189 elseif stanza.name == "message" then | |
| 190 -- TODO send message error, or store offline messages | |
| 191 elseif stanza.name == "iq" then | |
| 192 -- TODO send IQ error | |
| 193 end | |
| 194 else -- user does not exist | |
| 195 -- TODO we would get here for nodeless JIDs too. Do something fun maybe? Echo service? Let plugins use xmpp:server/resource addresses? | |
| 196 if stanza.name == "presence" then | |
| 197 if stanza.attr.type == "probe" then | |
| 198 send(origin, st.presence({from = user.."@"..host, to = origin.username.."@"..origin.host, type = "unsubscribed"})); | |
| 199 end | |
| 200 -- else ignore | |
| 201 else | |
| 202 send(origin, st.error_reply(stanza, "cancel", "service-unavailable")); | |
| 203 end | |
| 204 end | |
| 205 end | |
| 206 else | |
| 207 -- Remote host | |
| 208 if host_session then | |
| 209 -- Send to session | |
| 210 else | |
| 211 -- Need to establish the connection | |
| 212 end | |
| 213 end | |
| 214 stanza.attr.to = to; -- reset | |
| 215 end | |
| 216 | |
| 217 function handle_stanza_toremote(stanza) | |
| 218 log("error", "Stanza bound for remote host, but s2s is not implemented"); | |
| 219 end | |
|
138
cb0dd442ca63
Making the best attempt out of a bad merge from waqas
Matthew Wild <mwild1@gmail.com>
diff
changeset
|
220 ======= |
| 30 | 221 |
| 222 -- The code in this file should be self-explanatory, though the logic is horrible | |
| 223 -- for more info on that, see doc/stanza_routing.txt, which attempts to condense | |
| 224 -- the rules from the RFCs (mainly 3921) | |
| 225 | |
| 226 require "core.servermanager" | |
| 227 | |
| 228 local log = require "util.logger".init("stanzarouter") | |
| 229 | |
|
71
fbb4ef1da82e
Added: Local stanza routing
Waqas Hussain <waqas20@gmail.com>
parents:
68
diff
changeset
|
230 local st = require "util.stanza"; |
|
fbb4ef1da82e
Added: Local stanza routing
Waqas Hussain <waqas20@gmail.com>
parents:
68
diff
changeset
|
231 local send = require "core.sessionmanager".send_to_session; |
| 122 | 232 -- local send_s2s = require "core.s2smanager".send_to_host; |
|
113
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
233 local user_exists = require "core.usermanager".user_exists; |
|
71
fbb4ef1da82e
Added: Local stanza routing
Waqas Hussain <waqas20@gmail.com>
parents:
68
diff
changeset
|
234 |
|
105
b099f0f80775
Removed an unnecessary line
Waqas Hussain <waqas20@gmail.com>
parents:
83
diff
changeset
|
235 local jid_split = require "util.jid".split; |
|
113
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
236 local print = print; |
| 30 | 237 |
| 238 function core_process_stanza(origin, stanza) | |
| 66 | 239 log("debug", "Received: "..tostring(stanza)) |
| 73 | 240 -- TODO verify validity of stanza (as well as JID validity) |
|
83
79608fc8f98d
Fixed routing for IQs to bare JIDs, and added a simple IQ validity check
Waqas Hussain <waqas20@gmail.com>
parents:
78
diff
changeset
|
241 if stanza.name == "iq" and not(#stanza.tags == 1 and stanza.tags[1].attr.xmlns) then |
|
113
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
242 if stanza.attr.type == "set" or stanza.attr.type == "get" then |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
243 error("Invalid IQ"); |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
244 elseif #stanza.tags > 1 or not(stanza.attr.type == "error" or stanza.attr.type == "result") then |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
245 error("Invalid IQ"); |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
246 end |
|
83
79608fc8f98d
Fixed routing for IQs to bare JIDs, and added a simple IQ validity check
Waqas Hussain <waqas20@gmail.com>
parents:
78
diff
changeset
|
247 end |
|
78
972e31cc91e8
Fized: Added check to ensure that resource binding is done after auth.
Waqas Hussain <waqas20@gmail.com>
parents:
73
diff
changeset
|
248 |
|
972e31cc91e8
Fized: Added check to ensure that resource binding is done after auth.
Waqas Hussain <waqas20@gmail.com>
parents:
73
diff
changeset
|
249 if origin.type == "c2s" and not origin.full_jid |
|
83
79608fc8f98d
Fixed routing for IQs to bare JIDs, and added a simple IQ validity check
Waqas Hussain <waqas20@gmail.com>
parents:
78
diff
changeset
|
250 and not(stanza.name == "iq" and stanza.tags[1].name == "bind" |
|
78
972e31cc91e8
Fized: Added check to ensure that resource binding is done after auth.
Waqas Hussain <waqas20@gmail.com>
parents:
73
diff
changeset
|
251 and stanza.tags[1].attr.xmlns == "urn:ietf:params:xml:ns:xmpp-bind") then |
|
972e31cc91e8
Fized: Added check to ensure that resource binding is done after auth.
Waqas Hussain <waqas20@gmail.com>
parents:
73
diff
changeset
|
252 error("Client MUST bind resource after auth"); |
|
972e31cc91e8
Fized: Added check to ensure that resource binding is done after auth.
Waqas Hussain <waqas20@gmail.com>
parents:
73
diff
changeset
|
253 end |
|
972e31cc91e8
Fized: Added check to ensure that resource binding is done after auth.
Waqas Hussain <waqas20@gmail.com>
parents:
73
diff
changeset
|
254 |
| 30 | 255 local to = stanza.attr.to; |
|
113
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
256 stanza.attr.from = origin.full_jid; -- quick fix to prevent impersonation (FIXME this would be incorrect when the origin is not c2s) |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
257 -- TODO also, stazas should be returned to their original state before the function ends |
| 30 | 258 |
|
113
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
259 -- TODO presence subscriptions |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
260 if not to then |
|
119
b48a573608e8
Relocate presence broadcast to core_handle_stanza()
Matthew Wild <mwild1@gmail.com>
parents:
113
diff
changeset
|
261 core_handle_stanza(origin, stanza); |
|
b48a573608e8
Relocate presence broadcast to core_handle_stanza()
Matthew Wild <mwild1@gmail.com>
parents:
113
diff
changeset
|
262 elseif hosts[to] and hosts[to].type == "local" then |
|
b48a573608e8
Relocate presence broadcast to core_handle_stanza()
Matthew Wild <mwild1@gmail.com>
parents:
113
diff
changeset
|
263 core_handle_stanza(origin, stanza); |
|
b48a573608e8
Relocate presence broadcast to core_handle_stanza()
Matthew Wild <mwild1@gmail.com>
parents:
113
diff
changeset
|
264 elseif stanza.name == "iq" and not select(3, jid_split(to)) then |
|
b48a573608e8
Relocate presence broadcast to core_handle_stanza()
Matthew Wild <mwild1@gmail.com>
parents:
113
diff
changeset
|
265 core_handle_stanza(origin, stanza); |
|
b48a573608e8
Relocate presence broadcast to core_handle_stanza()
Matthew Wild <mwild1@gmail.com>
parents:
113
diff
changeset
|
266 elseif origin.type == "c2s" then |
|
b48a573608e8
Relocate presence broadcast to core_handle_stanza()
Matthew Wild <mwild1@gmail.com>
parents:
113
diff
changeset
|
267 core_route_stanza(origin, stanza); |
|
b48a573608e8
Relocate presence broadcast to core_handle_stanza()
Matthew Wild <mwild1@gmail.com>
parents:
113
diff
changeset
|
268 end |
|
b48a573608e8
Relocate presence broadcast to core_handle_stanza()
Matthew Wild <mwild1@gmail.com>
parents:
113
diff
changeset
|
269 end |
|
b48a573608e8
Relocate presence broadcast to core_handle_stanza()
Matthew Wild <mwild1@gmail.com>
parents:
113
diff
changeset
|
270 |
|
121
74e5919e4737
Added a comment, removed all the old code
Matthew Wild <mwild1@gmail.com>
parents:
119
diff
changeset
|
271 -- This function handles stanzas which are not routed any further, |
|
74e5919e4737
Added a comment, removed all the old code
Matthew Wild <mwild1@gmail.com>
parents:
119
diff
changeset
|
272 -- that is, they are handled by this server |
|
119
b48a573608e8
Relocate presence broadcast to core_handle_stanza()
Matthew Wild <mwild1@gmail.com>
parents:
113
diff
changeset
|
273 function core_handle_stanza(origin, stanza) |
|
b48a573608e8
Relocate presence broadcast to core_handle_stanza()
Matthew Wild <mwild1@gmail.com>
parents:
113
diff
changeset
|
274 -- Handlers |
|
b48a573608e8
Relocate presence broadcast to core_handle_stanza()
Matthew Wild <mwild1@gmail.com>
parents:
113
diff
changeset
|
275 if origin.type == "c2s" or origin.type == "c2s_unauthed" then |
|
b48a573608e8
Relocate presence broadcast to core_handle_stanza()
Matthew Wild <mwild1@gmail.com>
parents:
113
diff
changeset
|
276 local session = origin; |
|
b48a573608e8
Relocate presence broadcast to core_handle_stanza()
Matthew Wild <mwild1@gmail.com>
parents:
113
diff
changeset
|
277 |
|
113
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
278 if stanza.name == "presence" and origin.roster then |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
279 if stanza.attr.type == nil or stanza.attr.type == "available" or stanza.attr.type == "unavailable" then |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
280 for jid in pairs(origin.roster) do -- broadcast to all interested contacts |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
281 local subscription = origin.roster[jid].subscription; |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
282 if subscription == "both" or subscription == "from" then |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
283 stanza.attr.to = jid; |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
284 core_route_stanza(origin, stanza); |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
285 end |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
286 end |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
287 --[[local node, host = jid_split(stanza.attr.from); |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
288 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast to all resources |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
289 if res.full_jid then |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
290 res = user.sessions[k]; |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
291 break; |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
292 end |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
293 end]] |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
294 if not origin.presence then -- presence probes on initial presence |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
295 local probe = st.presence({from = origin.full_jid, type = "probe"}); |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
296 for jid in pairs(origin.roster) do |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
297 local subscription = origin.roster[jid].subscription; |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
298 if subscription == "both" or subscription == "to" then |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
299 probe.attr.to = jid; |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
300 core_route_stanza(origin, probe); |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
301 end |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
302 end |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
303 end |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
304 origin.presence = stanza; |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
305 stanza.attr.to = nil; -- reset it |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
306 else |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
307 -- TODO error, bad type |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
308 end |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
309 else |
|
119
b48a573608e8
Relocate presence broadcast to core_handle_stanza()
Matthew Wild <mwild1@gmail.com>
parents:
113
diff
changeset
|
310 log("debug", "Routing stanza to local"); |
|
b48a573608e8
Relocate presence broadcast to core_handle_stanza()
Matthew Wild <mwild1@gmail.com>
parents:
113
diff
changeset
|
311 handle_stanza(session, stanza); |
|
113
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
312 end |
|
71
fbb4ef1da82e
Added: Local stanza routing
Waqas Hussain <waqas20@gmail.com>
parents:
68
diff
changeset
|
313 end |
| 30 | 314 end |
| 315 | |
| 127 | 316 -- TODO: Does this function belong here? |
|
113
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
317 function is_authorized_to_see_presence(origin, username, host) |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
318 local roster = datamanager.load(username, host, "roster") or {}; |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
319 local item = roster[origin.username.."@"..origin.host]; |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
320 return item and (item.subscription == "both" or item.subscription == "from"); |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
321 end |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
322 |
| 30 | 323 function core_route_stanza(origin, stanza) |
| 324 -- Hooks | |
|
68
ceb7a55676a4
Beginnings of real stanza routing
Matthew Wild <mwild1@gmail.com>
parents:
66
diff
changeset
|
325 --- ...later |
|
ceb7a55676a4
Beginnings of real stanza routing
Matthew Wild <mwild1@gmail.com>
parents:
66
diff
changeset
|
326 |
| 30 | 327 -- Deliver |
|
113
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
328 local to = stanza.attr.to; |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
329 local node, host, resource = jid_split(to); |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
330 |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
331 if stanza.name == "presence" and stanza.attr.type == "probe" then resource = nil; end |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
332 |
|
68
ceb7a55676a4
Beginnings of real stanza routing
Matthew Wild <mwild1@gmail.com>
parents:
66
diff
changeset
|
333 local host_session = hosts[host] |
|
ceb7a55676a4
Beginnings of real stanza routing
Matthew Wild <mwild1@gmail.com>
parents:
66
diff
changeset
|
334 if host_session and host_session.type == "local" then |
|
ceb7a55676a4
Beginnings of real stanza routing
Matthew Wild <mwild1@gmail.com>
parents:
66
diff
changeset
|
335 -- Local host |
|
71
fbb4ef1da82e
Added: Local stanza routing
Waqas Hussain <waqas20@gmail.com>
parents:
68
diff
changeset
|
336 local user = host_session.sessions[node]; |
|
fbb4ef1da82e
Added: Local stanza routing
Waqas Hussain <waqas20@gmail.com>
parents:
68
diff
changeset
|
337 if user then |
| 72 | 338 local res = user.sessions[resource]; |
|
71
fbb4ef1da82e
Added: Local stanza routing
Waqas Hussain <waqas20@gmail.com>
parents:
68
diff
changeset
|
339 if not res then |
|
fbb4ef1da82e
Added: Local stanza routing
Waqas Hussain <waqas20@gmail.com>
parents:
68
diff
changeset
|
340 -- if we get here, resource was not specified or was unavailable |
|
106
f2a3d204a76a
Added: presence broadcast
Waqas Hussain <waqas20@gmail.com>
parents:
105
diff
changeset
|
341 if stanza.name == "presence" then |
|
113
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
342 if stanza.attr.type == "probe" then |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
343 if is_authorized_to_see_presence(origin, node, host) then |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
344 for k in pairs(user.sessions) do -- return presence for all resources |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
345 if user.sessions[k].presence then |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
346 local pres = user.sessions[k].presence; |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
347 pres.attr.to = origin.full_jid; |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
348 pres.attr.from = user.sessions[k].full_jid; |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
349 send(origin, pres); |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
350 pres.attr.to = nil; |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
351 pres.attr.from = nil; |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
352 end |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
353 end |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
354 else |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
355 send(origin, st.presence({from = user.."@"..host, to = origin.username.."@"..origin.host, type = "unsubscribed"})); |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
356 end |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
357 else |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
358 for k in pairs(user.sessions) do -- presence broadcast to all user resources |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
359 if user.sessions[k].full_jid then |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
360 stanza.attr.to = user.sessions[k].full_jid; |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
361 send(user.sessions[k], stanza); |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
362 end |
|
106
f2a3d204a76a
Added: presence broadcast
Waqas Hussain <waqas20@gmail.com>
parents:
105
diff
changeset
|
363 end |
|
f2a3d204a76a
Added: presence broadcast
Waqas Hussain <waqas20@gmail.com>
parents:
105
diff
changeset
|
364 end |
|
113
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
365 elseif stanza.name == "message" then -- select a resource to recieve message |
|
106
f2a3d204a76a
Added: presence broadcast
Waqas Hussain <waqas20@gmail.com>
parents:
105
diff
changeset
|
366 for k in pairs(user.sessions) do |
|
f2a3d204a76a
Added: presence broadcast
Waqas Hussain <waqas20@gmail.com>
parents:
105
diff
changeset
|
367 if user.sessions[k].full_jid then |
|
f2a3d204a76a
Added: presence broadcast
Waqas Hussain <waqas20@gmail.com>
parents:
105
diff
changeset
|
368 res = user.sessions[k]; |
|
f2a3d204a76a
Added: presence broadcast
Waqas Hussain <waqas20@gmail.com>
parents:
105
diff
changeset
|
369 break; |
|
f2a3d204a76a
Added: presence broadcast
Waqas Hussain <waqas20@gmail.com>
parents:
105
diff
changeset
|
370 end |
|
f2a3d204a76a
Added: presence broadcast
Waqas Hussain <waqas20@gmail.com>
parents:
105
diff
changeset
|
371 end |
|
f2a3d204a76a
Added: presence broadcast
Waqas Hussain <waqas20@gmail.com>
parents:
105
diff
changeset
|
372 -- TODO find resource with greatest priority |
|
113
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
373 send(res, stanza); |
|
106
f2a3d204a76a
Added: presence broadcast
Waqas Hussain <waqas20@gmail.com>
parents:
105
diff
changeset
|
374 else |
|
113
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
375 -- TODO send IQ error |
|
71
fbb4ef1da82e
Added: Local stanza routing
Waqas Hussain <waqas20@gmail.com>
parents:
68
diff
changeset
|
376 end |
|
113
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
377 else |
|
121
74e5919e4737
Added a comment, removed all the old code
Matthew Wild <mwild1@gmail.com>
parents:
119
diff
changeset
|
378 -- User + resource is online... |
|
106
f2a3d204a76a
Added: presence broadcast
Waqas Hussain <waqas20@gmail.com>
parents:
105
diff
changeset
|
379 stanza.attr.to = res.full_jid; |
|
f2a3d204a76a
Added: presence broadcast
Waqas Hussain <waqas20@gmail.com>
parents:
105
diff
changeset
|
380 send(res, stanza); -- Yay \o/ |
|
f2a3d204a76a
Added: presence broadcast
Waqas Hussain <waqas20@gmail.com>
parents:
105
diff
changeset
|
381 end |
|
71
fbb4ef1da82e
Added: Local stanza routing
Waqas Hussain <waqas20@gmail.com>
parents:
68
diff
changeset
|
382 else |
|
113
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
383 -- user not online |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
384 if user_exists(node, host) then |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
385 if stanza.name == "presence" then |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
386 if stanza.attr.type == "probe" and is_authorized_to_see_presence(origin, node, host) then -- FIXME what to do for not c2s? |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
387 -- TODO send last recieved unavailable presence |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
388 else |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
389 -- TODO send unavailable presence |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
390 end |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
391 elseif stanza.name == "message" then |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
392 -- TODO send message error, or store offline messages |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
393 elseif stanza.name == "iq" then |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
394 -- TODO send IQ error |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
395 end |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
396 else -- user does not exist |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
397 -- TODO we would get here for nodeless JIDs too. Do something fun maybe? Echo service? Let plugins use xmpp:server/resource addresses? |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
398 if stanza.name == "presence" then |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
399 if stanza.attr.type == "probe" then |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
400 send(origin, st.presence({from = user.."@"..host, to = origin.username.."@"..origin.host, type = "unsubscribed"})); |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
401 end |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
402 -- else ignore |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
403 else |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
404 send(origin, st.error_reply(stanza, "cancel", "service-unavailable")); |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
405 end |
|
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
406 end |
|
71
fbb4ef1da82e
Added: Local stanza routing
Waqas Hussain <waqas20@gmail.com>
parents:
68
diff
changeset
|
407 end |
|
68
ceb7a55676a4
Beginnings of real stanza routing
Matthew Wild <mwild1@gmail.com>
parents:
66
diff
changeset
|
408 else |
|
ceb7a55676a4
Beginnings of real stanza routing
Matthew Wild <mwild1@gmail.com>
parents:
66
diff
changeset
|
409 -- Remote host |
| 127 | 410 send_s2s(origin.host, host, stanza); |
|
68
ceb7a55676a4
Beginnings of real stanza routing
Matthew Wild <mwild1@gmail.com>
parents:
66
diff
changeset
|
411 end |
|
113
9026fdad1531
Working presence, presence probes and other fixes
Waqas Hussain <waqas20@gmail.com>
parents:
106
diff
changeset
|
412 stanza.attr.to = to; -- reset |
| 30 | 413 end |
| 414 | |
| 415 function handle_stanza_toremote(stanza) | |
| 416 log("error", "Stanza bound for remote host, but s2s is not implemented"); | |
| 417 end | |
|
138
cb0dd442ca63
Making the best attempt out of a bad merge from waqas
Matthew Wild <mwild1@gmail.com>
diff
changeset
|
418 >>>>>>> other |
