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