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
|
|
|
|
{
|
|
|
|
static event::id::buf invite_foreign(const event &);
|
|
|
|
static void on_invite_foreign(const event &, vm::eval &);
|
2018-02-22 00:22:04 +01:00
|
|
|
|
2019-08-10 06:27:12 +02:00
|
|
|
extern hookfn<vm::eval &> invite_foreign_hook;
|
2019-05-31 23:21:41 +02:00
|
|
|
}
|
2018-04-03 21:59:44 +02:00
|
|
|
|
2019-05-31 23:21:41 +02:00
|
|
|
decltype(ircd::m::invite_foreign_hook)
|
|
|
|
ircd::m::invite_foreign_hook
|
2018-02-22 00:22:04 +01:00
|
|
|
{
|
2019-05-31 23:21:41 +02:00
|
|
|
on_invite_foreign,
|
|
|
|
{
|
|
|
|
{ "_site", "vm.issue" },
|
|
|
|
{ "type", "m.room.member" },
|
|
|
|
{ "membership", "invite" },
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ircd::resource::response
|
|
|
|
post__invite(ircd::client &client,
|
|
|
|
const ircd::resource::request &request,
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2018-02-22 00:22:04 +01:00
|
|
|
return resource::response
|
|
|
|
{
|
|
|
|
client, http::OK
|
|
|
|
};
|
|
|
|
}
|
2018-04-03 21:56:46 +02:00
|
|
|
|
2019-05-31 23:21:41 +02:00
|
|
|
ircd::m::event::id::buf
|
2019-03-14 23:21:51 +01:00
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::invite(const m::room &room,
|
|
|
|
const m::user::id &target,
|
|
|
|
const m::user::id &sender,
|
|
|
|
json::iov &content)
|
2018-04-03 21:56:46 +02:00
|
|
|
{
|
|
|
|
if(!exists(room))
|
|
|
|
throw m::NOT_FOUND
|
|
|
|
{
|
|
|
|
"Not aware of room %s",
|
|
|
|
string_view{room.room_id}
|
|
|
|
};
|
|
|
|
|
|
|
|
json::iov event;
|
|
|
|
const json::iov::push push[]
|
|
|
|
{
|
|
|
|
{ event, { "type", "m.room.member" }},
|
|
|
|
{ event, { "sender", sender }},
|
|
|
|
{ event, { "state_key", target }},
|
|
|
|
{ content, { "membership", "invite" }},
|
|
|
|
};
|
|
|
|
|
|
|
|
return commit(room, event, content);
|
|
|
|
}
|
2018-04-03 21:59:44 +02:00
|
|
|
|
2019-05-31 23:21:41 +02:00
|
|
|
void
|
|
|
|
ircd::m::on_invite_foreign(const event &event,
|
|
|
|
vm::eval &eval)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
|
|
|
const m::room::origins origins
|
|
|
|
{
|
|
|
|
room_id
|
|
|
|
};
|
|
|
|
|
|
|
|
if(origins.has(target_host))
|
|
|
|
return;
|
|
|
|
|
|
|
|
const auto eid
|
|
|
|
{
|
|
|
|
invite_foreign(event)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::id::buf
|
|
|
|
ircd::m::invite_foreign(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
|
|
|
};
|
|
|
|
|
2019-07-12 06:07:32 +02:00
|
|
|
m::v2::invite::opts opts;
|
2018-04-03 21:59:44 +02:00
|
|
|
opts.remote = target.host();
|
2019-07-12 06:07:32 +02:00
|
|
|
m::v2::invite request
|
2018-04-03 21:59:44 +02:00
|
|
|
{
|
|
|
|
room_id, event_id, proto, buf, std::move(opts)
|
|
|
|
};
|
|
|
|
|
2019-07-12 06:47:52 +02:00
|
|
|
http::code rcode; try
|
2018-04-03 21:59:44 +02:00
|
|
|
{
|
2019-07-12 06:47:52 +02:00
|
|
|
request.wait(seconds(10)); //TODO: conf
|
|
|
|
rcode = request.get();
|
|
|
|
}
|
|
|
|
catch(const http::error &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
"Invite %s to %s :%s :%s",
|
|
|
|
string_view{event.event_id},
|
|
|
|
string(opts.remote),
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2019-07-14 05:39:13 +02:00
|
|
|
log::info
|
|
|
|
{
|
|
|
|
m::log, "Invite %s in %s accepted by '%s'",
|
|
|
|
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;
|
|
|
|
|
|
|
|
m::vm::eval(revent, vmopts);
|
2019-07-12 05:27:59 +02:00
|
|
|
return revent.event_id;
|
|
|
|
}
|
2019-07-12 06:47:52 +02:00
|
|
|
catch(const std::exception &e)
|
2019-07-12 05:27:59 +02:00
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
2019-07-12 06:47:52 +02:00
|
|
|
"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
|
|
|
}
|