1 /* hash.c - IRC network state database
2 * Copyright 2000-2004 srvx Development Team
4 * This file is part of x3.
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.
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.
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.
26 #if defined(HAVE_LIBGEOIP)&&defined(HAVE_GEOIP_H)&&defined(HAVE_GEOIPCITY_H)
28 #include <GeoIPCity.h>
38 unsigned int max_clients
, invis_clients
;
39 time_t max_clients_time
;
40 struct userList curr_opers
;
42 static void hash_cleanup(void);
44 void init_structs(void)
46 channels
= dict_new();
49 userList_init(&curr_opers
);
50 reg_exit_func(hash_cleanup
);
53 int userList_contains(struct userList
*list
, struct userNode
*user
)
57 for (ii
= 0; ii
< list
->used
; ++ii
) {
58 if (user
== list
->list
[ii
]) {
65 server_link_func_t
*slf_list
;
66 void **slf_list_extra
;
67 unsigned int slf_size
= 0, slf_used
= 0;
70 reg_server_link_func(server_link_func_t handler
, void *extra
)
72 if (slf_used
== slf_size
) {
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*));
79 slf_list
= malloc(slf_size
*sizeof(server_link_func_t
));
80 slf_list_extra
= malloc(slf_size
*sizeof(void*));
83 slf_list
[slf_used
] = handler
;
84 slf_list_extra
[slf_used
++] = extra
;
88 call_server_link_funcs(struct server
*server
)
92 for (i
= 0; i
< slf_used
; ++i
)
94 slf_list
[i
](server
, slf_list_extra
[i
]);
99 GetServerH(const char *name
)
101 return dict_find(servers
, name
, NULL
);
104 new_user_func_t
*nuf_list
;
105 void **nuf_list_extra
;
106 unsigned int nuf_size
= 0, nuf_used
= 0;
109 reg_new_user_func(new_user_func_t handler
, void *extra
)
111 if (nuf_used
== nuf_size
) {
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*));
118 nuf_list
= malloc(nuf_size
*sizeof(new_user_func_t
));
119 nuf_list_extra
= malloc(nuf_size
*sizeof(void*));
122 nuf_list
[nuf_used
] = handler
;
123 nuf_list_extra
[nuf_used
++] = extra
;
127 call_new_user_funcs(struct userNode
* user
)
131 for (i
= 0; i
< nuf_used
&& !(user
->dead
); ++i
)
133 nuf_list
[i
](user
, nuf_list_extra
[i
]);
137 static nick_change_func_t
*ncf2_list
;
138 static void **ncf2_list_extra
;
139 static unsigned int ncf2_size
= 0, ncf2_used
= 0;
142 reg_nick_change_func(nick_change_func_t handler
, void *extra
)
144 if (ncf2_used
== ncf2_size
) {
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*));
151 ncf2_list
= malloc(ncf2_size
*sizeof(nick_change_func_t
));
152 ncf2_list_extra
= malloc(ncf2_size
*sizeof(void*));
155 ncf2_list
[ncf2_used
] = handler
;
156 ncf2_list_extra
[ncf2_used
++] = extra
;
160 del_user_func_t
*duf_list
;
161 void **duf_list_extra
;
162 unsigned int duf_size
= 0, duf_used
= 0;
165 reg_del_user_func(del_user_func_t handler
, void *extra
)
167 if (duf_used
== duf_size
) {
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*));
174 duf_list
= malloc(duf_size
*sizeof(del_user_func_t
));
175 duf_list_extra
= malloc(duf_size
*sizeof(void*));
178 duf_list
[duf_used
] = handler
;
179 duf_list_extra
[duf_used
++] = extra
;
183 call_del_user_funcs(struct userNode
*user
, struct userNode
*killer
, const char *why
)
187 for (i
= 0; i
< duf_used
; ++i
)
189 duf_list
[i
](user
, killer
, why
, duf_list_extra
[i
]);
194 unreg_del_user_func(del_user_func_t handler
, void *extra
)
197 for (i
=0; i
<duf_used
; i
++) {
198 if (duf_list
[i
] == handler
&& duf_list_extra
[i
] == extra
) break;
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]));
206 /* reintroduces a user after it has been killed. */
208 ReintroduceUser(struct userNode
*user
)
210 struct mod_chanmode change
;
214 mod_chanmode_init(&change
);
216 for (n
= 0; n
< user
->channels
.used
; n
++) {
217 struct modeNode
*mn
= user
->channels
.list
[n
];
218 irc_join(user
, mn
->channel
);
220 change
.args
[0].mode
= mn
->modes
;
221 change
.args
[0].u
.member
= mn
;
222 mod_chanmode_announce(user
, mn
->channel
, &change
);
228 NickChange(struct userNode
* user
, const char *new_nick
, int no_announce
)
233 /* don't do anything if there's no change */
234 old_nick
= user
->nick
;
235 if (!strncmp(new_nick
, old_nick
, NICKLEN
))
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
);
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
);
251 /* Make callbacks for nick changes. Do this with new nick in
252 * place because that is slightly more useful.
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
);
263 SVSNickChange(struct userNode
* user
, const char *new_nick
)
268 /* don't do anything if there's no change */
269 old_nick
= user
->nick
;
270 if (!strncmp(new_nick
, old_nick
, NICKLEN
))
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
);
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
);
286 /* Make callbacks for nick changes. Do this with new nick in
287 * place because that is slightly more useful.
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
;
297 GetUserH(const char *nick
)
299 return dict_find(clients
, nick
, NULL
);
302 static account_func_t account_func
;
305 reg_account_func(account_func_t handler
)
308 log_module(MAIN_LOG
, LOG_WARNING
, "Reregistering ACCOUNT handler.");
310 account_func
= handler
;
314 call_account_func(struct userNode
*user
, const char *stamp
)
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
321 P10 Protocol violation if (user->modes & FLAGS_STAMPED) here.
324 account_func(user
, stamp
);
326 #ifdef WITH_PROTOCOL_P10
327 /* Mark the user so we don't stamp it again. */
328 user
->modes
|= FLAGS_STAMPED
;
333 StampUser(struct userNode
*user
, const char *stamp
, time_t timestamp
)
335 #ifdef WITH_PROTOCOL_P10
336 /* The P10 protocol says we can't stamp users who already
342 irc_account(user
, stamp
, timestamp
);
343 user
->modes
|= FLAGS_STAMPED
;
347 assign_fakehost(struct userNode
*user
, const char *host
, int announce
)
349 safestrncpy(user
->fakehost
, host
, sizeof(user
->fakehost
));
351 irc_fakehost(user
, host
);
355 set_geoip_info(struct userNode
*user
)
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)
362 const char *geoip_data_file
= NULL
;
363 const char *geoip_city_file
= NULL
;
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
);
368 if ((!geoip_data_file
&& !geoip_city_file
))
369 return; /* Admin doesnt want to use geoip functions */
371 if (geoip_data_file
&& !gi
)
372 gi
= GeoIP_open(geoip_data_file
, GEOIP_MEMORY_CACHE
| GEOIP_CHECK_CACHE
);
374 if (geoip_city_file
&& !cgi
)
375 cgi
= GeoIP_open(geoip_city_file
, GEOIP_MEMORY_CACHE
| GEOIP_CHECK_CACHE
);
378 gir
= GeoIP_record_by_name(cgi
, user
->hostname
);
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
: "");
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;
391 GeoIPRecord_delete(gir
);
396 const char *country
= GeoIP_country_name_by_name(gi
, user
->hostname
);
397 user
->country_name
= strdup(country
? country
: "");
405 static new_channel_func_t
*ncf_list
;
406 static void **ncf_list_extra
;
407 static unsigned int ncf_size
= 0, ncf_used
= 0;
410 reg_new_channel_func(new_channel_func_t handler
, void *extra
)
412 if (ncf_used
== ncf_size
) {
415 ncf_list
= realloc(ncf_list
, ncf_size
*sizeof(ncf_list
[0]));
416 ncf_list_extra
= realloc(ncf_list_extra
, ncf_size
*sizeof(void*));
419 ncf_list
= malloc(ncf_size
*sizeof(ncf_list
[0]));
420 ncf_list_extra
= malloc(ncf_size
*sizeof(void*));
423 ncf_list
[ncf_used
] = handler
;
424 ncf_list_extra
[ncf_used
++] = extra
;
427 static join_func_t
*jf_list
;
428 static void **jf_list_extra
;
429 static unsigned int jf_size
= 0, jf_used
= 0;
432 reg_join_func(join_func_t handler
, void *extra
)
434 if (jf_used
== jf_size
) {
437 jf_list
= realloc(jf_list
, jf_size
*sizeof(join_func_t
));
438 jf_list_extra
= realloc(jf_list_extra
, jf_size
*sizeof(void*));
441 jf_list
= malloc(jf_size
*sizeof(join_func_t
));
442 jf_list_extra
= malloc(jf_size
*sizeof(void*));
445 jf_list
[jf_used
] = handler
;
446 jf_list_extra
[jf_used
++] = extra
;
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
;
461 cNode
->topic
[0] = '\0';
462 cNode
->topic_nick
[0] = '\0';
463 cNode
->topic_time
= 0;
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
);
472 mod_chanmode(NULL
, cNode
, modes
, modec
, 0);
473 cNode
->timestamp
= new_time
;
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;
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;
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
))
493 struct mod_chanmode
*change
;
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
;
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
);
518 AddChannel(const char *name
, time_t time_
, const char *modes
, char *banlist
, char *exemptlist
)
520 struct chanNode
*cNode
;
521 char new_modes
[MAXLEN
], *argv
[MAXNUMPARAMS
];
524 if (!IsChannelName(name
)) {
525 log_module(MAIN_LOG
, LOG_ERROR
, "Somebody asked to add channel '%s', which isn't a channel name!", name
);
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_
;
543 } else if (cNode
->timestamp
> time_
) {
544 wipeout_channel(cNode
, time_
, argv
, nn
);
546 } else if (cNode
->timestamp
== time_
) {
547 mod_chanmode(NULL
, cNode
, argv
, nn
, MCP_FROM_SERVER
);
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. */
557 /* if it's a new or updated channel, make callbacks */
559 for (nn
=0; nn
<ncf_used
; nn
++)
560 ncf_list
[nn
](cNode
, ncf_list_extra
[nn
]);
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
;
567 while (banlist
[nn
] != ' ' && banlist
[nn
])
569 while (banlist
[nn
] == ' ')
571 bn
= calloc(1, sizeof(*bn
));
572 safestrncpy(bn
->ban
, ban
, sizeof(bn
->ban
));
573 safestrncpy(bn
->who
, "<unknown>", sizeof(bn
->who
));
575 banList_append(&cNode
->banlist
, bn
);
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
])
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
));
592 exemptList_append(&cNode
->exemptlist
, en
);
599 static del_channel_func_t
*dcf_list
;
600 static unsigned int dcf_size
= 0, dcf_used
= 0;
603 reg_del_channel_func(del_channel_func_t handler
)
605 if (dcf_used
== dcf_size
) {
608 dcf_list
= realloc(dcf_list
, dcf_size
*sizeof(dcf_list
[0]));
611 dcf_list
= malloc(dcf_size
*sizeof(dcf_list
[0]));
614 dcf_list
[dcf_used
++] = handler
;
618 DelChannel(struct chanNode
*channel
)
623 dict_remove(channels
, channel
->name
);
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
);
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);
633 /* delete all channel bans */
634 for (n
=channel
->banlist
.used
; n
>0; )
635 free(channel
->banlist
.list
[--n
]);
636 channel
->banlist
.used
= 0;
638 /* delete all channel exempts */
639 for (n
=channel
->exemptlist
.used
; n
>0; )
640 free(channel
->exemptlist
.list
[--n
]);
641 channel
->exemptlist
.used
= 0;
643 for (n
=0; n
<dcf_used
; n
++)
644 dcf_list
[n
](channel
);
646 modeList_clean(&channel
->members
);
647 banList_clean(&channel
->banlist
);
648 exemptList_clean(&channel
->exemptlist
);
653 AddChannelUser(struct userNode
*user
, struct chanNode
* channel
)
655 struct modeNode
*mNode
;
658 mNode
= GetUserMode(channel
, user
);
662 mNode
= malloc(sizeof(*mNode
));
664 /* set up modeNode */
665 mNode
->channel
= channel
;
669 mNode
->idle_since
= now
;
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).
675 modeList_append(&channel
->members
, mNode
);
676 modeList_append(&user
->channels
, mNode
);
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");
686 irc_join(user
, channel
);
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
]))
699 static part_func_t
*pf_list
;
700 static unsigned int pf_size
= 0, pf_used
= 0;
703 reg_part_func(part_func_t handler
)
705 if (pf_used
== pf_size
) {
708 pf_list
= realloc(pf_list
, pf_size
*sizeof(part_func_t
));
711 pf_list
= malloc(pf_size
*sizeof(part_func_t
));
714 pf_list
[pf_used
++] = handler
;
718 unreg_part_func(part_func_t handler
)
721 for (i
=0; i
<pf_used
; i
++)
722 if (pf_list
[i
] == handler
)
726 memmove(pf_list
+i
, pf_list
+i
+1, (pf_used
-i
-1)*sizeof(pf_list
[0]));
731 LockChannel(struct chanNode
* channel
)
737 UnlockChannel(struct chanNode
*channel
)
739 assert(channel
->locks
> 0);
740 if (!--channel
->locks
&& !channel
->members
.used
)
745 DelChannelUser(struct userNode
* user
, struct chanNode
* channel
, const char *reason
, int deleting
)
747 struct modeNode
* mNode
;
750 if (IsLocal(user
) && reason
)
751 irc_part(user
, channel
, reason
);
753 mNode
= GetUserMode(channel
, user
);
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.
761 /* remove modeNode from channel and user */
762 modeList_remove(&channel
->members
, mNode
);
763 modeList_remove(&user
->channels
, mNode
);
766 for (n
=0; n
<pf_used
; n
++)
767 pf_list
[n
](mNode
, reason
);
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
))
778 static kick_func_t
*kf_list
;
779 static unsigned int kf_size
= 0, kf_used
= 0;
782 KickChannelUser(struct userNode
* target
, struct chanNode
* channel
, struct userNode
*kicker
, const char *why
)
786 if (!target
|| !channel
|| IsService(target
) || !GetUserMode(channel
, target
))
789 /* This may break things, but lets see.. -Rubin */
790 for (n
=0; n
<kf_used
; n
++)
791 kf_list
[n
](kicker
, target
, channel
);
793 /* don't remove them from the channel, since the server will send a PART */
794 irc_kick(kicker
, target
, channel
, why
);
798 /* NULL reason because we don't want a PART message to be
799 sent by DelChannelUser. */
800 DelChannelUser(target
, channel
, NULL
, 0);
805 reg_kick_func(kick_func_t handler
)
807 if (kf_used
== kf_size
) {
810 kf_list
= realloc(kf_list
, kf_size
*sizeof(kick_func_t
));
813 kf_list
= malloc(kf_size
*sizeof(kick_func_t
));
816 kf_list
[kf_used
++] = handler
;
820 ChannelUserKicked(struct userNode
* kicker
, struct userNode
* victim
, struct chanNode
* channel
)
825 if (!victim
|| !channel
|| !GetUserMode(channel
, victim
))
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
;
832 for (n
=0; n
<kf_used
; n
++)
833 kf_list
[n
](kicker
, victim
, channel
);
835 DelChannelUser(victim
, channel
, 0, 0);
838 irc_part(victim
, channel
, NULL
);
841 int ChannelBanExists(struct chanNode
*channel
, const char *ban
)
845 for (n
= 0; n
< channel
->banlist
.used
; n
++)
846 if (match_ircglobs(channel
->banlist
.list
[n
]->ban
, ban
))
851 int ChannelExemptExists(struct chanNode
*channel
, const char *exempt
)
855 for (n
= 0; n
< channel
->exemptlist
.used
; n
++)
856 if (match_ircglobs(channel
->exemptlist
.list
[n
]->exempt
, exempt
))
861 static topic_func_t
*tf_list
;
862 static void **tf_list_extra
;
863 static unsigned int tf_size
= 0, tf_used
= 0;
866 reg_topic_func(topic_func_t handler
, void *extra
)
868 if (tf_used
== tf_size
) {
871 tf_list
= realloc(tf_list
, tf_size
*sizeof(topic_func_t
));
872 tf_list_extra
= realloc(tf_list_extra
, tf_size
*sizeof(void*));
875 tf_list
= malloc(tf_size
*sizeof(topic_func_t
));
876 tf_list_extra
= malloc(tf_size
*sizeof(void*));
879 tf_list
[tf_used
] = handler
;
880 tf_list_extra
[tf_used
++] = extra
;
884 SetChannelTopic(struct chanNode
*channel
, struct userNode
*service
, struct userNode
*user
, const char *topic
, int announce
)
888 char old_topic
[TOPICLEN
+1];
890 safestrncpy(old_topic
, channel
->topic
, sizeof(old_topic
));
891 safestrncpy(channel
->topic
, topic
, sizeof(channel
->topic
));
892 channel
->topic_time
= now
;
895 safestrncpy(channel
->topic_nick
, user
->nick
, sizeof(channel
->topic_nick
));
897 /* Update the setter's idle time */
898 if ((mn
= GetUserMode(channel
, user
)))
899 mn
->idle_since
= now
;
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
);
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.
912 if (tf_list
[n
](user
, channel
, old_topic
, tf_list_extra
[n
]))
918 GetChannel(const char *name
)
920 return dict_find(channels
, name
, NULL
);
924 GetUserMode(struct chanNode
*channel
, struct userNode
*user
)
927 struct modeNode
*mn
= NULL
;
930 verify(channel
->members
.list
);
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
];
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
];
953 struct userNode
*IsInChannel(struct chanNode
*channel
, struct userNode
*user
)
958 verify(channel
->members
.list
);
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
) {
969 for (n
=0; n
<user
->channels
.used
; n
++) {
970 verify(user
->channels
.list
[n
]);
971 if (channel
== user
->channels
.list
[n
]->channel
) {
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
*)
989 dict_iterator_t it
, next
;
991 DelServer(self
, 0, NULL
);
992 for (it
= dict_first(channels
); it
; it
= next
) {
993 next
= iter_next(it
);
994 DelChannel(iter_data(it
));
996 dict_delete(channels
);
997 dict_delete(clients
);
998 dict_delete(servers
);
999 userList_clean(&curr_opers
);
1002 free(slf_list_extra
);
1004 free(nuf_list_extra
);
1006 free(ncf2_list_extra
);
1008 free(duf_list_extra
);
1010 free(ncf_list_extra
);
1012 free(jf_list_extra
);
1017 free(tf_list_extra
);