2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2001 Hiroyuki Yamamoto & The Sylpheed Claws Team
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.
25 #include <sys/types.h>
29 #include "mbox_folder.h"
32 #include "procheader.h"
36 #define MSGBUFSIZE 8192
38 static Folder *mbox_folder_new(const gchar * name, const gchar * path);
39 static void mbox_folder_destroy(Folder * folder);
41 static gchar *mbox_fetch_msg(Folder * folder, FolderItem * item, gint num);
43 static void mbox_scan_folder(Folder * folder, FolderItem * item);
44 static gint mbox_add_msg(Folder * folder, FolderItem * dest,
45 const gchar * file, gboolean remove_source);
47 static gint mbox_remove_all_msg(Folder * folder, FolderItem * item);
48 static gint mbox_remove_msg(Folder * folder, FolderItem * item, gint num);
49 static void mbox_change_flags(Folder * folder, FolderItem * item,
50 MsgInfo * info, MsgPermFlags newflags);
51 static gint mbox_copy_msg(Folder * folder, FolderItem * dest,
53 static gint mbox_create_tree(Folder * folder);
54 static FolderItem *mbox_create_folder(Folder * folder, FolderItem * parent,
56 static gint mbox_rename_folder(Folder * folder, FolderItem * item,
58 static gint mbox_remove_folder(Folder * folder, FolderItem * item);
60 static void mbox_folder_init (Folder *folder,
64 static gboolean mbox_write_data(FILE * mbox_fp, FILE * new_fp,
65 gchar * new_filename, gint size);
66 static gboolean mbox_purge_deleted(gchar * mbox);
67 static gchar * mbox_get_new_path(FolderItem * parent, gchar * name);
68 static gchar * mbox_get_folderitem_name(gchar * name);
70 static MsgInfo *mbox_get_msginfo(Folder *folder, FolderItem *item, gint num);
71 static gint mbox_get_num_list(Folder *folder, FolderItem *item, GSList **list);
72 static gboolean mbox_check_msgnum_validity(Folder *folder, FolderItem *item);
73 static gchar *mbox_folder_get_path(Folder *folder, FolderItem *item);
75 FolderClass mbox_class =
81 /* Folder functions */
87 /* FolderItem functions */
98 mbox_check_msgnum_validity,
100 /* Message functions */
112 FolderClass *mbox_get_class(void)
117 Folder *mbox_folder_new(const gchar *name, const gchar *path)
121 folder = (Folder *)g_new0(MBOXFolder, 1);
122 folder->klass = &mbox_class;
123 mbox_folder_init(folder, name, path);
128 void mbox_folder_destroy(Folder *folder)
130 folder_local_folder_destroy(LOCAL_FOLDER(folder));
133 static void mbox_folder_init(Folder *folder, const gchar *name, const gchar *path)
135 folder_local_folder_init(folder, name, path);
138 static void mbox_folder_create_parent(const gchar * path)
140 if (!is_file_exist(path)) {
143 new_path = g_dirname(path);
144 if (new_path[strlen(new_path) - 1] == G_DIR_SEPARATOR)
145 new_path[strlen(new_path) - 1] = '\0';
147 if (!is_dir_exist(new_path))
148 make_dir_hier(new_path);
155 gchar *mbox_folder_get_path(Folder *folder, FolderItem *item)
160 g_return_val_if_fail(item != NULL, NULL);
162 if (item->path && item->path[0] == G_DIR_SEPARATOR) {
163 mbox_folder_create_parent(item->path);
164 return g_strdup(item->path);
167 folder_path = g_strdup(LOCAL_FOLDER(item->folder)->rootpath);
168 g_return_val_if_fail(folder_path != NULL, NULL);
170 if (folder_path[0] == G_DIR_SEPARATOR) {
172 path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
176 path = g_strdup(folder_path);
179 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
180 folder_path, G_DIR_SEPARATOR_S,
183 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
189 mbox_folder_create_parent(path);
195 /**********************************************************/
199 /**********************************************************/
202 static GSList * file_lock = NULL;
204 static gboolean mbox_file_lock_file(gchar * base)
206 gchar *lockfile, *locklink;
210 lockfile = g_strdup_printf("%s.%d", base, getpid());
211 if ((lockfp = fopen(lockfile, "wb")) == NULL) {
212 FILE_OP_ERROR(lockfile, "fopen");
213 g_warning("can't create lock file %s\n", lockfile);
214 g_warning("use 'flock' instead of 'file' if possible.\n");
219 fprintf(lockfp, "%d\n", getpid());
222 locklink = g_strconcat(base, ".lock", NULL);
223 while (link(lockfile, locklink) < 0) {
224 FILE_OP_ERROR(lockfile, "link");
226 g_warning("can't create %s\n", lockfile);
233 g_warning("mailbox is owned by another"
234 " process, waiting...\n");
245 static gboolean mbox_fcntl_lockwrite_file(FILE * fp)
249 lck.l_type = F_WRLCK;
254 if (fcntl(fileno(fp), F_SETLK, &lck) < 0)
260 static gboolean mbox_fcntl_lockread_file(FILE * fp)
264 lck.l_type = F_RDLCK;
269 if (fcntl(fileno(fp), F_SETLK, &lck) < 0)
275 static gboolean mbox_fcntl_unlock_file(FILE * fp)
279 lck.l_type = F_UNLCK;
284 if (fcntl(fileno(fp), F_SETLK, &lck) < 0)
290 static gboolean mbox_file_unlock_file(gchar * base)
294 lockfile = g_strdup_printf("%s.lock", base);
301 static gboolean mbox_lockread_file(FILE * fp, gchar * base)
305 result = mbox_fcntl_lockread_file(fp);
307 if ((result = mbox_file_lock_file(base)) == TRUE) {
308 file_lock = g_slist_append(file_lock, g_strdup(base));
309 debug_print("lockfile lock %s.\n", base);
312 g_warning("could not lock read file %s\n", base);
315 debug_print("fcntl lock %s.\n", base);
320 static gboolean mbox_lockwrite_file(FILE * fp, gchar * base)
324 result = mbox_fcntl_lockwrite_file(fp);
326 if ((result = mbox_file_lock_file(base)) == TRUE) {
327 file_lock = g_slist_append(file_lock, g_strdup(base));
328 debug_print("lockfile lock %s.\n", base);
331 g_warning("could not lock write file %s\n", base);
334 debug_print("fcntl lock %s.\n", base);
339 static gboolean mbox_unlock_file(FILE * fp, gchar * base)
341 gboolean result = FALSE;
343 gboolean unlocked = FALSE;
345 for(l = file_lock ; l != NULL ; l = g_slist_next(l)) {
346 gchar * data = l->data;
348 if (strcmp(data, base) == 0) {
349 file_lock = g_slist_remove(file_lock, data);
351 result = mbox_file_unlock_file(base);
353 debug_print("lockfile unlock - %s.\n", base);
359 result = mbox_fcntl_unlock_file(fp);
360 debug_print("fcntl unlock - %s.\n", base);
366 /**********************************************************/
370 /**********************************************************/
372 #define MAILFILE_ERROR_NO_ERROR 0x000
373 #define MAILFILE_ERROR_FILE_NOT_FOUND 0x001
374 #define MAILFILE_ERROR_MEMORY 0x002
375 #define MAILFILE_ERROR_MESSAGE_NOT_FOUND 0x003
377 static int mailfile_error = MAILFILE_ERROR_NO_ERROR;
379 #define STATE_BEGIN 0x000
380 #define STATE_TEXT_READ 0x001
381 #define STATE_FROM_READ 0x002
382 #define STATE_FIELD_READ 0x003
383 #define STATE_END 0x004
384 #define STATE_END_OF_FILE 0x005
385 #define STATE_MEM_ERROR 0x006
386 #define STATE_TEXT_BEGIN 0x007
388 #define STATE_MASK 0x0FF /* filter state from functions */
390 #define STATE_RESTORE_POS 0x100 /* go back while reading */
392 typedef struct _mailfile mailfile;
416 #define MSG_IS_INVALID(msg) \
417 ((msg).perm_flags == (msg).tmp_flags && (msg).tmp_flags == -1)
419 #define MSG_SET_INVALID(msg) \
420 ((msg).perm_flags = (msg).tmp_flags = -1)
422 static int startFrom(char * s)
424 return (strncmp(s, "From ", 5) == 0);
427 static int startSpace(char * s)
429 return ((*s == ' ') || (*s == '\t'));
432 static int startEmpty(char * s)
437 static void free_msg_list(GList * l)
439 GList * elt = g_list_first(l);
444 elt = g_list_next(elt);
451 static mailfile * mailfile_init_from_file(FILE * f, gchar * filename)
454 GList * msg_list = NULL;
461 struct _message * data = NULL;
467 while (state != STATE_END_OF_FILE) {
468 if ((state & STATE_RESTORE_POS) == 0) {
469 former_pos = lastpos;
472 r = fgets(s, 256, f);
475 ignore_next = (s[strlen(s) - 1] != '\n');
480 switch(state & 0x0F) {
483 state = STATE_END_OF_FILE;
484 else if (startFrom(s)) {
485 state = STATE_FROM_READ;
487 data = g_new0(struct _message, 1);
489 free_msg_list(msg_list);
494 data->msgnum = msgnum;
495 data->offset = lastpos;
496 data->header = lastpos;
499 data->messageid = NULL;
500 data->fromspace = NULL;
501 MSG_SET_INVALID(data->flags);
502 MSG_SET_INVALID(data->old_flags);
503 data->fetched = FALSE;
504 msg_list = g_list_append(msg_list,
512 case STATE_TEXT_READ:
515 else if (startFrom(s))
516 state = STATE_END | STATE_RESTORE_POS;
518 state = STATE_TEXT_READ;
521 case STATE_TEXT_BEGIN:
522 data->content = lastpos;
525 else if (startFrom(s)) {
526 state = STATE_END | STATE_RESTORE_POS;
529 state = STATE_TEXT_READ;
533 case STATE_FROM_READ:
534 data->content = lastpos;
537 else if (startSpace(s))
538 state = STATE_FROM_READ;
539 else if (startEmpty(s))
540 state = STATE_TEXT_READ;
542 state = STATE_FIELD_READ;
545 case STATE_FIELD_READ:
546 data->content = lastpos;
549 else if (startSpace(s))
550 state = STATE_FIELD_READ;
551 else if (startEmpty(s)) {
552 state = STATE_TEXT_BEGIN;
555 state = STATE_FIELD_READ;
559 if ((state & STATE_MASK) == STATE_END) {
560 state = STATE_BEGIN | (state & STATE_RESTORE_POS);
566 r = fgets(s, 256, f);
567 if (r == NULL || *r == '\0')
570 while (s[strlen(s) - 1] != '\n');
574 mf = (mailfile *) g_new0(struct _mailfile, 1);
576 free_msg_list(msg_list);
577 mailfile_error = MAILFILE_ERROR_MEMORY;
581 mf->msg_list = g_list_first(msg_list);
583 mf->filename = g_strdup(filename);
586 mailfile_error = MAILFILE_ERROR_NO_ERROR;
591 static mailfile * mailfile_init(char * filename)
597 f = fopen(filename, "rb");
600 mailfile_error = MAILFILE_ERROR_FILE_NOT_FOUND;
604 mbox_lockread_file(f, filename);
606 mf = mailfile_init_from_file(f, filename);
608 mbox_unlock_file(f, filename);
615 static void mailfile_done(mailfile * f)
617 free_msg_list(f->msg_list);
624 #define MAX_READ 4096
626 static char * readfile(char * filename, int offset, int max_offset)
635 handle = fopen(filename, "rb");
637 if (handle == NULL) {
638 mailfile_error = MAILFILE_ERROR_FILE_NOT_FOUND;
642 size = max_offset - offset;
644 message = (char *) malloc(size + 1);
645 if (message == NULL) {
647 mailfile_error = MAILFILE_ERROR_MEMORY;
651 fseek(handle, offset, SEEK_SET);
655 if ((size - pos) > MAX_READ)
660 bread = fread(message + pos, 1, max, handle);
676 static char * mailfile_readmsg(mailfile f, int index)
682 struct _message * msginfo;
684 nth = g_list_nth(f->msg_list, index);
687 mailfile_error = MAILFILE_ERROR_MESSAGE_NOT_FOUND;
691 msginfo = (struct _message *)nth->data;
693 offset = msginfo->offset;
694 max_offset = msginfo->end;
695 message = readfile(f->filename, offset, max_offset);
697 mailfile_error = MAILFILE_ERROR_NO_ERROR;
702 static char * mailfile_readheader(mailfile f, int index)
708 struct _message * msginfo;
710 nth = g_list_nth(f->msg_list, index);
713 mailfile_error = MAILFILE_ERROR_MESSAGE_NOT_FOUND;
717 msginfo = (struct _message *)nth->data;
719 offset = msginfo->offset;
720 max_offset = msginfo->content;
721 message = readfile(f->filename, offset, max_offset);
723 mailfile_error = MAILFILE_ERROR_NO_ERROR;
728 static int mailfile_count(mailfile * f)
730 return g_list_length(f->msg_list);
733 static int mailfile_find_deleted(mailfile f, char * filename)
737 handle = fopen(filename, "rb");
740 struct _message m = elt->data;
741 n = fread(&m.deleted, sizeof(int), 1, handle);
744 elt = g_list_next(elt);
753 /**********************************************************/
755 /* mbox cache operations */
757 /**********************************************************/
764 gboolean modification;
767 typedef struct _mboxcache mboxcache;
769 static GHashTable * mbox_cache_table = NULL;
771 static MsgInfo *mbox_parse_msg(FILE * fp, struct _message * msg,
775 MsgFlags flags = { 0, 0 };
777 MSG_SET_PERM_FLAGS(flags, MSG_NEW | MSG_UNREAD);
779 g_return_val_if_fail(fp != NULL, NULL);
782 if (item->stype == F_QUEUE) {
783 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
784 } else if (item->stype == F_DRAFT) {
785 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
789 msginfo = procheader_parse_stream(fp, flags, FALSE, FALSE);
791 if (!msginfo) return NULL;
794 msginfo->msgnum = msg->msgnum;
795 msginfo->folder = item;
801 static void mbox_cache_init()
803 mbox_cache_table = g_hash_table_new(g_str_hash, g_str_equal);
806 static void mbox_cache_free_mbox(mboxcache * cache)
808 g_hash_table_remove(mbox_cache_table, cache->filename);
811 mailfile_done(cache->mf);
813 g_ptr_array_free(cache->tab_mf, FALSE);
815 g_free(cache->filename);
819 static void mbox_cache_get_msginfo_from_file(FILE * fp, GList * msg_list)
824 for(l = msg_list ; l != NULL ; l = g_list_next(l)) {
825 struct _message * msg;
827 msg = (struct _message *) l->data;
829 fseek(fp, msg->header, SEEK_SET);
830 msginfo = mbox_parse_msg(fp, msg, NULL);
834 g_strdup(msginfo->msgid);
835 if (msginfo->fromspace)
837 g_strdup(msginfo->fromspace);
838 msg->flags = msginfo->flags;
839 msg->old_flags = msginfo->flags;
841 procmsg_msginfo_free(msginfo);
846 static void mbox_cache_get_msginfo(gchar * filename, GList * msg_list)
850 fp = fopen(filename, "rb");
854 mbox_cache_get_msginfo_from_file(fp, msg_list);
858 static mboxcache * mbox_cache_read_mbox(gchar * filename)
865 if (stat(filename, &s) < 0)
868 mf = mailfile_init(filename);
872 cache = g_new0(mboxcache, 1);
874 cache->mtime = s.st_mtime;
876 cache->filename = g_strdup(filename);
877 cache->modification = FALSE;
879 cache->tab_mf = g_ptr_array_new();
880 for(l = mf->msg_list ; l != NULL ; l = g_list_next(l))
881 g_ptr_array_add(cache->tab_mf, l->data);
883 mbox_cache_get_msginfo(filename, mf->msg_list);
885 debug_print("read mbox - %s\n", filename);
890 static mboxcache * mbox_cache_read_mbox_from_file(FILE * fp, gchar * filename)
897 if (stat(filename, &s) < 0)
900 mf = mailfile_init_from_file(fp, filename);
904 cache = g_new0(mboxcache, 1);
906 cache->mtime = s.st_mtime;
908 cache->filename = g_strdup(filename);
910 cache->tab_mf = g_ptr_array_new();
911 for(l = mf->msg_list ; l != NULL ; l = g_list_next(l))
912 g_ptr_array_add(cache->tab_mf, l->data);
914 mbox_cache_get_msginfo_from_file(fp, mf->msg_list);
916 debug_print("read mbox from file - %s\n", filename);
921 static void mbox_cache_insert_mbox(mboxcache * data)
923 if (mbox_cache_table == NULL)
926 g_hash_table_insert(mbox_cache_table, data->filename, data);
929 static mboxcache * mbox_cache_get_mbox(gchar * filename)
931 if (mbox_cache_table == NULL)
934 return g_hash_table_lookup(mbox_cache_table, filename);
938 static gint mbox_cache_get_count(gchar * filename)
942 cache = mbox_cache_get_mbox(filename);
945 if (cache->mf == NULL)
947 return cache->mf->count;
950 static GList * mbox_cache_get_msg_list(gchar * filename)
954 cache = mbox_cache_get_mbox(filename);
959 if (cache->mf == NULL)
962 return cache->mf->msg_list;
965 static void mbox_cache_synchronize_lists(GList * old_msg_list,
966 GList * new_msg_list)
971 for(l2 = old_msg_list ; l2 != NULL ; l2 = g_list_next(l2)) {
972 struct _message * msg2 = l2->data;
974 if ((msg2->messageid == NULL) ||
975 (msg2->fromspace == NULL))
978 for(l = new_msg_list ; l != NULL ; l = g_list_next(l)) {
979 struct _message * msg = l->data;
981 if ((msg->messageid == NULL) ||
982 (msg->fromspace == NULL))
985 if ((strcmp(msg->messageid, msg2->messageid) == 0) &&
986 (strcmp(msg->fromspace, msg2->fromspace) == 0)) {
987 if (msg2->flags.perm_flags != msg2->old_flags.perm_flags) {
988 msg->flags = msg2->flags;
996 static void mbox_cache_synchronize(gchar * filename, gboolean sync)
998 mboxcache * new_cache;
999 mboxcache * old_cache;
1000 gboolean scan_new = TRUE;
1003 old_cache = mbox_cache_get_mbox(filename);
1005 if (old_cache != NULL) {
1006 if (stat(filename, &s) < 0) {
1007 FILE_OP_ERROR(filename, "stat");
1008 } else if (old_cache->mtime == s.st_mtime) {
1009 debug_print("Folder is not modified.\n");
1016 if (strstr(filename, "trash") == 0)
1017 printf("old_cache: %p %s\n", old_cache, filename);
1019 printf("begin old\n");
1020 for(l = old_cache->mf->msg_list ; l != NULL ;
1021 l = g_list_next(l)) {
1022 struct _message * msg = l->data;
1023 printf("%p\n", msg);
1025 printf("end old\n");
1029 new_cache = mbox_cache_read_mbox(filename);
1032 if (strstr(filename, "trash") == 0)
1033 printf("new_cache: %p %s\n", new_cache, filename);
1035 printf("begin new\n");
1036 for(l = new_cache->mf->msg_list ; l != NULL ;
1037 l = g_list_next(l)) {
1038 struct _message * msg = l->data;
1039 printf("%p\n", msg);
1041 printf("end new\n");
1048 if (sync && new_cache && old_cache)
1049 mbox_cache_synchronize_lists(old_cache->mf->msg_list,
1050 new_cache->mf->msg_list);
1052 if (old_cache != NULL)
1053 mbox_cache_free_mbox(old_cache);
1056 mbox_cache_insert_mbox(new_cache);
1058 printf("insert %p %s\n", new_cache, new_cache->filename);
1059 printf("inserted %s %p\n", filename,
1060 mbox_cache_get_mbox(filename));
1066 static void mbox_cache_synchronize_from_file(FILE * fp, gchar * filename,
1069 mboxcache * new_cache;
1070 mboxcache * old_cache;
1071 gboolean scan_new = TRUE;
1074 old_cache = mbox_cache_get_mbox(filename);
1076 if (old_cache != NULL) {
1077 if (stat(filename, &s) < 0) {
1078 FILE_OP_ERROR(filename, "stat");
1079 } else if (old_cache->mtime == s.st_mtime) {
1080 debug_print("Folder is not modified.\n");
1091 if (strstr(filename, "trash") == 0)
1092 printf("old_cache: %p %s\n", old_cache, filename);
1095 printf("begin old\n");
1096 for(l = old_cache->mf->msg_list ; l != NULL ;
1097 l = g_list_next(l)) {
1098 struct _message * msg = l->data;
1099 printf("%p\n", msg);
1101 printf("end old\n");
1105 new_cache = mbox_cache_read_mbox_from_file(fp, filename);
1108 if (strstr(filename, "trash") == 0)
1109 printf("new_cache: %p %s\n", new_cache, filename);
1112 printf("begin new\n");
1113 for(l = new_cache->mf->msg_list ; l != NULL ;
1114 l = g_list_next(l)) {
1115 struct _message * msg = l->data;
1116 printf("%p\n", msg);
1118 printf("end new\n");
1125 if (sync && new_cache && old_cache)
1126 mbox_cache_synchronize_lists(old_cache->mf->msg_list,
1127 new_cache->mf->msg_list);
1129 if (old_cache != NULL)
1130 mbox_cache_free_mbox(old_cache);
1133 mbox_cache_insert_mbox(new_cache);
1135 printf("insert %p %s\n", new_cache, new_cache->filename);
1136 printf("inserted %s %p\n", filename,
1137 mbox_cache_get_mbox(filename));
1143 gboolean mbox_cache_msg_fetched(gchar * filename, gint num)
1145 struct _message * msg;
1148 cache = mbox_cache_get_mbox(filename);
1153 msg = (struct _message *) g_ptr_array_index(cache->tab_mf,
1158 return msg->fetched;
1161 void mbox_cache_msg_set_fetched(gchar * filename, gint num)
1163 struct _message * msg;
1166 cache = mbox_cache_get_mbox(filename);
1171 msg = (struct _message *) g_ptr_array_index(cache->tab_mf,
1176 msg->fetched = TRUE;
1179 struct _message * mbox_cache_get_msg(gchar * filename, gint num)
1183 cache = mbox_cache_get_mbox(filename);
1185 if (cache == NULL) {
1189 return (struct _message *) g_ptr_array_index(cache->tab_mf,
1194 /**********************************************************/
1196 /* mbox operations */
1198 /**********************************************************/
1200 static gboolean mbox_extract_msg(FolderItem * item, gint msgnum,
1201 gchar * dest_filename)
1203 struct _message * msg;
1210 /* GList * msg_list;*/
1211 gboolean already_fetched;
1214 mbox_path = mbox_folder_get_path(item->folder, item);
1216 if (mbox_path == NULL)
1219 src = fopen(mbox_path, "rb");
1225 mbox_lockread_file(src, mbox_path);
1227 mbox_cache_synchronize_from_file(src, mbox_path, TRUE);
1229 already_fetched = mbox_cache_msg_fetched(mbox_path, msgnum);
1231 if (already_fetched) {
1232 mbox_unlock_file(src, mbox_path);
1238 msg = mbox_cache_get_msg(mbox_path, msgnum);
1241 mbox_unlock_file(src, mbox_path);
1247 offset = msg->offset;
1248 max_offset = msg->end;
1250 size = max_offset - offset;
1252 fseek(src, offset, SEEK_SET);
1254 dest = fopen(dest_filename, "wb");
1256 mbox_unlock_file(src, mbox_path);
1262 if (change_file_mode_rw(dest, dest_filename) < 0) {
1263 FILE_OP_ERROR(dest_filename, "chmod");
1264 g_warning("can't change file mode\n");
1267 if (!mbox_write_data(src, dest, dest_filename, size)) {
1268 mbox_unlock_file(src, mbox_path);
1271 unlink(dest_filename);
1279 FILE_OP_ERROR(mbox_path, "fread");
1283 mbox_cache_msg_set_fetched(mbox_path, msgnum);
1285 if (fclose(dest) == -1) {
1286 FILE_OP_ERROR(dest_filename, "fclose");
1290 mbox_unlock_file(src, mbox_path);
1292 if (fclose(src) == -1) {
1293 FILE_OP_ERROR(mbox_path, "fclose");
1300 unlink(dest_filename);
1307 gchar *mbox_fetch_msg(Folder *folder, FolderItem *item, gint num)
1312 g_return_val_if_fail(item != NULL, NULL);
1314 path = folder_item_get_path(item);
1315 if (!is_dir_exist(path))
1316 make_dir_hier(path);
1318 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
1322 if (!mbox_extract_msg(item, num, filename)) {
1330 gint mbox_add_msg(Folder *folder, FolderItem *dest, const gchar *file,
1331 gboolean remove_source)
1340 gchar from_line[MSGBUFSIZE];
1342 if (dest->last_num < 0) {
1343 mbox_scan_folder(folder, dest);
1344 if (dest->last_num < 0) return -1;
1347 src_fp = fopen(file, "rb");
1348 if (src_fp == NULL) {
1352 mbox_path = mbox_folder_get_path(folder, dest);
1353 if (mbox_path == NULL)
1356 dest_fp = fopen(mbox_path, "ab");
1357 if (dest_fp == NULL) {
1363 if (change_file_mode_rw(dest_fp, mbox_path) < 0) {
1364 FILE_OP_ERROR(mbox_path, "chmod");
1365 g_warning("can't change file mode\n");
1368 old_size = ftell(dest_fp);
1370 mbox_lockwrite_file(dest_fp, mbox_path);
1372 if (fgets(from_line, sizeof(from_line), src_fp) == NULL) {
1373 mbox_unlock_file(dest_fp, mbox_path);
1374 g_warning("unvalid file - %s.\n", file);
1381 if (strncmp(from_line, "From ", 5) != 0) {
1384 if (stat(file, &s) < 0) {
1385 mbox_unlock_file(dest_fp, mbox_path);
1386 g_warning("invalid file - %s.\n", file);
1393 fprintf(dest_fp, "From - %s", ctime(&s.st_mtime));
1396 fputs(from_line, dest_fp);
1399 n_read = fread(buf, 1, sizeof(buf), src_fp);
1400 if ((n_read < (gint) sizeof(buf)) && ferror(src_fp))
1402 if (fwrite(buf, n_read, 1, dest_fp) < 1) {
1403 mbox_unlock_file(dest_fp, mbox_path);
1404 g_warning("writing to %s failed.\n", mbox_path);
1405 ftruncate(fileno(dest_fp), old_size);
1412 if (n_read < (gint) sizeof(buf))
1418 if (ferror(src_fp)) {
1419 FILE_OP_ERROR(mbox_path, "fread");
1422 mbox_unlock_file(dest_fp, mbox_path);
1424 if (fclose(src_fp) == -1) {
1425 FILE_OP_ERROR(file, "fclose");
1429 if (fclose(dest_fp) == -1) {
1430 FILE_OP_ERROR(mbox_path, "fclose");
1436 ftruncate(fileno(dest_fp), old_size);
1441 if (remove_source) {
1442 if (unlink(file) < 0)
1443 FILE_OP_ERROR(file, "unlink");
1449 return dest->last_num;
1453 gint mbox_remove_msg(Folder *folder, FolderItem *item, gint num)
1455 struct _message * msg;
1458 mbox_path = mbox_folder_get_path(folder, item);
1459 if (mbox_path == NULL)
1462 mbox_cache_synchronize(mbox_path, TRUE);
1464 msg = mbox_cache_get_msg(mbox_path, num);
1469 MSG_SET_PERM_FLAGS(msg->flags, MSG_REALLY_DELETED);
1474 gint mbox_remove_all_msg(Folder *folder, FolderItem *item)
1479 mbox_path = mbox_folder_get_path(folder, item);
1480 if (mbox_path == NULL)
1483 fp = fopen(mbox_path, "wb");
1497 gint mbox_move_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
1502 filename = mbox_fetch_msg(folder, msginfo->folder, msginfo->msgnum);
1503 if (filename == NULL)
1506 msgnum = mbox_add_msg(folder, dest, filename, TRUE);
1509 MSG_SET_FLAGS(msginfo->flags, MSG_REALLY_DELETED);
1510 mbox_change_flags(folder, msginfo->folder, msginfo);
1516 gint mbox_move_msgs_with_dest(Folder *folder, FolderItem *dest, GSList *msglist)
1519 gchar * mbox_path = NULL;
1521 for(l = msglist ; l != NULL ; l = g_slist_next(l)) {
1522 MsgInfo * msginfo = (MsgInfo *) l->data;
1524 if (msginfo->folder && mbox_path == NULL)
1525 mbox_path = mbox_folder_get_path(msginfo->folder);
1527 mbox_move_msg(folder, dest, msginfo);
1531 mbox_cache_synchronize(mbox_path);
1535 mbox_path = mbox_folder_get_path(dest);
1536 mbox_cache_synchronize(mbox_path);
1539 return dest->last_num;
1544 gint mbox_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
1549 filename = mbox_fetch_msg(folder, msginfo->folder, msginfo->msgnum);
1550 if (filename == NULL)
1553 msgnum = mbox_add_msg(folder, dest, filename, FALSE);
1558 gint mbox_copy_msgs_with_dest(Folder *folder, FolderItem *dest, GSList *msglist)
1561 gchar * mbox_path = NULL;
1563 for(l = msglist ; l != NULL ; l = g_slist_next(l)) {
1564 MsgInfo * msginfo = (MsgInfo *) l->data;
1566 if (msginfo->folder && mbox_path == NULL)
1567 mbox_path = mbox_folder_get_path(msginfo->folder);
1569 mbox_copy_msg(folder, dest, msginfo);
1573 mbox_cache_synchronize(mbox_path);
1577 mbox_path = mbox_folder_get_path(dest);
1578 mbox_cache_synchronize(mbox_path);
1581 return dest->last_num;
1585 struct _copy_flags_info
1591 typedef struct _copy_flags_info CopyFlagsInfo;
1593 GSList * copy_flags_data = NULL;
1595 gint mbox_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
1597 Folder * src_folder;
1600 CopyFlagsInfo * flags_info;
1602 src_folder = msginfo->folder->folder;
1605 mbox_path = mbox_folder_get_path(msginfo->folder);
1606 mbox_rewrite(mbox_path);
1610 filename = folder_item_fetch_msg(msginfo->folder,
1612 if (filename == NULL)
1615 num = mbox_add_msg(folder, dest, filename, FALSE);
1618 mbox_path = mbox_folder_get_path(dest);
1619 msg = mbox_cache_get_msg(mbox_path, num);
1621 msg->flags = msginfo->flags;
1628 flags_info = g_new0(CopyFlagsInfo, 1);
1629 flags_info->num = num;
1630 flags_info->flags = msginfo->flags;
1631 copy_flags_data = g_slist_append(copy_flags_data, flags_info);
1636 void mbox_scan_folder(Folder *folder, FolderItem *item)
1643 mbox_path = mbox_folder_get_path(folder, item);
1644 if (mbox_path == NULL)
1647 mbox_cache_synchronize(mbox_path, TRUE);
1649 cached = mbox_cache_get_mbox(mbox_path);
1651 if (cached == NULL) {
1653 item->unread_msgs = 0;
1654 item->total_msgs = 0;
1660 n_msg = mbox_cache_get_count(mbox_path);
1663 item->new_msgs = item->unread_msgs = item->total_msgs = 0;
1670 for(l = mbox_cache_get_msg_list(mbox_path) ; l != NULL ;
1671 l = g_list_next(l)) {
1672 struct _message * msg = (struct _message *) l->data;
1673 if (!MSG_IS_REALLY_DELETED(msg->flags))
1675 if (MSG_IS_NEW(msg->flags) && !MSG_IS_IGNORE_THREAD(msg->flags))
1677 if (MSG_IS_UNREAD(msg->flags) && !MSG_IS_IGNORE_THREAD(msg->flags))
1681 item->new_msgs = new;
1682 item->unread_msgs = unread;
1683 item->total_msgs = total;
1686 debug_print("Last number in dir %s = %d\n", mbox_path,
1688 item->last_num = n_msg;
1692 gchar * mbox_get_virtual_path(FolderItem * item)
1697 if (item->parent == NULL) {
1701 gchar * parent_path;
1702 gchar * result_path;
1704 parent_path = mbox_get_virtual_path(item->parent);
1705 if (parent_path == NULL)
1706 result_path = g_strdup(item->name);
1708 result_path = g_strconcat(parent_path,
1711 g_free(parent_path);
1717 static gboolean mbox_write_data(FILE * mbox_fp, FILE * new_fp,
1718 gchar * new_filename, gint size)
1726 while (pos < size) {
1727 if ((size - pos) > (gint) sizeof(buf))
1732 n_read = fread(buf, 1, max, mbox_fp);
1734 if (n_read < max && ferror(mbox_fp)) {
1737 if (fwrite(buf, n_read, 1, new_fp) < 1) {
1738 g_warning("writing to %s failed.\n", new_filename);
1751 static gboolean mbox_write_message(FILE * mbox_fp, FILE * new_fp,
1752 gchar * new_filename,
1753 struct _message * msg)
1756 GPtrArray * headers;
1759 fseek(mbox_fp, msg->header, SEEK_SET);
1761 headers = procheader_get_header_array_asis(mbox_fp);
1763 for (i = 0; i < (gint) headers->len; i++) {
1764 Header * h = g_ptr_array_index(headers, i);
1766 if (!procheader_headername_equal(h->name,
1768 !procheader_headername_equal(h->name,
1770 fwrite(h->name, strlen(h->name),
1772 if (h->name[strlen(h->name) - 1] != ' ')
1773 fwrite(" ", 1, 1, new_fp);
1774 fwrite(h->body, strlen(h->body),
1776 fwrite("\n", 1, 1, new_fp);
1778 procheader_header_free(h);
1779 g_ptr_array_remove_index(headers, i);
1783 g_ptr_array_free(headers, FALSE);
1785 if (!MSG_IS_INVALID(msg->flags)) {
1787 fwrite("Status: ", strlen("Status: "), 1, new_fp);
1788 if (!MSG_IS_UNREAD(msg->flags))
1789 fwrite("R", 1, 1, new_fp);
1790 fwrite("O", 1, 1, new_fp);
1791 fwrite("\n", 1, 1, new_fp);
1793 /* X-Status header */
1794 if (MSG_IS_REALLY_DELETED(msg->flags)
1795 || MSG_IS_MARKED(msg->flags)
1796 || MSG_IS_DELETED(msg->flags)
1797 || MSG_IS_REPLIED(msg->flags)
1798 || MSG_IS_FORWARDED(msg->flags)) {
1799 fwrite("X-Status: ", strlen("X-Status: "), 1, new_fp);
1800 if (MSG_IS_REALLY_DELETED(msg->flags))
1801 fwrite("D", 1, 1, new_fp); /* really deleted */
1803 if (MSG_IS_MARKED(msg->flags))
1804 fwrite("F", 1, 1, new_fp);
1805 if (MSG_IS_DELETED(msg->flags))
1806 fwrite("d", 1, 1, new_fp);
1807 if (MSG_IS_REPLIED(msg->flags))
1808 fwrite("r", 1, 1, new_fp);
1809 if (MSG_IS_FORWARDED(msg->flags))
1810 fwrite("f", 1, 1, new_fp);
1812 fwrite("\n", 1, 1, new_fp);
1816 fwrite("\n", 1, 1, new_fp);
1818 size = msg->end - msg->content;
1819 fseek(mbox_fp, msg->content, SEEK_SET);
1821 return mbox_write_data(mbox_fp, new_fp, new_filename, size);
1824 void mbox_change_flags(Folder * folder, FolderItem * item, MsgInfo * info, MsgPermFlags newflags)
1826 struct _message * msg;
1830 mbox_path = mbox_folder_get_path(folder, item);
1831 if (mbox_path == NULL)
1834 info->flags.perm_flags = newflags;
1836 msg = mbox_cache_get_msg(mbox_path, info->msgnum);
1838 cache = mbox_cache_get_mbox(mbox_path);
1842 if ((msg == NULL) || (cache == NULL))
1845 msg->flags = info->flags;
1847 cache->modification = TRUE;
1851 static gboolean mbox_purge_deleted(gchar * mbox)
1858 gboolean modification = FALSE;
1862 mbox_cache_synchronize(mbox, TRUE);
1864 msg_list = mbox_cache_get_msg_list(mbox);
1866 for(l = msg_list ; l != NULL ; l = g_list_next(l)) {
1867 struct _message * msg = (struct _message *) l->data;
1868 if (!MSG_IS_INVALID(msg->flags) && MSG_IS_REALLY_DELETED(msg->flags)) {
1869 modification = TRUE;
1874 if (!modification) {
1875 debug_print("no deleted messages - %s\n", mbox);
1879 debug_print("purge deleted messages - %s\n", mbox);
1881 mbox_fp = fopen(mbox, "rb+");
1882 mbox_lockwrite_file(mbox_fp, mbox);
1884 mbox_cache_synchronize_from_file(mbox_fp, mbox, TRUE);
1886 new = g_strconcat(mbox, ".", itos((int) mbox), NULL);
1887 new_fp = fopen(new, "wb");
1889 if (change_file_mode_rw(new_fp, new) < 0) {
1890 FILE_OP_ERROR(new, "chmod");
1891 g_warning("can't change file mode\n");
1894 mbox_lockwrite_file(new_fp, new);
1899 msg_list = mbox_cache_get_msg_list(mbox);
1900 for(l = msg_list ; l != NULL ; l = g_list_next(l)) {
1901 struct _message * msg = (struct _message *) l->data;
1902 if (MSG_IS_INVALID(msg->flags) || !MSG_IS_REALLY_DELETED(msg->flags)) {
1903 if (!mbox_write_message(mbox_fp, new_fp, new, msg)) {
1913 if (rename(new, mbox) == -1) {
1914 g_warning("can't rename %s to %s\n", new, mbox);
1915 mbox_unlock_file(new_fp, new);
1917 mbox_unlock_file(mbox_fp, mbox);
1923 if (change_file_mode_rw(new_fp, mbox) < 0) {
1924 FILE_OP_ERROR(new, "chmod");
1925 g_warning("can't change file mode\n");
1928 mbox_unlock_file(new_fp, new);
1932 mbox_unlock_file(mbox_fp, mbox);
1936 debug_print("%i messages written - %s\n", count, mbox);
1938 mbox_cache_synchronize(mbox, FALSE);
1943 #define MAKE_DIR_IF_NOT_EXIST(dir) \
1945 if (!is_dir_exist(dir)) { \
1946 if (is_file_exist(dir)) { \
1947 g_warning("File `%s' already exists.\n" \
1948 "Can't create folder.", dir); \
1951 if (mkdir(dir, S_IRWXU) < 0) { \
1952 FILE_OP_ERROR(dir, "mkdir"); \
1955 if (chmod(dir, S_IRWXU) < 0) \
1956 FILE_OP_ERROR(dir, "chmod"); \
1960 gint mbox_create_tree(Folder *folder)
1964 g_return_val_if_fail(folder != NULL, -1);
1966 CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), -1);
1967 rootpath = LOCAL_FOLDER(folder)->rootpath;
1968 MAKE_DIR_IF_NOT_EXIST(rootpath);
1969 CHDIR_RETURN_VAL_IF_FAIL(rootpath, -1);
1974 #undef MAKE_DIR_IF_NOT_EXIST
1976 static gchar * mbox_get_new_path(FolderItem * parent, gchar * name)
1980 if (strchr(name, '/') == NULL) {
1981 if (parent->path != NULL)
1982 path = g_strconcat(parent->path, ".sbd", G_DIR_SEPARATOR_S, name, NULL);
1984 path = g_strdup(name);
1987 path = g_strdup(name);
1992 static gchar * mbox_get_folderitem_name(gchar * name)
1996 foldername = g_strdup(g_basename(name));
2001 FolderItem *mbox_create_folder(Folder *folder, FolderItem *parent,
2005 FolderItem *new_item;
2008 g_return_val_if_fail(folder != NULL, NULL);
2009 g_return_val_if_fail(parent != NULL, NULL);
2010 g_return_val_if_fail(name != NULL, NULL);
2012 path = mbox_get_new_path(parent, (gchar *) name);
2014 foldername = mbox_get_folderitem_name((gchar *) name);
2016 new_item = folder_item_new(folder, foldername, path);
2017 folder_item_append(parent, new_item);
2019 if (!strcmp(name, "inbox")) {
2020 new_item->stype = F_INBOX;
2021 new_item->folder->inbox = new_item;
2022 } else if (!strcmp(name, "outbox")) {
2023 new_item->stype = F_OUTBOX;
2024 new_item->folder->outbox = new_item;
2025 } else if (!strcmp(name, "draft")) {
2026 new_item->stype = F_DRAFT;
2027 new_item->folder->draft = new_item;
2028 } else if (!strcmp(name, "queue")) {
2029 new_item->stype = F_QUEUE;
2030 new_item->folder->queue = new_item;
2031 } else if (!strcmp(name, "trash")) {
2032 new_item->stype = F_TRASH;
2033 new_item->folder->trash = new_item;
2042 gint mbox_rename_folder(Folder *folder, FolderItem *item, const gchar *name)
2047 g_return_val_if_fail(folder != NULL, -1);
2048 g_return_val_if_fail(item != NULL, -1);
2049 g_return_val_if_fail(item->path != NULL, -1);
2050 g_return_val_if_fail(name != NULL, -1);
2052 path = mbox_get_new_path(item->parent, (gchar *) name);
2053 foldername = mbox_get_folderitem_name((gchar *) name);
2055 if (rename(item->path, path) == -1) {
2058 g_warning("Cannot rename folder item");
2066 item->name = foldername;
2072 gint mbox_remove_folder(Folder *folder, FolderItem *item)
2074 g_return_val_if_fail(folder != NULL, -1);
2075 g_return_val_if_fail(item != NULL, -1);
2076 g_return_val_if_fail(item->path != NULL, -1);
2078 folder_item_remove(item);
2082 gint mbox_get_num_list(Folder *folder, FolderItem *item, GSList **mlist)
2089 mbox_path = mbox_folder_get_path(folder, item);
2091 if (mbox_path == NULL)
2094 mbox_purge_deleted(mbox_path);
2096 fp = fopen(mbox_path, "rb");
2103 mbox_lockread_file(fp, mbox_path);
2105 mbox_cache_synchronize_from_file(fp, mbox_path, TRUE);
2107 item->last_num = mbox_cache_get_count(mbox_path);
2109 for(l = mbox_cache_get_msg_list(mbox_path) ; l != NULL ;
2110 l = g_list_next(l)) {
2111 struct _message * msg;
2113 msg = (struct _message *) l->data;
2115 if (MSG_IS_INVALID(msg->flags) || !MSG_IS_REALLY_DELETED(msg->flags)) {
2116 *mlist = g_slist_append(*mlist, GINT_TO_POINTER(msg->msgnum));
2119 MSG_SET_PERM_FLAGS(msg->flags, MSG_REALLY_DELETED);
2123 mbox_unlock_file(fp, mbox_path);
2132 MsgInfo *mbox_get_msginfo(Folder *folder, FolderItem *item, gint num)
2135 struct _message *msg;
2139 g_return_val_if_fail(folder != NULL, NULL);
2140 g_return_val_if_fail(item != NULL, NULL);
2142 mbox_path = mbox_folder_get_path(folder, item);
2144 g_return_val_if_fail(mbox_path != NULL, NULL);
2146 src = fopen(mbox_path, "rb");
2151 mbox_lockread_file(src, mbox_path);
2152 mbox_cache_synchronize_from_file(src, mbox_path, TRUE);
2154 msg = mbox_cache_get_msg(mbox_path, num);
2156 mbox_unlock_file(src, mbox_path);
2162 fseek(src, msg->header, SEEK_SET);
2163 msginfo = mbox_parse_msg(src, msg, item);
2165 mbox_unlock_file(src, mbox_path);
2172 gboolean mbox_check_msgnum_validity(Folder *folder, FolderItem *item)
2174 mboxcache * old_cache;
2175 gboolean scan_new = TRUE;
2179 filename = mbox_folder_get_path(folder, item);
2181 old_cache = mbox_cache_get_mbox(filename);
2183 if (old_cache != NULL) {
2184 if (stat(filename, &s) < 0) {
2185 FILE_OP_ERROR(filename, "stat");
2186 } else if (old_cache->mtime == s.st_mtime) {
2187 debug_print("Folder is not modified.\n");