summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSébastien Helleu <flashcode@flashtux.org>2024-05-01 13:51:32 +0200
committerSébastien Helleu <flashcode@flashtux.org>2024-05-01 13:53:58 +0200
commitafe037c579b541bb86657916abef23a7ab8907bb (patch)
tree432e1b6fa950f00002314c638a4b7735b923467c
parentc5268bc5f9df20ae3de45776cee68e66035bd885 (diff)
downloadweechat-afe037c579b541bb86657916abef23a7ab8907bb.zip
relay/api: add buffer keys in GET /api/buffers (issue #2066)
-rw-r--r--src/plugins/relay/api/relay-api-msg.c55
-rw-r--r--src/plugins/relay/api/relay-api-msg.h8
-rw-r--r--src/plugins/relay/relay.c2
-rw-r--r--src/plugins/relay/relay.h1
-rw-r--r--src/plugins/weechat-plugin.h1
-rw-r--r--tests/unit/plugins/relay/api/test-relay-api-msg.cpp18
6 files changed, 81 insertions, 4 deletions
diff --git a/src/plugins/relay/api/relay-api-msg.c b/src/plugins/relay/api/relay-api-msg.c
index 929428469..2bcd5bc95 100644
--- a/src/plugins/relay/api/relay-api-msg.c
+++ b/src/plugins/relay/api/relay-api-msg.c
@@ -405,6 +405,9 @@ relay_api_msg_buffer_to_json (struct t_gui_buffer *buffer,
cJSON_AddItemToObject (json, "local_variables", json_local_vars);
}
+ /* keys local to buffer */
+ cJSON_AddItemToObject (json, "keys", relay_api_msg_keys_to_json (buffer));
+
/* lines */
if (lines != 0)
{
@@ -427,6 +430,58 @@ relay_api_msg_buffer_to_json (struct t_gui_buffer *buffer,
}
/*
+ * Creates a JSON object with a buffer key.
+ */
+
+cJSON *
+relay_api_msg_key_to_json (struct t_gui_key *key)
+{
+ struct t_hdata *hdata;
+ struct t_gui_key *pointer;
+ cJSON *json;
+ const char *ptr_string;
+
+ hdata = relay_hdata_key;
+ pointer = key;
+
+ json = cJSON_CreateObject ();
+ if (!json)
+ return NULL;
+
+ if (!key)
+ return json;
+
+ MSG_ADD_HDATA_STR("key", "key");
+ MSG_ADD_HDATA_STR("command", "command");
+
+ return json;
+}
+
+/*
+ * Creates a JSON object with an array of buffer keys.
+ */
+
+cJSON *
+relay_api_msg_keys_to_json (struct t_gui_buffer *buffer)
+{
+ cJSON *json;
+ struct t_gui_key *ptr_key;
+
+ json = cJSON_CreateArray ();
+ if (!json)
+ return NULL;
+
+ ptr_key = weechat_hdata_pointer (relay_hdata_buffer, buffer, "keys");
+ while (ptr_key)
+ {
+ cJSON_AddItemToArray (json, relay_api_msg_key_to_json (ptr_key));
+ ptr_key = weechat_hdata_move (relay_hdata_key, ptr_key, 1);
+ }
+
+ return json;
+}
+
+/*
* Creates a JSON object with a buffer line data.
*/
diff --git a/src/plugins/relay/api/relay-api-msg.h b/src/plugins/relay/api/relay-api-msg.h
index 16516b4f4..f28f3b642 100644
--- a/src/plugins/relay/api/relay-api-msg.h
+++ b/src/plugins/relay/api/relay-api-msg.h
@@ -41,11 +41,13 @@ extern cJSON *relay_api_msg_buffer_to_json (struct t_gui_buffer *buffer,
long lines,
int nicks,
enum t_relay_api_colors colors);
-extern cJSON *relay_api_msg_lines_to_json (struct t_gui_buffer *pointer,
- long lines,
- enum t_relay_api_colors colors);
+extern cJSON *relay_api_msg_key_to_json (struct t_gui_key *key);
+extern cJSON *relay_api_msg_keys_to_json (struct t_gui_buffer *buffer);
extern cJSON *relay_api_msg_line_data_to_json (struct t_gui_line_data *line_data,
enum t_relay_api_colors colors);
+extern cJSON *relay_api_msg_lines_to_json (struct t_gui_buffer *buffer,
+ long lines,
+ enum t_relay_api_colors colors);
extern cJSON *relay_api_msg_nick_to_json (struct t_gui_nick *nick,
enum t_relay_api_colors colors);
extern cJSON *relay_api_msg_nick_group_to_json (struct t_gui_nick_group *nick_group,
diff --git a/src/plugins/relay/relay.c b/src/plugins/relay/relay.c
index f643ec860..b81d08ac7 100644
--- a/src/plugins/relay/relay.c
+++ b/src/plugins/relay/relay.c
@@ -62,6 +62,7 @@ char *relay_msg_type_string[] = /* prefix in raw buffer for msg */
{ "", "[PING]\n", "[PONG]\n", "[CLOSE]\n" };
struct t_hdata *relay_hdata_buffer = NULL;
+struct t_hdata *relay_hdata_key = NULL;
struct t_hdata *relay_hdata_lines = NULL;
struct t_hdata *relay_hdata_line = NULL;
struct t_hdata *relay_hdata_line_data = NULL;
@@ -251,6 +252,7 @@ weechat_plugin_init (struct t_weechat_plugin *plugin, int argc, char *argv[])
weechat_plugin = plugin;
relay_hdata_buffer = weechat_hdata_get ("buffer");
+ relay_hdata_key = weechat_hdata_get ("key");
relay_hdata_lines = weechat_hdata_get ("lines");
relay_hdata_line = weechat_hdata_get ("line");
relay_hdata_line_data = weechat_hdata_get ("line_data");
diff --git a/src/plugins/relay/relay.h b/src/plugins/relay/relay.h
index c78efea74..f30a5c054 100644
--- a/src/plugins/relay/relay.h
+++ b/src/plugins/relay/relay.h
@@ -27,6 +27,7 @@
extern struct t_weechat_plugin *weechat_relay_plugin;
extern struct t_hdata *relay_hdata_buffer;
+extern struct t_hdata *relay_hdata_key;
extern struct t_hdata *relay_hdata_lines;
extern struct t_hdata *relay_hdata_line;
extern struct t_hdata *relay_hdata_line_data;
diff --git a/src/plugins/weechat-plugin.h b/src/plugins/weechat-plugin.h
index 052ccb90c..db5fbee7c 100644
--- a/src/plugins/weechat-plugin.h
+++ b/src/plugins/weechat-plugin.h
@@ -49,6 +49,7 @@ struct t_gui_bar_window;
struct t_gui_buffer;
struct t_gui_completion;
struct t_gui_hotlist;
+struct t_gui_key;
struct t_gui_line;
struct t_gui_line_data;
struct t_gui_lines;
diff --git a/tests/unit/plugins/relay/api/test-relay-api-msg.cpp b/tests/unit/plugins/relay/api/test-relay-api-msg.cpp
index 940321021..239dd80f8 100644
--- a/tests/unit/plugins/relay/api/test-relay-api-msg.cpp
+++ b/tests/unit/plugins/relay/api/test-relay-api-msg.cpp
@@ -118,7 +118,7 @@ TEST(RelayApiMsg, SendEvent)
TEST(RelayApiMsg, BufferToJson)
{
- cJSON *json, *json_obj, *json_local_vars, *json_lines;
+ cJSON *json, *json_obj, *json_local_vars, *json_keys, *json_key, *json_lines;
cJSON *json_nicklist_root, *json_nicks, *json_groups, *json_group;
cJSON *json_group_nicks, *json_nick;
struct t_gui_buffer *buffer;
@@ -132,6 +132,9 @@ TEST(RelayApiMsg, BufferToJson)
POINTERS_EQUAL(NULL, cJSON_GetObjectItem (json, "name"));
cJSON_Delete (json);
+ gui_buffer_set (gui_buffers, "key_bind_meta-y,1", "/test1");
+ gui_buffer_set (gui_buffers, "key_bind_meta-y,2", "/test2 arg");
+
/* buffer without lines and nicks */
json = relay_api_msg_buffer_to_json (gui_buffers, 0, 0, RELAY_API_COLORS_ANSI);
CHECK(json);
@@ -148,6 +151,17 @@ TEST(RelayApiMsg, BufferToJson)
json_local_vars = cJSON_GetObjectItem (json, "local_variables");
CHECK(json_local_vars);
CHECK(cJSON_IsObject (json_local_vars));
+ json_keys = cJSON_GetObjectItem (json, "keys");
+ CHECK(json_keys);
+ LONGS_EQUAL(2, cJSON_GetArraySize (json_keys));
+ json_key = cJSON_GetArrayItem (json_keys, 0);
+ CHECK(json_key);
+ WEE_CHECK_OBJ_STR("meta-y,1", json_key, "key");
+ WEE_CHECK_OBJ_STR("/test1", json_key, "command");
+ json_key = cJSON_GetArrayItem (json_keys, 1);
+ CHECK(json_key);
+ WEE_CHECK_OBJ_STR("meta-y,2", json_key, "key");
+ WEE_CHECK_OBJ_STR("/test2 arg", json_key, "command");
WEE_CHECK_OBJ_STR("core", json_local_vars, "plugin");
WEE_CHECK_OBJ_STR("weechat", json_local_vars, "name");
POINTERS_EQUAL(NULL, cJSON_GetObjectItem (json, "lines"));
@@ -298,6 +312,8 @@ TEST(RelayApiMsg, BufferToJson)
WEE_CHECK_OBJ_BOOL(0, json_nick, "visible");
cJSON_Delete (json);
+ gui_buffer_set (gui_buffers, "key_unbind_meta-y", "");
+
gui_buffer_close (buffer);
}