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