1 /* hash.c - IRC network state database
2 * Copyright 2000-2004 srvx Development Team
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version. Important limitations are
8 * listed in the COPYING file that accompanies this software.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, email srvx-maintainers@srvx.net.
28 unsigned int max_clients, invis_clients;
29 time_t max_clients_time;
30 struct userList curr_opers;
32 static void hash_cleanup(void);
34 void init_structs(void)
36 channels = dict_new();
39 userList_init(&curr_opers);
40 reg_exit_func(hash_cleanup);
43 server_link_func_t *slf_list;
44 unsigned int slf_size = 0, slf_used = 0;
47 reg_server_link_func(server_link_func_t handler)
49 if (slf_used == slf_size) {
52 slf_list = realloc(slf_list, slf_size*sizeof(server_link_func_t));
55 slf_list = malloc(slf_size*sizeof(server_link_func_t));
58 slf_list[slf_used++] = handler;
62 GetServerH(const char *name)
64 return dict_find(servers, name, NULL);
67 new_user_func_t *nuf_list;
68 unsigned int nuf_size = 0, nuf_used = 0;
71 reg_new_user_func(new_user_func_t handler)
73 if (nuf_used == nuf_size) {
76 nuf_list = realloc(nuf_list, nuf_size*sizeof(new_user_func_t));
79 nuf_list = malloc(nuf_size*sizeof(new_user_func_t));
82 nuf_list[nuf_used++] = handler;
85 static nick_change_func_t *ncf2_list;
86 static unsigned int ncf2_size = 0, ncf2_used = 0;
89 reg_nick_change_func(nick_change_func_t handler)
91 if (ncf2_used == ncf2_size) {
94 ncf2_list = realloc(ncf2_list, ncf2_size*sizeof(nick_change_func_t));
97 ncf2_list = malloc(ncf2_size*sizeof(nick_change_func_t));
100 ncf2_list[ncf2_used++] = handler;
104 del_user_func_t *duf_list;
105 unsigned int duf_size = 0, duf_used = 0;
108 reg_del_user_func(del_user_func_t handler)
110 if (duf_used == duf_size) {
113 duf_list = realloc(duf_list, duf_size*sizeof(del_user_func_t));
116 duf_list = malloc(duf_size*sizeof(del_user_func_t));
119 duf_list[duf_used++] = handler;
123 unreg_del_user_func(del_user_func_t handler)
126 for (i=0; i<duf_used; i++) {
127 if (duf_list[i] == handler) break;
129 if (i == duf_used) return;
130 memmove(duf_list+i, duf_list+i+1, (duf_used-i-1)*sizeof(duf_list[0]));
134 /* reintroduces a user after it has been killed. */
136 ReintroduceUser(struct userNode *user)
138 struct mod_chanmode change;
142 change.modes_set = change.modes_clear = 0;
144 for (n = 0; n < user->channels.used; n++) {
145 struct modeNode *mn = user->channels.list[n];
146 irc_join(user, mn->channel);
148 change.args[0].mode = mn->modes;
149 change.args[0].member = mn;
150 mod_chanmode_announce(user, mn->channel, &change);
156 NickChange(struct userNode* user, const char *new_nick, int no_announce)
161 /* don't do anything if there's no change */
162 old_nick = user->nick;
163 if (!strncmp(new_nick, old_nick, NICKLEN))
166 /* remove old entry from clients dictionary */
167 dict_remove(clients, old_nick);
168 #if !defined(WITH_PROTOCOL_P10)
169 /* Remove from uplink's clients dict */
170 dict_remove(user->uplink->users, old_nick);
173 user->nick = strdup(new_nick);
174 dict_insert(clients, user->nick, user);
175 #if !defined(WITH_PROTOCOL_P10)
176 dict_insert(user->uplink->users, user->nick, user);
179 /* Make callbacks for nick changes. Do this with new nick in
180 * place because that is slightly more useful.
182 for (nn=0; nn<ncf2_used; nn++)
183 ncf2_list[nn](user, old_nick);
184 user->timestamp = now;
185 if (IsLocal(user) && !no_announce)
186 irc_nick(user, old_nick);
191 GetUserH(const char *nick)
193 return dict_find(clients, nick, NULL);
196 static account_func_t account_func;
199 reg_account_func(account_func_t handler)
202 log_module(MAIN_LOG, LOG_WARNING, "Reregistering ACCOUNT handler.");
204 account_func = handler;
208 call_account_func(struct userNode *user, const char *stamp)
210 /* We've received an account stamp for a user; notify
211 NickServ, which registers the sole account_func
214 P10 Protocol violation if (user->modes & FLAGS_STAMPED) here.
217 account_func(user, stamp);
219 #ifdef WITH_PROTOCOL_P10
220 /* Mark the user so we don't stamp it again. */
221 user->modes |= FLAGS_STAMPED;
226 StampUser(struct userNode *user, const char *stamp)
228 #ifdef WITH_PROTOCOL_P10
229 /* The P10 protocol says we can't stamp users who already
235 irc_account(user, stamp);
236 user->modes |= FLAGS_STAMPED;
239 static new_channel_func_t *ncf_list;
240 static unsigned int ncf_size = 0, ncf_used = 0;
243 reg_new_channel_func(new_channel_func_t handler)
245 if (ncf_used == ncf_size) {
248 ncf_list = realloc(ncf_list, ncf_size*sizeof(ncf_list[0]));
251 ncf_list = malloc(ncf_size*sizeof(ncf_list[0]));
254 ncf_list[ncf_used++] = handler;
257 static join_func_t *jf_list;
258 static unsigned int jf_size = 0, jf_used = 0;
261 reg_join_func(join_func_t handler)
263 if (jf_used == jf_size) {
266 jf_list = realloc(jf_list, jf_size*sizeof(join_func_t));
269 jf_list = malloc(jf_size*sizeof(join_func_t));
272 jf_list[jf_used++] = handler;
278 wipeout_channel(struct chanNode *cNode, time_t new_time, char **modes, unsigned int modec) {
279 unsigned int orig_limit;
280 chan_mode_t orig_modes;
281 char orig_key[KEYLEN+1];
282 unsigned int nn, argc;
285 cNode->topic[0] = '\0';
286 cNode->topic_nick[0] = '\0';
287 cNode->topic_time = 0;
289 /* remember the old modes, and update them with the new */
290 orig_modes = cNode->modes;
291 orig_limit = cNode->limit;
292 strcpy(orig_key, cNode->key);
294 mod_chanmode(NULL, cNode, modes, modec, 0);
295 cNode->timestamp = new_time;
297 /* remove our old ban list, replace it with the new one */
298 for (nn=0; nn<cNode->banlist.used; nn++)
299 free(cNode->banlist.list[nn]);
300 cNode->banlist.used = 0;
302 /* deop anybody in the channel now, but count services to reop */
303 for (nn=argc=0; nn<cNode->members.used; nn++) {
304 struct modeNode *mn = cNode->members.list[nn];
305 if ((mn->modes & MODE_CHANOP) && IsService(mn->user) && IsLocal(mn->user))
309 extern struct userNode *opserv;
310 struct mod_chanmode *change;
312 change = mod_chanmode_alloc(argc);
313 change->modes_clear = 0;
314 change->modes_set = orig_modes;
315 change->new_limit = orig_limit;
316 strcpy(change->new_key, orig_key);
317 for (nn = argc = 0; nn < cNode->members.used; ++nn) {
318 struct modeNode *mn = cNode->members.list[nn];
319 if ((mn->modes & MODE_CHANOP) && IsService(mn->user) && IsLocal(mn->user)) {
320 change->args[argc].mode = MODE_CHANOP;
321 change->args[argc].member = mn;
325 assert(argc == change->argc);
326 if (change->argc > 0)
327 mod_chanmode_announce(change->args[0].member->user, cNode, change);
329 mod_chanmode_announce(opserv, cNode, change);
330 mod_chanmode_free(change);
335 AddChannel(const char *name, time_t time_, const char *modes, char *banlist)
337 struct chanNode *cNode;
338 char new_modes[MAXLEN], *argv[MAXNUMPARAMS];
341 if (!IsChannelName(name)) {
342 log_module(MAIN_LOG, LOG_ERROR, "Somebody asked to add channel '%s', which isn't a channel name!", name);
348 safestrncpy(new_modes, modes, sizeof(new_modes));
349 nn = split_line(new_modes, 0, ArrayLength(argv), argv);
350 if (!(cNode = GetChannel(name))) {
351 cNode = calloc(1, sizeof(*cNode) + strlen(name));
352 strcpy(cNode->name, name);
353 banList_init(&cNode->banlist);
354 modeList_init(&cNode->members);
355 mod_chanmode(NULL, cNode, argv, nn, 0);
356 dict_insert(channels, cNode->name, cNode);
357 cNode->timestamp = time_;
359 } else if (cNode->timestamp > time_) {
360 wipeout_channel(cNode, time_, argv, nn);
362 } else if (cNode->timestamp == time_) {
363 mod_chanmode(NULL, cNode, argv, nn, 0);
369 /* rel_age is the relative ages of our channel data versus what is
370 * in a BURST command. 1 means ours is younger, 0 means both are
371 * the same age, -1 means ours is older. */
373 /* if it's a new or updated channel, make callbacks */
375 for (nn=0; nn<ncf_used; nn++)
378 /* go through list of bans and add each one */
379 if (banlist && (rel_age >= 0)) {
380 for (nn=0; banlist[nn];) {
381 char *ban = banlist + nn;
383 while (banlist[nn] != ' ' && banlist[nn])
385 while (banlist[nn] == ' ')
387 bn = calloc(1, sizeof(*bn));
388 safestrncpy(bn->ban, ban, sizeof(bn->ban));
389 safestrncpy(bn->who, "<unknown>", sizeof(bn->who));
391 banList_append(&cNode->banlist, bn);
398 static del_channel_func_t *dcf_list;
399 static unsigned int dcf_size = 0, dcf_used = 0;
402 reg_del_channel_func(del_channel_func_t handler)
404 if (dcf_used == dcf_size) {
407 dcf_list = realloc(dcf_list, dcf_size*sizeof(dcf_list[0]));
410 dcf_list = malloc(dcf_size*sizeof(dcf_list[0]));
413 dcf_list[dcf_used++] = handler;
417 DelChannel(struct chanNode *channel)
421 dict_remove(channels, channel->name);
423 if (channel->members.used || channel->locks) {
424 log_module(MAIN_LOG, LOG_ERROR, "Warning: deleting channel %s with %d users and %d locks remaining.", channel->name, channel->members.used, channel->locks);
427 /* go through all channel members and delete them from the channel */
428 for (n=channel->members.used; n>0; )
429 DelChannelUser(channel->members.list[--n]->user, channel, false, 1);
431 /* delete all channel bans */
432 for (n=channel->banlist.used; n>0; )
433 free(channel->banlist.list[--n]);
434 channel->banlist.used = 0;
436 for (n=0; n<dcf_used; n++)
437 dcf_list[n](channel);
439 modeList_clean(&channel->members);
440 banList_clean(&channel->banlist);
445 AddChannelUser(struct userNode *user, struct chanNode* channel)
447 struct modeNode *mNode;
450 mNode = GetUserMode(channel, user);
454 mNode = malloc(sizeof(*mNode));
456 /* set up modeNode */
457 mNode->channel = channel;
460 mNode->idle_since = now;
462 /* Add modeNode to channel and to user.
463 * We have to do this before calling join funcs in case the
464 * modeNode is manipulated (e.g. chanserv ops the user).
466 modeList_append(&channel->members, mNode);
467 modeList_append(&user->channels, mNode);
469 if (channel->members.used == 1)
470 mNode->modes |= MODE_CHANOP;
472 for (n=0; n<jf_used; n++) {
473 /* Callbacks return true if they kick or kill the user,
474 * and we can continue without removing mNode. */
475 if (jf_list[n](mNode))
480 irc_join(user, channel);
485 static part_func_t *pf_list;
486 static unsigned int pf_size = 0, pf_used = 0;
489 reg_part_func(part_func_t handler)
491 if (pf_used == pf_size) {
494 pf_list = realloc(pf_list, pf_size*sizeof(part_func_t));
497 pf_list = malloc(pf_size*sizeof(part_func_t));
500 pf_list[pf_used++] = handler;
504 unreg_part_func(part_func_t handler)
507 for (i=0; i<pf_used; i++)
508 if (pf_list[i] == handler)
512 memmove(pf_list+i, pf_list+i+1, (pf_used-i-1)*sizeof(pf_list[0]));
517 LockChannel(struct chanNode* channel)
523 UnlockChannel(struct chanNode *channel)
525 assert(channel->locks > 0);
526 if (!--channel->locks && !channel->members.used)
531 DelChannelUser(struct userNode* user, struct chanNode* channel, const char *reason, int deleting)
533 struct modeNode* mNode;
537 irc_part(user, channel, reason);
540 mNode = GetUserMode(channel, user);
542 /* Sometimes we get a PART when the user has been KICKed.
543 * In this case, we get no usermode, and should not try to free it.
548 /* remove modeNode from channel and user */
549 modeList_remove(&channel->members, mNode);
550 modeList_remove(&user->channels, mNode);
553 for (n=0; n<pf_used; n++)
554 pf_list[n](user, channel, reason);
556 if (!deleting && !channel->members.used && !channel->locks)
561 KickChannelUser(struct userNode* target, struct chanNode* channel, struct userNode *kicker, const char *why)
563 if (!target || !channel || IsService(target) || !GetUserMode(channel, target))
565 /* don't remove them from the channel, since the server will send a PART */
566 irc_kick(kicker, target, channel, why);
570 /* NULL reason because we don't want a PART message to be
571 sent by DelChannelUser. */
572 DelChannelUser(target, channel, NULL, 0);
576 static kick_func_t *kf_list;
577 static unsigned int kf_size = 0, kf_used = 0;
580 reg_kick_func(kick_func_t handler)
582 if (kf_used == kf_size) {
585 kf_list = realloc(kf_list, kf_size*sizeof(kick_func_t));
588 kf_list = malloc(kf_size*sizeof(kick_func_t));
591 kf_list[kf_used++] = handler;
595 ChannelUserKicked(struct userNode* kicker, struct userNode* victim, struct chanNode* channel)
600 if (!victim || !channel || IsService(victim) || !GetUserMode(channel, victim))
603 /* Update the kicker's idle time (kicker may be null if it was a server) */
604 if (kicker && (mn = GetUserMode(channel, kicker)))
605 mn->idle_since = now;
607 for (n=0; n<kf_used; n++)
608 kf_list[n](kicker, victim, channel);
610 DelChannelUser(victim, channel, 0, 0);
613 irc_part(victim, channel, NULL);
616 int ChannelBanExists(struct chanNode *channel, const char *ban)
620 for (n = 0; n < channel->banlist.used; n++)
621 if (match_ircglobs(channel->banlist.list[n]->ban, ban))
626 static topic_func_t *tf_list;
627 static unsigned int tf_size = 0, tf_used = 0;
630 reg_topic_func(topic_func_t handler)
632 if (tf_used == tf_size) {
635 tf_list = realloc(tf_list, tf_size*sizeof(topic_func_t));
638 tf_list = malloc(tf_size*sizeof(topic_func_t));
641 tf_list[tf_used++] = handler;
645 SetChannelTopic(struct chanNode *channel, struct userNode *user, const char *topic, int announce)
649 char old_topic[TOPICLEN+1];
651 safestrncpy(old_topic, channel->topic, sizeof(old_topic));
652 safestrncpy(channel->topic, topic, sizeof(channel->topic));
653 channel->topic_time = now;
656 safestrncpy(channel->topic_nick, user->nick, sizeof(channel->topic_nick));
658 /* Update the setter's idle time */
659 if ((mn = GetUserMode(channel, user)))
660 mn->idle_since = now;
664 /* We don't really care if a local user messes with the topic,
665 * so don't call the tf_list functions. */
666 irc_topic(user, channel, topic);
668 for (n=0; n<tf_used; n++)
669 if (tf_list[n](user, channel, old_topic))
675 GetChannel(const char *name)
677 return dict_find(channels, name, NULL);
681 GetUserMode(struct chanNode *channel, struct userNode *user)
684 struct modeNode *mn = NULL;
685 if (channel->members.used < user->channels.used) {
686 for (n=0; n<channel->members.used; n++) {
687 if (user == channel->members.list[n]->user) {
688 mn = channel->members.list[n];
693 for (n=0; n<user->channels.used; n++) {
694 if (channel == user->channels.list[n]->channel) {
695 mn = user->channels.list[n];
703 DEFINE_LIST(userList, struct userNode*)
704 DEFINE_LIST(modeList, struct modeNode*)
705 DEFINE_LIST(banList, struct banNode*)
706 DEFINE_LIST(channelList, struct chanNode*)
707 DEFINE_LIST(serverList, struct server*)
712 DelServer(self, 0, NULL);
713 dict_delete(channels);
714 dict_delete(clients);
715 dict_delete(servers);
716 userList_clean(&curr_opers);