Initial import (again)
[srvx.git] / src / hash.c
1 /* hash.c - IRC network state database
2  * Copyright 2000-2004 srvx Development Team
3  *
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.
9  *
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.
14  *
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.
17  */
18
19 #include "conf.h"
20 #include "global.h"
21 #include "hash.h"
22 #include "log.h"
23
24 struct server *self;
25 dict_t channels;
26 dict_t clients;
27 dict_t servers;
28 unsigned int max_clients, invis_clients;
29 time_t max_clients_time;
30 struct userList curr_opers;
31
32 static void hash_cleanup(void);
33
34 void init_structs(void)
35 {
36     channels = dict_new();
37     clients = dict_new();
38     servers = dict_new();
39     userList_init(&curr_opers);
40     reg_exit_func(hash_cleanup);
41 }
42
43 server_link_func_t *slf_list;
44 unsigned int slf_size = 0, slf_used = 0;
45
46 void
47 reg_server_link_func(server_link_func_t handler)
48 {
49     if (slf_used == slf_size) {
50         if (slf_size) {
51             slf_size <<= 1;
52             slf_list = realloc(slf_list, slf_size*sizeof(server_link_func_t));
53         } else {
54             slf_size = 8;
55             slf_list = malloc(slf_size*sizeof(server_link_func_t));
56         }
57     }
58     slf_list[slf_used++] = handler;
59 }
60
61 struct server*
62 GetServerH(const char *name)
63 {
64     return dict_find(servers, name, NULL);
65 }
66
67 new_user_func_t *nuf_list;
68 unsigned int nuf_size = 0, nuf_used = 0;
69
70 void
71 reg_new_user_func(new_user_func_t handler)
72 {
73     if (nuf_used == nuf_size) {
74         if (nuf_size) {
75             nuf_size <<= 1;
76             nuf_list = realloc(nuf_list, nuf_size*sizeof(new_user_func_t));
77         } else {
78             nuf_size = 8;
79             nuf_list = malloc(nuf_size*sizeof(new_user_func_t));
80         }
81     }
82     nuf_list[nuf_used++] = handler;
83 }
84
85 static nick_change_func_t *ncf2_list;
86 static unsigned int ncf2_size = 0, ncf2_used = 0;
87
88 void
89 reg_nick_change_func(nick_change_func_t handler)
90 {
91     if (ncf2_used == ncf2_size) {
92         if (ncf2_size) {
93             ncf2_size <<= 1;
94             ncf2_list = realloc(ncf2_list, ncf2_size*sizeof(nick_change_func_t));
95         } else {
96             ncf2_size = 8;
97             ncf2_list = malloc(ncf2_size*sizeof(nick_change_func_t));
98         }
99     }
100     ncf2_list[ncf2_used++] = handler;
101 }
102
103
104 del_user_func_t *duf_list;
105 unsigned int duf_size = 0, duf_used = 0;
106
107 void
108 reg_del_user_func(del_user_func_t handler)
109 {
110     if (duf_used == duf_size) {
111         if (duf_size) {
112             duf_size <<= 1;
113             duf_list = realloc(duf_list, duf_size*sizeof(del_user_func_t));
114         } else {
115             duf_size = 8;
116             duf_list = malloc(duf_size*sizeof(del_user_func_t));
117         }
118     }
119     duf_list[duf_used++] = handler;
120 }
121
122 void
123 unreg_del_user_func(del_user_func_t handler)
124 {
125     unsigned int i;
126     for (i=0; i<duf_used; i++) {
127         if (duf_list[i] == handler) break;
128     }
129     if (i == duf_used) return;
130     memmove(duf_list+i, duf_list+i+1, (duf_used-i-1)*sizeof(duf_list[0]));
131     duf_used--;
132 }
133
134 /* reintroduces a user after it has been killed. */
135 void
136 ReintroduceUser(struct userNode *user)
137 {
138     struct mod_chanmode change;
139     unsigned int n;
140         
141     irc_user(user);
142     change.modes_set = change.modes_clear = 0;
143     change.argc = 1;
144     for (n = 0; n < user->channels.used; n++) {
145         struct modeNode *mn = user->channels.list[n];
146         irc_join(user, mn->channel);
147         if (mn->modes) {
148             change.args[0].mode = mn->modes;
149             change.args[0].member = mn;
150             mod_chanmode_announce(user, mn->channel, &change);
151         }
152     }
153 }
154
155 void
156 NickChange(struct userNode* user, const char *new_nick, int no_announce)
157 {
158     char *old_nick;
159     unsigned int nn;
160
161     /* don't do anything if there's no change */
162     old_nick = user->nick;
163     if (!strncmp(new_nick, old_nick, NICKLEN))
164         return;
165
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);
171 #endif
172     /* and reinsert */
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);
177 #endif
178
179     /* Make callbacks for nick changes.  Do this with new nick in
180      * place because that is slightly more useful.
181      */
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);
187     free(old_nick);
188 }
189
190 struct userNode *
191 GetUserH(const char *nick)
192 {
193     return dict_find(clients, nick, NULL);
194 }
195
196 static account_func_t account_func;
197
198 void
199 reg_account_func(account_func_t handler)
200 {
201     if (account_func) {
202         log_module(MAIN_LOG, LOG_WARNING, "Reregistering ACCOUNT handler.");
203     }
204     account_func = handler;
205 }
206
207 void
208 call_account_func(struct userNode *user, const char *stamp)
209 {
210     /* We've received an account stamp for a user; notify
211        NickServ, which registers the sole account_func
212        right now.
213
214        P10 Protocol violation if (user->modes & FLAGS_STAMPED) here.
215     */
216     if (account_func)
217         account_func(user, stamp);
218
219 #ifdef WITH_PROTOCOL_P10
220     /* Mark the user so we don't stamp it again. */
221     user->modes |= FLAGS_STAMPED;
222 #endif
223 }
224
225 void
226 StampUser(struct userNode *user, const char *stamp)
227 {
228 #ifdef WITH_PROTOCOL_P10
229     /* The P10 protocol says we can't stamp users who already
230        have a stamp. */
231     if (IsStamped(user))
232         return;
233 #endif
234
235     irc_account(user, stamp);
236     user->modes |= FLAGS_STAMPED;
237 }
238
239 static new_channel_func_t *ncf_list;
240 static unsigned int ncf_size = 0, ncf_used = 0;
241
242 void
243 reg_new_channel_func(new_channel_func_t handler)
244 {
245     if (ncf_used == ncf_size) {
246         if (ncf_size) {
247             ncf_size <<= 1;
248             ncf_list = realloc(ncf_list, ncf_size*sizeof(ncf_list[0]));
249         } else {
250             ncf_size = 8;
251             ncf_list = malloc(ncf_size*sizeof(ncf_list[0]));
252         }
253     }
254     ncf_list[ncf_used++] = handler;
255 }
256
257 static join_func_t *jf_list;
258 static unsigned int jf_size = 0, jf_used = 0;
259
260 void
261 reg_join_func(join_func_t handler)
262 {
263     if (jf_used == jf_size) {
264         if (jf_size) {
265             jf_size <<= 1;
266             jf_list = realloc(jf_list, jf_size*sizeof(join_func_t));
267         } else {
268             jf_size = 8;
269             jf_list = malloc(jf_size*sizeof(join_func_t));
270         }
271     }
272     jf_list[jf_used++] = handler;
273 }
274
275 int rel_age;
276
277 static void
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;
283
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);
288     cNode->modes = 0;
289     mod_chanmode(NULL, cNode, modes, modec, 0);
290     cNode->timestamp = new_time;
291
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;
296
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))
301             argc++;
302     }
303     if (argc) {
304         extern struct userNode *opserv;
305         struct mod_chanmode *change;
306
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;
317                 argc++;
318             }
319         }
320         if (change->argc > 0)
321             mod_chanmode_announce(change->args[0].member->user, cNode, change);
322         else
323             mod_chanmode_announce(opserv, cNode, change);
324         mod_chanmode_free(change);
325     }
326 }
327
328 struct chanNode *
329 AddChannel(const char *name, time_t time_, const char *modes, char *banlist)
330 {
331     struct chanNode *cNode;
332     char new_modes[MAXLEN], *argv[MAXNUMPARAMS];
333     unsigned int nn;
334
335     if (!IsChannelName(name)) {
336         log_module(MAIN_LOG, LOG_ERROR, "Somebody asked to add channel '%s', which isn't a channel name!", name);
337         return NULL;
338     }
339     if (!modes)
340         modes = "";
341
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_;
352         rel_age = 1;
353     } else if (cNode->timestamp > time_) {
354         wipeout_channel(cNode, time_, argv, nn);
355         rel_age = 1;
356     } else if (cNode->timestamp == time_) {
357         mod_chanmode(NULL, cNode, argv, nn, 0);
358         rel_age = 0;
359     } else {
360         rel_age = -1;
361     }
362
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. */
366
367     /* if it's a new or updated channel, make callbacks */
368     if (rel_age > 0)
369         for (nn=0; nn<ncf_used; nn++)
370             ncf_list[nn](cNode);
371
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;
376             struct banNode *bn;
377             while (banlist[nn] != ' ' && banlist[nn])
378                 nn++;
379             while (banlist[nn] == ' ')
380                 banlist[nn++] = 0;
381             bn = calloc(1, sizeof(*bn));
382             safestrncpy(bn->ban, ban, sizeof(bn->ban));
383             safestrncpy(bn->who, "<unknown>", sizeof(bn->who));
384             bn->set = now;
385             banList_append(&cNode->banlist, bn);
386         }
387     }
388
389     return cNode;
390 }
391
392 static del_channel_func_t *dcf_list;
393 static unsigned int dcf_size = 0, dcf_used = 0;
394
395 void
396 reg_del_channel_func(del_channel_func_t handler)
397 {
398     if (dcf_used == dcf_size) {
399         if (dcf_size) {
400             dcf_size <<= 1;
401             dcf_list = realloc(dcf_list, dcf_size*sizeof(dcf_list[0]));
402         } else {
403             dcf_size = 8;
404             dcf_list = malloc(dcf_size*sizeof(dcf_list[0]));
405         }
406     }
407     dcf_list[dcf_used++] = handler;
408 }
409
410 static void
411 DelChannel(struct chanNode *channel)
412 {
413     unsigned int n;
414
415     dict_remove(channels, channel->name);
416
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);
419     }
420
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);
424
425     /* delete all channel bans */
426     for (n=channel->banlist.used; n>0; )
427         free(channel->banlist.list[--n]);
428     channel->banlist.used = 0;
429
430     for (n=0; n<dcf_used; n++)
431         dcf_list[n](channel);
432
433     modeList_clean(&channel->members);
434     banList_clean(&channel->banlist);
435     free(channel);
436 }
437
438 struct modeNode *
439 AddChannelUser(struct userNode *user, struct chanNode* channel)
440 {
441         struct modeNode *mNode;
442         unsigned int n;
443
444         mNode = GetUserMode(channel, user);
445         if (mNode)
446             return mNode;
447
448         mNode = malloc(sizeof(*mNode));
449
450         /* set up modeNode */
451         mNode->channel = channel;
452         mNode->user = user;
453         mNode->modes = 0;
454         mNode->idle_since = now;
455
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).
459          */
460         modeList_append(&channel->members, mNode);
461         modeList_append(&user->channels, mNode);
462
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))
467                 return NULL;
468         }
469
470         if (IsLocal(user))
471             irc_join(user, channel);
472
473         return mNode;
474 }
475
476 static part_func_t *pf_list;
477 static unsigned int pf_size = 0, pf_used = 0;
478
479 void
480 reg_part_func(part_func_t handler)
481 {
482     if (pf_used == pf_size) {
483         if (pf_size) {
484             pf_size <<= 1;
485             pf_list = realloc(pf_list, pf_size*sizeof(part_func_t));
486         } else {
487             pf_size = 8;
488             pf_list = malloc(pf_size*sizeof(part_func_t));
489         }
490     }
491     pf_list[pf_used++] = handler;
492 }
493
494 void
495 unreg_part_func(part_func_t handler)
496 {
497     unsigned int i;
498     for (i=0; i<pf_used; i++)
499         if (pf_list[i] == handler)
500             break;
501     if (i == pf_used)
502         return;
503     memmove(pf_list+i, pf_list+i+1, (pf_used-i-1)*sizeof(pf_list[0]));
504     pf_used--;
505 }
506
507 void
508 LockChannel(struct chanNode* channel)
509 {
510     channel->locks++;
511 }
512
513 void
514 UnlockChannel(struct chanNode *channel)
515 {
516     if (!channel->locks)
517         return;
518     if (!--channel->locks && !channel->members.used)
519         DelChannel(channel);
520 }
521
522 void
523 DelChannelUser(struct userNode* user, struct chanNode* channel, const char *reason, int deleting)
524 {
525     struct modeNode* mNode;
526     unsigned int n;
527
528     if (reason) {
529         irc_part(user, channel, reason);
530     }
531
532     mNode = GetUserMode(channel, user);
533
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.
536      */
537     if (!mNode)
538         return;
539
540     /* remove modeNode from channel and user */
541     modeList_remove(&channel->members, mNode);
542     modeList_remove(&user->channels, mNode);
543     free(mNode);
544
545     for (n=0; n<pf_used; n++)
546         pf_list[n](user, channel, reason);
547
548     if (!deleting && !channel->members.used && !channel->locks)
549         DelChannel(channel);
550 }
551
552 void
553 KickChannelUser(struct userNode* target, struct chanNode* channel, struct userNode *kicker, const char *why)
554 {
555     if (!target || !channel || IsService(target) || !GetUserMode(channel, target))
556         return;
557     /* don't remove them from the channel, since the server will send a PART */
558     irc_kick(kicker, target, channel, why);
559
560     if (IsLocal(target))
561     {
562         /* NULL reason because we don't want a PART message to be
563            sent by DelChannelUser. */
564         DelChannelUser(target, channel, NULL, 0);
565     }
566 }
567
568 static kick_func_t *kf_list;
569 static unsigned int kf_size = 0, kf_used = 0;
570
571 void
572 reg_kick_func(kick_func_t handler)
573 {
574     if (kf_used == kf_size) {
575         if (kf_size) {
576             kf_size <<= 1;
577             kf_list = realloc(kf_list, kf_size*sizeof(kick_func_t));
578         } else {
579             kf_size = 8;
580             kf_list = malloc(kf_size*sizeof(kick_func_t));
581         }
582     }
583     kf_list[kf_used++] = handler;
584 }
585
586 void
587 ChannelUserKicked(struct userNode* kicker, struct userNode* victim, struct chanNode* channel)
588 {
589     unsigned int n;
590     struct modeNode *mn;
591
592     if (!victim || !channel || IsService(victim) || !GetUserMode(channel, victim))
593         return;
594
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;
598
599     for (n=0; n<kf_used; n++)
600         kf_list[n](kicker, victim, channel);
601
602     DelChannelUser(victim, channel, 0, 0);
603
604     if (IsLocal(victim))
605         irc_part(victim, channel, NULL);
606 }
607
608 int ChannelBanExists(struct chanNode *channel, const char *ban)
609 {
610     unsigned int n;
611
612     for (n = 0; n < channel->banlist.used; n++)
613         if (match_ircglobs(channel->banlist.list[n]->ban, ban))
614             return 1;
615     return 0;
616 }
617
618 static topic_func_t *tf_list;
619 static unsigned int tf_size = 0, tf_used = 0;
620
621 void
622 reg_topic_func(topic_func_t handler)
623 {
624     if (tf_used == tf_size) {
625         if (tf_size) {
626             tf_size <<= 1;
627             tf_list = realloc(tf_list, tf_size*sizeof(topic_func_t));
628         } else {
629             tf_size = 8;
630             tf_list = malloc(tf_size*sizeof(topic_func_t));
631         }
632     }
633     tf_list[tf_used++] = handler;
634 }
635
636 void
637 SetChannelTopic(struct chanNode *channel, struct userNode *user, const char *topic, int announce)
638 {
639     unsigned int n;
640     struct modeNode *mn;
641     char old_topic[TOPICLEN+1];
642
643     safestrncpy(old_topic, channel->topic, sizeof(old_topic));
644     safestrncpy(channel->topic, topic, sizeof(channel->topic));
645     channel->topic_time = now;
646
647     if (user) {
648         safestrncpy(channel->topic_nick, user->nick, sizeof(channel->topic_nick));
649
650         /* Update the setter's idle time */
651         if ((mn = GetUserMode(channel, user)))
652             mn->idle_since = now;
653     }
654
655     if (announce) {
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);
659     } else {
660         for (n=0; n<tf_used; n++)
661             if (tf_list[n](user, channel, old_topic))
662                 break;
663     }
664 }
665
666 struct chanNode *
667 GetChannel(const char *name)
668 {
669     return dict_find(channels, name, NULL);
670 }
671
672 struct modeNode *
673 GetUserMode(struct chanNode *channel, struct userNode *user)
674 {
675     unsigned int n;
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];
681                 break;
682             }
683         }
684     } else {
685         for (n=0; n<user->channels.used; n++) {
686             if (channel == user->channels.list[n]->channel) {
687                 mn = user->channels.list[n];
688                 break;
689             }
690         }
691     }
692     return mn;
693 }
694
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*)
700
701 static void
702 hash_cleanup(void)
703 {
704     DelServer(self, 0, NULL);
705     dict_delete(channels);
706     dict_delete(clients);
707     dict_delete(servers);
708     userList_clean(&curr_opers);
709
710     free(slf_list);
711     free(nuf_list);
712     free(ncf2_list);
713     free(duf_list);
714     free(ncf_list);
715     free(jf_list);
716     free(dcf_list);
717     free(pf_list);
718     free(kf_list);
719     free(tf_list);
720 }