You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
5.2 KiB
C++

7 years ago
// This Source Code Form is subject to the terms of the Mozilla Public
// License, version 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 years ago
#include <stdlib.h>
#include <stdint.h>
7 years ago
#include <string.h>
#include <sstream>
3 years ago
#include <weechat/weechat-plugin.h>
2 years ago
#include <strophe.h>
7 years ago
3 years ago
#include "plugin.hh"
#include "account.hh"
3 years ago
#include "config.hh"
7 years ago
bool account_read_cb(weechat::config_section& section,
const char *option_name, const char *value)
7 years ago
{
if (!option_name)
return WEECHAT_CONFIG_READ_MEMORY_ERROR;
std::istringstream breadcrumbs(option_name);
std::string account_name, option_id;
std::getline(breadcrumbs, account_name, '.');
std::getline(breadcrumbs, option_id, '.');
if (account_name.empty())
return false;
bool rc = true;
weechat::account* account = nullptr;
if (!weechat::account::search(account, account_name))
{
account = &weechat::accounts.emplace(
std::piecewise_construct, std::forward_as_tuple(account_name),
std::forward_as_tuple(weechat::config::instance->file, account_name)).first->second;
}
if (account)
3 years ago
{
auto options = {
std::ref(account->option_jid),
std::ref(account->option_password),
std::ref(account->option_tls),
std::ref(account->option_nickname),
std::ref(account->option_autoconnect),
std::ref(account->option_resource),
std::ref(account->option_status),
std::ref(account->option_pgp_path),
std::ref(account->option_pgp_keyid),
};
if (!account->reloading_from_config++)
{
for (auto option : options)
option.get().clear();
}
7 years ago
account->reloading_from_config %= options.size();
7 years ago
auto rc_ok = [](int rc) {
return rc == WEECHAT_CONFIG_OPTION_SET_OK_CHANGED
|| rc == WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE;
};
if (option_id == "jid") rc &= rc_ok(account->option_jid = value);
if (option_id == "password") rc &= rc_ok(account->option_password = value);
if (option_id == "tls") rc &= rc_ok(account->option_tls = value);
if (option_id == "nickname") rc &= rc_ok(account->option_nickname = value);
if (option_id == "autoconnect") rc &= rc_ok(account->option_autoconnect = value);
if (option_id == "resource") rc &= rc_ok(account->option_resource = value);
if (option_id == "status") rc &= rc_ok(account->option_status = value);
if (option_id == "pgp_path") rc &= rc_ok(account->option_pgp_path = value);
if (option_id == "pgp_keyid") rc &= rc_ok(account->option_pgp_keyid = value);
7 years ago
if (!account->reloading_from_config)
{
bool ac_global = std::stoul(std::unique_ptr<char, decltype(free)*>(
weechat_info_get("auto_connect", NULL), &free).get());
bool ac_local = account->autoconnect();
if (ac_local && ac_global)
account->connect();
}
7 years ago
}
else
3 years ago
{
weechat_printf(
3 years ago
NULL,
_("%s%s: error adding account \"%s\""),
weechat_prefix("error"), WEECHAT_XMPP_PLUGIN_NAME,
account_name.data());
7 years ago
}
if (!rc)
7 years ago
{
weechat_printf(
NULL,
3 years ago
_("%s%s: error creating account option \"%s\""),
weechat_prefix("error"), WEECHAT_XMPP_PLUGIN_NAME, option_name);
7 years ago
}
return rc;
}
bool account_write_cb(weechat::config_section& section, const char *section_name)
7 years ago
{
if (!weechat_config_write_line(section.file, section_name, NULL))
7 years ago
return WEECHAT_CONFIG_WRITE_ERROR;
for (auto& account : weechat::accounts)
7 years ago
{
if (!account.second.write())
return WEECHAT_CONFIG_WRITE_ERROR;
7 years ago
}
return WEECHAT_CONFIG_WRITE_OK;
}
3 years ago
bool config_reload(weechat::config_file &file)
3 years ago
{
//weechat_config_section_free_options(file.configuration.section_account_default);
//weechat_config_section_free_options(file.configuration.section_account);
weechat::accounts.clear();
3 years ago
return weechat_config_reload(file);
3 years ago
}
7 years ago
weechat::config::config()
: file{*this, "xmpp", &config_reload}
, section_account_default{file, "account_default", 0, 0, {}, {}, {}, {}, {}}
, section_account{file, "account", 0, 0, &account_read_cb, &account_write_cb, {}, {}, {}}
, section_look{file, "look", 0, 0, {}, {}, {}, {}, {}}
, account_default{file, section_account_default}
, look{
.nick_completion_smart{file, section_look, "nick_completion_smart", "integer",
("smart completion for nicks (completes first with last speakers): "
"speakers = all speakers (including highlights), "
"speakers_highlights = only speakers with highlight"),
"off|speakers|speakers_highlights", 0, 0,
"speakers", nullptr, false,
{}, {}, {}}}
7 years ago
{
}
weechat::config::~config() {}
7 years ago
std::optional<weechat::config> weechat::config::instance;
bool weechat::config::init() { instance.emplace(); return true; }
bool weechat::config::read() { return instance->file.read(); }
bool weechat::config::write() { return instance->file.read(); }