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
;
41 unsigned int count_opers
;
43 static void hash_cleanup(void *extra
);
45 void init_structs(void)
47 channels
= dict_new();
50 userList_init(&curr_opers
);
52 reg_exit_func(hash_cleanup
, NULL
);
55 int userList_contains(struct userList
*list
, struct userNode
*user
)
59 for (ii
= 0; ii
< list
->used
; ++ii
) {
60 if (user
== list
->list
[ii
]) {
67 server_link_func_t
*slf_list
;
68 void **slf_list_extra
;
69 unsigned int slf_size
= 0, slf_used
= 0;
72 reg_server_link_func(server_link_func_t handler
, void *extra
)
74 if (slf_used
== slf_size
) {
77 slf_list
= realloc(slf_list
, slf_size
*sizeof(server_link_func_t
));
78 slf_list_extra
= realloc(slf_list_extra
, slf_size
*sizeof(void*));
81 slf_list
= malloc(slf_size
*sizeof(server_link_func_t
));
82 slf_list_extra
= malloc(slf_size
*sizeof(void*));
85 slf_list
[slf_used
] = handler
;
86 slf_list_extra
[slf_used
++] = extra
;
90 call_server_link_funcs(struct server
*server
)
94 for (i
= 0; i
< slf_used
; ++i
)
96 slf_list
[i
](server
, slf_list_extra
[i
]);
101 GetServerH(const char *name
)
103 return dict_find(servers
, name
, NULL
);
106 sasl_input_func_t
*sif_list
;
107 void **sif_list_extra
;
108 unsigned int sif_size
= 0, sif_used
= 0;
111 reg_sasl_input_func(sasl_input_func_t handler
, void *extra
)
113 if (sif_used
== sif_size
) {
116 sif_list
= realloc(sif_list
, sif_size
*sizeof(new_user_func_t
));
117 sif_list_extra
= realloc(sif_list_extra
, sif_size
*sizeof(void*));
120 sif_list
= malloc(sif_size
*sizeof(new_user_func_t
));
121 sif_list_extra
= malloc(sif_size
*sizeof(void*));
124 sif_list
[sif_used
] = handler
;
125 sif_list_extra
[sif_used
++] = extra
;
129 call_sasl_input_func(struct server
* source
,const char *identifier
, const char *subcmd
, const char *data
, const char *ext
)
133 for (i
= 0; i
< sif_used
; ++i
)
135 sif_list
[i
](source
, identifier
, subcmd
, data
, ext
, sif_list_extra
[i
]);
140 unreg_sasl_input_func(sasl_input_func_t handler
, void *extra
)
143 for (i
=0; i
<sif_used
; i
++) {
144 if (sif_list
[i
] == handler
&& sif_list_extra
[i
] == extra
) break;
146 if (i
== sif_used
) return;
147 memmove(sif_list
+i
, sif_list
+i
+1, (sif_used
-i
-1)*sizeof(sif_list
[0]));
148 memmove(sif_list_extra
+i
, sif_list_extra
+i
+1, (sif_used
-i
-1)*sizeof(sif_list_extra
[0]));
152 new_user_func_t
*nuf_list
;
153 void **nuf_list_extra
;
154 unsigned int nuf_size
= 0, nuf_used
= 0;
157 reg_new_user_func(new_user_func_t handler
, void *extra
)
159 if (nuf_used
== nuf_size
) {
162 nuf_list
= realloc(nuf_list
, nuf_size
*sizeof(new_user_func_t
));
163 nuf_list_extra
= realloc(nuf_list_extra
, nuf_size
*sizeof(void*));
166 nuf_list
= malloc(nuf_size
*sizeof(new_user_func_t
));
167 nuf_list_extra
= malloc(nuf_size
*sizeof(void*));
170 nuf_list
[nuf_used
] = handler
;
171 nuf_list_extra
[nuf_used
++] = extra
;
175 call_new_user_funcs(struct userNode
* user
)
179 for (i
= 0; i
< nuf_used
&& !(user
->dead
); ++i
)
181 nuf_list
[i
](user
, nuf_list_extra
[i
]);
185 static nick_change_func_t
*ncf2_list
;
186 static void **ncf2_list_extra
;
187 static unsigned int ncf2_size
= 0, ncf2_used
= 0;
190 reg_nick_change_func(nick_change_func_t handler
, void *extra
)
192 if (ncf2_used
== ncf2_size
) {
195 ncf2_list
= realloc(ncf2_list
, ncf2_size
*sizeof(nick_change_func_t
));
196 ncf2_list_extra
= realloc(ncf2_list_extra
, ncf2_size
*sizeof(void*));
199 ncf2_list
= malloc(ncf2_size
*sizeof(nick_change_func_t
));
200 ncf2_list_extra
= malloc(ncf2_size
*sizeof(void*));
203 ncf2_list
[ncf2_used
] = handler
;
204 ncf2_list_extra
[ncf2_used
++] = extra
;
208 del_user_func_t
*duf_list
;
209 void **duf_list_extra
;
210 unsigned int duf_size
= 0, duf_used
= 0;
213 reg_del_user_func(del_user_func_t handler
, void *extra
)
215 if (duf_used
== duf_size
) {
218 duf_list
= realloc(duf_list
, duf_size
*sizeof(del_user_func_t
));
219 duf_list_extra
= realloc(duf_list_extra
, duf_size
*sizeof(void*));
222 duf_list
= malloc(duf_size
*sizeof(del_user_func_t
));
223 duf_list_extra
= malloc(duf_size
*sizeof(void*));
226 duf_list
[duf_used
] = handler
;
227 duf_list_extra
[duf_used
++] = extra
;
231 call_del_user_funcs(struct userNode
*user
, struct userNode
*killer
, const char *why
)
235 for (i
= 0; i
< duf_used
; ++i
)
237 duf_list
[i
](user
, killer
, why
, duf_list_extra
[i
]);
242 unreg_del_user_func(del_user_func_t handler
, void *extra
)
245 for (i
=0; i
<duf_used
; i
++) {
246 if (duf_list
[i
] == handler
&& duf_list_extra
[i
] == extra
) break;
248 if (i
== duf_used
) return;
249 memmove(duf_list
+i
, duf_list
+i
+1, (duf_used
-i
-1)*sizeof(duf_list
[0]));
250 memmove(duf_list_extra
+i
, duf_list_extra
+i
+1, (duf_used
-i
-1)*sizeof(duf_list_extra
[0]));
254 /* reintroduces a user after it has been killed. */
256 ReintroduceUser(struct userNode
*user
)
258 struct mod_chanmode change
;
262 mod_chanmode_init(&change
);
264 for (n
= 0; n
< user
->channels
.used
; n
++) {
265 struct modeNode
*mn
= user
->channels
.list
[n
];
266 irc_join(user
, mn
->channel
);
268 change
.args
[0].mode
= mn
->modes
;
269 change
.args
[0].u
.member
= mn
;
270 mod_chanmode_announce(user
, mn
->channel
, &change
);
276 NickChange(struct userNode
* user
, const char *new_nick
, int no_announce
)
281 /* don't do anything if there's no change */
282 old_nick
= user
->nick
;
283 if (!strncmp(new_nick
, old_nick
, NICKLEN
))
286 /* remove old entry from clients dictionary */
287 dict_remove(clients
, old_nick
);
288 #if !defined(WITH_PROTOCOL_P10)
289 /* Remove from uplink's clients dict */
290 dict_remove(user
->uplink
->users
, old_nick
);
293 user
->nick
= strdup(new_nick
);
294 dict_insert(clients
, user
->nick
, user
);
295 #if !defined(WITH_PROTOCOL_P10)
296 dict_insert(user
->uplink
->users
, user
->nick
, user
);
299 /* Make callbacks for nick changes. Do this with new nick in
300 * place because that is slightly more useful.
302 for (nn
=0; (nn
<ncf2_used
) && !user
->dead
; nn
++)
303 ncf2_list
[nn
](user
, old_nick
, ncf2_list_extra
[nn
]);
304 user
->timestamp
= now
;
305 if (IsLocal(user
) && !no_announce
)
306 irc_nick(user
, old_nick
);
311 SVSNickChange(struct userNode
* user
, const char *new_nick
)
316 /* don't do anything if there's no change */
317 old_nick
= user
->nick
;
318 if (!strncmp(new_nick
, old_nick
, NICKLEN
))
321 /* remove old entry from clients dictionary */
322 dict_remove(clients
, old_nick
);
323 #if !defined(WITH_PROTOCOL_P10)
324 /* Remove from uplink's clients dict */
325 dict_remove(user
->uplink
->users
, old_nick
);
328 user
->nick
= strdup(new_nick
);
329 dict_insert(clients
, user
->nick
, user
);
330 #if !defined(WITH_PROTOCOL_P10)
331 dict_insert(user
->uplink
->users
, user
->nick
, user
);
334 /* Make callbacks for nick changes. Do this with new nick in
335 * place because that is slightly more useful.
337 for (nn
=0; (nn
<ncf2_used
) && !user
->dead
; nn
++)
338 ncf2_list
[nn
](user
, old_nick
, ncf2_list_extra
[nn
]);
339 user
->timestamp
= now
;
345 GetUserH(const char *nick
)
347 return dict_find(clients
, nick
, NULL
);
350 static account_func_t account_func
;
353 reg_account_func(account_func_t handler
)
356 log_module(MAIN_LOG
, LOG_WARNING
, "Reregistering ACCOUNT handler.");
358 account_func
= handler
;
362 call_account_func(struct userNode
*user
, const char *stamp
)
364 /* We've received an account stamp for a user; notify
365 NickServ, which registers the sole account_func
366 right now. TODO: This is a bug. This needs to register
367 a proper list not just kill with each call!! -Rubin
369 P10 Protocol violation if (user->modes & FLAGS_STAMPED) here.
372 account_func(user
, stamp
);
374 #ifdef WITH_PROTOCOL_P10
375 /* Mark the user so we don't stamp it again. */
376 user
->modes
|= FLAGS_STAMPED
;
381 StampUser(struct userNode
*user
, const char *stamp
, time_t timestamp
)
383 #ifdef WITH_PROTOCOL_P10
384 /* The P10 protocol says we can't stamp users who already
390 irc_account(user
, stamp
, timestamp
);
391 user
->modes
|= FLAGS_STAMPED
;
395 assign_fakehost(struct userNode
*user
, const char *host
, int announce
)
397 safestrncpy(user
->fakehost
, host
, sizeof(user
->fakehost
));
399 irc_fakehost(user
, host
);
403 set_geoip_info(struct userNode
*user
)
407 /* Need the libs and the headers if this is going to compile properly */
408 #if defined(HAVE_LIBGEOIP)&&defined(HAVE_GEOIP_H)&&defined(HAVE_GEOIPCITY_H)
410 const char *geoip_data_file
= NULL
;
411 const char *geoip_city_file
= NULL
;
413 geoip_data_file
= conf_get_data("services/opserv/geoip_data_file", RECDB_QSTRING
);
414 geoip_city_file
= conf_get_data("services/opserv/geoip_city_data_file", RECDB_QSTRING
);
416 if ((!geoip_data_file
&& !geoip_city_file
))
417 return; /* Admin doesnt want to use geoip functions */
419 if (geoip_data_file
&& !gi
)
420 gi
= GeoIP_open(geoip_data_file
, GEOIP_MEMORY_CACHE
| GEOIP_CHECK_CACHE
);
422 if (geoip_city_file
&& !cgi
)
423 cgi
= GeoIP_open(geoip_city_file
, GEOIP_MEMORY_CACHE
| GEOIP_CHECK_CACHE
);
426 gir
= GeoIP_record_by_name(cgi
, user
->hostname
);
428 user
->country_name
= strdup(gir
->country_name
? gir
->country_name
: "");
429 user
->country_code
= strdup(gir
->country_code
? gir
->country_code
: "");
430 user
->city
= strdup(gir
->city
? gir
->city
: "");
431 user
->region
= strdup(gir
->region
? gir
->region
: "");
432 user
->postal_code
= strdup(gir
->postal_code
? gir
->postal_code
: "");
434 user
->latitude
= gir
->latitude
? gir
->latitude
: 0;
435 user
->longitude
= gir
->longitude
? gir
->longitude
: 0;
436 user
->dma_code
= gir
->dma_code
? gir
->dma_code
: 0;
437 user
->area_code
= gir
->area_code
? gir
->area_code
: 0;
439 GeoIPRecord_delete(gir
);
444 const char *country
= GeoIP_country_name_by_name(gi
, user
->hostname
);
445 user
->country_name
= strdup(country
? country
: "");
453 static new_channel_func_t
*ncf_list
;
454 static void **ncf_list_extra
;
455 static unsigned int ncf_size
= 0, ncf_used
= 0;
458 reg_new_channel_func(new_channel_func_t handler
, void *extra
)
460 if (ncf_used
== ncf_size
) {
463 ncf_list
= realloc(ncf_list
, ncf_size
*sizeof(ncf_list
[0]));
464 ncf_list_extra
= realloc(ncf_list_extra
, ncf_size
*sizeof(void*));
467 ncf_list
= malloc(ncf_size
*sizeof(ncf_list
[0]));
468 ncf_list_extra
= malloc(ncf_size
*sizeof(void*));
471 ncf_list
[ncf_used
] = handler
;
472 ncf_list_extra
[ncf_used
++] = extra
;
475 static join_func_t
*jf_list
;
476 static void **jf_list_extra
;
477 static unsigned int jf_size
= 0, jf_used
= 0;
480 reg_join_func(join_func_t handler
, void *extra
)
482 if (jf_used
== jf_size
) {
485 jf_list
= realloc(jf_list
, jf_size
*sizeof(join_func_t
));
486 jf_list_extra
= realloc(jf_list_extra
, jf_size
*sizeof(void*));
489 jf_list
= malloc(jf_size
*sizeof(join_func_t
));
490 jf_list_extra
= malloc(jf_size
*sizeof(void*));
493 jf_list
[jf_used
] = handler
;
494 jf_list_extra
[jf_used
++] = extra
;
500 wipeout_channel(struct chanNode
*cNode
, time_t new_time
, char **modes
, unsigned int modec
) {
501 unsigned int orig_limit
;
502 chan_mode_t orig_modes
;
503 char orig_key
[KEYLEN
+1];
504 char orig_apass
[KEYLEN
+1];
505 char orig_upass
[KEYLEN
+1];
506 unsigned int nn
, argc
;
509 cNode
->topic
[0] = '\0';
510 cNode
->topic_nick
[0] = '\0';
511 cNode
->topic_time
= 0;
513 /* remember the old modes, and update them with the new */
514 orig_modes
= cNode
->modes
;
515 orig_limit
= cNode
->limit
;
516 strcpy(orig_key
, cNode
->key
);
517 strcpy(orig_upass
, cNode
->upass
);
518 strcpy(orig_apass
, cNode
->apass
);
520 mod_chanmode(NULL
, cNode
, modes
, modec
, 0);
521 cNode
->timestamp
= new_time
;
523 /* remove our old ban list, replace it with the new one */
524 for (nn
=0; nn
<cNode
->banlist
.used
; nn
++)
525 free(cNode
->banlist
.list
[nn
]);
526 cNode
->banlist
.used
= 0;
528 /* remove our old exe,[t list, replace it with the new one */
529 for (nn
=0; nn
<cNode
->exemptlist
.used
; nn
++)
530 free(cNode
->exemptlist
.list
[nn
]);
531 cNode
->exemptlist
.used
= 0;
533 /* deop anybody in the channel now, but count services to reop */
534 for (nn
=argc
=0; nn
<cNode
->members
.used
; nn
++) {
535 struct modeNode
*mn
= cNode
->members
.list
[nn
];
536 if ((mn
->modes
& MODE_CHANOP
) && IsService(mn
->user
) && IsLocal(mn
->user
))
541 struct mod_chanmode
*change
;
543 change
= mod_chanmode_alloc(argc
);
544 change
->modes_clear
= 0;
545 change
->modes_set
= orig_modes
;
546 change
->new_limit
= orig_limit
;
547 strcpy(change
->new_key
, orig_key
);
548 strcpy(change
->new_upass
, orig_upass
);
549 strcpy(change
->new_apass
, orig_apass
);
550 for (nn
= argc
= 0; nn
< cNode
->members
.used
; ++nn
) {
551 struct modeNode
*mn
= cNode
->members
.list
[nn
];
552 if ((mn
->modes
& MODE_CHANOP
) && IsService(mn
->user
) && IsLocal(mn
->user
)) {
553 change
->args
[argc
].mode
= MODE_CHANOP
;
554 change
->args
[argc
].u
.member
= mn
;
558 assert(argc
== change
->argc
);
559 change
->args
[0].u
.member
->modes
&= ~MODE_CHANOP
;
560 mod_chanmode_announce(change
->args
[0].u
.member
->user
, cNode
, change
);
561 mod_chanmode_free(change
);
566 AddChannel(const char *name
, time_t time_
, const char *modes
, char *banlist
, char *exemptlist
)
568 struct chanNode
*cNode
;
569 char new_modes
[MAXLEN
], *argv
[MAXNUMPARAMS
];
572 if (!IsChannelName(name
)) {
573 log_module(MAIN_LOG
, LOG_ERROR
, "Somebody asked to add channel '%s', which isn't a channel name!", name
);
579 safestrncpy(new_modes
, modes
, sizeof(new_modes
));
580 nn
= split_line(new_modes
, 0, ArrayLength(argv
), argv
);
581 if (!(cNode
= GetChannel(name
))) {
582 cNode
= calloc(1, sizeof(*cNode
) + strlen(name
));
583 strcpy(cNode
->name
, name
);
584 banList_init(&cNode
->banlist
);
585 exemptList_init(&cNode
->exemptlist
);
586 modeList_init(&cNode
->members
);
587 mod_chanmode(NULL
, cNode
, argv
, nn
, MCP_FROM_SERVER
);
588 dict_insert(channels
, cNode
->name
, cNode
);
589 cNode
->timestamp
= time_
;
591 } else if (cNode
->timestamp
> time_
) {
592 wipeout_channel(cNode
, time_
, argv
, nn
);
594 } else if (cNode
->timestamp
== time_
) {
595 mod_chanmode(NULL
, cNode
, argv
, nn
, MCP_FROM_SERVER
);
601 /* rel_age is the relative ages of our channel data versus what is
602 * in a BURST command. 1 means ours is younger, 0 means both are
603 * the same age, -1 means ours is older. */
605 /* if it's a new or updated channel, make callbacks */
607 for (nn
=0; nn
<ncf_used
; nn
++)
608 ncf_list
[nn
](cNode
, ncf_list_extra
[nn
]);
610 /* go through list of bans and add each one */
611 if (banlist
&& (rel_age
>= 0)) {
612 for (nn
=0; banlist
[nn
];) {
613 char *ban
= banlist
+ nn
;
615 while (banlist
[nn
] != ' ' && banlist
[nn
])
617 while (banlist
[nn
] == ' ')
619 bn
= calloc(1, sizeof(*bn
));
620 safestrncpy(bn
->ban
, ban
, sizeof(bn
->ban
));
621 safestrncpy(bn
->who
, "<unknown>", sizeof(bn
->who
));
623 banList_append(&cNode
->banlist
, bn
);
627 /* go through list of exempts and add each one */
628 if (exemptlist
&& (rel_age
>= 0)) {
629 for (nn
=0; exemptlist
[nn
];) {
630 char *exempt
= exemptlist
+ nn
;
631 struct exemptNode
*en
;
632 while (exemptlist
[nn
] != ' ' && exemptlist
[nn
])
634 while (exemptlist
[nn
] == ' ')
635 exemptlist
[nn
++] = 0;
636 en
= calloc(1, sizeof(*en
));
637 safestrncpy(en
->exempt
, exempt
, sizeof(en
->exempt
));
638 safestrncpy(en
->who
, "<unknown>", sizeof(en
->who
));
640 exemptList_append(&cNode
->exemptlist
, en
);
647 static del_channel_func_t
*dcf_list
;
648 static void **dcf_list_extra
;
649 static unsigned int dcf_size
= 0, dcf_used
= 0;
652 reg_del_channel_func(del_channel_func_t handler
, void *extra
)
654 if (dcf_used
== dcf_size
) {
657 dcf_list
= realloc(dcf_list
, dcf_size
*sizeof(dcf_list
[0]));
658 dcf_list_extra
= realloc(dcf_list_extra
, dcf_size
*sizeof(void*));
661 dcf_list
= malloc(dcf_size
*sizeof(dcf_list
[0]));
662 dcf_list_extra
= malloc(dcf_size
*sizeof(dcf_list_extra
[0]));
665 dcf_list
[dcf_used
] = handler
;
666 dcf_list_extra
[dcf_used
++] = extra
;
670 DelChannel(struct chanNode
*channel
)
675 dict_remove(channels
, channel
->name
);
677 if (channel
->members
.used
|| channel
->locks
) {
678 log_module(MAIN_LOG
, LOG_ERROR
, "Warning: deleting channel %s with %d users and %d locks remaining.", channel
->name
, channel
->members
.used
, channel
->locks
);
681 /* go through all channel members and delete them from the channel */
682 for (n
=channel
->members
.used
; n
>0; )
683 DelChannelUser(channel
->members
.list
[--n
]->user
, channel
, NULL
, 1);
685 /* delete all channel bans */
686 for (n
=channel
->banlist
.used
; n
>0; )
687 free(channel
->banlist
.list
[--n
]);
688 channel
->banlist
.used
= 0;
690 /* delete all channel exempts */
691 for (n
=channel
->exemptlist
.used
; n
>0; )
692 free(channel
->exemptlist
.list
[--n
]);
693 channel
->exemptlist
.used
= 0;
695 for (n
=0; n
<dcf_used
; n
++)
696 dcf_list
[n
](channel
, dcf_list_extra
[n
]);
698 modeList_clean(&channel
->members
);
699 banList_clean(&channel
->banlist
);
700 exemptList_clean(&channel
->exemptlist
);
705 AddChannelUser(struct userNode
*user
, struct chanNode
* channel
)
707 struct modeNode
*mNode
;
710 mNode
= GetUserMode(channel
, user
);
714 mNode
= malloc(sizeof(*mNode
));
716 /* set up modeNode */
717 mNode
->channel
= channel
;
721 mNode
->idle_since
= now
;
723 /* Add modeNode to channel and to user.
724 * We have to do this before calling join funcs in case the
725 * modeNode is manipulated (e.g. chanserv ops the user).
727 modeList_append(&channel
->members
, mNode
);
728 modeList_append(&user
->channels
, mNode
);
730 if (channel
->members
.used
== 1
731 && !(channel
->modes
& MODE_REGISTERED
)
732 && !(channel
->modes
& MODE_APASS
)) {
733 mNode
->modes
|= MODE_CHANOP
;
737 irc_join(user
, channel
);
740 for (n
=0; (n
<jf_used
) && !user
->dead
; n
++) {
741 /* Callbacks return true if they kick or kill the user,
742 * and we can continue without removing mNode. */
743 if (jf_list
[n
](mNode
, jf_list_extra
[n
]))
750 static part_func_t
*pf_list
;
751 static void **pf_list_extra
;
752 static unsigned int pf_size
= 0, pf_used
= 0;
755 reg_part_func(part_func_t handler
, void *extra
)
757 if (pf_used
== pf_size
) {
760 pf_list
= realloc(pf_list
, pf_size
*sizeof(part_func_t
));
761 pf_list_extra
= realloc(pf_list_extra
, pf_size
*sizeof(void*));
764 pf_list
= malloc(pf_size
*sizeof(part_func_t
));
765 pf_list_extra
= malloc(pf_size
*sizeof(void*));
768 pf_list
[pf_used
] = handler
;
769 pf_list_extra
[pf_used
++] = extra
;
773 unreg_part_func(part_func_t handler
, void *extra
)
776 for (i
=0; i
<pf_used
; i
++)
777 if (pf_list
[i
] == handler
&& pf_list_extra
[i
] == extra
)
781 memmove(pf_list
+i
, pf_list
+i
+1, (pf_used
-i
-1)*sizeof(pf_list
[0]));
782 memmove(pf_list_extra
+i
, pf_list_extra
+i
+1, (pf_used
-i
-1)*sizeof(pf_list_extra
[0]));
787 LockChannel(struct chanNode
* channel
)
793 UnlockChannel(struct chanNode
*channel
)
795 assert(channel
->locks
> 0);
796 if (!--channel
->locks
&& !channel
->members
.used
)
801 DelChannelUser(struct userNode
* user
, struct chanNode
* channel
, const char *reason
, int deleting
)
803 struct modeNode
* mNode
;
806 if (IsLocal(user
) && reason
)
807 irc_part(user
, channel
, reason
);
809 mNode
= GetUserMode(channel
, user
);
811 /* Sometimes we get a PART when the user has been KICKed.
812 * In this case, we get no usermode, and should not try to free it.
817 /* remove modeNode from channel and user */
818 modeList_remove(&channel
->members
, mNode
);
819 modeList_remove(&user
->channels
, mNode
);
822 for (n
=0; n
<pf_used
; n
++)
823 pf_list
[n
](mNode
, reason
, pf_list_extra
[n
]);
828 /* A single check for APASS only should be enough here */
829 if (!deleting
&& !channel
->members
.used
&& !channel
->locks
830 && !(channel
->modes
& MODE_REGISTERED
) && !(channel
->modes
& MODE_APASS
))
834 static kick_func_t
*kf_list
;
835 static void **kf_list_extra
;
836 static unsigned int kf_size
= 0, kf_used
= 0;
839 KickChannelUser(struct userNode
* target
, struct chanNode
* channel
, struct userNode
*kicker
, const char *why
)
843 if (!target
|| !channel
|| IsService(target
) || !GetUserMode(channel
, target
))
846 /* This may break things, but lets see.. -Rubin */
847 for (n
=0; n
<kf_used
; n
++)
848 kf_list
[n
](kicker
, target
, channel
, kf_list_extra
[n
]);
850 /* don't remove them from the channel, since the server will send a PART */
851 irc_kick(kicker
, target
, channel
, why
);
855 /* NULL reason because we don't want a PART message to be
856 sent by DelChannelUser. */
857 DelChannelUser(target
, channel
, NULL
, 0);
862 reg_kick_func(kick_func_t handler
, void *extra
)
864 if (kf_used
== kf_size
) {
867 kf_list
= realloc(kf_list
, kf_size
*sizeof(kick_func_t
));
868 kf_list_extra
= realloc(kf_list_extra
, kf_size
*sizeof(void*));
871 kf_list
= malloc(kf_size
*sizeof(kick_func_t
));
872 kf_list_extra
= malloc(kf_size
*sizeof(void*));
875 kf_list
[kf_used
] = handler
;
876 kf_list_extra
[kf_used
++] = extra
;
880 ChannelUserKicked(struct userNode
* kicker
, struct userNode
* victim
, struct chanNode
* channel
)
885 if (!victim
|| !channel
|| !GetUserMode(channel
, victim
))
888 /* Update the kicker's idle time (kicker may be null if it was a server) */
889 if (kicker
&& (mn
= GetUserMode(channel
, kicker
)))
890 mn
->idle_since
= now
;
892 for (n
=0; n
<kf_used
; n
++)
893 kf_list
[n
](kicker
, victim
, channel
, kf_list_extra
[n
]);
895 DelChannelUser(victim
, channel
, 0, 0);
898 irc_part(victim
, channel
, NULL
);
901 int ChannelBanExists(struct chanNode
*channel
, const char *ban
)
905 for (n
= 0; n
< channel
->banlist
.used
; n
++)
906 if (match_ircglobs(channel
->banlist
.list
[n
]->ban
, ban
))
911 int ChannelExemptExists(struct chanNode
*channel
, const char *exempt
)
915 for (n
= 0; n
< channel
->exemptlist
.used
; n
++)
916 if (match_ircglobs(channel
->exemptlist
.list
[n
]->exempt
, exempt
))
921 static topic_func_t
*tf_list
;
922 static void **tf_list_extra
;
923 static unsigned int tf_size
= 0, tf_used
= 0;
926 reg_topic_func(topic_func_t handler
, void *extra
)
928 if (tf_used
== tf_size
) {
931 tf_list
= realloc(tf_list
, tf_size
*sizeof(topic_func_t
));
932 tf_list_extra
= realloc(tf_list_extra
, tf_size
*sizeof(void*));
935 tf_list
= malloc(tf_size
*sizeof(topic_func_t
));
936 tf_list_extra
= malloc(tf_size
*sizeof(void*));
939 tf_list
[tf_used
] = handler
;
940 tf_list_extra
[tf_used
++] = extra
;
944 SetChannelTopic(struct chanNode
*channel
, struct userNode
*service
, struct userNode
*user
, const char *topic
, int announce
)
948 char old_topic
[TOPICLEN
+1];
950 safestrncpy(old_topic
, channel
->topic
, sizeof(old_topic
));
951 safestrncpy(channel
->topic
, topic
, sizeof(channel
->topic
));
952 channel
->topic_time
= now
;
955 safestrncpy(channel
->topic_nick
, user
->nick
, sizeof(channel
->topic_nick
));
957 /* Update the setter's idle time */
958 if ((mn
= GetUserMode(channel
, user
)))
959 mn
->idle_since
= now
;
963 /* We don't really care if a local user messes with the topic,
964 * so don't call the tf_list functions. */
965 irc_topic(service
, user
, channel
, topic
);
967 for (n
=0; n
<tf_used
; n
++)
968 /* A topic change handler can return non-zero to indicate
969 * that it has reverted the topic change, and that further
970 * hooks should not be called.
972 if (tf_list
[n
](user
, channel
, old_topic
, tf_list_extra
[n
]))
978 GetChannel(const char *name
)
980 return dict_find(channels
, name
, NULL
);
984 GetUserMode(struct chanNode
*channel
, struct userNode
*user
)
987 struct modeNode
*mn
= NULL
;
990 verify(channel
->members
.list
);
992 verify(user
->channels
.list
);
993 if (channel
->members
.used
< user
->channels
.used
) {
994 for (n
=0; n
<channel
->members
.used
; n
++) {
995 verify(channel
->members
.list
[n
]);
996 if (user
== channel
->members
.list
[n
]->user
) {
997 mn
= channel
->members
.list
[n
];
1002 for (n
=0; n
<user
->channels
.used
; n
++) {
1003 verify(user
->channels
.list
[n
]);
1004 if (channel
== user
->channels
.list
[n
]->channel
) {
1005 mn
= user
->channels
.list
[n
];
1013 struct userNode
*IsInChannel(struct chanNode
*channel
, struct userNode
*user
)
1018 verify(channel
->members
.list
);
1020 verify(user
->channels
.list
);
1021 if (channel
->members
.used
< user
->channels
.used
) {
1022 for (n
=0; n
<channel
->members
.used
; n
++) {
1023 verify(channel
->members
.list
[n
]);
1024 if (user
== channel
->members
.list
[n
]->user
) {
1029 for (n
=0; n
<user
->channels
.used
; n
++) {
1030 verify(user
->channels
.list
[n
]);
1031 if (channel
== user
->channels
.list
[n
]->channel
) {
1039 DEFINE_LIST(userList
, struct userNode
*)
1040 DEFINE_LIST(modeList
, struct modeNode
*)
1041 DEFINE_LIST(banList
, struct banNode
*)
1042 DEFINE_LIST(exemptList
, struct exemptNode
*)
1043 DEFINE_LIST(channelList
, struct chanNode
*)
1044 DEFINE_LIST(serverList
, struct server
*)
1047 hash_cleanup(UNUSED_ARG(void *extra
))
1049 dict_iterator_t it
, next
;
1051 DelServer(self
, 0, NULL
);
1052 for (it
= dict_first(channels
); it
; it
= next
) {
1053 next
= iter_next(it
);
1054 DelChannel(iter_data(it
));
1056 dict_delete(channels
);
1057 dict_delete(clients
);
1058 dict_delete(servers
);
1059 userList_clean(&curr_opers
);
1063 free(slf_list_extra
);
1065 free(nuf_list_extra
);
1067 free(ncf2_list_extra
);
1069 free(duf_list_extra
);
1071 free(ncf_list_extra
);
1073 free(jf_list_extra
);
1075 free(dcf_list_extra
);
1077 free(pf_list_extra
);
1079 free(kf_list_extra
);
1081 free(tf_list_extra
);