1
0
mirror of https://github.com/weechat/weechat.git synced 2026-07-08 02:33:12 +02:00

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.
This commit is contained in:
Sébastien Helleu
2016-04-05 07:56:43 +02:00
parent e80ff72b97
commit fabd48cc6c
49 changed files with 1196 additions and 869 deletions
+2 -176
View File
@@ -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);
}
/*