0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-12-26 15:33:54 +01:00

modules/s_control: Move conf commands over to modules/console.

This commit is contained in:
Jason Volk 2018-04-23 16:39:29 -07:00
parent 1ab361a733
commit ed7375a653
2 changed files with 75 additions and 98 deletions

View file

@ -495,6 +495,80 @@ console_cmd__conf__list(opt &out, const string_view &line)
return true;
}
bool
console_cmd__conf(opt &out, const string_view &line)
{
return console_cmd__conf__list(out, line);
}
bool
console_cmd__conf__set(opt &out, const string_view &line)
{
const params param{line, " ",
{
"key", "value"
}};
const auto &key
{
param.at(0)
};
const auto &val
{
param.at(1)
};
using prototype = m::event::id::buf (const m::user::id &,
const string_view &key,
const string_view &val);
static m::import<prototype> set_conf_item
{
"s_conf", "set_conf_item"
};
const auto event_id
{
set_conf_item(m::me, key, val)
};
out << event_id << " <- " << key << " = " << val << std::endl;
return true;
}
bool
console_cmd__conf__get(opt &out, const string_view &line)
{
const params param{line, " ",
{
"key"
}};
const auto &key
{
param.at(0)
};
thread_local char val[4_KiB];
for(const auto &item : conf::items)
{
if(item.first != key)
continue;
out << std::setw(48) << std::right << item.first
<< " = " << item.second->get(val)
<< std::endl;
return true;
}
throw m::NOT_FOUND
{
"Conf item '%s' not found", key
};
}
//
// mod
//

View file

@ -34,100 +34,6 @@ conf_room_id
"!conf", ircd::my_host()
};
static void
_conf_set(const m::event &event,
const string_view &key,
const string_view &val)
{
const auto &sender
{
at<"sender"_>(event)
};
using prototype = m::event::id::buf (const m::user::id &,
const string_view &key,
const string_view &val);
static import<prototype> set_conf_item
{
"s_conf", "set_conf_item"
};
const auto event_id
{
set_conf_item(sender, key, val)
};
char kvbuf[768];
notice(control_room, fmt::sprintf
{
kvbuf, "[%s] %s = %s", string_view{event_id}, key, val
});
}
static void
_conf_get(const m::event &event,
const string_view &key)
{
using closure = std::function<void (const string_view &val)>;
using prototype = void (const string_view &key,
const closure &);
static import<prototype> get_conf_item
{
"s_conf", "get_conf_item"
};
get_conf_item(key, [&key]
(const string_view &value)
{
char kvbuf[256];
notice(control_room, fmt::sprintf
{
kvbuf, "%s = %s", key, value
});
});
}
static void
_conf_list(const m::event &event)
{
char val[512];
std::stringstream ss;
ss << "<table>";
for(const auto &p : conf::items)
ss << "<tr><td>" << std::setw(32) << std::right << p.first
<< "</td><td>" << p.second->get(val)
<< "</td></tr>";
ss << "</table>";
msghtml(control_room, m::me.user_id, ss.str());
}
static void
_cmd__conf(const m::event &event,
const string_view &line)
{
string_view tokens[4];
const size_t count
{
ircd::tokens(line, ' ', tokens)
};
const auto &cmd{tokens[0]};
const auto &key{tokens[1]};
const auto &val{tokens[3]};
if(cmd == "set" && count >= 4)
return _conf_set(event, key, val);
if(cmd == "get" && count >= 2)
return _conf_get(event, key);
if(cmd == "list")
return _conf_list(event);
}
static void
_cmd__die(const m::event &event,
const string_view &line)
@ -161,10 +67,7 @@ noexcept try
switch(hash(cmd))
{
case hash("conf"):
return _cmd__conf(event, tokens_after(body, ' ', 0));
case hash("die"):
case "die"_:
return _cmd__die(event, tokens_after(body, ' ', 0));
}