]>
jfr.im git - irc/quakenet/newserv.git/blob - glines/glines_store.c
2 #include "../lib/version.h"
3 #include "../core/schedule.h"
4 #include "../control/control.h"
9 static int glstore_savefile(const char *file
) {
14 fp
= fopen(file
, "w");
21 for (gl
= glinelist
; gl
; gl
= gl
->next
) {
22 fprintf(fp
, "%s %jd,%jd,%jd,%d,%s,%s\n",
23 glinetostring(gl
), (intmax_t)gl
->expire
, (intmax_t)gl
->lastmod
, (intmax_t)gl
->lifetime
,
24 (gl
->flags
& GLINE_ACTIVE
) ? 1 : 0,
25 gl
->creator
->content
, gl
->reason
? gl
->reason
->content
: "");
34 static int glstore_loadfile(const char *file
) {
36 char mask
[512], creator
[512], reason
[512];
37 intmax_t expire
, lastmod
, lifetime
;
41 fp
= fopen(file
, "r");
49 if (fscanf(fp
, "%[^ ]%jd,%jd,%jd,%d,%[^,],%[^\n]\n", mask
, &expire
, &lastmod
, &lifetime
, &active
, creator
, reason
) != 7)
57 continue; /* Don't update existing glines. */
64 gl
->creator
= getsstring(creator
, 512);
66 gl
->flags
|= active
? GLINE_ACTIVE
: 0;
68 gl
->reason
= getsstring(reason
, 512);
70 gl
->lastmod
= lastmod
;
71 gl
->lifetime
= lifetime
;
82 int glstore_save(void) {
83 char path
[512], srcfile
[512], dstfile
[512];
86 snprintf(path
, sizeof(path
), "%s.temp", GLSTORE_PATH_PREFIX
);
88 count
= glstore_savefile(path
);
91 Error("glines", ERR_ERROR
, "Could not save glines to %s", path
);
95 for (i
= GLSTORE_SAVE_FILES
; i
> 0; i
--) {
96 snprintf(srcfile
, sizeof(srcfile
), "%s.%d", GLSTORE_PATH_PREFIX
, i
- 1);
97 snprintf(dstfile
, sizeof(dstfile
), "%s.%d", GLSTORE_PATH_PREFIX
, i
);
98 (void) rename(srcfile
, dstfile
);
101 (void) rename(path
, srcfile
);
106 int glstore_load(void) {
108 snprintf(path
, sizeof(path
), "%s.0", GLSTORE_PATH_PREFIX
);
110 return glstore_loadfile(path
);
113 static int glines_cmdsaveglines(void *source
, int cargc
, char **cargv
) {
114 nick
*sender
= source
;
117 count
= glstore_save();
120 controlreply(sender
, "An error occured while saving G-Lines.");
122 controlreply(sender
, "Saved %d G-Line%s.", count
, (count
== 1) ? "" : "s");
127 static int glines_cmdloadglines(void *source
, int cargc
, char **cargv
) {
128 nick
*sender
= source
;
131 count
= glstore_load();
134 controlreply(sender
, "An error occured while loading the G-Lines file.");
136 controlreply(sender
, "Loaded %d G-Line%s.", count
, (count
== 1) ? "" : "s");
141 static void glines_sched_save(void *arg
) {
146 registercontrolhelpcmd("loadglines", NO_DEVELOPER
, 0, glines_cmdloadglines
, "Usage: loadglines\nForce load of glines.");
147 registercontrolhelpcmd("saveglines", NO_DEVELOPER
, 0, glines_cmdsaveglines
, "Usage: saveglines\nForce save of glines.");
149 schedulerecurring(time(NULL
), 0, GLSTORE_SAVE_INTERVAL
, &glines_sched_save
, NULL
);
155 deregistercontrolcmd("loadglines", glines_cmdloadglines
);
156 deregistercontrolcmd("saveglines", glines_cmdsaveglines
);
158 deleteschedule(NULL
, glines_sched_save
, NULL
);