]>
Commit | Line | Data |
---|---|---|
d76ed9a9 AS |
1 | /* hash.c - IRC network state database |
2 | * Copyright 2000-2004 srvx Development Team | |
3 | * | |
83ff05c3 | 4 | * This file is part of x3. |
d76ed9a9 | 5 | * |
d0f04f71 | 6 | * x3 is free software; you can redistribute it and/or modify |
d76ed9a9 | 7 | * it under the terms of the GNU General Public License as published by |
348683aa | 8 | * the Free Software Foundation; either version 3 of the License, or |
d76ed9a9 AS |
9 | * (at your option) any later version. |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with srvx; if not, write to the Free Software Foundation, | |
18 | * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. | |
19 | */ | |
20 | ||
21 | #include "conf.h" | |
22 | #include "global.h" | |
23 | #include "hash.h" | |
24 | #include "log.h" | |
25 | ||
58528547 | 26 | #if defined(HAVE_LIBGEOIP)&&defined(HAVE_GEOIP_H)&&defined(HAVE_GEOIPCITY_H) |
21f6caee | 27 | #include <GeoIP.h> |
21f6caee | 28 | #include <GeoIPCity.h> |
58528547 | 29 | |
30 | GeoIP * gi = NULL; | |
31 | GeoIP * cgi = NULL; | |
21f6caee | 32 | #endif |
33 | ||
d76ed9a9 AS |
34 | struct server *self; |
35 | dict_t channels; | |
36 | dict_t clients; | |
37 | dict_t servers; | |
38 | unsigned int max_clients, invis_clients; | |
39 | time_t max_clients_time; | |
40 | struct userList curr_opers; | |
41 | ||
30874d66 | 42 | static void hash_cleanup(void *extra); |
d76ed9a9 AS |
43 | |
44 | void init_structs(void) | |
45 | { | |
46 | channels = dict_new(); | |
47 | clients = dict_new(); | |
48 | servers = dict_new(); | |
49 | userList_init(&curr_opers); | |
30874d66 | 50 | reg_exit_func(hash_cleanup, NULL); |
d76ed9a9 AS |
51 | } |
52 | ||
1136f709 | 53 | int userList_contains(struct userList *list, struct userNode *user) |
54 | { | |
55 | unsigned int ii; | |
56 | ||
57 | for (ii = 0; ii < list->used; ++ii) { | |
58 | if (user == list->list[ii]) { | |
59 | return 1; | |
60 | } | |
61 | } | |
62 | return 0; | |
63 | } | |
64 | ||
d76ed9a9 | 65 | server_link_func_t *slf_list; |
9d59f196 | 66 | void **slf_list_extra; |
d76ed9a9 AS |
67 | unsigned int slf_size = 0, slf_used = 0; |
68 | ||
69 | void | |
9d59f196 | 70 | reg_server_link_func(server_link_func_t handler, void *extra) |
d76ed9a9 AS |
71 | { |
72 | if (slf_used == slf_size) { | |
9d59f196 | 73 | if (slf_size) { |
74 | slf_size <<= 1; | |
75 | slf_list = realloc(slf_list, slf_size*sizeof(server_link_func_t)); | |
76 | slf_list_extra = realloc(slf_list_extra, slf_size*sizeof(void*)); | |
77 | } else { | |
78 | slf_size = 8; | |
79 | slf_list = malloc(slf_size*sizeof(server_link_func_t)); | |
80 | slf_list_extra = malloc(slf_size*sizeof(void*)); | |
81 | } | |
82 | } | |
83 | slf_list[slf_used] = handler; | |
84 | slf_list_extra[slf_used++] = extra; | |
85 | } | |
86 | ||
87 | void | |
88 | call_server_link_funcs(struct server *server) | |
89 | { | |
90 | unsigned int i; | |
91 | ||
92 | for (i = 0; i < slf_used; ++i) | |
93 | { | |
94 | slf_list[i](server, slf_list_extra[i]); | |
d76ed9a9 | 95 | } |
d76ed9a9 AS |
96 | } |
97 | ||
98 | struct server* | |
99 | GetServerH(const char *name) | |
100 | { | |
101 | return dict_find(servers, name, NULL); | |
102 | } | |
103 | ||
104 | new_user_func_t *nuf_list; | |
f0fb2e2d | 105 | void **nuf_list_extra; |
d76ed9a9 AS |
106 | unsigned int nuf_size = 0, nuf_used = 0; |
107 | ||
108 | void | |
f0fb2e2d | 109 | reg_new_user_func(new_user_func_t handler, void *extra) |
d76ed9a9 AS |
110 | { |
111 | if (nuf_used == nuf_size) { | |
f0fb2e2d | 112 | if (nuf_size) { |
113 | nuf_size <<= 1; | |
114 | nuf_list = realloc(nuf_list, nuf_size*sizeof(new_user_func_t)); | |
115 | nuf_list_extra = realloc(nuf_list_extra, nuf_size*sizeof(void*)); | |
116 | } else { | |
117 | nuf_size = 8; | |
118 | nuf_list = malloc(nuf_size*sizeof(new_user_func_t)); | |
119 | nuf_list_extra = malloc(nuf_size*sizeof(void*)); | |
120 | } | |
121 | } | |
122 | nuf_list[nuf_used] = handler; | |
123 | nuf_list_extra[nuf_used++] = extra; | |
124 | } | |
125 | ||
126 | void | |
127 | call_new_user_funcs(struct userNode* user) | |
128 | { | |
129 | unsigned int i; | |
130 | ||
63189c10 | 131 | for (i = 0; i < nuf_used && !(user->dead); ++i) |
f0fb2e2d | 132 | { |
133 | nuf_list[i](user, nuf_list_extra[i]); | |
d76ed9a9 | 134 | } |
d76ed9a9 AS |
135 | } |
136 | ||
137 | static nick_change_func_t *ncf2_list; | |
63189c10 | 138 | static void **ncf2_list_extra; |
d76ed9a9 AS |
139 | static unsigned int ncf2_size = 0, ncf2_used = 0; |
140 | ||
141 | void | |
63189c10 | 142 | reg_nick_change_func(nick_change_func_t handler, void *extra) |
d76ed9a9 AS |
143 | { |
144 | if (ncf2_used == ncf2_size) { | |
145 | if (ncf2_size) { | |
146 | ncf2_size <<= 1; | |
147 | ncf2_list = realloc(ncf2_list, ncf2_size*sizeof(nick_change_func_t)); | |
63189c10 | 148 | ncf2_list_extra = realloc(ncf2_list_extra, ncf2_size*sizeof(void*)); |
d76ed9a9 AS |
149 | } else { |
150 | ncf2_size = 8; | |
151 | ncf2_list = malloc(ncf2_size*sizeof(nick_change_func_t)); | |
63189c10 | 152 | ncf2_list_extra = malloc(ncf2_size*sizeof(void*)); |
d76ed9a9 AS |
153 | } |
154 | } | |
63189c10 | 155 | ncf2_list[ncf2_used] = handler; |
63c0b8ad | 156 | ncf2_list_extra[ncf2_used++] = extra; |
d76ed9a9 AS |
157 | } |
158 | ||
159 | ||
160 | del_user_func_t *duf_list; | |
a6bcc929 | 161 | void **duf_list_extra; |
d76ed9a9 AS |
162 | unsigned int duf_size = 0, duf_used = 0; |
163 | ||
164 | void | |
a6bcc929 | 165 | reg_del_user_func(del_user_func_t handler, void *extra) |
d76ed9a9 AS |
166 | { |
167 | if (duf_used == duf_size) { | |
a6bcc929 | 168 | if (duf_size) { |
169 | duf_size <<= 1; | |
170 | duf_list = realloc(duf_list, duf_size*sizeof(del_user_func_t)); | |
171 | duf_list_extra = realloc(duf_list_extra, duf_size*sizeof(void*)); | |
172 | } else { | |
173 | duf_size = 8; | |
174 | duf_list = malloc(duf_size*sizeof(del_user_func_t)); | |
175 | duf_list_extra = malloc(duf_size*sizeof(void*)); | |
176 | } | |
177 | } | |
178 | duf_list[duf_used] = handler; | |
179 | duf_list_extra[duf_used++] = extra; | |
180 | } | |
181 | ||
182 | void | |
183 | call_del_user_funcs(struct userNode *user, struct userNode *killer, const char *why) | |
184 | { | |
185 | unsigned int i; | |
186 | ||
187 | for (i = 0; i < duf_used; ++i) | |
188 | { | |
189 | duf_list[i](user, killer, why, duf_list_extra[i]); | |
d76ed9a9 | 190 | } |
d76ed9a9 AS |
191 | } |
192 | ||
193 | void | |
a6bcc929 | 194 | unreg_del_user_func(del_user_func_t handler, void *extra) |
d76ed9a9 AS |
195 | { |
196 | unsigned int i; | |
197 | for (i=0; i<duf_used; i++) { | |
a6bcc929 | 198 | if (duf_list[i] == handler && duf_list_extra[i] == extra) break; |
d76ed9a9 AS |
199 | } |
200 | if (i == duf_used) return; | |
201 | memmove(duf_list+i, duf_list+i+1, (duf_used-i-1)*sizeof(duf_list[0])); | |
a6bcc929 | 202 | memmove(duf_list_extra+i, duf_list_extra+i+1, (duf_used-i-1)*sizeof(duf_list_extra[0])); |
d76ed9a9 AS |
203 | duf_used--; |
204 | } | |
205 | ||
206 | /* reintroduces a user after it has been killed. */ | |
207 | void | |
208 | ReintroduceUser(struct userNode *user) | |
209 | { | |
210 | struct mod_chanmode change; | |
211 | unsigned int n; | |
212 | ||
213 | irc_user(user); | |
214 | mod_chanmode_init(&change); | |
215 | change.argc = 1; | |
216 | for (n = 0; n < user->channels.used; n++) { | |
217 | struct modeNode *mn = user->channels.list[n]; | |
218 | irc_join(user, mn->channel); | |
219 | if (mn->modes) { | |
220 | change.args[0].mode = mn->modes; | |
a32da4c7 | 221 | change.args[0].u.member = mn; |
d76ed9a9 AS |
222 | mod_chanmode_announce(user, mn->channel, &change); |
223 | } | |
224 | } | |
225 | } | |
226 | ||
227 | void | |
228 | NickChange(struct userNode* user, const char *new_nick, int no_announce) | |
229 | { | |
230 | char *old_nick; | |
231 | unsigned int nn; | |
232 | ||
233 | /* don't do anything if there's no change */ | |
234 | old_nick = user->nick; | |
235 | if (!strncmp(new_nick, old_nick, NICKLEN)) | |
236 | return; | |
237 | ||
238 | /* remove old entry from clients dictionary */ | |
239 | dict_remove(clients, old_nick); | |
240 | #if !defined(WITH_PROTOCOL_P10) | |
241 | /* Remove from uplink's clients dict */ | |
242 | dict_remove(user->uplink->users, old_nick); | |
243 | #endif | |
244 | /* and reinsert */ | |
245 | user->nick = strdup(new_nick); | |
246 | dict_insert(clients, user->nick, user); | |
247 | #if !defined(WITH_PROTOCOL_P10) | |
248 | dict_insert(user->uplink->users, user->nick, user); | |
249 | #endif | |
250 | ||
251 | /* Make callbacks for nick changes. Do this with new nick in | |
252 | * place because that is slightly more useful. | |
253 | */ | |
1136f709 | 254 | for (nn=0; (nn<ncf2_used) && !user->dead; nn++) |
63189c10 | 255 | ncf2_list[nn](user, old_nick, ncf2_list_extra[nn]); |
d76ed9a9 AS |
256 | user->timestamp = now; |
257 | if (IsLocal(user) && !no_announce) | |
258 | irc_nick(user, old_nick); | |
259 | free(old_nick); | |
260 | } | |
261 | ||
b404335b | 262 | void |
cf8bedff | 263 | SVSNickChange(struct userNode* user, const char *new_nick) |
b404335b | 264 | { |
265 | char *old_nick; | |
266 | unsigned int nn; | |
267 | ||
268 | /* don't do anything if there's no change */ | |
269 | old_nick = user->nick; | |
270 | if (!strncmp(new_nick, old_nick, NICKLEN)) | |
271 | return; | |
272 | ||
273 | /* remove old entry from clients dictionary */ | |
274 | dict_remove(clients, old_nick); | |
275 | #if !defined(WITH_PROTOCOL_P10) | |
276 | /* Remove from uplink's clients dict */ | |
277 | dict_remove(user->uplink->users, old_nick); | |
278 | #endif | |
279 | /* and reinsert */ | |
280 | user->nick = strdup(new_nick); | |
281 | dict_insert(clients, user->nick, user); | |
282 | #if !defined(WITH_PROTOCOL_P10) | |
283 | dict_insert(user->uplink->users, user->nick, user); | |
284 | #endif | |
285 | ||
286 | /* Make callbacks for nick changes. Do this with new nick in | |
287 | * place because that is slightly more useful. | |
288 | */ | |
1136f709 | 289 | for (nn=0; (nn<ncf2_used) && !user->dead; nn++) |
63189c10 | 290 | ncf2_list[nn](user, old_nick, ncf2_list_extra[nn]); |
b404335b | 291 | user->timestamp = now; |
292 | ||
293 | free(old_nick); | |
294 | } | |
295 | ||
d76ed9a9 AS |
296 | struct userNode * |
297 | GetUserH(const char *nick) | |
298 | { | |
299 | return dict_find(clients, nick, NULL); | |
300 | } | |
301 | ||
302 | static account_func_t account_func; | |
303 | ||
304 | void | |
305 | reg_account_func(account_func_t handler) | |
306 | { | |
307 | if (account_func) { | |
308 | log_module(MAIN_LOG, LOG_WARNING, "Reregistering ACCOUNT handler."); | |
309 | } | |
310 | account_func = handler; | |
311 | } | |
312 | ||
313 | void | |
314 | call_account_func(struct userNode *user, const char *stamp) | |
315 | { | |
316 | /* We've received an account stamp for a user; notify | |
317 | NickServ, which registers the sole account_func | |
f1d4a7db AS |
318 | right now. TODO: This is a bug. This needs to register |
319 | a proper list not just kill with each call!! -Rubin | |
d76ed9a9 AS |
320 | |
321 | P10 Protocol violation if (user->modes & FLAGS_STAMPED) here. | |
322 | */ | |
323 | if (account_func) | |
324 | account_func(user, stamp); | |
325 | ||
326 | #ifdef WITH_PROTOCOL_P10 | |
327 | /* Mark the user so we don't stamp it again. */ | |
328 | user->modes |= FLAGS_STAMPED; | |
329 | #endif | |
330 | } | |
331 | ||
332 | void | |
b21e2cfe | 333 | StampUser(struct userNode *user, const char *stamp, time_t timestamp) |
d76ed9a9 AS |
334 | { |
335 | #ifdef WITH_PROTOCOL_P10 | |
336 | /* The P10 protocol says we can't stamp users who already | |
337 | have a stamp. */ | |
338 | if (IsStamped(user)) | |
339 | return; | |
340 | #endif | |
341 | ||
b21e2cfe | 342 | irc_account(user, stamp, timestamp); |
d76ed9a9 AS |
343 | user->modes |= FLAGS_STAMPED; |
344 | } | |
345 | ||
346 | void | |
347 | assign_fakehost(struct userNode *user, const char *host, int announce) | |
348 | { | |
349 | safestrncpy(user->fakehost, host, sizeof(user->fakehost)); | |
350 | if (announce) | |
351 | irc_fakehost(user, host); | |
352 | } | |
353 | ||
21f6caee | 354 | void |
355 | set_geoip_info(struct userNode *user) | |
356 | { | |
7a278540 AS |
357 | if(IsLocal(user)) |
358 | return; | |
21f6caee | 359 | /* Need the libs and the headers if this is going to compile properly */ |
360 | #if defined(HAVE_LIBGEOIP)&&defined(HAVE_GEOIP_H)&&defined(HAVE_GEOIPCITY_H) | |
21f6caee | 361 | GeoIPRecord * gir; |
362 | const char *geoip_data_file = NULL; | |
363 | const char *geoip_city_file = NULL; | |
364 | ||
365 | geoip_data_file = conf_get_data("services/opserv/geoip_data_file", RECDB_QSTRING); | |
366 | geoip_city_file = conf_get_data("services/opserv/geoip_city_data_file", RECDB_QSTRING); | |
367 | ||
7a278540 | 368 | if ((!geoip_data_file && !geoip_city_file)) |
21f6caee | 369 | return; /* Admin doesnt want to use geoip functions */ |
370 | ||
58528547 | 371 | if (geoip_data_file && !gi) |
372 | gi = GeoIP_open(geoip_data_file, GEOIP_MEMORY_CACHE | GEOIP_CHECK_CACHE); | |
21f6caee | 373 | |
58528547 | 374 | if (geoip_city_file && !cgi) |
375 | cgi = GeoIP_open(geoip_city_file, GEOIP_MEMORY_CACHE | GEOIP_CHECK_CACHE); | |
21f6caee | 376 | |
377 | if (cgi) { | |
378 | gir = GeoIP_record_by_name(cgi, user->hostname); | |
379 | if (gir) { | |
380 | user->country_name = strdup(gir->country_name ? gir->country_name : ""); | |
381 | user->country_code = strdup(gir->country_code ? gir->country_code : ""); | |
382 | user->city = strdup(gir->city ? gir->city : ""); | |
383 | user->region = strdup(gir->region ? gir->region : ""); | |
384 | user->postal_code = strdup(gir->postal_code ? gir->postal_code : ""); | |
385 | ||
386 | user->latitude = gir->latitude ? gir->latitude : 0; | |
387 | user->longitude = gir->longitude ? gir->longitude : 0; | |
388 | user->dma_code = gir->dma_code ? gir->dma_code : 0; | |
389 | user->area_code = gir->area_code ? gir->area_code : 0; | |
390 | ||
391 | GeoIPRecord_delete(gir); | |
392 | } | |
393 | ||
21f6caee | 394 | return; |
395 | } else if (gi) { | |
396 | const char *country = GeoIP_country_name_by_name(gi, user->hostname); | |
397 | user->country_name = strdup(country ? country : ""); | |
21f6caee | 398 | return; |
399 | } | |
400 | ||
401 | return; | |
402 | #endif | |
403 | } | |
404 | ||
d76ed9a9 | 405 | static new_channel_func_t *ncf_list; |
157ce145 | 406 | static void **ncf_list_extra; |
d76ed9a9 AS |
407 | static unsigned int ncf_size = 0, ncf_used = 0; |
408 | ||
409 | void | |
157ce145 | 410 | reg_new_channel_func(new_channel_func_t handler, void *extra) |
d76ed9a9 AS |
411 | { |
412 | if (ncf_used == ncf_size) { | |
413 | if (ncf_size) { | |
414 | ncf_size <<= 1; | |
415 | ncf_list = realloc(ncf_list, ncf_size*sizeof(ncf_list[0])); | |
157ce145 | 416 | ncf_list_extra = realloc(ncf_list_extra, ncf_size*sizeof(void*)); |
d76ed9a9 AS |
417 | } else { |
418 | ncf_size = 8; | |
419 | ncf_list = malloc(ncf_size*sizeof(ncf_list[0])); | |
157ce145 | 420 | ncf_list_extra = malloc(ncf_size*sizeof(void*)); |
d76ed9a9 AS |
421 | } |
422 | } | |
157ce145 | 423 | ncf_list[ncf_used] = handler; |
424 | ncf_list_extra[ncf_used++] = extra; | |
d76ed9a9 AS |
425 | } |
426 | ||
427 | static join_func_t *jf_list; | |
fb38e2bc | 428 | static void **jf_list_extra; |
d76ed9a9 AS |
429 | static unsigned int jf_size = 0, jf_used = 0; |
430 | ||
431 | void | |
fb38e2bc | 432 | reg_join_func(join_func_t handler, void *extra) |
d76ed9a9 AS |
433 | { |
434 | if (jf_used == jf_size) { | |
435 | if (jf_size) { | |
436 | jf_size <<= 1; | |
437 | jf_list = realloc(jf_list, jf_size*sizeof(join_func_t)); | |
fb38e2bc | 438 | jf_list_extra = realloc(jf_list_extra, jf_size*sizeof(void*)); |
d76ed9a9 AS |
439 | } else { |
440 | jf_size = 8; | |
441 | jf_list = malloc(jf_size*sizeof(join_func_t)); | |
fb38e2bc | 442 | jf_list_extra = malloc(jf_size*sizeof(void*)); |
d76ed9a9 AS |
443 | } |
444 | } | |
fb38e2bc | 445 | jf_list[jf_used] = handler; |
446 | jf_list_extra[jf_used++] = extra; | |
d76ed9a9 AS |
447 | } |
448 | ||
449 | int rel_age; | |
450 | ||
451 | static void | |
452 | wipeout_channel(struct chanNode *cNode, time_t new_time, char **modes, unsigned int modec) { | |
453 | unsigned int orig_limit; | |
454 | chan_mode_t orig_modes; | |
455 | char orig_key[KEYLEN+1]; | |
2f61d1d7 | 456 | char orig_apass[KEYLEN+1]; |
457 | char orig_upass[KEYLEN+1]; | |
d76ed9a9 AS |
458 | unsigned int nn, argc; |
459 | ||
460 | /* nuke old topic */ | |
461 | cNode->topic[0] = '\0'; | |
462 | cNode->topic_nick[0] = '\0'; | |
463 | cNode->topic_time = 0; | |
464 | ||
465 | /* remember the old modes, and update them with the new */ | |
466 | orig_modes = cNode->modes; | |
467 | orig_limit = cNode->limit; | |
468 | strcpy(orig_key, cNode->key); | |
2f61d1d7 | 469 | strcpy(orig_upass, cNode->upass); |
470 | strcpy(orig_apass, cNode->apass); | |
d76ed9a9 AS |
471 | cNode->modes = 0; |
472 | mod_chanmode(NULL, cNode, modes, modec, 0); | |
473 | cNode->timestamp = new_time; | |
474 | ||
475 | /* remove our old ban list, replace it with the new one */ | |
476 | for (nn=0; nn<cNode->banlist.used; nn++) | |
477 | free(cNode->banlist.list[nn]); | |
478 | cNode->banlist.used = 0; | |
479 | ||
2aef5f4b | 480 | /* remove our old exe,[t list, replace it with the new one */ |
481 | for (nn=0; nn<cNode->exemptlist.used; nn++) | |
482 | free(cNode->exemptlist.list[nn]); | |
483 | cNode->exemptlist.used = 0; | |
484 | ||
d76ed9a9 AS |
485 | /* deop anybody in the channel now, but count services to reop */ |
486 | for (nn=argc=0; nn<cNode->members.used; nn++) { | |
487 | struct modeNode *mn = cNode->members.list[nn]; | |
488 | if ((mn->modes & MODE_CHANOP) && IsService(mn->user) && IsLocal(mn->user)) | |
489 | argc++; | |
490 | } | |
55342ce8 | 491 | |
d76ed9a9 AS |
492 | if (argc) { |
493 | struct mod_chanmode *change; | |
494 | ||
495 | change = mod_chanmode_alloc(argc); | |
496 | change->modes_clear = 0; | |
497 | change->modes_set = orig_modes; | |
498 | change->new_limit = orig_limit; | |
499 | strcpy(change->new_key, orig_key); | |
2f61d1d7 | 500 | strcpy(change->new_upass, orig_upass); |
501 | strcpy(change->new_apass, orig_apass); | |
d76ed9a9 AS |
502 | for (nn = argc = 0; nn < cNode->members.used; ++nn) { |
503 | struct modeNode *mn = cNode->members.list[nn]; | |
504 | if ((mn->modes & MODE_CHANOP) && IsService(mn->user) && IsLocal(mn->user)) { | |
505 | change->args[argc].mode = MODE_CHANOP; | |
a32da4c7 | 506 | change->args[argc].u.member = mn; |
d76ed9a9 AS |
507 | argc++; |
508 | } | |
509 | } | |
510 | assert(argc == change->argc); | |
a32da4c7 | 511 | change->args[0].u.member->modes &= ~MODE_CHANOP; |
512 | mod_chanmode_announce(change->args[0].u.member->user, cNode, change); | |
d76ed9a9 AS |
513 | mod_chanmode_free(change); |
514 | } | |
515 | } | |
516 | ||
517 | struct chanNode * | |
2aef5f4b | 518 | AddChannel(const char *name, time_t time_, const char *modes, char *banlist, char *exemptlist) |
d76ed9a9 AS |
519 | { |
520 | struct chanNode *cNode; | |
521 | char new_modes[MAXLEN], *argv[MAXNUMPARAMS]; | |
522 | unsigned int nn; | |
523 | ||
524 | if (!IsChannelName(name)) { | |
525 | log_module(MAIN_LOG, LOG_ERROR, "Somebody asked to add channel '%s', which isn't a channel name!", name); | |
526 | return NULL; | |
527 | } | |
528 | if (!modes) | |
529 | modes = ""; | |
530 | ||
531 | safestrncpy(new_modes, modes, sizeof(new_modes)); | |
532 | nn = split_line(new_modes, 0, ArrayLength(argv), argv); | |
533 | if (!(cNode = GetChannel(name))) { | |
534 | cNode = calloc(1, sizeof(*cNode) + strlen(name)); | |
535 | strcpy(cNode->name, name); | |
536 | banList_init(&cNode->banlist); | |
2aef5f4b | 537 | exemptList_init(&cNode->exemptlist); |
d76ed9a9 | 538 | modeList_init(&cNode->members); |
2f61d1d7 | 539 | mod_chanmode(NULL, cNode, argv, nn, MCP_FROM_SERVER); |
d76ed9a9 AS |
540 | dict_insert(channels, cNode->name, cNode); |
541 | cNode->timestamp = time_; | |
542 | rel_age = 1; | |
543 | } else if (cNode->timestamp > time_) { | |
544 | wipeout_channel(cNode, time_, argv, nn); | |
545 | rel_age = 1; | |
546 | } else if (cNode->timestamp == time_) { | |
2f61d1d7 | 547 | mod_chanmode(NULL, cNode, argv, nn, MCP_FROM_SERVER); |
d76ed9a9 AS |
548 | rel_age = 0; |
549 | } else { | |
550 | rel_age = -1; | |
551 | } | |
552 | ||
553 | /* rel_age is the relative ages of our channel data versus what is | |
554 | * in a BURST command. 1 means ours is younger, 0 means both are | |
555 | * the same age, -1 means ours is older. */ | |
556 | ||
557 | /* if it's a new or updated channel, make callbacks */ | |
558 | if (rel_age > 0) | |
559 | for (nn=0; nn<ncf_used; nn++) | |
157ce145 | 560 | ncf_list[nn](cNode, ncf_list_extra[nn]); |
d76ed9a9 AS |
561 | |
562 | /* go through list of bans and add each one */ | |
563 | if (banlist && (rel_age >= 0)) { | |
564 | for (nn=0; banlist[nn];) { | |
565 | char *ban = banlist + nn; | |
566 | struct banNode *bn; | |
567 | while (banlist[nn] != ' ' && banlist[nn]) | |
568 | nn++; | |
569 | while (banlist[nn] == ' ') | |
570 | banlist[nn++] = 0; | |
571 | bn = calloc(1, sizeof(*bn)); | |
572 | safestrncpy(bn->ban, ban, sizeof(bn->ban)); | |
573 | safestrncpy(bn->who, "<unknown>", sizeof(bn->who)); | |
574 | bn->set = now; | |
575 | banList_append(&cNode->banlist, bn); | |
576 | } | |
577 | } | |
578 | ||
2aef5f4b | 579 | /* go through list of exempts and add each one */ |
580 | if (exemptlist && (rel_age >= 0)) { | |
581 | for (nn=0; exemptlist[nn];) { | |
582 | char *exempt = exemptlist + nn; | |
583 | struct exemptNode *en; | |
584 | while (exemptlist[nn] != ' ' && exemptlist[nn]) | |
585 | nn++; | |
586 | while (exemptlist[nn] == ' ') | |
587 | exemptlist[nn++] = 0; | |
588 | en = calloc(1, sizeof(*en)); | |
589 | safestrncpy(en->exempt, exempt, sizeof(en->exempt)); | |
590 | safestrncpy(en->who, "<unknown>", sizeof(en->who)); | |
591 | en->set = now; | |
592 | exemptList_append(&cNode->exemptlist, en); | |
593 | } | |
594 | } | |
595 | ||
d76ed9a9 AS |
596 | return cNode; |
597 | } | |
598 | ||
599 | static del_channel_func_t *dcf_list; | |
1898d8ea | 600 | static void **dcf_list_extra; |
d76ed9a9 AS |
601 | static unsigned int dcf_size = 0, dcf_used = 0; |
602 | ||
603 | void | |
1898d8ea | 604 | reg_del_channel_func(del_channel_func_t handler, void *extra) |
d76ed9a9 AS |
605 | { |
606 | if (dcf_used == dcf_size) { | |
607 | if (dcf_size) { | |
608 | dcf_size <<= 1; | |
609 | dcf_list = realloc(dcf_list, dcf_size*sizeof(dcf_list[0])); | |
1898d8ea | 610 | dcf_list_extra = realloc(dcf_list_extra, dcf_size*sizeof(void*)); |
d76ed9a9 AS |
611 | } else { |
612 | dcf_size = 8; | |
613 | dcf_list = malloc(dcf_size*sizeof(dcf_list[0])); | |
1898d8ea | 614 | dcf_list_extra = malloc(dcf_size*sizeof(dcf_list_extra[0])); |
d76ed9a9 AS |
615 | } |
616 | } | |
1898d8ea | 617 | dcf_list[dcf_used] = handler; |
618 | dcf_list_extra[dcf_used++] = extra; | |
d76ed9a9 AS |
619 | } |
620 | ||
621 | static void | |
622 | DelChannel(struct chanNode *channel) | |
623 | { | |
624 | unsigned int n; | |
625 | ||
1136f709 | 626 | verify(channel); |
d76ed9a9 AS |
627 | dict_remove(channels, channel->name); |
628 | ||
629 | if (channel->members.used || channel->locks) { | |
630 | log_module(MAIN_LOG, LOG_ERROR, "Warning: deleting channel %s with %d users and %d locks remaining.", channel->name, channel->members.used, channel->locks); | |
631 | } | |
632 | ||
633 | /* go through all channel members and delete them from the channel */ | |
634 | for (n=channel->members.used; n>0; ) | |
1136f709 | 635 | DelChannelUser(channel->members.list[--n]->user, channel, NULL, 1); |
d76ed9a9 AS |
636 | |
637 | /* delete all channel bans */ | |
638 | for (n=channel->banlist.used; n>0; ) | |
639 | free(channel->banlist.list[--n]); | |
640 | channel->banlist.used = 0; | |
641 | ||
2aef5f4b | 642 | /* delete all channel exempts */ |
643 | for (n=channel->exemptlist.used; n>0; ) | |
644 | free(channel->exemptlist.list[--n]); | |
645 | channel->exemptlist.used = 0; | |
646 | ||
d76ed9a9 | 647 | for (n=0; n<dcf_used; n++) |
1898d8ea | 648 | dcf_list[n](channel, dcf_list_extra[n]); |
d76ed9a9 AS |
649 | |
650 | modeList_clean(&channel->members); | |
651 | banList_clean(&channel->banlist); | |
2aef5f4b | 652 | exemptList_clean(&channel->exemptlist); |
d76ed9a9 AS |
653 | free(channel); |
654 | } | |
655 | ||
656 | struct modeNode * | |
657 | AddChannelUser(struct userNode *user, struct chanNode* channel) | |
658 | { | |
659 | struct modeNode *mNode; | |
660 | unsigned int n; | |
661 | ||
662 | mNode = GetUserMode(channel, user); | |
663 | if (mNode) | |
664 | return mNode; | |
665 | ||
666 | mNode = malloc(sizeof(*mNode)); | |
667 | ||
668 | /* set up modeNode */ | |
669 | mNode->channel = channel; | |
670 | mNode->user = user; | |
671 | mNode->modes = 0; | |
2f61d1d7 | 672 | mNode->oplevel = -1; |
d76ed9a9 AS |
673 | mNode->idle_since = now; |
674 | ||
675 | /* Add modeNode to channel and to user. | |
676 | * We have to do this before calling join funcs in case the | |
677 | * modeNode is manipulated (e.g. chanserv ops the user). | |
678 | */ | |
679 | modeList_append(&channel->members, mNode); | |
680 | modeList_append(&user->channels, mNode); | |
681 | ||
0d16e639 | 682 | if (channel->members.used == 1 |
2f61d1d7 | 683 | && !(channel->modes & MODE_REGISTERED) |
51e05af8 | 684 | && !(channel->modes & MODE_APASS)) { |
d76ed9a9 | 685 | mNode->modes |= MODE_CHANOP; |
51e05af8 | 686 | log_module(MAIN_LOG, LOG_DEBUG, "setting op"); |
687 | } | |
d76ed9a9 | 688 | |
1136f709 | 689 | if (IsLocal(user)) { |
690 | irc_join(user, channel); | |
691 | } | |
692 | ||
51e05af8 | 693 | for (n=0; (n<jf_used) && !user->dead; n++) { |
d76ed9a9 AS |
694 | /* Callbacks return true if they kick or kill the user, |
695 | * and we can continue without removing mNode. */ | |
fb38e2bc | 696 | if (jf_list[n](mNode, jf_list_extra[n])) |
d76ed9a9 AS |
697 | return NULL; |
698 | } | |
699 | ||
d76ed9a9 AS |
700 | return mNode; |
701 | } | |
702 | ||
703 | static part_func_t *pf_list; | |
63637aea | 704 | static void **pf_list_extra; |
d76ed9a9 AS |
705 | static unsigned int pf_size = 0, pf_used = 0; |
706 | ||
707 | void | |
63637aea | 708 | reg_part_func(part_func_t handler, void *extra) |
d76ed9a9 AS |
709 | { |
710 | if (pf_used == pf_size) { | |
711 | if (pf_size) { | |
712 | pf_size <<= 1; | |
713 | pf_list = realloc(pf_list, pf_size*sizeof(part_func_t)); | |
63637aea | 714 | pf_list_extra = realloc(pf_list_extra, pf_size*sizeof(void*)); |
d76ed9a9 AS |
715 | } else { |
716 | pf_size = 8; | |
717 | pf_list = malloc(pf_size*sizeof(part_func_t)); | |
63637aea | 718 | pf_list_extra = malloc(pf_size*sizeof(void*)); |
d76ed9a9 AS |
719 | } |
720 | } | |
63637aea | 721 | pf_list[pf_used] = handler; |
722 | pf_list_extra[pf_used++] = extra; | |
d76ed9a9 AS |
723 | } |
724 | ||
725 | void | |
63637aea | 726 | unreg_part_func(part_func_t handler, void *extra) |
d76ed9a9 AS |
727 | { |
728 | unsigned int i; | |
729 | for (i=0; i<pf_used; i++) | |
63637aea | 730 | if (pf_list[i] == handler && pf_list_extra[i] == extra) |
d76ed9a9 AS |
731 | break; |
732 | if (i == pf_used) | |
733 | return; | |
734 | memmove(pf_list+i, pf_list+i+1, (pf_used-i-1)*sizeof(pf_list[0])); | |
63637aea | 735 | memmove(pf_list_extra+i, pf_list_extra+i+1, (pf_used-i-1)*sizeof(pf_list_extra[0])); |
d76ed9a9 AS |
736 | pf_used--; |
737 | } | |
738 | ||
739 | void | |
740 | LockChannel(struct chanNode* channel) | |
741 | { | |
742 | channel->locks++; | |
743 | } | |
744 | ||
745 | void | |
746 | UnlockChannel(struct chanNode *channel) | |
747 | { | |
748 | assert(channel->locks > 0); | |
749 | if (!--channel->locks && !channel->members.used) | |
750 | DelChannel(channel); | |
751 | } | |
752 | ||
753 | void | |
754 | DelChannelUser(struct userNode* user, struct chanNode* channel, const char *reason, int deleting) | |
755 | { | |
756 | struct modeNode* mNode; | |
757 | unsigned int n; | |
758 | ||
1136f709 | 759 | if (IsLocal(user) && reason) |
d76ed9a9 AS |
760 | irc_part(user, channel, reason); |
761 | ||
762 | mNode = GetUserMode(channel, user); | |
763 | ||
764 | /* Sometimes we get a PART when the user has been KICKed. | |
765 | * In this case, we get no usermode, and should not try to free it. | |
766 | */ | |
767 | if (!mNode) | |
768 | return; | |
769 | ||
770 | /* remove modeNode from channel and user */ | |
771 | modeList_remove(&channel->members, mNode); | |
772 | modeList_remove(&user->channels, mNode); | |
773 | ||
774 | /* make callbacks */ | |
775 | for (n=0; n<pf_used; n++) | |
63637aea | 776 | pf_list[n](mNode, reason, pf_list_extra[n]); |
d76ed9a9 AS |
777 | |
778 | /* free memory */ | |
779 | free(mNode); | |
780 | ||
2f61d1d7 | 781 | /* A single check for APASS only should be enough here */ |
782 | if (!deleting && !channel->members.used && !channel->locks | |
783 | && !(channel->modes & MODE_REGISTERED) && !(channel->modes & MODE_APASS)) | |
d76ed9a9 AS |
784 | DelChannel(channel); |
785 | } | |
786 | ||
b34a7505 | 787 | static kick_func_t *kf_list; |
91667658 | 788 | static void **kf_list_extra; |
b34a7505 AS |
789 | static unsigned int kf_size = 0, kf_used = 0; |
790 | ||
d76ed9a9 AS |
791 | void |
792 | KickChannelUser(struct userNode* target, struct chanNode* channel, struct userNode *kicker, const char *why) | |
793 | { | |
b34a7505 AS |
794 | unsigned int n; |
795 | ||
d76ed9a9 AS |
796 | if (!target || !channel || IsService(target) || !GetUserMode(channel, target)) |
797 | return; | |
b34a7505 AS |
798 | |
799 | /* This may break things, but lets see.. -Rubin */ | |
800 | for (n=0; n<kf_used; n++) | |
91667658 | 801 | kf_list[n](kicker, target, channel, kf_list_extra[n]); |
b34a7505 | 802 | |
d76ed9a9 AS |
803 | /* don't remove them from the channel, since the server will send a PART */ |
804 | irc_kick(kicker, target, channel, why); | |
805 | ||
806 | if (IsLocal(target)) | |
807 | { | |
808 | /* NULL reason because we don't want a PART message to be | |
809 | sent by DelChannelUser. */ | |
810 | DelChannelUser(target, channel, NULL, 0); | |
811 | } | |
812 | } | |
813 | ||
d76ed9a9 | 814 | void |
91667658 | 815 | reg_kick_func(kick_func_t handler, void *extra) |
d76ed9a9 AS |
816 | { |
817 | if (kf_used == kf_size) { | |
818 | if (kf_size) { | |
819 | kf_size <<= 1; | |
820 | kf_list = realloc(kf_list, kf_size*sizeof(kick_func_t)); | |
91667658 | 821 | kf_list_extra = realloc(kf_list_extra, kf_size*sizeof(void*)); |
d76ed9a9 AS |
822 | } else { |
823 | kf_size = 8; | |
824 | kf_list = malloc(kf_size*sizeof(kick_func_t)); | |
91667658 | 825 | kf_list_extra = malloc(kf_size*sizeof(void*)); |
d76ed9a9 AS |
826 | } |
827 | } | |
91667658 | 828 | kf_list[kf_used] = handler; |
829 | kf_list_extra[kf_used++] = extra; | |
d76ed9a9 AS |
830 | } |
831 | ||
832 | void | |
833 | ChannelUserKicked(struct userNode* kicker, struct userNode* victim, struct chanNode* channel) | |
834 | { | |
835 | unsigned int n; | |
836 | struct modeNode *mn; | |
837 | ||
b2fd1d25 | 838 | if (!victim || !channel || !GetUserMode(channel, victim)) |
d76ed9a9 AS |
839 | return; |
840 | ||
841 | /* Update the kicker's idle time (kicker may be null if it was a server) */ | |
842 | if (kicker && (mn = GetUserMode(channel, kicker))) | |
843 | mn->idle_since = now; | |
844 | ||
845 | for (n=0; n<kf_used; n++) | |
91667658 | 846 | kf_list[n](kicker, victim, channel, kf_list_extra[n]); |
d76ed9a9 AS |
847 | |
848 | DelChannelUser(victim, channel, 0, 0); | |
849 | ||
850 | if (IsLocal(victim)) | |
851 | irc_part(victim, channel, NULL); | |
852 | } | |
853 | ||
854 | int ChannelBanExists(struct chanNode *channel, const char *ban) | |
855 | { | |
856 | unsigned int n; | |
857 | ||
858 | for (n = 0; n < channel->banlist.used; n++) | |
859 | if (match_ircglobs(channel->banlist.list[n]->ban, ban)) | |
860 | return 1; | |
861 | return 0; | |
862 | } | |
863 | ||
2aef5f4b | 864 | int ChannelExemptExists(struct chanNode *channel, const char *exempt) |
865 | { | |
866 | unsigned int n; | |
867 | ||
868 | for (n = 0; n < channel->exemptlist.used; n++) | |
869 | if (match_ircglobs(channel->exemptlist.list[n]->exempt, exempt)) | |
870 | return 1; | |
871 | return 0; | |
872 | } | |
873 | ||
d76ed9a9 | 874 | static topic_func_t *tf_list; |
5a2c7cf6 | 875 | static void **tf_list_extra; |
d76ed9a9 AS |
876 | static unsigned int tf_size = 0, tf_used = 0; |
877 | ||
878 | void | |
5a2c7cf6 | 879 | reg_topic_func(topic_func_t handler, void *extra) |
d76ed9a9 AS |
880 | { |
881 | if (tf_used == tf_size) { | |
882 | if (tf_size) { | |
883 | tf_size <<= 1; | |
884 | tf_list = realloc(tf_list, tf_size*sizeof(topic_func_t)); | |
5a2c7cf6 | 885 | tf_list_extra = realloc(tf_list_extra, tf_size*sizeof(void*)); |
d76ed9a9 AS |
886 | } else { |
887 | tf_size = 8; | |
888 | tf_list = malloc(tf_size*sizeof(topic_func_t)); | |
5a2c7cf6 | 889 | tf_list_extra = malloc(tf_size*sizeof(void*)); |
d76ed9a9 AS |
890 | } |
891 | } | |
5a2c7cf6 | 892 | tf_list[tf_used] = handler; |
893 | tf_list_extra[tf_used++] = extra; | |
d76ed9a9 AS |
894 | } |
895 | ||
896 | void | |
7fda2b52 | 897 | SetChannelTopic(struct chanNode *channel, struct userNode *service, struct userNode *user, const char *topic, int announce) |
d76ed9a9 AS |
898 | { |
899 | unsigned int n; | |
900 | struct modeNode *mn; | |
901 | char old_topic[TOPICLEN+1]; | |
902 | ||
903 | safestrncpy(old_topic, channel->topic, sizeof(old_topic)); | |
904 | safestrncpy(channel->topic, topic, sizeof(channel->topic)); | |
905 | channel->topic_time = now; | |
906 | ||
907 | if (user) { | |
908 | safestrncpy(channel->topic_nick, user->nick, sizeof(channel->topic_nick)); | |
909 | ||
910 | /* Update the setter's idle time */ | |
911 | if ((mn = GetUserMode(channel, user))) | |
912 | mn->idle_since = now; | |
913 | } | |
914 | ||
915 | if (announce) { | |
916 | /* We don't really care if a local user messes with the topic, | |
917 | * so don't call the tf_list functions. */ | |
7fda2b52 | 918 | irc_topic(service, user, channel, topic); |
d76ed9a9 AS |
919 | } else { |
920 | for (n=0; n<tf_used; n++) | |
1136f709 | 921 | /* A topic change handler can return non-zero to indicate |
922 | * that it has reverted the topic change, and that further | |
923 | * hooks should not be called. | |
924 | */ | |
5a2c7cf6 | 925 | if (tf_list[n](user, channel, old_topic, tf_list_extra[n])) |
d76ed9a9 AS |
926 | break; |
927 | } | |
928 | } | |
929 | ||
930 | struct chanNode * | |
931 | GetChannel(const char *name) | |
932 | { | |
933 | return dict_find(channels, name, NULL); | |
934 | } | |
935 | ||
936 | struct modeNode * | |
937 | GetUserMode(struct chanNode *channel, struct userNode *user) | |
938 | { | |
939 | unsigned int n; | |
940 | struct modeNode *mn = NULL; | |
ec1a68c8 | 941 | |
942 | verify(channel); | |
943 | verify(channel->members.list); | |
944 | verify(user); | |
945 | verify(user->channels.list); | |
d76ed9a9 AS |
946 | if (channel->members.used < user->channels.used) { |
947 | for (n=0; n<channel->members.used; n++) { | |
ec1a68c8 | 948 | verify(channel->members.list[n]); |
d76ed9a9 AS |
949 | if (user == channel->members.list[n]->user) { |
950 | mn = channel->members.list[n]; | |
951 | break; | |
952 | } | |
953 | } | |
954 | } else { | |
955 | for (n=0; n<user->channels.used; n++) { | |
ec1a68c8 | 956 | verify(user->channels.list[n]); |
d76ed9a9 AS |
957 | if (channel == user->channels.list[n]->channel) { |
958 | mn = user->channels.list[n]; | |
959 | break; | |
960 | } | |
961 | } | |
962 | } | |
963 | return mn; | |
964 | } | |
965 | ||
ac3bdc8d AS |
966 | struct userNode *IsInChannel(struct chanNode *channel, struct userNode *user) |
967 | { | |
968 | unsigned int n; | |
ac3bdc8d AS |
969 | |
970 | verify(channel); | |
971 | verify(channel->members.list); | |
972 | verify(user); | |
973 | verify(user->channels.list); | |
974 | if (channel->members.used < user->channels.used) { | |
975 | for (n=0; n<channel->members.used; n++) { | |
976 | verify(channel->members.list[n]); | |
977 | if (user == channel->members.list[n]->user) { | |
978 | return(user); | |
979 | } | |
980 | } | |
981 | } else { | |
982 | for (n=0; n<user->channels.used; n++) { | |
983 | verify(user->channels.list[n]); | |
984 | if (channel == user->channels.list[n]->channel) { | |
985 | return(user); | |
986 | } | |
987 | } | |
988 | } | |
989 | return NULL; | |
990 | } | |
991 | ||
d76ed9a9 AS |
992 | DEFINE_LIST(userList, struct userNode*) |
993 | DEFINE_LIST(modeList, struct modeNode*) | |
994 | DEFINE_LIST(banList, struct banNode*) | |
2aef5f4b | 995 | DEFINE_LIST(exemptList, struct exemptNode*) |
d76ed9a9 AS |
996 | DEFINE_LIST(channelList, struct chanNode*) |
997 | DEFINE_LIST(serverList, struct server*) | |
998 | ||
999 | static void | |
30874d66 | 1000 | hash_cleanup(UNUSED_ARG(void *extra)) |
d76ed9a9 AS |
1001 | { |
1002 | dict_iterator_t it, next; | |
1003 | ||
1004 | DelServer(self, 0, NULL); | |
1005 | for (it = dict_first(channels); it; it = next) { | |
1006 | next = iter_next(it); | |
1007 | DelChannel(iter_data(it)); | |
1008 | } | |
1009 | dict_delete(channels); | |
1010 | dict_delete(clients); | |
1011 | dict_delete(servers); | |
1012 | userList_clean(&curr_opers); | |
1013 | ||
1014 | free(slf_list); | |
63189c10 | 1015 | free(slf_list_extra); |
d76ed9a9 | 1016 | free(nuf_list); |
63189c10 | 1017 | free(nuf_list_extra); |
d76ed9a9 | 1018 | free(ncf2_list); |
63189c10 | 1019 | free(ncf2_list_extra); |
d76ed9a9 | 1020 | free(duf_list); |
63189c10 | 1021 | free(duf_list_extra); |
d76ed9a9 | 1022 | free(ncf_list); |
157ce145 | 1023 | free(ncf_list_extra); |
d76ed9a9 | 1024 | free(jf_list); |
fb38e2bc | 1025 | free(jf_list_extra); |
d76ed9a9 | 1026 | free(dcf_list); |
1898d8ea | 1027 | free(dcf_list_extra); |
d76ed9a9 | 1028 | free(pf_list); |
63637aea | 1029 | free(pf_list_extra); |
d76ed9a9 | 1030 | free(kf_list); |
91667658 | 1031 | free(kf_list_extra); |
d76ed9a9 | 1032 | free(tf_list); |
5a2c7cf6 | 1033 | free(tf_list_extra); |
d76ed9a9 | 1034 | } |