Fix several bugs; make off-channel a per-channel option
[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].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 static new_channel_func_t *ncf_list;
242 static unsigned int ncf_size = 0, ncf_used = 0;
243
244 void
245 reg_new_channel_func(new_channel_func_t handler)
246 {
247     if (ncf_used == ncf_size) {
248         if (ncf_size) {
249             ncf_size <<= 1;
250             ncf_list = realloc(ncf_list, ncf_size*sizeof(ncf_list[0]));
251         } else {
252             ncf_size = 8;
253             ncf_list = malloc(ncf_size*sizeof(ncf_list[0]));
254         }
255     }
256     ncf_list[ncf_used++] = handler;
257 }
258
259 static join_func_t *jf_list;
260 static unsigned int jf_size = 0, jf_used = 0;
261
262 void
263 reg_join_func(join_func_t handler)
264 {
265     if (jf_used == jf_size) {
266         if (jf_size) {
267             jf_size <<= 1;
268             jf_list = realloc(jf_list, jf_size*sizeof(join_func_t));
269         } else {
270             jf_size = 8;
271             jf_list = malloc(jf_size*sizeof(join_func_t));
272         }
273     }
274     jf_list[jf_used++] = handler;
275 }
276
277 int rel_age;
278
279 static void
280 wipeout_channel(struct chanNode *cNode, time_t new_time, char **modes, unsigned int modec) {
281     unsigned int orig_limit;
282     chan_mode_t orig_modes;
283     char orig_key[KEYLEN+1];
284     unsigned int nn, argc;
285
286     /* nuke old topic */
287     cNode->topic[0] = '\0';
288     cNode->topic_nick[0] = '\0';
289     cNode->topic_time = 0;
290
291     /* remember the old modes, and update them with the new */
292     orig_modes = cNode->modes;
293     orig_limit = cNode->limit;
294     strcpy(orig_key, cNode->key);
295     cNode->modes = 0;
296     mod_chanmode(NULL, cNode, modes, modec, 0);
297     cNode->timestamp = new_time;
298
299     /* remove our old ban list, replace it with the new one */
300     for (nn=0; nn<cNode->banlist.used; nn++)
301         free(cNode->banlist.list[nn]);
302     cNode->banlist.used = 0;
303
304     /* deop anybody in the channel now, but count services to reop */
305     for (nn=argc=0; nn<cNode->members.used; nn++) {
306         struct modeNode *mn = cNode->members.list[nn];
307         if ((mn->modes & MODE_CHANOP) && IsService(mn->user) && IsLocal(mn->user))
308             argc++;
309     }
310     if (argc) {
311         struct mod_chanmode *change;
312
313         change = mod_chanmode_alloc(argc);
314         change->modes_clear = 0;
315         change->modes_set = orig_modes;
316         change->new_limit = orig_limit;
317         strcpy(change->new_key, orig_key);
318         for (nn = argc = 0; nn < cNode->members.used; ++nn) {
319             struct modeNode *mn = cNode->members.list[nn];
320             if ((mn->modes & MODE_CHANOP) && IsService(mn->user) && IsLocal(mn->user)) {
321                 change->args[argc].mode = MODE_CHANOP;
322                 change->args[argc].member = mn;
323                 argc++;
324             }
325         }
326         assert(argc == change->argc);
327         change->args[0].member->modes &= ~MODE_CHANOP;
328         mod_chanmode_announce(change->args[0].member->user, cNode, change);
329         mod_chanmode_free(change);
330     }
331 }
332
333 struct chanNode *
334 AddChannel(const char *name, time_t time_, const char *modes, char *banlist)
335 {
336     struct chanNode *cNode;
337     char new_modes[MAXLEN], *argv[MAXNUMPARAMS];
338     unsigned int nn;
339
340     if (!IsChannelName(name)) {
341         log_module(MAIN_LOG, LOG_ERROR, "Somebody asked to add channel '%s', which isn't a channel name!", name);
342         return NULL;
343     }
344     if (!modes)
345         modes = "";
346
347     safestrncpy(new_modes, modes, sizeof(new_modes));
348     nn = split_line(new_modes, 0, ArrayLength(argv), argv);
349     if (!(cNode = GetChannel(name))) {
350         cNode = calloc(1, sizeof(*cNode) + strlen(name));
351         strcpy(cNode->name, name);
352         banList_init(&cNode->banlist);
353         modeList_init(&cNode->members);
354         mod_chanmode(NULL, cNode, argv, nn, 0);
355         dict_insert(channels, cNode->name, cNode);
356         cNode->timestamp = time_;
357         rel_age = 1;
358     } else if (cNode->timestamp > time_) {
359         wipeout_channel(cNode, time_, argv, nn);
360         rel_age = 1;
361     } else if (cNode->timestamp == time_) {
362         mod_chanmode(NULL, cNode, argv, nn, 0);
363         rel_age = 0;
364     } else {
365         rel_age = -1;
366     }
367
368     /* rel_age is the relative ages of our channel data versus what is
369      * in a BURST command.  1 means ours is younger, 0 means both are
370      * the same age, -1 means ours is older. */
371
372     /* if it's a new or updated channel, make callbacks */
373     if (rel_age > 0)
374         for (nn=0; nn<ncf_used; nn++)
375             ncf_list[nn](cNode);
376
377     /* go through list of bans and add each one */
378     if (banlist && (rel_age >= 0)) {
379         for (nn=0; banlist[nn];) {
380             char *ban = banlist + nn;
381             struct banNode *bn;
382             while (banlist[nn] != ' ' && banlist[nn])
383                 nn++;
384             while (banlist[nn] == ' ')
385                 banlist[nn++] = 0;
386             bn = calloc(1, sizeof(*bn));
387             safestrncpy(bn->ban, ban, sizeof(bn->ban));
388             safestrncpy(bn->who, "<unknown>", sizeof(bn->who));
389             bn->set = now;
390             banList_append(&cNode->banlist, bn);
391         }
392     }
393
394     return cNode;
395 }
396
397 static del_channel_func_t *dcf_list;
398 static unsigned int dcf_size = 0, dcf_used = 0;
399
400 void
401 reg_del_channel_func(del_channel_func_t handler)
402 {
403     if (dcf_used == dcf_size) {
404         if (dcf_size) {
405             dcf_size <<= 1;
406             dcf_list = realloc(dcf_list, dcf_size*sizeof(dcf_list[0]));
407         } else {
408             dcf_size = 8;
409             dcf_list = malloc(dcf_size*sizeof(dcf_list[0]));
410         }
411     }
412     dcf_list[dcf_used++] = handler;
413 }
414
415 static void
416 DelChannel(struct chanNode *channel)
417 {
418     unsigned int n;
419
420     dict_remove(channels, channel->name);
421
422     if (channel->members.used || channel->locks) {
423         log_module(MAIN_LOG, LOG_ERROR, "Warning: deleting channel %s with %d users and %d locks remaining.", channel->name, channel->members.used, channel->locks);
424     }
425
426     /* go through all channel members and delete them from the channel */
427     for (n=channel->members.used; n>0; )
428         DelChannelUser(channel->members.list[--n]->user, channel, false, 1);
429
430     /* delete all channel bans */
431     for (n=channel->banlist.used; n>0; )
432         free(channel->banlist.list[--n]);
433     channel->banlist.used = 0;
434
435     for (n=0; n<dcf_used; n++)
436         dcf_list[n](channel);
437
438     modeList_clean(&channel->members);
439     banList_clean(&channel->banlist);
440     free(channel);
441 }
442
443 struct modeNode *
444 AddChannelUser(struct userNode *user, struct chanNode* channel)
445 {
446         struct modeNode *mNode;
447         unsigned int n;
448
449         mNode = GetUserMode(channel, user);
450         if (mNode)
451             return mNode;
452
453         mNode = malloc(sizeof(*mNode));
454
455         /* set up modeNode */
456         mNode->channel = channel;
457         mNode->user = user;
458         mNode->modes = 0;
459         mNode->idle_since = now;
460
461         /* Add modeNode to channel and to user.
462          * We have to do this before calling join funcs in case the
463          * modeNode is manipulated (e.g. chanserv ops the user).
464          */
465         modeList_append(&channel->members, mNode);
466         modeList_append(&user->channels, mNode);
467
468         if (channel->members.used == 1)
469             mNode->modes |= MODE_CHANOP;
470
471         for (n=0; n<jf_used; n++) {
472             /* Callbacks return true if they kick or kill the user,
473              * and we can continue without removing mNode. */
474             if (jf_list[n](mNode))
475                 return NULL;
476         }
477
478         if (IsLocal(user))
479             irc_join(user, channel);
480
481         return mNode;
482 }
483
484 static part_func_t *pf_list;
485 static unsigned int pf_size = 0, pf_used = 0;
486
487 void
488 reg_part_func(part_func_t handler)
489 {
490     if (pf_used == pf_size) {
491         if (pf_size) {
492             pf_size <<= 1;
493             pf_list = realloc(pf_list, pf_size*sizeof(part_func_t));
494         } else {
495             pf_size = 8;
496             pf_list = malloc(pf_size*sizeof(part_func_t));
497         }
498     }
499     pf_list[pf_used++] = handler;
500 }
501
502 void
503 unreg_part_func(part_func_t handler)
504 {
505     unsigned int i;
506     for (i=0; i<pf_used; i++)
507         if (pf_list[i] == handler)
508             break;
509     if (i == pf_used)
510         return;
511     memmove(pf_list+i, pf_list+i+1, (pf_used-i-1)*sizeof(pf_list[0]));
512     pf_used--;
513 }
514
515 void
516 LockChannel(struct chanNode* channel)
517 {
518     channel->locks++;
519 }
520
521 void
522 UnlockChannel(struct chanNode *channel)
523 {
524     assert(channel->locks > 0);
525     if (!--channel->locks && !channel->members.used)
526         DelChannel(channel);
527 }
528
529 void
530 DelChannelUser(struct userNode* user, struct chanNode* channel, const char *reason, int deleting)
531 {
532     struct modeNode* mNode;
533     unsigned int n;
534
535     if (reason)
536         irc_part(user, channel, reason);
537
538     mNode = GetUserMode(channel, user);
539
540     /* Sometimes we get a PART when the user has been KICKed.
541      * In this case, we get no usermode, and should not try to free it.
542      */
543     if (!mNode)
544         return;
545
546     for (n=0; n<pf_used; n++)
547         pf_list[n](mNode, reason);
548
549     /* remove modeNode from channel and user */
550     modeList_remove(&channel->members, mNode);
551     modeList_remove(&user->channels, mNode);
552     free(mNode);
553
554     if (!deleting && !channel->members.used && !channel->locks && !(channel->modes & MODE_REGISTERED))
555         DelChannel(channel);
556 }
557
558 void
559 KickChannelUser(struct userNode* target, struct chanNode* channel, struct userNode *kicker, const char *why)
560 {
561     if (!target || !channel || IsService(target) || !GetUserMode(channel, target))
562         return;
563     /* don't remove them from the channel, since the server will send a PART */
564     irc_kick(kicker, target, channel, why);
565
566     if (IsLocal(target))
567     {
568         /* NULL reason because we don't want a PART message to be
569            sent by DelChannelUser. */
570         DelChannelUser(target, channel, NULL, 0);
571     }
572 }
573
574 static kick_func_t *kf_list;
575 static unsigned int kf_size = 0, kf_used = 0;
576
577 void
578 reg_kick_func(kick_func_t handler)
579 {
580     if (kf_used == kf_size) {
581         if (kf_size) {
582             kf_size <<= 1;
583             kf_list = realloc(kf_list, kf_size*sizeof(kick_func_t));
584         } else {
585             kf_size = 8;
586             kf_list = malloc(kf_size*sizeof(kick_func_t));
587         }
588     }
589     kf_list[kf_used++] = handler;
590 }
591
592 void
593 ChannelUserKicked(struct userNode* kicker, struct userNode* victim, struct chanNode* channel)
594 {
595     unsigned int n;
596     struct modeNode *mn;
597
598     if (!victim || !channel || IsService(victim) || !GetUserMode(channel, victim))
599         return;
600
601     /* Update the kicker's idle time (kicker may be null if it was a server) */
602     if (kicker && (mn = GetUserMode(channel, kicker)))
603         mn->idle_since = now;
604
605     for (n=0; n<kf_used; n++)
606         kf_list[n](kicker, victim, channel);
607
608     DelChannelUser(victim, channel, 0, 0);
609
610     if (IsLocal(victim))
611         irc_part(victim, channel, NULL);
612 }
613
614 int ChannelBanExists(struct chanNode *channel, const char *ban)
615 {
616     unsigned int n;
617
618     for (n = 0; n < channel->banlist.used; n++)
619         if (match_ircglobs(channel->banlist.list[n]->ban, ban))
620             return 1;
621     return 0;
622 }
623
624 static topic_func_t *tf_list;
625 static unsigned int tf_size = 0, tf_used = 0;
626
627 void
628 reg_topic_func(topic_func_t handler)
629 {
630     if (tf_used == tf_size) {
631         if (tf_size) {
632             tf_size <<= 1;
633             tf_list = realloc(tf_list, tf_size*sizeof(topic_func_t));
634         } else {
635             tf_size = 8;
636             tf_list = malloc(tf_size*sizeof(topic_func_t));
637         }
638     }
639     tf_list[tf_used++] = handler;
640 }
641
642 void
643 SetChannelTopic(struct chanNode *channel, struct userNode *user, const char *topic, int announce)
644 {
645     unsigned int n;
646     struct modeNode *mn;
647     char old_topic[TOPICLEN+1];
648
649     safestrncpy(old_topic, channel->topic, sizeof(old_topic));
650     safestrncpy(channel->topic, topic, sizeof(channel->topic));
651     channel->topic_time = now;
652
653     if (user) {
654         safestrncpy(channel->topic_nick, user->nick, sizeof(channel->topic_nick));
655
656         /* Update the setter's idle time */
657         if ((mn = GetUserMode(channel, user)))
658             mn->idle_since = now;
659     }
660
661     if (announce) {
662         /* We don't really care if a local user messes with the topic,
663          * so don't call the tf_list functions. */
664         irc_topic(user, channel, topic);
665     } else {
666         for (n=0; n<tf_used; n++)
667             if (tf_list[n](user, channel, old_topic))
668                 break;
669     }
670 }
671
672 struct chanNode *
673 GetChannel(const char *name)
674 {
675     return dict_find(channels, name, NULL);
676 }
677
678 struct modeNode *
679 GetUserMode(struct chanNode *channel, struct userNode *user)
680 {
681     unsigned int n;
682     struct modeNode *mn = NULL;
683     if (channel->members.used < user->channels.used) {
684         for (n=0; n<channel->members.used; n++) {
685             if (user == channel->members.list[n]->user) {
686                 mn = channel->members.list[n];
687                 break;
688             }
689         }
690     } else {
691         for (n=0; n<user->channels.used; n++) {
692             if (channel == user->channels.list[n]->channel) {
693                 mn = user->channels.list[n];
694                 break;
695             }
696         }
697     }
698     return mn;
699 }
700
701 DEFINE_LIST(userList, struct userNode*)
702 DEFINE_LIST(modeList, struct modeNode*)
703 DEFINE_LIST(banList, struct banNode*)
704 DEFINE_LIST(channelList, struct chanNode*)
705 DEFINE_LIST(serverList, struct server*)
706
707 static void
708 hash_cleanup(void)
709 {
710     dict_iterator_t it, next;
711
712     DelServer(self, 0, NULL);
713     for (it = dict_first(channels); it; it = next) {
714         next = iter_next(it);
715         DelChannel(iter_data(it));
716     }
717     dict_delete(channels);
718     dict_delete(clients);
719     dict_delete(servers);
720     userList_clean(&curr_opers);
721
722     free(slf_list);
723     free(nuf_list);
724     free(ncf2_list);
725     free(duf_list);
726     free(ncf_list);
727     free(jf_list);
728     free(dcf_list);
729     free(pf_list);
730     free(kf_list);
731     free(tf_list);
732 }