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