Session checkpoint on webui-plugin: - engine dump (nautctl dump) + engine endgame integration - per-file move locations persistence; torrent-level "Set location" with reset/keep-relative/leave-separate handling + residual prune - Lua: naut.get_labels, define_settings/get_setting (script_host struct) - daemon-owned labels (category+tags) + taxonomy persistence; webui write-through - fix: pausing a completed/seeding torrent now sticks (stop wins over result) - automation tab responsive layout; anime_sort label gating + settings Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
167 lines
5.2 KiB
C
167 lines
5.2 KiB
C
#include "naut/rpc.h"
|
|
|
|
#include <jansson.h>
|
|
#include <signal.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
|
|
#define DEFAULT_SOCKET "/tmp/nautd.sock"
|
|
|
|
static void usage(const char *program) {
|
|
fprintf(stderr,
|
|
"usage: %s [--socket PATH] COMMAND [ARGS]\n"
|
|
"\n"
|
|
"commands:\n"
|
|
" add SOURCE OUTPUT [IP:PORT ...]\n"
|
|
" list\n"
|
|
" show TORRENT_ID\n"
|
|
" dump TORRENT_ID\n"
|
|
" remove TORRENT_ID\n"
|
|
" script PATH | unscript\n"
|
|
" status | events | shutdown\n"
|
|
" METHOD [PARAMS_JSON] (raw RPC)\n", program);
|
|
}
|
|
|
|
static json_t *parse_params(const char *text) {
|
|
if (!text) return json_object();
|
|
json_error_t error;
|
|
return json_loads(text, JSON_REJECT_DUPLICATES | JSON_DECODE_ANY, &error);
|
|
}
|
|
|
|
static int print_json(json_t *json) {
|
|
if (!json) return 1;
|
|
if (json_dumpf(json, stdout, JSON_INDENT(2) | JSON_SORT_KEYS) != 0)
|
|
return 1;
|
|
putchar('\n');
|
|
return 0;
|
|
}
|
|
|
|
static int stream_events(const char *socket_path) {
|
|
int fd = naut_rpc_connect_unix(socket_path);
|
|
if (fd < 0) return 1;
|
|
json_t *request = json_pack("{s:s,s:o}", "method", "subscribe",
|
|
"params", json_object());
|
|
if (!request ||
|
|
naut_rpc_send_json(fd, NAUT_RPC_REQUEST, request) != NAUT_OK) {
|
|
json_decref(request);
|
|
close(fd);
|
|
return 1;
|
|
}
|
|
json_decref(request);
|
|
for (;;) {
|
|
naut_rpc_frame_type type;
|
|
json_t *payload = NULL;
|
|
if (naut_rpc_recv_json(fd, &type, &payload) != NAUT_OK) {
|
|
close(fd);
|
|
return 1;
|
|
}
|
|
print_json(payload);
|
|
fflush(stdout);
|
|
json_decref(payload);
|
|
}
|
|
}
|
|
|
|
static bool parse_id(const char *text, json_int_t *id) {
|
|
if (!text || !*text || *text == '-') return false;
|
|
char *end = NULL;
|
|
unsigned long long value = strtoull(text, &end, 10);
|
|
if (!end || *end || value > (unsigned long long)INT64_MAX) return false;
|
|
*id = (json_int_t)value;
|
|
return true;
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
const char *socket_path = getenv("NAUT_SOCKET");
|
|
if (!socket_path || !*socket_path) socket_path = DEFAULT_SOCKET;
|
|
int arg = 1;
|
|
if (arg < argc && strcmp(argv[arg], "--socket") == 0) {
|
|
if (arg + 1 >= argc) {
|
|
usage(argv[0]);
|
|
return 2;
|
|
}
|
|
socket_path = argv[arg + 1];
|
|
arg += 2;
|
|
}
|
|
if (arg >= argc) {
|
|
usage(argv[0]);
|
|
return 2;
|
|
}
|
|
signal(SIGPIPE, SIG_IGN);
|
|
const char *method = argv[arg++];
|
|
if (strcmp(method, "events") == 0)
|
|
return stream_events(socket_path);
|
|
|
|
json_t *params = NULL;
|
|
bool raw_dump = false;
|
|
if (strcmp(method, "add") == 0) {
|
|
if (arg + 1 >= argc) { usage(argv[0]); return 2; }
|
|
method = "add_torrent";
|
|
params = json_pack("{s:s,s:s}", "source", argv[arg],
|
|
"output", argv[arg + 1]);
|
|
arg += 2;
|
|
json_t *peers = json_array();
|
|
if (!params || !peers) {
|
|
json_decref(params);
|
|
json_decref(peers);
|
|
return 1;
|
|
}
|
|
while (arg < argc)
|
|
json_array_append_new(peers, json_string(argv[arg++]));
|
|
json_object_set_new(params, "peers", peers);
|
|
} else if (strcmp(method, "list") == 0) {
|
|
if (arg != argc) { usage(argv[0]); return 2; }
|
|
method = "torrents";
|
|
params = json_object();
|
|
} else if (strcmp(method, "show") == 0 ||
|
|
strcmp(method, "dump") == 0 ||
|
|
strcmp(method, "remove") == 0) {
|
|
json_int_t id;
|
|
if (arg + 1 != argc || !parse_id(argv[arg], &id)) {
|
|
usage(argv[0]);
|
|
return 2;
|
|
}
|
|
if (strcmp(method, "show") == 0) method = "torrent";
|
|
else if (strcmp(method, "dump") == 0) { method = "dump_torrent"; raw_dump = true; }
|
|
else method = "remove_torrent";
|
|
params = json_pack("{s:I}", "torrent_id", id);
|
|
} else if (strcmp(method, "script") == 0) {
|
|
if (arg + 1 != argc) { usage(argv[0]); return 2; }
|
|
method = "load_script";
|
|
params = json_pack("{s:s}", "path", argv[arg]);
|
|
} else if (strcmp(method, "unscript") == 0) {
|
|
if (arg != argc) { usage(argv[0]); return 2; }
|
|
method = "unload_script";
|
|
params = json_object();
|
|
} else {
|
|
if (arg + 1 < argc) { usage(argv[0]); return 2; }
|
|
params = parse_params(arg < argc ? argv[arg] : NULL);
|
|
}
|
|
if (!params) {
|
|
fprintf(stderr, "nautctl: invalid JSON parameters\n");
|
|
return 2;
|
|
}
|
|
json_t *reply = NULL;
|
|
naut_err error = naut_rpc_call(socket_path, method, params, &reply);
|
|
json_decref(params);
|
|
if (error != NAUT_OK) {
|
|
fprintf(stderr, "nautctl: RPC failed: %s\n", naut_strerror(error));
|
|
return 1;
|
|
}
|
|
bool ok = json_is_true(json_object_get(reply, "ok"));
|
|
int result;
|
|
/* `dump` returns a multi-line text blob; print it raw instead of escaped JSON. */
|
|
const char *dump = raw_dump
|
|
? json_string_value(json_object_get(
|
|
json_object_get(reply, "result"), "dump"))
|
|
: NULL;
|
|
if (dump) {
|
|
fputs(dump, stdout);
|
|
result = 0;
|
|
} else {
|
|
result = print_json(reply);
|
|
}
|
|
json_decref(reply);
|
|
return result || !ok;
|
|
}
|