From fabd48cc6cc00527c10072297a62fbd8154251ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Helleu?= Date: Tue, 5 Apr 2016 07:56:43 +0200 Subject: core: move nick coloring from irc plugin to core (closes #262) Options moved from irc.conf to weechat.conf: * "irc.look.nick_color_force" moved to "weechat.look.nick_color_force" * "irc.look.nick_color_hash" moved to "weechat.look.nick_color_hash" * "irc.look.nick_color_stop_chars" moved to "weechat.look.nick_color_stop_chars" New info (for API function "info_get"): * "nick_color" (replaces "irc_nick_color") * "nick_color_name" (replaced "irc_nick_color_name") Info "irc_nick_color" and "irc_nick_color_name" are now deprecated. And a bug has been fixed in nick coloring: stop chars are removed before looking at a forced color. --- src/plugins/irc/irc-nick.c | 178 +-------------------------------------------- 1 file changed, 2 insertions(+), 176 deletions(-) (limited to 'src/plugins/irc/irc-nick.c') diff --git a/src/plugins/irc/irc-nick.c b/src/plugins/irc/irc-nick.c index 308181c76..6f01f48d8 100644 --- a/src/plugins/irc/irc-nick.c +++ b/src/plugins/irc/irc-nick.c @@ -92,130 +92,6 @@ irc_nick_is_nick (const char *string) return 1; } -/* - * Duplicates a nick and stops at first char in list (using option - * irc.look.nick_color_stop_chars). - * - * Note: result must be freed after use. - */ - -char * -irc_nick_strdup_for_color (const char *nickname) -{ - int char_size, other_char_seen; - char *result, *pos, utf_char[16]; - - result = malloc (strlen (nickname) + 1); - pos = result; - other_char_seen = 0; - while (nickname[0]) - { - char_size = weechat_utf8_char_size (nickname); - memcpy (utf_char, nickname, char_size); - utf_char[char_size] = '\0'; - - if (strstr (weechat_config_string (irc_config_look_nick_color_stop_chars), - utf_char)) - { - if (other_char_seen) - { - pos[0] = '\0'; - return result; - } - } - else - { - other_char_seen = 1; - } - memcpy (pos, utf_char, char_size); - pos += char_size; - - nickname += char_size; - } - pos[0] = '\0'; - return result; -} - -/* - * Hashes a nickname to find color. - * - * Returns a number which is the index of color in the nicks colors of option - * "weechat.color.chat_nick_colors". - */ - -int -irc_nick_hash_color (const char *nickname) -{ - unsigned long color; - const char *ptr_nick; - - if (!irc_config_nick_colors) - irc_config_set_nick_colors (); - - if (irc_config_num_nick_colors == 0) - return 0; - - ptr_nick = nickname; - color = 0; - - switch (weechat_config_integer (irc_config_look_nick_color_hash)) - { - case IRC_CONFIG_LOOK_NICK_COLOR_HASH_DJB2: - /* variant of djb2 hash */ - color = 5381; - while (ptr_nick && ptr_nick[0]) - { - color ^= (color << 5) + (color >> 2) + weechat_utf8_char_int (ptr_nick); - ptr_nick = weechat_utf8_next_char (ptr_nick); - } - break; - case IRC_CONFIG_LOOK_NICK_COLOR_HASH_SUM: - /* sum of letters */ - color = 0; - while (ptr_nick && ptr_nick[0]) - { - color += weechat_utf8_char_int (ptr_nick); - ptr_nick = weechat_utf8_next_char (ptr_nick); - } - break; - } - - return (color % irc_config_num_nick_colors); -} - -/* - * Gets forced color for a nick. - * - * Returns the name of color (for example: "green"), NULL if no color is forced - * for nick. - */ - -const char * -irc_nick_get_forced_color (const char *nickname) -{ - const char *forced_color; - char *nick_lower; - - if (!nickname) - return NULL; - - forced_color = weechat_hashtable_get (irc_config_hashtable_nick_color_force, - nickname); - if (forced_color) - return forced_color; - - nick_lower = strdup (nickname); - if (nick_lower) - { - weechat_string_tolower (nick_lower); - forced_color = weechat_hashtable_get (irc_config_hashtable_nick_color_force, - nick_lower); - free (nick_lower); - } - - return forced_color; -} - /* * Finds a color code for a nick (according to nick letters). * @@ -225,34 +101,7 @@ irc_nick_get_forced_color (const char *nickname) const char * irc_nick_find_color (const char *nickname) { - int color; - char *nickname2; - const char *forced_color, *str_color; - - if (!irc_config_nick_colors) - irc_config_set_nick_colors (); - - if (irc_config_num_nick_colors == 0) - return weechat_color ("default"); - - /* look if color is forced */ - forced_color = irc_nick_get_forced_color (nickname); - if (forced_color) - { - forced_color = weechat_color (forced_color); - if (forced_color && forced_color[0]) - return forced_color; - } - - /* hash nickname to get color */ - nickname2 = irc_nick_strdup_for_color (nickname); - color = irc_nick_hash_color ((nickname2) ? nickname2 : nickname); - if (nickname2) - free (nickname2); - - /* return color */ - str_color = weechat_color (irc_config_nick_colors[color]); - return (str_color[0]) ? str_color : weechat_color("default"); + return weechat_info_get ("nick_color", nickname); } /* @@ -264,30 +113,7 @@ irc_nick_find_color (const char *nickname) const char * irc_nick_find_color_name (const char *nickname) { - int color; - char *nickname2; - const char *forced_color; - static char *default_color = "default"; - - if (!irc_config_nick_colors) - irc_config_set_nick_colors (); - - if (irc_config_num_nick_colors == 0) - return default_color; - - /* look if color is forced */ - forced_color = irc_nick_get_forced_color (nickname); - if (forced_color) - return forced_color; - - /* hash nickname to get color */ - nickname2 = irc_nick_strdup_for_color (nickname); - color = irc_nick_hash_color ((nickname2) ? nickname2 : nickname); - if (nickname2) - free (nickname2); - - /* return color name */ - return irc_config_nick_colors[color]; + return weechat_info_get ("nick_color_name", nickname); } /* -- cgit v1.2.3