Merge 0.9->0.10
[prosody.git] / plugins / mod_admin_telnet.lua
index a3352b101124edba86755be805359f73889425ba..b77a1f564fb2d02127ac0aff35d44ae8e5f2a9df 100644 (file)
@@ -27,6 +27,7 @@ local set, array = require "util.set", require "util.array";
 local cert_verify_identity = require "util.x509".verify_identity;
 local envload = require "util.envload".envload;
 local envloadfile = require "util.envload".envloadfile;
+local has_pposix, pposix = pcall(require, "util.pposix");
 
 local commands = module:shared("commands")
 local def_env = module:shared("env");
@@ -170,6 +171,10 @@ function console_listener.ondisconnect(conn, err)
        end
 end
 
+function console_listener.ondetach(conn)
+       sessions[conn] = nil;
+end
+
 -- Console commands --
 -- These are simple commands, not valid standalone in Lua
 
@@ -277,6 +282,8 @@ end
 -- Session environment --
 -- Anything in def_env will be accessible within the session as a global variable
 
+--luacheck: ignore 212/self
+
 def_env.server = {};
 
 function def_env.server:insane_reload()
@@ -318,7 +325,7 @@ local function human(kb)
 end
 
 function def_env.server:memory()
-       if not pposix.meminfo then
+       if not has_pposix or not pposix.meminfo then
                return true, "Lua is using "..collectgarbage("count");
        end
        local mem, lua_mem = pposix.meminfo(), collectgarbage("count");
@@ -341,10 +348,9 @@ local function get_hosts_set(hosts, module)
        elseif type(hosts) == "string" then
                return set.new { hosts };
        elseif hosts == nil then
-               local mm = require "modulemanager";
                local hosts_set = set.new(array.collect(keys(prosody.hosts)))
-                       / function (host) return (prosody.hosts[host].type == "local" or module and mm.is_loaded(host, module)) and host or nil; end;
-               if module and mm.get_module("*", module) then
+                       / function (host) return (prosody.hosts[host].type == "local" or module and modulemanager.is_loaded(host, module)) and host or nil; end;
+               if module and modulemanager.get_module("*", module) then
                        hosts_set:add("*");
                end
                return hosts_set;
@@ -352,15 +358,13 @@ local function get_hosts_set(hosts, module)
 end
 
 function def_env.module:load(name, hosts, config)
-       local mm = require "modulemanager";
-
        hosts = get_hosts_set(hosts);
 
        -- Load the module for each host
        local ok, err, count, mod = true, nil, 0, nil;
        for host in hosts do
-               if (not mm.is_loaded(host, name)) then
-                       mod, err = mm.load(host, name, config);
+               if (not modulemanager.is_loaded(host, name)) then
+                       mod, err = modulemanager.load(host, name, config);
                        if not mod then
                                ok = false;
                                if err == "global-module-already-loaded" then
@@ -381,15 +385,13 @@ function def_env.module:load(name, hosts, config)
 end
 
 function def_env.module:unload(name, hosts)
-       local mm = require "modulemanager";
-
        hosts = get_hosts_set(hosts, name);
 
        -- Unload the module for each host
        local ok, err, count = true, nil, 0;
        for host in hosts do
-               if mm.is_loaded(host, name) then
-                       ok, err = mm.unload(host, name);
+               if modulemanager.is_loaded(host, name) then
+                       ok, err = modulemanager.unload(host, name);
                        if not ok then
                                ok = false;
                                self.session.print(err or "Unknown error unloading module");
@@ -403,8 +405,6 @@ function def_env.module:unload(name, hosts)
 end
 
 function def_env.module:reload(name, hosts)
