1
0
mirror of https://github.com/weechat/weechat.git synced 2026-06-28 05:46:38 +02:00

relay/remote: fix close of remote buffer

This commit is contained in:
Sébastien Helleu
2024-05-04 11:40:49 +02:00
parent 4ac120e435
commit a21fa3359d
6 changed files with 140 additions and 35 deletions
+11 -21
View File
@@ -124,16 +124,14 @@ relay_api_msg_send_json_internal (struct t_relay_client *client,
int return_code,
const char *message,
const char *event_name,
struct t_gui_buffer *event_buffer,
long long event_buffer_id,
const char *headers,
const char *body_type,
cJSON *json_body)
{
cJSON *json, *json_event;
char *string, *request;
int num_bytes, length;
const char *ptr_id;
char *string, *error, *request;
long long id;
if (!client || !message)
return -1;
@@ -159,18 +157,9 @@ relay_api_msg_send_json_internal (struct t_relay_client *client,
cJSON_AddItemToObject (
json_event, "name",
cJSON_CreateString ((event_name) ? event_name : ""));
id = -1;
if (event_buffer)
{
ptr_id = weechat_buffer_get_string (event_buffer, "id");
error = NULL;
id = strtoll (ptr_id, &error, 10);
if (!error || error[0])
id = -1;
}
cJSON_AddItemToObject (
json_event, "buffer_id",
cJSON_CreateNumber (id));
cJSON_CreateNumber (event_buffer_id));
cJSON_AddItemToObject (json, "event", json_event);
}
}
@@ -193,12 +182,13 @@ relay_api_msg_send_json_internal (struct t_relay_client *client,
free (request);
}
}
if (json_body)
if (body_type)
{
cJSON_AddItemToObject (json, "body_type",
cJSON_CreateString ((body_type) ? body_type : ""));
cJSON_AddItemToObject (json, "body", json_body);
cJSON_CreateString (body_type));
}
if (json_body)
cJSON_AddItemToObject (json, "body", json_body);
string = cJSON_PrintUnformatted (json);
num_bytes = relay_client_send (
client,
@@ -239,7 +229,7 @@ relay_api_msg_send_json (struct t_relay_client *client,
return_code,
message,
NULL, /* event_name */
NULL, /* event_buffer */
-1, /* event_buffer_id */
NULL, /* headers */
body_type,
json_body);
@@ -286,7 +276,7 @@ relay_api_msg_send_error_json (struct t_relay_client *client,
return_code,
message,
NULL, /* event_name */
NULL, /* event_buffer */
-1, /* event_buffer_id */
headers,
NULL, /* body_type */
json);
@@ -321,14 +311,14 @@ relay_api_msg_send_error_json (struct t_relay_client *client,
int
relay_api_msg_send_event (struct t_relay_client *client,
const char *name,
struct t_gui_buffer *buffer,
long long buffer_id,
const char *body_type,
cJSON *json_body)
{
return relay_api_msg_send_json_internal (client,
RELAY_API_HTTP_0_EVENT,
name,
buffer,
buffer_id,
NULL, /* headers */
body_type,
json_body);
+1 -1
View File
@@ -34,7 +34,7 @@ extern int relay_api_msg_send_error_json (struct t_relay_client *client,
const char *format, ...);
extern int relay_api_msg_send_event (struct t_relay_client *client,
const char *name,
struct t_gui_buffer *buffer,
long long buffer_id,
const char *body_type,
cJSON *json_body);
extern cJSON *relay_api_msg_buffer_to_json (struct t_gui_buffer *buffer,
+56 -6
View File
@@ -72,6 +72,9 @@ relay_api_protocol_signal_buffer_cb (const void *pointer, void *data,
struct t_gui_line_data *ptr_line_data;
cJSON *json;
long lines;
long long buffer_id;
const char *ptr_id;
char *error;
/* make C compiler happy */
(void) data;
@@ -92,18 +95,61 @@ relay_api_protocol_signal_buffer_cb (const void *pointer, void *data,
|| (strcmp (signal, "buffer_title_changed") == 0)
|| (strncmp (signal, "buffer_localvar_", 16) == 0)
|| (strcmp (signal, "buffer_cleared") == 0)
|| (strcmp (signal, "buffer_closing") == 0))
|| (strcmp (signal, "buffer_closing") == 0)
|| (strcmp (signal, "buffer_closed") == 0))
{
ptr_buffer = (struct t_gui_buffer *)signal_data;
if (!ptr_buffer || relay_buffer_is_relay (ptr_buffer))
return WEECHAT_RC_OK;
if (strcmp (signal, "buffer_closed") == 0)
{
/*
* when a buffer is closed, we send the buffer id
* with body type "buffer" and empty body
*/
buffer_id = -1;
ptr_id = weechat_hashtable_get (
RELAY_API_DATA(ptr_client, buffers_closing),
ptr_buffer);
if (ptr_id)
{
error = NULL;
buffer_id = strtoll (ptr_id, &error, 10);
if (!error || error[0])
buffer_id = -1;
weechat_hashtable_remove (
RELAY_API_DATA(ptr_client, buffers_closing),
ptr_buffer);
}
relay_api_msg_send_event (ptr_client, signal, buffer_id, "buffer", NULL);
return WEECHAT_RC_OK;
}
if (strcmp (signal, "buffer_closing") == 0)
{
/*
* when a buffer is closing, we save its id in the hashtable
* "buffers_closing", it will be used when sending the event
* "buffer_closed"
*/
weechat_hashtable_set (RELAY_API_DATA(ptr_client, buffers_closing),
ptr_buffer,
weechat_buffer_get_string (ptr_buffer, "id"));
}
/* we get all lines when a buffer is opened, otherwise none */
lines = (strcmp (signal, "buffer_opened") == 0) ? LONG_MIN : 0;
/* build body with buffer info */
json = relay_api_msg_buffer_to_json (
ptr_buffer, lines, 0, RELAY_API_DATA(ptr_client, sync_colors));
/* send to client */
if (json)
{
relay_api_msg_send_event (ptr_client, signal, NULL, "buffer", json);
buffer_id = relay_api_get_buffer_id (ptr_buffer);
relay_api_msg_send_event (ptr_client, signal, buffer_id, "buffer", json);
cJSON_Delete (json);
}
}
@@ -127,7 +173,8 @@ relay_api_protocol_signal_buffer_cb (const void *pointer, void *data,
ptr_line_data, RELAY_API_DATA(ptr_client, sync_colors));
if (json)
{
relay_api_msg_send_event (ptr_client, signal, ptr_buffer,
buffer_id = relay_api_get_buffer_id (ptr_buffer);
relay_api_msg_send_event (ptr_client, signal, buffer_id,
"line", json);
cJSON_Delete (json);
}
@@ -150,6 +197,7 @@ relay_api_protocol_hsignal_nicklist_cb (const void *pointer, void *data,
struct t_gui_nick_group *ptr_parent_group, *ptr_group;
struct t_gui_nick *ptr_nick;
cJSON *json;
long long buffer_id;
/* make C compiler happy */
(void) data;
@@ -170,6 +218,8 @@ relay_api_protocol_hsignal_nicklist_cb (const void *pointer, void *data,
if (!ptr_buffer || relay_buffer_is_relay (ptr_buffer))
return WEECHAT_RC_OK;
buffer_id = relay_api_get_buffer_id (ptr_buffer);
if ((strcmp (signal, "nicklist_group_added") == 0)
|| (strcmp (signal, "nicklist_group_changed") == 0)
|| (strcmp (signal, "nicklist_group_removing") == 0))
@@ -179,7 +229,7 @@ relay_api_protocol_hsignal_nicklist_cb (const void *pointer, void *data,
RELAY_API_DATA(ptr_client, sync_colors));
if (json)
{
relay_api_msg_send_event (ptr_client, signal, ptr_buffer,
relay_api_msg_send_event (ptr_client, signal, buffer_id,
"nick_group", json);
cJSON_Delete (json);
}
@@ -193,7 +243,7 @@ relay_api_protocol_hsignal_nicklist_cb (const void *pointer, void *data,
RELAY_API_DATA(ptr_client, sync_colors));
if (json)
{
relay_api_msg_send_event (ptr_client, signal, ptr_buffer,
relay_api_msg_send_event (ptr_client, signal, buffer_id,
"nick", json);
cJSON_Delete (json);
}
@@ -226,7 +276,7 @@ relay_api_protocol_signal_upgrade_cb (const void *pointer, void *data,
if ((strcmp (signal, "upgrade") == 0)
|| (strcmp (signal, "upgrade_ended") == 0))
{
relay_api_msg_send_event (ptr_client, signal, NULL, NULL, NULL);
relay_api_msg_send_event (ptr_client, signal, -1, NULL, NULL);
}
return WEECHAT_RC_OK;
+44
View File
@@ -41,6 +41,32 @@
#include "relay-api-protocol.h"
/*
* Returns buffer id.
*/
long long
relay_api_get_buffer_id (struct t_gui_buffer *buffer)
{
const char *ptr_id;
char *error;
long long id;
if (!buffer)
return -1;
ptr_id = weechat_buffer_get_string (buffer, "id");
if (!ptr_id)
return -1;
error = NULL;
id = strtoll (ptr_id, &error, 10);
if (!error || error[0])
return -1;
return id;
}
/*
* Returns value of "colors" URL parameter, an enum with one of these values:
* - RELAY_API_COLORS_ANSI (default)
@@ -179,6 +205,12 @@ relay_api_alloc (struct t_relay_client *client)
RELAY_API_DATA(client, hook_signal_buffer) = NULL;
RELAY_API_DATA(client, hook_hsignal_nicklist) = NULL;
RELAY_API_DATA(client, hook_signal_upgrade) = NULL;
RELAY_API_DATA(client, buffers_closing) = weechat_hashtable_new (
32,
WEECHAT_HASHTABLE_POINTER,
WEECHAT_HASHTABLE_STRING,
NULL,
NULL);
RELAY_API_DATA(client, sync_enabled) = 0;
RELAY_API_DATA(client, sync_nicks) = 0;
RELAY_API_DATA(client, sync_colors) = RELAY_API_COLORS_ANSI;
@@ -201,6 +233,12 @@ relay_api_alloc_with_infolist (struct t_relay_client *client,
RELAY_API_DATA(client, hook_signal_buffer) = NULL;
RELAY_API_DATA(client, hook_hsignal_nicklist) = NULL;
RELAY_API_DATA(client, hook_signal_upgrade) = NULL;
RELAY_API_DATA(client, buffers_closing) = weechat_hashtable_new (
32,
WEECHAT_HASHTABLE_POINTER,
WEECHAT_HASHTABLE_STRING,
NULL,
NULL);
RELAY_API_DATA(client, sync_enabled) = weechat_infolist_integer (
infolist, "sync_enabled");
RELAY_API_DATA(client, sync_nicks) = weechat_infolist_integer (
@@ -244,6 +282,7 @@ relay_api_free (struct t_relay_client *client)
weechat_unhook (RELAY_API_DATA(client, hook_signal_buffer));
weechat_unhook (RELAY_API_DATA(client, hook_hsignal_nicklist));
weechat_unhook (RELAY_API_DATA(client, hook_signal_upgrade));
weechat_hashtable_free (RELAY_API_DATA(client, buffers_closing));
free (client->protocol_data);
@@ -302,6 +341,11 @@ relay_api_print_log (struct t_relay_client *client)
weechat_log_printf (" hook_signal_buffer. . . : %p", RELAY_API_DATA(client, hook_signal_buffer));
weechat_log_printf (" hook_hsignal_nicklist . : %p", RELAY_API_DATA(client, hook_hsignal_nicklist));
weechat_log_printf (" hook_signal_upgrade . . : %p", RELAY_API_DATA(client, hook_signal_upgrade));
weechat_log_printf (" buffers_closing. . . . .: %p (hashtable: '%s')",
RELAY_API_DATA(client, buffers_closing),
weechat_hashtable_get_string (
RELAY_API_DATA(client, buffers_closing),
"keys_values"));
weechat_log_printf (" sync_enabled. . . . . . : %d", RELAY_API_DATA(client, sync_enabled));
weechat_log_printf (" sync_nicks. . . . . . . : %d", RELAY_API_DATA(client, sync_nicks));
weechat_log_printf (" sync_colors . . . . . . : %d", RELAY_API_DATA(client, sync_colors));
+2
View File
@@ -56,11 +56,13 @@ struct t_relay_api_data
struct t_hook *hook_signal_buffer; /* hook for signals "buffer_*" */
struct t_hook *hook_hsignal_nicklist; /* hook for hsignals "nicklist_*" */
struct t_hook *hook_signal_upgrade; /* hook for signals "upgrade*" */
struct t_hashtable *buffers_closing; /* ptr -> "id" of buffers closing */
int sync_enabled; /* 1 if sync is enabled */
int sync_nicks; /* 1 if nicks are synchronized */
enum t_relay_api_colors sync_colors; /* colors to send with sync */
};
extern long long relay_api_get_buffer_id (struct t_gui_buffer *buffer);
extern enum t_relay_api_colors relay_api_search_colors (const char *colors);
extern void relay_api_hook_signals (struct t_relay_client *client);
extern void relay_api_unhook_signals (struct t_relay_client *client);
@@ -139,7 +139,7 @@ RELAY_REMOTE_EVENT_CALLBACK(line)
int y;
struct timeval tv_date;
if (!event->buffer)
if (!event->buffer || !event->json)
return WEECHAT_RC_OK;
JSON_GET_NUM(event->json, y, -1);
@@ -217,7 +217,7 @@ relay_remote_event_handle_nick (struct t_gui_buffer *buffer, cJSON *json)
long long id, parent_group_id;
int visible;
if (!buffer)
if (!buffer || !json)
return;
JSON_GET_NUM(json, id, -1);
@@ -276,7 +276,7 @@ relay_remote_event_handle_nick_group (struct t_gui_buffer *buffer, cJSON *json)
long long id, parent_group_id;
int visible;
if (!buffer)
if (!buffer || !json)
return;
JSON_GET_NUM(json, id, -1);
@@ -346,7 +346,7 @@ RELAY_REMOTE_EVENT_CALLBACK(nick_group)
cJSON *json_obj;
long long id;
if (!event->buffer)
if (!event->buffer || !event->json)
return WEECHAT_RC_OK;
if (weechat_strcmp (event->name, "nicklist_group_removing") == 0)
@@ -376,7 +376,7 @@ RELAY_REMOTE_EVENT_CALLBACK(nick)
cJSON *json_obj;
long long id;
if (!event->buffer)
if (!event->buffer || !event->json)
return WEECHAT_RC_OK;
if (weechat_strcmp (event->name, "nicklist_nick_removing") == 0)
@@ -482,6 +482,20 @@ RELAY_REMOTE_EVENT_CALLBACK(buffer)
long long id;
int number, nicklist, nicklist_case_sensitive, nicklist_display_groups;
if (weechat_strcmp (event->name, "buffer_closed") == 0)
{
weechat_buffer_close (event->buffer);
return WEECHAT_RC_OK;
}
/* ignore "buffer_closing" event */
if (weechat_strcmp (event->name, "buffer_closing") == 0)
return WEECHAT_RC_OK;
/* for other events, we need a body */
if (!event->json)
return WEECHAT_RC_OK;
JSON_GET_NUM(event->json, id, -1);
JSON_GET_STR(event->json, name);
JSON_GET_STR(event->json, short_name);
@@ -522,7 +536,7 @@ RELAY_REMOTE_EVENT_CALLBACK(buffer)
weechat_hashtable_set (buffer_props, "input_get_any_user_data", "1");
/* if buffer exists, set properties, otherwise create buffer */
ptr_buffer = relay_remote_event_search_buffer (event->remote, id);
ptr_buffer = event->buffer;
if (ptr_buffer)
{
weechat_hashtable_map (buffer_props,
@@ -568,7 +582,6 @@ RELAY_REMOTE_EVENT_CALLBACK(buffer)
}
}
/* add lines */
json_lines = cJSON_GetObjectItem (event->json, "lines");
if (json_lines && cJSON_IsArray (json_lines))
@@ -602,6 +615,9 @@ RELAY_REMOTE_EVENT_CALLBACK(version)
cJSON *json_obj;
const char *weechat_version, *weechat_version_git, *relay_api_version;
if (!event->json)
return WEECHAT_RC_OK;
JSON_GET_STR(event->json, weechat_version);
JSON_GET_STR(event->json, weechat_version_git);
JSON_GET_STR(event->json, relay_api_version);
@@ -625,6 +641,9 @@ relay_remote_event_sync_with_remote (struct t_relay_remote *remote)
{
cJSON *json, *json_body;
if (!remote)
return;
json = cJSON_CreateObject ();
if (!json)
goto end;