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"
31 #include <glib/gi18n.h>
48 #include "prefs_common.h"
49 #include "prefs_account.h"
52 #include "filtering.h"
53 #include "alertpanel.h"
54 #include "statusbar.h"
56 #define MESSAGEBUFSIZE 8192
58 #ifdef HAVE_FGETS_UNLOCKED
59 #define SC_FGETS fgets_unlocked
60 #define SC_FPUTS fputs_unlocked
61 #define SC_FPUTC fputc_unlocked
63 #define SC_FGETS fgets
64 #define SC_FPUTS fputs
65 #define SC_FPUTC fputc
68 #define FPUTS_TO_TMP_ABORT_IF_FAIL(s) \
71 if (fputs(s, tmp_fp) == EOF) { \
72 g_warning("can't write to temporary file\n"); \
75 claws_unlink(tmp_file); \
81 gint proc_mbox(FolderItem *dest, const gchar *mbox, gboolean apply_filter,
82 PrefsAccount *account)
83 /* return values: -1 error, >=0 number of msgs added */
86 gchar buf[MESSAGEBUFSIZE];
92 GSList *to_filter = NULL, *filtered = NULL, *unfiltered = NULL, *cur, *to_add = NULL;
93 gboolean printed = FALSE;
94 FolderItem *dropfolder;
96 cm_return_val_if_fail(dest != NULL, -1);
97 cm_return_val_if_fail(mbox != NULL, -1);
99 debug_print("Getting messages from %s into %s...\n", mbox, dest->path);
101 if ((mbox_fp = g_fopen(mbox, "rb")) == NULL) {
102 FILE_OP_ERROR(mbox, "fopen");
103 alertpanel_error(_("Could not open mbox file:\n%s\n"), mbox);
107 /* ignore empty lines on the head */
109 if (fgets(buf, sizeof(buf), mbox_fp) == NULL) {
110 g_warning("can't read mbox file.\n");
114 } while (buf[0] == '\n' || buf[0] == '\r');
116 if (strncmp(buf, "From ", 5) != 0) {
117 g_warning("invalid mbox format: %s\n", mbox);
122 tmp_file = get_tmp_file();
124 folder_item_update_freeze();
127 dropfolder = folder_get_default_processing();
136 if (msgs > 0 && msgs%500 == 0) {
139 statusbar_print_all(_("Importing from mbox... (%d mails imported)"), msgs);
144 if ((tmp_fp = g_fopen(tmp_file, "wb")) == NULL) {
145 FILE_OP_ERROR(tmp_file, "fopen");
146 g_warning("can't open temporary file\n");
151 if (change_file_mode_rw(tmp_fp, tmp_file) < 0) {
152 FILE_OP_ERROR(tmp_file, "chmod");
158 /* process all lines from mboxrc file */
159 while (fgets(buf, sizeof(buf), mbox_fp) != NULL) {
162 /* eat empty lines */
163 if (buf[0] == '\n' || buf[0] == '\r') {
168 /* From separator or quoted From */
170 /* detect leading '>' char(s) */
171 while ((buf[offset] == '>')) {
174 if (!strncmp(buf+offset, "From ", 5)) {
175 /* From separator: */
177 /* expect next mbox item */
182 /* flush any eaten empty line */
183 if (empty_lines > 0) {
184 while (empty_lines-- > 0) {
185 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
189 /* store the unquoted line */
190 FPUTS_TO_TMP_ABORT_IF_FAIL(buf + 1);
195 /* flush any eaten empty line */
196 if (empty_lines > 0) {
197 while (empty_lines-- > 0) {
198 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
202 /* store the line itself */
203 FPUTS_TO_TMP_ABORT_IF_FAIL(buf);
205 /* end of mbox item or end of mbox */
207 /* flush any eaten empty line (but the last one) */
208 if (empty_lines > 0) {
209 while (--empty_lines > 0) {
210 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
214 /* more emails to expect? */
215 more = !feof(mbox_fp);
217 /* warn if email part is empty (it's the minimum check
220 g_warning("malformed mbox: %s: message %d is empty\n", mbox, msgs);
223 claws_unlink(tmp_file);
227 if (fclose(tmp_fp) == EOF) {
228 FILE_OP_ERROR(tmp_file, "fclose");
229 g_warning("can't write to temporary file\n");
231 claws_unlink(tmp_file);
237 if ((msgnum = folder_item_add_msg(dropfolder, tmp_file, NULL, TRUE)) < 0) {
239 claws_unlink(tmp_file);
243 msginfo = folder_item_get_msginfo(dropfolder, msgnum);
244 to_filter = g_slist_prepend(to_filter, msginfo);
246 MsgFileInfo *finfo = g_new0(MsgFileInfo, 1);
247 finfo->file = tmp_file;
249 to_add = g_slist_prepend(to_add, finfo);
250 tmp_file = get_tmp_file();
252 /* flush every 500 */
253 if (msgs > 0 && msgs % 500 == 0) {
254 folder_item_add_msgs(dropfolder, to_add, TRUE);
255 procmsg_message_file_list_free(to_add);
267 folder_item_set_batch(dropfolder, FALSE);
268 procmsg_msglist_filter(to_filter, account,
269 &filtered, &unfiltered, TRUE);
270 folder_item_set_batch(dropfolder, 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);
280 folder_item_move_msgs(dest, unfiltered);
281 for (cur = unfiltered; cur; cur = g_slist_next(cur)) {
282 MsgInfo *info = (MsgInfo *)cur->data;
283 procmsg_msginfo_free(info);
287 g_slist_free(unfiltered);
288 g_slist_free(filtered);
289 g_slist_free(to_filter);
291 folder_item_add_msgs(dropfolder, to_add, TRUE);
292 procmsg_message_file_list_free(to_add);
296 folder_item_update_thaw();
300 debug_print("%d messages found.\n", msgs);
305 gint lock_mbox(const gchar *base, LockType type)
310 if (type == LOCK_FILE) {
311 gchar *lockfile, *locklink;
315 lockfile = g_strdup_printf("%s.%d", base, getpid());
316 if ((lockfp = g_fopen(lockfile, "wb")) == NULL) {
317 FILE_OP_ERROR(lockfile, "fopen");
318 g_warning("can't create lock file %s\n", lockfile);
319 g_warning("use 'flock' instead of 'file' if possible.\n");
324 if (fprintf(lockfp, "%d\n", getpid()) < 0) {
325 FILE_OP_ERROR(lockfile, "fprintf");
331 if (fclose(lockfp) == EOF) {
332 FILE_OP_ERROR(lockfile, "fclose");
337 locklink = g_strconcat(base, ".lock", NULL);
338 while (link(lockfile, locklink) < 0) {
339 FILE_OP_ERROR(lockfile, "link");
341 g_warning("can't create %s\n", lockfile);
342 claws_unlink(lockfile);
347 g_warning("mailbox is owned by another"
348 " process, waiting...\n");
352 claws_unlink(lockfile);
354 } else if (type == LOCK_FLOCK) {
356 gboolean fcntled = FALSE;
357 #if HAVE_FCNTL_H && !defined(G_OS_WIN32)
360 fl.l_whence = SEEK_SET;
366 if ((lockfd = g_open(base, O_RDWR, 0)) < 0) {
368 if ((lockfd = g_open(base, O_RDWR, 0)) < 0) {
370 FILE_OP_ERROR(base, "open");
374 #if HAVE_FCNTL_H && !defined(G_OS_WIN32)
375 if (fcntl(lockfd, F_SETLK, &fl) == -1) {
376 g_warning("can't fnctl %s (%s)", base, strerror(errno));
384 if (flock(lockfd, LOCK_EX|LOCK_NB) < 0 && !fcntled) {
388 if (lockf(lockfd, F_TLOCK, 0) < 0 && !fcntled) {
393 #endif /* HAVE_FLOCK */
394 g_warning("can't lock %s\n", base);
395 if (close(lockfd) < 0)
401 g_warning("invalid lock type\n");
408 #endif /* G_OS_UNIX */
411 gint unlock_mbox(const gchar *base, gint fd, LockType type)
413 if (type == LOCK_FILE) {
416 lockfile = g_strconcat(base, ".lock", NULL);
417 if (claws_unlink(lockfile) < 0) {
418 FILE_OP_ERROR(lockfile, "unlink");
425 } else if (type == LOCK_FLOCK) {
426 gboolean fcntled = FALSE;
427 #if HAVE_FCNTL_H && !defined(G_OS_WIN32)
430 fl.l_whence = SEEK_SET;
434 if (fcntl(fd, F_SETLK, &fl) == -1) {
435 g_warning("can't fnctl %s", base);
441 if (flock(fd, LOCK_UN) < 0 && !fcntled) {
445 if (lockf(fd, F_ULOCK, 0) < 0 && !fcntled) {
450 #endif /* HAVE_FLOCK */
451 g_warning("can't unlock %s\n", base);
465 g_warning("invalid lock type\n");
469 gint copy_mbox(gint srcfd, const gchar *dest)
474 gboolean err = FALSE;
481 if ((dest_fp = g_fopen(dest, "wb")) == NULL) {
482 FILE_OP_ERROR(dest, "fopen");
486 if (change_file_mode_rw(dest_fp, dest) < 0) {
487 FILE_OP_ERROR(dest, "chmod");
488 g_warning("can't change file mode\n");
491 while ((n_read = read(srcfd, buf, sizeof(buf))) > 0) {
492 if (n_read == -1 && errno != 0) {
496 if (fwrite(buf, 1, n_read, dest_fp) < n_read) {
497 g_warning("writing to %s failed.\n", dest);
504 if (save_errno != 0) {
505 g_warning("error %d reading mbox: %s\n", save_errno,
506 strerror(save_errno));
510 if (fclose(dest_fp) == EOF) {
511 FILE_OP_ERROR(dest, "fclose");
523 void empty_mbox(const gchar *mbox)
527 if ((fp = g_fopen(mbox, "wb")) == NULL) {
528 FILE_OP_ERROR(mbox, "fopen");
529 g_warning("can't truncate mailbox to zero.\n");
535 gint export_list_to_mbox(GSList *mlist, const gchar *mbox)
536 /* return values: -2 skipped, -1 error, 0 OK */
545 gint msgs = 1, total = g_slist_length(mlist);
546 if (g_file_test(mbox, G_FILE_TEST_EXISTS) == TRUE) {
547 if (alertpanel_full(_("Overwrite mbox file"),
548 _("This file already exists. Do you want to overwrite it?"),
549 GTK_STOCK_CANCEL, _("Overwrite"), NULL, FALSE,
550 NULL, ALERT_WARNING, G_ALERTDEFAULT)
551 != G_ALERTALTERNATE) {
556 if ((mbox_fp = g_fopen(mbox, "wb")) == NULL) {
557 FILE_OP_ERROR(mbox, "fopen");
558 alertpanel_error(_("Could not create mbox file:\n%s\n"), mbox);
562 #ifdef HAVE_FGETS_UNLOCKED
566 statuswindow_print_all(_("Exporting to mbox..."));
567 for (cur = mlist; cur != NULL; cur = cur->next) {
569 gchar buft[BUFFSIZE];
570 msginfo = (MsgInfo *)cur->data;
572 msg_fp = procmsg_open_message(msginfo);
577 #ifdef HAVE_FGETS_UNLOCKED
581 msginfo->from ? msginfo->from :
582 cur_account && cur_account->address ?
583 cur_account->address : "unknown",
585 extract_address(buf);
587 if (fprintf(mbox_fp, "From %s %s",
588 buf, ctime_r(&msginfo->date_t, buft)) < 0) {
590 #ifdef HAVE_FGETS_UNLOCKED
599 /* write email to mboxrc */
600 while (SC_FGETS(buf, sizeof(buf), msg_fp) != NULL) {
601 /* quote any From, >From, >>From, etc., according to mbox format specs */
605 /* detect leading '>' char(s) */
606 while ((buf[offset] == '>')) {
609 if (!strncmp(buf+offset, "From ", 5)) {
610 if (SC_FPUTC('>', mbox_fp) == EOF) {
612 #ifdef HAVE_FGETS_UNLOCKED
619 if (SC_FPUTS(buf, mbox_fp) == EOF) {
621 #ifdef HAVE_FGETS_UNLOCKED
629 /* force last line to end w/ a newline */
633 if ((buf[len] != '\n') && (buf[len] != '\r')) {
634 if (SC_FPUTC('\n', mbox_fp) == EOF) {
636 #ifdef HAVE_FGETS_UNLOCKED
645 /* add a trailing empty line */
646 if (SC_FPUTC('\n', mbox_fp) == EOF) {
648 #ifdef HAVE_FGETS_UNLOCKED
655 #ifdef HAVE_FGETS_UNLOCKED
659 statusbar_progress_all(msgs++,total, 500);
665 statusbar_progress_all(0,0,0);
666 statuswindow_pop_all();
668 #ifdef HAVE_FGETS_UNLOCKED
669 funlockfile(mbox_fp);
676 /* read all messages in SRC, and store them into one MBOX file. */
677 /* return values: -2 skipped, -1 error, 0 OK */
678 gint export_to_mbox(FolderItem *src, const gchar *mbox)
683 cm_return_val_if_fail(src != NULL, -1);
684 cm_return_val_if_fail(src->folder != NULL, -1);
685 cm_return_val_if_fail(mbox != NULL, -1);
687 debug_print("Exporting messages from %s into %s...\n",
690 mlist = folder_item_get_msg_list(src);
692 folder_item_update_freeze();
693 ret = export_list_to_mbox(mlist, mbox);
694 folder_item_update_thaw();
696 procmsg_msg_list_free(mlist);