X-Git-Url: https://jfr.im/git/irc/quakenet/newserv.git/blobdiff_plain/cb5815228b08881b5ede2063a29241431d7a129a..40814391c7b017da05d4daa2895093e5099801c7:/glines/glines_store.c diff --git a/glines/glines_store.c b/glines/glines_store.c index fb7446f1..a52f51bc 100644 --- a/glines/glines_store.c +++ b/glines/glines_store.c @@ -1,6 +1,11 @@ #include +#include "../lib/version.h" +#include "../core/schedule.h" +#include "../control/control.h" #include "glines.h" +MODULE_VERSION(""); + static int glstore_savefile(const char *file) { FILE *fp; gline *gl; @@ -16,8 +21,8 @@ static int glstore_savefile(const char *file) { for (gl = glinelist; gl; gl = gl->next) { fprintf(fp, "%s %jd,%jd,%jd,%d,%s,%s\n", glinetostring(gl), (intmax_t)gl->expire, (intmax_t)gl->lastmod, (intmax_t)gl->lifetime, - gl->flags & ~(GLINE_HOSTMASK | GLINE_IPMASK | GLINE_BADCHAN | GLINE_REALNAME), - gl->creator->content, gl->reason->content); + (gl->flags & GLINE_ACTIVE) ? 1 : 0, + gl->creator->content, gl->reason ? gl->reason->content : ""); count++; } @@ -30,7 +35,7 @@ static int glstore_loadfile(const char *file) { FILE *fp; char mask[512], creator[512], reason[512]; intmax_t expire, lastmod, lifetime; - int flags, count; + int active, count; gline *gl; fp = fopen(file, "r"); @@ -41,7 +46,7 @@ static int glstore_loadfile(const char *file) { count = 0; while (!feof(fp)) { - if (fscanf(fp, "%[^ ]%jd,%jd,%jd,%d,%[^,],%[^\n]\n", mask, &expire, &lastmod, &lifetime, &flags, creator, reason) != 7) + if (fscanf(fp, "%[^ ]%jd,%jd,%jd,%d,%[^,],%[^\n]\n", mask, &expire, &lastmod, &lifetime, &active, creator, reason) != 7) continue; count++; @@ -58,7 +63,7 @@ static int glstore_loadfile(const char *file) { gl->creator = getsstring(creator, 512); - gl->flags |= flags; + gl->flags |= active ? GLINE_ACTIVE : 0; gl->reason = getsstring(reason, 512); gl->expire = expire; @@ -69,6 +74,8 @@ static int glstore_loadfile(const char *file) { glinelist = gl; } + fclose(fp); + return count; } @@ -103,3 +110,50 @@ int glstore_load(void) { return glstore_loadfile(path); } +static int glines_cmdsaveglines(void *source, int cargc, char **cargv) { + nick *sender = source; + int count; + + count = glstore_save(); + + if (count < 0) + controlreply(sender, "An error occured while saving G-Lines."); + else + controlreply(sender, "Saved %d G-Line%s.", count, (count == 1) ? "" : "s"); + + return CMD_OK; +} + +static int glines_cmdloadglines(void *source, int cargc, char **cargv) { + nick *sender = source; + int count; + + count = glstore_load(); + + if (count < 0) + controlreply(sender, "An error occured while loading the G-Lines file."); + else + controlreply(sender, "Loaded %d G-Line%s.", count, (count == 1) ? "" : "s"); + + return CMD_OK; +} + +static void glines_sched_save(void *arg) { + glstore_save(); +} + +void _init() { + registercontrolhelpcmd("loadglines", NO_DEVELOPER, 0, glines_cmdloadglines, "Usage: loadglines\nForce load of glines."); + registercontrolhelpcmd("saveglines", NO_DEVELOPER, 0, glines_cmdsaveglines, "Usage: saveglines\nForce save of glines."); + + schedulerecurring(time(NULL), 0, GLSTORE_SAVE_INTERVAL, &glines_sched_save, NULL); + + glstore_load(); +} + +void _fini() { + deregistercontrolcmd("loadglines", glines_cmdloadglines); + deregistercontrolcmd("saveglines", glines_cmdsaveglines); + + deleteschedule(NULL, glines_sched_save, NULL); +}