2018-05-20 03:32:22 +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;
|
|
|
|
|
|
|
|
mapi::header
|
|
|
|
IRCD_MODULE
|
|
|
|
{
|
|
|
|
"Matrix m.room.history_visibility"
|
|
|
|
};
|
|
|
|
|
2018-05-31 16:38:28 +02:00
|
|
|
static bool
|
2019-05-27 02:44:12 +02:00
|
|
|
_visible_to_user(const m::event &event,
|
|
|
|
const m::user::id &user_id,
|
|
|
|
const m::room &room,
|
|
|
|
const string_view &history_visibility)
|
2018-05-21 05:15:50 +02:00
|
|
|
{
|
2019-07-25 23:02:25 +02:00
|
|
|
char membership_buf[m::room::MEMBERSHIP_MAX_SIZE];
|
2019-04-23 02:38:55 +02:00
|
|
|
string_view membership
|
2018-05-31 16:38:28 +02:00
|
|
|
{
|
2019-08-14 10:01:46 +02:00
|
|
|
m::membership(membership_buf, room, user_id)
|
2018-05-31 16:38:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if(membership == "join")
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if(history_visibility == "joined")
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(history_visibility == "invited")
|
|
|
|
return membership == "invite";
|
|
|
|
|
|
|
|
assert(history_visibility == "shared");
|
2019-04-23 02:38:55 +02:00
|
|
|
if(membership == "invite")
|
|
|
|
return true;
|
2018-05-31 20:09:14 +02:00
|
|
|
|
|
|
|
// If the room is not at the present event then we have to run another
|
|
|
|
// test for membership here. Otherwise the "join" test already failed.
|
|
|
|
if(room.event_id)
|
|
|
|
{
|
|
|
|
const m::room present{room.room_id};
|
2019-08-14 10:01:46 +02:00
|
|
|
membership = m::membership(membership_buf, present, user_id);
|
2019-04-23 02:38:55 +02:00
|
|
|
return membership == "join" || membership == "invite";
|
2018-05-31 20:09:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2018-05-21 05:15:50 +02:00
|
|
|
}
|
|
|
|
|
2018-05-31 16:38:28 +02:00
|
|
|
static bool
|
2019-05-27 02:44:12 +02:00
|
|
|
_visible_to_node(const m::event &event,
|
|
|
|
const string_view &node_id,
|
|
|
|
const m::room &room,
|
|
|
|
const string_view &history_visibility)
|
2018-05-21 05:15:50 +02:00
|
|
|
{
|
2018-05-31 20:34:44 +02:00
|
|
|
const m::room::origins origins
|
|
|
|
{
|
|
|
|
room
|
|
|
|
};
|
|
|
|
|
2019-04-28 02:23:37 +02:00
|
|
|
// Allow joined servers
|
2019-05-27 02:44:12 +02:00
|
|
|
if(origins.has(node_id))
|
2019-04-28 02:23:37 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// Allow auth chain events XXX: this is too broad
|
2019-08-18 08:19:05 +02:00
|
|
|
if(m::room::auth::is_power_event(event))
|
2019-04-28 02:23:37 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// Allow any event where the state_key string is a user mxid and the server
|
|
|
|
// is the host of that user. Note that applies to any type of event.
|
|
|
|
if(m::valid(m::id::USER, json::get<"state_key"_>(event)))
|
2019-05-27 02:44:12 +02:00
|
|
|
if(m::user::id(at<"state_key"_>(event)).host() == node_id)
|
2019-04-28 02:23:37 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
2018-05-31 16:38:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
_visible(const m::event &event,
|
|
|
|
const string_view &mxid,
|
|
|
|
const m::room &room,
|
|
|
|
const string_view &history_visibility)
|
|
|
|
{
|
|
|
|
if(history_visibility == "world_readable")
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if(empty(mxid))
|
|
|
|
return false;
|
|
|
|
|
2019-05-27 02:44:12 +02:00
|
|
|
if(m::valid(m::id::USER, mxid))
|
|
|
|
return _visible_to_user(event, mxid, room, history_visibility);
|
2018-05-31 16:38:28 +02:00
|
|
|
|
2019-05-27 02:44:12 +02:00
|
|
|
if(rfc3986::valid_remote(std::nothrow, mxid))
|
|
|
|
return _visible_to_node(event, mxid, room, history_visibility);
|
2018-05-31 16:38:28 +02:00
|
|
|
|
2019-05-27 02:44:12 +02:00
|
|
|
throw m::UNSUPPORTED
|
|
|
|
{
|
|
|
|
"Cannot determine visibility for '%s'", mxid
|
|
|
|
};
|
2018-05-31 16:38:28 +02:00
|
|
|
}
|
2018-05-21 05:15:50 +02:00
|
|
|
|
2019-04-07 03:35:01 +02:00
|
|
|
bool
|
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::visible(const m::event &event,
|
|
|
|
const string_view &mxid)
|
2018-05-21 05:15:50 +02:00
|
|
|
{
|
2018-05-31 16:38:28 +02:00
|
|
|
const m::room room
|
|
|
|
{
|
2019-07-06 07:01:34 +02:00
|
|
|
at<"room_id"_>(event), event.event_id
|
2018-05-31 16:38:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static const m::event::fetch::opts fopts
|
|
|
|
{
|
|
|
|
m::event::keys::include{"content"}
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::room::state state
|
|
|
|
{
|
|
|
|
room, &fopts
|
|
|
|
};
|
|
|
|
|
|
|
|
bool ret{false};
|
|
|
|
const bool has_state_event
|
|
|
|
{
|
|
|
|
state.get(std::nothrow, "m.room.history_visibility", "", [&]
|
2019-04-07 03:40:24 +02:00
|
|
|
(const m::event &visibility_event)
|
2018-05-31 16:38:28 +02:00
|
|
|
{
|
|
|
|
const json::object &content
|
|
|
|
{
|
2019-04-07 03:40:24 +02:00
|
|
|
json::get<"content"_>(visibility_event)
|
2018-05-31 16:38:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const string_view &history_visibility
|
|
|
|
{
|
|
|
|
unquote(content.get("history_visibility", "shared"))
|
|
|
|
};
|
|
|
|
|
|
|
|
ret = _visible(event, mxid, room, history_visibility);
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
return !has_state_event?
|
|
|
|
_visible(event, mxid, room, "shared"):
|
|
|
|
ret;
|
2018-05-21 05:15:50 +02:00
|
|
|
}
|
|
|
|
|
2018-05-20 03:32:22 +02:00
|
|
|
static void
|
2018-10-07 07:17:46 +02:00
|
|
|
_changed_visibility(const m::event &event,
|
|
|
|
m::vm::eval &)
|
2018-05-20 03:32:22 +02:00
|
|
|
{
|
2018-05-23 13:23:31 +02:00
|
|
|
log::info
|
|
|
|
{
|
2018-12-23 02:44:18 +01:00
|
|
|
m::log, "Changed visibility of %s to %s by %s => %s",
|
2018-05-23 13:23:31 +02:00
|
|
|
json::get<"room_id"_>(event),
|
|
|
|
json::get<"content"_>(event).get("history_visibility"),
|
|
|
|
json::get<"sender"_>(event),
|
2019-07-06 07:01:34 +02:00
|
|
|
string_view{event.event_id}
|
2018-05-23 13:23:31 +02:00
|
|
|
};
|
2018-05-20 03:32:22 +02:00
|
|
|
}
|
|
|
|
|
2019-08-10 06:27:12 +02:00
|
|
|
m::hookfn<m::vm::eval &>
|
2018-05-20 03:32:22 +02:00
|
|
|
_changed_visibility_hookfn
|
|
|
|
{
|
|
|
|
_changed_visibility,
|
|
|
|
{
|
2018-10-07 07:17:46 +02:00
|
|
|
{ "_site", "vm.effect" },
|
2018-05-20 03:32:22 +02:00
|
|
|
{ "type", "m.room.history_visibility" },
|
|
|
|
}
|
|
|
|
};
|