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.

500 lines
16 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>
3 years ago
#include <strophe.h>
#include <weechat/weechat-plugin.h>
7 years ago
3 years ago
#include "plugin.hh"
#include "account.hh"
3 years ago
#include "config.hh"
7 years ago
3 years ago
struct t_config_file *config_file;
7 years ago
3 years ago
struct t_config_section *config_section_account_default;
struct t_config_section *config_section_account;
7 years ago
3 years ago
struct t_config_option *config_look_nick_completion_smart;
3 years ago
struct t_config_option *config_account_default[ACCOUNT_NUM_OPTIONS];
7 years ago
3 years ago
int config__account_check_value_cb(const void *pointer, void *data,
3 years ago
struct t_config_option *option,
const char *value)
7 years ago
{
(void) pointer;
(void) data;
(void) option;
(void) value;
3 years ago
return 1;
7 years ago
}
3 years ago
void config__account_change_cb(const void *pointer, void *data,
3 years ago
struct t_config_option *option)
7 years ago
{
(void) pointer;
(void) data;
3 years ago
const char *name =
weechat_config_option_get_string(option, "name");
const char *value =
weechat_config_option_get_string(option, "value");
int split_num;
char **split = weechat_string_split(name, ".", NULL, 0, 2, &split_num);
struct t_account *account = account__search(split[0]);
if (split_num >= 2 && account)
{
const char *key = split[1];
(void) key;
(void) value;
}
weechat_string_free_split(split);
7 years ago
}
3 years ago
void config__account_default_change_cb(const void *pointer, void *data,
3 years ago
struct t_config_option *option)
7 years ago
{
(void) pointer;
(void) data;
(void) option;
7 years ago
}
struct t_config_option *
3 years ago
config__account_new_option (struct t_config_file *config_file,
struct t_config_section *section,
int index_option,
const char *option_name,
const char *default_value,
const char *value,
int null_value_allowed,
int (*callback_check_value)(const void *pointer,
void *data,
struct t_config_option *option,
const char *value),
const void *callback_check_value_pointer,
void *callback_check_value_data,
void (*callback_change)(const void *pointer,
void *data,
struct t_config_option *option),
const void *callback_change_pointer,
void *callback_change_data)
7 years ago
{
3 years ago
struct t_config_option *new_option;
7 years ago
new_option = NULL;
switch (index_option)
{
3 years ago
case ACCOUNT_OPTION_JID:
new_option = weechat_config_new_option (
config_file, section,
option_name, "string",
N_("XMPP Account JID"),
NULL, 0, 0,
default_value, value,
null_value_allowed,
callback_check_value,
callback_check_value_pointer,
callback_check_value_data,
callback_change,
callback_change_pointer,
callback_change_data,
NULL, NULL, NULL);
break;
case ACCOUNT_OPTION_PASSWORD:
new_option = weechat_config_new_option (
config_file, section,
option_name, "string",
N_("XMPP Account Password"),
NULL, 0, 0,
default_value, value,
null_value_allowed,
callback_check_value,
callback_check_value_pointer,
callback_check_value_data,
callback_change,
callback_change_pointer,
callback_change_data,
NULL, NULL, NULL);
break;
case ACCOUNT_OPTION_TLS:
new_option = weechat_config_new_option (
config_file, section,
option_name, "integer",
N_("XMPP Server TLS Policy"),
"disable|normal|trust", 0, 0,
default_value, value,
null_value_allowed,
callback_check_value,
callback_check_value_pointer,
callback_check_value_data,
callback_change,
callback_change_pointer,
callback_change_data,
NULL, NULL, NULL);
break;
case ACCOUNT_OPTION_NICKNAME:
7 years ago
new_option = weechat_config_new_option (
config_file, section,
option_name, "string",
3 years ago
N_("XMPP Account Nickname"),
NULL, 0, 0,
default_value, value,
null_value_allowed,
callback_check_value,
callback_check_value_pointer,
callback_check_value_data,
callback_change,
callback_change_pointer,
callback_change_data,
NULL, NULL, NULL);
break;
case ACCOUNT_OPTION_AUTOCONNECT:
new_option = weechat_config_new_option (
config_file, section,
option_name, "boolean",
N_("Autoconnect XMPP Account"),
7 years ago
NULL, 0, 0,
default_value, value,
null_value_allowed,
callback_check_value,
callback_check_value_pointer,
callback_check_value_data,
3 years ago
callback_change,
callback_change_pointer,
callback_change_data,
NULL, NULL, NULL);
break;
case ACCOUNT_OPTION_RESOURCE:
new_option = weechat_config_new_option (
config_file, section,
option_name, "string",
N_("XMPP Account Resource"),
NULL, 0, 0,
default_value, value,
null_value_allowed,
callback_check_value,
callback_check_value_pointer,
callback_check_value_data,
3 years ago
callback_change,
callback_change_pointer,
callback_change_data,
NULL, NULL, NULL);
break;
case ACCOUNT_OPTION_STATUS:
new_option = weechat_config_new_option (
config_file, section,
option_name, "string",
N_("XMPP Account Login Status"),
NULL, 0, 0,
default_value, value,
null_value_allowed,
callback_check_value,
callback_check_value_pointer,
callback_check_value_data,
3 years ago
callback_change,
callback_change_pointer,
callback_change_data,
NULL, NULL, NULL);
break;
case ACCOUNT_OPTION_PGP_PUBRING_PATH:
new_option = weechat_config_new_option (
config_file, section,
option_name, "string",
N_("XMPP Account PGP Public Keyring Path"),
NULL, 0, 0,
default_value, value,
null_value_allowed,
callback_check_value,
callback_check_value_pointer,
callback_check_value_data,
callback_change,
callback_change_pointer,
callback_change_data,
NULL, NULL, NULL);
break;
case ACCOUNT_OPTION_PGP_SECRING_PATH:
new_option = weechat_config_new_option (
config_file, section,
option_name, "string",
N_("XMPP Account PGP Secure Keyring Path"),
NULL, 0, 0,
default_value, value,
null_value_allowed,
callback_check_value,
callback_check_value_pointer,
callback_check_value_data,
3 years ago
callback_change,
callback_change_pointer,
callback_change_data,
NULL, NULL, NULL);
break;
case ACCOUNT_OPTION_PGP_KEYID:
new_option = weechat_config_new_option (
config_file, section,
option_name, "string",
N_("XMPP Account PGP Key ID"),
NULL, 0, 0,
default_value, value,
null_value_allowed,
callback_check_value,
callback_check_value_pointer,
callback_check_value_data,
7 years ago
callback_change,
callback_change_pointer,
callback_change_data,
NULL, NULL, NULL);
break;
3 years ago
case ACCOUNT_NUM_OPTIONS:
7 years ago
break;
}
return new_option;
}
3 years ago
void config__account_create_default_options(struct t_config_section *section)
7 years ago
{
int i;
3 years ago
for (i = 0; i < ACCOUNT_NUM_OPTIONS; i++)
{
config_account_default[i] = config__account_new_option(
config_file,
section,
i,
account_options[i][0],
account_options[i][1],
account_options[i][1],
0,
&config__account_check_value_cb,
account_options[i][0],
NULL,
&config__account_default_change_cb,
account_options[i][0],
NULL);
}
7 years ago
}
3 years ago
int config__account_read_cb (const void *pointer, void *data,
struct t_config_file *config_file,
struct t_config_section *section,
const char *option_name, const char *value)
7 years ago
{
3 years ago
struct t_account *ptr_account;
7 years ago
int index_option, rc, i;
3 years ago
char *pos_option, *account_name;
7 years ago
(void) pointer;
(void) data;
(void) config_file;
(void) section;
rc = WEECHAT_CONFIG_OPTION_SET_ERROR;
if (option_name)
{
3 years ago
pos_option = const_cast<char*>(strrchr(option_name, '.'));
7 years ago
if (pos_option)
{
3 years ago
account_name = weechat_strndup(option_name,
pos_option - option_name);
7 years ago
pos_option++;
3 years ago
if (account_name)
7 years ago
{
3 years ago
index_option = account__search_option(pos_option);
7 years ago
if (index_option >= 0)
{
3 years ago
ptr_account = account__search(account_name);
if (!ptr_account)
ptr_account = account__alloc(account_name);
if (ptr_account)
7 years ago
{
3 years ago
if (!ptr_account->reloading_from_config++)
7 years ago
{
3 years ago
for (i = 0; i < ACCOUNT_NUM_OPTIONS; i++)
7 years ago
{
weechat_config_option_set(
3 years ago
ptr_account->options[i], NULL, 1);
7 years ago
}
}
3 years ago
ptr_account->reloading_from_config %=
ACCOUNT_NUM_OPTIONS;
7 years ago
rc = weechat_config_option_set(
3 years ago
ptr_account->options[index_option], value, 1);
3 years ago
if (!ptr_account->reloading_from_config)
{
const char *ac_global = weechat_info_get("auto_connect", NULL);
int ac_local = weechat_config_boolean(
ptr_account->options[ACCOUNT_OPTION_AUTOCONNECT]);
if (ac_local && (strcmp(ac_global, "1") == 0))
account__connect(ptr_account);
}
7 years ago
}
else
{
weechat_printf(
NULL,
3 years ago
_("%s%s: error adding account \"%s\""),
weechat_prefix("error"), WEECHAT_XMPP_PLUGIN_NAME,
account_name);
7 years ago
}
}
3 years ago
free(account_name);
7 years ago
}
}
}
if (rc == WEECHAT_CONFIG_OPTION_SET_ERROR)
{
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;
}
3 years ago
int config__account_write_cb(const void *pointer, void *data,
3 years ago
struct t_config_file *config_file,
const char *section_name)
7 years ago
{
3 years ago
struct t_account *ptr_account;
7 years ago
int i;
(void) pointer;
(void) data;
if (!weechat_config_write_line(config_file, section_name, NULL))
return WEECHAT_CONFIG_WRITE_ERROR;
3 years ago
for (ptr_account = accounts; ptr_account;
ptr_account = ptr_account->next_account)
7 years ago
{
3 years ago
for (i = 0; i < ACCOUNT_NUM_OPTIONS; i++)
7 years ago
{
if (!weechat_config_write_option(config_file,
3 years ago
ptr_account->options[i]))
7 years ago
return WEECHAT_CONFIG_WRITE_ERROR;
}
}
return WEECHAT_CONFIG_WRITE_OK;
}
3 years ago
3 years ago
int config__reload (const void *pointer, void *data,
struct t_config_file *config_file)
3 years ago
{
(void) pointer;
(void) data;
3 years ago
weechat_config_section_free_options(config_section_account_default);
weechat_config_section_free_options(config_section_account);
account__free_all();
3 years ago
return weechat_config_reload(config_file);
}
7 years ago
3 years ago
int config__init()
7 years ago
{
3 years ago
struct t_config_section *ptr_section;
7 years ago
3 years ago
config_file = weechat_config_new(WEECHAT_XMPP_CONFIG_NAME,
&config__reload, NULL, NULL);
7 years ago
3 years ago
if(!config_file)
7 years ago
return 0;
3 years ago
ptr_section = weechat_config_new_section(
config_file, "look",
0, 0,
NULL, NULL, NULL,
NULL, NULL, NULL,
NULL, NULL, NULL,
NULL, NULL, NULL,
NULL, NULL, NULL);
if (!ptr_section)
7 years ago
{
3 years ago
weechat_config_free(config_file);
config_file = NULL;
7 years ago
return 0;
}
3 years ago
config_look_nick_completion_smart = weechat_config_new_option (
config_file, ptr_section,
3 years ago
"nick_completion_smart", "integer",
N_("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", NULL, 0,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
7 years ago
3 years ago
ptr_section = weechat_config_new_section(
config_file, "account_default",
0, 0,
NULL, NULL, NULL,
NULL, NULL, NULL,
NULL, NULL, NULL,
NULL, NULL, NULL,
NULL, NULL, NULL);
if (!ptr_section)
{
weechat_config_free(config_file);
config_file = NULL;
return 0;
}
config_section_account_default = ptr_section;
config__account_create_default_options(ptr_section);
ptr_section = weechat_config_new_section(
config_file, "account",
0, 0,
&config__account_read_cb, NULL, NULL,
&config__account_write_cb, NULL, NULL,
NULL, NULL, NULL,
NULL, NULL, NULL,
NULL, NULL, NULL);
if (!ptr_section)
{
weechat_config_free(config_file);
config_file = NULL;
return 0;
}
config_section_account = ptr_section;
7 years ago
return 1;
}
3 years ago
int config__read()
7 years ago
{
3 years ago
int rc;
3 years ago
rc = weechat_config_read(config_file);
return rc;
7 years ago
}
3 years ago
int config__write()
7 years ago
{
3 years ago
return weechat_config_write(config_file);
7 years ago
}
3 years ago
void config__free()
7 years ago
{
}