2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2007 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 2 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, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
30 #include <glib/gi18n.h>
43 #include "prefs_common.h"
44 #include "prefs_account.h"
47 #include "filtering.h"
48 #include "alertpanel.h"
49 #include "statusbar.h"
51 #define MSGBUFSIZE 8192
53 #ifdef HAVE_FGETS_UNLOCKED
54 #define SC_FGETS fgets_unlocked
55 #define SC_FPUTS fputs_unlocked
56 #define SC_FPUTC fputc_unlocked
58 #define SC_FGETS fgets
59 #define SC_FPUTS fputs
60 #define SC_FPUTC fputc
63 #define FPUTS_TO_TMP_ABORT_IF_FAIL(s) \
66 if (fputs(s, tmp_fp) == EOF) { \
67 g_warning("can't write to temporary file\n"); \
76 gint
proc_mbox(FolderItem
*dest
, const gchar
*mbox
, gboolean apply_filter
,
77 PrefsAccount
*account
)
78 /* return values: -1 error, >=0 number of msgs added */
81 gchar buf
[MSGBUFSIZE
];
87 GSList
*to_filter
= NULL
, *filtered
= NULL
, *unfiltered
= NULL
, *cur
, *to_add
= NULL
;
88 gboolean printed
= FALSE
;
89 FolderItem
*dropfolder
;
91 g_return_val_if_fail(dest
!= NULL
, -1);
92 g_return_val_if_fail(mbox
!= NULL
, -1);
94 debug_print("Getting messages from %s into %s...\n", mbox
, dest
->path
);
96 if ((mbox_fp
= g_fopen(mbox
, "rb")) == NULL
) {
97 FILE_OP_ERROR(mbox
, "fopen");
98 alertpanel_error(_("Could not open mbox file:\n%s\n"), mbox
);
102 /* ignore empty lines on the head */
104 if (fgets(buf
, sizeof(buf
), mbox_fp
) == NULL
) {
105 g_warning("can't read mbox file.\n");
109 } while (buf
[0] == '\n' || buf
[0] == '\r');
111 if (strncmp(buf
, "From ", 5) != 0) {
112 g_warning("invalid mbox format: %s\n", mbox
);
117 tmp_file
= get_tmp_file();
119 folder_item_update_freeze();
122 dropfolder
= folder_get_default_processing();
131 if (msgs
> 0 && msgs
%500 == 0) {
134 statusbar_print_all(_("Importing from mbox... (%d mails imported)"), msgs
);
139 if ((tmp_fp
= g_fopen(tmp_file
, "wb")) == NULL
) {
140 FILE_OP_ERROR(tmp_file
, "fopen");
141 g_warning("can't open temporary file\n");
146 if (change_file_mode_rw(tmp_fp
, tmp_file
) < 0) {
147 FILE_OP_ERROR(tmp_file
, "chmod");
154 /* process all lines from mboxrc file */
155 while (fgets(buf
, sizeof(buf
), mbox_fp
) != NULL
) {
158 /* eof not reached, expect more lines */
161 /* eat empty lines */
162 if (buf
[0] == '\n' || buf
[0] == '\r') {
167 /* From separator or quoted From */
169 /* detect leading '>' char(s) */
170 while ((buf
[offset
] == '>')) {
173 if (!strncmp(buf
+offset
, "From ", 5)) {
174 /* From separator: */
176 /* expect next mbox item */
181 /* flush any eaten empty line */
182 if (empty_lines
> 0) {
183 while (empty_lines
-- > 0) {
184 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
188 /* store the unquoted line */
189 FPUTS_TO_TMP_ABORT_IF_FAIL(buf
+ 1);
194 /* flush any eaten empty line */
195 if (empty_lines
> 0) {
196 while (empty_lines
-- > 0) {
197 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
201 /* store the line itself */
202 FPUTS_TO_TMP_ABORT_IF_FAIL(buf
);
204 /* end of mbox item or end of mbox */
206 /* flush any eaten empty line (but the last one) */
207 if (empty_lines
> 0) {
208 while (--empty_lines
> 0) {
209 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
213 /* more emails to expect? */
214 more
= !feof(mbox_fp
);
216 /* warn if email part is empty (it's the minimum check
219 g_warning("malformed mbox: %s: message %d is empty\n", mbox
, msgs
);
226 if (fclose(tmp_fp
) == EOF
) {
227 FILE_OP_ERROR(tmp_file
, "fclose");
228 g_warning("can't write to temporary file\n");
236 if ((msgnum
= folder_item_add_msg(dropfolder
, tmp_file
, NULL
, TRUE
)) < 0) {
242 msginfo
= folder_item_get_msginfo(dropfolder
, msgnum
);
243 to_filter
= g_slist_prepend(to_filter
, msginfo
);
245 MsgFileInfo
*finfo
= g_new0(MsgFileInfo
, 1);
246 finfo
->file
= tmp_file
;
248 to_add
= g_slist_prepend(to_add
, finfo
);
249 tmp_file
= get_tmp_file();
251 /* flush every 500 */
252 if (msgs
> 0 && msgs
% 500 == 0) {
253 folder_item_add_msgs(dropfolder
, to_add
, TRUE
);
254 procmsg_message_file_list_free(to_add
);
265 procmsg_msglist_filter(to_filter
, account
,
266 &filtered
, &unfiltered
, 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
);
275 folder_item_move_msgs(dest
, unfiltered
);
276 for (cur
= unfiltered
; cur
; cur
= g_slist_next(cur
)) {
277 MsgInfo
*info
= (MsgInfo
*)cur
->data
;
278 procmsg_msginfo_free(info
);
281 g_slist_free(unfiltered
);
282 g_slist_free(filtered
);
283 g_slist_free(to_filter
);
285 folder_item_add_msgs(dropfolder
, to_add
, TRUE
);
286 procmsg_message_file_list_free(to_add
);
290 folder_item_update_thaw();
294 debug_print("%d messages found.\n", msgs
);
299 gint
lock_mbox(const gchar
*base
, LockType type
)
304 if (type
== LOCK_FILE
) {
305 gchar
*lockfile
, *locklink
;
309 lockfile
= g_strdup_printf("%s.%d", base
, getpid());
310 if ((lockfp
= g_fopen(lockfile
, "wb")) == NULL
) {
311 FILE_OP_ERROR(lockfile
, "fopen");
312 g_warning("can't create lock file %s\n", lockfile
);
313 g_warning("use 'flock' instead of 'file' if possible.\n");
318 fprintf(lockfp
, "%d\n", getpid());
321 locklink
= g_strconcat(base
, ".lock", NULL
);
322 while (link(lockfile
, locklink
) < 0) {
323 FILE_OP_ERROR(lockfile
, "link");
325 g_warning("can't create %s\n", lockfile
);
331 g_warning("mailbox is owned by another"
332 " process, waiting...\n");
338 } else if (type
== LOCK_FLOCK
) {
340 gboolean fcntled
= FALSE
;
341 #if HAVE_FCNTL_H && !defined(G_OS_WIN32)
344 fl
.l_whence
= SEEK_SET
;
350 if ((lockfd
= open(base
, O_RDWR
)) < 0) {
352 if ((lockfd
= open(base
, O_RDWR
)) < 0) {
354 FILE_OP_ERROR(base
, "open");
358 #if HAVE_FCNTL_H && !defined(G_OS_WIN32)
359 if (fcntl(lockfd
, F_SETLK
, &fl
) == -1) {
360 g_warning("can't fnctl %s (%s)", base
, strerror(errno
));
368 if (flock(lockfd
, LOCK_EX
|LOCK_NB
) < 0 && !fcntled
) {
372 if (lockf(lockfd
, F_TLOCK
, 0) < 0 && !fcntled
) {
377 #endif /* HAVE_FLOCK */
378 g_warning("can't lock %s\n", base
);
379 if (close(lockfd
) < 0)
385 g_warning("invalid lock type\n");
392 #endif /* G_OS_UNIX */
395 gint
unlock_mbox(const gchar
*base
, gint fd
, LockType type
)
397 if (type
== LOCK_FILE
) {
400 lockfile
= g_strconcat(base
, ".lock", NULL
);
401 if (g_unlink(lockfile
) < 0) {
402 FILE_OP_ERROR(lockfile
, "unlink");
409 } else if (type
== LOCK_FLOCK
) {
410 gboolean fcntled
= FALSE
;
411 #if HAVE_FCNTL_H && !defined(G_OS_WIN32)
414 fl
.l_whence
= SEEK_SET
;
418 if (fcntl(fd
, F_SETLK
, &fl
) == -1) {
419 g_warning("can't fnctl %s", base
);
425 if (flock(fd
, LOCK_UN
) < 0 && !fcntled
) {
429 if (lockf(fd
, F_ULOCK
, 0) < 0 && !fcntled
) {
434 #endif /* HAVE_FLOCK */
435 g_warning("can't unlock %s\n", base
);
449 g_warning("invalid lock type\n");
453 gint
copy_mbox(gint srcfd
, const gchar
*dest
)
458 gboolean err
= FALSE
;
465 if ((dest_fp
= g_fopen(dest
, "wb")) == NULL
) {
466 FILE_OP_ERROR(dest
, "fopen");
470 if (change_file_mode_rw(dest_fp
, dest
) < 0) {
471 FILE_OP_ERROR(dest
, "chmod");
472 g_warning("can't change file mode\n");
475 while ((n_read
= read(srcfd
, buf
, sizeof(buf
))) > 0) {
476 if (n_read
== -1 && errno
!= 0) {
480 if (fwrite(buf
, 1, n_read
, dest_fp
) < n_read
) {
481 g_warning("writing to %s failed.\n", dest
);
488 if (save_errno
!= 0) {
489 g_warning("error %d reading mbox: %s\n", save_errno
,
490 strerror(save_errno
));
494 if (fclose(dest_fp
) == EOF
) {
495 FILE_OP_ERROR(dest
, "fclose");
507 void empty_mbox(const gchar
*mbox
)
511 if ((fp
= g_fopen(mbox
, "wb")) == NULL
) {
512 FILE_OP_ERROR(mbox
, "fopen");
513 g_warning("can't truncate mailbox to zero.\n");
519 gint
export_list_to_mbox(GSList
*mlist
, const gchar
*mbox
)
520 /* return values: -2 skipped, -1 error, 0 OK */
527 gint msgs
= 1, total
= g_slist_length(mlist
);
528 if (g_file_test(mbox
, G_FILE_TEST_EXISTS
) == TRUE
) {
529 if (alertpanel_full(_("Overwrite mbox file"),
530 _("This file already exists. Do you want to overwrite it?"),
531 GTK_STOCK_CANCEL
, _("Overwrite"), NULL
, FALSE
,
532 NULL
, ALERT_WARNING
, G_ALERTDEFAULT
)
533 != G_ALERTALTERNATE
) {
538 if ((mbox_fp
= g_fopen(mbox
, "wb")) == NULL
) {
539 FILE_OP_ERROR(mbox
, "fopen");
540 alertpanel_error(_("Could not create mbox file:\n%s\n"), mbox
);
544 #ifdef HAVE_FGETS_UNLOCKED
548 statusbar_print_all(_("Exporting to mbox..."));
549 for (cur
= mlist
; cur
!= NULL
; cur
= cur
->next
) {
551 gchar buft
[BUFFSIZE
];
552 msginfo
= (MsgInfo
*)cur
->data
;
554 msg_fp
= procmsg_open_message(msginfo
);
559 #ifdef HAVE_FGETS_UNLOCKED
563 msginfo
->from
? msginfo
->from
:
564 cur_account
&& cur_account
->address
?
565 cur_account
->address
: "unknown",
567 extract_address(buf
);
569 fprintf(mbox_fp
, "From %s %s",
570 buf
, ctime_r(&msginfo
->date_t
, buft
));
574 /* write email to mboxrc */
575 while (SC_FGETS(buf
, sizeof(buf
), msg_fp
) != NULL
) {
576 /* quote any From, >From, >>From, etc., according to mbox format specs */
580 /* detect leading '>' char(s) */
581 while ((buf
[offset
] == '>')) {
584 if (!strncmp(buf
+offset
, "From ", 5))
585 SC_FPUTC('>', mbox_fp
);
586 SC_FPUTS(buf
, mbox_fp
);
589 /* force last line to end w/ a newline */
593 if ((buf
[len
] != '\n') && (buf
[len
] != '\r'))
594 SC_FPUTC('\n', mbox_fp
);
597 /* add a trailing empty line */
598 SC_FPUTC('\n', mbox_fp
);
600 #ifdef HAVE_FGETS_UNLOCKED
604 statusbar_progress_all(msgs
++,total
, 500);
608 statusbar_progress_all(0,0,0);
611 #ifdef HAVE_FGETS_UNLOCKED
612 funlockfile(mbox_fp
);
619 /* read all messages in SRC, and store them into one MBOX file. */
620 /* return values: -2 skipped, -1 error, 0 OK */
621 gint
export_to_mbox(FolderItem
*src
, const gchar
*mbox
)
626 g_return_val_if_fail(src
!= NULL
, -1);
627 g_return_val_if_fail(src
->folder
!= NULL
, -1);
628 g_return_val_if_fail(mbox
!= NULL
, -1);
630 debug_print("Exporting messages from %s into %s...\n",
633 mlist
= folder_item_get_msg_list(src
);
635 folder_item_update_freeze();
636 ret
= export_list_to_mbox(mlist
, mbox
);
637 folder_item_update_thaw();
639 procmsg_msg_list_free(mlist
);