2019-04-08 01:10:40 +02:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2019 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
|
|
|
|
2020-04-11 22:35:15 +02:00
|
|
|
#include <ircd/util/params.h>
|
2019-04-26 11:37:21 +02:00
|
|
|
|
2019-04-08 01:10:40 +02:00
|
|
|
using namespace ircd;
|
|
|
|
|
|
|
|
mapi::header
|
|
|
|
IRCD_MODULE
|
|
|
|
{
|
|
|
|
"Server Command"
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
handle_command(const m::event &event,
|
|
|
|
m::vm::eval &eval);
|
|
|
|
|
2019-08-10 06:27:12 +02:00
|
|
|
m::hookfn<m::vm::eval &>
|
2019-04-08 01:10:40 +02:00
|
|
|
command_hook
|
|
|
|
{
|
|
|
|
handle_command,
|
|
|
|
{
|
|
|
|
{ "_site", "vm.effect" },
|
|
|
|
{ "type", "ircd.cmd" },
|
|
|
|
{ "origin", my_host() },
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-11-08 19:29:08 +01:00
|
|
|
struct command_result
|
|
|
|
{
|
|
|
|
string_view html;
|
2020-11-12 03:15:59 +01:00
|
|
|
string_view alt;
|
2020-11-08 19:29:08 +01:00
|
|
|
string_view msgtype {"m.notice"};
|
|
|
|
};
|
|
|
|
|
|
|
|
static command_result
|
|
|
|
execute_command(const mutable_buffer &buf,
|
|
|
|
const m::user &user,
|
|
|
|
const m::room &room,
|
|
|
|
const string_view &cmd);
|
|
|
|
|
2019-04-08 01:10:40 +02:00
|
|
|
void
|
|
|
|
handle_command(const m::event &event,
|
|
|
|
m::vm::eval &eval)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const m::user user
|
|
|
|
{
|
|
|
|
at<"sender"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!m::my(user.user_id))
|
|
|
|
return;
|
|
|
|
|
|
|
|
const json::object &content
|
|
|
|
{
|
|
|
|
json::get<"content"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::user::room user_room
|
|
|
|
{
|
|
|
|
user
|
|
|
|
};
|
|
|
|
|
|
|
|
if(json::get<"room_id"_>(event) != user_room.room_id)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const m::room::id &room_id
|
|
|
|
{
|
|
|
|
unquote(content.at("room_id"))
|
|
|
|
};
|
|
|
|
|
2021-02-01 02:22:59 +01:00
|
|
|
const json::string &event_id
|
|
|
|
{
|
|
|
|
content["event_id"]
|
|
|
|
};
|
|
|
|
|
2020-11-14 07:48:11 +01:00
|
|
|
const json::string &input_
|
2019-04-08 01:10:40 +02:00
|
|
|
{
|
|
|
|
content.at("body")
|
|
|
|
};
|
|
|
|
|
2020-11-14 07:48:11 +01:00
|
|
|
if(!startswith(input_, "\\\\"))
|
2019-04-08 01:10:40 +02:00
|
|
|
return;
|
|
|
|
|
2019-08-12 09:59:00 +02:00
|
|
|
// View of the command string without prefix
|
2020-11-14 07:48:11 +01:00
|
|
|
string_view input{input_};
|
|
|
|
input = lstrip(input, "\\\\");
|
2019-08-12 09:59:00 +02:00
|
|
|
|
|
|
|
// Determine if there's a bang after the prefix; if so the response's
|
|
|
|
// sender will be the user, and will be broadcast publicly to the room.
|
|
|
|
// Otherwise the response comes from the server and is only visible in
|
|
|
|
// the user's timeline.
|
2020-11-14 07:48:11 +01:00
|
|
|
const bool public_response
|
|
|
|
{
|
|
|
|
startswith(input, '!')
|
|
|
|
};
|
|
|
|
|
|
|
|
string_view cmd{input};
|
2019-08-12 09:59:00 +02:00
|
|
|
cmd = lstrip(cmd, '!');
|
2019-04-08 01:10:40 +02:00
|
|
|
|
|
|
|
log::debug
|
|
|
|
{
|
2019-08-12 09:59:00 +02:00
|
|
|
m::log, "Server command from %s in %s public:%b :%s",
|
2019-04-08 01:10:40 +02:00
|
|
|
string_view{room_id},
|
|
|
|
string_view{user.user_id},
|
2019-08-12 09:59:00 +02:00
|
|
|
public_response,
|
2019-04-08 01:10:40 +02:00
|
|
|
cmd
|
|
|
|
};
|
|
|
|
|
|
|
|
const unique_buffer<mutable_buffer> buf
|
|
|
|
{
|
2020-11-08 19:29:08 +01:00
|
|
|
56_KiB
|
2019-04-08 01:10:40 +02:00
|
|
|
};
|
|
|
|
|
2020-11-08 19:29:08 +01:00
|
|
|
const auto &[html, alt, msgtype]
|
2019-04-08 01:10:40 +02:00
|
|
|
{
|
|
|
|
execute_command(buf, user, room_id, cmd)
|
|
|
|
};
|
|
|
|
|
2019-04-26 11:37:21 +02:00
|
|
|
if(!html && !alt)
|
|
|
|
return;
|
|
|
|
|
2019-08-12 09:59:00 +02:00
|
|
|
const auto &response_sender
|
|
|
|
{
|
2019-10-01 05:50:58 +02:00
|
|
|
public_response? user : m::user(m::me())
|
2019-08-12 09:59:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const auto &response_room
|
|
|
|
{
|
|
|
|
public_response? room_id : user_room
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto &response_type
|
|
|
|
{
|
|
|
|
public_response? "m.room.message" : "ircd.cmd.result"
|
|
|
|
};
|
|
|
|
|
2021-02-01 02:22:59 +01:00
|
|
|
const auto &response_event_id
|
|
|
|
{
|
|
|
|
public_response?
|
|
|
|
string_view{event_id}:
|
|
|
|
string_view{event.event_id}
|
|
|
|
};
|
|
|
|
|
2020-11-08 19:29:08 +01:00
|
|
|
const json::value html_val
|
|
|
|
{
|
|
|
|
html, json::STRING
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::value content_body
|
|
|
|
{
|
2020-11-12 03:15:59 +01:00
|
|
|
alt?: "no alt text"_sv, json::STRING
|
2020-11-08 19:29:08 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static const json::value undef_val
|
|
|
|
{
|
|
|
|
string_view{nullptr}, json::STRING
|
|
|
|
};
|
|
|
|
|
|
|
|
static const json::value html_format
|
|
|
|
{
|
|
|
|
"org.matrix.custom.html", json::STRING
|
|
|
|
};
|
|
|
|
|
2021-01-30 20:17:01 +01:00
|
|
|
char reply_buf[288];
|
|
|
|
const json::object in_reply_to
|
|
|
|
{
|
|
|
|
json::stringify(reply_buf, json::members
|
|
|
|
{
|
2021-02-01 02:22:59 +01:00
|
|
|
{ "event_id", event_id },
|
2021-01-30 20:17:01 +01:00
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
char relates_buf[576];
|
|
|
|
const json::object relates_to
|
|
|
|
{
|
|
|
|
json::stringify(relates_buf, json::members
|
|
|
|
{
|
2021-02-01 02:22:59 +01:00
|
|
|
{ "event_id", event_id },
|
|
|
|
{ "rel_type", "ircd.cmd" },
|
|
|
|
{ "m.in_reply_to", in_reply_to },
|
2021-01-30 20:17:01 +01:00
|
|
|
})
|
|
|
|
};
|
|
|
|
|
2019-08-12 09:59:00 +02:00
|
|
|
m::send(response_room, response_sender, response_type,
|
2019-04-08 01:10:40 +02:00
|
|
|
{
|
2020-11-08 19:29:08 +01:00
|
|
|
{ "msgtype", msgtype?: "m.notice" },
|
|
|
|
{ "format", html? html_format: undef_val },
|
|
|
|
{ "body", content_body },
|
|
|
|
{ "formatted_body", html? html_val: undef_val },
|
|
|
|
{ "room_id", room_id },
|
|
|
|
{ "input", input },
|
2021-01-30 20:17:01 +01:00
|
|
|
{ "m.relates_to", relates_to },
|
2021-01-25 09:57:13 +01:00
|
|
|
});
|
2019-04-08 01:10:40 +02:00
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2020-11-12 03:16:48 +01:00
|
|
|
const json::object &content
|
|
|
|
{
|
|
|
|
json::get<"content"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::string &room_id
|
|
|
|
{
|
|
|
|
content["room_id"]
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::string &input
|
|
|
|
{
|
|
|
|
content["body"]
|
|
|
|
};
|
|
|
|
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
m::log, "Command in %s in %s by %s '%s' :%s",
|
|
|
|
string_view{event.event_id},
|
|
|
|
room_id,
|
|
|
|
at<"sender"_>(event),
|
|
|
|
input,
|
|
|
|
e.what(),
|
|
|
|
};
|
2019-04-08 01:10:40 +02:00
|
|
|
}
|
|
|
|
|
2020-11-09 18:19:10 +01:00
|
|
|
static command_result
|
|
|
|
command__caption(const mutable_buffer &buf,
|
|
|
|
const m::user &user,
|
|
|
|
const m::room &room,
|
|
|
|
const string_view &cmd);
|
|
|
|
|
2020-11-14 08:17:19 +01:00
|
|
|
static command_result
|
|
|
|
command__edit(const mutable_buffer &buf,
|
|
|
|
const m::user &user,
|
|
|
|
const m::room &room,
|
|
|
|
const string_view &cmd);
|
|
|
|
|
2020-11-08 19:29:08 +01:00
|
|
|
static command_result
|
2019-04-28 00:39:11 +02:00
|
|
|
command__ping(const mutable_buffer &buf,
|
|
|
|
const m::user &user,
|
|
|
|
const m::room &room,
|
|
|
|
const string_view &cmd);
|
|
|
|
|
2020-11-08 19:29:08 +01:00
|
|
|
static command_result
|
2019-04-26 11:37:21 +02:00
|
|
|
command__dash(const mutable_buffer &buf,
|
|
|
|
const m::user &user,
|
|
|
|
const m::room &room,
|
|
|
|
const string_view &cmd);
|
|
|
|
|
2020-11-08 19:29:08 +01:00
|
|
|
static command_result
|
2019-04-26 11:37:21 +02:00
|
|
|
command__read(const mutable_buffer &buf,
|
|
|
|
const m::user &user,
|
|
|
|
const m::room &room,
|
|
|
|
const string_view &cmd);
|
|
|
|
|
2020-11-08 19:29:08 +01:00
|
|
|
static command_result
|
2019-04-26 11:37:21 +02:00
|
|
|
command__version(const mutable_buffer &buf,
|
|
|
|
const m::user &user,
|
|
|
|
const m::room &room,
|
|
|
|
const string_view &cmd);
|
|
|
|
|
2020-11-22 00:24:30 +01:00
|
|
|
static command_result
|
|
|
|
command__control(const mutable_buffer &buf,
|
|
|
|
const m::user &user,
|
|
|
|
const m::room &room,
|
|
|
|
const string_view &cmd);
|
|
|
|
|
2020-11-08 19:29:08 +01:00
|
|
|
command_result
|
2019-04-08 01:10:40 +02:00
|
|
|
execute_command(const mutable_buffer &buf,
|
|
|
|
const m::user &user,
|
|
|
|
const m::room &room,
|
|
|
|
const string_view &cmd)
|
|
|
|
try
|
|
|
|
{
|
2020-11-22 00:24:30 +01:00
|
|
|
if(startswith(cmd, '#'))
|
|
|
|
return command__control(buf, user, room, lstrip(cmd, '#'));
|
|
|
|
|
2019-04-26 11:37:21 +02:00
|
|
|
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);
|
|
|
|
|
2019-04-28 00:39:11 +02:00
|
|
|
case "ping"_:
|
|
|
|
return command__ping(buf, user, room, cmd);
|
|
|
|
|
2020-11-14 08:17:19 +01:00
|
|
|
case "edit"_:
|
|
|
|
return command__edit(buf, user, room, cmd);
|
|
|
|
|
2020-11-09 18:19:10 +01:00
|
|
|
case "caption"_:
|
|
|
|
return command__caption(buf, user, room, cmd);
|
|
|
|
|
2020-11-22 00:24:30 +01:00
|
|
|
case "control"_:
|
|
|
|
{
|
|
|
|
const auto subcmd
|
|
|
|
{
|
|
|
|
tokens_after(cmd, ' ', 0)
|
|
|
|
};
|
|
|
|
|
|
|
|
return command__control(buf, user, room, subcmd);
|
|
|
|
}
|
|
|
|
|
2019-04-26 11:37:21 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-04-08 01:10:40 +02:00
|
|
|
const string_view out{fmt::sprintf
|
|
|
|
{
|
|
|
|
buf, "unknown command :%s", cmd
|
|
|
|
}};
|
|
|
|
|
|
|
|
const string_view alt
|
|
|
|
{
|
2019-04-26 11:37:21 +02:00
|
|
|
out
|
2019-04-08 01:10:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return { out, alt };
|
|
|
|
}
|
2019-04-26 11:37:21 +02:00
|
|
|
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
|
|
|
|
<< "<h5>"
|
|
|
|
<< "<font color=\"" << fg[0] << "\" data-mx-bg-color=\"" << bg[0] << "\">"
|
|
|
|
<< "<b>"
|
2020-11-14 08:36:54 +01:00
|
|
|
//<< sp << sp << e.what() << sp << sp
|
2019-04-26 11:37:21 +02:00
|
|
|
<< "</b>"
|
|
|
|
<< "</font> "
|
|
|
|
<< "<font color=\"" << fg[1] << "\" data-mx-bg-color=\"" << bg[1] << "\">"
|
|
|
|
<< "<b>"
|
|
|
|
<< sp << sp << unquote(error.get("errcode")) << sp << sp
|
|
|
|
<< "</b>"
|
|
|
|
<< "</font> "
|
|
|
|
<< "</h5>"
|
|
|
|
<< "<pre>"
|
|
|
|
<< unquote(error.get("error"))
|
|
|
|
<< "</pre>"
|
|
|
|
;
|
|
|
|
|
|
|
|
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
|
|
|
|
<< "<h5>"
|
|
|
|
<< "<font color=\"" << fg[0] << "\" data-mx-bg-color=\"" << bg[0] << "\">"
|
|
|
|
<< "<b>"
|
|
|
|
<< sp << sp << e.what() << sp << sp
|
|
|
|
<< "</b>"
|
|
|
|
<< "</font> "
|
|
|
|
<< "</h5>"
|
|
|
|
<< "<pre>"
|
|
|
|
<< e.content
|
|
|
|
<< "</pre>"
|
|
|
|
;
|
|
|
|
|
|
|
|
return
|
|
|
|
{
|
|
|
|
view(out, buf), e.what()
|
|
|
|
};
|
|
|
|
}
|
2019-04-08 01:10:40 +02:00
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
m::log, "Server command from %s in %s '%s' :%s",
|
|
|
|
string_view{room.room_id},
|
|
|
|
string_view{user.user_id},
|
|
|
|
cmd,
|
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
|
|
|
|
const size_t len
|
|
|
|
{
|
|
|
|
copy(buf, string_view(e.what()))
|
|
|
|
};
|
|
|
|
|
|
|
|
return
|
|
|
|
{
|
|
|
|
{ data(buf), len },
|
|
|
|
{ data(buf), len }
|
|
|
|
};
|
|
|
|
}
|
2019-04-26 11:37:21 +02:00
|
|
|
|
2020-11-08 19:29:08 +01:00
|
|
|
command_result
|
2019-04-26 11:37:21 +02:00
|
|
|
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
|
|
|
|
<< "<h1>"
|
|
|
|
<< info::name
|
|
|
|
<< "</h1>"
|
|
|
|
<< "<pre><code>"
|
|
|
|
<< info::version
|
|
|
|
<< "</code></pre>"
|
|
|
|
;
|
|
|
|
|
|
|
|
const string_view alt
|
|
|
|
{
|
|
|
|
info::version
|
|
|
|
};
|
|
|
|
|
|
|
|
return { view(out, buf), alt };
|
|
|
|
}
|
|
|
|
|
2020-11-08 19:29:08 +01:00
|
|
|
command_result
|
2019-04-26 11:37:21 +02:00
|
|
|
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), " ",
|
|
|
|
{
|
2019-08-24 00:11:28 +02:00
|
|
|
"arg", "[time]"
|
2019-04-26 11:37:21 +02:00
|
|
|
}};
|
|
|
|
|
2019-08-24 00:11:28 +02:00
|
|
|
const string_view &arg
|
2019-04-26 11:37:21 +02:00
|
|
|
{
|
2019-08-24 00:11:28 +02:00
|
|
|
param["arg"]
|
2019-04-26 11:37:21 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const time_t &ms
|
|
|
|
{
|
|
|
|
param.at("[time]", ircd::time<milliseconds>())
|
|
|
|
};
|
|
|
|
|
2019-08-29 02:25:00 +02:00
|
|
|
std::ostringstream out;
|
|
|
|
pubsetbuf(out, buf);
|
|
|
|
|
2019-08-24 00:11:28 +02:00
|
|
|
if(m::valid(m::id::EVENT, arg))
|
|
|
|
{
|
|
|
|
const m::event::id::buf event_id
|
|
|
|
{
|
|
|
|
arg?
|
|
|
|
m::event::id::buf{arg}:
|
|
|
|
m::head(room)
|
|
|
|
};
|
|
|
|
|
2020-10-28 06:25:20 +01:00
|
|
|
const json::strung content{json::members
|
2019-09-06 20:13:58 +02:00
|
|
|
{
|
|
|
|
{ "ts", ms },
|
2020-10-28 06:25:20 +01:00
|
|
|
}};
|
2019-09-06 20:13:58 +02:00
|
|
|
|
2020-10-28 06:25:20 +01:00
|
|
|
m::receipt::read(room, user, event_id, json::object(content));
|
2019-08-24 00:11:28 +02:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
else if(m::valid(m::id::ROOM, arg) || m::valid(m::id::ROOM_ALIAS, arg))
|
|
|
|
{
|
|
|
|
const auto room_id
|
|
|
|
{
|
|
|
|
m::room_id(arg)
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::room room
|
|
|
|
{
|
|
|
|
room_id
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::event::id::buf event_id
|
|
|
|
{
|
|
|
|
m::head(room)
|
|
|
|
};
|
|
|
|
|
2020-10-28 06:25:20 +01:00
|
|
|
const json::strung content{json::members
|
2019-09-06 20:13:58 +02:00
|
|
|
{
|
|
|
|
{ "ts", ms },
|
2020-10-28 06:25:20 +01:00
|
|
|
}};
|
2019-09-06 20:13:58 +02:00
|
|
|
|
2020-10-28 06:25:20 +01:00
|
|
|
m::receipt::read(room, user, event_id, json::object(content));
|
2019-08-24 00:11:28 +02:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-08-29 02:25:00 +02:00
|
|
|
// We don't accept no-argument as a wildcard to prevent a naive user
|
|
|
|
// just probing the command interface from performing the following large
|
|
|
|
// operation on all their rooms...
|
|
|
|
if(!arg)
|
2019-08-24 00:11:28 +02:00
|
|
|
return {};
|
|
|
|
|
2019-08-29 02:25:00 +02:00
|
|
|
// The string argument can be a globular expression of room tags, like
|
|
|
|
// `m.*` or just `*`.
|
2020-03-19 19:29:11 +01:00
|
|
|
const globular_imatch match
|
2019-08-29 02:25:00 +02:00
|
|
|
{
|
|
|
|
arg
|
|
|
|
};
|
|
|
|
|
|
|
|
// Iterate all joined rooms for a user. For each room we'll test if the
|
|
|
|
// room's tag matches the arg string globular expression.
|
2019-08-24 00:11:28 +02:00
|
|
|
const m::user::rooms user_rooms
|
|
|
|
{
|
|
|
|
user
|
|
|
|
};
|
|
|
|
|
2019-08-29 02:25:00 +02:00
|
|
|
const string_view fg[] {"#FFFFFF"};
|
|
|
|
const string_view bg[] {"#000000"};
|
|
|
|
out
|
|
|
|
<< "<pre>"
|
|
|
|
<< "<font color=\"" << fg[0] << "\" data-mx-bg-color=\"" << bg[0] << "\">"
|
|
|
|
<< "<table>"
|
|
|
|
;
|
|
|
|
|
|
|
|
size_t matched(0);
|
|
|
|
user_rooms.for_each("join", [&user, &ms, &match, &out, &matched]
|
2019-08-24 00:11:28 +02:00
|
|
|
(const m::room::id &room_id, const string_view &)
|
|
|
|
{
|
2019-08-29 02:25:00 +02:00
|
|
|
const m::user::room_tags room_tags
|
2019-08-24 00:11:28 +02:00
|
|
|
{
|
2019-08-29 02:25:00 +02:00
|
|
|
user, room_id
|
|
|
|
};
|
|
|
|
|
|
|
|
// return true if the expression is not matched for this room.
|
|
|
|
const auto without_match{[&match]
|
2022-06-24 04:18:05 +02:00
|
|
|
(const string_view &key, const json::object &object) noexcept
|
2019-08-29 02:25:00 +02:00
|
|
|
{
|
|
|
|
return !match(key);
|
|
|
|
}};
|
|
|
|
|
|
|
|
// for_each returns true if it didn't break from the loop, which means
|
|
|
|
// no match and skip actions for this room.
|
2020-04-27 01:38:01 +02:00
|
|
|
if(match.a != "*")
|
2019-08-29 02:25:00 +02:00
|
|
|
if(room_tags.for_each(without_match))
|
2019-08-24 00:11:28 +02:00
|
|
|
return;
|
|
|
|
|
2019-08-29 02:25:00 +02:00
|
|
|
// Get the room head (if there are multiple, the best is selected for
|
|
|
|
// us) which will be the target of our receipt.
|
2019-08-24 00:11:28 +02:00
|
|
|
const m::event::id::buf event_id
|
|
|
|
{
|
|
|
|
m::head(std::nothrow, room_id)
|
|
|
|
};
|
|
|
|
|
2019-08-29 02:25:00 +02:00
|
|
|
// Nothing to send a receipt for.
|
2019-08-24 00:11:28 +02:00
|
|
|
if(!event_id)
|
|
|
|
return;
|
|
|
|
|
2019-08-29 02:25:00 +02:00
|
|
|
const auto put{[&out]
|
|
|
|
(const string_view &room_id, const string_view &event_id)
|
|
|
|
{
|
|
|
|
out
|
|
|
|
<< "<tr>"
|
|
|
|
<< "<td>"
|
|
|
|
<< "<b>"
|
|
|
|
<< room_id
|
|
|
|
<< "</b>"
|
|
|
|
<< "</td>"
|
|
|
|
<< "<td>"
|
|
|
|
<< event_id
|
|
|
|
<< "</td>"
|
|
|
|
<< "</tr>"
|
|
|
|
;
|
|
|
|
}};
|
|
|
|
|
|
|
|
// Check if event_id is more recent than the last receipt's event_id.
|
|
|
|
if(!m::receipt::freshest(room_id, user, event_id))
|
|
|
|
{
|
|
|
|
put(room_id, "You already read this or a later event in the room.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if user wants to prevent sending receipts to this room.
|
|
|
|
if(m::receipt::ignoring(user, room_id))
|
|
|
|
{
|
|
|
|
put(room_id, "You have configured to not send receipts to this room.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if user wants to prevent based on this event's specifics.
|
|
|
|
if(m::receipt::ignoring(user, event_id))
|
|
|
|
{
|
|
|
|
put(room_id, "You have configured to not send receipts for this event.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commit the receipt.
|
2020-10-28 06:25:20 +01:00
|
|
|
const json::strung content{json::members
|
2019-09-06 20:13:58 +02:00
|
|
|
{
|
2019-09-06 20:51:45 +02:00
|
|
|
{ "ts", ms },
|
|
|
|
{ "m.hidden", true },
|
2020-10-28 06:25:20 +01:00
|
|
|
}};
|
2019-09-06 20:13:58 +02:00
|
|
|
|
2020-10-28 06:25:20 +01:00
|
|
|
m::receipt::read(room_id, user, event_id, json::object(content));
|
2019-08-29 02:25:00 +02:00
|
|
|
put(room_id, event_id);
|
|
|
|
++matched;
|
2019-08-24 00:11:28 +02:00
|
|
|
});
|
|
|
|
|
2019-08-29 02:25:00 +02:00
|
|
|
out
|
|
|
|
<< "</table>"
|
|
|
|
<< "</font>"
|
|
|
|
<< "<br />*** Marked "
|
|
|
|
<< matched
|
|
|
|
<< " rooms as read.<br />"
|
|
|
|
<< "</pre>"
|
|
|
|
;
|
|
|
|
|
|
|
|
return
|
|
|
|
{
|
|
|
|
view(out, buf), "TODO: alt text."
|
|
|
|
};
|
2019-04-26 11:37:21 +02:00
|
|
|
}
|
|
|
|
|
2020-11-08 19:29:08 +01:00
|
|
|
static command_result
|
2019-07-24 06:23:22 +02:00
|
|
|
command__ping__room(const mutable_buffer &buf,
|
|
|
|
const m::user &user,
|
|
|
|
const m::room &room,
|
|
|
|
const string_view &cmd);
|
|
|
|
|
2020-11-08 19:29:08 +01:00
|
|
|
command_result
|
2019-04-28 00:39:11 +02:00
|
|
|
command__ping(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
|
|
|
|
{
|
2019-07-24 06:23:22 +02:00
|
|
|
param["target"]
|
|
|
|
};
|
|
|
|
|
|
|
|
const bool room_ping
|
|
|
|
{
|
|
|
|
!target
|
|
|
|
|| m::valid(m::id::ROOM, target)
|
|
|
|
|| m::valid(m::id::ROOM_ALIAS, target)
|
2019-04-28 00:39:11 +02:00
|
|
|
};
|
|
|
|
|
2019-07-24 06:23:22 +02:00
|
|
|
if(room_ping)
|
|
|
|
return command__ping__room(buf, user, room, cmd);
|
|
|
|
|
2020-03-06 04:35:20 +01:00
|
|
|
m::fed::version::opts opts;
|
2019-04-28 00:39:11 +02:00
|
|
|
if(m::valid(m::id::USER, target))
|
|
|
|
opts.remote = m::user::id(target).host();
|
|
|
|
else
|
|
|
|
opts.remote = target;
|
|
|
|
|
|
|
|
const unique_buffer<mutable_buffer> http_buf
|
|
|
|
{
|
|
|
|
8_KiB
|
|
|
|
};
|
|
|
|
|
|
|
|
util::timer timer;
|
2020-03-06 04:35:20 +01:00
|
|
|
m::fed::version request
|
2019-04-28 00:39:11 +02:00
|
|
|
{
|
|
|
|
http_buf, std::move(opts)
|
|
|
|
};
|
|
|
|
|
2019-05-16 12:43:06 +02:00
|
|
|
std::exception_ptr eptr; try
|
|
|
|
{
|
|
|
|
request.wait(seconds(10));
|
|
|
|
const auto code(request.get());
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
eptr = std::current_exception();
|
|
|
|
}
|
2019-04-28 00:39:11 +02:00
|
|
|
|
2019-05-16 12:43:06 +02:00
|
|
|
const auto time
|
|
|
|
{
|
2020-10-24 10:18:37 +02:00
|
|
|
timer.at<milliseconds>()
|
2019-05-16 12:43:06 +02:00
|
|
|
};
|
|
|
|
|
2019-07-24 08:34:17 +02:00
|
|
|
static const string_view
|
|
|
|
sp{" "}, fg{"#e8e8e8"}, host_bg{"#181b21"},
|
|
|
|
online_bg{"#008000"}, offline_bg{"#A01810"};
|
|
|
|
|
|
|
|
const string_view
|
|
|
|
bg{eptr? offline_bg : online_bg},
|
|
|
|
status{eptr? "FAILED " : "ONLINE"};
|
2019-04-28 00:39:11 +02:00
|
|
|
|
|
|
|
std::ostringstream out;
|
|
|
|
pubsetbuf(out, buf);
|
2020-12-21 15:43:15 +01:00
|
|
|
char tmbuf[32];
|
2019-04-28 00:39:11 +02:00
|
|
|
out
|
2019-05-16 12:43:06 +02:00
|
|
|
<< " <font color=\"" << fg << "\" data-mx-bg-color=\"" << bg << "\">"
|
|
|
|
<< " <b>"
|
|
|
|
<< sp << sp << status << sp << sp
|
|
|
|
<< " </b>"
|
|
|
|
<< " </font>"
|
|
|
|
<< " <font color=\"" << fg << "\" data-mx-bg-color=\"" << host_bg << "\">"
|
|
|
|
<< sp << sp << " " << target << " " << sp
|
|
|
|
<< " </font> "
|
2019-04-28 00:39:11 +02:00
|
|
|
;
|
|
|
|
|
2019-05-16 12:43:06 +02:00
|
|
|
if(!eptr)
|
|
|
|
out << " <b>"
|
|
|
|
<< pretty(tmbuf, time)
|
|
|
|
<< " </b>"
|
|
|
|
<< " application layer round-trip time.";
|
|
|
|
|
|
|
|
if(eptr)
|
|
|
|
out << "<pre>"
|
|
|
|
<< what(eptr)
|
|
|
|
<< "</pre>";
|
|
|
|
|
2019-04-28 00:39:11 +02:00
|
|
|
const string_view rich
|
|
|
|
{
|
|
|
|
view(out, buf)
|
|
|
|
};
|
|
|
|
|
|
|
|
const string_view alt{fmt::sprintf
|
|
|
|
{
|
|
|
|
buf + size(rich), "response in %s", pretty(tmbuf, time)
|
|
|
|
}};
|
|
|
|
|
|
|
|
return { view(out, buf), alt };
|
|
|
|
}
|
|
|
|
|
2020-11-08 19:29:08 +01:00
|
|
|
command_result
|
2019-07-24 06:23:22 +02:00
|
|
|
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;
|
2019-07-24 07:38:09 +02:00
|
|
|
opts.closure_cached_errors = true;
|
|
|
|
opts.timeout = seconds(10); //TODO: conf
|
2019-07-24 06:23:22 +02:00
|
|
|
|
2020-12-21 15:43:15 +01:00
|
|
|
char tmbuf[32];
|
2019-07-24 06:23:22 +02:00
|
|
|
std::ostringstream out;
|
|
|
|
pubsetbuf(out, buf);
|
|
|
|
|
|
|
|
util::timer timer;
|
|
|
|
size_t responses{0};
|
2020-12-21 15:43:15 +01:00
|
|
|
m::feds::execute(opts, [&tmbuf, &timer, &responses, &buf, &out]
|
2019-07-24 06:23:22 +02:00
|
|
|
(const auto &result)
|
|
|
|
{
|
|
|
|
++responses;
|
2019-07-24 08:34:17 +02:00
|
|
|
|
|
|
|
static const string_view
|
|
|
|
sp{" "}, fg{"#e8e8e8"}, host_bg{"#181b21"},
|
|
|
|
online_bg{"#008000"}, offline_bg{"#A01810"};
|
|
|
|
|
|
|
|
const string_view
|
|
|
|
bg{result.eptr? offline_bg : online_bg},
|
|
|
|
status{result.eptr? "FAILED " : "ONLINE"};
|
|
|
|
|
2020-10-24 10:18:37 +02:00
|
|
|
assert(result.origin);
|
2019-07-24 06:23:22 +02:00
|
|
|
out
|
|
|
|
<< " <font color=\"" << fg << "\" data-mx-bg-color=\"" << bg << "\">"
|
|
|
|
<< " <b>"
|
|
|
|
<< sp << sp << status << sp << sp
|
|
|
|
<< " </b>"
|
|
|
|
<< " </font>"
|
|
|
|
<< " <font color=\"" << fg << "\" data-mx-bg-color=\"" << host_bg << "\">"
|
|
|
|
<< sp << sp << " " << result.origin << " " << sp
|
|
|
|
<< " </font> "
|
|
|
|
;
|
|
|
|
|
|
|
|
if(!result.eptr)
|
|
|
|
out << " <b>"
|
2020-10-24 10:18:37 +02:00
|
|
|
<< pretty(tmbuf, timer.at<milliseconds>())
|
2019-07-24 06:23:22 +02:00
|
|
|
<< " </b>"
|
|
|
|
<< " application layer round-trip time."
|
|
|
|
<< "<br />";
|
|
|
|
|
|
|
|
if(result.eptr)
|
|
|
|
out << "<code>"
|
|
|
|
<< what(result.eptr)
|
|
|
|
<< "</code>"
|
|
|
|
<< "<br />";
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
const string_view rich
|
|
|
|
{
|
|
|
|
view(out, buf)
|
|
|
|
};
|
|
|
|
|
|
|
|
const string_view alt{fmt::sprintf
|
|
|
|
{
|
|
|
|
buf + size(rich), "%zu responses in %s",
|
|
|
|
responses,
|
2020-10-24 10:18:37 +02:00
|
|
|
pretty(tmbuf, timer.at<milliseconds>())
|
2019-07-24 06:23:22 +02:00
|
|
|
}};
|
|
|
|
|
|
|
|
return { view(out, buf), alt };
|
|
|
|
}
|
|
|
|
|
2020-11-08 19:29:08 +01:00
|
|
|
command_result
|
2019-04-26 11:37:21 +02:00
|
|
|
command__dash(const mutable_buffer &buf,
|
|
|
|
const m::user &user,
|
|
|
|
const m::room &room,
|
|
|
|
const string_view &cmd)
|
|
|
|
{
|
|
|
|
std::ostringstream out;
|
|
|
|
pubsetbuf(out, buf);
|
|
|
|
|
2019-05-16 12:43:06 +02:00
|
|
|
const string_view fg[] {"#3EA6FF", "#FFFFFF"};
|
|
|
|
const string_view bg[] {"#000000", "#008000"};
|
2019-04-26 11:37:21 +02:00
|
|
|
const string_view sp {" "};
|
|
|
|
out
|
|
|
|
<< "<h5>"
|
|
|
|
<< "<font color=\"" << fg[0] << "\" data-mx-bg-color=\"" << bg[0] << "\">"
|
|
|
|
<< "<b>"
|
|
|
|
<< sp << sp << " CONSTRUCT STATUS " << sp << sp
|
|
|
|
<< "</b>"
|
|
|
|
<< "</font> "
|
|
|
|
<< "<font color=\"" << fg[1] << "\" data-mx-bg-color=\"" << bg[1] << "\">"
|
|
|
|
<< "<b>"
|
|
|
|
<< sp << sp << " OK " << sp << sp
|
|
|
|
<< "</b>"
|
|
|
|
<< "</font> "
|
|
|
|
<< "</h5>"
|
|
|
|
<< "<pre>"
|
|
|
|
<< " "
|
|
|
|
<< "</pre>"
|
|
|
|
;
|
|
|
|
|
|
|
|
const string_view alt
|
|
|
|
{
|
|
|
|
"no alt text"
|
|
|
|
};
|
|
|
|
|
|
|
|
return { view(out, buf), alt };
|
|
|
|
}
|
2020-11-09 18:19:10 +01:00
|
|
|
|
2020-11-14 08:17:19 +01:00
|
|
|
static void
|
|
|
|
handle_edit(const m::event &event,
|
|
|
|
m::vm::eval &eval);
|
|
|
|
|
|
|
|
m::hookfn<m::vm::eval &>
|
|
|
|
edit_hook
|
|
|
|
{
|
|
|
|
handle_edit,
|
|
|
|
{
|
|
|
|
{ "_site", "vm.eval" },
|
|
|
|
{ "type", "m.room.message" },
|
|
|
|
{ "content",
|
|
|
|
{
|
|
|
|
{ "msgtype", "m.text" }
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
conf::item<std::string>
|
|
|
|
edit_path
|
|
|
|
{
|
|
|
|
{ "name", "ircd.m.cmd.edit.path" },
|
|
|
|
{ "default", string_view{} },
|
|
|
|
};
|
|
|
|
|
2020-11-19 20:57:04 +01:00
|
|
|
conf::item<std::string>
|
|
|
|
edit_whitelist
|
|
|
|
{
|
|
|
|
{ "name", "ircd.m.cmd.edit.whitelist" },
|
|
|
|
{ "default", string_view{} },
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool
|
|
|
|
edit_whitelisted(const m::user::id &user_id)
|
|
|
|
{
|
|
|
|
bool ret(false);
|
|
|
|
ircd::tokens(edit_whitelist, ' ', [&user_id, &ret]
|
|
|
|
(const string_view &whitelisted_user_id)
|
|
|
|
{
|
|
|
|
ret |= whitelisted_user_id == user_id;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-11-14 08:17:19 +01:00
|
|
|
command_result
|
|
|
|
command__edit(const mutable_buffer &buf_,
|
|
|
|
const m::user &user,
|
|
|
|
const m::room &room,
|
|
|
|
const string_view &cmd)
|
|
|
|
{
|
|
|
|
const params param
|
|
|
|
{
|
|
|
|
tokens_after(cmd, ' ', 0), " ",
|
|
|
|
{
|
|
|
|
"path"
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!edit_path)
|
|
|
|
throw m::UNAVAILABLE
|
|
|
|
{
|
|
|
|
"Configure the 'ircd.m.cmd.edit.path' to use this feature.",
|
|
|
|
};
|
|
|
|
|
2020-11-19 20:57:04 +01:00
|
|
|
if(!edit_whitelisted(user))
|
|
|
|
throw m::ACCESS_DENIED
|
|
|
|
{
|
|
|
|
"'%s' is not listed in the 'ircd.m.cmd.edit.whitelist'.",
|
|
|
|
string_view{user.user_id},
|
|
|
|
};
|
|
|
|
|
2020-11-15 11:24:07 +01:00
|
|
|
const string_view parts[2]
|
2020-11-14 08:17:19 +01:00
|
|
|
{
|
2020-11-15 11:24:07 +01:00
|
|
|
edit_path, param["path"]
|
2020-11-14 08:17:19 +01:00
|
|
|
};
|
|
|
|
|
2020-11-15 11:24:07 +01:00
|
|
|
const string_view path
|
2020-11-14 08:17:19 +01:00
|
|
|
{
|
2020-11-15 11:24:07 +01:00
|
|
|
fs::path(buf_, edit_path, parts)
|
2020-11-14 08:17:19 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const fs::fd fd
|
|
|
|
{
|
|
|
|
path
|
|
|
|
};
|
|
|
|
|
|
|
|
const std::string content
|
|
|
|
{
|
|
|
|
fs::read(fd)
|
|
|
|
};
|
|
|
|
|
|
|
|
mutable_buffer buf(buf_);
|
|
|
|
consume(buf, copy(buf, "<pre><code>"_sv));
|
|
|
|
consume(buf, size(replace(buf, content, '<', "<"_sv)));
|
|
|
|
consume(buf, copy(buf, "</code></pre>"_sv));
|
|
|
|
if(empty(buf))
|
|
|
|
return
|
|
|
|
{
|
|
|
|
{}, "File too large.", "m.notice",
|
|
|
|
};
|
|
|
|
|
|
|
|
const string_view html
|
|
|
|
{
|
|
|
|
data(buf_), data(buf)
|
|
|
|
};
|
|
|
|
|
|
|
|
return
|
|
|
|
{
|
|
|
|
html, {}, "m.text"
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
handle_edit(const m::event &event,
|
|
|
|
m::vm::eval &eval)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if(!edit_path)
|
|
|
|
return;
|
|
|
|
|
2020-11-19 20:57:04 +01:00
|
|
|
if(!edit_whitelisted(json::get<"sender"_>(event)))
|
|
|
|
return;
|
|
|
|
|
2020-11-14 08:17:19 +01:00
|
|
|
const json::object &content
|
|
|
|
{
|
|
|
|
json::get<"content"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::relates_to relates_to
|
|
|
|
{
|
|
|
|
content["m.relates_to"]
|
|
|
|
};
|
|
|
|
|
|
|
|
if(json::get<"rel_type"_>(relates_to) != "m.replace")
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(!m::valid(m::id::EVENT, json::get<"event_id"_>(relates_to)))
|
|
|
|
return;
|
|
|
|
|
|
|
|
const m::event::fetch relates_event
|
|
|
|
{
|
|
|
|
std::nothrow, json::get<"event_id"_>(relates_to)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!relates_event.valid)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(json::get<"sender"_>(relates_event) != json::get<"sender"_>(event))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(json::get<"room_id"_>(relates_event) != json::get<"room_id"_>(event))
|
|
|
|
return;
|
|
|
|
|
|
|
|
const json::object &relates_content
|
|
|
|
{
|
|
|
|
json::get<"content"_>(relates_event)
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::string &input
|
|
|
|
{
|
|
|
|
relates_content["input"]
|
|
|
|
};
|
|
|
|
|
|
|
|
const string_view cmd_input
|
|
|
|
{
|
|
|
|
lstrip(input, '!')
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!startswith(cmd_input, "edit"))
|
|
|
|
return;
|
|
|
|
|
|
|
|
const auto &[cmd, args]
|
|
|
|
{
|
|
|
|
split(cmd_input, ' ')
|
|
|
|
};
|
|
|
|
|
|
|
|
const bool pub_cmd
|
|
|
|
{
|
|
|
|
startswith(input, '!')
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::object &new_content
|
|
|
|
{
|
|
|
|
content["m.new_content"]
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::string new_body
|
|
|
|
{
|
|
|
|
new_content["body"]
|
|
|
|
};
|
|
|
|
|
|
|
|
const unique_mutable_buffer body_buf
|
|
|
|
{
|
|
|
|
size(new_body), simd::alignment
|
|
|
|
};
|
|
|
|
|
|
|
|
string_view body(new_body);
|
|
|
|
body = strip(body, "```");
|
|
|
|
body = lstrip(body, "\\n"_sv);
|
|
|
|
body = json::unescape(body_buf, body);
|
|
|
|
if(!body)
|
|
|
|
return;
|
|
|
|
|
2020-11-15 11:24:07 +01:00
|
|
|
const string_view path_parts[2]
|
2020-11-14 08:17:19 +01:00
|
|
|
{
|
2020-11-15 11:24:07 +01:00
|
|
|
edit_path, args
|
2020-11-14 08:17:19 +01:00
|
|
|
};
|
|
|
|
|
2020-11-15 11:24:07 +01:00
|
|
|
const std::string path
|
2020-11-14 08:17:19 +01:00
|
|
|
{
|
2020-11-15 11:24:07 +01:00
|
|
|
fs::path(fs::path_scratch, edit_path, path_parts)
|
2020-11-14 08:17:19 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
fs::write_opts wopts;
|
|
|
|
const const_buffer written
|
|
|
|
{
|
|
|
|
fs::overwrite(path, body, wopts)
|
|
|
|
};
|
|
|
|
|
|
|
|
log::info
|
|
|
|
{
|
|
|
|
m::log, "Edit %s in %s by %s to `%s' wrote %zu/%zu bytes",
|
|
|
|
string_view{event.event_id},
|
|
|
|
json::get<"room_id"_>(event),
|
|
|
|
json::get<"sender"_>(event),
|
|
|
|
path,
|
|
|
|
size(written),
|
|
|
|
size(body),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
catch(const ctx::interrupted &)
|
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
m::log, "Edit %s in %s by %s failed :%s",
|
|
|
|
string_view{event.event_id},
|
|
|
|
json::get<"room_id"_>(event),
|
|
|
|
json::get<"sender"_>(event),
|
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-11-09 18:19:10 +01:00
|
|
|
command_result
|
|
|
|
command__caption(const mutable_buffer &buf,
|
|
|
|
const m::user &user,
|
|
|
|
const m::room &room,
|
|
|
|
const string_view &cmd)
|
|
|
|
{
|
|
|
|
const params param{tokens_after(cmd, ' ', 0), " ",
|
|
|
|
{
|
|
|
|
"url",
|
|
|
|
}};
|
|
|
|
|
|
|
|
const string_view caption
|
|
|
|
{
|
|
|
|
tokens_after(cmd, ' ', 1)
|
|
|
|
};
|
|
|
|
|
|
|
|
std::ostringstream out;
|
|
|
|
pubsetbuf(out, buf);
|
|
|
|
out
|
|
|
|
<< "<img"
|
|
|
|
<< " src=\"" << param.at("url") << "\""
|
|
|
|
<< " height=\"100%\""
|
|
|
|
<< " width=\"100%\""
|
|
|
|
<< " />"
|
|
|
|
<< "<caption>"
|
|
|
|
<< caption
|
|
|
|
<< "</caption>"
|
|
|
|
;
|
|
|
|
|
|
|
|
const string_view html
|
|
|
|
{
|
|
|
|
view(out, buf)
|
|
|
|
};
|
|
|
|
|
|
|
|
return
|
|
|
|
{
|
|
|
|
html, caption
|
|
|
|
};
|
|
|
|
}
|
2020-11-22 00:24:30 +01:00
|
|
|
|
|
|
|
command_result
|
|
|
|
command__control(const mutable_buffer &buf,
|
|
|
|
const m::user &user,
|
|
|
|
const m::room &room,
|
|
|
|
const string_view &cmd)
|
|
|
|
{
|
|
|
|
if(!is_oper(user))
|
|
|
|
throw m::ACCESS_DENIED
|
|
|
|
{
|
|
|
|
"You do not have access to the !control room."
|
|
|
|
};
|
|
|
|
|
|
|
|
const ircd::module console_module
|
|
|
|
{
|
|
|
|
"console"
|
|
|
|
};
|
|
|
|
|
|
|
|
using prototype = int (std::ostream &, const string_view &, const string_view &);
|
|
|
|
mods::import<prototype> command
|
|
|
|
{
|
|
|
|
console_module, "console_command"s
|
|
|
|
};
|
|
|
|
|
|
|
|
std::ostringstream out;
|
|
|
|
out.exceptions(out.badbit | out.failbit | out.eofbit);
|
|
|
|
pubsetbuf(out, buf);
|
|
|
|
|
|
|
|
static const string_view opts
|
|
|
|
{
|
|
|
|
"html"
|
|
|
|
};
|
|
|
|
|
|
|
|
out << "<pre>";
|
|
|
|
command(out, cmd, opts);
|
|
|
|
out << "</pre>";
|
|
|
|
|
|
|
|
const string_view html
|
|
|
|
{
|
|
|
|
view(out, buf)
|
|
|
|
};
|
|
|
|
|
|
|
|
const string_view alt //TODO: X
|
|
|
|
{
|
|
|
|
"no alt text"
|
|
|
|
};
|
|
|
|
|
|
|
|
return
|
|
|
|
{
|
|
|
|
html, alt
|
|
|
|
};
|
|
|
|
}
|