2018-02-04 03:22:01 +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.
|
2017-12-12 21:33:14 +01:00
|
|
|
|
|
|
|
using namespace ircd;
|
|
|
|
|
2018-02-15 22:06:49 +01:00
|
|
|
mapi::header
|
|
|
|
IRCD_MODULE
|
2017-12-12 21:33:14 +01:00
|
|
|
{
|
2018-02-15 22:06:49 +01:00
|
|
|
"Client 7.2 :Room aliases"
|
|
|
|
};
|
|
|
|
|
2018-03-01 05:42:47 +01:00
|
|
|
const ircd::m::room::id::buf
|
|
|
|
alias_room_id
|
|
|
|
{
|
|
|
|
"alias", ircd::my_host()
|
|
|
|
};
|
|
|
|
|
2018-02-15 22:06:49 +01:00
|
|
|
resource
|
|
|
|
directory_room_resource
|
|
|
|
{
|
|
|
|
"/_matrix/client/r0/directory/room/",
|
2017-12-12 21:33:14 +01:00
|
|
|
{
|
|
|
|
"(7.2) Room aliases",
|
|
|
|
directory_room_resource.DIRECTORY
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-02-26 13:55:27 +01:00
|
|
|
extern "C" m::id::room
|
|
|
|
room_id__room_alias(const mutable_buffer &out,
|
|
|
|
const m::id::room_alias &);
|
|
|
|
|
2018-03-08 20:39:09 +01:00
|
|
|
extern "C" bool
|
|
|
|
room_alias_exists(const m::id::room_alias &,
|
|
|
|
const bool &remote_query);
|
|
|
|
|
2017-12-12 21:33:14 +01:00
|
|
|
resource::response
|
2018-02-22 01:32:53 +01:00
|
|
|
get__directory_room(client &client,
|
|
|
|
const resource::request &request)
|
2017-12-12 21:33:14 +01:00
|
|
|
{
|
|
|
|
m::room::alias::buf room_alias
|
|
|
|
{
|
|
|
|
url::decode(request.parv[0], room_alias)
|
|
|
|
};
|
|
|
|
|
2018-02-26 13:55:27 +01:00
|
|
|
char buf[256];
|
2018-04-05 03:36:54 +02:00
|
|
|
const m::room::id room_id
|
2018-02-25 09:09:42 +01:00
|
|
|
{
|
2018-02-26 13:55:27 +01:00
|
|
|
m::room_id(buf, room_alias)
|
2018-02-25 09:09:42 +01:00
|
|
|
};
|
|
|
|
|
2017-12-12 21:33:14 +01:00
|
|
|
return resource::response
|
|
|
|
{
|
2018-02-26 13:55:27 +01:00
|
|
|
client, json::members
|
|
|
|
{
|
|
|
|
{ "room_id", room_id }
|
|
|
|
}
|
2017-12-12 21:33:14 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-02-15 22:06:49 +01:00
|
|
|
resource::method
|
|
|
|
directory_room_get
|
2017-12-12 21:33:14 +01:00
|
|
|
{
|
2018-02-22 01:32:53 +01:00
|
|
|
directory_room_resource, "GET", get__directory_room
|
|
|
|
};
|
|
|
|
|
|
|
|
resource::response
|
|
|
|
put__directory_room(client &client,
|
|
|
|
const resource::request &request)
|
|
|
|
{
|
|
|
|
m::room::alias::buf room_alias
|
|
|
|
{
|
|
|
|
url::decode(request.parv[0], room_alias)
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::room::id &room_id
|
|
|
|
{
|
|
|
|
unquote(request.at("room_id"))
|
|
|
|
};
|
|
|
|
|
|
|
|
if(false)
|
|
|
|
throw m::error
|
|
|
|
{
|
|
|
|
http::CONFLICT, "M_EXISTS",
|
|
|
|
"Room alias %s already exists",
|
|
|
|
room_alias
|
|
|
|
};
|
|
|
|
|
|
|
|
return resource::response
|
|
|
|
{
|
|
|
|
client, http::OK
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
resource::method
|
|
|
|
directory_room_put
|
|
|
|
{
|
|
|
|
directory_room_resource, "PUT", put__directory_room
|
2017-12-12 21:33:14 +01:00
|
|
|
};
|
2018-02-26 13:55:27 +01:00
|
|
|
|
2018-03-01 05:42:47 +01:00
|
|
|
static json::object
|
|
|
|
room_alias_fetch(const mutable_buffer &out,
|
|
|
|
const m::id::room_alias &alias);
|
|
|
|
|
2018-03-08 20:39:09 +01:00
|
|
|
bool
|
|
|
|
room_alias_exists(const m::id::room_alias &alias,
|
|
|
|
const bool &remote_query)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const m::room alias_room{alias_room_id};
|
|
|
|
if(alias_room.has("ircd.alias", alias))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if(!remote_query)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
char buf[256];
|
|
|
|
room_id__room_alias(buf, alias);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch(const m::NOT_FOUND &)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-09-05 06:12:47 +02:00
|
|
|
conf::item<seconds>
|
|
|
|
room_alias_cache_ttl
|
|
|
|
{
|
|
|
|
{ "name", "ircd.client.directory.room.alias.cache.ttl" },
|
|
|
|
{ "default", 60 * 60 * 72L },
|
|
|
|
};
|
|
|
|
|
2018-03-01 05:42:47 +01:00
|
|
|
/// Translate a room alias into a room_id. This function first checks the
|
|
|
|
/// local cache. A cache miss will then cause in a query to the remote, the
|
|
|
|
/// result of which will be added to cache.
|
2018-02-26 13:55:27 +01:00
|
|
|
m::id::room
|
|
|
|
room_id__room_alias(const mutable_buffer &out,
|
|
|
|
const m::id::room_alias &alias)
|
2018-03-08 20:39:09 +01:00
|
|
|
try
|
2018-02-26 13:55:27 +01:00
|
|
|
{
|
2018-03-01 05:42:47 +01:00
|
|
|
m::id::room ret;
|
|
|
|
const auto cache_closure{[&out, &ret]
|
|
|
|
(const m::event &event)
|
|
|
|
{
|
|
|
|
const string_view &room_id
|
|
|
|
{
|
|
|
|
unquote(at<"content"_>(event).get("room_id"))
|
|
|
|
};
|
|
|
|
|
2018-03-01 05:50:10 +01:00
|
|
|
if(!room_id)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const auto age
|
2018-03-01 05:42:47 +01:00
|
|
|
{
|
2018-03-01 05:50:10 +01:00
|
|
|
ircd::now() - milliseconds(at<"origin_server_ts"_>(event))
|
|
|
|
};
|
|
|
|
|
2018-09-05 06:12:47 +02:00
|
|
|
if(age > seconds(room_alias_cache_ttl))
|
2018-03-01 05:50:10 +01:00
|
|
|
return;
|
|
|
|
|
2018-03-01 13:47:14 +01:00
|
|
|
ret = string_view { data(out), copy(out, room_id) };
|
2018-03-01 05:42:47 +01:00
|
|
|
}};
|
|
|
|
|
|
|
|
const m::room alias_room{alias_room_id};
|
|
|
|
alias_room.get(std::nothrow, "ircd.alias", alias, cache_closure);
|
|
|
|
if(ret)
|
|
|
|
return ret;
|
2018-02-26 13:55:27 +01:00
|
|
|
|
2018-03-01 05:42:47 +01:00
|
|
|
// Buf has to hold our output headers, their received headers, and
|
|
|
|
// the received aliasing content.
|
2018-02-26 13:55:27 +01:00
|
|
|
const unique_buffer<mutable_buffer> buf
|
|
|
|
{
|
2018-03-01 05:42:47 +01:00
|
|
|
32_KiB
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::object &response
|
|
|
|
{
|
|
|
|
room_alias_fetch(buf, alias)
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::id::room &room_id
|
|
|
|
{
|
|
|
|
unquote(response.at("room_id"))
|
|
|
|
};
|
|
|
|
|
2018-04-05 03:36:54 +02:00
|
|
|
// Cache the result
|
|
|
|
send(alias_room, m::me.user_id, "ircd.alias", alias, response);
|
|
|
|
|
2018-03-01 05:42:47 +01:00
|
|
|
return m::room::id
|
|
|
|
{
|
|
|
|
string_view
|
|
|
|
{
|
|
|
|
data(out), copy(out, room_id)
|
|
|
|
}
|
2018-02-26 13:55:27 +01:00
|
|
|
};
|
2018-03-01 05:42:47 +01:00
|
|
|
}
|
2018-03-08 20:39:09 +01:00
|
|
|
catch(const http::error &e)
|
|
|
|
{
|
|
|
|
if(e.code == http::NOT_FOUND)
|
|
|
|
throw m::NOT_FOUND{};
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
2018-04-05 03:36:54 +02:00
|
|
|
catch(const json::not_found &e)
|
|
|
|
{
|
|
|
|
throw m::NOT_FOUND
|
|
|
|
{
|
|
|
|
"Remote sent malformed response: %s", e.what()
|
|
|
|
};
|
|
|
|
}
|
2018-02-26 13:55:27 +01:00
|
|
|
|
2018-09-05 06:12:47 +02:00
|
|
|
conf::item<seconds>
|
|
|
|
room_alias_fetch_timeout
|
|
|
|
{
|
|
|
|
{ "name", "ircd.client.directory.room.alias.fetch.timeout" },
|
|
|
|
{ "default", 20L },
|
|
|
|
};
|
|
|
|
|
2018-03-01 05:42:47 +01:00
|
|
|
/// This function makes a room alias request to a remote. The alias
|
|
|
|
/// room cache is not checked or updated from here, this is only the
|
|
|
|
/// query operation.
|
|
|
|
json::object
|
|
|
|
room_alias_fetch(const mutable_buffer &out,
|
|
|
|
const m::id::room_alias &alias)
|
|
|
|
{
|
2018-02-26 13:55:27 +01:00
|
|
|
m::v1::query::directory federation_request
|
|
|
|
{
|
2018-03-01 05:42:47 +01:00
|
|
|
alias, out
|
2018-02-26 13:55:27 +01:00
|
|
|
};
|
|
|
|
|
2018-09-05 06:12:47 +02:00
|
|
|
if(!federation_request.wait(seconds(room_alias_fetch_timeout), std::nothrow))
|
2018-02-26 13:55:27 +01:00
|
|
|
throw http::error
|
|
|
|
{
|
|
|
|
http::REQUEST_TIMEOUT
|
|
|
|
};
|
|
|
|
|
|
|
|
const http::code &code
|
|
|
|
{
|
|
|
|
federation_request.get()
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::object &response
|
|
|
|
{
|
|
|
|
federation_request
|
|
|
|
};
|
|
|
|
|
2018-03-01 05:42:47 +01:00
|
|
|
return response;
|
|
|
|
}
|
2018-02-26 13:55:27 +01:00
|
|
|
|
2018-10-07 07:17:46 +02:00
|
|
|
const m::hookfn<m::vm::eval &>
|
2018-03-01 05:42:47 +01:00
|
|
|
_create_alias_room
|
|
|
|
{
|
2018-02-26 13:55:27 +01:00
|
|
|
{
|
2018-10-07 07:17:46 +02:00
|
|
|
{ "_site", "vm.effect" },
|
2018-03-03 11:38:05 +01:00
|
|
|
{ "room_id", "!ircd" },
|
|
|
|
{ "type", "m.room.create" },
|
2018-03-01 05:42:47 +01:00
|
|
|
},
|
2018-05-27 07:05:56 +02:00
|
|
|
|
2018-10-07 07:17:46 +02:00
|
|
|
[](const m::event &, m::vm::eval &)
|
2018-02-26 13:55:27 +01:00
|
|
|
{
|
2018-03-01 05:42:47 +01:00
|
|
|
m::create(alias_room_id, m::me.user_id);
|
|
|
|
}
|
|
|
|
};
|