From dd8f1f58a84e37f924a5861676456a3ca5afb0e7 Mon Sep 17 00:00:00 2001 From: nogaems Date: Tue, 2 Apr 2019 14:22:56 +0300 Subject: [PATCH] improve: circumvention of the short ID collisions Even though it's a pretty rare case to happen, it's still possible to occure that there are two+ peers in a group chat with the same first 2 bytes of the ID. Since we're using 2 (that means 4 character length) as a minimal possible value for the shortened ID form, I've changed the ignore list storage in the way that you can now store as many bytes as you want regardless of the option `tox.look.short_id_size`. If the ID collision happens, you can increase the value of that option and you don't have to change your ignore list. --- src/twc-chat.c | 10 +++++++--- src/twc-commands.c | 2 +- src/twc-tox-callbacks.c | 11 ++++++----- src/twc-utils.c | 11 ++++++----- src/twc-utils.h | 3 ++- 5 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/twc-chat.c b/src/twc-chat.c index 657a71d..4da921e 100644 --- a/src/twc-chat.c +++ b/src/twc-chat.c @@ -24,6 +24,7 @@ #include #include +#include "twc-config.h" #include "twc-list.h" #include "twc-message-queue.h" #include "twc-profile.h" @@ -297,10 +298,13 @@ twc_chat_update_prefix(struct t_twc_chat *chat, const char *id, { const char *name_field = weechat_nicklist_nick_get_string( chat->buffer, ptr_nick, "name"); - if (!weechat_strncasecmp(id, name_field + sizeof(char), strlen(id))) + size_t short_id_length = + weechat_config_integer(twc_config_short_id_size); + if (!weechat_strncasecmp(id, name_field + sizeof(char), + short_id_length)) { - weechat_nicklist_nick_set(chat->buffer, ptr_nick, - "prefix", prefix); + weechat_nicklist_nick_set(chat->buffer, ptr_nick, "prefix", + prefix); weechat_nicklist_nick_set(chat->buffer, ptr_nick, "prefix_color", prefix_color); weechat_nicklist_nick_set(chat->buffer, ptr_nick, "color", diff --git a/src/twc-commands.c b/src/twc-commands.c index 44ed2c3..5ad92d4 100644 --- a/src/twc-commands.c +++ b/src/twc-commands.c @@ -1437,7 +1437,7 @@ twc_cmd_ignore(const void *pointer, void *data, struct t_gui_buffer *buffer, const char *id = argv_eol[2]; struct t_weelist_item *item; - item = weechat_list_casesearch(profile->ignores, id); + item = twc_is_id_ignored(profile, id); if (weechat_strcasecmp(argv[1], "add") == 0) { diff --git a/src/twc-tox-callbacks.c b/src/twc-tox-callbacks.c index 918fe42..b1af588 100644 --- a/src/twc-tox-callbacks.c +++ b/src/twc-tox-callbacks.c @@ -533,7 +533,8 @@ twc_group_peer_list_changed_callback(Tox *tox, uint32_t group_number, full_name, NULL, NULL, NULL, 1); nick = weechat_nicklist_search_nick( chat->buffer, chat->nicklist_group, full_name); - bool ignored = twc_is_id_ignored(profile, short_id); + struct t_weelist_item *ignored = + twc_is_id_ignored(profile, short_id); twc_chat_update_prefix_by_nick(chat->buffer, nick, ignored ? "-" : " ", ignored ? "yellow" : "default"); @@ -625,10 +626,10 @@ twc_group_peer_name_callback(Tox *tox, uint32_t group_number, weechat_list_set(n, name); weechat_nicklist_add_nick(chat->buffer, chat->nicklist_group, full_name, NULL, NULL, NULL, 1); - bool ignored = twc_is_id_ignored(profile, short_id); - nick = weechat_nicklist_search_nick(chat->buffer, chat->nicklist_group, full_name); - twc_chat_update_prefix_by_nick(chat->buffer, nick, - ignored ? "-" : " ", + struct t_weelist_item *ignored = twc_is_id_ignored(profile, short_id); + nick = weechat_nicklist_search_nick(chat->buffer, chat->nicklist_group, + full_name); + twc_chat_update_prefix_by_nick(chat->buffer, nick, ignored ? "-" : " ", ignored ? "yellow" : "default"); free(prev_full_name); free(full_name); diff --git a/src/twc-utils.c b/src/twc-utils.c index fa8990c..97d4293 100644 --- a/src/twc-utils.c +++ b/src/twc-utils.c @@ -277,9 +277,10 @@ twc_get_next_completion(struct t_weelist *completion_list, } /** - * Checks if an ID is ignored. + * Checks if an ID is ignored. Returns the item from the ignore list if so, NULL + * otherwise. */ -bool +struct t_weelist_item * twc_is_id_ignored(struct t_twc_profile *profile, const char *short_id) { struct t_weelist_item *ignore_item; @@ -287,10 +288,10 @@ twc_is_id_ignored(struct t_twc_profile *profile, const char *short_id) ignore_item = weechat_list_next(ignore_item)) { if (!weechat_strncasecmp(short_id, weechat_list_string(ignore_item), - strlen(weechat_list_string(ignore_item)))) - return true; + strlen(short_id))) + return ignore_item; } - return false; + return NULL; } /** diff --git a/src/twc-utils.h b/src/twc-utils.h index 377683f..18baa0e 100644 --- a/src/twc-utils.h +++ b/src/twc-utils.h @@ -74,7 +74,8 @@ twc_starts_with(struct t_weelist *list, const char *search, const char * twc_get_next_completion(struct t_weelist *completion_list, const char *prev_comp); -bool + +struct t_weelist_item * twc_is_id_ignored(struct t_twc_profile *profile, const char *short_id); uint32_t