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"
35 #include <glib/gi18n.h>
52 #include "prefs_common.h"
53 #include "prefs_account.h"
56 #include "filtering.h"
57 #include "alertpanel.h"
58 #include "statusbar.h"
60 #define MESSAGEBUFSIZE 8192
62 #ifdef HAVE_FGETS_UNLOCKED
63 #define SC_FGETS fgets_unlocked
64 #define SC_FPUTS fputs_unlocked
65 #define SC_FPUTC fputc_unlocked
67 #define SC_FGETS fgets
68 #define SC_FPUTS fputs
69 #define SC_FPUTC fputc
72 #define FPUTS_TO_TMP_ABORT_IF_FAIL(s) \
75 if (fputs(s, tmp_fp) == EOF) { \
76 g_warning("can't write to temporary file"); \
79 claws_unlink(tmp_file); \
85 gint
proc_mbox(FolderItem
*dest
, const gchar
*mbox
, gboolean apply_filter
,
86 PrefsAccount
*account
)
87 /* return values: -1 error, >=0 number of msgs added */
90 gchar buf
[MESSAGEBUFSIZE
];
96 GSList
*to_filter
= NULL
, *filtered
= NULL
, *unfiltered
= NULL
, *cur
, *to_add
= NULL
;
97 gboolean printed
= FALSE
;
98 FolderItem
*dropfolder
;
100 cm_return_val_if_fail(dest
!= NULL
, -1);
101 cm_return_val_if_fail(mbox
!= NULL
, -1);
103 debug_print("Getting messages from %s into %s...\n", mbox
, dest
->path
);
105 if ((mbox_fp
= g_fopen(mbox
, "rb")) == NULL
) {
106 FILE_OP_ERROR(mbox
, "fopen");
107 alertpanel_error(_("Could not open mbox file:\n%s\n"), mbox
);
111 /* ignore empty lines on the head */
113 if (fgets(buf
, sizeof(buf
), mbox_fp
) == NULL
) {
114 g_warning("can't read mbox file.");
118 } while (buf
[0] == '\n' || buf
[0] == '\r');
120 if (strncmp(buf
, "From ", 5) != 0) {
121 g_warning("invalid mbox format: %s", mbox
);
126 tmp_file
= get_tmp_file();
128 folder_item_update_freeze();
131 dropfolder
= folder_get_default_processing(account
->account_id
);
140 if (msgs
> 0 && msgs
%500 == 0) {
144 ngettext("Importing from mbox... (%d mail imported)",
145 "Importing from mbox... (%d mails imported)", msgs
), msgs
);
150 if ((tmp_fp
= g_fopen(tmp_file
, "wb")) == NULL
) {
151 FILE_OP_ERROR(tmp_file
, "fopen");
152 g_warning("can't open temporary file");
157 if (change_file_mode_rw(tmp_fp
, tmp_file
) < 0) {
158 FILE_OP_ERROR(tmp_file
, "chmod");
164 /* process all lines from mboxrc file */
165 while (fgets(buf
, sizeof(buf
), mbox_fp
) != NULL
) {
168 /* eat empty lines */
169 if (buf
[0] == '\n' || buf
[0] == '\r') {
174 /* From separator or quoted From */
176 /* detect leading '>' char(s) */
177 while ((buf
[offset
] == '>')) {
180 if (!strncmp(buf
+offset
, "From ", 5)) {
181 /* From separator: */
183 /* expect next mbox item */
188 /* flush any eaten empty line */
189 if (empty_lines
> 0) {
190 while (empty_lines
-- > 0) {
191 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
195 /* store the unquoted line */
196 FPUTS_TO_TMP_ABORT_IF_FAIL(buf
+ 1);
201 /* flush any eaten empty line */
202 if (empty_lines
> 0) {
203 while (empty_lines
-- > 0) {
204 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
208 /* store the line itself */
209 FPUTS_TO_TMP_ABORT_IF_FAIL(buf
);
211 /* end of mbox item or end of mbox */
213 /* flush any eaten empty line (but the last one) */
214 if (empty_lines
> 0) {
215 while (--empty_lines
> 0) {
216 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
220 /* more emails to expect? */
221 more
= !feof(mbox_fp
);
223 /* warn if email part is empty (it's the minimum check
226 g_warning("malformed mbox: %s: message %d is empty", mbox
, msgs
);
229 claws_unlink(tmp_file
);
233 if (fclose(tmp_fp
) == EOF
) {
234 FILE_OP_ERROR(tmp_file
, "fclose");
235 g_warning("can't write to temporary file");
237 claws_unlink(tmp_file
);
243 if ((msgnum
= folder_item_add_msg(dropfolder
, tmp_file
, NULL
, TRUE
)) < 0) {
245 claws_unlink(tmp_file
);
249 msginfo
= folder_item_get_msginfo(dropfolder
, msgnum
);
250 to_filter
= g_slist_prepend(to_filter
, msginfo
);
252 MsgFileInfo
*finfo
= g_new0(MsgFileInfo
, 1);
253 finfo
->file
= tmp_file
;
255 to_add
= g_slist_prepend(to_add
, finfo
);
256 tmp_file
= get_tmp_file();
258 /* flush every 500 */
259 if (msgs
> 0 && msgs
% 500 == 0) {
260 folder_item_add_msgs(dropfolder
, to_add
, TRUE
);
261 procmsg_message_file_list_free(to_add
);
273 folder_item_set_batch(dropfolder
, FALSE
);
274 procmsg_msglist_filter(to_filter
, account
,
275 &filtered
, &unfiltered
, TRUE
);
276 folder_item_set_batch(dropfolder
, TRUE
);
278 filtering_move_and_copy_msgs(to_filter
);
279 for (cur
= filtered
; cur
; cur
= g_slist_next(cur
)) {
280 MsgInfo
*info
= (MsgInfo
*)cur
->data
;
281 procmsg_msginfo_free(&info
);
284 unfiltered
= g_slist_reverse(unfiltered
);
286 folder_item_move_msgs(dest
, unfiltered
);
287 for (cur
= unfiltered
; cur
; cur
= g_slist_next(cur
)) {
288 MsgInfo
*info
= (MsgInfo
*)cur
->data
;
289 procmsg_msginfo_free(&info
);
293 g_slist_free(unfiltered
);
294 g_slist_free(filtered
);
295 g_slist_free(to_filter
);
297 folder_item_add_msgs(dropfolder
, to_add
, TRUE
);
298 procmsg_message_file_list_free(to_add
);
302 folder_item_update_thaw();
306 debug_print("%d messages found.\n", msgs
);
311 gint
lock_mbox(const gchar
*base
, LockType type
)
316 if (type
== LOCK_FILE
) {
317 gchar
*lockfile
, *locklink
;
321 lockfile
= g_strdup_printf("%s.%d", base
, getpid());
322 if ((lockfp
= g_fopen(lockfile
, "wb")) == NULL
) {
323 FILE_OP_ERROR(lockfile
, "fopen");
324 g_warning("can't create lock file '%s', use 'flock' instead of 'file' if possible.", lockfile
);
329 if (fprintf(lockfp
, "%d\n", getpid()) < 0) {
330 FILE_OP_ERROR(lockfile
, "fprintf");
336 if (fclose(lockfp
) == EOF
) {
337 FILE_OP_ERROR(lockfile
, "fclose");
342 locklink
= g_strconcat(base
, ".lock", NULL
);
343 while (link(lockfile
, locklink
) < 0) {
344 FILE_OP_ERROR(lockfile
, "link");
346 g_warning("can't create '%s'", lockfile
);
347 claws_unlink(lockfile
);
352 g_warning("mailbox is owned by another"
353 " process, waiting...");
357 claws_unlink(lockfile
);
359 } else if (type
== LOCK_FLOCK
) {
361 gboolean fcntled
= FALSE
;
362 #if HAVE_FCNTL_H && !defined(G_OS_WIN32)
365 fl
.l_whence
= SEEK_SET
;
371 if ((lockfd
= g_open(base
, O_RDWR
, 0)) < 0) {
373 if ((lockfd
= g_open(base
, O_RDWR
, 0)) < 0) {
375 FILE_OP_ERROR(base
, "open");
379 #if HAVE_FCNTL_H && !defined(G_OS_WIN32)
380 if (fcntl(lockfd
, F_SETLK
, &fl
) == -1) {
381 g_warning("can't fnctl %s (%s)", base
, g_strerror(errno
));
390 if (flock(lockfd
, LOCK_EX
|LOCK_NB
) < 0 && !fcntled
) {
394 if (lockf(lockfd
, F_TLOCK
, 0) < 0 && !fcntled
) {
399 #endif /* HAVE_FLOCK */
400 g_warning("can't lock %s", base
);
401 if (close(lockfd
) < 0)
407 g_warning("invalid lock type");
414 #endif /* G_OS_UNIX */
417 gint
unlock_mbox(const gchar
*base
, gint fd
, LockType type
)
419 if (type
== LOCK_FILE
) {
422 lockfile
= g_strconcat(base
, ".lock", NULL
);
423 if (claws_unlink(lockfile
) < 0) {
424 FILE_OP_ERROR(lockfile
, "unlink");
431 } else if (type
== LOCK_FLOCK
) {
432 #if HAVE_FCNTL_H && !defined(G_OS_WIN32)
433 gboolean fcntled
= FALSE
;
436 fl
.l_whence
= SEEK_SET
;
440 if (fcntl(fd
, F_SETLK
, &fl
) == -1) {
441 g_warning("can't fnctl %s", base
);
447 if (flock(fd
, LOCK_UN
) < 0 && !fcntled
) {
451 if (lockf(fd
, F_ULOCK
, 0) < 0 && !fcntled
) {
456 #endif /* HAVE_FLOCK */
457 g_warning("can't unlock %s", base
);
471 g_warning("invalid lock type");
475 gint
copy_mbox(gint srcfd
, const gchar
*dest
)
480 gboolean err
= FALSE
;
487 if ((dest_fp
= g_fopen(dest
, "wb")) == NULL
) {
488 FILE_OP_ERROR(dest
, "fopen");
492 if (change_file_mode_rw(dest_fp
, dest
) < 0) {
493 FILE_OP_ERROR(dest
, "chmod");
494 g_warning("can't change file mode");
497 while ((n_read
= read(srcfd
, buf
, sizeof(buf
))) > 0) {
498 if (fwrite(buf
, 1, n_read
, dest_fp
) < n_read
) {
499 g_warning("writing to %s failed.", dest
);
506 if (save_errno
!= 0) {
507 g_warning("error %d reading mbox: %s", save_errno
,
508 g_strerror(save_errno
));
512 if (fclose(dest_fp
) == EOF
) {
513 FILE_OP_ERROR(dest
, "fclose");
525 void empty_mbox(const gchar
*mbox
)
529 if ((fp
= g_fopen(mbox
, "wb")) == NULL
) {
530 FILE_OP_ERROR(mbox
, "fopen");
531 g_warning("can't truncate mailbox to zero.");
537 gint
export_list_to_mbox(GSList
*mlist
, const gchar
*mbox
)
538 /* return values: -2 skipped, -1 error, 0 OK */
547 gint msgs
= 1, total
= g_slist_length(mlist
);
548 if (g_file_test(mbox
, G_FILE_TEST_EXISTS
) == TRUE
) {
549 if (alertpanel_full(_("Overwrite mbox file"),
550 _("This file already exists. Do you want to overwrite it?"),
551 GTK_STOCK_CANCEL
, _("Overwrite"), NULL
, FALSE
,
552 NULL
, ALERT_WARNING
, G_ALERTDEFAULT
)
553 != G_ALERTALTERNATE
) {
558 if ((mbox_fp
= g_fopen(mbox
, "wb")) == NULL
) {
559 FILE_OP_ERROR(mbox
, "fopen");
560 alertpanel_error(_("Could not create mbox file:\n%s\n"), mbox
);
564 #ifdef HAVE_FGETS_UNLOCKED
568 statuswindow_print_all(_("Exporting to mbox..."));
569 for (cur
= mlist
; cur
!= NULL
; cur
= cur
->next
) {
571 gchar buft
[BUFFSIZE
];
572 msginfo
= (MsgInfo
*)cur
->data
;
574 msg_fp
= procmsg_open_message(msginfo
);
579 #ifdef HAVE_FGETS_UNLOCKED
583 msginfo
->from
? msginfo
->from
:
584 cur_account
&& cur_account
->address
?
585 cur_account
->address
: "unknown",
587 extract_address(buf
);
589 if (fprintf(mbox_fp
, "From %s %s",
590 buf
, ctime_r(&msginfo
->date_t
, buft
)) < 0) {
592 #ifdef HAVE_FGETS_UNLOCKED
601 /* write email to mboxrc */
602 while (SC_FGETS(buf
, sizeof(buf
), msg_fp
) != NULL
) {
603 /* quote any From, >From, >>From, etc., according to mbox format specs */
607 /* detect leading '>' char(s) */
608 while ((buf
[offset
] == '>')) {
611 if (!strncmp(buf
+offset
, "From ", 5)) {
612 if (SC_FPUTC('>', mbox_fp
) == EOF
) {
614 #ifdef HAVE_FGETS_UNLOCKED
621 if (SC_FPUTS(buf
, mbox_fp
) == EOF
) {
623 #ifdef HAVE_FGETS_UNLOCKED
631 /* force last line to end w/ a newline */
635 if ((buf
[len
] != '\n') && (buf
[len
] != '\r')) {
636 if (SC_FPUTC('\n', mbox_fp
) == EOF
) {
638 #ifdef HAVE_FGETS_UNLOCKED
647 /* add a trailing empty line */
648 if (SC_FPUTC('\n', mbox_fp
) == EOF
) {
650 #ifdef HAVE_FGETS_UNLOCKED
657 #ifdef HAVE_FGETS_UNLOCKED
661 statusbar_progress_all(msgs
++,total
, 500);
667 statusbar_progress_all(0,0,0);
668 statuswindow_pop_all();
670 #ifdef HAVE_FGETS_UNLOCKED
671 funlockfile(mbox_fp
);
678 /* read all messages in SRC, and store them into one MBOX file. */
679 /* return values: -2 skipped, -1 error, 0 OK */
680 gint
export_to_mbox(FolderItem
*src
, const gchar
*mbox
)
685 cm_return_val_if_fail(src
!= NULL
, -1);
686 cm_return_val_if_fail(src
->folder
!= NULL
, -1);
687 cm_return_val_if_fail(mbox
!= NULL
, -1);
689 debug_print("Exporting messages from %s into %s...\n",
692 mlist
= folder_item_get_msg_list(src
);
694 folder_item_update_freeze();
695 ret
= export_list_to_mbox(mlist
, mbox
);
696 folder_item_update_thaw();
698 procmsg_msg_list_free(mlist
);