From eb603c22c143f1c1e59a954084af1903d4eb3184 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Mon, 11 Jun 2018 23:11:28 -0700 Subject: [PATCH] modules: Add preliminary m_room_aliases protocol suite. --- modules/Makefile.am | 2 + modules/m_room_aliases.cc | 108 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 modules/m_room_aliases.cc diff --git a/modules/Makefile.am b/modules/Makefile.am index 50fa90046..2bb005fb1 100644 --- a/modules/Makefile.am +++ b/modules/Makefile.am @@ -90,6 +90,7 @@ m_room_member_la_SOURCES = m_room_member.cc m_room_join_rules_la_SOURCES = m_room_join_rules.cc m_room_history_visibility_la_SOURCES = m_room_history_visibility.cc m_room_canonical_alias_la_SOURCES = m_room_canonical_alias.cc +m_room_aliases_la_SOURCES = m_room_aliases.cc m_module_LTLIBRARIES = \ m_noop.la \ @@ -102,6 +103,7 @@ m_module_LTLIBRARIES = \ m_room_join_rules.la \ m_room_history_visibility.la \ m_room_canonical_alias.la \ + m_room_aliases.la \ ### ############################################################################### diff --git a/modules/m_room_aliases.cc b/modules/m_room_aliases.cc new file mode 100644 index 000000000..b6d230c7f --- /dev/null +++ b/modules/m_room_aliases.cc @@ -0,0 +1,108 @@ +// Matrix Construct +// +// Copyright (C) Matrix Construct Developers, Authors & Contributors +// Copyright (C) 2016-2018 Jason Volk +// +// 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.aliases" +}; + +const m::room::id::buf +alias_room_id +{ + "alias", ircd::my_host() +}; + +const m::room +alias_room +{ + alias_room_id +}; + +void +_changed_aliases(const m::event &event) +{ + const m::room::id &room_id + { + at<"room_id"_>(event) + }; + + const json::array &aliases + { + at<"content"_>(event).get("aliases") + }; + + for(const json::string &alias_ : aliases) + { + const m::room::alias &alias{alias_}; + const auto event_id + { + send(alias_room, m::me.user_id, "ircd.alias", alias, json::strung{event}) + }; + + log::info + { + "Updated aliases of %s by %s in %s [%s] => %s", + string_view{room_id}, + json::get<"sender"_>(event), + json::get<"event_id"_>(event), + string_view{alias}, + string_view{event_id} + }; + } +} + +const m::hookfn<> +_changed_aliases_hookfn +{ + _changed_aliases, + { + { "_site", "vm.notify" }, + { "type", "m.room.aliases" }, + } +}; + +void +_can_change_aliases(const m::event &event) +{ + const m::room::id &room_id + { + at<"room_id"_>(event) + }; + + const json::array &aliases + { + at<"content"_>(event).get("aliases") + }; + + for(const json::string &alias_ : aliases) + { + const m::room::alias &alias{alias_}; + if(at<"origin"_>(event) != alias.host()) + throw m::ACCESS_DENIED + { + "Cannot set alias for host '%s' from origin '%s'", + alias.host(), + at<"origin"_>(event) + }; + } +} + +const m::hookfn<> +_can_change_aliases_hookfn +{ + _can_change_aliases, + { + { "_site", "vm.eval" }, + { "type", "m.room.aliases" }, + } +};