]>
Commit | Line | Data |
---|---|---|
1 | #include <stdio.h> | |
2 | #include "../lib/version.h" | |
3 | #include "../core/schedule.h" | |
4 | #include "../control/control.h" | |
5 | #include "glines.h" | |
6 | ||
7 | MODULE_VERSION(""); | |
8 | ||
9 | static 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, | |
24 | (gl->flags & GLINE_ACTIVE) ? 1 : 0, | |
25 | gl->creator->content, gl->reason ? gl->reason->content : ""); | |
26 | count++; | |
27 | } | |
28 | ||
29 | fclose(fp); | |
30 | ||
31 | return count; | |
32 | } | |
33 | ||
34 | static int glstore_loadfile(const char *file) { | |
35 | FILE *fp; | |
36 | char mask[512], creator[512], reason[512]; | |
37 | intmax_t expire, lastmod, lifetime; | |
38 | int active, count; | |
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)) { | |
49 | if (fscanf(fp, "%[^ ]%jd,%jd,%jd,%d,%[^,],%[^\n]\n", mask, &expire, &lastmod, &lifetime, &active, creator, reason) != 7) | |
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 | ||
66 | gl->flags |= active ? GLINE_ACTIVE : 0; | |
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 | ||
77 | fclose(fp); | |
78 | ||
79 | return count; | |
80 | } | |
81 | ||
82 | int 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 | ||
106 | int 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 | ||
113 | static 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 | ||
127 | static 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 | ||
141 | static void glines_sched_save(void *arg) { | |
142 | glstore_save(); | |
143 | } | |
144 | ||
145 | void _init() { | |
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 | ||
154 | void _fini() { | |
155 | deregistercontrolcmd("loadglines", glines_cmdloadglines); | |
156 | deregistercontrolcmd("saveglines", glines_cmdsaveglines); | |
157 | ||
158 | deleteschedule(NULL, glines_sched_save, NULL); | |
159 | } |