2018-02-22 00:22:04 +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"
|
|
|
|
|
2019-05-31 23:21:41 +02:00
|
|
|
namespace ircd::m
|
|
|
|
{
|
2020-03-17 20:00:40 +01:00
|
|
|
static event::id::buf invite_remote(const event &);
|
|
|
|
static void on_invite_remote(const event &, vm::eval &);
|
2018-02-22 00:22:04 +01:00
|
|
|
|
2020-03-17 20:01:16 +01:00
|
|
|
extern conf::item<seconds> invite_remote_timeout;
|
2020-03-17 20:00:40 +01:00
|
|
|
extern hookfn<vm::eval &> invite_remote_hook;
|
2019-05-31 23:21:41 +02:00
|
|
|
}
|
2018-04-03 21:59:44 +02:00
|
|
|
|
2020-03-17 20:01:16 +01:00
|
|
|
decltype(ircd::m::invite_remote_timeout)
|
|
|
|
ircd::m::invite_remote_timeout
|
|
|
|
{
|
|
|
|
{ "name", "ircd.client.rooms.invite.remote.timeout" },
|
2020-03-20 21:54:44 +01:00
|
|
|
{ "default", 30L },
|
2020-03-17 20:01:16 +01:00
|
|
|
};
|
|
|
|
|
2020-03-17 20:00:40 +01:00
|
|
|
decltype(ircd::m::invite_remote_hook)
|
|
|
|
ircd::m::invite_remote_hook
|
2018-02-22 00:22:04 +01:00
|
|
|
{
|
2020-03-17 20:00:40 +01:00
|
|
|
on_invite_remote,
|
2019-05-31 23:21:41 +02:00
|
|
|
{
|
|
|
|
{ "_site", "vm.issue" },
|
|
|
|
{ "type", "m.room.member" },
|
|
|
|
{ "membership", "invite" },
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
ircd::m::resource::response
|
2019-05-31 23:21:41 +02:00
|
|
|
post__invite(ircd::client &client,
|
2019-09-29 01:12:07 +02:00
|
|
|
const ircd::m::resource::request &request,
|
2019-05-31 23:21:41 +02:00
|
|
|
const ircd::m::room::id &room_id)
|
|
|
|
{
|
|
|
|
using namespace ircd;
|
|
|
|
|
2018-04-03 21:56:46 +02:00
|
|
|
const m::user::id &target
|
2018-02-22 10:35:00 +01:00
|
|
|
{
|
|
|
|
unquote(request.at("user_id"))
|
|
|
|
};
|
|
|
|
|
2018-04-03 21:56:46 +02:00
|
|
|
const m::user::id &sender
|
|
|
|
{
|
|
|
|
request.user_id
|
|
|
|
};
|
|
|
|
|
2020-04-20 09:51:01 +02:00
|
|
|
const json::string &id_server
|
|
|
|
{
|
|
|
|
request["id_server"]
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::string &id_access_token
|
|
|
|
{
|
|
|
|
request["id_access_token"]
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::string &medium
|
|
|
|
{
|
|
|
|
request["medium"]
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::string &address
|
|
|
|
{
|
|
|
|
request["address"]
|
|
|
|
};
|
|
|
|
|
2018-04-03 21:56:46 +02:00
|
|
|
const auto event_id
|
|
|
|
{
|
2019-03-14 23:21:51 +01:00
|
|
|
m::invite(room_id, target, sender)
|
2018-04-03 21:56:46 +02:00
|
|
|
};
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
return m::resource::response
|
2018-02-22 00:22:04 +01:00
|
|
|
{
|
|
|
|
client, http::OK
|
|
|
|
};
|
|
|
|
}
|
2018-04-03 21:56:46 +02:00
|
|
|
|
2019-05-31 23:21:41 +02:00
|
|
|
void
|
2020-03-17 20:00:40 +01:00
|
|
|
ircd::m::on_invite_remote(const event &event,
|
|
|
|
vm::eval &eval)
|
2019-05-31 23:21:41 +02:00
|
|
|
{
|
|
|
|
const m::room::id &room_id
|
|
|
|
{
|
|
|
|
at<"room_id"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::user::id &target
|
|
|
|
{
|
|
|
|
at<"state_key"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto target_host
|
|
|
|
{
|
|
|
|
target.host()
|
|
|
|
};
|
|
|
|
|
|
|
|
if(m::my_host(target_host))
|
|
|
|
return;
|
|
|
|
|
2020-04-16 07:01:10 +02:00
|
|
|
// Host already signed event elsehow.
|
|
|
|
for(const auto &[host, sig] : json::get<"signatures"_>(event))
|
|
|
|
if(host == target_host)
|
|
|
|
return;
|
|
|
|
|
2019-05-31 23:21:41 +02:00
|
|
|
const m::room::origins origins
|
|
|
|
{
|
|
|
|
room_id
|
|
|
|
};
|
|
|
|
|
|
|
|
if(origins.has(target_host))
|
|
|
|
return;
|
|
|
|
|
|
|
|
const auto eid
|
|
|
|
{
|
2020-03-17 20:00:40 +01:00
|
|
|
invite_remote(event)
|
2019-05-31 23:21:41 +02:00
|
|
|
};
|
2020-03-17 19:17:44 +01:00
|
|
|
|
|
|
|
log::info
|
|
|
|
{
|
|
|
|
m::log, "Invite %s to %s by %s completed with %s",
|
|
|
|
string_view{target},
|
|
|
|
string_view{room_id},
|
|
|
|
json::get<"sender"_>(event),
|
|
|
|
string_view{eid},
|
|
|
|
};
|
2019-05-31 23:21:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::id::buf
|
2020-03-17 20:00:40 +01:00
|
|
|
ircd::m::invite_remote(const event &event)
|
2019-07-12 05:27:59 +02:00
|
|
|
try
|
2018-04-03 21:59:44 +02:00
|
|
|
{
|
|
|
|
const auto &event_id
|
|
|
|
{
|
2019-07-10 09:26:25 +02:00
|
|
|
event.event_id
|
2018-04-03 21:59:44 +02:00
|
|
|
};
|
|
|
|
|
2019-07-12 07:27:52 +02:00
|
|
|
const m::room::id &room_id
|
2018-04-03 21:59:44 +02:00
|
|
|
{
|
|
|
|
at<"room_id"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::user::id &target
|
|
|
|
{
|
|
|
|
at<"state_key"_>(event)
|
|
|
|
};
|
|
|
|
|
2019-02-05 07:50:51 +01:00
|
|
|
assert(!my(target));
|
2018-04-03 21:59:44 +02:00
|
|
|
const unique_buffer<mutable_buffer> bufs
|
|
|
|
{
|
|
|
|
148_KiB
|
|
|
|
};
|
|
|
|
|
2019-07-12 07:27:52 +02:00
|
|
|
json::stack out{bufs};
|
|
|
|
json::stack::object top{out};
|
|
|
|
{
|
|
|
|
char versionbuf[32];
|
|
|
|
json::stack::member room_version
|
|
|
|
{
|
|
|
|
top, "room_version", json::value
|
|
|
|
{
|
|
|
|
m::version(versionbuf, room_id, std::nothrow), json::STRING
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2019-07-14 05:35:17 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
json::stack::object _event
|
|
|
|
{
|
|
|
|
top, "event"
|
|
|
|
};
|
|
|
|
|
|
|
|
_event.append(event);
|
|
|
|
}
|
|
|
|
|
2019-07-12 06:07:32 +02:00
|
|
|
{
|
2019-07-12 07:27:52 +02:00
|
|
|
json::stack::array invite_room_state
|
|
|
|
{
|
|
|
|
top, "invite_room_state"
|
|
|
|
};
|
|
|
|
|
2019-07-14 05:35:17 +02:00
|
|
|
const auto append
|
|
|
|
{
|
|
|
|
[&invite_room_state](const m::event &event)
|
|
|
|
{
|
|
|
|
invite_room_state.append(event);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-07-12 07:27:52 +02:00
|
|
|
const m::room::state state
|
|
|
|
{
|
|
|
|
room_id
|
|
|
|
};
|
|
|
|
|
2019-07-14 05:35:17 +02:00
|
|
|
state.get(std::nothrow, "m.room.create", "", append);
|
|
|
|
state.get(std::nothrow, "m.room.power_levels", "", append);
|
|
|
|
state.get(std::nothrow, "m.room.join_rules", "", append);
|
2019-07-15 22:56:50 +02:00
|
|
|
state.get(std::nothrow, "m.room.history_visibility", "", append);
|
2019-07-14 05:35:17 +02:00
|
|
|
state.get(std::nothrow, "m.room.aliases", my_host(), append);
|
|
|
|
state.get(std::nothrow, "m.room.canonical_alias", "", append);
|
|
|
|
state.get(std::nothrow, "m.room.avatar", "", append);
|
|
|
|
state.get(std::nothrow, "m.room.name", "", append);
|
2019-07-15 22:56:50 +02:00
|
|
|
state.get(std::nothrow, "m.room.member", at<"sender"_>(event), append);
|
2019-07-14 05:35:17 +02:00
|
|
|
}
|
2019-07-12 06:07:32 +02:00
|
|
|
|
2019-07-12 07:27:52 +02:00
|
|
|
top.~object();
|
|
|
|
const string_view &proto
|
2018-04-03 21:59:44 +02:00
|
|
|
{
|
2019-07-12 07:27:52 +02:00
|
|
|
out.completed()
|
|
|
|
};
|
|
|
|
|
|
|
|
const mutable_buffer buf
|
|
|
|
{
|
|
|
|
bufs + size(proto)
|
2018-04-03 21:59:44 +02:00
|
|
|
};
|
|
|
|
|
2020-03-06 04:35:20 +01:00
|
|
|
m::fed::invite2::opts opts;
|
2018-04-03 21:59:44 +02:00
|
|
|
opts.remote = target.host();
|
2020-03-06 04:35:20 +01:00
|
|
|
m::fed::invite2 request
|
2018-04-03 21:59:44 +02:00
|
|
|
{
|
|
|
|
room_id, event_id, proto, buf, std::move(opts)
|
|
|
|
};
|
|
|
|
|
2020-04-16 05:51:13 +02:00
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
m::log, "Sending invite %s to %s",
|
|
|
|
string_view{event.event_id},
|
|
|
|
target.host(),
|
|
|
|
};
|
|
|
|
|
2019-07-12 06:47:52 +02:00
|
|
|
http::code rcode; try
|
2018-04-03 21:59:44 +02:00
|
|
|
{
|
2020-03-17 20:01:16 +01:00
|
|
|
request.wait(seconds(invite_remote_timeout));
|
2019-07-12 06:47:52 +02:00
|
|
|
rcode = request.get();
|
|
|
|
}
|
|
|
|
catch(const http::error &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
2020-04-16 05:51:13 +02:00
|
|
|
m::log, "Invite %s to %s :%s :%s",
|
2019-07-12 06:47:52 +02:00
|
|
|
string_view{event.event_id},
|
2020-04-16 05:51:13 +02:00
|
|
|
target.host(),
|
2019-07-12 06:47:52 +02:00
|
|
|
e.what(),
|
|
|
|
e.content,
|
|
|
|
};
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
2018-04-03 21:59:44 +02:00
|
|
|
|
2019-07-30 19:24:36 +02:00
|
|
|
const json::object response
|
2018-04-03 21:59:44 +02:00
|
|
|
{
|
2019-07-12 06:07:32 +02:00
|
|
|
request
|
2018-04-03 21:59:44 +02:00
|
|
|
};
|
|
|
|
|
2019-07-12 05:07:12 +02:00
|
|
|
m::event::id::buf revent_id;
|
2018-04-03 21:59:44 +02:00
|
|
|
const m::event &revent
|
|
|
|
{
|
2019-07-12 06:07:32 +02:00
|
|
|
revent_id, response.at("event")
|
2018-04-03 21:59:44 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if(!verify(revent, target.host()))
|
|
|
|
throw m::error
|
|
|
|
{
|
|
|
|
http::UNAUTHORIZED, "M_INVITE_UNSIGNED",
|
|
|
|
"Invitee's host '%s' did not sign the invite.",
|
|
|
|
target.host()
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!verify(revent, my_host()))
|
|
|
|
throw m::error
|
|
|
|
{
|
|
|
|
http::FORBIDDEN, "M_INVITE_MODIFIED",
|
2018-04-05 13:16:13 +02:00
|
|
|
"Invite event no longer verified by our signature."
|
2018-04-03 21:59:44 +02:00
|
|
|
};
|
|
|
|
|
2020-03-17 19:17:44 +01:00
|
|
|
log::logf
|
2019-07-14 05:39:13 +02:00
|
|
|
{
|
2020-03-17 19:17:44 +01:00
|
|
|
m::log, log::level::DEBUG,
|
|
|
|
"Invite %s in %s accepted by '%s'",
|
2019-07-14 05:39:13 +02:00
|
|
|
string_view{event.event_id},
|
|
|
|
string_view{room_id},
|
|
|
|
string_view{target.host()},
|
|
|
|
};
|
|
|
|
|
2018-04-03 21:59:44 +02:00
|
|
|
m::vm::opts vmopts;
|
|
|
|
vmopts.infolog_accept = true;
|
2020-03-17 19:17:44 +01:00
|
|
|
vmopts.unique = false;
|
|
|
|
m::vm::eval
|
|
|
|
{
|
|
|
|
revent, vmopts
|
|
|
|
};
|
2018-04-03 21:59:44 +02:00
|
|
|
|
2020-04-16 07:15:00 +02:00
|
|
|
return revent_id;
|
2019-07-12 05:27:59 +02:00
|
|
|
}
|
2019-07-12 06:47:52 +02:00
|
|
|
catch(const std::exception &e)
|
2019-07-12 05:27:59 +02:00
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
2020-04-16 05:51:13 +02:00
|
|
|
m::log, "Invite remote %s :%s",
|
2019-07-12 05:27:59 +02:00
|
|
|
string_view{event.event_id},
|
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
|
|
|
|
throw;
|
2018-04-03 21:59:44 +02:00
|
|
|
}
|