summaryrefslogtreecommitdiff
path: root/src/plugins/relay
diff options
context:
space:
mode:
authorSébastien Helleu <flashcode@flashtux.org>2024-03-31 14:26:51 +0200
committerSébastien Helleu <flashcode@flashtux.org>2024-04-07 13:18:13 +0200
commit643adecff5b2d16679e106dce771cb111f338a42 (patch)
tree4cbeba982ddbc7429847ddbd593ec65812f18aca /src/plugins/relay
parent4460f92727dc024bf89407c94bf899b914823a67 (diff)
downloadweechat-643adecff5b2d16679e106dce771cb111f338a42.zip
relay: add option `send` in command `/remote` (issue #2066)
This can be used to send raw JSON data encapsulated in a websocket frame to the remote. Example: /remote send test {"request":"GET /api/version"}
Diffstat (limited to 'src/plugins/relay')
-rw-r--r--src/plugins/relay/api/remote/relay-remote-network.h3
-rw-r--r--src/plugins/relay/relay-command.c70
-rw-r--r--src/plugins/relay/relay-remote.c73
-rw-r--r--src/plugins/relay/relay-remote.h5
4 files changed, 103 insertions, 48 deletions
diff --git a/src/plugins/relay/api/remote/relay-remote-network.h b/src/plugins/relay/api/remote/relay-remote-network.h
index 7635beb63..d0defa87e 100644
--- a/src/plugins/relay/api/remote/relay-remote-network.h
+++ b/src/plugins/relay/api/remote/relay-remote-network.h
@@ -20,6 +20,9 @@
#ifndef WEECHAT_PLUGIN_RELAY_REMOTE_NETWORK_H
#define WEECHAT_PLUGIN_RELAY_REMOTE_NETWORK_H
+extern int relay_remote_network_send (struct t_relay_remote *remote,
+ enum t_relay_msg_type msg_type,
+ const char *data, int data_size);
extern int relay_remote_network_connect (struct t_relay_remote *remote);
extern void relay_remote_network_disconnect (struct t_relay_remote *remote);
diff --git a/src/plugins/relay/relay-command.c b/src/plugins/relay/relay-command.c
index 2da4f53a3..843d3d00f 100644
--- a/src/plugins/relay/relay-command.c
+++ b/src/plugins/relay/relay-command.c
@@ -591,6 +591,54 @@ relay_command_remote (const void *pointer, void *data,
return WEECHAT_RC_OK;
}
+ if (weechat_strcmp (argv[1], "send") == 0)
+ {
+ WEECHAT_COMMAND_MIN_ARGS(4, "send");
+ ptr_remote = relay_remote_search (argv[2]);
+ if (!ptr_remote)
+ {
+ weechat_printf (
+ NULL,
+ _("%s%s: remote \"%s\" not found for \"%s\" command"),
+ weechat_prefix ("error"),
+ RELAY_PLUGIN_NAME,
+ argv[2],
+ "remote send");
+ return WEECHAT_RC_OK;
+ }
+ if (ptr_remote->status != RELAY_STATUS_CONNECTED)
+ {
+ weechat_printf (
+ NULL,
+ _("%s%s: no connection to remote \"%s\""),
+ weechat_prefix ("error"),
+ RELAY_PLUGIN_NAME,
+ argv[2]);
+ return WEECHAT_RC_OK;
+ }
+ relay_remote_send (ptr_remote, argv_eol[3]);
+ return WEECHAT_RC_OK;
+ }
+
+ if (weechat_strcmp (argv[1], "disconnect") == 0)
+ {
+ WEECHAT_COMMAND_MIN_ARGS(3, "disconnect");
+ ptr_remote = relay_remote_search (argv[2]);
+ if (!ptr_remote)
+ {
+ weechat_printf (
+ NULL,
+ _("%s%s: remote \"%s\" not found for \"%s\" command"),
+ weechat_prefix ("error"),
+ RELAY_PLUGIN_NAME,
+ argv[2],
+ "remote disconnect");
+ return WEECHAT_RC_OK;
+ }
+ relay_remote_disconnect (ptr_remote);
+ return WEECHAT_RC_OK;
+ }
+
if (weechat_strcmp (argv[1], "rename") == 0)
{
WEECHAT_COMMAND_MIN_ARGS(4, "rename");
@@ -634,25 +682,6 @@ relay_command_remote (const void *pointer, void *data,
WEECHAT_COMMAND_ERROR;
}
- if (weechat_strcmp (argv[1], "disconnect") == 0)
- {
- WEECHAT_COMMAND_MIN_ARGS(3, "disconnect");
- ptr_remote = relay_remote_search (argv[2]);
- if (!ptr_remote)
- {
- weechat_printf (
- NULL,
- _("%s%s: remote \"%s\" not found for \"%s\" command"),
- weechat_prefix ("error"),
- RELAY_PLUGIN_NAME,
- argv[2],
- "remote disconnect");
- return WEECHAT_RC_OK;
- }
- relay_remote_disconnect (ptr_remote);
- return WEECHAT_RC_OK;
- }
-
if (weechat_strcmp (argv[1], "del") == 0)
{
WEECHAT_COMMAND_MIN_ARGS(3, "del");
@@ -784,6 +813,7 @@ relay_command_init ()
N_("list|listfull [<name>]"
" || add <name> <url> [-<option>[=<value>]]"
" || connect <name>"
+ " || send <name> <json>"
" || disconnect <name>"
" || rename <name> <new_name>"
" || del <name>"),
@@ -798,6 +828,7 @@ relay_command_init ()
"or http://example.com:9000 (plain-text connection, not recommended)"),
N_("option: set option for remote: proxy, password or totp_secret"),
N_("raw[connect]: connect to a remote relay server"),
+ N_("raw[send]: send JSON data to a remote"),
N_("raw[disconnect]: disconnect from a remote relay server"),
N_("raw[rename]: rename a remote relay server"),
N_("raw[del]: delete a remote relay server"),
@@ -815,6 +846,7 @@ relay_command_init ()
" || add %(relay_remotes) https://localhost:9000 "
"-password=${xxx}|-proxy=xxx|-totp_secret=${xxx}|%*"
" || connect %(relay_remotes)"
+ " || send %(relay_remotes) {\"request\":\"\"}"
" || disconnect %(relay_remotes)"
" || rename %(relay_remotes) %(relay_remotes)"
" || del %(relay_remotes)",
diff --git a/src/plugins/relay/relay-remote.c b/src/plugins/relay/relay-remote.c
index f7d9becb4..43c85d508 100644
--- a/src/plugins/relay/relay-remote.c
+++ b/src/plugins/relay/relay-remote.c
@@ -607,6 +607,25 @@ relay_remote_connect (struct t_relay_remote *remote)
}
/*
+ * Sends JSON data to a remote WeeChat relay/api.
+ *
+ * Returns:
+ * 1: OK
+ * 0: error
+ */
+
+int
+relay_remote_send (struct t_relay_remote *remote, const char *json)
+{
+ if (!remote || (remote->status != RELAY_STATUS_CONNECTED) || !json)
+ return 0;
+
+ return (relay_remote_network_send (remote, RELAY_MSG_STANDARD,
+ json, strlen (json)) > 0) ?
+ 1 : 0;
+}
+
+/*
* Renames a remote.
*
* Returns:
@@ -664,6 +683,33 @@ relay_remote_rename (struct t_relay_remote *remote, const char *name)
}
/*
+ * Disconnects one remote.
+ */
+
+void
+relay_remote_disconnect (struct t_relay_remote *remote)
+{
+ if (remote->sock >= 0)
+ relay_remote_network_disconnect (remote);
+}
+
+/*
+ * Disconnects all remotes.
+ */
+
+void
+relay_remote_disconnect_all ()
+{
+ struct t_relay_remote *ptr_remote;
+
+ for (ptr_remote = relay_remotes; ptr_remote;
+ ptr_remote = ptr_remote->next_remote)
+ {
+ relay_remote_disconnect (ptr_remote);
+ }
+}
+
+/*
* Deletes a remote.
*/
@@ -716,33 +762,6 @@ relay_remote_free_all ()
}
/*
- * Disconnects one remote.
- */
-
-void
-relay_remote_disconnect (struct t_relay_remote *remote)
-{
- if (remote->sock >= 0)
- relay_remote_network_disconnect (remote);
-}
-
-/*
- * Disconnects all remotes.
- */
-
-void
-relay_remote_disconnect_all ()
-{
- struct t_relay_remote *ptr_remote;
-
- for (ptr_remote = relay_remotes; ptr_remote;
- ptr_remote = ptr_remote->next_remote)
- {
- relay_remote_disconnect (ptr_remote);
- }
-}
-
-/*
* Adds a remote in an infolist.
*
* If force_disconnected_state == 1, the infolist contains the remote
diff --git a/src/plugins/relay/relay-remote.h b/src/plugins/relay/relay-remote.h
index b2f0b2cdb..231e1e608 100644
--- a/src/plugins/relay/relay-remote.h
+++ b/src/plugins/relay/relay-remote.h
@@ -88,11 +88,12 @@ extern struct t_relay_remote *relay_remote_new_with_infolist (struct t_infolist
extern void relay_remote_set_status (struct t_relay_remote *remote,
enum t_relay_status status);
extern int relay_remote_connect (struct t_relay_remote *remote);
+extern int relay_remote_send (struct t_relay_remote *remote, const char *json);
+extern void relay_remote_disconnect (struct t_relay_remote *remote);
+extern void relay_remote_disconnect_all ();
extern int relay_remote_rename (struct t_relay_remote *remote, const char *name);
extern void relay_remote_free (struct t_relay_remote *remote);
extern void relay_remote_free_all ();
-extern void relay_remote_disconnect (struct t_relay_remote *remote);
-extern void relay_remote_disconnect_all ();
extern int relay_remote_add_to_infolist (struct t_infolist *infolist,
struct t_relay_remote *remote,
int force_disconnected_state);