Merge 0.6->0.7
[prosody.git] / plugins / mod_proxy65.lua
index 02b18ec1a483b7fba0b93f4824107fa5dd88fc06..190d30bef5a9d9fae1b41be3bf1c6c937b3e5776 100644 (file)
@@ -14,7 +14,7 @@ if module:get_host_type() ~= "component" then
        error("proxy65 should be loaded as a component, please see http://prosody.im/doc/components", 0);
 end
 
-local jid_split, jid_join, jid_compare = require "util.jid".split, require "util.jid".join, require "util.jid".compare;
+local jid_split, jid_join = require "util.jid".split, require "util.jid".join;
 local st = require "util.stanza";
 local componentmanager = require "core.componentmanager";
 local config_get = require "core.configmanager".get;
@@ -151,11 +151,24 @@ local function get_stream_host(origin, stanza)
        local err_reply = replies_cache.stream_host_err;
        local sid = stanza.tags[1].attr.sid;
        local allow = false;
-       local jid = stanza.attr.from;
+       local jid_node, jid_host, jid_resource = jid_split(stanza.attr.from);
+       
+       if stanza.attr.from == nil then
+               jid_node = origin.username;
+               jid_host = origin.host;
+               jid_resource = origin.resource;
+       end
        
        if proxy_acl and #proxy_acl > 0 then
-               for _, acl in ipairs(proxy_acl) do
-                       if jid_compare(jid, acl) then allow = true; end
+               if host ~= nil then -- at least a domain is needed.
+                       for _, acl in ipairs(proxy_acl) do
+                               local acl_node, acl_host, acl_resource = jid_split(acl);
+                               if ((acl_node ~= nil and acl_node == jid_node) or acl_node == nil) and
+                                  ((acl_host ~= nil and acl_host == jid_host) or acl_host == nil) and
+                                  ((acl_resource ~= nil and acl_resource == jid_resource) or acl_resource == nil) then
+                                       allow = true;
+                               end
+                       end
                end
        else
                allow = true;
@@ -168,7 +181,7 @@ local function get_stream_host(origin, stanza)
                        replies_cache.stream_host = reply;
                end
        else
-               module:log("warn", "Denying use of proxy for %s", tostring(jid));
+               module:log("warn", "Denying use of proxy for %s", tostring(jid_join(jid_node, jid_host, jid_resource)));
                if err_reply == nil then
                        err_reply = st.iq({type="error", from=host})
                                :query("http://jabber.org/protocol/bytestreams")
@@ -207,8 +220,7 @@ local function set_activation(stanza)
        return reply, from, to, sid;
 end
 
-function handle_to_domain(event)
-       local origin, stanza = event.origin, event.stanza;
+function handle_to_domain(origin, stanza)
        local to_node, to_host, to_resource = jid_split(stanza.attr.to);
        if to_node == nil then
                local type = stanza.attr.type;
@@ -257,7 +269,6 @@ function handle_to_domain(event)
        end
        return;
 end
-module:hook("iq/host", handle_to_domain);
 
 if not connlisteners.register(module.host .. ':proxy65', connlistener) then
        module:log("error", "mod_proxy65: Could not establish a connection listener. Check your configuration please.");
@@ -265,4 +276,4 @@ if not connlisteners.register(module.host .. ':proxy65', connlistener) then
 end
 
 connlisteners.start(module.host .. ':proxy65');
-component = componentmanager.register_component(host, function() end);
+component = componentmanager.register_component(host, handle_to_domain);