Mercurial > prosody-hg
comparison plugins/mod_presence.lua @ 2877:1edeb8fe7d14
Merge 0.6.2/waqas with 0.6.2/MattJ
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Wed, 03 Mar 2010 22:05:05 +0000 |
| parents | b5924a017849 |
| children | c75c22c316d6 b7049746bd29 |
comparison
equal
deleted
inserted
replaced
| 2813:46dfcc33ea9e | 2877:1edeb8fe7d14 |
|---|---|
| 16 | 16 |
| 17 local st = require "util.stanza"; | 17 local st = require "util.stanza"; |
| 18 local jid_split = require "util.jid".split; | 18 local jid_split = require "util.jid".split; |
| 19 local jid_bare = require "util.jid".bare; | 19 local jid_bare = require "util.jid".bare; |
| 20 local hosts = hosts; | 20 local hosts = hosts; |
| 21 local NULL = {}; | |
| 21 | 22 |
| 22 local rostermanager = require "core.rostermanager"; | 23 local rostermanager = require "core.rostermanager"; |
| 23 local sessionmanager = require "core.sessionmanager"; | 24 local sessionmanager = require "core.sessionmanager"; |
| 24 local offlinemanager = require "core.offlinemanager"; | 25 local offlinemanager = require "core.offlinemanager"; |
| 25 | 26 |
| 52 end | 53 end |
| 53 end | 54 end |
| 54 end | 55 end |
| 55 return recipients; | 56 return recipients; |
| 56 end | 57 end |
| 57 local function recalc_resource_map(origin) | 58 local function recalc_resource_map(user) |
| 58 local user = hosts[origin.host].sessions[origin.username]; | 59 if user then |
| 59 user.top_resources = select_top_resources(user); | 60 user.top_resources = select_top_resources(user); |
| 60 if #user.top_resources == 0 then user.top_resources = nil; end | 61 if #user.top_resources == 0 then user.top_resources = nil; end |
| 62 end | |
| 61 end | 63 end |
| 62 | 64 |
| 63 function handle_normal_presence(origin, stanza, core_route_stanza) | 65 function handle_normal_presence(origin, stanza, core_route_stanza) |
| 66 if full_sessions[origin.full_jid] then -- if user is still connected | |
| 67 origin.send(stanza); -- reflect their presence back to them | |
| 68 end | |
| 64 local roster = origin.roster; | 69 local roster = origin.roster; |
| 65 local node, host = origin.username, origin.host; | 70 local node, host = origin.username, origin.host; |
| 66 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast to all resources | 71 local user = bare_sessions[node.."@"..host]; |
| 72 for _, res in pairs(user and user.sessions or NULL) do -- broadcast to all resources | |
| 67 if res ~= origin and res.presence then -- to resource | 73 if res ~= origin and res.presence then -- to resource |
| 68 stanza.attr.to = res.full_jid; | 74 stanza.attr.to = res.full_jid; |
| 69 core_route_stanza(origin, stanza); | 75 core_route_stanza(origin, stanza); |
| 70 end | 76 end |
| 71 end | 77 end |
| 74 stanza.attr.to = jid; | 80 stanza.attr.to = jid; |
| 75 core_route_stanza(origin, stanza); | 81 core_route_stanza(origin, stanza); |
| 76 end | 82 end |
| 77 end | 83 end |
| 78 if stanza.attr.type == nil and not origin.presence then -- initial presence | 84 if stanza.attr.type == nil and not origin.presence then -- initial presence |
| 85 origin.presence = stanza; -- FIXME repeated later | |
| 79 local probe = st.presence({from = origin.full_jid, type = "probe"}); | 86 local probe = st.presence({from = origin.full_jid, type = "probe"}); |
| 80 for jid, item in pairs(roster) do -- probe all contacts we are subscribed to | 87 for jid, item in pairs(roster) do -- probe all contacts we are subscribed to |
| 81 if item.subscription == "both" or item.subscription == "to" then | 88 if item.subscription == "both" or item.subscription == "to" then |
| 82 probe.attr.to = jid; | 89 probe.attr.to = jid; |
| 83 core_route_stanza(origin, probe); | 90 core_route_stanza(origin, probe); |
| 84 end | 91 end |
| 85 end | 92 end |
| 86 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast from all available resources | 93 for _, res in pairs(user and user.sessions or NULL) do -- broadcast from all available resources |
| 87 if res ~= origin and res.presence then | 94 if res ~= origin and res.presence then |
| 88 res.presence.attr.to = origin.full_jid; | 95 res.presence.attr.to = origin.full_jid; |
| 89 core_route_stanza(res, res.presence); | 96 core_route_stanza(res, res.presence); |
| 90 res.presence.attr.to = nil; | 97 res.presence.attr.to = nil; |
| 91 end | 98 end |
| 112 end | 119 end |
| 113 if stanza.attr.type == "unavailable" then | 120 if stanza.attr.type == "unavailable" then |
| 114 origin.presence = nil; | 121 origin.presence = nil; |
| 115 if origin.priority then | 122 if origin.priority then |
| 116 origin.priority = nil; | 123 origin.priority = nil; |
| 117 recalc_resource_map(origin); | 124 recalc_resource_map(user); |
| 118 end | 125 end |
| 119 if origin.directed then | 126 if origin.directed then |
| 120 for jid in pairs(origin.directed) do | 127 for jid in pairs(origin.directed) do |
| 121 stanza.attr.to = jid; | 128 stanza.attr.to = jid; |
| 122 core_route_stanza(origin, stanza); | 129 core_route_stanza(origin, stanza); |
| 134 if priority > 127 then priority = 127 end | 141 if priority > 127 then priority = 127 end |
| 135 else priority = 0; end | 142 else priority = 0; end |
| 136 else priority = 0; end | 143 else priority = 0; end |
| 137 if origin.priority ~= priority then | 144 if origin.priority ~= priority then |
| 138 origin.priority = priority; | 145 origin.priority = priority; |
| 139 recalc_resource_map(origin); | 146 recalc_resource_map(user); |
| 140 end | 147 end |
| 141 end | 148 end |
| 142 stanza.attr.to = nil; -- reset it | 149 stanza.attr.to = nil; -- reset it |
| 143 end | 150 end |
| 144 | 151 |
| 215 end | 222 end |
| 216 | 223 |
| 217 if stanza.attr.type == "probe" then | 224 if stanza.attr.type == "probe" then |
| 218 if rostermanager.is_contact_subscribed(node, host, from_bare) then | 225 if rostermanager.is_contact_subscribed(node, host, from_bare) then |
| 219 if 0 == send_presence_of_available_resources(node, host, st_from, origin, core_route_stanza) then | 226 if 0 == send_presence_of_available_resources(node, host, st_from, origin, core_route_stanza) then |
| 220 -- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too) | 227 core_route_stanza(hosts[host], st.presence({from=to_bare, to=from_bare, type="unavailable"})); -- TODO send last activity |
| 221 end | 228 end |
| 222 else | 229 else |
| 223 core_route_stanza(hosts[host], st.presence({from=to_bare, to=from_bare, type="unsubscribed"})); | 230 core_route_stanza(hosts[host], st.presence({from=to_bare, to=from_bare, type="unsubscribed"})); |
| 224 end | 231 end |
| 225 elseif stanza.attr.type == "subscribe" then | 232 elseif stanza.attr.type == "subscribe" then |
| 226 if rostermanager.is_contact_subscribed(node, host, from_bare) then | 233 if rostermanager.is_contact_subscribed(node, host, from_bare) then |
| 227 core_route_stanza(hosts[host], st.presence({from=to_bare, to=from_bare, type="subscribed"})); -- already subscribed | 234 core_route_stanza(hosts[host], st.presence({from=to_bare, to=from_bare, type="subscribed"})); -- already subscribed |
| 228 -- Sending presence is not clearly stated in the RFC, but it seems appropriate | 235 -- Sending presence is not clearly stated in the RFC, but it seems appropriate |
| 229 if 0 == send_presence_of_available_resources(node, host, from_bare, origin, core_route_stanza) then | 236 if 0 == send_presence_of_available_resources(node, host, from_bare, origin, core_route_stanza) then |
| 230 -- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too) | 237 core_route_stanza(hosts[host], st.presence({from=to_bare, to=from_bare, type="unavailable"})); -- TODO send last activity |
| 231 end | 238 end |
| 232 else | 239 else |
| 233 core_route_stanza(hosts[host], st.presence({from=to_bare, to=from_bare, type="unavailable"})); -- acknowledging receipt | 240 core_route_stanza(hosts[host], st.presence({from=to_bare, to=from_bare, type="unavailable"})); -- acknowledging receipt |
| 234 if not rostermanager.is_contact_pending_in(node, host, from_bare) then | 241 if not rostermanager.is_contact_pending_in(node, host, from_bare) then |
| 235 if rostermanager.set_contact_pending_in(node, host, from_bare) then | 242 if rostermanager.set_contact_pending_in(node, host, from_bare) then |
| 322 local session = full_sessions[stanza.attr.to]; | 329 local session = full_sessions[stanza.attr.to]; |
| 323 if session then | 330 if session then |
| 324 -- TODO fire post processing event | 331 -- TODO fire post processing event |
| 325 session.send(stanza); | 332 session.send(stanza); |
| 326 end -- resource not online, discard | 333 end -- resource not online, discard |
| 334 return true; | |
| 335 end); | |
| 336 module:hook("presence/host", function(data) | |
| 337 -- inbound presence to the host | |
| 338 local origin, stanza = data.origin, data.stanza; | |
| 339 | |
| 340 local from_bare = jid_bare(stanza.attr.from); | |
| 341 local t = stanza.attr.type; | |
| 342 if t == "probe" then | |
| 343 core_route_stanza(hosts[module.host], st.presence({ from = module.host, to = from_bare, id = stanza.attr.id })); | |
| 344 elseif t == "subscribe" then | |
| 345 core_route_stanza(hosts[module.host], st.presence({ from = module.host, to = from_bare, id = stanza.attr.id, type = "subscribed" })); | |
| 346 core_route_stanza(hosts[module.host], st.presence({ from = module.host, to = from_bare, id = stanza.attr.id })); | |
| 347 end | |
| 327 return true; | 348 return true; |
| 328 end); | 349 end); |
| 329 | 350 |
| 330 module:hook("resource-unbind", function(event) | 351 module:hook("resource-unbind", function(event) |
| 331 local session, err = event.session, event.error; | 352 local session, err = event.session, event.error; |
