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-11-16 02:37:09 +01:00
|
|
|
|
2018-03-28 22:05:15 +02:00
|
|
|
decltype(ircd::m::log)
|
|
|
|
ircd::m::log
|
|
|
|
{
|
|
|
|
"matrix", 'm'
|
|
|
|
};
|
|
|
|
|
2017-12-12 21:33:14 +01:00
|
|
|
//
|
|
|
|
// init
|
|
|
|
//
|
|
|
|
|
2018-05-12 04:29:09 +02:00
|
|
|
ircd::conf::item<std::string>
|
|
|
|
me_online_status_msg
|
|
|
|
{
|
|
|
|
{ "name", "ircd.me.online.status_msg" },
|
|
|
|
{ "default", "Wanna chat? IRCd at your service!" }
|
|
|
|
};
|
|
|
|
|
|
|
|
ircd::conf::item<std::string>
|
|
|
|
me_offline_status_msg
|
|
|
|
{
|
|
|
|
{ "name", "ircd.me.offline.status_msg" },
|
|
|
|
{ "default", "Catch ya on the flip side..." }
|
|
|
|
};
|
|
|
|
|
2018-03-28 22:27:39 +02:00
|
|
|
//
|
|
|
|
// init::init
|
|
|
|
//
|
|
|
|
|
2018-12-01 01:46:34 +01:00
|
|
|
ircd::m::init::init(const string_view &origin,
|
2019-03-28 21:07:30 +01:00
|
|
|
const string_view &servername)
|
2017-12-12 21:33:14 +01:00
|
|
|
try
|
2018-08-15 01:47:42 +02:00
|
|
|
:_self
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
2019-03-28 21:07:30 +01:00
|
|
|
origin, servername
|
2018-04-23 00:17:07 +02:00
|
|
|
}
|
2019-06-07 02:34:33 +02:00
|
|
|
,_dbs
|
|
|
|
{
|
|
|
|
self::servername, std::string{}
|
|
|
|
}
|
2018-09-21 23:59:58 +02:00
|
|
|
,_modules
|
|
|
|
{
|
|
|
|
std::make_unique<modules>()
|
|
|
|
}
|
2018-03-28 22:27:39 +02:00
|
|
|
{
|
2019-07-15 00:42:36 +02:00
|
|
|
if(!ircd::write_avoid && vm::sequence::retired != 0)
|
2019-04-15 20:08:40 +02:00
|
|
|
presence::set(me, "online", me_online_status_msg);
|
2017-11-16 02:37:09 +01:00
|
|
|
}
|
2019-06-07 04:01:53 +02:00
|
|
|
catch(const m::error &e)
|
2017-11-16 02:37:09 +01:00
|
|
|
{
|
2019-06-07 04:01:53 +02:00
|
|
|
log::error
|
2018-06-15 06:29:30 +02:00
|
|
|
{
|
2019-06-07 04:01:53 +02:00
|
|
|
log, "Failed to start matrix (%u) %s :%s :%s",
|
|
|
|
uint(e.code),
|
|
|
|
http::status(e.code),
|
|
|
|
e.errcode(),
|
|
|
|
e.errstr(),
|
2018-06-15 06:29:30 +02:00
|
|
|
};
|
2018-09-21 23:59:58 +02:00
|
|
|
|
|
|
|
throw;
|
2017-11-30 19:51:01 +01:00
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2019-06-07 04:01:53 +02:00
|
|
|
log::error
|
2018-06-15 06:29:30 +02:00
|
|
|
{
|
2018-09-21 23:59:58 +02:00
|
|
|
log, "Failed to start matrix :%s", e.what()
|
2018-06-15 06:29:30 +02:00
|
|
|
};
|
2018-09-21 23:59:58 +02:00
|
|
|
|
|
|
|
throw;
|
2017-11-16 02:37:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::init::~init()
|
|
|
|
noexcept try
|
|
|
|
{
|
2019-06-07 04:01:53 +02:00
|
|
|
if(m::sync::pool.size())
|
|
|
|
m::sync::pool.join();
|
2019-01-09 00:10:06 +01:00
|
|
|
|
2019-04-15 20:08:40 +02:00
|
|
|
if(!std::uncaught_exceptions() && !ircd::write_avoid)
|
2018-08-31 05:57:36 +02:00
|
|
|
presence::set(me, "offline", me_offline_status_msg);
|
2017-11-16 02:37:09 +01:00
|
|
|
}
|
|
|
|
catch(const m::error &e)
|
|
|
|
{
|
2018-08-17 19:37:12 +02:00
|
|
|
log::critical
|
|
|
|
{
|
|
|
|
log, "%s %s", e.what(), e.content
|
|
|
|
};
|
|
|
|
|
2017-11-26 01:20:42 +01:00
|
|
|
ircd::terminate();
|
2017-11-16 02:37:09 +01:00
|
|
|
}
|
|
|
|
|
2018-05-15 02:24:34 +02:00
|
|
|
void
|
|
|
|
ircd::m::init::close()
|
|
|
|
{
|
2019-06-27 08:05:18 +02:00
|
|
|
mods::imports.erase("net_listener"s);
|
2018-05-15 02:24:34 +02:00
|
|
|
}
|
|
|
|
|
2019-06-07 04:01:53 +02:00
|
|
|
//
|
|
|
|
// init::modules
|
|
|
|
//
|
|
|
|
|
|
|
|
namespace ircd::m
|
2018-08-17 19:37:12 +02:00
|
|
|
{
|
2019-06-14 00:24:56 +02:00
|
|
|
extern const std::vector<string_view> module_names;
|
2019-06-14 00:25:17 +02:00
|
|
|
extern const std::vector<string_view> module_names_optional;
|
2018-09-21 23:59:58 +02:00
|
|
|
}
|
2018-08-31 06:17:05 +02:00
|
|
|
|
2019-06-07 04:01:53 +02:00
|
|
|
ircd::m::init::modules::modules()
|
2018-08-17 19:37:12 +02:00
|
|
|
{
|
2019-06-07 04:01:53 +02:00
|
|
|
const unwind::exceptional unload{[this]
|
2018-08-17 19:37:12 +02:00
|
|
|
{
|
2019-06-07 04:01:53 +02:00
|
|
|
this->fini_imports();
|
|
|
|
}};
|
2018-08-17 19:37:12 +02:00
|
|
|
|
2019-06-07 04:01:53 +02:00
|
|
|
init_imports();
|
2018-09-21 23:59:58 +02:00
|
|
|
}
|
2019-06-07 04:01:53 +02:00
|
|
|
|
|
|
|
ircd::m::init::modules::~modules()
|
|
|
|
noexcept
|
2018-09-21 23:59:58 +02:00
|
|
|
{
|
2019-06-07 04:01:53 +02:00
|
|
|
fini_imports();
|
2018-08-17 19:37:12 +02:00
|
|
|
}
|
|
|
|
|
2018-09-21 23:59:58 +02:00
|
|
|
void
|
|
|
|
ircd::m::init::modules::init_imports()
|
2017-12-12 21:33:14 +01:00
|
|
|
{
|
2018-12-09 00:17:13 +01:00
|
|
|
if(!bool(ircd::mods::autoload))
|
2018-03-17 03:03:09 +01:00
|
|
|
{
|
|
|
|
log::warning
|
|
|
|
{
|
|
|
|
"Not loading modules because noautomod flag is set. "
|
|
|
|
"You may still load modules manually."
|
|
|
|
};
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-06-14 00:25:17 +02:00
|
|
|
for(const auto &name : module_names) try
|
|
|
|
{
|
2019-06-05 02:53:28 +02:00
|
|
|
mods::imports.emplace(name, name);
|
2019-06-14 00:25:17 +02:00
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
const auto &optional(module_names_optional);
|
|
|
|
if(std::count(begin(optional), end(optional), name))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
2018-09-14 03:02:10 +02:00
|
|
|
|
2019-07-15 00:42:36 +02:00
|
|
|
if(vm::sequence::retired == 0)
|
|
|
|
{
|
|
|
|
log::notice
|
|
|
|
{
|
|
|
|
log, "This appears to be your first time running IRCd because the events "
|
|
|
|
"database is empty. I will be bootstrapping it with initial events now..."
|
|
|
|
};
|
|
|
|
|
|
|
|
const module m_init_bootstrap
|
|
|
|
{
|
|
|
|
"m_init_bootstrap"
|
|
|
|
};
|
|
|
|
|
|
|
|
const mods::import<void ()> bootstrap
|
|
|
|
{
|
|
|
|
m_init_bootstrap, "ircd::m::init::bootstrap"
|
|
|
|
};
|
|
|
|
|
2018-08-15 01:47:42 +02:00
|
|
|
bootstrap();
|
2019-07-15 00:42:36 +02:00
|
|
|
}
|
2017-12-12 21:33:14 +01:00
|
|
|
}
|
2018-09-21 23:59:58 +02:00
|
|
|
|
2019-06-07 04:01:53 +02:00
|
|
|
void
|
|
|
|
ircd::m::init::modules::fini_imports()
|
2018-09-21 23:59:58 +02:00
|
|
|
noexcept
|
2018-03-28 22:27:39 +02:00
|
|
|
{
|
2019-06-24 01:27:45 +02:00
|
|
|
// Stop the vm (unload) this first even though it loads first.
|
2019-06-27 08:05:18 +02:00
|
|
|
mods::imports.erase("m_vm");
|
2019-06-24 01:27:45 +02:00
|
|
|
|
2019-06-14 00:24:56 +02:00
|
|
|
for(auto it(module_names.rbegin()); it != module_names.rend(); ++it)
|
2019-06-05 02:53:28 +02:00
|
|
|
mods::imports.erase(*it);
|
2018-03-28 22:27:39 +02:00
|
|
|
}
|
2017-12-12 21:33:14 +01:00
|
|
|
|
2019-06-05 02:53:28 +02:00
|
|
|
/// This is an ordered list for loading and unloading modules. This is not the
|
|
|
|
/// solution I really want at all so consider it temporary. Modules are loaded
|
|
|
|
/// in the order of the lines and unloaded in reverse order.
|
|
|
|
decltype(ircd::m::module_names)
|
|
|
|
ircd::m::module_names
|
|
|
|
{
|
2019-07-17 01:26:52 +02:00
|
|
|
"net_dns",
|
2019-06-27 08:05:18 +02:00
|
|
|
"conf",
|
2019-07-17 01:26:52 +02:00
|
|
|
"m_noop",
|
2019-06-27 08:05:18 +02:00
|
|
|
"m_node",
|
|
|
|
"m_keys",
|
|
|
|
"m_fetch",
|
|
|
|
"m_feds",
|
2019-06-14 00:24:56 +02:00
|
|
|
"m_events",
|
|
|
|
"m_rooms",
|
|
|
|
"m_user",
|
2019-07-17 23:20:28 +02:00
|
|
|
"m_user_rooms",
|
|
|
|
"m_user_events",
|
2019-07-15 19:07:36 +02:00
|
|
|
"m_user_highlight",
|
2019-07-15 01:26:23 +02:00
|
|
|
"m_user_profile",
|
2019-07-22 23:29:00 +02:00
|
|
|
"m_room_timeline",
|
2019-06-14 00:24:56 +02:00
|
|
|
"m_room_aliases",
|
|
|
|
"m_room_canonical_alias",
|
|
|
|
"m_room_create",
|
|
|
|
"m_room_history_visibility",
|
|
|
|
"m_room_join_rules",
|
|
|
|
"m_room_member",
|
2019-07-26 02:08:27 +02:00
|
|
|
"m_room_third_party_invite",
|
2019-06-14 00:24:56 +02:00
|
|
|
"m_room_message",
|
|
|
|
"m_room_power_levels",
|
|
|
|
"m_room_server_acl",
|
2019-07-26 02:08:27 +02:00
|
|
|
"m_room_redaction",
|
2019-07-24 00:37:32 +02:00
|
|
|
"m_room_bootstrap",
|
2019-06-14 00:24:56 +02:00
|
|
|
"m_presence",
|
|
|
|
"m_receipt",
|
|
|
|
"m_typing",
|
|
|
|
"m_device_list_update",
|
|
|
|
"m_device",
|
|
|
|
"m_direct",
|
|
|
|
"m_direct_to_device",
|
2019-07-27 01:57:27 +02:00
|
|
|
"m_breadcrumb_rooms",
|
|
|
|
"m_ignored_user_list",
|
2019-08-03 01:56:18 +02:00
|
|
|
"m_event_append",
|
2019-06-14 00:24:56 +02:00
|
|
|
"key_query",
|
|
|
|
"key_server",
|
2019-07-17 01:26:52 +02:00
|
|
|
"identity_pubkey",
|
|
|
|
"identity_v1",
|
2019-06-14 00:24:56 +02:00
|
|
|
"media_media",
|
2019-07-18 20:29:03 +02:00
|
|
|
"m_command",
|
|
|
|
"m_control",
|
|
|
|
"well_known",
|
2019-07-17 01:26:52 +02:00
|
|
|
"federation_backfill_ids",
|
|
|
|
"federation_backfill",
|
|
|
|
"federation_event_auth",
|
|
|
|
"federation_event",
|
|
|
|
"federation_get_groups_publicised",
|
|
|
|
"federation_get_missing_events",
|
|
|
|
"federation_invite",
|
|
|
|
"federation_invite2",
|
|
|
|
"federation_make_join",
|
|
|
|
"federation_make_leave",
|
|
|
|
"federation_publicrooms",
|
|
|
|
"federation_query_auth",
|
|
|
|
"federation_query",
|
|
|
|
"federation_sender",
|
|
|
|
"federation_send_join",
|
|
|
|
"federation_send_leave",
|
|
|
|
"federation_send",
|
|
|
|
"federation_state_ids",
|
|
|
|
"federation_state",
|
|
|
|
"federation_user_devices",
|
|
|
|
"federation_user_keys_claim",
|
|
|
|
"federation_user_keys_query",
|
|
|
|
"federation_version",
|
2019-06-14 00:24:56 +02:00
|
|
|
"client_account",
|
2019-07-14 01:17:46 +02:00
|
|
|
"client_user",
|
2019-07-15 00:32:21 +02:00
|
|
|
"client_profile",
|
2019-06-14 00:24:56 +02:00
|
|
|
"client_capabilities",
|
|
|
|
"client_createroom",
|
|
|
|
"client_delete_devices",
|
|
|
|
"client_devices",
|
|
|
|
"client_directory_list_appservice",
|
|
|
|
"client_directory_list_room",
|
|
|
|
"client_directory_room",
|
|
|
|
"client_directory_user",
|
|
|
|
"client_events",
|
|
|
|
"client_initialsync",
|
|
|
|
"client_joined_groups",
|
|
|
|
"client_join",
|
|
|
|
"client_keys_changes",
|
|
|
|
"client_keys_claim",
|
|
|
|
"client_keys_query",
|
|
|
|
"client_keys_upload",
|
|
|
|
"client_login",
|
|
|
|
"client_logout",
|
|
|
|
"client_notifications",
|
|
|
|
"client_presence",
|
|
|
|
"client_publicised_groups",
|
|
|
|
"client_publicrooms",
|
|
|
|
"client_pushers",
|
|
|
|
"client_pushrules",
|
|
|
|
"client_register_available",
|
|
|
|
"client_register",
|
|
|
|
"client_rooms",
|
|
|
|
"client_search",
|
|
|
|
"client_send_to_device",
|
|
|
|
"client_sync_account_data",
|
|
|
|
"client_sync_device_lists",
|
|
|
|
"client_sync_device_one_time_keys_count",
|
|
|
|
"client_sync_presence",
|
|
|
|
"client_sync_rooms_account_data",
|
|
|
|
"client_sync_rooms_ephemeral_receipt",
|
|
|
|
"client_sync_rooms_ephemeral",
|
|
|
|
"client_sync_rooms_ephemeral_typing",
|
|
|
|
"client_sync_rooms",
|
|
|
|
"client_sync_rooms_state",
|
|
|
|
"client_sync_rooms_timeline",
|
|
|
|
"client_sync_rooms_unread_notifications",
|
2019-07-16 18:32:06 +02:00
|
|
|
"client_sync_rooms_summary",
|
2019-06-14 00:24:56 +02:00
|
|
|
"client_sync",
|
|
|
|
"client_sync_to_device",
|
|
|
|
"client_thirdparty_protocols",
|
|
|
|
"client_versions",
|
|
|
|
"client_voip_turnserver",
|
2019-08-03 23:56:28 +02:00
|
|
|
"web_root",
|
|
|
|
"web_hook",
|
2019-07-18 20:27:08 +02:00
|
|
|
"m_listen",
|
2019-07-17 01:26:52 +02:00
|
|
|
"m_vm",
|
2019-08-03 23:56:28 +02:00
|
|
|
"stats",
|
2019-06-05 02:53:28 +02:00
|
|
|
};
|
|
|
|
|
2019-06-14 00:25:17 +02:00
|
|
|
/// This is a list of modules that are considered "optional" and any loading
|
|
|
|
/// error for them will not propagate and interrupt m::init.
|
|
|
|
decltype(ircd::m::module_names_optional)
|
|
|
|
ircd::m::module_names_optional
|
|
|
|
{
|
2019-08-03 23:56:28 +02:00
|
|
|
"web_hook",
|
2019-06-14 00:25:17 +02:00
|
|
|
};
|
|
|
|
|
2018-04-23 00:17:07 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// m/self.h
|
|
|
|
//
|
|
|
|
|
2018-08-17 19:37:12 +02:00
|
|
|
std::string
|
|
|
|
ircd::m::self::origin
|
|
|
|
{};
|
|
|
|
|
2019-03-28 21:07:30 +01:00
|
|
|
std::string
|
|
|
|
ircd::m::self::servername
|
|
|
|
{};
|
|
|
|
|
2018-08-17 19:37:12 +02:00
|
|
|
ircd::ed25519::sk
|
|
|
|
ircd::m::self::secret_key
|
|
|
|
{};
|
|
|
|
|
|
|
|
ircd::ed25519::pk
|
|
|
|
ircd::m::self::public_key
|
|
|
|
{};
|
|
|
|
|
|
|
|
std::string
|
|
|
|
ircd::m::self::public_key_b64
|
|
|
|
{};
|
|
|
|
|
|
|
|
std::string
|
|
|
|
ircd::m::self::public_key_id
|
|
|
|
{};
|
|
|
|
|
|
|
|
std::string
|
|
|
|
ircd::m::self::tls_cert_der
|
|
|
|
{};
|
|
|
|
|
|
|
|
std::string
|
|
|
|
ircd::m::self::tls_cert_der_sha256_b64
|
|
|
|
{};
|
|
|
|
|
2018-04-23 00:17:07 +02:00
|
|
|
//
|
|
|
|
// my user
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::m::user::id::buf
|
|
|
|
ircd_user_id
|
|
|
|
{
|
|
|
|
"ircd", "localhost" // gets replaced after conf init
|
|
|
|
};
|
|
|
|
|
|
|
|
ircd::m::user
|
|
|
|
ircd::m::me
|
|
|
|
{
|
|
|
|
ircd_user_id
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// my room
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::m::room::id::buf
|
|
|
|
ircd_room_id
|
|
|
|
{
|
|
|
|
"ircd", "localhost" // replaced after conf init
|
|
|
|
};
|
|
|
|
|
|
|
|
ircd::m::room
|
|
|
|
ircd::m::my_room
|
|
|
|
{
|
|
|
|
ircd_room_id
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// my node
|
|
|
|
//
|
|
|
|
|
2019-05-27 02:44:12 +02:00
|
|
|
std::array<char, ircd::rfc3986::DOMAIN_BUFSIZE>
|
2018-04-23 00:17:07 +02:00
|
|
|
ircd_node_id
|
|
|
|
{
|
2019-05-27 02:44:12 +02:00
|
|
|
"localhost" // replaced after conf init
|
2018-04-23 00:17:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
ircd::m::node
|
|
|
|
ircd::m::my_node
|
|
|
|
{
|
|
|
|
ircd_node_id
|
|
|
|
};
|
|
|
|
|
|
|
|
bool
|
2019-03-05 18:47:54 +01:00
|
|
|
ircd::m::self::host(const string_view &other)
|
2018-04-23 00:17:07 +02:00
|
|
|
{
|
2019-03-05 18:47:54 +01:00
|
|
|
// port() is 0 when the origin has no port (and implies 8448)
|
|
|
|
const auto port
|
|
|
|
{
|
|
|
|
me.user_id.port()
|
|
|
|
};
|
|
|
|
|
|
|
|
// If my_host has a port number, then the argument must also have the
|
|
|
|
// same port number.
|
|
|
|
if(port)
|
|
|
|
return host() == other;
|
|
|
|
|
|
|
|
/// If my_host has no port number, then the argument can have port
|
|
|
|
/// 8448 or no port number, which will initialize net::hostport.port to
|
|
|
|
/// the "canon_port" of 8448.
|
|
|
|
assert(net::canon_port == 8448);
|
|
|
|
const net::hostport _other{other};
|
|
|
|
if(net::port(_other) != net::canon_port)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(host() != host(_other))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
2018-04-23 00:17:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::m::self::host()
|
|
|
|
{
|
|
|
|
return me.user_id.host();
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// init
|
|
|
|
//
|
|
|
|
|
2018-06-17 07:00:47 +02:00
|
|
|
//TODO: XXX
|
|
|
|
extern ircd::m::room::id::buf users_room_id;
|
|
|
|
extern ircd::m::room::id::buf tokens_room_id;
|
|
|
|
extern ircd::m::room::id::buf nodes_room_id;
|
|
|
|
|
2019-03-28 21:07:30 +01:00
|
|
|
ircd::m::self::init::init(const string_view &origin,
|
|
|
|
const string_view &servername)
|
2018-04-23 00:17:07 +02:00
|
|
|
{
|
2019-05-10 01:47:47 +02:00
|
|
|
// Sanity check that these are valid hostname strings. This was likely
|
|
|
|
// already checked, so these validators will simply throw without very
|
|
|
|
// useful error messages if invalid strings ever make it this far.
|
|
|
|
rfc3986::valid_host(origin);
|
|
|
|
rfc3986::valid_host(servername);
|
|
|
|
|
2019-03-28 21:07:30 +01:00
|
|
|
self::origin = origin;
|
|
|
|
self::servername = servername;
|
2018-08-17 19:37:12 +02:00
|
|
|
|
2019-05-27 02:44:12 +02:00
|
|
|
m::my_node = string_view{strlcpy
|
|
|
|
{
|
|
|
|
ircd_node_id, origin
|
|
|
|
}};
|
|
|
|
|
2018-08-15 01:47:42 +02:00
|
|
|
ircd_user_id = {"ircd", origin};
|
2018-04-23 00:17:07 +02:00
|
|
|
m::me = {ircd_user_id};
|
|
|
|
|
2018-08-15 01:47:42 +02:00
|
|
|
ircd_room_id = {"ircd", origin};
|
2018-04-23 00:17:07 +02:00
|
|
|
m::my_room = {ircd_room_id};
|
|
|
|
|
2018-08-15 01:47:42 +02:00
|
|
|
users_room_id = {"users", origin};
|
2018-06-17 07:00:47 +02:00
|
|
|
m::user::users = {users_room_id};
|
|
|
|
|
2018-08-15 01:47:42 +02:00
|
|
|
tokens_room_id = {"tokens", origin};
|
2018-06-17 07:00:47 +02:00
|
|
|
m::user::tokens = {tokens_room_id};
|
|
|
|
|
2018-08-15 01:47:42 +02:00
|
|
|
nodes_room_id = {"nodes", origin};
|
2018-06-17 07:00:47 +02:00
|
|
|
m::nodes = {nodes_room_id};
|
|
|
|
|
2018-08-15 01:47:42 +02:00
|
|
|
if(origin == "localhost")
|
2018-04-23 00:17:07 +02:00
|
|
|
log::warning
|
|
|
|
{
|
2018-08-15 01:47:42 +02:00
|
|
|
"The origin is configured or has defaulted to 'localhost'"
|
2018-04-23 00:17:07 +02:00
|
|
|
};
|
2019-06-22 01:41:43 +02:00
|
|
|
|
2019-06-22 05:27:30 +02:00
|
|
|
// Loading the keys module in runlevel::START will do further
|
|
|
|
// inits of m::self::globals. Calling the inits directly from
|
|
|
|
// here makes the module dependent on libircd and unloadable.
|
|
|
|
assert(ircd::run::level == run::level::START);
|
2019-06-27 08:05:18 +02:00
|
|
|
mods::imports.emplace("m_keys"s, "m_keys"s);
|
2018-04-23 00:17:07 +02:00
|
|
|
}
|
|
|
|
|
2019-01-04 02:21:02 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// m/sync.h
|
|
|
|
//
|
|
|
|
|
|
|
|
decltype(ircd::m::sync::log)
|
|
|
|
ircd::m::sync::log
|
|
|
|
{
|
2019-08-01 04:40:18 +02:00
|
|
|
"m.sync", 's'
|
2019-01-04 02:21:02 +01:00
|
|
|
};
|
|
|
|
|
2019-01-09 00:10:06 +01:00
|
|
|
namespace ircd::m::sync
|
|
|
|
{
|
|
|
|
const ctx::pool::opts pool_opts
|
|
|
|
{
|
|
|
|
ctx::DEFAULT_STACK_SIZE,
|
|
|
|
0,
|
|
|
|
-1,
|
|
|
|
0
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
decltype(ircd::m::sync::pool)
|
|
|
|
ircd::m::sync::pool
|
|
|
|
{
|
2019-07-20 23:56:10 +02:00
|
|
|
"m.sync", pool_opts
|
2019-01-09 00:10:06 +01:00
|
|
|
};
|
|
|
|
|
2019-02-22 20:13:55 +01:00
|
|
|
decltype(ircd::m::sync::stats_info)
|
|
|
|
ircd::m::sync::stats_info
|
2019-02-22 18:53:51 +01:00
|
|
|
{
|
2019-02-22 20:13:55 +01:00
|
|
|
{ "name", "ircd.m.sync.stats.info" },
|
|
|
|
{ "default", false },
|
|
|
|
};
|
|
|
|
|
2019-06-23 08:28:48 +02:00
|
|
|
template<>
|
2019-07-07 05:17:42 +02:00
|
|
|
decltype(ircd::util::instance_multimap<std::string, ircd::m::sync::item, std::less<>>::map)
|
|
|
|
ircd::util::instance_multimap<std::string, ircd::m::sync::item, std::less<>>::map
|
2019-06-23 08:28:48 +02:00
|
|
|
{};
|
|
|
|
|
2019-07-07 05:17:42 +02:00
|
|
|
template<>
|
|
|
|
decltype(ircd::util::instance_list<ircd::m::sync::data>::allocator)
|
|
|
|
ircd::util::instance_list<ircd::m::sync::data>::allocator
|
|
|
|
{};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
decltype(ircd::util::instance_list<ircd::m::sync::data>::list)
|
|
|
|
ircd::util::instance_list<ircd::m::sync::data>::list
|
|
|
|
{
|
|
|
|
allocator
|
|
|
|
};
|
|
|
|
|
2019-01-09 00:10:06 +01:00
|
|
|
bool
|
|
|
|
ircd::m::sync::for_each(const item_closure_bool &closure)
|
|
|
|
{
|
2019-01-10 05:39:12 +01:00
|
|
|
auto it(begin(item::map));
|
|
|
|
for(; it != end(item::map); ++it)
|
|
|
|
if(!closure(*it->second))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
2019-01-09 00:10:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::sync::for_each(const string_view &prefix,
|
|
|
|
const item_closure_bool &closure)
|
|
|
|
{
|
|
|
|
const auto depth
|
|
|
|
{
|
|
|
|
token_count(prefix, '.')
|
|
|
|
};
|
|
|
|
|
|
|
|
auto it
|
|
|
|
{
|
|
|
|
item::map.lower_bound(prefix)
|
|
|
|
};
|
|
|
|
|
|
|
|
for(; it != end(item::map); ++it)
|
|
|
|
{
|
|
|
|
const auto item_depth
|
|
|
|
{
|
|
|
|
token_count(it->first, '.')
|
|
|
|
};
|
|
|
|
|
|
|
|
if(item_depth > depth + 1)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if(it->first == prefix)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if(item_depth < depth + 1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if(!closure(*it->second))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::sync::apropos(const data &d,
|
|
|
|
const event &event)
|
|
|
|
{
|
|
|
|
return apropos(d, index(event, std::nothrow));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::sync::apropos(const data &d,
|
|
|
|
const event::id &event_id)
|
|
|
|
{
|
|
|
|
return apropos(d, index(event_id, std::nothrow));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::sync::apropos(const data &d,
|
|
|
|
const event::idx &event_idx)
|
|
|
|
{
|
2019-01-10 22:19:07 +01:00
|
|
|
return event_idx >= d.range.first &&
|
|
|
|
event_idx < d.range.second;
|
2019-01-09 00:10:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::m::sync::loghead(const data &data)
|
|
|
|
{
|
|
|
|
thread_local char headbuf[256], rembuf[128], iecbuf[2][64], tmbuf[32];
|
2019-01-10 22:19:07 +01:00
|
|
|
|
|
|
|
const auto remstr
|
2019-01-09 00:10:06 +01:00
|
|
|
{
|
2019-01-10 22:19:07 +01:00
|
|
|
data.client?
|
|
|
|
string(rembuf, ircd::remote(*data.client)):
|
|
|
|
string_view{}
|
2019-01-09 00:10:06 +01:00
|
|
|
};
|
2019-01-04 02:21:02 +01:00
|
|
|
|
2019-01-10 22:19:07 +01:00
|
|
|
const auto flush_bytes
|
|
|
|
{
|
|
|
|
data.stats?
|
|
|
|
data.stats->flush_bytes:
|
|
|
|
0U
|
|
|
|
};
|
2019-01-04 02:21:02 +01:00
|
|
|
|
2019-01-10 22:19:07 +01:00
|
|
|
const auto flush_count
|
|
|
|
{
|
|
|
|
data.stats?
|
|
|
|
data.stats->flush_count:
|
|
|
|
0U
|
|
|
|
};
|
2019-01-04 02:21:02 +01:00
|
|
|
|
2019-01-10 22:19:07 +01:00
|
|
|
const auto tmstr
|
2019-01-04 02:21:02 +01:00
|
|
|
{
|
2019-01-10 22:19:07 +01:00
|
|
|
data.stats?
|
|
|
|
ircd::pretty(tmbuf, data.stats->timer.at<milliseconds>(), true):
|
|
|
|
string_view{}
|
2019-01-04 02:21:02 +01:00
|
|
|
};
|
|
|
|
|
2019-01-10 22:19:07 +01:00
|
|
|
return fmt::sprintf
|
|
|
|
{
|
2019-07-07 07:24:27 +02:00
|
|
|
headbuf, "%s %s %ld:%lu|%lu%s chunk:%zu sent:%s of %s in %s",
|
2019-01-10 22:19:07 +01:00
|
|
|
remstr,
|
|
|
|
string_view{data.user.user_id},
|
|
|
|
data.range.first,
|
|
|
|
data.range.second,
|
2019-03-19 19:45:01 +01:00
|
|
|
vm::sequence::retired,
|
2019-07-07 07:24:27 +02:00
|
|
|
data.phased?
|
|
|
|
"|P"_sv : ""_sv,
|
2019-03-02 20:40:51 +01:00
|
|
|
flush_count,
|
|
|
|
ircd::pretty(iecbuf[1], iec(flush_bytes)),
|
2019-02-27 00:50:58 +01:00
|
|
|
data.out?
|
|
|
|
ircd::pretty(iecbuf[0], iec(flush_bytes + size(data.out->completed()))):
|
|
|
|
string_view{},
|
2019-01-10 22:19:07 +01:00
|
|
|
tmstr
|
|
|
|
};
|
2019-01-04 02:21:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// item
|
|
|
|
//
|
|
|
|
|
|
|
|
//
|
|
|
|
// item::item
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::m::sync::item::item(std::string name,
|
|
|
|
handle polylog,
|
2019-04-08 10:53:09 +02:00
|
|
|
handle linear,
|
|
|
|
const json::members &feature)
|
2019-01-04 02:21:02 +01:00
|
|
|
:instance_multimap
|
|
|
|
{
|
|
|
|
std::move(name)
|
|
|
|
}
|
2019-02-23 02:05:28 +01:00
|
|
|
,conf_name
|
|
|
|
{
|
|
|
|
fmt::snstringf{128, "ircd.m.sync.%s.enable", this->name()},
|
|
|
|
fmt::snstringf{128, "ircd.m.sync.%s.stats.debug", this->name()},
|
|
|
|
}
|
|
|
|
,enable
|
|
|
|
{
|
|
|
|
{ "name", conf_name[0] },
|
|
|
|
{ "default", true },
|
|
|
|
}
|
|
|
|
,stats_debug
|
|
|
|
{
|
|
|
|
{ "name", conf_name[1] },
|
|
|
|
{ "default", false },
|
|
|
|
}
|
2019-01-04 02:21:02 +01:00
|
|
|
,_polylog
|
|
|
|
{
|
|
|
|
std::move(polylog)
|
|
|
|
}
|
|
|
|
,_linear
|
|
|
|
{
|
|
|
|
std::move(linear)
|
|
|
|
}
|
2019-04-08 10:53:09 +02:00
|
|
|
,feature
|
|
|
|
{
|
|
|
|
feature
|
|
|
|
}
|
|
|
|
,opts
|
|
|
|
{
|
|
|
|
this->feature
|
|
|
|
}
|
2019-04-08 11:04:24 +02:00
|
|
|
,phased
|
|
|
|
{
|
|
|
|
opts.get<bool>("phased", false)
|
|
|
|
}
|
2019-04-16 09:32:37 +02:00
|
|
|
,initial
|
|
|
|
{
|
|
|
|
opts.get<bool>("initial", false)
|
|
|
|
}
|
2019-01-04 02:21:02 +01:00
|
|
|
{
|
|
|
|
log::debug
|
|
|
|
{
|
2019-04-08 10:53:09 +02:00
|
|
|
log, "Registered sync item(%p) '%s' (%zu features)",
|
2019-01-04 02:21:02 +01:00
|
|
|
this,
|
2019-04-08 10:53:09 +02:00
|
|
|
this->name(),
|
|
|
|
opts.size(),
|
2019-01-04 02:21:02 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::sync::item::~item()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
log, "Unregistered sync item(%p) '%s'",
|
|
|
|
this,
|
|
|
|
this->name()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-02-24 20:59:53 +01:00
|
|
|
bool
|
2019-01-04 02:21:02 +01:00
|
|
|
ircd::m::sync::item::polylog(data &data)
|
|
|
|
try
|
|
|
|
{
|
2019-04-08 11:04:24 +02:00
|
|
|
// Skip the item if disabled by configuration
|
2019-02-23 02:05:28 +01:00
|
|
|
if(!enable)
|
2019-02-24 20:59:53 +01:00
|
|
|
return false;
|
2019-02-23 02:05:28 +01:00
|
|
|
|
2019-04-08 11:04:24 +02:00
|
|
|
// Skip the item for phased-sync ranges if it's not phased-sync aware.
|
2019-04-16 09:32:37 +02:00
|
|
|
if(data.phased && !phased && !initial)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Skip the item for phased-sync ranges after initial sync if it has initial=true
|
|
|
|
if(data.phased && initial && int64_t(data.range.first) < 0L)
|
2019-04-08 11:04:24 +02:00
|
|
|
return false;
|
|
|
|
|
2019-01-04 02:21:02 +01:00
|
|
|
#ifdef RB_DEBUG
|
2019-01-10 22:19:07 +01:00
|
|
|
sync::stats stats
|
2019-01-04 02:21:02 +01:00
|
|
|
{
|
2019-02-23 02:05:28 +01:00
|
|
|
data.stats && (stats_info || stats_debug)?
|
2019-01-10 22:19:07 +01:00
|
|
|
*data.stats:
|
|
|
|
sync::stats{}
|
2019-01-04 02:21:02 +01:00
|
|
|
};
|
|
|
|
|
2019-02-23 02:05:28 +01:00
|
|
|
if(data.stats && (stats_info || stats_debug))
|
2019-01-10 22:19:07 +01:00
|
|
|
stats.timer = {};
|
|
|
|
#endif
|
|
|
|
|
2019-02-24 20:59:53 +01:00
|
|
|
const bool ret
|
|
|
|
{
|
|
|
|
_polylog(data)
|
|
|
|
};
|
2019-01-10 22:19:07 +01:00
|
|
|
|
2019-01-04 02:21:02 +01:00
|
|
|
#ifdef RB_DEBUG
|
2019-02-23 02:05:28 +01:00
|
|
|
if(data.stats && (stats_info || stats_debug))
|
2019-01-04 02:21:02 +01:00
|
|
|
{
|
2019-01-10 22:19:07 +01:00
|
|
|
//data.out.flush();
|
|
|
|
thread_local char tmbuf[32];
|
|
|
|
log::debug
|
|
|
|
{
|
2019-02-24 20:59:53 +01:00
|
|
|
log, "polylog %s commit:%b '%s' %s",
|
2019-01-10 22:19:07 +01:00
|
|
|
loghead(data),
|
2019-02-24 20:59:53 +01:00
|
|
|
ret,
|
2019-01-10 22:19:07 +01:00
|
|
|
name(),
|
|
|
|
ircd::pretty(tmbuf, stats.timer.at<microseconds>(), true)
|
|
|
|
};
|
|
|
|
}
|
2019-01-04 02:21:02 +01:00
|
|
|
#endif
|
2019-02-24 20:59:53 +01:00
|
|
|
|
|
|
|
return ret;
|
2019-01-04 02:21:02 +01:00
|
|
|
}
|
2019-01-04 23:47:01 +01:00
|
|
|
catch(const std::bad_function_call &e)
|
2019-01-04 02:21:02 +01:00
|
|
|
{
|
2019-01-04 23:47:01 +01:00
|
|
|
log::dwarning
|
|
|
|
{
|
2019-01-09 00:10:06 +01:00
|
|
|
log, "polylog %s '%s' missing handler :%s",
|
|
|
|
loghead(data),
|
2019-01-04 23:47:01 +01:00
|
|
|
name(),
|
|
|
|
e.what()
|
|
|
|
};
|
2019-02-24 20:59:53 +01:00
|
|
|
|
|
|
|
return false;
|
2019-01-04 02:21:02 +01:00
|
|
|
}
|
2019-01-19 00:09:39 +01:00
|
|
|
catch(const m::error &e)
|
|
|
|
{
|
|
|
|
log::derror
|
|
|
|
{
|
|
|
|
log, "polylog %s '%s' :%s %s",
|
|
|
|
loghead(data),
|
|
|
|
name(),
|
|
|
|
e.what(),
|
|
|
|
e.content
|
|
|
|
};
|
2019-02-24 20:59:53 +01:00
|
|
|
|
|
|
|
return false;
|
2019-01-19 00:09:39 +01:00
|
|
|
}
|
2019-01-04 02:21:02 +01:00
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::derror
|
|
|
|
{
|
2019-01-09 00:10:06 +01:00
|
|
|
log, "polylog %s '%s' :%s",
|
|
|
|
loghead(data),
|
2019-01-04 02:21:02 +01:00
|
|
|
name(),
|
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
2019-02-24 20:59:53 +01:00
|
|
|
bool
|
2019-01-10 05:39:12 +01:00
|
|
|
ircd::m::sync::item::linear(data &data)
|
|
|
|
try
|
|
|
|
{
|
2019-02-27 02:02:21 +01:00
|
|
|
if(!enable)
|
|
|
|
return false;
|
|
|
|
|
2019-02-24 20:59:53 +01:00
|
|
|
return _linear(data);
|
2019-01-10 05:39:12 +01:00
|
|
|
}
|
|
|
|
catch(const std::bad_function_call &e)
|
|
|
|
{
|
|
|
|
thread_local char rembuf[128];
|
|
|
|
log::dwarning
|
|
|
|
{
|
2019-01-10 22:19:07 +01:00
|
|
|
log, "linear %s '%s' missing handler :%s",
|
|
|
|
loghead(data),
|
2019-01-10 05:39:12 +01:00
|
|
|
name(),
|
|
|
|
e.what()
|
|
|
|
};
|
2019-02-24 20:59:53 +01:00
|
|
|
|
|
|
|
return false;
|
2019-01-10 05:39:12 +01:00
|
|
|
}
|
2019-04-02 18:47:35 +02:00
|
|
|
catch(const m::error &e)
|
|
|
|
{
|
|
|
|
log::derror
|
|
|
|
{
|
|
|
|
log, "linear %s '%s' :%s %s",
|
|
|
|
loghead(data),
|
|
|
|
name(),
|
|
|
|
e.what(),
|
|
|
|
e.content
|
|
|
|
};
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::derror
|
|
|
|
{
|
|
|
|
log, "linear %s '%s' :%s",
|
|
|
|
loghead(data),
|
|
|
|
name(),
|
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
2019-01-10 05:39:12 +01:00
|
|
|
|
2019-02-24 20:59:53 +01:00
|
|
|
bool
|
2019-01-10 05:39:12 +01:00
|
|
|
ircd::m::sync::item::poll(data &data,
|
|
|
|
const m::event &event)
|
|
|
|
try
|
|
|
|
{
|
2019-03-02 21:33:32 +01:00
|
|
|
const scope_restore theirs
|
2019-01-10 05:39:12 +01:00
|
|
|
{
|
|
|
|
data.event, &event
|
|
|
|
};
|
|
|
|
|
2019-02-24 20:59:53 +01:00
|
|
|
return _linear(data);
|
2019-01-10 05:39:12 +01:00
|
|
|
}
|
|
|
|
catch(const std::bad_function_call &e)
|
|
|
|
{
|
|
|
|
thread_local char rembuf[128];
|
|
|
|
log::dwarning
|
|
|
|
{
|
2019-01-10 22:19:07 +01:00
|
|
|
log, "poll %s '%s' missing handler :%s",
|
|
|
|
loghead(data),
|
2019-01-10 05:39:12 +01:00
|
|
|
name(),
|
|
|
|
e.what()
|
|
|
|
};
|
2019-02-24 20:59:53 +01:00
|
|
|
|
|
|
|
return false;
|
2019-01-10 05:39:12 +01:00
|
|
|
}
|
|
|
|
|
2019-07-07 05:42:45 +02:00
|
|
|
size_t
|
|
|
|
ircd::m::sync::item::children()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
size_t ret(0);
|
|
|
|
sync::for_each(this->name(), [&ret]
|
|
|
|
(auto &item)
|
|
|
|
{
|
|
|
|
++ret;
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-01-09 00:10:06 +01:00
|
|
|
ircd::string_view
|
|
|
|
ircd::m::sync::item::member_name()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return token_last(name(), '.');
|
|
|
|
}
|
|
|
|
|
2019-01-04 02:21:02 +01:00
|
|
|
ircd::string_view
|
|
|
|
ircd::m::sync::item::name()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return this->instance_multimap::it->first;
|
|
|
|
}
|
|
|
|
|
2019-03-15 23:31:55 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// m/app.h
|
|
|
|
//
|
|
|
|
|
|
|
|
decltype(ircd::m::app::log)
|
|
|
|
ircd::m::app::log
|
|
|
|
{
|
2019-08-01 04:40:18 +02:00
|
|
|
"m.app"
|
2019-03-15 23:31:55 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
std::string
|
|
|
|
ircd::m::app::config::get(const string_view &id)
|
|
|
|
{
|
|
|
|
using prototype = std::string (const string_view &);
|
|
|
|
|
|
|
|
static mods::import<prototype> call
|
|
|
|
{
|
|
|
|
"app_app", "ircd::m::app::config::get"
|
|
|
|
};
|
|
|
|
|
|
|
|
return call(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
ircd::m::app::config::get(std::nothrow_t,
|
|
|
|
const string_view &id)
|
|
|
|
{
|
|
|
|
using prototype = std::string (std::nothrow_t, const string_view &);
|
|
|
|
|
|
|
|
static mods::import<prototype> call
|
|
|
|
{
|
|
|
|
"app_app", "ircd::m::app::config::get"
|
|
|
|
};
|
|
|
|
|
|
|
|
return call(std::nothrow, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::app::config::get(const string_view &id,
|
|
|
|
const event::fetch::view_closure &closure)
|
|
|
|
{
|
|
|
|
using prototype = void (const string_view &, const event::fetch::view_closure &);
|
|
|
|
|
|
|
|
static mods::import<prototype> call
|
|
|
|
{
|
|
|
|
"app_app", "ircd::m::app::config::get"
|
|
|
|
};
|
|
|
|
|
|
|
|
return call(id, closure);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::app::config::get(std::nothrow_t,
|
|
|
|
const string_view &id,
|
|
|
|
const event::fetch::view_closure &closure)
|
|
|
|
{
|
|
|
|
using prototype = bool (std::nothrow_t, const string_view &, const event::fetch::view_closure &);
|
|
|
|
|
|
|
|
static mods::import<prototype> call
|
|
|
|
{
|
|
|
|
"app_app", "ircd::m::app::config::get"
|
|
|
|
};
|
|
|
|
|
|
|
|
return call(std::nothrow, id, closure);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::idx
|
|
|
|
ircd::m::app::config::idx(const string_view &id)
|
|
|
|
{
|
|
|
|
using prototype = event::idx (const string_view &);
|
|
|
|
|
|
|
|
static mods::import<prototype> call
|
|
|
|
{
|
|
|
|
"app_app", "ircd::m::app::config::idx"
|
|
|
|
};
|
|
|
|
|
|
|
|
return call(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::idx
|
|
|
|
ircd::m::app::config::idx(std::nothrow_t,
|
|
|
|
const string_view &id)
|
|
|
|
{
|
|
|
|
using prototype = event::idx (std::nothrow_t, const string_view &);
|
|
|
|
|
|
|
|
static mods::import<prototype> call
|
|
|
|
{
|
|
|
|
"app_app", "ircd::m::app::config::idx"
|
|
|
|
};
|
|
|
|
|
|
|
|
return call(std::nothrow, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::app::exists(const string_view &id)
|
|
|
|
{
|
|
|
|
using prototype = bool (const string_view &);
|
|
|
|
|
|
|
|
static mods::import<prototype> call
|
|
|
|
{
|
|
|
|
"app_app", "exists"
|
|
|
|
};
|
|
|
|
|
|
|
|
return call(id);
|
|
|
|
}
|
|
|
|
|
2018-08-26 04:52:46 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// m/feds.h
|
|
|
|
//
|
|
|
|
|
2019-04-18 14:16:21 +02:00
|
|
|
ircd::m::feds::acquire::acquire(const opts &o,
|
|
|
|
const closure &c)
|
2019-04-19 00:25:01 +02:00
|
|
|
:acquire
|
2018-09-26 02:09:31 +02:00
|
|
|
{
|
2019-04-19 00:25:01 +02:00
|
|
|
vector_view<const opts>{&o, 1}, c
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::feds::acquire::acquire(const vector_view<const opts> &o,
|
|
|
|
const closure &c)
|
|
|
|
{
|
|
|
|
using prototype = bool (const vector_view<const opts> &, const closure &);
|
2018-09-26 02:09:31 +02:00
|
|
|
|
2019-04-11 14:29:59 +02:00
|
|
|
static mods::import<prototype> call
|
2018-09-26 02:09:31 +02:00
|
|
|
{
|
2019-06-27 08:05:18 +02:00
|
|
|
"m_feds", "ircd::m::feds::execute"
|
2018-09-26 02:09:31 +02:00
|
|
|
};
|
|
|
|
|
2019-04-18 14:16:21 +02:00
|
|
|
call(o, c);
|
2018-09-26 02:09:31 +02:00
|
|
|
}
|
|
|
|
|
2018-05-07 06:15:25 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// m/vm.h
|
|
|
|
//
|
|
|
|
|
2019-03-21 23:58:18 +01:00
|
|
|
decltype(ircd::m::vm::default_opts)
|
|
|
|
ircd::m::vm::default_opts;
|
|
|
|
|
|
|
|
decltype(ircd::m::vm::default_copts)
|
|
|
|
ircd::m::vm::default_copts;
|
|
|
|
|
2018-06-03 18:58:45 +02:00
|
|
|
decltype(ircd::m::vm::log)
|
|
|
|
ircd::m::vm::log
|
|
|
|
{
|
2019-08-01 04:40:18 +02:00
|
|
|
"m.vm", 'v'
|
2018-06-03 18:58:45 +02:00
|
|
|
};
|
|
|
|
|
2019-03-21 23:58:18 +01:00
|
|
|
decltype(ircd::m::vm::dock)
|
|
|
|
ircd::m::vm::dock;
|
2018-05-07 06:15:25 +02:00
|
|
|
|
2019-03-21 23:58:18 +01:00
|
|
|
decltype(ircd::m::vm::ready)
|
|
|
|
ircd::m::vm::ready;
|
2018-05-07 06:15:25 +02:00
|
|
|
|
2019-03-21 20:49:02 +01:00
|
|
|
ircd::string_view
|
|
|
|
ircd::m::vm::loghead(const eval &eval)
|
|
|
|
{
|
|
|
|
thread_local char buf[128];
|
|
|
|
return loghead(buf, eval);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::m::vm::loghead(const mutable_buffer &buf,
|
|
|
|
const eval &eval)
|
|
|
|
{
|
|
|
|
return fmt::sprintf
|
|
|
|
{
|
2019-04-26 12:23:09 +02:00
|
|
|
buf, "vm:%lu:%lu:%lu eval:%lu seq:%lu share:%lu:%lu %s",
|
2019-03-21 20:49:02 +01:00
|
|
|
sequence::uncommitted,
|
|
|
|
sequence::committed,
|
|
|
|
sequence::retired,
|
|
|
|
eval.id,
|
2019-03-29 00:00:54 +01:00
|
|
|
sequence::get(eval),
|
|
|
|
eval.sequence_shared[0],
|
|
|
|
eval.sequence_shared[1],
|
|
|
|
eval.event_?
|
2019-07-06 06:09:07 +02:00
|
|
|
string_view{eval.event_->event_id}:
|
2019-03-29 00:00:54 +01:00
|
|
|
"<unidentified>"_sv,
|
2019-03-21 20:49:02 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-03-09 22:51:03 +01:00
|
|
|
ircd::http::code
|
|
|
|
ircd::m::vm::http_code(const fault &code)
|
|
|
|
{
|
|
|
|
switch(code)
|
|
|
|
{
|
|
|
|
case fault::ACCEPT: return http::OK;
|
|
|
|
case fault::EXISTS: return http::CONFLICT;
|
|
|
|
case fault::INVALID: return http::BAD_REQUEST;
|
2019-04-22 21:27:43 +02:00
|
|
|
case fault::GENERAL: return http::UNAUTHORIZED;
|
|
|
|
case fault::AUTH: return http::FORBIDDEN;
|
2019-03-09 22:51:03 +01:00
|
|
|
case fault::STATE: return http::NOT_FOUND;
|
2019-04-22 21:27:43 +02:00
|
|
|
case fault::EVENT: return http::NOT_FOUND;
|
2019-03-09 22:51:03 +01:00
|
|
|
default: return http::INTERNAL_SERVER_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-05 08:27:01 +02:00
|
|
|
ircd::string_view
|
|
|
|
ircd::m::vm::reflect(const enum fault &code)
|
|
|
|
{
|
|
|
|
switch(code)
|
|
|
|
{
|
2019-04-22 21:27:43 +02:00
|
|
|
case fault::ACCEPT: return "#ACCEPT";
|
|
|
|
case fault::EXISTS: return "#EXISTS";
|
|
|
|
case fault::GENERAL: return "#GENERAL";
|
|
|
|
case fault::INVALID: return "#INVALID";
|
|
|
|
case fault::AUTH: return "#AUTH";
|
|
|
|
case fault::EVENT: return "#EVENT";
|
|
|
|
case fault::STATE: return "#STATE";
|
|
|
|
case fault::INTERRUPT: return "#INTERRUPT";
|
2018-09-05 08:27:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return "??????";
|
|
|
|
}
|
|
|
|
|
2018-05-29 13:04:32 +02:00
|
|
|
//
|
|
|
|
// Eval
|
|
|
|
//
|
|
|
|
// Processes any event from any place from any time and does whatever is
|
|
|
|
// necessary to validate, reject, learn from new information, ignore old
|
|
|
|
// information and advance the state of IRCd as best as possible.
|
|
|
|
|
2018-05-07 06:15:25 +02:00
|
|
|
/// Instance list linkage for all of the evaluations.
|
2019-04-17 05:48:00 +02:00
|
|
|
template<>
|
|
|
|
decltype(ircd::util::instance_list<ircd::m::vm::eval>::allocator)
|
|
|
|
ircd::util::instance_list<ircd::m::vm::eval>::allocator
|
|
|
|
{};
|
|
|
|
|
2018-05-07 06:15:25 +02:00
|
|
|
template<>
|
|
|
|
decltype(ircd::util::instance_list<ircd::m::vm::eval>::list)
|
|
|
|
ircd::util::instance_list<ircd::m::vm::eval>::list
|
2019-04-17 05:48:00 +02:00
|
|
|
{
|
|
|
|
allocator
|
|
|
|
};
|
2018-05-07 06:15:25 +02:00
|
|
|
|
|
|
|
decltype(ircd::m::vm::eval::id_ctr)
|
2019-03-21 23:58:18 +01:00
|
|
|
ircd::m::vm::eval::id_ctr;
|
|
|
|
|
|
|
|
decltype(ircd::m::vm::eval::executing)
|
|
|
|
ircd::m::vm::eval::executing;
|
|
|
|
|
|
|
|
decltype(ircd::m::vm::eval::injecting)
|
|
|
|
ircd::m::vm::eval::injecting;
|
|
|
|
|
2019-03-19 19:45:01 +01:00
|
|
|
void
|
|
|
|
ircd::m::vm::eval::seqsort()
|
2019-02-07 05:54:21 +01:00
|
|
|
{
|
2019-03-19 19:45:01 +01:00
|
|
|
eval::list.sort([]
|
|
|
|
(const auto *const &a, const auto *const &b)
|
2019-02-07 05:54:21 +01:00
|
|
|
{
|
2019-03-19 19:45:01 +01:00
|
|
|
if(sequence::get(*a) == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(sequence::get(*b) == 0)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return sequence::get(*a) < sequence::get(*b);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::vm::eval *
|
|
|
|
ircd::m::vm::eval::seqmin()
|
|
|
|
{
|
|
|
|
const auto it
|
|
|
|
{
|
|
|
|
std::min_element(begin(eval::list), end(eval::list), []
|
|
|
|
(const auto *const &a, const auto *const &b)
|
|
|
|
{
|
|
|
|
if(sequence::get(*a) == 0)
|
2019-02-07 05:54:21 +01:00
|
|
|
return false;
|
|
|
|
|
2019-03-19 19:45:01 +01:00
|
|
|
if(sequence::get(*b) == 0)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return sequence::get(*a) < sequence::get(*b);
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
if(it == end(eval::list))
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
if(sequence::get(**it) == 0)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return *it;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::vm::eval *
|
|
|
|
ircd::m::vm::eval::seqmax()
|
|
|
|
{
|
|
|
|
const auto it
|
|
|
|
{
|
|
|
|
std::max_element(begin(eval::list), end(eval::list), []
|
|
|
|
(const auto *const &a, const auto *const &b)
|
|
|
|
{
|
|
|
|
return sequence::get(*a) < sequence::get(*b);
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
if(it == end(eval::list))
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
if(sequence::get(**it) == 0)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return *it;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::vm::eval *
|
|
|
|
ircd::m::vm::eval::seqnext(const uint64_t &seq)
|
|
|
|
{
|
|
|
|
eval *ret{nullptr};
|
|
|
|
for(auto *const &eval : eval::list)
|
|
|
|
{
|
|
|
|
if(sequence::get(*eval) <= seq)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if(!ret || sequence::get(*eval) < sequence::get(*ret))
|
|
|
|
ret = eval;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(!ret || sequence::get(*ret) > seq);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::vm::eval::sequnique(const uint64_t &seq)
|
|
|
|
{
|
|
|
|
return 1 == std::count_if(begin(eval::list), end(eval::list), [&seq]
|
|
|
|
(const auto *const &eval)
|
|
|
|
{
|
|
|
|
return sequence::get(*eval) == seq;
|
2019-02-07 05:54:21 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::vm::eval &
|
|
|
|
ircd::m::vm::eval::get(const event::id &event_id)
|
|
|
|
{
|
|
|
|
auto *const ret
|
|
|
|
{
|
|
|
|
find(event_id)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(unlikely(!ret))
|
|
|
|
throw std::out_of_range
|
|
|
|
{
|
|
|
|
"eval::get(): event_id not being evaluated."
|
|
|
|
};
|
|
|
|
|
|
|
|
return *ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::vm::eval *
|
|
|
|
ircd::m::vm::eval::find(const event::id &event_id)
|
|
|
|
{
|
|
|
|
eval *ret{nullptr};
|
|
|
|
for_each([&event_id, &ret](eval &e)
|
|
|
|
{
|
|
|
|
if(e.event_)
|
|
|
|
{
|
2019-07-10 09:26:25 +02:00
|
|
|
if(e.event_->event_id == event_id)
|
2019-02-07 05:54:21 +01:00
|
|
|
ret = &e;
|
|
|
|
}
|
|
|
|
else if(e.issue)
|
|
|
|
{
|
|
|
|
if(e.issue->has("event_id"))
|
|
|
|
if(string_view{e.issue->at("event_id")} == event_id)
|
|
|
|
ret = &e;
|
|
|
|
}
|
|
|
|
else if(e.event_id == event_id)
|
|
|
|
ret = &e;
|
|
|
|
|
|
|
|
return ret == nullptr;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-03-21 22:11:42 +01:00
|
|
|
size_t
|
|
|
|
ircd::m::vm::eval::count(const ctx::ctx *const &c)
|
|
|
|
{
|
|
|
|
return std::count_if(begin(eval::list), end(eval::list), [&c]
|
|
|
|
(const eval *const &eval)
|
|
|
|
{
|
|
|
|
return eval->ctx == c;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-02-07 05:54:21 +01:00
|
|
|
bool
|
|
|
|
ircd::m::vm::eval::for_each(const std::function<bool (eval &)> &closure)
|
|
|
|
{
|
|
|
|
for(eval *const &eval : eval::list)
|
|
|
|
if(!closure(*eval))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-03-21 22:11:42 +01:00
|
|
|
bool
|
|
|
|
ircd::m::vm::eval::for_each(const ctx::ctx *const &c,
|
|
|
|
const std::function<bool (eval &)> &closure)
|
|
|
|
{
|
|
|
|
for(eval *const &eval : eval::list)
|
|
|
|
if(eval->ctx == c)
|
|
|
|
if(!closure(*eval))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-05-07 06:15:25 +02:00
|
|
|
//
|
|
|
|
// eval::eval
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::m::vm::eval::eval(json::iov &event,
|
|
|
|
const json::iov &content,
|
|
|
|
const vm::copts &opts)
|
|
|
|
:eval{opts}
|
|
|
|
{
|
|
|
|
operator()(event, content);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::vm::eval::eval(const event &event,
|
|
|
|
const vm::opts &opts)
|
|
|
|
:eval{opts}
|
|
|
|
{
|
|
|
|
operator()(event);
|
|
|
|
}
|
|
|
|
|
2019-06-02 02:03:17 +02:00
|
|
|
ircd::m::vm::eval::eval(const json::array &pdus,
|
2019-02-07 05:54:04 +01:00
|
|
|
const vm::opts &opts)
|
|
|
|
:opts{&opts}
|
|
|
|
{
|
2019-06-02 02:03:17 +02:00
|
|
|
// Sort the events first to avoid complicating the evals; the events might
|
|
|
|
// be from different rooms but it doesn't matter.
|
|
|
|
std::vector<m::event> events(begin(pdus), end(pdus));
|
|
|
|
std::sort(begin(events), end(events));
|
|
|
|
this->pdus = events;
|
|
|
|
|
|
|
|
// Conduct each eval without letting any one exception ruin things for the
|
|
|
|
// others, including an interrupt. The only exception is a termination.
|
2019-07-10 10:11:05 +02:00
|
|
|
for(auto it(begin(events)); it != end(events); ++it) try
|
2019-06-02 02:03:17 +02:00
|
|
|
{
|
2019-07-10 10:11:05 +02:00
|
|
|
auto &event{*it};
|
|
|
|
if(!json::get<"event_id"_>(event))
|
|
|
|
event.event_id = event::id::v4
|
|
|
|
{
|
|
|
|
this->event_id, event
|
|
|
|
};
|
|
|
|
|
2019-06-02 02:03:17 +02:00
|
|
|
// When a fault::EXISTS would not actually be revealed to the user in
|
|
|
|
// any way we can elide a lot of grief by checking this here first and
|
|
|
|
// skipping the event. The query path will be adequately cached anyway.
|
|
|
|
if(~(opts.warnlog | opts.errorlog) & fault::EXISTS)
|
2019-07-10 09:26:25 +02:00
|
|
|
if(m::exists(event.event_id))
|
2019-06-02 02:03:17 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
operator()(event);
|
|
|
|
}
|
2019-06-07 12:23:40 +02:00
|
|
|
catch(const ctx::interrupted &e)
|
|
|
|
{
|
|
|
|
if(opts.nothrows & fault::INTERRUPT)
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
throw;
|
|
|
|
}
|
2019-06-02 02:03:17 +02:00
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
catch(const ctx::terminated &)
|
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
2019-02-07 05:54:04 +01:00
|
|
|
}
|
|
|
|
|
2018-05-07 06:15:25 +02:00
|
|
|
ircd::m::vm::eval::eval(const vm::copts &opts)
|
|
|
|
:opts{&opts}
|
|
|
|
,copts{&opts}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::vm::eval::eval(const vm::opts &opts)
|
|
|
|
:opts{&opts}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::vm::eval::~eval()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::vm::eval::operator
|
|
|
|
const event::id::buf &()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return event_id;
|
|
|
|
}
|
|
|
|
|
2019-07-21 02:21:27 +02:00
|
|
|
const ircd::m::event *
|
|
|
|
ircd::m::vm::eval::find_pdu(const event::id &event_id)
|
|
|
|
{
|
|
|
|
const m::event *ret{nullptr};
|
|
|
|
for_each_pdu([&ret, &event_id]
|
|
|
|
(const m::event &event)
|
|
|
|
{
|
|
|
|
if(event.event_id != event_id)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
ret = std::addressof(event);
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ircd::m::event *
|
|
|
|
ircd::m::vm::eval::find_pdu(const eval &eval,
|
|
|
|
const event::id &event_id)
|
|
|
|
{
|
|
|
|
const m::event *ret{nullptr};
|
|
|
|
for(const auto &event : eval.pdus)
|
|
|
|
{
|
|
|
|
if(event.event_id != event_id)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ret = std::addressof(event);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-03-19 19:45:01 +01:00
|
|
|
bool
|
2019-06-02 02:03:17 +02:00
|
|
|
ircd::m::vm::eval::for_each_pdu(const std::function<bool (const event &)> &closure)
|
2019-03-19 19:45:01 +01:00
|
|
|
{
|
|
|
|
return for_each([&closure](eval &e)
|
|
|
|
{
|
2019-06-02 02:03:17 +02:00
|
|
|
if(!empty(e.pdus))
|
|
|
|
{
|
|
|
|
for(const auto &pdu : e.pdus)
|
|
|
|
if(!closure(pdu))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if(e.event_)
|
|
|
|
{
|
|
|
|
if(!closure(*e.event_))
|
2019-03-19 19:45:01 +01:00
|
|
|
return false;
|
2019-06-02 02:03:17 +02:00
|
|
|
}
|
2019-03-19 19:45:01 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-05-07 06:15:25 +02:00
|
|
|
///
|
|
|
|
/// Figure 1:
|
|
|
|
/// in . <-- injection
|
|
|
|
/// ===:::::::==//
|
|
|
|
/// | ||||||| // <-- these functions
|
|
|
|
/// | \\|// //|
|
|
|
|
/// | ||| // | | acceleration
|
|
|
|
/// | |||// | |
|
|
|
|
/// | |||/ | |
|
|
|
|
/// | ||| | V
|
|
|
|
/// | !!! |
|
|
|
|
/// | * | <----- nozzle
|
|
|
|
/// | ///|||\\\ |
|
|
|
|
/// |/|/|/|\|\|\| <---- propagation cone
|
|
|
|
/// _/|/|/|/|\|\|\|\_
|
|
|
|
/// out
|
|
|
|
///
|
|
|
|
|
|
|
|
/// Inject a new event originating from this server.
|
|
|
|
///
|
|
|
|
enum ircd::m::vm::fault
|
|
|
|
ircd::m::vm::eval::operator()(json::iov &event,
|
|
|
|
const json::iov &contents)
|
|
|
|
{
|
|
|
|
using prototype = fault (eval &, json::iov &, const json::iov &);
|
|
|
|
|
2019-03-21 23:00:58 +01:00
|
|
|
static mods::import<prototype> call
|
2018-05-07 06:15:25 +02:00
|
|
|
{
|
2019-06-27 08:05:18 +02:00
|
|
|
"m_vm", "ircd::m::vm::inject"
|
2018-05-07 06:15:25 +02:00
|
|
|
};
|
|
|
|
|
2019-03-21 23:58:18 +01:00
|
|
|
vm::dock.wait([]
|
|
|
|
{
|
|
|
|
return vm::ready;
|
|
|
|
});
|
|
|
|
|
2019-03-21 23:00:58 +01:00
|
|
|
return call(*this, event, contents);
|
2018-05-07 06:15:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
enum ircd::m::vm::fault
|
|
|
|
ircd::m::vm::eval::operator()(const event &event)
|
|
|
|
{
|
|
|
|
using prototype = fault (eval &, const m::event &);
|
|
|
|
|
2019-03-21 23:00:58 +01:00
|
|
|
static mods::import<prototype> call
|
2018-05-07 06:15:25 +02:00
|
|
|
{
|
2019-06-27 08:05:18 +02:00
|
|
|
"m_vm", "ircd::m::vm::execute"
|
2018-05-07 06:15:25 +02:00
|
|
|
};
|
|
|
|
|
2019-03-21 23:58:18 +01:00
|
|
|
vm::dock.wait([]
|
|
|
|
{
|
|
|
|
return vm::ready;
|
|
|
|
});
|
|
|
|
|
2019-03-21 23:00:58 +01:00
|
|
|
return call(*this, event);
|
2018-05-07 06:15:25 +02:00
|
|
|
}
|
|
|
|
|
2019-03-19 19:45:01 +01:00
|
|
|
//
|
|
|
|
// sequence
|
|
|
|
//
|
|
|
|
|
2019-03-21 23:58:18 +01:00
|
|
|
decltype(ircd::m::vm::sequence::dock)
|
|
|
|
ircd::m::vm::sequence::dock;
|
|
|
|
|
2019-03-19 19:45:01 +01:00
|
|
|
decltype(ircd::m::vm::sequence::retired)
|
|
|
|
ircd::m::vm::sequence::retired;
|
|
|
|
|
|
|
|
decltype(ircd::m::vm::sequence::committed)
|
|
|
|
ircd::m::vm::sequence::committed;
|
|
|
|
|
|
|
|
decltype(ircd::m::vm::sequence::uncommitted)
|
|
|
|
ircd::m::vm::sequence::uncommitted;
|
|
|
|
|
|
|
|
uint64_t
|
|
|
|
ircd::m::vm::sequence::min()
|
|
|
|
{
|
|
|
|
const auto *const e
|
|
|
|
{
|
|
|
|
eval::seqmin()
|
|
|
|
};
|
|
|
|
|
|
|
|
return e? get(*e) : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t
|
|
|
|
ircd::m::vm::sequence::max()
|
|
|
|
{
|
|
|
|
const auto *const e
|
|
|
|
{
|
|
|
|
eval::seqmax()
|
|
|
|
};
|
|
|
|
|
|
|
|
return e? get(*e) : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t
|
|
|
|
ircd::m::vm::sequence::get(id::event::buf &event_id)
|
|
|
|
{
|
|
|
|
static constexpr auto column_idx
|
|
|
|
{
|
|
|
|
json::indexof<event, "event_id"_>()
|
|
|
|
};
|
|
|
|
|
|
|
|
auto &column
|
|
|
|
{
|
|
|
|
dbs::event_column.at(column_idx)
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto it
|
|
|
|
{
|
|
|
|
column.rbegin()
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!it)
|
|
|
|
{
|
|
|
|
// If this iterator is invalid the events db should
|
|
|
|
// be completely fresh.
|
|
|
|
assert(db::sequence(*dbs::events) == 0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto &ret
|
|
|
|
{
|
|
|
|
byte_view<uint64_t>(it->first)
|
|
|
|
};
|
|
|
|
|
|
|
|
event_id = it->second;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint64_t &
|
|
|
|
ircd::m::vm::sequence::get(const eval &eval)
|
|
|
|
{
|
|
|
|
return eval.sequence;
|
|
|
|
}
|
|
|
|
|
2019-07-16 19:38:25 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// m/redacted.h
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::m::redacted::redacted(const event::idx &event_idx)
|
|
|
|
:ret
|
|
|
|
{
|
|
|
|
event_idx?
|
|
|
|
event::refs(event_idx).has(dbs::ref::M_ROOM_REDACTION):
|
|
|
|
false
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-05-28 07:34:54 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// m/visible.h
|
|
|
|
//
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::visible(const event::id &event_id,
|
2018-05-31 13:21:54 +02:00
|
|
|
const string_view &mxid)
|
2018-05-28 07:34:54 +02:00
|
|
|
{
|
|
|
|
m::room::id::buf room_id
|
|
|
|
{
|
|
|
|
get(event_id, "room_id", room_id)
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::event event
|
|
|
|
{
|
2019-07-06 04:45:02 +02:00
|
|
|
json::members
|
|
|
|
{
|
|
|
|
{ "event_id", event_id },
|
|
|
|
{ "room_id", room_id },
|
|
|
|
}
|
2018-05-28 07:34:54 +02:00
|
|
|
};
|
|
|
|
|
2018-05-31 13:21:54 +02:00
|
|
|
return visible(event, mxid);
|
2018-05-28 07:34:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::visible(const event &event,
|
2018-05-31 13:21:54 +02:00
|
|
|
const string_view &mxid)
|
2018-05-28 07:34:54 +02:00
|
|
|
{
|
2018-05-31 13:21:54 +02:00
|
|
|
using prototype = bool (const m::event &, const string_view &);
|
2018-05-28 07:34:54 +02:00
|
|
|
|
2019-04-07 03:35:01 +02:00
|
|
|
static mods::import<prototype> call
|
2018-05-28 07:34:54 +02:00
|
|
|
{
|
2019-04-07 03:35:01 +02:00
|
|
|
"m_room_history_visibility", "ircd::m::visible"
|
2018-05-28 07:34:54 +02:00
|
|
|
};
|
|
|
|
|
2019-04-07 03:35:01 +02:00
|
|
|
return call(event, mxid);
|
2018-05-28 07:34:54 +02:00
|
|
|
}
|
|
|
|
|
2018-03-15 06:10:40 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// m/receipt.h
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::m::event::id::buf
|
|
|
|
ircd::m::receipt::read(const id::room &room_id,
|
|
|
|
const id::user &user_id,
|
|
|
|
const id::event &event_id)
|
|
|
|
{
|
|
|
|
return read(room_id, user_id, event_id, ircd::time<milliseconds>());
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::id::buf
|
2019-02-12 00:23:43 +01:00
|
|
|
ircd::m::receipt::read(const room::id &room_id,
|
|
|
|
const user::id &user_id,
|
|
|
|
const event::id &event_id,
|
2018-03-15 06:10:40 +01:00
|
|
|
const time_t &ms)
|
|
|
|
{
|
2019-02-12 00:23:43 +01:00
|
|
|
using prototype = event::id::buf (const room::id &, const user::id &, const event::id &, const time_t &);
|
2018-03-15 06:10:40 +01:00
|
|
|
|
2018-09-14 16:39:11 +02:00
|
|
|
static mods::import<prototype> function
|
2018-03-15 06:10:40 +01:00
|
|
|
{
|
2019-02-12 00:23:43 +01:00
|
|
|
"m_receipt", "ircd::m::receipt::read"
|
2018-03-15 06:10:40 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return function(room_id, user_id, event_id, ms);
|
|
|
|
}
|
|
|
|
|
2018-09-07 15:22:09 +02:00
|
|
|
ircd::m::event::id
|
2019-02-12 00:23:43 +01:00
|
|
|
ircd::m::receipt::read(event::id::buf &out,
|
|
|
|
const room::id &room_id,
|
|
|
|
const user::id &user_id)
|
2018-09-07 15:22:09 +02:00
|
|
|
{
|
|
|
|
const event::id::closure copy{[&out]
|
|
|
|
(const event::id &event_id)
|
|
|
|
{
|
|
|
|
out = event_id;
|
|
|
|
}};
|
|
|
|
|
|
|
|
return read(room_id, user_id, copy)?
|
|
|
|
event::id{out}:
|
|
|
|
event::id{};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-02-12 00:23:43 +01:00
|
|
|
ircd::m::receipt::read(const room::id &room_id,
|
|
|
|
const user::id &user_id,
|
2018-09-07 15:22:09 +02:00
|
|
|
const event::id::closure &closure)
|
|
|
|
{
|
2019-02-12 00:23:43 +01:00
|
|
|
using prototype = bool (const room::id &, const user::id &, const event::id::closure &);
|
2018-09-07 15:22:09 +02:00
|
|
|
|
2018-09-14 16:39:11 +02:00
|
|
|
static mods::import<prototype> function
|
2018-09-07 15:22:09 +02:00
|
|
|
{
|
2019-02-12 00:23:43 +01:00
|
|
|
"m_receipt", "ircd::m::receipt::read"
|
2018-09-07 15:22:09 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return function(room_id, user_id, closure);
|
|
|
|
}
|
|
|
|
|
2019-02-12 00:23:43 +01:00
|
|
|
bool
|
|
|
|
ircd::m::receipt::ignoring(const user &user,
|
|
|
|
const room::id &room_id)
|
|
|
|
{
|
|
|
|
using prototype = bool (const m::user &, const m::room::id &);
|
|
|
|
|
|
|
|
static mods::import<prototype> function
|
|
|
|
{
|
|
|
|
"m_receipt", "ircd::m::receipt::ignoring"
|
|
|
|
};
|
|
|
|
|
|
|
|
return function(user, room_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::receipt::ignoring(const user &user,
|
|
|
|
const event::id &event_id)
|
|
|
|
{
|
|
|
|
using prototype = bool (const m::user &, const m::event::id &);
|
|
|
|
|
|
|
|
static mods::import<prototype> function
|
|
|
|
{
|
|
|
|
"m_receipt", "ircd::m::receipt::ignoring"
|
|
|
|
};
|
|
|
|
|
|
|
|
return function(user, event_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::receipt::freshest(const room::id &room_id,
|
|
|
|
const user::id &user_id,
|
|
|
|
const event::id &event_id)
|
|
|
|
{
|
|
|
|
using prototype = bool (const m::room::id &, const m::user::id &, const m::event::id &);
|
|
|
|
|
|
|
|
static mods::import<prototype> function
|
|
|
|
{
|
|
|
|
"m_receipt", "ircd::m::receipt::freshest"
|
|
|
|
};
|
|
|
|
|
|
|
|
return function(room_id, user_id, event_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::receipt::exists(const room::id &room_id,
|
|
|
|
const user::id &user_id,
|
|
|
|
const event::id &event_id)
|
|
|
|
{
|
|
|
|
using prototype = bool (const m::room::id &, const m::user::id &, const m::event::id &);
|
|
|
|
|
|
|
|
static mods::import<prototype> function
|
|
|
|
{
|
|
|
|
"m_receipt", "ircd::m::receipt::exists"
|
|
|
|
};
|
|
|
|
|
|
|
|
return function(room_id, user_id, event_id);
|
|
|
|
}
|
|
|
|
|
2018-03-15 00:24:58 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// m/typing.h
|
|
|
|
//
|
|
|
|
|
2018-09-17 04:27:58 +02:00
|
|
|
//
|
|
|
|
// m::typing::commit::commit
|
|
|
|
//
|
|
|
|
|
2018-09-17 03:27:43 +02:00
|
|
|
ircd::m::typing::commit::commit(const m::typing &object)
|
|
|
|
:ircd::m::event::id::buf{[&object]
|
2018-03-15 00:24:58 +01:00
|
|
|
{
|
2018-09-17 03:27:43 +02:00
|
|
|
using prototype = m::event::id::buf (const m::typing &);
|
2018-03-15 00:24:58 +01:00
|
|
|
|
2018-09-14 16:39:11 +02:00
|
|
|
static mods::import<prototype> function
|
2018-03-15 00:24:58 +01:00
|
|
|
{
|
2018-09-17 03:27:43 +02:00
|
|
|
"m_typing", "commit"
|
2018-03-15 00:24:58 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return function(object);
|
2018-09-17 03:27:43 +02:00
|
|
|
}()}
|
|
|
|
{
|
2018-03-15 00:24:58 +01:00
|
|
|
}
|
|
|
|
|
2018-03-02 16:25:37 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2018-03-04 16:15:50 +01:00
|
|
|
// m/presence.h
|
2018-03-02 16:25:37 +01:00
|
|
|
//
|
|
|
|
|
2018-03-04 16:15:50 +01:00
|
|
|
ircd::m::presence::presence(const user &user,
|
|
|
|
const mutable_buffer &buf)
|
2019-01-27 00:24:29 +01:00
|
|
|
:edu::m_presence{[&user, &buf]
|
2018-03-04 16:15:50 +01:00
|
|
|
{
|
2019-01-27 00:24:29 +01:00
|
|
|
json::object ret;
|
|
|
|
get(user, [&ret, &buf]
|
|
|
|
(const json::object &content)
|
|
|
|
{
|
|
|
|
ret =
|
|
|
|
{
|
|
|
|
data(buf), copy(buf, string_view{content})
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}()}
|
2018-03-04 16:15:50 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::id::buf
|
|
|
|
ircd::m::presence::set(const user &user,
|
|
|
|
const string_view &presence,
|
|
|
|
const string_view &status_msg)
|
|
|
|
{
|
|
|
|
return set(m::presence
|
|
|
|
{
|
|
|
|
{ "user_id", user.user_id },
|
|
|
|
{ "presence", presence },
|
|
|
|
{ "status_msg", status_msg },
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-02 16:25:37 +01:00
|
|
|
ircd::m::event::id::buf
|
2018-03-04 16:15:50 +01:00
|
|
|
ircd::m::presence::set(const presence &object)
|
|
|
|
{
|
|
|
|
using prototype = event::id::buf (const presence &);
|
|
|
|
|
2018-09-14 16:39:11 +02:00
|
|
|
static mods::import<prototype> function
|
2018-03-04 16:15:50 +01:00
|
|
|
{
|
2019-02-11 23:13:13 +01:00
|
|
|
"m_presence", "ircd::m::presence::set"
|
2018-03-04 16:15:50 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return function(object);
|
|
|
|
}
|
|
|
|
|
2018-04-12 01:24:49 +02:00
|
|
|
void
|
|
|
|
ircd::m::presence::get(const user &user,
|
|
|
|
const closure &closure)
|
|
|
|
{
|
|
|
|
if(!get(std::nothrow, user, closure))
|
|
|
|
throw m::NOT_FOUND
|
|
|
|
{
|
2019-01-27 00:24:29 +01:00
|
|
|
"No presence found for %s", string_view{user.user_id}
|
2018-04-12 01:24:49 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::presence::get(std::nothrow_t,
|
|
|
|
const user &user,
|
2018-04-28 00:26:17 +02:00
|
|
|
const closure &closure)
|
|
|
|
{
|
2019-01-27 00:24:29 +01:00
|
|
|
static const m::event::fetch::opts fopts
|
2018-04-28 00:26:17 +02:00
|
|
|
{
|
2019-01-27 00:24:29 +01:00
|
|
|
m::event::keys::include {"content"}
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto reclosure{[&closure]
|
|
|
|
(const m::event &event)
|
|
|
|
{
|
|
|
|
closure(json::get<"content"_>(event));
|
|
|
|
}};
|
|
|
|
|
|
|
|
return get(std::nothrow, user, reclosure, &fopts);
|
2018-04-28 00:26:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::presence::get(std::nothrow_t,
|
|
|
|
const user &user,
|
2019-01-27 00:24:29 +01:00
|
|
|
const closure_event &closure,
|
|
|
|
const event::fetch::opts *const &opts)
|
2018-04-12 01:24:49 +02:00
|
|
|
{
|
2019-02-11 23:13:13 +01:00
|
|
|
using prototype = bool (std::nothrow_t, const m::user &, const closure_event &, const event::fetch::opts *const &);
|
2018-03-04 16:15:50 +01:00
|
|
|
|
2018-09-14 16:39:11 +02:00
|
|
|
static mods::import<prototype> function
|
2018-03-04 16:15:50 +01:00
|
|
|
{
|
2019-02-11 23:13:13 +01:00
|
|
|
"m_presence", "ircd::m::presence::get"
|
2018-03-04 16:15:50 +01:00
|
|
|
};
|
|
|
|
|
2019-02-11 23:13:13 +01:00
|
|
|
return function(std::nothrow, user, closure, opts);
|
2019-01-27 00:24:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::idx
|
|
|
|
ircd::m::presence::get(const user &user)
|
|
|
|
{
|
|
|
|
const event::idx ret
|
|
|
|
{
|
|
|
|
get(std::nothrow, user)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!ret)
|
|
|
|
throw m::NOT_FOUND
|
|
|
|
{
|
|
|
|
"No presence found for %s", string_view{user.user_id}
|
|
|
|
};
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::idx
|
|
|
|
ircd::m::presence::get(std::nothrow_t,
|
|
|
|
const user &user)
|
|
|
|
{
|
|
|
|
using prototype = event::idx (std::nothrow_t, const m::user &);
|
|
|
|
|
|
|
|
static mods::import<prototype> function
|
|
|
|
{
|
2019-02-11 23:13:13 +01:00
|
|
|
"m_presence", "ircd::m::presence::get"
|
2019-01-27 00:24:29 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return function(std::nothrow, user);
|
2018-03-04 16:15:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::presence::valid_state(const string_view &state)
|
2018-03-03 09:18:07 +01:00
|
|
|
{
|
2018-03-04 16:15:50 +01:00
|
|
|
using prototype = bool (const string_view &);
|
2018-03-03 09:18:07 +01:00
|
|
|
|
2018-09-14 16:39:11 +02:00
|
|
|
static mods::import<prototype> function
|
2018-03-03 09:18:07 +01:00
|
|
|
{
|
2019-02-11 23:13:13 +01:00
|
|
|
"m_presence", "ircd::m::presence::valid_state"
|
2018-03-03 09:18:07 +01:00
|
|
|
};
|
|
|
|
|
2018-03-04 16:15:50 +01:00
|
|
|
return function(state);
|
2018-03-03 09:18:07 +01:00
|
|
|
}
|
|
|
|
|
2019-02-20 02:39:24 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// m/device.h
|
|
|
|
//
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::device::set(const m::user &user,
|
|
|
|
const device &device_)
|
|
|
|
{
|
|
|
|
using prototype = bool (const m::user &, const device &);
|
|
|
|
|
|
|
|
static mods::import<prototype> function
|
|
|
|
{
|
2019-02-20 18:39:57 +01:00
|
|
|
"m_device", "ircd::m::device::set"
|
2019-02-20 02:39:24 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return function(user, device_);
|
|
|
|
}
|
|
|
|
|
2019-02-25 00:38:48 +01:00
|
|
|
bool
|
|
|
|
ircd::m::device::set(const m::user &user,
|
|
|
|
const string_view &id,
|
|
|
|
const string_view &prop,
|
|
|
|
const string_view &val)
|
|
|
|
{
|
|
|
|
using prototype = bool (const m::user &, const string_view &, const string_view &, const string_view &);
|
|
|
|
|
|
|
|
static mods::import<prototype> function
|
|
|
|
{
|
|
|
|
"m_device", "ircd::m::device::set"
|
|
|
|
};
|
|
|
|
|
|
|
|
return function(user, id, prop, val);
|
|
|
|
}
|
|
|
|
|
2019-02-20 02:39:24 +01:00
|
|
|
bool
|
|
|
|
ircd::m::device::del(const m::user &user,
|
|
|
|
const string_view &id)
|
|
|
|
{
|
|
|
|
using prototype = bool (const m::user &, const string_view &);
|
|
|
|
|
|
|
|
static mods::import<prototype> function
|
|
|
|
{
|
2019-02-20 18:39:57 +01:00
|
|
|
"m_device", "ircd::m::device::del"
|
2019-02-20 02:39:24 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return function(user, id);
|
|
|
|
}
|
|
|
|
|
2019-02-20 22:09:32 +01:00
|
|
|
bool
|
|
|
|
ircd::m::device::has(const m::user &user,
|
|
|
|
const string_view &id)
|
|
|
|
{
|
|
|
|
using prototype = bool (const m::user &, const string_view &id);
|
|
|
|
|
|
|
|
static mods::import<prototype> function
|
|
|
|
{
|
|
|
|
"m_device", "ircd::m::device::has"
|
|
|
|
};
|
|
|
|
|
|
|
|
return function(user, id);
|
|
|
|
}
|
|
|
|
|
2019-03-10 03:48:32 +01:00
|
|
|
bool
|
|
|
|
ircd::m::device::has(const m::user &user,
|
|
|
|
const string_view &id,
|
|
|
|
const string_view &prop)
|
|
|
|
{
|
|
|
|
using prototype = bool (const m::user &, const string_view &id, const string_view &prop);
|
|
|
|
|
|
|
|
static mods::import<prototype> function
|
|
|
|
{
|
|
|
|
"m_device", "ircd::m::device::has"
|
|
|
|
};
|
|
|
|
|
|
|
|
return function(user, id, prop);
|
|
|
|
}
|
|
|
|
|
2019-02-20 02:39:24 +01:00
|
|
|
bool
|
|
|
|
ircd::m::device::get(const m::user &user,
|
|
|
|
const string_view &id,
|
2019-02-20 22:09:32 +01:00
|
|
|
const string_view &prop,
|
2019-02-20 02:39:24 +01:00
|
|
|
const closure &c)
|
|
|
|
{
|
|
|
|
const bool ret
|
|
|
|
{
|
2019-02-20 22:09:32 +01:00
|
|
|
get(std::nothrow, user, id, prop, c)
|
2019-02-20 02:39:24 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
if(!ret)
|
|
|
|
throw m::NOT_FOUND
|
|
|
|
{
|
2019-02-20 22:09:32 +01:00
|
|
|
"Property '%s' for device '%s' for user %s not found",
|
2019-02-20 02:39:24 +01:00
|
|
|
id,
|
2019-02-20 22:09:32 +01:00
|
|
|
prop,
|
2019-02-20 02:39:24 +01:00
|
|
|
string_view{user.user_id}
|
|
|
|
};
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::device::get(std::nothrow_t,
|
|
|
|
const m::user &user,
|
|
|
|
const string_view &id,
|
2019-02-20 22:09:32 +01:00
|
|
|
const string_view &prop,
|
2019-02-20 02:39:24 +01:00
|
|
|
const closure &c)
|
|
|
|
{
|
2019-02-20 22:09:32 +01:00
|
|
|
using prototype = bool (std::nothrow_t,
|
|
|
|
const m::user &,
|
|
|
|
const string_view &,
|
|
|
|
const string_view &,
|
|
|
|
const closure &);
|
2019-02-20 02:39:24 +01:00
|
|
|
|
|
|
|
static mods::import<prototype> function
|
|
|
|
{
|
2019-02-20 18:39:57 +01:00
|
|
|
"m_device", "ircd::m::device::get"
|
2019-02-20 02:39:24 +01:00
|
|
|
};
|
|
|
|
|
2019-02-20 22:09:32 +01:00
|
|
|
return function(std::nothrow, user, id, prop, c);
|
2019-02-20 02:39:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::device::for_each(const m::user &user,
|
2019-02-20 22:09:32 +01:00
|
|
|
const string_view &id,
|
|
|
|
const closure_bool &c)
|
2019-02-20 02:39:24 +01:00
|
|
|
{
|
2019-02-20 22:09:32 +01:00
|
|
|
using prototype = bool (const m::user &, const string_view &id, const closure_bool &);
|
2019-02-20 02:39:24 +01:00
|
|
|
|
|
|
|
static mods::import<prototype> function
|
|
|
|
{
|
2019-02-20 18:39:57 +01:00
|
|
|
"m_device", "ircd::m::device::for_each"
|
2019-02-20 02:39:24 +01:00
|
|
|
};
|
|
|
|
|
2019-02-20 22:09:32 +01:00
|
|
|
return function(user, id, c);
|
2019-02-20 02:39:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::device::for_each(const m::user &user,
|
|
|
|
const closure_bool &c)
|
|
|
|
{
|
|
|
|
using prototype = bool (const m::user &, const closure_bool &);
|
|
|
|
|
|
|
|
static mods::import<prototype> function
|
|
|
|
{
|
2019-02-20 18:39:57 +01:00
|
|
|
"m_device", "ircd::m::device::for_each"
|
2019-02-20 02:39:24 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return function(user, c);
|
|
|
|
}
|
|
|
|
|
2018-03-05 17:53:48 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// m/node.h
|
|
|
|
//
|
|
|
|
|
|
|
|
/// ID of the room which indexes all nodes (an instance of the room is
|
|
|
|
/// provided below).
|
2018-06-17 07:00:47 +02:00
|
|
|
ircd::m::room::id::buf
|
2018-03-05 17:53:48 +01:00
|
|
|
nodes_room_id
|
|
|
|
{
|
|
|
|
"nodes", ircd::my_host()
|
|
|
|
};
|
|
|
|
|
|
|
|
/// The nodes room is the database of all nodes. It primarily serves as an
|
|
|
|
/// indexing mechanism and for top-level node related keys.
|
|
|
|
///
|
2018-06-17 07:00:47 +02:00
|
|
|
ircd::m::room
|
2018-03-05 17:53:48 +01:00
|
|
|
ircd::m::nodes
|
|
|
|
{
|
|
|
|
nodes_room_id
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// node
|
|
|
|
//
|
|
|
|
|
2019-06-22 05:59:41 +02:00
|
|
|
ircd::m::node::node(const string_view &node_id)
|
|
|
|
:node_id{node_id}
|
2018-05-11 11:05:08 +02:00
|
|
|
{
|
2019-06-22 05:59:41 +02:00
|
|
|
rfc3986::valid_remote(node_id);
|
2018-05-11 11:05:08 +02:00
|
|
|
}
|
|
|
|
|
2019-06-23 03:18:37 +02:00
|
|
|
void
|
|
|
|
ircd::m::node::key(const string_view &key_id,
|
|
|
|
const ed25519_closure &closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
key(key_id, key_closure{[&closure]
|
|
|
|
(const string_view &keyb64)
|
|
|
|
{
|
|
|
|
const ed25519::pk pk
|
|
|
|
{
|
|
|
|
[&keyb64](auto &buf)
|
|
|
|
{
|
|
|
|
b64decode(buf, unquote(keyb64));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
closure(pk);
|
|
|
|
}});
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::node::key(const string_view &key_id,
|
|
|
|
const key_closure &closure)
|
|
|
|
const
|
|
|
|
{
|
2019-06-24 01:35:42 +02:00
|
|
|
using prototype = void (const string_view &, const string_view &, const keys::closure &);
|
|
|
|
|
|
|
|
//TODO: Remove this import once this callsite is outside of libircd.
|
|
|
|
static mods::import<prototype> call
|
|
|
|
{
|
2019-06-27 08:05:18 +02:00
|
|
|
"m_keys", "ircd::m::keys::get"
|
2019-06-24 01:35:42 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
call(node_id, key_id, [&closure, &key_id]
|
2019-06-23 03:18:37 +02:00
|
|
|
(const json::object &keys)
|
|
|
|
{
|
|
|
|
const json::object &vks
|
|
|
|
{
|
|
|
|
keys.at("verify_keys")
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::object &vkk
|
|
|
|
{
|
|
|
|
vks.at(key_id)
|
|
|
|
};
|
|
|
|
|
|
|
|
const string_view &key
|
|
|
|
{
|
|
|
|
vkk.at("key")
|
|
|
|
};
|
|
|
|
|
|
|
|
closure(key);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-05 17:53:48 +01:00
|
|
|
/// Generates a node-room ID into buffer; see room_id() overload.
|
|
|
|
ircd::m::id::room::buf
|
|
|
|
ircd::m::node::room_id()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
ircd::m::id::room::buf buf;
|
|
|
|
return buf.assigned(room_id(buf));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This generates a room mxid for the "node's room" essentially serving as
|
|
|
|
/// a database mechanism for this specific node. This room_id is a hash of
|
|
|
|
/// the node's full mxid.
|
|
|
|
///
|
|
|
|
ircd::m::id::room
|
|
|
|
ircd::m::node::room_id(const mutable_buffer &buf)
|
|
|
|
const
|
|
|
|
{
|
2019-05-27 02:44:12 +02:00
|
|
|
assert(!empty(this->node_id));
|
|
|
|
|
|
|
|
// for compatibility with hashing legacy node_id's
|
|
|
|
thread_local char node_id_buf[m::id::MAX_SIZE];
|
|
|
|
mutable_buffer mb{node_id_buf};
|
|
|
|
consume(mb, copy(mb, "::"_sv));
|
|
|
|
consume(mb, copy(mb, this->node_id));
|
|
|
|
const string_view &node_id
|
|
|
|
{
|
|
|
|
node_id_buf, data(mb)
|
|
|
|
};
|
|
|
|
|
2018-03-05 17:53:48 +01:00
|
|
|
const sha256::buf hash
|
|
|
|
{
|
|
|
|
sha256{node_id}
|
|
|
|
};
|
|
|
|
|
2019-05-27 02:44:12 +02:00
|
|
|
thread_local char b58_buf[b58encode_size(size(hash))];
|
|
|
|
const string_view b58
|
2018-03-05 17:53:48 +01:00
|
|
|
{
|
2019-05-27 02:44:12 +02:00
|
|
|
b58encode(b58_buf, hash)
|
|
|
|
};
|
|
|
|
|
|
|
|
return id::room
|
|
|
|
{
|
|
|
|
buf, b58, my_host()
|
2018-03-05 17:53:48 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// node::room
|
|
|
|
//
|
|
|
|
|
2019-05-27 02:44:12 +02:00
|
|
|
ircd::m::node::room::room(const string_view &node_id)
|
2018-03-05 17:53:48 +01:00
|
|
|
:room{m::node{node_id}}
|
|
|
|
{}
|
|
|
|
|
|
|
|
ircd::m::node::room::room(const m::node &node)
|
|
|
|
:node{node}
|
|
|
|
,room_id{node.room_id()}
|
|
|
|
{
|
|
|
|
static_cast<m::room &>(*this) = room_id;
|
|
|
|
}
|
|
|
|
|
2018-05-13 01:00:37 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// m/filter.h
|
|
|
|
//
|
|
|
|
|
|
|
|
//TODO: globular expression
|
2018-05-13 01:13:55 +02:00
|
|
|
//TODO: tribool for contains_url; we currently ignore the false value.
|
2018-05-13 01:00:37 +02:00
|
|
|
bool
|
|
|
|
ircd::m::match(const room_event_filter &filter,
|
|
|
|
const event &event)
|
|
|
|
{
|
2018-05-13 01:13:55 +02:00
|
|
|
if(json::get<"contains_url"_>(filter) == true)
|
|
|
|
if(!at<"content"_>(event).has("url"))
|
|
|
|
return false;
|
|
|
|
|
2018-05-13 01:00:37 +02:00
|
|
|
for(const auto &room_id : json::get<"not_rooms"_>(filter))
|
|
|
|
if(at<"room_id"_>(event) == unquote(room_id))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(empty(json::get<"rooms"_>(filter)))
|
|
|
|
return match(event_filter{filter}, event);
|
|
|
|
|
|
|
|
for(const auto &room_id : json::get<"rooms"_>(filter))
|
|
|
|
if(at<"room_id"_>(event) == unquote(room_id))
|
|
|
|
return match(event_filter{filter}, event);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO: globular expression
|
|
|
|
bool
|
|
|
|
ircd::m::match(const event_filter &filter,
|
|
|
|
const event &event)
|
|
|
|
{
|
|
|
|
for(const auto &type : json::get<"not_types"_>(filter))
|
|
|
|
if(at<"type"_>(event) == unquote(type))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for(const auto &sender : json::get<"not_senders"_>(filter))
|
|
|
|
if(at<"sender"_>(event) == unquote(sender))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(empty(json::get<"senders"_>(filter)) && empty(json::get<"types"_>(filter)))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if(empty(json::get<"senders"_>(filter)))
|
|
|
|
{
|
|
|
|
for(const auto &type : json::get<"types"_>(filter))
|
|
|
|
if(at<"type"_>(event) == unquote(type))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(empty(json::get<"types"_>(filter)))
|
|
|
|
{
|
|
|
|
for(const auto &sender : json::get<"senders"_>(filter))
|
|
|
|
if(at<"sender"_>(event) == unquote(sender))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// filter
|
|
|
|
//
|
|
|
|
|
2019-03-31 23:33:12 +02:00
|
|
|
/// Convenience interface for filters out of common `?filter=` query string
|
|
|
|
/// arguments. This function expects a raw urlencoded value of the filter
|
|
|
|
/// query parameter. It detects if the value is an "inline" filter by being
|
|
|
|
/// a valid JSON object; otherwise it considers the value an ID and fetches
|
|
|
|
/// the filter stored previously by the user.
|
|
|
|
std::string
|
|
|
|
ircd::m::filter::get(const string_view &val,
|
|
|
|
const m::user &user)
|
|
|
|
{
|
|
|
|
if(!val)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
const bool is_inline
|
|
|
|
{
|
|
|
|
startswith(val, "{") || startswith(val, "%7B")
|
|
|
|
};
|
|
|
|
|
|
|
|
if(is_inline)
|
|
|
|
return util::string(val.size(), [&val]
|
|
|
|
(const mutable_buffer &buf)
|
|
|
|
{
|
|
|
|
return url::decode(buf, val);
|
|
|
|
});
|
|
|
|
|
|
|
|
if(!user.user_id)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
char idbuf[m::event::STATE_KEY_MAX_SIZE];
|
|
|
|
const string_view &id
|
|
|
|
{
|
|
|
|
url::decode(idbuf, val)
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::user::filter filter
|
|
|
|
{
|
|
|
|
user
|
|
|
|
};
|
|
|
|
|
|
|
|
return filter.get(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// filter::filter
|
|
|
|
//
|
|
|
|
|
2018-05-13 01:00:37 +02:00
|
|
|
ircd::m::filter::filter(const user &user,
|
|
|
|
const string_view &filter_id,
|
|
|
|
const mutable_buffer &buf)
|
|
|
|
{
|
2019-03-31 23:33:12 +02:00
|
|
|
const json::object &obj
|
2018-05-13 01:00:37 +02:00
|
|
|
{
|
2019-03-31 23:33:12 +02:00
|
|
|
user::filter(user).get(buf, filter_id)
|
|
|
|
};
|
2018-05-13 01:00:37 +02:00
|
|
|
|
2019-03-31 23:33:12 +02:00
|
|
|
new (this) m::filter
|
|
|
|
{
|
|
|
|
obj
|
|
|
|
};
|
2018-05-13 01:00:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// room_filter
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::m::room_filter::room_filter(const mutable_buffer &buf,
|
|
|
|
const json::members &members)
|
|
|
|
:super_type::tuple
|
|
|
|
{
|
|
|
|
json::stringify(mutable_buffer{buf}, members)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-03-31 22:25:00 +02:00
|
|
|
//
|
|
|
|
// state_filter
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::m::state_filter::state_filter(const mutable_buffer &buf,
|
|
|
|
const json::members &members)
|
|
|
|
:super_type::tuple
|
|
|
|
{
|
|
|
|
json::stringify(mutable_buffer{buf}, members)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-05-13 01:00:37 +02:00
|
|
|
//
|
|
|
|
// room_event_filter
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::m::room_event_filter::room_event_filter(const mutable_buffer &buf,
|
|
|
|
const json::members &members)
|
|
|
|
:super_type::tuple
|
|
|
|
{
|
|
|
|
json::stringify(mutable_buffer{buf}, members)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// event_filter
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::m::event_filter::event_filter(const mutable_buffer &buf,
|
|
|
|
const json::members &members)
|
|
|
|
:super_type::tuple
|
|
|
|
{
|
|
|
|
json::stringify(mutable_buffer{buf}, members)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-04-12 00:10:45 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// m/rooms.h
|
|
|
|
//
|
|
|
|
|
2019-03-14 23:07:19 +01:00
|
|
|
ircd::m::event::id::buf
|
|
|
|
ircd::m::rooms::summary_del(const m::room &r)
|
|
|
|
{
|
|
|
|
using prototype = event::id::buf (const m::room &);
|
|
|
|
|
|
|
|
static mods::import<prototype> call
|
|
|
|
{
|
|
|
|
"m_rooms", "ircd::m::rooms::summary_del"
|
|
|
|
};
|
|
|
|
|
|
|
|
return call(r);
|
|
|
|
}
|
|
|
|
|
2018-10-25 02:51:33 +02:00
|
|
|
ircd::m::event::id::buf
|
|
|
|
ircd::m::rooms::summary_set(const m::room &room)
|
|
|
|
{
|
|
|
|
if(!exists(room))
|
|
|
|
throw m::NOT_FOUND
|
|
|
|
{
|
|
|
|
"Cannot set a summary for room '%s' which I have no state for",
|
|
|
|
string_view{room.room_id}
|
|
|
|
};
|
|
|
|
|
|
|
|
const unique_buffer<mutable_buffer> buf
|
|
|
|
{
|
|
|
|
48_KiB
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::object summary
|
|
|
|
{
|
|
|
|
summary_chunk(room, buf)
|
|
|
|
};
|
|
|
|
|
|
|
|
return summary_set(room.room_id, summary);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::id::buf
|
|
|
|
ircd::m::rooms::summary_set(const m::room::id &room_id,
|
|
|
|
const json::object &summary)
|
|
|
|
{
|
|
|
|
using prototype = event::id::buf (const m::room::id &, const json::object &);
|
|
|
|
|
|
|
|
static mods::import<prototype> function
|
|
|
|
{
|
2019-05-06 20:46:05 +02:00
|
|
|
"m_rooms", "ircd::m::rooms::summary_set"
|
2018-10-25 02:51:33 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return function(room_id, summary);
|
|
|
|
}
|
|
|
|
|
2018-10-24 23:01:21 +02:00
|
|
|
ircd::json::object
|
|
|
|
ircd::m::rooms::summary_chunk(const m::room &room,
|
|
|
|
const mutable_buffer &buf)
|
|
|
|
{
|
|
|
|
json::stack out{buf};
|
|
|
|
{
|
|
|
|
json::stack::object obj{out};
|
|
|
|
summary_chunk(room, obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
return json::object
|
|
|
|
{
|
|
|
|
out.completed()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::rooms::summary_chunk(const m::room &room,
|
|
|
|
json::stack::object &chunk)
|
|
|
|
{
|
|
|
|
using prototype = void (const m::room &, json::stack::object &);
|
|
|
|
|
|
|
|
static mods::import<prototype> function
|
|
|
|
{
|
2019-05-06 20:46:05 +02:00
|
|
|
"m_rooms", "ircd::m::rooms::summary_chunk"
|
2018-10-24 23:01:21 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return function(room, chunk);
|
|
|
|
}
|
|
|
|
|
2018-10-24 23:08:14 +02:00
|
|
|
bool
|
2019-04-18 05:35:34 +02:00
|
|
|
ircd::m::rooms::for_each(const each_opts &opts)
|
2018-04-12 00:10:45 +02:00
|
|
|
{
|
2019-04-18 05:35:34 +02:00
|
|
|
using prototype = bool (const each_opts &);
|
2018-04-12 00:10:45 +02:00
|
|
|
|
2019-04-18 05:35:34 +02:00
|
|
|
static mods::import<prototype> call
|
|
|
|
{
|
|
|
|
"m_rooms", "ircd::m::rooms::for_each"
|
|
|
|
};
|
2018-04-12 00:10:45 +02:00
|
|
|
|
2019-04-18 05:35:34 +02:00
|
|
|
return call(opts);
|
2018-04-12 00:10:45 +02:00
|
|
|
}
|
|
|
|
|
2019-03-01 23:51:30 +01:00
|
|
|
bool
|
|
|
|
ircd::m::rooms::is_public(const room::id &room_id)
|
|
|
|
{
|
|
|
|
using prototype = bool (const room::id &);
|
|
|
|
|
|
|
|
static mods::import<prototype> call
|
|
|
|
{
|
|
|
|
"m_rooms", "ircd::m::rooms::is_public"
|
|
|
|
};
|
|
|
|
|
|
|
|
return call(room_id);
|
|
|
|
}
|
|
|
|
|
2018-10-24 23:22:10 +02:00
|
|
|
size_t
|
|
|
|
ircd::m::rooms::count_public(const string_view &server)
|
|
|
|
{
|
|
|
|
using prototype = size_t (const string_view &);
|
|
|
|
|
|
|
|
static mods::import<prototype> function
|
|
|
|
{
|
2019-05-06 20:46:05 +02:00
|
|
|
"m_rooms", "ircd::m::rooms::count_public"
|
2018-10-24 23:22:10 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return function(server);
|
|
|
|
}
|
|
|
|
|
2018-10-23 22:26:57 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// m/users.h
|
|
|
|
//
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::users::for_each(const user::closure &closure)
|
|
|
|
{
|
|
|
|
for_each(user::closure_bool{[&closure]
|
|
|
|
(const m::user &user)
|
|
|
|
{
|
|
|
|
closure(user);
|
|
|
|
return true;
|
|
|
|
}});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::users::for_each(const user::closure_bool &closure)
|
2018-10-23 23:17:42 +02:00
|
|
|
{
|
|
|
|
return for_each(string_view{}, closure);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::users::for_each(const string_view &lower_bound,
|
|
|
|
const user::closure_bool &closure)
|
2018-10-23 22:26:57 +02:00
|
|
|
{
|
|
|
|
const m::room::state state
|
|
|
|
{
|
|
|
|
user::users
|
|
|
|
};
|
|
|
|
|
2018-10-23 23:17:42 +02:00
|
|
|
return state.for_each("ircd.user", lower_bound, m::room::state::keys_bool{[&closure]
|
2018-10-23 22:26:57 +02:00
|
|
|
(const string_view &user_id)
|
|
|
|
{
|
|
|
|
const m::user &user
|
|
|
|
{
|
|
|
|
user_id
|
|
|
|
};
|
|
|
|
|
|
|
|
return closure(user);
|
|
|
|
}});
|
|
|
|
}
|
|
|
|
|
2018-03-05 12:42:41 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// m/user.h
|
|
|
|
//
|
|
|
|
|
2018-03-05 17:25:59 +01:00
|
|
|
/// ID of the room which indexes all users (an instance of the room is
|
|
|
|
/// provided below).
|
2018-06-17 07:00:47 +02:00
|
|
|
ircd::m::room::id::buf
|
2018-03-05 17:25:59 +01:00
|
|
|
users_room_id
|
|
|
|
{
|
|
|
|
"users", ircd::my_host()
|
|
|
|
};
|
|
|
|
|
|
|
|
/// The users room is the database of all users. It primarily serves as an
|
|
|
|
/// indexing mechanism and for top-level user related keys. Accounts
|
|
|
|
/// registered on this server will be among state events in this room.
|
|
|
|
/// Users do not have access to this room, it is used internally.
|
|
|
|
///
|
|
|
|
ircd::m::room
|
|
|
|
ircd::m::user::users
|
|
|
|
{
|
|
|
|
users_room_id
|
|
|
|
};
|
|
|
|
|
|
|
|
/// ID of the room which stores ephemeral tokens (an instance of the room is
|
|
|
|
/// provided below).
|
2018-06-17 07:00:47 +02:00
|
|
|
ircd::m::room::id::buf
|
2018-03-05 17:25:59 +01:00
|
|
|
tokens_room_id
|
|
|
|
{
|
|
|
|
"tokens", ircd::my_host()
|
|
|
|
};
|
|
|
|
|
|
|
|
/// The tokens room serves as a key-value lookup for various tokens to
|
|
|
|
/// users, etc. It primarily serves to store access tokens for users. This
|
|
|
|
/// is a separate room from the users room because in the future it may
|
|
|
|
/// have an optimized configuration as well as being more easily cleared.
|
|
|
|
///
|
|
|
|
ircd::m::room
|
|
|
|
ircd::m::user::tokens
|
|
|
|
{
|
|
|
|
tokens_room_id
|
|
|
|
};
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::exists(const user::id &user_id)
|
|
|
|
{
|
|
|
|
return user::users.has("ircd.user", user_id);
|
|
|
|
}
|
|
|
|
|
2018-03-09 18:00:28 +01:00
|
|
|
bool
|
|
|
|
ircd::m::exists(const user &user)
|
|
|
|
{
|
|
|
|
return exists(user.user_id);
|
|
|
|
}
|
|
|
|
|
2018-03-05 17:25:59 +01:00
|
|
|
bool
|
|
|
|
ircd::m::my(const user &user)
|
|
|
|
{
|
|
|
|
return my(user.user_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// user
|
|
|
|
//
|
|
|
|
|
|
|
|
/// Generates a user-room ID into buffer; see room_id() overload.
|
|
|
|
ircd::m::id::room::buf
|
|
|
|
ircd::m::user::room_id()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
ircd::m::id::room::buf buf;
|
|
|
|
return buf.assigned(room_id(buf));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This generates a room mxid for the "user's room" essentially serving as
|
|
|
|
/// a database mechanism for this specific user. This room_id is a hash of
|
|
|
|
/// the user's full mxid.
|
|
|
|
///
|
|
|
|
ircd::m::id::room
|
|
|
|
ircd::m::user::room_id(const mutable_buffer &buf)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
assert(!empty(user_id));
|
2018-04-08 19:39:42 +02:00
|
|
|
const ripemd160::buf hash
|
2018-03-05 17:25:59 +01:00
|
|
|
{
|
2018-04-08 19:39:42 +02:00
|
|
|
ripemd160{user_id}
|
2018-03-05 17:25:59 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
char b58[size(hash) * 2];
|
|
|
|
return
|
|
|
|
{
|
|
|
|
buf, b58encode(b58, hash), my_host()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-02-21 03:58:49 +01:00
|
|
|
ircd::m::device::id::buf
|
|
|
|
ircd::m::user::get_device_from_access_token(const string_view &token)
|
|
|
|
{
|
|
|
|
const event::idx event_idx
|
|
|
|
{
|
|
|
|
user::tokens.get("ircd.access_token", token)
|
|
|
|
};
|
|
|
|
|
|
|
|
device::id::buf ret;
|
|
|
|
m::get(event_idx, "content", [&ret]
|
|
|
|
(const json::object &content)
|
|
|
|
{
|
|
|
|
ret = unquote(content.at("device_id"));
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-03-05 17:25:59 +01:00
|
|
|
ircd::string_view
|
|
|
|
ircd::m::user::gen_access_token(const mutable_buffer &buf)
|
|
|
|
{
|
|
|
|
static const size_t token_max{32};
|
|
|
|
static const auto &token_dict{rand::dict::alpha};
|
|
|
|
|
|
|
|
const mutable_buffer out
|
|
|
|
{
|
|
|
|
data(buf), std::min(token_max, size(buf))
|
|
|
|
};
|
|
|
|
|
|
|
|
return rand::string(token_dict, out);
|
|
|
|
}
|
|
|
|
|
2018-03-05 12:42:41 +01:00
|
|
|
ircd::m::event::id::buf
|
2018-03-09 18:00:28 +01:00
|
|
|
ircd::m::user::activate()
|
2018-03-05 12:42:41 +01:00
|
|
|
{
|
2018-03-09 18:00:28 +01:00
|
|
|
using prototype = event::id::buf (const m::user &);
|
2018-03-05 12:42:41 +01:00
|
|
|
|
2018-09-14 16:39:11 +02:00
|
|
|
static mods::import<prototype> function
|
2018-03-05 12:42:41 +01:00
|
|
|
{
|
2018-03-05 13:21:19 +01:00
|
|
|
"client_account", "activate__user"
|
2018-03-05 12:42:41 +01:00
|
|
|
};
|
|
|
|
|
2018-03-09 18:00:28 +01:00
|
|
|
return function(*this);
|
2018-03-05 12:42:41 +01:00
|
|
|
}
|
|
|
|
|
2018-03-03 09:18:07 +01:00
|
|
|
ircd::m::event::id::buf
|
2018-03-09 18:00:28 +01:00
|
|
|
ircd::m::user::deactivate()
|
2018-03-02 16:25:37 +01:00
|
|
|
{
|
2018-03-09 18:00:28 +01:00
|
|
|
using prototype = event::id::buf (const m::user &);
|
2018-03-02 16:25:37 +01:00
|
|
|
|
2018-09-14 16:39:11 +02:00
|
|
|
static mods::import<prototype> function
|
2018-03-02 16:25:37 +01:00
|
|
|
{
|
2018-03-05 12:42:41 +01:00
|
|
|
"client_account", "deactivate__user"
|
2018-03-02 16:25:37 +01:00
|
|
|
};
|
|
|
|
|
2018-03-09 18:00:28 +01:00
|
|
|
return function(*this);
|
2018-03-02 16:25:37 +01:00
|
|
|
}
|
|
|
|
|
2018-03-05 12:59:46 +01:00
|
|
|
bool
|
|
|
|
ircd::m::user::is_active()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
using prototype = bool (const m::user &);
|
|
|
|
|
2018-09-14 16:39:11 +02:00
|
|
|
static mods::import<prototype> function
|
2018-03-05 12:59:46 +01:00
|
|
|
{
|
|
|
|
"client_account", "is_active__user"
|
|
|
|
};
|
|
|
|
|
|
|
|
return function(*this);
|
|
|
|
}
|
|
|
|
|
2018-03-05 13:00:17 +01:00
|
|
|
ircd::m::event::id::buf
|
|
|
|
ircd::m::user::password(const string_view &password)
|
|
|
|
{
|
|
|
|
using prototype = event::id::buf (const m::user::id &, const string_view &) noexcept;
|
|
|
|
|
2018-09-14 16:39:11 +02:00
|
|
|
static mods::import<prototype> function
|
2018-03-05 13:00:17 +01:00
|
|
|
{
|
|
|
|
"client_account", "set_password"
|
|
|
|
};
|
|
|
|
|
|
|
|
return function(user_id, password);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::user::is_password(const string_view &password)
|
|
|
|
const noexcept try
|
|
|
|
{
|
|
|
|
using prototype = bool (const m::user::id &, const string_view &) noexcept;
|
|
|
|
|
2018-09-14 16:39:11 +02:00
|
|
|
static mods::import<prototype> function
|
2018-03-05 13:00:17 +01:00
|
|
|
{
|
|
|
|
"client_account", "is_password"
|
|
|
|
};
|
|
|
|
|
|
|
|
return function(user_id, password);
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::critical
|
|
|
|
{
|
|
|
|
"user::is_password(): %s %s",
|
|
|
|
string_view{user_id},
|
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-03-05 17:25:59 +01:00
|
|
|
//
|
|
|
|
// user::room
|
|
|
|
//
|
|
|
|
|
2018-05-24 01:44:16 +02:00
|
|
|
ircd::m::user::room::room(const m::user::id &user_id,
|
|
|
|
const vm::copts *const &copts,
|
|
|
|
const event::fetch::opts *const &fopts)
|
|
|
|
:room
|
|
|
|
{
|
|
|
|
m::user{user_id}, copts, fopts
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
2018-03-05 17:25:59 +01:00
|
|
|
|
2018-05-24 01:44:16 +02:00
|
|
|
ircd::m::user::room::room(const m::user &user,
|
|
|
|
const vm::copts *const &copts,
|
|
|
|
const event::fetch::opts *const &fopts)
|
2018-03-05 17:25:59 +01:00
|
|
|
:user{user}
|
|
|
|
,room_id{user.room_id()}
|
|
|
|
{
|
2018-05-24 01:44:16 +02:00
|
|
|
static_cast<m::room &>(*this) =
|
|
|
|
{
|
|
|
|
room_id, copts, fopts
|
|
|
|
};
|
2018-03-05 17:25:59 +01:00
|
|
|
}
|
|
|
|
|
2018-04-12 01:12:26 +02:00
|
|
|
//
|
|
|
|
// user::mitsein
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::m::user::mitsein::mitsein(const m::user &user)
|
|
|
|
:user{user}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-04 23:47:46 +01:00
|
|
|
bool
|
|
|
|
ircd::m::user::mitsein::has(const m::user &other,
|
|
|
|
const string_view &membership)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
// Return true if broken out of loop.
|
|
|
|
return !for_each(other, membership, rooms::closure_bool{[]
|
|
|
|
(const m::room &, const string_view &)
|
|
|
|
{
|
|
|
|
// Break out of loop at first shared room
|
|
|
|
return false;
|
|
|
|
}});
|
|
|
|
}
|
|
|
|
|
2018-05-08 09:44:23 +02:00
|
|
|
size_t
|
|
|
|
ircd::m::user::mitsein::count(const string_view &membership)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
size_t ret{0};
|
|
|
|
for_each(membership, [&ret](const m::user &)
|
|
|
|
{
|
|
|
|
++ret;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::m::user::mitsein::count(const m::user &user,
|
|
|
|
const string_view &membership)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
size_t ret{0};
|
|
|
|
for_each(user, membership, [&ret](const m::room &, const string_view &)
|
|
|
|
{
|
|
|
|
++ret;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-04-12 01:12:26 +02:00
|
|
|
void
|
|
|
|
ircd::m::user::mitsein::for_each(const closure &closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
for_each(string_view{}, closure);
|
|
|
|
}
|
|
|
|
|
2018-08-24 22:55:33 +02:00
|
|
|
bool
|
2018-04-12 01:12:26 +02:00
|
|
|
ircd::m::user::mitsein::for_each(const closure_bool &closure)
|
|
|
|
const
|
|
|
|
{
|
2018-08-24 22:55:33 +02:00
|
|
|
return for_each(string_view{}, closure);
|
2018-04-12 01:12:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::user::mitsein::for_each(const string_view &membership,
|
|
|
|
const closure &closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
for_each(membership, closure_bool{[&closure]
|
|
|
|
(const m::user &user)
|
|
|
|
{
|
|
|
|
closure(user);
|
|
|
|
return true;
|
|
|
|
}});
|
|
|
|
}
|
|
|
|
|
2018-08-24 22:55:33 +02:00
|
|
|
bool
|
2018-04-12 01:12:26 +02:00
|
|
|
ircd::m::user::mitsein::for_each(const string_view &membership,
|
|
|
|
const closure_bool &closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
const m::user::rooms rooms
|
|
|
|
{
|
|
|
|
user
|
|
|
|
};
|
|
|
|
|
|
|
|
// here we gooooooo :/
|
|
|
|
///TODO: ideal: db schema
|
|
|
|
///TODO: minimally: custom alloc?
|
2018-04-13 07:48:00 +02:00
|
|
|
std::set<std::string, std::less<>> seen;
|
2018-08-24 22:55:33 +02:00
|
|
|
return rooms.for_each(membership, rooms::closure_bool{[&membership, &closure, &seen]
|
2019-01-09 00:11:06 +01:00
|
|
|
(m::room room, const string_view &)
|
2018-04-12 01:12:26 +02:00
|
|
|
{
|
2019-01-09 00:11:06 +01:00
|
|
|
static const event::fetch::opts fopts
|
|
|
|
{
|
|
|
|
event::keys::include {"state_key"}
|
|
|
|
};
|
|
|
|
|
|
|
|
room.fopts = &fopts;
|
2019-07-24 04:31:40 +02:00
|
|
|
const m::room::members members
|
2018-04-12 01:12:26 +02:00
|
|
|
{
|
2019-07-24 04:31:40 +02:00
|
|
|
room
|
|
|
|
};
|
2018-04-12 01:12:26 +02:00
|
|
|
|
2019-07-24 04:31:40 +02:00
|
|
|
return members.for_each(membership, [&seen, &closure]
|
|
|
|
(const user::id &other)
|
|
|
|
{
|
2018-04-12 01:12:26 +02:00
|
|
|
const auto it
|
|
|
|
{
|
|
|
|
seen.lower_bound(other)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(it != end(seen) && *it == other)
|
2018-09-26 01:16:47 +02:00
|
|
|
return true;
|
2018-04-12 01:12:26 +02:00
|
|
|
|
|
|
|
seen.emplace_hint(it, std::string{other});
|
2018-09-26 01:16:47 +02:00
|
|
|
return closure(m::user{other});
|
2019-07-24 04:31:40 +02:00
|
|
|
});
|
2018-04-12 01:12:26 +02:00
|
|
|
}});
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::user::mitsein::for_each(const m::user &user,
|
|
|
|
const rooms::closure &closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
for_each(user, string_view{}, closure);
|
|
|
|
}
|
|
|
|
|
2018-08-24 22:55:33 +02:00
|
|
|
bool
|
2018-04-12 01:12:26 +02:00
|
|
|
ircd::m::user::mitsein::for_each(const m::user &user,
|
|
|
|
const rooms::closure_bool &closure)
|
|
|
|
const
|
|
|
|
{
|
2018-08-24 22:55:33 +02:00
|
|
|
return for_each(user, string_view{}, closure);
|
2018-04-12 01:12:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::user::mitsein::for_each(const m::user &user,
|
|
|
|
const string_view &membership,
|
|
|
|
const rooms::closure &closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
for_each(user, membership, rooms::closure_bool{[&membership, &closure]
|
|
|
|
(const m::room &room, const string_view &)
|
|
|
|
{
|
|
|
|
closure(room, membership);
|
|
|
|
return true;
|
|
|
|
}});
|
|
|
|
}
|
|
|
|
|
2018-08-24 22:55:33 +02:00
|
|
|
bool
|
2018-04-12 01:12:26 +02:00
|
|
|
ircd::m::user::mitsein::for_each(const m::user &user,
|
|
|
|
const string_view &membership,
|
|
|
|
const rooms::closure_bool &closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
const m::user::rooms our_rooms{this->user};
|
|
|
|
const m::user::rooms their_rooms{user};
|
|
|
|
const bool use_our
|
|
|
|
{
|
|
|
|
our_rooms.count() <= their_rooms.count()
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::user::rooms &rooms
|
|
|
|
{
|
|
|
|
use_our? our_rooms : their_rooms
|
|
|
|
};
|
|
|
|
|
|
|
|
const string_view &test_key
|
|
|
|
{
|
|
|
|
use_our? user.user_id : this->user.user_id
|
|
|
|
};
|
|
|
|
|
2018-08-24 22:55:33 +02:00
|
|
|
return rooms.for_each(membership, rooms::closure_bool{[&membership, &closure, &test_key]
|
2018-04-12 01:12:26 +02:00
|
|
|
(const m::room &room, const string_view &)
|
|
|
|
{
|
|
|
|
if(!room.has("m.room.member", test_key))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return closure(room, membership);
|
|
|
|
}});
|
|
|
|
}
|
|
|
|
|
2019-03-03 01:45:13 +01:00
|
|
|
//
|
|
|
|
// user::filter
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::m::user::filter::set(const mutable_buffer &buf,
|
|
|
|
const json::object &val)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return set(buf, user, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
ircd::m::user::filter::get(const string_view &id)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
std::string ret;
|
|
|
|
get(std::nothrow, id, [&ret]
|
|
|
|
(const string_view &id, const json::object &val)
|
|
|
|
{
|
2019-06-07 08:38:36 +02:00
|
|
|
ret = val;
|
2019-03-03 01:45:13 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::json::object
|
|
|
|
ircd::m::user::filter::get(const mutable_buffer &out,
|
|
|
|
const string_view &id)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
json::object ret;
|
|
|
|
get(std::nothrow, id, [&out, &ret]
|
|
|
|
(const string_view &id, const json::object &val)
|
|
|
|
{
|
|
|
|
ret = string_view { data(out), copy(out, val) };
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::user::filter::get(const string_view &id,
|
|
|
|
const closure &closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
if(!get(std::nothrow, user, id, closure))
|
|
|
|
throw m::NOT_FOUND
|
|
|
|
{
|
|
|
|
"filter id '%s' for user %s not found",
|
|
|
|
id,
|
|
|
|
string_view{user.user_id}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::user::filter::get(std::nothrow_t,
|
|
|
|
const string_view &id,
|
|
|
|
const closure &closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return get(std::nothrow, user, id, closure);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::user::filter::for_each(const closure_bool &closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return for_each(user, closure);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::m::user::filter::set(const mutable_buffer &buf,
|
|
|
|
const m::user &u,
|
|
|
|
const json::object &v)
|
|
|
|
{
|
|
|
|
using prototype = string_view (const mutable_buffer &, const m::user &, const json::object &);
|
|
|
|
|
|
|
|
static mods::import<prototype> function
|
|
|
|
{
|
|
|
|
"client_user", "ircd::m::user::filter::set"
|
|
|
|
};
|
|
|
|
|
|
|
|
return function(buf, u, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::user::filter::get(std::nothrow_t,
|
|
|
|
const m::user &u,
|
|
|
|
const string_view &id,
|
|
|
|
const closure &c)
|
|
|
|
{
|
|
|
|
using prototype = bool (std::nothrow_t, const m::user &, const string_view &, const closure &);
|
|
|
|
|
|
|
|
static mods::import<prototype> function
|
|
|
|
{
|
|
|
|
"client_user", "ircd::m::user::filter::get"
|
|
|
|
};
|
|
|
|
|
|
|
|
return function(std::nothrow, u, id, c);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::user::filter::for_each(const m::user &u,
|
|
|
|
const closure_bool &c)
|
|
|
|
{
|
|
|
|
using prototype = bool (const m::user &, const closure_bool &);
|
|
|
|
|
|
|
|
static mods::import<prototype> function
|
|
|
|
{
|
|
|
|
"client_user", "ircd::m::user::filter::for_each"
|
|
|
|
};
|
|
|
|
|
|
|
|
return function(u, c);
|
|
|
|
}
|
|
|
|
|
2018-03-10 02:53:18 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// m/txn.h
|
|
|
|
//
|
|
|
|
|
2018-04-07 06:46:20 +02:00
|
|
|
/// Returns the serial size of the JSON this txn would consume. Note: this
|
|
|
|
/// creates a json::iov involving a timestamp to figure out the total size
|
|
|
|
/// of the txn. When the user creates the actual txn a different timestamp
|
|
|
|
/// is created which may be a different size. Consider using the lower-level
|
|
|
|
/// create(closure) or add some pad to be sure.
|
|
|
|
///
|
|
|
|
size_t
|
|
|
|
ircd::m::txn::serialized(const array &pdu,
|
|
|
|
const array &edu,
|
|
|
|
const array &pdu_failure)
|
|
|
|
{
|
|
|
|
size_t ret;
|
|
|
|
const auto closure{[&ret]
|
|
|
|
(const json::iov &iov)
|
|
|
|
{
|
|
|
|
ret = json::serialized(iov);
|
|
|
|
}};
|
|
|
|
|
|
|
|
create(closure, pdu, edu, pdu_failure);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Stringifies a txn from the inputs into the returned std::string
|
|
|
|
///
|
2018-03-10 02:53:18 +01:00
|
|
|
std::string
|
|
|
|
ircd::m::txn::create(const array &pdu,
|
|
|
|
const array &edu,
|
|
|
|
const array &pdu_failure)
|
2018-04-07 06:46:20 +02:00
|
|
|
{
|
|
|
|
std::string ret;
|
|
|
|
const auto closure{[&ret]
|
|
|
|
(const json::iov &iov)
|
|
|
|
{
|
|
|
|
ret = json::strung(iov);
|
|
|
|
}};
|
|
|
|
|
|
|
|
create(closure, pdu, edu, pdu_failure);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Stringifies a txn from the inputs into the buffer
|
|
|
|
///
|
|
|
|
ircd::string_view
|
|
|
|
ircd::m::txn::create(const mutable_buffer &buf,
|
|
|
|
const array &pdu,
|
|
|
|
const array &edu,
|
|
|
|
const array &pdu_failure)
|
|
|
|
{
|
|
|
|
string_view ret;
|
|
|
|
const auto closure{[&buf, &ret]
|
|
|
|
(const json::iov &iov)
|
|
|
|
{
|
|
|
|
ret = json::stringify(mutable_buffer{buf}, iov);
|
|
|
|
}};
|
|
|
|
|
|
|
|
create(closure, pdu, edu, pdu_failure);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Forms a txn from the inputs into a json::iov and presents that iov
|
|
|
|
/// to the user's closure.
|
|
|
|
///
|
|
|
|
void
|
|
|
|
ircd::m::txn::create(const closure &closure,
|
|
|
|
const array &pdu,
|
|
|
|
const array &edu,
|
|
|
|
const array &pdu_failure)
|
2018-03-10 02:53:18 +01:00
|
|
|
{
|
2018-03-28 22:05:15 +02:00
|
|
|
using ircd::size;
|
|
|
|
|
2018-03-10 02:53:18 +01:00
|
|
|
json::iov iov;
|
|
|
|
const json::iov::push push[]
|
|
|
|
{
|
|
|
|
{ iov, { "origin", my_host() }},
|
|
|
|
{ iov, { "origin_server_ts", ircd::time<milliseconds>() }},
|
|
|
|
};
|
|
|
|
|
2018-06-05 20:19:40 +02:00
|
|
|
const json::iov::add _pdus
|
2018-03-10 02:53:18 +01:00
|
|
|
{
|
2018-03-14 23:56:49 +01:00
|
|
|
iov, !empty(pdu),
|
2018-03-10 02:53:18 +01:00
|
|
|
{
|
2018-06-05 20:19:40 +02:00
|
|
|
"pdus", [&pdu]() -> json::value
|
|
|
|
{
|
|
|
|
return { data(pdu), size(pdu) };
|
|
|
|
}
|
2018-03-10 02:53:18 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-06-05 20:19:40 +02:00
|
|
|
const json::iov::add _edus
|
2018-03-10 02:53:18 +01:00
|
|
|
{
|
2018-03-14 23:56:49 +01:00
|
|
|
iov, !empty(edu),
|
2018-03-10 02:53:18 +01:00
|
|
|
{
|
2018-06-05 20:19:40 +02:00
|
|
|
"edus", [&edu]() -> json::value
|
|
|
|
{
|
|
|
|
return { data(edu), size(edu) };
|
|
|
|
}
|
2018-03-10 02:53:18 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-06-05 20:19:40 +02:00
|
|
|
const json::iov::add _pdu_failures
|
2018-03-10 02:53:18 +01:00
|
|
|
{
|
2018-03-14 23:56:49 +01:00
|
|
|
iov, !empty(pdu_failure),
|
2018-03-10 02:53:18 +01:00
|
|
|
{
|
2018-06-05 20:19:40 +02:00
|
|
|
"pdu_failures", [&pdu_failure]() -> json::value
|
|
|
|
{
|
|
|
|
return { data(pdu_failure), size(pdu_failure) };
|
|
|
|
}
|
2018-03-10 02:53:18 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-04-07 06:46:20 +02:00
|
|
|
closure(iov);
|
2018-03-10 02:53:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::m::txn::create_id(const mutable_buffer &out,
|
|
|
|
const string_view &txn)
|
|
|
|
{
|
|
|
|
const sha256::buf hash
|
|
|
|
{
|
|
|
|
sha256{txn}
|
|
|
|
};
|
|
|
|
|
|
|
|
const string_view txnid
|
|
|
|
{
|
|
|
|
b58encode(out, hash)
|
|
|
|
};
|
|
|
|
|
|
|
|
return txnid;
|
|
|
|
}
|
|
|
|
|
2019-01-23 22:28:51 +01:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// m/request.h
|
|
|
|
//
|
|
|
|
|
|
|
|
//
|
|
|
|
// request
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::m::request::request(const string_view &method,
|
|
|
|
const string_view &uri,
|
|
|
|
const mutable_buffer &body_buf,
|
|
|
|
const json::members &body)
|
|
|
|
:request
|
|
|
|
{
|
|
|
|
my_host(),
|
|
|
|
string_view{},
|
|
|
|
method,
|
|
|
|
uri,
|
|
|
|
json::stringify(mutable_buffer{body_buf}, body)
|
|
|
|
}
|
|
|
|
{}
|
|
|
|
|
|
|
|
ircd::m::request::request(const string_view &method,
|
|
|
|
const string_view &uri)
|
|
|
|
:request
|
|
|
|
{
|
|
|
|
my_host(),
|
|
|
|
string_view{},
|
|
|
|
method,
|
|
|
|
uri,
|
|
|
|
json::object{}
|
|
|
|
}
|
|
|
|
{}
|
|
|
|
|
|
|
|
ircd::m::request::request(const string_view &method,
|
|
|
|
const string_view &uri,
|
|
|
|
const json::object &content)
|
|
|
|
:request
|
|
|
|
{
|
|
|
|
my_host(),
|
|
|
|
string_view{},
|
|
|
|
method,
|
|
|
|
uri,
|
|
|
|
content
|
|
|
|
}
|
|
|
|
{}
|
|
|
|
|
|
|
|
ircd::m::request::request(const string_view &origin,
|
|
|
|
const string_view &destination,
|
|
|
|
const string_view &method,
|
|
|
|
const string_view &uri,
|
|
|
|
const json::object &content)
|
|
|
|
{
|
|
|
|
json::get<"origin"_>(*this) = origin;
|
|
|
|
json::get<"destination"_>(*this) = destination;
|
|
|
|
json::get<"method"_>(*this) = method;
|
|
|
|
json::get<"uri"_>(*this) = uri;
|
|
|
|
json::get<"content"_>(*this) = content;
|
2019-05-06 20:08:33 +02:00
|
|
|
|
|
|
|
if(unlikely(origin && !rfc3986::valid_remote(std::nothrow, origin)))
|
|
|
|
throw m::error
|
|
|
|
{
|
|
|
|
http::BAD_REQUEST, "M_REQUEST_INVALID_ORIGIN",
|
|
|
|
"This origin string '%s' is not a valid remote.",
|
|
|
|
origin
|
|
|
|
};
|
|
|
|
|
|
|
|
if(unlikely(destination && !rfc3986::valid_remote(std::nothrow, destination)))
|
|
|
|
throw m::error
|
|
|
|
{
|
|
|
|
http::BAD_REQUEST, "M_REQUEST_INVALID_DESTINATION",
|
|
|
|
"This destination string '%s' is not a valid remote.",
|
|
|
|
destination
|
|
|
|
};
|
2019-01-23 22:28:51 +01:00
|
|
|
}
|
|
|
|
|
2019-02-06 11:21:28 +01:00
|
|
|
decltype(ircd::m::request::headers_max)
|
|
|
|
ircd::m::request::headers_max
|
|
|
|
{
|
|
|
|
32UL
|
|
|
|
};
|
|
|
|
|
2019-01-23 22:28:51 +01:00
|
|
|
ircd::string_view
|
|
|
|
ircd::m::request::operator()(const mutable_buffer &out,
|
|
|
|
const vector_view<const http::header> &addl_headers)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
thread_local http::header header[headers_max];
|
2019-02-06 11:21:28 +01:00
|
|
|
const ctx::critical_assertion ca;
|
2019-01-23 22:28:51 +01:00
|
|
|
size_t headers{0};
|
|
|
|
|
|
|
|
header[headers++] =
|
|
|
|
{
|
|
|
|
"User-Agent", info::user_agent
|
|
|
|
};
|
|
|
|
|
|
|
|
thread_local char x_matrix[2_KiB];
|
|
|
|
if(startswith(at<"uri"_>(*this), "/_matrix/federation"))
|
|
|
|
{
|
|
|
|
const auto &sk{self::secret_key};
|
|
|
|
const auto &pkid{self::public_key_id};
|
|
|
|
header[headers++] =
|
|
|
|
{
|
|
|
|
"Authorization", generate(x_matrix, sk, pkid)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(headers <= headers_max);
|
|
|
|
assert(headers + addl_headers.size() <= headers_max);
|
|
|
|
for(size_t i(0); i < addl_headers.size() && headers < headers_max; ++i)
|
|
|
|
header[headers++] = addl_headers.at(i);
|
|
|
|
|
|
|
|
static const string_view content_type
|
|
|
|
{
|
|
|
|
"application/json; charset=utf-8"_sv
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto content_length
|
|
|
|
{
|
|
|
|
string_view(json::get<"content"_>(*this)).size()
|
|
|
|
};
|
|
|
|
|
|
|
|
window_buffer sb{out};
|
|
|
|
http::request
|
|
|
|
{
|
|
|
|
sb,
|
|
|
|
at<"destination"_>(*this),
|
|
|
|
at<"method"_>(*this),
|
|
|
|
at<"uri"_>(*this),
|
|
|
|
content_length,
|
|
|
|
content_type,
|
|
|
|
{ header, headers }
|
|
|
|
};
|
|
|
|
|
|
|
|
return sb.completed();
|
|
|
|
}
|
|
|
|
|
2019-02-06 11:21:28 +01:00
|
|
|
decltype(ircd::m::request::generate_content_max)
|
|
|
|
ircd::m::request::generate_content_max
|
|
|
|
{
|
|
|
|
{ "name", "ircd.m.request.generate.content_max" },
|
|
|
|
{ "default", long(1_MiB) },
|
|
|
|
};
|
|
|
|
|
2019-01-23 22:28:51 +01:00
|
|
|
ircd::string_view
|
|
|
|
ircd::m::request::generate(const mutable_buffer &out,
|
|
|
|
const ed25519::sk &sk,
|
|
|
|
const string_view &pkid)
|
|
|
|
const
|
|
|
|
{
|
2019-02-06 11:21:28 +01:00
|
|
|
const ctx::critical_assertion ca;
|
|
|
|
thread_local unique_buffer<mutable_buffer> buf
|
2019-01-23 22:28:51 +01:00
|
|
|
{
|
2019-02-06 11:21:28 +01:00
|
|
|
size_t(generate_content_max)
|
2019-01-23 22:28:51 +01:00
|
|
|
};
|
|
|
|
|
2019-02-06 11:21:28 +01:00
|
|
|
if(unlikely(json::serialized(*this) > buffer::size(buf)))
|
2019-01-23 22:28:51 +01:00
|
|
|
throw m::error
|
|
|
|
{
|
|
|
|
"M_REQUEST_TOO_LARGE", "This server generated a request of %zu bytes; limit is %zu",
|
|
|
|
json::serialized(*this),
|
2019-02-06 11:21:28 +01:00
|
|
|
buffer::size(buf)
|
2019-01-23 22:28:51 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const json::object object
|
|
|
|
{
|
|
|
|
stringify(mutable_buffer{buf}, *this)
|
|
|
|
};
|
|
|
|
|
|
|
|
const ed25519::sig sig
|
|
|
|
{
|
|
|
|
self::secret_key.sign(object)
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto &origin
|
|
|
|
{
|
|
|
|
unquote(string_view{at<"origin"_>(*this)})
|
|
|
|
};
|
|
|
|
|
|
|
|
thread_local char sigb64[1_KiB];
|
|
|
|
return fmt::sprintf
|
|
|
|
{
|
|
|
|
out, "X-Matrix origin=%s,key=\"%s\",sig=\"%s\"",
|
|
|
|
origin,
|
|
|
|
pkid,
|
|
|
|
b64encode_unpadded(sigb64, sig)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::request::verify(const string_view &key,
|
|
|
|
const string_view &sig_)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
const ed25519::sig sig
|
|
|
|
{
|
|
|
|
[&sig_](auto &buf)
|
|
|
|
{
|
|
|
|
b64decode(buf, sig_);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-05-27 02:44:12 +02:00
|
|
|
const json::string &origin
|
2019-01-23 22:28:51 +01:00
|
|
|
{
|
2019-05-27 02:44:12 +02:00
|
|
|
at<"origin"_>(*this)
|
2019-01-23 22:28:51 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const m::node node
|
|
|
|
{
|
2019-05-27 02:44:12 +02:00
|
|
|
origin
|
2019-01-23 22:28:51 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
bool verified{false};
|
|
|
|
node.key(key, [this, &verified, &sig]
|
|
|
|
(const ed25519::pk &pk)
|
|
|
|
{
|
|
|
|
verified = verify(pk, sig);
|
|
|
|
});
|
|
|
|
|
|
|
|
return verified;
|
|
|
|
}
|
|
|
|
|
2019-02-06 11:21:28 +01:00
|
|
|
decltype(ircd::m::request::verify_content_max)
|
|
|
|
ircd::m::request::verify_content_max
|
|
|
|
{
|
|
|
|
{ "name", "ircd.m.request.verify.content_max" },
|
|
|
|
{ "default", long(1_MiB) },
|
|
|
|
};
|
|
|
|
|
2019-01-23 22:28:51 +01:00
|
|
|
bool
|
|
|
|
ircd::m::request::verify(const ed25519::pk &pk,
|
|
|
|
const ed25519::sig &sig)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
// Matrix spec sez that an empty content object {} is excluded entirely
|
|
|
|
// from the verification. Our JSON only excludes members if they evaluate
|
|
|
|
// to undefined i.e json::object{}/string_view{} but not json::object{"{}"}
|
|
|
|
// or even json::object{""}; rather than burdening the caller with ensuring
|
|
|
|
// their assignment conforms perfectly, we ensure correctness manually.
|
|
|
|
auto _this(*this);
|
|
|
|
if(empty(json::get<"content"_>(*this)))
|
|
|
|
json::get<"content"_>(_this) = json::object{};
|
|
|
|
|
2019-02-06 11:21:28 +01:00
|
|
|
const ctx::critical_assertion ca;
|
|
|
|
thread_local unique_buffer<mutable_buffer> buf
|
|
|
|
{
|
|
|
|
size_t(verify_content_max)
|
|
|
|
};
|
|
|
|
|
2019-01-23 22:28:51 +01:00
|
|
|
const size_t request_size
|
|
|
|
{
|
|
|
|
json::serialized(_this)
|
|
|
|
};
|
|
|
|
|
2019-02-06 11:21:28 +01:00
|
|
|
if(unlikely(request_size > buffer::size(buf)))
|
2019-01-23 22:28:51 +01:00
|
|
|
throw m::error
|
|
|
|
{
|
|
|
|
http::PAYLOAD_TOO_LARGE, "M_REQUEST_TOO_LARGE",
|
|
|
|
"The request size %zu bytes exceeds maximum of %zu bytes",
|
|
|
|
request_size,
|
2019-02-06 11:21:28 +01:00
|
|
|
buffer::size(buf)
|
2019-01-23 22:28:51 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const json::object object
|
|
|
|
{
|
|
|
|
stringify(mutable_buffer{buf}, _this)
|
|
|
|
};
|
|
|
|
|
|
|
|
return verify(pk, sig, object);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::request::verify(const ed25519::pk &pk,
|
|
|
|
const ed25519::sig &sig,
|
|
|
|
const json::object &object)
|
|
|
|
{
|
|
|
|
return pk.verify(object, sig);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// x_matrix
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::m::request::x_matrix::x_matrix(const string_view &input)
|
|
|
|
{
|
|
|
|
string_view tokens[3];
|
|
|
|
if(ircd::tokens(split(input, ' ').second, ',', tokens) != 3)
|
|
|
|
throw std::out_of_range{"The x_matrix header is malformed"};
|
|
|
|
|
|
|
|
for(const auto &token : tokens)
|
|
|
|
{
|
|
|
|
const auto &kv{split(token, '=')};
|
|
|
|
const auto &val{unquote(kv.second)};
|
|
|
|
switch(hash(kv.first))
|
|
|
|
{
|
|
|
|
case hash("origin"): origin = val; break;
|
|
|
|
case hash("key"): key = val; break;
|
|
|
|
case hash("sig"): sig = val; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(empty(origin))
|
|
|
|
throw std::out_of_range{"The x_matrix header is missing 'origin='"};
|
|
|
|
|
|
|
|
if(empty(key))
|
|
|
|
throw std::out_of_range{"The x_matrix header is missing 'key='"};
|
|
|
|
|
|
|
|
if(empty(sig))
|
|
|
|
throw std::out_of_range{"The x_matrix header is missing 'sig='"};
|
|
|
|
}
|
|
|
|
|
2018-02-26 08:24:12 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// m/hook.h
|
|
|
|
//
|
|
|
|
|
2018-05-19 04:26:25 +02:00
|
|
|
// Internal utils
|
2018-05-14 04:38:37 +02:00
|
|
|
namespace ircd::m
|
|
|
|
{
|
2018-05-25 03:27:46 +02:00
|
|
|
static bool _hook_match(const m::event &matching, const m::event &);
|
2018-05-14 04:38:37 +02:00
|
|
|
static void _hook_fix_state_key(const json::members &, json::member &);
|
|
|
|
static void _hook_fix_room_id(const json::members &, json::member &);
|
|
|
|
static void _hook_fix_sender(const json::members &, json::member &);
|
|
|
|
static json::strung _hook_make_feature(const json::members &);
|
|
|
|
}
|
|
|
|
|
2019-06-23 08:28:48 +02:00
|
|
|
/// Instance list linkage for all hook sites
|
|
|
|
template<>
|
|
|
|
decltype(ircd::util::instance_list<ircd::m::hook::base::site>::allocator)
|
|
|
|
ircd::util::instance_list<ircd::m::hook::base::site>::allocator
|
|
|
|
{};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
decltype(ircd::util::instance_list<ircd::m::hook::base::site>::list)
|
|
|
|
ircd::util::instance_list<ircd::m::hook::base::site>::list
|
|
|
|
{
|
|
|
|
allocator
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Instance list linkage for all hooks
|
|
|
|
template<>
|
|
|
|
decltype(ircd::util::instance_list<ircd::m::hook::base>::allocator)
|
|
|
|
ircd::util::instance_list<ircd::m::hook::base>::allocator
|
|
|
|
{};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
decltype(ircd::util::instance_list<ircd::m::hook::base>::list)
|
|
|
|
ircd::util::instance_list<ircd::m::hook::base>::list
|
|
|
|
{
|
|
|
|
allocator
|
|
|
|
};
|
|
|
|
|
2018-05-27 07:05:56 +02:00
|
|
|
//
|
|
|
|
// hook::maps
|
|
|
|
//
|
|
|
|
|
|
|
|
struct ircd::m::hook::maps
|
|
|
|
{
|
|
|
|
std::multimap<string_view, base *> origin;
|
|
|
|
std::multimap<string_view, base *> room_id;
|
|
|
|
std::multimap<string_view, base *> sender;
|
|
|
|
std::multimap<string_view, base *> state_key;
|
|
|
|
std::multimap<string_view, base *> type;
|
|
|
|
std::vector<base *> always;
|
|
|
|
|
|
|
|
size_t match(const event &match, const std::function<bool (base &)> &) const;
|
|
|
|
size_t add(base &hook, const event &matching);
|
|
|
|
size_t del(base &hook, const event &matching);
|
|
|
|
|
|
|
|
maps();
|
|
|
|
~maps() noexcept;
|
|
|
|
};
|
2018-05-07 06:53:23 +02:00
|
|
|
|
2018-05-27 07:05:56 +02:00
|
|
|
ircd::m::hook::maps::maps()
|
2018-03-03 06:12:11 +01:00
|
|
|
{
|
|
|
|
}
|
2018-05-27 07:05:56 +02:00
|
|
|
|
|
|
|
ircd::m::hook::maps::~maps()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::m::hook::maps::add(base &hook,
|
|
|
|
const event &matching)
|
2018-03-03 06:12:11 +01:00
|
|
|
{
|
2018-05-27 07:05:56 +02:00
|
|
|
size_t ret{0};
|
|
|
|
const auto map{[&hook, &ret]
|
|
|
|
(auto &map, const string_view &value)
|
|
|
|
{
|
|
|
|
map.emplace(value, &hook);
|
|
|
|
++ret;
|
|
|
|
}};
|
|
|
|
|
|
|
|
if(json::get<"origin"_>(matching))
|
|
|
|
map(origin, at<"origin"_>(matching));
|
|
|
|
|
|
|
|
if(json::get<"room_id"_>(matching))
|
|
|
|
map(room_id, at<"room_id"_>(matching));
|
|
|
|
|
|
|
|
if(json::get<"sender"_>(matching))
|
|
|
|
map(sender, at<"sender"_>(matching));
|
|
|
|
|
|
|
|
if(json::get<"state_key"_>(matching))
|
|
|
|
map(state_key, at<"state_key"_>(matching));
|
|
|
|
|
|
|
|
if(json::get<"type"_>(matching))
|
|
|
|
map(type, at<"type"_>(matching));
|
|
|
|
|
|
|
|
// Hook had no mappings which means it will match everything.
|
|
|
|
// We don't increment the matcher count for this case.
|
|
|
|
if(!ret)
|
|
|
|
always.emplace_back(&hook);
|
|
|
|
|
|
|
|
return ret;
|
2018-03-03 06:12:11 +01:00
|
|
|
}
|
|
|
|
|
2018-05-27 07:05:56 +02:00
|
|
|
size_t
|
|
|
|
ircd::m::hook::maps::del(base &hook,
|
|
|
|
const event &matching)
|
|
|
|
{
|
|
|
|
size_t ret{0};
|
|
|
|
const auto unmap{[&hook, &ret]
|
2018-10-08 03:55:29 +02:00
|
|
|
(auto &map, const string_view &value)
|
2018-05-27 07:05:56 +02:00
|
|
|
{
|
2018-10-08 03:55:29 +02:00
|
|
|
auto pit{map.equal_range(value)};
|
|
|
|
while(pit.first != pit.second)
|
2018-05-27 07:05:56 +02:00
|
|
|
if(pit.first->second == &hook)
|
|
|
|
{
|
2018-10-08 03:55:29 +02:00
|
|
|
pit.first = map.erase(pit.first);
|
2018-05-27 07:05:56 +02:00
|
|
|
++ret;
|
|
|
|
}
|
2018-10-08 03:55:29 +02:00
|
|
|
else ++pit.first;
|
2018-05-27 07:05:56 +02:00
|
|
|
}};
|
|
|
|
|
|
|
|
// Unconditional attempt to remove from always.
|
|
|
|
std::remove(begin(always), end(always), &hook);
|
|
|
|
|
|
|
|
if(json::get<"origin"_>(matching))
|
|
|
|
unmap(origin, at<"origin"_>(matching));
|
|
|
|
|
|
|
|
if(json::get<"room_id"_>(matching))
|
|
|
|
unmap(room_id, at<"room_id"_>(matching));
|
|
|
|
|
|
|
|
if(json::get<"sender"_>(matching))
|
|
|
|
unmap(sender, at<"sender"_>(matching));
|
|
|
|
|
|
|
|
if(json::get<"state_key"_>(matching))
|
|
|
|
unmap(state_key, at<"state_key"_>(matching));
|
|
|
|
|
|
|
|
if(json::get<"type"_>(matching))
|
|
|
|
unmap(type, at<"type"_>(matching));
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::m::hook::maps::match(const event &event,
|
|
|
|
const std::function<bool (base &)> &callback)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
std::set<base *> matching
|
|
|
|
{
|
|
|
|
begin(always), end(always)
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto site_match{[&matching]
|
|
|
|
(auto &map, const string_view &key)
|
|
|
|
{
|
|
|
|
auto pit{map.equal_range(key)};
|
|
|
|
for(; pit.first != pit.second; ++pit.first)
|
|
|
|
matching.emplace(pit.first->second);
|
|
|
|
}};
|
|
|
|
|
|
|
|
if(json::get<"origin"_>(event))
|
|
|
|
site_match(origin, at<"origin"_>(event));
|
|
|
|
|
|
|
|
if(json::get<"room_id"_>(event))
|
|
|
|
site_match(room_id, at<"room_id"_>(event));
|
|
|
|
|
|
|
|
if(json::get<"sender"_>(event))
|
|
|
|
site_match(sender, at<"sender"_>(event));
|
|
|
|
|
|
|
|
if(json::get<"type"_>(event))
|
|
|
|
site_match(type, at<"type"_>(event));
|
|
|
|
|
|
|
|
if(json::get<"state_key"_>(event))
|
|
|
|
site_match(state_key, at<"state_key"_>(event));
|
|
|
|
|
|
|
|
auto it(begin(matching));
|
|
|
|
while(it != end(matching))
|
|
|
|
{
|
|
|
|
const base &hook(**it);
|
|
|
|
if(!_hook_match(hook.matching, event))
|
|
|
|
it = matching.erase(it);
|
|
|
|
else
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t ret{0};
|
|
|
|
for(auto it(begin(matching)); it != end(matching); ++it, ++ret)
|
|
|
|
if(!callback(**it))
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// hook::base
|
|
|
|
//
|
|
|
|
|
2018-03-03 06:12:11 +01:00
|
|
|
/// Primary hook ctor
|
2018-05-27 07:05:56 +02:00
|
|
|
ircd::m::hook::base::base(const json::members &members)
|
2018-05-14 04:38:37 +02:00
|
|
|
:_feature
|
|
|
|
{
|
|
|
|
_hook_make_feature(members)
|
|
|
|
}
|
|
|
|
,feature
|
|
|
|
{
|
|
|
|
_feature
|
|
|
|
}
|
|
|
|
,matching
|
|
|
|
{
|
|
|
|
feature
|
|
|
|
}
|
|
|
|
{
|
2019-06-24 07:17:49 +02:00
|
|
|
site *site; try
|
|
|
|
{
|
|
|
|
if((site = find_site()))
|
|
|
|
site->add(*this);
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
if(registered)
|
|
|
|
{
|
|
|
|
auto *const site(find_site());
|
|
|
|
assert(site != nullptr);
|
|
|
|
site->del(*this);
|
|
|
|
}
|
|
|
|
}
|
2018-05-14 04:38:37 +02:00
|
|
|
}
|
|
|
|
|
2018-05-27 07:05:56 +02:00
|
|
|
ircd::m::hook::base::~base()
|
2018-02-26 08:24:12 +01:00
|
|
|
noexcept
|
|
|
|
{
|
2018-05-14 04:38:37 +02:00
|
|
|
if(!registered)
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto *const site(find_site());
|
|
|
|
assert(site != nullptr);
|
2019-04-19 06:42:47 +02:00
|
|
|
assert(site->calling == 0);
|
|
|
|
assert(calling == 0);
|
2018-05-14 04:38:37 +02:00
|
|
|
site->del(*this);
|
2018-05-07 06:53:23 +02:00
|
|
|
}
|
|
|
|
|
2018-05-27 07:05:56 +02:00
|
|
|
ircd::m::hook::base::site *
|
|
|
|
ircd::m::hook::base::find_site()
|
2018-05-07 06:53:23 +02:00
|
|
|
const
|
|
|
|
{
|
|
|
|
const auto &site_name
|
|
|
|
{
|
|
|
|
this->site_name()
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!site_name)
|
|
|
|
return nullptr;
|
|
|
|
|
2018-05-27 07:05:56 +02:00
|
|
|
for(auto *const &site : m::hook::base::site::list)
|
2018-05-07 06:53:23 +02:00
|
|
|
if(site->name() == site_name)
|
|
|
|
return site;
|
|
|
|
|
|
|
|
return nullptr;
|
2018-02-26 08:24:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
2018-05-27 07:05:56 +02:00
|
|
|
ircd::m::hook::base::site_name()
|
2018-02-26 08:24:12 +01:00
|
|
|
const try
|
|
|
|
{
|
|
|
|
return unquote(feature.at("_site"));
|
|
|
|
}
|
|
|
|
catch(const std::out_of_range &e)
|
|
|
|
{
|
2019-01-14 00:50:04 +01:00
|
|
|
throw panic
|
2018-02-26 08:24:12 +01:00
|
|
|
{
|
|
|
|
"Hook %p must name a '_site' to register with.", this
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// hook::site
|
|
|
|
//
|
|
|
|
|
2018-05-25 03:27:46 +02:00
|
|
|
//
|
|
|
|
// hook::site::site
|
|
|
|
//
|
|
|
|
|
2018-05-27 07:05:56 +02:00
|
|
|
ircd::m::hook::base::site::site(const json::members &members)
|
2018-02-26 08:24:12 +01:00
|
|
|
:_feature
|
|
|
|
{
|
|
|
|
members
|
|
|
|
}
|
|
|
|
,feature
|
|
|
|
{
|
|
|
|
_feature
|
|
|
|
}
|
2018-05-25 03:27:46 +02:00
|
|
|
,maps
|
|
|
|
{
|
2018-05-27 07:05:56 +02:00
|
|
|
std::make_unique<struct maps>()
|
2018-05-25 03:27:46 +02:00
|
|
|
}
|
2018-09-18 05:45:05 +02:00
|
|
|
,exceptions
|
|
|
|
{
|
|
|
|
feature.get<bool>("exceptions", true)
|
|
|
|
}
|
2018-02-26 08:24:12 +01:00
|
|
|
{
|
2018-05-07 06:53:23 +02:00
|
|
|
for(const auto &site : list)
|
|
|
|
if(site->name() == name() && site != this)
|
|
|
|
throw error
|
|
|
|
{
|
|
|
|
"Hook site '%s' already registered at %p",
|
|
|
|
name(),
|
|
|
|
site
|
|
|
|
};
|
|
|
|
|
2018-05-06 23:53:00 +02:00
|
|
|
// Find and register all of the orphan hooks which were constructed before
|
|
|
|
// this site was constructed.
|
2018-05-27 07:05:56 +02:00
|
|
|
for(auto *const &hook : m::hook::base::list)
|
2019-02-06 07:55:45 +01:00
|
|
|
if(hook->site_name() == name())
|
2018-05-06 23:53:00 +02:00
|
|
|
add(*hook);
|
2018-02-26 08:24:12 +01:00
|
|
|
}
|
|
|
|
|
2018-05-27 07:05:56 +02:00
|
|
|
ircd::m::hook::base::site::~site()
|
2018-02-26 08:24:12 +01:00
|
|
|
noexcept
|
|
|
|
{
|
2018-05-27 07:05:56 +02:00
|
|
|
const std::vector<base *> hooks
|
2018-05-20 02:43:02 +02:00
|
|
|
{
|
|
|
|
begin(this->hooks), end(this->hooks)
|
|
|
|
};
|
|
|
|
|
2018-05-07 06:53:23 +02:00
|
|
|
for(auto *const hook : hooks)
|
|
|
|
del(*hook);
|
2018-02-26 08:24:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-05-27 07:05:56 +02:00
|
|
|
ircd::m::hook::base::site::match(const event &event,
|
|
|
|
const std::function<bool (base &)> &callback)
|
2018-02-26 08:24:12 +01:00
|
|
|
{
|
2018-05-27 07:05:56 +02:00
|
|
|
maps->match(event, callback);
|
2018-02-26 08:24:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-05-27 07:05:56 +02:00
|
|
|
ircd::m::hook::base::site::add(base &hook)
|
2018-02-26 08:24:12 +01:00
|
|
|
{
|
2018-05-07 06:53:23 +02:00
|
|
|
assert(!hook.registered);
|
|
|
|
assert(hook.site_name() == name());
|
2018-05-13 04:52:25 +02:00
|
|
|
assert(hook.matchers == 0);
|
2018-05-07 06:53:23 +02:00
|
|
|
|
2018-04-27 00:17:28 +02:00
|
|
|
if(!hooks.emplace(&hook).second)
|
|
|
|
{
|
|
|
|
log::warning
|
|
|
|
{
|
2019-07-26 21:17:47 +02:00
|
|
|
log, "Hook %p already registered to site %s",
|
|
|
|
&hook,
|
|
|
|
name()
|
2018-04-27 00:17:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-05-27 07:05:56 +02:00
|
|
|
assert(maps);
|
|
|
|
const size_t matched
|
2018-05-13 04:52:25 +02:00
|
|
|
{
|
2018-05-27 07:05:56 +02:00
|
|
|
maps->add(hook, hook.matching)
|
|
|
|
};
|
2018-05-13 04:58:08 +02:00
|
|
|
|
2018-05-27 07:05:56 +02:00
|
|
|
hook.matchers = matched;
|
2018-05-07 06:53:23 +02:00
|
|
|
hook.registered = true;
|
2018-05-27 07:05:56 +02:00
|
|
|
matchers += matched;
|
|
|
|
++count;
|
2019-05-02 11:25:37 +02:00
|
|
|
|
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
log, "Registered hook %p to site %s",
|
|
|
|
&hook,
|
|
|
|
name()
|
|
|
|
};
|
|
|
|
|
2018-02-26 08:24:12 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-05-27 07:05:56 +02:00
|
|
|
ircd::m::hook::base::site::del(base &hook)
|
2018-02-26 08:24:12 +01:00
|
|
|
{
|
2019-05-02 11:25:37 +02:00
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
log, "Removing hook %p from site %s",
|
|
|
|
&hook,
|
|
|
|
name()
|
|
|
|
};
|
|
|
|
|
2018-05-07 06:53:23 +02:00
|
|
|
assert(hook.registered);
|
|
|
|
assert(hook.site_name() == name());
|
|
|
|
|
2018-05-27 07:05:56 +02:00
|
|
|
const size_t matched
|
2018-02-26 08:24:12 +01:00
|
|
|
{
|
2018-05-27 07:05:56 +02:00
|
|
|
maps->del(hook, hook.matching)
|
|
|
|
};
|
2018-02-26 08:24:12 +01:00
|
|
|
|
2018-04-27 00:17:28 +02:00
|
|
|
const auto erased
|
|
|
|
{
|
|
|
|
hooks.erase(&hook)
|
|
|
|
};
|
|
|
|
|
2018-05-27 07:05:56 +02:00
|
|
|
hook.matchers -= matched;
|
2018-05-07 06:53:23 +02:00
|
|
|
hook.registered = false;
|
2018-05-27 07:05:56 +02:00
|
|
|
matchers -= matched;
|
|
|
|
--count;
|
|
|
|
assert(hook.matchers == 0);
|
|
|
|
assert(erased);
|
2018-02-26 08:24:12 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
2018-05-27 07:05:56 +02:00
|
|
|
ircd::m::hook::base::site::name()
|
2018-02-26 08:24:12 +01:00
|
|
|
const try
|
|
|
|
{
|
|
|
|
return unquote(feature.at("name"));
|
|
|
|
}
|
|
|
|
catch(const std::out_of_range &e)
|
|
|
|
{
|
2019-01-14 00:50:04 +01:00
|
|
|
throw panic
|
2018-02-26 08:24:12 +01:00
|
|
|
{
|
|
|
|
"Hook site %p requires a name", this
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-05-27 07:05:56 +02:00
|
|
|
//
|
|
|
|
// hook<void>
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::m::hook::hook<void>::hook(const json::members &feature,
|
|
|
|
decltype(function) function)
|
|
|
|
:base{feature}
|
|
|
|
,function{std::move(function)}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::hook::hook<void>::hook(decltype(function) function,
|
|
|
|
const json::members &feature)
|
|
|
|
:base{feature}
|
|
|
|
,function{std::move(function)}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::hook::site<void>::site(const json::members &feature)
|
|
|
|
:base::site{feature}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::hook::site<void>::operator()(const event &event)
|
|
|
|
{
|
|
|
|
match(event, [this, &event]
|
|
|
|
(base &base)
|
|
|
|
{
|
|
|
|
call(dynamic_cast<hook<void> &>(base), event);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::hook::site<void>::call(hook<void> &hfn,
|
|
|
|
const event &event)
|
|
|
|
try
|
|
|
|
{
|
2019-04-19 06:42:47 +02:00
|
|
|
// stats for site
|
|
|
|
++calls;
|
2019-07-26 21:17:47 +02:00
|
|
|
const scope_count site_calling
|
|
|
|
{
|
|
|
|
calling
|
|
|
|
};
|
2019-04-19 06:42:47 +02:00
|
|
|
|
|
|
|
// stats for hook
|
2018-05-27 07:05:56 +02:00
|
|
|
++hfn.calls;
|
2019-07-26 21:17:47 +02:00
|
|
|
const scope_count hook_calling
|
|
|
|
{
|
|
|
|
hfn.calling
|
|
|
|
};
|
2019-04-19 06:42:47 +02:00
|
|
|
|
|
|
|
// call hook
|
2018-05-27 07:05:56 +02:00
|
|
|
hfn.function(event);
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2018-09-18 05:45:05 +02:00
|
|
|
if(exceptions)
|
|
|
|
throw;
|
|
|
|
|
2018-05-27 07:05:56 +02:00
|
|
|
log::critical
|
|
|
|
{
|
2019-07-26 21:17:47 +02:00
|
|
|
log, "Unhandled hookfn(%p) %s error :%s",
|
2018-05-27 07:05:56 +02:00
|
|
|
&hfn,
|
|
|
|
string_view{hfn.feature},
|
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-05-25 03:27:46 +02:00
|
|
|
//
|
|
|
|
// hook internal
|
|
|
|
//
|
|
|
|
|
|
|
|
/// Internal interface which manipulates the initializer supplied by the
|
|
|
|
/// developer to the hook to create the proper JSON output. i.e They supply
|
|
|
|
/// a "room_id" of "!config" which has no hostname, that is added here
|
|
|
|
/// depending on my_host() in the deployment runtime...
|
|
|
|
///
|
|
|
|
ircd::json::strung
|
|
|
|
ircd::m::_hook_make_feature(const json::members &members)
|
|
|
|
{
|
|
|
|
const ctx::critical_assertion ca;
|
|
|
|
std::vector<json::member> copy
|
|
|
|
{
|
|
|
|
begin(members), end(members)
|
|
|
|
};
|
|
|
|
|
|
|
|
for(auto &member : copy) switch(hash(member.first))
|
|
|
|
{
|
|
|
|
case hash("room_id"):
|
|
|
|
_hook_fix_room_id(members, member);
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case hash("sender"):
|
|
|
|
_hook_fix_sender(members, member);
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case hash("state_key"):
|
|
|
|
_hook_fix_state_key(members, member);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return { copy.data(), copy.data() + copy.size() };
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::_hook_fix_sender(const json::members &members,
|
|
|
|
json::member &member)
|
|
|
|
{
|
|
|
|
// Rewrite the sender if the supplied input has no hostname
|
|
|
|
if(valid_local_only(id::USER, member.second))
|
|
|
|
{
|
|
|
|
assert(my_host());
|
|
|
|
thread_local char buf[256];
|
|
|
|
member.second = id::user { buf, member.second, my_host() };
|
|
|
|
}
|
|
|
|
|
|
|
|
validate(id::USER, member.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::_hook_fix_room_id(const json::members &members,
|
|
|
|
json::member &member)
|
|
|
|
{
|
|
|
|
// Rewrite the room_id if the supplied input has no hostname
|
|
|
|
if(valid_local_only(id::ROOM, member.second))
|
|
|
|
{
|
|
|
|
assert(my_host());
|
|
|
|
thread_local char buf[256];
|
|
|
|
member.second = id::room { buf, member.second, my_host() };
|
|
|
|
}
|
|
|
|
|
|
|
|
validate(id::ROOM, member.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::_hook_fix_state_key(const json::members &members,
|
|
|
|
json::member &member)
|
|
|
|
{
|
|
|
|
const bool is_member_event
|
|
|
|
{
|
|
|
|
end(members) != std::find_if(begin(members), end(members), []
|
|
|
|
(const auto &member)
|
|
|
|
{
|
|
|
|
return member.first == "type" && member.second == "m.room.member";
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
// Rewrite the sender if the supplied input has no hostname
|
|
|
|
if(valid_local_only(id::USER, member.second))
|
|
|
|
{
|
|
|
|
assert(my_host());
|
|
|
|
thread_local char buf[256];
|
|
|
|
member.second = id::user { buf, member.second, my_host() };
|
|
|
|
}
|
|
|
|
|
|
|
|
validate(id::USER, member.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::_hook_match(const m::event &matching,
|
|
|
|
const m::event &event)
|
|
|
|
{
|
|
|
|
if(json::get<"origin"_>(matching))
|
|
|
|
if(at<"origin"_>(matching) != json::get<"origin"_>(event))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(json::get<"room_id"_>(matching))
|
|
|
|
if(at<"room_id"_>(matching) != json::get<"room_id"_>(event))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(json::get<"sender"_>(matching))
|
|
|
|
if(at<"sender"_>(matching) != json::get<"sender"_>(event))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(json::get<"type"_>(matching))
|
|
|
|
if(at<"type"_>(matching) != json::get<"type"_>(event))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(json::get<"state_key"_>(matching))
|
|
|
|
if(at<"state_key"_>(matching) != json::get<"state_key"_>(event))
|
|
|
|
return false;
|
|
|
|
|
2018-10-25 23:51:14 +02:00
|
|
|
if(membership(matching))
|
|
|
|
if(membership(matching) != membership(event))
|
2018-05-25 03:27:46 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if(json::get<"content"_>(matching))
|
|
|
|
if(json::get<"type"_>(event) == "m.room.message")
|
|
|
|
if(at<"content"_>(matching).has("msgtype"))
|
|
|
|
if(at<"content"_>(matching).get("msgtype") != json::get<"content"_>(event).get("msgtype"))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-03-04 18:20:24 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// m/error.h
|
|
|
|
//
|
|
|
|
|
2018-12-31 02:34:27 +01:00
|
|
|
namespace ircd::m
|
|
|
|
{
|
|
|
|
const std::array<http::header, 1> _error_headers
|
|
|
|
{{
|
|
|
|
{ "Content-Type", "application/json; charset=utf-8" },
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
2018-12-20 00:57:04 +01:00
|
|
|
thread_local
|
|
|
|
decltype(ircd::m::error::fmtbuf)
|
|
|
|
ircd::m::error::fmtbuf
|
2018-03-04 18:20:24 +01:00
|
|
|
{};
|
2018-03-05 09:58:10 +01:00
|
|
|
|
2019-04-26 08:35:46 +02:00
|
|
|
//
|
|
|
|
// error::error
|
|
|
|
//
|
|
|
|
|
2018-03-05 10:34:03 +01:00
|
|
|
ircd::m::error::error()
|
2019-07-12 02:22:16 +02:00
|
|
|
:error
|
2018-12-31 02:34:27 +01:00
|
|
|
{
|
2019-07-12 02:22:16 +02:00
|
|
|
internal, http::INTERNAL_SERVER_ERROR, std::string{},
|
2018-12-31 02:34:27 +01:00
|
|
|
}
|
2018-03-05 10:34:03 +01:00
|
|
|
{}
|
|
|
|
|
2018-03-05 09:58:10 +01:00
|
|
|
ircd::m::error::error(std::string c)
|
2019-07-12 02:22:16 +02:00
|
|
|
:error
|
2018-12-31 02:34:27 +01:00
|
|
|
{
|
2019-07-12 02:22:16 +02:00
|
|
|
internal, http::INTERNAL_SERVER_ERROR, std::move(c),
|
2018-12-31 02:34:27 +01:00
|
|
|
}
|
2018-03-05 09:58:10 +01:00
|
|
|
{}
|
|
|
|
|
|
|
|
ircd::m::error::error(const http::code &c)
|
2019-07-12 02:22:16 +02:00
|
|
|
:error
|
2018-12-31 02:34:27 +01:00
|
|
|
{
|
2019-07-12 02:22:16 +02:00
|
|
|
internal, c, std::string{},
|
2018-12-31 02:34:27 +01:00
|
|
|
}
|
2018-03-05 09:58:10 +01:00
|
|
|
{}
|
|
|
|
|
|
|
|
ircd::m::error::error(const http::code &c,
|
|
|
|
const json::members &members)
|
2018-12-31 02:34:27 +01:00
|
|
|
:error
|
|
|
|
{
|
2019-07-12 02:22:16 +02:00
|
|
|
internal, c, json::strung{members},
|
2018-12-31 02:34:27 +01:00
|
|
|
}
|
2018-03-05 09:58:10 +01:00
|
|
|
{}
|
|
|
|
|
|
|
|
ircd::m::error::error(const http::code &c,
|
|
|
|
const json::iov &iov)
|
2018-12-31 02:34:27 +01:00
|
|
|
:error
|
|
|
|
{
|
2019-07-12 02:22:16 +02:00
|
|
|
internal, c, json::strung{iov},
|
2018-12-31 02:34:27 +01:00
|
|
|
}
|
2018-03-05 09:58:10 +01:00
|
|
|
{}
|
|
|
|
|
|
|
|
ircd::m::error::error(const http::code &c,
|
|
|
|
const json::object &object)
|
2019-07-12 02:22:16 +02:00
|
|
|
:error
|
2018-12-31 02:34:27 +01:00
|
|
|
{
|
2019-07-12 02:22:16 +02:00
|
|
|
internal, c, json::strung{object},
|
2018-12-31 02:34:27 +01:00
|
|
|
}
|
|
|
|
{}
|
|
|
|
|
|
|
|
ircd::m::error::error(internal_t,
|
|
|
|
const http::code &c,
|
2019-07-12 02:22:16 +02:00
|
|
|
std::string object)
|
2018-12-31 02:34:27 +01:00
|
|
|
:http::error
|
|
|
|
{
|
2019-07-12 02:22:16 +02:00
|
|
|
c, std::move(object), vector_view<const http::header>{_error_headers}
|
2018-12-31 02:34:27 +01:00
|
|
|
}
|
2019-07-12 02:25:23 +02:00
|
|
|
{
|
2019-07-18 22:39:10 +02:00
|
|
|
if(!content.empty())
|
2019-07-12 02:25:23 +02:00
|
|
|
{
|
2019-07-18 22:39:10 +02:00
|
|
|
strlcat(ircd::exception::buf, " ");
|
|
|
|
strlcat(ircd::exception::buf, errcode());
|
2019-07-12 02:25:23 +02:00
|
|
|
strlcat(ircd::exception::buf, " :");
|
2019-07-18 22:39:10 +02:00
|
|
|
strlcat(ircd::exception::buf, errstr());
|
2019-07-12 02:25:23 +02:00
|
|
|
}
|
|
|
|
}
|
2019-04-26 08:35:46 +02:00
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::m::error::errstr()
|
|
|
|
const noexcept
|
|
|
|
{
|
|
|
|
const json::object &content
|
|
|
|
{
|
|
|
|
this->http::error::content
|
|
|
|
};
|
|
|
|
|
2019-04-28 03:26:14 +02:00
|
|
|
return unquote(content.get("error"));
|
2019-04-26 08:35:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::m::error::errcode()
|
|
|
|
const noexcept
|
|
|
|
{
|
|
|
|
const json::object &content
|
|
|
|
{
|
|
|
|
this->http::error::content
|
|
|
|
};
|
|
|
|
|
2019-04-28 03:26:14 +02:00
|
|
|
return unquote(content.get("errcode", "M_UNKNOWN"_sv));
|
2019-04-26 08:35:46 +02:00
|
|
|
}
|