2018-03-29 04:26:16 +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.
|
|
|
|
|
2019-07-18 22:45:15 +02:00
|
|
|
namespace ircd::m
|
|
|
|
{
|
2019-08-18 08:19:05 +02:00
|
|
|
static void auth_room_create(const event &, room::auth::hookdata &);
|
|
|
|
extern hookfn<room::auth::hookdata &> auth_room_create_hookfn;
|
2019-07-21 01:32:22 +02:00
|
|
|
|
|
|
|
static void created_room(const event &, vm::eval &);
|
|
|
|
extern hookfn<vm::eval &> created_room_hookfn;
|
2019-07-18 22:45:15 +02:00
|
|
|
}
|
2018-03-29 04:26:16 +02:00
|
|
|
|
2019-07-18 22:45:15 +02:00
|
|
|
ircd::mapi::header
|
2018-03-29 04:26:16 +02:00
|
|
|
IRCD_MODULE
|
|
|
|
{
|
|
|
|
"Matrix m.room.create"
|
|
|
|
};
|
|
|
|
|
2019-07-21 01:32:22 +02:00
|
|
|
//
|
|
|
|
// an effect of room created
|
|
|
|
//
|
|
|
|
|
2019-07-18 22:45:15 +02:00
|
|
|
decltype(ircd::m::created_room_hookfn)
|
|
|
|
ircd::m::created_room_hookfn
|
|
|
|
{
|
|
|
|
created_room,
|
|
|
|
{
|
|
|
|
{ "_site", "vm.effect" },
|
|
|
|
{ "type", "m.room.create" },
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::created_room(const m::event &event,
|
|
|
|
m::vm::eval &)
|
2019-08-06 01:26:52 +02:00
|
|
|
try
|
2018-03-29 04:26:16 +02:00
|
|
|
{
|
2019-08-06 01:32:39 +02:00
|
|
|
const m::user::id &sender
|
2018-05-13 05:19:17 +02:00
|
|
|
{
|
2019-08-06 01:32:39 +02:00
|
|
|
at<"sender"_>(event)
|
2018-05-13 05:19:17 +02:00
|
|
|
};
|
|
|
|
|
2019-08-06 01:32:39 +02:00
|
|
|
const auto &level
|
2018-05-13 05:19:17 +02:00
|
|
|
{
|
2019-10-01 05:50:58 +02:00
|
|
|
my(sender) && sender != me()?
|
2019-08-06 01:32:39 +02:00
|
|
|
log::INFO:
|
|
|
|
log::DEBUG
|
2018-05-13 05:19:17 +02:00
|
|
|
};
|
2018-03-29 04:26:16 +02:00
|
|
|
|
2020-03-17 05:27:11 +01:00
|
|
|
if(RB_DEBUG_LEVEL || level != log::DEBUG)
|
|
|
|
log::logf
|
|
|
|
{
|
|
|
|
m::log, level, "Created room %s with %s by %s",
|
|
|
|
json::get<"room_id"_>(event),
|
|
|
|
json::get<"sender"_>(event),
|
|
|
|
string_view{event.event_id},
|
|
|
|
};
|
2018-03-29 04:26:16 +02:00
|
|
|
}
|
2019-08-06 01:26:52 +02:00
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
m::log, "Effect of creating room %s with %s by %s :%s",
|
|
|
|
json::get<"room_id"_>(event),
|
|
|
|
string_view{event.event_id},
|
|
|
|
json::get<"sender"_>(event),
|
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
}
|
2018-03-29 04:26:16 +02:00
|
|
|
|
2019-07-21 01:32:22 +02:00
|
|
|
//
|
|
|
|
// auth handler
|
|
|
|
//
|
|
|
|
|
2019-07-18 22:45:15 +02:00
|
|
|
decltype(ircd::m::auth_room_create_hookfn)
|
|
|
|
ircd::m::auth_room_create_hookfn
|
2018-03-29 04:26:16 +02:00
|
|
|
{
|
2019-07-18 22:45:15 +02:00
|
|
|
auth_room_create,
|
2018-03-29 04:26:16 +02:00
|
|
|
{
|
2019-08-18 08:19:05 +02:00
|
|
|
{ "_site", "room.auth" },
|
2019-07-18 22:45:15 +02:00
|
|
|
{ "type", "m.room.create" },
|
2018-03-29 04:26:16 +02:00
|
|
|
}
|
|
|
|
};
|
2019-07-18 22:45:15 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::auth_room_create(const event &event,
|
2019-08-18 08:19:05 +02:00
|
|
|
room::auth::hookdata &data)
|
2019-07-18 22:45:15 +02:00
|
|
|
{
|
2019-08-18 08:19:05 +02:00
|
|
|
using FAIL = m::room::auth::FAIL;
|
2019-07-18 22:45:15 +02:00
|
|
|
using conforms = m::event::conforms;
|
2019-07-21 01:32:22 +02:00
|
|
|
|
|
|
|
// 1. If type is m.room.create:
|
2019-07-18 22:45:15 +02:00
|
|
|
assert(json::get<"type"_>(event) == "m.room.create");
|
|
|
|
|
|
|
|
// a. If it has any previous events, reject.
|
2019-08-21 07:59:05 +02:00
|
|
|
if(count(data.prev))
|
2019-07-18 22:45:15 +02:00
|
|
|
throw FAIL
|
|
|
|
{
|
|
|
|
"m.room.create has previous events."
|
|
|
|
};
|
|
|
|
|
|
|
|
// b. If the domain of the room_id does not match the domain of the
|
|
|
|
// sender, reject.
|
2019-09-13 22:40:24 +02:00
|
|
|
if(room::id(at<"room_id"_>(event)).host() != user::id(at<"sender"_>(event)).host())
|
2019-07-18 22:45:15 +02:00
|
|
|
throw FAIL
|
|
|
|
{
|
|
|
|
"m.room.create room_id domain does not match sender domain."
|
|
|
|
};
|
|
|
|
|
|
|
|
// c. If content.room_version is present and is not a recognised
|
|
|
|
// version, reject.
|
|
|
|
if(json::get<"content"_>(event).has("room_version"))
|
|
|
|
{
|
|
|
|
const json::string &claim_version
|
|
|
|
{
|
2019-08-16 07:38:20 +02:00
|
|
|
json::get<"content"_>(event).get("room_version", "1"_sv)
|
2019-07-18 22:45:15 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const string_view &id_version
|
|
|
|
{
|
|
|
|
event.event_id.version()
|
|
|
|
};
|
|
|
|
|
2019-08-16 07:37:37 +02:00
|
|
|
if(claim_version == "1" || claim_version == "2")
|
|
|
|
{
|
|
|
|
// When the claimed version is 1 or 2 we don't actually
|
|
|
|
// care if the event_id is version 1, 3 or 4 etc; the server
|
|
|
|
// has eliminated use of the event_id hostpart in all rooms.
|
|
|
|
|
|
|
|
//if(id_version != "1")
|
|
|
|
// throw FAIL
|
|
|
|
// {
|
|
|
|
// "m.room.create room_version not 1"
|
|
|
|
// };
|
|
|
|
}
|
|
|
|
else if(claim_version == "3")
|
2019-07-18 22:45:15 +02:00
|
|
|
{
|
|
|
|
if(id_version != "3")
|
|
|
|
throw FAIL
|
|
|
|
{
|
|
|
|
"m.room.create room_version not 3"
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else if(claim_version == "4")
|
|
|
|
{
|
|
|
|
if(id_version != "4")
|
|
|
|
throw FAIL
|
|
|
|
{
|
|
|
|
"m.room.create room_version not 4"
|
|
|
|
};
|
|
|
|
}
|
2019-08-16 07:37:37 +02:00
|
|
|
else
|
2019-07-18 22:45:15 +02:00
|
|
|
{
|
2019-08-16 07:37:37 +02:00
|
|
|
// Note that id_version reports "4" even for version "5"
|
|
|
|
// and beyond room versions. When a room version requires
|
|
|
|
// a new ID version these branches must be updated.
|
2019-07-18 22:45:15 +02:00
|
|
|
if(id_version != "4")
|
|
|
|
throw FAIL
|
|
|
|
{
|
|
|
|
"m.room.create room_version not 4"
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// d. If content has no creator field, reject.
|
|
|
|
if(empty(json::get<"content"_>(event).get("creator")))
|
|
|
|
throw FAIL
|
|
|
|
{
|
|
|
|
"m.room.create content.creator is missing."
|
|
|
|
};
|
|
|
|
|
|
|
|
// e. Otherwise, allow.
|
|
|
|
data.allow = true;
|
|
|
|
}
|