more revision of filtering system
[claws.git] / src / mh.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 Hiroyuki Yamamoto
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 <dirent.h>
28 #include <sys/stat.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include <errno.h>
32
33 #undef MEASURE_TIME
34
35 #ifdef MEASURE_TIME
36 #  include <sys/time.h>
37 #endif
38
39 #include "intl.h"
40 #include "folder.h"
41 #include "mh.h"
42 #include "procmsg.h"
43 #include "procheader.h"
44 #include "utils.h"
45
46 static GSList  *mh_get_uncached_msgs            (GHashTable     *msg_table,
47                                                  FolderItem     *item);
48 static MsgInfo *mh_parse_msg                    (const gchar    *file,
49                                                  FolderItem     *item);
50 static void     mh_scan_tree_recursive          (FolderItem     *item);
51
52 static gboolean mh_rename_folder_func           (GNode          *node,
53                                                  gpointer        data);
54
55
56 GSList *mh_get_msg_list(Folder *folder, FolderItem *item, gboolean use_cache)
57 {
58         GSList *mlist;
59         GHashTable *msg_table;
60         gchar *path;
61         struct stat s;
62         gboolean scan_new = TRUE;
63 #ifdef MEASURE_TIME
64         struct timeval tv_before, tv_after, tv_result;
65
66         gettimeofday(&tv_before, NULL);
67 #endif
68
69         g_return_val_if_fail(item != NULL, NULL);
70
71         path = folder_item_get_path(item);
72         if (stat(path, &s) < 0) {
73                 FILE_OP_ERROR(path, "stat");
74         } else {
75                 if (item->mtime == s.st_mtime) {
76                         debug_print("Folder is not modified.\n");
77                         scan_new = FALSE;
78                 } else
79                         item->mtime = s.st_mtime;
80         }
81         g_free(path);
82
83         if (use_cache && !scan_new) {
84                 mlist = procmsg_read_cache(item, FALSE);
85                 if (!mlist)
86                         mlist = mh_get_uncached_msgs(NULL, item);
87         } else if (use_cache) {
88                 GSList *newlist;
89
90                 mlist = procmsg_read_cache(item, TRUE);
91                 msg_table = procmsg_msg_hash_table_create(mlist);
92
93                 newlist = mh_get_uncached_msgs(msg_table, item);
94                 if (msg_table)
95                         g_hash_table_destroy(msg_table);
96
97                 mlist = g_slist_concat(mlist, newlist);
98         } else
99                 mlist = mh_get_uncached_msgs(NULL, item);
100
101         procmsg_set_flags(mlist, item);
102
103 #ifdef MEASURE_TIME
104         gettimeofday(&tv_after, NULL);
105
106         timersub(&tv_after, &tv_before, &tv_result);
107         g_print("mh_get_msg_list: %s: elapsed time: %ld.%06ld sec\n",
108                 item->path, tv_result.tv_sec, tv_result.tv_usec);
109 #endif
110
111         return mlist;
112 }
113
114 gchar *mh_fetch_msg(Folder *folder, FolderItem *item, gint num)
115 {
116         gchar *path;
117         gchar *file;
118
119         g_return_val_if_fail(item != NULL, NULL);
120         g_return_val_if_fail(num > 0 && num <= item->last_num, NULL);
121
122         path = folder_item_get_path(item);
123         file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
124         g_free(path);
125         if (!is_file_exist(file)) {
126                 g_free(file);
127                 return NULL;
128         }
129
130         return file;
131 }
132
133 gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file,
134                 gboolean remove_source)
135 {
136         gchar *destpath;
137         gchar *destfile;
138
139         g_return_val_if_fail(dest != NULL, -1);
140         g_return_val_if_fail(file != NULL, -1);
141
142         if (dest->last_num < 0) {
143                 mh_scan_folder(folder, dest);
144                 if (dest->last_num < 0) return -1;
145         }
146
147         destpath = folder_item_get_path(dest);
148         g_return_val_if_fail(destpath != NULL, -1);
149         if (!is_dir_exist(destpath))
150                 make_dir_hier(destpath);
151
152         destfile = g_strdup_printf("%s%c%d", destpath, G_DIR_SEPARATOR,
153                                    dest->last_num + 1);
154
155         if (link(file, destfile) < 0) {
156                 if (copy_file(file, destfile) < 0) {
157                         g_warning(_("can't copy message %s to %s\n"),
158                                   file, destfile);
159                         g_free(destfile);
160                         return -1;
161                 }
162         }
163
164         if (remove_source) {
165                 if (unlink(file) < 0)
166                         FILE_OP_ERROR(file, "unlink");
167         }
168
169         g_free(destfile);
170         dest->last_num++;
171         return dest->last_num;
172 }
173
174 gint mh_move_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
175 {
176         gchar *destdir;
177         gchar *srcfile;
178         gchar *destfile;
179         FILE *fp;
180         gint filemode = 0;
181         PrefsFolderItem *prefs;
182
183         g_return_val_if_fail(dest != NULL, -1);
184         g_return_val_if_fail(msginfo != NULL, -1);
185
186         if (msginfo->folder == dest) {
187                 g_warning(_("the src folder is identical to the dest.\n"));
188                 return -1;
189         }
190
191         if (dest->last_num < 0) {
192                 mh_scan_folder(folder, dest);
193                 if (dest->last_num < 0) return -1;
194         }
195
196         prefs = dest->prefs;
197
198         destdir = folder_item_get_path(dest);
199         if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
200                 g_warning(_("Can't open mark file.\n"));
201
202         debug_print(_("Moving message %s%c%d to %s ...\n"),
203                     msginfo->folder->path, G_DIR_SEPARATOR,
204                     msginfo->msgnum, dest->path);
205         srcfile = procmsg_get_message_file_path(msginfo);
206         destfile = g_strdup_printf("%s%c%d", destdir, G_DIR_SEPARATOR,
207                                    dest->last_num + 1);
208         g_free(destdir);
209
210         if (move_file(srcfile, destfile) < 0) {
211                 g_free(srcfile);
212                 g_free(destfile);
213                 if (fp) fclose(fp);
214                 return -1;
215         }
216
217         if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
218                 if (chmod(destfile, prefs->folder_chmod) < 0)
219                         FILE_OP_ERROR(destfile, "chmod");
220
221                 /* for mark file */
222                 filemode = prefs->folder_chmod;
223                 if (filemode & S_IRGRP) filemode |= S_IWGRP;
224                 if (filemode & S_IROTH) filemode |= S_IWOTH;
225         }
226
227         g_free(srcfile);
228         g_free(destfile);
229         dest->last_num++;
230
231         if (fp) {
232                 MsgInfo newmsginfo;
233
234                 newmsginfo.msgnum = dest->last_num;
235                 newmsginfo.flags = msginfo->flags;
236                 if (dest->stype == F_OUTBOX ||
237                     dest->stype == F_QUEUE  ||
238                     dest->stype == F_DRAFT  ||
239                     dest->stype == F_TRASH)
240                         MSG_UNSET_PERM_FLAGS(newmsginfo.flags,
241                                              MSG_NEW|MSG_UNREAD|MSG_DELETED);
242
243                 procmsg_write_flags(&newmsginfo, fp);
244
245                 if (filemode) {
246 #if HAVE_FCHMOD
247                         fchmod(fileno(fp), filemode);
248 #else
249                         gchar *markfile;
250
251                         markfile = folder_item_get_mark_file(dest);
252                         if (markfile) {
253                                 chmod(markfile, filemode);
254                                 g_free(markfile);
255                         }
256 #endif
257                 }
258
259                 fclose(fp);
260         }
261
262         return dest->last_num;
263 }
264
265 gint mh_move_msgs_with_dest(Folder *folder, FolderItem *dest, GSList *msglist)
266 {
267         gchar *destdir;
268         gchar *srcfile;
269         gchar *destfile;
270         FILE *fp;
271         GSList *cur;
272         MsgInfo *msginfo;
273         PrefsFolderItem *prefs;
274
275         g_return_val_if_fail(dest != NULL, -1);
276         g_return_val_if_fail(msglist != NULL, -1);
277
278         if (dest->last_num < 0) {
279                 mh_scan_folder(folder, dest);
280                 if (dest->last_num < 0) return -1;
281         }
282
283         prefs = dest->prefs;
284
285         destdir = folder_item_get_path(dest);
286         if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
287                 g_warning(_("Can't open mark file.\n"));
288
289         for (cur = msglist; cur != NULL; cur = cur->next) {
290                 msginfo = (MsgInfo *)cur->data;
291
292                 if (msginfo->folder == dest) {
293                         g_warning(_("the src folder is identical to the dest.\n"));
294                         continue;
295                 }
296                 debug_print(_("Moving message %s%c%d to %s ...\n"),
297                             msginfo->folder->path, G_DIR_SEPARATOR,
298                             msginfo->msgnum, dest->path);
299
300                 srcfile = procmsg_get_message_file_path(msginfo);
301                 destfile = g_strdup_printf("%s%c%d", destdir, G_DIR_SEPARATOR,
302                                            dest->last_num + 1);
303
304                 if (move_file(srcfile, destfile) < 0) {
305                         g_free(srcfile);
306                         g_free(destfile);
307                         break;
308                 }
309
310                 g_free(srcfile);
311                 g_free(destfile);
312                 dest->last_num++;
313
314                 if (fp) {
315                         MsgInfo newmsginfo;
316
317                         newmsginfo.msgnum = dest->last_num;
318                         newmsginfo.flags = msginfo->flags;
319                         if (dest->stype == F_OUTBOX ||
320                             dest->stype == F_QUEUE  ||
321                             dest->stype == F_DRAFT  ||
322                             dest->stype == F_TRASH)
323                                 MSG_UNSET_PERM_FLAGS
324                                         (newmsginfo.flags,
325                                          MSG_NEW|MSG_UNREAD|MSG_DELETED);
326
327                         procmsg_write_flags(&newmsginfo, fp);
328                 }
329         }
330
331         g_free(destdir);
332         if (fp) fclose(fp);
333
334         return dest->last_num;
335 }
336
337 gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
338 {
339         gchar *destdir;
340         gchar *srcfile;
341         gchar *destfile;
342         FILE *fp;
343         gint filemode = 0;
344         PrefsFolderItem *prefs;
345
346         g_return_val_if_fail(dest != NULL, -1);
347         g_return_val_if_fail(msginfo != NULL, -1);
348
349         if (msginfo->folder == dest) {
350                 g_warning(_("the src folder is identical to the dest.\n"));
351                 return -1;
352         }
353
354         if (dest->last_num < 0) {
355                 mh_scan_folder(folder, dest);
356                 if (dest->last_num < 0) return -1;
357         }
358
359         prefs = dest->prefs;
360
361         destdir = folder_item_get_path(dest);
362         if (!is_dir_exist(destdir))
363                 make_dir_hier(destdir);
364
365         if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
366                 g_warning(_("Can't open mark file.\n"));
367
368         debug_print(_("Copying message %s%c%d to %s ...\n"),
369                     msginfo->folder->path, G_DIR_SEPARATOR,
370                     msginfo->msgnum, dest->path);
371         srcfile = procmsg_get_message_file_path(msginfo);
372         destfile = g_strdup_printf("%s%c%d", destdir, G_DIR_SEPARATOR,
373                                    dest->last_num + 1);
374         g_free(destdir);
375
376         dest->op_count--;
377
378         if (is_file_exist(destfile)) {
379                 g_warning(_("%s already exists."), destfile);
380                 g_free(srcfile);
381                 g_free(destfile);
382                 if (fp) fclose(fp);
383                 return -1;
384         }
385
386         if (copy_file(srcfile, destfile) < 0) {
387                 FILE_OP_ERROR(srcfile, "copy");
388                 g_free(srcfile);
389                 g_free(destfile);
390                 if (fp) fclose(fp);
391                 return -1;
392         }
393
394         if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
395                 if (chmod(destfile, prefs->folder_chmod) < 0)
396                         FILE_OP_ERROR(destfile, "chmod");
397
398                 /* for mark file */
399                 filemode = prefs->folder_chmod;
400                 if (filemode & S_IRGRP) filemode |= S_IWGRP;
401                 if (filemode & S_IROTH) filemode |= S_IWOTH;
402         }
403
404         g_free(srcfile);
405         g_free(destfile);
406         dest->last_num++;
407
408         if (fp) {
409                 MsgInfo newmsginfo;
410
411                 newmsginfo.msgnum = dest->last_num;
412                 newmsginfo.flags = msginfo->flags;
413                 if (dest->stype == F_OUTBOX ||
414                     dest->stype == F_QUEUE  ||
415                     dest->stype == F_DRAFT  ||
416                     dest->stype == F_TRASH)
417                         MSG_UNSET_PERM_FLAGS(newmsginfo.flags,
418                                              MSG_NEW|MSG_UNREAD|MSG_DELETED);
419                 procmsg_write_flags(&newmsginfo, fp);
420
421                 if (filemode) {
422 #if HAVE_FCHMOD
423                         fchmod(fileno(fp), filemode);
424 #else
425                         gchar *markfile;
426
427                         markfile = folder_item_get_mark_file(dest);
428                         if (markfile) {
429                                 chmod(markfile, filemode);
430                                 g_free(markfile);
431                         }
432 #endif
433                 }
434
435                 fclose(fp);
436         }
437
438         return dest->last_num;
439 }
440
441 /*
442 gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
443 {
444         Folder * src_folder;
445         gchar * filename;
446         gint num;
447         gchar * destdir;
448         FILE * fp;
449
450         src_folder = msginfo->folder->folder;
451         
452         g_return_val_if_fail(src_folder->fetch_msg != NULL, -1);
453         
454         filename = src_folder->fetch_msg(src_folder,
455                                          msginfo->folder,
456                                          msginfo->msgnum);
457         if (filename == NULL)
458                 return -1;
459
460         num = folder->add_msg(folder, dest, filename, FALSE);
461
462         destdir = folder_item_get_path(dest);
463         if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
464                 g_warning(_("Can't open mark file.\n"));
465
466         if (fp) {
467                 MsgInfo newmsginfo;
468
469                 newmsginfo.msgnum = dest->last_num;
470                 newmsginfo.flags = msginfo->flags;
471                 if (dest->stype == F_OUTBOX ||
472                     dest->stype == F_QUEUE  ||
473                     dest->stype == F_DRAFT  ||
474                     dest->stype == F_TRASH)
475                         MSG_UNSET_FLAGS(newmsginfo.flags,
476                                         MSG_NEW|MSG_UNREAD|MSG_DELETED);
477
478                 procmsg_write_flags(&newmsginfo, fp);
479                 fclose(fp);
480         }
481         
482         return num;
483 }
484 */
485
486 gint mh_copy_msgs_with_dest(Folder *folder, FolderItem *dest, GSList *msglist)
487 {
488         gchar *destdir;
489         gchar *srcfile;
490         gchar *destfile;
491         FILE *fp;
492         GSList *cur;
493         MsgInfo *msginfo;
494
495         g_return_val_if_fail(dest != NULL, -1);
496         g_return_val_if_fail(msglist != NULL, -1);
497
498         if (dest->last_num < 0) {
499                 mh_scan_folder(folder, dest);
500                 if (dest->last_num < 0) return -1;
501         }
502
503         destdir = folder_item_get_path(dest);
504         if (!is_dir_exist(destdir))
505                 make_dir_hier(destdir);
506
507         if ((fp = procmsg_open_mark_file(destdir, TRUE)) == NULL)
508                 g_warning(_("Can't open mark file.\n"));
509
510         for (cur = msglist; cur != NULL; cur = cur->next) {
511                 msginfo = (MsgInfo *)cur->data;
512
513                 if (msginfo->folder == dest) {
514                         g_warning(_("the src folder is identical to the dest.\n"));
515                         continue;
516                 }
517                 debug_print(_("Copying message %s%c%d to %s ...\n"),
518                             msginfo->folder->path, G_DIR_SEPARATOR,
519                             msginfo->msgnum, dest->path);
520
521                 srcfile = procmsg_get_message_file_path(msginfo);
522                 destfile = g_strdup_printf("%s%c%d", destdir, G_DIR_SEPARATOR,
523                                            dest->last_num + 1);
524
525                 if (is_file_exist(destfile)) {
526                         g_warning(_("%s already exists."), destfile);
527                         g_free(srcfile);
528                         g_free(destfile);
529                         break;
530                 }
531
532                 if (copy_file(srcfile, destfile) < 0) {
533                         FILE_OP_ERROR(srcfile, "copy");
534                         g_free(srcfile);
535                         g_free(destfile);
536                         break;
537                 }
538
539                 g_free(srcfile);
540                 g_free(destfile);
541                 dest->last_num++;
542
543                 if (fp) {
544                         MsgInfo newmsginfo;
545
546                         newmsginfo.msgnum = dest->last_num;
547                         newmsginfo.flags = msginfo->flags;
548                         if (dest->stype == F_OUTBOX ||
549                             dest->stype == F_QUEUE  ||
550                             dest->stype == F_DRAFT  ||
551                             dest->stype == F_TRASH)
552                                 MSG_UNSET_PERM_FLAGS
553                                         (newmsginfo.flags,
554                                          MSG_NEW|MSG_UNREAD|MSG_DELETED);
555                         procmsg_write_flags(&newmsginfo, fp);
556                 }
557         }
558
559         g_free(destdir);
560         if (fp) fclose(fp);
561
562         return dest->last_num;
563 }
564
565 gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
566 {
567         gchar *file;
568
569         g_return_val_if_fail(item != NULL, -1);
570
571         file = mh_fetch_msg(folder, item, num);
572         g_return_val_if_fail(file != NULL, -1);
573
574         if (unlink(file) < 0) {
575                 FILE_OP_ERROR(file, "unlink");
576                 g_free(file);
577                 return -1;
578         }
579
580         g_free(file);
581         return 0;
582 }
583
584 gint mh_remove_all_msg(Folder *folder, FolderItem *item)
585 {
586         gchar *path;
587         gint val;
588
589         g_return_val_if_fail(item != NULL, -1);
590
591         path = folder_item_get_path(item);
592         g_return_val_if_fail(path != NULL, -1);
593         val = remove_all_numbered_files(path);
594         g_free(path);
595
596         return val;
597 }
598
599 gboolean mh_is_msg_changed(Folder *folder, FolderItem *item, MsgInfo *msginfo)
600 {
601         struct stat s;
602
603         if (stat(itos(msginfo->msgnum), &s) < 0 ||
604             msginfo->size  != s.st_size ||
605             msginfo->mtime != s.st_mtime)
606                 return TRUE;
607
608         return FALSE;
609 }
610
611 void mh_scan_folder(Folder *folder, FolderItem *item)
612 {
613         gchar *path;
614         DIR *dp;
615         struct dirent *d;
616         struct stat s;
617         gint max = 0;
618         gint num;
619         gint n_msg = 0;
620
621         g_return_if_fail(item != NULL);
622
623         debug_print("mh_scan_folder(): Scanning %s ...\n", item->path);
624
625         path = folder_item_get_path(item);
626         g_return_if_fail(path != NULL);
627         if (change_dir(path) < 0) {
628                 g_free(path);
629                 return;
630         }
631         g_free(path);
632
633         if ((dp = opendir(".")) == NULL) {
634                 FILE_OP_ERROR(item->path, "opendir");
635                 return;
636         }
637
638         if (folder->ui_func)
639                 folder->ui_func(folder, item, folder->ui_func_data);
640
641         while ((d = readdir(dp)) != NULL) {
642                 if ((num = to_number(d->d_name)) >= 0 &&
643                     stat(d->d_name, &s) == 0 &&
644                     S_ISREG(s.st_mode)) {
645                         n_msg++;
646                         if (max < num)
647                                 max = num;
648                 }
649         }
650
651         closedir(dp);
652
653         if (n_msg == 0)
654                 item->new = item->unread = item->total = 0;
655         else {
656                 gint new, unread, total;
657
658                 procmsg_get_mark_sum(".", &new, &unread, &total);
659                 if (n_msg > total) {
660                         new += n_msg - total;
661                         unread += n_msg - total;
662                 }
663                 item->new = new;
664                 item->unread = unread;
665                 item->total = n_msg;
666         }
667
668         debug_print(_("Last number in dir %s = %d\n"), item->path, max);
669         item->last_num = max;
670 }
671
672 void mh_scan_tree(Folder *folder)
673 {
674         FolderItem *item;
675         gchar *rootpath;
676
677         g_return_if_fail(folder != NULL);
678
679         folder_tree_destroy(folder);
680         item = folder_item_new(folder->name, NULL);
681         item->folder = folder;
682         folder->node = g_node_new(item);
683
684         rootpath = folder_item_get_path(item);
685         if (change_dir(rootpath) < 0) {
686                 g_free(rootpath);
687                 return;
688         }
689         g_free(rootpath);
690
691         mh_scan_tree_recursive(item);
692 }
693
694 #define MAKE_DIR_IF_NOT_EXIST(dir) \
695 { \
696         if (!is_dir_exist(dir)) { \
697                 if (is_file_exist(dir)) { \
698                         g_warning(_("File `%s' already exists.\n" \
699                                     "Can't create folder."), dir); \
700                         return -1; \
701                 } \
702                 if (mkdir(dir, S_IRWXU) < 0) { \
703                         FILE_OP_ERROR(dir, "mkdir"); \
704                         return -1; \
705                 } \
706                 if (chmod(dir, S_IRWXU) < 0) \
707                         FILE_OP_ERROR(dir, "chmod"); \
708         } \
709 }
710
711 gint mh_create_tree(Folder *folder)
712 {
713         gchar *rootpath;
714
715         g_return_val_if_fail(folder != NULL, -1);
716
717         CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), -1);
718         rootpath = LOCAL_FOLDER(folder)->rootpath;
719         MAKE_DIR_IF_NOT_EXIST(rootpath);
720         CHDIR_RETURN_VAL_IF_FAIL(rootpath, -1);
721         MAKE_DIR_IF_NOT_EXIST(INBOX_DIR);
722         MAKE_DIR_IF_NOT_EXIST(OUTBOX_DIR);
723         MAKE_DIR_IF_NOT_EXIST(QUEUE_DIR);
724         MAKE_DIR_IF_NOT_EXIST(DRAFT_DIR);
725         MAKE_DIR_IF_NOT_EXIST(TRASH_DIR);
726
727         return 0;
728 }
729
730 #undef MAKE_DIR_IF_NOT_EXIST
731
732 FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
733                              const gchar *name)
734 {
735         gchar *path;
736         gchar *fullpath;
737         FolderItem *new_item;
738
739         g_return_val_if_fail(folder != NULL, NULL);
740         g_return_val_if_fail(parent != NULL, NULL);
741         g_return_val_if_fail(name != NULL, NULL);
742
743         path = folder_item_get_path(parent);
744         if (!is_dir_exist(path))
745                 make_dir_hier(path);
746
747         fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, name, NULL);
748         g_free(path);
749
750         if (mkdir(fullpath, S_IRWXU) < 0) {
751                 FILE_OP_ERROR(fullpath, "mkdir");
752                 g_free(fullpath);
753                 return NULL;
754         }
755         if (chmod(fullpath, S_IRWXU) < 0)
756                 FILE_OP_ERROR(fullpath, "chmod");
757
758         g_free(fullpath);
759
760         if (parent->path)
761                 path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
762                                    NULL);
763         else
764                 path = g_strdup(name);
765         new_item = folder_item_new(name, path);
766         folder_item_append(parent, new_item);
767         g_free(path);
768
769         return new_item;
770 }
771
772 gint mh_rename_folder(Folder *folder, FolderItem *item, const gchar *name)
773 {
774         gchar *oldpath;
775         gchar *dirname;
776         gchar *newpath;
777         GNode *node;
778         gchar *paths[2];
779
780         g_return_val_if_fail(folder != NULL, -1);
781         g_return_val_if_fail(item != NULL, -1);
782         g_return_val_if_fail(item->path != NULL, -1);
783         g_return_val_if_fail(name != NULL, -1);
784
785         oldpath = folder_item_get_path(item);
786         if (!is_dir_exist(oldpath))
787                 make_dir_hier(oldpath);
788
789         dirname = g_dirname(oldpath);
790         newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
791         g_free(dirname);
792
793         if (rename(oldpath, newpath) < 0) {
794                 FILE_OP_ERROR(oldpath, "rename");
795                 g_free(oldpath);
796                 g_free(newpath);
797                 return -1;
798         }
799
800         g_free(oldpath);
801         g_free(newpath);
802
803         if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
804                 dirname = g_dirname(item->path);
805                 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
806                 g_free(dirname);
807         } else
808                 newpath = g_strdup(name);
809
810         g_free(item->name);
811         item->name = g_strdup(name);
812
813         node = g_node_find(item->folder->node, G_PRE_ORDER, G_TRAVERSE_ALL,
814                            item);
815         paths[0] = g_strdup(item->path);
816         paths[1] = newpath;
817         g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
818                         mh_rename_folder_func, paths);
819
820         g_free(paths[0]);
821         g_free(paths[1]);
822         return 0;
823 }
824
825 gint mh_remove_folder(Folder *folder, FolderItem *item)
826 {
827         gchar *path;
828
829         g_return_val_if_fail(folder != NULL, -1);
830         g_return_val_if_fail(item != NULL, -1);
831         g_return_val_if_fail(item->path != NULL, -1);
832
833         path = folder_item_get_path(item);
834         if (remove_dir_recursive(path) < 0) {
835                 g_warning("can't remove directory `%s'\n", path);
836                 g_free(path);
837                 return -1;
838         }
839
840         g_free(path);
841         folder_item_remove(item);
842         return 0;
843 }
844
845
846 static GSList *mh_get_uncached_msgs(GHashTable *msg_table, FolderItem *item)
847 {
848         gchar *path;
849         DIR *dp;
850         struct dirent *d;
851         struct stat s;
852         GSList *newlist = NULL;
853         GSList *last = NULL;
854         MsgInfo *msginfo;
855         gint n_newmsg = 0;
856         gint num;
857
858         g_return_val_if_fail(item != NULL, NULL);
859
860         path = folder_item_get_path(item);
861         g_return_val_if_fail(path != NULL, NULL);
862         if (change_dir(path) < 0) {
863                 g_free(path);
864                 return NULL;
865         }
866         g_free(path);
867
868         if ((dp = opendir(".")) == NULL) {
869                 FILE_OP_ERROR(item->path, "opendir");
870                 return NULL;
871         }
872
873         debug_print(_("\tSearching uncached messages... "));
874
875         if (msg_table) {
876                 while ((d = readdir(dp)) != NULL) {
877                         if ((num = to_number(d->d_name)) < 0) continue;
878                         if (stat(d->d_name, &s) < 0) {
879                                 FILE_OP_ERROR(d->d_name, "stat");
880                                 continue;
881                         }
882                         if (!S_ISREG(s.st_mode)) continue;
883
884                         msginfo = g_hash_table_lookup
885                                 (msg_table, GUINT_TO_POINTER(num));
886
887                         if (!msginfo) {
888                                 /* not found in the cache (uncached message) */
889                                 msginfo = mh_parse_msg(d->d_name, item);
890                                 if (!msginfo) continue;
891
892                                 if (!newlist)
893                                         last = newlist =
894                                                 g_slist_append(NULL, msginfo);
895                                 else {
896                                         last = g_slist_append(last, msginfo);
897                                         last = last->next;
898                                 }
899                                 n_newmsg++;
900                         }
901                 }
902         } else {
903                 /* discard all previous cache */
904                 while ((d = readdir(dp)) != NULL) {
905                         if (to_number(d->d_name) < 0) continue;
906                         if (stat(d->d_name, &s) < 0) {
907                                 FILE_OP_ERROR(d->d_name, "stat");
908                                 continue;
909                         }
910                         if (!S_ISREG(s.st_mode)) continue;
911
912                         msginfo = mh_parse_msg(d->d_name, item);
913                         if (!msginfo) continue;
914
915                         if (!newlist)
916                                 last = newlist = g_slist_append(NULL, msginfo);
917                         else {
918                                 last = g_slist_append(last, msginfo);
919                                 last = last->next;
920                         }
921                         n_newmsg++;
922                 }
923         }
924
925         closedir(dp);
926
927         if (n_newmsg)
928                 debug_print(_("%d uncached message(s) found.\n"), n_newmsg);
929         else
930                 debug_print(_("done.\n"));
931
932         /* sort new messages in numerical order */
933         if (newlist) {
934                 debug_print(_("\tSorting uncached messages in numerical order... "));
935                 newlist = g_slist_sort
936                         (newlist, (GCompareFunc)procmsg_cmp_msgnum_for_sort);
937                 debug_print(_("done.\n"));
938         }
939
940         return newlist;
941 }
942
943 static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
944 {
945         struct stat s;
946         MsgInfo *msginfo;
947         MsgFlags flags;
948
949         flags.perm_flags = MSG_NEW|MSG_UNREAD;
950         flags.tmp_flags = 0;
951
952         g_return_val_if_fail(item != NULL, NULL);
953         g_return_val_if_fail(file != NULL, NULL);
954
955         if (item->stype == F_QUEUE) {
956                 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
957         } else if (item->stype == F_DRAFT) {
958                 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
959         }
960
961         msginfo = procheader_parse(file, flags, FALSE);
962         if (!msginfo) return NULL;
963
964         msginfo->msgnum = atoi(file);
965         msginfo->folder = item;
966
967         if (stat(file, &s) < 0) {
968                 FILE_OP_ERROR(file, "stat");
969                 msginfo->size = 0;
970                 msginfo->mtime = 0;
971         } else {
972                 msginfo->size = s.st_size;
973                 msginfo->mtime = s.st_mtime;
974         }
975
976         return msginfo;
977 }
978
979 static gboolean mh_is_maildir_one(const gchar *path, const gchar *dir)
980 {
981         gchar *entry;
982         gboolean result;
983
984         entry = g_strconcat(path, G_DIR_SEPARATOR_S, dir, NULL);
985         result = is_dir_exist(entry);
986         g_free(entry);
987
988         return result;
989 }
990
991 /*
992  * check whether PATH is a Maildir style mailbox.
993  * This is the case if the 3 subdir: new, cur, tmp are existing.
994  * This functon assumes that entry is an directory
995  */
996 static gboolean mh_is_maildir(const gchar *path)
997 {
998         return mh_is_maildir_one(path, "new") &&
999                mh_is_maildir_one(path, "cur") &&
1000                mh_is_maildir_one(path, "tmp");
1001 }
1002
1003 static void mh_scan_tree_recursive(FolderItem *item)
1004 {
1005         DIR *dp;
1006         struct dirent *d;
1007         struct stat s;
1008         gchar *entry;
1009         gint n_msg = 0;
1010
1011         g_return_if_fail(item != NULL);
1012         g_return_if_fail(item->folder != NULL);
1013
1014         dp = opendir(item->path ? item->path : ".");
1015         if (!dp) {
1016                 FILE_OP_ERROR(item->path ? item->path : ".", "opendir");
1017                 return;
1018         }
1019
1020         debug_print("scanning %s ...\n",
1021                     item->path ? item->path
1022                     : LOCAL_FOLDER(item->folder)->rootpath);
1023         if (item->folder->ui_func)
1024                 item->folder->ui_func(item->folder, item,
1025                                       item->folder->ui_func_data);
1026
1027         while ((d = readdir(dp)) != NULL) {
1028                 if (d->d_name[0] == '.') continue;
1029
1030                 if (item->path)
1031                         entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
1032                                             d->d_name, NULL);
1033                 else
1034                         entry = g_strdup(d->d_name);
1035
1036                 if (stat(entry, &s) < 0) {
1037                         FILE_OP_ERROR(entry, "stat");
1038                         g_free(entry);
1039                         continue;
1040                 }
1041
1042                 if (S_ISDIR(s.st_mode)) {
1043                         FolderItem *new_item;
1044
1045                         if (mh_is_maildir(entry)) {
1046                                 g_free(entry);
1047                                 continue;
1048                         }
1049
1050                         new_item = folder_item_new(d->d_name, entry);
1051                         folder_item_append(item, new_item);
1052                         if (!item->path) {
1053                                 if (!strcmp(d->d_name, "inbox")) {
1054                                         new_item->stype = F_INBOX;
1055                                         item->folder->inbox = new_item;
1056                                 } else if (!strcmp(d->d_name, "outbox")) {
1057                                         new_item->stype = F_OUTBOX;
1058                                         item->folder->outbox = new_item;
1059                                 } else if (!strcmp(d->d_name, "draft")) {
1060                                         new_item->stype = F_DRAFT;
1061                                         item->folder->draft = new_item;
1062                                 } else if (!strcmp(d->d_name, "queue")) {
1063                                         new_item->stype = F_QUEUE;
1064                                         item->folder->queue = new_item;
1065                                 } else if (!strcmp(d->d_name, "trash")) {
1066                                         new_item->stype = F_TRASH;
1067                                         item->folder->trash = new_item;
1068                                 }
1069                         }
1070                         mh_scan_tree_recursive(new_item);
1071                 } else if (to_number(d->d_name) != -1) n_msg++;
1072
1073                 g_free(entry);
1074         }
1075
1076         closedir(dp);
1077
1078         if (item->path) {
1079                 gint new, unread, total;
1080
1081                 procmsg_get_mark_sum(item->path, &new, &unread, &total);
1082                 if (n_msg > total) {
1083                         new += n_msg - total;
1084                         unread += n_msg - total;
1085                 }
1086                 item->new = new;
1087                 item->unread = unread;
1088                 item->total = n_msg;
1089         }
1090 }
1091
1092 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
1093 {
1094         FolderItem *item = node->data;
1095         gchar **paths = data;
1096         const gchar *oldpath = paths[0];
1097         const gchar *newpath = paths[1];
1098         gchar *base;
1099         gchar *new_itempath;
1100         gint oldpathlen;
1101
1102         oldpathlen = strlen(oldpath);
1103         if (strncmp(oldpath, item->path, oldpathlen) != 0) {
1104                 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
1105                 return TRUE;
1106         }
1107
1108         base = item->path + oldpathlen;
1109         while (*base == G_DIR_SEPARATOR) base++;
1110         if (*base == '\0')
1111                 new_itempath = g_strdup(newpath);
1112         else
1113                 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
1114                                            NULL);
1115         g_free(item->path);
1116         item->path = new_itempath;
1117
1118         return FALSE;
1119 }