2018-06-12 08:11:28 +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-21 01:45:18 +02:00
|
|
|
namespace ircd::m
|
|
|
|
{
|
2019-07-21 01:50:05 +02:00
|
|
|
extern conf::item<seconds> alias_fetch_timeout;
|
|
|
|
extern conf::item<seconds> alias_cache_ttl;
|
|
|
|
extern const room::id::buf alias_room_id;
|
|
|
|
extern const room alias_room;
|
|
|
|
|
2019-08-18 08:19:05 +02:00
|
|
|
static void auth_room_aliases(const event &, room::auth::hookdata &);
|
|
|
|
extern hookfn<room::auth::hookdata &> auth_room_aliases_hookfn;
|
2019-07-21 01:45:18 +02:00
|
|
|
|
|
|
|
static void changed_room_aliases(const event &, vm::eval &);
|
|
|
|
extern hookfn<vm::eval &> changed_room_aliases_hookfn;
|
|
|
|
|
2019-07-21 01:50:05 +02:00
|
|
|
extern hookfn<m::vm::eval &> create_alias_room_hookfn;
|
|
|
|
}
|
2018-06-12 08:11:28 +02:00
|
|
|
|
2019-07-21 01:45:18 +02:00
|
|
|
ircd::mapi::header
|
2018-06-12 08:11:28 +02:00
|
|
|
IRCD_MODULE
|
|
|
|
{
|
|
|
|
"Matrix m.room.aliases"
|
|
|
|
};
|
|
|
|
|
2019-07-21 01:50:05 +02:00
|
|
|
decltype(ircd::m::alias_room_id)
|
|
|
|
ircd::m::alias_room_id
|
2018-06-12 08:11:28 +02:00
|
|
|
{
|
|
|
|
"alias", ircd::my_host()
|
|
|
|
};
|
|
|
|
|
2019-07-21 01:50:05 +02:00
|
|
|
decltype(ircd::m::alias_room)
|
|
|
|
ircd::m::alias_room
|
2018-06-12 08:11:28 +02:00
|
|
|
{
|
|
|
|
alias_room_id
|
|
|
|
};
|
|
|
|
|
2019-07-21 01:50:05 +02:00
|
|
|
decltype(ircd::m::alias_cache_ttl)
|
|
|
|
ircd::m::alias_cache_ttl
|
2019-03-31 01:16:15 +01:00
|
|
|
{
|
|
|
|
{ "name", "ircd.m.room.aliases.cache.ttl" },
|
|
|
|
{ "default", 604800L },
|
|
|
|
};
|
|
|
|
|
2019-07-21 01:50:05 +02:00
|
|
|
decltype(ircd::m::alias_fetch_timeout)
|
|
|
|
ircd::m::alias_fetch_timeout
|
2019-03-31 01:52:28 +01:00
|
|
|
{
|
|
|
|
{ "name", "ircd.m.room.aliases.fetch.timeout" },
|
|
|
|
{ "default", 10L },
|
|
|
|
};
|
|
|
|
|
2019-03-31 03:10:14 +02:00
|
|
|
//
|
2019-07-21 01:45:18 +02:00
|
|
|
// create the alias room as an effect of !ircd created on bootstrap
|
2019-03-31 03:10:14 +02:00
|
|
|
//
|
|
|
|
|
2019-07-21 01:50:05 +02:00
|
|
|
decltype(ircd::m::create_alias_room_hookfn)
|
|
|
|
ircd::m::create_alias_room_hookfn
|
2019-03-31 03:10:14 +02:00
|
|
|
{
|
|
|
|
{
|
|
|
|
{ "_site", "vm.effect" },
|
|
|
|
{ "room_id", "!ircd" },
|
|
|
|
{ "type", "m.room.create" },
|
|
|
|
},
|
2019-07-21 01:50:05 +02:00
|
|
|
|
2019-03-31 03:10:14 +02:00
|
|
|
[](const m::event &, m::vm::eval &)
|
|
|
|
{
|
2019-07-21 01:50:05 +02:00
|
|
|
create(alias_room_id, m::me.user_id);
|
2019-03-31 03:10:14 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-07-21 01:45:18 +02:00
|
|
|
//
|
|
|
|
// an effect of room aliases changed
|
|
|
|
//
|
2019-03-31 03:10:14 +02:00
|
|
|
|
2019-07-21 01:45:18 +02:00
|
|
|
decltype(ircd::m::changed_room_aliases_hookfn)
|
|
|
|
ircd::m::changed_room_aliases_hookfn
|
2019-03-31 03:10:14 +02:00
|
|
|
{
|
2019-07-21 01:45:18 +02:00
|
|
|
changed_room_aliases,
|
2019-03-31 03:10:14 +02:00
|
|
|
{
|
|
|
|
{ "_site", "vm.effect" },
|
|
|
|
{ "type", "m.room.aliases" },
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
2019-07-21 01:45:18 +02:00
|
|
|
ircd::m::changed_room_aliases(const m::event &event,
|
|
|
|
m::vm::eval &)
|
2019-03-31 03:10:14 +02:00
|
|
|
{
|
|
|
|
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) try
|
|
|
|
{
|
|
|
|
m::room::aliases::cache::set(alias, room_id);
|
|
|
|
|
|
|
|
log::info
|
|
|
|
{
|
|
|
|
m::log, "Updated aliases of %s by %s in %s with %s",
|
|
|
|
string_view{room_id},
|
|
|
|
json::get<"sender"_>(event),
|
2019-07-06 07:01:34 +02:00
|
|
|
string_view{event.event_id},
|
2019-03-31 03:10:14 +02:00
|
|
|
string_view{alias},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
m::log, "Updating aliases of %s by %s in %s with %s :%s",
|
|
|
|
string_view{room_id},
|
|
|
|
json::get<"sender"_>(event),
|
2019-07-06 07:01:34 +02:00
|
|
|
string_view{event.event_id},
|
2019-03-31 03:10:14 +02:00
|
|
|
string_view{alias},
|
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-21 01:45:18 +02:00
|
|
|
//
|
|
|
|
// auth handler
|
|
|
|
//
|
|
|
|
|
|
|
|
decltype(ircd::m::auth_room_aliases_hookfn)
|
|
|
|
ircd::m::auth_room_aliases_hookfn
|
|
|
|
{
|
|
|
|
auth_room_aliases,
|
|
|
|
{
|
2019-08-18 08:19:05 +02:00
|
|
|
{ "_site", "room.auth" },
|
2019-07-21 01:45:18 +02:00
|
|
|
{ "type", "m.room.aliases" },
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::auth_room_aliases(const event &event,
|
2019-08-18 08:19:05 +02:00
|
|
|
room::auth::hookdata &data)
|
2019-07-21 01:45:18 +02:00
|
|
|
{
|
2019-08-18 08:19:05 +02:00
|
|
|
using FAIL = m::room::auth::FAIL;
|
2019-07-21 01:45:18 +02:00
|
|
|
using conforms = m::event::conforms;
|
|
|
|
|
|
|
|
// 4. If type is m.room.aliases:
|
|
|
|
assert(json::get<"type"_>(event) == "m.room.aliases");
|
|
|
|
|
|
|
|
// a. If event has no state_key, reject.
|
|
|
|
if(empty(json::get<"state_key"_>(event)))
|
|
|
|
throw FAIL
|
|
|
|
{
|
|
|
|
"m.room.aliases event is missing a state_key."
|
|
|
|
};
|
|
|
|
|
|
|
|
// b. If sender's domain doesn't matches state_key, reject.
|
|
|
|
if(json::get<"state_key"_>(event) != m::user::id(json::get<"sender"_>(event)).host())
|
|
|
|
throw FAIL
|
|
|
|
{
|
|
|
|
"m.room.aliases event state_key is not the sender's domain."
|
|
|
|
};
|
|
|
|
|
|
|
|
// c. Otherwise, allow
|
|
|
|
data.allow = true;
|
|
|
|
}
|
|
|
|
|
2019-03-31 00:02:40 +01:00
|
|
|
//
|
2019-03-31 01:04:35 +01:00
|
|
|
// m::room::aliases
|
2019-03-31 00:02:40 +01:00
|
|
|
//
|
|
|
|
|
|
|
|
bool
|
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::room::aliases::for_each(const m::room &room,
|
|
|
|
const string_view &server,
|
|
|
|
const closure_bool &closure)
|
|
|
|
{
|
|
|
|
const room::state state
|
|
|
|
{
|
|
|
|
room
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(server);
|
|
|
|
const event::idx &event_idx
|
|
|
|
{
|
|
|
|
state.get(std::nothrow, "m.room.aliases", server)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!event_idx)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
bool ret{true};
|
|
|
|
m::get(std::nothrow, event_idx, "content", [&closure, &ret]
|
|
|
|
(const json::object &content)
|
|
|
|
{
|
|
|
|
const json::array &aliases
|
|
|
|
{
|
|
|
|
content["aliases"]
|
|
|
|
};
|
|
|
|
|
|
|
|
for(auto it(begin(aliases)); it != end(aliases) && ret; ++it)
|
|
|
|
{
|
|
|
|
const json::string &alias(*it);
|
|
|
|
if(!valid(m::id::ROOM_ALIAS, alias))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if(!closure(alias))
|
|
|
|
ret = false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-03-31 01:04:35 +01:00
|
|
|
//
|
|
|
|
// m::room::aliases::cache
|
|
|
|
//
|
|
|
|
|
|
|
|
bool
|
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::room::aliases::cache::del(const alias &alias)
|
|
|
|
{
|
2019-08-03 06:16:42 +02:00
|
|
|
char buf[m::id::room_alias::buf::SIZE];
|
2019-03-31 01:04:35 +01:00
|
|
|
const string_view &key
|
|
|
|
{
|
2019-08-03 06:16:42 +02:00
|
|
|
make_key(buf, alias)
|
2019-03-31 01:04:35 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const auto &event_idx
|
|
|
|
{
|
2019-03-31 01:52:28 +01:00
|
|
|
alias_room.get(std::nothrow, "ircd.room.alias", key)
|
2019-03-31 01:04:35 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
if(!event_idx)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const auto event_id
|
|
|
|
{
|
2019-03-31 01:52:28 +01:00
|
|
|
m::event_id(event_idx, std::nothrow)
|
2019-03-31 01:04:35 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
if(!event_id)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const auto ret
|
|
|
|
{
|
|
|
|
redact(alias_room, m::me.user_id, event_id, "deleted")
|
|
|
|
};
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::room::aliases::cache::set(const alias &alias,
|
|
|
|
const id &id)
|
|
|
|
{
|
2019-08-03 06:16:42 +02:00
|
|
|
char buf[m::id::room_alias::buf::SIZE];
|
2019-03-31 01:04:35 +01:00
|
|
|
const string_view &key
|
|
|
|
{
|
2019-08-03 06:16:42 +02:00
|
|
|
make_key(buf, alias)
|
2019-03-31 01:04:35 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const auto ret
|
|
|
|
{
|
|
|
|
send(alias_room, m::me.user_id, "ircd.room.alias", key,
|
|
|
|
{
|
|
|
|
{ "room_id", id }
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::room::aliases::cache::get(std::nothrow_t,
|
|
|
|
const alias &alias,
|
|
|
|
const id::closure &closure)
|
|
|
|
{
|
2019-03-31 01:52:28 +01:00
|
|
|
m::event::idx event_idx
|
2019-03-31 01:04:35 +01:00
|
|
|
{
|
2019-08-03 00:59:58 +02:00
|
|
|
getidx(alias)
|
2019-03-31 01:04:35 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
if(!event_idx)
|
2019-03-31 01:52:28 +01:00
|
|
|
{
|
2019-03-31 03:12:46 +02:00
|
|
|
if(my_host(alias.host()))
|
|
|
|
return false;
|
|
|
|
|
2019-03-31 01:52:28 +01:00
|
|
|
if(!fetch(std::nothrow, alias, alias.host()))
|
|
|
|
return false;
|
|
|
|
|
2019-08-03 00:59:58 +02:00
|
|
|
event_idx = getidx(alias);
|
2019-03-31 01:52:28 +01:00
|
|
|
}
|
2019-03-31 01:16:15 +01:00
|
|
|
|
2019-07-24 05:46:26 +02:00
|
|
|
const bool expired
|
|
|
|
{
|
2019-08-03 00:59:58 +02:00
|
|
|
!my_host(alias.host()) && cache::expired(event_idx)
|
2019-07-24 05:46:26 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if(expired)
|
|
|
|
{
|
|
|
|
log::dwarning
|
|
|
|
{
|
2019-08-03 00:59:58 +02:00
|
|
|
log, "Cached alias %s expired age:%ld ttl:%ld",
|
2019-07-24 05:46:26 +02:00
|
|
|
string_view{alias},
|
2019-08-03 00:59:58 +02:00
|
|
|
cache::age(event_idx).count(),
|
|
|
|
milliseconds(seconds(alias_cache_ttl)).count(),
|
2019-07-24 05:46:26 +02:00
|
|
|
};
|
|
|
|
|
2019-04-16 00:18:54 +02:00
|
|
|
fetch(std::nothrow, alias, alias.host());
|
2019-08-03 00:59:58 +02:00
|
|
|
event_idx = getidx(alias);
|
2019-03-31 01:52:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!event_idx)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool ret{false};
|
2019-03-31 01:04:35 +01:00
|
|
|
m::get(std::nothrow, event_idx, "content", [&closure, &ret]
|
|
|
|
(const json::object &content)
|
|
|
|
{
|
|
|
|
const json::string &room_id
|
|
|
|
{
|
|
|
|
content.get("room_id")
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!empty(room_id))
|
|
|
|
{
|
|
|
|
ret = true;
|
|
|
|
closure(room_id);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-03-31 01:16:15 +01:00
|
|
|
namespace ircd::m
|
|
|
|
{
|
|
|
|
thread_local char room_aliases_cache_fetch_hpbuf[384];
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::room::aliases::cache::fetch(const alias &alias,
|
|
|
|
const net::hostport &hp)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const unique_buffer<mutable_buffer> buf
|
|
|
|
{
|
|
|
|
16_KiB
|
|
|
|
};
|
|
|
|
|
|
|
|
m::v1::query::opts opts;
|
|
|
|
opts.remote = hp;
|
2019-08-03 00:09:53 +02:00
|
|
|
opts.dynamic = true;
|
2019-03-31 01:16:15 +01:00
|
|
|
|
|
|
|
m::v1::query::directory request
|
|
|
|
{
|
|
|
|
alias, buf, std::move(opts)
|
|
|
|
};
|
|
|
|
|
|
|
|
request.wait(seconds(alias_fetch_timeout));
|
|
|
|
const http::code &code
|
|
|
|
{
|
|
|
|
request.get()
|
|
|
|
};
|
|
|
|
|
2019-06-23 01:12:14 +02:00
|
|
|
const json::object response
|
2019-03-31 01:16:15 +01:00
|
|
|
{
|
|
|
|
request
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!response.has("room_id"))
|
|
|
|
throw m::NOT_FOUND
|
|
|
|
{
|
|
|
|
"Server '%s' does not know room_id for %s",
|
|
|
|
string(room_aliases_cache_fetch_hpbuf, hp),
|
|
|
|
string_view{alias},
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::room::id &room_id
|
|
|
|
{
|
|
|
|
unquote(response["room_id"])
|
|
|
|
};
|
|
|
|
|
|
|
|
set(alias, room_id);
|
|
|
|
}
|
|
|
|
catch(const ctx::timeout &e)
|
|
|
|
{
|
|
|
|
throw m::error
|
|
|
|
{
|
|
|
|
http::GATEWAY_TIMEOUT, "M_ROOM_ALIAS_TIMEOUT",
|
|
|
|
"Server '%s' did not respond with a room_id for %s in time",
|
|
|
|
string(room_aliases_cache_fetch_hpbuf, hp),
|
|
|
|
string_view{alias},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
catch(const server::unavailable &e)
|
|
|
|
{
|
|
|
|
throw m::error
|
|
|
|
{
|
|
|
|
http::BAD_GATEWAY, "M_ROOM_ALIAS_UNAVAILABLE",
|
|
|
|
"Server '%s' is not available to query a room_id for %s",
|
|
|
|
string(room_aliases_cache_fetch_hpbuf, hp),
|
|
|
|
string_view{alias},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-03-31 01:04:35 +01:00
|
|
|
bool
|
|
|
|
IRCD_MODULE_EXPORT
|
2019-08-03 00:59:58 +02:00
|
|
|
ircd::m::room::aliases::cache::for_each(const string_view &server,
|
|
|
|
const closure_bool &closure)
|
2019-03-31 01:04:35 +01:00
|
|
|
{
|
2019-08-03 00:59:58 +02:00
|
|
|
const m::room::state state
|
2019-03-31 01:04:35 +01:00
|
|
|
{
|
2019-08-03 00:59:58 +02:00
|
|
|
alias_room
|
2019-03-31 01:04:35 +01:00
|
|
|
};
|
|
|
|
|
2019-08-03 00:59:58 +02:00
|
|
|
bool ret{true};
|
|
|
|
const m::room::state::closure_bool reclosure{[&server, &closure, &ret]
|
|
|
|
(const string_view &type, const string_view &state_key, const m::event::idx &event_idx)
|
|
|
|
{
|
|
|
|
thread_local char swapbuf[m::id::room_alias::buf::SIZE];
|
|
|
|
const alias &alias
|
|
|
|
{
|
|
|
|
m::id::unswap(state_key, swapbuf)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(server && alias.host() != server)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(expired(event_idx))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
m::get(std::nothrow, event_idx, "content", [&closure, &ret, &alias]
|
|
|
|
(const json::object &content)
|
|
|
|
{
|
|
|
|
const json::string &room_id
|
|
|
|
{
|
|
|
|
content.get("room_id")
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!empty(room_id))
|
|
|
|
ret = closure(alias, room_id);
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}};
|
|
|
|
|
|
|
|
state.for_each("ircd.room.alias", server, reclosure);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::room::aliases::cache::has(const alias &alias)
|
|
|
|
{
|
2019-03-31 01:16:15 +01:00
|
|
|
const auto &event_idx
|
|
|
|
{
|
2019-08-03 00:59:58 +02:00
|
|
|
getidx(alias)
|
2019-03-31 01:16:15 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
if(!event_idx)
|
|
|
|
return false;
|
|
|
|
|
2019-08-03 00:59:58 +02:00
|
|
|
if(expired(event_idx))
|
2019-03-31 01:16:15 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
bool ret{false};
|
|
|
|
m::get(std::nothrow, event_idx, "content", [&ret]
|
|
|
|
(const json::object &content)
|
|
|
|
{
|
|
|
|
const json::string &room_id
|
|
|
|
{
|
|
|
|
content.get("room_id")
|
|
|
|
};
|
|
|
|
|
|
|
|
ret = !empty(room_id);
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
2019-03-31 01:04:35 +01:00
|
|
|
}
|
|
|
|
|
2019-08-03 00:59:58 +02:00
|
|
|
ircd::system_point
|
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::room::aliases::cache::expires(const alias &alias)
|
|
|
|
{
|
|
|
|
const auto event_idx
|
|
|
|
{
|
|
|
|
getidx(alias)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!event_idx)
|
|
|
|
return system_point::min();
|
|
|
|
|
|
|
|
const milliseconds age
|
|
|
|
{
|
|
|
|
cache::age(event_idx)
|
|
|
|
};
|
|
|
|
|
|
|
|
const seconds ttl
|
|
|
|
{
|
|
|
|
alias_cache_ttl
|
|
|
|
};
|
|
|
|
|
|
|
|
return now<system_point>() + (ttl - age);
|
|
|
|
}
|
|
|
|
|
2019-03-31 01:04:35 +01:00
|
|
|
bool
|
|
|
|
IRCD_MODULE_EXPORT
|
2019-08-03 00:59:58 +02:00
|
|
|
ircd::m::room::aliases::cache::expired(const event::idx &event_idx)
|
2019-03-31 01:04:35 +01:00
|
|
|
{
|
2019-08-03 00:59:58 +02:00
|
|
|
const milliseconds age
|
2019-03-31 01:04:35 +01:00
|
|
|
{
|
2019-08-03 00:59:58 +02:00
|
|
|
cache::age(event_idx)
|
2019-03-31 01:04:35 +01:00
|
|
|
};
|
|
|
|
|
2019-08-03 00:59:58 +02:00
|
|
|
const seconds ttl
|
2019-03-31 01:04:35 +01:00
|
|
|
{
|
2019-08-03 00:59:58 +02:00
|
|
|
alias_cache_ttl
|
|
|
|
};
|
2019-03-31 01:04:35 +01:00
|
|
|
|
2019-08-03 00:59:58 +02:00
|
|
|
return age > ttl;
|
|
|
|
}
|
2019-03-31 01:04:35 +01:00
|
|
|
|
2019-08-03 00:59:58 +02:00
|
|
|
ircd::milliseconds
|
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::room::aliases::cache::age(const event::idx &event_idx)
|
|
|
|
{
|
|
|
|
time_t ts;
|
|
|
|
if(!m::get(event_idx, "origin_server_ts", ts))
|
|
|
|
return milliseconds::max();
|
2019-03-31 01:16:15 +01:00
|
|
|
|
2019-08-03 00:59:58 +02:00
|
|
|
const time_t now
|
|
|
|
{
|
|
|
|
ircd::time<milliseconds>()
|
|
|
|
};
|
2019-03-31 01:16:15 +01:00
|
|
|
|
2019-08-03 00:59:58 +02:00
|
|
|
return milliseconds
|
|
|
|
{
|
|
|
|
now - ts
|
|
|
|
};
|
|
|
|
}
|
2019-03-31 01:04:35 +01:00
|
|
|
|
2019-08-03 00:59:58 +02:00
|
|
|
ircd::m::event::idx
|
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::room::aliases::cache::getidx(const alias &alias)
|
|
|
|
{
|
2019-08-03 06:16:42 +02:00
|
|
|
thread_local char swapbuf[m::id::room_alias::buf::SIZE];
|
|
|
|
const string_view &swapped
|
2019-08-03 00:59:58 +02:00
|
|
|
{
|
|
|
|
alias.swap(swapbuf)
|
|
|
|
};
|
2019-03-31 01:04:35 +01:00
|
|
|
|
2019-08-03 06:16:42 +02:00
|
|
|
char buf[m::id::room_alias::buf::SIZE];
|
|
|
|
const string_view &key
|
|
|
|
{
|
|
|
|
tolower(buf, swapped)
|
|
|
|
};
|
|
|
|
|
2019-08-03 00:59:58 +02:00
|
|
|
const auto &event_idx
|
|
|
|
{
|
|
|
|
alias_room.get(std::nothrow, "ircd.room.alias", key)
|
|
|
|
};
|
2019-03-31 01:04:35 +01:00
|
|
|
|
2019-08-03 00:59:58 +02:00
|
|
|
return event_idx;
|
2019-03-31 01:04:35 +01:00
|
|
|
}
|
2019-08-03 06:16:42 +02:00
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::room::aliases::cache::make_key(const mutable_buffer &out,
|
|
|
|
const alias &alias)
|
|
|
|
{
|
|
|
|
|
|
|
|
thread_local char swapbuf[m::id::room_alias::buf::SIZE] alignas(16);
|
|
|
|
const string_view &swapped
|
|
|
|
{
|
|
|
|
alias.swap(swapbuf)
|
|
|
|
};
|
|
|
|
|
|
|
|
const string_view &key
|
|
|
|
{
|
|
|
|
tolower(out, swapped)
|
|
|
|
};
|
|
|
|
|
|
|
|
return key;
|
|
|
|
}
|