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 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/>.
30 #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"
55 #define MSGBUFSIZE 8192
57 #ifdef HAVE_FGETS_UNLOCKED
58 #define SC_FGETS fgets_unlocked
59 #define SC_FPUTS fputs_unlocked
60 #define SC_FPUTC fputc_unlocked
62 #define SC_FGETS fgets
63 #define SC_FPUTS fputs
64 #define SC_FPUTC fputc
67 #define FPUTS_TO_TMP_ABORT_IF_FAIL(s) \
70 if (fputs(s, tmp_fp) == EOF) { \
71 g_warning("can't write to temporary file\n"); \
80 gint proc_mbox(FolderItem *dest, const gchar *mbox, gboolean apply_filter,
81 PrefsAccount *account)
82 /* return values: -1 error, >=0 number of msgs added */
85 gchar buf[MSGBUFSIZE];
91 GSList *to_filter = NULL, *filtered = NULL, *unfiltered = NULL, *cur, *to_add = NULL;
92 gboolean printed = FALSE;
93 FolderItem *dropfolder;
95 g_return_val_if_fail(dest != NULL, -1);
96 g_return_val_if_fail(mbox != NULL, -1);
98 debug_print("Getting messages from %s into %s...\n", mbox, dest->path);
100 if ((mbox_fp = g_fopen(mbox, "rb")) == NULL) {
101 FILE_OP_ERROR(mbox, "fopen");
102 alertpanel_error(_("Could not open mbox file:\n%s\n"), mbox);
106 /* ignore empty lines on the head */
108 if (fgets(buf, sizeof(buf), mbox_fp) == NULL) {
109 g_warning("can't read mbox file.\n");
113 } while (buf[0] == '\n' || buf[0] == '\r');
115 if (strncmp(buf, "From ", 5) != 0) {
116 g_warning("invalid mbox format: %s\n", mbox);
121 tmp_file = get_tmp_file();
123 folder_item_update_freeze();
126 dropfolder = folder_get_default_processing();
135 if (msgs > 0 && msgs%500 == 0) {
138 statusbar_print_all(_("Importing from mbox... (%d mails imported)"), msgs);
143 if ((tmp_fp = g_fopen(tmp_file, "wb")) == NULL) {
144 FILE_OP_ERROR(tmp_file, "fopen");
145 g_warning("can't open temporary file\n");
150 if (change_file_mode_rw(tmp_fp, tmp_file) < 0) {
151 FILE_OP_ERROR(tmp_file, "chmod");
158 /* process all lines from mboxrc file */
159 while (fgets(buf, sizeof(buf), mbox_fp) != NULL) {
162 /* eof not reached, expect more lines */
165 /* eat empty lines */
166 if (buf[0] == '\n' || buf[0] == '\r') {
171 /* From separator or quoted From */
173 /* detect leading '>' char(s) */
174 while ((buf[offset] == '>')) {
177 if (!strncmp(buf+offset, "From ", 5)) {
178 /* From separator: */
180 /* expect next mbox item */
185 /* flush any eaten empty line */
186 if (empty_lines > 0) {
187 while (empty_lines-- > 0) {
188 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
192 /* store the unquoted line */
193 FPUTS_TO_TMP_ABORT_IF_FAIL(buf + 1);
198 /* flush any eaten empty line */
199 if (empty_lines > 0) {
200 while (empty_lines-- > 0) {
201 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
205 /* store the line itself */
206 FPUTS_TO_TMP_ABORT_IF_FAIL(buf);
208 /* end of mbox item or end of mbox */
210 /* flush any eaten empty line (but the last one) */
211 if (empty_lines > 0) {
212 while (--empty_lines > 0) {
213 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
217 /* more emails to expect? */
218 more = !feof(mbox_fp);
220 /* warn if email part is empty (it's the minimum check
223 g_warning("malformed mbox: %s: message %d is empty\n", mbox, msgs);
230 if (fclose(tmp_fp) == EOF) {
231 FILE_OP_ERROR(tmp_file, "fclose");
232 g_warning("can't write to temporary file\n");
240 if ((msgnum = folder_item_add_msg(dropfolder, tmp_file, NULL, TRUE)) < 0) {
246 msginfo = folder_item_get_msginfo(dropfolder, msgnum);
247 to_filter = g_slist_prepend(to_filter, msginfo);
249 MsgFileInfo *finfo = g_new0(MsgFileInfo, 1);
250 finfo->file = tmp_file;
252 to_add = g_slist_prepend(to_add, finfo);
253 tmp_file = get_tmp_file();
255 /* flush every 500 */
256 if (msgs > 0 && msgs % 500 == 0) {
257 folder_item_add_msgs(dropfolder, to_add, TRUE);
258 procmsg_message_file_list_free(to_add);
269 procmsg_msglist_filter(to_filter, account,
270 &filtered, &unfiltered, TRUE);
272 filtering_move_and_copy_msgs(to_filter);
273 for (cur = filtered; cur; cur = g_slist_next(cur)) {
274 MsgInfo *info = (MsgInfo *)cur->data;
275 procmsg_msginfo_free(info);
278 unfiltered = g_slist_reverse(unfiltered);
279 folder_item_move_msgs(dest, unfiltered);
280 for (cur = unfiltered; cur; cur = g_slist_next(cur)) {
281 MsgInfo *info = (MsgInfo *)cur->data;
282 procmsg_msginfo_free(info);
285 g_slist_free(unfiltered);
286 g_slist_free(filtered);
287 g_slist_free(to_filter);
289 folder_item_add_msgs(dropfolder, to_add, TRUE);
290 procmsg_message_file_list_free(to_add);
294 folder_item_update_thaw();
298 debug_print("%d messages found.\n", msgs);
303 gint lock_mbox(const gchar *base, LockType type)
308 if (type == LOCK_FILE) {
309 gchar *lockfile, *locklink;
313 lockfile = g_strdup_printf("%s.%d", base, getpid());
314 if ((lockfp = g_fopen(lockfile, "wb")) == NULL) {
315 FILE_OP_ERROR(lockfile, "fopen");
316 g_warning("can't create lock file %s\n", lockfile);
317 g_warning("use 'flock' instead of 'file' if possible.\n");
322 if (fprintf(lockfp, "%d\n", getpid()) < 0 ||
323 fclose(lockfp) == EOF) {
324 FILE_OP_ERROR(lockfile, "fopen||fclose");
329 locklink = g_strconcat(base, ".lock", NULL);
330 while (link(lockfile, locklink) < 0) {
331 FILE_OP_ERROR(lockfile, "link");
333 g_warning("can't create %s\n", lockfile);
339 g_warning("mailbox is owned by another"
340 " process, waiting...\n");
346 } else if (type == LOCK_FLOCK) {
348 gboolean fcntled = FALSE;
349 #if HAVE_FCNTL_H && !defined(G_OS_WIN32)
352 fl.l_whence = SEEK_SET;
358 if ((lockfd = open(base, O_RDWR)) < 0) {
360 if ((lockfd = open(base, O_RDWR)) < 0) {
362 FILE_OP_ERROR(base, "open");
366 #if HAVE_FCNTL_H && !defined(G_OS_WIN32)
367 if (fcntl(lockfd, F_SETLK, &fl) == -1) {
368 g_warning("can't fnctl %s (%s)", base, strerror(errno));
376 if (flock(lockfd, LOCK_EX|LOCK_NB) < 0 && !fcntled) {
380 if (lockf(lockfd, F_TLOCK, 0) < 0 && !fcntled) {
385 #endif /* HAVE_FLOCK */
386 g_warning("can't lock %s\n", base);
387 if (close(lockfd) < 0)
393 g_warning("invalid lock type\n");
400 #endif /* G_OS_UNIX */
403 gint unlock_mbox(const gchar *base, gint fd, LockType type)
405 if (type == LOCK_FILE) {
408 lockfile = g_strconcat(base, ".lock", NULL);
409 if (g_unlink(lockfile) < 0) {
410 FILE_OP_ERROR(lockfile, "unlink");
417 } else if (type == LOCK_FLOCK) {
418 gboolean fcntled = FALSE;
419 #if HAVE_FCNTL_H && !defined(G_OS_WIN32)
422 fl.l_whence = SEEK_SET;
426 if (fcntl(fd, F_SETLK, &fl) == -1) {
427 g_warning("can't fnctl %s", base);
433 if (flock(fd, LOCK_UN) < 0 && !fcntled) {
437 if (lockf(fd, F_ULOCK, 0) < 0 && !fcntled) {
442 #endif /* HAVE_FLOCK */
443 g_warning("can't unlock %s\n", base);
457 g_warning("invalid lock type\n");
461 gint copy_mbox(gint srcfd, const gchar *dest)
466 gboolean err = FALSE;
473 if ((dest_fp = g_fopen(dest, "wb")) == NULL) {
474 FILE_OP_ERROR(dest, "fopen");
478 if (change_file_mode_rw(dest_fp, dest) < 0) {
479 FILE_OP_ERROR(dest, "chmod");
480 g_warning("can't change file mode\n");
483 while ((n_read = read(srcfd, buf, sizeof(buf))) > 0) {
484 if (n_read == -1 && errno != 0) {
488 if (fwrite(buf, 1, n_read, dest_fp) < n_read) {
489 g_warning("writing to %s failed.\n", dest);
496 if (save_errno != 0) {
497 g_warning("error %d reading mbox: %s\n", save_errno,
498 strerror(save_errno));
502 if (fclose(dest_fp) == EOF) {
503 FILE_OP_ERROR(dest, "fclose");
515 void empty_mbox(const gchar *mbox)
519 if ((fp = g_fopen(mbox, "wb")) == NULL) {
520 FILE_OP_ERROR(mbox, "fopen");
521 g_warning("can't truncate mailbox to zero.\n");
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, FALSE,
542 NULL, ALERT_WARNING, G_ALERTDEFAULT)
543 != G_ALERTALTERNATE) {
548 if ((mbox_fp = g_fopen(mbox, "wb")) == NULL) {
549 FILE_OP_ERROR(mbox, "fopen");
550 alertpanel_error(_("Could not create mbox file:\n%s\n"), mbox);
554 #ifdef HAVE_FGETS_UNLOCKED
558 statuswindow_print_all(_("Exporting to mbox..."));
559 for (cur = mlist; cur != NULL; cur = cur->next) {
561 gchar buft[BUFFSIZE];
562 msginfo = (MsgInfo *)cur->data;
564 msg_fp = procmsg_open_message(msginfo);
569 #ifdef HAVE_FGETS_UNLOCKED
573 msginfo->from ? msginfo->from :
574 cur_account && cur_account->address ?
575 cur_account->address : "unknown",
577 extract_address(buf);
580 if (fprintf(mbox_fp, "From %s %s",
581 buf, ctime_r(&msginfo->date_t, buft, sizeof(buft))) < 0) {
583 if (fprintf(mbox_fp, "From %s %s",
584 buf, ctime_r(&msginfo->date_t, buft)) < 0) {
587 #ifdef HAVE_FGETS_UNLOCKED
596 /* write email to mboxrc */
597 while (SC_FGETS(buf, sizeof(buf), msg_fp) != NULL) {
598 /* quote any From, >From, >>From, etc., according to mbox format specs */
602 /* detect leading '>' char(s) */
603 while ((buf[offset] == '>')) {
606 if (!strncmp(buf+offset, "From ", 5)) {
607 if (SC_FPUTC('>', mbox_fp) == EOF) {
609 #ifdef HAVE_FGETS_UNLOCKED
616 if (SC_FPUTS(buf, mbox_fp) == EOF) {
618 #ifdef HAVE_FGETS_UNLOCKED
626 /* force last line to end w/ a newline */
630 if ((buf[len] != '\n') && (buf[len] != '\r')) {
631 if (SC_FPUTC('\n', mbox_fp) == EOF) {
633 #ifdef HAVE_FGETS_UNLOCKED
642 /* add a trailing empty line */
643 if (SC_FPUTC('\n', mbox_fp) == EOF) {
645 #ifdef HAVE_FGETS_UNLOCKED
652 #ifdef HAVE_FGETS_UNLOCKED
656 statusbar_progress_all(msgs++,total, 500);
662 statusbar_progress_all(0,0,0);
663 statuswindow_pop_all();
665 #ifdef HAVE_FGETS_UNLOCKED
666 funlockfile(mbox_fp);
673 /* read all messages in SRC, and store them into one MBOX file. */
674 /* return values: -2 skipped, -1 error, 0 OK */
675 gint export_to_mbox(FolderItem *src, const gchar *mbox)
680 g_return_val_if_fail(src != NULL, -1);
681 g_return_val_if_fail(src->folder != NULL, -1);
682 g_return_val_if_fail(mbox != NULL, -1);
684 debug_print("Exporting messages from %s into %s...\n",
687 mlist = folder_item_get_msg_list(src);
689 folder_item_update_freeze();
690 ret = export_list_to_mbox(mlist, mbox);
691 folder_item_update_thaw();
693 procmsg_msg_list_free(mlist);