mod_presence: Removed dead code
[prosody.git] / plugins / mod_console.lua
index 097d7bd89c020c6bb501c14a71e2ac93137837c6..0f2c67112e2f844168833dd0554ae5d948946fb5 100644 (file)
@@ -8,6 +8,8 @@
 
 module.host = "*";
 
+local _G = _G;
+
 local prosody = _G.prosody;
 local hosts = prosody.hosts;
 local connlisteners_register = require "net.connlisteners".register;
@@ -21,6 +23,10 @@ local commands = {};
 local def_env = {};
 local default_env_mt = { __index = def_env };
 
+local function redirect_output(_G, session)
+       return setmetatable({ print = session.print }, { __index = function (t, k) return rawget(_G, k); end, __newindex = function (t, k, v) rawset(_G, k, v); end });
+end
+
 console = {};
 
 function console:new_session(conn)
@@ -86,7 +92,7 @@ function console_listener.listener(conn, data)
                                end
                        end
                        
-                       setfenv(chunk, (useglobalenv and _G) or session.env or nil);
+                       setfenv(chunk, (useglobalenv and redirect_output(_G, session)) or session.env or nil);
                        
                        local ranok, taskok, message = pcall(chunk);
                        
@@ -156,7 +162,7 @@ end
 
 def_env.module = {};
 
-local function get_hosts_set(hosts)
+local function get_hosts_set(hosts, module)
        if type(hosts) == "table" then
                if hosts[1] then
                        return set.new(hosts);
@@ -166,8 +172,9 @@ local function get_hosts_set(hosts)
        elseif type(hosts) == "string" then
                return set.new { hosts };
        elseif hosts == nil then
+               local mm = require "modulemanager";
                return set.new(array.collect(keys(prosody.hosts)))
-                       / function (host) return prosody.hosts[host].type == "local"; end;
+                       / function (host) return prosody.hosts[host].type == "local" or module and mm.is_loaded(host, module); end;
        end
 end
 
@@ -197,7 +204,7 @@ end
 function def_env.module:unload(name, hosts)
        local mm = require "modulemanager";
 
-       hosts = get_hosts_set(hosts);
+       hosts = get_hosts_set(hosts, name);
        
        -- Unload the module for each host
        local ok, err, count = true, nil, 0;
@@ -219,7 +226,7 @@ end
 function def_env.module:reload(name, hosts)
        local mm = require "modulemanager";
 
-       hosts = get_hosts_set(hosts);
+       hosts = get_hosts_set(hosts, name);
        
        -- Reload the module for each host
        local ok, err, count = true, nil, 0;
@@ -418,7 +425,7 @@ function def_env.s2s:close(from, to)
                else
                        print("Closed "..count.." incoming session"..((count == 1 and "") or "s").." from "..from.." to "..to);
                end
-       elseif hosts[to] and hosts[from]
+       elseif hosts[to] and hosts[from] then
                return false, "Both of the hostnames you specified are local, there are no s2s sessions to close";
        else
                return false, "Neither of the hostnames you specified are being used on this server";