From 5571eefdb8e1b6de7803e90be543c24662c45346 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Tue, 23 Jul 2019 21:23:22 -0700 Subject: [PATCH] modules/m_command: Add a room ping command. --- modules/m_command.cc | 104 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 103 insertions(+), 1 deletion(-) diff --git a/modules/m_command.cc b/modules/m_command.cc index 631573fef..66bd3a9b0 100644 --- a/modules/m_command.cc +++ b/modules/m_command.cc @@ -340,6 +340,12 @@ command__read(const mutable_buffer &buf, return {}; } +static std::pair +command__ping__room(const mutable_buffer &buf, + const m::user &user, + const m::room &room, + const string_view &cmd); + std::pair command__ping(const mutable_buffer &buf, const m::user &user, @@ -353,9 +359,19 @@ command__ping(const mutable_buffer &buf, const string_view &target { - param.at("target") + param["target"] }; + const bool room_ping + { + !target + || m::valid(m::id::ROOM, target) + || m::valid(m::id::ROOM_ALIAS, target) + }; + + if(room_ping) + return command__ping__room(buf, user, room, cmd); + m::v1::version::opts opts; if(m::valid(m::id::USER, target)) opts.remote = m::user::id(target).host(); @@ -434,6 +450,92 @@ command__ping(const mutable_buffer &buf, return { view(out, buf), alt }; } +std::pair +command__ping__room(const mutable_buffer &buf, + const m::user &user, + const m::room &room, + const string_view &cmd) +{ + const params param{tokens_after(cmd, ' ', 0), " ", + { + "target" + }}; + + const string_view &target + { + param["target"] + }; + + m::feds::opts opts; + opts.op = m::feds::op::version; + opts.room_id = room.room_id; + + thread_local char tmbuf[32]; + std::ostringstream out; + pubsetbuf(out, buf); + + util::timer timer; + size_t responses{0}; + m::feds::acquire(opts, [&timer, &responses, &buf, &out] + (const auto &result) + { + ++responses; + const auto time + { + timer.at() + }; + + const string_view sp{" "}; + const string_view fg{"#e8e8e8"}; + const string_view host_bg{"#181b21"}; + const string_view online_bg{"#008000"}; + const string_view offline_bg{"#A01810"}; + const auto bg{result.eptr? offline_bg : online_bg}; + const auto status{result.eptr? "FAILED " : "ONLINE"}; + + thread_local char tmbuf[32]; + out + << " " + << " " + << sp << sp << status << sp << sp + << " " + << " " + << " " + << sp << sp << " " << result.origin << " " << sp + << " " + ; + + if(!result.eptr) + out << " " + << pretty(tmbuf, time) + << " " + << " application layer round-trip time." + << "
"; + + if(result.eptr) + out << "" + << what(result.eptr) + << "" + << "
"; + + return true; + }); + + const string_view rich + { + view(out, buf) + }; + + const string_view alt{fmt::sprintf + { + buf + size(rich), "%zu responses in %s", + responses, + pretty(tmbuf, timer.at()) + }}; + + return { view(out, buf), alt }; +} + std::pair command__dash(const mutable_buffer &buf, const m::user &user,