Software / code / prosody
Comparison
plugins/mod_admin_telnet.lua @ 9404:f40b0cd41a87
mod_admin_telnet: Remove or rename various unused arguments and variables [luacheck]
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Sun, 30 Sep 2018 14:32:32 +0200 |
| parent | 9403:4416c1a96400 |
| child | 9405:86e3e37f25ff |
comparison
equal
deleted
inserted
replaced
| 9403:4416c1a96400 | 9404:f40b0cd41a87 |
|---|---|
| 3 -- Copyright (C) 2008-2010 Waqas Hussain | 3 -- Copyright (C) 2008-2010 Waqas Hussain |
| 4 -- | 4 -- |
| 5 -- This project is MIT/X11 licensed. Please see the | 5 -- This project is MIT/X11 licensed. Please see the |
| 6 -- COPYING file in the source package for more information. | 6 -- COPYING file in the source package for more information. |
| 7 -- | 7 -- |
| 8 -- luacheck: ignore 212/self | |
| 8 | 9 |
| 9 module:set_global(); | 10 module:set_global(); |
| 10 | 11 |
| 11 local hostmanager = require "core.hostmanager"; | 12 local hostmanager = require "core.hostmanager"; |
| 12 local modulemanager = require "core.modulemanager"; | 13 local modulemanager = require "core.modulemanager"; |
| 33 local commands = module:shared("commands") | 34 local commands = module:shared("commands") |
| 34 local def_env = module:shared("env"); | 35 local def_env = module:shared("env"); |
| 35 local default_env_mt = { __index = def_env }; | 36 local default_env_mt = { __index = def_env }; |
| 36 | 37 |
| 37 local function redirect_output(target, session) | 38 local function redirect_output(target, session) |
| 38 local env = setmetatable({ print = session.print }, { __index = function (t, k) return rawget(target, k); end }); | 39 local env = setmetatable({ print = session.print }, { __index = function (_, k) return rawget(target, k); end }); |
| 39 env.dofile = function(name) | 40 env.dofile = function(name) |
| 40 local f, err = envloadfile(name, env); | 41 local f, err = envloadfile(name, env); |
| 41 if not f then return f, err; end | 42 if not f then return f, err; end |
| 42 return f(); | 43 return f(); |
| 43 end; | 44 end; |
| 161 session.send("\0"); | 162 session.send("\0"); |
| 162 return true; | 163 return true; |
| 163 end | 164 end |
| 164 end | 165 end |
| 165 | 166 |
| 166 function console_listener.ondisconnect(conn, err) | 167 function console_listener.ondisconnect(conn, err) -- luacheck: ignore 212/err |
| 167 local session = sessions[conn]; | 168 local session = sessions[conn]; |
| 168 if session then | 169 if session then |
| 169 session.disconnect(); | 170 session.disconnect(); |
| 170 sessions[conn] = nil; | 171 sessions[conn] = nil; |
| 171 end | 172 end |
| 359 | 360 |
| 360 function def_env.module:load(name, hosts, config) | 361 function def_env.module:load(name, hosts, config) |
| 361 hosts = get_hosts_set(hosts); | 362 hosts = get_hosts_set(hosts); |
| 362 | 363 |
| 363 -- Load the module for each host | 364 -- Load the module for each host |
| 364 local ok, err, count, mod = true, nil, 0, nil; | 365 local ok, err, count, mod = true, nil, 0; |
| 365 for host in hosts do | 366 for host in hosts do |
| 366 if (not modulemanager.is_loaded(host, name)) then | 367 if (not modulemanager.is_loaded(host, name)) then |
| 367 mod, err = modulemanager.load(host, name, config); | 368 mod, err = modulemanager.load(host, name, config); |
| 368 if not mod then | 369 if not mod then |
| 369 ok = false; | 370 ok = false; |
| 567 end):map(function (session) | 568 end):map(function (session) |
| 568 callback(get_jid(session), session) | 569 callback(get_jid(session), session) |
| 569 end); | 570 end); |
| 570 end | 571 end |
| 571 | 572 |
| 572 function def_env.c2s:count(match_jid) | 573 function def_env.c2s:count() |
| 573 return true, "Total: ".. iterators.count(values(module:shared"/*/c2s/sessions")) .." clients"; | 574 return true, "Total: ".. iterators.count(values(module:shared"/*/c2s/sessions")) .." clients"; |
| 574 end | 575 end |
| 575 | 576 |
| 576 function def_env.c2s:show(match_jid, annotate) | 577 function def_env.c2s:show(match_jid, annotate) |
| 577 local print, count = self.session.print, 0; | 578 local print, count = self.session.print, 0; |
| 651 local sess_lines = { l = localhost, r = remotehost, | 652 local sess_lines = { l = localhost, r = remotehost, |
| 652 annotate(session, { "", direction, remotehost or "?" })}; | 653 annotate(session, { "", direction, remotehost or "?" })}; |
| 653 | 654 |
| 654 if (not match_jid) or remotehost:match(match_jid) or localhost:match(match_jid) then | 655 if (not match_jid) or remotehost:match(match_jid) or localhost:match(match_jid) then |
| 655 table.insert(s2s_list, sess_lines); | 656 table.insert(s2s_list, sess_lines); |
| 657 -- luacheck: ignore 421/print | |
| 656 local print = function (s) table.insert(sess_lines, " "..s); end | 658 local print = function (s) table.insert(sess_lines, " "..s); end |
| 657 if session.sendq then | 659 if session.sendq then |
| 658 print("There are "..#session.sendq.." queued outgoing stanzas for this connection"); | 660 print("There are "..#session.sendq.." queued outgoing stanzas for this connection"); |
| 659 end | 661 end |
| 660 if session.type == "s2sout_unauthed" then | 662 if session.type == "s2sout_unauthed" then |
| 830 local print, count = self.session.print, 0; | 832 local print, count = self.session.print, 0; |
| 831 local s2s_sessions = module:shared"/*/s2s/sessions"; | 833 local s2s_sessions = module:shared"/*/s2s/sessions"; |
| 832 | 834 |
| 833 local match_id; | 835 local match_id; |
| 834 if from and not to then | 836 if from and not to then |
| 835 match_id, from = from; | 837 match_id, from = from, nil; |
| 836 elseif not to then | 838 elseif not to then |
| 837 return false, "Syntax: s2s:close('from', 'to') - Closes all s2s sessions from 'from' to 'to'"; | 839 return false, "Syntax: s2s:close('from', 'to') - Closes all s2s sessions from 'from' to 'to'"; |
| 838 elseif from == to then | 840 elseif from == to then |
| 839 return false, "Both from and to are the same... you can't do that :)"; | 841 return false, "Both from and to are the same... you can't do that :)"; |
| 840 end | 842 end |
| 915 | 917 |
| 916 function def_env.port:close(close_port, close_interface) | 918 function def_env.port:close(close_port, close_interface) |
| 917 close_port = assert(tonumber(close_port), "Invalid port number"); | 919 close_port = assert(tonumber(close_port), "Invalid port number"); |
| 918 local n_closed = 0; | 920 local n_closed = 0; |
| 919 local services = portmanager.get_active_services().data; | 921 local services = portmanager.get_active_services().data; |
| 920 for service, interfaces in pairs(services) do | 922 for service, interfaces in pairs(services) do -- luacheck: ignore 213 |
| 921 for interface, ports in pairs(interfaces) do | 923 for interface, ports in pairs(interfaces) do |
| 922 if not close_interface or close_interface == interface then | 924 if not close_interface or close_interface == interface then |
| 923 if ports[close_port] then | 925 if ports[close_port] then |
| 924 self.session.print("Closing ["..interface.."]:"..close_port.."..."); | 926 self.session.print("Closing ["..interface.."]:"..close_port.."..."); |
| 925 local ok, err = portmanager.close(interface, close_port) | 927 local ok, err = portmanager.close(interface, close_port) |
| 1149 events_obj = prosody.events; | 1151 events_obj = prosody.events; |
| 1150 end | 1152 end |
| 1151 return true, helpers.show_events(events_obj, event); | 1153 return true, helpers.show_events(events_obj, event); |
| 1152 end | 1154 end |
| 1153 | 1155 |
| 1154 function def_env.debug:timers(filter) | 1156 function def_env.debug:timers() |
| 1155 local socket = require "socket"; | 1157 local socket = require "socket"; |
| 1156 local print = self.session.print; | 1158 local print = self.session.print; |
| 1157 local add_task = require"util.timer".add_task; | 1159 local add_task = require"util.timer".add_task; |
| 1158 local h, params = add_task.h, add_task.params; | 1160 local h, params = add_task.h, add_task.params; |
| 1159 if h then | 1161 if h then |
| 1168 end | 1170 end |
| 1169 end | 1171 end |
| 1170 end | 1172 end |
| 1171 if server.event_base then | 1173 if server.event_base then |
| 1172 local count = 0; | 1174 local count = 0; |
| 1173 for k, v in pairs(debug.getregistry()) do | 1175 for _, v in pairs(debug.getregistry()) do |
| 1174 if type(v) == "function" and v.callback and v.callback == add_task._on_timer then | 1176 if type(v) == "function" and v.callback and v.callback == add_task._on_timer then |
| 1175 count = count + 1; | 1177 count = count + 1; |
| 1176 end | 1178 end |
| 1177 end | 1179 end |
| 1178 print(count .. " libevent callbacks"); | 1180 print(count .. " libevent callbacks"); |
| 1188 | 1190 |
| 1189 -- COMPAT: debug:timers() was timer:info() for some time in trunk | 1191 -- COMPAT: debug:timers() was timer:info() for some time in trunk |
| 1190 def_env.timer = { info = def_env.debug.timers }; | 1192 def_env.timer = { info = def_env.debug.timers }; |
| 1191 | 1193 |
| 1192 module:hook("server-stopping", function(event) | 1194 module:hook("server-stopping", function(event) |
| 1193 for conn, session in pairs(sessions) do | 1195 for _, session in pairs(sessions) do |
| 1194 session.print("Shutting down: "..(event.reason or "unknown reason")); | 1196 session.print("Shutting down: "..(event.reason or "unknown reason")); |
| 1195 end | 1197 end |
| 1196 end); | 1198 end); |
| 1197 | 1199 |
| 1198 def_env.stats = {}; | 1200 def_env.stats = {}; |
| 1223 return tostring(value); | 1225 return tostring(value); |
| 1224 end | 1226 end |
| 1225 | 1227 |
| 1226 local stats_methods = {}; | 1228 local stats_methods = {}; |
| 1227 function stats_methods:bounds(_lower, _upper) | 1229 function stats_methods:bounds(_lower, _upper) |
| 1228 local statistics = require "util.statistics"; | |
| 1229 for _, stat_info in ipairs(self) do | 1230 for _, stat_info in ipairs(self) do |
| 1230 local data = stat_info[4]; | 1231 local data = stat_info[4]; |
| 1231 if data then | 1232 if data then |
| 1232 local lower = _lower or data.min; | 1233 local lower = _lower or data.min; |
| 1233 local upper = _upper or data.max; | 1234 local upper = _upper or data.max; |
| 1238 sample_count = 0; | 1239 sample_count = 0; |
| 1239 count = data.count; | 1240 count = data.count; |
| 1240 units = data.units; | 1241 units = data.units; |
| 1241 }; | 1242 }; |
| 1242 local sum = 0; | 1243 local sum = 0; |
| 1243 for i, v in ipairs(data.samples) do | 1244 for _, v in ipairs(data.samples) do |
| 1244 if v > upper then | 1245 if v > upper then |
| 1245 break; | 1246 break; |
| 1246 elseif v>=lower then | 1247 elseif v>=lower then |
| 1247 table.insert(new_data.samples, v); | 1248 table.insert(new_data.samples, v); |
| 1248 sum = sum + v; | 1249 sum = sum + v; |
| 1270 sample_count = 0; | 1271 sample_count = 0; |
| 1271 count = data.count; | 1272 count = data.count; |
| 1272 units = data.units; | 1273 units = data.units; |
| 1273 }; | 1274 }; |
| 1274 local sum = 0; | 1275 local sum = 0; |
| 1275 for i, v in ipairs(data.samples) do | 1276 for _, v in ipairs(data.samples) do |
| 1276 if v > new_data.max then | 1277 if v > new_data.max then |
| 1277 break; | 1278 break; |
| 1278 elseif v>=new_data.min then | 1279 elseif v>=new_data.min then |
| 1279 table.insert(new_data.samples, v); | 1280 table.insert(new_data.samples, v); |
| 1280 sum = sum + v; | 1281 sum = sum + v; |
| 1414 local histogram = {}; | 1415 local histogram = {}; |
| 1415 for i = 1, n_buckets do | 1416 for i = 1, n_buckets do |
| 1416 histogram[i] = 0; | 1417 histogram[i] = 0; |
| 1417 end | 1418 end |
| 1418 local max_bin_samples = 0; | 1419 local max_bin_samples = 0; |
| 1419 for i, d in ipairs(data.samples) do | 1420 for _, d in ipairs(data.samples) do |
| 1420 local bucket = math.floor(1+(n_buckets-1)/(range/(d-data.min))); | 1421 local bucket = math.floor(1+(n_buckets-1)/(range/(d-data.min))); |
| 1421 histogram[bucket] = histogram[bucket] + 1; | 1422 histogram[bucket] = histogram[bucket] + 1; |
| 1422 if histogram[bucket] > max_bin_samples then | 1423 if histogram[bucket] > max_bin_samples then |
| 1423 max_bin_samples = histogram[bucket]; | 1424 max_bin_samples = histogram[bucket]; |
| 1424 end | 1425 end |
| 1471 local print = stats.session.print; | 1472 local print = stats.session.print; |
| 1472 for _, stat_info in ipairs(stats) do | 1473 for _, stat_info in ipairs(stats) do |
| 1473 if #stat_info.output > 0 then | 1474 if #stat_info.output > 0 then |
| 1474 print("\n#"..stat_info[1]); | 1475 print("\n#"..stat_info[1]); |
| 1475 print(""); | 1476 print(""); |
| 1476 for i, v in ipairs(stat_info.output) do | 1477 for _, v in ipairs(stat_info.output) do |
| 1477 print(v); | 1478 print(v); |
| 1478 end | 1479 end |
| 1479 print(""); | 1480 print(""); |
| 1480 else | 1481 else |
| 1481 print(("%-50s %s"):format(stat_info[1], format_stat(stat_info[2], stat_info[3]))); | 1482 print(("%-50s %s"):format(stat_info[1], format_stat(stat_info[2], stat_info[3]))); |
| 1487 local function new_stats_context(self) | 1488 local function new_stats_context(self) |
| 1488 return setmetatable({ session = self.session, stats = true }, {__index = stats_methods, __tostring = stats_tostring }); | 1489 return setmetatable({ session = self.session, stats = true }, {__index = stats_methods, __tostring = stats_tostring }); |
| 1489 end | 1490 end |
| 1490 | 1491 |
| 1491 function def_env.stats:show(filter) | 1492 function def_env.stats:show(filter) |
| 1492 local print = self.session.print; | |
| 1493 local stats, changed, extra = require "core.statsmanager".get_stats(); | 1493 local stats, changed, extra = require "core.statsmanager".get_stats(); |
| 1494 local available, displayed = 0, 0; | 1494 local available, displayed = 0, 0; |
| 1495 local displayed_stats = new_stats_context(self); | 1495 local displayed_stats = new_stats_context(self); |
| 1496 for name, value in pairs(stats) do | 1496 for name, value in pairs(stats) do |
| 1497 available = available + 1; | 1497 available = available + 1; |