diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/core/wee-string.c | 64 | ||||
-rw-r--r-- | src/core/wee-string.h | 2 | ||||
-rw-r--r-- | src/gui/gui-nick.c | 36 | ||||
-rw-r--r-- | src/gui/gui-nick.h | 4 | ||||
-rw-r--r-- | src/plugins/irc/irc-info.c | 10 | ||||
-rw-r--r-- | src/plugins/irc/irc-nick.c | 38 | ||||
-rw-r--r-- | src/plugins/irc/irc-server.c | 66 | ||||
-rw-r--r-- | src/plugins/irc/irc-server.h | 5 | ||||
-rw-r--r-- | src/plugins/plugin-api-info.c | 100 |
9 files changed, 228 insertions, 97 deletions
diff --git a/src/core/wee-string.c b/src/core/wee-string.c index 83ee9e63c..ab0bee82e 100644 --- a/src/core/wee-string.c +++ b/src/core/wee-string.c @@ -395,6 +395,70 @@ string_toupper (const char *string) } /* + * Converts string to lower case (using a range of chars). + * + * Note: result must be freed after use. + */ + +char * +string_tolower_range (const char *string, int range) +{ + char *result, *ptr_result; + + if (!string) + return NULL; + + if (range <= 0) + return string_tolower (string); + + result = strdup (string); + if (!result) + return NULL; + + ptr_result = result; + while (ptr_result && ptr_result[0]) + { + if ((ptr_result[0] >= 'A') && (ptr_result[0] < 'A' + range)) + ptr_result[0] += ('a' - 'A'); + ptr_result = (char *)utf8_next_char (ptr_result); + } + + return result; +} + +/* + * Converts string to upper case (using a range of char). + * + * Note: result must be freed after use. + */ + +char * +string_toupper_range (const char *string, int range) +{ + char *result, *ptr_result; + + if (!string) + return NULL; + + if (range <= 0) + return string_toupper (string); + + result = strdup (string); + if (!result) + return NULL; + + ptr_result = result; + while (ptr_result && ptr_result[0]) + { + if ((ptr_result[0] >= 'a') && (ptr_result[0] < 'a' + range)) + ptr_result[0] -= ('a' - 'A'); + ptr_result = (char *)utf8_next_char (ptr_result); + } + + return result; +} + +/* * Compares two chars (case sensitive). * * Returns: arithmetic result of subtracting the first UTF-8 char in string2 diff --git a/src/core/wee-string.h b/src/core/wee-string.h index 19c616e5e..522d3e440 100644 --- a/src/core/wee-string.h +++ b/src/core/wee-string.h @@ -44,6 +44,8 @@ extern char *string_reverse_screen (const char *string); extern char *string_repeat (const char *string, int count); extern char *string_tolower (const char *string); extern char *string_toupper (const char *string); +extern char *string_tolower_range (const char *string, int range); +extern char *string_toupper_range (const char *string, int range); extern int string_charcmp (const char *string1, const char *string2); extern int string_charcasecmp (const char *string1, const char *string2); extern int string_charcasecmp_range (const char *string1, const char *string2, diff --git a/src/gui/gui-nick.c b/src/gui/gui-nick.c index 75712a654..6b4152664 100644 --- a/src/gui/gui-nick.c +++ b/src/gui/gui-nick.c @@ -243,6 +243,10 @@ gui_nick_strdup_for_color (const char *nickname) /* * Finds a color name for a nick (according to nick letters). * + * If case_range < 0, nick is case sensitive. + * If case_range == 0, nick is converted to lower case (with string_tolower). + * If case_range > 0, nick is converted to lower case (with string_tolower_range). + * * If colors is NULL (most common case), the color returned is either a forced * color (from option "weechat.look.nick_color_force") or a color from option * "weechat.color.chat_nick_colors". @@ -258,16 +262,18 @@ gui_nick_strdup_for_color (const char *nickname) */ char * -gui_nick_find_color_name (const char *nickname, const char *colors) +gui_nick_find_color_name (const char *nickname, int case_range, + const char *colors) { int color, num_colors; - char *nickname2, **list_colors, *result; + char *nickname2, *nickname3, **list_colors, *result; const char *forced_color, *ptr_result; static char *default_color = "default"; list_colors = NULL; num_colors = 0; nickname2 = NULL; + nickname3 = NULL; ptr_result = NULL; if (!nickname || !nickname[0]) @@ -281,12 +287,13 @@ gui_nick_find_color_name (const char *nickname, const char *colors) } nickname2 = gui_nick_strdup_for_color (nickname); + if (!nickname2) + goto end; if (!list_colors) { /* look if color is forced for the nick */ - forced_color = gui_nick_get_forced_color ( - (nickname2) ? nickname2 : nickname); + forced_color = gui_nick_get_forced_color (nickname2); if (forced_color) { ptr_result = forced_color; @@ -299,9 +306,18 @@ gui_nick_find_color_name (const char *nickname, const char *colors) goto end; } + if (case_range < 0) + nickname3 = strdup (nickname2); + else if (case_range == 0) + nickname3 = string_tolower (nickname2); + else + nickname3 = string_tolower_range (nickname2, case_range); + if (!nickname3) + goto end; + /* hash nickname to get color */ color = gui_nick_hash_color ( - (nickname2) ? nickname2 : nickname, + nickname3, (list_colors) ? num_colors : config_num_nick_colors); ptr_result = (list_colors) ? list_colors[color] : config_nick_colors[color]; @@ -312,12 +328,18 @@ end: string_free_split (list_colors); if (nickname2) free (nickname2); + if (nickname3) + free (nickname3); return result; } /* * Finds a color code for a nick (according to nick letters). * + * If case_range < 0, nick is case sensitive. + * If case_range == 0, nick is converted to lower case (with string_tolower). + * If case_range > 0, nick is converted to lower case (with string_tolower_range). + * * If colors is NULL (most common case), the color returned is either a forced * color (from option "weechat.look.nick_color_force") or a color from option * "weechat.color.chat_nick_colors". @@ -333,12 +355,12 @@ end: */ char * -gui_nick_find_color (const char *nickname, const char *colors) +gui_nick_find_color (const char *nickname, int case_range, const char *colors) { char *color; const char *ptr_result; - color = gui_nick_find_color_name (nickname, colors); + color = gui_nick_find_color_name (nickname, case_range, colors); ptr_result = gui_color_get_custom (color); if (color) free (color); diff --git a/src/gui/gui-nick.h b/src/gui/gui-nick.h index 45d88afa5..64f7b2c18 100644 --- a/src/gui/gui-nick.h +++ b/src/gui/gui-nick.h @@ -20,9 +20,9 @@ #ifndef WEECHAT_GUI_NICK_H #define WEECHAT_GUI_NICK_H -extern char *gui_nick_find_color_name (const char *nickname, +extern char *gui_nick_find_color_name (const char *nickname, int case_range, const char *colors); -extern char *gui_nick_find_color (const char *nickname, +extern char *gui_nick_find_color (const char *nickname, int case_range, const char *colors); #endif /* WEECHAT_GUI_NICK_H */ diff --git a/src/plugins/irc/irc-info.c b/src/plugins/irc/irc-info.c index e3318d416..ebd0265cd 100644 --- a/src/plugins/irc/irc-info.c +++ b/src/plugins/irc/irc-info.c @@ -1259,15 +1259,17 @@ irc_info_init () &irc_info_info_irc_nick_from_host_cb, NULL, NULL); weechat_hook_info ( "irc_nick_color", - N_("get nick color code (nick is first converted to lower case, " - "following the value of CASEMAPPING on the server, " + N_("get nick color code, ignoring case (this calls the info " + "\"nick_color_ignore_case\" with appropriate range, according " + "to the value of CASEMAPPING on the server, " "defaulting to \"rfc1459\" if the server is not given)"), N_("server,nickname (server is optional)"), &irc_info_info_irc_nick_color_cb, NULL, NULL); weechat_hook_info ( "irc_nick_color_name", - N_("get nick color name (nick is first converted to lower case, " - "following the value of CASEMAPPING on the server, " + N_("get nick color name, ignoring case (this calls the info " + "\"nick_color_name_ignore_case\" with appropriate range, according " + "to the value of CASEMAPPING on the server, " "defaulting to \"rfc1459\" if the server is not given)"), N_("server,nickname (server is optional)"), &irc_info_info_irc_nick_color_name_cb, NULL, NULL); diff --git a/src/plugins/irc/irc-nick.c b/src/plugins/irc/irc-nick.c index f593c9312..286907999 100644 --- a/src/plugins/irc/irc-nick.c +++ b/src/plugins/irc/irc-nick.c @@ -141,15 +141,20 @@ irc_nick_is_nick (struct t_irc_server *server, const char *string) char * irc_nick_find_color (struct t_irc_server *server, const char *nickname) { - char *nickname_lower, *result; + char str_args[4096]; + int casemapping, range; - nickname_lower = irc_server_string_tolower (server, nickname); - if (!nickname_lower) - return NULL; + casemapping = (server) ? server->casemapping : -1; + if ((casemapping < 0) || (casemapping >= IRC_SERVER_NUM_CASEMAPPING)) + casemapping = IRC_SERVER_CASEMAPPING_RFC1459; + range = irc_server_casemapping_range[casemapping]; - result = weechat_info_get ("nick_color", nickname_lower); - free (nickname_lower); - return result; + snprintf (str_args, sizeof (str_args), + "%s;%d", + (nickname) ? nickname : "", + range); + + return weechat_info_get ("nick_color_ignore_case", str_args); } /* @@ -161,15 +166,20 @@ irc_nick_find_color (struct t_irc_server *server, const char *nickname) char * irc_nick_find_color_name (struct t_irc_server *server, const char *nickname) { - char *nickname_lower, *result; + char str_args[4096]; + int casemapping, range; - nickname_lower = irc_server_string_tolower (server, nickname); - if (!nickname_lower) - return NULL; + casemapping = (server) ? server->casemapping : -1; + if ((casemapping < 0) || (casemapping >= IRC_SERVER_NUM_CASEMAPPING)) + casemapping = IRC_SERVER_CASEMAPPING_RFC1459; + range = irc_server_casemapping_range[casemapping]; - result = weechat_info_get ("nick_color_name", nickname_lower); - free (nickname_lower); - return result; + snprintf (str_args, sizeof (str_args), + "%s;%d", + (nickname) ? nickname: "", + range); + + return weechat_info_get ("nick_color_name_ignore_case", str_args); } /* diff --git a/src/plugins/irc/irc-server.c b/src/plugins/irc/irc-server.c index 1c4106796..2cd37d1ff 100644 --- a/src/plugins/irc/irc-server.c +++ b/src/plugins/irc/irc-server.c @@ -334,72 +334,6 @@ irc_server_strncasecmp (struct t_irc_server *server, } /* - * Converts string to lower case, following server casemapping. - * - * Note: result must be freed after use. - */ - -char * -irc_server_string_tolower (struct t_irc_server *server, const char *string) -{ - char *result, *ptr_result; - int casemapping, range; - - if (!string) - return NULL; - - casemapping = (server) ? server->casemapping : -1; - if ((casemapping < 0) || (casemapping >= IRC_SERVER_NUM_CASEMAPPING)) - casemapping = IRC_SERVER_CASEMAPPING_RFC1459; - - range = irc_server_casemapping_range[casemapping]; - - result = strdup (string); - ptr_result = result; - while (ptr_result && ptr_result[0]) - { - if ((ptr_result[0] >= 'A') && (ptr_result[0] < 'A' + range)) - ptr_result[0] += ('a' - 'A'); - ptr_result = (char *)weechat_utf8_next_char (ptr_result); - } - - return result; -} - -/* - * Converts string to upper case, following server casemapping. - * - * Note: result must be freed after use. - */ - -char * -irc_server_string_toupper (struct t_irc_server *server, const char *string) -{ - char *result, *ptr_result; - int casemapping, range; - - if (!string) - return NULL; - - casemapping = (server) ? server->casemapping : -1; - if ((casemapping < 0) || (casemapping >= IRC_SERVER_NUM_CASEMAPPING)) - casemapping = IRC_SERVER_CASEMAPPING_RFC1459; - - range = irc_server_casemapping_range[casemapping]; - - result = strdup (string); - ptr_result = result; - while (ptr_result && ptr_result[0]) - { - if ((ptr_result[0] >= 'a') && (ptr_result[0] < 'a' + range)) - ptr_result[0] -= ('a' - 'A'); - ptr_result = (char *)weechat_utf8_next_char (ptr_result); - } - - return result; -} - -/* * Evaluates a string using the server as context: * ${irc_server.xxx} and ${server} are replaced by a server option and the * server name. diff --git a/src/plugins/irc/irc-server.h b/src/plugins/irc/irc-server.h index 4ae647ed2..054516a8f 100644 --- a/src/plugins/irc/irc-server.h +++ b/src/plugins/irc/irc-server.h @@ -328,6 +328,7 @@ enum t_irc_fingerprint_digest_algo IRC_FINGERPRINT_NUM_ALGOS, }; +extern int irc_server_casemapping_range[]; extern char *irc_server_prefix_modes_default; extern char *irc_server_prefix_chars_default; extern char *irc_server_chanmodes_default; @@ -348,10 +349,6 @@ extern int irc_server_strcasecmp (struct t_irc_server *server, extern int irc_server_strncasecmp (struct t_irc_server *server, const char *string1, const char *string2, int max); -extern char *irc_server_string_tolower (struct t_irc_server *server, - const char *string); -extern char *irc_server_string_toupper (struct t_irc_server *server, - const char *string); extern char *irc_server_eval_expression (struct t_irc_server *server, const char *string); extern void irc_server_sasl_get_creds (struct t_irc_server *server, diff --git a/src/plugins/plugin-api-info.c b/src/plugins/plugin-api-info.c index 86443bf22..4d9f07b42 100644 --- a/src/plugins/plugin-api-info.c +++ b/src/plugins/plugin-api-info.c @@ -815,6 +815,7 @@ plugin_api_info_nick_color_cb (const void *pointer, void *data, result = gui_nick_find_color ( (num_items >= 1) ? items[0] : NULL, + -1, (num_items >= 2) ? items[1] : NULL); if (items) @@ -844,6 +845,7 @@ plugin_api_info_nick_color_name_cb (const void *pointer, void *data, result = gui_nick_find_color_name ( (num_items >= 1) ? items[0] : NULL, + -1, (num_items >= 2) ? items[1] : NULL); if (items) @@ -853,6 +855,84 @@ plugin_api_info_nick_color_name_cb (const void *pointer, void *data, } /* + * Returns nick color code for a nickname (case ignored using a range of chars). + */ + +char * +plugin_api_info_nick_color_ignore_case_cb (const void *pointer, void *data, + const char *info_name, + const char *arguments) +{ + char **items, *result, *error; + int num_items, case_range; + long number; + + /* make C compiler happy */ + (void) pointer; + (void) data; + (void) info_name; + + items = string_split (arguments, ";", NULL, 0, 3, &num_items); + + case_range = -1; + if (num_items >= 2) + { + number = strtol (items[1], &error, 10); + if (error && !error[0]) + case_range = (int)number; + } + + result = gui_nick_find_color ( + (num_items >= 1) ? items[0] : NULL, + case_range, + (num_items >= 3) ? items[2] : NULL); + + if (items) + string_free_split (items); + + return result; +} + +/* + * Returns nick color name for a nickname (case ignored using a range of chars). + */ + +char * +plugin_api_info_nick_color_name_ignore_case_cb (const void *pointer, void *data, + const char *info_name, + const char *arguments) +{ + char **items, *result, *error; + int num_items, case_range; + long number; + + /* make C compiler happy */ + (void) pointer; + (void) data; + (void) info_name; + + items = string_split (arguments, ";", NULL, 0, 3, &num_items); + + case_range = -1; + if (num_items >= 2) + { + number = strtol (items[1], &error, 10); + if (error && !error[0]) + case_range = (int)number; + } + + result = gui_nick_find_color_name ( + (num_items >= 1) ? items[0] : NULL, + case_range, + (num_items >= 3) ? items[2] : NULL); + + if (items) + string_free_split (items); + + return result; +} + +/* * Returns uptime according to the start date and arguments. */ @@ -2083,6 +2163,26 @@ plugin_api_info_init () "options with nick colors and forced nick colors are " "ignored)"), &plugin_api_info_nick_color_name_cb, NULL, NULL); + hook_info (NULL, "nick_color_ignore_case", + N_("get nick color code, ignoring case"), + N_("nickname;range;colors (range is a number of chars (see " + "function strcasecmp_range, 0 = convert to lower case without " + "using a range), colors is an optional comma-separated list " + "of colors to use; background is allowed for a color with " + "format text:background; if colors is present, WeeChat " + "options with nick colors and forced nick colors are " + "ignored)"), + &plugin_api_info_nick_color_ignore_case_cb, NULL, NULL); + hook_info (NULL, "nick_color_name_ignore_case", + N_("get nick color name, ignoring case"), + N_("nickname;range;colors (range is a number of chars (see " + "function strcasecmp_range, 0 = convert to lower case without " + "using a range), colors is an optional comma-separated list " + "of colors to use; background is allowed for a color with " + "format text:background; if colors is present, WeeChat " + "options with nick colors and forced nick colors are " + "ignored)"), + &plugin_api_info_nick_color_name_ignore_case_cb, NULL, NULL); hook_info (NULL, "uptime", N_("WeeChat uptime (format: \"days:hh:mm:ss\")"), N_("\"days\" (number of days) or \"seconds\" (number of " |