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;
|
|
|
|
|
|
|
|
mapi::header
|
|
|
|
IRCD_MODULE
|
|
|
|
{
|
2018-03-17 07:48:05 +01:00
|
|
|
"Server Configuration"
|
2018-03-05 11:44:03 +01: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()
|
|
|
|
};
|
|
|
|
|
|
|
|
m::room
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
conf_room.get("ircd.conf.item", key, [&closure]
|
|
|
|
(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
|
|
|
|
update_conf(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"))
|
|
|
|
};
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
"Failed to set conf item '%s' :%s",
|
|
|
|
json::get<"state_key"_>(event),
|
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
}
|
2018-03-05 11:44:03 +01:00
|
|
|
|
2018-05-14 06:13:23 +02:00
|
|
|
const m::hook<>
|
2018-03-05 11:44:03 +01:00
|
|
|
update_conf_hook
|
|
|
|
{
|
|
|
|
update_conf,
|
|
|
|
{
|
2018-03-16 21:27:25 +01:00
|
|
|
{ "_site", "vm.notify" },
|
2018-03-05 11:44:03 +01:00
|
|
|
{ "room_id", "!conf" },
|
|
|
|
{ "type", "ircd.conf.item" },
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
init_conf_items(const m::event &)
|
|
|
|
{
|
2018-03-06 20:57:57 +01:00
|
|
|
const m::room::state state
|
|
|
|
{
|
|
|
|
conf_room
|
|
|
|
};
|
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)
|
|
|
|
{
|
|
|
|
update_conf(event);
|
|
|
|
});
|
2018-03-05 11:44:03 +01:00
|
|
|
}
|
|
|
|
|
2018-05-14 06:13:23 +02:00
|
|
|
const m::hook<>
|
2018-03-05 11:44:03 +01:00
|
|
|
init_conf_items_hook
|
|
|
|
{
|
|
|
|
init_conf_items,
|
|
|
|
{
|
2018-03-16 21:27:25 +01:00
|
|
|
{ "_site", "vm.notify" },
|
2018-03-05 11:44:03 +01:00
|
|
|
{ "room_id", "!ircd" },
|
|
|
|
{ "type", "m.room.member" },
|
|
|
|
{ "membership", "join" },
|
|
|
|
{ "state_key", "@ircd" },
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
create_conf_item(const string_view &key,
|
|
|
|
const conf::item<> &item)
|
|
|
|
{
|
|
|
|
thread_local char vbuf[4_KiB];
|
|
|
|
const string_view &val
|
|
|
|
{
|
|
|
|
item.get(vbuf)
|
|
|
|
};
|
|
|
|
|
|
|
|
send(conf_room, m::me.user_id, "ircd.conf.item", key,
|
|
|
|
{
|
|
|
|
{ "value", val }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
create_conf_room(const m::event &)
|
|
|
|
{
|
|
|
|
m::create(conf_room_id, m::me.user_id);
|
|
|
|
|
|
|
|
for(const auto &p : conf::items)
|
|
|
|
{
|
|
|
|
const auto &key{p.first};
|
|
|
|
const auto &item{p.second}; assert(item);
|
|
|
|
create_conf_item(key, *item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-14 06:13:23 +02:00
|
|
|
const m::hook<>
|
2018-03-05 11:44:03 +01:00
|
|
|
create_conf_room_hook
|
|
|
|
{
|
|
|
|
create_conf_room,
|
|
|
|
{
|
2018-03-16 21:27:25 +01:00
|
|
|
{ "_site", "vm.notify" },
|
2018-03-05 11:44:03 +01:00
|
|
|
{ "room_id", "!ircd" },
|
|
|
|
{ "type", "m.room.create" },
|
|
|
|
}
|
|
|
|
};
|