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

relay/api: use "buffer_name" first if received, then "buffer_id" in completion and input resources

This fixes some tests done by shemathesis, which sends "buffer_id" to
0 (unknown buffer) and "buffer_name" to a valid buffer name.
This commit is contained in:
Sébastien Helleu
2025-06-23 21:27:06 +02:00
parent 4348036e2e
commit d8baabd250
+40 -44
View File
@@ -712,57 +712,55 @@ RELAY_API_PROTOCOL_CALLBACK(input)
char str_delay[32];
json_body = cJSON_Parse (client->http_req->body);
if (!json_body)
if (!json_body || !cJSON_IsObject (json_body))
return RELAY_API_PROTOCOL_RC_BAD_REQUEST;
/* get buffer either by id or by name */
/* get buffer either by name or by id */
ptr_buffer = NULL;
json_buffer_id = cJSON_GetObjectItem (json_body, "buffer_id");
if (json_buffer_id)
json_buffer_name = cJSON_GetObjectItem (json_body, "buffer_name");
if (json_buffer_name)
{
if (cJSON_IsNumber (json_buffer_id))
if (cJSON_IsString (json_buffer_name))
{
snprintf (str_id, sizeof (str_id),
"%lld", (long long)cJSON_GetNumberValue (json_buffer_id));
ptr_buffer = weechat_buffer_search ("==id", str_id);
ptr_buffer_name = cJSON_GetStringValue (json_buffer_name);
ptr_buffer = weechat_buffer_search ("==", ptr_buffer_name);
if (!ptr_buffer)
{
relay_api_msg_send_error_json (
client,
RELAY_HTTP_400_BAD_REQUEST, NULL,
"Buffer \"%lld\" not found",
(long long)cJSON_GetNumberValue (json_buffer_id));
"Buffer \"%s\" not found",
ptr_buffer_name);
cJSON_Delete (json_body);
return RELAY_API_PROTOCOL_RC_OK;
}
}
}
else
if (!ptr_buffer)
{
json_buffer_name = cJSON_GetObjectItem (json_body, "buffer_name");
if (json_buffer_name)
json_buffer_id = cJSON_GetObjectItem (json_body, "buffer_id");
if (json_buffer_id)
{
if (cJSON_IsString (json_buffer_name))
if (cJSON_IsNumber (json_buffer_id))
{
ptr_buffer_name = cJSON_GetStringValue (json_buffer_name);
ptr_buffer = weechat_buffer_search ("==", ptr_buffer_name);
snprintf (str_id, sizeof (str_id),
"%lld", (long long)cJSON_GetNumberValue (json_buffer_id));
ptr_buffer = weechat_buffer_search ("==id", str_id);
if (!ptr_buffer)
{
relay_api_msg_send_error_json (
client,
RELAY_HTTP_400_BAD_REQUEST, NULL,
"Buffer \"%s\" not found",
ptr_buffer_name);
"Buffer \"%lld\" not found",
(long long)cJSON_GetNumberValue (json_buffer_id));
cJSON_Delete (json_body);
return RELAY_API_PROTOCOL_RC_OK;
}
}
}
else
{
ptr_buffer = weechat_buffer_search_main ();
}
}
if (!ptr_buffer)
ptr_buffer = weechat_buffer_search_main ();
if (!ptr_buffer)
{
cJSON_Delete (json_body);
@@ -841,57 +839,55 @@ RELAY_API_PROTOCOL_CALLBACK(completion)
struct t_gui_buffer *ptr_buffer;
json_body = cJSON_Parse (client->http_req->body);
if (!json_body)
if (!json_body || !cJSON_IsObject(json_body))
return RELAY_API_PROTOCOL_RC_BAD_REQUEST;
/* get buffer either by id or by name */
/* get buffer either by name or by id */
ptr_buffer = NULL;
json_buffer_id = cJSON_GetObjectItem (json_body, "buffer_id");
if (json_buffer_id)
json_buffer_name = cJSON_GetObjectItem (json_body, "buffer_name");
if (json_buffer_name)
{
if (cJSON_IsNumber (json_buffer_id))
if (cJSON_IsString (json_buffer_name))
{
snprintf (str_id, sizeof(str_id),
"%lld", (long long)cJSON_GetNumberValue (json_buffer_id));
ptr_buffer = weechat_buffer_search ("==id", str_id);
ptr_buffer_name = cJSON_GetStringValue (json_buffer_name);
ptr_buffer = weechat_buffer_search ("==", ptr_buffer_name);
if (!ptr_buffer)
{
relay_api_msg_send_error_json (
client,
RELAY_HTTP_400_BAD_REQUEST, NULL,
"Buffer \"%lld\" not found",
(long long)cJSON_GetNumberValue (json_buffer_id));
"Buffer \"%s\" not found",
ptr_buffer_name);
cJSON_Delete (json_body);
return RELAY_API_PROTOCOL_RC_OK;
}
}
}
else
if (!ptr_buffer)
{
json_buffer_name = cJSON_GetObjectItem (json_body, "buffer_name");
if (json_buffer_name)
json_buffer_id = cJSON_GetObjectItem (json_body, "buffer_id");
if (json_buffer_id)
{
if (cJSON_IsString (json_buffer_name))
if (cJSON_IsNumber (json_buffer_id))
{
ptr_buffer_name = cJSON_GetStringValue (json_buffer_name);
ptr_buffer = weechat_buffer_search ("==", ptr_buffer_name);
snprintf (str_id, sizeof(str_id),
"%lld", (long long)cJSON_GetNumberValue (json_buffer_id));
ptr_buffer = weechat_buffer_search ("==id", str_id);
if (!ptr_buffer)
{
relay_api_msg_send_error_json (
client,
RELAY_HTTP_400_BAD_REQUEST, NULL,
"Buffer \"%s\" not found",
ptr_buffer_name);
"Buffer \"%lld\" not found",
(long long)cJSON_GetNumberValue (json_buffer_id));
cJSON_Delete (json_body);
return RELAY_API_PROTOCOL_RC_OK;
}
}
}
else
{
ptr_buffer = weechat_buffer_search_main ();
}
}
if (!ptr_buffer)
ptr_buffer = weechat_buffer_search_main ();
if (!ptr_buffer)
{
cJSON_Delete (json_body);