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