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