]> jfr.im git - irc/quakenet/newserv.git/blob - glines/glines_store.c
Fix help text for smartgline.
[irc/quakenet/newserv.git] / glines / glines_store.c
1 #include <stdio.h>
2 #include "glines.h"
3
4 static int glstore_savefile(const char *file) {
5 FILE *fp;
6 gline *gl;
7 int count;
8
9 fp = fopen(file, "w");
10
11 if (!fp)
12 return -1;
13
14 count = 0;
15
16 for (gl = glinelist; gl; gl = gl->next) {
17 fprintf(fp, "%s %jd,%jd,%jd,%d,%s,%s\n",
18 glinetostring(gl), (intmax_t)gl->expire, (intmax_t)gl->lastmod, (intmax_t)gl->lifetime,
19 (gl->flags & GLINE_ACTIVE) ? 1 : 0,
20 gl->creator->content, gl->reason->content);
21 count++;
22 }
23
24 fclose(fp);
25
26 return count;
27 }
28
29 static int glstore_loadfile(const char *file) {
30 FILE *fp;
31 char mask[512], creator[512], reason[512];
32 intmax_t expire, lastmod, lifetime;
33 int active, count;
34 gline *gl;
35
36 fp = fopen(file, "r");
37
38 if (!fp)
39 return -1;
40
41 count = 0;
42
43 while (!feof(fp)) {
44 if (fscanf(fp, "%[^ ]%jd,%jd,%jd,%d,%[^,],%[^\n]\n", mask, &expire, &lastmod, &lifetime, &active, creator, reason) != 7)
45 continue;
46
47 count++;
48
49 gl = findgline(mask);
50
51 if (gl)
52 continue; /* Don't update existing glines. */
53
54 gl = makegline(mask);
55
56 if (!gl)
57 continue;
58
59 gl->creator = getsstring(creator, 512);
60
61 gl->flags = active ? GLINE_ACTIVE : 0;
62
63 gl->reason = getsstring(reason, 512);
64 gl->expire = expire;
65 gl->lastmod = lastmod;
66 gl->lifetime = lifetime;
67
68 gl->next = glinelist;
69 glinelist = gl;
70 }
71
72 return count;
73 }
74
75 int glstore_save(void) {
76 char path[512], srcfile[512], dstfile[512];
77 int i, count;
78
79 snprintf(path, sizeof(path), "%s.temp", GLSTORE_PATH_PREFIX);
80
81 count = glstore_savefile(path);
82
83 if (count < 0) {
84 Error("glines", ERR_ERROR, "Could not save glines to %s", path);
85 return -1;
86 }
87
88 for (i = GLSTORE_SAVE_FILES; i > 0; i--) {
89 snprintf(srcfile, sizeof(srcfile), "%s.%d", GLSTORE_PATH_PREFIX, i - 1);
90 snprintf(dstfile, sizeof(dstfile), "%s.%d", GLSTORE_PATH_PREFIX, i);
91 (void) rename(srcfile, dstfile);
92 }
93
94 (void) rename(path, srcfile);
95
96 return count;
97 }
98
99 int glstore_load(void) {
100 char path[512];
101 snprintf(path, sizeof(path), "%s.0", GLSTORE_PATH_PREFIX);
102
103 return glstore_loadfile(path);
104 }
105