Fix registered channel desync; fix HelpServ crash for old cfg files
[srvx.git] / src / hash.c
1 /* hash.c - IRC network state database
2  * Copyright 2000-2004 srvx Development Team
3  *
4  * This file is part of srvx.
5  *
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.
10  *
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.
15  *
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.
19  */
20
21 #include "conf.h"
22 #include "global.h"
23 #include "hash.h"
24 #include "log.h"
25
26 struct server *self;
27 dict_t channels;
28 dict_t clients;
29 dict_t servers;
30 unsigned int max_clients, invis_clients;
31 time_t max_clients_time;
32 struct userList curr_opers;
33
34 static void hash_cleanup(void);
35
36 void init_structs(void)
37 {
38     channels = dict_new();
39     clients = dict_new();
40     servers = dict_new();
41     userList_init(&curr_opers);
42     reg_exit_func(hash_cleanup);
43 }
44
45 server_link_func_t *slf_list;
46 unsigned int slf_size = 0, slf_used = 0;
47
48 void
49 reg_server_link_func(server_link_func_t handler)
50 {
51     if (slf_used == slf_size) {
52         if (slf_size) {
53             slf_size <<= 1;
54             slf_list = realloc(slf_list, slf_size*sizeof(server_link_func_t));
55         } else {
56             slf_size = 8;
57             slf_list = malloc(slf_size*sizeof(server_link_func_t));
58         }
59     }
60     slf_list[slf_used++] = handler;
61 }
62
63 struct server*
64 GetServerH(const char *name)
65 {
66     return dict_find(servers, name, NULL);
67 }
68
69 new_user_func_t *nuf_list;
70 unsigned int nuf_size = 0, nuf_used = 0;
71
72 void
73 reg_new_user_func(new_user_func_t handler)
74 {
75     if (nuf_used == nuf_size) {
76         if (nuf_size) {
77             nuf_size <<= 1;
78             nuf_list = realloc(nuf_list, nuf_size*sizeof(new_user_func_t));
79         } else {
80             nuf_size = 8;
81             nuf_list = malloc(nuf_size*sizeof(new_user_func_t));
82         }
83     }
84     nuf_list[nuf_used++] = handler;
85 }
86
87 static nick_change_func_t *ncf2_list;
88 static unsigned int ncf2_size = 0, ncf2_used = 0;
89
90 void
91 reg_nick_change_func(nick_change_func_t handler)
92 {
93     if (ncf2_used == ncf2_size) {
94         if (ncf2_size) {
95             ncf2_size <<= 1;
96             ncf2_list = realloc(ncf2_list, ncf2_size*sizeof(nick_change_func_t));
97         } else {
98             ncf2_size = 8;
99             ncf2_list = malloc(ncf2_size*sizeof(nick_change_func_t));
100         }
101     }
102     ncf2_list[ncf2_used++] = handler;
103 }
104
105
106 del_user_func_t *duf_list;
107 unsigned int duf_size = 0, duf_used = 0;
108
109 void
110 reg_del_user_func(del_user_func_t handler)
111 {
112     if (duf_used == duf_size) {
113         if (duf_size) {
114             duf_size <<= 1;
115             duf_list = realloc(duf_list, duf_size*sizeof(del_user_func_t));
116         } else {
117             duf_size = 8;
118             duf_list = malloc(duf_size*sizeof(del_user_func_t));
119         }
120     }
121     duf_list[duf_used++] = handler;
122 }
123
124 void
125 unreg_del_user_func(del_user_func_t handler)
126 {
127     unsigned int i;
128     for (i=0; i<duf_used; i++) {
129         if (duf_list[i] == handler) break;
130     }
131     if (i == duf_used) return;
132     memmove(duf_list+i, duf_list+i+1, (duf_used-i-1)*sizeof(duf_list[0]));
133     duf_used--;
134 }
135
136 /* reintroduces a user after it has been killed. */
137 void
138 ReintroduceUser(struct userNode *user)
139 {
140     struct mod_chanmode change;
141     unsigned int n;
142         
143     irc_user(user);
144     mod_chanmode_init(&change);
145     change.argc = 1;
146     for (n = 0; n < user->channels.used; n++) {
147         struct modeNode *mn = user->channels.list[n];
148         irc_join(user, mn->channel);
149         if (mn->modes) {
150             change.args[0].mode = mn->modes;
151             change.args[0].u.member = mn;
152             mod_chanmode_announce(user, mn->channel, &change);
153         }
154     }
155 }
156
157 void
158 NickChange(struct userNode* user, const char *new_nick, int no_announce)
159 {
160     char *old_nick;
161     unsigned int nn;
162
163     /* don't do anything if there's no change */
164     old_nick = user->nick;
165     if (!strncmp(new_nick, old_nick, NICKLEN))
166         return;
167
168     /* remove old entry from clients dictionary */
169     dict_remove(clients, old_nick);
170 #if !defined(WITH_PROTOCOL_P10)
171     /* Remove from uplink's clients dict */
172     dict_remove(user->uplink->users, old_nick);
173 #endif
174     /* and reinsert */
175     user->nick = strdup(new_nick);
176     dict_insert(clients, user->nick, user);
177 #if !defined(WITH_PROTOCOL_P10)
178     dict_insert(user->uplink->users, user->nick, user);
179 #endif
180
181     /* Make callbacks for nick changes.  Do this with new nick in
182      * place because that is slightly more useful.
183      */
184     for (nn=0; nn<ncf2_used; nn++)
185         ncf2_list[nn](user, old_nick);
186     user->timestamp = now;
187     if (IsLocal(user) && !no_announce)
188         irc_nick(user, old_nick);
189     free(old_nick);
190 }
191
192 struct userNode *
193 GetUserH(const char *nick)
194 {
195     return dict_find(clients, nick, NULL);
196 }
197
198 static account_func_t account_func;
199
200 void
201 reg_account_func(account_func_t handler)
202 {
203     if (account_func) {
204         log_module(MAIN_LOG, LOG_WARNING, "Reregistering ACCOUNT handler.");
205     }
206     account_func = handler;
207 }
208
209 void
210 call_account_func(struct userNode *user, const char *stamp)
211 {
212     /* We've received an account stamp for a user; notify
213        NickServ, which registers the sole account_func
214        right now.
215
216        P10 Protocol violation if (user->modes & FLAGS_STAMPED) here.
217     */
218     if (account_func)
219         account_func(user, stamp);
220
221 #ifdef WITH_PROTOCOL_P10
222     /* Mark the user so we don't stamp it again. */
223     user->modes |= FLAGS_STAMPED;
224 #endif
225 }
226
227 void
228 StampUser(struct userNode *user, const char *stamp)
229 {
230 #ifdef WITH_PROTOCOL_P10
231     /* The P10 protocol says we can't stamp users who already
232        have a stamp. */
233     if (IsStamped(user))
234         return;
235 #endif
236
237     irc_account(user, stamp);
238     user->modes |= FLAGS_STAMPED;
239 }
240
241 void
242 assign_fakehost(struct userNode *user, const char *host, int announce)
243 {
244     safestrncpy(user->fakehost, host, sizeof(user->fakehost));
245     if (announce)
246         irc_fakehost(user, host);
247 }
248
249 static new_channel_func_t *ncf_list;
250 static unsigned int ncf_size = 0, ncf_used = 0;
251
252 void
253 reg_new_channel_func(new_channel_func_t handler)
254 {
255     if (ncf_used == ncf_size) {
256         if (ncf_size) {
257             ncf_size <<= 1;
258             ncf_list = realloc(ncf_list, ncf_size*sizeof(ncf_list[0]));
259         } else {
260             ncf_size = 8;
261             ncf_list = malloc(ncf_size*sizeof(ncf_list[0]));
262         }
263     }
264     ncf_list[ncf_used++] = handler;
265 }
266
267 static join_func_t *jf_list;
268 static unsigned int jf_size = 0, jf_used = 0;
269
270 void
271 reg_join_func(join_func_t handler)
272 {
273     if (jf_used == jf_size) {
274         if (jf_size) {
275             jf_size <<= 1;
276             jf_list = realloc(jf_list, jf_size*sizeof(join_func_t));
277         } else {
278             jf_size = 8;
279             jf_list = malloc(jf_size*sizeof(join_func_t));
280         }
281     }
282     jf_list[jf_used++] = handler;
283 }
284
285 int rel_age;
286
287 static void
288 wipeout_channel(struct chanNode *cNode, time_t new_time, char **modes, unsigned int modec) {
289     unsigned int orig_limit;
290     chan_mode_t orig_modes;
291     char orig_key[KEYLEN+1];
292     unsigned int nn, argc;
293
294     /* nuke old topic */
295     cNode->topic[0] = '\0';
296     cNode->topic_nick[0] = '\0';
297     cNode->topic_time = 0;
298
299     /* remember the old modes, and update them with the new */
300     orig_modes = cNode->modes;
301     orig_limit = cNode->limit;
302     strcpy(orig_key, cNode->key);
303     cNode->modes = 0;
304     mod_chanmode(NULL, cNode, modes, modec, 0);
305     cNode->timestamp = new_time;
306
307     /* remove our old ban list, replace it with the new one */
308     for (nn=0; nn<cNode->banlist.used; nn++)
309         free(cNode->banlist.list[nn]);
310     cNode->banlist.used = 0;
311
312     /* deop anybody in the channel now, but count services to reop */
313     for (nn=argc=0; nn<cNode->members.used; nn++) {
314         struct modeNode *mn = cNode->members.list[nn];
315         if ((mn->modes & MODE_CHANOP) && IsService(mn->user) && IsLocal(mn->user))
316             argc++;
317     }
318     if (argc) {
319         struct mod_chanmode *change;
320
321         change = mod_chanmode_alloc(argc);
322         change->modes_clear = 0;
323         change->modes_set = orig_modes;
324         change->new_limit = orig_limit;
325         strcpy(change->new_key, orig_key);
326         for (nn = argc = 0; nn < cNode->members.used; ++nn) {
327             struct modeNode *mn = cNode->members.list[nn];
328             if ((mn->modes & MODE_CHANOP) && IsService(mn->user) && IsLocal(mn->user)) {
329                 change->args[argc].mode = MODE_CHANOP;
330                 change->args[argc].u.member = mn;
331                 argc++;
332             }
333         }
334         assert(argc == change->argc);
335         change->args[0].u.member->modes &= ~MODE_CHANOP;
336         mod_chanmode_announce(change->args[0].u.member->user, cNode, change);
337         mod_chanmode_free(change);
338     }
339 }
340
341 struct chanNode *
342 AddChannel(const char *name, time_t time_, const char *modes, char *banlist)
343 {
344     struct chanNode *cNode;
345     char new_modes[MAXLEN], *argv[MAXNUMPARAMS];
346     unsigned int nn;
347
348     if (!IsChannelName(name)) {
349         log_module(MAIN_LOG, LOG_ERROR, "Somebody asked to add channel '%s', which isn't a channel name!", name);
350         return NULL;
351     }
352     if (!modes)
353         modes = "";
354
355     safestrncpy(new_modes, modes, sizeof(new_modes));
356     nn = split_line(new_modes, 0, ArrayLength(argv), argv);
357     if (!(cNode = GetChannel(name))) {
358         cNode = calloc(1, sizeof(*cNode) + strlen(name));
359         strcpy(cNode->name, name);
360         banList_init(&cNode->banlist);
361         modeList_init(&cNode->members);
362         mod_chanmode(NULL, cNode, argv, nn, 0);
363         dict_insert(channels, cNode->name, cNode);
364         cNode->timestamp = time_;
365         rel_age = 1;
366     } else if (cNode->timestamp > time_) {
367         wipeout_channel(cNode, time_, argv, nn);
368         rel_age = 1;
369     } else if (cNode->timestamp == time_) {
370         mod_chanmode(NULL, cNode, argv, nn, 0);
371         rel_age = 0;
372     } else {
373         rel_age = -1;
374     }
375
376     /* rel_age is the relative ages of our channel data versus what is
377      * in a BURST command.  1 means ours is younger, 0 means both are
378      * the same age, -1 means ours is older. */
379
380     /* if it's a new or updated channel, make callbacks */
381     if (rel_age > 0)
382         for (nn=0; nn<ncf_used; nn++)
383             ncf_list[nn](cNode);
384
385     /* go through list of bans and add each one */
386     if (banlist && (rel_age >= 0)) {
387         for (nn=0; banlist[nn];) {
388             char *ban = banlist + nn;
389             struct banNode *bn;
390             while (banlist[nn] != ' ' && banlist[nn])
391                 nn++;
392             while (banlist[nn] == ' ')
393                 banlist[nn++] = 0;
394             bn = calloc(1, sizeof(*bn));
395             safestrncpy(bn->ban, ban, sizeof(bn->ban));
396             safestrncpy(bn->who, "<unknown>", sizeof(bn->who));
397             bn->set = now;
398             banList_append(&cNode->banlist, bn);
399         }
400     }
401
402     return cNode;
403 }
404
405 static del_channel_func_t *dcf_list;
406 static unsigned int dcf_size = 0, dcf_used = 0;
407
408 void
409 reg_del_channel_func(del_channel_func_t handler)
410 {
411     if (dcf_used == dcf_size) {
412         if (dcf_size) {
413             dcf_size <<= 1;
414             dcf_list = realloc(dcf_list, dcf_size*sizeof(dcf_list[0]));
415         } else {
416             dcf_size = 8;
417             dcf_list = malloc(dcf_size*sizeof(dcf_list[0]));
418         }
419     }
420     dcf_list[dcf_used++] = handler;
421 }
422
423 static void
424 DelChannel(struct chanNode *channel)
425 {
426     unsigned int n;
427
428     verify(channel);
429     dict_remove(channels, channel->name);
430
431     if (channel->members.used || channel->locks) {
432         log_module(MAIN_LOG, LOG_ERROR, "Warning: deleting channel %s with %d users and %d locks remaining.", channel->name, channel->members.used, channel->locks);
433     }
434
435     /* go through all channel members and delete them from the channel */
436     for (n=channel->members.used; n>0; )
437         DelChannelUser(channel->members.list[--n]->user, channel, false, 1);
438
439     /* delete all channel bans */
440     for (n=channel->banlist.used; n>0; )
441         free(channel->banlist.list[--n]);
442     channel->banlist.used = 0;
443
444     for (n=0; n<dcf_used; n++)
445         dcf_list[n](channel);
446
447     modeList_clean(&channel->members);
448     banList_clean(&channel->banlist);
449     free(channel);
450 }
451
452 struct modeNode *
453 AddChannelUser(struct userNode *user, struct chanNode* channel)
454 {
455         struct modeNode *mNode;
456         unsigned int n;
457
458         mNode = GetUserMode(channel, user);
459         if (mNode)
460             return mNode;
461
462         mNode = malloc(sizeof(*mNode));
463
464         /* set up modeNode */
465         mNode->channel = channel;
466         mNode->user = user;
467         mNode->modes = 0;
468         mNode->idle_since = now;
469
470         /* Add modeNode to channel and to user.
471          * We have to do this before calling join funcs in case the
472          * modeNode is manipulated (e.g. chanserv ops the user).
473          */
474         modeList_append(&channel->members, mNode);
475         modeList_append(&user->channels, mNode);
476
477         if (channel->members.used == 1
478             && !(channel->modes & MODE_REGISTERED))
479             mNode->modes |= MODE_CHANOP;
480
481         for (n=0; n<jf_used; n++) {
482             /* Callbacks return true if they kick or kill the user,
483              * and we can continue without removing mNode. */
484             if (jf_list[n](mNode))
485                 return NULL;
486         }
487
488         if (IsLocal(user))
489             irc_join(user, channel);
490
491         return mNode;
492 }
493
494 static part_func_t *pf_list;
495 static unsigned int pf_size = 0, pf_used = 0;
496
497 void
498 reg_part_func(part_func_t handler)
499 {
500     if (pf_used == pf_size) {
501         if (pf_size) {
502             pf_size <<= 1;
503             pf_list = realloc(pf_list, pf_size*sizeof(part_func_t));
504         } else {
505             pf_size = 8;
506             pf_list = malloc(pf_size*sizeof(part_func_t));
507         }
508     }
509     pf_list[pf_used++] = handler;
510 }
511
512 void
513 unreg_part_func(part_func_t handler)
514 {
515     unsigned int i;
516     for (i=0; i<pf_used; i++)
517         if (pf_list[i] == handler)
518             break;
519     if (i == pf_used)
520         return;
521     memmove(pf_list+i, pf_list+i+1, (pf_used-i-1)*sizeof(pf_list[0]));
522     pf_used--;
523 }
524
525 void
526 LockChannel(struct chanNode* channel)
527 {
528     channel->locks++;
529 }
530
531 void
532 UnlockChannel(struct chanNode *channel)
533 {
534     assert(channel->locks > 0);
535     if (!--channel->locks && !channel->members.used)
536         DelChannel(channel);
537 }
538
539 void
540 DelChannelUser(struct userNode* user, struct chanNode* channel, const char *reason, int deleting)
541 {
542     struct modeNode* mNode;
543     unsigned int n;
544
545     if (reason)
546         irc_part(user, channel, reason);
547
548     mNode = GetUserMode(channel, user);
549
550     /* Sometimes we get a PART when the user has been KICKed.
551      * In this case, we get no usermode, and should not try to free it.
552      */
553     if (!mNode)
554         return;
555
556     /* remove modeNode from channel and user */
557     modeList_remove(&channel->members, mNode);
558     modeList_remove(&user->channels, mNode);
559
560     /* make callbacks */
561     for (n=0; n<pf_used; n++)
562         pf_list[n](mNode, reason);
563
564     /* free memory */
565     free(mNode);
566
567     if (!deleting && !channel->members.used && !channel->locks && !(channel->modes & MODE_REGISTERED))
568         DelChannel(channel);
569 }
570
571 void
572 KickChannelUser(struct userNode* target, struct chanNode* channel, struct userNode *kicker, const char *why)
573 {
574     if (!target || !channel || IsService(target) || !GetUserMode(channel, target))
575         return;
576     /* don't remove them from the channel, since the server will send a PART */
577     irc_kick(kicker, target, channel, why);
578
579     if (IsLocal(target))
580     {
581         /* NULL reason because we don't want a PART message to be
582            sent by DelChannelUser. */
583         DelChannelUser(target, channel, NULL, 0);
584     }
585 }
586
587 static kick_func_t *kf_list;
588 static unsigned int kf_size = 0, kf_used = 0;
589
590 void
591 reg_kick_func(kick_func_t handler)
592 {
593     if (kf_used == kf_size) {
594         if (kf_size) {
595             kf_size <<= 1;
596             kf_list = realloc(kf_list, kf_size*sizeof(kick_func_t));
597         } else {
598             kf_size = 8;
599             kf_list = malloc(kf_size*sizeof(kick_func_t));
600         }
601     }
602     kf_list[kf_used++] = handler;
603 }
604
605 void
606 ChannelUserKicked(struct userNode* kicker, struct userNode* victim, struct chanNode* channel)
607 {
608     unsigned int n;
609     struct modeNode *mn;
610
611     if (!victim || !channel || IsService(victim) || !GetUserMode(channel, victim))
612         return;
613
614     /* Update the kicker's idle time (kicker may be null if it was a server) */
615     if (kicker && (mn = GetUserMode(channel, kicker)))
616         mn->idle_since = now;
617
618     for (n=0; n<kf_used; n++)
619         kf_list[n](kicker, victim, channel);
620
621     DelChannelUser(victim, channel, 0, 0);
622
623     if (IsLocal(victim))
624         irc_part(victim, channel, NULL);
625 }
626
627 int ChannelBanExists(struct chanNode *channel, const char *ban)
628 {
629     unsigned int n;
630
631     for (n = 0; n < channel->banlist.used; n++)
632         if (match_ircglobs(channel->banlist.list[n]->ban, ban))
633             return 1;
634     return 0;
635 }
636
637 static topic_func_t *tf_list;
638 static unsigned int tf_size = 0, tf_used = 0;
639
640 void
641 reg_topic_func(topic_func_t handler)
642 {
643     if (tf_used == tf_size) {
644         if (tf_size) {
645             tf_size <<= 1;
646             tf_list = realloc(tf_list, tf_size*sizeof(topic_func_t));
647         } else {
648             tf_size = 8;
649             tf_list = malloc(tf_size*sizeof(topic_func_t));
650         }
651     }
652     tf_list[tf_used++] = handler;
653 }
654
655 void
656 SetChannelTopic(struct chanNode *channel, struct userNode *user, const char *topic, int announce)
657 {
658     unsigned int n;
659     struct modeNode *mn;
660     char old_topic[TOPICLEN+1];
661
662     safestrncpy(old_topic, channel->topic, sizeof(old_topic));
663     safestrncpy(channel->topic, topic, sizeof(channel->topic));
664     channel->topic_time = now;
665
666     if (user) {
667         safestrncpy(channel->topic_nick, user->nick, sizeof(channel->topic_nick));
668
669         /* Update the setter's idle time */
670         if ((mn = GetUserMode(channel, user)))
671             mn->idle_since = now;
672     }
673
674     if (announce) {
675         /* We don't really care if a local user messes with the topic,
676          * so don't call the tf_list functions. */
677         irc_topic(user, channel, topic);
678     } else {
679         for (n=0; n<tf_used; n++)
680             if (tf_list[n](user, channel, old_topic))
681                 break;
682     }
683 }
684
685 struct chanNode *
686 GetChannel(const char *name)
687 {
688     return dict_find(channels, name, NULL);
689 }
690
691 struct modeNode *
692 GetUserMode(struct chanNode *channel, struct userNode *user)
693 {
694     unsigned int n;
695     struct modeNode *mn = NULL;
696
697     verify(channel);
698     verify(channel->members.list);
699     verify(user);
700     verify(user->channels.list);
701     if (channel->members.used < user->channels.used) {
702         for (n=0; n<channel->members.used; n++) {
703             verify(channel->members.list[n]);
704             if (user == channel->members.list[n]->user) {
705                 mn = channel->members.list[n];
706                 break;
707             }
708         }
709     } else {
710         for (n=0; n<user->channels.used; n++) {
711             verify(user->channels.list[n]);
712             if (channel == user->channels.list[n]->channel) {
713                 mn = user->channels.list[n];
714                 break;
715             }
716         }
717     }
718     return mn;
719 }
720
721 DEFINE_LIST(userList, struct userNode*)
722 DEFINE_LIST(modeList, struct modeNode*)
723 DEFINE_LIST(banList, struct banNode*)
724 DEFINE_LIST(channelList, struct chanNode*)
725 DEFINE_LIST(serverList, struct server*)
726
727 static void
728 hash_cleanup(void)
729 {
730     dict_iterator_t it, next;
731
732     DelServer(self, 0, NULL);
733     for (it = dict_first(channels); it; it = next) {
734         next = iter_next(it);
735         DelChannel(iter_data(it));
736     }
737     dict_delete(channels);
738     dict_delete(clients);
739     dict_delete(servers);
740     userList_clean(&curr_opers);
741
742     free(slf_list);
743     free(nuf_list);
744     free(ncf2_list);
745     free(duf_list);
746     free(ncf_list);
747     free(jf_list);
748     free(dcf_list);
749     free(pf_list);
750     free(kf_list);
751     free(tf_list);
752 }