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