From 3dc8d84af1753b41fe37b3b3954731379dc579aa Mon Sep 17 00:00:00 2001 From: David Härdeman Date: Thu, 9 Jul 2020 22:58:22 +0200 Subject: Implement new command for minecctl --- minecctl/misc-commands.c | 140 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 117 insertions(+), 23 deletions(-) (limited to 'minecctl/misc-commands.c') diff --git a/minecctl/misc-commands.c b/minecctl/misc-commands.c index 5a6fe3a..34cbedf 100644 --- a/minecctl/misc-commands.c +++ b/minecctl/misc-commands.c @@ -149,26 +149,17 @@ out: return rv; } -static bool create_config_tree(int xfd) +static bool create_server_cfg(int sfd, int cfd, const char *name, + const char *filename) { - int mfd = -1, sfd = -1, efd = -1, cfd = -1; + int efd = -1; bool rv = false; - mfd = open_subdir(xfd, "minecproxy", true); - if (mfd < 0) - goto out; - - sfd = open_subdir(mfd, "servers", true); - if (sfd < 0) - goto out; - - if (!write_cfg_file(sfd, "README.TXT", ___examples_README_TXT, - ___examples_README_TXT_len)) - goto out; - - efd = open_subdir(sfd, "example", true); - if (efd < 0) + efd = open_subdir(sfd, name, true); + if (efd < 0) { + error("Failed to create configuration directory \"%s\"", name); goto out; + } if (!write_cfg_file(efd, "eula.txt", ___examples_eula_txt, ___examples_eula_txt_len)) @@ -182,19 +173,48 @@ static bool create_config_tree(int xfd) if (!create_link(efd, "server.jar", "../server.jar")) goto out; - cfd = open_subdir(mfd, "config", true); - if (cfd < 0) - goto out; - - if (!write_cfg_file(cfd, "example.mcserver", + if (!write_cfg_file(cfd, filename, ___examples_example_mcserver, ___examples_example_mcserver_len)) goto out; + rv = true; + +out: + if (efd >= 0) + close(efd); + + return rv; +} + +static bool create_config_tree(int xfd) +{ + int mfd = -1, sfd = -1, cfd = -1; + bool rv = false; + + mfd = open_subdir(xfd, "minecproxy", true); + if (mfd < 0) + goto out; + + sfd = open_subdir(mfd, "servers", true); + if (sfd < 0) + goto out; + + if (!write_cfg_file(sfd, "README.TXT", ___examples_README_TXT, + ___examples_README_TXT_len)) + goto out; + + cfd = open_subdir(mfd, "config", true); + if (cfd < 0) + goto out; + if (!write_cfg_file(cfd, "minecproxy.conf", ___examples_minecproxy_conf, ___examples_minecproxy_conf_len)) goto out; + if (!create_server_cfg(sfd, cfd, "example", "example.mcserver")) + goto out; + info("Created configuration in $XDG_CONFIG_HOME/minecproxy"); rv = true; @@ -203,8 +223,6 @@ out: close(mfd); if (sfd >= 0) close(sfd); - if (efd >= 0) - close(efd); if (cfd >= 0) close(cfd); return rv; @@ -251,6 +269,82 @@ out: return rv; } +static bool valid_name(const char *name) +{ + const char *f = name; + + if (empty_str(name)) { + error("Empty server name given"); + return false; + } + + while (*f != '\0') { + if ((*f >= 'a' && *f <= 'z') || + (*f >= 'A' && *f <= 'Z') || + (*f >= '0' && *f <= '9') || + (*f == ':') || (*f == '-') || + (*f == '_') || (*f == '.') || + (*f == '\\')) { + f++; + continue; + } + error("Server name \"%s\" contains invalid characters", name); + return false; + } + + if ((f - name) > (256 - STRLEN("minecserver@"))) { + error("Server name \"%s\" is too long", name); + return false; + } + + return true; +} + +bool do_new(struct cfg *cfg) +{ + const char *name = cfg->commands[0]; + struct server *server; + int sfd = -1; + bool rv = false; + + if (!valid_name(name)) + return false; + + char filename[strlen(name) + STRLEN(".mcserver") + 1]; + sprintf(filename, "%s.mcserver", name); + + server_load_all_known(cfg); + + if (!cfg->dir) + goto out; + + list_for_each_entry(server, &cfg->servers, list) { + if (streq(server->name, name)) { + error("Server \"%s\" already exists", name); + goto out; + } + } + + sfd = open_subdir(dirfd(cfg->dir), "../servers", false); + if (sfd < 0) { + error("Failed to open configuration directory \"servers\""); + goto out; + } + + if (!create_server_cfg(sfd, dirfd(cfg->dir), name, filename)) + goto out; + + info("Created server configuration %s", name); + + rv = true; + +out: + if (sfd >= 0) + close(sfd); + + return rv; +} + bool do_list(struct cfg *cfg) { struct server *server; -- cgit v1.2.3