2018-02-14 21:23:20 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 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.
|
|
|
|
|
|
|
|
#include "rooms.h"
|
|
|
|
|
2018-02-23 05:16:12 +01:00
|
|
|
using namespace ircd::m;
|
2018-02-14 21:23:20 +01:00
|
|
|
using namespace ircd;
|
|
|
|
|
2020-03-01 21:13:33 +01:00
|
|
|
static bool
|
|
|
|
check_transaction_id(const m::user::id &user_id,
|
|
|
|
const string_view &transaction_id);
|
|
|
|
|
2019-04-08 01:10:40 +02:00
|
|
|
static void
|
|
|
|
save_transaction_id(const m::event &,
|
|
|
|
m::vm::eval &);
|
|
|
|
|
2021-01-25 09:57:13 +01:00
|
|
|
static m::event::id::buf
|
2019-04-08 01:10:40 +02:00
|
|
|
handle_command(client &,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::resource::request &,
|
2021-02-01 02:22:59 +01:00
|
|
|
const m::room &,
|
|
|
|
const string_view & = {});
|
2019-04-08 01:10:40 +02:00
|
|
|
|
|
|
|
m::hookfn<m::vm::eval &>
|
|
|
|
save_transaction_id_hookfn
|
|
|
|
{
|
|
|
|
save_transaction_id,
|
|
|
|
{
|
|
|
|
{ "_site", "vm.post" },
|
|
|
|
{ "origin", my_host() },
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-10-27 07:40:19 +01:00
|
|
|
conf::item<bool>
|
|
|
|
new_content_workaround
|
|
|
|
{
|
|
|
|
{ "name", "ircd.client.rooms.send.new_content_workaround" },
|
|
|
|
{ "default", true },
|
|
|
|
};
|
|
|
|
|
|
|
|
static_assert
|
|
|
|
(
|
|
|
|
m::event::MAX_SIZE >= 1_KiB
|
|
|
|
);
|
|
|
|
|
|
|
|
static const size_t
|
|
|
|
content_max
|
|
|
|
{
|
|
|
|
m::event::MAX_SIZE - 1_KiB
|
|
|
|
};
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
m::resource::response
|
2018-02-14 21:23:20 +01:00
|
|
|
put__send(client &client,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::resource::request &request,
|
2018-02-23 05:16:12 +01:00
|
|
|
const room::id &room_id)
|
2018-02-14 21:23:20 +01:00
|
|
|
{
|
|
|
|
if(request.parv.size() < 3)
|
2018-02-23 05:16:12 +01:00
|
|
|
throw NEED_MORE_PARAMS
|
2018-02-15 06:52:19 +01:00
|
|
|
{
|
|
|
|
"type parameter missing"
|
|
|
|
};
|
2018-02-14 21:23:20 +01:00
|
|
|
|
2019-03-08 00:32:03 +01:00
|
|
|
char type_buf[m::event::TYPE_MAX_SIZE];
|
2018-02-14 21:23:20 +01:00
|
|
|
const string_view &type
|
|
|
|
{
|
2019-03-08 00:32:03 +01:00
|
|
|
url::decode(type_buf, request.parv[2])
|
2018-02-14 21:23:20 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
if(request.parv.size() < 4)
|
2018-02-23 05:16:12 +01:00
|
|
|
throw NEED_MORE_PARAMS
|
2018-02-15 06:52:19 +01:00
|
|
|
{
|
|
|
|
"txnid parameter missing"
|
|
|
|
};
|
2018-02-14 21:23:20 +01:00
|
|
|
|
2019-03-08 00:32:03 +01:00
|
|
|
char transaction_id_buf[64];
|
2018-09-06 04:32:14 +02:00
|
|
|
const string_view &transaction_id
|
2018-02-14 21:23:20 +01:00
|
|
|
{
|
2019-03-08 00:32:03 +01:00
|
|
|
url::decode(transaction_id_buf, request.parv[3])
|
2018-02-14 21:23:20 +01:00
|
|
|
};
|
|
|
|
|
2020-03-01 21:13:33 +01:00
|
|
|
if(!check_transaction_id(request.user_id, transaction_id))
|
|
|
|
throw m::error
|
|
|
|
{
|
|
|
|
http::CONFLICT, "M_DUPLICATE_TXNID",
|
|
|
|
"Already processed request with txnid '%s'",
|
|
|
|
transaction_id
|
|
|
|
};
|
|
|
|
|
2020-10-27 07:40:19 +01:00
|
|
|
json::object content
|
2020-10-25 13:19:38 +01:00
|
|
|
{
|
|
|
|
request
|
|
|
|
};
|
|
|
|
|
2020-10-27 07:40:19 +01:00
|
|
|
// Workaround for the quadruplication of content by certain clients
|
|
|
|
// supporting message edits via `m.new_content`. The skinny is that
|
|
|
|
// there's effectively four copies of the user's message data which can
|
|
|
|
// be found within the content here. For this reason if the content size
|
|
|
|
// with all four copies will exceed the threshold, we truncate two of the
|
|
|
|
// four copies which won't be used by supporting clients.
|
|
|
|
const bool workaround_new_content
|
2020-10-25 13:19:38 +01:00
|
|
|
{
|
2020-10-27 07:40:19 +01:00
|
|
|
// This functionality is enabled by the configuration
|
|
|
|
new_content_workaround
|
|
|
|
|
|
|
|
// For good-faith compatibility this functionality is enabled iff the
|
|
|
|
// content size is excessive.
|
|
|
|
&& size(string_view(content)) > content_max
|
|
|
|
|
|
|
|
// This workaround is only effective for m.new_content edits.
|
|
|
|
&& content.has("m.new_content")
|
2020-10-25 13:19:38 +01:00
|
|
|
};
|
|
|
|
|
2020-10-27 07:40:19 +01:00
|
|
|
json::strung shorter_content;
|
|
|
|
if(workaround_new_content)
|
|
|
|
content = shorter_content = json::replace(content, json::members
|
|
|
|
{
|
|
|
|
{ "body", " * "_sv },
|
|
|
|
{ "formatted_body", " * "_sv },
|
|
|
|
});
|
|
|
|
|
2020-10-25 13:19:38 +01:00
|
|
|
// This is only a preliminary check that the content size is sane and
|
|
|
|
// will fit. There may still be a rejection at a deeper stage.
|
|
|
|
if(size(string_view(content)) > content_max)
|
|
|
|
throw m::error
|
|
|
|
{
|
|
|
|
http::PAYLOAD_TOO_LARGE, "M_TOO_LARGE",
|
|
|
|
"Message of %zu bytes exceeds maximum of %zu bytes.",
|
|
|
|
size(request.content),
|
|
|
|
content_max,
|
|
|
|
};
|
|
|
|
|
2018-09-06 04:32:14 +02:00
|
|
|
m::vm::copts copts;
|
|
|
|
copts.client_txnid = transaction_id;
|
2019-04-08 01:10:40 +02:00
|
|
|
const room room
|
2018-02-14 21:23:20 +01:00
|
|
|
{
|
2018-09-06 04:32:14 +02:00
|
|
|
room_id, &copts
|
2018-02-14 21:23:20 +01:00
|
|
|
};
|
|
|
|
|
2021-01-25 09:57:13 +01:00
|
|
|
bool command_echo {false};
|
2020-11-13 14:20:01 +01:00
|
|
|
if(type == "m.room.message")
|
2019-04-08 01:10:40 +02:00
|
|
|
{
|
2020-11-13 14:20:01 +01:00
|
|
|
const m::room::message message
|
|
|
|
{
|
|
|
|
content
|
|
|
|
};
|
2018-02-14 21:23:20 +01:00
|
|
|
|
2021-01-25 09:57:13 +01:00
|
|
|
const bool command
|
2020-11-13 14:20:01 +01:00
|
|
|
{
|
2021-01-25 09:57:13 +01:00
|
|
|
json::get<"msgtype"_>(message) == "m.text"
|
|
|
|
&& startswith(json::get<"body"_>(message), "\\\\")
|
2020-11-13 14:20:01 +01:00
|
|
|
};
|
|
|
|
|
2021-01-25 09:57:13 +01:00
|
|
|
const bool echo
|
|
|
|
{
|
|
|
|
startswith(lstrip(json::get<"body"_>(message), "\\\\"), '!')
|
|
|
|
};
|
|
|
|
|
|
|
|
if(command && !echo)
|
|
|
|
{
|
|
|
|
const auto event_id
|
|
|
|
{
|
|
|
|
handle_command(client, request, room)
|
|
|
|
};
|
|
|
|
|
|
|
|
return m::resource::response
|
|
|
|
{
|
|
|
|
client, json::members
|
|
|
|
{
|
|
|
|
{ "event_id", event_id },
|
|
|
|
{ "cmd", true },
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else command_echo = command && echo;
|
2020-11-13 14:20:01 +01:00
|
|
|
}
|
2019-04-08 01:10:40 +02:00
|
|
|
|
2018-02-14 21:23:20 +01:00
|
|
|
const auto event_id
|
|
|
|
{
|
2019-08-26 04:28:06 +02:00
|
|
|
m::send(room, request.user_id, type, content)
|
2018-02-14 21:23:20 +01:00
|
|
|
};
|
|
|
|
|
2021-01-25 09:57:13 +01:00
|
|
|
// For public echo, run the command after sending the command to the room.
|
|
|
|
if(command_echo)
|
|
|
|
{
|
|
|
|
const auto cmd_event_id
|
|
|
|
{
|
2021-02-01 02:22:59 +01:00
|
|
|
handle_command(client, request, room, event_id)
|
2021-01-25 09:57:13 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
return m::resource::response
|
2018-02-14 21:23:20 +01:00
|
|
|
{
|
|
|
|
client, json::members
|
|
|
|
{
|
|
|
|
{ "event_id", event_id }
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2019-03-05 23:05:06 +01:00
|
|
|
|
2021-01-25 09:57:13 +01:00
|
|
|
m::event::id::buf
|
2019-04-08 01:10:40 +02:00
|
|
|
handle_command(client &client,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::resource::request &request,
|
2021-02-01 02:22:59 +01:00
|
|
|
const m::room &room,
|
|
|
|
const string_view &echo_id)
|
2019-03-05 23:05:06 +01:00
|
|
|
{
|
2019-04-08 01:10:40 +02:00
|
|
|
const user::room user_room
|
2019-03-05 23:05:06 +01:00
|
|
|
{
|
2019-04-08 01:10:40 +02:00
|
|
|
request.user_id, room.copts
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto event_id
|
|
|
|
{
|
|
|
|
send(user_room, request.user_id, "ircd.cmd",
|
|
|
|
{
|
|
|
|
{ "msgtype", "m.text" },
|
|
|
|
{ "body", request["body"] },
|
|
|
|
{ "room_id", room.room_id },
|
2021-02-01 02:22:59 +01:00
|
|
|
{ "event_id", echo_id },
|
2019-04-08 01:10:40 +02:00
|
|
|
})
|
|
|
|
};
|
|
|
|
|
2021-01-25 09:57:13 +01:00
|
|
|
return event_id;
|
2019-04-08 01:10:40 +02:00
|
|
|
}
|
2019-03-05 23:05:06 +01:00
|
|
|
|
|
|
|
void
|
|
|
|
save_transaction_id(const m::event &event,
|
|
|
|
m::vm::eval &eval)
|
|
|
|
{
|
|
|
|
if(!eval.copts)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(!eval.copts->client_txnid)
|
|
|
|
return;
|
|
|
|
|
2019-07-06 07:01:34 +02:00
|
|
|
if(!event.event_id)
|
2019-03-05 23:05:06 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
assert(my_host(at<"origin"_>(event)));
|
|
|
|
const m::user::room user_room
|
|
|
|
{
|
|
|
|
at<"sender"_>(event)
|
|
|
|
};
|
|
|
|
|
2019-04-08 01:10:40 +02:00
|
|
|
static const string_view &type
|
|
|
|
{
|
|
|
|
"ircd.client.txnid"
|
|
|
|
};
|
|
|
|
|
2019-03-05 23:05:06 +01:00
|
|
|
const auto &state_key
|
|
|
|
{
|
2019-07-10 09:26:25 +02:00
|
|
|
event.event_id
|
2019-03-05 23:05:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
send(user_room, at<"sender"_>(event), type, state_key,
|
|
|
|
{
|
|
|
|
{ "transaction_id", eval.copts->client_txnid }
|
|
|
|
});
|
|
|
|
}
|
2020-03-01 21:13:33 +01:00
|
|
|
|
|
|
|
// Using a linear search here because we have no index on txnids as this
|
|
|
|
// is the only codepath where we'd perform that lookup; in contrast the
|
|
|
|
// event_id -> txnid query is made far more often for client sync.
|
|
|
|
//
|
|
|
|
// This means we have to set some arbitrary limits on the linear search:
|
|
|
|
// lim[0] is a total limit of events to iterate, so if the user's room
|
|
|
|
// has a lot of activity we might return a false non-match and allow a
|
|
|
|
// duplicate txnid; this is highly unlikely. lim[1] allows the user to
|
|
|
|
// have several /sends in flight at the same time, also unlikely but we
|
|
|
|
// avoid that case for false non-match.
|
|
|
|
static bool
|
|
|
|
check_transaction_id(const m::user::id &user_id,
|
|
|
|
const string_view &transaction_id)
|
|
|
|
{
|
|
|
|
static const auto type_match
|
|
|
|
{
|
|
|
|
[](const string_view &type)
|
|
|
|
{
|
|
|
|
return type == "ircd.client.txnid";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto content_match
|
|
|
|
{
|
|
|
|
[&transaction_id](const json::object &content)
|
|
|
|
{
|
|
|
|
const json::string &value
|
|
|
|
{
|
|
|
|
content["transaction_id"]
|
|
|
|
};
|
|
|
|
|
|
|
|
return value == transaction_id;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ssize_t lim[] { 128, 3 };
|
|
|
|
const m::user::room user_room{user_id};
|
|
|
|
for(m::room::events it(user_room); it && lim[0] > 0 && lim[1] > 0; --it, --lim[0])
|
|
|
|
{
|
|
|
|
if(!m::query(std::nothrow, it.event_idx(), "type", type_match))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
--lim[1];
|
|
|
|
if(!m::query(std::nothrow, it.event_idx(), "content", content_match))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|