1 /* hash.c - IRC network state database
2 * Copyright 2000-2004 srvx Development Team
4 * This file is part of srvx.
6 * srvx is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
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.
30 unsigned int max_clients, invis_clients;
31 unsigned long max_clients_time;
32 struct userList curr_opers;
34 static void hash_cleanup(void);
36 void init_structs(void)
38 channels = dict_new();
41 userList_init(&curr_opers);
42 reg_exit_func(hash_cleanup);
45 int userList_contains(struct userList *list, struct userNode *user)
49 for (ii = 0; ii < list->used; ++ii) {
50 if (user == list->list[ii]) {
57 server_link_func_t *slf_list;
58 unsigned int slf_size = 0, slf_used = 0;
61 reg_server_link_func(server_link_func_t handler)
63 if (slf_used == slf_size) {
66 slf_list = realloc(slf_list, slf_size*sizeof(server_link_func_t));
69 slf_list = malloc(slf_size*sizeof(server_link_func_t));
72 slf_list[slf_used++] = handler;
76 GetServerH(const char *name)
78 return dict_find(servers, name, NULL);
81 new_user_func_t *nuf_list;
82 unsigned int nuf_size = 0, nuf_used = 0;
85 reg_new_user_func(new_user_func_t handler)
87 if (nuf_used == nuf_size) {
90 nuf_list = realloc(nuf_list, nuf_size*sizeof(new_user_func_t));
93 nuf_list = malloc(nuf_size*sizeof(new_user_func_t));
96 nuf_list[nuf_used++] = handler;
99 static nick_change_func_t *ncf2_list;
100 static unsigned int ncf2_size = 0, ncf2_used = 0;
103 reg_nick_change_func(nick_change_func_t handler)
105 if (ncf2_used == ncf2_size) {
108 ncf2_list = realloc(ncf2_list, ncf2_size*sizeof(nick_change_func_t));
111 ncf2_list = malloc(ncf2_size*sizeof(nick_change_func_t));
114 ncf2_list[ncf2_used++] = handler;
118 del_user_func_t *duf_list;
119 unsigned int duf_size = 0, duf_used = 0;
122 reg_del_user_func(del_user_func_t handler)
124 if (duf_used == duf_size) {
127 duf_list = realloc(duf_list, duf_size*sizeof(del_user_func_t));
130 duf_list = malloc(duf_size*sizeof(del_user_func_t));
133 duf_list[duf_used++] = handler;
137 unreg_del_user_func(del_user_func_t handler)
140 for (i=0; i<duf_used; i++) {
141 if (duf_list[i] == handler) break;
143 if (i == duf_used) return;
144 memmove(duf_list+i, duf_list+i+1, (duf_used-i-1)*sizeof(duf_list[0]));
148 /* reintroduces a user after it has been killed. */
150 ReintroduceUser(struct userNode *user)
152 struct mod_chanmode change;
156 mod_chanmode_init(&change);
158 for (n = 0; n < user->channels.used; n++) {
159 struct modeNode *mn = user->channels.list[n];
160 irc_join(user, mn->channel);
162 change.args[0].mode = mn->modes;
163 change.args[0].u.member = mn;
164 mod_chanmode_announce(user, mn->channel, &change);
170 NickChange(struct userNode* user, const char *new_nick, int no_announce)
175 /* don't do anything if there's no change */
176 old_nick = user->nick;
177 if (!strncmp(new_nick, old_nick, NICKLEN))
180 /* remove old entry from clients dictionary */
181 dict_remove(clients, old_nick);
182 #if !defined(WITH_PROTOCOL_P10)
183 /* Remove from uplink's clients dict */
184 dict_remove(user->uplink->users, old_nick);
187 user->nick = strdup(new_nick);
188 dict_insert(clients, user->nick, user);
189 #if !defined(WITH_PROTOCOL_P10)
190 dict_insert(user->uplink->users, user->nick, user);
193 /* Make callbacks for nick changes. Do this with new nick in
194 * place because that is slightly more useful. Stop if the user
195 * gets killed by any of the hooks, so that later hooks do not get
196 * confused by the user having disappeared.
198 for (nn=0; (nn<ncf2_used) && !user->dead; nn++)
199 ncf2_list[nn](user, old_nick);
200 user->timestamp = now;
201 if (IsLocal(user) && !no_announce)
202 irc_nick(user, old_nick);
207 GetUserH(const char *nick)
209 return dict_find(clients, nick, NULL);
212 static account_func_t account_func;
215 reg_account_func(account_func_t handler)
218 log_module(MAIN_LOG, LOG_WARNING, "Reregistering ACCOUNT handler.");
220 account_func = handler;
224 call_account_func(struct userNode *user, const char *stamp, unsigned long timestamp, unsigned long serial)
226 /* We've received an account stamp for a user; notify
227 NickServ, which registers the sole account_func
230 P10 Protocol violation if (user->modes & FLAGS_STAMPED) here.
233 account_func(user, stamp, timestamp, serial);
235 #ifdef WITH_PROTOCOL_P10
236 /* Mark the user so we don't stamp it again. */
237 user->modes |= FLAGS_STAMPED;
242 StampUser(struct userNode *user, const char *stamp, unsigned long timestamp, unsigned long serial)
244 #ifdef WITH_PROTOCOL_P10
245 /* The P10 protocol says we can't stamp users who already
251 irc_account(user, stamp, timestamp, serial);
252 user->modes |= FLAGS_STAMPED;
256 assign_fakehost(struct userNode *user, const char *host, const char *ident, int force, int announce)
259 safestrncpy(user->fakehost, host, sizeof(user->fakehost));
261 safestrncpy(user->fakeident, ident, sizeof(user->ident));
263 irc_fakehost(user, host, ident, force);
266 static new_channel_func_t *ncf_list;
267 static unsigned int ncf_size = 0, ncf_used = 0;
270 reg_new_channel_func(new_channel_func_t handler)
272 if (ncf_used == ncf_size) {
275 ncf_list = realloc(ncf_list, ncf_size*sizeof(ncf_list[0]));
278 ncf_list = malloc(ncf_size*sizeof(ncf_list[0]));
281 ncf_list[ncf_used++] = handler;
284 static join_func_t *jf_list;
285 static unsigned int jf_size = 0, jf_used = 0;
288 reg_join_func(join_func_t handler)
290 if (jf_used == jf_size) {
293 jf_list = realloc(jf_list, jf_size*sizeof(join_func_t));
296 jf_list = malloc(jf_size*sizeof(join_func_t));
299 jf_list[jf_used++] = handler;
305 wipeout_channel(struct chanNode *cNode, unsigned long new_time, char **modes, unsigned int modec) {
306 unsigned int orig_limit;
307 chan_mode_t orig_modes;
308 char orig_key[KEYLEN+1];
309 char orig_apass[KEYLEN+1];
310 char orig_upass[KEYLEN+1];
311 unsigned int nn, argc;
314 cNode->topic[0] = '\0';
315 cNode->topic_nick[0] = '\0';
316 cNode->topic_time = 0;
318 /* remember the old modes, and update them with the new */
319 orig_modes = cNode->modes;
320 orig_limit = cNode->limit;
321 strcpy(orig_key, cNode->key);
322 strcpy(orig_upass, cNode->upass);
323 strcpy(orig_apass, cNode->apass);
325 mod_chanmode(NULL, cNode, modes, modec, 0);
326 cNode->timestamp = new_time;
328 /* remove our old ban list, replace it with the new one */
329 for (nn=0; nn<cNode->banlist.used; nn++)
330 free(cNode->banlist.list[nn]);
331 cNode->banlist.used = 0;
333 /* deop anybody in the channel now, but count services to reop */
334 for (nn=argc=0; nn<cNode->members.used; nn++) {
335 struct modeNode *mn = cNode->members.list[nn];
336 if ((mn->modes & MODE_CHANOP) && IsService(mn->user) && IsLocal(mn->user))
340 struct mod_chanmode *change;
342 change = mod_chanmode_alloc(argc);
343 change->modes_clear = 0;
344 change->modes_set = orig_modes;
345 change->new_limit = orig_limit;
346 strcpy(change->new_key, orig_key);
347 strcpy(change->new_upass, orig_upass);
348 strcpy(change->new_apass, orig_apass);
349 for (nn = argc = 0; nn < cNode->members.used; ++nn) {
350 struct modeNode *mn = cNode->members.list[nn];
351 if ((mn->modes & MODE_CHANOP) && IsService(mn->user) && IsLocal(mn->user)) {
352 change->args[argc].mode = MODE_CHANOP;
353 change->args[argc].u.member = mn;
357 assert(argc == change->argc);
358 change->args[0].u.member->modes &= ~MODE_CHANOP;
359 mod_chanmode_announce(change->args[0].u.member->user, cNode, change);
360 mod_chanmode_free(change);
365 AddChannel(const char *name, unsigned long time_, const char *modes, char *banlist)
367 struct chanNode *cNode;
368 char new_modes[MAXLEN], *argv[MAXNUMPARAMS];
371 if (!IsChannelName(name)) {
372 log_module(MAIN_LOG, LOG_ERROR, "Somebody asked to add channel '%s', which isn't a channel name!", name);
378 safestrncpy(new_modes, modes, sizeof(new_modes));
379 nn = split_line(new_modes, 0, ArrayLength(argv), argv);
380 if (!(cNode = GetChannel(name))) {
381 cNode = calloc(1, sizeof(*cNode) + strlen(name));
382 strcpy(cNode->name, name);
383 banList_init(&cNode->banlist);
384 modeList_init(&cNode->members);
385 userList_init(&cNode->invited);
386 mod_chanmode(NULL, cNode, argv, nn, MCP_FROM_SERVER);
387 dict_insert(channels, cNode->name, cNode);
388 cNode->timestamp = time_;
390 } else if (cNode->timestamp > time_) {
391 wipeout_channel(cNode, time_, argv, nn);
393 } else if (cNode->timestamp == time_) {
394 mod_chanmode(NULL, cNode, argv, nn, MCP_FROM_SERVER);
400 /* rel_age is the relative ages of our channel data versus what is
401 * in a BURST command. 1 means ours is younger, 0 means both are
402 * the same age, -1 means ours is older. */
404 /* if it's a new or updated channel, make callbacks */
406 for (nn=0; nn<ncf_used; nn++)
409 /* go through list of bans and add each one */
410 if (banlist && (rel_age >= 0)) {
411 for (nn=0; banlist[nn];) {
412 char *ban = banlist + nn;
414 while (banlist[nn] != ' ' && banlist[nn])
416 while (banlist[nn] == ' ')
418 bn = calloc(1, sizeof(*bn));
419 safestrncpy(bn->ban, ban, sizeof(bn->ban));
420 safestrncpy(bn->who, "<unknown>", sizeof(bn->who));
422 banList_append(&cNode->banlist, bn);
429 static del_channel_func_t *dcf_list;
430 static unsigned int dcf_size = 0, dcf_used = 0;
433 reg_del_channel_func(del_channel_func_t handler)
435 if (dcf_used == dcf_size) {
438 dcf_list = realloc(dcf_list, dcf_size*sizeof(dcf_list[0]));
441 dcf_list = malloc(dcf_size*sizeof(dcf_list[0]));
444 dcf_list[dcf_used++] = handler;
448 DelChannel(struct chanNode *channel)
453 dict_remove(channels, channel->name);
455 if (channel->members.used || channel->locks) {
456 log_module(MAIN_LOG, LOG_ERROR, "Warning: deleting channel %s with %d users and %d locks remaining.", channel->name, channel->members.used, channel->locks);
459 /* go through all channel members and delete them from the channel */
460 for (n=channel->members.used; n>0; )
461 DelChannelUser(channel->members.list[--n]->user, channel, NULL, 1);
463 /* delete all channel bans */
464 for (n=channel->banlist.used; n>0; )
465 free(channel->banlist.list[--n]);
466 channel->banlist.used = 0;
468 for (n=0; n<dcf_used; n++)
469 dcf_list[n](channel);
471 modeList_clean(&channel->members);
472 banList_clean(&channel->banlist);
473 userList_clean(&channel->invited);
478 AddChannelUser(struct userNode *user, struct chanNode* channel)
480 struct modeNode *mNode;
483 mNode = GetUserMode(channel, user);
487 mNode = malloc(sizeof(*mNode));
489 /* set up modeNode */
490 mNode->channel = channel;
493 mNode->oplevel = MAXOPLEVEL;
494 mNode->idle_since = now;
496 /* Add modeNode to channel and to user.
497 * We have to do this before calling join funcs in case the
498 * modeNode is manipulated (e.g. chanserv ops the user).
500 modeList_append(&channel->members, mNode);
501 modeList_append(&user->channels, mNode);
503 if (channel->members.used == 1
504 && !(channel->modes & MODE_REGISTERED)
505 && !(channel->modes & MODE_APASS))
506 mNode->modes |= MODE_CHANOP;
509 irc_join(user, channel);
512 for (n=0; (n<jf_used) && !user->dead; n++) {
513 /* Callbacks return true if they kick or kill the user,
514 * and we can continue without removing mNode. */
515 if (jf_list[n](mNode))
522 /* Return negative if *(struct modeNode**)pa is "less than" pb,
523 * positive if pa is "larger than" pb. Comparison is based on sorting
524 * so that non-voiced/non-opped users are first, voiced-only users are
525 * next, and the "strongest" oplevels are before "weaker" oplevels.
526 * Within those sets, ordering is arbitrary.
529 modeNode_sort(const void *pa, const void *pb)
531 struct modeNode *a = *(struct modeNode**)pa;
532 struct modeNode *b = *(struct modeNode**)pb;
534 if (a->modes & MODE_CHANOP) {
535 if (!(b->modes & MODE_CHANOP))
537 else if ((b->modes & MODE_VOICE) != (a->modes & MODE_VOICE))
538 return (b->modes & MODE_VOICE) - (a->modes & MODE_VOICE);
539 else if (a->oplevel != b->oplevel)
540 return a->oplevel - b->oplevel;
541 } else if (b->modes & MODE_CHANOP)
543 else if ((b->modes & MODE_VOICE) != (a->modes & MODE_VOICE))
544 return (b->modes & MODE_VOICE) - (a->modes & MODE_VOICE);
546 return irccasecmp(a->user->nick, b->user->nick);
549 static part_func_t *pf_list;
550 static unsigned int pf_size = 0, pf_used = 0;
553 reg_part_func(part_func_t handler)
555 if (pf_used == pf_size) {
558 pf_list = realloc(pf_list, pf_size*sizeof(part_func_t));
561 pf_list = malloc(pf_size*sizeof(part_func_t));
564 pf_list[pf_used++] = handler;
568 unreg_part_func(part_func_t handler)
571 for (i=0; i<pf_used; i++)
572 if (pf_list[i] == handler)
576 memmove(pf_list+i, pf_list+i+1, (pf_used-i-1)*sizeof(pf_list[0]));
581 LockChannel(struct chanNode* channel)
587 UnlockChannel(struct chanNode *channel)
589 assert(channel->locks > 0);
590 if (!--channel->locks && !channel->members.used)
595 DelChannelUser(struct userNode* user, struct chanNode* channel, const char *reason, int deleting)
597 struct modeNode* mNode;
600 if (IsLocal(user) && reason)
601 irc_part(user, channel, reason);
603 mNode = GetUserMode(channel, user);
605 /* Sometimes we get a PART when the user has been KICKed.
606 * In this case, we get no usermode, and should not try to free it.
611 /* remove modeNode from channel and user */
612 modeList_remove(&channel->members, mNode);
613 modeList_remove(&user->channels, mNode);
616 for (n=0; n<pf_used; n++)
617 pf_list[n](mNode, reason);
622 /* A single check for APASS only should be enough here */
623 if (!deleting && !channel->members.used && !channel->locks
624 && !(channel->modes & MODE_REGISTERED) && !(channel->modes & MODE_APASS))
629 KickChannelUser(struct userNode* target, struct chanNode* channel, struct userNode *kicker, const char *why)
631 if (!target || !channel || IsService(target) || !GetUserMode(channel, target))
633 /* don't remove them from the channel, since the server will send a PART */
634 irc_kick(kicker, target, channel, why);
638 /* NULL reason because we don't want a PART message to be
639 sent by DelChannelUser. */
640 DelChannelUser(target, channel, NULL, 0);
644 static kick_func_t *kf_list;
645 static unsigned int kf_size = 0, kf_used = 0;
648 reg_kick_func(kick_func_t handler)
650 if (kf_used == kf_size) {
653 kf_list = realloc(kf_list, kf_size*sizeof(kick_func_t));
656 kf_list = malloc(kf_size*sizeof(kick_func_t));
659 kf_list[kf_used++] = handler;
663 ChannelUserKicked(struct userNode* kicker, struct userNode* victim, struct chanNode* channel)
668 if (!victim || !channel || !GetUserMode(channel, victim))
671 /* Update the kicker's idle time (kicker may be null if it was a server) */
672 if (kicker && (mn = GetUserMode(channel, kicker)))
673 mn->idle_since = now;
675 for (n=0; n<kf_used; n++)
676 kf_list[n](kicker, victim, channel);
678 DelChannelUser(victim, channel, 0, 0);
681 irc_part(victim, channel, NULL);
684 int ChannelBanExists(struct chanNode *channel, const char *ban)
688 for (n = 0; n < channel->banlist.used; n++)
689 if (match_ircglobs(channel->banlist.list[n]->ban, ban))
694 static topic_func_t *tf_list;
695 static unsigned int tf_size = 0, tf_used = 0;
698 reg_topic_func(topic_func_t handler)
700 if (tf_used == tf_size) {
703 tf_list = realloc(tf_list, tf_size*sizeof(topic_func_t));
706 tf_list = malloc(tf_size*sizeof(topic_func_t));
709 tf_list[tf_used++] = handler;
713 SetChannelTopic(struct chanNode *channel, struct userNode *user, const char *topic, int announce)
717 char old_topic[TOPICLEN+1];
719 safestrncpy(old_topic, channel->topic, sizeof(old_topic));
720 safestrncpy(channel->topic, topic, sizeof(channel->topic));
721 channel->topic_time = now;
724 safestrncpy(channel->topic_nick, user->nick, sizeof(channel->topic_nick));
726 /* Update the setter's idle time */
727 if ((mn = GetUserMode(channel, user)))
728 mn->idle_since = now;
732 /* We don't really care if a local user messes with the topic,
733 * so don't call the tf_list functions. */
734 irc_topic(user, channel, topic);
736 for (n=0; n<tf_used; n++)
737 /* A topic change handler can return non-zero to indicate
738 * that it has reverted the topic change, and that further
739 * hooks should not be called.
741 if (tf_list[n](user, channel, old_topic))
747 GetChannel(const char *name)
749 return dict_find(channels, name, NULL);
753 GetUserMode(struct chanNode *channel, struct userNode *user)
756 struct modeNode *mn = NULL;
759 verify(channel->members.list);
761 verify(user->channels.list);
762 if (channel->members.used < user->channels.used) {
763 for (n=0; n<channel->members.used; n++) {
764 verify(channel->members.list[n]);
765 if (user == channel->members.list[n]->user) {
766 mn = channel->members.list[n];
771 for (n=0; n<user->channels.used; n++) {
772 verify(user->channels.list[n]);
773 if (channel == user->channels.list[n]->channel) {
774 mn = user->channels.list[n];
782 DEFINE_LIST(userList, struct userNode*)
783 DEFINE_LIST(modeList, struct modeNode*)
784 DEFINE_LIST(banList, struct banNode*)
785 DEFINE_LIST(channelList, struct chanNode*)
786 DEFINE_LIST(serverList, struct server*)
791 dict_iterator_t it, next;
793 DelServer(self, 0, NULL);
794 for (it = dict_first(channels); it; it = next) {
795 next = iter_next(it);
796 DelChannel(iter_data(it));
798 dict_delete(channels);
799 dict_delete(clients);
800 dict_delete(servers);
801 userList_clean(&curr_opers);