summaryrefslogtreecommitdiff
path: root/src/plugins
diff options
context:
space:
mode:
authorSébastien Helleu <flashcode@flashtux.org>2016-04-05 07:56:43 +0200
committerSébastien Helleu <flashcode@flashtux.org>2016-04-05 07:56:43 +0200
commitfabd48cc6cc00527c10072297a62fbd8154251ed (patch)
tree9477279254b3198900a3e01060cbe32e8c7649db /src/plugins
parente80ff72b97294e87cb59f715d65ad8879c0101b9 (diff)
downloadweechat-fabd48cc6cc00527c10072297a62fbd8154251ed.zip
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.
Diffstat (limited to 'src/plugins')
-rw-r--r--src/plugins/irc/irc-config.c163
-rw-r--r--src/plugins/irc/irc-config.h14
-rw-r--r--src/plugins/irc/irc-info.c6
-rw-r--r--src/plugins/irc/irc-nick.c178
-rw-r--r--src/plugins/plugin-api.c43
5 files changed, 63 insertions, 341 deletions
diff --git a/src/plugins/irc/irc-config.c b/src/plugins/irc/irc-config.c
index 5365bfe9e..a488936b0 100644
--- a/src/plugins/irc/irc-config.c
+++ b/src/plugins/irc/irc-config.c
@@ -83,9 +83,6 @@ struct t_config_option *irc_config_look_join_auto_add_chantype;
struct t_config_option *irc_config_look_msgbuffer_fallback;
struct t_config_option *irc_config_look_new_channel_position;
struct t_config_option *irc_config_look_new_pv_position;
-struct t_config_option *irc_config_look_nick_color_force;
-struct t_config_option *irc_config_look_nick_color_hash;
-struct t_config_option *irc_config_look_nick_color_stop_chars;
struct t_config_option *irc_config_look_nick_completion_smart;
struct t_config_option *irc_config_look_nick_mode;
struct t_config_option *irc_config_look_nick_mode_empty;
@@ -150,11 +147,9 @@ struct t_config_option *irc_config_network_whois_double_nick;
struct t_config_option *irc_config_server_default[IRC_SERVER_NUM_OPTIONS];
-struct t_hook *irc_config_hook_config_nick_colors = NULL;
-char **irc_config_nick_colors = NULL;
-int irc_config_num_nick_colors = 0;
+struct t_hook *irc_config_hook_config_nick_color_options = NULL;
+struct t_hook *irc_config_hook_config_chat_nick_colors = NULL;
struct t_hashtable *irc_config_hashtable_display_join_message = NULL;
-struct t_hashtable *irc_config_hashtable_nick_color_force = NULL;
struct t_hashtable *irc_config_hashtable_nick_prefixes = NULL;
struct t_hashtable *irc_config_hashtable_color_mirc_remap = NULL;
char **irc_config_nicks_hide_password = NULL;
@@ -234,28 +229,6 @@ irc_config_compute_nick_colors ()
}
/*
- * Sets nick colors using option "weechat.color.chat_nick_colors".
- */
-
-void
-irc_config_set_nick_colors ()
-{
- if (irc_config_nick_colors)
- {
- weechat_string_free_split (irc_config_nick_colors);
- irc_config_nick_colors = NULL;
- irc_config_num_nick_colors = 0;
- }
-
- irc_config_nick_colors =
- weechat_string_split (
- weechat_config_string (
- weechat_config_get ("weechat.color.chat_nick_colors")),
- ",", 0, 0,
- &irc_config_num_nick_colors);
-}
-
-/*
* Checks if channel modes arguments must be displayed or hidden
* (according to option irc.look.item_channel_modes_hide_args).
*
@@ -295,7 +268,7 @@ irc_config_display_channel_modes_arguments (const char *modes)
}
/*
- * Callback for changes on option "weechat.color.chat_nick_colors".
+ * Callback for changes on options changing nick colors.
*/
int
@@ -308,7 +281,6 @@ irc_config_change_nick_colors_cb (const void *pointer, void *data,
(void) option;
(void) value;
- irc_config_set_nick_colors ();
irc_config_compute_nick_colors ();
return WEECHAT_RC_OK;
@@ -566,71 +538,6 @@ irc_config_change_look_highlight_tags_restrict (const void *pointer, void *data,
}
/*
- * Callback for changes on option "irc.look.nick_color_force".
- */
-
-void
-irc_config_change_look_nick_color_force (const void *pointer, void *data,
- struct t_config_option *option)
-{
- char **items, *pos;
- int num_items, i;
-
- /* make C compiler happy */
- (void) pointer;
- (void) data;
- (void) option;
-
- if (!irc_config_hashtable_nick_color_force)
- {
- irc_config_hashtable_nick_color_force = weechat_hashtable_new (
- 32,
- WEECHAT_HASHTABLE_STRING,
- WEECHAT_HASHTABLE_STRING,
- NULL, NULL);
- }
- else
- weechat_hashtable_remove_all (irc_config_hashtable_nick_color_force);
-
- items = weechat_string_split (
- weechat_config_string (irc_config_look_nick_color_force),
- ";", 0, 0, &num_items);
- if (items)
- {
- for (i = 0; i < num_items; i++)
- {
- pos = strchr (items[i], ':');
- if (pos)
- {
- pos[0] = '\0';
- weechat_hashtable_set (irc_config_hashtable_nick_color_force,
- items[i],
- pos + 1);
- }
- }
- weechat_string_free_split (items);
- }
-
- irc_config_compute_nick_colors ();
-}
-
-/*
- * Callback for changes on options that change nick colors.
- */
-
-void
-irc_config_change_look_nick_colors (const void *pointer, void *data,
- struct t_config_option *option)
-{
- /* make C compiler happy */
- (void) pointer;
- (void) data;
- (void) option;
-
- irc_config_compute_nick_colors ();
-}
-
-/*
* Callback for changes on option "irc.look.item_display_server".
*/
@@ -2530,11 +2437,6 @@ irc_config_init ()
WEECHAT_HASHTABLE_STRING,
WEECHAT_HASHTABLE_STRING,
NULL, NULL);
- irc_config_hashtable_nick_color_force = weechat_hashtable_new (
- 32,
- WEECHAT_HASHTABLE_STRING,
- WEECHAT_HASHTABLE_STRING,
- NULL, NULL);
irc_config_hashtable_nick_prefixes = weechat_hashtable_new (
32,
WEECHAT_HASHTABLE_STRING,
@@ -2821,39 +2723,6 @@ irc_config_init ()
"of server)"),
"none|next|near_server", 0, 0, "none", NULL, 0,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
- irc_config_look_nick_color_force = weechat_config_new_option (
- irc_config_file, ptr_section,
- "nick_color_force", "string",
- N_("force color for some nicks: hash computed with nickname "
- "to find color will not be used for these nicks (format is: "
- "\"nick1:color1;nick2:color2\"); look up for nicks is with "
- "exact case then lower case, so it's possible to use only lower "
- "case for nicks in this option"),
- NULL, 0, 0, "", NULL, 0,
- NULL, NULL, NULL,
- &irc_config_change_look_nick_color_force, NULL, NULL,
- NULL, NULL, NULL);
- irc_config_look_nick_color_hash = weechat_config_new_option (
- irc_config_file, ptr_section,
- "nick_color_hash", "integer",
- N_("hash algorithm used to find the color for a nick: djb2 = variant "
- "of djb2 (position of letters matters: anagrams of a nick have "
- "different color), sum = sum of letters"),
- "djb2|sum", 0, 0, "sum", NULL, 0,
- NULL, NULL, NULL,
- &irc_config_change_look_nick_colors, NULL, NULL,
- NULL, NULL, NULL);
- irc_config_look_nick_color_stop_chars = weechat_config_new_option (
- irc_config_file, ptr_section,
- "nick_color_stop_chars", "string",
- N_("chars used to stop in nick when computing color with letters of "
- "nick (at least one char outside this list must be in string before "
- "stopping) (example: nick \"|nick|away\" with \"|\" in chars will "
- "return color of nick \"|nick\")"),
- NULL, 0, 0, "_|[", NULL, 0,
- NULL, NULL, NULL,
- &irc_config_change_look_nick_colors, NULL, NULL,
- NULL, NULL, NULL);
irc_config_look_nick_completion_smart = weechat_config_new_option (
irc_config_file, ptr_section,
"nick_completion_smart", "integer",
@@ -3396,7 +3265,10 @@ irc_config_init ()
}
irc_config_section_server = ptr_section;
- irc_config_hook_config_nick_colors = weechat_hook_config (
+ irc_config_hook_config_nick_color_options = weechat_hook_config (
+ "weechat.look.nick_color_*",
+ &irc_config_change_nick_colors_cb, NULL, NULL);
+ irc_config_hook_config_chat_nick_colors = weechat_hook_config (
"weechat.color.chat_nick_colors",
&irc_config_change_nick_colors_cb, NULL, NULL);
@@ -3420,7 +3292,6 @@ irc_config_read ()
{
irc_notify_new_for_all_servers ();
irc_config_change_look_display_join_message (NULL, NULL, NULL);
- irc_config_change_look_nick_color_force (NULL, NULL, NULL);
irc_config_change_look_nicks_hide_password (NULL, NULL, NULL);
irc_config_change_color_nick_prefixes (NULL, NULL, NULL);
irc_config_change_color_mirc_remap (NULL, NULL, NULL);
@@ -3452,16 +3323,16 @@ irc_config_free ()
{
weechat_config_free (irc_config_file);
- if (irc_config_hook_config_nick_colors)
+ if (irc_config_hook_config_nick_color_options)
{
- weechat_unhook (irc_config_hook_config_nick_colors);
- irc_config_hook_config_nick_colors = NULL;
+ weechat_unhook (irc_config_hook_config_nick_color_options);
+ irc_config_hook_config_nick_color_options = NULL;
}
- if (irc_config_nick_colors)
+
+ if (irc_config_hook_config_chat_nick_colors)
{
- weechat_string_free_split (irc_config_nick_colors);
- irc_config_nick_colors = NULL;
- irc_config_num_nick_colors = 0;
+ weechat_unhook (irc_config_hook_config_chat_nick_colors);
+ irc_config_hook_config_chat_nick_colors = NULL;
}
if (irc_config_nicks_hide_password)
@@ -3477,12 +3348,6 @@ irc_config_free ()
irc_config_hashtable_display_join_message = NULL;
}
- if (irc_config_hashtable_nick_color_force)
- {
- weechat_hashtable_free (irc_config_hashtable_nick_color_force);
- irc_config_hashtable_nick_color_force = NULL;
- }
-
if (irc_config_hashtable_nick_prefixes)
{
weechat_hashtable_free (irc_config_hashtable_nick_prefixes);
diff --git a/src/plugins/irc/irc-config.h b/src/plugins/irc/irc-config.h
index dc75af49e..ca1f5b531 100644
--- a/src/plugins/irc/irc-config.h
+++ b/src/plugins/irc/irc-config.h
@@ -62,12 +62,6 @@ enum t_irc_config_look_notice_as_pv
IRC_CONFIG_LOOK_NOTICE_AS_PV_ALWAYS,
};
-enum t_irc_config_look_nick_color_hash
-{
- IRC_CONFIG_LOOK_NICK_COLOR_HASH_DJB2 = 0,
- IRC_CONFIG_LOOK_NICK_COLOR_HASH_SUM,
-};
-
enum t_irc_config_look_nick_mode
{
IRC_CONFIG_LOOK_NICK_MODE_NONE = 0,
@@ -128,9 +122,6 @@ extern struct t_config_option *irc_config_look_join_auto_add_chantype;
extern struct t_config_option *irc_config_look_msgbuffer_fallback;
extern struct t_config_option *irc_config_look_new_channel_position;
extern struct t_config_option *irc_config_look_new_pv_position;
-extern struct t_config_option *irc_config_look_nick_color_force;
-extern struct t_config_option *irc_config_look_nick_color_hash;
-extern struct t_config_option *irc_config_look_nick_color_stop_chars;
extern struct t_config_option *irc_config_look_nick_completion_smart;
extern struct t_config_option *irc_config_look_nick_mode;
extern struct t_config_option *irc_config_look_nick_mode_empty;
@@ -189,17 +180,12 @@ extern struct t_config_option *irc_config_network_whois_double_nick;
extern struct t_config_option *irc_config_server_default[];
-extern char **irc_config_nick_colors;
-extern int irc_config_num_nick_colors;
-
extern struct t_hashtable *irc_config_hashtable_display_join_message;
-extern struct t_hashtable *irc_config_hashtable_nick_color_force;
extern struct t_hashtable *irc_config_hashtable_nick_prefixes;
extern struct t_hashtable *irc_config_hashtable_color_mirc_remap;
extern char **irc_config_nicks_hide_password;
extern int irc_config_num_nicks_hide_password;
-extern void irc_config_set_nick_colors ();
extern int irc_config_display_channel_modes_arguments (const char *modes);
extern int irc_config_server_check_value_cb (const void *pointer, void *data,
struct t_config_option *option,
diff --git a/src/plugins/irc/irc-info.c b/src/plugins/irc/irc-info.c
index 7133b21a1..d4f75439d 100644
--- a/src/plugins/irc/irc-info.c
+++ b/src/plugins/irc/irc-info.c
@@ -847,12 +847,14 @@ irc_info_init ()
&irc_info_info_irc_nick_from_host_cb, NULL, NULL);
weechat_hook_info (
"irc_nick_color",
- N_("get nick color code"),
+ N_("get nick color code "
+ "(*deprecated* since version 1.5, replaced by \"nick_color\")"),
N_("nickname"),
&irc_info_info_irc_nick_color_cb, NULL, NULL);
weechat_hook_info (
"irc_nick_color_name",
- N_("get nick color name"),
+ N_("get nick color name "
+ "(*deprecated* since version 1.5, replaced by \"nick_color_name\")"),
N_("nickname"),
&irc_info_info_irc_nick_color_name_cb, NULL, NULL);
weechat_hook_info (
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
@@ -93,130 +93,6 @@ irc_nick_is_nick (const char *string)
}
/*
- * 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).
*
* Returns a WeeChat color code (that can be used for display).
@@ -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);
}
/*
diff --git a/src/plugins/plugin-api.c b/src/plugins/plugin-api.c
index 4be88b077..cc4e4b0a9 100644
--- a/src/plugins/plugin-api.c
+++ b/src/plugins/plugin-api.c
@@ -59,6 +59,7 @@
#include "../gui/gui-key.h"
#include "../gui/gui-layout.h"
#include "../gui/gui-line.h"
+#include "../gui/gui-nick.h"
#include "../gui/gui-nicklist.h"
#include "../gui/gui-window.h"
#include "plugin.h"
@@ -786,6 +787,40 @@ plugin_api_info_color_rgb2term_cb (const void *pointer, void *data,
}
/*
+ * Returns nick color code for a nickname.
+ */
+
+const char *
+plugin_api_info_nick_color_cb (const void *pointer, void *data,
+ const char *info_name,
+ const char *arguments)
+{
+ /* make C compiler happy */
+ (void) pointer;
+ (void) data;
+ (void) info_name;
+
+ return gui_nick_find_color (arguments);
+}
+
+/*
+ * Returns nick color name for a nickname.
+ */
+
+const char *
+plugin_api_info_nick_color_name_cb (const void *pointer, void *data,
+ const char *info_name,
+ const char *arguments)
+{
+ /* make C compiler happy */
+ (void) pointer;
+ (void) data;
+ (void) info_name;
+
+ return gui_nick_find_color_name (arguments);
+}
+
+/*
* Returns WeeChat infolist "bar".
*
* Note: result must be freed after use with function weechat_infolist_free().
@@ -1855,6 +1890,14 @@ plugin_api_init ()
N_("RGB color converted to terminal color (0-255)"),
N_("rgb,limit (limit is optional and is set to 256 by default)"),
&plugin_api_info_color_rgb2term_cb, NULL, NULL);
+ hook_info (NULL, "nick_color",
+ N_("get nick color code"),
+ N_("nickname"),
+ &plugin_api_info_nick_color_cb, NULL, NULL);
+ hook_info (NULL, "nick_color_name",
+ N_("get nick color name"),
+ N_("nickname"),
+ &plugin_api_info_nick_color_name_cb, NULL, NULL);
/* WeeChat core infolist hooks */
hook_infolist (NULL, "bar",