diff --git a/modules/s_command.cc b/modules/s_command.cc index 5f28ab61b..fe35d868e 100644 --- a/modules/s_command.cc +++ b/modules/s_command.cc @@ -8,6 +8,8 @@ // copyright notice and this permission notice is present in all copies. The // full license for this software is available in the LICENSE file. +#include + using namespace ircd; mapi::header @@ -99,6 +101,9 @@ try execute_command(buf, user, room_id, cmd) }; + if(!html && !alt) + return; + m::send(user_room, m::me, "ircd.cmd.result", { { "msgtype", "m.text" }, @@ -114,6 +119,24 @@ catch(const std::exception &e) throw; } +static std::pair +command__dash(const mutable_buffer &buf, + const m::user &user, + const m::room &room, + const string_view &cmd); + +static std::pair +command__read(const mutable_buffer &buf, + const m::user &user, + const m::room &room, + const string_view &cmd); + +static std::pair +command__version(const mutable_buffer &buf, + const m::user &user, + const m::room &room, + const string_view &cmd); + std::pair execute_command(const mutable_buffer &buf, const m::user &user, @@ -121,6 +144,21 @@ execute_command(const mutable_buffer &buf, const string_view &cmd) try { + switch(hash(token(cmd, ' ', 0))) + { + case "version"_: + return command__version(buf, user, room, cmd); + + case "read"_: + return command__read(buf, user, room, cmd); + + case "dash"_: + return command__dash(buf, user, room, cmd); + + default: + break; + } + const string_view out{fmt::sprintf { buf, "unknown command :%s", cmd @@ -128,11 +166,94 @@ try const string_view alt { - "no alt text" + out }; return { out, alt }; } +catch(const m::error &e) +{ + const json::object &error + { + e.content + }; + + log::error + { + m::log, "Server command from %s in %s '%s' :%s :%s :%s", + string_view{room.room_id}, + string_view{user.user_id}, + cmd, + e.what(), + unquote(error.get("errcode")), + unquote(error.get("error")), + }; + + std::ostringstream out; + pubsetbuf(out, buf); + + const string_view fg[] {"#FCFCFC", "#FFFFFF"}; + const string_view bg[] {"#A01810", "#C81810"}; + const string_view sp {" "}; + out + << "
" + << "" + << "" + << sp << sp << e.what() << sp << sp + << "" + << " " + << "" + << "" + << sp << sp << unquote(error.get("errcode")) << sp << sp + << "" + << " " + << "
" + << "
"
+	    << unquote(error.get("error"))
+	    << "
" + ; + + return + { + view(out, buf), e.what() + }; +} +catch(const http::error &e) +{ + log::error + { + m::log, "Server command from %s in %s '%s' :%s :%s", + string_view{room.room_id}, + string_view{user.user_id}, + cmd, + e.what(), + e.content + }; + + std::ostringstream out; + pubsetbuf(out, buf); + + const string_view fg[] {"#FCFCFC"}; + const string_view bg[] {"#A01810"}; + const string_view sp {" "}; + out + << "
" + << "" + << "" + << sp << sp << e.what() << sp << sp + << "" + << " " + << "
" + << "
"
+	    << e.content
+	    << "
" + ; + + return + { + view(out, buf), e.what() + }; +} catch(const std::exception &e) { log::error @@ -155,3 +276,95 @@ catch(const std::exception &e) { data(buf), len } }; } + +std::pair +command__version(const mutable_buffer &buf, + const m::user &user, + const m::room &room, + const string_view &cmd) +{ + std::ostringstream out; + pubsetbuf(out, buf); + + const string_view sp {" "}; + out + << "

" + << info::name + << "

" + << "
"
+	    << info::version
+	    << "
" + ; + + const string_view alt + { + info::version + }; + + return { view(out, buf), alt }; +} + +std::pair +command__read(const mutable_buffer &buf, + const m::user &user, + const m::room &room, + const string_view &cmd) +{ + const params param{tokens_after(cmd, ' ', 0), " ", + { + "event_id", "[time]" + }}; + + const m::event::id::buf event_id + { + param["event_id"]? + m::event::id::buf{param["event_id"]}: + m::head(room) + }; + + const time_t &ms + { + param.at("[time]", ircd::time()) + }; + + m::receipt::read(room, user, event_id, ms); + return {}; +} + +std::pair +command__dash(const mutable_buffer &buf, + const m::user &user, + const m::room &room, + const string_view &cmd) +{ + std::ostringstream out; + pubsetbuf(out, buf); + + const string_view fg[] {"#E8E8E8", "#FFFFFF"}; + const string_view bg[] {"#303030", "#008000"}; + const string_view sp {" "}; + out + << "
" + << "" + << "" + << sp << sp << " CONSTRUCT STATUS " << sp << sp + << "" + << " " + << "" + << "" + << sp << sp << " OK " << sp << sp + << "" + << " " + << "
" + << "
"
+	    << " "
+	    << "
" + ; + + const string_view alt + { + "no alt text" + }; + + return { view(out, buf), alt }; +}