2014-09-28 03:29:34 +02:00
|
|
|
/*
|
2017-02-04 01:00:25 +01:00
|
|
|
* Copyright (c) 2017 Håvard Pettersson <mail@haavard.me>
|
2014-09-28 03:29:34 +02:00
|
|
|
*
|
|
|
|
* This file is part of Tox-WeeChat.
|
|
|
|
*
|
|
|
|
* Tox-WeeChat is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Tox-WeeChat is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Tox-WeeChat. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <weechat/weechat-plugin.h>
|
|
|
|
|
|
|
|
#include "twc.h"
|
|
|
|
#include "twc-list.h"
|
|
|
|
#include "twc-profile.h"
|
|
|
|
|
|
|
|
#include "twc-config.h"
|
|
|
|
|
|
|
|
struct t_config_file *twc_config_file = NULL;
|
2014-10-12 12:24:49 +02:00
|
|
|
struct t_config_section *twc_config_section_look = NULL;
|
2014-09-28 03:29:34 +02:00
|
|
|
struct t_config_section *twc_config_section_profile = NULL;
|
2014-10-11 11:23:26 +02:00
|
|
|
struct t_config_section *twc_config_section_profile_default = NULL;
|
|
|
|
|
2014-10-12 12:24:49 +02:00
|
|
|
struct t_config_option *twc_config_friend_request_message;
|
|
|
|
struct t_config_option *twc_config_short_id_size;
|
|
|
|
|
2014-09-28 03:29:34 +02:00
|
|
|
char *twc_profile_option_names[TWC_PROFILE_NUM_OPTIONS] =
|
|
|
|
{
|
|
|
|
"save_file",
|
|
|
|
"autoload",
|
|
|
|
"max_friend_requests",
|
2014-10-11 08:28:41 +02:00
|
|
|
"proxy_address",
|
|
|
|
"proxy_port",
|
2015-01-04 14:56:05 +01:00
|
|
|
"proxy_type",
|
2014-10-11 10:28:59 +02:00
|
|
|
"udp",
|
2014-10-11 12:27:31 +02:00
|
|
|
"ipv6",
|
2015-09-05 22:04:10 +02:00
|
|
|
"passphrase",
|
2014-09-28 03:29:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the index of a profile option name.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
twc_config_profile_option_search(const char *option_name)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < TWC_PROFILE_NUM_OPTIONS; ++i)
|
|
|
|
{
|
|
|
|
if (strcmp(twc_profile_option_names[i], option_name) == 0)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when a profile option is read.
|
|
|
|
*/
|
|
|
|
int
|
2016-05-10 10:51:08 +02:00
|
|
|
twc_config_profile_read_callback(const void *pointer, void *data,
|
2014-09-28 03:29:34 +02:00
|
|
|
struct t_config_file *config_file,
|
|
|
|
struct t_config_section *section,
|
|
|
|
const char *option_name,
|
|
|
|
const char *value)
|
|
|
|
{
|
|
|
|
int rc = WEECHAT_CONFIG_OPTION_SET_ERROR;
|
|
|
|
|
|
|
|
if (option_name)
|
|
|
|
{
|
|
|
|
char *dot_pos = strrchr(option_name, '.');
|
|
|
|
if (dot_pos)
|
|
|
|
{
|
|
|
|
char *profile_name = weechat_strndup(option_name,
|
|
|
|
dot_pos-option_name);
|
|
|
|
char *option_name = dot_pos + 1;
|
|
|
|
if (profile_name)
|
|
|
|
{
|
|
|
|
int option_index = twc_config_profile_option_search(option_name);
|
|
|
|
if (option_index >= 0)
|
|
|
|
{
|
|
|
|
struct t_twc_profile *profile =
|
|
|
|
twc_profile_search_name(profile_name);
|
|
|
|
|
|
|
|
if (!profile)
|
|
|
|
profile = twc_profile_new(profile_name);
|
|
|
|
|
|
|
|
if (profile)
|
|
|
|
{
|
|
|
|
rc = weechat_config_option_set(profile->options[option_index],
|
|
|
|
value, 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
weechat_printf(NULL,
|
|
|
|
"%s%s: error creating profile \"%s\"",
|
|
|
|
weechat_prefix("error"),
|
|
|
|
weechat_plugin->name,
|
|
|
|
profile_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(profile_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rc == WEECHAT_CONFIG_OPTION_SET_ERROR)
|
|
|
|
{
|
|
|
|
weechat_printf(NULL,
|
|
|
|
"%s%s: error creating profile option \"%s\"",
|
|
|
|
weechat_prefix("error"), weechat_plugin->name,
|
|
|
|
option_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2014-10-12 12:24:49 +02:00
|
|
|
/**
|
|
|
|
* Callback for checking an option value being set.
|
|
|
|
*/
|
|
|
|
int
|
2016-05-10 10:51:08 +02:00
|
|
|
twc_config_check_value_callback(const void *pointer, void *data,
|
2014-10-12 12:24:49 +02:00
|
|
|
struct t_config_option *option,
|
|
|
|
const char *value)
|
|
|
|
{
|
|
|
|
int int_value = atoi(value);
|
|
|
|
|
|
|
|
// must be multiple of two
|
|
|
|
if (option == twc_config_short_id_size && int_value % 2)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-09-28 03:29:34 +02:00
|
|
|
/**
|
|
|
|
* Callback for checking an option value being set for a profile.
|
|
|
|
*/
|
|
|
|
int
|
2016-05-10 10:51:08 +02:00
|
|
|
twc_config_profile_check_value_callback(const void *pointer, void *data,
|
2014-09-28 03:29:34 +02:00
|
|
|
struct t_config_option *option,
|
|
|
|
const char *value)
|
|
|
|
{
|
2016-05-10 10:51:08 +02:00
|
|
|
enum t_twc_profile_option option_index = (intptr_t)pointer;
|
2014-10-11 08:46:26 +02:00
|
|
|
|
|
|
|
switch (option_index)
|
|
|
|
{
|
|
|
|
case TWC_PROFILE_OPTION_PROXY_ADDRESS:
|
2014-10-11 10:24:25 +02:00
|
|
|
return !value || strlen(value) < 256;
|
2014-10-11 08:46:26 +02:00
|
|
|
default:
|
|
|
|
return 1;
|
|
|
|
}
|
2014-09-28 03:29:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback for option being changed for a profile.
|
|
|
|
*/
|
|
|
|
void
|
2016-05-10 10:51:08 +02:00
|
|
|
twc_config_profile_change_callback(const void *pointer, void *data,
|
2014-09-28 03:29:34 +02:00
|
|
|
struct t_config_option *option)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-10-11 11:23:26 +02:00
|
|
|
/**
|
|
|
|
* Create a new option for a profile.
|
|
|
|
*/
|
|
|
|
struct t_config_option *
|
|
|
|
twc_config_init_option(struct t_config_section *section,
|
|
|
|
int option_index, const char *option_name,
|
|
|
|
bool is_default_profile)
|
|
|
|
{
|
|
|
|
char *type;
|
|
|
|
char *description;
|
|
|
|
char *string_values = NULL;
|
|
|
|
int min = 0, max = 0;
|
2015-01-04 15:05:18 +01:00
|
|
|
char *value;
|
|
|
|
char *default_value = NULL;
|
|
|
|
bool null_allowed = false;
|
|
|
|
|
2014-10-11 11:23:26 +02:00
|
|
|
|
|
|
|
switch (option_index)
|
|
|
|
{
|
|
|
|
case TWC_PROFILE_OPTION_AUTOLOAD:
|
|
|
|
type = "boolean";
|
|
|
|
description = "automatically load profile and connect to the Tox "
|
|
|
|
"network when WeeChat starts";
|
2015-01-04 15:05:18 +01:00
|
|
|
default_value = "off";
|
2014-10-11 11:23:26 +02:00
|
|
|
break;
|
2014-10-11 12:27:31 +02:00
|
|
|
case TWC_PROFILE_OPTION_IPV6:
|
|
|
|
type = "boolean";
|
|
|
|
description = "use IPv6 as well as IPv4 to connect to the Tox "
|
|
|
|
"network";
|
2015-01-04 15:05:18 +01:00
|
|
|
default_value = "on";
|
2014-10-11 12:27:31 +02:00
|
|
|
break;
|
2014-10-11 11:23:26 +02:00
|
|
|
case TWC_PROFILE_OPTION_MAX_FRIEND_REQUESTS:
|
|
|
|
type = "integer";
|
|
|
|
description = "maximum amount of friend requests to retain before "
|
|
|
|
"ignoring new ones";
|
|
|
|
min = 0; max = INT_MAX;
|
2015-01-04 15:05:18 +01:00
|
|
|
default_value = "100";
|
2014-10-11 11:23:26 +02:00
|
|
|
break;
|
2015-09-05 22:04:10 +02:00
|
|
|
case TWC_PROFILE_OPTION_PASSPHRASE:
|
|
|
|
type = "string";
|
|
|
|
description = "passphrase for encrypted profile";
|
|
|
|
null_allowed = true;
|
|
|
|
break;
|
2014-10-11 11:23:26 +02:00
|
|
|
case TWC_PROFILE_OPTION_PROXY_ADDRESS:
|
|
|
|
type = "string";
|
|
|
|
description = "proxy address";
|
2015-01-04 15:05:18 +01:00
|
|
|
null_allowed = true;
|
2014-10-11 11:23:26 +02:00
|
|
|
break;
|
|
|
|
case TWC_PROFILE_OPTION_PROXY_PORT:
|
|
|
|
type = "integer";
|
|
|
|
description = "proxy port";
|
|
|
|
min = 0; max = UINT16_MAX;
|
2015-01-04 15:05:18 +01:00
|
|
|
null_allowed = true;
|
2014-10-11 11:23:26 +02:00
|
|
|
break;
|
2015-01-04 14:56:05 +01:00
|
|
|
case TWC_PROFILE_OPTION_PROXY_TYPE:
|
|
|
|
type = "integer";
|
|
|
|
description = "proxy type; requires profile reload to take effect";
|
|
|
|
string_values = "none|socks5|http";
|
|
|
|
min = 0; max = 0;
|
2015-01-04 15:05:18 +01:00
|
|
|
default_value = "none";
|
2015-01-04 14:56:05 +01:00
|
|
|
break;
|
2014-10-11 11:23:26 +02:00
|
|
|
case TWC_PROFILE_OPTION_SAVEFILE:
|
|
|
|
type = "string";
|
|
|
|
description = "path to Tox data file (\"%h\" will be replaced by "
|
|
|
|
"WeeChat home folder and \"%p\" by profile name";
|
2015-01-04 15:05:18 +01:00
|
|
|
default_value = "%h/tox/%p";
|
2014-10-11 11:23:26 +02:00
|
|
|
break;
|
|
|
|
case TWC_PROFILE_OPTION_UDP:
|
|
|
|
type = "boolean";
|
|
|
|
description = "use UDP when communicating with the Tox network";
|
2015-01-04 15:05:18 +01:00
|
|
|
default_value = "on";
|
2014-10-11 11:23:26 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-01-04 15:05:18 +01:00
|
|
|
null_allowed = null_allowed || !is_default_profile;
|
|
|
|
value = is_default_profile ? default_value : NULL;
|
2014-10-11 11:23:26 +02:00
|
|
|
|
|
|
|
return weechat_config_new_option(
|
|
|
|
twc_config_file, section,
|
|
|
|
option_name, type, description, string_values, min, max,
|
|
|
|
default_value, value, null_allowed,
|
2016-05-10 10:51:08 +02:00
|
|
|
twc_config_profile_check_value_callback,
|
|
|
|
(void *)(intptr_t)option_index, NULL,
|
|
|
|
twc_config_profile_change_callback,
|
|
|
|
(void *)(intptr_t)option_index, NULL,
|
|
|
|
NULL, NULL, NULL);
|
2014-10-11 11:23:26 +02:00
|
|
|
}
|
|
|
|
|
2014-09-28 03:29:34 +02:00
|
|
|
/**
|
|
|
|
* Initialize Tox-WeeChat config. Creates file and section objects.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
twc_config_init()
|
|
|
|
{
|
2016-05-10 10:51:08 +02:00
|
|
|
twc_config_file = weechat_config_new("tox", NULL, NULL, NULL);
|
2014-09-28 03:29:34 +02:00
|
|
|
|
|
|
|
twc_config_section_profile =
|
|
|
|
weechat_config_new_section(twc_config_file, "profile",
|
|
|
|
0, 0,
|
2016-05-10 10:51:08 +02:00
|
|
|
twc_config_profile_read_callback,
|
2014-10-12 12:31:22 +02:00
|
|
|
NULL, NULL,
|
2016-05-10 10:51:08 +02:00
|
|
|
NULL, NULL, NULL,
|
|
|
|
NULL, NULL, NULL,
|
|
|
|
NULL, NULL, NULL,
|
|
|
|
NULL, NULL, NULL);
|
2014-09-28 03:29:34 +02:00
|
|
|
|
2014-10-11 11:23:26 +02:00
|
|
|
twc_config_section_profile_default =
|
|
|
|
weechat_config_new_section(twc_config_file, "profile_default",
|
|
|
|
0, 0,
|
2016-05-10 10:51:08 +02:00
|
|
|
NULL, NULL, NULL,
|
|
|
|
NULL, NULL, NULL,
|
|
|
|
NULL, NULL, NULL,
|
|
|
|
NULL, NULL, NULL,
|
|
|
|
NULL, NULL, NULL);
|
2014-10-11 11:23:26 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < TWC_PROFILE_NUM_OPTIONS; ++i)
|
2014-09-28 03:29:34 +02:00
|
|
|
{
|
2014-10-11 11:23:26 +02:00
|
|
|
twc_config_profile_default[i] =
|
|
|
|
twc_config_init_option(twc_config_section_profile_default,
|
|
|
|
i, twc_profile_option_names[i], true);
|
2014-09-28 03:29:34 +02:00
|
|
|
}
|
2014-10-12 12:24:49 +02:00
|
|
|
|
|
|
|
twc_config_section_look =
|
|
|
|
weechat_config_new_section(twc_config_file, "look",
|
|
|
|
0, 0,
|
2016-05-10 10:51:08 +02:00
|
|
|
NULL, NULL, NULL,
|
|
|
|
NULL, NULL, NULL,
|
|
|
|
NULL, NULL, NULL,
|
|
|
|
NULL, NULL, NULL,
|
|
|
|
NULL, NULL, NULL);
|
2014-10-12 12:24:49 +02:00
|
|
|
|
|
|
|
twc_config_friend_request_message = weechat_config_new_option(
|
|
|
|
twc_config_file, twc_config_section_look,
|
|
|
|
"friend_request_message", "string",
|
|
|
|
"message sent with friend requests if no other message is specified",
|
|
|
|
NULL, 0, 0,
|
|
|
|
"Hi! Please add me on Tox!", NULL, 0,
|
2016-05-10 10:51:08 +02:00
|
|
|
twc_config_check_value_callback, NULL, NULL,
|
|
|
|
NULL, NULL, NULL, NULL, NULL, NULL);
|
2014-10-12 12:24:49 +02:00
|
|
|
twc_config_short_id_size = weechat_config_new_option(
|
|
|
|
twc_config_file, twc_config_section_look,
|
|
|
|
"short_id_size", "integer",
|
|
|
|
"length of Tox IDs shown in short format; must be a multiple of two",
|
2015-04-09 19:01:33 +02:00
|
|
|
NULL, 2, TOX_PUBLIC_KEY_SIZE * 2,
|
2014-10-12 12:24:49 +02:00
|
|
|
"8", NULL, 0,
|
2016-05-10 10:51:08 +02:00
|
|
|
twc_config_check_value_callback, NULL, NULL,
|
|
|
|
NULL, NULL, NULL, NULL, NULL, NULL);
|
2014-09-28 03:29:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize options for a given profile.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
twc_config_init_profile(struct t_twc_profile *profile)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < TWC_PROFILE_NUM_OPTIONS; ++i)
|
|
|
|
{
|
|
|
|
// length: name + . + option + \0
|
|
|
|
size_t length = strlen(profile->name) + 1
|
|
|
|
+ strlen(twc_profile_option_names[i]) + 1;
|
|
|
|
|
|
|
|
char *option_name = malloc(sizeof(*option_name) * length);
|
|
|
|
if (option_name)
|
|
|
|
{
|
|
|
|
snprintf(option_name, length, "%s.%s",
|
|
|
|
profile->name,
|
|
|
|
twc_profile_option_names[i]);
|
|
|
|
|
2014-10-11 11:23:26 +02:00
|
|
|
profile->options[i] = twc_config_init_option(twc_config_section_profile,
|
|
|
|
i, option_name, false);
|
2014-10-11 08:28:41 +02:00
|
|
|
free(option_name);
|
2014-09-28 03:29:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read config data from file, creating profile objects for stored profiles.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
twc_config_read()
|
|
|
|
{
|
|
|
|
return weechat_config_read(twc_config_file);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write config data to disk.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
twc_config_write()
|
|
|
|
{
|
|
|
|
return weechat_config_write(twc_config_file);
|
|
|
|
}
|
|
|
|
|