update copyright year
[claws.git] / src / mh.c
1 /*
2  * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2024 Hiroyuki Yamamoto and the Claws Mail team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
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, see <http://www.gnu.org/licenses/>.
17  */
18
19 #ifdef HAVE_CONFIG_H
20 #  include "config.h"
21 #include "claws-features.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27 #include <glib/gi18n.h>
28 #include <sys/stat.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <time.h>
33
34 #include "folder.h"
35 #include "folder_item_prefs.h"
36 #include "mh.h"
37 #include "procmsg.h"
38 #include "procheader.h"
39 #include "utils.h"
40 #include "codeconv.h"
41 #include "statusbar.h"
42 #include "gtkutils.h"
43 #include "timing.h"
44 #include "msgcache.h"
45 #include "file-utils.h"
46
47 /* Define possible missing constants for Windows. */
48 #ifdef G_OS_WIN32
49 # ifndef S_IRGRP
50 # define S_IRGRP 0
51 # define S_IWGRP 0
52 # endif
53 # ifndef S_IROTH
54 # define S_IROTH 0
55 # define S_IWOTH 0
56 # endif
57 #endif
58
59
60 static void     mh_folder_init          (Folder         *folder,
61                                          const gchar    *name,
62                                          const gchar    *path);
63
64 static Folder   *mh_folder_new          (const gchar    *name,
65                                          const gchar    *path);
66 static void     mh_folder_destroy       (Folder         *folder);
67 static gchar   *mh_fetch_msg            (Folder         *folder,
68                                          FolderItem     *item,
69                                          gint            num);
70 static MsgInfo *mh_get_msginfo          (Folder         *folder,
71                                          FolderItem     *item,
72                                          gint            num);
73 static gint     mh_add_msg              (Folder         *folder,
74                                          FolderItem     *dest,
75                                          const gchar    *file,
76                                          MsgFlags       *flags);
77 static gint     mh_add_msgs             (Folder         *folder,
78                                          FolderItem     *dest,
79                                          GSList         *file_list,
80                                          GHashTable     *relation);
81 static gint     mh_copy_msg             (Folder         *folder,
82                                          FolderItem     *dest,
83                                          MsgInfo        *msginfo);
84 static gint     mh_copy_msgs            (Folder         *folder, 
85                                          FolderItem     *dest, 
86                                          MsgInfoList    *msglist, 
87                                          GHashTable     *relation);
88 static gint     mh_remove_msg           (Folder         *folder,
89                                          FolderItem     *item,
90                                          gint            num);
91 static gint     mh_remove_msgs          (Folder         *folder, 
92                                          FolderItem     *item, 
93                                          MsgInfoList    *msglist, 
94                                          GHashTable     *relation);
95 static gint     mh_remove_all_msg       (Folder         *folder,
96                                          FolderItem     *item);
97 static gboolean mh_is_msg_changed       (Folder         *folder,
98                                          FolderItem     *item,
99                                          MsgInfo        *msginfo);
100
101 static gint     mh_get_num_list         (Folder         *folder,
102                                          FolderItem     *item, 
103                                          GSList         **list, 
104                                          gboolean       *old_uids_valid);
105 static gint     mh_scan_tree            (Folder         *folder);
106
107 static gint    mh_create_tree           (Folder         *folder);
108 static FolderItem *mh_create_folder     (Folder         *folder,
109                                          FolderItem     *parent,
110                                          const gchar    *name);
111 static gint    mh_rename_folder         (Folder         *folder,
112                                          FolderItem     *item,
113                                          const gchar    *name);
114 static gint    mh_remove_folder         (Folder         *folder,
115                                          FolderItem     *item);
116
117 static gchar   *mh_get_new_msg_filename         (FolderItem     *dest);
118
119 static MsgInfo *mh_parse_msg                    (const gchar    *file,
120                                                  FolderItem     *item);
121 static void     mh_remove_missing_folder_items  (Folder         *folder);
122 static gchar    *mh_filename_from_utf8          (const gchar    *path);
123 static gchar    *mh_filename_to_utf8            (const gchar    *path);
124 static void     mh_scan_tree_recursive          (FolderItem     *item);
125
126 static gboolean mh_rename_folder_func           (GNode          *node,
127                                                  gpointer        data);
128 static gchar   *mh_item_get_path                (Folder *folder, 
129                                                  FolderItem *item);
130
131 static gboolean mh_scan_required        (Folder         *folder,
132                                          FolderItem     *item);
133 static void mh_set_mtime                (Folder         *folder,
134                                          FolderItem *item);
135 static int mh_item_close                (Folder         *folder,
136                                          FolderItem     *item);
137 #if 0
138 static gint mh_get_flags                (Folder *folder, FolderItem *item,
139                                          MsgInfoList *msginfo_list, GHashTable *msgflags);
140 #endif
141 static void mh_write_sequences          (FolderItem     *item, gboolean remove_unseen);
142
143 static FolderClass mh_class;
144
145 FolderClass *mh_get_class(void)
146 {
147         if (mh_class.idstr == NULL) {
148                 mh_class.type = F_MH;
149                 mh_class.idstr = "mh";
150                 mh_class.uistr = "MH";
151                 mh_class.supports_server_search = FALSE;
152                 
153                 /* Folder functions */
154                 mh_class.new_folder = mh_folder_new;
155                 mh_class.destroy_folder = mh_folder_destroy;
156                 mh_class.set_xml = folder_local_set_xml;
157                 mh_class.get_xml = folder_local_get_xml;
158                 mh_class.scan_tree = mh_scan_tree;
159                 mh_class.create_tree = mh_create_tree;
160
161                 /* FolderItem functions */
162                 mh_class.item_get_path = mh_item_get_path;
163                 mh_class.create_folder = mh_create_folder;
164                 mh_class.rename_folder = mh_rename_folder;
165                 mh_class.remove_folder = mh_remove_folder;
166                 mh_class.get_num_list = mh_get_num_list;
167                 mh_class.scan_required = mh_scan_required;
168                 mh_class.set_mtime = mh_set_mtime;
169                 mh_class.close = mh_item_close;
170                 mh_class.get_flags = NULL; /*mh_get_flags */;
171
172                 /* Message functions */
173                 mh_class.get_msginfo = mh_get_msginfo;
174                 mh_class.fetch_msg = mh_fetch_msg;
175                 mh_class.add_msg = mh_add_msg;
176                 mh_class.add_msgs = mh_add_msgs;
177                 mh_class.copy_msg = mh_copy_msg;
178                 mh_class.copy_msgs = mh_copy_msgs;
179                 mh_class.search_msgs = folder_item_search_msgs_local;
180                 mh_class.remove_msg = mh_remove_msg;
181                 mh_class.remove_msgs = mh_remove_msgs;
182                 mh_class.remove_all_msg = mh_remove_all_msg;
183                 mh_class.is_msg_changed = mh_is_msg_changed;
184         }
185
186         return &mh_class;
187 }
188
189 static Folder *mh_folder_new(const gchar *name, const gchar *path)
190 {
191         Folder *folder;
192
193         folder = (Folder *)g_new0(MHFolder, 1);
194         folder->klass = &mh_class;
195         mh_folder_init(folder, name, path);
196
197         return folder;
198 }
199
200 static void mh_folder_destroy(Folder *folder)
201 {
202         folder_local_folder_destroy(LOCAL_FOLDER(folder));
203 }
204
205 static void mh_folder_init(Folder *folder, const gchar *name, const gchar *path)
206 {
207         folder_local_folder_init(folder, name, path);
208
209 }
210
211 gboolean mh_scan_required(Folder *folder, FolderItem *item)
212 {
213         gchar *path;
214         GStatBuf s;
215
216         path = folder_item_get_path(item);
217         cm_return_val_if_fail(path != NULL, FALSE);
218
219         if (g_stat(path, &s) < 0) {
220                 FILE_OP_ERROR(path, "stat");
221                 g_free(path);
222                 return FALSE;
223         }
224
225         if ((s.st_mtime > item->mtime) &&
226                 (s.st_mtime - 3600 != item->mtime)) {
227                 debug_print("MH scan required, folder updated: %s (%ld > %ld)\n",
228                             path,
229                             (long int) s.st_mtime,
230                             (long int) item->mtime);
231                 g_free(path);
232                 return TRUE;
233         }
234
235         debug_print("MH scan not required: %s (%ld <= %ld)\n",
236                     path,
237                     (long int) s.st_mtime,
238                     (long int) item->mtime);
239         g_free(path);
240         return FALSE;
241 }
242
243 static void mh_get_last_num(Folder *folder, FolderItem *item)
244 {
245         gchar *path, *fullpath;
246         GDir *dp;
247         const gchar *d;
248         GError *error = NULL;
249         gint max = 0;
250         gint num;
251
252         cm_return_if_fail(item != NULL);
253
254         debug_print("mh_get_last_num(): Scanning %s ...\n", item->path?item->path:"(null)");
255
256         path = folder_item_get_path(item);
257         cm_return_if_fail(path != NULL);
258
259         if ((dp = g_dir_open(path, 0, &error)) == NULL) {
260                 g_warning("couldn't open directory '%s': %s (%d)",
261                                 path, error->message, error->code);
262                 g_error_free(error);
263                 g_free(path);
264                 return;
265         }
266
267         while ((d = g_dir_read_name(dp)) != NULL) {
268                 fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, d, NULL);
269                 if ((num = to_number(d)) > 0 &&
270                     g_file_test(fullpath, G_FILE_TEST_IS_REGULAR)) {
271                         if (max < num)
272                                 max = num;
273                 }
274                 g_free(fullpath);
275
276                 if (num % 2000 == 0)
277                         GTK_EVENTS_FLUSH();
278         }
279         g_dir_close(dp);
280         g_free(path);
281
282         debug_print("Last number in dir %s = %d\n", item->path?item->path:"(null)", max);
283         item->last_num = max;
284 }
285
286 gint mh_get_num_list(Folder *folder, FolderItem *item, GSList **list, gboolean *old_uids_valid)
287 {
288
289         gchar *path;
290         GDir *dp;
291         const gchar *d;
292         GError *error = NULL;
293         gint num, nummsgs = 0;
294
295         cm_return_val_if_fail(item != NULL, -1);
296
297         debug_print("mh_get_num_list(): Scanning %s ...\n", item->path?item->path:"(null)");
298
299         *old_uids_valid = TRUE;
300
301         path = folder_item_get_path(item);
302         cm_return_val_if_fail(path != NULL, -1);
303
304         if ((dp = g_dir_open(path, 0, &error)) == NULL) {
305                 g_message("Couldn't open current directory: %s (%d).\n",
306                                 error->message, error->code);
307                 g_error_free(error);
308                 g_free(path);
309                 return -1;
310         }
311         g_free(path);
312
313         while ((d = g_dir_read_name(dp)) != NULL) {
314                 if ((num = to_number(d)) > 0) {
315                         *list = g_slist_prepend(*list, GINT_TO_POINTER(num));
316                         nummsgs++;
317                 }
318         }
319         g_dir_close(dp);
320
321         mh_set_mtime(folder, item);
322         return nummsgs;
323 }
324
325 static gchar *mh_fetch_msg(Folder *folder, FolderItem *item, gint num)
326 {
327         gchar *path;
328         gchar *file;
329
330         cm_return_val_if_fail(item != NULL, NULL);
331         cm_return_val_if_fail(num > 0, NULL);
332
333         path = folder_item_get_path(item);
334         file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
335
336         if (!is_file_exist(file)) {
337                 g_free(file);
338                 g_free(path);
339                 return NULL;
340         }
341         g_free(path);
342         return file;
343 }
344
345 static MsgInfo *mh_get_msginfo(Folder *folder, FolderItem *item, gint num)
346 {
347         MsgInfo *msginfo;
348         gchar *file;
349
350         cm_return_val_if_fail(item != NULL, NULL);
351         if (num <= 0)
352                 return NULL;
353
354         file = mh_fetch_msg(folder, item, num);
355         if (!file) return NULL;
356
357         msginfo = mh_parse_msg(file, item);
358         if (msginfo)
359                 msginfo->msgnum = num;
360
361         g_free(file);
362
363         return msginfo;
364 }
365
366 static gchar *mh_get_new_msg_filename(FolderItem *dest)
367 {
368         gchar *destfile;
369         gchar *destpath;
370
371         destpath = folder_item_get_path(dest);
372         cm_return_val_if_fail(destpath != NULL, NULL);
373
374         if (dest->last_num < 0) {
375                 mh_get_last_num(dest->folder, dest);
376                 if (dest->last_num < 0) return NULL;
377         }
378
379         if (!is_dir_exist(destpath))
380                 make_dir_hier(destpath);
381
382         for (;;) {
383                 destfile = g_strdup_printf("%s%c%d", destpath, G_DIR_SEPARATOR,
384                                            dest->last_num + 1);
385                 if (is_file_entry_exist(destfile)) {
386                         dest->last_num++;
387                         g_free(destfile);
388                 } else
389                         break;
390         }
391
392         g_free(destpath);
393
394         return destfile;
395 }
396
397 static gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file, MsgFlags *flags)
398 {
399         gint ret;
400         GSList file_list;
401         MsgFileInfo fileinfo;
402
403         cm_return_val_if_fail(file != NULL, -1);
404
405         fileinfo.msginfo = NULL;
406         fileinfo.file = (gchar *)file;
407         fileinfo.flags = flags;
408         file_list.data = &fileinfo;
409         file_list.next = NULL;
410
411         ret = mh_add_msgs(folder, dest, &file_list, NULL);
412         return ret;
413
414  
415 static gint mh_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list, 
416                  GHashTable *relation)
417
418         gchar *destfile;
419         GSList *cur;
420         MsgFileInfo *fileinfo;
421
422         cm_return_val_if_fail(dest != NULL, -1);
423         cm_return_val_if_fail(file_list != NULL, -1);
424
425         if (dest->last_num < 0) {
426                 mh_get_last_num(folder, dest);
427                 if (dest->last_num < 0) return -1;
428         }
429
430         for (cur = file_list; cur != NULL; cur = cur->next) {
431                 fileinfo = (MsgFileInfo *)cur->data;
432
433                 destfile = mh_get_new_msg_filename(dest);
434                 if (destfile == NULL) return -1;
435
436 #ifdef G_OS_UNIX
437                 if (link(fileinfo->file, destfile) < 0) {
438 #endif
439                         if (copy_file(fileinfo->file, destfile, TRUE) < 0) {
440                                 g_warning("can't copy message %s to %s",
441                                           fileinfo->file, destfile);
442                                 g_free(destfile);
443                                 return -1;
444                         }
445 #ifdef G_OS_UNIX
446                 }
447 #endif
448
449                 if (relation != NULL)
450                         g_hash_table_insert(relation, fileinfo, GINT_TO_POINTER(dest->last_num + 1));
451                 g_free(destfile);
452                 dest->last_num++;
453         }
454         mh_write_sequences(dest, TRUE);
455         return dest->last_num;
456 }
457
458 static gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
459 {
460         GSList msglist;
461
462         cm_return_val_if_fail(msginfo != NULL, -1);
463
464         msglist.data = msginfo;
465         msglist.next = NULL;
466
467         return mh_copy_msgs(folder, dest, &msglist, NULL);      
468 }
469
470 static gint mh_copy_msgs(Folder *folder, FolderItem *dest, MsgInfoList *msglist, 
471                          GHashTable *relation)
472 {
473         gboolean dest_need_scan = FALSE;
474         gboolean src_need_scan = FALSE;
475         FolderItem *src = NULL;
476         gchar *srcfile;
477         gchar *destfile;
478         FolderItemPrefs *prefs;
479         MsgInfo *msginfo = NULL;
480         MsgInfoList *cur = NULL;
481         gint curnum = 0, total = 0;
482         gchar *srcpath = NULL;
483         gboolean full_fetch = FALSE;
484         time_t last_dest_mtime = (time_t)0;
485         time_t last_src_mtime = (time_t)0;
486
487         cm_return_val_if_fail(dest != NULL, -1);
488         cm_return_val_if_fail(msglist != NULL, -1);
489         
490         msginfo = (MsgInfo *)msglist->data;
491
492         cm_return_val_if_fail(msginfo != NULL, -1);
493
494         if (msginfo->folder == dest) {
495                 g_warning("the src folder is identical to the dest");
496                 return -1;
497         }
498
499         if (msginfo->folder->folder != dest->folder)
500                 full_fetch = TRUE;
501         
502         if (FOLDER_TYPE(msginfo->folder->folder) == F_MH) {
503                 src = msginfo->folder;
504         }
505
506         if (dest->last_num < 0) {
507                 mh_get_last_num(folder, dest);
508                 if (dest->last_num < 0) return -1;
509         }
510
511         prefs = dest->prefs;
512
513         srcpath = folder_item_get_path(msginfo->folder);
514
515         dest_need_scan = mh_scan_required(dest->folder, dest);
516         last_dest_mtime = dest->mtime;
517
518         if (src) {
519                 src_need_scan = mh_scan_required(src->folder, src);
520                 last_src_mtime = src->mtime;
521         }
522
523         total = g_slist_length(msglist);
524         if (total > 100) {
525                 if (MSG_IS_MOVE(msginfo->flags))
526                         statusbar_print_all(_("Moving messages..."));
527                 else
528                         statusbar_print_all(_("Copying messages..."));
529         }
530         for (cur = msglist; cur; cur = cur->next) {
531                 msginfo = (MsgInfo *)cur->data;
532                 if (!msginfo) {
533                         goto err_reset_status;
534                 }
535                 if (!full_fetch) {
536                         srcfile = g_strconcat(srcpath, 
537                                 G_DIR_SEPARATOR_S, 
538                                 itos(msginfo->msgnum), NULL);
539                 } else {
540                         srcfile = procmsg_get_message_file(msginfo);
541                 }
542                 if (!srcfile) {
543                         goto err_reset_status;
544                 }
545                 destfile = mh_get_new_msg_filename(dest);
546                 if (!destfile) {
547                         g_free(srcfile);
548                         goto err_reset_status;
549                 }
550
551                 if (total > 100) {
552                         statusbar_progress_all(curnum, total, 100);
553                         if (curnum % 100 == 0)
554                                 GTK_EVENTS_FLUSH();
555                         curnum++;
556                 }
557
558                 debug_print("Copying message %s%c%d to %s ...\n",
559                             msginfo->folder->path, G_DIR_SEPARATOR,
560                             msginfo->msgnum, dest->path);
561
562
563                 if (MSG_IS_MOVE(msginfo->flags)) {
564                         msginfo->flags.tmp_flags &= ~MSG_MOVE_DONE;
565                         if (move_file(srcfile, destfile, TRUE) < 0) {
566                                 FILE_OP_ERROR(srcfile, "move");
567                                 if (copy_file(srcfile, destfile, TRUE) < 0) {
568                                         FILE_OP_ERROR(srcfile, "copy");
569                                         g_free(srcfile);
570                                         g_free(destfile);
571                                         goto err_reset_status;
572                                 }
573                         } else {
574                                 /* say unlinking's not necessary */
575                                 msginfo->flags.tmp_flags |= MSG_MOVE_DONE;
576                         }
577                 } else if (copy_file(srcfile, destfile, TRUE) < 0) {
578                         FILE_OP_ERROR(srcfile, "copy");
579                         g_free(srcfile);
580                         g_free(destfile);
581                         goto err_reset_status;
582                 } 
583                 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
584                         if (chmod(destfile, prefs->folder_chmod) < 0)
585                                 FILE_OP_ERROR(destfile, "chmod");
586                 }
587                 if (relation) {
588                         if (g_hash_table_lookup(relation, msginfo) != NULL)
589                                 g_warning("already in: %p", msginfo);
590                         
591                         g_hash_table_insert(relation, msginfo, GINT_TO_POINTER(dest->last_num+1));
592                 }
593                 g_free(srcfile);
594                 g_free(destfile);
595                 dest->last_num++;
596         }
597
598         g_free(srcpath);
599         mh_write_sequences(dest, TRUE);
600
601         if (dest->mtime == last_dest_mtime && !dest_need_scan) {
602                 mh_set_mtime(folder, dest);
603         }
604
605         if (src && src->mtime == last_src_mtime && !src_need_scan) {
606                 mh_set_mtime(folder, src);
607         }
608
609         if (total > 100) {
610                 statusbar_progress_all(0,0,0);
611                 statusbar_pop_all();
612         }
613         return dest->last_num;
614 err_reset_status:
615         g_free(srcpath);
616         mh_write_sequences(dest, TRUE);
617         if (total > 100) {
618                 statusbar_progress_all(0,0,0);
619                 statusbar_pop_all();
620         }
621         return -1;
622
623 }
624
625 static gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
626 {
627         gboolean need_scan = FALSE;
628         time_t last_mtime = (time_t)0;
629         gchar *file;
630
631         cm_return_val_if_fail(item != NULL, -1);
632
633         file = mh_fetch_msg(folder, item, num);
634         cm_return_val_if_fail(file != NULL, -1);
635
636         need_scan = mh_scan_required(folder, item);
637         last_mtime = item->mtime;
638
639         if (claws_unlink(file) < 0) {
640                 FILE_OP_ERROR(file, "unlink");
641                 g_free(file);
642                 return -1;
643         }
644
645         if (item->mtime == last_mtime && !need_scan) {
646                 mh_set_mtime(folder, item);
647         }
648         g_free(file);
649         return 0;
650 }
651
652 static gint mh_remove_msgs(Folder *folder, FolderItem *item, 
653                     MsgInfoList *msglist, GHashTable *relation)
654 {
655         gboolean need_scan = FALSE;
656         gchar *path, *file;
657         time_t last_mtime = (time_t)0;
658         MsgInfoList *cur;
659         gint total = 0, curnum = 0;
660
661         cm_return_val_if_fail(item != NULL, -1);
662
663         path = folder_item_get_path(item);
664         
665         need_scan = mh_scan_required(folder, item);
666         last_mtime = item->mtime;
667
668         total = g_slist_length(msglist);
669         if (total > 100) {
670                 statusbar_print_all(_("Deleting messages..."));
671         }
672
673         for (cur = msglist; cur; cur = cur->next) {
674                 MsgInfo *msginfo = (MsgInfo *)cur->data;
675                 if (msginfo == NULL)
676                         continue;
677                 if (MSG_IS_MOVE(msginfo->flags) && MSG_IS_MOVE_DONE(msginfo->flags)) {
678                         msginfo->flags.tmp_flags &= ~MSG_MOVE_DONE;
679                         continue;
680                 }
681                 if (total > 100) {
682                         statusbar_progress_all(curnum, total, 100);
683                         if (curnum % 100 == 0)
684                                 GTK_EVENTS_FLUSH();
685                         curnum++;
686                 }
687
688                 file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(msginfo->msgnum), NULL);
689                 if (file == NULL)
690                         continue;
691                 
692                 if (claws_unlink(file) < 0) {
693                         g_free(file);
694                         continue;
695                 }
696                 
697                 g_free(file);
698         }
699
700         if (total > 100) {
701                 statusbar_progress_all(0,0,0);
702                 statusbar_pop_all();
703         }
704         if (item->mtime == last_mtime && !need_scan) {
705                 mh_set_mtime(folder, item);
706         }
707
708         g_free(path);
709         return 0;
710 }
711
712 static gint mh_remove_all_msg(Folder *folder, FolderItem *item)
713 {
714         gchar *path;
715         gint val;
716
717         cm_return_val_if_fail(item != NULL, -1);
718
719         path = folder_item_get_path(item);
720         cm_return_val_if_fail(path != NULL, -1);
721         val = remove_all_numbered_files(path);
722         g_free(path);
723
724         mh_write_sequences(item, TRUE);
725
726         return val;
727 }
728
729 static gboolean mh_is_msg_changed(Folder *folder, FolderItem *item,
730                                   MsgInfo *msginfo)
731 {
732 #ifdef G_OS_WIN32
733         GFile *f;
734         GFileInfo *fi;
735         GTimeVal tv;
736         GError *error = NULL;
737 #else
738         GStatBuf s;
739         int r;
740 #endif
741         gchar *path;
742         gchar *parent_path;
743
744         parent_path = folder_item_get_path(item);
745         path = g_strdup_printf("%s%c%d", parent_path,
746                         G_DIR_SEPARATOR, msginfo->msgnum);
747         g_free(parent_path);
748
749 #ifdef G_OS_WIN32
750         f = g_file_new_for_path(path);
751         g_free(path);
752         fi = g_file_query_info(f, "standard::size,time::modified",
753                         G_FILE_QUERY_INFO_NONE, NULL, &error);
754         if (error != NULL) {
755                 g_warning(error->message);
756                 g_error_free(error);
757                 g_object_unref(f);
758                 return TRUE;
759         }
760
761         g_file_info_get_modification_time(fi, &tv);
762         if (msginfo->size != g_file_info_get_size(fi) || (
763                         (msginfo->mtime - tv.tv_sec != 0) &&
764                         abs(msginfo->mtime - tv.tv_sec) != 3600)) {
765                 g_error_free(error);
766                 g_object_unref(f);
767                 return TRUE;
768         }
769 #else
770         r = g_stat(path, &s);
771         g_free(path);
772         if (r < 0 ||
773             msginfo->size  != s.st_size || (
774                 (msginfo->mtime - s.st_mtime != 0) &&
775                 (msginfo->mtime - s.st_mtime != 3600) &&
776                 (msginfo->mtime - s.st_mtime != -3600))) {
777                 return TRUE;
778         }
779 #endif
780
781         return FALSE;
782 }
783
784 static gint mh_scan_tree(Folder *folder)
785 {
786         FolderItem *item;
787
788         cm_return_val_if_fail(folder != NULL, -1);
789
790         if (!folder->node) {
791                 item = folder_item_new(folder, folder->name, NULL);
792                 item->folder = folder;
793                 folder->node = item->node = g_node_new(item);
794         } else
795                 item = FOLDER_ITEM(folder->node->data);
796
797         mh_create_tree(folder);
798         mh_remove_missing_folder_items(folder);
799         mh_scan_tree_recursive(item);
800
801         return 0;
802 }
803
804 #define MAKE_DIR_IF_NOT_EXIST(dir) \
805 { \
806         if (!is_dir_exist(dir)) { \
807                 if (is_file_exist(dir)) { \
808                         g_warning("file '%s' already exists, " \
809                                     "can't create folder", dir); \
810                         if (rootpath) \
811                                 g_free(rootpath); \
812                         if (path) \
813                                 g_free(path); \
814                         return -1; \
815                 } \
816                 if (make_dir_hier(dir) < 0) { \
817                         if (rootpath) \
818                                 g_free(rootpath); \
819                         if (path) \
820                                 g_free(path); \
821                         return -1; \
822                 } \
823                 debug_print("Created dir '%s'\n", dir); \
824         } \
825 }
826
827 static gint mh_create_tree(Folder *folder)
828 {
829         gchar *rootpath, *f, *path = NULL;
830
831         cm_return_val_if_fail(folder != NULL, -1);
832
833         rootpath = LOCAL_FOLDER(folder)->rootpath;
834 #ifdef G_OS_UNIX
835         if (*rootpath == '/') {
836 #elif defined G_OS_WIN32
837         if (g_ascii_isalpha(*rootpath) && !strncmp(rootpath + 1, ":\\", 2)) {
838 #endif
839                 /* Folder path is absolute. */
840                 rootpath = g_strdup(rootpath);
841         } else {
842                 /* Folder path is relative, using mail base dir. */
843                 rootpath = g_strconcat(get_mail_base_dir(), G_DIR_SEPARATOR_S,
844                                 rootpath, NULL);
845         }
846
847         MAKE_DIR_IF_NOT_EXIST(rootpath);
848
849         /* Create special directories as needed */
850         if (folder->inbox != NULL &&
851                         folder->inbox->path != NULL)
852                 f = folder->inbox->path;
853         else
854                 f = INBOX_DIR;
855         path = g_strconcat(rootpath, G_DIR_SEPARATOR_S, f, NULL);
856         MAKE_DIR_IF_NOT_EXIST(path);
857         g_free(path);
858
859         if (folder->outbox != NULL &&
860                         folder->outbox->path != NULL)
861                 f = folder->outbox->path;
862         else
863                 f = OUTBOX_DIR;
864         path = g_strconcat(rootpath, G_DIR_SEPARATOR_S, f, NULL);
865         MAKE_DIR_IF_NOT_EXIST(path);
866         g_free(path);
867
868         if (folder->draft != NULL &&
869                         folder->draft->path != NULL)
870                 f = folder->draft->path;
871         else
872                 f = DRAFT_DIR;
873         path = g_strconcat(rootpath, G_DIR_SEPARATOR_S, f, NULL);
874         MAKE_DIR_IF_NOT_EXIST(path);
875         g_free(path);
876
877         if (folder->queue != NULL &&
878                         folder->queue->path != NULL)
879                 f = folder->queue->path;
880         else
881                 f = QUEUE_DIR;
882         path = g_strconcat(rootpath, G_DIR_SEPARATOR_S, f, NULL);
883         MAKE_DIR_IF_NOT_EXIST(path);
884         g_free(path);
885
886         if (folder->trash != NULL &&
887                         folder->trash->path != NULL)
888                 f = folder->trash->path;
889         else
890                 f = TRASH_DIR;
891         path = g_strconcat(rootpath, G_DIR_SEPARATOR_S, f, NULL);
892         MAKE_DIR_IF_NOT_EXIST(path);
893         g_free(path);
894
895         g_free(rootpath);
896         return 0;
897 }
898
899 #undef MAKE_DIR_IF_NOT_EXIST
900
901 static gchar *mh_item_get_path(Folder *folder, FolderItem *item)
902 {
903         gchar *folder_path, *path;
904         gchar *real_path;
905         cm_return_val_if_fail(folder != NULL, NULL);
906         cm_return_val_if_fail(item != NULL, NULL);
907
908         folder_path = g_strdup(LOCAL_FOLDER(folder)->rootpath);
909         cm_return_val_if_fail(folder_path != NULL, NULL);
910
911         /* FIXME: [W32] The code below does not correctly merge
912            relative filenames; there should be a function to handle
913            this.  */
914         if ( !is_relative_filename (folder_path) ) {
915                 if (item->path)
916                         path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
917                                            item->path, NULL);
918                 else
919                         path = g_strdup(folder_path);
920         } else {
921                 if (item->path)
922                         path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
923                                            folder_path, G_DIR_SEPARATOR_S,
924                                            item->path, NULL);
925                 else
926                         path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
927                                            folder_path, NULL);
928         }
929         g_free(folder_path);
930         real_path = mh_filename_from_utf8(path);
931         if (!is_dir_exist(real_path) && is_dir_exist(path)) {
932                 /* mmh, older version did put utf8 filenames instead of
933                  * the correct encoding */
934                 if (g_rename(path, real_path) == 0)
935                         folder_item_scan(item);
936         }
937
938         g_free(path);
939         return real_path;
940 }
941
942 static gboolean mh_renumber_msg(MsgInfo *info)
943 {
944         gchar *src, *dest;
945         gboolean result = FALSE;
946         guint num;
947         cm_return_val_if_fail(info != NULL, FALSE);
948
949         src = folder_item_fetch_msg(info->folder, info->msgnum);
950         dest = mh_get_new_msg_filename(info->folder);
951         num = info->folder->last_num + 1;
952
953         if (move_file(src, dest, FALSE) == 0) {
954                 msgcache_remove_msg(info->folder->cache, info->msgnum);
955                 info->msgnum = num;
956                 msgcache_add_msg(info->folder->cache, info);
957                 result = TRUE;
958         }
959
960         g_free(src);
961         g_free(dest);
962
963         return result;
964 }
965
966 static FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
967                                     const gchar *name)
968 {
969         gchar *path, *real_name;
970         gchar *fullpath;
971         FolderItem *new_item;
972         gchar *mh_sequences_filename;
973         FILE *mh_sequences_file;
974
975         cm_return_val_if_fail(folder != NULL, NULL);
976         cm_return_val_if_fail(parent != NULL, NULL);
977         cm_return_val_if_fail(name != NULL, NULL);
978
979         path = folder_item_get_path(parent);
980         if (!is_dir_exist(path)) 
981                 if (make_dir_hier(path) != 0)
982                         return NULL;
983
984         real_name = mh_filename_from_utf8(name);
985         fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, real_name, NULL);
986         g_free(real_name);
987         g_free(path);
988
989         if (to_number(name) > 0) {
990                 MsgInfo *info = folder_item_get_msginfo(parent, to_number(name));
991                 if (info != NULL) {
992                         gboolean ok = mh_renumber_msg(info);
993                         procmsg_msginfo_free(&info);
994                         if (!ok) {
995                                 g_free(fullpath);
996                                 return NULL;
997                         }
998                 }
999         }
1000
1001         if (make_dir(fullpath) < 0) {
1002                 g_free(fullpath);
1003                 return NULL;
1004         }
1005
1006         g_free(fullpath);
1007
1008         if (parent->path)
1009                 path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
1010                                    NULL);
1011         else
1012                 path = g_strdup(name);
1013         new_item = folder_item_new(folder, name, path);
1014         folder_item_append(parent, new_item);
1015
1016         g_free(path);
1017
1018         path = folder_item_get_path(new_item);
1019         mh_sequences_filename = g_strconcat(path, G_DIR_SEPARATOR_S,
1020                                             ".mh_sequences", NULL);
1021         if ((mh_sequences_file = claws_fopen(mh_sequences_filename, "a+b")) != NULL) {
1022                 claws_fclose(mh_sequences_file);
1023         }
1024         g_free(mh_sequences_filename);
1025         g_free(path);
1026
1027         return new_item;
1028 }
1029
1030 static gint mh_rename_folder(Folder *folder, FolderItem *item,
1031                              const gchar *name)
1032 {
1033         gchar *real_name;
1034         gchar *oldpath;
1035         gchar *dirname;
1036         gchar *newpath, *utf8newpath;
1037         gchar *paths[2];
1038
1039         cm_return_val_if_fail(folder != NULL, -1);
1040         cm_return_val_if_fail(item != NULL, -1);
1041         cm_return_val_if_fail(item->path != NULL, -1);
1042         cm_return_val_if_fail(name != NULL, -1);
1043
1044         oldpath = folder_item_get_path(item);
1045         if (!is_dir_exist(oldpath))
1046                 make_dir_hier(oldpath);
1047
1048         dirname = g_path_get_dirname(oldpath);
1049         real_name = mh_filename_from_utf8(name);
1050         newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, real_name, NULL);
1051         g_free(real_name);
1052         g_free(dirname);
1053
1054         if (g_rename(oldpath, newpath) < 0) {
1055                 FILE_OP_ERROR(oldpath, "rename");
1056                 g_free(oldpath);
1057                 g_free(newpath);
1058                 return -1;
1059         }
1060
1061         g_free(oldpath);
1062         g_free(newpath);
1063
1064         if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
1065                 dirname = g_path_get_dirname(item->path);
1066                 utf8newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S,
1067                                           name, NULL);
1068                 g_free(dirname);
1069         } else
1070                 utf8newpath = g_strdup(name);
1071
1072         g_free(item->name);
1073         item->name = g_strdup(name);
1074
1075         paths[0] = g_strdup(item->path);
1076         paths[1] = utf8newpath;
1077         g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
1078                         mh_rename_folder_func, paths);
1079
1080         g_free(paths[0]);
1081         g_free(paths[1]);
1082         return 0;
1083 }
1084
1085 static gint mh_remove_folder(Folder *folder, FolderItem *item)
1086 {
1087         gchar *path;
1088         gint ret;
1089
1090         cm_return_val_if_fail(folder != NULL, -1);
1091         cm_return_val_if_fail(item != NULL, -1);
1092         cm_return_val_if_fail(item->path != NULL, -1);
1093
1094         path = folder_item_get_path(item);
1095         if ((ret = remove_dir_recursive(path)) < 0) {
1096                 g_warning("can't remove directory '%s'", path);
1097                 g_free(path);
1098                 return ret;
1099         }
1100
1101         g_free(path);
1102         folder_item_remove(item);
1103         return 0;
1104 }
1105
1106 static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
1107 {
1108         MsgInfo *msginfo;
1109         MsgFlags flags;
1110
1111         cm_return_val_if_fail(item != NULL, NULL);
1112         cm_return_val_if_fail(file != NULL, NULL);
1113
1114         flags.perm_flags = MSG_NEW|MSG_UNREAD;
1115         flags.tmp_flags = 0;
1116
1117         if (folder_has_parent_of_type(item, F_QUEUE)) {
1118                 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
1119         } else if (folder_has_parent_of_type(item, F_DRAFT)) {
1120                 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
1121         }
1122
1123         msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
1124         if (!msginfo) return NULL;
1125
1126         msginfo->msgnum = atoi(file);
1127         msginfo->folder = item;
1128
1129         return msginfo;
1130 }
1131
1132 static gboolean mh_remove_missing_folder_items_func(GNode *node, gpointer data)
1133 {
1134         FolderItem *item;
1135         gchar *path;
1136
1137         cm_return_val_if_fail(node->data != NULL, FALSE);
1138
1139         if (G_NODE_IS_ROOT(node))
1140                 return FALSE;
1141
1142         item = FOLDER_ITEM(node->data);
1143
1144         path = folder_item_get_path(item);
1145         if (!is_dir_exist(path)) {
1146                 debug_print("folder '%s' not found. removing...\n", path?path:"(null)");
1147                 folder_item_remove(item);
1148         }
1149         g_free(path);
1150
1151         return FALSE;
1152 }
1153
1154 static void mh_remove_missing_folder_items(Folder *folder)
1155 {
1156         cm_return_if_fail(folder != NULL);
1157
1158         debug_print("searching missing folders...\n");
1159
1160         g_node_traverse(folder->node, G_POST_ORDER, G_TRAVERSE_ALL, -1,
1161                         mh_remove_missing_folder_items_func, folder);
1162 }
1163
1164 static void mh_scan_tree_recursive(FolderItem *item)
1165 {
1166         Folder *folder;
1167         GDir *dir;
1168         const gchar *dir_name;
1169         gchar *entry, *utf8entry, *utf8name, *path;
1170         gint n_msg = 0;
1171         GError *error = NULL;
1172
1173         cm_return_if_fail(item != NULL);
1174         cm_return_if_fail(item->folder != NULL);
1175
1176         folder = item->folder;
1177
1178         path = folder_item_get_path(item);
1179         debug_print("mh_scan_tree_recursive() opening '%s'\n", path);
1180         dir = g_dir_open(path, 0, &error);
1181         if (!dir) {
1182                 g_warning("failed to open directory '%s': %s (%d)",
1183                                 path, error->message, error->code);
1184                 g_error_free(error);
1185                 g_free(path);
1186                 return;
1187         }
1188
1189         debug_print("scanning %s ...\n",
1190                     item->path ? item->path
1191                     : LOCAL_FOLDER(item->folder)->rootpath);
1192         if (folder->ui_func)
1193                 folder->ui_func(folder, item, folder->ui_func_data);
1194
1195         while ((dir_name = g_dir_read_name(dir)) != NULL) {
1196                 if (dir_name[0] == '.') continue;
1197
1198                 entry = g_strconcat(path, G_DIR_SEPARATOR_S, dir_name, NULL);
1199
1200                 utf8name = mh_filename_to_utf8(dir_name);
1201                 if (item->path)
1202                         utf8entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
1203                                                 utf8name, NULL);
1204                 else
1205                         utf8entry = g_strdup(utf8name);
1206
1207                 if (g_file_test(entry, G_FILE_TEST_IS_DIR)) {
1208                         FolderItem *new_item = NULL;
1209                         GNode *node;
1210
1211                         node = item->node;
1212                         for (node = node->children; node != NULL; node = node->next) {
1213                                 FolderItem *cur_item = FOLDER_ITEM(node->data);
1214                                 gchar *curpath = folder_item_get_path(cur_item);
1215                                 if (!g_strcmp0(curpath, entry)) {
1216                                         new_item = cur_item;
1217                                         g_free(curpath);
1218                                         break;
1219                                 }
1220                                 g_free(curpath);
1221                         }
1222                         if (!new_item) {
1223                                 debug_print("new folder '%s' found.\n", entry);
1224                                 new_item = folder_item_new(folder, utf8name, utf8entry);
1225                                 folder_item_append(item, new_item);
1226                         }
1227
1228                         if (!item->path) {
1229                                 if (!folder->inbox &&
1230                                     !strcmp(dir_name, INBOX_DIR)) {
1231                                         new_item->stype = F_INBOX;
1232                                         folder->inbox = new_item;
1233                                 } else if (!folder->outbox &&
1234                                            !strcmp(dir_name, OUTBOX_DIR)) {
1235                                         new_item->stype = F_OUTBOX;
1236                                         folder->outbox = new_item;
1237                                 } else if (!folder->draft &&
1238                                            !strcmp(dir_name, DRAFT_DIR)) {
1239                                         new_item->stype = F_DRAFT;
1240                                         folder->draft = new_item;
1241                                 } else if (!folder->queue &&
1242                                            !strcmp(dir_name, QUEUE_DIR)) {
1243                                         new_item->stype = F_QUEUE;
1244                                         folder->queue = new_item;
1245                                 } else if (!folder->trash &&
1246                                            !strcmp(dir_name, TRASH_DIR)) {
1247                                         new_item->stype = F_TRASH;
1248                                         folder->trash = new_item;
1249                                 }
1250                         }
1251
1252                         mh_scan_tree_recursive(new_item);
1253                 } else if (to_number(dir_name) > 0) n_msg++;
1254
1255                 g_free(entry);
1256                 g_free(utf8entry);
1257                 g_free(utf8name);
1258         }
1259
1260         g_dir_close(dir);
1261         g_free(path);
1262
1263         mh_set_mtime(folder, item);
1264 }
1265
1266 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
1267 {
1268         FolderItem *item = node->data;
1269         gchar **paths = data;
1270         const gchar *oldpath = paths[0];
1271         const gchar *newpath = paths[1];
1272         gchar *base;
1273         gchar *new_itempath;
1274         gint oldpathlen;
1275
1276         oldpathlen = strlen(oldpath);
1277         if (strncmp(oldpath, item->path, oldpathlen) != 0) {
1278                 g_warning("path doesn't match: %s, %s", oldpath, item->path);
1279                 return TRUE;
1280         }
1281
1282         base = item->path + oldpathlen;
1283         while (*base == G_DIR_SEPARATOR) base++;
1284         if (*base == '\0')
1285                 new_itempath = g_strdup(newpath);
1286         else
1287                 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
1288                                            NULL);
1289         g_free(item->path);
1290         item->path = new_itempath;
1291
1292         return FALSE;
1293 }
1294
1295 static gchar *mh_filename_from_utf8(const gchar *path)
1296 {
1297         gchar *real_path = g_filename_from_utf8(path, -1, NULL, NULL, NULL);
1298
1299         if (!real_path) {
1300                 g_warning("mh_filename_from_utf8: failed to convert character set");
1301                 real_path = g_strdup(path);
1302         }
1303
1304         return real_path;
1305 }
1306
1307 static gchar *mh_filename_to_utf8(const gchar *path)
1308 {
1309         gchar *utf8path = g_filename_to_utf8(path, -1, NULL, NULL, NULL);
1310         if (!utf8path) {
1311                 g_warning("mh_filename_to_utf8: failed to convert character set");
1312                 utf8path = g_strdup(path);
1313         }
1314
1315         return utf8path;
1316 }
1317
1318 static gint sort_cache_list_by_msgnum(gconstpointer a, gconstpointer b)
1319 {
1320         MsgInfo *msginfo_a = (MsgInfo *) a;
1321         MsgInfo *msginfo_b = (MsgInfo *) b;
1322
1323         return (msginfo_a->msgnum - msginfo_b->msgnum);
1324 }
1325
1326 static gchar *get_unseen_seq_name(void)
1327 {
1328         static gchar *seq_name = NULL;
1329         if (!seq_name) {
1330                 gchar buf[BUFFSIZE];
1331                 gchar *tmp;
1332                 gchar *profile_path = g_strconcat(
1333                         get_home_dir(), G_DIR_SEPARATOR_S,
1334                         ".mh_profile", NULL);
1335                 FILE *fp = claws_fopen(profile_path, "r");
1336                 g_free(profile_path);
1337                 if (fp) {
1338                         while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
1339                                 if (!strncmp(buf, "Unseen-Sequence:", strlen("Unseen-Sequence:"))) {
1340                                         gchar *seq_tmp = buf+strlen("Unseen-Sequence:");
1341                                         while (*seq_tmp == ' ')
1342                                                 seq_tmp++;
1343                                         seq_name = g_strdup(seq_tmp);
1344                                         seq_name = strretchomp(seq_name);
1345                                         break;
1346                                 }
1347                         }
1348                         claws_fclose(fp);
1349                 }
1350                 if (!seq_name)
1351                         seq_name = g_strdup("unseen");
1352                 tmp = g_strdup_printf("%s:", seq_name);
1353                 g_free(seq_name);
1354                 seq_name = tmp;
1355         }
1356         return seq_name;        
1357 }
1358
1359 static void mh_write_sequences(FolderItem *item, gboolean remove_unseen)
1360 {
1361         gchar *mh_sequences_old, *mh_sequences_new;
1362         FILE *mh_sequences_old_fp, *mh_sequences_new_fp;
1363         gchar buf[BUFFSIZE];
1364         gchar *path = NULL;
1365         gboolean err = FALSE;
1366         START_TIMING("");
1367
1368         if (!item)
1369                 return;
1370         
1371         path = folder_item_get_path(item);
1372
1373         mh_sequences_old = g_strconcat(path, G_DIR_SEPARATOR_S,
1374                                             ".mh_sequences", NULL);
1375         mh_sequences_new = g_strconcat(path, G_DIR_SEPARATOR_S,
1376                                             ".mh_sequences.new", NULL);
1377         if ((mh_sequences_new_fp = claws_fopen(mh_sequences_new, "w+b")) != NULL) {
1378                 GSList *msglist = folder_item_get_msg_list(item);
1379                 GSList *cur;
1380                 MsgInfo *info = NULL;
1381                 gint start = -1, end = -1;
1382                 gchar *sequence = g_strdup("");
1383                 gint seq_len = 0;
1384                 msglist = g_slist_sort(msglist, sort_cache_list_by_msgnum);
1385                 cur = msglist;
1386                 
1387                 /* write the unseen sequence if we don't have to scrap it */
1388                 if (!remove_unseen) do {
1389                         info = (MsgInfo *)(cur ? cur->data:NULL);
1390                         if (info && (MSG_IS_UNREAD(info->flags) || MSG_IS_NEW(info->flags))) {
1391                                 if (start < 0)
1392                                         start = end = info->msgnum;
1393                                 else
1394                                         end = info->msgnum;
1395                         } else {
1396                                 if (start > 0 && end > 0) {
1397                                         gchar tmp[32];
1398                                         gint tmp_len = 0;
1399                                         if (start != end)
1400                                                 snprintf(tmp, 31, " %d-%d", start, end);
1401                                         else
1402                                                 snprintf(tmp, 31, " %d", start);
1403                                         
1404                                         tmp_len = strlen(tmp);
1405                                         sequence = g_realloc(sequence, seq_len+tmp_len+1);
1406                                         strcpy(sequence+seq_len, tmp);
1407                                         seq_len += tmp_len;
1408
1409                                         start = end = -1;
1410                                 }
1411                         }
1412                         cur = cur ? cur->next:NULL;
1413                 } while (cur || (start > 0 && end > 0));
1414                 if (sequence && *sequence) {
1415                         if (fprintf(mh_sequences_new_fp, "%s%s\n", 
1416                                         get_unseen_seq_name(), sequence) < 0)
1417                                 err = TRUE;
1418                         else
1419                                 debug_print("wrote unseen sequence: '%s%s'\n", 
1420                                         get_unseen_seq_name(), sequence);
1421                 }
1422                 /* rewrite the rest of the file */
1423                 if ((mh_sequences_old_fp = claws_fopen(mh_sequences_old, "r+b")) != NULL) {
1424                         while (claws_fgets(buf, sizeof(buf), mh_sequences_old_fp) != NULL) {
1425                                 if (strncmp(buf, get_unseen_seq_name(), strlen(get_unseen_seq_name())))
1426                                         if (fprintf(mh_sequences_new_fp, "%s", buf) < 0) {
1427                                                 err = TRUE;
1428                                                 break;
1429                                         }
1430                         }
1431                         claws_fclose(mh_sequences_old_fp);
1432                 }
1433                 
1434                 if (claws_safe_fclose(mh_sequences_new_fp) == EOF)
1435                         err = TRUE;
1436
1437                 if (!err) {
1438                         if (g_rename(mh_sequences_new, mh_sequences_old) < 0)
1439                                 FILE_OP_ERROR(mh_sequences_new, "rename");
1440                 }
1441                 g_free(sequence);
1442                 procmsg_msg_list_free(msglist);
1443         }
1444         g_free(mh_sequences_old);
1445         g_free(mh_sequences_new);
1446         g_free(path);
1447
1448         END_TIMING();
1449 }
1450
1451 static int mh_item_close(Folder *folder, FolderItem *item)
1452 {
1453         time_t last_mtime = (time_t)0;
1454         gboolean need_scan = mh_scan_required(item->folder, item);
1455         last_mtime = item->mtime;
1456
1457         mh_write_sequences(item, FALSE);
1458
1459         if (item->mtime == last_mtime && !need_scan) {
1460                 mh_set_mtime(folder, item);
1461         }
1462
1463         return 0;
1464 }
1465
1466 static void mh_set_mtime(Folder *folder, FolderItem *item)
1467 {
1468         GStatBuf s;
1469         gchar *path = folder_item_get_path(item);
1470
1471         cm_return_if_fail(path != NULL);
1472
1473         if (g_stat(path, &s) < 0) {
1474                 FILE_OP_ERROR(path, "stat");
1475                 g_free(path);
1476                 return;
1477         }
1478
1479         item->mtime = s.st_mtime;
1480         debug_print("MH: forced mtime of %s to %"CM_TIME_FORMAT"\n", item->name?item->name:"(null)", item->mtime);
1481         g_free(path);
1482 }