2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2020 the Claws Mail team and Hiroyuki Yamamoto
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 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
;
91 cm_return_val_if_fail(dest
!= NULL
, -1);
92 cm_return_val_if_fail(mbox
!= NULL
, -1);
94 debug_print("Getting messages from %s into %s...\n", mbox
, dest
->path
);
96 if (g_stat(mbox
, &src_stat
) < 0) {
97 FILE_OP_ERROR(mbox
, "g_stat");
98 alertpanel_error(_("Could not stat mbox file:\n%s\n"), mbox
);
102 if ((mbox_fp
= claws_fopen(mbox
, "rb")) == NULL
) {
103 FILE_OP_ERROR(mbox
, "claws_fopen");
104 alertpanel_error(_("Could not open mbox file:\n%s\n"), mbox
);
108 /* ignore empty lines on the head */
110 if (claws_fgets(buf
, sizeof(buf
), mbox_fp
) == NULL
) {
111 g_warning("can't read mbox file.");
112 claws_fclose(mbox_fp
);
115 } while (buf
[0] == '\n' || buf
[0] == '\r');
117 if (strncmp(buf
, "From ", 5) != 0) {
118 g_warning("invalid mbox format: %s", mbox
);
119 claws_fclose(mbox_fp
);
123 tmp_file
= get_tmp_file();
125 folder_item_update_freeze();
128 dropfolder
= folder_get_default_processing(account
->account_id
);
138 long cur_offset_mb
= ftell(mbox_fp
) / (1024 * 1024);
142 ngettext("Importing from mbox... (%ld MB imported)",
143 "Importing from mbox... (%ld MB imported)", cur_offset_mb
), cur_offset_mb
);
144 statusbar_progress_all(cur_offset_mb
, src_stat
.st_size
/ (1024*1024), 1);
149 if ((tmp_fp
= claws_fopen(tmp_file
, "wb")) == NULL
) {
150 FILE_OP_ERROR(tmp_file
, "claws_fopen");
151 g_warning("can't open temporary file");
152 claws_fclose(mbox_fp
);
156 if (change_file_mode_rw(tmp_fp
, tmp_file
) < 0) {
157 FILE_OP_ERROR(tmp_file
, "chmod");
163 /* process all lines from mboxrc file */
164 while (claws_fgets(buf
, sizeof(buf
), mbox_fp
) != NULL
) {
167 /* eat empty lines */
168 if (buf
[0] == '\n' || buf
[0] == '\r') {
173 /* From separator or quoted From */
175 /* detect leading '>' char(s) */
176 while (buf
[offset
] == '>') {
179 if (!strncmp(buf
+offset
, "From ", 5)) {
180 /* From separator: */
182 /* expect next mbox item */
187 /* flush any eaten empty line */
188 if (empty_lines
> 0) {
189 while (empty_lines
-- > 0) {
190 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
194 /* store the unquoted line */
195 FPUTS_TO_TMP_ABORT_IF_FAIL(buf
+ 1);
200 /* flush any eaten empty line */
201 if (empty_lines
> 0) {
202 while (empty_lines
-- > 0) {
203 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
207 /* store the line itself */
208 FPUTS_TO_TMP_ABORT_IF_FAIL(buf
);
210 /* end of mbox item or end of mbox */
212 /* flush any eaten empty line (but the last one) */
213 if (empty_lines
> 0) {
214 while (--empty_lines
> 0) {
215 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
219 /* more emails to expect? */
220 more
= !claws_feof(mbox_fp
);
222 /* warn if email part is empty (it's the minimum check
225 g_warning("malformed mbox: %s: message %d is empty", mbox
, msgs
);
226 claws_fclose(tmp_fp
);
227 claws_fclose(mbox_fp
);
228 claws_unlink(tmp_file
);
232 if (claws_safe_fclose(tmp_fp
) == EOF
) {
233 FILE_OP_ERROR(tmp_file
, "claws_fclose");
234 g_warning("can't write to temporary file");
235 claws_fclose(mbox_fp
);
236 claws_unlink(tmp_file
);
242 if ((msgnum
= folder_item_add_msg(dropfolder
, tmp_file
, NULL
, TRUE
)) < 0) {
243 claws_fclose(mbox_fp
);
244 claws_unlink(tmp_file
);
248 msginfo
= folder_item_get_msginfo(dropfolder
, msgnum
);
249 to_filter
= g_slist_prepend(to_filter
, msginfo
);
251 MsgFileInfo
*finfo
= g_new0(MsgFileInfo
, 1);
252 finfo
->file
= tmp_file
;
254 to_add
= g_slist_prepend(to_add
, finfo
);
255 tmp_file
= get_tmp_file();
257 /* flush every 500 */
258 if (msgs
> 0 && msgs
% 500 == 0) {
259 folder_item_add_msgs(dropfolder
, to_add
, TRUE
);
260 procmsg_message_file_list_free(to_add
);
269 statusbar_progress_all(0, 0, 0);
274 folder_item_set_batch(dropfolder
, FALSE
);
275 procmsg_msglist_filter(to_filter
, account
,
276 &filtered
, &unfiltered
, TRUE
);
277 folder_item_set_batch(dropfolder
, TRUE
);
279 filtering_move_and_copy_msgs(to_filter
);
280 for (cur
= filtered
; cur
; cur
= g_slist_next(cur
)) {
281 MsgInfo
*info
= (MsgInfo
*)cur
->data
;
282 procmsg_msginfo_free(&info
);
285 unfiltered
= g_slist_reverse(unfiltered
);
287 folder_item_move_msgs(dest
, unfiltered
);
288 for (cur
= unfiltered
; cur
; cur
= g_slist_next(cur
)) {
289 MsgInfo
*info
= (MsgInfo
*)cur
->data
;
290 procmsg_msginfo_free(&info
);
294 g_slist_free(unfiltered
);
295 g_slist_free(filtered
);
296 g_slist_free(to_filter
);
298 folder_item_add_msgs(dropfolder
, to_add
, TRUE
);
299 procmsg_message_file_list_free(to_add
);
303 folder_item_update_thaw();
306 claws_fclose(mbox_fp
);
307 debug_print("%d messages found.\n", msgs
);
312 gint
lock_mbox(const gchar
*base
, LockType type
)
317 if (type
== LOCK_FILE
) {
318 gchar
*lockfile
, *locklink
;
322 lockfile
= g_strdup_printf("%s.%d", base
, getpid());
323 if ((lockfp
= claws_fopen(lockfile
, "wb")) == NULL
) {
324 FILE_OP_ERROR(lockfile
, "claws_fopen");
325 g_warning("can't create lock file '%s', use 'flock' instead of 'file' if possible.", lockfile
);
330 if (fprintf(lockfp
, "%d\n", getpid()) < 0) {
331 FILE_OP_ERROR(lockfile
, "fprintf");
333 claws_fclose(lockfp
);
337 if (claws_safe_fclose(lockfp
) == EOF
) {
338 FILE_OP_ERROR(lockfile
, "claws_fclose");
343 locklink
= g_strconcat(base
, ".lock", NULL
);
344 while (link(lockfile
, locklink
) < 0) {
345 FILE_OP_ERROR(lockfile
, "link");
347 g_warning("can't create '%s'", lockfile
);
348 claws_unlink(lockfile
);
353 g_warning("mailbox is owned by another"
354 " process, waiting...");
358 claws_unlink(lockfile
);
360 } else if (type
== LOCK_FLOCK
) {
362 gboolean fcntled
= FALSE
;
363 #if HAVE_FCNTL_H && !defined(G_OS_WIN32)
366 fl
.l_whence
= SEEK_SET
;
372 if ((lockfd
= g_open(base
, O_RDWR
, 0)) < 0) {
374 if ((lockfd
= g_open(base
, O_RDWR
, 0)) < 0) {
376 FILE_OP_ERROR(base
, "open");
380 #if HAVE_FCNTL_H && !defined(G_OS_WIN32)
381 if (fcntl(lockfd
, F_SETLK
, &fl
) == -1) {
382 g_warning("can't fnctl %s (%s)", base
, g_strerror(errno
));
391 if (flock(lockfd
, LOCK_EX
|LOCK_NB
) < 0 && !fcntled
) {
395 if (lockf(lockfd
, F_TLOCK
, 0) < 0 && !fcntled
) {
400 #endif /* HAVE_FLOCK */
401 g_warning("can't lock %s", base
);
402 if (close(lockfd
) < 0)
408 g_warning("invalid lock type");
415 #endif /* G_OS_UNIX */
418 gint
unlock_mbox(const gchar
*base
, gint fd
, LockType type
)
420 if (type
== LOCK_FILE
) {
423 lockfile
= g_strconcat(base
, ".lock", NULL
);
424 if (claws_unlink(lockfile
) < 0) {
425 FILE_OP_ERROR(lockfile
, "unlink");
432 } else if (type
== LOCK_FLOCK
) {
433 #if HAVE_FCNTL_H && !defined(G_OS_WIN32)
434 gboolean fcntled
= FALSE
;
437 fl
.l_whence
= SEEK_SET
;
441 if (fcntl(fd
, F_SETLK
, &fl
) == -1) {
442 g_warning("can't fnctl %s", base
);
448 if (flock(fd
, LOCK_UN
) < 0 && !fcntled
) {
452 if (lockf(fd
, F_ULOCK
, 0) < 0 && !fcntled
) {
457 #endif /* HAVE_FLOCK */
458 g_warning("can't unlock %s", base
);
472 g_warning("invalid lock type");
476 gint
copy_mbox(gint srcfd
, const gchar
*dest
)
481 gboolean err
= FALSE
;
488 if ((dest_fp
= claws_fopen(dest
, "wb")) == NULL
) {
489 FILE_OP_ERROR(dest
, "claws_fopen");
493 if (change_file_mode_rw(dest_fp
, dest
) < 0) {
494 FILE_OP_ERROR(dest
, "chmod");
495 g_warning("can't change file mode");
498 while ((n_read
= read(srcfd
, buf
, sizeof(buf
))) > 0) {
499 if (claws_fwrite(buf
, 1, n_read
, dest_fp
) < n_read
) {
500 g_warning("writing to %s failed.", dest
);
501 claws_fclose(dest_fp
);
507 if (save_errno
!= 0) {
508 g_warning("error %d reading mbox: %s", save_errno
,
509 g_strerror(save_errno
));
513 if (claws_safe_fclose(dest_fp
) == EOF
) {
514 FILE_OP_ERROR(dest
, "claws_fclose");
526 void empty_mbox(const gchar
*mbox
)
530 if ((fp
= claws_fopen(mbox
, "wb")) == NULL
) {
531 FILE_OP_ERROR(mbox
, "claws_fopen");
532 g_warning("can't truncate mailbox to zero.");
535 claws_safe_fclose(fp
);
538 gint
export_list_to_mbox(GSList
*mlist
, const gchar
*mbox
)
539 /* return values: -2 skipped, -1 error, 0 OK */
548 gint msgs
= 1, total
= g_slist_length(mlist
);
549 if (g_file_test(mbox
, G_FILE_TEST_EXISTS
) == TRUE
) {
550 if (alertpanel_full(_("Overwrite mbox file"),
551 _("This file already exists. Do you want to overwrite it?"),
552 GTK_STOCK_CANCEL
, _("Overwrite"), NULL
,
553 ALERTFOCUS_FIRST
, FALSE
, NULL
, ALERT_WARNING
)
554 != G_ALERTALTERNATE
) {
559 if ((mbox_fp
= claws_fopen(mbox
, "wb")) == NULL
) {
560 FILE_OP_ERROR(mbox
, "claws_fopen");
561 alertpanel_error(_("Could not create mbox file:\n%s\n"), mbox
);
565 statusbar_print_all(_("Exporting to mbox..."));
566 for (cur
= mlist
; cur
!= NULL
; cur
= cur
->next
) {
568 gchar buft
[BUFFSIZE
];
569 msginfo
= (MsgInfo
*)cur
->data
;
571 msg_fp
= procmsg_open_message(msginfo
, TRUE
);
577 msginfo
->from
? msginfo
->from
:
578 cur_account
&& cur_account
->address
?
579 cur_account
->address
: "unknown",
581 extract_address(buf
);
583 if (fprintf(mbox_fp
, "From %s %s",
584 buf
, ctime_r(&msginfo
->date_t
, buft
)) < 0) {
586 claws_fclose(msg_fp
);
592 /* write email to mboxrc */
593 while (claws_fgets(buf
, sizeof(buf
), msg_fp
) != NULL
) {
594 /* quote any From, >From, >>From, etc., according to mbox format specs */
598 /* detect leading '>' char(s) */
599 while (buf
[offset
] == '>') {
602 if (!strncmp(buf
+offset
, "From ", 5)) {
603 if (claws_fputc('>', mbox_fp
) == EOF
) {
605 claws_fclose(msg_fp
);
609 if (claws_fputs(buf
, mbox_fp
) == EOF
) {
611 claws_fclose(msg_fp
);
616 /* force last line to end w/ a newline */
620 if ((buf
[len
] != '\n') && (buf
[len
] != '\r')) {
621 if (claws_fputc('\n', mbox_fp
) == EOF
) {
623 claws_fclose(msg_fp
);
629 /* add a trailing empty line */
630 if (claws_fputc('\n', mbox_fp
) == EOF
) {
632 claws_fclose(msg_fp
);
636 claws_safe_fclose(msg_fp
);
637 statusbar_progress_all(msgs
++,total
, 500);
643 statusbar_progress_all(0,0,0);
646 claws_safe_fclose(mbox_fp
);
651 /* read all messages in SRC, and store them into one MBOX file. */
652 /* return values: -2 skipped, -1 error, 0 OK */
653 gint
export_to_mbox(FolderItem
*src
, const gchar
*mbox
)
658 cm_return_val_if_fail(src
!= NULL
, -1);
659 cm_return_val_if_fail(src
->folder
!= NULL
, -1);
660 cm_return_val_if_fail(mbox
!= NULL
, -1);
662 debug_print("Exporting messages from %s into %s...\n",
665 mlist
= folder_item_get_msg_list(src
);
667 folder_item_update_freeze();
668 ret
= export_list_to_mbox(mlist
, mbox
);
669 folder_item_update_thaw();
671 procmsg_msg_list_free(mlist
);