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