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