summaryrefslogtreecommitdiff
path: root/src
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
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')
-rw-r--r--src/core/wee-config.c122
-rw-r--r--src/core/wee-config.h13
-rw-r--r--src/gui/CMakeLists.txt2
-rw-r--r--src/gui/Makefile.am2
-rw-r--r--src/gui/curses/CMakeLists.txt2
-rw-r--r--src/gui/gui-nick.c259
-rw-r--r--src/gui/gui-nick.h26
-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
12 files changed, 488 insertions, 342 deletions
diff --git a/src/core/wee-config.c b/src/core/wee-config.c
index 798bfea7a..e17743090 100644
--- a/src/core/wee-config.c
+++ b/src/core/wee-config.c
@@ -37,6 +37,7 @@
#include "weechat.h"
#include "wee-config.h"
+#include "wee-hashtable.h"
#include "wee-hook.h"
#include "wee-log.h"
#include "wee-network.h"
@@ -148,6 +149,9 @@ struct t_config_option *config_look_key_bind_safe;
struct t_config_option *config_look_key_grab_delay;
struct t_config_option *config_look_mouse;
struct t_config_option *config_look_mouse_timer_delay;
+struct t_config_option *config_look_nick_color_force;
+struct t_config_option *config_look_nick_color_hash;
+struct t_config_option *config_look_nick_color_stop_chars;
struct t_config_option *config_look_nick_prefix;
struct t_config_option *config_look_nick_suffix;
struct t_config_option *config_look_paste_auto_add_newline;
@@ -307,6 +311,9 @@ struct t_config_look_word_char_item *config_word_chars_highlight = NULL;
int config_word_chars_highlight_count = 0;
struct t_config_look_word_char_item *config_word_chars_input = NULL;
int config_word_chars_input_count = 0;
+char **config_nick_colors = NULL;
+int config_num_nick_colors = 0;
+struct t_hashtable *config_hashtable_nick_color_force = NULL;
/*
@@ -660,6 +667,74 @@ config_compute_prefix_max_length_all_buffers ()
}
/*
+ * Sets nick colors using option "weechat.color.chat_nick_colors".
+ */
+
+void
+config_set_nick_colors ()
+{
+ if (config_nick_colors)
+ {
+ string_free_split (config_nick_colors);
+ config_nick_colors = NULL;
+ config_num_nick_colors = 0;
+ }
+
+ config_nick_colors = string_split (
+ CONFIG_STRING(config_color_chat_nick_colors),
+ ",", 0, 0,
+ &config_num_nick_colors);
+}
+
+/*
+ * Callback for changes on option "weechat.look.nick_color_force".
+ */
+
+void
+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 (!config_hashtable_nick_color_force)
+ {
+ config_hashtable_nick_color_force = hashtable_new (
+ 32,
+ WEECHAT_HASHTABLE_STRING,
+ WEECHAT_HASHTABLE_STRING,
+ NULL, NULL);
+ }
+ else
+ {
+ hashtable_remove_all (config_hashtable_nick_color_force);
+ }
+
+ items = string_split (CONFIG_STRING(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';
+ hashtable_set (config_hashtable_nick_color_force,
+ items[i],
+ pos + 1);
+ }
+ }
+ string_free_split (items);
+ }
+}
+
+/*
* Callback for changes on options "weechat.look.nick_prefix" and
* "weechat.look.nick_suffix".
*/
@@ -1047,6 +1122,7 @@ config_change_nick_colors (const void *pointer, void *data,
(void) data;
(void) option;
+ config_set_nick_colors ();
gui_color_buffer_display ();
}
@@ -1208,6 +1284,8 @@ config_weechat_init_after_read ()
/* apply filters on all buffers */
gui_filter_all_buffers ();
+
+ config_change_look_nick_color_force (NULL, NULL, NULL);
}
/*
@@ -2926,6 +3004,37 @@ config_weechat_init_options ()
"wait this delay before processing event"),
NULL, 1, 10000, "100", NULL, 0,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
+ config_look_nick_color_force = config_file_new_option (
+ weechat_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,
+ &config_change_look_nick_color_force, NULL, NULL,
+ NULL, NULL, NULL);
+ config_look_nick_color_hash = config_file_new_option (
+ weechat_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, NULL, NULL, NULL, NULL, NULL, NULL);
+ config_look_nick_color_stop_chars = config_file_new_option (
+ weechat_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,
+ NULL, NULL, NULL,
+ NULL, NULL, NULL);
config_look_nick_prefix = config_file_new_option (
weechat_config_file, ptr_section,
"nick_prefix", "string",
@@ -4387,4 +4496,17 @@ config_weechat_free ()
config_word_chars_input = NULL;
config_word_chars_input_count = 0;
}
+
+ if (config_nick_colors)
+ {
+ string_free_split (config_nick_colors);
+ config_nick_colors = NULL;
+ config_num_nick_colors = 0;
+ }
+
+ if (config_hashtable_nick_color_force)
+ {
+ hashtable_free (config_hashtable_nick_color_force);
+ config_hashtable_nick_color_force = NULL;
+ }
}
diff --git a/src/core/wee-config.h b/src/core/wee-config.h
index 9681b03cb..e4c4a3d33 100644
--- a/src/core/wee-config.h
+++ b/src/core/wee-config.h
@@ -53,6 +53,12 @@ enum t_config_look_buffer_search_where
CONFIG_LOOK_BUFFER_SEARCH_PREFIX_MESSAGE,
};
+enum t_config_look_nick_color_hash
+{
+ CONFIG_LOOK_NICK_COLOR_HASH_DJB2 = 0,
+ CONFIG_LOOK_NICK_COLOR_HASH_SUM,
+};
+
enum t_config_look_prefix_align
{
CONFIG_LOOK_PREFIX_ALIGN_NONE = 0,
@@ -193,6 +199,9 @@ extern struct t_config_option *config_look_key_bind_safe;
extern struct t_config_option *config_look_key_grab_delay;
extern struct t_config_option *config_look_mouse;
extern struct t_config_option *config_look_mouse_timer_delay;
+extern struct t_config_option *config_look_nick_color_force;
+extern struct t_config_option *config_look_nick_color_hash;
+extern struct t_config_option *config_look_nick_color_stop_chars;
extern struct t_config_option *config_look_nick_prefix;
extern struct t_config_option *config_look_nick_suffix;
extern struct t_config_option *config_look_paste_auto_add_newline;
@@ -338,7 +347,11 @@ extern struct t_config_look_word_char_item *config_word_chars_highlight;
extern int config_word_chars_highlight_count;
extern struct t_config_look_word_char_item *config_word_chars_input;
extern int config_word_chars_input_count;
+extern char **config_nick_colors;
+extern int config_num_nick_colors;
+extern struct t_hashtable *config_hashtable_nick_color_force;
+extern void config_set_nick_colors ();
extern struct t_config_option *config_weechat_debug_get (const char *plugin_name);
extern int config_weechat_debug_set (const char *plugin_name,
const char *value);
diff --git a/src/gui/CMakeLists.txt b/src/gui/CMakeLists.txt
index 6a4d89156..26da7e7f0 100644
--- a/src/gui/CMakeLists.txt
+++ b/src/gui/CMakeLists.txt
@@ -36,11 +36,13 @@ gui-layout.c gui-layout.h
gui-line.c gui-line.h
gui-main.h
gui-mouse.c gui-mouse.h
+gui-nick.c gui-nick.h
gui-nicklist.c gui-nicklist.h
gui-window.c gui-window.h)
include_directories(${CMAKE_BINARY_DIR})
add_library(weechat_gui_common STATIC ${LIB_GUI_COMMON_SRC})
+list(APPEND STATIC_LIBS weechat_gui_common)
if(ENABLE_NCURSES)
subdirs(curses)
diff --git a/src/gui/Makefile.am b/src/gui/Makefile.am
index 6ecaca6f8..528a7a66f 100644
--- a/src/gui/Makefile.am
+++ b/src/gui/Makefile.am
@@ -56,6 +56,8 @@ lib_weechat_gui_common_a_SOURCES = gui-bar.c \
gui-main.h \
gui-mouse.c \
gui-mouse.h \
+ gui-nick.c \
+ gui-nick.h \
gui-nicklist.c \
gui-nicklist.h \
gui-window.c \
diff --git a/src/gui/curses/CMakeLists.txt b/src/gui/curses/CMakeLists.txt
index 81af7bd34..8fadce2d2 100644
--- a/src/gui/curses/CMakeLists.txt
+++ b/src/gui/curses/CMakeLists.txt
@@ -81,7 +81,7 @@ add_executable(${EXECUTABLE} ${WEECHAT_CURSES_MAIN_SRC})
add_dependencies(${EXECUTABLE} weechat_gui_curses)
-# Due to circular references, we must link two times with libweechat_core.a
+# Due to circular references, we must link two times with libweechat_core.a and libweechat_gui_common.a
target_link_libraries(${EXECUTABLE} ${STATIC_LIBS} weechat_gui_curses ${EXTRA_LIBS} ${STATIC_LIBS})
# Create a symbolic link weechat-curses -> weechat
diff --git a/src/gui/gui-nick.c b/src/gui/gui-nick.c
new file mode 100644
index 000000000..899ee444f
--- /dev/null
+++ b/src/gui/gui-nick.c
@@ -0,0 +1,259 @@
+/*
+ * gui-nick.c - nick functions (used by all GUI)
+ *
+ * Copyright (C) 2003-2016 Sébastien Helleu <flashcode@flashtux.org>
+ *
+ * This file is part of WeeChat, the extensible chat client.
+ *
+ * WeeChat is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * WeeChat is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with WeeChat. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <string.h>
+
+#include "../core/weechat.h"
+#include "../core/wee-config.h"
+#include "../core/wee-hashtable.h"
+#include "../core/wee-string.h"
+#include "../core/wee-utf8.h"
+#include "gui-nick.h"
+#include "gui-color.h"
+
+
+/*
+ * 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
+gui_nick_hash_color (const char *nickname)
+{
+ unsigned long color;
+ const char *ptr_nick;
+
+ if (!nickname || !nickname[0])
+ return 0;
+
+ if (!config_nick_colors)
+ config_set_nick_colors ();
+
+ if (config_num_nick_colors == 0)
+ return 0;
+
+ ptr_nick = nickname;
+ color = 0;
+
+ switch (CONFIG_INTEGER(config_look_nick_color_hash))
+ {
+ case CONFIG_LOOK_NICK_COLOR_HASH_DJB2:
+ /* variant of djb2 hash */
+ color = 5381;
+ while (ptr_nick && ptr_nick[0])
+ {
+ color ^= (color << 5) + (color >> 2) + utf8_char_int (ptr_nick);
+ ptr_nick = utf8_next_char (ptr_nick);
+ }
+ break;
+ case CONFIG_LOOK_NICK_COLOR_HASH_SUM:
+ /* sum of letters */
+ color = 0;
+ while (ptr_nick && ptr_nick[0])
+ {
+ color += utf8_char_int (ptr_nick);
+ ptr_nick = utf8_next_char (ptr_nick);
+ }
+ break;
+ }
+
+ return (color % 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 *
+gui_nick_get_forced_color (const char *nickname)
+{
+ const char *forced_color;
+ char *nick_lower;
+
+ if (!nickname || !nickname[0])
+ return NULL;
+
+ forced_color = hashtable_get (config_hashtable_nick_color_force, nickname);
+ if (forced_color)
+ return forced_color;
+
+ nick_lower = strdup (nickname);
+ if (nick_lower)
+ {
+ string_tolower (nick_lower);
+ forced_color = hashtable_get (config_hashtable_nick_color_force,
+ nick_lower);
+ free (nick_lower);
+ }
+
+ return forced_color;
+}
+
+/*
+ * Duplicates a nick and stops at first char in list (using option
+ * weechat.look.nick_color_stop_chars).
+ *
+ * Note: result must be freed after use.
+ */
+
+char *
+gui_nick_strdup_for_color (const char *nickname)
+{
+ int char_size, other_char_seen;
+ char *result, *pos, utf_char[16];
+
+ if (!nickname)
+ return NULL;
+
+ result = malloc (strlen (nickname) + 1);
+ pos = result;
+ other_char_seen = 0;
+ while (nickname[0])
+ {
+ char_size = utf8_char_size (nickname);
+ memcpy (utf_char, nickname, char_size);
+ utf_char[char_size] = '\0';
+
+ if (strstr (CONFIG_STRING(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;
+}
+
+/*
+ * Finds a color code for a nick (according to nick letters).
+ *
+ * Returns a WeeChat color code (that can be used for display).
+ */
+
+const char *
+gui_nick_find_color (const char *nickname)
+{
+ int color;
+ char *nickname2;
+ const char *forced_color, *str_color;
+
+ if (!nickname || !nickname[0])
+ return gui_color_get_custom ("default");
+
+ if (!config_nick_colors)
+ config_set_nick_colors ();
+
+ if (config_num_nick_colors == 0)
+ return gui_color_get_custom ("default");
+
+ nickname2 = gui_nick_strdup_for_color (nickname);
+
+ /* look if color is forced */
+ forced_color = gui_nick_get_forced_color (
+ (nickname2) ? nickname2 : nickname);
+ if (forced_color)
+ {
+ forced_color = gui_color_get_custom (forced_color);
+ if (forced_color && forced_color[0])
+ {
+ if (nickname2)
+ free (nickname2);
+ return forced_color;
+ }
+ }
+
+ /* hash nickname to get color */
+ color = gui_nick_hash_color ((nickname2) ? nickname2 : nickname);
+
+ if (nickname2)
+ free (nickname2);
+
+ /* return color */
+ str_color = gui_color_get_custom (config_nick_colors[color]);
+ return (str_color[0]) ? str_color : gui_color_get_custom ("default");
+}
+
+/*
+ * Finds a color name for a nick (according to nick letters).
+ *
+ * Returns the name of a color (for example: "green").
+ */
+
+const char *
+gui_nick_find_color_name (const char *nickname)
+{
+ int color;
+ char *nickname2;
+ const char *forced_color;
+ static char *default_color = "default";
+
+ if (!nickname || !nickname[0])
+ return default_color;
+
+ if (!config_nick_colors)
+ config_set_nick_colors ();
+
+ if (config_num_nick_colors == 0)
+ return default_color;
+
+ nickname2 = gui_nick_strdup_for_color (nickname);
+
+ /* look if color is forced */
+ forced_color = gui_nick_get_forced_color (
+ (nickname2) ? nickname2 : nickname);
+ if (forced_color)
+ {
+ if (nickname2)
+ free (nickname2);
+ return forced_color;
+ }
+
+ /* hash nickname to get color */
+ color = gui_nick_hash_color ((nickname2) ? nickname2 : nickname);
+
+ if (nickname2)
+ free (nickname2);
+
+ /* return color name */
+ return config_nick_colors[color];
+}
diff --git a/src/gui/gui-nick.h b/src/gui/gui-nick.h
new file mode 100644
index 000000000..7c6bf896f
--- /dev/null
+++ b/src/gui/gui-nick.h
@@ -0,0 +1,26 @@
+/*
+ * Copyright (C) 2003-2016 Sébastien Helleu <flashcode@flashtux.org>
+ *
+ * This file is part of WeeChat, the extensible chat client.
+ *
+ * WeeChat is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * WeeChat is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with WeeChat. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef WEECHAT_GUI_NICK_H
+#define WEECHAT_GUI_NICK_H 1
+
+extern const char *gui_nick_find_color (const char *nickname);
+extern const char *gui_nick_find_color_name (const char *nickname);
+
+#endif /* WEECHAT_GUI_NICK_H */
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",