]>
Commit | Line | Data |
---|---|---|
01250ca8 P |
1 | #include <stdlib.h> |
2 | #include <sys/time.h> | |
3 | ||
4 | #include "../core/hooks.h" | |
5 | #include "../lib/sstring.h" | |
6 | #include "../server/server.h" | |
7 | #include "../irc/irc.h" | |
8 | #include "../core/error.h" | |
9 | #include "../lib/irc_string.h" | |
10 | #include "jupe.h" | |
11 | ||
12 | jupe_t *jupes; | |
13 | ||
14 | int handlejupe(void *source, int cargc, char **cargv); | |
15 | void sendjupeburst(int hook, void *args); | |
16 | ||
17 | void _init() { | |
18 | jupes = NULL; | |
19 | ||
20 | registerhook(HOOK_IRC_SENDBURSTBURSTS, &sendjupeburst); | |
21 | ||
22 | registerserverhandler("JU", &handlejupe, 5); | |
23 | } | |
24 | ||
25 | void _fini() { | |
26 | jupe_t *next; | |
27 | ||
28 | while (jupes) { | |
29 | /* keep a pointer to the next item */ | |
30 | next = jupes->ju_next; | |
31 | ||
32 | free(jupes); | |
33 | ||
34 | jupes = next; | |
35 | } | |
36 | ||
37 | deregisterhook(HOOK_IRC_SENDBURSTBURSTS, &sendjupeburst); | |
38 | ||
39 | deregisterserverhandler("JU", &handlejupe); | |
40 | } | |
41 | ||
42 | int handlejupe(void *source, int cargc, char **cargv) { | |
43 | char *target, *server, *expire, *modtime, *reason; | |
44 | jupe_t *jupe, *last_jupe; | |
45 | unsigned int flags; | |
46 | ||
47 | if (cargc < 5) | |
48 | return CMD_OK; /* local jupe or not a valid.. we don't care */ | |
49 | ||
50 | target = cargv[0]; | |
51 | server = cargv[1]; | |
52 | expire = cargv[2]; | |
53 | modtime = cargv[3]; | |
54 | reason = cargv[4]; | |
55 | ||
56 | if (atoi(expire) > JUPE_MAX_EXPIRE || atoi(expire) <= 0) | |
57 | return; /* jupe's expiry date is not valid */ | |
58 | ||
59 | if (server[0] != '+' && server[0] != '-') | |
60 | return CMD_OK; /* not a valid jupe either */ | |
61 | else { | |
62 | flags = (server[0] == '+') ? JUPE_ACTIVE : 0; | |
63 | server++; | |
64 | } | |
65 | ||
66 | jupe = jupe_find(server); | |
67 | ||
68 | if (jupe != NULL && atoi(modtime) > jupe->ju_lastmod) { | |
69 | jupe->ju_flags = flags; | |
70 | jupe->ju_lastmod = atoi(modtime); | |
71 | ||
72 | Error("jupe", ERR_WARNING, "jupe modified for %s (%s) expiring in %s," | |
73 | " lastmod: %s, active: %s", server, reason, expire, modtime, flags ? "yes" : "no"); | |
74 | ||
75 | return CMD_OK; | |
76 | } | |
77 | ||
78 | jupe = make_jupe(server, reason, getnettime() + atoi(expire), | |
79 | atoi(modtime), flags); | |
80 | ||
81 | if (jupe == NULL) | |
82 | return CMD_ERROR; | |
83 | ||
84 | Error("jupe", ERR_WARNING, "jupe added for %s (%s) expiring in %s," | |
85 | " lastmod: %s, active: %s", server, reason, expire, modtime, flags ? "yes" : "no"); | |
86 | ||
87 | return CMD_OK; | |
88 | } | |
89 | ||
90 | void sendjupeburst(int hook, void *args) { | |
91 | jupe_t *jupe = jupes; | |
92 | ||
93 | if (hook != HOOK_IRC_SENDBURSTBURSTS) | |
94 | return; | |
95 | ||
96 | while (jupe) { | |
97 | jupe_propagate(jupe); | |
98 | ||
99 | jupe = jupe->ju_next; | |
100 | } | |
101 | } | |
102 | ||
103 | jupe_t *make_jupe(char *server, char *reason, time_t expirets, time_t lastmod, unsigned int flags) { | |
104 | jupe_t *jupe, *last_jupe; | |
105 | ||
106 | jupe = (jupe_t*)malloc(sizeof(jupe_t)); | |
107 | ||
108 | if (jupe == NULL) | |
109 | return NULL; | |
110 | ||
111 | jupe->ju_server = getsstring(server, HOSTLEN); | |
112 | jupe->ju_reason = getsstring(reason, TOPICLEN); | |
113 | jupe->ju_expire = expirets; | |
114 | jupe->ju_lastmod = lastmod; | |
115 | jupe->ju_flags = flags; | |
116 | jupe->ju_next = NULL; | |
117 | ||
118 | /* add the jupe to our linked list */ | |
119 | if (jupes == NULL) | |
120 | jupes = jupe; | |
121 | else { | |
122 | last_jupe = jupes; | |
123 | ||
124 | while (last_jupe->ju_next) | |
125 | last_jupe = last_jupe->ju_next; | |
126 | ||
127 | last_jupe->ju_next = jupe; | |
128 | } | |
129 | ||
130 | return jupe; | |
131 | } | |
132 | ||
133 | void jupe_propagate(jupe_t *jupe) { | |
134 | irc_send("%s JU * %c%s %d %d :%s", mynumeric->content, | |
135 | JupeIsRemActive(jupe) ? '+' : '-', | |
136 | JupeServer(jupe), | |
137 | jupe->ju_expire - getnettime(), | |
138 | JupeLastMod(jupe), | |
139 | JupeReason(jupe)); | |
140 | } | |
141 | ||
142 | void jupe_expire(void) { | |
143 | jupe_find(NULL); | |
144 | } | |
145 | ||
146 | jupe_t *jupe_find(char *server) { | |
147 | jupe_t *jupe = jupes; | |
148 | ||
149 | while (jupe) { | |
150 | /* server == NULL if jupe_find() is used by jupe_expire */ | |
151 | if (server && ircd_strcmp(server, JupeServer(jupe)) == 0) | |
152 | return jupe; | |
153 | ||
154 | if (jupe->ju_next && jupe->ju_next->ju_expire < getnettime()) | |
155 | jupe_free(jupe->ju_next); | |
156 | ||
157 | jupe = jupe->ju_next; | |
158 | } | |
159 | ||
160 | if (jupes && jupes->ju_expire < getnettime()) | |
161 | jupe_free(jupes); | |
162 | ||
163 | return NULL; | |
164 | } | |
165 | ||
166 | void jupe_free(jupe_t *jupe) { | |
167 | jupe_t *next = jupe->ju_next; | |
168 | jupe_t *trav = jupes; | |
169 | ||
170 | if (jupe == jupes) | |
171 | jupes = jupe->ju_next; | |
172 | else { | |
173 | while (trav) { | |
174 | if (trav->ju_next == jupe) { | |
175 | trav->ju_next = jupe->ju_next; | |
176 | ||
177 | break; | |
178 | } | |
179 | ||
180 | trav = trav->ju_next; | |
181 | } | |
182 | } | |
183 | ||
184 | freesstring(jupe->ju_server); | |
185 | freesstring(jupe->ju_reason); | |
186 | free(jupe); | |
187 | } | |
188 | ||
189 | void jupe_activate(jupe_t *jupe) { | |
190 | if (jupe->ju_flags & JUPE_ACTIVE) | |
191 | return; | |
192 | ||
193 | jupe->ju_flags |= JUPE_ACTIVE; | |
194 | jupe->ju_lastmod = getnettime(); | |
195 | ||
196 | jupe_propagate(jupe); | |
197 | } | |
198 | ||
199 | void jupe_deactivate(jupe_t *jupe) { | |
200 | if ((jupe->ju_flags & JUPE_ACTIVE) == 0) | |
201 | return; | |
202 | ||
203 | jupe->ju_flags &= ~JUPE_ACTIVE; | |
204 | jupe->ju_lastmod = getnettime(); | |
205 | ||
206 | jupe_propagate(jupe); | |
207 | } | |
208 | ||
209 | int jupe_add(char *server, char *reason, time_t duration, unsigned int flags) { | |
210 | jupe_t *jupe; | |
211 | ||
212 | if (jupe_find(server) != NULL) | |
213 | return 0; | |
214 | ||
215 | if (duration > JUPE_MAX_EXPIRE || duration <= 0) | |
216 | return 0; | |
217 | ||
218 | jupe = make_jupe(server, reason, getnettime() + duration, | |
219 | getnettime(), flags); | |
220 | ||
221 | if (jupe == NULL) | |
222 | return 0; | |
223 | ||
224 | jupe_propagate(jupe); | |
225 | ||
226 | return 1; | |
227 | } |