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

relay/api: add resource GET /api/scripts

This commit is contained in:
Sébastien Helleu
2026-06-08 23:04:40 +02:00
parent 12016c4c05
commit 551c12e049
11 changed files with 419 additions and 4 deletions
+37
View File
@@ -848,3 +848,40 @@ relay_api_msg_hotlist_to_json (struct t_gui_hotlist *hotlist)
return json;
}
/*
* Create a JSON object with a script.
*/
cJSON *
relay_api_msg_script_to_json (struct t_hdata *hdata, void *script, const char *extension)
{
cJSON *json;
void *pointer;
const char *ptr_string;
char name[1024];
if (!hdata)
return NULL;
pointer = script;
json = cJSON_CreateObject ();
if (!json)
return NULL;
if (!script)
return json;
snprintf (name, sizeof (name),
"%s.%s",
weechat_hdata_string (hdata, script, "name"),
extension);
MSG_ADD_STR_BUF("name", name);
MSG_ADD_HDATA_STR("version", "version");
MSG_ADD_HDATA_STR("description", "description");
MSG_ADD_HDATA_STR("author", "author");
MSG_ADD_HDATA_STR("license", "license");
return json;
}
+2
View File
@@ -58,5 +58,7 @@ extern cJSON *relay_api_msg_nick_group_to_json (struct t_gui_nick_group *nick_gr
enum t_relay_api_colors colors);
extern cJSON *relay_api_msg_completion_to_json (struct t_gui_completion *completion);
extern cJSON *relay_api_msg_hotlist_to_json (struct t_gui_hotlist *hotlist);
extern cJSON *relay_api_msg_script_to_json (struct t_hdata *hdata, void *script,
const char *extension);
#endif /* WEECHAT_PLUGIN_RELAY_API_MSG_H */
@@ -764,6 +764,64 @@ RELAY_API_PROTOCOL_CALLBACK(hotlist)
return RELAY_API_PROTOCOL_RC_OK;
}
/*
* Callback for resource "scripts".
*
* Routes:
* GET /api/scripts
*/
RELAY_API_PROTOCOL_CALLBACK(scripts)
{
cJSON *json;
char *info_languages, **languages, hdata_name[256], *pos;
int num_languages, i;
struct t_hdata *ptr_hdata;
void *ptr_script;
json = cJSON_CreateArray ();
if (!json)
return RELAY_API_PROTOCOL_RC_MEMORY;
info_languages = weechat_info_get ("script_languages", NULL);
if (info_languages)
{
languages = weechat_string_split (info_languages, ",", NULL,
WEECHAT_STRING_SPLIT_STRIP_LEFT
| WEECHAT_STRING_SPLIT_STRIP_RIGHT
| WEECHAT_STRING_SPLIT_COLLAPSE_SEPS,
0, &num_languages);
if (languages)
{
for (i = 0; i < num_languages; i++)
{
pos = strchr (languages[i], ':');
if (pos)
{
pos[0] = '\0';
snprintf (hdata_name, sizeof (hdata_name),
"%s_script", languages[i]);
ptr_hdata = weechat_hdata_get (hdata_name);
ptr_script = weechat_hdata_get_list (ptr_hdata, "scripts");
while (ptr_script)
{
cJSON_AddItemToArray (
json,
relay_api_msg_script_to_json (ptr_hdata, ptr_script, pos + 1));
ptr_script = weechat_hdata_move (ptr_hdata, ptr_script, 1);
}
}
}
weechat_string_free_split (languages);
}
free (info_languages);
}
relay_api_msg_send_json (client, RELAY_HTTP_200_OK, NULL, "scripts", json);
cJSON_Delete (json);
return RELAY_API_PROTOCOL_RC_OK;
}
/*
* Callback for resource "input".
*
@@ -1286,6 +1344,7 @@ relay_api_protocol_recv_http (struct t_relay_client *client)
{ "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) },
{ "GET", "scripts", 1, 0, 0, RELAY_API_CB(scripts) },
{ "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) },
+1 -1
View File
@@ -26,7 +26,7 @@ struct t_relay_client;
enum t_relay_status;
#define RELAY_API_VERSION_MAJOR 0
#define RELAY_API_VERSION_MINOR 5
#define RELAY_API_VERSION_MINOR 6
#define RELAY_API_VERSION_PATCH 0
#define RELAY_API_VERSION_NUMBER \
((RELAY_API_VERSION_MAJOR << 16) \
+54 -3
View File
@@ -17,7 +17,7 @@ info:
license:
name: CC BY-NC-SA 4.0
url: https://creativecommons.org/licenses/by-nc-sa/4.0/
version: 0.5.0
version: 0.6.0
externalDocs:
url: https://weechat.org/doc/
@@ -32,6 +32,7 @@ tags:
- name: version
- name: buffers
- name: hotlist
- name: scripts
- name: input
- name: completion
- name: ping
@@ -96,7 +97,7 @@ paths:
get:
tags:
- version
description: Get the WeeChat and API versions
description: Get the WeeChat and API versions.
parameters:
- $ref: '#/components/parameters/totp'
operationId: getVersion
@@ -531,7 +532,7 @@ paths:
get:
tags:
- hotlist
description: Get hotlist
description: Get hotlist.
operationId: getHotlist
parameters:
- $ref: '#/components/parameters/totp'
@@ -564,6 +565,43 @@ paths:
$ref: '#/components/schemas/Error'
security:
- password: []
/scripts:
get:
tags:
- scripts
description: Get list of loaded scripts.
operationId: getScripts
parameters:
- $ref: '#/components/parameters/totp'
responses:
'200':
description: Successful operation
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Script'
'401':
description: Unauthorized
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
'400':
description: Bad request
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
'503':
description: Out of memory
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
security:
- password: []
/input:
post:
tags:
@@ -1336,6 +1374,19 @@ components:
- date
- buffer_id
- count
Script:
type: object
properties:
name:
type: string
description: script name
example: 'go.py'
version:
type: string
example: '3.1.1'
required:
- name
- version
Completion:
type: object
properties: