]> jfr.im git - irc/quakenet/newserv.git/blob - glines/glines_store.c
BUILD: specify all dependencies and upgrade to bionic
[irc/quakenet/newserv.git] / glines / glines_store.c
1 #include <stdio.h>
2 #include <stdint.h>
3 #include "../lib/version.h"
4 #include "../core/schedule.h"
5 #include "../control/control.h"
6 #include "glines.h"
7
8 MODULE_VERSION("");
9
10 static int glstore_savefile(const char *file) {
11 FILE *fp;
12 gline *gl;
13 int count;
14
15 fp = fopen(file, "w");
16
17 if (!fp)
18 return -1;
19
20 count = 0;
21
22 for (gl = glinelist; gl; gl = gl->next) {
23 fprintf(fp, "%s %jd,%jd,%jd,%d,%s,%s\n",
24 glinetostring(gl), (intmax_t)gl->expire, (intmax_t)gl->lastmod, (intmax_t)gl->lifetime,
25 (gl->flags & GLINE_ACTIVE) ? 1 : 0,
26 gl->creator->content, gl->reason ? gl->reason->content : "");
27 count++;
28 }
29
30 fclose(fp);
31
32 return count;
33 }
34
35 static int glstore_loadfile(const char *file) {
36 FILE *fp;
37 char mask[512], creator[512], reason[512];
38 intmax_t expire, lastmod, lifetime;
39 int active, count;
40 gline *gl;
41
42 fp = fopen(file, "r");
43
44 if (!fp)
45 return -1;
46
47 count = 0;
48
49 while (!feof(fp)) {
50 if (fscanf(fp, "%[^ ]%jd,%jd,%jd,%d,%[^,],%[^\n]\n", mask, &expire, &lastmod, &lifetime, &active, creator, reason) != 7)
51 continue;
52
53 count++;
54
55 gl = findgline(mask);
56
57 if (gl)
58 continue; /* Don't update existing glines. */
59
60 gl = makegline(mask);
61
62 if (!gl)
63 continue;
64
65 gl->creator = getsstring(creator, 512);
66
67 gl->flags |= active ? GLINE_ACTIVE : 0;
68
69 gl->reason = getsstring(reason, 512);
70 gl->expire = expire;
71 gl->lastmod = lastmod;
72 gl->lifetime = lifetime;
73
74 gl->next = glinelist;
75 glinelist = gl;
76 }
77
78 fclose(fp);
79
80 return count;
81 }
82
83 int glstore_save(void) {
84 char path[512], srcfile[512], dstfile[512];
85 int i, count;
86
87 snprintf(path, sizeof(path), "%s.temp", GLSTORE_PATH_PREFIX);
88
89 count = glstore_savefile(path);
90
91 if (count < 0) {
92 Error("glines", ERR_ERROR, "Could not save glines to %s", path);
93 return -1;
94 }
95
96 for (i = GLSTORE_SAVE_FILES; i > 0; i--) {
97 snprintf(srcfile, sizeof(srcfile), "%s.%d", GLSTORE_PATH_PREFIX, i - 1);
98 snprintf(dstfile, sizeof(dstfile), "%s.%d", GLSTORE_PATH_PREFIX, i);
99 (void) rename(srcfile, dstfile);
100 }
101
102 (void) rename(path, srcfile);
103
104 return count;
105 }
106
107 int glstore_load(void) {
108 char path[512];
109 snprintf(path, sizeof(path), "%s.0", GLSTORE_PATH_PREFIX);
110
111 return glstore_loadfile(path);
112 }
113
114 static int glines_cmdsaveglines(void *source, int cargc, char **cargv) {
115 nick *sender = source;
116 int count;
117
118 count = glstore_save();
119
120 if (count < 0)
121 controlreply(sender, "An error occured while saving G-Lines.");
122 else
123 controlreply(sender, "Saved %d G-Line%s.", count, (count == 1) ? "" : "s");
124
125 return CMD_OK;
126 }
127
128 static int glines_cmdloadglines(void *source, int cargc, char **cargv) {
129 nick *sender = source;
130 int count;
131
132 count = glstore_load();
133
134 if (count < 0)
135 controlreply(sender, "An error occured while loading the G-Lines file.");
136 else
137 controlreply(sender, "Loaded %d G-Line%s.", count, (count == 1) ? "" : "s");
138
139 return CMD_OK;
140 }
141
142 static void glines_sched_save(void *arg) {
143 glstore_save();
144 }
145
146 void _init() {
147 registercontrolhelpcmd("loadglines", NO_DEVELOPER, 0, glines_cmdloadglines, "Usage: loadglines\nForce load of glines.");
148 registercontrolhelpcmd("saveglines", NO_DEVELOPER, 0, glines_cmdsaveglines, "Usage: saveglines\nForce save of glines.");
149
150 schedulerecurring(time(NULL), 0, GLSTORE_SAVE_INTERVAL, &glines_sched_save, NULL);
151
152 glstore_load();
153 }
154
155 void _fini() {
156 deregistercontrolcmd("loadglines", glines_cmdloadglines);
157 deregistercontrolcmd("saveglines", glines_cmdsaveglines);
158
159 deleteschedule(NULL, glines_sched_save, NULL);
160 }