From 063021b43b7a6bd56cae3c41a19ffa4845498ede Mon Sep 17 00:00:00 2001 From: auric Date: Mon, 23 Feb 2026 20:04:40 -0600 Subject: Resolve cmd_tail handler in client.c to send comprehensible packets for the bot --- src/client.c | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/client.c b/src/client.c index 53b8e53..10cba04 100644 --- a/src/client.c +++ b/src/client.c @@ -375,8 +375,25 @@ static void cmd_tail(Client *c, const char *unit_name) { } data[dpos] = '\0'; - /* Build response — use proto_send_output with history flag */ - proto_send_output(c->fd, unit_name, data, 1); + /* Escape the data for JSON */ + char escaped[PROTO_MAX_MSG]; + int j = 0; + for (int i = 0; data[i] && j < (int)sizeof(escaped) - 2; i++) { + switch (data[i]) { + case '"': escaped[j++] = '\\'; escaped[j++] = '"'; break; + case '\\': escaped[j++] = '\\'; escaped[j++] = '\\'; break; + case '\n': escaped[j++] = '\\'; escaped[j++] = 'n'; break; + case '\r': escaped[j++] = '\\'; escaped[j++] = 'r'; break; + default: escaped[j++] = data[i]; break; + } + } + escaped[j] = '\0'; + + /* Send response envelope like other request/response commands */ + char buf[PROTO_MAX_MSG]; + snprintf(buf, sizeof(buf), + "{\"type\":\"ok\",\"data\":\"%s\"}", escaped); + proto_send(c->fd, buf); } /* ── cmd_broadcast ───────────────────────────────────────────────────────── */ @@ -531,7 +548,7 @@ static void cmd_action(Client *c, const char *unit_name, } if (pid == 0) { /* Child: exec the script */ - execl("/bin/bash", "bash", script, NULL); + execl("/bin/bash", "bash", "-c", script, NULL); _exit(127); } -- cgit v1.2.3