2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2015 Hiroyuki Yamamoto and the Claws Mail 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 3 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, see <http://www.gnu.org/licenses/>.
22 #include "claws-features.h"
34 #include <glib/gi18n.h>
51 #include "prefs_common.h"
52 #include "prefs_account.h"
55 #include "filtering.h"
56 #include "alertpanel.h"
57 #include "statusbar.h"
58 #include "file-utils.h"
60 #define MESSAGEBUFSIZE 8192
62 #define FPUTS_TO_TMP_ABORT_IF_FAIL(s) \
65 if (claws_fputs(s, tmp_fp) == EOF) { \
66 g_warning("can't write to temporary file"); \
67 claws_fclose(tmp_fp); \
68 claws_fclose(mbox_fp); \
69 claws_unlink(tmp_file); \
75 gint proc_mbox(FolderItem *dest, const gchar *mbox, gboolean apply_filter,
76 PrefsAccount *account)
77 /* return values: -1 error, >=0 number of msgs added */
80 gchar buf[MESSAGEBUFSIZE];
86 GSList *to_filter = NULL, *filtered = NULL, *unfiltered = NULL, *cur, *to_add = NULL;
87 gboolean printed = FALSE;
88 FolderItem *dropfolder;
90 cm_return_val_if_fail(dest != NULL, -1);
91 cm_return_val_if_fail(mbox != NULL, -1);
93 debug_print("Getting messages from %s into %s...\n", mbox, dest->path);
95 if ((mbox_fp = claws_fopen(mbox, "rb")) == NULL) {
96 FILE_OP_ERROR(mbox, "claws_fopen");
97 alertpanel_error(_("Could not open mbox file:\n%s\n"), mbox);
101 /* ignore empty lines on the head */
103 if (claws_fgets(buf, sizeof(buf), mbox_fp) == NULL) {
104 g_warning("can't read mbox file.");
105 claws_fclose(mbox_fp);
108 } while (buf[0] == '\n' || buf[0] == '\r');
110 if (strncmp(buf, "From ", 5) != 0) {
111 g_warning("invalid mbox format: %s", mbox);
112 claws_fclose(mbox_fp);
116 tmp_file = get_tmp_file();
118 folder_item_update_freeze();
121 dropfolder = folder_get_default_processing(account->account_id);
130 if (msgs > 0 && msgs%500 == 0) {
134 ngettext("Importing from mbox... (%d mail imported)",
135 "Importing from mbox... (%d mails imported)", msgs), msgs);
140 if ((tmp_fp = claws_fopen(tmp_file, "wb")) == NULL) {
141 FILE_OP_ERROR(tmp_file, "claws_fopen");
142 g_warning("can't open temporary file");
143 claws_fclose(mbox_fp);
147 if (change_file_mode_rw(tmp_fp, tmp_file) < 0) {
148 FILE_OP_ERROR(tmp_file, "chmod");
154 /* process all lines from mboxrc file */
155 while (claws_fgets(buf, sizeof(buf), mbox_fp) != NULL) {
158 /* eat empty lines */
159 if (buf[0] == '\n' || buf[0] == '\r') {
164 /* From separator or quoted From */
166 /* detect leading '>' char(s) */
167 while (buf[offset] == '>') {
170 if (!strncmp(buf+offset, "From ", 5)) {
171 /* From separator: */
173 /* expect next mbox item */
178 /* flush any eaten empty line */
179 if (empty_lines > 0) {
180 while (empty_lines-- > 0) {
181 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
185 /* store the unquoted line */
186 FPUTS_TO_TMP_ABORT_IF_FAIL(buf + 1);
191 /* flush any eaten empty line */
192 if (empty_lines > 0) {
193 while (empty_lines-- > 0) {
194 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
198 /* store the line itself */
199 FPUTS_TO_TMP_ABORT_IF_FAIL(buf);
201 /* end of mbox item or end of mbox */
203 /* flush any eaten empty line (but the last one) */
204 if (empty_lines > 0) {
205 while (--empty_lines > 0) {
206 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
210 /* more emails to expect? */
211 more = !claws_feof(mbox_fp);
213 /* warn if email part is empty (it's the minimum check
216 g_warning("malformed mbox: %s: message %d is empty", mbox, msgs);
217 claws_fclose(tmp_fp);
218 claws_fclose(mbox_fp);
219 claws_unlink(tmp_file);
223 if (claws_safe_fclose(tmp_fp) == EOF) {
224 FILE_OP_ERROR(tmp_file, "claws_fclose");
225 g_warning("can't write to temporary file");
226 claws_fclose(mbox_fp);
227 claws_unlink(tmp_file);
233 if ((msgnum = folder_item_add_msg(dropfolder, tmp_file, NULL, TRUE)) < 0) {
234 claws_fclose(mbox_fp);
235 claws_unlink(tmp_file);
239 msginfo = folder_item_get_msginfo(dropfolder, msgnum);
240 to_filter = g_slist_prepend(to_filter, msginfo);
242 MsgFileInfo *finfo = g_new0(MsgFileInfo, 1);
243 finfo->file = tmp_file;
245 to_add = g_slist_prepend(to_add, finfo);
246 tmp_file = get_tmp_file();
248 /* flush every 500 */
249 if (msgs > 0 && msgs % 500 == 0) {
250 folder_item_add_msgs(dropfolder, to_add, TRUE);
251 procmsg_message_file_list_free(to_add);
263 folder_item_set_batch(dropfolder, FALSE);
264 procmsg_msglist_filter(to_filter, account,
265 &filtered, &unfiltered, TRUE);
266 folder_item_set_batch(dropfolder, TRUE);
268 filtering_move_and_copy_msgs(to_filter);
269 for (cur = filtered; cur; cur = g_slist_next(cur)) {
270 MsgInfo *info = (MsgInfo *)cur->data;
271 procmsg_msginfo_free(&info);
274 unfiltered = g_slist_reverse(unfiltered);
276 folder_item_move_msgs(dest, unfiltered);
277 for (cur = unfiltered; cur; cur = g_slist_next(cur)) {
278 MsgInfo *info = (MsgInfo *)cur->data;
279 procmsg_msginfo_free(&info);
283 g_slist_free(unfiltered);
284 g_slist_free(filtered);
285 g_slist_free(to_filter);
287 folder_item_add_msgs(dropfolder, to_add, TRUE);
288 procmsg_message_file_list_free(to_add);
292 folder_item_update_thaw();
295 claws_fclose(mbox_fp);
296 debug_print("%d messages found.\n", msgs);
301 gint lock_mbox(const gchar *base, LockType type)
306 if (type == LOCK_FILE) {
307 gchar *lockfile, *locklink;
311 lockfile = g_strdup_printf("%s.%d", base, getpid());
312 if ((lockfp = claws_fopen(lockfile, "wb")) == NULL) {
313 FILE_OP_ERROR(lockfile, "claws_fopen");
314 g_warning("can't create lock file '%s', use 'flock' instead of 'file' if possible.", lockfile);
319 if (fprintf(lockfp, "%d\n", getpid()) < 0) {
320 FILE_OP_ERROR(lockfile, "fprintf");
322 claws_fclose(lockfp);
326 if (claws_safe_fclose(lockfp) == EOF) {
327 FILE_OP_ERROR(lockfile, "claws_fclose");
332 locklink = g_strconcat(base, ".lock", NULL);
333 while (link(lockfile, locklink) < 0) {
334 FILE_OP_ERROR(lockfile, "link");
336 g_warning("can't create '%s'", lockfile);
337 claws_unlink(lockfile);
342 g_warning("mailbox is owned by another"
343 " process, waiting...");
347 claws_unlink(lockfile);
349 } else if (type == LOCK_FLOCK) {
351 gboolean fcntled = FALSE;
352 #if HAVE_FCNTL_H && !defined(G_OS_WIN32)
355 fl.l_whence = SEEK_SET;
361 if ((lockfd = g_open(base, O_RDWR, 0)) < 0) {
363 if ((lockfd = g_open(base, O_RDWR, 0)) < 0) {
365 FILE_OP_ERROR(base, "open");
369 #if HAVE_FCNTL_H && !defined(G_OS_WIN32)
370 if (fcntl(lockfd, F_SETLK, &fl) == -1) {
371 g_warning("can't fnctl %s (%s)", base, g_strerror(errno));
380 if (flock(lockfd, LOCK_EX|LOCK_NB) < 0 && !fcntled) {
384 if (lockf(lockfd, F_TLOCK, 0) < 0 && !fcntled) {
389 #endif /* HAVE_FLOCK */
390 g_warning("can't lock %s", base);
391 if (close(lockfd) < 0)
397 g_warning("invalid lock type");
404 #endif /* G_OS_UNIX */
407 gint unlock_mbox(const gchar *base, gint fd, LockType type)
409 if (type == LOCK_FILE) {
412 lockfile = g_strconcat(base, ".lock", NULL);
413 if (claws_unlink(lockfile) < 0) {
414 FILE_OP_ERROR(lockfile, "unlink");
421 } else if (type == LOCK_FLOCK) {
422 #if HAVE_FCNTL_H && !defined(G_OS_WIN32)
423 gboolean fcntled = FALSE;
426 fl.l_whence = SEEK_SET;
430 if (fcntl(fd, F_SETLK, &fl) == -1) {
431 g_warning("can't fnctl %s", base);
437 if (flock(fd, LOCK_UN) < 0 && !fcntled) {
441 if (lockf(fd, F_ULOCK, 0) < 0 && !fcntled) {
446 #endif /* HAVE_FLOCK */
447 g_warning("can't unlock %s", base);
461 g_warning("invalid lock type");
465 gint copy_mbox(gint srcfd, const gchar *dest)
470 gboolean err = FALSE;
477 if ((dest_fp = claws_fopen(dest, "wb")) == NULL) {
478 FILE_OP_ERROR(dest, "claws_fopen");
482 if (change_file_mode_rw(dest_fp, dest) < 0) {
483 FILE_OP_ERROR(dest, "chmod");
484 g_warning("can't change file mode");
487 while ((n_read = read(srcfd, buf, sizeof(buf))) > 0) {
488 if (claws_fwrite(buf, 1, n_read, dest_fp) < n_read) {
489 g_warning("writing to %s failed.", dest);
490 claws_fclose(dest_fp);
496 if (save_errno != 0) {
497 g_warning("error %d reading mbox: %s", save_errno,
498 g_strerror(save_errno));
502 if (claws_safe_fclose(dest_fp) == EOF) {
503 FILE_OP_ERROR(dest, "claws_fclose");
515 void empty_mbox(const gchar *mbox)
519 if ((fp = claws_fopen(mbox, "wb")) == NULL) {
520 FILE_OP_ERROR(mbox, "claws_fopen");
521 g_warning("can't truncate mailbox to zero.");
524 claws_safe_fclose(fp);
527 gint export_list_to_mbox(GSList *mlist, const gchar *mbox)
528 /* return values: -2 skipped, -1 error, 0 OK */
537 gint msgs = 1, total = g_slist_length(mlist);
538 if (g_file_test(mbox, G_FILE_TEST_EXISTS) == TRUE) {
539 if (alertpanel_full(_("Overwrite mbox file"),
540 _("This file already exists. Do you want to overwrite it?"),
541 GTK_STOCK_CANCEL, _("Overwrite"), NULL,
542 ALERTFOCUS_FIRST, FALSE, NULL, ALERT_WARNING)
543 != G_ALERTALTERNATE) {
548 if ((mbox_fp = claws_fopen(mbox, "wb")) == NULL) {
549 FILE_OP_ERROR(mbox, "claws_fopen");
550 alertpanel_error(_("Could not create mbox file:\n%s\n"), mbox);
554 statusbar_print_all(_("Exporting to mbox..."));
555 for (cur = mlist; cur != NULL; cur = cur->next) {
557 gchar buft[BUFFSIZE];
558 msginfo = (MsgInfo *)cur->data;
560 msg_fp = procmsg_open_message(msginfo);
566 msginfo->from ? msginfo->from :
567 cur_account && cur_account->address ?
568 cur_account->address : "unknown",
570 extract_address(buf);
572 if (fprintf(mbox_fp, "From %s %s",
573 buf, ctime_r(&msginfo->date_t, buft)) < 0) {
575 claws_fclose(msg_fp);
581 /* write email to mboxrc */
582 while (claws_fgets(buf, sizeof(buf), msg_fp) != NULL) {
583 /* quote any From, >From, >>From, etc., according to mbox format specs */
587 /* detect leading '>' char(s) */
588 while (buf[offset] == '>') {
591 if (!strncmp(buf+offset, "From ", 5)) {
592 if (claws_fputc('>', mbox_fp) == EOF) {
594 claws_fclose(msg_fp);
598 if (claws_fputs(buf, mbox_fp) == EOF) {
600 claws_fclose(msg_fp);
605 /* force last line to end w/ a newline */
609 if ((buf[len] != '\n') && (buf[len] != '\r')) {
610 if (claws_fputc('\n', mbox_fp) == EOF) {
612 claws_fclose(msg_fp);
618 /* add a trailing empty line */
619 if (claws_fputc('\n', mbox_fp) == EOF) {
621 claws_fclose(msg_fp);
625 claws_safe_fclose(msg_fp);
626 statusbar_progress_all(msgs++,total, 500);
632 statusbar_progress_all(0,0,0);
635 claws_safe_fclose(mbox_fp);
640 /* read all messages in SRC, and store them into one MBOX file. */
641 /* return values: -2 skipped, -1 error, 0 OK */
642 gint export_to_mbox(FolderItem *src, const gchar *mbox)
647 cm_return_val_if_fail(src != NULL, -1);
648 cm_return_val_if_fail(src->folder != NULL, -1);
649 cm_return_val_if_fail(mbox != NULL, -1);
651 debug_print("Exporting messages from %s into %s...\n",
654 mlist = folder_item_get_msg_list(src);
656 folder_item_update_freeze();
657 ret = export_list_to_mbox(mlist, mbox);
658 folder_item_update_thaw();
660 procmsg_msg_list_free(mlist);