diff options
Diffstat (limited to 'minecctl/rcon-commands.c')
-rw-r--r-- | minecctl/rcon-commands.c | 122 |
1 files changed, 58 insertions, 64 deletions
diff --git a/minecctl/rcon-commands.c b/minecctl/rcon-commands.c index cf43c5e..b6a216f 100644 --- a/minecctl/rcon-commands.c +++ b/minecctl/rcon-commands.c @@ -16,8 +16,7 @@ #include "rcon-protocol.h" #include "misc.h" -static void -send_packet(int sfd, const char *buf, size_t len) +static void send_packet(int sfd, const char *buf, size_t len) { size_t off = 0; ssize_t r; @@ -38,8 +37,8 @@ send_packet(int sfd, const char *buf, size_t len) } /* Note: msg is null-terminated due to the mc protocol trailer */ -static void -read_packet(int sfd, char *buf, size_t len, int32_t *id, int32_t *type, const char **msg) +static void read_packet(int sfd, char *buf, size_t len, int32_t *id, + int32_t *type, const char **msg) { size_t off = 0; ssize_t r; @@ -69,16 +68,16 @@ read_packet(int sfd, char *buf, size_t len, int32_t *id, int32_t *type, const ch die("Failed to parse response: %s", error); } -static void -send_msg(int sfd, char *buf, size_t len, enum rcon_packet_type type, - const char *msg, enum rcon_packet_type *rtype, const char **reply) +static void send_msg(int sfd, char *buf, size_t len, enum rcon_packet_type type, + const char *msg, enum rcon_packet_type *rtype, + const char **reply) { static uint32_t rcon_packet_id = 1; size_t plen; int32_t id; - if (!rcon_protocol_create_packet(buf, len, &plen, - rcon_packet_id, type, msg)) + if (!rcon_protocol_create_packet(buf, len, &plen, rcon_packet_id, type, + msg)) die("Failed to create rcon packet"); send_packet(sfd, buf, plen); @@ -102,8 +101,7 @@ send_msg(int sfd, char *buf, size_t len, enum rcon_packet_type type, rcon_packet_id++; } -static int -rcon_login(struct cfg *cfg, struct server *server) +static int rcon_login(struct cfg *cfg, struct server *server) { char buf[4096]; int32_t rtype; @@ -121,7 +119,7 @@ rcon_login(struct cfg *cfg, struct server *server) if (fd < 0) { error("%s: unable to connect", server->name); goto error; - } + } if (!server->rcon_password) server->rcon_password = ask_password(); @@ -154,21 +152,20 @@ error: return -1; } -static bool -send_cmd(int sfd, const char *cmd) +static bool send_cmd(int sfd, const char *cmd) { char buf[4096]; int32_t rtype; const char *reply; - send_msg(sfd, buf, sizeof(buf), RCON_PACKET_COMMAND, cmd, &rtype, &reply); + send_msg(sfd, buf, sizeof(buf), RCON_PACKET_COMMAND, cmd, &rtype, + &reply); if (rtype != RCON_PACKET_RESPONSE) { die("Invalid return code: %" PRIi32, rtype); return false; } - if (use_colors) info("%s%s%s", ANSI_GREY, reply, ANSI_NORMAL); else @@ -177,13 +174,12 @@ send_cmd(int sfd, const char *cmd) return true; } -static void -eat_whitespace(char **pos) +static void eat_whitespace(char **pos) { char *end; size_t len; - while(isspace(**pos)) + while (isspace(**pos)) (*pos)++; len = strlen(*pos); @@ -203,24 +199,24 @@ eat_whitespace(char **pos) #define MCTIME_PER_HOUR 1000 #define MIN_PER_HOUR 60 -static inline unsigned -mctime_days(unsigned mctime) { +static inline unsigned mctime_days(unsigned mctime) +{ return (mctime / MCTIME_PER_DAY); } -static inline unsigned -mctime_hh(unsigned mctime) { +static inline unsigned mctime_hh(unsigned mctime) +{ return (mctime % MCTIME_PER_DAY) / MCTIME_PER_HOUR; } -static inline unsigned -mctime_mm(unsigned mctime) { +static inline unsigned mctime_mm(unsigned mctime) +{ return ((mctime % MCTIME_PER_HOUR) * MIN_PER_HOUR) / MCTIME_PER_HOUR; } -static bool -get_one_status(int fd, char *buf, size_t len, const char *cmd, - size_t argc, const char *replyscan, const char **reply, ...) +static bool get_one_status(int fd, char *buf, size_t len, const char *cmd, + size_t argc, const char *replyscan, + const char **reply, ...) { int32_t rtype; va_list ap; @@ -240,8 +236,8 @@ get_one_status(int fd, char *buf, size_t len, const char *cmd, return false; } -bool -do_status(struct cfg *cfg) { +bool do_status(struct cfg *cfg) +{ char buf[4096]; char tbuf[4096]; const char *reply; @@ -256,17 +252,17 @@ do_status(struct cfg *cfg) { if (fd < 0) return false; - if (get_one_status(fd, buf, sizeof(buf), "seed", 1, - "Seed : [ %[^]]]", &reply, tbuf)) + if (get_one_status(fd, buf, sizeof(buf), "seed", 1, "Seed : [ %[^]]]", + &reply, tbuf)) info("Seed: %s", tbuf); - + if (get_one_status(fd, buf, sizeof(buf), "difficulty", 1, "The difficulty is %s", &reply, tbuf)) info("Difficulty: %s", tbuf); if (get_one_status(fd, buf, sizeof(buf), "list", 2, - "There are %u of a max %u players online", - &reply, &cplayers, &maxplayers)) + "There are %u of a max %u players online", &reply, + &cplayers, &maxplayers)) info("Players: %u/%u", cplayers, maxplayers); if (get_one_status(fd, buf, sizeof(buf), "time query day", 1, @@ -275,20 +271,23 @@ do_status(struct cfg *cfg) { if (get_one_status(fd, buf, sizeof(buf), "time query gametime", 1, "The time is %u", &reply, >ime)) - info("World age: %ud:%02uh:%02um", - mctime_days(gtime), mctime_hh(gtime), mctime_mm(gtime)); + info("World age: %ud:%02uh:%02um", mctime_days(gtime), + mctime_hh(gtime), mctime_mm(gtime)); if (get_one_status(fd, buf, sizeof(buf), "time query daytime", 1, "The time is %u", &reply, >ime)) info("Current in-game time: %02uh:%02um", - mctime_hh(gtime + MCTIME_OFFSET), mctime_mm(gtime + MCTIME_OFFSET)); + mctime_hh(gtime + MCTIME_OFFSET), + mctime_mm(gtime + MCTIME_OFFSET)); if (get_one_status(fd, buf, sizeof(buf), "datapack list enabled", 2, - "There are %u data packs enabled: %[^\n]", &reply, &epacks, tbuf)) + "There are %u data packs enabled: %[^\n]", &reply, + &epacks, tbuf)) info("Enabled data packs (%u): %s", epacks, tbuf); if (get_one_status(fd, buf, sizeof(buf), "datapack list available", 2, - "There are %u data packs available : %[^\n]", &reply, &apacks, tbuf)) + "There are %u data packs available : %[^\n]", &reply, + &apacks, tbuf)) info("Available data packs (%u): %s", apacks, tbuf); else if (streq(reply, "There are no more data packs available")) info("Available data packs: none"); @@ -308,22 +307,21 @@ do_status(struct cfg *cfg) { return true; } -bool -do_ping(_unused_ struct cfg *cfg) { +bool do_ping(_unused_ struct cfg *cfg) +{ die("Not implemented"); return false; } -static bool -get_player_count(int fd, unsigned *current, unsigned *max) +static bool get_player_count(int fd, unsigned *current, unsigned *max) { char buf[4096]; const char *reply; unsigned c, m; if (!get_one_status(fd, buf, sizeof(buf), "list", 2, - "There are %u of a max %u players online", - &reply, &c, &m)) + "There are %u of a max %u players online", &reply, + &c, &m)) return false; if (current) @@ -335,8 +333,7 @@ get_player_count(int fd, unsigned *current, unsigned *max) return true; } -static bool -stop_one_server(struct cfg *cfg, struct server *server) +static bool stop_one_server(struct cfg *cfg, struct server *server) { int fd; bool rv; @@ -366,16 +363,16 @@ stop_one_server(struct cfg *cfg, struct server *server) return rv; } -bool -do_stop(struct cfg *cfg) { +bool do_stop(struct cfg *cfg) +{ struct server *server; server = server_get_default(cfg); return stop_one_server(cfg, server); } -bool -do_stop_all(struct cfg *cfg) { +bool do_stop_all(struct cfg *cfg) +{ struct server *server; list_for_each_entry(server, &cfg->servers, list) { @@ -386,8 +383,7 @@ do_stop_all(struct cfg *cfg) { return true; } -bool -do_rcon_pcount(struct cfg *cfg, unsigned *online, unsigned *max) +bool do_rcon_pcount(struct cfg *cfg, unsigned *online, unsigned *max) { struct server *server; bool rv; @@ -399,14 +395,13 @@ do_rcon_pcount(struct cfg *cfg, unsigned *online, unsigned *max) return false; rv = get_player_count(fd, online, max); - + close(fd); return rv; } -bool -do_console(struct cfg *cfg) +bool do_console(struct cfg *cfg) { char *prompt; char *cmd; @@ -418,8 +413,8 @@ do_console(struct cfg *cfg) if (fd < 0) return false; - prompt = alloca(strlen(program_invocation_short_name) + - STRLEN(" (") + strlen(server->name) + STRLEN("): ") + 1); + prompt = alloca(strlen(program_invocation_short_name) + STRLEN(" (") + + strlen(server->name) + STRLEN("): ") + 1); sprintf(prompt, "%s (%s): ", program_invocation_short_name, server->name); @@ -437,8 +432,8 @@ do_console(struct cfg *cfg) continue; } - if (streq(tmp, "q") || streq(tmp, "quit") || - streq(tmp, "/q") || streq(tmp, "/quit")) + if (streq(tmp, "q") || streq(tmp, "quit") || streq(tmp, "/q") || + streq(tmp, "/quit")) break; send_cmd(fd, tmp); @@ -454,8 +449,8 @@ do_console(struct cfg *cfg) return true; } -bool -do_command(struct cfg *cfg) { +bool do_command(struct cfg *cfg) +{ int fd; struct server *server; @@ -466,4 +461,3 @@ do_command(struct cfg *cfg) { return send_cmd(fd, cfg->cmdstr); } - |