92e97b191138f570392432605f83edfecd6e9805
[claws.git] / src / mh.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws 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 2 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, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27 #include <glib/gi18n.h>
28 #include <dirent.h>
29 #include <sys/stat.h>
30 #include <unistd.h>
31 #include <string.h>
32 #include <errno.h>
33
34 #undef MEASURE_TIME
35
36 #ifdef MEASURE_TIME
37 #  include <sys/time.h>
38 #endif
39
40 #include "folder.h"
41 #include "mh.h"
42 #include "procmsg.h"
43 #include "procheader.h"
44 #include "utils.h"
45 #include "codeconv.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                                          GRelation      *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                                          GRelation      *relation);
88 static gint     mh_remove_msg           (Folder         *folder,
89                                          FolderItem     *item,
90                                          gint            num);
91 static gint     mh_remove_all_msg       (Folder         *folder,
92                                          FolderItem     *item);
93 static gboolean mh_is_msg_changed       (Folder         *folder,
94                                          FolderItem     *item,
95                                          MsgInfo        *msginfo);
96
97 static gint     mh_get_num_list         (Folder         *folder,
98                                          FolderItem     *item, 
99                                          GSList         **list, 
100                                          gboolean       *old_uids_valid);
101 static gint     mh_scan_tree            (Folder         *folder);
102
103 static gint    mh_create_tree           (Folder         *folder);
104 static FolderItem *mh_create_folder     (Folder         *folder,
105                                          FolderItem     *parent,
106                                          const gchar    *name);
107 static gint    mh_rename_folder         (Folder         *folder,
108                                          FolderItem     *item,
109                                          const gchar    *name);
110 static gint    mh_remove_folder         (Folder         *folder,
111                                          FolderItem     *item);
112
113 static gchar   *mh_get_new_msg_filename         (FolderItem     *dest);
114
115 static MsgInfo *mh_parse_msg                    (const gchar    *file,
116                                                  FolderItem     *item);
117 static void     mh_remove_missing_folder_items  (Folder         *folder);
118 static gchar    *mh_filename_from_utf8          (const gchar    *path);
119 static gchar    *mh_filename_to_utf8            (const gchar    *path);
120 static void     mh_scan_tree_recursive          (FolderItem     *item);
121
122 static gboolean mh_rename_folder_func           (GNode          *node,
123                                                  gpointer        data);
124 static gchar   *mh_item_get_path                (Folder *folder, 
125                                                  FolderItem *item);
126
127 static gboolean mh_scan_required        (Folder         *folder,
128                                          FolderItem     *item);
129 static int mh_item_close                (Folder         *folder,
130                                          FolderItem     *item);
131 static gint mh_get_flags                (Folder *folder, FolderItem *item,
132                                          MsgInfoList *msginfo_list, GRelation *msgflags);
133 static void mh_write_sequences          (FolderItem     *item);
134
135 static FolderClass mh_class;
136
137 FolderClass *mh_get_class(void)
138 {
139         if (mh_class.idstr == NULL) {
140                 mh_class.type = F_MH;
141                 mh_class.idstr = "mh";
142                 mh_class.uistr = "MH";
143                 
144                 /* Folder functions */
145                 mh_class.new_folder = mh_folder_new;
146                 mh_class.destroy_folder = mh_folder_destroy;
147                 mh_class.set_xml = folder_local_set_xml;
148                 mh_class.get_xml = folder_local_get_xml;
149                 mh_class.scan_tree = mh_scan_tree;
150                 mh_class.create_tree = mh_create_tree;
151
152                 /* FolderItem functions */
153                 mh_class.item_get_path = mh_item_get_path;
154                 mh_class.create_folder = mh_create_folder;
155                 mh_class.rename_folder = mh_rename_folder;
156                 mh_class.remove_folder = mh_remove_folder;
157                 mh_class.get_num_list = mh_get_num_list;
158                 mh_class.scan_required = mh_scan_required;
159                 mh_class.close = mh_item_close;
160                 mh_class.get_flags = mh_get_flags;
161
162                 /* Message functions */
163                 mh_class.get_msginfo = mh_get_msginfo;
164                 mh_class.fetch_msg = mh_fetch_msg;
165                 mh_class.add_msg = mh_add_msg;
166                 mh_class.add_msgs = mh_add_msgs;
167                 mh_class.copy_msg = mh_copy_msg;
168                 mh_class.copy_msgs = mh_copy_msgs;
169                 mh_class.remove_msg = mh_remove_msg;
170                 mh_class.remove_all_msg = mh_remove_all_msg;
171                 mh_class.is_msg_changed = mh_is_msg_changed;
172         }
173
174         return &mh_class;
175 }
176
177 static Folder *mh_folder_new(const gchar *name, const gchar *path)
178 {
179         Folder *folder;
180
181         folder = (Folder *)g_new0(MHFolder, 1);
182         folder->klass = &mh_class;
183         mh_folder_init(folder, name, path);
184
185         return folder;
186 }
187
188 static void mh_folder_destroy(Folder *folder)
189 {
190         folder_local_folder_destroy(LOCAL_FOLDER(folder));
191 }
192
193 static void mh_folder_init(Folder *folder, const gchar *name, const gchar *path)
194 {
195         folder_local_folder_init(folder, name, path);
196
197 }
198
199 gboolean mh_scan_required(Folder *folder, FolderItem *item)
200 {
201         gchar *path;
202         struct stat s;
203
204         path = folder_item_get_path(item);
205         g_return_val_if_fail(path != NULL, FALSE);
206
207         if (stat(path, &s) < 0) {
208                 FILE_OP_ERROR(path, "stat");
209                 g_free(path);
210                 return FALSE;
211         }
212
213         if ((s.st_mtime > item->mtime) &&
214                 (s.st_mtime - 3600 != item->mtime)) {
215                 debug_print("MH scan required, folder updated: %s (%ld > %ld)\n",
216                             path,
217                             (long int) s.st_mtime,
218                             (long int) item->mtime);
219                 g_free(path);
220                 return TRUE;
221         }
222
223         debug_print("MH scan not required: %s (%ld <= %ld)\n",
224                     path,
225                     (long int) s.st_mtime,
226                     (long int) item->mtime);
227         g_free(path);
228         return FALSE;
229 }
230
231 void mh_get_last_num(Folder *folder, FolderItem *item)
232 {
233         gchar *path;
234         DIR *dp;
235         struct dirent *d;
236         gint max = 0;
237         gint num;
238
239         g_return_if_fail(item != NULL);
240
241         debug_print("mh_get_last_num(): Scanning %s ...\n", item->path);
242
243         path = folder_item_get_path(item);
244         g_return_if_fail(path != NULL);
245         if (change_dir(path) < 0) {
246                 g_free(path);
247                 return;
248         }
249         g_free(path);
250
251         if ((dp = opendir(".")) == NULL) {
252                 FILE_OP_ERROR(item->path, "opendir");
253                 return;
254         }
255
256         while ((d = readdir(dp)) != NULL) {
257                 if ((num = to_number(d->d_name)) > 0 &&
258                     dirent_is_regular_file(d)) {
259                         if (max < num)
260                                 max = num;
261                 }
262         }
263         closedir(dp);
264
265         debug_print("Last number in dir %s = %d\n", item->path, max);
266         item->last_num = max;
267 }
268
269 gint mh_get_num_list(Folder *folder, FolderItem *item, GSList **list, gboolean *old_uids_valid)
270 {
271
272         gchar *path;
273         DIR *dp;
274         struct dirent *d;
275         gint num, nummsgs = 0;
276
277         g_return_val_if_fail(item != NULL, -1);
278
279         debug_print("mh_get_num_list(): Scanning %s ...\n", item->path);
280
281         *old_uids_valid = TRUE;
282
283         path = folder_item_get_path(item);
284         g_return_val_if_fail(path != NULL, -1);
285         if (change_dir(path) < 0) {
286                 g_free(path);
287                 return -1;
288         }
289         g_free(path);
290
291         if ((dp = opendir(".")) == NULL) {
292                 FILE_OP_ERROR(item->path, "opendir");
293                 return -1;
294         }
295
296         while ((d = readdir(dp)) != NULL) {
297                 if ((num = to_number(d->d_name)) > 0) {
298                         *list = g_slist_prepend(*list, GINT_TO_POINTER(num));
299                         nummsgs++;
300                 }
301         }
302         closedir(dp);
303
304         item->mtime = time(NULL);
305         return nummsgs;
306 }
307
308 static gchar *mh_fetch_msg(Folder *folder, FolderItem *item, gint num)
309 {
310         gchar *path;
311         gchar *file;
312
313         g_return_val_if_fail(item != NULL, NULL);
314         g_return_val_if_fail(num > 0, NULL);
315
316         path = folder_item_get_path(item);
317         file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
318
319         if (!is_file_exist(file)) {
320                 g_free(file);
321                 g_free(path);
322                 return NULL;
323         }
324         g_free(path);
325         return file;
326 }
327
328 static MsgInfo *mh_get_msginfo(Folder *folder, FolderItem *item, gint num)
329 {
330         MsgInfo *msginfo;
331         gchar *file;
332
333         g_return_val_if_fail(item != NULL, NULL);
334         g_return_val_if_fail(num > 0, NULL);
335
336         file = mh_fetch_msg(folder, item, num);
337         if (!file) return NULL;
338
339         msginfo = mh_parse_msg(file, item);
340         if (msginfo)
341                 msginfo->msgnum = num;
342
343         g_free(file);
344
345         return msginfo;
346 }
347
348 static gchar *mh_get_new_msg_filename(FolderItem *dest)
349 {
350         gchar *destfile;
351         gchar *destpath;
352
353         destpath = folder_item_get_path(dest);
354         g_return_val_if_fail(destpath != NULL, NULL);
355
356         if (!is_dir_exist(destpath))
357                 make_dir_hier(destpath);
358
359         for (;;) {
360                 destfile = g_strdup_printf("%s%c%d", destpath, G_DIR_SEPARATOR,
361                                            dest->last_num + 1);
362                 if (is_file_entry_exist(destfile)) {
363                         dest->last_num++;
364                         g_free(destfile);
365                 } else
366                         break;
367         }
368
369         g_free(destpath);
370
371         return destfile;
372 }
373
374 static gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file, MsgFlags *flags)
375 {
376         gint ret;
377         GSList file_list;
378         MsgFileInfo fileinfo;
379
380         g_return_val_if_fail(file != NULL, -1);
381
382         fileinfo.msginfo = NULL;
383         fileinfo.file = (gchar *)file;
384         fileinfo.flags = flags;
385         file_list.data = &fileinfo;
386         file_list.next = NULL;
387
388         ret = mh_add_msgs(folder, dest, &file_list, NULL);
389         return ret;
390
391  
392 static gint mh_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list, 
393                  GRelation *relation)
394
395         gchar *destfile;
396         GSList *cur;
397         MsgFileInfo *fileinfo;
398
399         g_return_val_if_fail(dest != NULL, -1);
400         g_return_val_if_fail(file_list != NULL, -1);
401
402         if (dest->last_num < 0) {
403                 mh_get_last_num(folder, dest);
404                 if (dest->last_num < 0) return -1;
405         }
406
407         for (cur = file_list; cur != NULL; cur = cur->next) {
408                 fileinfo = (MsgFileInfo *)cur->data;
409
410                 destfile = mh_get_new_msg_filename(dest);
411                 if (destfile == NULL) return -1;
412
413 #ifdef G_OS_UNIX
414                 if (link(fileinfo->file, destfile) < 0) {
415 #endif
416                         if (copy_file(fileinfo->file, destfile, TRUE) < 0) {
417                                 g_warning(_("can't copy message %s to %s\n"),
418                                           fileinfo->file, destfile);
419                                 g_free(destfile);
420                                 return -1;
421                         }
422 #ifdef G_OS_UNIX
423                 }
424 #endif
425
426                 if (relation != NULL)
427                         g_relation_insert(relation, fileinfo, GINT_TO_POINTER(dest->last_num + 1));
428                 g_free(destfile);
429                 dest->last_num++;
430         }
431         mh_write_sequences(dest);
432         return dest->last_num;
433 }
434
435 static gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
436 {
437         GSList msglist;
438
439         g_return_val_if_fail(msginfo != NULL, -1);
440
441         msglist.data = msginfo;
442         msglist.next = NULL;
443
444         return mh_copy_msgs(folder, dest, &msglist, NULL);      
445 }
446
447 static gint mh_copy_msgs(Folder *folder, FolderItem *dest, MsgInfoList *msglist, 
448                          GRelation *relation)
449 {
450         gboolean dest_need_scan = FALSE;
451         gchar *srcfile;
452         gchar *destfile;
453         gint filemode = 0;
454         FolderItemPrefs *prefs;
455         MsgInfo *msginfo = NULL;
456         gboolean remove_special_headers = FALSE;
457         MsgInfoList *cur = NULL;
458         g_return_val_if_fail(dest != NULL, -1);
459         g_return_val_if_fail(msglist != NULL, -1);
460         
461         msginfo = (MsgInfo *)msglist->data;
462
463         g_return_val_if_fail(msginfo != NULL, -1);
464
465         if (msginfo->folder == dest) {
466                 g_warning("the src folder is identical to the dest.\n");
467                 return -1;
468         }
469
470         if (dest->last_num < 0) {
471                 mh_get_last_num(folder, dest);
472                 if (dest->last_num < 0) return -1;
473         }
474
475         if ((MSG_IS_QUEUED(msginfo->flags) || MSG_IS_DRAFT(msginfo->flags))
476         && !folder_has_parent_of_type(dest, F_QUEUE)
477         && !folder_has_parent_of_type(dest, F_DRAFT)) {
478                 /* as every msginfo comes from the same folder, it is supposed they
479                  * will either match the preceding condition either all or none.
480                  */
481                 remove_special_headers = TRUE;
482         } else if (!(MSG_IS_QUEUED(msginfo->flags) || MSG_IS_DRAFT(msginfo->flags))
483         && (folder_has_parent_of_type(dest, F_QUEUE)
484          || folder_has_parent_of_type(dest, F_DRAFT))) {
485                 return -1;
486         } 
487
488         prefs = dest->prefs;
489
490         for (cur = msglist; cur; cur = cur->next) {
491                 msginfo = (MsgInfo *)cur->data;
492                 if (!msginfo)
493                         continue;
494                 srcfile = procmsg_get_message_file(msginfo);
495                 destfile = mh_get_new_msg_filename(dest);
496                 if (!destfile) {
497                         g_free(srcfile);
498                         continue;
499                 }
500
501                 debug_print("Copying message %s%c%d to %s ...\n",
502                             msginfo->folder->path, G_DIR_SEPARATOR,
503                             msginfo->msgnum, dest->path);
504
505
506                 if (remove_special_headers) {
507                         if (procmsg_remove_special_headers(srcfile, destfile) !=0) {
508                                 g_free(srcfile);
509                                 g_free(destfile);
510                                 continue;
511                         }
512                 } else if (copy_file(srcfile, destfile, TRUE) < 0) {
513                         FILE_OP_ERROR(srcfile, "copy");
514                         g_free(srcfile);
515                         g_free(destfile);
516                         continue;
517                 }
518                 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
519                         if (chmod(destfile, prefs->folder_chmod) < 0)
520                                 FILE_OP_ERROR(destfile, "chmod");
521
522                         /* for mark file */
523                         filemode = prefs->folder_chmod;
524                         if (filemode & S_IRGRP) filemode |= S_IWGRP;
525                         if (filemode & S_IROTH) filemode |= S_IWOTH;
526                 }
527                 if (relation)
528                         g_relation_insert(relation, msginfo, GINT_TO_POINTER(dest->last_num+1));
529                 g_free(srcfile);
530                 g_free(destfile);
531                 dest->last_num++;
532         }
533
534         dest_need_scan = mh_scan_required(dest->folder, dest);
535         if (!dest_need_scan)
536                 dest->mtime = time(NULL);
537         else
538                 mh_write_sequences(dest);
539
540         return dest->last_num;
541 }
542
543 static gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
544 {
545         gboolean need_scan = FALSE;
546         gchar *file;
547
548         g_return_val_if_fail(item != NULL, -1);
549
550         file = mh_fetch_msg(folder, item, num);
551         g_return_val_if_fail(file != NULL, -1);
552
553         need_scan = mh_scan_required(folder, item);
554
555         if (g_unlink(file) < 0) {
556                 FILE_OP_ERROR(file, "unlink");
557                 g_free(file);
558                 return -1;
559         }
560
561         if (!need_scan)
562                 item->mtime = time(NULL);
563
564         g_free(file);
565         return 0;
566 }
567
568 static gint mh_remove_all_msg(Folder *folder, FolderItem *item)
569 {
570         gchar *path;
571         gint val;
572
573         g_return_val_if_fail(item != NULL, -1);
574
575         path = folder_item_get_path(item);
576         g_return_val_if_fail(path != NULL, -1);
577         val = remove_all_numbered_files(path);
578         g_free(path);
579
580         mh_write_sequences(item);
581
582         return val;
583 }
584
585 static gboolean mh_is_msg_changed(Folder *folder, FolderItem *item,
586                                   MsgInfo *msginfo)
587 {
588         struct stat s;
589
590         if (g_stat(itos(msginfo->msgnum), &s) < 0 ||
591             msginfo->size  != s.st_size || (
592                 (msginfo->mtime - s.st_mtime != 0) &&
593                 (msginfo->mtime - s.st_mtime != 3600) &&
594                 (msginfo->mtime - s.st_mtime != -3600)))
595                 return TRUE;
596
597         return FALSE;
598 }
599
600 static gint mh_scan_tree(Folder *folder)
601 {
602         FolderItem *item;
603         gchar *rootpath;
604
605         g_return_val_if_fail(folder != NULL, -1);
606
607         if (!folder->node) {
608                 item = folder_item_new(folder, folder->name, NULL);
609                 item->folder = folder;
610                 folder->node = item->node = g_node_new(item);
611         } else
612                 item = FOLDER_ITEM(folder->node->data);
613
614         rootpath = folder_item_get_path(item);
615         if (change_dir(rootpath) < 0) {
616                 g_free(rootpath);
617                 return -1;
618         }
619         g_free(rootpath);
620
621         mh_create_tree(folder);
622         mh_remove_missing_folder_items(folder);
623         mh_scan_tree_recursive(item);
624
625         return 0;
626 }
627
628 #define MAKE_DIR_IF_NOT_EXIST(dir) \
629 { \
630         if (!is_dir_exist(dir)) { \
631                 if (is_file_exist(dir)) { \
632                         g_warning("File `%s' already exists.\n" \
633                                     "Can't create folder.", dir); \
634                         return -1; \
635                 } \
636                 if (make_dir(dir) < 0) \
637                         return -1; \
638         } \
639 }
640
641 static gint mh_create_tree(Folder *folder)
642 {
643         gchar *rootpath;
644
645         g_return_val_if_fail(folder != NULL, -1);
646
647         CHDIR_RETURN_VAL_IF_FAIL(get_mail_base_dir(), -1);
648         rootpath = LOCAL_FOLDER(folder)->rootpath;
649         MAKE_DIR_IF_NOT_EXIST(rootpath);
650         CHDIR_RETURN_VAL_IF_FAIL(rootpath, -1);
651         MAKE_DIR_IF_NOT_EXIST(INBOX_DIR);
652         MAKE_DIR_IF_NOT_EXIST(OUTBOX_DIR);
653         MAKE_DIR_IF_NOT_EXIST(QUEUE_DIR);
654         MAKE_DIR_IF_NOT_EXIST(DRAFT_DIR);
655         MAKE_DIR_IF_NOT_EXIST(TRASH_DIR);
656
657         return 0;
658 }
659
660 #undef MAKE_DIR_IF_NOT_EXIST
661
662 static gchar *mh_item_get_path(Folder *folder, FolderItem *item)
663 {
664         gchar *folder_path, *path;
665         gchar *real_path;
666         g_return_val_if_fail(folder != NULL, NULL);
667         g_return_val_if_fail(item != NULL, NULL);
668
669         folder_path = g_strdup(LOCAL_FOLDER(folder)->rootpath);
670         g_return_val_if_fail(folder_path != NULL, NULL);
671
672         /* FIXME: [W32] The code below does not correctly merge
673            relative filenames; there should be a function to handle
674            this.  */
675         if ( !is_relative_filename (folder_path) ) {
676                 if (item->path)
677                         path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
678                                            item->path, NULL);
679                 else
680                         path = g_strdup(folder_path);
681         } else {
682                 if (item->path)
683                         path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
684                                            folder_path, G_DIR_SEPARATOR_S,
685                                            item->path, NULL);
686                 else
687                         path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
688                                            folder_path, NULL);
689         }
690         g_free(folder_path);
691         real_path = mh_filename_from_utf8(path);
692         if (!is_dir_exist(real_path) && is_dir_exist(path)) {
693                 /* mmh, older version did put utf8 filenames instead of
694                  * the correct encoding */
695                 rename(path, real_path);
696                 folder_item_scan(item);
697         }
698
699         g_free(path);
700         return real_path;
701 }
702
703 static FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
704                                     const gchar *name)
705 {
706         gchar *path, *real_name;
707         gchar *fullpath;
708         FolderItem *new_item;
709         gchar *mh_sequences_filename;
710         FILE *mh_sequences_file;
711
712         g_return_val_if_fail(folder != NULL, NULL);
713         g_return_val_if_fail(parent != NULL, NULL);
714         g_return_val_if_fail(name != NULL, NULL);
715
716         path = folder_item_get_path(parent);
717         if (!is_dir_exist(path)) 
718                 if (make_dir_hier(path) != 0)
719                         return NULL;
720                 
721         real_name = mh_filename_from_utf8(name);
722         fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, real_name, NULL);
723         g_free(real_name);
724         g_free(path);
725
726         if (make_dir(fullpath) < 0) {
727                 g_free(fullpath);
728                 return NULL;
729         }
730
731         g_free(fullpath);
732
733         if (parent->path)
734                 path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
735                                    NULL);
736         else
737                 path = g_strdup(name);
738         new_item = folder_item_new(folder, name, path);
739         folder_item_append(parent, new_item);
740
741         g_free(path);
742
743         path = folder_item_get_path(new_item);
744         mh_sequences_filename = g_strconcat(path, G_DIR_SEPARATOR_S,
745                                             ".mh_sequences", NULL);
746         if ((mh_sequences_file = g_fopen(mh_sequences_filename, "a+b")) != NULL) {
747                 fclose(mh_sequences_file);
748         }
749         g_free(mh_sequences_filename);
750         g_free(path);
751
752         return new_item;
753 }
754
755 static gint mh_rename_folder(Folder *folder, FolderItem *item,
756                              const gchar *name)
757 {
758         gchar *real_name;
759         gchar *oldpath;
760         gchar *dirname;
761         gchar *newpath, *utf8newpath;
762         gchar *paths[2];
763
764         g_return_val_if_fail(folder != NULL, -1);
765         g_return_val_if_fail(item != NULL, -1);
766         g_return_val_if_fail(item->path != NULL, -1);
767         g_return_val_if_fail(name != NULL, -1);
768
769         oldpath = folder_item_get_path(item);
770         if (!is_dir_exist(oldpath))
771                 make_dir_hier(oldpath);
772
773         dirname = g_path_get_dirname(oldpath);
774         real_name = mh_filename_from_utf8(name);
775         newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, real_name, NULL);
776         g_free(real_name);
777
778         if (g_rename(oldpath, newpath) < 0) {
779                 FILE_OP_ERROR(oldpath, "rename");
780                 g_free(oldpath);
781                 g_free(newpath);
782                 return -1;
783         }
784
785         g_free(oldpath);
786         g_free(newpath);
787
788         if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
789                 dirname = g_path_get_dirname(item->path);
790                 utf8newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S,
791                                           name, NULL);
792                 g_free(dirname);
793         } else
794                 utf8newpath = g_strdup(name);
795
796         g_free(item->name);
797         item->name = g_strdup(name);
798
799         paths[0] = g_strdup(item->path);
800         paths[1] = utf8newpath;
801         g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
802                         mh_rename_folder_func, paths);
803
804         g_free(paths[0]);
805         g_free(paths[1]);
806         return 0;
807 }
808
809 static gint mh_remove_folder(Folder *folder, FolderItem *item)
810 {
811         gchar *path;
812
813         g_return_val_if_fail(folder != NULL, -1);
814         g_return_val_if_fail(item != NULL, -1);
815         g_return_val_if_fail(item->path != NULL, -1);
816
817         path = folder_item_get_path(item);
818         if (remove_dir_recursive(path) < 0) {
819                 g_warning("can't remove directory `%s'\n", path);
820                 g_free(path);
821                 return -1;
822         }
823
824         g_free(path);
825         folder_item_remove(item);
826         return 0;
827 }
828
829 static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
830 {
831         MsgInfo *msginfo;
832         MsgFlags flags;
833
834         g_return_val_if_fail(item != NULL, NULL);
835         g_return_val_if_fail(file != NULL, NULL);
836
837         flags.perm_flags = MSG_NEW|MSG_UNREAD;
838         flags.tmp_flags = 0;
839
840         if (folder_has_parent_of_type(item, F_QUEUE)) {
841                 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
842         } else if (folder_has_parent_of_type(item, F_DRAFT)) {
843                 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
844         }
845
846         msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
847         if (!msginfo) return NULL;
848
849         msginfo->msgnum = atoi(file);
850         msginfo->folder = item;
851
852         return msginfo;
853 }
854
855 static gboolean mh_remove_missing_folder_items_func(GNode *node, gpointer data)
856 {
857         FolderItem *item;
858         gchar *path;
859
860         g_return_val_if_fail(node->data != NULL, FALSE);
861
862         if (G_NODE_IS_ROOT(node))
863                 return FALSE;
864
865         item = FOLDER_ITEM(node->data);
866
867         path = folder_item_get_path(item);
868         if (!is_dir_exist(path)) {
869                 debug_print("folder '%s' not found. removing...\n", path);
870                 folder_item_remove(item);
871         }
872         g_free(path);
873
874         return FALSE;
875 }
876
877 static void mh_remove_missing_folder_items(Folder *folder)
878 {
879         g_return_if_fail(folder != NULL);
880
881         debug_print("searching missing folders...\n");
882
883         g_node_traverse(folder->node, G_POST_ORDER, G_TRAVERSE_ALL, -1,
884                         mh_remove_missing_folder_items_func, folder);
885 }
886
887 static void mh_scan_tree_recursive(FolderItem *item)
888 {
889         Folder *folder;
890 #ifdef G_OS_WIN32
891         GDir *dir;
892 #else
893         DIR *dp;
894         struct dirent *d;
895 #endif
896         const gchar *dir_name;
897         struct stat s;
898         gchar *real_path, *entry, *utf8entry, *utf8name;
899         gint n_msg = 0;
900
901         g_return_if_fail(item != NULL);
902         g_return_if_fail(item->folder != NULL);
903
904         folder = item->folder;
905
906         real_path = item->path ? mh_filename_from_utf8(item->path) : g_strdup(".");
907 #ifdef G_OS_WIN32
908         dir = g_dir_open(real_path, 0, NULL);
909         if (!dir) {
910                 g_warning("failed to open directory: %s\n", real_path);
911                 g_free(real_path);
912                 return;
913         }
914 #else
915         dp = opendir(real_path);
916         if (!dp) {
917                 FILE_OP_ERROR(real_path, "opendir");
918                 return;
919         }
920 #endif
921         g_free(real_path);
922
923         debug_print("scanning %s ...\n",
924                     item->path ? item->path
925                     : LOCAL_FOLDER(item->folder)->rootpath);
926         if (folder->ui_func)
927                 folder->ui_func(folder, item, folder->ui_func_data);
928
929 #ifdef G_OS_WIN32
930         while ((dir_name = g_dir_read_name(dir)) != NULL) {
931 #else
932         while ((d = readdir(dp)) != NULL) {
933                 dir_name = d->d_name;
934 #endif
935                 if (dir_name[0] == '.') continue;
936
937                 utf8name = mh_filename_to_utf8(dir_name);
938                 if (item->path)
939                         utf8entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
940                                                 utf8name, NULL);
941                 else
942                         utf8entry = g_strdup(utf8name);
943                 entry = mh_filename_from_utf8(utf8entry);
944
945                 if (
946 #if !defined(G_OS_WIN32) && defined(HAVE_DIRENT_D_TYPE)
947                         d->d_type == DT_DIR ||
948                         (d->d_type == DT_UNKNOWN &&
949 #endif
950                         g_stat(entry, &s) == 0 && S_ISDIR(s.st_mode)
951 #if !defined(G_OS_WIN32) && defined(HAVE_DIRENT_D_TYPE)
952                         )
953 #endif
954                    ) {
955                         FolderItem *new_item = NULL;
956                         GNode *node;
957
958                         node = item->node;
959                         for (node = node->children; node != NULL; node = node->next) {
960                                 FolderItem *cur_item = FOLDER_ITEM(node->data);
961                                 if (!strcmp2(cur_item->path, entry)) {
962                                         new_item = cur_item;
963                                         break;
964                                 }
965                         }
966                         if (!new_item) {
967                                 debug_print("new folder '%s' found.\n", entry);
968                                 new_item = folder_item_new(folder, utf8name, utf8entry);
969                                 folder_item_append(item, new_item);
970                         }
971
972                         if (!item->path) {
973                                 if (!folder->inbox &&
974                                     !strcmp(dir_name, INBOX_DIR)) {
975                                         new_item->stype = F_INBOX;
976                                         folder->inbox = new_item;
977                                 } else if (!folder->outbox &&
978                                            !strcmp(dir_name, OUTBOX_DIR)) {
979                                         new_item->stype = F_OUTBOX;
980                                         folder->outbox = new_item;
981                                 } else if (!folder->draft &&
982                                            !strcmp(dir_name, DRAFT_DIR)) {
983                                         new_item->stype = F_DRAFT;
984                                         folder->draft = new_item;
985                                 } else if (!folder->queue &&
986                                            !strcmp(dir_name, QUEUE_DIR)) {
987                                         new_item->stype = F_QUEUE;
988                                         folder->queue = new_item;
989                                 } else if (!folder->trash &&
990                                            !strcmp(dir_name, TRASH_DIR)) {
991                                         new_item->stype = F_TRASH;
992                                         folder->trash = new_item;
993                                 }
994                         }
995
996                         mh_scan_tree_recursive(new_item);
997                 } else if (to_number(dir_name) > 0) n_msg++;
998
999                 g_free(entry);
1000                 g_free(utf8entry);
1001                 g_free(utf8name);
1002         }
1003
1004 #ifdef G_OS_WIN32
1005         g_dir_close(dir);
1006 #else
1007         closedir(dp);
1008 #endif
1009
1010         item->mtime = time(NULL);
1011 }
1012
1013 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
1014 {
1015         FolderItem *item = node->data;
1016         gchar **paths = data;
1017         const gchar *oldpath = paths[0];
1018         const gchar *newpath = paths[1];
1019         gchar *base;
1020         gchar *new_itempath;
1021         gint oldpathlen;
1022
1023         oldpathlen = strlen(oldpath);
1024         if (strncmp(oldpath, item->path, oldpathlen) != 0) {
1025                 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
1026                 return TRUE;
1027         }
1028
1029         base = item->path + oldpathlen;
1030         while (*base == G_DIR_SEPARATOR) base++;
1031         if (*base == '\0')
1032                 new_itempath = g_strdup(newpath);
1033         else
1034                 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
1035                                            NULL);
1036         g_free(item->path);
1037         item->path = new_itempath;
1038
1039         return FALSE;
1040 }
1041
1042 static gchar *mh_filename_from_utf8(const gchar *path)
1043 {
1044         gchar *real_path = g_filename_from_utf8(path, -1, NULL, NULL, NULL);
1045
1046         if (!real_path) {
1047                 g_warning("mh_filename_from_utf8: faild to convert character set\n");
1048                 real_path = g_strdup(path);
1049         }
1050
1051         return real_path;
1052 }
1053
1054 static gchar *mh_filename_to_utf8(const gchar *path)
1055 {
1056         gchar *utf8path = g_filename_to_utf8(path, -1, NULL, NULL, NULL);
1057         if (!utf8path) {
1058                 g_warning("mh_filename_to_utf8: faild to convert character set\n");
1059                 utf8path = g_strdup(path);
1060         }
1061
1062         return utf8path;
1063 }
1064
1065 static gint sort_cache_list_by_msgnum(gconstpointer a, gconstpointer b)
1066 {
1067         MsgInfo *msginfo_a = (MsgInfo *) a;
1068         MsgInfo *msginfo_b = (MsgInfo *) b;
1069
1070         return (msginfo_a->msgnum - msginfo_b->msgnum);
1071 }
1072
1073 static gchar *get_unseen_seq_name(void)
1074 {
1075         static gchar *seq_name = NULL;
1076         if (!seq_name) {
1077                 gchar buf[BUFFSIZE];
1078                 gchar *tmp;
1079                 gchar *profile_path = g_strconcat(
1080                         g_get_home_dir(), G_DIR_SEPARATOR_S,
1081                         ".mh_profile", NULL);
1082                 FILE *fp = g_fopen(profile_path, "r");
1083                 if (fp) {
1084                         while (fgets(buf, sizeof(buf), fp) != NULL) {
1085                                 if (!strncmp(buf, "Unseen-Sequence:", strlen("Unseen-Sequence:"))) {
1086                                         gchar *seq_tmp = buf+strlen("Unseen-Sequence:");
1087                                         while (*seq_tmp == ' ')
1088                                                 seq_tmp++;
1089                                         seq_name = g_strdup(seq_tmp);
1090                                         seq_name = strretchomp(seq_name);
1091                                         break;
1092                                 }
1093                         }
1094                         fclose(fp);
1095                 }
1096                 if (!seq_name)
1097                         seq_name = g_strdup("unseen");
1098                 tmp = g_strdup_printf("%s:", seq_name);
1099                 g_free(seq_name);
1100                 seq_name = tmp;
1101         }
1102         return seq_name;        
1103 }
1104
1105 static gint mh_get_flags(Folder *folder, FolderItem *item,
1106                            MsgInfoList *msginfo_list, GRelation *msgflags)
1107 {
1108         gchar *mh_sequences_filename;
1109         FILE *mh_sequences_file;
1110         gchar buf[BUFFSIZE];
1111         gchar *unseen_list = NULL;
1112         gchar *path;
1113         MsgInfoList *mcur = NULL;
1114 /*
1115         GTimer *timer = g_timer_new();
1116         g_timer_start(timer);
1117 */
1118         if (!item)
1119                 return 0;
1120         path = folder_item_get_path(item);
1121
1122         mh_sequences_filename = g_strconcat(path, G_DIR_SEPARATOR_S,
1123                                             ".mh_sequences", NULL);
1124         g_free(path);
1125         if ((mh_sequences_file = g_fopen(mh_sequences_filename, "r+b")) != NULL) {
1126                 while (fgets(buf, sizeof(buf), mh_sequences_file) != NULL) {
1127                         if (!strncmp(buf, get_unseen_seq_name(), strlen(get_unseen_seq_name()))) {
1128                                 unseen_list = g_strdup(buf+strlen(get_unseen_seq_name()));
1129                                 break;
1130                         }
1131                 }
1132                 fclose(mh_sequences_file);
1133         }
1134         if (unseen_list) {
1135                 gchar *cur = NULL;
1136                 gchar *token = NULL, *next = NULL, *boundary = NULL;
1137                 gint num = 0;
1138                 GHashTable *unseen_table = g_hash_table_new(g_direct_hash, g_direct_equal);
1139
1140                 cur = unseen_list = strretchomp(unseen_list);
1141                 debug_print("found unseen list in .mh_sequences: %s\n", unseen_list);
1142 next_token:
1143                 while (*cur && *cur == ' ')
1144                         cur++;
1145                 
1146                 if ((next = strchr(cur, ' ')) != NULL) {
1147                         token = cur;
1148                         cur = next+1;
1149                         *next = '\0';
1150                 } else {
1151                         token = cur;
1152                         cur = NULL;
1153                 }
1154                 
1155                 if ((boundary = strchr(token, '-')) != NULL) {
1156                         gchar *start, *end;
1157                         int i;
1158                         start = token;
1159                         end = boundary+1;
1160                         *boundary='\0';
1161                         for (i = atoi(start); i <= atoi(end); i++) {
1162                                 g_hash_table_insert(unseen_table, GINT_TO_POINTER(i), GINT_TO_POINTER(1));
1163                         }
1164                 } else if ((num = atoi(token)) > 0) {
1165                         g_hash_table_insert(unseen_table, GINT_TO_POINTER(num), GINT_TO_POINTER(1));
1166                 }
1167                 
1168                 if (cur)
1169                         goto next_token;
1170                 for (mcur = msginfo_list; mcur; mcur = mcur->next) {
1171                         MsgInfo *msginfo = (MsgInfo *)mcur->data;
1172                         MsgPermFlags flags = msginfo->flags.perm_flags;
1173                         if (g_hash_table_lookup(unseen_table, GINT_TO_POINTER(msginfo->msgnum))) {
1174                                 flags |= MSG_UNREAD;
1175                         } else if (!(flags & MSG_NEW)) { /* don't mark new msgs as read */
1176                                 flags &= ~(MSG_UNREAD);
1177                         }
1178                         if (flags != msginfo->flags.perm_flags)
1179                                 g_relation_insert(msgflags, msginfo, GINT_TO_POINTER(flags));
1180                 }
1181                 g_hash_table_destroy(unseen_table);
1182                 g_free(unseen_list);
1183         }
1184 /*
1185         g_timer_stop(timer);
1186         printf("mh_get_flags: %f secs\n", g_timer_elapsed(timer, NULL));
1187         g_timer_destroy(timer);
1188 */
1189         return 0;
1190 }
1191
1192 static void mh_write_sequences(FolderItem *item)
1193 {
1194         gchar *mh_sequences_old, *mh_sequences_new;
1195         FILE *mh_sequences_old_fp, *mh_sequences_new_fp;
1196         gchar buf[BUFFSIZE];
1197         gchar *path = NULL;
1198 /*
1199         GTimer *timer = g_timer_new();
1200         g_timer_start(timer);
1201 */
1202         if (!item)
1203                 return;
1204         
1205         path = folder_item_get_path(item);
1206
1207         mh_sequences_old = g_strconcat(path, G_DIR_SEPARATOR_S,
1208                                             ".mh_sequences", NULL);
1209         mh_sequences_new = g_strconcat(path, G_DIR_SEPARATOR_S,
1210                                             ".mh_sequences.new", NULL);
1211         if ((mh_sequences_new_fp = g_fopen(mh_sequences_new, "w+b")) != NULL) {
1212                 GSList *msglist = folder_item_get_msg_list(item);
1213                 GSList *cur;
1214                 MsgInfo *info = NULL;
1215                 gint start = -1, end = -1;
1216                 gchar *sequence = g_strdup("");
1217                 msglist = g_slist_sort(msglist, sort_cache_list_by_msgnum);
1218                 cur = msglist;
1219                 
1220                 /* write the unseen sequence */
1221                 do {
1222                         info = (MsgInfo *)(cur ? cur->data:NULL);
1223                         if (info && (MSG_IS_UNREAD(info->flags) || MSG_IS_NEW(info->flags))) {
1224                                 if (start < 0)
1225                                         start = end = info->msgnum;
1226                                 else
1227                                         end = info->msgnum;
1228                         } else {
1229                                 if (start > 0 && end > 0) {
1230                                         gchar *tmp = sequence;
1231                                         if (start != end)
1232                                                 sequence = g_strdup_printf("%s %d-%d ", tmp, start, end);
1233                                         else
1234                                                 sequence = g_strdup_printf("%s %d ", tmp, start);
1235                                         g_free(tmp);
1236                                         start = end = -1;
1237                                 }
1238                         }
1239                         cur = cur ? cur->next:NULL;
1240                 } while (cur || (start > 0 && end > 0));
1241                 if (sequence && strlen(sequence)) {
1242                         fprintf(mh_sequences_new_fp, "%s%s\n", 
1243                                         get_unseen_seq_name(), sequence);
1244                         debug_print("wrote unseen sequence: '%s%s'\n", 
1245                                         get_unseen_seq_name(), sequence);
1246                 }
1247                 /* rewrite the rest of the file */
1248                 if ((mh_sequences_old_fp = g_fopen(mh_sequences_old, "r+b")) != NULL) {
1249                         while (fgets(buf, sizeof(buf), mh_sequences_old_fp) != NULL) {
1250                                 if (strncmp(buf, get_unseen_seq_name(), strlen(get_unseen_seq_name())))
1251                                         fprintf(mh_sequences_new_fp, "%s", buf);
1252                         }
1253                         fclose(mh_sequences_old_fp);
1254                 }
1255                 
1256                 fclose(mh_sequences_new_fp);
1257                 g_rename(mh_sequences_new, mh_sequences_old);
1258                 g_free(sequence);
1259                 procmsg_msg_list_free(msglist);
1260         }
1261         g_free(mh_sequences_old);
1262         g_free(mh_sequences_new);
1263         g_free(path);
1264 /*
1265         g_timer_stop(timer);
1266         printf("mh_get_flags: %f secs\n", g_timer_elapsed(timer, NULL));
1267         g_timer_destroy(timer);
1268 */
1269 }
1270
1271 static int mh_item_close(Folder *folder, FolderItem *item)
1272 {
1273         mh_write_sequences(item);
1274         return 0;
1275 }