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 */
133 /* Message functions */
147 FolderClass *mh_get_class(void)
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 void mh_get_last_num(Folder *folder, FolderItem *item)
183 g_return_if_fail(item != NULL);
185 debug_print("mh_get_last_num(): Scanning %s ...\n", item->path);
187 path = folder_item_get_path(item);
188 g_return_if_fail(path != NULL);
189 if (change_dir(path) < 0) {
195 if ((dp = opendir(".")) == NULL) {
196 FILE_OP_ERROR(item->path, "opendir");
200 while ((d = readdir(dp)) != NULL) {
201 if ((num = to_number(d->d_name)) >= 0 &&
202 stat(d->d_name, &s) == 0 &&
203 S_ISREG(s.st_mode)) {
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)
221 gint num, nummsgs = 0;
223 g_return_val_if_fail(item != NULL, -1);
225 debug_print("mh_get_last_num(): Scanning %s ...\n", item->path);
227 *old_uids_valid = TRUE;
229 path = folder_item_get_path(item);
230 g_return_val_if_fail(path != NULL, -1);
231 if (change_dir(path) < 0) {
237 if ((dp = opendir(".")) == NULL) {
238 FILE_OP_ERROR(item->path, "opendir");
242 while ((d = readdir(dp)) != NULL) {
243 if ((num = to_number(d->d_name)) >= 0 &&
244 stat(d->d_name, &s) == 0 &&
245 S_ISREG(s.st_mode)) {
246 *list = g_slist_prepend(*list, GINT_TO_POINTER(num));
255 static gchar *mh_fetch_msg(Folder *folder, FolderItem *item, gint num)
260 g_return_val_if_fail(item != NULL, NULL);
261 g_return_val_if_fail(num > 0, NULL);
263 path = folder_item_get_path(item);
264 file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
266 if (!is_file_exist(file)) {
274 static MsgInfo *mh_get_msginfo(Folder *folder, FolderItem *item, gint num)
279 g_return_val_if_fail(item != NULL, NULL);
280 g_return_val_if_fail(num > 0, NULL);
282 file = mh_fetch_msg(folder, item, num);
283 if (!file) return NULL;
285 msginfo = mh_parse_msg(file, item);
287 msginfo->msgnum = num;
294 static gchar *mh_get_new_msg_filename(FolderItem *dest)
299 destpath = folder_item_get_path(dest);
300 g_return_val_if_fail(destpath != NULL, NULL);
302 if (!is_dir_exist(destpath))
303 make_dir_hier(destpath);
306 destfile = g_strdup_printf("%s%c%d", destpath, G_DIR_SEPARATOR,
308 if (is_file_entry_exist(destfile)) {
320 static gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file, MsgFlags *flags)
324 MsgFileInfo fileinfo;
326 g_return_val_if_fail(file != NULL, -1);
328 fileinfo.msginfo = NULL;
329 fileinfo.file = (gchar *)file;
330 fileinfo.flags = flags;
331 file_list.data = &fileinfo;
332 file_list.next = NULL;
334 ret = mh_add_msgs(folder, dest, &file_list, NULL);
338 static gint mh_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
343 MsgFileInfo *fileinfo;
345 g_return_val_if_fail(dest != NULL, -1);
346 g_return_val_if_fail(file_list != NULL, -1);
348 if (dest->last_num < 0) {
349 mh_get_last_num(folder, dest);
350 if (dest->last_num < 0) return -1;
353 for (cur = file_list; cur != NULL; cur = cur->next) {
354 fileinfo = (MsgFileInfo *)cur->data;
356 destfile = mh_get_new_msg_filename(dest);
357 if (destfile == NULL) return -1;
359 if (link(fileinfo->file, destfile) < 0) {
360 if (copy_file(fileinfo->file, destfile, TRUE) < 0) {
361 g_warning(_("can't copy message %s to %s\n"),
362 fileinfo->file, destfile);
367 if (relation != NULL)
368 g_relation_insert(relation, fileinfo, GINT_TO_POINTER(dest->last_num + 1));
373 return dest->last_num;
376 static gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
381 FolderItemPrefs *prefs;
383 g_return_val_if_fail(dest != NULL, -1);
384 g_return_val_if_fail(msginfo != NULL, -1);
386 if (msginfo->folder == dest) {
387 g_warning("the src folder is identical to the dest.\n");
391 if (dest->last_num < 0) {
392 mh_get_last_num(folder, dest);
393 if (dest->last_num < 0) return -1;
398 srcfile = procmsg_get_message_file(msginfo);
399 destfile = mh_get_new_msg_filename(dest);
405 debug_print("Copying message %s%c%d to %s ...\n",
406 msginfo->folder->path, G_DIR_SEPARATOR,
407 msginfo->msgnum, dest->path);
410 if ((MSG_IS_QUEUED(msginfo->flags) || MSG_IS_DRAFT(msginfo->flags))
411 && dest->stype != F_QUEUE && dest->stype != F_DRAFT) {
412 if (procmsg_remove_special_headers(srcfile, destfile) !=0) {
417 } else if (copy_file(srcfile, destfile, TRUE) < 0) {
418 FILE_OP_ERROR(srcfile, "copy");
425 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
426 if (chmod(destfile, prefs->folder_chmod) < 0)
427 FILE_OP_ERROR(destfile, "chmod");
430 filemode = prefs->folder_chmod;
431 if (filemode & S_IRGRP) filemode |= S_IWGRP;
432 if (filemode & S_IROTH) filemode |= S_IWOTH;
439 return dest->last_num;
442 static gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
446 g_return_val_if_fail(item != NULL, -1);
448 file = mh_fetch_msg(folder, item, num);
449 g_return_val_if_fail(file != NULL, -1);
451 if (unlink(file) < 0) {
452 FILE_OP_ERROR(file, "unlink");
461 static gint mh_remove_all_msg(Folder *folder, FolderItem *item)
466 g_return_val_if_fail(item != NULL, -1);
468 path = folder_item_get_path(item);
469 g_return_val_if_fail(path != NULL, -1);
470 val = remove_all_numbered_files(path);
476 static gboolean mh_is_msg_changed(Folder *folder, FolderItem *item,
481 if (stat(itos(msginfo->msgnum), &s) < 0 ||
482 msginfo->size != s.st_size ||
483 msginfo->mtime != s.st_mtime)
489 static gint mh_scan_tree(Folder *folder)
494 g_return_val_if_fail(folder != NULL, -1);
497 item = folder_item_new(folder, folder->name, NULL);
498 item->folder = folder;
499 folder->node = item->node = g_node_new(item);
501 item = FOLDER_ITEM(folder->node->data);
503 rootpath = folder_item_get_path(item);
504 if (change_dir(rootpath) < 0) {
510 mh_create_tree(folder);
511 mh_remove_missing_folder_items(folder);
512 mh_scan_tree_recursive(item);
517 #define MAKE_DIR_IF_NOT_EXIST(dir) \
519 if (!is_dir_exist(dir)) { \
520 if (is_file_exist(dir)) { \
521 g_warning("File `%s' already exists.\n" \
522 "Can't create folder.", dir); \
525 if (make_dir(dir) < 0) \
530 static gint mh_create_tree(Folder *folder)
534 g_return_val_if_fail(folder != NULL, -1);
536 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), -1);
537 rootpath = LOCAL_FOLDER(folder)->rootpath;
538 MAKE_DIR_IF_NOT_EXIST(rootpath);
539 CHDIR_RETURN_VAL_IF_FAIL(rootpath, -1);
540 MAKE_DIR_IF_NOT_EXIST(INBOX_DIR);
541 MAKE_DIR_IF_NOT_EXIST(OUTBOX_DIR);
542 MAKE_DIR_IF_NOT_EXIST(QUEUE_DIR);
543 MAKE_DIR_IF_NOT_EXIST(DRAFT_DIR);
544 MAKE_DIR_IF_NOT_EXIST(TRASH_DIR);
549 #undef MAKE_DIR_IF_NOT_EXIST
551 static gchar *mh_item_get_path(Folder *folder, FolderItem *item)
553 gchar *folder_path, *path;
555 g_return_val_if_fail(folder != NULL, NULL);
556 g_return_val_if_fail(item != NULL, NULL);
558 folder_path = g_strdup(LOCAL_FOLDER(folder)->rootpath);
559 g_return_val_if_fail(folder_path != NULL, NULL);
561 if (folder_path[0] == G_DIR_SEPARATOR) {
563 path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
566 path = g_strdup(folder_path);
569 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
570 folder_path, G_DIR_SEPARATOR_S,
573 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
581 static FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
586 FolderItem *new_item;
587 gchar *mh_sequences_filename;
588 FILE *mh_sequences_file;
590 g_return_val_if_fail(folder != NULL, NULL);
591 g_return_val_if_fail(parent != NULL, NULL);
592 g_return_val_if_fail(name != NULL, NULL);
594 path = folder_item_get_path(parent);
595 if (!is_dir_exist(path))
596 if (make_dir_hier(path) != 0)
599 fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, name, NULL);
602 if (make_dir(fullpath) < 0) {
610 path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
613 path = g_strdup(name);
614 new_item = folder_item_new(folder, name, path);
615 folder_item_append(parent, new_item);
619 path = folder_item_get_path(new_item);
620 mh_sequences_filename = g_strconcat(path, G_DIR_SEPARATOR_S,
621 ".mh_sequences", NULL);
622 if ((mh_sequences_file = fopen(mh_sequences_filename, "a+b")) != NULL) {
623 fclose(mh_sequences_file);
625 g_free(mh_sequences_filename);
631 static gint mh_rename_folder(Folder *folder, FolderItem *item,
639 g_return_val_if_fail(folder != NULL, -1);
640 g_return_val_if_fail(item != NULL, -1);
641 g_return_val_if_fail(item->path != NULL, -1);
642 g_return_val_if_fail(name != NULL, -1);
644 oldpath = folder_item_get_path(item);
645 if (!is_dir_exist(oldpath))
646 make_dir_hier(oldpath);
648 dirname = g_dirname(oldpath);
649 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
652 if (rename(oldpath, newpath) < 0) {
653 FILE_OP_ERROR(oldpath, "rename");
662 if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
663 dirname = g_dirname(item->path);
664 newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, name, NULL);
667 newpath = g_strdup(name);
670 item->name = g_strdup(name);
672 paths[0] = g_strdup(item->path);
674 g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
675 mh_rename_folder_func, paths);
682 static gint mh_remove_folder(Folder *folder, FolderItem *item)
686 g_return_val_if_fail(folder != NULL, -1);
687 g_return_val_if_fail(item != NULL, -1);
688 g_return_val_if_fail(item->path != NULL, -1);
690 path = folder_item_get_path(item);
691 if (remove_dir_recursive(path) < 0) {
692 g_warning("can't remove directory `%s'\n", path);
698 folder_item_remove(item);
702 static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
708 flags.perm_flags = MSG_NEW|MSG_UNREAD;
711 g_return_val_if_fail(item != NULL, NULL);
712 g_return_val_if_fail(file != NULL, NULL);
714 if (item->stype == F_QUEUE) {
715 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
716 } else if (item->stype == F_DRAFT) {
717 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
720 msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
721 if (!msginfo) return NULL;
723 msginfo->msgnum = atoi(file);
724 msginfo->folder = item;
726 if (stat(file, &s) < 0) {
727 FILE_OP_ERROR(file, "stat");
731 msginfo->size = s.st_size;
732 msginfo->mtime = s.st_mtime;
739 static gboolean mh_is_maildir_one(const gchar *path, const gchar *dir)
744 entry = g_strconcat(path, G_DIR_SEPARATOR_S, dir, NULL);
745 result = is_dir_exist(entry);
752 * check whether PATH is a Maildir style mailbox.
753 * This is the case if the 3 subdir: new, cur, tmp are existing.
754 * This functon assumes that entry is an directory
756 static gboolean mh_is_maildir(const gchar *path)
758 return mh_is_maildir_one(path, "new") &&
759 mh_is_maildir_one(path, "cur") &&
760 mh_is_maildir_one(path, "tmp");
764 static gboolean mh_remove_missing_folder_items_func(GNode *node, gpointer data)
769 g_return_val_if_fail(node->data != NULL, FALSE);
771 if (G_NODE_IS_ROOT(node))
774 item = FOLDER_ITEM(node->data);
776 path = folder_item_get_path(item);
777 if (!is_dir_exist(path)) {
778 debug_print("folder '%s' not found. removing...\n", path);
779 folder_item_remove(item);
786 static void mh_remove_missing_folder_items(Folder *folder)
788 g_return_if_fail(folder != NULL);
790 debug_print("searching missing folders...\n");
792 g_node_traverse(folder->node, G_POST_ORDER, G_TRAVERSE_ALL, -1,
793 mh_remove_missing_folder_items_func, folder);
796 static void mh_scan_tree_recursive(FolderItem *item)
805 g_return_if_fail(item != NULL);
806 g_return_if_fail(item->folder != NULL);
808 folder = item->folder;
810 dp = opendir(item->path ? item->path : ".");
812 FILE_OP_ERROR(item->path ? item->path : ".", "opendir");
816 debug_print("scanning %s ...\n",
817 item->path ? item->path
818 : LOCAL_FOLDER(item->folder)->rootpath);
820 folder->ui_func(folder, item, folder->ui_func_data);
822 while ((d = readdir(dp)) != NULL) {
823 if (d->d_name[0] == '.') continue;
826 entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
829 entry = g_strdup(d->d_name);
831 if (stat(entry, &s) < 0) {
832 FILE_OP_ERROR(entry, "stat");
837 if (S_ISDIR(s.st_mode)) {
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;