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