]> jfr.im git - irc/evilnet/x3.git/blob - src/hash.c
Updated OpServ level in LDAP feature to remove the attribute when 0 instead of settin...
[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_extra[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 void **ncf_list_extra;
407 static unsigned int ncf_size = 0, ncf_used = 0;
408
409 void
410 reg_new_channel_func(new_channel_func_t handler, void *extra)
411 {
412 if (ncf_used == ncf_size) {
413 if (ncf_size) {
414 ncf_size <<= 1;
415 ncf_list = realloc(ncf_list, ncf_size*sizeof(ncf_list[0]));
416 ncf_list_extra = realloc(ncf_list_extra, ncf_size*sizeof(void*));
417 } else {
418 ncf_size = 8;
419 ncf_list = malloc(ncf_size*sizeof(ncf_list[0]));
420 ncf_list_extra = malloc(ncf_size*sizeof(void*));
421 }
422 }
423 ncf_list[ncf_used] = handler;
424 ncf_list_extra[ncf_used++] = extra;
425 }
426
427 static join_func_t *jf_list;
428 static void **jf_list_extra;
429 static unsigned int jf_size = 0, jf_used = 0;
430
431 void
432 reg_join_func(join_func_t handler, void *extra)
433 {
434 if (jf_used == jf_size) {
435 if (jf_size) {
436 jf_size <<= 1;
437 jf_list = realloc(jf_list, jf_size*sizeof(join_func_t));
438 jf_list_extra = realloc(jf_list_extra, jf_size*sizeof(void*));
439 } else {
440 jf_size = 8;
441 jf_list = malloc(jf_size*sizeof(join_func_t));
442 jf_list_extra = malloc(jf_size*sizeof(void*));
443 }
444 }
445 jf_list[jf_used] = handler;
446 jf_list_extra[jf_used++] = extra;
447 }
448
449 int rel_age;
450
451 static void
452 wipeout_channel(struct chanNode *cNode, time_t new_time, char **modes, unsigned int modec) {
453 unsigned int orig_limit;
454 chan_mode_t orig_modes;
455 char orig_key[KEYLEN+1];
456 char orig_apass[KEYLEN+1];
457 char orig_upass[KEYLEN+1];
458 unsigned int nn, argc;
459
460 /* nuke old topic */
461 cNode->topic[0] = '\0';
462 cNode->topic_nick[0] = '\0';
463 cNode->topic_time = 0;
464
465 /* remember the old modes, and update them with the new */
466 orig_modes = cNode->modes;
467 orig_limit = cNode->limit;
468 strcpy(orig_key, cNode->key);
469 strcpy(orig_upass, cNode->upass);
470 strcpy(orig_apass, cNode->apass);
471 cNode->modes = 0;
472 mod_chanmode(NULL, cNode, modes, modec, 0);
473 cNode->timestamp = new_time;
474
475 /* remove our old ban list, replace it with the new one */
476 for (nn=0; nn<cNode->banlist.used; nn++)
477 free(cNode->banlist.list[nn]);
478 cNode->banlist.used = 0;
479
480 /* remove our old exe,[t list, replace it with the new one */
481 for (nn=0; nn<cNode->exemptlist.used; nn++)
482 free(cNode->exemptlist.list[nn]);
483 cNode->exemptlist.used = 0;
484
485 /* deop anybody in the channel now, but count services to reop */
486 for (nn=argc=0; nn<cNode->members.used; nn++) {
487 struct modeNode *mn = cNode->members.list[nn];
488 if ((mn->modes & MODE_CHANOP) && IsService(mn->user) && IsLocal(mn->user))
489 argc++;
490 }
491
492 if (argc) {
493 struct mod_chanmode *change;
494
495 change = mod_chanmode_alloc(argc);
496 change->modes_clear = 0;
497 change->modes_set = orig_modes;
498 change->new_limit = orig_limit;
499 strcpy(change->new_key, orig_key);
500 strcpy(change->new_upass, orig_upass);
501 strcpy(change->new_apass, orig_apass);
502 for (nn = argc = 0; nn < cNode->members.used; ++nn) {
503 struct modeNode *mn = cNode->members.list[nn];
504 if ((mn->modes & MODE_CHANOP) && IsService(mn->user) && IsLocal(mn->user)) {
505 change->args[argc].mode = MODE_CHANOP;
506 change->args[argc].u.member = mn;
507 argc++;
508 }
509 }
510 assert(argc == change->argc);
511 change->args[0].u.member->modes &= ~MODE_CHANOP;
512 mod_chanmode_announce(change->args[0].u.member->user, cNode, change);
513 mod_chanmode_free(change);
514 }
515 }
516
517 struct chanNode *
518 AddChannel(const char *name, time_t time_, const char *modes, char *banlist, char *exemptlist)
519 {
520 struct chanNode *cNode;
521 char new_modes[MAXLEN], *argv[MAXNUMPARAMS];
522 unsigned int nn;
523
524 if (!IsChannelName(name)) {
525 log_module(MAIN_LOG, LOG_ERROR, "Somebody asked to add channel '%s', which isn't a channel name!", name);
526 return NULL;
527 }
528 if (!modes)
529 modes = "";
530
531 safestrncpy(new_modes, modes, sizeof(new_modes));
532 nn = split_line(new_modes, 0, ArrayLength(argv), argv);
533 if (!(cNode = GetChannel(name))) {
534 cNode = calloc(1, sizeof(*cNode) + strlen(name));
535 strcpy(cNode->name, name);
536 banList_init(&cNode->banlist);
537 exemptList_init(&cNode->exemptlist);
538 modeList_init(&cNode->members);
539 mod_chanmode(NULL, cNode, argv, nn, MCP_FROM_SERVER);
540 dict_insert(channels, cNode->name, cNode);
541 cNode->timestamp = time_;
542 rel_age = 1;
543 } else if (cNode->timestamp > time_) {
544 wipeout_channel(cNode, time_, argv, nn);
545 rel_age = 1;
546 } else if (cNode->timestamp == time_) {
547 mod_chanmode(NULL, cNode, argv, nn, MCP_FROM_SERVER);
548 rel_age = 0;
549 } else {
550 rel_age = -1;
551 }
552
553 /* rel_age is the relative ages of our channel data versus what is
554 * in a BURST command. 1 means ours is younger, 0 means both are
555 * the same age, -1 means ours is older. */
556
557 /* if it's a new or updated channel, make callbacks */
558 if (rel_age > 0)
559 for (nn=0; nn<ncf_used; nn++)
560 ncf_list[nn](cNode, ncf_list_extra[nn]);
561
562 /* go through list of bans and add each one */
563 if (banlist && (rel_age >= 0)) {
564 for (nn=0; banlist[nn];) {
565 char *ban = banlist + nn;
566 struct banNode *bn;
567 while (banlist[nn] != ' ' && banlist[nn])
568 nn++;
569 while (banlist[nn] == ' ')
570 banlist[nn++] = 0;
571 bn = calloc(1, sizeof(*bn));
572 safestrncpy(bn->ban, ban, sizeof(bn->ban));
573 safestrncpy(bn->who, "<unknown>", sizeof(bn->who));
574 bn->set = now;
575 banList_append(&cNode->banlist, bn);
576 }
577 }
578
579 /* go through list of exempts and add each one */
580 if (exemptlist && (rel_age >= 0)) {
581 for (nn=0; exemptlist[nn];) {
582 char *exempt = exemptlist + nn;
583 struct exemptNode *en;
584 while (exemptlist[nn] != ' ' && exemptlist[nn])
585 nn++;
586 while (exemptlist[nn] == ' ')
587 exemptlist[nn++] = 0;
588 en = calloc(1, sizeof(*en));
589 safestrncpy(en->exempt, exempt, sizeof(en->exempt));
590 safestrncpy(en->who, "<unknown>", sizeof(en->who));
591 en->set = now;
592 exemptList_append(&cNode->exemptlist, en);
593 }
594 }
595
596 return cNode;
597 }
598
599 static del_channel_func_t *dcf_list;
600 static unsigned int dcf_size = 0, dcf_used = 0;
601
602 void
603 reg_del_channel_func(del_channel_func_t handler)
604 {
605 if (dcf_used == dcf_size) {
606 if (dcf_size) {
607 dcf_size <<= 1;
608 dcf_list = realloc(dcf_list, dcf_size*sizeof(dcf_list[0]));
609 } else {
610 dcf_size = 8;
611 dcf_list = malloc(dcf_size*sizeof(dcf_list[0]));
612 }
613 }
614 dcf_list[dcf_used++] = handler;
615 }
616
617 static void
618 DelChannel(struct chanNode *channel)
619 {
620 unsigned int n;
621
622 verify(channel);
623 dict_remove(channels, channel->name);
624
625 if (channel->members.used || channel->locks) {
626 log_module(MAIN_LOG, LOG_ERROR, "Warning: deleting channel %s with %d users and %d locks remaining.", channel->name, channel->members.used, channel->locks);
627 }
628
629 /* go through all channel members and delete them from the channel */
630 for (n=channel->members.used; n>0; )
631 DelChannelUser(channel->members.list[--n]->user, channel, NULL, 1);
632
633 /* delete all channel bans */
634 for (n=channel->banlist.used; n>0; )
635 free(channel->banlist.list[--n]);
636 channel->banlist.used = 0;
637
638 /* delete all channel exempts */
639 for (n=channel->exemptlist.used; n>0; )
640 free(channel->exemptlist.list[--n]);
641 channel->exemptlist.used = 0;
642
643 for (n=0; n<dcf_used; n++)
644 dcf_list[n](channel);
645
646 modeList_clean(&channel->members);
647 banList_clean(&channel->banlist);
648 exemptList_clean(&channel->exemptlist);
649 free(channel);
650 }
651
652 struct modeNode *
653 AddChannelUser(struct userNode *user, struct chanNode* channel)
654 {
655 struct modeNode *mNode;
656 unsigned int n;
657
658 mNode = GetUserMode(channel, user);
659 if (mNode)
660 return mNode;
661
662 mNode = malloc(sizeof(*mNode));
663
664 /* set up modeNode */
665 mNode->channel = channel;
666 mNode->user = user;
667 mNode->modes = 0;
668 mNode->oplevel = -1;
669 mNode->idle_since = now;
670
671 /* Add modeNode to channel and to user.
672 * We have to do this before calling join funcs in case the
673 * modeNode is manipulated (e.g. chanserv ops the user).
674 */
675 modeList_append(&channel->members, mNode);
676 modeList_append(&user->channels, mNode);
677
678 if (channel->members.used == 1
679 && !(channel->modes & MODE_REGISTERED)
680 && !(channel->modes & MODE_APASS)) {
681 mNode->modes |= MODE_CHANOP;
682 log_module(MAIN_LOG, LOG_DEBUG, "setting op");
683 }
684
685 if (IsLocal(user)) {
686 irc_join(user, channel);
687 }
688
689 for (n=0; (n<jf_used) && !user->dead; n++) {
690 /* Callbacks return true if they kick or kill the user,
691 * and we can continue without removing mNode. */
692 if (jf_list[n](mNode, jf_list_extra[n]))
693 return NULL;
694 }
695
696 return mNode;
697 }
698
699 static part_func_t *pf_list;
700 static unsigned int pf_size = 0, pf_used = 0;
701
702 void
703 reg_part_func(part_func_t handler)
704 {
705 if (pf_used == pf_size) {
706 if (pf_size) {
707 pf_size <<= 1;
708 pf_list = realloc(pf_list, pf_size*sizeof(part_func_t));
709 } else {
710 pf_size = 8;
711 pf_list = malloc(pf_size*sizeof(part_func_t));
712 }
713 }
714 pf_list[pf_used++] = handler;
715 }
716
717 void
718 unreg_part_func(part_func_t handler)
719 {
720 unsigned int i;
721 for (i=0; i<pf_used; i++)
722 if (pf_list[i] == handler)
723 break;
724 if (i == pf_used)
725 return;
726 memmove(pf_list+i, pf_list+i+1, (pf_used-i-1)*sizeof(pf_list[0]));
727 pf_used--;
728 }
729
730 void
731 LockChannel(struct chanNode* channel)
732 {
733 channel->locks++;
734 }
735
736 void
737 UnlockChannel(struct chanNode *channel)
738 {
739 assert(channel->locks > 0);
740 if (!--channel->locks && !channel->members.used)
741 DelChannel(channel);
742 }
743
744 void
745 DelChannelUser(struct userNode* user, struct chanNode* channel, const char *reason, int deleting)
746 {
747 struct modeNode* mNode;
748 unsigned int n;
749
750 if (IsLocal(user) && reason)
751 irc_part(user, channel, reason);
752
753 mNode = GetUserMode(channel, user);
754
755 /* Sometimes we get a PART when the user has been KICKed.
756 * In this case, we get no usermode, and should not try to free it.
757 */
758 if (!mNode)
759 return;
760
761 /* remove modeNode from channel and user */
762 modeList_remove(&channel->members, mNode);
763 modeList_remove(&user->channels, mNode);
764
765 /* make callbacks */
766 for (n=0; n<pf_used; n++)
767 pf_list[n](mNode, reason);
768
769 /* free memory */
770 free(mNode);
771
772 /* A single check for APASS only should be enough here */
773 if (!deleting && !channel->members.used && !channel->locks
774 && !(channel->modes & MODE_REGISTERED) && !(channel->modes & MODE_APASS))
775 DelChannel(channel);
776 }
777
778 static kick_func_t *kf_list;
779 static unsigned int kf_size = 0, kf_used = 0;
780
781 void
782 KickChannelUser(struct userNode* target, struct chanNode* channel, struct userNode *kicker, const char *why)
783 {
784 unsigned int n;
785
786 if (!target || !channel || IsService(target) || !GetUserMode(channel, target))
787 return;
788
789 /* This may break things, but lets see.. -Rubin */
790 for (n=0; n<kf_used; n++)
791 kf_list[n](kicker, target, channel);
792
793 /* don't remove them from the channel, since the server will send a PART */
794 irc_kick(kicker, target, channel, why);
795
796 if (IsLocal(target))
797 {
798 /* NULL reason because we don't want a PART message to be
799 sent by DelChannelUser. */
800 DelChannelUser(target, channel, NULL, 0);
801 }
802 }
803
804 void
805 reg_kick_func(kick_func_t handler)
806 {
807 if (kf_used == kf_size) {
808 if (kf_size) {
809 kf_size <<= 1;
810 kf_list = realloc(kf_list, kf_size*sizeof(kick_func_t));
811 } else {
812 kf_size = 8;
813 kf_list = malloc(kf_size*sizeof(kick_func_t));
814 }
815 }
816 kf_list[kf_used++] = handler;
817 }
818
819 void
820 ChannelUserKicked(struct userNode* kicker, struct userNode* victim, struct chanNode* channel)
821 {
822 unsigned int n;
823 struct modeNode *mn;
824
825 if (!victim || !channel || !GetUserMode(channel, victim))
826 return;
827
828 /* Update the kicker's idle time (kicker may be null if it was a server) */
829 if (kicker && (mn = GetUserMode(channel, kicker)))
830 mn->idle_since = now;
831
832 for (n=0; n<kf_used; n++)
833 kf_list[n](kicker, victim, channel);
834
835 DelChannelUser(victim, channel, 0, 0);
836
837 if (IsLocal(victim))
838 irc_part(victim, channel, NULL);
839 }
840
841 int ChannelBanExists(struct chanNode *channel, const char *ban)
842 {
843 unsigned int n;
844
845 for (n = 0; n < channel->banlist.used; n++)
846 if (match_ircglobs(channel->banlist.list[n]->ban, ban))
847 return 1;
848 return 0;
849 }
850
851 int ChannelExemptExists(struct chanNode *channel, const char *exempt)
852 {
853 unsigned int n;
854
855 for (n = 0; n < channel->exemptlist.used; n++)
856 if (match_ircglobs(channel->exemptlist.list[n]->exempt, exempt))
857 return 1;
858 return 0;
859 }
860
861 static topic_func_t *tf_list;
862 static void **tf_list_extra;
863 static unsigned int tf_size = 0, tf_used = 0;
864
865 void
866 reg_topic_func(topic_func_t handler, void *extra)
867 {
868 if (tf_used == tf_size) {
869 if (tf_size) {
870 tf_size <<= 1;
871 tf_list = realloc(tf_list, tf_size*sizeof(topic_func_t));
872 tf_list_extra = realloc(tf_list_extra, tf_size*sizeof(void*));
873 } else {
874 tf_size = 8;
875 tf_list = malloc(tf_size*sizeof(topic_func_t));
876 tf_list_extra = malloc(tf_size*sizeof(void*));
877 }
878 }
879 tf_list[tf_used] = handler;
880 tf_list_extra[tf_used++] = extra;
881 }
882
883 void
884 SetChannelTopic(struct chanNode *channel, struct userNode *service, struct userNode *user, const char *topic, int announce)
885 {
886 unsigned int n;
887 struct modeNode *mn;
888 char old_topic[TOPICLEN+1];
889
890 safestrncpy(old_topic, channel->topic, sizeof(old_topic));
891 safestrncpy(channel->topic, topic, sizeof(channel->topic));
892 channel->topic_time = now;
893
894 if (user) {
895 safestrncpy(channel->topic_nick, user->nick, sizeof(channel->topic_nick));
896
897 /* Update the setter's idle time */
898 if ((mn = GetUserMode(channel, user)))
899 mn->idle_since = now;
900 }
901
902 if (announce) {
903 /* We don't really care if a local user messes with the topic,
904 * so don't call the tf_list functions. */
905 irc_topic(service, user, channel, topic);
906 } else {
907 for (n=0; n<tf_used; n++)
908 /* A topic change handler can return non-zero to indicate
909 * that it has reverted the topic change, and that further
910 * hooks should not be called.
911 */
912 if (tf_list[n](user, channel, old_topic, tf_list_extra[n]))
913 break;
914 }
915 }
916
917 struct chanNode *
918 GetChannel(const char *name)
919 {
920 return dict_find(channels, name, NULL);
921 }
922
923 struct modeNode *
924 GetUserMode(struct chanNode *channel, struct userNode *user)
925 {
926 unsigned int n;
927 struct modeNode *mn = NULL;
928
929 verify(channel);
930 verify(channel->members.list);
931 verify(user);
932 verify(user->channels.list);
933 if (channel->members.used < user->channels.used) {
934 for (n=0; n<channel->members.used; n++) {
935 verify(channel->members.list[n]);
936 if (user == channel->members.list[n]->user) {
937 mn = channel->members.list[n];
938 break;
939 }
940 }
941 } else {
942 for (n=0; n<user->channels.used; n++) {
943 verify(user->channels.list[n]);
944 if (channel == user->channels.list[n]->channel) {
945 mn = user->channels.list[n];
946 break;
947 }
948 }
949 }
950 return mn;
951 }
952
953 struct userNode *IsInChannel(struct chanNode *channel, struct userNode *user)
954 {
955 unsigned int n;
956
957 verify(channel);
958 verify(channel->members.list);
959 verify(user);
960 verify(user->channels.list);
961 if (channel->members.used < user->channels.used) {
962 for (n=0; n<channel->members.used; n++) {
963 verify(channel->members.list[n]);
964 if (user == channel->members.list[n]->user) {
965 return(user);
966 }
967 }
968 } else {
969 for (n=0; n<user->channels.used; n++) {
970 verify(user->channels.list[n]);
971 if (channel == user->channels.list[n]->channel) {
972 return(user);
973 }
974 }
975 }
976 return NULL;
977 }
978
979 DEFINE_LIST(userList, struct userNode*)
980 DEFINE_LIST(modeList, struct modeNode*)
981 DEFINE_LIST(banList, struct banNode*)
982 DEFINE_LIST(exemptList, struct exemptNode*)
983 DEFINE_LIST(channelList, struct chanNode*)
984 DEFINE_LIST(serverList, struct server*)
985
986 static void
987 hash_cleanup(void)
988 {
989 dict_iterator_t it, next;
990
991 DelServer(self, 0, NULL);
992 for (it = dict_first(channels); it; it = next) {
993 next = iter_next(it);
994 DelChannel(iter_data(it));
995 }
996 dict_delete(channels);
997 dict_delete(clients);
998 dict_delete(servers);
999 userList_clean(&curr_opers);
1000
1001 free(slf_list);
1002 free(slf_list_extra);
1003 free(nuf_list);
1004 free(nuf_list_extra);
1005 free(ncf2_list);
1006 free(ncf2_list_extra);
1007 free(duf_list);
1008 free(duf_list_extra);
1009 free(ncf_list);
1010 free(ncf_list_extra);
1011 free(jf_list);
1012 free(jf_list_extra);
1013 free(dcf_list);
1014 free(pf_list);
1015 free(kf_list);
1016 free(tf_list);
1017 free(tf_list_extra);
1018 }