2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2005 Hiroyuki Yamamoto
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.
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, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 #include <glib/gi18n.h>
37 # include <sys/time.h>
43 #include "procheader.h"
47 static void mh_folder_init (Folder *folder,
51 static Folder *mh_folder_new (const gchar *name,
53 static void mh_folder_destroy (Folder *folder);
54 static gchar *mh_fetch_msg (Folder *folder,
57 static MsgInfo *mh_get_msginfo (Folder *folder,
60 static gint mh_add_msg (Folder *folder,
64 static gint mh_add_msgs (Folder *folder,
68 static gint mh_copy_msg (Folder *folder,
71 static gint mh_remove_msg (Folder *folder,
74 static gint mh_remove_all_msg (Folder *folder,
76 static gboolean mh_is_msg_changed (Folder *folder,
80 static gint mh_get_num_list (Folder *folder,
83 gboolean *old_uids_valid);
84 static gint mh_scan_tree (Folder *folder);
86 static gint mh_create_tree (Folder *folder);
87 static FolderItem *mh_create_folder (Folder *folder,
90 static gint mh_rename_folder (Folder *folder,
93 static gint mh_remove_folder (Folder *folder,
96 static gchar *mh_get_new_msg_filename (FolderItem *dest);
98 static MsgInfo *mh_parse_msg (const gchar *file,
100 static void mh_remove_missing_folder_items (Folder *folder);
101 static gchar *mh_filename_from_utf8 (const gchar *path);
102 static gchar *mh_filename_to_utf8 (const gchar *path);
103 static void mh_scan_tree_recursive (FolderItem *item);
105 static gboolean mh_rename_folder_func (GNode *node,
107 static gchar *mh_item_get_path (Folder *folder,
110 static gboolean mh_scan_required (Folder *folder,
113 static FolderClass mh_class;
115 FolderClass *mh_get_class(void)
117 if (mh_class.idstr == NULL) {
118 mh_class.type = F_MH;
119 mh_class.idstr = "mh";
120 mh_class.uistr = "MH";
122 /* Folder functions */
123 mh_class.new_folder = mh_folder_new;
124 mh_class.destroy_folder = mh_folder_destroy;
125 mh_class.set_xml = folder_local_set_xml;
126 mh_class.get_xml = folder_local_get_xml;
127 mh_class.scan_tree = mh_scan_tree;
128 mh_class.create_tree = mh_create_tree;
130 /* FolderItem functions */
131 mh_class.item_get_path = mh_item_get_path;
132 mh_class.create_folder = mh_create_folder;
133 mh_class.rename_folder = mh_rename_folder;
134 mh_class.remove_folder = mh_remove_folder;
135 mh_class.get_num_list = mh_get_num_list;
136 mh_class.scan_required = mh_scan_required;
138 /* Message functions */
139 mh_class.get_msginfo = mh_get_msginfo;
140 mh_class.fetch_msg = mh_fetch_msg;
141 mh_class.add_msg = mh_add_msg;
142 mh_class.add_msgs = mh_add_msgs;
143 mh_class.copy_msg = mh_copy_msg;
144 mh_class.remove_msg = mh_remove_msg;
145 mh_class.remove_all_msg = mh_remove_all_msg;
146 mh_class.is_msg_changed = mh_is_msg_changed;
152 static Folder *mh_folder_new(const gchar *name, const gchar *path)
156 folder = (Folder *)g_new0(MHFolder, 1);
157 folder->klass = &mh_class;
158 mh_folder_init(folder, name, path);
163 static void mh_folder_destroy(Folder *folder)
165 folder_local_folder_destroy(LOCAL_FOLDER(folder));
168 static void mh_folder_init(Folder *folder, const gchar *name, const gchar *path)
170 folder_local_folder_init(folder, name, path);
174 gboolean mh_scan_required(Folder *folder, FolderItem *item)
179 path = folder_item_get_path(item);
180 g_return_val_if_fail(path != NULL, FALSE);
182 if (stat(path, &s) < 0) {
183 FILE_OP_ERROR(path, "stat");
188 if ((s.st_mtime > item->mtime) &&
189 (s.st_mtime - 3600 != item->mtime)) {
190 debug_print("MH scan required, folder updated: %s (%ld > %ld)\n",
198 debug_print("MH scan not required: %s (%ld <= %ld)\n",
206 void mh_get_last_num(Folder *folder, FolderItem *item)
214 g_return_if_fail(item != NULL);
216 debug_print("mh_get_last_num(): Scanning %s ...\n", item->path);
218 path = folder_item_get_path(item);
219 g_return_if_fail(path != NULL);
220 if (change_dir(path) < 0) {
226 if ((dp = opendir(".")) == NULL) {
227 FILE_OP_ERROR(item->path, "opendir");
231 while ((d = readdir(dp)) != NULL) {
232 if ((num = to_number(d->d_name)) > 0 &&
233 dirent_is_regular_file(d)) {
240 debug_print("Last number in dir %s = %d\n", item->path, max);
241 item->last_num = max;
244 gint mh_get_num_list(Folder *folder, FolderItem *item, GSList **list, gboolean *old_uids_valid)
250 gint num, nummsgs = 0;
252 g_return_val_if_fail(item != NULL, -1);
254 debug_print("mh_get_num_list(): Scanning %s ...\n", item->path);
256 *old_uids_valid = TRUE;
258 path = folder_item_get_path(item);
259 g_return_val_if_fail(path != NULL, -1);
260 if (change_dir(path) < 0) {
266 if ((dp = opendir(".")) == NULL) {
267 FILE_OP_ERROR(item->path, "opendir");
271 while ((d = readdir(dp)) != NULL) {
272 if ((num = to_number(d->d_name)) > 0) {
273 *list = g_slist_prepend(*list, GINT_TO_POINTER(num));
279 item->mtime = time(NULL);
283 static gchar *mh_fetch_msg(Folder *folder, FolderItem *item, gint num)
288 g_return_val_if_fail(item != NULL, NULL);
289 g_return_val_if_fail(num > 0, NULL);
291 path = folder_item_get_path(item);
292 file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
294 if (!is_file_exist(file)) {
303 static MsgInfo *mh_get_msginfo(Folder *folder, FolderItem *item, gint num)
308 g_return_val_if_fail(item != NULL, NULL);
309 g_return_val_if_fail(num > 0, NULL);
311 file = mh_fetch_msg(folder, item, num);
312 if (!file) return NULL;
314 msginfo = mh_parse_msg(file, item);
316 msginfo->msgnum = num;
323 static gchar *mh_get_new_msg_filename(FolderItem *dest)
328 destpath = folder_item_get_path(dest);
329 g_return_val_if_fail(destpath != NULL, NULL);
331 if (!is_dir_exist(destpath))
332 make_dir_hier(destpath);
335 destfile = g_strdup_printf("%s%c%d", destpath, G_DIR_SEPARATOR,
337 if (is_file_entry_exist(destfile)) {
349 static gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file, MsgFlags *flags)
353 MsgFileInfo fileinfo;
355 g_return_val_if_fail(file != NULL, -1);
357 fileinfo.msginfo = NULL;
358 fileinfo.file = (gchar *)file;
359 fileinfo.flags = flags;
360 file_list.data = &fileinfo;
361 file_list.next = NULL;
363 ret = mh_add_msgs(folder, dest, &file_list, NULL);
367 static gint mh_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
372 MsgFileInfo *fileinfo;
374 g_return_val_if_fail(dest != NULL, -1);
375 g_return_val_if_fail(file_list != NULL, -1);
377 if (dest->last_num < 0) {
378 mh_get_last_num(folder, dest);
379 if (dest->last_num < 0) return -1;
382 for (cur = file_list; cur != NULL; cur = cur->next) {
383 fileinfo = (MsgFileInfo *)cur->data;
385 destfile = mh_get_new_msg_filename(dest);
386 if (destfile == NULL) return -1;
388 if (link(fileinfo->file, destfile) < 0) {
389 if (copy_file(fileinfo->file, destfile, TRUE) < 0) {
390 g_warning(_("can't copy message %s to %s\n"),
391 fileinfo->file, destfile);
396 if (relation != NULL)
397 g_relation_insert(relation, fileinfo, GINT_TO_POINTER(dest->last_num + 1));
402 return dest->last_num;
405 static gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
407 gboolean dest_need_scan = FALSE;
411 FolderItemPrefs *prefs;
413 g_return_val_if_fail(dest != NULL, -1);
414 g_return_val_if_fail(msginfo != NULL, -1);
416 if (msginfo->folder == dest) {
417 g_warning("the src folder is identical to the dest.\n");
421 if (dest->last_num < 0) {
422 mh_get_last_num(folder, dest);
423 if (dest->last_num < 0) return -1;
428 srcfile = procmsg_get_message_file(msginfo);
429 destfile = mh_get_new_msg_filename(dest);
435 debug_print("Copying message %s%c%d to %s ...\n",
436 msginfo->folder->path, G_DIR_SEPARATOR,
437 msginfo->msgnum, dest->path);
440 if ((MSG_IS_QUEUED(msginfo->flags) || MSG_IS_DRAFT(msginfo->flags))
441 && dest->stype != F_QUEUE && dest->stype != F_DRAFT) {
442 if (procmsg_remove_special_headers(srcfile, destfile) !=0) {
447 } else if (!(MSG_IS_QUEUED(msginfo->flags) || MSG_IS_DRAFT(msginfo->flags))
448 && (dest->stype == F_QUEUE || dest->stype == F_DRAFT)) {
452 } else if (copy_file(srcfile, destfile, TRUE) < 0) {
453 FILE_OP_ERROR(srcfile, "copy");
459 dest_need_scan = mh_scan_required(dest->folder, dest);
461 dest->mtime = time(NULL);
464 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
465 if (chmod(destfile, prefs->folder_chmod) < 0)
466 FILE_OP_ERROR(destfile, "chmod");
469 filemode = prefs->folder_chmod;
470 if (filemode & S_IRGRP) filemode |= S_IWGRP;
471 if (filemode & S_IROTH) filemode |= S_IWOTH;
478 return dest->last_num;
481 static gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
483 gboolean need_scan = FALSE;
486 g_return_val_if_fail(item != NULL, -1);
488 file = mh_fetch_msg(folder, item, num);
489 g_return_val_if_fail(file != NULL, -1);
491 need_scan = mh_scan_required(folder, item);
493 if (unlink(file) < 0) {
494 FILE_OP_ERROR(file, "unlink");
500 item->mtime = time(NULL);
506 static gint mh_remove_all_msg(Folder *folder, FolderItem *item)
511 g_return_val_if_fail(item != NULL, -1);
513 path = folder_item_get_path(item);
514 g_return_val_if_fail(path != NULL, -1);
515 val = remove_all_numbered_files(path);
521 static gboolean mh_is_msg_changed(Folder *folder, FolderItem *item,
526 if (stat(itos(msginfo->msgnum), &s) < 0 ||
527 msginfo->size != s.st_size || (
528 (msginfo->mtime - s.st_mtime != 0) &&
529 (msginfo->mtime - s.st_mtime != 3600) &&
530 (msginfo->mtime - s.st_mtime != -3600)))
536 static gint mh_scan_tree(Folder *folder)
541 g_return_val_if_fail(folder != NULL, -1);
544 item = folder_item_new(folder, folder->name, NULL);
545 item->folder = folder;
546 folder->node = item->node = g_node_new(item);
548 item = FOLDER_ITEM(folder->node->data);
550 rootpath = folder_item_get_path(item);
551 if (change_dir(rootpath) < 0) {
557 mh_create_tree(folder);
558 mh_remove_missing_folder_items(folder);
559 mh_scan_tree_recursive(item);
564 #define MAKE_DIR_IF_NOT_EXIST(dir) \
566 if (!is_dir_exist(dir)) { \
567 if (is_file_exist(dir)) { \
568 g_warning("File `%s' already exists.\n" \
569 "Can't create folder.", dir); \
572 if (make_dir(dir) < 0) \
577 static gint mh_create_tree(Folder *folder)
581 g_return_val_if_fail(folder != NULL, -1);
583 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), -1);
584 rootpath = LOCAL_FOLDER(folder)->rootpath;
585 MAKE_DIR_IF_NOT_EXIST(rootpath);
586 CHDIR_RETURN_VAL_IF_FAIL(rootpath, -1);
587 MAKE_DIR_IF_NOT_EXIST(INBOX_DIR);
588 MAKE_DIR_IF_NOT_EXIST(OUTBOX_DIR);
589 MAKE_DIR_IF_NOT_EXIST(QUEUE_DIR);
590 MAKE_DIR_IF_NOT_EXIST(DRAFT_DIR);
591 MAKE_DIR_IF_NOT_EXIST(TRASH_DIR);
596 #undef MAKE_DIR_IF_NOT_EXIST
598 static gchar *mh_item_get_path(Folder *folder, FolderItem *item)
600 gchar *folder_path, *path;
602 g_return_val_if_fail(folder != NULL, NULL);
603 g_return_val_if_fail(item != NULL, NULL);
605 folder_path = g_strdup(LOCAL_FOLDER(folder)->rootpath);
606 g_return_val_if_fail(folder_path != NULL, NULL);
608 if (folder_path[0] == G_DIR_SEPARATOR) {
610 path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
613 path = g_strdup(folder_path);
616 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
617 folder_path, G_DIR_SEPARATOR_S,
620 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
624 real_path = mh_filename_from_utf8(path);
625 if (!is_dir_exist(real_path) && is_dir_exist(path)) {
626 /* mmh, older version did put utf8 filenames instead of
627 * the correct encoding */
628 rename(path, real_path);
629 folder_item_scan(item);
636 static FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
639 gchar *path, *real_name;
641 FolderItem *new_item;
642 gchar *mh_sequences_filename;
643 FILE *mh_sequences_file;
645 g_return_val_if_fail(folder != NULL, NULL);
646 g_return_val_if_fail(parent != NULL, NULL);
647 g_return_val_if_fail(name != NULL, NULL);
649 path = folder_item_get_path(parent);
650 if (!is_dir_exist(path))
651 if (make_dir_hier(path) != 0)
654 real_name = mh_filename_from_utf8(name);
655 fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, real_name, NULL);
659 if (make_dir(fullpath) < 0) {
667 path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
670 path = g_strdup(name);
671 new_item = folder_item_new(folder, name, path);
672 folder_item_append(parent, new_item);
676 path = folder_item_get_path(new_item);
677 mh_sequences_filename = g_strconcat(path, G_DIR_SEPARATOR_S,
678 ".mh_sequences", NULL);
679 if ((mh_sequences_file = fopen(mh_sequences_filename, "a+b")) != NULL) {
680 fclose(mh_sequences_file);
682 g_free(mh_sequences_filename);
688 static gint mh_rename_folder(Folder *folder, FolderItem *item,
694 gchar *newpath, *utf8newpath;
697 g_return_val_if_fail(folder != NULL, -1);
698 g_return_val_if_fail(item != NULL, -1);
699 g_return_val_if_fail(item->path != NULL, -1);
700 g_return_val_if_fail(name != NULL, -1);
702 oldpath = folder_item_get_path(item);
703 if (!is_dir_exist(oldpath))
704 make_dir_hier(oldpath);
706 dirname = g_path_get_dirname(oldpath);
707 real_name = mh_filename_from_utf8(name);
708 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, real_name, NULL);
711 if (rename(oldpath, newpath) < 0) {
712 FILE_OP_ERROR(oldpath, "rename");
721 if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
722 dirname = g_path_get_dirname(item->path);
723 utf8newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S,
727 utf8newpath = g_strdup(name);
730 item->name = g_strdup(name);
732 paths[0] = g_strdup(item->path);
733 paths[1] = utf8newpath;
734 g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
735 mh_rename_folder_func, paths);
742 static gint mh_remove_folder(Folder *folder, FolderItem *item)
746 g_return_val_if_fail(folder != NULL, -1);
747 g_return_val_if_fail(item != NULL, -1);
748 g_return_val_if_fail(item->path != NULL, -1);
750 path = folder_item_get_path(item);
751 if (remove_dir_recursive(path) < 0) {
752 g_warning("can't remove directory `%s'\n", path);
758 folder_item_remove(item);
762 static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
767 g_return_val_if_fail(item != NULL, NULL);
768 g_return_val_if_fail(file != NULL, NULL);
770 flags.perm_flags = MSG_NEW|MSG_UNREAD;
773 if (item->stype == F_QUEUE) {
774 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
775 } else if (item->stype == F_DRAFT) {
776 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
779 msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
780 if (!msginfo) return NULL;
782 msginfo->msgnum = atoi(file);
783 msginfo->folder = item;
789 static gboolean mh_is_maildir_one(const gchar *path, const gchar *dir)
794 entry = g_strconcat(path, G_DIR_SEPARATOR_S, dir, NULL);
795 result = is_dir_exist(entry);
802 * check whether PATH is a Maildir style mailbox.
803 * This is the case if the 3 subdir: new, cur, tmp are existing.
804 * This functon assumes that entry is an directory
806 static gboolean mh_is_maildir(const gchar *path)
808 return mh_is_maildir_one(path, "new") &&
809 mh_is_maildir_one(path, "cur") &&
810 mh_is_maildir_one(path, "tmp");
814 static gboolean mh_remove_missing_folder_items_func(GNode *node, gpointer data)
819 g_return_val_if_fail(node->data != NULL, FALSE);
821 if (G_NODE_IS_ROOT(node))
824 item = FOLDER_ITEM(node->data);
826 path = folder_item_get_path(item);
827 if (!is_dir_exist(path)) {
828 debug_print("folder '%s' not found. removing...\n", path);
829 folder_item_remove(item);
836 static void mh_remove_missing_folder_items(Folder *folder)
838 g_return_if_fail(folder != NULL);
840 debug_print("searching missing folders...\n");
842 g_node_traverse(folder->node, G_POST_ORDER, G_TRAVERSE_ALL, -1,
843 mh_remove_missing_folder_items_func, folder);
846 static void mh_scan_tree_recursive(FolderItem *item)
852 gchar *real_path, *entry, *utf8entry, *utf8name;
855 g_return_if_fail(item != NULL);
856 g_return_if_fail(item->folder != NULL);
858 folder = item->folder;
860 real_path = item->path ? mh_filename_from_utf8(item->path) : g_strdup(".");
861 dp = opendir(real_path);
863 FILE_OP_ERROR(real_path, "opendir");
868 debug_print("scanning %s ...\n",
869 item->path ? item->path
870 : LOCAL_FOLDER(item->folder)->rootpath);
872 folder->ui_func(folder, item, folder->ui_func_data);
874 while ((d = readdir(dp)) != NULL) {
875 if (d->d_name[0] == '.') continue;
877 utf8name = mh_filename_to_utf8(d->d_name);
879 utf8entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
882 utf8entry = g_strdup(utf8name);
883 entry = mh_filename_from_utf8(utf8entry);
886 #ifdef HAVE_DIRENT_D_TYPE
887 d->d_type == DT_DIR ||
888 (d->d_type == DT_UNKNOWN &&
890 stat(entry, &s) == 0 && S_ISDIR(s.st_mode)
891 #ifdef HAVE_DIRENT_D_TYPE
895 FolderItem *new_item = NULL;
899 if (mh_is_maildir(entry)) {
908 for (node = node->children; node != NULL; node = node->next) {
909 FolderItem *cur_item = FOLDER_ITEM(node->data);
910 if (!strcmp2(cur_item->path, entry)) {
916 debug_print("new folder '%s' found.\n", entry);
917 new_item = folder_item_new(folder, utf8name, utf8entry);
918 folder_item_append(item, new_item);
922 if (!folder->inbox &&
923 !strcmp(d->d_name, INBOX_DIR)) {
924 new_item->stype = F_INBOX;
925 folder->inbox = new_item;
926 } else if (!folder->outbox &&
927 !strcmp(d->d_name, OUTBOX_DIR)) {
928 new_item->stype = F_OUTBOX;
929 folder->outbox = new_item;
930 } else if (!folder->draft &&
931 !strcmp(d->d_name, DRAFT_DIR)) {
932 new_item->stype = F_DRAFT;
933 folder->draft = new_item;
934 } else if (!folder->queue &&
935 !strcmp(d->d_name, QUEUE_DIR)) {
936 new_item->stype = F_QUEUE;
937 folder->queue = new_item;
938 } else if (!folder->trash &&
939 !strcmp(d->d_name, TRASH_DIR)) {
940 new_item->stype = F_TRASH;
941 folder->trash = new_item;
945 mh_scan_tree_recursive(new_item);
946 } else if (to_number(d->d_name) != -1) n_msg++;
955 item->mtime = time(NULL);
959 gint new, unread, total, min, max;
961 procmsg_get_mark_sum(item->path, &new, &unread, &total,
964 new += n_msg - total;
965 unread += n_msg - total;
968 item->unread = unread;
974 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
976 FolderItem *item = node->data;
977 gchar **paths = data;
978 const gchar *oldpath = paths[0];
979 const gchar *newpath = paths[1];
984 oldpathlen = strlen(oldpath);
985 if (strncmp(oldpath, item->path, oldpathlen) != 0) {
986 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
990 base = item->path + oldpathlen;
991 while (*base == G_DIR_SEPARATOR) base++;
993 new_itempath = g_strdup(newpath);
995 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
998 item->path = new_itempath;
1003 static gchar *mh_filename_from_utf8(const gchar *path)
1005 gchar *real_path = g_filename_from_utf8(path, -1, NULL, NULL, NULL);
1008 g_warning("mh_filename_from_utf8: faild to convert character set\n");
1009 real_path = g_strdup(path);
1015 static gchar *mh_filename_to_utf8(const gchar *path)
1017 gchar *utf8path = g_filename_to_utf8(path, -1, NULL, NULL, NULL);
1019 g_warning("mh_filename_to_utf8: faild to convert character set\n");
1020 utf8path = g_strdup(path);