summaryrefslogtreecommitdiff
path: root/minecctl/minecctl-rcon.c
diff options
context:
space:
mode:
authorDavid Härdeman <david@hardeman.nu>2020-06-26 23:07:28 +0200
committerDavid Härdeman <david@hardeman.nu>2020-06-26 23:07:28 +0200
commit957634c7c434b6c8d696ca8f4098e96a201fdfb0 (patch)
treeb29f8a70453dcd7bab00079660330a47dd633428 /minecctl/minecctl-rcon.c
parenta6b905895fef7bdd51781def5c003a95983a231e (diff)
Allow commands to return success or not rather than just dying
Diffstat (limited to 'minecctl/minecctl-rcon.c')
-rw-r--r--minecctl/minecctl-rcon.c66
1 files changed, 42 insertions, 24 deletions
diff --git a/minecctl/minecctl-rcon.c b/minecctl/minecctl-rcon.c
index eb8d335..c387b38 100644
--- a/minecctl/minecctl-rcon.c
+++ b/minecctl/minecctl-rcon.c
@@ -154,7 +154,7 @@ error:
return -1;
}
-static void
+static bool
send_cmd(int sfd, const char *cmd)
{
char buf[4096];
@@ -163,12 +163,18 @@ send_cmd(int sfd, const char *cmd)
send_msg(sfd, buf, sizeof(buf), RCON_PACKET_COMMAND, cmd, &rtype, &reply);
- if (rtype != RCON_PACKET_RESPONSE)
+ if (rtype != RCON_PACKET_RESPONSE) {
die("Invalid return code: %" PRIi32, rtype);
- else if (use_colors)
+ return false;
+ }
+
+
+ if (use_colors)
info("%s%s%s", ANSI_GREY, reply, ANSI_NORMAL);
else
info("%s", reply);
+
+ return true;
}
static void
@@ -234,7 +240,7 @@ get_one_status(int fd, char *buf, size_t len, const char *cmd,
return false;
}
-void
+bool
do_status(struct cfg *cfg) {
char buf[4096];
char tbuf[4096];
@@ -248,7 +254,7 @@ do_status(struct cfg *cfg) {
server = server_get_default(cfg);
fd = rcon_login(cfg, server);
if (fd < 0)
- return;
+ return false;
if (get_one_status(fd, buf, sizeof(buf), "seed", 1,
"Seed : [ %[^]]]", &reply, tbuf))
@@ -298,11 +304,14 @@ do_status(struct cfg *cfg) {
info("Banned IPs: %u", bannedips);
else if (streq(reply, "There are no bans"))
info("Banned IPs: 0");
+
+ return true;
}
-void
+bool
do_ping(_unused_ struct cfg *cfg) {
die("Not implemented");
+ return false;
}
static bool
@@ -326,14 +335,15 @@ get_player_count(int fd, unsigned *current, unsigned *max)
return true;
}
-static void
+static bool
stop_one_server(struct cfg *cfg, struct server *server)
{
int fd;
+ bool rv;
fd = rcon_login(cfg, server);
if (fd < 0)
- return;
+ return false;
if (cfg->force_stop) {
unsigned current;
@@ -341,28 +351,30 @@ stop_one_server(struct cfg *cfg, struct server *server)
if (!get_player_count(fd, &current, NULL)) {
error("%s: unable to get player count, not stopping",
server->name);
- return;
+ return false;
} else if (current > 0) {
error("%s: has active players (use -f to force)",
server->name);
- return;
+ return false;
}
}
info("%s: sending stop command", server->name);
- send_cmd(fd, "stop");
+ rv = send_cmd(fd, "stop");
close(fd);
+
+ return rv;
}
-void
+bool
do_stop(struct cfg *cfg) {
struct server *server;
server = server_get_default(cfg);
- stop_one_server(cfg, server);
+ return stop_one_server(cfg, server);
}
-void
+bool
do_stop_all(struct cfg *cfg) {
struct server *server;
@@ -370,9 +382,11 @@ do_stop_all(struct cfg *cfg) {
server_read_config(cfg, server);
stop_one_server(cfg, server);
}
+
+ return true;
}
-void
+bool
do_pcount(struct cfg *cfg) {
int fd;
unsigned current, max;
@@ -381,15 +395,18 @@ do_pcount(struct cfg *cfg) {
server = server_get_default(cfg);
fd = rcon_login(cfg, server);
if (fd < 0)
- return;
+ return false;
- if (get_player_count(fd, &current, &max))
+ if (get_player_count(fd, &current, &max)) {
info("Players: %u/%u", current, max);
- else
+ return true;
+ } else {
die("Failed to get player count");
+ return false;
+ }
}
-void
+bool
do_console(struct cfg *cfg)
{
char *prompt;
@@ -400,7 +417,7 @@ do_console(struct cfg *cfg)
server = server_get_default(cfg);
fd = rcon_login(cfg, server);
if (fd < 0)
- return;
+ return false;
prompt = alloca(strlen(program_invocation_short_name) +
STRLEN(" (") + strlen(server->name) + STRLEN("): ") + 1);
@@ -431,13 +448,14 @@ do_console(struct cfg *cfg)
/* The server waits for us to close the connection */
break;
- xfree(cmd);
+ free(cmd);
}
xfree(cmd);
+ return true;
}
-void
+bool
do_command(struct cfg *cfg) {
int fd;
struct server *server;
@@ -445,8 +463,8 @@ do_command(struct cfg *cfg) {
server = server_get_default(cfg);
fd = rcon_login(cfg, server);
if (fd < 0)
- return;
+ return false;
- send_cmd(fd, cfg->cmdstr);
+ return send_cmd(fd, cfg->cmdstr);
}