mod_admin_telnet: rename variable to make it defined (room -> room_name)
[prosody.git] / plugins / mod_admin_telnet.lua
index a17b1c57856cf263c3c9be0f07151aa9b585a2f4..b2a013247c7026fa60dbdf6b46e215df9aa6fd97 100644 (file)
@@ -22,11 +22,12 @@ local console_listener = { default_port = 5582; default_mode = "*a"; interface =
 
 local iterators = require "util.iterators";
 local keys, values = iterators.keys, iterators.values;
-local jid_bare, jid_split = import("util.jid", "bare", "prepped_split");
+local jid_bare, jid_split, jid_join = import("util.jid", "bare", "prepped_split", "join");
 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");
@@ -281,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()
@@ -322,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");
@@ -345,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;
@@ -356,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
@@ -385,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");
@@ -407,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
@@ -418,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");
@@ -518,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
 
@@ -540,35 +539,46 @@ end
 
 def_env.c2s = {};
 
+local function get_jid(session)
+       if session.username then
+               return session.full_jid or jid_join(session.username, session.host, session.resource);
+       end
+
+       local conn = session.conn;
+       local ip = session.ip or "?";
+       local clientport = conn and conn:clientport() or "?";
+       local serverip = conn and conn.server and conn:server():ip() or "?";
+       local serverport = conn and conn:serverport() or "?"
+       return jid_join("["..ip.."]:"..clientport, session.host or "["..serverip.."]:"..serverport);
+end
+
 local function show_c2s(callback)
-       for hostname, host in pairs(hosts) do
-               for username, user in pairs(host.sessions or {}) do
-                       for resource, session in pairs(user.sessions or {}) do
-                               local jid = username.."@"..hostname.."/"..resource;
-                               callback(jid, session);
+       local c2s = array.collect(values(module:shared"/*/c2s/sessions"));
+       c2s:sort(function(a, b)
+               if a.host == b.host then
+                       if a.username == b.username then
+                               return (a.resource or "") > (b.resource or "");
                        end
+                       return (a.username or "") > (b.username or "");
                end
-       end
+               return (a.host or "") > (b.host or "");
+       end):map(function (session)
+               callback(get_jid(session), session)
+       end);
 end
 
 function def_env.c2s:count(match_jid)
-       local count = 0;
-       show_c2s(function (jid, session)
-               if (not match_jid) or jid:match(match_jid) then
-                       count = count + 1;
-               end
-       end);
-       return true, "Total: "..count.." clients";
+       return true, "Total: "..  iterators.count(values(module:shared"/*/c2s/sessions")) .." clients";
 end
 
 function def_env.c2s:show(match_jid, annotate)
        local print, count = self.session.print, 0;
        annotate = annotate or session_flags;
-       local curr_host;
+       local curr_host = false;
        show_c2s(function (jid, session)
                if curr_host ~= session.host then
                        curr_host = session.host;
-                       print(curr_host);
+                       print(curr_host or "(not connected to any host yet)");
                end
                if (not match_jid) or jid:match(match_jid) then
                        count = count + 1;
@@ -836,19 +846,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
@@ -947,11 +957,11 @@ local function check_muc(jid)
 end
 
 function def_env.muc:create(room_jid)
-       local room, host = check_muc(room_jid);
+       local room_name, host = check_muc(room_jid);
        if not room_name then
                return room_name, host;
        end
-       if not room then return nil, host end
+       if not room_name then return nil, host end
        if hosts[host].modules.muc.rooms[room_jid] then return nil, "Room exists already" end
        return hosts[host].modules.muc.create_room(room_jid);
 end
@@ -973,6 +983,7 @@ function def_env.muc:list(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);
@@ -1095,11 +1106,38 @@ 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)
-       local option = module:get_option("console_banner");
-       if option == nil or option == "full" or option == "graphic" then
+       local option = module:get_option_string("console_banner", "full");
+       if option == "full" or option == "graphic" then
                session.print [[
                    ____                \   /     _
                     |  _ \ _ __ ___  ___  _-_   __| |_   _
@@ -1110,17 +1148,13 @@ function printbanner(session)
 
 ]]
        end
-       if option == nil or option == "short" or option == "full" then
+       if option == "short" or option == "full" then
        session.print("Welcome to the Prosody administration console. For a list of commands, type: help");
        session.print("You may find more help on using this console in our online documentation at ");
        session.print("http://prosody.im/doc/console\n");
        end
-       if option and option ~= "short" and option ~= "full" and option ~= "graphic" then
-               if type(option) == "string" then
-                       session.print(option)
-               elseif type(option) == "function" then
-                       module:log("warn", "Using functions as value for the console_banner option is no longer supported");
-               end
+       if option ~= "short" and option ~= "full" and option ~= "graphic" then
+               session.print(option);
        end
 end