2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2012 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\n"); \
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.\n");
118 } while (buf[0] == '\n' || buf[0] == '\r');
120 if (strncmp(buf, "From ", 5) != 0) {
121 g_warning("invalid mbox format: %s\n", 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\n");
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\n", 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\n");
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\n", lockfile);
325 g_warning("use 'flock' instead of 'file' if possible.\n");
330 if (fprintf(lockfp, "%d\n", getpid()) < 0) {
331 FILE_OP_ERROR(lockfile, "fprintf");
337 if (fclose(lockfp) == EOF) {
338 FILE_OP_ERROR(lockfile, "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\n", lockfile);
348 claws_unlink(lockfile);
353 g_warning("mailbox is owned by another"
354 " process, waiting...\n");
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, 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\n", base);
402 if (close(lockfd) < 0)
408 g_warning("invalid lock type\n");
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 gboolean fcntled = FALSE;
434 #if HAVE_FCNTL_H && !defined(G_OS_WIN32)
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\n", base);
472 g_warning("invalid lock type\n");
476 gint copy_mbox(gint srcfd, const gchar *dest)
481 gboolean err = FALSE;
488 if ((dest_fp = g_fopen(dest, "wb")) == NULL) {
489 FILE_OP_ERROR(dest, "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\n");
498 while ((n_read = read(srcfd, buf, sizeof(buf))) > 0) {
499 if (n_read == -1 && errno != 0) {
503 if (fwrite(buf, 1, n_read, dest_fp) < n_read) {
504 g_warning("writing to %s failed.\n", dest);
511 if (save_errno != 0) {
512 g_warning("error %d reading mbox: %s\n", save_errno,
513 strerror(save_errno));
517 if (fclose(dest_fp) == EOF) {
518 FILE_OP_ERROR(dest, "fclose");
530 void empty_mbox(const gchar *mbox)
534 if ((fp = g_fopen(mbox, "wb")) == NULL) {
535 FILE_OP_ERROR(mbox, "fopen");
536 g_warning("can't truncate mailbox to zero.\n");
542 gint export_list_to_mbox(GSList *mlist, const gchar *mbox)
543 /* return values: -2 skipped, -1 error, 0 OK */
552 gint msgs = 1, total = g_slist_length(mlist);
553 if (g_file_test(mbox, G_FILE_TEST_EXISTS) == TRUE) {
554 if (alertpanel_full(_("Overwrite mbox file"),
555 _("This file already exists. Do you want to overwrite it?"),
556 GTK_STOCK_CANCEL, _("Overwrite"), NULL, FALSE,
557 NULL, ALERT_WARNING, G_ALERTDEFAULT)
558 != G_ALERTALTERNATE) {
563 if ((mbox_fp = g_fopen(mbox, "wb")) == NULL) {
564 FILE_OP_ERROR(mbox, "fopen");
565 alertpanel_error(_("Could not create mbox file:\n%s\n"), mbox);
569 #ifdef HAVE_FGETS_UNLOCKED
573 statuswindow_print_all(_("Exporting to mbox..."));
574 for (cur = mlist; cur != NULL; cur = cur->next) {
576 gchar buft[BUFFSIZE];
577 msginfo = (MsgInfo *)cur->data;
579 msg_fp = procmsg_open_message(msginfo);
584 #ifdef HAVE_FGETS_UNLOCKED
588 msginfo->from ? msginfo->from :
589 cur_account && cur_account->address ?
590 cur_account->address : "unknown",
592 extract_address(buf);
594 if (fprintf(mbox_fp, "From %s %s",
595 buf, ctime_r(&msginfo->date_t, buft)) < 0) {
597 #ifdef HAVE_FGETS_UNLOCKED
606 /* write email to mboxrc */
607 while (SC_FGETS(buf, sizeof(buf), msg_fp) != NULL) {
608 /* quote any From, >From, >>From, etc., according to mbox format specs */
612 /* detect leading '>' char(s) */
613 while ((buf[offset] == '>')) {
616 if (!strncmp(buf+offset, "From ", 5)) {
617 if (SC_FPUTC('>', mbox_fp) == EOF) {
619 #ifdef HAVE_FGETS_UNLOCKED
626 if (SC_FPUTS(buf, mbox_fp) == EOF) {
628 #ifdef HAVE_FGETS_UNLOCKED
636 /* force last line to end w/ a newline */
640 if ((buf[len] != '\n') && (buf[len] != '\r')) {
641 if (SC_FPUTC('\n', mbox_fp) == EOF) {
643 #ifdef HAVE_FGETS_UNLOCKED
652 /* add a trailing empty line */
653 if (SC_FPUTC('\n', mbox_fp) == EOF) {
655 #ifdef HAVE_FGETS_UNLOCKED
662 #ifdef HAVE_FGETS_UNLOCKED
666 statusbar_progress_all(msgs++,total, 500);
672 statusbar_progress_all(0,0,0);
673 statuswindow_pop_all();
675 #ifdef HAVE_FGETS_UNLOCKED
676 funlockfile(mbox_fp);
683 /* read all messages in SRC, and store them into one MBOX file. */
684 /* return values: -2 skipped, -1 error, 0 OK */
685 gint export_to_mbox(FolderItem *src, const gchar *mbox)
690 cm_return_val_if_fail(src != NULL, -1);
691 cm_return_val_if_fail(src->folder != NULL, -1);
692 cm_return_val_if_fail(mbox != NULL, -1);
694 debug_print("Exporting messages from %s into %s...\n",
697 mlist = folder_item_get_msg_list(src);
699 folder_item_update_freeze();
700 ret = export_list_to_mbox(mlist, mbox);
701 folder_item_update_thaw();
703 procmsg_msg_list_free(mlist);