From e6e4fd568e15e25a6e75a7d4b0a5587e8236425f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Pettersson?= Date: Sun, 12 Oct 2014 12:24:49 +0200 Subject: [PATCH] Added some config options. --- src/twc-commands.c | 10 +++++----- src/twc-config.c | 47 ++++++++++++++++++++++++++++++++++++++++++++++ src/twc-config.h | 5 +++++ src/twc-sqlite.c | 5 +---- src/twc-utils.c | 6 ++++-- 5 files changed, 62 insertions(+), 11 deletions(-) diff --git a/src/twc-commands.c b/src/twc-commands.c index 3c7a801..5241a3f 100644 --- a/src/twc-commands.c +++ b/src/twc-commands.c @@ -31,6 +31,7 @@ #include "twc-group-invite.h" #include "twc-bootstrap.h" #include "twc-sqlite.h" +#include "twc-config.h" #include "twc-utils.h" #include "twc-commands.h" @@ -267,8 +268,7 @@ twc_cmd_friend(void *data, struct t_gui_buffer *buffer, } if (!message) - // TODO: default message as option - message = "Hi! Please add me on Tox!"; + message = weechat_config_string(twc_config_friend_request_message); if (strlen(hex_id) != TOX_FRIEND_ADDRESS_SIZE * 2) { @@ -455,10 +455,10 @@ twc_cmd_friend(void *data, struct t_gui_buffer *buffer, struct t_twc_list_item *item; twc_list_foreach(friend_requests, index, item) { - // TODO: load short form address length from config - char hex_address[12 + 1]; + size_t short_id_length = weechat_config_integer(twc_config_short_id_size); + char hex_address[short_id_length + 1]; twc_bin2hex(item->friend_request->tox_id, - 6, + short_id_length / 2, hex_address); weechat_printf(profile->buffer, diff --git a/src/twc-config.c b/src/twc-config.c index e9c94b5..1c03bb6 100644 --- a/src/twc-config.c +++ b/src/twc-config.c @@ -31,9 +31,13 @@ #include "twc-config.h" struct t_config_file *twc_config_file = NULL; +struct t_config_section *twc_config_section_look = NULL; struct t_config_section *twc_config_section_profile = NULL; struct t_config_section *twc_config_section_profile_default = NULL; +struct t_config_option *twc_config_friend_request_message; +struct t_config_option *twc_config_short_id_size; + char *twc_profile_option_names[TWC_PROFILE_NUM_OPTIONS] = { "save_file", @@ -175,6 +179,23 @@ twc_config_profile_write_callback(void *data, return WEECHAT_CONFIG_WRITE_OK; } +/** + * Callback for checking an option value being set. + */ +int +twc_config_check_value_callback(void *data, + 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; +} + /** * Callback for checking an option value being set for a profile. */ @@ -307,6 +328,32 @@ twc_config_init() twc_config_init_option(twc_config_section_profile_default, i, twc_profile_option_names[i], true); } + + twc_config_section_look = + weechat_config_new_section(twc_config_file, "look", + 0, 0, + NULL, NULL, + NULL, NULL, + NULL, NULL, + NULL, NULL, + NULL, NULL); + + 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, + twc_config_check_value_callback, NULL, + NULL, NULL, NULL, NULL); + 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", + NULL, 2, TOX_CLIENT_ID_SIZE * 2, + "8", NULL, 0, + twc_config_check_value_callback, NULL, + NULL, NULL, NULL, NULL); } /** diff --git a/src/twc-config.h b/src/twc-config.h index b90fe72..7142929 100644 --- a/src/twc-config.h +++ b/src/twc-config.h @@ -20,6 +20,11 @@ #ifndef TOX_WEECHAT_CONFIG_H #define TOX_WEECHAT_CONFIG_H +struct t_twc_profile; + +extern struct t_config_option *twc_config_friend_request_message; +extern struct t_config_option *twc_config_short_id_size; + void twc_config_init(); diff --git a/src/twc-sqlite.c b/src/twc-sqlite.c index deac518..d83d053 100644 --- a/src/twc-sqlite.c +++ b/src/twc-sqlite.c @@ -34,9 +34,6 @@ sqlite3 *twc_sqlite_db = NULL; struct t_twc_list *twc_sqlite_statements = NULL; -// TODO: move to config -#define TWC_SQLITE_PATH "%h/tox/data.db" - #define TWC_SQLITE_DEBUG_RC(rc, expected_rc) \ if (rc != expected_rc) \ weechat_printf(NULL, \ @@ -74,7 +71,7 @@ char * twc_sqlite_db_path() { const char *weechat_dir = weechat_info_get("weechat_dir", NULL); - return weechat_string_replace(TWC_SQLITE_PATH, "%h", weechat_dir); + return weechat_string_replace("%h/tox/data.db", "%h", weechat_dir); } /** diff --git a/src/twc-utils.c b/src/twc-utils.c index c8f4253..ed3b314 100644 --- a/src/twc-utils.c +++ b/src/twc-utils.c @@ -23,6 +23,9 @@ #include #include +#include "twc.h" +#include "twc-config.h" + #include "twc-utils.h" /** @@ -140,8 +143,7 @@ twc_get_friend_id_short(Tox *tox, int32_t friend_number) uint8_t client_id[TOX_CLIENT_ID_SIZE]; tox_get_client_id(tox, friend_number, client_id); - // TODO: config - size_t short_id_length = 8; + size_t short_id_length = weechat_config_integer(twc_config_short_id_size); char *hex_address = malloc(short_id_length + 1); twc_bin2hex(client_id,