8b601fcf62e7510df54df2de3a998816e8d54fa3
[claws.git] / src / plugins / mailmbox / mailmbox_folder.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2003 Hiroyuki Yamamoto
4  *
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 3 of the License, or
8  * (at your option) any later version.
9  *
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.
14  *
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.
18  */
19
20
21 #ifdef HAVE_CONFIG_H
22 #  include "config.h"
23 #  include "claws-features.h"
24 #endif
25
26 #include <glib.h>
27 #include <glib/gi18n.h>
28
29 #include "defs.h"
30
31 #include <glib.h>
32 #include <glib/gi18n.h>
33 #include <dirent.h>
34 #include <sys/stat.h>
35 #include <unistd.h>
36 #include <string.h>
37 #include <errno.h>
38 #include <sys/types.h>
39 #include <fcntl.h>
40 #include <unistd.h>
41 #include <sys/mman.h>
42
43 #undef MEASURE_TIME
44
45 #ifdef MEASURE_TIME
46 #  include <sys/time.h>
47 #endif
48
49 #include "folder.h"
50 #include "procmsg.h"
51 #include "procheader.h"
52 #include "statusbar.h"
53 #include "utils.h"
54 #include "gtkutils.h"
55 #include "localfolder.h"
56 #include "mailmbox.h"
57 #include "mailmbox_folder.h"
58 #include "mailmbox_parse.h"
59
60 #define MAILMBOX_CACHE_DIR           "mailmboxcache"
61
62 static Folder *s_claws_mailmbox_folder_new(const gchar *name, const gchar *path);
63
64 static void claws_mailmbox_folder_destroy(Folder *folder);
65
66 static FolderItem *claws_mailmbox_folder_item_new(Folder *folder);
67
68 static void claws_mailmbox_folder_item_destroy(Folder *folder, FolderItem *_item);
69
70 static gchar *claws_mailmbox_item_get_path(Folder *folder, FolderItem *item);
71
72 static gint claws_mailmbox_get_num_list(Folder *folder, FolderItem *item,
73     GSList **list, gboolean *old_uids_valid);
74
75 static MsgInfo *claws_mailmbox_get_msginfo(Folder *folder,
76     FolderItem *item, gint num);
77
78 static GSList *claws_mailmbox_get_msginfos(Folder *folder, FolderItem *item,
79     GSList *msgnum_list);
80
81 static gchar *s_claws_mailmbox_fetch_msg(Folder *folder, FolderItem *item, gint num);
82
83 static gint claws_mailmbox_add_msg(Folder *folder, FolderItem *dest,
84     const gchar *file, MsgFlags *flags);
85
86 static gint claws_mailmbox_add_msgs(Folder *folder, FolderItem *dest,
87     GSList *file_list, 
88     GHashTable *relation);
89
90 static gint s_claws_mailmbox_copy_msg(Folder *folder,
91     FolderItem *dest, MsgInfo *msginfo);
92
93 static gint claws_mailmbox_copy_msgs(Folder *folder, FolderItem *dest, 
94     MsgInfoList *msglist, GHashTable *relation);
95
96 static gint claws_mailmbox_remove_msg(Folder *folder, FolderItem *item, gint num);
97 static gint claws_mailmbox_remove_msgs( Folder *folder, FolderItem *item, MsgInfoList *msglist, GHashTable *relation );
98 static gint claws_mailmbox_remove_all_msg(Folder *folder, FolderItem *item);
99
100 static FolderItem *claws_mailmbox_create_folder(Folder *folder, FolderItem *parent,
101     const gchar *name);
102
103 static gboolean claws_mailmbox_scan_required(Folder *folder, FolderItem *_item);
104
105 static gint claws_mailmbox_rename_folder(Folder *folder,
106     FolderItem *item, const gchar *name);
107
108 static gint claws_mailmbox_remove_folder(Folder *folder, FolderItem *item);
109
110 static gint claws_mailmbox_create_tree(Folder *folder);
111
112 static gint claws_mailmbox_folder_item_close(Folder *folder, FolderItem *item);
113
114 static FolderClass claws_mailmbox_class;
115
116 static gchar * get_cache_dir(void)
117 {
118         static gchar *mbox_cache_dir = NULL;
119
120         if (!mbox_cache_dir)
121                 mbox_cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
122                                              MAILMBOX_CACHE_DIR, NULL);
123
124         return mbox_cache_dir;
125 }
126
127
128 FolderClass *claws_mailmbox_get_class(void)
129 {
130         if (claws_mailmbox_class.idstr == NULL) {
131                 claws_mailmbox_class.type = F_MBOX;
132                 claws_mailmbox_class.idstr = "mailmbox";
133                 claws_mailmbox_class.uistr = "mbox";
134
135                 /* Folder functions */
136                 claws_mailmbox_class.new_folder = s_claws_mailmbox_folder_new;
137                 claws_mailmbox_class.destroy_folder = claws_mailmbox_folder_destroy;
138                 claws_mailmbox_class.set_xml = folder_local_set_xml;
139                 claws_mailmbox_class.get_xml = folder_local_get_xml;
140                 claws_mailmbox_class.create_tree = claws_mailmbox_create_tree;
141
142                 /* FolderItem functions */
143                 claws_mailmbox_class.item_new = claws_mailmbox_folder_item_new;
144                 claws_mailmbox_class.item_destroy = claws_mailmbox_folder_item_destroy;
145                 claws_mailmbox_class.item_get_path = claws_mailmbox_item_get_path;
146                 claws_mailmbox_class.create_folder = claws_mailmbox_create_folder;
147                 claws_mailmbox_class.rename_folder = claws_mailmbox_rename_folder;
148                 claws_mailmbox_class.remove_folder = claws_mailmbox_remove_folder;
149                 claws_mailmbox_class.close = claws_mailmbox_folder_item_close;
150                 claws_mailmbox_class.get_num_list = claws_mailmbox_get_num_list;
151                 claws_mailmbox_class.scan_required = claws_mailmbox_scan_required;
152
153                 /* Message functions */
154                 claws_mailmbox_class.get_msginfo = claws_mailmbox_get_msginfo;
155                 claws_mailmbox_class.get_msginfos = claws_mailmbox_get_msginfos;
156                 claws_mailmbox_class.fetch_msg = s_claws_mailmbox_fetch_msg;
157                 claws_mailmbox_class.add_msg = claws_mailmbox_add_msg;
158                 claws_mailmbox_class.add_msgs = claws_mailmbox_add_msgs;
159                 claws_mailmbox_class.copy_msg = s_claws_mailmbox_copy_msg;
160                 claws_mailmbox_class.copy_msgs = claws_mailmbox_copy_msgs;
161                 claws_mailmbox_class.remove_msg = claws_mailmbox_remove_msg;
162                 claws_mailmbox_class.remove_msgs = claws_mailmbox_remove_msgs;
163                 claws_mailmbox_class.remove_all_msg = claws_mailmbox_remove_all_msg;
164         }
165         return &claws_mailmbox_class;
166 }
167
168
169 static void claws_mailmbox_folder_init(Folder *folder,
170     const gchar *name, const gchar *path)
171 {
172         folder_local_folder_init(folder, name, path);
173 }
174
175 static Folder *s_claws_mailmbox_folder_new(const gchar *name, const gchar *path)
176 {
177         Folder *folder;
178
179         folder = (Folder *)g_new0(MAILMBOXFolder, 1);
180         folder->klass = &claws_mailmbox_class;
181         claws_mailmbox_folder_init(folder, name, path);
182         
183         return folder;
184 }
185
186 static void claws_mailmbox_folder_destroy(Folder *folder)
187 {
188         folder_local_folder_destroy(LOCAL_FOLDER(folder));
189 }
190
191 typedef struct _MAILMBOXFolderItem      MAILMBOXFolderItem;
192 struct _MAILMBOXFolderItem
193 {
194         FolderItem item;
195         guint old_max_uid;
196         struct claws_mailmbox_folder * mbox;
197 };
198
199 static FolderItem *claws_mailmbox_folder_item_new(Folder *folder)
200 {
201         MAILMBOXFolderItem *item;
202         
203         item = g_new0(MAILMBOXFolderItem, 1);
204         item->mbox = NULL;
205         item->old_max_uid = 0;
206
207         return (FolderItem *)item;
208 }
209
210 #define MAX_UID_FILE "max-uid"
211
212 static void read_max_uid_value(FolderItem *item, guint * pmax_uid)
213 {
214         gchar * path;
215         gchar * file;
216         FILE * f;
217         guint max_uid;
218         size_t r;
219         
220         path = folder_item_get_path(item);
221         file = g_strconcat(path, G_DIR_SEPARATOR_S, MAX_UID_FILE, NULL);
222         g_free(path);
223         
224         f = fopen(file, "r");
225         g_free(file);
226         if (f == NULL)
227                 return;
228         r = fread(&max_uid, sizeof(max_uid), 1, f);
229         if (r == 0) {
230                 fclose(f);
231                 return;
232         }
233         
234         fclose(f);
235         
236         * pmax_uid = max_uid;
237 }
238
239 static void write_max_uid_value(FolderItem *item, guint max_uid)
240 {
241         gchar * path;
242         gchar * file;
243         FILE * f;
244         size_t r;
245         
246         path = folder_item_get_path(item);
247         file = g_strconcat(path, G_DIR_SEPARATOR_S, MAX_UID_FILE, NULL);
248         g_free(path);
249         
250         f = fopen(file, "w");
251         g_free(file);
252         if (f == NULL)
253                 return;
254         r = fwrite(&max_uid, sizeof(max_uid), 1, f);
255         if (r == 0) {
256                 fclose(f);
257                 return;
258         }
259         
260         fclose(f);
261 }
262
263 static void claws_mailmbox_folder_item_destroy(Folder *folder, FolderItem *_item)
264 {
265         MAILMBOXFolderItem *item = (MAILMBOXFolderItem *)_item;
266
267         g_return_if_fail(item != NULL);
268         
269         if (item->mbox != NULL) {
270                 write_max_uid_value(_item, item->mbox->mb_written_uid);
271                 claws_mailmbox_done(item->mbox);
272         }
273         g_free(_item);
274 }
275
276 static gint claws_mailmbox_folder_item_close(Folder *folder, FolderItem *item_)
277 {
278         MAILMBOXFolderItem *item = (MAILMBOXFolderItem *)item_;
279
280         g_return_val_if_fail(folder->klass->type == F_MBOX, -1);
281         g_return_val_if_fail(item != NULL, -1);
282         g_return_val_if_fail(item->mbox != NULL, -1);
283
284         return -claws_mailmbox_expunge(item->mbox);
285 }
286
287 static void claws_mailmbox_folder_create_parent(const gchar * path)
288 {
289         if (!is_file_exist(path)) {
290                 gchar * new_path;
291
292                 new_path = g_path_get_dirname(path);
293                 if (new_path[strlen(new_path) - 1] == G_DIR_SEPARATOR)
294                         new_path[strlen(new_path) - 1] = '\0';
295
296                 if (!is_dir_exist(new_path))
297                         make_dir_hier(new_path);
298                 g_free(new_path);
299                 
300         }
301 }
302
303 static gchar * claws_mailmbox_folder_get_path(Folder *folder, FolderItem *item)
304 {
305         gchar *folder_path;
306         gchar *path;
307
308         g_return_val_if_fail(item != NULL, NULL);
309
310         if (item->path && item->path[0] == G_DIR_SEPARATOR) {
311                 claws_mailmbox_folder_create_parent(item->path);
312                 return g_strdup(item->path);
313         }
314
315         folder_path = g_strdup(LOCAL_FOLDER(item->folder)->rootpath);
316         g_return_val_if_fail(folder_path != NULL, NULL);
317
318         if (folder_path[0] == G_DIR_SEPARATOR) {
319                 if (item->path) {
320                         path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
321                                            item->path, NULL);
322                 }
323                 else
324                         path = g_strdup(folder_path);
325         } else {
326                 if (item->path)
327                         path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
328                                            folder_path, G_DIR_SEPARATOR_S,
329                                            item->path, NULL);
330                 else
331                         path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
332                                            folder_path, NULL);
333         }
334
335         g_free(folder_path);
336         
337         claws_mailmbox_folder_create_parent(path);
338
339         return path;
340 }
341
342 static int claws_mailmbox_item_sync(FolderItem *_item, int validate_uid)
343 {
344         MAILMBOXFolderItem *item = (MAILMBOXFolderItem *)_item;
345         int r;
346
347         if (item->mbox == NULL) {
348                 guint written_uid;
349                 gchar * path;
350                 
351                 written_uid = 0;
352                 read_max_uid_value(_item, &written_uid);
353                 path = claws_mailmbox_folder_get_path(_item->folder, _item);
354                 r = claws_mailmbox_init(path, 0, 0, written_uid, &item->mbox);
355                 debug_print("init %d: %p\n", r, item->mbox);
356                 g_free(path);
357                 if (r != MAILMBOX_NO_ERROR)
358                         return -1;
359         }
360
361         if (!validate_uid) {
362                 r = claws_mailmbox_validate_read_lock(item->mbox);
363                 if (r != MAILMBOX_NO_ERROR) {
364                         debug_print("read lock: %d\n", r);
365                         goto err;
366                 }
367                 
368                 claws_mailmbox_read_unlock(item->mbox);
369         }
370         else {
371                 r = claws_mailmbox_validate_write_lock(item->mbox);
372                 if (r != MAILMBOX_NO_ERROR) {
373                         debug_print("write lock: %d\n", r);
374                         goto err;
375                 }
376                 
377                 if (item->mbox->mb_written_uid < item->mbox->mb_max_uid) {
378                         r = claws_mailmbox_expunge_no_lock(item->mbox);
379                         if (r != MAILMBOX_NO_ERROR)
380                                 goto unlock;
381                 }
382                 claws_mailmbox_write_unlock(item->mbox);
383         }
384         
385         return 0;
386         
387  unlock:
388         claws_mailmbox_write_unlock(item->mbox);
389  err:
390         return -1;
391 }
392
393 static struct claws_mailmbox_folder * get_mbox(FolderItem *_item, int validate_uid)
394 {
395         MAILMBOXFolderItem *item = (MAILMBOXFolderItem *)_item;
396         
397         claws_mailmbox_item_sync(_item, validate_uid);
398         
399         return item->mbox;
400 }
401
402 static gint claws_mailmbox_get_num_list(Folder *folder, FolderItem *item,
403     GSList **list, gboolean *old_uids_valid)
404 {
405         gint nummsgs = 0;
406         guint i;
407         struct claws_mailmbox_folder * mbox;
408
409         g_return_val_if_fail(item != NULL, -1);
410
411         debug_print("mbox_get_last_num(): Scanning %s ...\n", item->path);
412         
413         *old_uids_valid = TRUE;
414         
415         mbox = get_mbox(item, 1);
416         if (mbox == NULL)
417                 return -1;
418         
419         for(i = 0 ; i < carray_count(mbox->mb_tab) ; i ++) {
420                 struct claws_mailmbox_msg_info * msg;
421                 
422                 msg = carray_get(mbox->mb_tab, i);
423                 if (msg != NULL) {
424                         *list = g_slist_prepend(*list,
425                             GINT_TO_POINTER(msg->msg_uid));
426                         nummsgs ++;
427                 }
428         }
429         
430         return nummsgs;
431 }
432
433 static gchar *s_claws_mailmbox_fetch_msg(Folder *folder, FolderItem *item, gint num)
434 {
435         gchar *path;
436         gchar *file;
437         int r;
438         struct claws_mailmbox_folder * mbox;
439         const char * data;
440         size_t len;
441         FILE * f;
442         mode_t old_mask;
443
444         g_return_val_if_fail(item != NULL, NULL);
445         g_return_val_if_fail(num > 0, NULL);
446         
447         mbox = get_mbox(item, 0);
448         if (mbox == NULL)
449                 return NULL;
450
451         path = folder_item_get_path(item);
452         if (!is_dir_exist(path))
453                 make_dir_hier(path);
454         file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
455         g_free(path);
456         if (is_file_exist(file)) {
457                 return file;
458         }
459         
460         r = claws_mailmbox_fetch_msg(mbox, num, &data, &len);
461         if (r != MAILMBOX_NO_ERROR)
462                 goto free;
463         
464         old_mask = umask(0077);
465         f = fopen(file, "w");
466         umask(old_mask);
467         if (f == NULL)
468                 goto free;
469         
470         r = fwrite(data, 1, len, f);
471         if (r == 0)
472                 goto close;
473         
474         fclose(f);
475         
476         return file;
477         
478  close:
479         fclose(f);
480         unlink(file);
481  free:
482         free(file);
483         return NULL;
484 }
485
486 static MsgInfo *claws_mailmbox_parse_msg(guint uid,
487     const char * data, size_t len, FolderItem *_item)
488 {
489         MsgInfo *msginfo;
490         MsgFlags flags;
491         struct claws_mailmbox_folder * mbox;
492         chashdatum key;
493         chashdatum value;
494         struct claws_mailmbox_msg_info * info;
495         int r;
496         MAILMBOXFolderItem * item = (MAILMBOXFolderItem *)_item;
497         
498         flags.perm_flags = MSG_NEW|MSG_UNREAD;
499         flags.tmp_flags = 0;
500
501         g_return_val_if_fail(item != NULL, NULL);
502         g_return_val_if_fail(data != NULL, NULL);
503
504         if (_item->stype == F_QUEUE) {
505                 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
506         } else if (_item->stype == F_DRAFT) {
507                 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
508         }
509
510         mbox = item->mbox;
511         
512         key.data = (char *) &uid;
513         key.len = sizeof(uid);
514         
515         r = chash_get(mbox->mb_hash, &key, &value);
516         if (r < 0)
517                 return NULL;
518         
519         info = (struct claws_mailmbox_msg_info *) value.data;
520         
521         msginfo = procheader_parse_str(data, flags, FALSE, FALSE);
522         if (!msginfo) return NULL;
523
524         msginfo->msgnum = uid;
525         msginfo->folder = _item;
526         msginfo->size = (goffset)(info->msg_size - info->msg_start_len);
527
528         return msginfo;
529 }
530
531 static MsgInfo *claws_mailmbox_get_msginfo(Folder *folder,
532     FolderItem *item, gint num)
533 {
534         MsgInfo *msginfo;
535         int r;
536         const char * data;
537         size_t len;
538         struct claws_mailmbox_folder * mbox;
539
540         g_return_val_if_fail(item != NULL, NULL);
541         g_return_val_if_fail(num > 0, NULL);
542
543         mbox = get_mbox(item, 0);
544         if (mbox == NULL)
545                 goto err;
546
547         r = claws_mailmbox_validate_read_lock(mbox);
548         if (r != MAILMBOX_NO_ERROR)
549                 goto err;
550         
551         r = claws_mailmbox_fetch_msg_headers_no_lock(mbox, num, &data, &len);
552         if (r != MAILMBOX_NO_ERROR)
553                 goto unlock;
554         
555         msginfo = claws_mailmbox_parse_msg(num, data, len, item);
556         if (!msginfo)
557                 goto unlock;
558
559         claws_mailmbox_read_unlock(mbox);
560
561         return msginfo;
562
563  unlock:
564         claws_mailmbox_read_unlock(mbox);
565  err:
566         return NULL;
567 }
568
569 static GSList *claws_mailmbox_get_msginfos(Folder *folder, FolderItem *item,
570     GSList *msgnum_list)
571 {
572         int r;
573         GSList * cur;
574         GSList * ret;
575         struct claws_mailmbox_folder * mbox;
576         
577         g_return_val_if_fail(item != NULL, NULL);
578
579         mbox = get_mbox(item, 0);
580         if (mbox == NULL)
581                 goto err;
582
583         r = claws_mailmbox_validate_read_lock(mbox);
584         if (r != MAILMBOX_NO_ERROR)
585                 goto err;
586
587         ret = NULL;
588         
589         for (cur = msgnum_list ; cur != NULL ; cur = g_slist_next(cur)) {
590                 const char * data;
591                 size_t len;
592                 gint num;
593                 MsgInfo *msginfo;
594                 
595                 num = GPOINTER_TO_INT(cur->data);
596                 
597                 r = claws_mailmbox_fetch_msg_headers_no_lock(mbox, num, &data, &len);
598                 if (r != MAILMBOX_NO_ERROR)
599                         continue;
600                 
601                 msginfo = claws_mailmbox_parse_msg(num, data, len, item);
602                 if (!msginfo)
603                         continue;
604                 
605                 ret = g_slist_append(ret, msginfo);
606         }
607         
608         claws_mailmbox_read_unlock(mbox);
609
610         return ret;
611
612  err:
613         return NULL;
614 }
615
616 static gint claws_mailmbox_add_msg(Folder *folder, FolderItem *dest,
617     const gchar *file, MsgFlags *flags)
618 {
619         gint ret;
620         GSList file_list;
621         MsgFileInfo fileinfo;
622
623         g_return_val_if_fail(file != NULL, -1);
624
625         fileinfo.msginfo = NULL;
626         fileinfo.file = (gchar *)file;
627         fileinfo.flags = flags;
628         file_list.data = &fileinfo;
629         file_list.next = NULL;
630
631         ret = claws_mailmbox_add_msgs(folder, dest, &file_list, NULL);
632         return ret;
633
634
635 /* ok */
636  
637 static gint claws_mailmbox_add_msgs(Folder *folder, FolderItem *dest,
638     GSList *file_list, 
639     GHashTable *relation)
640
641         GSList *cur;
642         gint last_num;
643         struct claws_mailmbox_folder * mbox;
644         carray * append_list;
645         struct claws_mailmbox_append_info append_info;
646         int r;
647
648         g_return_val_if_fail(dest != NULL, -1);
649         g_return_val_if_fail(file_list != NULL, -1);
650         
651         mbox = get_mbox(dest, 0);
652         if (mbox == NULL) {
653                 debug_print("mbox not found\n");
654                 return -1;
655         }
656         r = claws_mailmbox_validate_write_lock(mbox);
657         if (r != MAILMBOX_NO_ERROR) {
658                 debug_print("claws_mailmbox_validate_write_lock failed with %d\n", r);
659                 return -1;
660         }
661         r = claws_mailmbox_expunge_no_lock(mbox);
662         if (r != MAILMBOX_NO_ERROR) {
663                 debug_print("claws_mailmbox_expunge_no_lock failed with %d\n", r);
664                 goto unlock;
665         }
666
667         last_num = -1;
668
669         append_list = carray_new(1);
670         if (append_list == NULL) {
671                 debug_print("append_list is null\n");
672                 goto unlock;
673         }
674
675         r = carray_set_size(append_list, 1);
676         if (r < 0) {
677                 debug_print("carray_set_size failed with %d\n", r);
678                 goto free;
679         }
680
681         carray_set(append_list, 0, &append_info);
682         
683         for (cur = file_list; cur != NULL; cur = cur->next) {
684                 int fd;
685                 struct stat stat_info;
686                 char * data;
687                 size_t len;
688                 struct claws_mailmbox_msg_info * msg;
689                 size_t cur_token;
690                 MsgFileInfo *fileinfo;
691                 
692                 fileinfo = (MsgFileInfo *)cur->data;
693                 
694                 fd = open(fileinfo->file, O_RDONLY);
695                 if (fd == -1) {
696                         debug_print("%s couldn't be opened\n", fileinfo->file);
697                         goto err;
698                 }
699
700                 r = fstat(fd, &stat_info);
701                 if (r < 0) {
702                         debug_print("%s couldn't be stat'ed\n", fileinfo->file);
703                         goto close;
704                 }
705
706                 len = stat_info.st_size;
707                 data = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
708                 if (data == MAP_FAILED) {
709                         debug_print("mmap failed\n");
710                         goto close;
711                 }
712
713                 append_info.ai_message = data;
714                 append_info.ai_size = len;
715                 
716                 cur_token = mbox->mb_mapping_size;
717                 
718                 r = claws_mailmbox_append_message_list_no_lock(mbox, append_list);
719                 if (r != MAILMBOX_NO_ERROR) {
720                         debug_print("claws_mailmbox_append_message_list_no_lock failed with %d\n", r);
721                         goto unmap;
722                 }
723
724                 munmap(data, len);
725                 close(fd);
726
727                 r = claws_mailmbox_parse_additionnal(mbox, &cur_token);
728                 if (r != MAILMBOX_NO_ERROR) {
729                         debug_print("claws_mailmbox_parse_additionnal failed with %d\n", r);
730                         goto unlock;
731                 }
732
733                 msg = carray_get(mbox->mb_tab, carray_count(mbox->mb_tab) - 1);
734
735                 if (relation != NULL)
736                         g_hash_table_insert(relation,
737                             fileinfo->msginfo != NULL ?
738                             (gpointer) fileinfo->msginfo :
739                             (gpointer) fileinfo,
740                             GINT_TO_POINTER(msg->msg_uid));
741                 
742                 last_num = msg->msg_uid;
743                 
744                 continue;
745                 
746         unmap:
747                 munmap(data, len);
748         close:
749                 close(fd);
750         err:
751                 continue;
752         }
753
754         claws_mailmbox_sync(mbox);
755
756         carray_free(append_list);
757         claws_mailmbox_write_unlock(mbox);
758         
759         return last_num;
760
761  free:
762         carray_free(append_list);
763  unlock:
764         claws_mailmbox_write_unlock(mbox);
765 return -1;
766 }
767
768 static gint s_claws_mailmbox_copy_msg(Folder *folder,
769     FolderItem *dest, MsgInfo *msginfo)
770 {
771         GSList msglist;
772
773         g_return_val_if_fail(msginfo != NULL, -1);
774
775         msglist.data = msginfo;
776         msglist.next = NULL;
777
778         return claws_mailmbox_copy_msgs(folder, dest, &msglist, NULL);
779 }
780
781 static gint claws_mailmbox_copy_msgs(Folder *folder, FolderItem *dest, 
782     MsgInfoList *msglist, GHashTable *relation)
783 {
784         MsgInfo *msginfo;
785         GSList *file_list;
786         gint ret;
787         
788         g_return_val_if_fail(folder != NULL, -1);
789         g_return_val_if_fail(dest != NULL, -1);
790         g_return_val_if_fail(msglist != NULL, -1);
791
792         msginfo = (MsgInfo *)msglist->data;
793         g_return_val_if_fail(msginfo->folder != NULL, -1);
794
795         file_list = procmsg_get_message_file_list(msglist);
796         g_return_val_if_fail(file_list != NULL, -1);
797
798         ret = claws_mailmbox_add_msgs(folder, dest, file_list, relation);
799
800         procmsg_message_file_list_free(file_list);
801
802         return ret;
803 }
804
805
806 static gint claws_mailmbox_remove_msg(Folder *folder, FolderItem *item, gint num)
807 {
808         struct claws_mailmbox_folder * mbox;
809         int r;
810         
811         g_return_val_if_fail(item != NULL, -1);
812         
813         mbox = get_mbox(item, 0);
814         if (mbox == NULL)
815                 return -1;
816         
817         r = claws_mailmbox_delete_msg(mbox, num);
818         if (r != MAILMBOX_NO_ERROR)
819                 return -1;
820
821         return 0;
822 }
823
824 static gint
825 claws_mailmbox_remove_msgs( Folder *folder, FolderItem *item,
826                             MsgInfoList *msglist, GHashTable *relation )
827 {
828     struct claws_mailmbox_folder *mbox;
829     int r;
830     gint total = 0, curnum = 0;
831
832     g_return_val_if_fail( item!=NULL, -1 );
833     mbox=get_mbox(item,0);
834     g_return_val_if_fail( mbox!=NULL, -1 );
835
836     total = g_slist_length(msglist);
837     if (total > 100) {
838         statusbar_print_all(_("Deleting messages..."));
839     }
840
841     MsgInfoList *cur;
842     for( cur=msglist; cur; cur=cur->next )
843     {
844        MsgInfo *msginfo=(MsgInfo*) cur->data;
845        if( !msginfo )
846        {
847            continue;
848        }
849        if( MSG_IS_MOVE(msginfo->flags) && MSG_IS_MOVE_DONE(msginfo->flags) )
850        {
851            msginfo->flags.tmp_flags&=~MSG_MOVE_DONE;
852            continue;
853        }
854        if (total > 100) {
855                 statusbar_progress_all(curnum, total, 100);
856                 if (curnum % 100 == 0)
857                         GTK_EVENTS_FLUSH();
858                 curnum++;
859        }
860       claws_mailmbox_delete_msg(mbox,msginfo->msgnum);
861     }
862
863     /* Fix for bug 1434
864      */
865     r = claws_mailmbox_expunge(mbox);
866     if (total > 100) {
867                 statusbar_progress_all(0,0,0);
868                 statusbar_pop_all();
869     }
870
871     return r;
872 }
873
874
875 static gint claws_mailmbox_remove_all_msg(Folder *folder, FolderItem *item)
876 {
877         struct claws_mailmbox_folder * mbox;
878         int r;
879         guint i;
880         
881         g_return_val_if_fail(item != NULL, -1);
882         
883         mbox = get_mbox(item, 0);
884         if (mbox == NULL)
885                 return -1;
886        
887         for(i = 0 ; i < carray_count(mbox->mb_tab) ; i ++) {
888                 struct claws_mailmbox_msg_info * msg;
889                 
890                 msg = carray_get(mbox->mb_tab, i);
891                 if (msg == NULL)
892                         continue;
893                 
894                 r = claws_mailmbox_delete_msg(mbox, msg->msg_uid);
895                 if (r != MAILMBOX_NO_ERROR)
896                         continue;
897         }
898         
899         return 0;
900 }
901
902
903 static gchar * claws_mailmbox_get_new_path(FolderItem * parent, gchar * name)
904 {
905         gchar * path;
906
907         if (strchr(name, G_DIR_SEPARATOR) == NULL) {
908                 if (parent->path != NULL)
909                         path = g_strconcat(parent->path, ".sbd", G_DIR_SEPARATOR_S, name, NULL);
910                 else
911                         path = g_strdup(name);
912         }
913         else
914                 path = g_strdup(name);
915
916         return path;
917 }
918
919 static gchar * claws_mailmbox_get_folderitem_name(gchar * name)
920 {
921         gchar * foldername;
922
923         foldername = g_path_get_basename(name);
924         
925         return foldername;
926 }
927
928 static FolderItem *claws_mailmbox_create_folder(Folder *folder, FolderItem *parent,
929     const gchar *name)
930 {
931         gchar * path;
932         FolderItem *new_item;
933         gchar * foldername;
934
935         g_return_val_if_fail(folder != NULL, NULL);
936         g_return_val_if_fail(parent != NULL, NULL);
937         g_return_val_if_fail(name != NULL, NULL);
938
939         path = claws_mailmbox_get_new_path(parent, (gchar *) name);
940
941         foldername = claws_mailmbox_get_folderitem_name((gchar *) name);
942
943         new_item = folder_item_new(folder, foldername, path);
944         folder_item_append(parent, new_item);
945
946         if (!strcmp(name, "inbox")) {
947                 new_item->stype = F_INBOX;
948                 new_item->folder->inbox = new_item;
949         } else if (!strcmp(name, "outbox")) {
950                 new_item->stype = F_OUTBOX;
951                 new_item->folder->outbox = new_item;
952         } else if (!strcmp(name, "draft")) {
953                 new_item->stype = F_DRAFT;
954                 new_item->folder->draft = new_item;
955         } else if (!strcmp(name, "queue")) {
956                 new_item->stype = F_QUEUE;
957                 new_item->folder->queue = new_item;
958         } else if (!strcmp(name, "trash")) {
959                 new_item->stype = F_TRASH;
960                 new_item->folder->trash = new_item;
961         }
962         
963         g_free(foldername);
964         g_free(path);
965         
966         return new_item;
967 }
968
969
970
971 static gboolean claws_mailmbox_scan_required(Folder *folder, FolderItem *_item)
972 {
973         struct claws_mailmbox_folder * mbox;
974         MAILMBOXFolderItem *item = (MAILMBOXFolderItem *)_item;
975         int scan_required;
976         
977         g_return_val_if_fail(folder != NULL, FALSE);
978         g_return_val_if_fail(item != NULL, FALSE);
979
980         if (item->item.path == NULL)
981                 return FALSE;
982         
983         mbox = get_mbox(_item, 0);
984         if (mbox == NULL)
985                 return FALSE;
986         
987         scan_required = (item->old_max_uid != item->mbox->mb_max_uid);
988         
989         item->old_max_uid = item->mbox->mb_max_uid;
990
991         return scan_required;
992 }
993
994
995 static gint claws_mailmbox_rename_folder(Folder *folder,
996     FolderItem *item, const gchar *name)
997 {
998         gchar * path;
999         gchar * foldername;
1000         FolderItem *parent;
1001
1002         g_return_val_if_fail(folder != NULL, -1);
1003         g_return_val_if_fail(item != NULL, -1);
1004         g_return_val_if_fail(item->path != NULL, -1);
1005         g_return_val_if_fail(name != NULL, -1);
1006         
1007         parent = folder_item_parent(item);
1008         g_return_val_if_fail(parent, -1);
1009         
1010         path = claws_mailmbox_get_new_path(parent, (gchar *) name);
1011         foldername = claws_mailmbox_get_folderitem_name((gchar *) name);
1012
1013         if (rename(item->path, path) == -1) {
1014                 g_free(foldername);
1015                 g_free(path);
1016                 debug_print("Cannot rename folder item\n");
1017
1018                 return -1;
1019         }
1020         else {
1021                 g_free(item->name);
1022                 g_free(item->path);
1023                 item->path = path;
1024                 item->name = foldername;
1025                 
1026                 return 0;
1027         }
1028 }
1029
1030 static gint claws_mailmbox_remove_folder(Folder *folder, FolderItem *item)
1031 {
1032         g_return_val_if_fail(folder != NULL, -1);
1033         g_return_val_if_fail(item != NULL, -1);
1034         g_return_val_if_fail(item->path != NULL, -1);
1035
1036         folder_item_remove(item);
1037         return 0;
1038 }
1039
1040 #define MAKE_DIR_IF_NOT_EXIST(dir) \
1041 { \
1042         if (!is_dir_exist(dir)) { \
1043                 if (is_file_exist(dir)) { \
1044                         debug_print("File `%s' already exists.\n" \
1045                                     "Can't create folder.", dir); \
1046                         return -1; \
1047                 } \
1048                 if (mkdir(dir, S_IRWXU) < 0) { \
1049                         FILE_OP_ERROR(dir, "mkdir"); \
1050                         return -1; \
1051                 } \
1052                 if (chmod(dir, S_IRWXU) < 0) \
1053                         FILE_OP_ERROR(dir, "chmod"); \
1054         } \
1055 }
1056
1057 static gint claws_mailmbox_create_tree(Folder *folder)
1058 {
1059         gchar *rootpath;
1060
1061         g_return_val_if_fail(folder != NULL, -1);
1062
1063         CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), -1);
1064         rootpath = LOCAL_FOLDER(folder)->rootpath;
1065         MAKE_DIR_IF_NOT_EXIST(rootpath);
1066         CHDIR_RETURN_VAL_IF_FAIL(rootpath, -1);
1067
1068         return 0;
1069 }
1070
1071 #undef MAKE_DIR_IF_NOT_EXIST
1072
1073
1074 static char * quote_mailbox(char * mb)
1075 {
1076         char path[PATH_MAX];
1077         char * str;
1078         size_t remaining;
1079         char * p;
1080         
1081         remaining = sizeof(path) - 1;
1082         p = path;
1083
1084         while (* mb != 0) {
1085                 
1086                 if (((* mb >= 'a') && (* mb <= 'z')) ||
1087                     ((* mb >= 'A') && (* mb <= 'Z')) ||
1088                     ((* mb >= '0') && (* mb <= '9'))) {
1089                         if (remaining < 1)
1090                                 return NULL;
1091                         * p = * mb;
1092                         p ++;
1093                         remaining --;
1094                 }
1095                 else {
1096                         if (remaining < 3)
1097                                 return NULL;
1098                         * p = '%';
1099                         p ++;
1100                         snprintf(p, 3, "%02x", (unsigned char) (* mb));
1101                         p += 2;
1102                 }
1103                 mb ++;
1104         }
1105         
1106         * p = 0;
1107
1108         str = strdup(path);
1109         if (str == NULL)
1110                 return NULL;
1111         
1112         return str;
1113 }
1114
1115 static gchar *claws_mailmbox_item_get_path(Folder *folder, FolderItem *item)
1116 {
1117         gchar *itempath, *path;
1118         gchar * folderpath;
1119
1120         if (item->path == NULL)
1121                 return NULL;
1122
1123         if (folder->name == NULL)
1124                 return NULL;
1125
1126         folderpath = quote_mailbox(folder->name);
1127         if (folderpath == NULL)  
1128                 return NULL;
1129         itempath = quote_mailbox(item->path);
1130         if (itempath == NULL) {
1131                 free(folderpath);
1132                 return NULL;
1133         }
1134         path = g_strconcat(get_cache_dir(),
1135             G_DIR_SEPARATOR_S, folderpath,
1136             G_DIR_SEPARATOR_S, itempath, NULL);  
1137         free(itempath);
1138         free(folderpath);
1139         
1140         return path;
1141 }