set chmod 0600 on log files, history files, saved parts, etc.
[claws.git] / src / mh.c
blob135975c05821cbbb13f0b84e34d21e376c3a5289
1 /*
2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2013 Hiroyuki Yamamoto and the Claws Mail team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #include "claws-features.h"
23 #endif
25 #include "defs.h"
27 #include <glib.h>
28 #include <glib/gi18n.h>
29 #include <dirent.h>
30 #include <sys/stat.h>
31 #include <unistd.h>
32 #include <string.h>
33 #include <errno.h>
34 #include <time.h>
36 #include "folder.h"
37 #include "folder_item_prefs.h"
38 #include "mh.h"
39 #include "procmsg.h"
40 #include "procheader.h"
41 #include "utils.h"
42 #include "codeconv.h"
43 #include "statusbar.h"
44 #include "gtkutils.h"
45 #include "timing.h"
46 #include "msgcache.h"
47 #include "file-utils.h"
49 /* Define possible missing constants for Windows. */
50 #ifdef G_OS_WIN32
51 # ifndef S_IRGRP
52 # define S_IRGRP 0
53 # define S_IWGRP 0
54 # endif
55 # ifndef S_IROTH
56 # define S_IROTH 0
57 # define S_IWOTH 0
58 # endif
59 #endif
62 static void mh_folder_init (Folder *folder,
63 const gchar *name,
64 const gchar *path);
66 static Folder *mh_folder_new (const gchar *name,
67 const gchar *path);
68 static void mh_folder_destroy (Folder *folder);
69 static gchar *mh_fetch_msg (Folder *folder,
70 FolderItem *item,
71 gint num);
72 static MsgInfo *mh_get_msginfo (Folder *folder,
73 FolderItem *item,
74 gint num);
75 static gint mh_add_msg (Folder *folder,
76 FolderItem *dest,
77 const gchar *file,
78 MsgFlags *flags);
79 static gint mh_add_msgs (Folder *folder,
80 FolderItem *dest,
81 GSList *file_list,
82 GHashTable *relation);
83 static gint mh_copy_msg (Folder *folder,
84 FolderItem *dest,
85 MsgInfo *msginfo);
86 static gint mh_copy_msgs (Folder *folder,
87 FolderItem *dest,
88 MsgInfoList *msglist,
89 GHashTable *relation);
90 static gint mh_remove_msg (Folder *folder,
91 FolderItem *item,
92 gint num);
93 static gint mh_remove_msgs (Folder *folder,
94 FolderItem *item,
95 MsgInfoList *msglist,
96 GHashTable *relation);
97 static gint mh_remove_all_msg (Folder *folder,
98 FolderItem *item);
99 static gboolean mh_is_msg_changed (Folder *folder,
100 FolderItem *item,
101 MsgInfo *msginfo);
103 static gint mh_get_num_list (Folder *folder,
104 FolderItem *item,
105 GSList **list,
106 gboolean *old_uids_valid);
107 static gint mh_scan_tree (Folder *folder);
109 static gint mh_create_tree (Folder *folder);
110 static FolderItem *mh_create_folder (Folder *folder,
111 FolderItem *parent,
112 const gchar *name);
113 static gint mh_rename_folder (Folder *folder,
114 FolderItem *item,
115 const gchar *name);
116 static gint mh_remove_folder (Folder *folder,
117 FolderItem *item);
119 static gchar *mh_get_new_msg_filename (FolderItem *dest);
121 static MsgInfo *mh_parse_msg (const gchar *file,
122 FolderItem *item);
123 static void mh_remove_missing_folder_items (Folder *folder);
124 static gchar *mh_filename_from_utf8 (const gchar *path);
125 static gchar *mh_filename_to_utf8 (const gchar *path);
126 static void mh_scan_tree_recursive (FolderItem *item);
128 static gboolean mh_rename_folder_func (GNode *node,
129 gpointer data);
130 static gchar *mh_item_get_path (Folder *folder,
131 FolderItem *item);
133 static gboolean mh_scan_required (Folder *folder,
134 FolderItem *item);
135 static void mh_set_mtime (Folder *folder,
136 FolderItem *item);
137 static int mh_item_close (Folder *folder,
138 FolderItem *item);
139 #if 0
140 static gint mh_get_flags (Folder *folder, FolderItem *item,
141 MsgInfoList *msginfo_list, GHashTable *msgflags);
142 #endif
143 static void mh_write_sequences (FolderItem *item, gboolean remove_unseen);
145 static FolderClass mh_class;
147 FolderClass *mh_get_class(void)
149 if (mh_class.idstr == NULL) {
150 mh_class.type = F_MH;
151 mh_class.idstr = "mh";
152 mh_class.uistr = "MH";
153 mh_class.supports_server_search = FALSE;
155 /* Folder functions */
156 mh_class.new_folder = mh_folder_new;
157 mh_class.destroy_folder = mh_folder_destroy;
158 mh_class.set_xml = folder_local_set_xml;
159 mh_class.get_xml = folder_local_get_xml;
160 mh_class.scan_tree = mh_scan_tree;
161 mh_class.create_tree = mh_create_tree;
163 /* FolderItem functions */
164 mh_class.item_get_path = mh_item_get_path;
165 mh_class.create_folder = mh_create_folder;
166 mh_class.rename_folder = mh_rename_folder;
167 mh_class.remove_folder = mh_remove_folder;
168 mh_class.get_num_list = mh_get_num_list;
169 mh_class.scan_required = mh_scan_required;
170 mh_class.set_mtime = mh_set_mtime;
171 mh_class.close = mh_item_close;
172 mh_class.get_flags = NULL; /*mh_get_flags */;
174 /* Message functions */
175 mh_class.get_msginfo = mh_get_msginfo;
176 mh_class.fetch_msg = mh_fetch_msg;
177 mh_class.add_msg = mh_add_msg;
178 mh_class.add_msgs = mh_add_msgs;
179 mh_class.copy_msg = mh_copy_msg;
180 mh_class.copy_msgs = mh_copy_msgs;
181 mh_class.search_msgs = folder_item_search_msgs_local;
182 mh_class.remove_msg = mh_remove_msg;
183 mh_class.remove_msgs = mh_remove_msgs;
184 mh_class.remove_all_msg = mh_remove_all_msg;
185 mh_class.is_msg_changed = mh_is_msg_changed;
188 return &mh_class;
191 static Folder *mh_folder_new(const gchar *name, const gchar *path)
193 Folder *folder;
195 folder = (Folder *)g_new0(MHFolder, 1);
196 folder->klass = &mh_class;
197 mh_folder_init(folder, name, path);
199 return folder;
202 static void mh_folder_destroy(Folder *folder)
204 folder_local_folder_destroy(LOCAL_FOLDER(folder));
207 static void mh_folder_init(Folder *folder, const gchar *name, const gchar *path)
209 folder_local_folder_init(folder, name, path);
213 gboolean mh_scan_required(Folder *folder, FolderItem *item)
215 gchar *path;
216 GStatBuf s;
218 path = folder_item_get_path(item);
219 cm_return_val_if_fail(path != NULL, FALSE);
221 if (g_stat(path, &s) < 0) {
222 FILE_OP_ERROR(path, "stat");
223 g_free(path);
224 return FALSE;
227 if ((s.st_mtime > item->mtime) &&
228 (s.st_mtime - 3600 != item->mtime)) {
229 debug_print("MH scan required, folder updated: %s (%ld > %ld)\n",
230 path,
231 (long int) s.st_mtime,
232 (long int) item->mtime);
233 g_free(path);
234 return TRUE;
237 debug_print("MH scan not required: %s (%ld <= %ld)\n",
238 path,
239 (long int) s.st_mtime,
240 (long int) item->mtime);
241 g_free(path);
242 return FALSE;
245 static void mh_get_last_num(Folder *folder, FolderItem *item)
247 gchar *path, *fullpath;
248 GDir *dp;
249 const gchar *d;
250 GError *error = NULL;
251 gint max = 0;
252 gint num;
254 cm_return_if_fail(item != NULL);
256 debug_print("mh_get_last_num(): Scanning %s ...\n", item->path?item->path:"(null)");
258 path = folder_item_get_path(item);
259 cm_return_if_fail(path != NULL);
261 if ((dp = g_dir_open(path, 0, &error)) == NULL) {
262 g_warning("Couldn't open directory '%s': %s (%d)",
263 path, error->message, error->code);
264 g_error_free(error);
265 g_free(path);
266 return;
269 while ((d = g_dir_read_name(dp)) != NULL) {
270 fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, d, NULL);
271 if ((num = to_number(d)) > 0 &&
272 g_file_test(fullpath, G_FILE_TEST_IS_REGULAR)) {
273 if (max < num)
274 max = num;
276 g_free(fullpath);
278 if (num % 2000 == 0)
279 GTK_EVENTS_FLUSH();
281 g_dir_close(dp);
282 g_free(path);
284 debug_print("Last number in dir %s = %d\n", item->path?item->path:"(null)", max);
285 item->last_num = max;
288 gint mh_get_num_list(Folder *folder, FolderItem *item, GSList **list, gboolean *old_uids_valid)
291 gchar *path;
292 GDir *dp;
293 const gchar *d;
294 GError *error = NULL;
295 gint num, nummsgs = 0;
297 cm_return_val_if_fail(item != NULL, -1);
299 debug_print("mh_get_num_list(): Scanning %s ...\n", item->path?item->path:"(null)");
301 *old_uids_valid = TRUE;
303 path = folder_item_get_path(item);
304 cm_return_val_if_fail(path != NULL, -1);
306 if ((dp = g_dir_open(path, 0, &error)) == NULL) {
307 g_message("Couldn't open current directory: %s (%d).\n",
308 error->message, error->code);
309 g_error_free(error);
310 g_free(path);
311 return -1;
313 g_free(path);
315 while ((d = g_dir_read_name(dp)) != NULL) {
316 if ((num = to_number(d)) > 0) {
317 *list = g_slist_prepend(*list, GINT_TO_POINTER(num));
318 nummsgs++;
321 g_dir_close(dp);
323 mh_set_mtime(folder, item);
324 return nummsgs;
327 static gchar *mh_fetch_msg(Folder *folder, FolderItem *item, gint num)
329 gchar *path;
330 gchar *file;
332 cm_return_val_if_fail(item != NULL, NULL);
333 cm_return_val_if_fail(num > 0, NULL);
335 path = folder_item_get_path(item);
336 file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
338 if (!is_file_exist(file)) {
339 g_free(file);
340 g_free(path);
341 return NULL;
343 g_free(path);
344 return file;
347 static MsgInfo *mh_get_msginfo(Folder *folder, FolderItem *item, gint num)
349 MsgInfo *msginfo;
350 gchar *file;
352 cm_return_val_if_fail(item != NULL, NULL);
353 if (num <= 0)
354 return NULL;
356 file = mh_fetch_msg(folder, item, num);
357 if (!file) return NULL;
359 msginfo = mh_parse_msg(file, item);
360 if (msginfo)
361 msginfo->msgnum = num;
363 g_free(file);
365 return msginfo;
368 static gchar *mh_get_new_msg_filename(FolderItem *dest)
370 gchar *destfile;
371 gchar *destpath;
373 destpath = folder_item_get_path(dest);
374 cm_return_val_if_fail(destpath != NULL, NULL);
376 if (dest->last_num < 0) {
377 mh_get_last_num(dest->folder, dest);
378 if (dest->last_num < 0) return NULL;
381 if (!is_dir_exist(destpath))
382 make_dir_hier(destpath);
384 for (;;) {
385 destfile = g_strdup_printf("%s%c%d", destpath, G_DIR_SEPARATOR,
386 dest->last_num + 1);
387 if (is_file_entry_exist(destfile)) {
388 dest->last_num++;
389 g_free(destfile);
390 } else
391 break;
394 g_free(destpath);
396 return destfile;
399 static gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file, MsgFlags *flags)
401 gint ret;
402 GSList file_list;
403 MsgFileInfo fileinfo;
405 cm_return_val_if_fail(file != NULL, -1);
407 fileinfo.msginfo = NULL;
408 fileinfo.file = (gchar *)file;
409 fileinfo.flags = flags;
410 file_list.data = &fileinfo;
411 file_list.next = NULL;
413 ret = mh_add_msgs(folder, dest, &file_list, NULL);
414 return ret;
417 static gint mh_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
418 GHashTable *relation)
420 gchar *destfile;
421 GSList *cur;
422 MsgFileInfo *fileinfo;
424 cm_return_val_if_fail(dest != NULL, -1);
425 cm_return_val_if_fail(file_list != NULL, -1);
427 if (dest->last_num < 0) {
428 mh_get_last_num(folder, dest);
429 if (dest->last_num < 0) return -1;
432 for (cur = file_list; cur != NULL; cur = cur->next) {
433 fileinfo = (MsgFileInfo *)cur->data;
435 destfile = mh_get_new_msg_filename(dest);
436 if (destfile == NULL) return -1;
438 #ifdef G_OS_UNIX
439 if (link(fileinfo->file, destfile) < 0) {
440 #endif
441 if (copy_file(fileinfo->file, destfile, TRUE) < 0) {
442 g_warning("can't copy message %s to %s",
443 fileinfo->file, destfile);
444 g_free(destfile);
445 return -1;
447 #ifdef G_OS_UNIX
449 #endif
451 if (relation != NULL)
452 g_hash_table_insert(relation, fileinfo, GINT_TO_POINTER(dest->last_num + 1));
453 g_free(destfile);
454 dest->last_num++;
456 mh_write_sequences(dest, TRUE);
457 return dest->last_num;
460 static gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
462 GSList msglist;
464 cm_return_val_if_fail(msginfo != NULL, -1);
466 msglist.data = msginfo;
467 msglist.next = NULL;
469 return mh_copy_msgs(folder, dest, &msglist, NULL);
472 static gint mh_copy_msgs(Folder *folder, FolderItem *dest, MsgInfoList *msglist,
473 GHashTable *relation)
475 gboolean dest_need_scan = FALSE;
476 gboolean src_need_scan = FALSE;
477 FolderItem *src = NULL;
478 gchar *srcfile;
479 gchar *destfile;
480 FolderItemPrefs *prefs;
481 MsgInfo *msginfo = NULL;
482 MsgInfoList *cur = NULL;
483 gint curnum = 0, total = 0;
484 gchar *srcpath = NULL;
485 gboolean full_fetch = FALSE;
486 time_t last_dest_mtime = (time_t)0;
487 time_t last_src_mtime = (time_t)0;
489 cm_return_val_if_fail(dest != NULL, -1);
490 cm_return_val_if_fail(msglist != NULL, -1);
492 msginfo = (MsgInfo *)msglist->data;
494 cm_return_val_if_fail(msginfo != NULL, -1);
496 if (msginfo->folder == dest) {
497 g_warning("the src folder is identical to the dest.");
498 return -1;
501 if (msginfo->folder->folder != dest->folder)
502 full_fetch = TRUE;
504 if (FOLDER_TYPE(msginfo->folder->folder) == F_MH) {
505 src = msginfo->folder;
508 if (dest->last_num < 0) {
509 mh_get_last_num(folder, dest);
510 if (dest->last_num < 0) return -1;
513 prefs = dest->prefs;
515 srcpath = folder_item_get_path(msginfo->folder);
517 dest_need_scan = mh_scan_required(dest->folder, dest);
518 last_dest_mtime = dest->mtime;
520 if (src) {
521 src_need_scan = mh_scan_required(src->folder, src);
522 last_src_mtime = src->mtime;
525 total = g_slist_length(msglist);
526 if (total > 100) {
527 if (MSG_IS_MOVE(msginfo->flags))
528 statusbar_print_all(_("Moving messages..."));
529 else
530 statusbar_print_all(_("Copying messages..."));
532 for (cur = msglist; cur; cur = cur->next) {
533 msginfo = (MsgInfo *)cur->data;
534 if (!msginfo) {
535 goto err_reset_status;
537 if (!full_fetch) {
538 srcfile = g_strconcat(srcpath,
539 G_DIR_SEPARATOR_S,
540 itos(msginfo->msgnum), NULL);
541 } else {
542 srcfile = procmsg_get_message_file(msginfo);
544 if (!srcfile) {
545 goto err_reset_status;
547 destfile = mh_get_new_msg_filename(dest);
548 if (!destfile) {
549 g_free(srcfile);
550 goto err_reset_status;
553 if (total > 100) {
554 statusbar_progress_all(curnum, total, 100);
555 if (curnum % 100 == 0)
556 GTK_EVENTS_FLUSH();
557 curnum++;
560 debug_print("Copying message %s%c%d to %s ...\n",
561 msginfo->folder->path, G_DIR_SEPARATOR,
562 msginfo->msgnum, dest->path);
565 if (MSG_IS_MOVE(msginfo->flags)) {
566 msginfo->flags.tmp_flags &= ~MSG_MOVE_DONE;
567 if (move_file(srcfile, destfile, TRUE) < 0) {
568 FILE_OP_ERROR(srcfile, "move");
569 if (copy_file(srcfile, destfile, TRUE) < 0) {
570 FILE_OP_ERROR(srcfile, "copy");
571 g_free(srcfile);
572 g_free(destfile);
573 goto err_reset_status;
575 } else {
576 /* say unlinking's not necessary */
577 msginfo->flags.tmp_flags |= MSG_MOVE_DONE;
579 } else if (copy_file(srcfile, destfile, TRUE) < 0) {
580 FILE_OP_ERROR(srcfile, "copy");
581 g_free(srcfile);
582 g_free(destfile);
583 goto err_reset_status;
585 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
586 if (chmod(destfile, prefs->folder_chmod) < 0)
587 FILE_OP_ERROR(destfile, "chmod");
589 if (relation) {
590 if (g_hash_table_lookup(relation, msginfo) != NULL)
591 g_warning("already in: %p", msginfo);
593 g_hash_table_insert(relation, msginfo, GINT_TO_POINTER(dest->last_num+1));
595 g_free(srcfile);
596 g_free(destfile);
597 dest->last_num++;
600 g_free(srcpath);
601 mh_write_sequences(dest, TRUE);
603 if (dest->mtime == last_dest_mtime && !dest_need_scan) {
604 mh_set_mtime(folder, dest);
607 if (src && src->mtime == last_src_mtime && !src_need_scan) {
608 mh_set_mtime(folder, src);
611 if (total > 100) {
612 statusbar_progress_all(0,0,0);
613 statusbar_pop_all();
615 return dest->last_num;
616 err_reset_status:
617 g_free(srcpath);
618 mh_write_sequences(dest, TRUE);
619 if (total > 100) {
620 statusbar_progress_all(0,0,0);
621 statusbar_pop_all();
623 return -1;
627 static gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
629 gboolean need_scan = FALSE;
630 time_t last_mtime = (time_t)0;
631 gchar *file;
633 cm_return_val_if_fail(item != NULL, -1);
635 file = mh_fetch_msg(folder, item, num);
636 cm_return_val_if_fail(file != NULL, -1);
638 need_scan = mh_scan_required(folder, item);
639 last_mtime = item->mtime;
641 if (claws_unlink(file) < 0) {
642 FILE_OP_ERROR(file, "unlink");
643 g_free(file);
644 return -1;
647 if (item->mtime == last_mtime && !need_scan) {
648 mh_set_mtime(folder, item);
650 g_free(file);
651 return 0;
654 static gint mh_remove_msgs(Folder *folder, FolderItem *item,
655 MsgInfoList *msglist, GHashTable *relation)
657 gboolean need_scan = FALSE;
658 gchar *path, *file;
659 time_t last_mtime = (time_t)0;
660 MsgInfoList *cur;
661 gint total = 0, curnum = 0;
663 cm_return_val_if_fail(item != NULL, -1);
665 path = folder_item_get_path(item);
667 need_scan = mh_scan_required(folder, item);
668 last_mtime = item->mtime;
670 total = g_slist_length(msglist);
671 if (total > 100) {
672 statusbar_print_all(_("Deleting messages..."));
675 for (cur = msglist; cur; cur = cur->next) {
676 MsgInfo *msginfo = (MsgInfo *)cur->data;
677 if (msginfo == NULL)
678 continue;
679 if (MSG_IS_MOVE(msginfo->flags) && MSG_IS_MOVE_DONE(msginfo->flags)) {
680 msginfo->flags.tmp_flags &= ~MSG_MOVE_DONE;
681 continue;
683 if (total > 100) {
684 statusbar_progress_all(curnum, total, 100);
685 if (curnum % 100 == 0)
686 GTK_EVENTS_FLUSH();
687 curnum++;
690 file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(msginfo->msgnum), NULL);
691 if (file == NULL)
692 continue;
694 if (claws_unlink(file) < 0) {
695 g_free(file);
696 continue;
699 g_free(file);
702 if (total > 100) {
703 statusbar_progress_all(0,0,0);
704 statusbar_pop_all();
706 if (item->mtime == last_mtime && !need_scan) {
707 mh_set_mtime(folder, item);
710 g_free(path);
711 return 0;
714 static gint mh_remove_all_msg(Folder *folder, FolderItem *item)
716 gchar *path;
717 gint val;
719 cm_return_val_if_fail(item != NULL, -1);
721 path = folder_item_get_path(item);
722 cm_return_val_if_fail(path != NULL, -1);
723 val = remove_all_numbered_files(path);
724 g_free(path);
726 mh_write_sequences(item, TRUE);
728 return val;
731 static gboolean mh_is_msg_changed(Folder *folder, FolderItem *item,
732 MsgInfo *msginfo)
734 #ifdef G_OS_WIN32
735 GFile *f;
736 GFileInfo *fi;
737 GTimeVal tv;
738 GError *error = NULL;
739 #else
740 GStatBuf s;
741 int r;
742 #endif
743 gchar *path;
744 gchar *parent_path;
746 parent_path = folder_item_get_path(item);
747 path = g_strdup_printf("%s%c%d", parent_path,
748 G_DIR_SEPARATOR, msginfo->msgnum);
749 g_free(parent_path);
751 #ifdef G_OS_WIN32
752 f = g_file_new_for_path(path);
753 g_free(path);
754 fi = g_file_query_info(f, "standard::size,time::modified",
755 G_FILE_QUERY_INFO_NONE, NULL, &error);
756 if (error != NULL) {
757 g_warning(error->message);
758 g_error_free(error);
759 g_object_unref(f);
760 return TRUE;
763 g_file_info_get_modification_time(fi, &tv);
764 if (msginfo->size != g_file_info_get_size(fi) || (
765 (msginfo->mtime - tv.tv_sec != 0) &&
766 abs(msginfo->mtime - tv.tv_sec) != 3600)) {
767 g_error_free(error);
768 g_object_unref(f);
769 return TRUE;
771 #else
772 r = g_stat(path, &s);
773 g_free(path);
774 if (r < 0 ||
775 msginfo->size != s.st_size || (
776 (msginfo->mtime - s.st_mtime != 0) &&
777 (msginfo->mtime - s.st_mtime != 3600) &&
778 (msginfo->mtime - s.st_mtime != -3600))) {
779 return TRUE;
781 #endif
783 return FALSE;
786 static gint mh_scan_tree(Folder *folder)
788 FolderItem *item;
790 cm_return_val_if_fail(folder != NULL, -1);
792 if (!folder->node) {
793 item = folder_item_new(folder, folder->name, NULL);
794 item->folder = folder;
795 folder->node = item->node = g_node_new(item);
796 } else
797 item = FOLDER_ITEM(folder->node->data);
799 mh_create_tree(folder);
800 mh_remove_missing_folder_items(folder);
801 mh_scan_tree_recursive(item);
803 return 0;
806 #define MAKE_DIR_IF_NOT_EXIST(dir) \
808 if (!is_dir_exist(dir)) { \
809 if (is_file_exist(dir)) { \
810 g_warning("File '%s' already exists. " \
811 "Can't create folder.", dir); \
812 return -1; \
814 if (make_dir_hier(dir) < 0) \
815 return -1; \
816 debug_print("Created dir '%s'\n", dir); \
820 static gint mh_create_tree(Folder *folder)
822 gchar *rootpath, *f, *path;
824 cm_return_val_if_fail(folder != NULL, -1);
826 rootpath = LOCAL_FOLDER(folder)->rootpath;
827 #ifdef G_OS_UNIX
828 if (*rootpath == '/') {
829 #elif defined G_OS_WIN32
830 if (g_ascii_isalpha(*rootpath) && !strncmp(rootpath + 1, ":\\", 2)) {
831 #endif
832 /* Folder path is absolute. */
833 rootpath = g_strdup(rootpath);
834 } else {
835 /* Folder path is relative, using mail base dir. */
836 rootpath = g_strconcat(get_mail_base_dir(), G_DIR_SEPARATOR_S,
837 rootpath, NULL);
840 MAKE_DIR_IF_NOT_EXIST(rootpath);
842 /* Create special directories as needed */
843 if (folder->inbox != NULL &&
844 folder->inbox->path != NULL)
845 f = folder->inbox->path;
846 else
847 f = INBOX_DIR;
848 path = g_strconcat(rootpath, G_DIR_SEPARATOR_S, f, NULL);
849 MAKE_DIR_IF_NOT_EXIST(path);
850 g_free(path);
852 if (folder->outbox != NULL &&
853 folder->outbox->path != NULL)
854 f = folder->outbox->path;
855 else
856 f = OUTBOX_DIR;
857 path = g_strconcat(rootpath, G_DIR_SEPARATOR_S, f, NULL);
858 MAKE_DIR_IF_NOT_EXIST(path);
859 g_free(path);
861 if (folder->draft != NULL &&
862 folder->draft->path != NULL)
863 f = folder->draft->path;
864 else
865 f = DRAFT_DIR;
866 path = g_strconcat(rootpath, G_DIR_SEPARATOR_S, f, NULL);
867 MAKE_DIR_IF_NOT_EXIST(path);
868 g_free(path);
870 if (folder->queue != NULL &&
871 folder->queue->path != NULL)
872 f = folder->queue->path;
873 else
874 f = QUEUE_DIR;
875 path = g_strconcat(rootpath, G_DIR_SEPARATOR_S, f, NULL);
876 MAKE_DIR_IF_NOT_EXIST(path);
877 g_free(path);
879 if (folder->trash != NULL &&
880 folder->trash->path != NULL)
881 f = folder->trash->path;
882 else
883 f = TRASH_DIR;
884 path = g_strconcat(rootpath, G_DIR_SEPARATOR_S, f, NULL);
885 MAKE_DIR_IF_NOT_EXIST(path);
886 g_free(path);
888 g_free(rootpath);
889 return 0;
892 #undef MAKE_DIR_IF_NOT_EXIST
894 static gchar *mh_item_get_path(Folder *folder, FolderItem *item)
896 gchar *folder_path, *path;
897 gchar *real_path;
898 cm_return_val_if_fail(folder != NULL, NULL);
899 cm_return_val_if_fail(item != NULL, NULL);
901 folder_path = g_strdup(LOCAL_FOLDER(folder)->rootpath);
902 cm_return_val_if_fail(folder_path != NULL, NULL);
904 /* FIXME: [W32] The code below does not correctly merge
905 relative filenames; there should be a function to handle
906 this. */
907 if ( !is_relative_filename (folder_path) ) {
908 if (item->path)
909 path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
910 item->path, NULL);
911 else
912 path = g_strdup(folder_path);
913 } else {
914 if (item->path)
915 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
916 folder_path, G_DIR_SEPARATOR_S,
917 item->path, NULL);
918 else
919 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
920 folder_path, NULL);
922 g_free(folder_path);
923 real_path = mh_filename_from_utf8(path);
924 if (!is_dir_exist(real_path) && is_dir_exist(path)) {
925 /* mmh, older version did put utf8 filenames instead of
926 * the correct encoding */
927 if (g_rename(path, real_path) == 0)
928 folder_item_scan(item);
931 g_free(path);
932 return real_path;
935 static gboolean mh_renumber_msg(MsgInfo *info)
937 gchar *src, *dest;
938 gboolean result = FALSE;
939 guint num;
940 cm_return_val_if_fail(info != NULL, FALSE);
942 src = folder_item_fetch_msg(info->folder, info->msgnum);
943 dest = mh_get_new_msg_filename(info->folder);
944 num = info->folder->last_num + 1;
946 if (move_file(src, dest, FALSE) == 0) {
947 msgcache_remove_msg(info->folder->cache, info->msgnum);
948 info->msgnum = num;
949 msgcache_add_msg(info->folder->cache, info);
950 result = TRUE;
953 g_free(src);
954 g_free(dest);
956 return result;
959 static FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
960 const gchar *name)
962 gchar *path, *real_name;
963 gchar *fullpath;
964 FolderItem *new_item;
965 gchar *mh_sequences_filename;
966 FILE *mh_sequences_file;
968 cm_return_val_if_fail(folder != NULL, NULL);
969 cm_return_val_if_fail(parent != NULL, NULL);
970 cm_return_val_if_fail(name != NULL, NULL);
972 path = folder_item_get_path(parent);
973 if (!is_dir_exist(path))
974 if (make_dir_hier(path) != 0)
975 return NULL;
977 real_name = mh_filename_from_utf8(name);
978 fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, real_name, NULL);
979 g_free(real_name);
980 g_free(path);
982 if (to_number(name) > 0) {
983 MsgInfo *info = folder_item_get_msginfo(parent, to_number(name));
984 if (info != NULL) {
985 gboolean ok = mh_renumber_msg(info);
986 procmsg_msginfo_free(&info);
987 if (!ok) {
988 g_free(fullpath);
989 return NULL;
994 if (make_dir(fullpath) < 0) {
995 g_free(fullpath);
996 return NULL;
999 g_free(fullpath);
1001 if (parent->path)
1002 path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
1003 NULL);
1004 else
1005 path = g_strdup(name);
1006 new_item = folder_item_new(folder, name, path);
1007 folder_item_append(parent, new_item);
1009 g_free(path);
1011 path = folder_item_get_path(new_item);
1012 mh_sequences_filename = g_strconcat(path, G_DIR_SEPARATOR_S,
1013 ".mh_sequences", NULL);
1014 if ((mh_sequences_file = claws_fopen(mh_sequences_filename, "a+b")) != NULL) {
1015 claws_fclose(mh_sequences_file);
1017 g_free(mh_sequences_filename);
1018 g_free(path);
1020 return new_item;
1023 static gint mh_rename_folder(Folder *folder, FolderItem *item,
1024 const gchar *name)
1026 gchar *real_name;
1027 gchar *oldpath;
1028 gchar *dirname;
1029 gchar *newpath, *utf8newpath;
1030 gchar *paths[2];
1032 cm_return_val_if_fail(folder != NULL, -1);
1033 cm_return_val_if_fail(item != NULL, -1);
1034 cm_return_val_if_fail(item->path != NULL, -1);
1035 cm_return_val_if_fail(name != NULL, -1);
1037 oldpath = folder_item_get_path(item);
1038 if (!is_dir_exist(oldpath))
1039 make_dir_hier(oldpath);
1041 dirname = g_path_get_dirname(oldpath);
1042 real_name = mh_filename_from_utf8(name);
1043 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, real_name, NULL);
1044 g_free(real_name);
1046 if (g_rename(oldpath, newpath) < 0) {
1047 FILE_OP_ERROR(oldpath, "rename");
1048 g_free(oldpath);
1049 g_free(newpath);
1050 return -1;
1053 g_free(oldpath);
1054 g_free(newpath);
1056 if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
1057 dirname = g_path_get_dirname(item->path);
1058 utf8newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S,
1059 name, NULL);
1060 g_free(dirname);
1061 } else
1062 utf8newpath = g_strdup(name);
1064 g_free(item->name);
1065 item->name = g_strdup(name);
1067 paths[0] = g_strdup(item->path);
1068 paths[1] = utf8newpath;
1069 g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
1070 mh_rename_folder_func, paths);
1072 g_free(paths[0]);
1073 g_free(paths[1]);
1074 return 0;
1077 static gint mh_remove_folder(Folder *folder, FolderItem *item)
1079 gchar *path;
1080 gint ret;
1082 cm_return_val_if_fail(folder != NULL, -1);
1083 cm_return_val_if_fail(item != NULL, -1);
1084 cm_return_val_if_fail(item->path != NULL, -1);
1086 path = folder_item_get_path(item);
1087 if ((ret = remove_dir_recursive(path)) < 0) {
1088 g_warning("can't remove directory '%s'", path);
1089 g_free(path);
1090 return ret;
1093 g_free(path);
1094 folder_item_remove(item);
1095 return 0;
1098 static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
1100 MsgInfo *msginfo;
1101 MsgFlags flags;
1103 cm_return_val_if_fail(item != NULL, NULL);
1104 cm_return_val_if_fail(file != NULL, NULL);
1106 flags.perm_flags = MSG_NEW|MSG_UNREAD;
1107 flags.tmp_flags = 0;
1109 if (folder_has_parent_of_type(item, F_QUEUE)) {
1110 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
1111 } else if (folder_has_parent_of_type(item, F_DRAFT)) {
1112 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
1115 msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
1116 if (!msginfo) return NULL;
1118 msginfo->msgnum = atoi(file);
1119 msginfo->folder = item;
1121 return msginfo;
1124 static gboolean mh_remove_missing_folder_items_func(GNode *node, gpointer data)
1126 FolderItem *item;
1127 gchar *path;
1129 cm_return_val_if_fail(node->data != NULL, FALSE);
1131 if (G_NODE_IS_ROOT(node))
1132 return FALSE;
1134 item = FOLDER_ITEM(node->data);
1136 path = folder_item_get_path(item);
1137 if (!is_dir_exist(path)) {
1138 debug_print("folder '%s' not found. removing...\n", path?path:"(null)");
1139 folder_item_remove(item);
1141 g_free(path);
1143 return FALSE;
1146 static void mh_remove_missing_folder_items(Folder *folder)
1148 cm_return_if_fail(folder != NULL);
1150 debug_print("searching missing folders...\n");
1152 g_node_traverse(folder->node, G_POST_ORDER, G_TRAVERSE_ALL, -1,
1153 mh_remove_missing_folder_items_func, folder);
1156 static void mh_scan_tree_recursive(FolderItem *item)
1158 Folder *folder;
1159 GDir *dir;
1160 const gchar *dir_name;
1161 gchar *entry, *utf8entry, *utf8name, *path;
1162 gint n_msg = 0;
1163 GError *error = NULL;
1165 cm_return_if_fail(item != NULL);
1166 cm_return_if_fail(item->folder != NULL);
1168 folder = item->folder;
1170 path = folder_item_get_path(item);
1171 debug_print("mh_scan_tree_recursive() opening '%s'\n", path);
1172 dir = g_dir_open(path, 0, &error);
1173 if (!dir) {
1174 g_warning("failed to open directory '%s': %s (%d)",
1175 path, error->message, error->code);
1176 g_error_free(error);
1177 g_free(path);
1178 return;
1181 debug_print("scanning %s ...\n",
1182 item->path ? item->path
1183 : LOCAL_FOLDER(item->folder)->rootpath);
1184 if (folder->ui_func)
1185 folder->ui_func(folder, item, folder->ui_func_data);
1187 while ((dir_name = g_dir_read_name(dir)) != NULL) {
1188 if (dir_name[0] == '.') continue;
1190 entry = g_strconcat(path, G_DIR_SEPARATOR_S, dir_name, NULL);
1192 utf8name = mh_filename_to_utf8(dir_name);
1193 if (item->path)
1194 utf8entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
1195 utf8name, NULL);
1196 else
1197 utf8entry = g_strdup(utf8name);
1199 if (g_file_test(entry, G_FILE_TEST_IS_DIR)) {
1200 FolderItem *new_item = NULL;
1201 GNode *node;
1203 node = item->node;
1204 for (node = node->children; node != NULL; node = node->next) {
1205 FolderItem *cur_item = FOLDER_ITEM(node->data);
1206 gchar *curpath = folder_item_get_path(cur_item);
1207 if (!g_strcmp0(curpath, entry)) {
1208 new_item = cur_item;
1209 g_free(curpath);
1210 break;
1212 g_free(curpath);
1214 if (!new_item) {
1215 debug_print("new folder '%s' found.\n", entry);
1216 new_item = folder_item_new(folder, utf8name, utf8entry);
1217 folder_item_append(item, new_item);
1220 if (!item->path) {
1221 if (!folder->inbox &&
1222 !strcmp(dir_name, INBOX_DIR)) {
1223 new_item->stype = F_INBOX;
1224 folder->inbox = new_item;
1225 } else if (!folder->outbox &&
1226 !strcmp(dir_name, OUTBOX_DIR)) {
1227 new_item->stype = F_OUTBOX;
1228 folder->outbox = new_item;
1229 } else if (!folder->draft &&
1230 !strcmp(dir_name, DRAFT_DIR)) {
1231 new_item->stype = F_DRAFT;
1232 folder->draft = new_item;
1233 } else if (!folder->queue &&
1234 !strcmp(dir_name, QUEUE_DIR)) {
1235 new_item->stype = F_QUEUE;
1236 folder->queue = new_item;
1237 } else if (!folder->trash &&
1238 !strcmp(dir_name, TRASH_DIR)) {
1239 new_item->stype = F_TRASH;
1240 folder->trash = new_item;
1244 mh_scan_tree_recursive(new_item);
1245 } else if (to_number(dir_name) > 0) n_msg++;
1247 g_free(entry);
1248 g_free(utf8entry);
1249 g_free(utf8name);
1252 g_dir_close(dir);
1253 g_free(path);
1255 mh_set_mtime(folder, item);
1258 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
1260 FolderItem *item = node->data;
1261 gchar **paths = data;
1262 const gchar *oldpath = paths[0];
1263 const gchar *newpath = paths[1];
1264 gchar *base;
1265 gchar *new_itempath;
1266 gint oldpathlen;
1268 oldpathlen = strlen(oldpath);
1269 if (strncmp(oldpath, item->path, oldpathlen) != 0) {
1270 g_warning("path doesn't match: %s, %s", oldpath, item->path);
1271 return TRUE;
1274 base = item->path + oldpathlen;
1275 while (*base == G_DIR_SEPARATOR) base++;
1276 if (*base == '\0')
1277 new_itempath = g_strdup(newpath);
1278 else
1279 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
1280 NULL);
1281 g_free(item->path);
1282 item->path = new_itempath;
1284 return FALSE;
1287 static gchar *mh_filename_from_utf8(const gchar *path)
1289 gchar *real_path = g_filename_from_utf8(path, -1, NULL, NULL, NULL);
1291 if (!real_path) {
1292 g_warning("mh_filename_from_utf8: failed to convert character set");
1293 real_path = g_strdup(path);
1296 return real_path;
1299 static gchar *mh_filename_to_utf8(const gchar *path)
1301 gchar *utf8path = g_filename_to_utf8(path, -1, NULL, NULL, NULL);
1302 if (!utf8path) {
1303 g_warning("mh_filename_to_utf8: failed to convert character set");
1304 utf8path = g_strdup(path);
1307 return utf8path;
1310 static gint sort_cache_list_by_msgnum(gconstpointer a, gconstpointer b)
1312 MsgInfo *msginfo_a = (MsgInfo *) a;
1313 MsgInfo *msginfo_b = (MsgInfo *) b;
1315 return (msginfo_a->msgnum - msginfo_b->msgnum);
1318 static gchar *get_unseen_seq_name(void)
1320 static gchar *seq_name = NULL;
1321 if (!seq_name) {
1322 gchar buf[BUFFSIZE];
1323 gchar *tmp;
1324 gchar *profile_path = g_strconcat(
1325 get_home_dir(), G_DIR_SEPARATOR_S,
1326 ".mh_profile", NULL);
1327 FILE *fp = claws_fopen(profile_path, "r");
1328 if (fp) {
1329 while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
1330 if (!strncmp(buf, "Unseen-Sequence:", strlen("Unseen-Sequence:"))) {
1331 gchar *seq_tmp = buf+strlen("Unseen-Sequence:");
1332 while (*seq_tmp == ' ')
1333 seq_tmp++;
1334 seq_name = g_strdup(seq_tmp);
1335 seq_name = strretchomp(seq_name);
1336 break;
1339 claws_fclose(fp);
1341 if (!seq_name)
1342 seq_name = g_strdup("unseen");
1343 tmp = g_strdup_printf("%s:", seq_name);
1344 g_free(seq_name);
1345 seq_name = tmp;
1347 return seq_name;
1350 static void mh_write_sequences(FolderItem *item, gboolean remove_unseen)
1352 gchar *mh_sequences_old, *mh_sequences_new;
1353 FILE *mh_sequences_old_fp, *mh_sequences_new_fp;
1354 gchar buf[BUFFSIZE];
1355 gchar *path = NULL;
1356 gboolean err = FALSE;
1357 START_TIMING("");
1359 if (!item)
1360 return;
1362 path = folder_item_get_path(item);
1364 mh_sequences_old = g_strconcat(path, G_DIR_SEPARATOR_S,
1365 ".mh_sequences", NULL);
1366 mh_sequences_new = g_strconcat(path, G_DIR_SEPARATOR_S,
1367 ".mh_sequences.new", NULL);
1368 if ((mh_sequences_new_fp = claws_fopen(mh_sequences_new, "w+b")) != NULL) {
1369 GSList *msglist = folder_item_get_msg_list(item);
1370 GSList *cur;
1371 MsgInfo *info = NULL;
1372 gint start = -1, end = -1;
1373 gchar *sequence = g_strdup("");
1374 gint seq_len = 0;
1375 msglist = g_slist_sort(msglist, sort_cache_list_by_msgnum);
1376 cur = msglist;
1378 /* write the unseen sequence if we don't have to scrap it */
1379 if (!remove_unseen) do {
1380 info = (MsgInfo *)(cur ? cur->data:NULL);
1381 if (info && (MSG_IS_UNREAD(info->flags) || MSG_IS_NEW(info->flags))) {
1382 if (start < 0)
1383 start = end = info->msgnum;
1384 else
1385 end = info->msgnum;
1386 } else {
1387 if (start > 0 && end > 0) {
1388 gchar tmp[32];
1389 gint tmp_len = 0;
1390 if (start != end)
1391 snprintf(tmp, 31, " %d-%d", start, end);
1392 else
1393 snprintf(tmp, 31, " %d", start);
1395 tmp_len = strlen(tmp);
1396 sequence = g_realloc(sequence, seq_len+tmp_len+1);
1397 strcpy(sequence+seq_len, tmp);
1398 seq_len += tmp_len;
1400 start = end = -1;
1403 cur = cur ? cur->next:NULL;
1404 } while (cur || (start > 0 && end > 0));
1405 if (sequence && *sequence) {
1406 if (fprintf(mh_sequences_new_fp, "%s%s\n",
1407 get_unseen_seq_name(), sequence) < 0)
1408 err = TRUE;
1409 else
1410 debug_print("wrote unseen sequence: '%s%s'\n",
1411 get_unseen_seq_name(), sequence);
1413 /* rewrite the rest of the file */
1414 if ((mh_sequences_old_fp = claws_fopen(mh_sequences_old, "r+b")) != NULL) {
1415 while (claws_fgets(buf, sizeof(buf), mh_sequences_old_fp) != NULL) {
1416 if (strncmp(buf, get_unseen_seq_name(), strlen(get_unseen_seq_name())))
1417 if (fprintf(mh_sequences_new_fp, "%s", buf) < 0) {
1418 err = TRUE;
1419 break;
1422 claws_fclose(mh_sequences_old_fp);
1425 if (claws_safe_fclose(mh_sequences_new_fp) == EOF)
1426 err = TRUE;
1428 if (!err) {
1429 if (g_rename(mh_sequences_new, mh_sequences_old) < 0)
1430 FILE_OP_ERROR(mh_sequences_new, "rename");
1432 g_free(sequence);
1433 procmsg_msg_list_free(msglist);
1435 g_free(mh_sequences_old);
1436 g_free(mh_sequences_new);
1437 g_free(path);
1439 END_TIMING();
1442 static int mh_item_close(Folder *folder, FolderItem *item)
1444 time_t last_mtime = (time_t)0;
1445 gboolean need_scan = mh_scan_required(item->folder, item);
1446 last_mtime = item->mtime;
1448 mh_write_sequences(item, FALSE);
1450 if (item->mtime == last_mtime && !need_scan) {
1451 mh_set_mtime(folder, item);
1454 return 0;
1457 static void mh_set_mtime(Folder *folder, FolderItem *item)
1459 GStatBuf s;
1460 gchar *path = folder_item_get_path(item);
1462 cm_return_if_fail(path != NULL);
1464 if (g_stat(path, &s) < 0) {
1465 FILE_OP_ERROR(path, "stat");
1466 g_free(path);
1467 return;
1470 item->mtime = s.st_mtime;
1471 debug_print("MH: forced mtime of %s to %"G_GSIZE_FORMAT"\n", item->name?item->name:"(null)", item->mtime);
1472 g_free(path);