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

relay/api: add support of buffer id in POST /api/input (issue #2081)

This commit is contained in:
Sébastien Helleu
2024-03-10 14:22:56 +01:00
parent aa989767a1
commit 48ca390f79
2 changed files with 54 additions and 14 deletions
+37 -12
View File
@@ -475,13 +475,14 @@ error:
* Callback for resource "input".
*
* Routes:
* POST /api/input/{buffer_name}
* POST /api/input
*/
RELAY_API_PROTOCOL_CALLBACK(input)
{
cJSON *json_body, *json_buffer, *json_command;
cJSON *json_body, *json_buffer_id, *json_buffer_name, *json_command;
const char *ptr_buffer_name, *ptr_command, *ptr_commands;
char str_id[64];
struct t_gui_buffer *ptr_buffer;
struct t_hashtable *options;
char str_delay[32];
@@ -490,19 +491,21 @@ RELAY_API_PROTOCOL_CALLBACK(input)
if (!json_body)
return WEECHAT_RC_ERROR;
json_buffer = cJSON_GetObjectItem (json_body, "buffer");
if (json_buffer)
json_buffer_id = cJSON_GetObjectItem (json_body, "buffer_id");
if (json_buffer_id)
{
if (cJSON_IsString (json_buffer))
if (cJSON_IsNumber (json_buffer_id))
{
ptr_buffer_name = cJSON_GetStringValue (json_buffer);
ptr_buffer = weechat_buffer_search ("==", ptr_buffer_name);
snprintf (str_id, sizeof (str_id),
"%lld", (long long)json_buffer_id->valuedouble);
ptr_buffer = weechat_buffer_search ("==id", str_id);
if (!ptr_buffer)
{
relay_api_msg_send_error_json (client,
RELAY_HTTP_404_NOT_FOUND, NULL,
"Buffer \"%s\" not found",
ptr_buffer_name);
relay_api_msg_send_error_json (
client,
RELAY_HTTP_404_NOT_FOUND, NULL,
"Buffer \"%lld\" not found",
(long long)json_buffer_id->valuedouble);
cJSON_Delete (json_body);
return WEECHAT_RC_OK;
}
@@ -510,7 +513,29 @@ RELAY_API_PROTOCOL_CALLBACK(input)
}
else
{
ptr_buffer = weechat_buffer_search_main ();
json_buffer_name = cJSON_GetObjectItem (json_body, "buffer_name");
if (json_buffer_name)
{
if (cJSON_IsString (json_buffer_name))
{
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_404_NOT_FOUND, NULL,
"Buffer \"%s\" not found",
ptr_buffer_name);
cJSON_Delete (json_body);
return WEECHAT_RC_OK;
}
}
}
else
{
ptr_buffer = weechat_buffer_search_main ();
}
}
if (!ptr_buffer)
{
@@ -482,6 +482,7 @@ TEST(RelayApiProtocolWithClient, CbBuffers)
TEST(RelayApiProtocolWithClient, CbInput)
{
char str_body[1024];
int old_delay;
/* error: no body */
@@ -494,7 +495,7 @@ TEST(RelayApiProtocolWithClient, CbInput)
/* error: invalid buffer name */
test_client_recv_http ("POST /api/input",
"{\"buffer\": \"invalid\", "
"{\"buffer_name\": \"invalid\", "
"\"command\": \"/print test\"}");
STRCMP_EQUAL("HTTP/1.1 404 Not Found\r\n"
"Content-Type: application/json; charset=utf-8\r\n"
@@ -519,12 +520,26 @@ TEST(RelayApiProtocolWithClient, CbInput)
old_delay = relay_api_protocol_command_delay;
relay_api_protocol_command_delay = 0;
test_client_recv_http ("POST /api/input",
"{\"buffer\": \"core.weechat\", "
"{\"buffer_name\": \"core.weechat\", "
"\"command\": \"/print test from relay 2\"}");
relay_api_protocol_command_delay = old_delay;
record_stop ();
WEE_CHECK_HTTP_CODE(204, "No Content");
CHECK(record_search ("core.weechat", "", "test from relay 2", NULL));
/* on core buffer, with buffer id */
record_start ();
old_delay = relay_api_protocol_command_delay;
relay_api_protocol_command_delay = 0;
snprintf (str_body, sizeof (str_body),
"{\"buffer_id\": %lld, "
"\"command\": \"/print test from relay 3\"}",
gui_buffers->id);
test_client_recv_http ("POST /api/input", str_body);
relay_api_protocol_command_delay = old_delay;
record_stop ();
WEE_CHECK_HTTP_CODE(204, "No Content");
CHECK(record_search ("core.weechat", "", "test from relay 3", NULL));
}
/*