2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2022 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>
47 #include "prefs_common.h"
48 #include "prefs_account.h"
51 #include "filtering.h"
52 #include "alertpanel.h"
53 #include "statusbar.h"
54 #include "file-utils.h"
56 #define MESSAGEBUFSIZE 8192
58 #define FPUTS_TO_TMP_ABORT_IF_FAIL(s) \
61 if (claws_fputs(s, tmp_fp) == EOF) { \
62 g_warning("can't write to temporary file"); \
63 claws_fclose(tmp_fp); \
64 claws_fclose(mbox_fp); \
65 claws_unlink(tmp_file); \
71 gint
proc_mbox(FolderItem
*dest
, const gchar
*mbox
, gboolean apply_filter
,
72 PrefsAccount
*account
)
73 /* return values: -1 error, >=0 number of msgs added */
76 gchar buf
[MESSAGEBUFSIZE
];
82 GSList
*to_filter
= NULL
, *filtered
= NULL
, *unfiltered
= NULL
, *cur
, *to_add
= NULL
;
83 gboolean printed
= FALSE
;
84 FolderItem
*dropfolder
;
87 cm_return_val_if_fail(dest
!= NULL
, -1);
88 cm_return_val_if_fail(mbox
!= NULL
, -1);
90 debug_print("Getting messages from %s into %s...\n", mbox
, dest
->path
);
92 if (g_stat(mbox
, &src_stat
) < 0) {
93 FILE_OP_ERROR(mbox
, "g_stat");
94 alertpanel_error(_("Could not stat mbox file:\n%s\n"), mbox
);
98 if ((mbox_fp
= claws_fopen(mbox
, "rb")) == NULL
) {
99 FILE_OP_ERROR(mbox
, "claws_fopen");
100 alertpanel_error(_("Could not open mbox file:\n%s\n"), mbox
);
104 /* ignore empty lines on the head */
106 if (claws_fgets(buf
, sizeof(buf
), mbox_fp
) == NULL
) {
107 g_warning("can't read mbox file");
108 claws_fclose(mbox_fp
);
111 } while (buf
[0] == '\n' || buf
[0] == '\r');
113 if (strncmp(buf
, "From ", 5) != 0) {
114 g_warning("invalid mbox format: %s", mbox
);
115 claws_fclose(mbox_fp
);
119 tmp_file
= get_tmp_file();
121 folder_item_update_freeze();
124 dropfolder
= folder_get_default_processing(account
->account_id
);
134 long cur_offset_mb
= ftell(mbox_fp
) / (1024 * 1024);
138 ngettext("Importing from mbox... (%ld MB imported)",
139 "Importing from mbox... (%ld MB imported)", cur_offset_mb
), cur_offset_mb
);
140 statusbar_progress_all(cur_offset_mb
, src_stat
.st_size
/ (1024*1024), 1);
145 if ((tmp_fp
= claws_fopen(tmp_file
, "wb")) == NULL
) {
146 FILE_OP_ERROR(tmp_file
, "claws_fopen");
147 g_warning("can't open temporary file");
148 claws_fclose(mbox_fp
);
152 if (change_file_mode_rw(tmp_fp
, tmp_file
) < 0) {
153 FILE_OP_ERROR(tmp_file
, "chmod");
159 /* process all lines from mboxrc file */
160 while (claws_fgets(buf
, sizeof(buf
), mbox_fp
) != NULL
) {
163 /* eat empty lines */
164 if (buf
[0] == '\n' || buf
[0] == '\r') {
169 /* From separator or quoted From */
171 /* detect leading '>' char(s) */
172 while (buf
[offset
] == '>') {
175 if (!strncmp(buf
+offset
, "From ", 5)) {
176 /* From separator: */
178 /* expect next mbox item */
183 /* flush any eaten empty line */
184 if (empty_lines
> 0) {
185 while (empty_lines
-- > 0) {
186 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
190 /* store the unquoted line */
191 FPUTS_TO_TMP_ABORT_IF_FAIL(buf
+ 1);
196 /* flush any eaten empty line */
197 if (empty_lines
> 0) {
198 while (empty_lines
-- > 0) {
199 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
203 /* store the line itself */
204 FPUTS_TO_TMP_ABORT_IF_FAIL(buf
);
206 /* end of mbox item or end of mbox */
208 /* flush any eaten empty line (but the last one) */
209 if (empty_lines
> 0) {
210 while (--empty_lines
> 0) {
211 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
215 /* more emails to expect? */
216 more
= !claws_feof(mbox_fp
);
218 /* warn if email part is empty (it's the minimum check
221 g_warning("malformed mbox: %s: message %d is empty", mbox
, msgs
);
222 claws_fclose(tmp_fp
);
223 claws_fclose(mbox_fp
);
224 claws_unlink(tmp_file
);
228 if (claws_safe_fclose(tmp_fp
) == EOF
) {
229 FILE_OP_ERROR(tmp_file
, "claws_fclose");
230 g_warning("can't write to temporary file");
231 claws_fclose(mbox_fp
);
232 claws_unlink(tmp_file
);
238 if ((msgnum
= folder_item_add_msg(dropfolder
, tmp_file
, NULL
, TRUE
)) < 0) {
239 claws_fclose(mbox_fp
);
240 claws_unlink(tmp_file
);
244 msginfo
= folder_item_get_msginfo(dropfolder
, msgnum
);
245 to_filter
= g_slist_prepend(to_filter
, msginfo
);
247 MsgFileInfo
*finfo
= g_new0(MsgFileInfo
, 1);
248 finfo
->file
= tmp_file
;
250 to_add
= g_slist_prepend(to_add
, finfo
);
251 tmp_file
= get_tmp_file();
253 /* flush every 500 */
254 if (msgs
> 0 && msgs
% 500 == 0) {
255 folder_item_add_msgs(dropfolder
, to_add
, TRUE
);
256 procmsg_message_file_list_free(to_add
);
265 statusbar_progress_all(0, 0, 0);
270 folder_item_set_batch(dropfolder
, FALSE
);
271 procmsg_msglist_filter(to_filter
, account
,
272 &filtered
, &unfiltered
, TRUE
);
273 folder_item_set_batch(dropfolder
, TRUE
);
275 filtering_move_and_copy_msgs(to_filter
);
276 for (cur
= filtered
; cur
; cur
= g_slist_next(cur
)) {
277 MsgInfo
*info
= (MsgInfo
*)cur
->data
;
278 procmsg_msginfo_free(&info
);
281 unfiltered
= g_slist_reverse(unfiltered
);
283 folder_item_move_msgs(dest
, unfiltered
);
284 for (cur
= unfiltered
; cur
; cur
= g_slist_next(cur
)) {
285 MsgInfo
*info
= (MsgInfo
*)cur
->data
;
286 procmsg_msginfo_free(&info
);
290 g_slist_free(unfiltered
);
291 g_slist_free(filtered
);
292 g_slist_free(to_filter
);
294 folder_item_add_msgs(dropfolder
, to_add
, TRUE
);
295 procmsg_message_file_list_free(to_add
);
299 folder_item_update_thaw();
302 claws_fclose(mbox_fp
);
303 debug_print("%d messages found.\n", msgs
);
308 gint
lock_mbox(const gchar
*base
, LockType type
)
313 if (type
== LOCK_FILE
) {
314 gchar
*lockfile
, *locklink
;
318 lockfile
= g_strdup_printf("%s.%d", base
, getpid());
319 if ((lockfp
= claws_fopen(lockfile
, "wb")) == NULL
) {
320 FILE_OP_ERROR(lockfile
, "claws_fopen");
321 g_warning("can't create lock file '%s', use 'flock' instead of 'file' if possible", lockfile
);
326 if (fprintf(lockfp
, "%d\n", getpid()) < 0) {
327 FILE_OP_ERROR(lockfile
, "fprintf");
329 claws_fclose(lockfp
);
333 if (claws_safe_fclose(lockfp
) == EOF
) {
334 FILE_OP_ERROR(lockfile
, "claws_fclose");
339 locklink
= g_strconcat(base
, ".lock", NULL
);
340 while (link(lockfile
, locklink
) < 0) {
341 FILE_OP_ERROR(lockfile
, "link");
343 g_warning("can't create '%s'", lockfile
);
344 claws_unlink(lockfile
);
350 g_warning("mailbox is owned by another process, waiting");
354 claws_unlink(lockfile
);
357 } else if (type
== LOCK_FLOCK
) {
359 gboolean fcntled
= FALSE
;
360 #if HAVE_FCNTL_H && !defined(G_OS_WIN32)
363 fl
.l_whence
= SEEK_SET
;
369 if ((lockfd
= g_open(base
, O_RDWR
, 0)) < 0) {
371 if ((lockfd
= g_open(base
, O_RDWR
, 0)) < 0) {
373 FILE_OP_ERROR(base
, "open");
377 #if HAVE_FCNTL_H && !defined(G_OS_WIN32)
378 if (fcntl(lockfd
, F_SETLK
, &fl
) == -1) {
379 g_warning("can't fnctl %s (%s)", base
, g_strerror(errno
));
388 if (flock(lockfd
, LOCK_EX
|LOCK_NB
) < 0 && !fcntled
) {
392 if (lockf(lockfd
, F_TLOCK
, 0) < 0 && !fcntled
) {
397 #endif /* HAVE_FLOCK */
398 g_warning("can't lock %s", base
);
399 if (close(lockfd
) < 0)
405 g_warning("invalid lock type");
412 #endif /* G_OS_UNIX */
415 gint
unlock_mbox(const gchar
*base
, gint fd
, LockType type
)
417 if (type
== LOCK_FILE
) {
420 lockfile
= g_strconcat(base
, ".lock", NULL
);
421 if (claws_unlink(lockfile
) < 0) {
422 FILE_OP_ERROR(lockfile
, "unlink");
429 } else if (type
== LOCK_FLOCK
) {
430 #if HAVE_FCNTL_H && !defined(G_OS_WIN32)
431 gboolean fcntled
= FALSE
;
434 fl
.l_whence
= SEEK_SET
;
438 if (fcntl(fd
, F_SETLK
, &fl
) == -1) {
439 g_warning("can't fnctl %s", base
);
445 if (flock(fd
, LOCK_UN
) < 0 && !fcntled
) {
449 if (lockf(fd
, F_ULOCK
, 0) < 0 && !fcntled
) {
454 #endif /* HAVE_FLOCK */
455 g_warning("can't unlock %s", base
);
469 g_warning("invalid lock type");
473 gint
copy_mbox(gint srcfd
, const gchar
*dest
)
478 gboolean err
= FALSE
;
485 if ((dest_fp
= claws_fopen(dest
, "wb")) == NULL
) {
486 FILE_OP_ERROR(dest
, "claws_fopen");
490 if (change_file_mode_rw(dest_fp
, dest
) < 0) {
491 FILE_OP_ERROR(dest
, "chmod");
492 g_warning("can't change file mode");
495 while ((n_read
= read(srcfd
, buf
, sizeof(buf
))) > 0) {
496 if (claws_fwrite(buf
, 1, n_read
, dest_fp
) < n_read
) {
497 g_warning("writing to %s failed", dest
);
498 claws_fclose(dest_fp
);
504 if (save_errno
!= 0) {
505 g_warning("error %d reading mbox: %s", save_errno
,
506 g_strerror(save_errno
));
510 if (claws_safe_fclose(dest_fp
) == EOF
) {
511 FILE_OP_ERROR(dest
, "claws_fclose");
523 void empty_mbox(const gchar
*mbox
)
527 if ((fp
= claws_fopen(mbox
, "wb")) == NULL
) {
528 FILE_OP_ERROR(mbox
, "claws_fopen");
529 g_warning("can't truncate mailbox to zero");
532 claws_safe_fclose(fp
);
535 gint
export_list_to_mbox(GSList
*mlist
, const gchar
*mbox
)
536 /* return values: -2 skipped, -1 error, 0 OK */
545 gint msgs
= 1, total
= g_slist_length(mlist
);
546 if (g_file_test(mbox
, G_FILE_TEST_EXISTS
) == TRUE
) {
547 if (alertpanel_full(_("Overwrite mbox file"),
548 _("This file already exists. Do you want to overwrite it?"),
549 NULL
, _("_Cancel"), NULL
, _("Overwrite"), NULL
, NULL
,
550 ALERTFOCUS_FIRST
, FALSE
, NULL
, ALERT_WARNING
)
551 != G_ALERTALTERNATE
) {
556 if ((mbox_fp
= claws_fopen(mbox
, "wb")) == NULL
) {
557 FILE_OP_ERROR(mbox
, "claws_fopen");
558 alertpanel_error(_("Could not create mbox file:\n%s\n"), mbox
);
562 statusbar_print_all(_("Exporting to mbox..."));
563 for (cur
= mlist
; cur
!= NULL
; cur
= cur
->next
) {
565 gchar buft
[BUFFSIZE
];
566 msginfo
= (MsgInfo
*)cur
->data
;
568 msg_fp
= procmsg_open_message(msginfo
, TRUE
);
574 msginfo
->from
? msginfo
->from
:
575 cur_account
&& cur_account
->address
?
576 cur_account
->address
: "unknown",
578 extract_address(buf
);
580 if (fprintf(mbox_fp
, "From %s %s",
581 buf
, ctime_r(&msginfo
->date_t
, buft
)) < 0) {
583 claws_fclose(msg_fp
);
589 /* write email to mboxrc */
590 while (claws_fgets(buf
, sizeof(buf
), msg_fp
) != NULL
) {
591 /* quote any From, >From, >>From, etc., according to mbox format specs */
595 /* detect leading '>' char(s) */
596 while (buf
[offset
] == '>') {
599 if (!strncmp(buf
+offset
, "From ", 5)) {
600 if (claws_fputc('>', mbox_fp
) == EOF
) {
602 claws_fclose(msg_fp
);
606 if (claws_fputs(buf
, mbox_fp
) == EOF
) {
608 claws_fclose(msg_fp
);
613 /* force last line to end w/ a newline */
617 if ((buf
[len
] != '\n') && (buf
[len
] != '\r')) {
618 if (claws_fputc('\n', mbox_fp
) == EOF
) {
620 claws_fclose(msg_fp
);
626 /* add a trailing empty line */
627 if (claws_fputc('\n', mbox_fp
) == EOF
) {
629 claws_fclose(msg_fp
);
633 claws_safe_fclose(msg_fp
);
634 statusbar_progress_all(msgs
++,total
, 500);
640 statusbar_progress_all(0,0,0);
643 claws_safe_fclose(mbox_fp
);
648 /* read all messages in SRC, and store them into one MBOX file. */
649 /* return values: -2 skipped, -1 error, 0 OK */
650 gint
export_to_mbox(FolderItem
*src
, const gchar
*mbox
)
655 cm_return_val_if_fail(src
!= NULL
, -1);
656 cm_return_val_if_fail(src
->folder
!= NULL
, -1);
657 cm_return_val_if_fail(mbox
!= NULL
, -1);
659 debug_print("Exporting messages from %s into %s...\n",
662 mlist
= folder_item_get_msg_list(src
);
664 folder_item_update_freeze();
665 ret
= export_list_to_mbox(mlist
, mbox
);
666 folder_item_update_thaw();
668 procmsg_msg_list_free(mlist
);