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();
140 if (msgs > 0 && msgs%500 == 0) {
143 statusbar_print_all(_("Importing from mbox... (%d mails imported)"), msgs);
148 if ((tmp_fp = g_fopen(tmp_file, "wb")) == NULL) {
149 FILE_OP_ERROR(tmp_file, "fopen");
150 g_warning("can't open temporary file\n");
155 if (change_file_mode_rw(tmp_fp, tmp_file) < 0) {
156 FILE_OP_ERROR(tmp_file, "chmod");
162 /* process all lines from mboxrc file */
163 while (fgets(buf, sizeof(buf), mbox_fp) != NULL) {
166 /* eat empty lines */
167 if (buf[0] == '\n' || buf[0] == '\r') {
172 /* From separator or quoted From */
174 /* detect leading '>' char(s) */
175 while ((buf[offset] == '>')) {
178 if (!strncmp(buf+offset, "From ", 5)) {
179 /* From separator: */
181 /* expect next mbox item */
186 /* flush any eaten empty line */
187 if (empty_lines > 0) {
188 while (empty_lines-- > 0) {
189 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
193 /* store the unquoted line */
194 FPUTS_TO_TMP_ABORT_IF_FAIL(buf + 1);
199 /* flush any eaten empty line */
200 if (empty_lines > 0) {
201 while (empty_lines-- > 0) {
202 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
206 /* store the line itself */
207 FPUTS_TO_TMP_ABORT_IF_FAIL(buf);
209 /* end of mbox item or end of mbox */
211 /* flush any eaten empty line (but the last one) */
212 if (empty_lines > 0) {
213 while (--empty_lines > 0) {
214 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
218 /* more emails to expect? */
219 more = !feof(mbox_fp);
221 /* warn if email part is empty (it's the minimum check
224 g_warning("malformed mbox: %s: message %d is empty\n", mbox, msgs);
227 claws_unlink(tmp_file);
231 if (fclose(tmp_fp) == EOF) {
232 FILE_OP_ERROR(tmp_file, "fclose");
233 g_warning("can't write to temporary file\n");
235 claws_unlink(tmp_file);
241 if ((msgnum = folder_item_add_msg(dropfolder, tmp_file, NULL, TRUE)) < 0) {
243 claws_unlink(tmp_file);
247 msginfo = folder_item_get_msginfo(dropfolder, msgnum);
248 to_filter = g_slist_prepend(to_filter, msginfo);
250 MsgFileInfo *finfo = g_new0(MsgFileInfo, 1);
251 finfo->file = tmp_file;
253 to_add = g_slist_prepend(to_add, finfo);
254 tmp_file = get_tmp_file();
256 /* flush every 500 */
257 if (msgs > 0 && msgs % 500 == 0) {
258 folder_item_add_msgs(dropfolder, to_add, TRUE);
259 procmsg_message_file_list_free(to_add);
271 folder_item_set_batch(dropfolder, FALSE);
272 procmsg_msglist_filter(to_filter, account,
273 &filtered, &unfiltered, TRUE);
274 folder_item_set_batch(dropfolder, TRUE);
276 filtering_move_and_copy_msgs(to_filter);
277 for (cur = filtered; cur; cur = g_slist_next(cur)) {
278 MsgInfo *info = (MsgInfo *)cur->data;
279 procmsg_msginfo_free(info);
282 unfiltered = g_slist_reverse(unfiltered);
284 folder_item_move_msgs(dest, unfiltered);
285 for (cur = unfiltered; cur; cur = g_slist_next(cur)) {
286 MsgInfo *info = (MsgInfo *)cur->data;
287 procmsg_msginfo_free(info);
291 g_slist_free(unfiltered);
292 g_slist_free(filtered);
293 g_slist_free(to_filter);
295 folder_item_add_msgs(dropfolder, to_add, TRUE);
296 procmsg_message_file_list_free(to_add);
300 folder_item_update_thaw();
304 debug_print("%d messages found.\n", msgs);
309 gint lock_mbox(const gchar *base, LockType type)
314 if (type == LOCK_FILE) {
315 gchar *lockfile, *locklink;
319 lockfile = g_strdup_printf("%s.%d", base, getpid());
320 if ((lockfp = g_fopen(lockfile, "wb")) == NULL) {
321 FILE_OP_ERROR(lockfile, "fopen");
322 g_warning("can't create lock file %s\n", lockfile);
323 g_warning("use 'flock' instead of 'file' if possible.\n");
328 if (fprintf(lockfp, "%d\n", getpid()) < 0) {
329 FILE_OP_ERROR(lockfile, "fprintf");
335 if (fclose(lockfp) == EOF) {
336 FILE_OP_ERROR(lockfile, "fclose");
341 locklink = g_strconcat(base, ".lock", NULL);
342 while (link(lockfile, locklink) < 0) {
343 FILE_OP_ERROR(lockfile, "link");
345 g_warning("can't create %s\n", lockfile);
346 claws_unlink(lockfile);
351 g_warning("mailbox is owned by another"
352 " process, waiting...\n");
356 claws_unlink(lockfile);
358 } else if (type == LOCK_FLOCK) {
360 gboolean fcntled = FALSE;
361 #if HAVE_FCNTL_H && !defined(G_OS_WIN32)
364 fl.l_whence = SEEK_SET;
370 if ((lockfd = g_open(base, O_RDWR, 0)) < 0) {
372 if ((lockfd = g_open(base, O_RDWR, 0)) < 0) {
374 FILE_OP_ERROR(base, "open");
378 #if HAVE_FCNTL_H && !defined(G_OS_WIN32)
379 if (fcntl(lockfd, F_SETLK, &fl) == -1) {
380 g_warning("can't fnctl %s (%s)", base, 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\n", base);
399 if (close(lockfd) < 0)
405 g_warning("invalid lock type\n");
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 gboolean fcntled = FALSE;
431 #if HAVE_FCNTL_H && !defined(G_OS_WIN32)
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\n", base);
469 g_warning("invalid lock type\n");
473 gint copy_mbox(gint srcfd, const gchar *dest)
478 gboolean err = FALSE;
485 if ((dest_fp = g_fopen(dest, "wb")) == NULL) {
486 FILE_OP_ERROR(dest, "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\n");
495 while ((n_read = read(srcfd, buf, sizeof(buf))) > 0) {
496 if (n_read == -1 && errno != 0) {
500 if (fwrite(buf, 1, n_read, dest_fp) < n_read) {
501 g_warning("writing to %s failed.\n", dest);
508 if (save_errno != 0) {
509 g_warning("error %d reading mbox: %s\n", save_errno,
510 strerror(save_errno));
514 if (fclose(dest_fp) == EOF) {
515 FILE_OP_ERROR(dest, "fclose");
527 void empty_mbox(const gchar *mbox)
531 if ((fp = g_fopen(mbox, "wb")) == NULL) {
532 FILE_OP_ERROR(mbox, "fopen");
533 g_warning("can't truncate mailbox to zero.\n");
539 gint export_list_to_mbox(GSList *mlist, const gchar *mbox)
540 /* return values: -2 skipped, -1 error, 0 OK */
549 gint msgs = 1, total = g_slist_length(mlist);
550 if (g_file_test(mbox, G_FILE_TEST_EXISTS) == TRUE) {
551 if (alertpanel_full(_("Overwrite mbox file"),
552 _("This file already exists. Do you want to overwrite it?"),
553 GTK_STOCK_CANCEL, _("Overwrite"), NULL, FALSE,
554 NULL, ALERT_WARNING, G_ALERTDEFAULT)
555 != G_ALERTALTERNATE) {
560 if ((mbox_fp = g_fopen(mbox, "wb")) == NULL) {
561 FILE_OP_ERROR(mbox, "fopen");
562 alertpanel_error(_("Could not create mbox file:\n%s\n"), mbox);
566 #ifdef HAVE_FGETS_UNLOCKED
570 statuswindow_print_all(_("Exporting to mbox..."));
571 for (cur = mlist; cur != NULL; cur = cur->next) {
573 gchar buft[BUFFSIZE];
574 msginfo = (MsgInfo *)cur->data;
576 msg_fp = procmsg_open_message(msginfo);
581 #ifdef HAVE_FGETS_UNLOCKED
585 msginfo->from ? msginfo->from :
586 cur_account && cur_account->address ?
587 cur_account->address : "unknown",
589 extract_address(buf);
591 if (fprintf(mbox_fp, "From %s %s",
592 buf, ctime_r(&msginfo->date_t, buft)) < 0) {
594 #ifdef HAVE_FGETS_UNLOCKED
603 /* write email to mboxrc */
604 while (SC_FGETS(buf, sizeof(buf), msg_fp) != NULL) {
605 /* quote any From, >From, >>From, etc., according to mbox format specs */
609 /* detect leading '>' char(s) */
610 while ((buf[offset] == '>')) {
613 if (!strncmp(buf+offset, "From ", 5)) {
614 if (SC_FPUTC('>', mbox_fp) == EOF) {
616 #ifdef HAVE_FGETS_UNLOCKED
623 if (SC_FPUTS(buf, mbox_fp) == EOF) {
625 #ifdef HAVE_FGETS_UNLOCKED
633 /* force last line to end w/ a newline */
637 if ((buf[len] != '\n') && (buf[len] != '\r')) {
638 if (SC_FPUTC('\n', mbox_fp) == EOF) {
640 #ifdef HAVE_FGETS_UNLOCKED
649 /* add a trailing empty line */
650 if (SC_FPUTC('\n', mbox_fp) == EOF) {
652 #ifdef HAVE_FGETS_UNLOCKED
659 #ifdef HAVE_FGETS_UNLOCKED
663 statusbar_progress_all(msgs++,total, 500);
669 statusbar_progress_all(0,0,0);
670 statuswindow_pop_all();
672 #ifdef HAVE_FGETS_UNLOCKED
673 funlockfile(mbox_fp);
680 /* read all messages in SRC, and store them into one MBOX file. */
681 /* return values: -2 skipped, -1 error, 0 OK */
682 gint export_to_mbox(FolderItem *src, const gchar *mbox)
687 cm_return_val_if_fail(src != NULL, -1);
688 cm_return_val_if_fail(src->folder != NULL, -1);
689 cm_return_val_if_fail(mbox != NULL, -1);
691 debug_print("Exporting messages from %s into %s...\n",
694 mlist = folder_item_get_msg_list(src);
696 folder_item_update_freeze();
697 ret = export_list_to_mbox(mlist, mbox);
698 folder_item_update_thaw();
700 procmsg_msg_list_free(mlist);