]> jfr.im git - irc/evilnet/x3.git/blob - src/hash.c
more python work. Not safe to run yet
[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.
257
258 P10 Protocol violation if (user->modes & FLAGS_STAMPED) here.
259 */
260 if (account_func)
261 account_func(user, stamp);
262
263 #ifdef WITH_PROTOCOL_P10
264 /* Mark the user so we don't stamp it again. */
265 user->modes |= FLAGS_STAMPED;
266 #endif
267 }
268
269 void
270 StampUser(struct userNode *user, const char *stamp, time_t timestamp)
271 {
272 #ifdef WITH_PROTOCOL_P10
273 /* The P10 protocol says we can't stamp users who already
274 have a stamp. */
275 if (IsStamped(user))
276 return;
277 #endif
278
279 irc_account(user, stamp, timestamp);
280 user->modes |= FLAGS_STAMPED;
281 }
282
283 void
284 assign_fakehost(struct userNode *user, const char *host, int announce)
285 {
286 safestrncpy(user->fakehost, host, sizeof(user->fakehost));
287 if (announce)
288 irc_fakehost(user, host);
289 }
290
291 void
292 set_geoip_info(struct userNode *user)
293 {
294 if(IsLocal(user))
295 return;
296 /* Need the libs and the headers if this is going to compile properly */
297 #if defined(HAVE_LIBGEOIP)&&defined(HAVE_GEOIP_H)&&defined(HAVE_GEOIPCITY_H)
298 GeoIPRecord * gir;
299 const char *geoip_data_file = NULL;
300 const char *geoip_city_file = NULL;
301
302 geoip_data_file = conf_get_data("services/opserv/geoip_data_file", RECDB_QSTRING);
303 geoip_city_file = conf_get_data("services/opserv/geoip_city_data_file", RECDB_QSTRING);
304
305 if ((!geoip_data_file && !geoip_city_file))
306 return; /* Admin doesnt want to use geoip functions */
307
308 if (geoip_data_file && !gi)
309 gi = GeoIP_open(geoip_data_file, GEOIP_MEMORY_CACHE | GEOIP_CHECK_CACHE);
310
311 if (geoip_city_file && !cgi)
312 cgi = GeoIP_open(geoip_city_file, GEOIP_MEMORY_CACHE | GEOIP_CHECK_CACHE);
313
314 if (cgi) {
315 gir = GeoIP_record_by_name(cgi, user->hostname);
316 if (gir) {
317 user->country_name = strdup(gir->country_name ? gir->country_name : "");
318 user->country_code = strdup(gir->country_code ? gir->country_code : "");
319 user->city = strdup(gir->city ? gir->city : "");
320 user->region = strdup(gir->region ? gir->region : "");
321 user->postal_code = strdup(gir->postal_code ? gir->postal_code : "");
322
323 user->latitude = gir->latitude ? gir->latitude : 0;
324 user->longitude = gir->longitude ? gir->longitude : 0;
325 user->dma_code = gir->dma_code ? gir->dma_code : 0;
326 user->area_code = gir->area_code ? gir->area_code : 0;
327
328 GeoIPRecord_delete(gir);
329 }
330
331 return;
332 } else if (gi) {
333 const char *country = GeoIP_country_name_by_name(gi, user->hostname);
334 user->country_name = strdup(country ? country : "");
335 return;
336 }
337
338 return;
339 #endif
340 }
341
342 static new_channel_func_t *ncf_list;
343 static unsigned int ncf_size = 0, ncf_used = 0;
344
345 void
346 reg_new_channel_func(new_channel_func_t handler)
347 {
348 if (ncf_used == ncf_size) {
349 if (ncf_size) {
350 ncf_size <<= 1;
351 ncf_list = realloc(ncf_list, ncf_size*sizeof(ncf_list[0]));
352 } else {
353 ncf_size = 8;
354 ncf_list = malloc(ncf_size*sizeof(ncf_list[0]));
355 }
356 }
357 ncf_list[ncf_used++] = handler;
358 }
359
360 static join_func_t *jf_list;
361 static unsigned int jf_size = 0, jf_used = 0;
362
363 void
364 reg_join_func(join_func_t handler)
365 {
366 if (jf_used == jf_size) {
367 if (jf_size) {
368 jf_size <<= 1;
369 jf_list = realloc(jf_list, jf_size*sizeof(join_func_t));
370 } else {
371 jf_size = 8;
372 jf_list = malloc(jf_size*sizeof(join_func_t));
373 }
374 }
375 jf_list[jf_used++] = handler;
376 }
377
378 int rel_age;
379
380 static void
381 wipeout_channel(struct chanNode *cNode, time_t new_time, char **modes, unsigned int modec) {
382 unsigned int orig_limit;
383 chan_mode_t orig_modes;
384 char orig_key[KEYLEN+1];
385 char orig_apass[KEYLEN+1];
386 char orig_upass[KEYLEN+1];
387 unsigned int nn, argc;
388
389 /* nuke old topic */
390 cNode->topic[0] = '\0';
391 cNode->topic_nick[0] = '\0';
392 cNode->topic_time = 0;
393
394 /* remember the old modes, and update them with the new */
395 orig_modes = cNode->modes;
396 orig_limit = cNode->limit;
397 strcpy(orig_key, cNode->key);
398 strcpy(orig_upass, cNode->upass);
399 strcpy(orig_apass, cNode->apass);
400 cNode->modes = 0;
401 mod_chanmode(NULL, cNode, modes, modec, 0);
402 cNode->timestamp = new_time;
403
404 /* remove our old ban list, replace it with the new one */
405 for (nn=0; nn<cNode->banlist.used; nn++)
406 free(cNode->banlist.list[nn]);
407 cNode->banlist.used = 0;
408
409 /* remove our old exe,[t list, replace it with the new one */
410 for (nn=0; nn<cNode->exemptlist.used; nn++)
411 free(cNode->exemptlist.list[nn]);
412 cNode->exemptlist.used = 0;
413
414 /* deop anybody in the channel now, but count services to reop */
415 for (nn=argc=0; nn<cNode->members.used; nn++) {
416 struct modeNode *mn = cNode->members.list[nn];
417 if ((mn->modes & MODE_CHANOP) && IsService(mn->user) && IsLocal(mn->user))
418 argc++;
419 }
420
421 if (argc) {
422 struct mod_chanmode *change;
423
424 change = mod_chanmode_alloc(argc);
425 change->modes_clear = 0;
426 change->modes_set = orig_modes;
427 change->new_limit = orig_limit;
428 strcpy(change->new_key, orig_key);
429 strcpy(change->new_upass, orig_upass);
430 strcpy(change->new_apass, orig_apass);
431 for (nn = argc = 0; nn < cNode->members.used; ++nn) {
432 struct modeNode *mn = cNode->members.list[nn];
433 if ((mn->modes & MODE_CHANOP) && IsService(mn->user) && IsLocal(mn->user)) {
434 change->args[argc].mode = MODE_CHANOP;
435 change->args[argc].u.member = mn;
436 argc++;
437 }
438 }
439 assert(argc == change->argc);
440 change->args[0].u.member->modes &= ~MODE_CHANOP;
441 mod_chanmode_announce(change->args[0].u.member->user, cNode, change);
442 mod_chanmode_free(change);
443 }
444 }
445
446 struct chanNode *
447 AddChannel(const char *name, time_t time_, const char *modes, char *banlist, char *exemptlist)
448 {
449 struct chanNode *cNode;
450 char new_modes[MAXLEN], *argv[MAXNUMPARAMS];
451 unsigned int nn;
452
453 if (!IsChannelName(name)) {
454 log_module(MAIN_LOG, LOG_ERROR, "Somebody asked to add channel '%s', which isn't a channel name!", name);
455 return NULL;
456 }
457 if (!modes)
458 modes = "";
459
460 safestrncpy(new_modes, modes, sizeof(new_modes));
461 nn = split_line(new_modes, 0, ArrayLength(argv), argv);
462 if (!(cNode = GetChannel(name))) {
463 cNode = calloc(1, sizeof(*cNode) + strlen(name));
464 strcpy(cNode->name, name);
465 banList_init(&cNode->banlist);
466 exemptList_init(&cNode->exemptlist);
467 modeList_init(&cNode->members);
468 mod_chanmode(NULL, cNode, argv, nn, MCP_FROM_SERVER);
469 dict_insert(channels, cNode->name, cNode);
470 cNode->timestamp = time_;
471 rel_age = 1;
472 } else if (cNode->timestamp > time_) {
473 wipeout_channel(cNode, time_, argv, nn);
474 rel_age = 1;
475 } else if (cNode->timestamp == time_) {
476 mod_chanmode(NULL, cNode, argv, nn, MCP_FROM_SERVER);
477 rel_age = 0;
478 } else {
479 rel_age = -1;
480 }
481
482 /* rel_age is the relative ages of our channel data versus what is
483 * in a BURST command. 1 means ours is younger, 0 means both are
484 * the same age, -1 means ours is older. */
485
486 /* if it's a new or updated channel, make callbacks */
487 if (rel_age > 0)
488 for (nn=0; nn<ncf_used; nn++)
489 ncf_list[nn](cNode);
490
491 /* go through list of bans and add each one */
492 if (banlist && (rel_age >= 0)) {
493 for (nn=0; banlist[nn];) {
494 char *ban = banlist + nn;
495 struct banNode *bn;
496 while (banlist[nn] != ' ' && banlist[nn])
497 nn++;
498 while (banlist[nn] == ' ')
499 banlist[nn++] = 0;
500 bn = calloc(1, sizeof(*bn));
501 safestrncpy(bn->ban, ban, sizeof(bn->ban));
502 safestrncpy(bn->who, "<unknown>", sizeof(bn->who));
503 bn->set = now;
504 banList_append(&cNode->banlist, bn);
505 }
506 }
507
508 /* go through list of exempts and add each one */
509 if (exemptlist && (rel_age >= 0)) {
510 for (nn=0; exemptlist[nn];) {
511 char *exempt = exemptlist + nn;
512 struct exemptNode *en;
513 while (exemptlist[nn] != ' ' && exemptlist[nn])
514 nn++;
515 while (exemptlist[nn] == ' ')
516 exemptlist[nn++] = 0;
517 en = calloc(1, sizeof(*en));
518 safestrncpy(en->exempt, exempt, sizeof(en->exempt));
519 safestrncpy(en->who, "<unknown>", sizeof(en->who));
520 en->set = now;
521 exemptList_append(&cNode->exemptlist, en);
522 }
523 }
524
525 return cNode;
526 }
527
528 static del_channel_func_t *dcf_list;
529 static unsigned int dcf_size = 0, dcf_used = 0;
530
531 void
532 reg_del_channel_func(del_channel_func_t handler)
533 {
534 if (dcf_used == dcf_size) {
535 if (dcf_size) {
536 dcf_size <<= 1;
537 dcf_list = realloc(dcf_list, dcf_size*sizeof(dcf_list[0]));
538 } else {
539 dcf_size = 8;
540 dcf_list = malloc(dcf_size*sizeof(dcf_list[0]));
541 }
542 }
543 dcf_list[dcf_used++] = handler;
544 }
545
546 static void
547 DelChannel(struct chanNode *channel)
548 {
549 unsigned int n;
550
551 dict_remove(channels, channel->name);
552
553 if (channel->members.used || channel->locks) {
554 log_module(MAIN_LOG, LOG_ERROR, "Warning: deleting channel %s with %d users and %d locks remaining.", channel->name, channel->members.used, channel->locks);
555 }
556
557 /* go through all channel members and delete them from the channel */
558 for (n=channel->members.used; n>0; )
559 DelChannelUser(channel->members.list[--n]->user, channel, false, 1);
560
561 /* delete all channel bans */
562 for (n=channel->banlist.used; n>0; )
563 free(channel->banlist.list[--n]);
564 channel->banlist.used = 0;
565
566 /* delete all channel exempts */
567 for (n=channel->exemptlist.used; n>0; )
568 free(channel->exemptlist.list[--n]);
569 channel->exemptlist.used = 0;
570
571 for (n=0; n<dcf_used; n++)
572 dcf_list[n](channel);
573
574 modeList_clean(&channel->members);
575 banList_clean(&channel->banlist);
576 exemptList_clean(&channel->exemptlist);
577 free(channel);
578 }
579
580 struct modeNode *
581 AddChannelUser(struct userNode *user, struct chanNode* channel)
582 {
583 struct modeNode *mNode;
584 unsigned int n;
585
586 mNode = GetUserMode(channel, user);
587 if (mNode)
588 return mNode;
589
590 mNode = malloc(sizeof(*mNode));
591
592 /* set up modeNode */
593 mNode->channel = channel;
594 mNode->user = user;
595 mNode->modes = 0;
596 mNode->oplevel = -1;
597 mNode->idle_since = now;
598
599 /* Add modeNode to channel and to user.
600 * We have to do this before calling join funcs in case the
601 * modeNode is manipulated (e.g. chanserv ops the user).
602 */
603 modeList_append(&channel->members, mNode);
604 modeList_append(&user->channels, mNode);
605
606 if (channel->members.used == 1
607 && !(channel->modes & MODE_REGISTERED)
608 && !(channel->modes & MODE_APASS))
609 mNode->modes |= MODE_CHANOP;
610
611 for (n=0; n<jf_used; n++) {
612 /* Callbacks return true if they kick or kill the user,
613 * and we can continue without removing mNode. */
614 if (jf_list[n](mNode))
615 return NULL;
616 }
617
618 if (IsLocal(user))
619 irc_join(user, channel);
620
621 return mNode;
622 }
623
624 static part_func_t *pf_list;
625 static unsigned int pf_size = 0, pf_used = 0;
626
627 void
628 reg_part_func(part_func_t handler)
629 {
630 if (pf_used == pf_size) {
631 if (pf_size) {
632 pf_size <<= 1;
633 pf_list = realloc(pf_list, pf_size*sizeof(part_func_t));
634 } else {
635 pf_size = 8;
636 pf_list = malloc(pf_size*sizeof(part_func_t));
637 }
638 }
639 pf_list[pf_used++] = handler;
640 }
641
642 void
643 unreg_part_func(part_func_t handler)
644 {
645 unsigned int i;
646 for (i=0; i<pf_used; i++)
647 if (pf_list[i] == handler)
648 break;
649 if (i == pf_used)
650 return;
651 memmove(pf_list+i, pf_list+i+1, (pf_used-i-1)*sizeof(pf_list[0]));
652 pf_used--;
653 }
654
655 void
656 LockChannel(struct chanNode* channel)
657 {
658 channel->locks++;
659 }
660
661 void
662 UnlockChannel(struct chanNode *channel)
663 {
664 assert(channel->locks > 0);
665 if (!--channel->locks && !channel->members.used)
666 DelChannel(channel);
667 }
668
669 void
670 DelChannelUser(struct userNode* user, struct chanNode* channel, const char *reason, int deleting)
671 {
672 struct modeNode* mNode;
673 unsigned int n;
674
675 if (reason)
676 irc_part(user, channel, reason);
677
678 mNode = GetUserMode(channel, user);
679
680 /* Sometimes we get a PART when the user has been KICKed.
681 * In this case, we get no usermode, and should not try to free it.
682 */
683 if (!mNode)
684 return;
685
686 /* remove modeNode from channel and user */
687 modeList_remove(&channel->members, mNode);
688 modeList_remove(&user->channels, mNode);
689
690 /* make callbacks */
691 for (n=0; n<pf_used; n++)
692 pf_list[n](mNode, reason);
693
694 /* free memory */
695 free(mNode);
696
697 /* A single check for APASS only should be enough here */
698 if (!deleting && !channel->members.used && !channel->locks
699 && !(channel->modes & MODE_REGISTERED) && !(channel->modes & MODE_APASS))
700 DelChannel(channel);
701 }
702
703 static kick_func_t *kf_list;
704 static unsigned int kf_size = 0, kf_used = 0;
705
706 void
707 KickChannelUser(struct userNode* target, struct chanNode* channel, struct userNode *kicker, const char *why)
708 {
709 unsigned int n;
710
711 if (!target || !channel || IsService(target) || !GetUserMode(channel, target))
712 return;
713
714 /* This may break things, but lets see.. -Rubin */
715 for (n=0; n<kf_used; n++)
716 kf_list[n](kicker, target, channel);
717
718 /* don't remove them from the channel, since the server will send a PART */
719 irc_kick(kicker, target, channel, why);
720
721 if (IsLocal(target))
722 {
723 /* NULL reason because we don't want a PART message to be
724 sent by DelChannelUser. */
725 DelChannelUser(target, channel, NULL, 0);
726 }
727 }
728
729 void
730 reg_kick_func(kick_func_t handler)
731 {
732 if (kf_used == kf_size) {
733 if (kf_size) {
734 kf_size <<= 1;
735 kf_list = realloc(kf_list, kf_size*sizeof(kick_func_t));
736 } else {
737 kf_size = 8;
738 kf_list = malloc(kf_size*sizeof(kick_func_t));
739 }
740 }
741 kf_list[kf_used++] = handler;
742 }
743
744 void
745 ChannelUserKicked(struct userNode* kicker, struct userNode* victim, struct chanNode* channel)
746 {
747 unsigned int n;
748 struct modeNode *mn;
749
750 if (!victim || !channel || !GetUserMode(channel, victim))
751 return;
752
753 /* Update the kicker's idle time (kicker may be null if it was a server) */
754 if (kicker && (mn = GetUserMode(channel, kicker)))
755 mn->idle_since = now;
756
757 for (n=0; n<kf_used; n++)
758 kf_list[n](kicker, victim, channel);
759
760 DelChannelUser(victim, channel, 0, 0);
761
762 if (IsLocal(victim))
763 irc_part(victim, channel, NULL);
764 }
765
766 int ChannelBanExists(struct chanNode *channel, const char *ban)
767 {
768 unsigned int n;
769
770 for (n = 0; n < channel->banlist.used; n++)
771 if (match_ircglobs(channel->banlist.list[n]->ban, ban))
772 return 1;
773 return 0;
774 }
775
776 int ChannelExemptExists(struct chanNode *channel, const char *exempt)
777 {
778 unsigned int n;
779
780 for (n = 0; n < channel->exemptlist.used; n++)
781 if (match_ircglobs(channel->exemptlist.list[n]->exempt, exempt))
782 return 1;
783 return 0;
784 }
785
786 static topic_func_t *tf_list;
787 static unsigned int tf_size = 0, tf_used = 0;
788
789 void
790 reg_topic_func(topic_func_t handler)
791 {
792 if (tf_used == tf_size) {
793 if (tf_size) {
794 tf_size <<= 1;
795 tf_list = realloc(tf_list, tf_size*sizeof(topic_func_t));
796 } else {
797 tf_size = 8;
798 tf_list = malloc(tf_size*sizeof(topic_func_t));
799 }
800 }
801 tf_list[tf_used++] = handler;
802 }
803
804 void
805 SetChannelTopic(struct chanNode *channel, struct userNode *service, struct userNode *user, const char *topic, int announce)
806 {
807 unsigned int n;
808 struct modeNode *mn;
809 char old_topic[TOPICLEN+1];
810
811 safestrncpy(old_topic, channel->topic, sizeof(old_topic));
812 safestrncpy(channel->topic, topic, sizeof(channel->topic));
813 channel->topic_time = now;
814
815 if (user) {
816 safestrncpy(channel->topic_nick, user->nick, sizeof(channel->topic_nick));
817
818 /* Update the setter's idle time */
819 if ((mn = GetUserMode(channel, user)))
820 mn->idle_since = now;
821 }
822
823 if (announce) {
824 /* We don't really care if a local user messes with the topic,
825 * so don't call the tf_list functions. */
826 irc_topic(service, user, channel, topic);
827 } else {
828 for (n=0; n<tf_used; n++)
829 if (tf_list[n](user, channel, old_topic))
830 break;
831 }
832 }
833
834 struct chanNode *
835 GetChannel(const char *name)
836 {
837 return dict_find(channels, name, NULL);
838 }
839
840 struct modeNode *
841 GetUserMode(struct chanNode *channel, struct userNode *user)
842 {
843 unsigned int n;
844 struct modeNode *mn = NULL;
845
846 verify(channel);
847 verify(channel->members.list);
848 verify(user);
849 verify(user->channels.list);
850 if (channel->members.used < user->channels.used) {
851 for (n=0; n<channel->members.used; n++) {
852 verify(channel->members.list[n]);
853 if (user == channel->members.list[n]->user) {
854 mn = channel->members.list[n];
855 break;
856 }
857 }
858 } else {
859 for (n=0; n<user->channels.used; n++) {
860 verify(user->channels.list[n]);
861 if (channel == user->channels.list[n]->channel) {
862 mn = user->channels.list[n];
863 break;
864 }
865 }
866 }
867 return mn;
868 }
869
870 struct userNode *IsInChannel(struct chanNode *channel, struct userNode *user)
871 {
872 unsigned int n;
873
874 verify(channel);
875 verify(channel->members.list);
876 verify(user);
877 verify(user->channels.list);
878 if (channel->members.used < user->channels.used) {
879 for (n=0; n<channel->members.used; n++) {
880 verify(channel->members.list[n]);
881 if (user == channel->members.list[n]->user) {
882 return(user);
883 }
884 }
885 } else {
886 for (n=0; n<user->channels.used; n++) {
887 verify(user->channels.list[n]);
888 if (channel == user->channels.list[n]->channel) {
889 return(user);
890 }
891 }
892 }
893 return NULL;
894 }
895
896 DEFINE_LIST(userList, struct userNode*)
897 DEFINE_LIST(modeList, struct modeNode*)
898 DEFINE_LIST(banList, struct banNode*)
899 DEFINE_LIST(exemptList, struct exemptNode*)
900 DEFINE_LIST(channelList, struct chanNode*)
901 DEFINE_LIST(serverList, struct server*)
902
903 static void
904 hash_cleanup(void)
905 {
906 dict_iterator_t it, next;
907
908 DelServer(self, 0, NULL);
909 for (it = dict_first(channels); it; it = next) {
910 next = iter_next(it);
911 DelChannel(iter_data(it));
912 }
913 dict_delete(channels);
914 dict_delete(clients);
915 dict_delete(servers);
916 userList_clean(&curr_opers);
917
918 free(slf_list);
919 free(nuf_list);
920 free(ncf2_list);
921 free(duf_list);
922 free(ncf_list);
923 free(jf_list);
924 free(dcf_list);
925 free(pf_list);
926 free(kf_list);
927 free(tf_list);
928 }