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