1
0
mirror of https://github.com/weechat/weechat.git synced 2026-07-07 10:13:12 +02:00

relay: add completion resource

This commit is contained in:
Nils
2024-10-30 15:58:29 +01:00
committed by Sébastien Helleu
parent cfe34388fb
commit c6c420c698
8 changed files with 466 additions and 17 deletions
+147 -12
View File
@@ -713,6 +713,7 @@ RELAY_API_PROTOCOL_CALLBACK(input)
if (!json_body)
return RELAY_API_PROTOCOL_RC_BAD_REQUEST;
/* get buffer either by id or by name */
ptr_buffer = NULL;
json_buffer_id = cJSON_GetObjectItem (json_body, "buffer_id");
if (json_buffer_id)
@@ -726,7 +727,7 @@ RELAY_API_PROTOCOL_CALLBACK(input)
{
relay_api_msg_send_error_json (
client,
RELAY_HTTP_404_NOT_FOUND, NULL,
RELAY_HTTP_400_BAD_REQUEST, NULL,
"Buffer \"%lld\" not found",
(long long)cJSON_GetNumberValue (json_buffer_id));
cJSON_Delete (json_body);
@@ -747,7 +748,7 @@ RELAY_API_PROTOCOL_CALLBACK(input)
{
relay_api_msg_send_error_json (
client,
RELAY_HTTP_404_NOT_FOUND, NULL,
RELAY_HTTP_400_BAD_REQUEST, NULL,
"Buffer \"%s\" not found",
ptr_buffer_name);
cJSON_Delete (json_body);
@@ -819,6 +820,138 @@ RELAY_API_PROTOCOL_CALLBACK(input)
return RELAY_API_PROTOCOL_RC_OK;
}
/*
* Callback for resource "completion".
*
* Routes:
* POST /api/completion
*/
RELAY_API_PROTOCOL_CALLBACK(completion)
{
cJSON *json_response, *json_body;
cJSON *json_buffer_id, *json_buffer_name;
cJSON *json_command, *json_position;
const char *ptr_buffer_name, *ptr_command;
int position;
char str_id[64];
struct t_gui_completion *ptr_completion;
struct t_gui_buffer *ptr_buffer;
json_body = cJSON_Parse (client->http_req->body);
if (!json_body)
return RELAY_API_PROTOCOL_RC_BAD_REQUEST;
/* get buffer either by id or by name */
ptr_buffer = NULL;
json_buffer_id = cJSON_GetObjectItem (json_body, "buffer_id");
if (json_buffer_id)
{
if (cJSON_IsNumber (json_buffer_id))
{
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 \"%lld\" not found",
(long long)cJSON_GetNumberValue (json_buffer_id));
cJSON_Delete (json_body);
return RELAY_API_PROTOCOL_RC_BAD_REQUEST;
}
}
}
else
{
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_400_BAD_REQUEST, NULL,
"Buffer \"%s\" not found",
ptr_buffer_name);
cJSON_Delete (json_body);
return RELAY_API_PROTOCOL_RC_BAD_REQUEST;
}
}
}
else
{
ptr_buffer = weechat_buffer_search_main ();
}
}
if (!ptr_buffer)
{
cJSON_Delete (json_body);
return RELAY_API_PROTOCOL_RC_BAD_REQUEST;
}
/* get command and position (optional) from input json object */
json_command = cJSON_GetObjectItem (json_body, "command");
if (json_command && cJSON_IsString (json_command))
{
ptr_command = cJSON_GetStringValue (json_command);
}
else
{
cJSON_Delete (json_body);
return RELAY_API_PROTOCOL_RC_BAD_REQUEST;
}
json_position = cJSON_GetObjectItem (json_body, "position");
if (json_position)
{
if (cJSON_IsNumber (json_position))
{
position = cJSON_GetNumberValue (json_position);
}
else
{
cJSON_Delete (json_body);
return RELAY_API_PROTOCOL_RC_BAD_REQUEST;
}
}
else
{
position = strlen (ptr_command);
}
/* perform completion */
ptr_completion = weechat_completion_new (ptr_buffer);
if (!ptr_completion)
{
cJSON_Delete (json_body);
return RELAY_API_PROTOCOL_RC_MEMORY;
}
if (!weechat_completion_search (ptr_completion, ptr_command, position, 1))
{
weechat_completion_free (ptr_completion);
cJSON_Delete (json_body);
return RELAY_API_PROTOCOL_RC_BAD_REQUEST;
}
/* create response */
json_response = relay_api_msg_completion_to_json (ptr_completion);
relay_api_msg_send_json (client, RELAY_HTTP_200_OK, NULL, "completion",
json_response);
cJSON_Delete (json_response);
cJSON_Delete (json_body);
weechat_completion_free (ptr_completion);
return RELAY_API_PROTOCOL_RC_OK;
}
/*
* Callback for resource "ping".
*
@@ -1062,16 +1195,18 @@ relay_api_protocol_recv_http (struct t_relay_client *client)
int i, num_args;
enum t_relay_api_protocol_rc return_code;
struct t_relay_api_protocol_cb protocol_cb[] = {
/* method, resource, auth, min args, max args, callback */
{ "OPTIONS", "*", 0, 0, -1, &relay_api_protocol_cb_options },
{ "POST", "handshake", 0, 0, 0, &relay_api_protocol_cb_handshake },
{ "GET", "version", 1, 0, 0, &relay_api_protocol_cb_version },
{ "GET", "buffers", 1, 0, 3, &relay_api_protocol_cb_buffers },
{ "GET", "hotlist", 1, 0, 3, &relay_api_protocol_cb_hotlist },
{ "POST", "input", 1, 0, 0, &relay_api_protocol_cb_input },
{ "POST", "ping", 1, 0, 0, &relay_api_protocol_cb_ping },
{ "POST", "sync", 1, 0, 0, &relay_api_protocol_cb_sync },
{ NULL, NULL, 0, 0, 0, NULL },
/* method, resource, auth, args, callback */
/* min,max */
{ "OPTIONS", "*", 0, 0, -1, RELAY_API_CB(options) },
{ "POST", "handshake", 0, 0, 0, RELAY_API_CB(handshake) },
{ "GET", "version", 1, 0, 0, RELAY_API_CB(version) },
{ "GET", "buffers", 1, 0, 3, RELAY_API_CB(buffers) },
{ "GET", "hotlist", 1, 0, 3, RELAY_API_CB(hotlist) },
{ "POST", "input", 1, 0, 0, RELAY_API_CB(input) },
{ "POST", "completion", 1, 0, 0, RELAY_API_CB(completion) },
{ "POST", "ping", 1, 0, 0, RELAY_API_CB(ping) },
{ "POST", "sync", 1, 0, 0, RELAY_API_CB(sync) },
{ NULL, NULL, 0, 0, 0, NULL },
};
if (!client->http_req || RELAY_STATUS_HAS_ENDED(client->status))