1
0
mirror of https://github.com/weechat/weechat.git synced 2026-06-30 14:56:39 +02:00

irc: decode IRC color codes only when displaying messages

Before parsing IRC messages, they were almost all changed to convert IRC color
codes to WeeChat color codes, which caused some bugs when storing data like
account and real names (stored with WeeChat color codes instead of IRC colors).

Now the messages are parsed as-is, then the colors are converted only when
strings are displayed in a buffer by `weechat_printf()`.
This commit is contained in:
Sébastien Helleu
2024-09-19 08:35:56 +02:00
parent 02847246b2
commit 6534919868
10 changed files with 885 additions and 855 deletions
+1
View File
@@ -15,6 +15,7 @@
### Fixed
- core, plugins: fix integer overflow in loops ([#2178](https://github.com/weechat/weechat/issues/2178))
- irc: decode IRC colors only when displaying messages in buffer, store nick info with IRC colors (host, account, real name)
- irc: do not strip trailing spaces from incoming IRC messages
- relay/api: fix empty nicklist in remote buffers after connection or reconnection
- lua: fix compilation on Fedora with Lua < 5.2.0 ([#2173](https://github.com/weechat/weechat/issues/2173), [#2174](https://github.com/weechat/weechat/issues/2174))
+1 -1
View File
@@ -1593,7 +1593,7 @@ irc_channel_display_nick_back_in_pv (struct t_irc_server *server,
(nick) ? nick->name : nickname,
IRC_COLOR_CHAT_DELIMITERS,
IRC_COLOR_CHAT_HOST,
(nick && nick->host) ? nick->host : "",
(nick && nick->host) ? IRC_COLOR_MSG(nick->host) : "",
IRC_COLOR_CHAT_DELIMITERS,
IRC_COLOR_MESSAGE_JOIN);
}
+39
View File
@@ -81,6 +81,8 @@ char irc_color_term2irc[IRC_COLOR_TERM2IRC_NUM_COLORS] =
0, /* 15 0 (white) */
};
regex_t *irc_color_regex_ansi = NULL;
int irc_color_index_string_decoded = 0;
char *irc_color_string_decoded[32];
/*
@@ -484,6 +486,26 @@ irc_color_decode (const char *string, int keep_colors)
return weechat_string_dyn_free (out, 0);
}
/*
* Replaces IRC colors by WeeChat colors and returns a pointer to an allocated
* string that doesn't need to be freed by the caller.
*
* If keep_colors == 0: removes any color/style in message otherwise keeps
* colors.
*/
const char *
irc_color_decode_const (const char *string, int keep_colors)
{
irc_color_index_string_decoded = (irc_color_index_string_decoded + 1) % 32;
free (irc_color_string_decoded[irc_color_index_string_decoded]);
irc_color_string_decoded[irc_color_index_string_decoded] = irc_color_decode (
string, keep_colors);
if (!irc_color_string_decoded[irc_color_index_string_decoded])
irc_color_string_decoded[irc_color_index_string_decoded] = strdup ("");
return (const char *)irc_color_string_decoded[irc_color_index_string_decoded];
}
/*
* Replaces color codes in command line by IRC color codes.
*
@@ -1053,6 +1075,17 @@ irc_color_weechat_add_to_infolist (struct t_infolist *infolist)
return 1;
}
/*
* Initializes IRC colors.
*/
void
irc_color_init ()
{
irc_color_index_string_decoded = 0;
memset (irc_color_string_decoded, 0, sizeof (irc_color_string_decoded));
}
/*
* Ends IRC colors.
*/
@@ -1060,10 +1093,16 @@ irc_color_weechat_add_to_infolist (struct t_infolist *infolist)
void
irc_color_end ()
{
int i;
if (irc_color_regex_ansi)
{
regfree (irc_color_regex_ansi);
free (irc_color_regex_ansi);
irc_color_regex_ansi = NULL;
}
for (i = 0; i < 32; i++)
{
free (irc_color_string_decoded[i]);
}
}
+7
View File
@@ -104,6 +104,11 @@
#define IRC_COLOR_ITEM_TLS_VERSION_DEPRECATED weechat_color(weechat_config_string(irc_config_color_item_tls_version_deprecated))
#define IRC_COLOR_ITEM_TLS_VERSION_INSECURE weechat_color(weechat_config_string(irc_config_color_item_tls_version_insecure))
#define IRC_COLOR_MSG(__string) \
irc_color_decode_const ( \
__string, \
weechat_config_boolean (irc_config_network_colors_receive))
struct t_irc_color_ansi_state
{
char keep_colors;
@@ -113,6 +118,7 @@ struct t_irc_color_ansi_state
};
extern char *irc_color_decode (const char *string, int keep_colors);
extern const char *irc_color_decode_const (const char *string, int keep_colors);
extern char *irc_color_encode (const char *string, int keep_colors);
extern char *irc_color_decode_ansi (const char *string, int keep_colors);
extern char *irc_color_modifier_cb (const void *pointer, void *data,
@@ -121,6 +127,7 @@ extern char *irc_color_modifier_cb (const void *pointer, void *data,
const char *string);
extern char *irc_color_for_tags (const char *color);
extern int irc_color_weechat_add_to_infolist (struct t_infolist *infolist);
extern void irc_color_init ();
extern void irc_color_end ();
#endif /* WEECHAT_PLUGIN_IRC_COLOR_H */
+10 -10
View File
@@ -238,8 +238,8 @@ irc_ctcp_display_request (struct t_irc_protocol_ctxt *ctxt,
IRC_COLOR_CHAT_CHANNEL,
ctcp,
IRC_COLOR_RESET,
(arguments) ? " " : "",
(arguments) ? arguments : "",
(arguments && arguments[0]) ? " " : "",
(arguments && arguments[0]) ? arguments : "",
(reply && !reply[0]) ? _(" (blocked)") : "");
}
@@ -327,7 +327,7 @@ irc_ctcp_display_reply_from_nick (struct t_irc_protocol_ctxt *ctxt,
ptr_args + 1,
IRC_COLOR_RESET,
" ",
pos_args);
IRC_COLOR_MSG(pos_args));
}
}
else
@@ -382,7 +382,7 @@ irc_ctcp_display_reply_to_nick_internal (struct t_irc_protocol_ctxt *ctxt,
type,
(args && args[0]) ? IRC_COLOR_RESET : "",
(args && args[0]) ? " " : "",
(args) ? args : "");
(args && args[0]) ? IRC_COLOR_MSG(args) : "");
}
/*
@@ -1481,7 +1481,7 @@ irc_ctcp_recv (struct t_irc_protocol_ctxt *ctxt,
ctxt->nick,
(pos_args) ? IRC_COLOR_RESET : "",
(pos_args) ? " " : "",
(pos_args) ? pos_args : "");
(pos_args) ? IRC_COLOR_MSG(pos_args) : "");
}
else
{
@@ -1502,7 +1502,7 @@ irc_ctcp_recv (struct t_irc_protocol_ctxt *ctxt,
ctxt->nick,
(pos_args) ? IRC_COLOR_RESET : "",
(pos_args) ? " " : "",
(pos_args) ? pos_args : "");
(pos_args) ? IRC_COLOR_MSG(pos_args) : "");
}
free (nick_color);
}
@@ -1545,7 +1545,7 @@ irc_ctcp_recv (struct t_irc_protocol_ctxt *ctxt,
ctxt->nick,
(pos_args) ? IRC_COLOR_RESET : "",
(pos_args) ? " " : "",
(pos_args) ? pos_args : "");
(pos_args) ? IRC_COLOR_MSG(pos_args) : "");
(void) weechat_hook_signal_send ("irc_pv",
WEECHAT_HOOK_SIGNAL_STRING,
(void *)ctxt->irc_message);
@@ -1558,7 +1558,7 @@ irc_ctcp_recv (struct t_irc_protocol_ctxt *ctxt,
reply = irc_ctcp_get_reply (ctxt->server, ptr_args + 1);
irc_ctcp_display_request (ctxt, channel,
ptr_args + 1,
pos_args, reply);
IRC_COLOR_MSG(pos_args), reply);
if (!reply || reply[0])
{
if (reply)
@@ -1588,7 +1588,7 @@ irc_ctcp_recv (struct t_irc_protocol_ctxt *ctxt,
if (reply)
{
irc_ctcp_display_request (ctxt, channel,
ptr_args + 1, pos_args,
ptr_args + 1, IRC_COLOR_MSG(pos_args),
reply);
if (reply[0])
@@ -1621,7 +1621,7 @@ irc_ctcp_recv (struct t_irc_protocol_ctxt *ctxt,
ptr_args + 1,
(pos_args) ? IRC_COLOR_RESET : "",
(pos_args) ? " " : "",
(pos_args) ? pos_args : "");
(pos_args) ? IRC_COLOR_MSG(pos_args) : "");
}
}
}
+4 -4
View File
@@ -193,7 +193,7 @@ irc_input_user_message_display (struct t_irc_server *server,
server->nick,
(ptr_text && ptr_text[0]) ? IRC_COLOR_RESET : "",
(ptr_text && ptr_text[0]) ? " " : "",
(ptr_text && ptr_text[0]) ? ptr_text : "");
IRC_COLOR_MSG(ptr_text));
}
else
{
@@ -209,7 +209,7 @@ irc_input_user_message_display (struct t_irc_server *server,
server->nick,
IRC_COLOR_RESET,
(ptr_text && ptr_text[0]) ? " " : "",
(ptr_text && ptr_text[0]) ? ptr_text : "");
IRC_COLOR_MSG(ptr_text));
}
}
else if (ctcp_type)
@@ -229,7 +229,7 @@ irc_input_user_message_display (struct t_irc_server *server,
ctcp_type,
IRC_COLOR_RESET,
(ptr_text && ptr_text[0]) ? " " : "",
(ptr_text && ptr_text[0]) ? ptr_text : "");
IRC_COLOR_MSG(ptr_text));
}
else if (display_target)
{
@@ -256,7 +256,7 @@ irc_input_user_message_display (struct t_irc_server *server,
IRC_COLOR_CHAT_CHANNEL : irc_nick_color_for_msg (server, 0, NULL, target),
target,
IRC_COLOR_RESET,
(ptr_text) ? ptr_text : "");
IRC_COLOR_MSG(ptr_text));
}
else
{
File diff suppressed because it is too large Load Diff
+1 -3
View File
@@ -27,9 +27,8 @@
irc_protocol_cb_##__command ( \
struct t_irc_protocol_ctxt *ctxt)
#define IRCB(__message, __decode_color, __func_cb) \
#define IRCB(__message, __func_cb) \
{ #__message, \
__decode_color, \
&irc_protocol_cb_##__func_cb }
#define IRC_PROTOCOL_MIN_PARAMS(__min_params) \
@@ -80,7 +79,6 @@ typedef int (t_irc_recv_func)(struct t_irc_protocol_ctxt *ctxt);
struct t_irc_protocol_msg
{
char *name; /* IRC message name */
int decode_color; /* decode color before calling function */
t_irc_recv_func *recv_function; /* function called when msg is received */
};
+2
View File
@@ -196,6 +196,8 @@ weechat_plugin_init (struct t_weechat_plugin *plugin, int argc, char *argv[])
irc_signal_quit_received = 0;
irc_signal_upgrade_received = 0;
irc_color_init ();
if (!irc_config_init ())
return WEECHAT_RC_ERROR;
File diff suppressed because it is too large Load Diff