Comparison

plugins/mod_admin_telnet.lua @ 5710:e66bbfdf588e

mod_admin_telnet: Refactor s2s:show()
author Kim Alvefur <zash@zash.se>
date Thu, 20 Jun 2013 21:47:38 +0200
parent 5709:5557b4a4cb49
child 5770:7722372aa087
comparison
equal deleted inserted replaced
5709:5557b4a4cb49 5710:e66bbfdf588e
15 15
16 local _G = _G; 16 local _G = _G;
17 17
18 local prosody = _G.prosody; 18 local prosody = _G.prosody;
19 local hosts = prosody.hosts; 19 local hosts = prosody.hosts;
20 local incoming_s2s = prosody.incoming_s2s;
21 20
22 local console_listener = { default_port = 5582; default_mode = "*a"; interface = "127.0.0.1" }; 21 local console_listener = { default_port = 5582; default_mode = "*a"; interface = "127.0.0.1" };
23 22
24 local iterators = require "util.iterators"; 23 local iterators = require "util.iterators";
25 local keys, values = iterators.keys, iterators.values; 24 local keys, values = iterators.keys, iterators.values;
580 end 579 end
581 580
582 581
583 def_env.s2s = {}; 582 def_env.s2s = {};
584 function def_env.s2s:show(match_jid) 583 function def_env.s2s:show(match_jid)
585 local _print = self.session.print;
586 local print = self.session.print; 584 local print = self.session.print;
587 585
588 local count_in, count_out = 0,0; 586 local count_in, count_out = 0,0;
589 587 local s2s_list = { };
590 for host, host_session in pairs(hosts) do 588
591 print = function (...) _print(host); _print(...); print = _print; end 589 local s2s_sessions = module:shared"/*/s2s/sessions";
592 for remotehost, session in pairs(host_session.s2sout) do 590 for _, session in pairs(s2s_sessions) do
593 if (not match_jid) or remotehost:match(match_jid) or host:match(match_jid) then 591 local remotehost, localhost, direction;
594 count_out = count_out + 1; 592 if session.direction == "outgoing" then
595 print(session_flags(session, {" ", host, "->", remotehost})); 593 direction = "->";
596 if session.sendq then 594 count_out = count_out + 1;
597 print(" There are "..#session.sendq.." queued outgoing stanzas for this connection"); 595 remotehost, localhost = session.to_host or "?", session.from_host or "?";
596 else
597 direction = "<-";
598 count_in = count_in + 1;
599 remotehost, localhost = session.from_host or "?", session.to_host or "?";
600 end
601 local sess_lines = { l = localhost, r = remotehost,
602 session_flags(session, { "", direction, remotehost or "?",
603 "["..session.type..tostring(session):match("[a-f0-9]*$").."]" })};
604
605 if (not match_jid) or remotehost:match(match_jid) or localhost:match(match_jid) then
606 table.insert(s2s_list, sess_lines);
607 local print = function (s) table.insert(sess_lines, " "..s); end
608 if session.sendq then
609 print("There are "..#session.sendq.." queued outgoing stanzas for this connection");
610 end
611 if session.type == "s2sout_unauthed" then
612 if session.connecting then
613 print("Connection not yet established");
614 if not session.srv_hosts then
615 if not session.conn then
616 print("We do not yet have a DNS answer for this host's SRV records");
617 else
618 print("This host has no SRV records, using A record instead");
619 end
620 elseif session.srv_choice then
621 print("We are on SRV record "..session.srv_choice.." of "..#session.srv_hosts);
622 local srv_choice = session.srv_hosts[session.srv_choice];
623 print("Using "..(srv_choice.target or ".")..":"..(srv_choice.port or 5269));
624 end
625 elseif session.notopen then
626 print("The <stream> has not yet been opened");
627 elseif not session.dialback_key then
628 print("Dialback has not been initiated yet");
629 elseif session.dialback_key then
630 print("Dialback has been requested, but no result received");
598 end 631 end
599 if session.type == "s2sout_unauthed" then 632 end
600 if session.connecting then 633 if session.type == "s2sin_unauthed" then
601 print(" Connection not yet established"); 634 print("Connection not yet authenticated");
602 if not session.srv_hosts then 635 elseif session.type == "s2sin" then
603 if not session.conn then 636 for name in pairs(session.hosts) do
604 print(" We do not yet have a DNS answer for this host's SRV records"); 637 if name ~= session.from_host then
605 else 638 print("also hosts "..tostring(name));
606 print(" This host has no SRV records, using A record instead");
607 end
608 elseif session.srv_choice then
609 print(" We are on SRV record "..session.srv_choice.." of "..#session.srv_hosts);
610 local srv_choice = session.srv_hosts[session.srv_choice];
611 print(" Using "..(srv_choice.target or ".")..":"..(srv_choice.port or 5269));
612 end
613 elseif session.notopen then
614 print(" The <stream> has not yet been opened");
615 elseif not session.dialback_key then
616 print(" Dialback has not been initiated yet");
617 elseif session.dialback_key then
618 print(" Dialback has been requested, but no result received");
619 end 639 end
620 end 640 end
621 end 641 end
622 end 642 end
623 local subhost_filter = function (h) 643 end
624 return (match_jid and h:match(match_jid)); 644
625 end 645 -- Sort by local host, then remote host
626 for session in pairs(incoming_s2s) do 646 table.sort(s2s_list, function(a,b)
627 if session.to_host == host and ((not match_jid) or host:match(match_jid) 647 if a.l == b.l then return a.r < b.r; end
628 or (session.from_host and session.from_host:match(match_jid)) 648 return a.l < b.l;
629 -- Pft! is what I say to list comprehensions 649 end);
630 or (session.hosts and #array.collect(keys(session.hosts)):filter(subhost_filter)>0)) then 650 local lasthost;
631 count_in = count_in + 1; 651 for _, sess_lines in ipairs(s2s_list) do
632 print(session_flags(session, {" ", host, "<-", session.from_host or "(unknown)"})); 652 if sess_lines.l ~= lasthost then print(sess_lines.l); lasthost=sess_lines.l end
633 if session.type == "s2sin_unauthed" then 653 for _, line in ipairs(sess_lines) do print(line); end
634 print(" Connection not yet authenticated"); 654 end
635 end
636 for name in pairs(session.hosts) do
637 if name ~= session.from_host then
638 print(" also hosts "..tostring(name));
639 end
640 end
641 end
642 end
643
644 print = _print;
645 end
646
647 for session in pairs(incoming_s2s) do
648 if not session.to_host and ((not match_jid) or session.from_host and session.from_host:match(match_jid)) then
649 count_in = count_in + 1;
650 print("Other incoming s2s connections");
651 print(" (unknown) <- "..(session.from_host or "(unknown)"));
652 end
653 end
654
655 return true, "Total: "..count_out.." outgoing, "..count_in.." incoming connections"; 655 return true, "Total: "..count_out.." outgoing, "..count_in.." incoming connections";
656 end 656 end
657 657
658 local function print_subject(print, subject) 658 local function print_subject(print, subject)
659 for _, entry in ipairs(subject) do 659 for _, entry in ipairs(subject) do