From 48ca390f79de87689b2ce63c830bf19377907583 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Helleu?= Date: Sun, 10 Mar 2024 14:22:56 +0100 Subject: relay/api: add support of buffer id in POST /api/input (issue #2081) --- src/plugins/relay/api/relay-api-protocol.c | 49 ++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 12 deletions(-) (limited to 'src/plugins') diff --git a/src/plugins/relay/api/relay-api-protocol.c b/src/plugins/relay/api/relay-api-protocol.c index 7bab96505..70f3cc9c3 100644 --- a/src/plugins/relay/api/relay-api-protocol.c +++ b/src/plugins/relay/api/relay-api-protocol.c @@ -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) { -- cgit v1.2.3