diff --git a/src/main/lkt.c b/src/main/lkt.c index fb2b8c25b184aad6091ec859a5df26d1af12ea05..6ed06b2230d591b1e0892b0d3641df14e0efbe82 100644 --- a/src/main/lkt.c +++ b/src/main/lkt.c @@ -249,15 +249,29 @@ prev__(struct lkt_cmd_args *args) } noreturn void -kill__(struct lkt_cmd_args *args) +simple_send_with_password__(const char *cmd) { - if (args->argc != 0) - fail("Invalid argument, the previous command takes no arguments"); if (!password) fail("Password not provided"); - static const char cmd__[] = "password %s\nkill\nclose\n"; + static const char cmd__[] = "password %s\n%s\nclose\n"; FILE *sock = lkt_connect(); - exit(write_socket_format(sock, cmd__, password)); + exit(write_socket_format(sock, cmd__, password, cmd)); +} + +noreturn void +restart__(struct lkt_cmd_args *args) +{ + if (args->argc != 0) + fail("Invalid argument, the previous command takes no arguments"); + simple_send_with_password__("restart"); +} + +noreturn void +kill__(struct lkt_cmd_args *args) +{ + if (args->argc != 0) + fail("Invalid argument, the previous command takes no arguments"); + simple_send_with_password__("kill"); } noreturn void @@ -284,13 +298,13 @@ rescan_or_update__(struct lkt_cmd_args *args, const char *cmd) abort(); } -noreturn inline void +noreturn void rescan__(struct lkt_cmd_args *args) { rescan_or_update__(args, "rescan"); } -noreturn inline void +noreturn void update__(struct lkt_cmd_args *args) { rescan_or_update__(args, "update"); @@ -355,6 +369,23 @@ error: exit(EXIT_FAILURE); } +noreturn void +ping__(struct lkt_cmd_args *args) +{ + if (args->argc != 0) + fail("Invalid argument, the ping command takes no arguments"); + char buff[6] = {0}; + FILE *sock = lkt_connect(); + if (write_socket(sock, "ping\nclose\n", sizeof("ping\n"))) + fail("Failed to send the ping to lektord"); + if (read_socket(sock, buff, 6 * sizeof(char)) <= 0) + fail("Failed to recieve the response of lektord"); + if (strncmp(buff, "OK", 2)) + fail("ACK"); + write(1, "OK\n", sizeof("OK\n")); + exit(EXIT_SUCCESS); +} + noreturn void current__(struct lkt_cmd_args *args) { @@ -971,6 +1002,24 @@ static struct lkt_cmd_opt options_search[] = { LKT_OPT_NULL, }; +static struct lkt_cmd_opt options_admin[] = { + { .name = "ping", .call = ping__, .help = "Only pings the server" }, + { .name = "kill", .call = kill__, .help = "Kills the lektord server" }, + { .name = "restart", .call = restart__, .help = "Restarts the lektord server" }, + { .name = "rescan", .call = rescan__, .help = "Scan the filesystem and update the database with present files" }, + { .name = "update", .call = update__, .help = "Update the database with files present on Kurisu" }, + LKT_OPT_NULL, +}; + +noreturn void +admin__(struct lkt_cmd_args *args) +{ + if (args->argc == 0) + fail("Invalid argument, you must specify a sub command for the admin command"); + + lkt_cmd_parse(options_admin, args->argc, args->argv, help); +} + noreturn void queue__(struct lkt_cmd_args *args) { @@ -1009,7 +1058,7 @@ static struct lkt_cmd_opt options_[] = { { .name = "stop", .call = stop__, .help = "Stop playing the queue, won't close lektord's window" }, { .name = "plt", .call = plt__, .help = "The playlist sub-command", .sub = options_plt }, { .name = "search", .call = search__, .help = "The search sub-command", .sub = options_search }, - { .name = "kill", .call = kill__, .help = "Kills the lektord server" }, + { .name = "admin", .call = admin__, .help = "Administration commands", .sub = options_admin }, LKT_OPT_NULL, };