+static MODCMD_FUNC(cmd_renamehost) {
+ struct handle_info *hi;
+ if (!(hi = user->handle_info)) {
+ reply("NSMSG_MUST_AUTH");
+ return 0;
+ }
+ //old fakehost name
+ char *slfh_name = argv[1];
+ char *tlfh_name = strchr(argv[1], '.');
+ if(!tlfh_name) {
+ reply("HSMSG_TOPLEVEL_INVALID", slfh_name);
+ return 0;
+ }
+ *tlfh_name = '\0';
+ tlfh_name++;
+ if(strchr(tlfh_name, '.')) {
+ reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name);
+ return 0;
+ }
+ struct hs_toplevel *tlfh;
+ for(tlfh = toplevels; tlfh; tlfh = tlfh->next) {
+ if(!irccasecmp(tlfh->fakehost, tlfh_name)) break;
+ }
+ if(!tlfh) {
+ reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name);
+ return 0;
+ }
+ //new fakehost name
+ char *new_slfh_name = argv[2];
+ char *new_tlfh_name = strchr(argv[2], '.');
+ if(!new_tlfh_name) {
+ reply("HSMSG_TOPLEVEL_INVALID", new_slfh_name);
+ return 0;
+ }
+ *new_tlfh_name = '\0';
+ new_tlfh_name++;
+ if(strchr(new_tlfh_name, '.')) {
+ new_tlfh_name--;
+ *new_tlfh_name = '.';
+ reply("HSMSG_TOPLEVEL_INVALID", new_slfh_name);
+ return 0;
+ }
+ if(!irccasecmp(slfh_name, "*")) {
+ if(!check_management_access(hi, (hostserv_conf.manager_can_rename_toplevel ? tlfh : NULL), NULL)) { /* manager access is enough to delete whole toplevel? */
+ reply("HSMSG_ACCESS_DENIED");
+ return 0;
+ }
+ if(irccasecmp(new_slfh_name, "*")) {
+ //can't rename toplevel into secondlevel fakehost!
+ new_tlfh_name--;
+ *new_tlfh_name = '.';
+ reply("HSMSG_TOPLEVEL_INVALID", new_slfh_name);
+ return 0;
+ }
+ struct hs_toplevel *ctlfh;
+ for(ctlfh = toplevels; ctlfh; ctlfh = ctlfh->next) {
+ if(!irccasecmp(ctlfh->fakehost, new_tlfh_name)) break;
+ }
+ if(ctlfh) {
+ reply("HSMSG_FAKEHOST_TOPLEVEL_ALREADY_EXISTS", new_slfh_name, new_tlfh_name);
+ return 0;
+ }
+ if(strcmp(new_tlfh_name, tlfh->fakehost))
+ hs_rename_toplevel(tlfh, new_tlfh_name);
+ reply("HSMSG_FAKEHOST_RENAMED", slfh_name, tlfh_name, new_slfh_name, new_tlfh_name);
+ } else {
+ struct hs_secondlevel *slfh;
+ for(slfh = tlfh->secondlevel; slfh; slfh = slfh->next) {
+ if(!irccasecmp(slfh->fakehost, slfh_name)) break;
+ }
+ if(!slfh) {
+ reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name);
+ return 0;
+ }
+ if(!check_management_access(hi, tlfh, (hostserv_conf.manager_can_rename_secondlevel ? slfh : NULL))) {
+ reply("HSMSG_ACCESS_DENIED");
+ return 0;
+ }
+ if(irccasecmp(new_tlfh_name, tlfh_name)) {
+ //can't rename toplevel and secondlevel fakehost with one command!
+ new_tlfh_name--;
+ *new_tlfh_name = '.';
+ reply("HSMSG_TOPLEVEL_INVALID", new_slfh_name);
+ }
+ struct hs_secondlevel *cslfh;
+ for(cslfh = tlfh->secondlevel; cslfh; cslfh = cslfh->next) {
+ if(!irccasecmp(cslfh->fakehost, new_slfh_name)) break;
+ }
+ if(cslfh) {
+ reply("HSMSG_FAKEHOST_SECONDLEVEL_ALREADY_EXISTS", new_slfh_name, new_tlfh_name);
+ return 0;
+ }
+ if(strcmp(new_slfh_name, slfh->fakehost))
+ hs_rename_secondlevel(slfh, new_slfh_name);
+ reply("HSMSG_FAKEHOST_RENAMED", slfh_name, tlfh_name, new_slfh_name, new_tlfh_name);
+ }
+ return 1;
+}
+