From ab1cef34fc9534785239784ff0c28985aa32af06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Pettersson?= Date: Sat, 11 Oct 2014 10:24:25 +0200 Subject: [PATCH] Implemented proxy capabilities. --- src/twc-config.c | 45 +++++++++++++++++++++++---------------------- src/twc-profile.c | 40 ++++++++++++++++++++++++++++++++-------- src/twc-profile.h | 16 ++++++++++++++++ 3 files changed, 71 insertions(+), 30 deletions(-) diff --git a/src/twc-config.c b/src/twc-config.c index 8d403d3..255a2d7 100644 --- a/src/twc-config.c +++ b/src/twc-config.c @@ -41,6 +41,7 @@ char *twc_profile_option_names[TWC_PROFILE_NUM_OPTIONS] = "proxy_address", "proxy_port", "proxy_enabled", + "udp_disabled", }; char *twc_profile_option_defaults[TWC_PROFILE_NUM_OPTIONS] = @@ -49,7 +50,8 @@ char *twc_profile_option_defaults[TWC_PROFILE_NUM_OPTIONS] = "off", "100", NULL, - NULL, + "0", + "off", "off", }; @@ -171,7 +173,7 @@ twc_config_profile_check_value_callback(void *data, switch (option_index) { case TWC_PROFILE_OPTION_PROXY_ADDRESS: - return strlen(value) < 256; + return !value || strlen(value) < 256; default: return 1; } @@ -219,9 +221,7 @@ twc_config_init_option(int option_index, const char *option_name) "automatically load a profile and connect to the Tox network " "when WeeChat starts", NULL, 0, 0, - twc_profile_option_defaults[option_index], - NULL, - 0, + twc_profile_option_defaults[option_index], NULL, 1, twc_config_profile_check_value_callback, (void *)(intptr_t)option_index, twc_config_profile_change_callback, (void *)(intptr_t)option_index, NULL, NULL); @@ -232,9 +232,7 @@ twc_config_init_option(int option_index, const char *option_name) "maximum amount of friend requests to retain before dropping " "new ones", NULL, 0, INT_MAX, - twc_profile_option_defaults[option_index], - NULL, - 0, + twc_profile_option_defaults[option_index], NULL, 1, twc_config_profile_check_value_callback, (void *)(intptr_t)option_index, twc_config_profile_change_callback, (void *)(intptr_t)option_index, NULL, NULL); @@ -244,9 +242,7 @@ twc_config_init_option(int option_index, const char *option_name) option_name, "string", "proxy address ", NULL, 0, 0, - twc_profile_option_defaults[option_index], - NULL, - 1, + twc_profile_option_defaults[option_index], NULL, 1, twc_config_profile_check_value_callback, (void *)(intptr_t)option_index, twc_config_profile_change_callback, (void *)(intptr_t)option_index, NULL, NULL); @@ -257,9 +253,7 @@ twc_config_init_option(int option_index, const char *option_name) "whether or not to proxy this profile; requires reload to " "effect", NULL, 0, 0, - twc_profile_option_defaults[option_index], - NULL, - 0, + twc_profile_option_defaults[option_index], NULL, 1, twc_config_profile_check_value_callback, (void *)(intptr_t)option_index, twc_config_profile_change_callback, (void *)(intptr_t)option_index, NULL, NULL); @@ -267,11 +261,9 @@ twc_config_init_option(int option_index, const char *option_name) return weechat_config_new_option( twc_config_file, twc_config_section_profile, option_name, "integer", - "proxy address ", - NULL, 0, UINT16_MAX , - twc_profile_option_defaults[option_index], - NULL, - 1, + "proxy address", + NULL, 1, UINT16_MAX , + twc_profile_option_defaults[option_index], NULL, 1, twc_config_profile_check_value_callback, (void *)(intptr_t)option_index, twc_config_profile_change_callback, (void *)(intptr_t)option_index, NULL, NULL); @@ -283,9 +275,17 @@ twc_config_init_option(int option_index, const char *option_name) "home, \"%p\" by the profile name); will be created if it does " "not exist.", NULL, 0, 0, - twc_profile_option_defaults[option_index], - NULL, - 0, + twc_profile_option_defaults[option_index], NULL, 1, + twc_config_profile_check_value_callback, (void *)(intptr_t)option_index, + twc_config_profile_change_callback, (void *)(intptr_t)option_index, + NULL, NULL); + case TWC_PROFILE_OPTION_UDP_DISABLED: + return weechat_config_new_option( + twc_config_file, twc_config_section_profile, + option_name, "boolean", + "disable UDP; may be necessary for certain proxies", + NULL, 0, 0, + twc_profile_option_defaults[option_index], NULL, 1, twc_config_profile_check_value_callback, (void *)(intptr_t)option_index, twc_config_profile_change_callback, (void *)(intptr_t)option_index, NULL, NULL); @@ -314,6 +314,7 @@ twc_config_init_profile(struct t_twc_profile *profile) twc_profile_option_names[i]); profile->options[i] = twc_config_init_option(i, option_name); + weechat_log_printf("Initialized option %s %p", option_name, profile->options[i]); free(option_name); } } diff --git a/src/twc-profile.c b/src/twc-profile.c index 2ad4160..8008e32 100644 --- a/src/twc-profile.c +++ b/src/twc-profile.c @@ -52,7 +52,7 @@ char * twc_profile_expanded_data_path(struct t_twc_profile *profile) { const char *weechat_dir = weechat_info_get ("weechat_dir", NULL); - const char *base_path = weechat_config_string(profile->options[TWC_PROFILE_OPTION_SAVEFILE]); + const char *base_path = TWC_PROFILE_OPTION_STRING(profile, TWC_PROFILE_OPTION_SAVEFILE); char *home_expanded = weechat_string_replace(base_path, "%h", weechat_dir); char *full_path = weechat_string_replace(home_expanded, "%p", profile->name); free(home_expanded); @@ -218,20 +218,44 @@ twc_profile_load(struct t_twc_profile *profile) // create Tox options object Tox_Options *options = malloc(sizeof(Tox_Options)); options->proxy_enabled = - weechat_config_boolean(profile->options[TWC_PROFILE_OPTION_PROXY_ENABLED]); + TWC_PROFILE_OPTION_BOOLEAN(profile, TWC_PROFILE_OPTION_PROXY_ENABLED); + const char *proxy_address = - weechat_config_string(profile->options[TWC_PROFILE_OPTION_PROXY_ADDRESS]); - memcpy(options->proxy_address, proxy_address, strlen(proxy_address) + 1); + TWC_PROFILE_OPTION_STRING(profile, TWC_PROFILE_OPTION_PROXY_ADDRESS); + if (proxy_address) + memcpy(options->proxy_address, proxy_address, strlen(proxy_address) + 1); + options->proxy_port = - weechat_config_integer(profile->options[TWC_PROFILE_OPTION_PROXY_PORT]); + TWC_PROFILE_OPTION_INTEGER(profile, TWC_PROFILE_OPTION_PROXY_PORT); + options->udp_disabled = + TWC_PROFILE_OPTION_BOOLEAN(profile, TWC_PROFILE_OPTION_UDP_DISABLED); + + if (options->proxy_enabled) + { + if (!options->proxy_address || !options->proxy_port) + { + weechat_printf(profile->buffer, + "%sproxy is enabled, but address or port is " + "missing; aborting", + weechat_prefix("error"), + options->proxy_address, options->proxy_port); + } + else + { + weechat_printf(profile->buffer, + "%sUsing proxy %s:%d", + weechat_prefix("network"), + options->proxy_address, options->proxy_port); + } + } // create Tox profile->tox = tox_new(options); if (!(profile->tox)) { weechat_printf(profile->buffer, - "Could not create Tox instance!", - weechat_prefix("error"), weechat_plugin->name); + "%sCould not create Tox instance!", + weechat_prefix("error")); return; } @@ -331,7 +355,7 @@ twc_profile_autoload() struct t_twc_list_item *item; twc_list_foreach(twc_profiles, index, item) { - if (weechat_config_boolean(item->profile->options[TWC_PROFILE_OPTION_AUTOLOAD])) + if (TWC_PROFILE_OPTION_BOOLEAN(item->profile, TWC_PROFILE_OPTION_AUTOLOAD)) twc_profile_load(item->profile); } } diff --git a/src/twc-profile.h b/src/twc-profile.h index f6db377..0cb989d 100644 --- a/src/twc-profile.h +++ b/src/twc-profile.h @@ -34,6 +34,7 @@ enum t_twc_profile_option TWC_PROFILE_OPTION_PROXY_ADDRESS, TWC_PROFILE_OPTION_PROXY_PORT, TWC_PROFILE_OPTION_PROXY_ENABLED, + TWC_PROFILE_OPTION_UDP_DISABLED, TWC_PROFILE_NUM_OPTIONS, }; @@ -56,6 +57,21 @@ struct t_twc_profile extern struct t_twc_list *twc_profiles; +#define TWC_PROFILE_OPTION_BOOLEAN(profile, index) \ + ((!weechat_config_option_is_null(profile->options[index])) ? \ + weechat_config_boolean(profile->options[index]) : \ + weechat_config_boolean_default(profile->options[index])) \ + +#define TWC_PROFILE_OPTION_INTEGER(profile, index) \ + ((!weechat_config_option_is_null(profile->options[index])) ? \ + weechat_config_integer(profile->options[index]) : \ + weechat_config_integer_default(profile->options[index])) \ + +#define TWC_PROFILE_OPTION_STRING(profile, index) \ + ((!weechat_config_option_is_null(profile->options[index])) ? \ + weechat_config_string(profile->options[index]) : \ + weechat_config_string_default(profile->options[index])) \ + void twc_profile_init();