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