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;
109 FolderClass *mh_get_class(void)
111 if (mh_class.idstr == NULL) {
112 mh_class.type = F_MH;
113 mh_class.idstr = "mh";
114 mh_class.uistr = "MH";
116 /* Folder functions */
117 mh_class.new_folder = mh_folder_new;
118 mh_class.destroy_folder = mh_folder_destroy;
119 mh_class.set_xml = folder_local_set_xml;
120 mh_class.get_xml = folder_local_get_xml;
121 mh_class.scan_tree = mh_scan_tree;
122 mh_class.create_tree = mh_create_tree;
124 /* FolderItem functions */
125 mh_class.item_get_path = mh_item_get_path;
126 mh_class.create_folder = mh_create_folder;
127 mh_class.rename_folder = mh_rename_folder;
128 mh_class.remove_folder = mh_remove_folder;
129 mh_class.get_num_list = mh_get_num_list;
131 /* Message functions */
132 mh_class.get_msginfo = mh_get_msginfo;
133 mh_class.fetch_msg = mh_fetch_msg;
134 mh_class.add_msg = mh_add_msg;
135 mh_class.add_msgs = mh_add_msgs;
136 mh_class.copy_msg = mh_copy_msg;
137 mh_class.remove_msg = mh_remove_msg;
138 mh_class.remove_all_msg = mh_remove_all_msg;
139 mh_class.is_msg_changed = mh_is_msg_changed;
145 static Folder *mh_folder_new(const gchar *name, const gchar *path)
149 folder = (Folder *)g_new0(MHFolder, 1);
150 folder->klass = &mh_class;
151 mh_folder_init(folder, name, path);
156 static void mh_folder_destroy(Folder *folder)
158 folder_local_folder_destroy(LOCAL_FOLDER(folder));
161 static void mh_folder_init(Folder *folder, const gchar *name, const gchar *path)
163 folder_local_folder_init(folder, name, path);
167 void mh_get_last_num(Folder *folder, FolderItem *item)
175 g_return_if_fail(item != NULL);
177 debug_print("mh_get_last_num(): Scanning %s ...\n", item->path);
179 path = folder_item_get_path(item);
180 g_return_if_fail(path != NULL);
181 if (change_dir(path) < 0) {
187 if ((dp = opendir(".")) == NULL) {
188 FILE_OP_ERROR(item->path, "opendir");
192 while ((d = readdir(dp)) != NULL) {
193 if ((num = to_number(d->d_name)) >= 0 &&
194 dirent_is_regular_file(d)) {
201 debug_print("Last number in dir %s = %d\n", item->path, max);
202 item->last_num = max;
205 gint mh_get_num_list(Folder *folder, FolderItem *item, GSList **list, gboolean *old_uids_valid)
211 gint num, nummsgs = 0;
213 g_return_val_if_fail(item != NULL, -1);
215 debug_print("mh_get_last_num(): Scanning %s ...\n", item->path);
217 *old_uids_valid = TRUE;
219 path = folder_item_get_path(item);
220 g_return_val_if_fail(path != NULL, -1);
221 if (change_dir(path) < 0) {
227 if ((dp = opendir(".")) == NULL) {
228 FILE_OP_ERROR(item->path, "opendir");
232 while ((d = readdir(dp)) != NULL) {
233 if ((num = to_number(d->d_name)) >= 0) {
234 *list = g_slist_prepend(*list, GINT_TO_POINTER(num));
243 static gchar *mh_fetch_msg(Folder *folder, FolderItem *item, gint num)
248 g_return_val_if_fail(item != NULL, NULL);
249 g_return_val_if_fail(num > 0, NULL);
251 path = folder_item_get_path(item);
252 file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
254 if (!is_file_exist(file)) {
262 static MsgInfo *mh_get_msginfo(Folder *folder, FolderItem *item, gint num)
267 g_return_val_if_fail(item != NULL, NULL);
268 g_return_val_if_fail(num > 0, NULL);
270 file = mh_fetch_msg(folder, item, num);
271 if (!file) return NULL;
273 msginfo = mh_parse_msg(file, item);
275 msginfo->msgnum = num;
282 static gchar *mh_get_new_msg_filename(FolderItem *dest)
287 destpath = folder_item_get_path(dest);
288 g_return_val_if_fail(destpath != NULL, NULL);
290 if (!is_dir_exist(destpath))
291 make_dir_hier(destpath);
294 destfile = g_strdup_printf("%s%c%d", destpath, G_DIR_SEPARATOR,
296 if (is_file_entry_exist(destfile)) {
308 static gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file, MsgFlags *flags)
312 MsgFileInfo fileinfo;
314 g_return_val_if_fail(file != NULL, -1);
316 fileinfo.msginfo = NULL;
317 fileinfo.file = (gchar *)file;
318 fileinfo.flags = flags;
319 file_list.data = &fileinfo;
320 file_list.next = NULL;
322 ret = mh_add_msgs(folder, dest, &file_list, NULL);
326 static gint mh_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
331 MsgFileInfo *fileinfo;
333 g_return_val_if_fail(dest != NULL, -1);
334 g_return_val_if_fail(file_list != NULL, -1);
336 if (dest->last_num < 0) {
337 mh_get_last_num(folder, dest);
338 if (dest->last_num < 0) return -1;
341 for (cur = file_list; cur != NULL; cur = cur->next) {
342 fileinfo = (MsgFileInfo *)cur->data;
344 destfile = mh_get_new_msg_filename(dest);
345 if (destfile == NULL) return -1;
347 if (link(fileinfo->file, destfile) < 0) {
348 if (copy_file(fileinfo->file, destfile, TRUE) < 0) {
349 g_warning(_("can't copy message %s to %s\n"),
350 fileinfo->file, destfile);
355 if (relation != NULL)
356 g_relation_insert(relation, fileinfo, GINT_TO_POINTER(dest->last_num + 1));
361 return dest->last_num;
364 static gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
369 FolderItemPrefs *prefs;
371 g_return_val_if_fail(dest != NULL, -1);
372 g_return_val_if_fail(msginfo != NULL, -1);
374 if (msginfo->folder == dest) {
375 g_warning("the src folder is identical to the dest.\n");
379 if (dest->last_num < 0) {
380 mh_get_last_num(folder, dest);
381 if (dest->last_num < 0) return -1;
386 srcfile = procmsg_get_message_file(msginfo);
387 destfile = mh_get_new_msg_filename(dest);
393 debug_print("Copying message %s%c%d to %s ...\n",
394 msginfo->folder->path, G_DIR_SEPARATOR,
395 msginfo->msgnum, dest->path);
398 if ((MSG_IS_QUEUED(msginfo->flags) || MSG_IS_DRAFT(msginfo->flags))
399 && dest->stype != F_QUEUE && dest->stype != F_DRAFT) {
400 if (procmsg_remove_special_headers(srcfile, destfile) !=0) {
405 } else if (copy_file(srcfile, destfile, TRUE) < 0) {
406 FILE_OP_ERROR(srcfile, "copy");
413 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
414 if (chmod(destfile, prefs->folder_chmod) < 0)
415 FILE_OP_ERROR(destfile, "chmod");
418 filemode = prefs->folder_chmod;
419 if (filemode & S_IRGRP) filemode |= S_IWGRP;
420 if (filemode & S_IROTH) filemode |= S_IWOTH;
427 return dest->last_num;
430 static gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
434 g_return_val_if_fail(item != NULL, -1);
436 file = mh_fetch_msg(folder, item, num);
437 g_return_val_if_fail(file != NULL, -1);
439 if (unlink(file) < 0) {
440 FILE_OP_ERROR(file, "unlink");
449 static gint mh_remove_all_msg(Folder *folder, FolderItem *item)
454 g_return_val_if_fail(item != NULL, -1);
456 path = folder_item_get_path(item);
457 g_return_val_if_fail(path != NULL, -1);
458 val = remove_all_numbered_files(path);
464 static gboolean mh_is_msg_changed(Folder *folder, FolderItem *item,
469 if (stat(itos(msginfo->msgnum), &s) < 0 ||
470 msginfo->size != s.st_size ||
471 msginfo->mtime != s.st_mtime)
477 static gint mh_scan_tree(Folder *folder)
482 g_return_val_if_fail(folder != NULL, -1);
485 item = folder_item_new(folder, folder->name, NULL);
486 item->folder = folder;
487 folder->node = item->node = g_node_new(item);
489 item = FOLDER_ITEM(folder->node->data);
491 rootpath = folder_item_get_path(item);
492 if (change_dir(rootpath) < 0) {
498 mh_create_tree(folder);
499 mh_remove_missing_folder_items(folder);
500 mh_scan_tree_recursive(item);
505 #define MAKE_DIR_IF_NOT_EXIST(dir) \
507 if (!is_dir_exist(dir)) { \
508 if (is_file_exist(dir)) { \
509 g_warning("File `%s' already exists.\n" \
510 "Can't create folder.", dir); \
513 if (make_dir(dir) < 0) \
518 static gint mh_create_tree(Folder *folder)
522 g_return_val_if_fail(folder != NULL, -1);
524 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), -1);
525 rootpath = LOCAL_FOLDER(folder)->rootpath;
526 MAKE_DIR_IF_NOT_EXIST(rootpath);
527 CHDIR_RETURN_VAL_IF_FAIL(rootpath, -1);
528 MAKE_DIR_IF_NOT_EXIST(INBOX_DIR);
529 MAKE_DIR_IF_NOT_EXIST(OUTBOX_DIR);
530 MAKE_DIR_IF_NOT_EXIST(QUEUE_DIR);
531 MAKE_DIR_IF_NOT_EXIST(DRAFT_DIR);
532 MAKE_DIR_IF_NOT_EXIST(TRASH_DIR);
537 #undef MAKE_DIR_IF_NOT_EXIST
539 static gchar *mh_item_get_path(Folder *folder, FolderItem *item)
541 gchar *folder_path, *path;
543 g_return_val_if_fail(folder != NULL, NULL);
544 g_return_val_if_fail(item != NULL, NULL);
546 folder_path = g_strdup(LOCAL_FOLDER(folder)->rootpath);
547 g_return_val_if_fail(folder_path != NULL, NULL);
549 if (folder_path[0] == G_DIR_SEPARATOR) {
551 path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
554 path = g_strdup(folder_path);
557 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
558 folder_path, G_DIR_SEPARATOR_S,
561 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
569 static FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
574 FolderItem *new_item;
575 gchar *mh_sequences_filename;
576 FILE *mh_sequences_file;
578 g_return_val_if_fail(folder != NULL, NULL);
579 g_return_val_if_fail(parent != NULL, NULL);
580 g_return_val_if_fail(name != NULL, NULL);
582 path = folder_item_get_path(parent);
583 if (!is_dir_exist(path))
584 if (make_dir_hier(path) != 0)
587 fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, name, NULL);
590 if (make_dir(fullpath) < 0) {
598 path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
601 path = g_strdup(name);
602 new_item = folder_item_new(folder, name, path);
603 folder_item_append(parent, new_item);
607 path = folder_item_get_path(new_item);
608 mh_sequences_filename = g_strconcat(path, G_DIR_SEPARATOR_S,
609 ".mh_sequences", NULL);
610 if ((mh_sequences_file = fopen(mh_sequences_filename, "a+b")) != NULL) {
611 fclose(mh_sequences_file);
613 g_free(mh_sequences_filename);
619 static gint mh_rename_folder(Folder *folder, FolderItem *item,
627 g_return_val_if_fail(folder != NULL, -1);
628 g_return_val_if_fail(item != NULL, -1);
629 g_return_val_if_fail(item->path != NULL, -1);
630 g_return_val_if_fail(name != NULL, -1);
632 oldpath = folder_item_get_path(item);
633 if (!is_dir_exist(oldpath))
634 make_dir_hier(oldpath);
636 dirname = g_dirname(oldpath);
637 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
640 if (rename(oldpath, newpath) < 0) {
641 FILE_OP_ERROR(oldpath, "rename");
650 if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
651 dirname = g_dirname(item->path);
652 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
655 newpath = g_strdup(name);
658 item->name = g_strdup(name);
660 paths[0] = g_strdup(item->path);
662 g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
663 mh_rename_folder_func, paths);
670 static gint mh_remove_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);
678 path = folder_item_get_path(item);
679 if (remove_dir_recursive(path) < 0) {
680 g_warning("can't remove directory `%s'\n", path);
686 folder_item_remove(item);
690 static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
695 g_return_val_if_fail(item != NULL, NULL);
696 g_return_val_if_fail(file != NULL, NULL);
698 flags.perm_flags = MSG_NEW|MSG_UNREAD;
701 if (item->stype == F_QUEUE) {
702 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
703 } else if (item->stype == F_DRAFT) {
704 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
707 msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
708 if (!msginfo) return NULL;
710 msginfo->msgnum = atoi(file);
711 msginfo->folder = item;
717 static gboolean mh_is_maildir_one(const gchar *path, const gchar *dir)
722 entry = g_strconcat(path, G_DIR_SEPARATOR_S, dir, NULL);
723 result = is_dir_exist(entry);
730 * check whether PATH is a Maildir style mailbox.
731 * This is the case if the 3 subdir: new, cur, tmp are existing.
732 * This functon assumes that entry is an directory
734 static gboolean mh_is_maildir(const gchar *path)
736 return mh_is_maildir_one(path, "new") &&
737 mh_is_maildir_one(path, "cur") &&
738 mh_is_maildir_one(path, "tmp");
742 static gboolean mh_remove_missing_folder_items_func(GNode *node, gpointer data)
747 g_return_val_if_fail(node->data != NULL, FALSE);
749 if (G_NODE_IS_ROOT(node))
752 item = FOLDER_ITEM(node->data);
754 path = folder_item_get_path(item);
755 if (!is_dir_exist(path)) {
756 debug_print("folder '%s' not found. removing...\n", path);
757 folder_item_remove(item);
764 static void mh_remove_missing_folder_items(Folder *folder)
766 g_return_if_fail(folder != NULL);
768 debug_print("searching missing folders...\n");
770 g_node_traverse(folder->node, G_POST_ORDER, G_TRAVERSE_ALL, -1,
771 mh_remove_missing_folder_items_func, folder);
774 static void mh_scan_tree_recursive(FolderItem *item)
783 g_return_if_fail(item != NULL);
784 g_return_if_fail(item->folder != NULL);
786 folder = item->folder;
788 dp = opendir(item->path ? item->path : ".");
790 FILE_OP_ERROR(item->path ? item->path : ".", "opendir");
794 debug_print("scanning %s ...\n",
795 item->path ? item->path
796 : LOCAL_FOLDER(item->folder)->rootpath);
798 folder->ui_func(folder, item, folder->ui_func_data);
800 while ((d = readdir(dp)) != NULL) {
801 if (d->d_name[0] == '.') continue;
804 entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
807 entry = g_strdup(d->d_name);
810 #ifdef HAVE_DIRENT_D_TYPE
811 d->d_type == DT_DIR ||
812 (d->d_type == DT_UNKNOWN &&
814 stat(entry, &s) == 0 && S_ISDIR(s.st_mode)
815 #ifdef HAVE_DIRENT_D_TYPE
819 FolderItem *new_item = NULL;
823 if (mh_is_maildir(entry)) {
830 for (node = node->children; node != NULL; node = node->next) {
831 FolderItem *cur_item = FOLDER_ITEM(node->data);
832 if (!strcmp2(cur_item->path, entry)) {
838 debug_print("new folder '%s' found.\n", entry);
839 new_item = folder_item_new(folder, d->d_name, entry);
840 folder_item_append(item, new_item);
844 if (!folder->inbox &&
845 !strcmp(d->d_name, INBOX_DIR)) {
846 new_item->stype = F_INBOX;
847 folder->inbox = new_item;
848 } else if (!folder->outbox &&
849 !strcmp(d->d_name, OUTBOX_DIR)) {
850 new_item->stype = F_OUTBOX;
851 folder->outbox = new_item;
852 } else if (!folder->draft &&
853 !strcmp(d->d_name, DRAFT_DIR)) {
854 new_item->stype = F_DRAFT;
855 folder->draft = new_item;
856 } else if (!folder->queue &&
857 !strcmp(d->d_name, QUEUE_DIR)) {
858 new_item->stype = F_QUEUE;
859 folder->queue = new_item;
860 } else if (!folder->trash &&
861 !strcmp(d->d_name, TRASH_DIR)) {
862 new_item->stype = F_TRASH;
863 folder->trash = new_item;
867 mh_scan_tree_recursive(new_item);
868 } else if (to_number(d->d_name) != -1) n_msg++;
877 gint new, unread, total, min, max;
879 procmsg_get_mark_sum(item->path, &new, &unread, &total,
882 new += n_msg - total;
883 unread += n_msg - total;
886 item->unread = unread;
892 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
894 FolderItem *item = node->data;
895 gchar **paths = data;
896 const gchar *oldpath = paths[0];
897 const gchar *newpath = paths[1];
902 oldpathlen = strlen(oldpath);
903 if (strncmp(oldpath, item->path, oldpathlen) != 0) {
904 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
908 base = item->path + oldpathlen;
909 while (*base == G_DIR_SEPARATOR) base++;
911 new_itempath = g_strdup(newpath);
913 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
916 item->path = new_itempath;