From 11e4a5fa67c252cd80b5717869b562b2ea69c8b2 Mon Sep 17 00:00:00 2001 From: Waqas Hussain Date: Thu, 2 Jul 2009 23:46:27 +0500 Subject: mod_presence: Removed dead code --- plugins/mod_presence.lua | 48 ------------------------------------------------ 1 file changed, 48 deletions(-) (limited to 'plugins/mod_presence.lua') diff --git a/plugins/mod_presence.lua b/plugins/mod_presence.lua index 80a2ecca..f69c91d8 100644 --- a/plugins/mod_presence.lua +++ b/plugins/mod_presence.lua @@ -37,21 +37,6 @@ function core_route_stanza(origin, stanza) _core_route_stanza(origin, stanza); end -function handle_presence(origin, stanza, from_bare, to_bare, core_route_stanza, inbound) - local type = stanza.attr.type; - if type and type ~= "unavailable" and type ~= "error" then - if inbound then - handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza); - else - handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza); - end - elseif not inbound and not stanza.attr.to then - handle_normal_presence(origin, stanza, core_route_stanza); - else - core_route_stanza(origin, stanza); - end -end - local function select_top_resources(user) local priority = 0; local recipients = {}; @@ -268,39 +253,6 @@ function handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_b stanza.attr.from, stanza.attr.to = st_from, st_to; end -local function presence_handler(data) - local origin, stanza = data.origin, data.stanza; - local to = stanza.attr.to; - local node, host = jid_split(to); - local to_bare = jid_bare(to); - local from_bare = jid_bare(stanza.attr.from); - if origin.type == "c2s" then - if to ~= nil and not(origin.roster[to_bare] and (origin.roster[to_bare].subscription == "both" or origin.roster[to_bare].subscription == "from")) then -- directed presence - origin.directed = origin.directed or {}; - origin.directed[to] = true; -- FIXME does it make more sense to add to_bare rather than to? - end - if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" and stanza.attr.type ~= "error" then - handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza); - elseif not to then - handle_normal_presence(origin, stanza, core_route_stanza); - else - core_route_stanza(origin, stanza); - end - elseif (origin.type == "s2sin" or origin.type == "component") and hosts[host] then - if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" and stanza.attr.type ~= "error" then - handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza); - else - core_route_stanza(origin, stanza); - end - end - return true; -end - -prosody.events.add_handler(module:get_host().."/presence", presence_handler); -module.unload = function() - prosody.events.remove_handler(module:get_host().."/presence", presence_handler); -end - local outbound_presence_handler = function(data) -- outbound presence recieved local origin, stanza = data.origin, data.stanza; -- cgit v1.2.3 From 6d2714d32f29dd35496e4b2b3c1d84ff1467ab2d Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Fri, 3 Jul 2009 21:44:30 +0100 Subject: mod_presence: Declare t_insert, fixes a potential traceback --- plugins/mod_presence.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'plugins/mod_presence.lua') diff --git a/plugins/mod_presence.lua b/plugins/mod_presence.lua index f69c91d8..df31a4c5 100644 --- a/plugins/mod_presence.lua +++ b/plugins/mod_presence.lua @@ -10,7 +10,7 @@ local log = module._log; local require = require; local pairs, ipairs = pairs, ipairs; -local t_concat = table.concat; +local t_concat, t_insert = table.concat, table.insert; local s_find = string.find; local tonumber = tonumber; -- cgit v1.2.3 From 720184a0b5d035382045a38b93b6e30b23e2d0a1 Mon Sep 17 00:00:00 2001 From: Waqas Hussain Date: Sun, 5 Jul 2009 11:57:18 +0500 Subject: mod_presence: Removed some unnecessary code --- plugins/mod_presence.lua | 3 --- 1 file changed, 3 deletions(-) (limited to 'plugins/mod_presence.lua') diff --git a/plugins/mod_presence.lua b/plugins/mod_presence.lua index df31a4c5..02c3945b 100644 --- a/plugins/mod_presence.lua +++ b/plugins/mod_presence.lua @@ -119,13 +119,10 @@ function handle_normal_presence(origin, stanza, core_route_stanza) recalc_resource_map(origin); end if origin.directed then - local old_from = stanza.attr.from; - stanza.attr.from = origin.full_jid; for jid in pairs(origin.directed) do stanza.attr.to = jid; core_route_stanza(origin, stanza); end - stanza.attr.from = old_from; origin.directed = nil; end else -- cgit v1.2.3 From b5bf437c6f0b92758772e32f9973d0a85550d616 Mon Sep 17 00:00:00 2001 From: Waqas Hussain Date: Sun, 5 Jul 2009 11:59:51 +0500 Subject: mod_presence: Eliminated a jid_split --- plugins/mod_presence.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'plugins/mod_presence.lua') diff --git a/plugins/mod_presence.lua b/plugins/mod_presence.lua index 02c3945b..27881215 100644 --- a/plugins/mod_presence.lua +++ b/plugins/mod_presence.lua @@ -69,7 +69,7 @@ function handle_normal_presence(origin, stanza, core_route_stanza) core_route_stanza(origin, stanza); end end - local node, host = jid_split(stanza.attr.from); + local node, host = origin.username, origin.host; for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast to all resources if res ~= origin and res.presence then -- to resource stanza.attr.to = res.full_jid; -- cgit v1.2.3 From 0d35651c137aee84768c826891fdde2dd00466a5 Mon Sep 17 00:00:00 2001 From: Waqas Hussain Date: Sun, 5 Jul 2009 12:17:22 +0500 Subject: mod_presence: Simplified roster iteration --- plugins/mod_presence.lua | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'plugins/mod_presence.lua') diff --git a/plugins/mod_presence.lua b/plugins/mod_presence.lua index 27881215..6b706cd5 100644 --- a/plugins/mod_presence.lua +++ b/plugins/mod_presence.lua @@ -62,9 +62,8 @@ end function handle_normal_presence(origin, stanza, core_route_stanza) if origin.roster then - for jid in pairs(origin.roster) do -- broadcast to all interested contacts - local subscription = origin.roster[jid].subscription; - if subscription == "both" or subscription == "from" then + for jid, item in pairs(origin.roster) do -- broadcast to all interested contacts + if item.subscription == "both" or item.subscription == "from" then stanza.attr.to = jid; core_route_stanza(origin, stanza); end @@ -78,9 +77,8 @@ function handle_normal_presence(origin, stanza, core_route_stanza) end if stanza.attr.type == nil and not origin.presence then -- initial presence local probe = st.presence({from = origin.full_jid, type = "probe"}); - for jid in pairs(origin.roster) do -- probe all contacts we are subscribed to - local subscription = origin.roster[jid].subscription; - if subscription == "both" or subscription == "to" then + for jid, item in pairs(origin.roster) do -- probe all contacts we are subscribed to + if item.subscription == "both" or item.subscription == "to" then probe.attr.to = jid; core_route_stanza(origin, probe); end -- cgit v1.2.3 From 25a62bae4e109d8fd1b8f5bd27881ab201badd61 Mon Sep 17 00:00:00 2001 From: Waqas Hussain Date: Sun, 5 Jul 2009 12:25:15 +0500 Subject: mod_presence: Changed handle_normal_presence to use a local roster rather than origin's roster --- plugins/mod_presence.lua | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'plugins/mod_presence.lua') diff --git a/plugins/mod_presence.lua b/plugins/mod_presence.lua index 6b706cd5..e2fcf152 100644 --- a/plugins/mod_presence.lua +++ b/plugins/mod_presence.lua @@ -61,8 +61,9 @@ local function recalc_resource_map(origin) end function handle_normal_presence(origin, stanza, core_route_stanza) - if origin.roster then - for jid, item in pairs(origin.roster) do -- broadcast to all interested contacts + local roster = origin.roster; + if roster then + for jid, item in pairs(roster) do -- broadcast to all interested contacts if item.subscription == "both" or item.subscription == "from" then stanza.attr.to = jid; core_route_stanza(origin, stanza); @@ -77,7 +78,7 @@ function handle_normal_presence(origin, stanza, core_route_stanza) end if stanza.attr.type == nil and not origin.presence then -- initial presence local probe = st.presence({from = origin.full_jid, type = "probe"}); - for jid, item in pairs(origin.roster) do -- probe all contacts we are subscribed to + for jid, item in pairs(roster) do -- probe all contacts we are subscribed to if item.subscription == "both" or item.subscription == "to" then probe.attr.to = jid; core_route_stanza(origin, probe); @@ -90,13 +91,13 @@ function handle_normal_presence(origin, stanza, core_route_stanza) res.presence.attr.to = nil; end end - if origin.roster.pending then -- resend incoming subscription requests - for jid in pairs(origin.roster.pending) do + if roster.pending then -- resend incoming subscription requests + for jid in pairs(roster.pending) do origin.send(st.presence({type="subscribe", from=jid})); -- TODO add to attribute? Use original? end end local request = st.presence({type="subscribe", from=origin.username.."@"..origin.host}); - for jid, item in pairs(origin.roster) do -- resend outgoing subscription requests + for jid, item in pairs(roster) do -- resend outgoing subscription requests if item.ask then request.attr.to = jid; core_route_stanza(origin, request); -- cgit v1.2.3 From d7fd01bb426a887feab7c19c1d580fd91b85ba79 Mon Sep 17 00:00:00 2001 From: Waqas Hussain Date: Sun, 5 Jul 2009 12:40:18 +0500 Subject: mod_presence: Prevented normal presence from non-interested resources from being dropped --- plugins/mod_presence.lua | 136 +++++++++++++++++++++++------------------------ 1 file changed, 66 insertions(+), 70 deletions(-) (limited to 'plugins/mod_presence.lua') diff --git a/plugins/mod_presence.lua b/plugins/mod_presence.lua index e2fcf152..68bcb632 100644 --- a/plugins/mod_presence.lua +++ b/plugins/mod_presence.lua @@ -62,88 +62,84 @@ end function handle_normal_presence(origin, stanza, core_route_stanza) local roster = origin.roster; - if roster then - for jid, item in pairs(roster) do -- broadcast to all interested contacts - if item.subscription == "both" or item.subscription == "from" then - stanza.attr.to = jid; - core_route_stanza(origin, stanza); - end + for jid, item in pairs(roster) do -- broadcast to all interested contacts + if item.subscription == "both" or item.subscription == "from" then + stanza.attr.to = jid; + core_route_stanza(origin, stanza); end - local node, host = origin.username, origin.host; - for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast to all resources - if res ~= origin and res.presence then -- to resource - stanza.attr.to = res.full_jid; - core_route_stanza(origin, stanza); - end + end + local node, host = origin.username, origin.host; + for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast to all resources + if res ~= origin and res.presence then -- to resource + stanza.attr.to = res.full_jid; + core_route_stanza(origin, stanza); end - if stanza.attr.type == nil and not origin.presence then -- initial presence - local probe = st.presence({from = origin.full_jid, type = "probe"}); - for jid, item in pairs(roster) do -- probe all contacts we are subscribed to - if item.subscription == "both" or item.subscription == "to" then - probe.attr.to = jid; - core_route_stanza(origin, probe); - end - end - for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast from all available resources - if res ~= origin and res.presence then - res.presence.attr.to = origin.full_jid; - core_route_stanza(res, res.presence); - res.presence.attr.to = nil; - end - end - if roster.pending then -- resend incoming subscription requests - for jid in pairs(roster.pending) do - origin.send(st.presence({type="subscribe", from=jid})); -- TODO add to attribute? Use original? - end + end + if stanza.attr.type == nil and not origin.presence then -- initial presence + local probe = st.presence({from = origin.full_jid, type = "probe"}); + for jid, item in pairs(roster) do -- probe all contacts we are subscribed to + if item.subscription == "both" or item.subscription == "to" then + probe.attr.to = jid; + core_route_stanza(origin, probe); end - local request = st.presence({type="subscribe", from=origin.username.."@"..origin.host}); - for jid, item in pairs(roster) do -- resend outgoing subscription requests - if item.ask then - request.attr.to = jid; - core_route_stanza(origin, request); - end + end + for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast from all available resources + if res ~= origin and res.presence then + res.presence.attr.to = origin.full_jid; + core_route_stanza(res, res.presence); + res.presence.attr.to = nil; end - local offline = offlinemanager.load(node, host); - if offline then - for _, msg in ipairs(offline) do - origin.send(msg); -- FIXME do we need to modify to/from in any way? - end - offlinemanager.deleteAll(node, host); + end + if roster.pending then -- resend incoming subscription requests + for jid in pairs(roster.pending) do + origin.send(st.presence({type="subscribe", from=jid})); -- TODO add to attribute? Use original? end end - if stanza.attr.type == "unavailable" then - origin.presence = nil; - if origin.priority then - origin.priority = nil; - recalc_resource_map(origin); + local request = st.presence({type="subscribe", from=origin.username.."@"..origin.host}); + for jid, item in pairs(roster) do -- resend outgoing subscription requests + if item.ask then + request.attr.to = jid; + core_route_stanza(origin, request); end - if origin.directed then - for jid in pairs(origin.directed) do - stanza.attr.to = jid; - core_route_stanza(origin, stanza); - end - origin.directed = nil; + end + local offline = offlinemanager.load(node, host); + if offline then + for _, msg in ipairs(offline) do + origin.send(msg); -- FIXME do we need to modify to/from in any way? end - else - origin.presence = stanza; - local priority = stanza:child_with_name("priority"); - if priority and #priority > 0 then - priority = t_concat(priority); - if s_find(priority, "^[+-]?[0-9]+$") then - priority = tonumber(priority); - if priority < -128 then priority = -128 end - if priority > 127 then priority = 127 end - else priority = 0; end - else priority = 0; end - if origin.priority ~= priority then - origin.priority = priority; - recalc_resource_map(origin); + offlinemanager.deleteAll(node, host); + end + end + if stanza.attr.type == "unavailable" then + origin.presence = nil; + if origin.priority then + origin.priority = nil; + recalc_resource_map(origin); + end + if origin.directed then + for jid in pairs(origin.directed) do + stanza.attr.to = jid; + core_route_stanza(origin, stanza); end + origin.directed = nil; end - stanza.attr.to = nil; -- reset it else - log("warn", "presence recieved from client with no roster"); + origin.presence = stanza; + local priority = stanza:child_with_name("priority"); + if priority and #priority > 0 then + priority = t_concat(priority); + if s_find(priority, "^[+-]?[0-9]+$") then + priority = tonumber(priority); + if priority < -128 then priority = -128 end + if priority > 127 then priority = 127 end + else priority = 0; end + else priority = 0; end + if origin.priority ~= priority then + origin.priority = priority; + recalc_resource_map(origin); + end end + stanza.attr.to = nil; -- reset it end function send_presence_of_available_resources(user, host, jid, recipient_session, core_route_stanza) -- cgit v1.2.3 From 4fecbc0bbf5c912f3b8383d2e396cf7282b9c1a4 Mon Sep 17 00:00:00 2001 From: Waqas Hussain Date: Sun, 5 Jul 2009 12:43:25 +0500 Subject: mod_presence: Broadcast presence to resources before contacts --- plugins/mod_presence.lua | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'plugins/mod_presence.lua') diff --git a/plugins/mod_presence.lua b/plugins/mod_presence.lua index 68bcb632..9ae068c2 100644 --- a/plugins/mod_presence.lua +++ b/plugins/mod_presence.lua @@ -62,12 +62,6 @@ end function handle_normal_presence(origin, stanza, core_route_stanza) local roster = origin.roster; - for jid, item in pairs(roster) do -- broadcast to all interested contacts - if item.subscription == "both" or item.subscription == "from" then - stanza.attr.to = jid; - core_route_stanza(origin, stanza); - end - end local node, host = origin.username, origin.host; for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast to all resources if res ~= origin and res.presence then -- to resource @@ -75,6 +69,12 @@ function handle_normal_presence(origin, stanza, core_route_stanza) core_route_stanza(origin, stanza); end end + for jid, item in pairs(roster) do -- broadcast to all interested contacts + if item.subscription == "both" or item.subscription == "from" then + stanza.attr.to = jid; + core_route_stanza(origin, stanza); + end + end if stanza.attr.type == nil and not origin.presence then -- initial presence local probe = st.presence({from = origin.full_jid, type = "probe"}); for jid, item in pairs(roster) do -- probe all contacts we are subscribed to -- cgit v1.2.3 From 44ba02eef0075861e2398dfb5959288c9330f781 Mon Sep 17 00:00:00 2001 From: Waqas Hussain Date: Sun, 5 Jul 2009 17:45:44 +0500 Subject: Moved automatic unavailable presence generation on disconnect from sessionmanager to mod_presence --- plugins/mod_presence.lua | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'plugins/mod_presence.lua') diff --git a/plugins/mod_presence.lua b/plugins/mod_presence.lua index 9ae068c2..4fa9f431 100644 --- a/plugins/mod_presence.lua +++ b/plugins/mod_presence.lua @@ -315,3 +315,14 @@ module:hook("presence/full", function(data) end -- resource not online, discard return true; end); + +module:hook("resource-unbind", function(event) + local session, err = event.session, event.error; + -- Send unavailable presence + if session.presence then + local pres = st.presence{ type = "unavailable" }; + if not(err) or err == "closed" then err = "connection closed"; end + pres:tag("status"):text("Disconnected: "..err):up(); + session:dispatch_stanza(pres); + end +end); -- cgit v1.2.3 From d1ee8e0f537fdc3c5b31557a021a9601544d9cd4 Mon Sep 17 00:00:00 2001 From: Waqas Hussain Date: Sun, 5 Jul 2009 20:40:31 +0500 Subject: mod_presence: Send unavailable presence to resources from non-available resources on disconnect --- plugins/mod_presence.lua | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'plugins/mod_presence.lua') diff --git a/plugins/mod_presence.lua b/plugins/mod_presence.lua index 4fa9f431..89136e3d 100644 --- a/plugins/mod_presence.lua +++ b/plugins/mod_presence.lua @@ -324,5 +324,14 @@ module:hook("resource-unbind", function(event) if not(err) or err == "closed" then err = "connection closed"; end pres:tag("status"):text("Disconnected: "..err):up(); session:dispatch_stanza(pres); + elseif session.directed then + local pres = st.presence{ type = "unavailable" }; + if not(err) or err == "closed" then err = "connection closed"; end + pres:tag("status"):text("Disconnected: "..err):up(); + for jid in pairs(session.directed) do + pres.attr.to = jid; + core_route_stanza(session, pres); + end + session.directed = nil; end end); -- cgit v1.2.3