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