From 3d8c55722c79639680b249c2a5636b36b0fc4feb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Helleu?= Date: Thu, 1 Jun 2023 21:39:29 +0200 Subject: [PATCH] relay: add modifiers "relay_client_irc_in", "relay_client_irc_out1" and "relay_client_irc_out" --- ChangeLog.adoc | 1 + doc/en/weechat_plugin_api.en.adoc | 17 ++++ doc/fr/weechat_plugin_api.fr.adoc | 17 ++++ doc/it/weechat_plugin_api.it.adoc | 20 +++++ doc/ja/weechat_plugin_api.ja.adoc | 20 +++++ doc/sr/weechat_plugin_api.sr.adoc | 20 +++++ src/plugins/relay/irc/relay-irc.c | 130 ++++++++++++++++++++++-------- 7 files changed, 193 insertions(+), 32 deletions(-) diff --git a/ChangeLog.adoc b/ChangeLog.adoc index 3e203db91..929fbaf2b 100644 --- a/ChangeLog.adoc +++ b/ChangeLog.adoc @@ -64,6 +64,7 @@ New features:: * logger: add info "logger_log_file" * relay: rename "ssl" options and protocol to "tls" * relay: make TLS certificate/key loading error handling more verbose (issue #1558) + * relay: add modifiers "relay_client_irc_in", "relay_client_irc_out1" and "relay_client_irc_out" Bug fixes:: diff --git a/doc/en/weechat_plugin_api.en.adoc b/doc/en/weechat_plugin_api.en.adoc index ac7936173..a8854b94c 100644 --- a/doc/en/weechat_plugin_api.en.adoc +++ b/doc/en/weechat_plugin_api.en.adoc @@ -12366,6 +12366,23 @@ List of modifiers used by WeeChat and plugins: (to fit in 512 bytes by default). | New content of message. +| [[hook_modifier_relay_client_irc_in]] relay_client_irc_in | 4.0.0 +| String with relay client pointer (eg: "0x1234abcd") +| Content of message received from relay IRC client. +| New content of message. + +| [[hook_modifier_relay_client_irc_out1]] relay_client_irc_out1 | 4.0.0 +| String with relay client pointer (eg: "0x1234abcd") +| Content of message about to be sent to relay IRC client before automatic split + (to fit in 512 bytes by default). +| New content of message. + +| [[hook_modifier_relay_client_irc_out]] relay_client_irc_out | 4.0.0 +| String with relay client pointer (eg: "0x1234abcd") +| Content of message about to be sent to relay IRC client after automatic split + (to fit in 512 bytes by default). +| New content of message. + | [[hook_modifier_bar_condition_yyy]] bar_condition_yyy ^(2)^ | | String with window pointer (eg: "0x1234abcd") | Empty string. diff --git a/doc/fr/weechat_plugin_api.fr.adoc b/doc/fr/weechat_plugin_api.fr.adoc index ee1101d64..d28058e2e 100644 --- a/doc/fr/weechat_plugin_api.fr.adoc +++ b/doc/fr/weechat_plugin_api.fr.adoc @@ -12631,6 +12631,23 @@ Liste des modificateurs utilisés par WeeChat et les extensions : (pour tenir dans les 512 octets par défaut). | Nouveau contenu du message. +| [[hook_modifier_relay_client_irc_in]] relay_client_irc_in | 4.0.0 +| Chaîne avec un pointeur vers le client relay (par exemple : "0x1234abcd") +| Contenu du message reçu du client relay IRC. +| Nouveau contenu du message. + +| [[hook_modifier_relay_client_irc_out1]] relay_client_irc_out1 | 4.0.0 +| Chaîne avec un pointeur vers le client relay (par exemple : "0x1234abcd") +| Contenu du message qui va être envoyé au client relay IRC avant découpage automatique + (pour tenir dans les 512 octets par défaut). +| Nouveau contenu du message. + +| [[hook_modifier_relay_client_irc_out]] relay_client_irc_out | 4.0.0 +| Chaîne avec un pointeur vers le client relay (par exemple : "0x1234abcd") +| Contenu du message qui va être envoyé au client relay IRC après découpage automatique + (pour tenir dans les 512 octets par défaut). +| Nouveau contenu du message. + | [[hook_modifier_bar_condition_yyy]] bar_condition_yyy ^(2)^ | | Chaîne avec un pointeur vers la fenêtre (par exemple : "0x1234abcd") | Chaîne vide. diff --git a/doc/it/weechat_plugin_api.it.adoc b/doc/it/weechat_plugin_api.it.adoc index 7397fc9a1..3e28d2ce2 100644 --- a/doc/it/weechat_plugin_api.it.adoc +++ b/doc/it/weechat_plugin_api.it.adoc @@ -12911,6 +12911,26 @@ List of modifiers used by WeeChat and plugins: (to fit in 512 bytes by default). | Nuovo contenuto del messaggio. +// TRANSLATION MISSING +| [[hook_modifier_relay_client_irc_in]] relay_client_irc_in | 4.0.0 +| String with relay client pointer (eg: "0x1234abcd") +| Content of message received from relay IRC client. +| New content of message. + +// TRANSLATION MISSING +| [[hook_modifier_relay_client_irc_out1]] relay_client_irc_out1 | 4.0.0 +| String with relay client pointer (eg: "0x1234abcd") +| Content of message about to be sent to relay IRC client before automatic split + (to fit in 512 bytes by default). +| New content of message. + +// TRANSLATION MISSING +| [[hook_modifier_relay_client_irc_out]] relay_client_irc_out | 4.0.0 +| String with relay client pointer (eg: "0x1234abcd") +| Content of message about to be sent to relay IRC client after automatic split + (to fit in 512 bytes by default). +| New content of message. + // TRANSLATION MISSING | [[hook_modifier_bar_condition_yyy]] bar_condition_yyy ^(2)^ | | Stringa con puntatore alla finestra (eg: "0x1234abcd") diff --git a/doc/ja/weechat_plugin_api.ja.adoc b/doc/ja/weechat_plugin_api.ja.adoc index f4097473f..c7a14bc8d 100644 --- a/doc/ja/weechat_plugin_api.ja.adoc +++ b/doc/ja/weechat_plugin_api.ja.adoc @@ -12495,6 +12495,26 @@ WeeChat とプラグインが使う修飾子のリスト: (自動分割後、自動分割はデフォルトでメッセージを 512 バイト内に収まるように分割します)。| メッセージの新しい内容 +// TRANSLATION MISSING +| [[hook_modifier_relay_client_irc_in]] relay_client_irc_in | 4.0.0 +| String with relay client pointer (eg: "0x1234abcd") +| Content of message received from relay IRC client. +| New content of message. + +// TRANSLATION MISSING +| [[hook_modifier_relay_client_irc_out1]] relay_client_irc_out1 | 4.0.0 +| String with relay client pointer (eg: "0x1234abcd") +| Content of message about to be sent to relay IRC client before automatic split + (to fit in 512 bytes by default). +| New content of message. + +// TRANSLATION MISSING +| [[hook_modifier_relay_client_irc_out]] relay_client_irc_out | 4.0.0 +| String with relay client pointer (eg: "0x1234abcd") +| Content of message about to be sent to relay IRC client after automatic split + (to fit in 512 bytes by default). +| New content of message. + // TRANSLATION MISSING | [[hook_modifier_bar_condition_yyy]] bar_condition_yyy ^(2)^ | | ウィンドウへのポインタの文字列 (eg: "0x1234abcd") diff --git a/doc/sr/weechat_plugin_api.sr.adoc b/doc/sr/weechat_plugin_api.sr.adoc index 8510dd229..e4647c4ae 100644 --- a/doc/sr/weechat_plugin_api.sr.adoc +++ b/doc/sr/weechat_plugin_api.sr.adoc @@ -11960,6 +11960,26 @@ struct t_hook *weechat_hook_modifier (const char *modifier, (да стане у 512 подразумевано). | Нови садржај поруке. +// TRANSLATION MISSING +| [[hook_modifier_relay_client_irc_in]] relay_client_irc_in | 4.0.0 +| String with relay client pointer (eg: "0x1234abcd") +| Content of message received from relay IRC client. +| New content of message. + +// TRANSLATION MISSING +| [[hook_modifier_relay_client_irc_out1]] relay_client_irc_out1 | 4.0.0 +| String with relay client pointer (eg: "0x1234abcd") +| Content of message about to be sent to relay IRC client before automatic split + (to fit in 512 bytes by default). +| New content of message. + +// TRANSLATION MISSING +| [[hook_modifier_relay_client_irc_out]] relay_client_irc_out | 4.0.0 +| String with relay client pointer (eg: "0x1234abcd") +| Content of message about to be sent to relay IRC client after automatic split + (to fit in 512 bytes by default). +| New content of message. + | [[hook_modifier_bar_condition_yyy]] bar_condition_yyy ^(2)^ | | Стринг са показивачем на прозор (нпр: "0x1234abcd") | Празан стринг. diff --git a/src/plugins/relay/irc/relay-irc.c b/src/plugins/relay/irc/relay-irc.c index bd8be06ae..d6953590c 100644 --- a/src/plugins/relay/irc/relay-irc.c +++ b/src/plugins/relay/irc/relay-irc.c @@ -198,8 +198,9 @@ void relay_irc_sendf (struct t_relay_client *client, const char *format, ...) { int length, number; - char *pos, hash_key[32], *message; - const char *str_message; + char *pos, hash_key[32], *message, *new_msg1, *new_msg2; + char modifier_data[128]; + const char *str_message, *ptr_msg1, *ptr_msg2; struct t_hashtable *hashtable_in, *hashtable_out; if (!client) @@ -209,10 +210,32 @@ relay_irc_sendf (struct t_relay_client *client, const char *format, ...) if (!vbuffer) return; - pos = strchr (vbuffer, '\r'); + new_msg1 = NULL; + hashtable_in = NULL; + hashtable_out = NULL; + + snprintf (modifier_data, sizeof (modifier_data), + "0x%lx", (unsigned long)client); + new_msg1 = weechat_hook_modifier_exec ("relay_client_irc_out1", + modifier_data, vbuffer); + + /* no changes in new message? */ + if (new_msg1 && (strcmp (vbuffer, new_msg1) == 0)) + { + free (new_msg1); + new_msg1 = NULL; + } + + /* message dropped? */ + if (new_msg1 && !new_msg1[0]) + goto end; + + ptr_msg1 = (new_msg1) ? new_msg1 : vbuffer; + + pos = strchr (ptr_msg1, '\r'); if (pos) pos[0] = '\0'; - pos = strchr (vbuffer, '\n'); + pos = strchr (ptr_msg1, '\n'); if (pos) pos[0] = '\0'; @@ -220,37 +243,57 @@ relay_irc_sendf (struct t_relay_client *client, const char *format, ...) WEECHAT_HASHTABLE_STRING, WEECHAT_HASHTABLE_STRING, NULL, NULL); - if (hashtable_in) + if (!hashtable_in) + goto end; + + weechat_hashtable_set (hashtable_in, "server", client->protocol_args); + weechat_hashtable_set (hashtable_in, "message", ptr_msg1); + hashtable_out = weechat_info_get_hashtable ("irc_message_split", + hashtable_in); + if (!hashtable_out) + goto end; + + number = 1; + while (1) { - weechat_hashtable_set (hashtable_in, "server", client->protocol_args); - weechat_hashtable_set (hashtable_in, "message", vbuffer); - hashtable_out = weechat_info_get_hashtable ("irc_message_split", - hashtable_in); - if (hashtable_out) + snprintf (hash_key, sizeof (hash_key), "msg%d", number); + str_message = weechat_hashtable_get (hashtable_out, hash_key); + if (!str_message) + break; + new_msg2 = weechat_hook_modifier_exec ("relay_client_irc_out", + modifier_data, str_message); + /* no changes in new message? */ + if (new_msg2 && (strcmp (str_message, new_msg2) == 0)) { - number = 1; - while (1) - { - snprintf (hash_key, sizeof (hash_key), "msg%d", number); - str_message = weechat_hashtable_get (hashtable_out, hash_key); - if (!str_message) - break; - length = strlen (str_message) + 16 + 1; - message = malloc (length); - if (message) - { - snprintf (message, length, "%s\r\n", str_message); - relay_client_send (client, RELAY_CLIENT_MSG_STANDARD, - message, strlen (message), NULL); - free (message); - } - number++; - } - weechat_hashtable_free (hashtable_out); + free (new_msg2); + new_msg2 = NULL; } - weechat_hashtable_free (hashtable_in); + /* message not dropped? */ + if (!new_msg2 || new_msg2[0]) + { + ptr_msg2 = (new_msg2) ? new_msg2 : str_message; + length = strlen (ptr_msg2) + 16 + 1; + message = malloc (length); + if (message) + { + snprintf (message, length, "%s\r\n", ptr_msg2); + relay_client_send (client, RELAY_CLIENT_MSG_STANDARD, + message, strlen (message), NULL); + free (message); + } + } + if (new_msg2) + free (new_msg2); + number++; } +end: + if (new_msg1) + free (new_msg1); + if (hashtable_in) + weechat_hashtable_free (hashtable_in); + if (hashtable_out) + weechat_hashtable_free (hashtable_out); free (vbuffer); } @@ -1334,12 +1377,15 @@ relay_irc_recv (struct t_relay_client *client, const char *data) struct t_hashtable *hash_parsed, *hash_redirect; struct t_infolist *infolist_server; const char *irc_command, *str_num_params, *isupport, *pos_password; + const char *ptr_data; char str_time[128], str_signal[128], str_server_channel[256], *nick; char str_param[128], *str_args, *version, str_command[128], **params; char *pos, *password, *irc_is_channel, *info, *error, *str_cmd_lower; + char modifier_data[128], *new_data; long num_params; int i, redirect_msg; + new_data = NULL; hash_parsed = NULL; params = NULL; @@ -1354,8 +1400,26 @@ relay_irc_recv (struct t_relay_client *client, const char *data) data); } + snprintf (modifier_data, sizeof (modifier_data), + "0x%lx", (unsigned long)client); + new_data = weechat_hook_modifier_exec ("relay_client_irc_in", + modifier_data, data); + + /* no changes in new data */ + if (new_data && (strcmp (data, new_data) == 0)) + { + free (new_data); + new_data = NULL; + } + + /* message dropped? */ + if (new_data && !new_data[0]) + goto end; + + ptr_data = (new_data) ? new_data : data; + /* parse IRC message */ - hash_parsed = relay_irc_message_parse (data); + hash_parsed = relay_irc_message_parse (ptr_data); if (!hash_parsed) goto end; irc_command = weechat_hashtable_get (hash_parsed, "command"); @@ -1808,11 +1872,13 @@ relay_irc_recv (struct t_relay_client *client, const char *data) relay_irc_input_send (client, NULL, "priority_high", "/quote %s", - data); + ptr_data); } } end: + if (new_data) + free (new_data); if (hash_parsed) weechat_hashtable_free (hash_parsed); if (params)