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;
284 /* remember the old modes, and update them with the new */
285 orig_modes = cNode->modes;
286 orig_limit = cNode->limit;
287 strcpy(orig_key, cNode->key);
289 mod_chanmode(NULL, cNode, modes, modec, 0);
290 cNode->timestamp = new_time;
292 /* remove our old ban list, replace it with the new one */
293 for (nn=0; nn<cNode->banlist.used; nn++)
294 free(cNode->banlist.list[nn]);
295 cNode->banlist.used = 0;
297 /* deop anybody in the channel now, but count services to reop */
298 for (nn=argc=0; nn<cNode->members.used; nn++) {
299 struct modeNode *mn = cNode->members.list[nn];
300 if (mn->modes & MODE_CHANOP && IsService(mn->user) && IsLocal(mn->user))
304 extern struct userNode *opserv;
305 struct mod_chanmode *change;
307 change = mod_chanmode_alloc(nn);
308 change->modes_clear = 0;
309 change->modes_set = orig_modes;
310 change->new_limit = orig_limit;
311 strcpy(change->new_key, orig_key);
312 for (nn = argc = 0; nn < cNode->members.used; ++nn) {
313 struct modeNode *mn = cNode->members.list[nn];
314 if ((mn->modes & MODE_CHANOP) && IsService(mn->user) && IsLocal(mn->user)) {
315 change->args[argc].mode = MODE_CHANOP;
316 change->args[argc].member = mn;
320 if (change->argc > 0)
321 mod_chanmode_announce(change->args[0].member->user, cNode, change);
323 mod_chanmode_announce(opserv, cNode, change);
324 mod_chanmode_free(change);
329 AddChannel(const char *name, time_t time_, const char *modes, char *banlist)
331 struct chanNode *cNode;
332 char new_modes[MAXLEN], *argv[MAXNUMPARAMS];
335 if (!IsChannelName(name)) {
336 log_module(MAIN_LOG, LOG_ERROR, "Somebody asked to add channel '%s', which isn't a channel name!", name);
342 safestrncpy(new_modes, modes, sizeof(new_modes));
343 nn = split_line(new_modes, 0, ArrayLength(argv), argv);
344 if (!(cNode = GetChannel(name))) {
345 cNode = calloc(1, sizeof(*cNode) + strlen(name));
346 strcpy(cNode->name, name);
347 banList_init(&cNode->banlist);
348 modeList_init(&cNode->members);
349 mod_chanmode(NULL, cNode, argv, nn, 0);
350 dict_insert(channels, cNode->name, cNode);
351 cNode->timestamp = time_;
353 } else if (cNode->timestamp > time_) {
354 wipeout_channel(cNode, time_, argv, nn);
356 } else if (cNode->timestamp == time_) {
357 mod_chanmode(NULL, cNode, argv, nn, 0);
363 /* rel_age is the relative ages of our channel data versus what is
364 * in a BURST command. 1 means ours is younger, 0 means both are
365 * the same age, -1 means ours is older. */
367 /* if it's a new or updated channel, make callbacks */
369 for (nn=0; nn<ncf_used; nn++)
372 /* go through list of bans and add each one */
373 if (banlist && (rel_age >= 0)) {
374 for (nn=0; banlist[nn];) {
375 char *ban = banlist + nn;
377 while (banlist[nn] != ' ' && banlist[nn])
379 while (banlist[nn] == ' ')
381 bn = calloc(1, sizeof(*bn));
382 safestrncpy(bn->ban, ban, sizeof(bn->ban));
383 safestrncpy(bn->who, "<unknown>", sizeof(bn->who));
385 banList_append(&cNode->banlist, bn);
392 static del_channel_func_t *dcf_list;
393 static unsigned int dcf_size = 0, dcf_used = 0;
396 reg_del_channel_func(del_channel_func_t handler)
398 if (dcf_used == dcf_size) {
401 dcf_list = realloc(dcf_list, dcf_size*sizeof(dcf_list[0]));
404 dcf_list = malloc(dcf_size*sizeof(dcf_list[0]));
407 dcf_list[dcf_used++] = handler;
411 DelChannel(struct chanNode *channel)
415 dict_remove(channels, channel->name);
417 if (channel->members.used || channel->locks) {
418 log_module(MAIN_LOG, LOG_ERROR, "Warning: deleting channel %s with %d users and %d locks remaining.", channel->name, channel->members.used, channel->locks);
421 /* go through all channel members and delete them from the channel */
422 for (n=channel->members.used; n>0; )
423 DelChannelUser(channel->members.list[--n]->user, channel, false, 1);
425 /* delete all channel bans */
426 for (n=channel->banlist.used; n>0; )
427 free(channel->banlist.list[--n]);
428 channel->banlist.used = 0;
430 for (n=0; n<dcf_used; n++)
431 dcf_list[n](channel);
433 modeList_clean(&channel->members);
434 banList_clean(&channel->banlist);
439 AddChannelUser(struct userNode *user, struct chanNode* channel)
441 struct modeNode *mNode;
444 mNode = GetUserMode(channel, user);
448 mNode = malloc(sizeof(*mNode));
450 /* set up modeNode */
451 mNode->channel = channel;
454 mNode->idle_since = now;
456 /* Add modeNode to channel and to user.
457 * We have to do this before calling join funcs in case the
458 * modeNode is manipulated (e.g. chanserv ops the user).
460 modeList_append(&channel->members, mNode);
461 modeList_append(&user->channels, mNode);
463 for (n=0; n<jf_used; n++) {
464 /* Callbacks return true if they kick or kill the user,
465 * and we can continue without removing mNode. */
466 if (jf_list[n](mNode))
471 irc_join(user, channel);
476 static part_func_t *pf_list;
477 static unsigned int pf_size = 0, pf_used = 0;
480 reg_part_func(part_func_t handler)
482 if (pf_used == pf_size) {
485 pf_list = realloc(pf_list, pf_size*sizeof(part_func_t));
488 pf_list = malloc(pf_size*sizeof(part_func_t));
491 pf_list[pf_used++] = handler;
495 unreg_part_func(part_func_t handler)
498 for (i=0; i<pf_used; i++)
499 if (pf_list[i] == handler)
503 memmove(pf_list+i, pf_list+i+1, (pf_used-i-1)*sizeof(pf_list[0]));
508 LockChannel(struct chanNode* channel)
514 UnlockChannel(struct chanNode *channel)
518 if (!--channel->locks && !channel->members.used)
523 DelChannelUser(struct userNode* user, struct chanNode* channel, const char *reason, int deleting)
525 struct modeNode* mNode;
529 irc_part(user, channel, reason);
532 mNode = GetUserMode(channel, user);
534 /* Sometimes we get a PART when the user has been KICKed.
535 * In this case, we get no usermode, and should not try to free it.
540 /* remove modeNode from channel and user */
541 modeList_remove(&channel->members, mNode);
542 modeList_remove(&user->channels, mNode);
545 for (n=0; n<pf_used; n++)
546 pf_list[n](user, channel, reason);
548 if (!deleting && !channel->members.used && !channel->locks)
553 KickChannelUser(struct userNode* target, struct chanNode* channel, struct userNode *kicker, const char *why)
555 if (!target || !channel || IsService(target) || !GetUserMode(channel, target))
557 /* don't remove them from the channel, since the server will send a PART */
558 irc_kick(kicker, target, channel, why);
562 /* NULL reason because we don't want a PART message to be
563 sent by DelChannelUser. */
564 DelChannelUser(target, channel, NULL, 0);
568 static kick_func_t *kf_list;
569 static unsigned int kf_size = 0, kf_used = 0;
572 reg_kick_func(kick_func_t handler)
574 if (kf_used == kf_size) {
577 kf_list = realloc(kf_list, kf_size*sizeof(kick_func_t));
580 kf_list = malloc(kf_size*sizeof(kick_func_t));
583 kf_list[kf_used++] = handler;
587 ChannelUserKicked(struct userNode* kicker, struct userNode* victim, struct chanNode* channel)
592 if (!victim || !channel || IsService(victim) || !GetUserMode(channel, victim))
595 /* Update the kicker's idle time (kicker may be null if it was a server) */
596 if (kicker && (mn = GetUserMode(channel, kicker)))
597 mn->idle_since = now;
599 for (n=0; n<kf_used; n++)
600 kf_list[n](kicker, victim, channel);
602 DelChannelUser(victim, channel, 0, 0);
605 irc_part(victim, channel, NULL);
608 int ChannelBanExists(struct chanNode *channel, const char *ban)
612 for (n = 0; n < channel->banlist.used; n++)
613 if (match_ircglobs(channel->banlist.list[n]->ban, ban))
618 static topic_func_t *tf_list;
619 static unsigned int tf_size = 0, tf_used = 0;
622 reg_topic_func(topic_func_t handler)
624 if (tf_used == tf_size) {
627 tf_list = realloc(tf_list, tf_size*sizeof(topic_func_t));
630 tf_list = malloc(tf_size*sizeof(topic_func_t));
633 tf_list[tf_used++] = handler;
637 SetChannelTopic(struct chanNode *channel, struct userNode *user, const char *topic, int announce)
641 char old_topic[TOPICLEN+1];
643 safestrncpy(old_topic, channel->topic, sizeof(old_topic));
644 safestrncpy(channel->topic, topic, sizeof(channel->topic));
645 channel->topic_time = now;
648 safestrncpy(channel->topic_nick, user->nick, sizeof(channel->topic_nick));
650 /* Update the setter's idle time */
651 if ((mn = GetUserMode(channel, user)))
652 mn->idle_since = now;
656 /* We don't really care if a local user messes with the topic,
657 * so don't call the tf_list functions. */
658 irc_topic(user, channel, topic);
660 for (n=0; n<tf_used; n++)
661 if (tf_list[n](user, channel, old_topic))
667 GetChannel(const char *name)
669 return dict_find(channels, name, NULL);
673 GetUserMode(struct chanNode *channel, struct userNode *user)
676 struct modeNode *mn = NULL;
677 if (channel->members.used < user->channels.used) {
678 for (n=0; n<channel->members.used; n++) {
679 if (user == channel->members.list[n]->user) {
680 mn = channel->members.list[n];
685 for (n=0; n<user->channels.used; n++) {
686 if (channel == user->channels.list[n]->channel) {
687 mn = user->channels.list[n];
695 DEFINE_LIST(userList, struct userNode*)
696 DEFINE_LIST(modeList, struct modeNode*)
697 DEFINE_LIST(banList, struct banNode*)
698 DEFINE_LIST(channelList, struct chanNode*)
699 DEFINE_LIST(serverList, struct server*)
704 DelServer(self, 0, NULL);
705 dict_delete(channels);
706 dict_delete(clients);
707 dict_delete(servers);
708 userList_clean(&curr_opers);