weechat-xmpp/config.c

434 lines
14 KiB
C
Raw Normal View History

2018-05-09 04:39:16 +00:00
// 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/.
2018-04-29 21:26:14 +00:00
#include <stdlib.h>
#include <string.h>
2021-06-26 01:31:45 +00:00
#include <strophe.h>
#include <weechat/weechat-plugin.h>
2018-04-29 21:26:14 +00:00
2021-06-28 01:17:02 +00:00
#include "plugin.h"
2021-06-30 06:26:06 +00:00
#include "account.h"
2021-06-28 01:17:02 +00:00
#include "config.h"
2018-04-29 21:26:14 +00:00
2021-06-30 06:26:06 +00:00
struct t_config_file *config_file;
2018-04-29 21:26:14 +00:00
2021-06-30 06:26:06 +00:00
struct t_config_section *config_section_account_default;
struct t_config_section *config_section_account;
2018-04-29 21:26:14 +00:00
2021-06-30 06:26:06 +00:00
struct t_config_option *config_look_nick_completion_smart;
2021-06-30 06:26:06 +00:00
struct t_config_option *config_account_default[ACCOUNT_NUM_OPTIONS];
2018-04-29 21:26:14 +00:00
2021-06-30 06:26:06 +00:00
int config__account_check_value_cb(const void *pointer, void *data,
struct t_config_option *option,
const char *value)
2018-04-29 21:26:14 +00:00
{
2018-05-01 10:23:37 +00:00
(void) pointer;
(void) data;
(void) option;
(void) value;
2021-06-30 06:26:06 +00:00
return 1;
2018-04-29 21:26:14 +00:00
}
2021-06-30 06:26:06 +00:00
void config__account_change_cb(const void *pointer, void *data,
struct t_config_option *option)
2018-04-29 21:26:14 +00:00
{
2018-05-01 10:23:37 +00:00
(void) pointer;
(void) data;
(void) option;
2018-04-29 21:26:14 +00:00
}
2021-06-30 06:26:06 +00:00
void config__account_default_change_cb(const void *pointer, void *data,
struct t_config_option *option)
2018-04-29 21:26:14 +00:00
{
2018-05-01 10:23:37 +00:00
(void) pointer;
(void) data;
(void) option;
2018-04-29 21:26:14 +00:00
}
struct t_config_option *
2021-06-30 06:26:06 +00:00
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)
2018-04-29 21:26:14 +00:00
{
2021-06-30 06:26:06 +00:00
struct t_config_option *new_option;
2018-04-29 21:26:14 +00:00
new_option = NULL;
switch (index_option)
{
2021-06-30 06:26:06 +00:00
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:
2018-04-29 21:26:14 +00:00
new_option = weechat_config_new_option (
config_file, section,
option_name, "string",
2021-06-30 08:54:59 +00:00
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"),
2018-04-29 21:26:14 +00:00
NULL, 0, 0,
default_value, value,
null_value_allowed,
callback_check_value,
callback_check_value_pointer,
callback_check_value_data,
2021-07-02 06:48:09 +00:00
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,
2021-07-02 23:05:36 +00:00
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,
2018-04-29 21:26:14 +00:00
callback_change,
callback_change_pointer,
callback_change_data,
NULL, NULL, NULL);
break;
2021-06-30 06:26:06 +00:00
case ACCOUNT_NUM_OPTIONS:
2018-04-29 21:26:14 +00:00
break;
}
return new_option;
}
2021-06-30 06:26:06 +00:00
void config__account_create_default_options(struct t_config_section *section)
2018-04-29 21:26:14 +00:00
{
int i;
2021-06-30 06:26:06 +00:00
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);
}
2018-04-29 21:26:14 +00:00
}
2021-06-30 06:26:06 +00:00
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)
2018-04-29 21:26:14 +00:00
{
2021-06-30 06:26:06 +00:00
struct t_account *ptr_account;
2018-04-29 21:26:14 +00:00
int index_option, rc, i;
2021-06-30 06:26:06 +00:00
char *pos_option, *account_name;
2018-04-29 21:26:14 +00:00
(void) pointer;
(void) data;
(void) config_file;
(void) section;
rc = WEECHAT_CONFIG_OPTION_SET_ERROR;
if (option_name)
{
pos_option = strrchr(option_name, '.');
if (pos_option)
{
2021-06-30 06:26:06 +00:00
account_name = weechat_strndup(option_name,
pos_option - option_name);
2018-04-29 21:26:14 +00:00
pos_option++;
2021-06-30 06:26:06 +00:00
if (account_name)
2018-04-29 21:26:14 +00:00
{
2021-06-30 06:26:06 +00:00
index_option = account__search_option(pos_option);
2018-04-29 21:26:14 +00:00
if (index_option >= 0)
{
2021-06-30 06:26:06 +00:00
ptr_account = account__search(account_name);
if (!ptr_account)
ptr_account = account__alloc(account_name);
if (ptr_account)
2018-04-29 21:26:14 +00:00
{
2021-06-30 06:26:06 +00:00
if (!ptr_account->reloading_from_config++)
2018-04-29 21:26:14 +00:00
{
2021-06-30 06:26:06 +00:00
for (i = 0; i < ACCOUNT_NUM_OPTIONS; i++)
2018-04-29 21:26:14 +00:00
{
weechat_config_option_set(
2021-06-30 06:26:06 +00:00
ptr_account->options[i], NULL, 1);
2018-04-29 21:26:14 +00:00
}
}
2021-06-30 06:26:06 +00:00
ptr_account->reloading_from_config %=
ACCOUNT_NUM_OPTIONS;
2018-04-29 21:26:14 +00:00
rc = weechat_config_option_set(
2021-06-30 06:26:06 +00:00
ptr_account->options[index_option], value, 1);
2021-06-30 08:54:59 +00:00
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);
}
2018-04-29 21:26:14 +00:00
}
else
{
weechat_printf(
NULL,
2021-06-30 06:26:06 +00:00
_("%s%s: error adding account \"%s\""),
weechat_prefix("error"), WEECHAT_XMPP_PLUGIN_NAME,
account_name);
2018-04-29 21:26:14 +00:00
}
}
2021-06-30 06:26:06 +00:00
free(account_name);
2018-04-29 21:26:14 +00:00
}
}
}
if (rc == WEECHAT_CONFIG_OPTION_SET_ERROR)
{
weechat_printf(
NULL,
2021-06-30 06:26:06 +00:00
_("%s%s: error creating account option \"%s\""),
weechat_prefix("error"), WEECHAT_XMPP_PLUGIN_NAME, option_name);
2018-04-29 21:26:14 +00:00
}
return rc;
}
2021-06-30 06:26:06 +00:00
int config__account_write_cb (const void *pointer, void *data,
struct t_config_file *config_file,
const char *section_name)
2018-04-29 21:26:14 +00:00
{
2021-06-30 06:26:06 +00:00
struct t_account *ptr_account;
2018-04-29 21:26:14 +00:00
int i;
(void) pointer;
(void) data;
if (!weechat_config_write_line(config_file, section_name, NULL))
return WEECHAT_CONFIG_WRITE_ERROR;
2021-06-30 06:26:06 +00:00
for (ptr_account = accounts; ptr_account;
ptr_account = ptr_account->next_account)
2018-04-29 21:26:14 +00:00
{
2021-06-30 06:26:06 +00:00
for (i = 0; i < ACCOUNT_NUM_OPTIONS; i++)
2018-04-29 21:26:14 +00:00
{
if (!weechat_config_write_option(config_file,
2021-06-30 06:26:06 +00:00
ptr_account->options[i]))
2018-04-29 21:26:14 +00:00
return WEECHAT_CONFIG_WRITE_ERROR;
}
}
return WEECHAT_CONFIG_WRITE_OK;
}
2021-06-26 01:31:45 +00:00
2021-06-30 06:26:06 +00:00
int config__reload (const void *pointer, void *data,
struct t_config_file *config_file)
2021-06-26 01:31:45 +00:00
{
(void) pointer;
(void) data;
2021-06-30 06:26:06 +00:00
weechat_config_section_free_options(config_section_account_default);
weechat_config_section_free_options(config_section_account);
account__free_all();
2021-06-26 01:31:45 +00:00
return weechat_config_reload(config_file);
}
2018-04-29 21:26:14 +00:00
2021-06-30 06:26:06 +00:00
int config__init()
2018-04-29 21:26:14 +00:00
{
2021-06-30 06:26:06 +00:00
struct t_config_section *ptr_section;
2018-04-29 21:26:14 +00:00
2021-06-30 06:26:06 +00:00
config_file = weechat_config_new(WEECHAT_XMPP_CONFIG_NAME,
&config__reload, NULL, NULL);
2018-04-29 21:26:14 +00:00
2021-06-30 06:26:06 +00:00
if(!config_file)
2018-04-29 21:26:14 +00:00
return 0;
2021-06-30 06:26:06 +00:00
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)
2018-04-29 21:26:14 +00:00
{
2021-06-30 06:26:06 +00:00
weechat_config_free(config_file);
config_file = NULL;
2018-04-29 21:26:14 +00:00
return 0;
}
2021-06-30 06:26:06 +00:00
config_look_nick_completion_smart = weechat_config_new_option (
config_file, ptr_section,
2021-06-26 01:31:45 +00:00
"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);
2018-04-29 21:26:14 +00:00
2021-06-30 06:26:06 +00:00
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;
2018-04-29 21:26:14 +00:00
return 1;
}
2021-06-30 06:26:06 +00:00
int config__read()
2018-04-29 21:26:14 +00:00
{
2021-06-30 06:26:06 +00:00
int rc;
2018-05-01 10:23:37 +00:00
2021-06-30 06:26:06 +00:00
rc = weechat_config_read(config_file);
2018-05-01 10:23:37 +00:00
return rc;
2018-04-29 21:26:14 +00:00
}
2021-06-30 06:26:06 +00:00
int config__write()
2018-04-29 21:26:14 +00:00
{
2021-06-30 06:26:06 +00:00
return weechat_config_write(config_file);
2018-04-29 21:26:14 +00:00
}
2021-06-30 06:26:06 +00:00
void config__free()
2018-04-29 21:26:14 +00:00
{
}