2018-03-05 11:44:03 +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.
|
|
|
|
|
|
|
|
using namespace ircd;
|
|
|
|
|
2018-11-02 12:49:56 +01:00
|
|
|
extern "C" void default_conf(const string_view &prefix);
|
2018-11-02 12:48:57 +01:00
|
|
|
extern "C" void rehash_conf(const string_view &prefix, const bool &existing);
|
2018-09-03 06:13:03 +02:00
|
|
|
extern "C" void reload_conf();
|
|
|
|
extern "C" void refresh_conf();
|
2018-09-03 13:40:28 +02:00
|
|
|
static void init_conf_item(conf::item<> &);
|
2018-05-26 05:37:33 +02:00
|
|
|
|
2018-09-15 09:37:16 +02:00
|
|
|
// This module registers with conf::on_init to be called back
|
|
|
|
// when a conf item is initialized; when this module is unloaded
|
|
|
|
// we have to unregister that listener using this state.
|
|
|
|
decltype(conf::on_init)::const_iterator
|
|
|
|
conf_on_init_iter
|
|
|
|
{
|
|
|
|
end(conf::on_init)
|
|
|
|
};
|
|
|
|
|
2018-03-05 11:44:03 +01:00
|
|
|
mapi::header
|
|
|
|
IRCD_MODULE
|
|
|
|
{
|
2018-05-26 05:37:33 +02:00
|
|
|
"Server Configuration", []
|
|
|
|
{
|
2018-09-15 09:37:16 +02:00
|
|
|
conf_on_init_iter = conf::on_init.emplace(end(conf::on_init), init_conf_item);
|
2018-09-03 06:13:03 +02:00
|
|
|
reload_conf();
|
2018-09-03 13:40:28 +02:00
|
|
|
}, []
|
|
|
|
{
|
2018-09-15 09:37:16 +02:00
|
|
|
assert(conf_on_init_iter != end(conf::on_init));
|
|
|
|
conf::on_init.erase(conf_on_init_iter);
|
2018-05-26 05:37:33 +02:00
|
|
|
}
|
2018-03-05 11:44:03 +01:00
|
|
|
};
|
|
|
|
|
2018-09-03 06:52:14 +02:00
|
|
|
/// Set to false to quiet errors from a conf item failing to set
|
|
|
|
bool
|
|
|
|
item_error_log
|
|
|
|
{
|
|
|
|
true
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
on_run()
|
|
|
|
{
|
|
|
|
// Suppress errors for this scope.
|
|
|
|
const unwind uw{[] { item_error_log = true; }};
|
|
|
|
item_error_log = false;
|
2018-11-02 12:48:57 +01:00
|
|
|
rehash_conf({}, false);
|
2018-09-03 06:52:14 +02:00
|
|
|
}
|
|
|
|
|
2018-09-03 06:33:00 +02:00
|
|
|
/// Waits for the daemon to transition to the RUN state so we can gather all
|
|
|
|
/// of the registered conf items and save any new ones to the !conf room.
|
|
|
|
/// We can't do that on this module init for two reason:
|
|
|
|
/// - More conf items will load in other modules after this module.
|
|
|
|
/// - Events can't be safely sent to the !conf room until the RUN state.
|
2019-01-18 17:55:06 +01:00
|
|
|
const ircd::run::changed
|
2018-09-03 06:33:00 +02:00
|
|
|
rehash_on_run{[]
|
2019-01-18 17:55:06 +01:00
|
|
|
(const auto &level)
|
2018-09-03 06:33:00 +02:00
|
|
|
{
|
2019-01-18 17:55:06 +01:00
|
|
|
if(level == ircd::run::level::RUN)
|
2018-09-03 06:52:14 +02:00
|
|
|
ctx::context
|
|
|
|
{
|
|
|
|
"confhash", 256_KiB, on_run, ctx::context::POST
|
|
|
|
};
|
2018-09-03 06:33:00 +02:00
|
|
|
}};
|
|
|
|
|
2018-03-22 22:43:18 +01:00
|
|
|
const m::room::id::buf
|
2018-03-05 11:44:03 +01:00
|
|
|
conf_room_id
|
|
|
|
{
|
|
|
|
"conf", ircd::my_host()
|
|
|
|
};
|
|
|
|
|
2018-12-29 03:45:26 +01:00
|
|
|
const m::room
|
2018-03-05 11:44:03 +01:00
|
|
|
conf_room
|
|
|
|
{
|
|
|
|
conf_room_id
|
|
|
|
};
|
|
|
|
|
2018-03-22 22:43:18 +01:00
|
|
|
extern "C" m::event::id::buf
|
|
|
|
set_conf_item(const m::user::id &sender,
|
|
|
|
const string_view &key,
|
|
|
|
const string_view &val)
|
|
|
|
{
|
2019-04-17 13:59:00 +02:00
|
|
|
if(conf::exists(key) && !conf::persists(key))
|
|
|
|
{
|
|
|
|
conf::set(key, val);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2018-03-22 22:43:18 +01:00
|
|
|
return send(conf_room, sender, "ircd.conf.item", key,
|
|
|
|
{
|
|
|
|
{ "value", val }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void
|
|
|
|
get_conf_item(const string_view &key,
|
|
|
|
const std::function<void (const string_view &)> &closure)
|
|
|
|
{
|
2018-12-29 03:45:26 +01:00
|
|
|
static const m::event::fetch::opts fopts
|
|
|
|
{
|
|
|
|
m::event::keys::include
|
|
|
|
{
|
|
|
|
"content"
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::room::state state
|
|
|
|
{
|
|
|
|
conf_room, &fopts
|
|
|
|
};
|
|
|
|
|
|
|
|
state.get("ircd.conf.item", key, [&closure]
|
2018-03-22 22:43:18 +01:00
|
|
|
(const m::event &event)
|
|
|
|
{
|
|
|
|
const auto &value
|
|
|
|
{
|
|
|
|
unquote(at<"content"_>(event).at("value"))
|
|
|
|
};
|
|
|
|
|
|
|
|
closure(value);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-05 11:44:03 +01:00
|
|
|
static void
|
2018-05-26 05:37:33 +02:00
|
|
|
conf_updated(const m::event &event)
|
2018-04-22 07:54:37 +02:00
|
|
|
noexcept try
|
2018-03-05 11:44:03 +01:00
|
|
|
{
|
|
|
|
const auto &content
|
|
|
|
{
|
|
|
|
at<"content"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto &key
|
|
|
|
{
|
|
|
|
at<"state_key"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
const string_view &value
|
|
|
|
{
|
|
|
|
unquote(content.at("value"))
|
|
|
|
};
|
|
|
|
|
2019-01-18 17:55:06 +01:00
|
|
|
if(run::level == run::level::START && !conf::exists(key))
|
2018-09-30 04:57:14 +02:00
|
|
|
return;
|
|
|
|
|
2018-12-09 01:15:22 +01:00
|
|
|
// Conf items marked with a persist=false property are not read from
|
|
|
|
// the conf room into the item, even if the value exists in the room.
|
|
|
|
if(conf::exists(key) && !conf::persists(key))
|
|
|
|
return;
|
|
|
|
|
2018-03-06 20:57:57 +01:00
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
"Updating conf [%s] => %s", key, value
|
|
|
|
};
|
|
|
|
|
2018-03-05 11:44:03 +01:00
|
|
|
ircd::conf::set(key, value);
|
|
|
|
}
|
2018-04-22 07:54:37 +02:00
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2018-09-03 06:52:14 +02:00
|
|
|
if(item_error_log) log::error
|
2018-04-22 07:54:37 +02:00
|
|
|
{
|
|
|
|
"Failed to set conf item '%s' :%s",
|
|
|
|
json::get<"state_key"_>(event),
|
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
}
|
2018-03-05 11:44:03 +01:00
|
|
|
|
2018-10-07 07:17:46 +02:00
|
|
|
static void
|
|
|
|
handle_conf_updated(const m::event &event,
|
|
|
|
m::vm::eval &)
|
|
|
|
{
|
|
|
|
conf_updated(event);
|
|
|
|
}
|
|
|
|
|
2019-08-10 06:27:12 +02:00
|
|
|
m::hookfn<m::vm::eval &>
|
2018-05-26 05:37:33 +02:00
|
|
|
conf_updated_hook
|
2018-03-05 11:44:03 +01:00
|
|
|
{
|
2018-10-07 07:17:46 +02:00
|
|
|
handle_conf_updated,
|
2018-03-05 11:44:03 +01:00
|
|
|
{
|
2018-10-07 07:17:46 +02:00
|
|
|
{ "_site", "vm.effect" },
|
2018-03-05 11:44:03 +01:00
|
|
|
{ "room_id", "!conf" },
|
|
|
|
{ "type", "ircd.conf.item" },
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2018-10-07 07:17:46 +02:00
|
|
|
init_conf_items()
|
2018-03-05 11:44:03 +01:00
|
|
|
{
|
2018-12-29 03:45:26 +01:00
|
|
|
static const m::event::fetch::opts fopts
|
|
|
|
{
|
|
|
|
m::event::keys::include
|
|
|
|
{
|
|
|
|
"content", "state_key"
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-03-06 20:57:57 +01:00
|
|
|
const m::room::state state
|
|
|
|
{
|
2018-12-29 03:45:26 +01:00
|
|
|
conf_room, &fopts
|
2018-03-06 20:57:57 +01:00
|
|
|
};
|
2018-03-05 11:44:03 +01:00
|
|
|
|
2018-03-06 20:57:57 +01:00
|
|
|
state.for_each("ircd.conf.item", []
|
|
|
|
(const m::event &event)
|
|
|
|
{
|
2018-05-26 05:37:33 +02:00
|
|
|
conf_updated(event);
|
2018-03-06 20:57:57 +01:00
|
|
|
});
|
2018-03-05 11:44:03 +01:00
|
|
|
}
|
|
|
|
|
2018-09-03 13:40:28 +02:00
|
|
|
static void
|
|
|
|
init_conf_item(conf::item<> &item)
|
|
|
|
{
|
2018-12-29 03:45:26 +01:00
|
|
|
static const m::event::fetch::opts fopts
|
|
|
|
{
|
|
|
|
m::event::keys::include
|
|
|
|
{
|
|
|
|
"content", "state_key"
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-09-03 13:40:28 +02:00
|
|
|
const m::room::state state
|
|
|
|
{
|
2018-12-29 03:45:26 +01:00
|
|
|
conf_room, &fopts
|
2018-09-03 13:40:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
state.get(std::nothrow, "ircd.conf.item", item.name, []
|
|
|
|
(const m::event &event)
|
|
|
|
{
|
|
|
|
conf_updated(event);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-10-07 07:17:46 +02:00
|
|
|
static void
|
|
|
|
handle_init_conf_items(const m::event &,
|
|
|
|
m::vm::eval &eval)
|
|
|
|
{
|
|
|
|
init_conf_items();
|
|
|
|
}
|
|
|
|
|
2019-08-10 06:27:12 +02:00
|
|
|
m::hookfn<m::vm::eval &>
|
2018-03-05 11:44:03 +01:00
|
|
|
init_conf_items_hook
|
|
|
|
{
|
2018-10-07 07:17:46 +02:00
|
|
|
handle_init_conf_items,
|
2018-03-05 11:44:03 +01:00
|
|
|
{
|
2018-10-07 07:17:46 +02:00
|
|
|
{ "_site", "vm.effect" },
|
2018-03-05 11:44:03 +01:00
|
|
|
{ "room_id", "!ircd" },
|
|
|
|
{ "type", "m.room.member" },
|
|
|
|
{ "membership", "join" },
|
|
|
|
{ "state_key", "@ircd" },
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-09-25 09:27:36 +02:00
|
|
|
static m::event::id::buf
|
2018-03-05 11:44:03 +01:00
|
|
|
create_conf_item(const string_view &key,
|
|
|
|
const conf::item<> &item)
|
2018-09-25 09:27:36 +02:00
|
|
|
try
|
2018-03-05 11:44:03 +01:00
|
|
|
{
|
|
|
|
thread_local char vbuf[4_KiB];
|
|
|
|
const string_view &val
|
|
|
|
{
|
|
|
|
item.get(vbuf)
|
|
|
|
};
|
|
|
|
|
2018-09-25 09:27:36 +02:00
|
|
|
return set_conf_item(m::me.user_id, key, val);
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
if(item_error_log) log::error
|
2018-03-05 11:44:03 +01:00
|
|
|
{
|
2018-09-25 09:27:36 +02:00
|
|
|
"Failed to create conf item '%s' :%s",
|
|
|
|
key,
|
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
|
|
|
|
return {};
|
2018-03-05 11:44:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2018-10-07 07:17:46 +02:00
|
|
|
create_conf_room(const m::event &,
|
|
|
|
m::vm::eval &)
|
2018-03-05 11:44:03 +01:00
|
|
|
{
|
|
|
|
m::create(conf_room_id, m::me.user_id);
|
2018-12-09 01:15:22 +01:00
|
|
|
rehash_conf({}, true);
|
2018-03-05 11:44:03 +01:00
|
|
|
}
|
|
|
|
|
2019-08-10 06:27:12 +02:00
|
|
|
m::hookfn<m::vm::eval &>
|
2018-03-05 11:44:03 +01:00
|
|
|
create_conf_room_hook
|
|
|
|
{
|
|
|
|
create_conf_room,
|
|
|
|
{
|
2018-10-07 07:17:46 +02:00
|
|
|
{ "_site", "vm.effect" },
|
2018-03-05 11:44:03 +01:00
|
|
|
{ "room_id", "!ircd" },
|
|
|
|
{ "type", "m.room.create" },
|
|
|
|
}
|
|
|
|
};
|
2018-09-03 06:13:03 +02:00
|
|
|
|
|
|
|
void
|
2018-11-02 12:48:57 +01:00
|
|
|
rehash_conf(const string_view &prefix,
|
|
|
|
const bool &existing)
|
2018-09-03 06:13:03 +02:00
|
|
|
{
|
|
|
|
const m::room::state state
|
|
|
|
{
|
|
|
|
conf_room
|
|
|
|
};
|
|
|
|
|
|
|
|
for(const auto &p : conf::items)
|
|
|
|
{
|
|
|
|
const auto &key{p.first};
|
2018-11-02 12:48:57 +01:00
|
|
|
if(prefix && !startswith(key, prefix))
|
|
|
|
continue;
|
|
|
|
|
2018-09-03 06:13:03 +02:00
|
|
|
const auto &item{p.second}; assert(item);
|
2018-12-09 01:15:22 +01:00
|
|
|
|
|
|
|
// Conf items marked with a persist=false property are not written
|
|
|
|
// to the conf room.
|
|
|
|
if(!item->feature.get("persist", true))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Use the `existing` argument to toggle a force-overwrite
|
2018-09-03 06:13:03 +02:00
|
|
|
if(!existing)
|
|
|
|
if(state.has("ircd.conf.item", key))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
create_conf_item(key, *item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-02 12:49:56 +01:00
|
|
|
void
|
|
|
|
default_conf(const string_view &prefix)
|
|
|
|
{
|
|
|
|
for(const auto &p : conf::items)
|
|
|
|
{
|
|
|
|
const auto &key{p.first};
|
|
|
|
if(prefix && !startswith(key, prefix))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const auto &item{p.second}; assert(item);
|
|
|
|
const auto value
|
|
|
|
{
|
|
|
|
unquote(item->feature["default"])
|
|
|
|
};
|
|
|
|
|
|
|
|
conf::set(key, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-03 06:13:03 +02:00
|
|
|
void
|
|
|
|
reload_conf()
|
|
|
|
{
|
2018-10-07 07:17:46 +02:00
|
|
|
init_conf_items();
|
2018-09-03 06:13:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
refresh_conf()
|
|
|
|
{
|
|
|
|
ircd::conf::reset();
|
|
|
|
}
|