2018-04-04 01:53:30 +02: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.
|
|
|
|
|
|
|
|
using namespace ircd;
|
|
|
|
|
2020-03-19 07:29:02 +01:00
|
|
|
static void
|
|
|
|
send_join__response(client &,
|
|
|
|
const m::resource::request &,
|
|
|
|
const m::event &,
|
|
|
|
const m::room::state &,
|
|
|
|
const m::room::auth::chain &,
|
|
|
|
json::stack::object &out);
|
|
|
|
|
|
|
|
static m::resource::response
|
|
|
|
put__send_join(client &,
|
|
|
|
const m::resource::request &);
|
|
|
|
|
2018-04-04 01:53:30 +02:00
|
|
|
mapi::header
|
|
|
|
IRCD_MODULE
|
|
|
|
{
|
|
|
|
"Federation :Send join event"
|
|
|
|
};
|
|
|
|
|
|
|
|
const string_view
|
|
|
|
send_join_description
|
|
|
|
{R"(
|
|
|
|
|
|
|
|
Inject a join event into a room originating from a server without any joined
|
|
|
|
users in that room.
|
|
|
|
|
|
|
|
)"};
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
m::resource
|
2018-04-04 01:53:30 +02:00
|
|
|
send_join_resource
|
|
|
|
{
|
|
|
|
"/_matrix/federation/v1/send_join/",
|
|
|
|
{
|
|
|
|
send_join_description,
|
|
|
|
resource::DIRECTORY
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-03-19 07:29:02 +01:00
|
|
|
m::resource::method
|
|
|
|
method_put
|
|
|
|
{
|
|
|
|
send_join_resource, "PUT", put__send_join,
|
|
|
|
{
|
|
|
|
method_put.VERIFY_ORIGIN
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
m::resource::response
|
2018-04-04 01:53:30 +02:00
|
|
|
put__send_join(client &client,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::resource::request &request)
|
2018-04-04 01:53:30 +02:00
|
|
|
{
|
2020-03-19 07:29:02 +01:00
|
|
|
const bool v1
|
|
|
|
{
|
|
|
|
startswith(request.head.path, "/_matrix/federation/v1/")
|
|
|
|
};
|
|
|
|
|
2018-04-04 01:53:30 +02:00
|
|
|
if(request.parv.size() < 1)
|
|
|
|
throw m::NEED_MORE_PARAMS
|
|
|
|
{
|
|
|
|
"room_id path parameter required"
|
|
|
|
};
|
|
|
|
|
|
|
|
m::room::id::buf room_id
|
|
|
|
{
|
2018-12-07 01:41:47 +01:00
|
|
|
url::decode(room_id, request.parv[0])
|
2018-04-04 01:53:30 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if(request.parv.size() < 2)
|
|
|
|
throw m::NEED_MORE_PARAMS
|
|
|
|
{
|
|
|
|
"event_id path parameter required"
|
|
|
|
};
|
|
|
|
|
|
|
|
m::event::id::buf event_id
|
|
|
|
{
|
2018-12-07 01:41:47 +01:00
|
|
|
url::decode(event_id, request.parv[1])
|
2018-04-04 01:53:30 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const m::event event
|
|
|
|
{
|
2019-07-10 09:25:42 +02:00
|
|
|
request, event_id
|
2018-04-04 01:53:30 +02:00
|
|
|
};
|
|
|
|
|
2019-07-10 09:25:42 +02:00
|
|
|
if(!check_id(event))
|
|
|
|
throw m::BAD_REQUEST
|
2018-04-04 01:53:30 +02:00
|
|
|
{
|
2019-07-10 09:25:42 +02:00
|
|
|
"ID of event in request does not match path parameter %s",
|
|
|
|
string_view{event_id},
|
2018-04-04 01:53:30 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if(at<"room_id"_>(event) != room_id)
|
|
|
|
throw m::error
|
|
|
|
{
|
|
|
|
http::NOT_MODIFIED, "M_MISMATCH_ROOM_ID",
|
|
|
|
"ID of room in request body does not match path parameter."
|
|
|
|
};
|
|
|
|
|
|
|
|
if(json::get<"type"_>(event) != "m.room.member")
|
|
|
|
throw m::error
|
|
|
|
{
|
|
|
|
http::NOT_MODIFIED, "M_INVALID_TYPE",
|
|
|
|
"Event type must be m.room.member"
|
|
|
|
};
|
|
|
|
|
|
|
|
if(unquote(json::get<"content"_>(event).get("membership")) != "join")
|
|
|
|
throw m::error
|
|
|
|
{
|
|
|
|
http::NOT_MODIFIED, "M_INVALID_CONTENT_MEMBERSHIP",
|
|
|
|
"Event content.membership state must be 'join'."
|
|
|
|
};
|
|
|
|
|
2020-04-12 23:55:54 +02:00
|
|
|
if(json::get<"origin"_>(event) != request.node_id)
|
2018-04-05 01:12:13 +02:00
|
|
|
throw m::error
|
|
|
|
{
|
|
|
|
http::NOT_MODIFIED, "M_MISMATCH_ORIGIN",
|
|
|
|
"Event origin must be you."
|
|
|
|
};
|
|
|
|
|
2019-05-27 05:36:26 +02:00
|
|
|
if(m::room::server_acl::enable_write && !m::room::server_acl::check(room_id, request.node_id))
|
|
|
|
throw m::ACCESS_DENIED
|
|
|
|
{
|
|
|
|
"You are not permitted by the room's server access control list."
|
|
|
|
};
|
|
|
|
|
2018-04-04 01:53:30 +02:00
|
|
|
m::vm::opts vmopts;
|
2020-05-12 05:24:54 +02:00
|
|
|
vmopts.fetch = false;
|
2018-04-04 01:53:30 +02:00
|
|
|
m::vm::eval eval
|
|
|
|
{
|
|
|
|
event, vmopts
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::room::state state
|
|
|
|
{
|
2018-04-05 02:26:27 +02:00
|
|
|
room_id
|
2018-04-04 01:53:30 +02:00
|
|
|
};
|
|
|
|
|
2019-08-18 08:19:05 +02:00
|
|
|
const m::room::auth::chain auth_chain
|
2018-04-04 01:53:30 +02:00
|
|
|
{
|
2019-03-04 00:43:55 +01:00
|
|
|
m::head_idx(room_id)
|
|
|
|
};
|
2018-04-04 01:53:30 +02:00
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
m::resource::response::chunked response
|
2019-03-04 00:43:55 +01:00
|
|
|
{
|
|
|
|
client, http::OK
|
|
|
|
};
|
2018-04-04 01:53:30 +02:00
|
|
|
|
2019-03-04 00:43:55 +01:00
|
|
|
json::stack out
|
|
|
|
{
|
|
|
|
response.buf, response.flusher()
|
|
|
|
};
|
2018-04-04 01:53:30 +02:00
|
|
|
|
2020-03-19 07:29:02 +01:00
|
|
|
if(v1)
|
|
|
|
{
|
|
|
|
json::stack::array top
|
|
|
|
{
|
|
|
|
out
|
|
|
|
};
|
|
|
|
|
|
|
|
// First element is the 200
|
|
|
|
top.append(json::value(200L));
|
|
|
|
|
|
|
|
// Second element is the object
|
|
|
|
json::stack::object data
|
|
|
|
{
|
|
|
|
top
|
|
|
|
};
|
|
|
|
|
|
|
|
send_join__response(client, request, event, state, auth_chain, data);
|
2022-06-25 22:47:43 +02:00
|
|
|
return response;
|
2020-03-19 07:29:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
json::stack::object top
|
2019-03-04 00:43:55 +01:00
|
|
|
{
|
|
|
|
out
|
|
|
|
};
|
2018-04-05 02:26:27 +02:00
|
|
|
|
2020-03-19 07:29:02 +01:00
|
|
|
// Top element is the object
|
|
|
|
send_join__response(client, request, event, state, auth_chain, top);
|
2022-06-25 22:47:43 +02:00
|
|
|
return response;
|
2020-03-19 07:29:02 +01:00
|
|
|
}
|
2018-04-04 01:53:30 +02:00
|
|
|
|
2020-03-19 07:29:02 +01:00
|
|
|
void
|
|
|
|
send_join__response(client &client,
|
|
|
|
const m::resource::request &request,
|
|
|
|
const m::event &event,
|
|
|
|
const m::room::state &state,
|
|
|
|
const m::room::auth::chain &auth_chain,
|
|
|
|
json::stack::object &data)
|
|
|
|
{
|
2019-03-06 23:00:12 +01:00
|
|
|
// Required. The resident server's DNS name.
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
data, "origin", my_host()
|
|
|
|
};
|
|
|
|
|
2019-03-04 00:43:55 +01:00
|
|
|
// auth_chain
|
2019-06-05 06:55:51 +02:00
|
|
|
if(request.query.get<bool>("auth_chain", true))
|
2019-03-04 00:43:55 +01:00
|
|
|
{
|
|
|
|
json::stack::array auth_chain_a
|
2018-04-04 01:53:30 +02:00
|
|
|
{
|
2019-03-04 00:43:55 +01:00
|
|
|
data, "auth_chain"
|
|
|
|
};
|
2018-04-04 01:53:30 +02:00
|
|
|
|
2019-03-04 00:43:55 +01:00
|
|
|
auth_chain.for_each(m::event::closure_idx_bool{[&auth_chain_a]
|
|
|
|
(const m::event::idx &event_idx)
|
|
|
|
{
|
|
|
|
const m::event::fetch event
|
2018-04-04 01:53:30 +02:00
|
|
|
{
|
2020-04-03 04:50:00 +02:00
|
|
|
std::nothrow, event_idx
|
2018-04-04 01:53:30 +02:00
|
|
|
};
|
|
|
|
|
2019-03-04 00:43:55 +01:00
|
|
|
if(event.valid)
|
|
|
|
auth_chain_a.append(event);
|
2018-04-05 02:26:27 +02:00
|
|
|
|
2019-03-04 00:43:55 +01:00
|
|
|
return true;
|
|
|
|
}});
|
2018-04-04 01:53:30 +02:00
|
|
|
}
|
|
|
|
|
2019-06-05 06:56:15 +02:00
|
|
|
// auth_chain_ids (non-spec)
|
|
|
|
if(request.query.get<bool>("auth_chain_ids", false))
|
|
|
|
{
|
|
|
|
json::stack::array auth_chain_a
|
|
|
|
{
|
|
|
|
data, "auth_chain_ids"
|
|
|
|
};
|
|
|
|
|
|
|
|
auth_chain.for_each(m::event::closure_idx_bool{[&auth_chain_a]
|
|
|
|
(const m::event::idx &event_idx)
|
|
|
|
{
|
|
|
|
const auto &event_id
|
|
|
|
{
|
2020-04-02 00:42:33 +02:00
|
|
|
m::event_id(std::nothrow, event_idx)
|
2019-06-05 06:56:15 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if(event_id)
|
|
|
|
auth_chain_a.append(event_id);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}});
|
|
|
|
}
|
|
|
|
|
2019-03-04 00:43:55 +01:00
|
|
|
// state
|
2019-06-05 06:55:51 +02:00
|
|
|
if(request.query.get<bool>("state", true))
|
2018-04-04 01:53:30 +02:00
|
|
|
{
|
2019-03-04 00:43:55 +01:00
|
|
|
json::stack::array state_a
|
2018-04-05 02:26:27 +02:00
|
|
|
{
|
2019-03-04 00:43:55 +01:00
|
|
|
data, "state"
|
|
|
|
};
|
|
|
|
|
|
|
|
state.for_each([&state_a]
|
|
|
|
(const m::event &event)
|
|
|
|
{
|
|
|
|
state_a.append(event);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-06-05 06:56:15 +02:00
|
|
|
// state_ids (non-spec)
|
|
|
|
if(request.query.get<bool>("state_ids", false))
|
|
|
|
{
|
|
|
|
json::stack::array state_ids
|
|
|
|
{
|
|
|
|
data, "state_ids"
|
|
|
|
};
|
|
|
|
|
|
|
|
state.for_each(m::event::id::closure{[&state_ids]
|
|
|
|
(const m::event::id &event_id)
|
|
|
|
{
|
|
|
|
state_ids.append(event_id);
|
2019-08-18 14:38:44 +02:00
|
|
|
return true;
|
2019-06-05 06:56:15 +02:00
|
|
|
}});
|
|
|
|
}
|
2018-04-04 01:53:30 +02:00
|
|
|
}
|