2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2003 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 FolderClass mh_class =
113 /* Folder functions */
119 /* FolderItem functions */
135 /* Message functions */
149 FolderClass *mh_get_class(void)
154 static Folder *mh_folder_new(const gchar *name, const gchar *path)
158 folder = (Folder *)g_new0(MHFolder, 1);
159 folder->klass = &mh_class;
160 mh_folder_init(folder, name, path);
165 static void mh_folder_destroy(Folder *folder)
167 folder_local_folder_destroy(LOCAL_FOLDER(folder));
170 static void mh_folder_init(Folder *folder, const gchar *name, const gchar *path)
172 folder_local_folder_init(folder, name, path);
176 void mh_get_last_num(Folder *folder, FolderItem *item)
184 g_return_if_fail(item != NULL);
186 debug_print("mh_get_last_num(): Scanning %s ...\n", item->path);
188 path = folder_item_get_path(item);
189 g_return_if_fail(path != NULL);
190 if (change_dir(path) < 0) {
196 if ((dp = opendir(".")) == NULL) {
197 FILE_OP_ERROR(item->path, "opendir");
201 while ((d = readdir(dp)) != NULL) {
202 if ((num = to_number(d->d_name)) >= 0 &&
203 dirent_is_regular_file(d)) {
210 debug_print("Last number in dir %s = %d\n", item->path, max);
211 item->last_num = max;
214 gint mh_get_num_list(Folder *folder, FolderItem *item, GSList **list, gboolean *old_uids_valid)
220 gint num, nummsgs = 0;
222 g_return_val_if_fail(item != NULL, -1);
224 debug_print("mh_get_last_num(): Scanning %s ...\n", item->path);
226 *old_uids_valid = TRUE;
228 path = folder_item_get_path(item);
229 g_return_val_if_fail(path != NULL, -1);
230 if (change_dir(path) < 0) {
236 if ((dp = opendir(".")) == NULL) {
237 FILE_OP_ERROR(item->path, "opendir");
241 while ((d = readdir(dp)) != NULL) {
242 if ((num = to_number(d->d_name)) >= 0) {
243 *list = g_slist_prepend(*list, GINT_TO_POINTER(num));
252 static gchar *mh_fetch_msg(Folder *folder, FolderItem *item, gint num)
257 g_return_val_if_fail(item != NULL, NULL);
258 g_return_val_if_fail(num > 0, NULL);
260 path = folder_item_get_path(item);
261 file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
263 if (!is_file_exist(file)) {
271 static MsgInfo *mh_get_msginfo(Folder *folder, FolderItem *item, gint num)
276 g_return_val_if_fail(item != NULL, NULL);
277 g_return_val_if_fail(num > 0, NULL);
279 file = mh_fetch_msg(folder, item, num);
280 if (!file) return NULL;
282 msginfo = mh_parse_msg(file, item);
284 msginfo->msgnum = num;
291 static gchar *mh_get_new_msg_filename(FolderItem *dest)
296 destpath = folder_item_get_path(dest);
297 g_return_val_if_fail(destpath != NULL, NULL);
299 if (!is_dir_exist(destpath))
300 make_dir_hier(destpath);
303 destfile = g_strdup_printf("%s%c%d", destpath, G_DIR_SEPARATOR,
305 if (is_file_entry_exist(destfile)) {
317 static gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file, MsgFlags *flags)
321 MsgFileInfo fileinfo;
323 g_return_val_if_fail(file != NULL, -1);
325 fileinfo.msginfo = NULL;
326 fileinfo.file = (gchar *)file;
327 fileinfo.flags = flags;
328 file_list.data = &fileinfo;
329 file_list.next = NULL;
331 ret = mh_add_msgs(folder, dest, &file_list, NULL);
335 static gint mh_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
340 MsgFileInfo *fileinfo;
342 g_return_val_if_fail(dest != NULL, -1);
343 g_return_val_if_fail(file_list != NULL, -1);
345 if (dest->last_num < 0) {
346 mh_get_last_num(folder, dest);
347 if (dest->last_num < 0) return -1;
350 for (cur = file_list; cur != NULL; cur = cur->next) {
351 fileinfo = (MsgFileInfo *)cur->data;
353 destfile = mh_get_new_msg_filename(dest);
354 if (destfile == NULL) return -1;
356 if (link(fileinfo->file, destfile) < 0) {
357 if (copy_file(fileinfo->file, destfile, TRUE) < 0) {
358 g_warning(_("can't copy message %s to %s\n"),
359 fileinfo->file, destfile);
364 if (relation != NULL)
365 g_relation_insert(relation, fileinfo, GINT_TO_POINTER(dest->last_num + 1));
370 return dest->last_num;
373 static gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
378 FolderItemPrefs *prefs;
380 g_return_val_if_fail(dest != NULL, -1);
381 g_return_val_if_fail(msginfo != NULL, -1);
383 if (msginfo->folder == dest) {
384 g_warning("the src folder is identical to the dest.\n");
388 if (dest->last_num < 0) {
389 mh_get_last_num(folder, dest);
390 if (dest->last_num < 0) return -1;
395 srcfile = procmsg_get_message_file(msginfo);
396 destfile = mh_get_new_msg_filename(dest);
402 debug_print("Copying message %s%c%d to %s ...\n",
403 msginfo->folder->path, G_DIR_SEPARATOR,
404 msginfo->msgnum, dest->path);
407 if ((MSG_IS_QUEUED(msginfo->flags) || MSG_IS_DRAFT(msginfo->flags))
408 && dest->stype != F_QUEUE && dest->stype != F_DRAFT) {
409 if (procmsg_remove_special_headers(srcfile, destfile) !=0) {
414 } else if (copy_file(srcfile, destfile, TRUE) < 0) {
415 FILE_OP_ERROR(srcfile, "copy");
422 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
423 if (chmod(destfile, prefs->folder_chmod) < 0)
424 FILE_OP_ERROR(destfile, "chmod");
427 filemode = prefs->folder_chmod;
428 if (filemode & S_IRGRP) filemode |= S_IWGRP;
429 if (filemode & S_IROTH) filemode |= S_IWOTH;
436 return dest->last_num;
439 static gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
443 g_return_val_if_fail(item != NULL, -1);
445 file = mh_fetch_msg(folder, item, num);
446 g_return_val_if_fail(file != NULL, -1);
448 if (unlink(file) < 0) {
449 FILE_OP_ERROR(file, "unlink");
458 static gint mh_remove_all_msg(Folder *folder, FolderItem *item)
463 g_return_val_if_fail(item != NULL, -1);
465 path = folder_item_get_path(item);
466 g_return_val_if_fail(path != NULL, -1);
467 val = remove_all_numbered_files(path);
473 static gboolean mh_is_msg_changed(Folder *folder, FolderItem *item,
478 if (stat(itos(msginfo->msgnum), &s) < 0 ||
479 msginfo->size != s.st_size ||
480 msginfo->mtime != s.st_mtime)
486 static gint mh_scan_tree(Folder *folder)
491 g_return_val_if_fail(folder != NULL, -1);
494 item = folder_item_new(folder, folder->name, NULL);
495 item->folder = folder;
496 folder->node = item->node = g_node_new(item);
498 item = FOLDER_ITEM(folder->node->data);
500 rootpath = folder_item_get_path(item);
501 if (change_dir(rootpath) < 0) {
507 mh_create_tree(folder);
508 mh_remove_missing_folder_items(folder);
509 mh_scan_tree_recursive(item);
514 #define MAKE_DIR_IF_NOT_EXIST(dir) \
516 if (!is_dir_exist(dir)) { \
517 if (is_file_exist(dir)) { \
518 g_warning("File `%s' already exists.\n" \
519 "Can't create folder.", dir); \
522 if (make_dir(dir) < 0) \
527 static gint mh_create_tree(Folder *folder)
531 g_return_val_if_fail(folder != NULL, -1);
533 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), -1);
534 rootpath = LOCAL_FOLDER(folder)->rootpath;
535 MAKE_DIR_IF_NOT_EXIST(rootpath);
536 CHDIR_RETURN_VAL_IF_FAIL(rootpath, -1);
537 MAKE_DIR_IF_NOT_EXIST(INBOX_DIR);
538 MAKE_DIR_IF_NOT_EXIST(OUTBOX_DIR);
539 MAKE_DIR_IF_NOT_EXIST(QUEUE_DIR);
540 MAKE_DIR_IF_NOT_EXIST(DRAFT_DIR);
541 MAKE_DIR_IF_NOT_EXIST(TRASH_DIR);
546 #undef MAKE_DIR_IF_NOT_EXIST
548 static gchar *mh_item_get_path(Folder *folder, FolderItem *item)
550 gchar *folder_path, *path;
552 g_return_val_if_fail(folder != NULL, NULL);
553 g_return_val_if_fail(item != NULL, NULL);
555 folder_path = g_strdup(LOCAL_FOLDER(folder)->rootpath);
556 g_return_val_if_fail(folder_path != NULL, NULL);
558 if (folder_path[0] == G_DIR_SEPARATOR) {
560 path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
563 path = g_strdup(folder_path);
566 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
567 folder_path, G_DIR_SEPARATOR_S,
570 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
578 static FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
583 FolderItem *new_item;
584 gchar *mh_sequences_filename;
585 FILE *mh_sequences_file;
587 g_return_val_if_fail(folder != NULL, NULL);
588 g_return_val_if_fail(parent != NULL, NULL);
589 g_return_val_if_fail(name != NULL, NULL);
591 path = folder_item_get_path(parent);
592 if (!is_dir_exist(path))
593 if (make_dir_hier(path) != 0)
596 fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, name, NULL);
599 if (make_dir(fullpath) < 0) {
607 path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
610 path = g_strdup(name);
611 new_item = folder_item_new(folder, name, path);
612 folder_item_append(parent, new_item);
616 path = folder_item_get_path(new_item);
617 mh_sequences_filename = g_strconcat(path, G_DIR_SEPARATOR_S,
618 ".mh_sequences", NULL);
619 if ((mh_sequences_file = fopen(mh_sequences_filename, "a+b")) != NULL) {
620 fclose(mh_sequences_file);
622 g_free(mh_sequences_filename);
628 static gint mh_rename_folder(Folder *folder, FolderItem *item,
636 g_return_val_if_fail(folder != NULL, -1);
637 g_return_val_if_fail(item != NULL, -1);
638 g_return_val_if_fail(item->path != NULL, -1);
639 g_return_val_if_fail(name != NULL, -1);
641 oldpath = folder_item_get_path(item);
642 if (!is_dir_exist(oldpath))
643 make_dir_hier(oldpath);
645 dirname = g_dirname(oldpath);
646 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
649 if (rename(oldpath, newpath) < 0) {
650 FILE_OP_ERROR(oldpath, "rename");
659 if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
660 dirname = g_dirname(item->path);
661 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
664 newpath = g_strdup(name);
667 item->name = g_strdup(name);
669 paths[0] = g_strdup(item->path);
671 g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
672 mh_rename_folder_func, paths);
679 static gint mh_remove_folder(Folder *folder, FolderItem *item)
683 g_return_val_if_fail(folder != NULL, -1);
684 g_return_val_if_fail(item != NULL, -1);
685 g_return_val_if_fail(item->path != NULL, -1);
687 path = folder_item_get_path(item);
688 if (remove_dir_recursive(path) < 0) {
689 g_warning("can't remove directory `%s'\n", path);
695 folder_item_remove(item);
699 static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
705 g_return_val_if_fail(item != NULL, NULL);
706 g_return_val_if_fail(file != NULL, NULL);
708 flags.perm_flags = MSG_NEW|MSG_UNREAD;
711 if (item->stype == F_QUEUE) {
712 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
713 } else if (item->stype == F_DRAFT) {
714 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
717 if (stat(file, &s) < 0) {
718 FILE_OP_ERROR(file, "stat");
721 if (!S_ISREG(s.st_mode))
724 msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
725 if (!msginfo) return NULL;
727 msginfo->msgnum = atoi(file);
728 msginfo->folder = item;
729 msginfo->size = s.st_size;
730 msginfo->mtime = s.st_mtime;
736 static gboolean mh_is_maildir_one(const gchar *path, const gchar *dir)
741 entry = g_strconcat(path, G_DIR_SEPARATOR_S, dir, NULL);
742 result = is_dir_exist(entry);
749 * check whether PATH is a Maildir style mailbox.
750 * This is the case if the 3 subdir: new, cur, tmp are existing.
751 * This functon assumes that entry is an directory
753 static gboolean mh_is_maildir(const gchar *path)
755 return mh_is_maildir_one(path, "new") &&
756 mh_is_maildir_one(path, "cur") &&
757 mh_is_maildir_one(path, "tmp");
761 static gboolean mh_remove_missing_folder_items_func(GNode *node, gpointer data)
766 g_return_val_if_fail(node->data != NULL, FALSE);
768 if (G_NODE_IS_ROOT(node))
771 item = FOLDER_ITEM(node->data);
773 path = folder_item_get_path(item);
774 if (!is_dir_exist(path)) {
775 debug_print("folder '%s' not found. removing...\n", path);
776 folder_item_remove(item);
783 static void mh_remove_missing_folder_items(Folder *folder)
785 g_return_if_fail(folder != NULL);
787 debug_print("searching missing folders...\n");
789 g_node_traverse(folder->node, G_POST_ORDER, G_TRAVERSE_ALL, -1,
790 mh_remove_missing_folder_items_func, folder);
793 static void mh_scan_tree_recursive(FolderItem *item)
802 g_return_if_fail(item != NULL);
803 g_return_if_fail(item->folder != NULL);
805 folder = item->folder;
807 dp = opendir(item->path ? item->path : ".");
809 FILE_OP_ERROR(item->path ? item->path : ".", "opendir");
813 debug_print("scanning %s ...\n",
814 item->path ? item->path
815 : LOCAL_FOLDER(item->folder)->rootpath);
817 folder->ui_func(folder, item, folder->ui_func_data);
819 while ((d = readdir(dp)) != NULL) {
820 if (d->d_name[0] == '.') continue;
823 entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
826 entry = g_strdup(d->d_name);
829 #ifdef HAVE_DIRENT_D_TYPE
830 d->d_type == DT_DIR ||
831 (d->d_type == DT_UNKNOWN &&
833 stat(entry, &s) == 0 && S_ISDIR(s.st_mode)
834 #ifdef HAVE_DIRENT_D_TYPE
838 FolderItem *new_item = NULL;
842 if (mh_is_maildir(entry)) {
849 for (node = node->children; node != NULL; node = node->next) {
850 FolderItem *cur_item = FOLDER_ITEM(node->data);
851 if (!strcmp2(cur_item->path, entry)) {
857 debug_print("new folder '%s' found.\n", entry);
858 new_item = folder_item_new(folder, d->d_name, entry);
859 folder_item_append(item, new_item);
863 if (!folder->inbox &&
864 !strcmp(d->d_name, INBOX_DIR)) {
865 new_item->stype = F_INBOX;
866 folder->inbox = new_item;
867 } else if (!folder->outbox &&
868 !strcmp(d->d_name, OUTBOX_DIR)) {
869 new_item->stype = F_OUTBOX;
870 folder->outbox = new_item;
871 } else if (!folder->draft &&
872 !strcmp(d->d_name, DRAFT_DIR)) {
873 new_item->stype = F_DRAFT;
874 folder->draft = new_item;
875 } else if (!folder->queue &&
876 !strcmp(d->d_name, QUEUE_DIR)) {
877 new_item->stype = F_QUEUE;
878 folder->queue = new_item;
879 } else if (!folder->trash &&
880 !strcmp(d->d_name, TRASH_DIR)) {
881 new_item->stype = F_TRASH;
882 folder->trash = new_item;
886 mh_scan_tree_recursive(new_item);
887 } else if (to_number(d->d_name) != -1) n_msg++;
896 gint new, unread, total, min, max;
898 procmsg_get_mark_sum(item->path, &new, &unread, &total,
901 new += n_msg - total;
902 unread += n_msg - total;
905 item->unread = unread;
911 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
913 FolderItem *item = node->data;
914 gchar **paths = data;
915 const gchar *oldpath = paths[0];
916 const gchar *newpath = paths[1];
921 oldpathlen = strlen(oldpath);
922 if (strncmp(oldpath, item->path, oldpathlen) != 0) {
923 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
927 base = item->path + oldpathlen;
928 while (*base == G_DIR_SEPARATOR) base++;
930 new_itempath = g_strdup(newpath);
932 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
935 item->path = new_itempath;