1
0
mirror of https://github.com/weechat/weechat.git synced 2026-07-05 09:13:14 +02:00

api: add infos "nick_color_ignore_case" and "nick_color_name_ignore_case" (issue #194)

This commit is contained in:
Sébastien Helleu
2023-08-24 18:19:32 +02:00
parent 965c6ac2ea
commit 5d9af29024
29 changed files with 772 additions and 384 deletions
+64
View File
@@ -394,6 +394,70 @@ string_toupper (const char *string)
return string_dyn_free (result, 0);
}
/*
* 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).
*
+2
View File
@@ -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,
+29 -7
View File
@@ -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);
+2 -2
View File
@@ -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 */
+6 -4
View File
@@ -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);
+24 -14
View File
@@ -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);
}
/*
-66
View File
@@ -333,72 +333,6 @@ irc_server_strncasecmp (struct t_irc_server *server,
return weechat_strncasecmp_range (string1, string2, max, range);
}
/*
* 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
+1 -4
View File
@@ -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,
+100
View File
@@ -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)
@@ -852,6 +854,84 @@ plugin_api_info_nick_color_name_cb (const void *pointer, void *data,
return result;
}
/*
* 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 "