2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws 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.
27 #include <glib/gi18n.h>
40 #include "prefs_common.h"
41 #include "prefs_account.h"
44 #include "filtering.h"
45 #include "alertpanel.h"
46 #include "statusbar.h"
48 #define MSGBUFSIZE 8192
50 #define FPUTS_TO_TMP_ABORT_IF_FAIL(s) \
53 if (fputs(s, tmp_fp) == EOF) { \
54 g_warning("can't write to temporary file\n"); \
63 gint proc_mbox(FolderItem *dest, const gchar *mbox, gboolean apply_filter)
64 /* return values: -1 error, >=0 number of msgs added */
67 gchar buf[MSGBUFSIZE];
73 GSList *to_filter = NULL, *filtered = NULL, *unfiltered = NULL, *cur, *to_add = NULL;
74 gboolean printed = FALSE;
75 FolderItem *dropfolder;
77 g_return_val_if_fail(dest != NULL, -1);
78 g_return_val_if_fail(mbox != NULL, -1);
80 debug_print("Getting messages from %s into %s...\n", mbox, dest->path);
82 if ((mbox_fp = g_fopen(mbox, "rb")) == NULL) {
83 FILE_OP_ERROR(mbox, "fopen");
84 alertpanel_error(_("Could not open mbox file:\n%s\n"), mbox);
88 /* ignore empty lines on the head */
90 if (fgets(buf, sizeof(buf), mbox_fp) == NULL) {
91 g_warning("can't read mbox file.\n");
95 } while (buf[0] == '\n' || buf[0] == '\r');
97 if (strncmp(buf, "From ", 5) != 0) {
98 g_warning("invalid mbox format: %s\n", mbox);
103 tmp_file = get_tmp_file();
105 folder_item_update_freeze();
108 dropfolder = folder_get_default_processing();
117 if (msgs > 0 && msgs%500 == 0) {
120 statusbar_print_all(_("Importing from mbox... (%d mails imported)"), msgs);
125 if ((tmp_fp = g_fopen(tmp_file, "wb")) == NULL) {
126 FILE_OP_ERROR(tmp_file, "fopen");
127 g_warning("can't open temporary file\n");
132 if (change_file_mode_rw(tmp_fp, tmp_file) < 0) {
133 FILE_OP_ERROR(tmp_file, "chmod");
140 /* process all lines from mboxrc file */
141 while (fgets(buf, sizeof(buf), mbox_fp) != NULL) {
144 /* eof not reached, expect more lines */
147 /* eat empty lines */
148 if (buf[0] == '\n' || buf[0] == '\r') {
153 /* From separator or quoted From */
155 /* detect leading '>' char(s) */
156 while ((buf[offset] == '>')) {
159 if (!strncmp(buf+offset, "From ", 5)) {
160 /* From separator: */
162 /* expect next mbox item */
167 /* flush any eaten empty line */
168 if (empty_lines > 0) {
169 while (empty_lines-- > 0) {
170 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
174 /* store the unquoted line */
175 FPUTS_TO_TMP_ABORT_IF_FAIL(buf + 1);
180 /* flush any eaten empty line */
181 if (empty_lines > 0) {
182 while (empty_lines-- > 0) {
183 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
187 /* store the line itself */
188 FPUTS_TO_TMP_ABORT_IF_FAIL(buf);
190 /* end of mbox item or end of mbox */
192 /* flush any eaten empty line (but the last one) */
193 if (empty_lines > 0) {
194 while (--empty_lines > 0) {
195 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
199 /* more emails to expect? */
200 more = !feof(mbox_fp);
202 /* warn if email part is empty (it's the minimum check
205 g_warning("malformed mbox: %s: message %d is empty\n", mbox, msgs);
212 if (fclose(tmp_fp) == EOF) {
213 FILE_OP_ERROR(tmp_file, "fclose");
214 g_warning("can't write to temporary file\n");
222 if ((msgnum = folder_item_add_msg(dropfolder, tmp_file, NULL, TRUE)) < 0) {
228 msginfo = folder_item_get_msginfo(dropfolder, msgnum);
229 to_filter = g_slist_prepend(to_filter, msginfo);
231 MsgFileInfo *finfo = g_new0(MsgFileInfo, 1);
232 finfo->file = tmp_file;
234 to_add = g_slist_prepend(to_add, finfo);
235 tmp_file = get_tmp_file();
237 /* flush every 500 */
238 if (msgs > 0 && msgs % 500 == 0) {
239 folder_item_add_msgs(dropfolder, to_add, TRUE);
240 procmsg_message_file_list_free(to_add);
251 procmsg_msglist_filter(to_filter, NULL, &filtered, &unfiltered, TRUE);
252 unfiltered = g_slist_reverse(unfiltered);
253 folder_item_move_msgs(dest, unfiltered);
254 for (cur = unfiltered; cur; cur = g_slist_next(cur)) {
255 MsgInfo *info = (MsgInfo *)cur->data;
256 procmsg_msginfo_free(info);
259 filtered = g_slist_reverse(filtered);
260 filtering_move_and_copy_msgs(filtered);
261 for (cur = filtered; cur; cur = g_slist_next(cur)) {
262 MsgInfo *info = (MsgInfo *)cur->data;
263 procmsg_msginfo_free(info);
266 g_slist_free(unfiltered);
267 g_slist_free(filtered);
268 g_slist_free(to_filter);
270 folder_item_add_msgs(dropfolder, to_add, TRUE);
271 procmsg_message_file_list_free(to_add);
275 folder_item_update_thaw();
279 debug_print("%d messages found.\n", msgs);
284 gint lock_mbox(const gchar *base, LockType type)
289 if (type == LOCK_FILE) {
290 gchar *lockfile, *locklink;
294 lockfile = g_strdup_printf("%s.%d", base, getpid());
295 if ((lockfp = g_fopen(lockfile, "wb")) == NULL) {
296 FILE_OP_ERROR(lockfile, "fopen");
297 g_warning("can't create lock file %s\n", lockfile);
298 g_warning("use 'flock' instead of 'file' if possible.\n");
303 fprintf(lockfp, "%d\n", getpid());
306 locklink = g_strconcat(base, ".lock", NULL);
307 while (link(lockfile, locklink) < 0) {
308 FILE_OP_ERROR(lockfile, "link");
310 g_warning("can't create %s\n", lockfile);
316 g_warning("mailbox is owned by another"
317 " process, waiting...\n");
323 } else if (type == LOCK_FLOCK) {
325 gboolean fcntled = FALSE;
326 #if HAVE_FCNTL_H && !defined(G_OS_WIN32)
329 fl.l_whence = SEEK_SET;
335 if ((lockfd = open(base, O_RDWR)) < 0) {
337 if ((lockfd = open(base, O_RDWR)) < 0) {
339 FILE_OP_ERROR(base, "open");
343 #if HAVE_FCNTL_H && !defined(G_OS_WIN32)
344 if (fcntl(lockfd, F_SETLK, &fl) == -1) {
345 g_warning("can't fnctl %s (%s)", base, strerror(errno));
353 if (flock(lockfd, LOCK_EX|LOCK_NB) < 0 && !fcntled) {
357 if (lockf(lockfd, F_TLOCK, 0) < 0 && !fcntled) {
362 #endif /* HAVE_FLOCK */
363 g_warning("can't lock %s\n", base);
364 if (close(lockfd) < 0)
370 g_warning("invalid lock type\n");
377 #endif /* G_OS_UNIX */
380 gint unlock_mbox(const gchar *base, gint fd, LockType type)
382 if (type == LOCK_FILE) {
385 lockfile = g_strconcat(base, ".lock", NULL);
386 if (g_unlink(lockfile) < 0) {
387 FILE_OP_ERROR(lockfile, "unlink");
394 } else if (type == LOCK_FLOCK) {
395 gboolean fcntled = FALSE;
396 #if HAVE_FCNTL_H && !defined(G_OS_WIN32)
399 fl.l_whence = SEEK_SET;
403 if (fcntl(fd, F_SETLK, &fl) == -1) {
404 g_warning("can't fnctl %s", base);
411 if (flock(fd, LOCK_UN) < 0 && !fcntled) {
415 if (lockf(fd, F_ULOCK, 0) < 0 && !fcntled) {
420 #endif /* HAVE_FLOCK */
421 g_warning("can't unlock %s\n", base);
435 g_warning("invalid lock type\n");
439 gint copy_mbox(const gchar *src, const gchar *dest)
441 return copy_file(src, dest, TRUE);
444 void empty_mbox(const gchar *mbox)
448 if ((fp = g_fopen(mbox, "wb")) == NULL) {
449 FILE_OP_ERROR(mbox, "fopen");
450 g_warning("can't truncate mailbox to zero.\n");
456 gint export_list_to_mbox(GSList *mlist, const gchar *mbox)
457 /* return values: -2 skipped, -1 error, 0 OK */
464 gint msgs = 1, total = g_slist_length(mlist);
465 if (g_file_test(mbox, G_FILE_TEST_EXISTS) == TRUE) {
466 if (alertpanel_full(_("Overwrite mbox file"),
467 _("This file already exists. Do you want to overwrite it?"),
468 GTK_STOCK_CANCEL, _("Overwrite"), NULL, FALSE,
469 NULL, ALERT_WARNING, G_ALERTDEFAULT)
470 != G_ALERTALTERNATE) {
475 if ((mbox_fp = g_fopen(mbox, "wb")) == NULL) {
476 FILE_OP_ERROR(mbox, "fopen");
477 alertpanel_error(_("Could not create mbox file:\n%s\n"), mbox);
481 statusbar_print_all(_("Exporting to mbox..."));
482 for (cur = mlist; cur != NULL; cur = cur->next) {
484 msginfo = (MsgInfo *)cur->data;
486 msg_fp = procmsg_open_message(msginfo);
492 msginfo->from ? msginfo->from :
493 cur_account && cur_account->address ?
494 cur_account->address : "unknown",
496 extract_address(buf);
498 fprintf(mbox_fp, "From %s %s",
499 buf, ctime(&msginfo->date_t));
503 /* write email to mboxrc */
504 while (fgets(buf, sizeof(buf), msg_fp) != NULL) {
505 /* quote any From, >From, >>From, etc., according to mbox format specs */
509 /* detect leading '>' char(s) */
510 while ((buf[offset] == '>')) {
513 if (!strncmp(buf+offset, "From ", 5))
518 /* force last line to end w/ a newline */
522 if ((buf[len] != '\n') && (buf[len] != '\r'))
523 fputc('\n', mbox_fp);
526 /* add a trailing empty line */
527 fputc('\n', mbox_fp);
530 statusbar_progress_all(msgs++,total, 500);
534 statusbar_progress_all(0,0,0);
542 /* read all messages in SRC, and store them into one MBOX file. */
543 /* return values: -2 skipped, -1 error, 0 OK */
544 gint export_to_mbox(FolderItem *src, const gchar *mbox)
549 g_return_val_if_fail(src != NULL, -1);
550 g_return_val_if_fail(src->folder != NULL, -1);
551 g_return_val_if_fail(mbox != NULL, -1);
553 debug_print("Exporting messages from %s into %s...\n",
556 mlist = folder_item_get_msg_list(src);
558 ret = export_list_to_mbox(mlist, mbox);
560 procmsg_msg_list_free(mlist);