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