-       local mm = require "modulemanager";
-
        hosts = array.collect(get_hosts_set(hosts, name)):sort(function (a, b)
                if a == "*" then return true
                elseif b == "*" then return false
@@ -414,8 +414,8 @@ function def_env.module:reload(name, hosts)
        -- Reload the module for each host
        local ok, err, count = true, nil, 0;
        for _, host in ipairs(hosts) do
-               if mm.is_loaded(host, name) then
-                       ok, err = mm.reload(host, name);
+               if modulemanager.is_loaded(host, name) then
+                       ok, err = modulemanager.reload(host, name);
                        if not ok then
                                ok = false;
                                self.session.print(err or "Unknown error reloading module");
@@ -481,17 +481,6 @@ function def_env.config:reload()
        return ok, (ok and "Config reloaded (you may need to reload modules to take effect)") or tostring(err);
 end
 
-def_env.hosts = {};
-function def_env.hosts:list()
-       for host, host_session in pairs(hosts) do
-               self.session.print(host);
-       end
-       return true, "Done";
-end
-
-function def_env.hosts:add(name)
-end
-
 local function common_info(session, line)
        if session.id then
                line[#line+1] = "["..session.id.."]"
@@ -525,6 +514,9 @@ local function session_flags(session, line)
        if session.ip and session.ip:match(":") then
                line[#line+1] = "(IPv6)";
        end
+       if session.remote then
+               line[#line+1] = "(remote)";
+       end
        return table.concat(line, " ");
 end
 
@@ -843,19 +835,19 @@ function def_env.s2s:close(from, to)
                        (session.close or s2smanager.destroy_session)(session);
                        count = count + 1 ;
                end
-                       end
+       end
        return true, "Closed "..count.." s2s session"..((count == 1 and "") or "s");
 end
 
 function def_env.s2s:closeall(host)
-        local count = 0;
+       local count = 0;
        local s2s_sessions = module:shared"/*/s2s/sessions";
        for _,session in pairs(s2s_sessions) do
                if not host or session.from_host == host or session.to_host == host then
                        session:close();
-                                count = count + 1;
-                        end
-                end
+                       count = count + 1;
+               end
+       end
        if count == 0 then return false, "No sessions to close.";
        else return true, "Closed "..count.." s2s session"..((count == 1 and "") or "s"); end
 end
@@ -872,9 +864,19 @@ end
 function def_env.host:list()
        local print = self.session.print;
        local i = 0;
+       local type;
        for host in values(array.collect(keys(prosody.hosts)):sort()) do
                i = i + 1;
-               print(host);
+               type = hosts[host].type;
+               if type == "local" then
+                       print(host);
+               else
+                       type = module:context(host):get_option_string("component_module", type);
+                       if type ~= "component" then
+                               type = type .. " component";
+                       end
+                       print(("%s (%s)"):format(host, type));
+               end
        end
        return true, i.." hosts";
 end
@@ -965,6 +967,20 @@ function def_env.muc:room(room_jid)
        return setmetatable({ room = room_obj }, console_room_mt);
 end
 
+function def_env.muc:list(host)
+       local host_session = hosts[host];
+       if not host_session or not host_session.modules.muc then
+               return nil, "Please supply the address of a local MUC component";
+       end
+       local print = self.session.print;
+       local c = 0;
+       for name in keys(host_session.modules.muc.rooms) do
+               print(name);
+               c = c + 1;
+       end
+       return true, c.." rooms";
+end
+
 local um = require"core.usermanager";
 
 def_env.user = {};
@@ -1061,12 +1077,12 @@ function def_env.dns:lookup(name, typ, class)
 end
 
 function def_env.dns:addnameserver(...)
-       dns.addnameserver(...)
+       dns._resolver:addnameserver(...)
        return true
 end
 
 function def_env.dns:setnameserver(...)
-       dns.setnameserver(...)
+       dns._resolver:setnameserver(...)
        return true
 end
 
@@ -1079,6 +1095,33 @@ function def_env.dns:cache()
        return true, "Cache:\n"..tostring(dns.cache())
 end
 
+def_env.http = {};
+
+function def_env.http:list()
+       local print = self.session.print;
+
+       for host in pairs(prosody.hosts) do
+               local http_apps = modulemanager.get_items("http-provider", host);
+               if #http_apps > 0 then
+                       local http_host = module:context(host):get_option("http_host");
+                       print("HTTP endpoints on "..host..(http_host and (" (using "..http_host.."):") or ":"));
+                       for _, provider in ipairs(http_apps) do
+                               local url = module:context(host):http_url(provider.name);
+                               print("", url);
+                       end
+                       print("");
+               end
+       end
+
+       local default_host = module:get_option("http_default_host");
+       if not default_host then
+               print("HTTP requests to unknown hosts will return 404 Not Found");
+       else
+               print("HTTP requests to unknown hosts will be handled by "..default_host);
+       end
+       return true;
+end
+
 -------------
 
 function printbanner(session)