mod_pep: Fix undefined global access
[prosody.git] / plugins / mod_presence.lua
index fa070b7f502c419acc4b91654ee813263bdd4865..623000625d78276b55f67afe7d076118f6bc6ad1 100644 (file)
@@ -276,22 +276,24 @@ local outbound_presence_handler = function(data)
        -- outbound presence recieved\r
        local origin, stanza = data.origin, data.stanza;\r
 \r
-       local t = stanza.attr.type;\r
-       if t ~= nil and t ~= "unavailable" and t ~= "error" then -- check for subscriptions and probes\r
-               handle_outbound_presence_subscriptions_and_probes(origin, stanza, jid_bare(stanza.attr.from), jid_bare(stanza.attr.to), core_route_stanza);\r
-               return true;\r
-       end\r
-\r
        local to = stanza.attr.to;\r
-       local to_bare = jid_bare(to);\r
-       if not(origin.roster[to_bare] and (origin.roster[to_bare].subscription == "both" or origin.roster[to_bare].subscription == "from")) then -- directed presence\r
-               origin.directed = origin.directed or {};\r
-               if t then -- removing from directed presence list on sending an error or unavailable\r
-                       origin.directed[to] = nil; -- FIXME does it make more sense to add to_bare rather than to?\r
-               else\r
-                       origin.directed[to] = true; -- FIXME does it make more sense to add to_bare rather than to?\r
+       if to then\r
+               local t = stanza.attr.type;\r
+               if t ~= nil and t ~= "unavailable" and t ~= "error" then -- check for subscriptions and probes\r
+                       handle_outbound_presence_subscriptions_and_probes(origin, stanza, jid_bare(stanza.attr.from), jid_bare(stanza.attr.to), core_route_stanza);\r
+                       return true;\r
                end\r
-       end\r
+\r
+               local to_bare = jid_bare(to);\r
+               if not(origin.roster[to_bare] and (origin.roster[to_bare].subscription == "both" or origin.roster[to_bare].subscription == "from")) then -- directed presence\r
+                       origin.directed = origin.directed or {};\r
+                       if t then -- removing from directed presence list on sending an error or unavailable\r
+                               origin.directed[to] = nil; -- FIXME does it make more sense to add to_bare rather than to?\r
+                       else\r
+                               origin.directed[to] = true; -- FIXME does it make more sense to add to_bare rather than to?\r
+                       end\r
+               end\r
+       end -- TODO maybe handle normal presence here, instead of letting it pass to incoming handlers?\r
 end\r
 \r
 module:hook("pre-presence/full", outbound_presence_handler);\r
@@ -302,14 +304,14 @@ module:hook("presence/bare", function(data)
        -- inbound presence to bare JID recieved\r
        local origin, stanza = data.origin, data.stanza;\r
 \r
-       local t = stanza.attr.type;\r
-       if t ~= nil and t ~= "unavailable" and t ~= "error" then -- check for subscriptions and probes sent to bare JID\r
-               handle_inbound_presence_subscriptions_and_probes(origin, stanza, jid_bare(stanza.attr.from), jid_bare(stanza.attr.to), core_route_stanza);\r
-               return true;\r
-       end\r
-       \r
        local to = stanza.attr.to;\r
+       local t = stanza.attr.type;\r
        if to then\r
+               if t ~= nil and t ~= "unavailable" and t ~= "error" then -- check for subscriptions and probes sent to bare JID\r
+                       handle_inbound_presence_subscriptions_and_probes(origin, stanza, jid_bare(stanza.attr.from), jid_bare(stanza.attr.to), core_route_stanza);\r
+                       return true;\r
+               end\r
+       \r
                local user = bare_sessions[to];\r
                if user then\r
                        for _, session in pairs(user.sessions) do\r
@@ -318,7 +320,7 @@ module:hook("presence/bare", function(data)
                                end\r
                        end\r
                end -- no resources not online, discard\r
-       else\r
+       elseif not t or t == "unavailable" then\r
                handle_normal_presence(origin, stanza, core_route_stanza);\r
        end\r
        return true;\r