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.
36 # include <sys/time.h>
43 #include "procheader.h"
46 static void mh_folder_init (Folder *folder,
50 static Folder *mh_folder_new (const gchar *name,
52 static void mh_folder_destroy (Folder *folder);
53 static gchar *mh_fetch_msg (Folder *folder,
56 static MsgInfo *mh_get_msginfo (Folder *folder,
59 static gint mh_add_msg (Folder *folder,
63 static gint mh_add_msgs (Folder *folder,
67 static gint mh_copy_msg (Folder *folder,
70 static gint mh_remove_msg (Folder *folder,
73 static gint mh_remove_all_msg (Folder *folder,
75 static gboolean mh_is_msg_changed (Folder *folder,
79 static gint mh_get_num_list (Folder *folder,
82 gboolean *old_uids_valid);
83 static gint mh_scan_tree (Folder *folder);
85 static gint mh_create_tree (Folder *folder);
86 static FolderItem *mh_create_folder (Folder *folder,
89 static gint mh_rename_folder (Folder *folder,
92 static gint mh_remove_folder (Folder *folder,
95 static gchar *mh_get_new_msg_filename (FolderItem *dest);
97 static MsgInfo *mh_parse_msg (const gchar *file,
99 static void mh_remove_missing_folder_items (Folder *folder);
100 static void mh_scan_tree_recursive (FolderItem *item);
102 static gboolean mh_rename_folder_func (GNode *node,
104 static gchar *mh_item_get_path (Folder *folder,
107 static gboolean mh_scan_required (Folder *folder,
110 static FolderClass mh_class;
112 FolderClass *mh_get_class(void)
114 if (mh_class.idstr == NULL) {
115 mh_class.type = F_MH;
116 mh_class.idstr = "mh";
117 mh_class.uistr = "MH";
119 /* Folder functions */
120 mh_class.new_folder = mh_folder_new;
121 mh_class.destroy_folder = mh_folder_destroy;
122 mh_class.set_xml = folder_local_set_xml;
123 mh_class.get_xml = folder_local_get_xml;
124 mh_class.scan_tree = mh_scan_tree;
125 mh_class.create_tree = mh_create_tree;
127 /* FolderItem functions */
128 mh_class.item_get_path = mh_item_get_path;
129 mh_class.create_folder = mh_create_folder;
130 mh_class.rename_folder = mh_rename_folder;
131 mh_class.remove_folder = mh_remove_folder;
132 mh_class.get_num_list = mh_get_num_list;
133 mh_class.scan_required = mh_scan_required;
135 /* Message functions */
136 mh_class.get_msginfo = mh_get_msginfo;
137 mh_class.fetch_msg = mh_fetch_msg;
138 mh_class.add_msg = mh_add_msg;
139 mh_class.add_msgs = mh_add_msgs;
140 mh_class.copy_msg = mh_copy_msg;
141 mh_class.remove_msg = mh_remove_msg;
142 mh_class.remove_all_msg = mh_remove_all_msg;
143 mh_class.is_msg_changed = mh_is_msg_changed;
149 static Folder *mh_folder_new(const gchar *name, const gchar *path)
153 folder = (Folder *)g_new0(MHFolder, 1);
154 folder->klass = &mh_class;
155 mh_folder_init(folder, name, path);
160 static void mh_folder_destroy(Folder *folder)
162 folder_local_folder_destroy(LOCAL_FOLDER(folder));
165 static void mh_folder_init(Folder *folder, const gchar *name, const gchar *path)
167 folder_local_folder_init(folder, name, path);
171 gboolean mh_scan_required(Folder *folder, FolderItem *item)
176 path = folder_item_get_path(item);
177 g_return_val_if_fail(path != NULL, FALSE);
179 if (stat(path, &s) < 0) {
180 FILE_OP_ERROR(path, "stat");
185 if (s.st_mtime > item->mtime) {
186 debug_print("MH scan required, folder updated: %s (%ld > %ld)\n",
194 debug_print("MH scan not required: %s (%ld <= %ld)\n",
202 void mh_get_last_num(Folder *folder, FolderItem *item)
210 g_return_if_fail(item != NULL);
212 debug_print("mh_get_last_num(): Scanning %s ...\n", item->path);
214 path = folder_item_get_path(item);
215 g_return_if_fail(path != NULL);
216 if (change_dir(path) < 0) {
222 if ((dp = opendir(".")) == NULL) {
223 FILE_OP_ERROR(item->path, "opendir");
227 while ((d = readdir(dp)) != NULL) {
228 if ((num = to_number(d->d_name)) > 0 &&
229 dirent_is_regular_file(d)) {
236 debug_print("Last number in dir %s = %d\n", item->path, max);
237 item->last_num = max;
240 gint mh_get_num_list(Folder *folder, FolderItem *item, GSList **list, gboolean *old_uids_valid)
246 gint num, nummsgs = 0;
248 g_return_val_if_fail(item != NULL, -1);
250 debug_print("mh_get_num_list(): Scanning %s ...\n", item->path);
252 *old_uids_valid = TRUE;
254 path = folder_item_get_path(item);
255 g_return_val_if_fail(path != NULL, -1);
256 if (change_dir(path) < 0) {
262 if ((dp = opendir(".")) == NULL) {
263 FILE_OP_ERROR(item->path, "opendir");
267 while ((d = readdir(dp)) != NULL) {
268 if ((num = to_number(d->d_name)) > 0) {
269 *list = g_slist_prepend(*list, GINT_TO_POINTER(num));
275 item->mtime = time(NULL);
279 static gchar *mh_fetch_msg(Folder *folder, FolderItem *item, gint num)
284 g_return_val_if_fail(item != NULL, NULL);
285 g_return_val_if_fail(num > 0, NULL);
287 path = folder_item_get_path(item);
288 file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
290 if (!is_file_exist(file)) {
298 static MsgInfo *mh_get_msginfo(Folder *folder, FolderItem *item, gint num)
303 g_return_val_if_fail(item != NULL, NULL);
304 g_return_val_if_fail(num > 0, NULL);
306 file = mh_fetch_msg(folder, item, num);
307 if (!file) return NULL;
309 msginfo = mh_parse_msg(file, item);
311 msginfo->msgnum = num;
318 static gchar *mh_get_new_msg_filename(FolderItem *dest)
323 destpath = folder_item_get_path(dest);
324 g_return_val_if_fail(destpath != NULL, NULL);
326 if (!is_dir_exist(destpath))
327 make_dir_hier(destpath);
330 destfile = g_strdup_printf("%s%c%d", destpath, G_DIR_SEPARATOR,
332 if (is_file_entry_exist(destfile)) {
344 static gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file, MsgFlags *flags)
348 MsgFileInfo fileinfo;
350 g_return_val_if_fail(file != NULL, -1);
352 fileinfo.msginfo = NULL;
353 fileinfo.file = (gchar *)file;
354 fileinfo.flags = flags;
355 file_list.data = &fileinfo;
356 file_list.next = NULL;
358 ret = mh_add_msgs(folder, dest, &file_list, NULL);
362 static gint mh_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
367 MsgFileInfo *fileinfo;
369 g_return_val_if_fail(dest != NULL, -1);
370 g_return_val_if_fail(file_list != NULL, -1);
372 if (dest->last_num < 0) {
373 mh_get_last_num(folder, dest);
374 if (dest->last_num < 0) return -1;
377 for (cur = file_list; cur != NULL; cur = cur->next) {
378 fileinfo = (MsgFileInfo *)cur->data;
380 destfile = mh_get_new_msg_filename(dest);
381 if (destfile == NULL) return -1;
383 if (link(fileinfo->file, destfile) < 0) {
384 if (copy_file(fileinfo->file, destfile, TRUE) < 0) {
385 g_warning(_("can't copy message %s to %s\n"),
386 fileinfo->file, destfile);
391 if (relation != NULL)
392 g_relation_insert(relation, fileinfo, GINT_TO_POINTER(dest->last_num + 1));
397 return dest->last_num;
400 static gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
402 gboolean dest_need_scan = FALSE;
406 FolderItemPrefs *prefs;
408 g_return_val_if_fail(dest != NULL, -1);
409 g_return_val_if_fail(msginfo != NULL, -1);
411 if (msginfo->folder == dest) {
412 g_warning("the src folder is identical to the dest.\n");
416 if (dest->last_num < 0) {
417 mh_get_last_num(folder, dest);
418 if (dest->last_num < 0) return -1;
423 srcfile = procmsg_get_message_file(msginfo);
424 destfile = mh_get_new_msg_filename(dest);
430 debug_print("Copying message %s%c%d to %s ...\n",
431 msginfo->folder->path, G_DIR_SEPARATOR,
432 msginfo->msgnum, dest->path);
435 if ((MSG_IS_QUEUED(msginfo->flags) || MSG_IS_DRAFT(msginfo->flags))
436 && dest->stype != F_QUEUE && dest->stype != F_DRAFT) {
437 if (procmsg_remove_special_headers(srcfile, destfile) !=0) {
442 } else if (copy_file(srcfile, destfile, TRUE) < 0) {
443 FILE_OP_ERROR(srcfile, "copy");
449 dest_need_scan = mh_scan_required(dest->folder, dest);
451 dest->mtime = time(NULL);
454 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
455 if (chmod(destfile, prefs->folder_chmod) < 0)
456 FILE_OP_ERROR(destfile, "chmod");
459 filemode = prefs->folder_chmod;
460 if (filemode & S_IRGRP) filemode |= S_IWGRP;
461 if (filemode & S_IROTH) filemode |= S_IWOTH;
468 return dest->last_num;
471 static gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
473 gboolean need_scan = FALSE;
476 g_return_val_if_fail(item != NULL, -1);
478 file = mh_fetch_msg(folder, item, num);
479 g_return_val_if_fail(file != NULL, -1);
481 need_scan = mh_scan_required(folder, item);
483 if (unlink(file) < 0) {
484 FILE_OP_ERROR(file, "unlink");
490 item->mtime = time(NULL);
496 static gint mh_remove_all_msg(Folder *folder, FolderItem *item)
501 g_return_val_if_fail(item != NULL, -1);
503 path = folder_item_get_path(item);
504 g_return_val_if_fail(path != NULL, -1);
505 val = remove_all_numbered_files(path);
511 static gboolean mh_is_msg_changed(Folder *folder, FolderItem *item,
516 if (stat(itos(msginfo->msgnum), &s) < 0 ||
517 msginfo->size != s.st_size ||
518 msginfo->mtime != s.st_mtime)
524 static gint mh_scan_tree(Folder *folder)
529 g_return_val_if_fail(folder != NULL, -1);
532 item = folder_item_new(folder, folder->name, NULL);
533 item->folder = folder;
534 folder->node = item->node = g_node_new(item);
536 item = FOLDER_ITEM(folder->node->data);
538 rootpath = folder_item_get_path(item);
539 if (change_dir(rootpath) < 0) {
545 mh_create_tree(folder);
546 mh_remove_missing_folder_items(folder);
547 mh_scan_tree_recursive(item);
552 #define MAKE_DIR_IF_NOT_EXIST(dir) \
554 if (!is_dir_exist(dir)) { \
555 if (is_file_exist(dir)) { \
556 g_warning("File `%s' already exists.\n" \
557 "Can't create folder.", dir); \
560 if (make_dir(dir) < 0) \
565 static gint mh_create_tree(Folder *folder)
569 g_return_val_if_fail(folder != NULL, -1);
571 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), -1);
572 rootpath = LOCAL_FOLDER(folder)->rootpath;
573 MAKE_DIR_IF_NOT_EXIST(rootpath);
574 CHDIR_RETURN_VAL_IF_FAIL(rootpath, -1);
575 MAKE_DIR_IF_NOT_EXIST(INBOX_DIR);
576 MAKE_DIR_IF_NOT_EXIST(OUTBOX_DIR);
577 MAKE_DIR_IF_NOT_EXIST(QUEUE_DIR);
578 MAKE_DIR_IF_NOT_EXIST(DRAFT_DIR);
579 MAKE_DIR_IF_NOT_EXIST(TRASH_DIR);
584 #undef MAKE_DIR_IF_NOT_EXIST
586 static gchar *mh_item_get_path(Folder *folder, FolderItem *item)
588 gchar *folder_path, *path;
590 g_return_val_if_fail(folder != NULL, NULL);
591 g_return_val_if_fail(item != NULL, NULL);
593 folder_path = g_strdup(LOCAL_FOLDER(folder)->rootpath);
594 g_return_val_if_fail(folder_path != NULL, NULL);
596 if (folder_path[0] == G_DIR_SEPARATOR) {
598 path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
601 path = g_strdup(folder_path);
604 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
605 folder_path, G_DIR_SEPARATOR_S,
608 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
616 static FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
621 FolderItem *new_item;
622 gchar *mh_sequences_filename;
623 FILE *mh_sequences_file;
625 g_return_val_if_fail(folder != NULL, NULL);
626 g_return_val_if_fail(parent != NULL, NULL);
627 g_return_val_if_fail(name != NULL, NULL);
629 path = folder_item_get_path(parent);
630 if (!is_dir_exist(path))
631 if (make_dir_hier(path) != 0)
634 fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, name, NULL);
637 if (make_dir(fullpath) < 0) {
645 path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
648 path = g_strdup(name);
649 new_item = folder_item_new(folder, name, path);
650 folder_item_append(parent, new_item);
654 path = folder_item_get_path(new_item);
655 mh_sequences_filename = g_strconcat(path, G_DIR_SEPARATOR_S,
656 ".mh_sequences", NULL);
657 if ((mh_sequences_file = fopen(mh_sequences_filename, "a+b")) != NULL) {
658 fclose(mh_sequences_file);
660 g_free(mh_sequences_filename);
666 static gint mh_rename_folder(Folder *folder, FolderItem *item,
674 g_return_val_if_fail(folder != NULL, -1);
675 g_return_val_if_fail(item != NULL, -1);
676 g_return_val_if_fail(item->path != NULL, -1);
677 g_return_val_if_fail(name != NULL, -1);
679 oldpath = folder_item_get_path(item);
680 if (!is_dir_exist(oldpath))
681 make_dir_hier(oldpath);
683 dirname = g_dirname(oldpath);
684 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
687 if (rename(oldpath, newpath) < 0) {
688 FILE_OP_ERROR(oldpath, "rename");
697 if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
698 dirname = g_dirname(item->path);
699 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
702 newpath = g_strdup(name);
705 item->name = g_strdup(name);
707 paths[0] = g_strdup(item->path);
709 g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
710 mh_rename_folder_func, paths);
717 static gint mh_remove_folder(Folder *folder, FolderItem *item)
721 g_return_val_if_fail(folder != NULL, -1);
722 g_return_val_if_fail(item != NULL, -1);
723 g_return_val_if_fail(item->path != NULL, -1);
725 path = folder_item_get_path(item);
726 if (remove_dir_recursive(path) < 0) {
727 g_warning("can't remove directory `%s'\n", path);
733 folder_item_remove(item);
737 static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
742 g_return_val_if_fail(item != NULL, NULL);
743 g_return_val_if_fail(file != NULL, NULL);
745 flags.perm_flags = MSG_NEW|MSG_UNREAD;
748 if (item->stype == F_QUEUE) {
749 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
750 } else if (item->stype == F_DRAFT) {
751 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
754 msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
755 if (!msginfo) return NULL;
757 msginfo->msgnum = atoi(file);
758 msginfo->folder = item;
764 static gboolean mh_is_maildir_one(const gchar *path, const gchar *dir)
769 entry = g_strconcat(path, G_DIR_SEPARATOR_S, dir, NULL);
770 result = is_dir_exist(entry);
777 * check whether PATH is a Maildir style mailbox.
778 * This is the case if the 3 subdir: new, cur, tmp are existing.
779 * This functon assumes that entry is an directory
781 static gboolean mh_is_maildir(const gchar *path)
783 return mh_is_maildir_one(path, "new") &&
784 mh_is_maildir_one(path, "cur") &&
785 mh_is_maildir_one(path, "tmp");
789 static gboolean mh_remove_missing_folder_items_func(GNode *node, gpointer data)
794 g_return_val_if_fail(node->data != NULL, FALSE);
796 if (G_NODE_IS_ROOT(node))
799 item = FOLDER_ITEM(node->data);
801 path = folder_item_get_path(item);
802 if (!is_dir_exist(path)) {
803 debug_print("folder '%s' not found. removing...\n", path);
804 folder_item_remove(item);
811 static void mh_remove_missing_folder_items(Folder *folder)
813 g_return_if_fail(folder != NULL);
815 debug_print("searching missing folders...\n");
817 g_node_traverse(folder->node, G_POST_ORDER, G_TRAVERSE_ALL, -1,
818 mh_remove_missing_folder_items_func, folder);
821 static void mh_scan_tree_recursive(FolderItem *item)
830 g_return_if_fail(item != NULL);
831 g_return_if_fail(item->folder != NULL);
833 folder = item->folder;
835 dp = opendir(item->path ? item->path : ".");
837 FILE_OP_ERROR(item->path ? item->path : ".", "opendir");
841 debug_print("scanning %s ...\n",
842 item->path ? item->path
843 : LOCAL_FOLDER(item->folder)->rootpath);
845 folder->ui_func(folder, item, folder->ui_func_data);
847 while ((d = readdir(dp)) != NULL) {
848 if (d->d_name[0] == '.') continue;
851 entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
854 entry = g_strdup(d->d_name);
857 #ifdef HAVE_DIRENT_D_TYPE
858 d->d_type == DT_DIR ||
859 (d->d_type == DT_UNKNOWN &&
861 stat(entry, &s) == 0 && S_ISDIR(s.st_mode)
862 #ifdef HAVE_DIRENT_D_TYPE
866 FolderItem *new_item = NULL;
870 if (mh_is_maildir(entry)) {
877 for (node = node->children; node != NULL; node = node->next) {
878 FolderItem *cur_item = FOLDER_ITEM(node->data);
879 if (!strcmp2(cur_item->path, entry)) {
885 debug_print("new folder '%s' found.\n", entry);
886 new_item = folder_item_new(folder, d->d_name, entry);
887 folder_item_append(item, new_item);
891 if (!folder->inbox &&
892 !strcmp(d->d_name, INBOX_DIR)) {
893 new_item->stype = F_INBOX;
894 folder->inbox = new_item;
895 } else if (!folder->outbox &&
896 !strcmp(d->d_name, OUTBOX_DIR)) {
897 new_item->stype = F_OUTBOX;
898 folder->outbox = new_item;
899 } else if (!folder->draft &&
900 !strcmp(d->d_name, DRAFT_DIR)) {
901 new_item->stype = F_DRAFT;
902 folder->draft = new_item;
903 } else if (!folder->queue &&
904 !strcmp(d->d_name, QUEUE_DIR)) {
905 new_item->stype = F_QUEUE;
906 folder->queue = new_item;
907 } else if (!folder->trash &&
908 !strcmp(d->d_name, TRASH_DIR)) {
909 new_item->stype = F_TRASH;
910 folder->trash = new_item;
914 mh_scan_tree_recursive(new_item);
915 } else if (to_number(d->d_name) != -1) n_msg++;
922 item->mtime = time(NULL);
926 gint new, unread, total, min, max;
928 procmsg_get_mark_sum(item->path, &new, &unread, &total,
931 new += n_msg - total;
932 unread += n_msg - total;
935 item->unread = unread;
941 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
943 FolderItem *item = node->data;
944 gchar **paths = data;
945 const gchar *oldpath = paths[0];
946 const gchar *newpath = paths[1];
951 oldpathlen = strlen(oldpath);
952 if (strncmp(oldpath, item->path, oldpathlen) != 0) {
953 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
957 base = item->path + oldpathlen;
958 while (*base == G_DIR_SEPARATOR) base++;
960 new_itempath = g_strdup(newpath);
962 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
965 item->path = new_itempath;