mod_admin_telnet: c2s:show() etc, list all sessions including unauthenticated or...
[prosody.git] / plugins / mod_admin_telnet.lua
index 6a5f87d41f49f6c18ac70498ed15172fef0e3c1f..c3434d3323bfb5c65ed9da51ea1a0d37db0a2ce9 100644 (file)
@@ -22,7 +22,7 @@ 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;
@@ -282,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()
@@ -512,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
 
@@ -534,15 +539,26 @@ 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
+       return "(unknown)";
+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)
@@ -558,11 +574,11 @@ 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;
@@ -1090,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 [[
                    ____                \   /     _
                     |  _ \ _ __ ___  ___  _-_   __| |_   _
@@ -1105,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