]>
Commit | Line | Data |
---|---|---|
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 |
7 | MODULE_VERSION(""); |
8 | ||
580103bc GB |
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, | |
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 | ||
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; | |
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 | ||
77 | return count; | |
78 | } | |
79 | ||
80 | int glstore_save(void) { | |
81 | char path[512], srcfile[512], dstfile[512]; | |
82 | int i, count; | |
83 | ||
84 | snprintf(path, sizeof(path), "%s.temp", GLSTORE_PATH_PREFIX); | |
85 | ||
86 | count = glstore_savefile(path); | |
87 | ||
88 | if (count < 0) { | |
89 | Error("glines", ERR_ERROR, "Could not save glines to %s", path); | |
90 | return -1; | |
91 | } | |
92 | ||
93 | for (i = GLSTORE_SAVE_FILES; i > 0; i--) { | |
94 | snprintf(srcfile, sizeof(srcfile), "%s.%d", GLSTORE_PATH_PREFIX, i - 1); | |
95 | snprintf(dstfile, sizeof(dstfile), "%s.%d", GLSTORE_PATH_PREFIX, i); | |
96 | (void) rename(srcfile, dstfile); | |
97 | } | |
98 | ||
99 | (void) rename(path, srcfile); | |
100 | ||
101 | return count; | |
102 | } | |
103 | ||
104 | int glstore_load(void) { | |
105 | char path[512]; | |
106 | snprintf(path, sizeof(path), "%s.0", GLSTORE_PATH_PREFIX); | |
107 | ||
108 | return glstore_loadfile(path); | |
109 | } | |
110 | ||
4c2a84c4 GB |
111 | static int glines_cmdsaveglines(void *source, int cargc, char **cargv) { |
112 | nick *sender = source; | |
113 | int count; | |
114 | ||
115 | count = glstore_save(); | |
116 | ||
117 | if (count < 0) | |
118 | controlreply(sender, "An error occured while saving G-Lines."); | |
119 | else | |
120 | controlreply(sender, "Saved %d G-Line%s.", count, (count == 1) ? "" : "s"); | |
121 | ||
122 | return CMD_OK; | |
123 | } | |
124 | ||
125 | static int glines_cmdloadglines(void *source, int cargc, char **cargv) { | |
126 | nick *sender = source; | |
127 | int count; | |
128 | ||
129 | count = glstore_load(); | |
130 | ||
131 | if (count < 0) | |
132 | controlreply(sender, "An error occured while loading the G-Lines file."); | |
133 | else | |
134 | controlreply(sender, "Loaded %d G-Line%s.", count, (count == 1) ? "" : "s"); | |
135 | ||
136 | return CMD_OK; | |
137 | } | |
138 | ||
139 | static void glines_sched_save(void *arg) { | |
140 | glstore_save(); | |
141 | } | |
142 | ||
143 | void _init() { | |
4c2a84c4 GB |
144 | registercontrolhelpcmd("loadglines", NO_DEVELOPER, 0, glines_cmdloadglines, "Usage: loadglines\nForce load of glines."); |
145 | registercontrolhelpcmd("saveglines", NO_DEVELOPER, 0, glines_cmdsaveglines, "Usage: saveglines\nForce save of glines."); | |
146 | ||
147 | schedulerecurring(time(NULL), 0, GLSTORE_SAVE_INTERVAL, &glines_sched_save, NULL); | |
148 | ||
149 | glstore_load(); | |
150 | } | |
151 | ||
152 | void _fini() { | |
4c2a84c4 GB |
153 | deregistercontrolcmd("loadglines", glines_cmdloadglines); |
154 | deregistercontrolcmd("saveglines", glines_cmdsaveglines); | |
155 | ||
156 | deleteschedule(NULL, glines_sched_save, NULL); | |
157 | } |