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"
47 #define MSGBUFSIZE 8192
49 #define FPUTS_TO_TMP_ABORT_IF_FAIL(s) \
52 if (fputs(s, tmp_fp) == EOF) { \
53 g_warning("can't write to temporary file\n"); \
62 gint proc_mbox(FolderItem *dest, const gchar *mbox, gboolean apply_filter)
63 /* return values: -1 error, >=0 number of msgs added */
66 gchar buf[MSGBUFSIZE];
72 GSList *to_filter = NULL, *cur;
74 g_return_val_if_fail(dest != NULL, -1);
75 g_return_val_if_fail(mbox != NULL, -1);
77 debug_print("Getting messages from %s into %s...\n", mbox, dest->path);
79 if ((mbox_fp = g_fopen(mbox, "rb")) == NULL) {
80 FILE_OP_ERROR(mbox, "fopen");
81 alertpanel_error(_("Could not open mbox file:\n%s\n"), mbox);
85 /* ignore empty lines on the head */
87 if (fgets(buf, sizeof(buf), mbox_fp) == NULL) {
88 g_warning("can't read mbox file.\n");
92 } while (buf[0] == '\n' || buf[0] == '\r');
94 if (strncmp(buf, "From ", 5) != 0) {
95 g_warning("invalid mbox format: %s\n", mbox);
100 tmp_file = get_tmp_file();
102 folder_item_update_freeze();
106 FolderItem *dropfolder;
110 if ((tmp_fp = g_fopen(tmp_file, "wb")) == NULL) {
111 FILE_OP_ERROR(tmp_file, "fopen");
112 g_warning("can't open temporary file\n");
117 if (change_file_mode_rw(tmp_fp, tmp_file) < 0) {
118 FILE_OP_ERROR(tmp_file, "chmod");
125 /* process all lines from mboxrc file */
126 while (fgets(buf, sizeof(buf), mbox_fp) != NULL) {
129 /* eof not reached, expect more lines */
132 /* eat empty lines */
133 if (buf[0] == '\n' || buf[0] == '\r') {
138 /* From separator or quoted From */
140 /* detect leading '>' char(s) */
141 while ((buf[offset] == '>')) {
144 if (!strncmp(buf+offset, "From ", 5)) {
145 /* From separator: */
147 /* expect next mbox item */
152 /* flush any eaten empty line */
153 if (empty_lines > 0) {
154 while (empty_lines-- > 0) {
155 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
159 /* store the unquoted line */
160 FPUTS_TO_TMP_ABORT_IF_FAIL(buf + 1);
165 /* flush any eaten empty line */
166 if (empty_lines > 0) {
167 while (empty_lines-- > 0) {
168 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
172 /* store the line itself */
173 FPUTS_TO_TMP_ABORT_IF_FAIL(buf);
175 /* end of mbox item or end of mbox */
177 /* flush any eaten empty line (but the last one) */
178 if (empty_lines > 0) {
179 while (--empty_lines > 0) {
180 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
184 /* more emails to expect? */
185 more = !feof(mbox_fp);
187 /* warn if email part is empty (it's the minimum check
190 g_warning("malformed mbox: %s: message %d is empty\n", mbox, msgs);
197 if (fclose(tmp_fp) == EOF) {
198 FILE_OP_ERROR(tmp_file, "fclose");
199 g_warning("can't write to temporary file\n");
206 dropfolder = folder_get_default_processing();
208 if ((msgnum = folder_item_add_msg(dropfolder, tmp_file, NULL, TRUE)) < 0) {
215 msginfo = folder_item_get_msginfo(dropfolder, msgnum);
216 if (!apply_filter || !procmsg_msginfo_filter(msginfo)) {
217 folder_item_move_msg(dest, msginfo);
218 procmsg_msginfo_free(msginfo);
220 to_filter = g_slist_append(to_filter, msginfo);
225 filtering_move_and_copy_msgs(to_filter);
226 for (cur = to_filter; cur; cur = g_slist_next(cur)) {
227 MsgInfo *info = (MsgInfo *)cur->data;
228 procmsg_msginfo_free(info);
231 folder_item_update_thaw();
235 debug_print("%d messages found.\n", msgs);
240 gint lock_mbox(const gchar *base, LockType type)
245 if (type == LOCK_FILE) {
246 gchar *lockfile, *locklink;
250 lockfile = g_strdup_printf("%s.%d", base, getpid());
251 if ((lockfp = g_fopen(lockfile, "wb")) == NULL) {
252 FILE_OP_ERROR(lockfile, "fopen");
253 g_warning("can't create lock file %s\n", lockfile);
254 g_warning("use 'flock' instead of 'file' if possible.\n");
259 fprintf(lockfp, "%d\n", getpid());
262 locklink = g_strconcat(base, ".lock", NULL);
263 while (link(lockfile, locklink) < 0) {
264 FILE_OP_ERROR(lockfile, "link");
266 g_warning("can't create %s\n", lockfile);
272 g_warning("mailbox is owned by another"
273 " process, waiting...\n");
279 } else if (type == LOCK_FLOCK) {
281 gboolean fcntled = FALSE;
285 fl.l_whence = SEEK_SET;
291 if ((lockfd = open(base, O_RDWR)) < 0) {
293 if ((lockfd = open(base, O_RDWR)) < 0) {
295 FILE_OP_ERROR(base, "open");
300 if (fcntl(lockfd, F_SETLK, &fl) == -1) {
301 g_warning("can't fnctl %s (%s)", base, strerror(errno));
309 if (flock(lockfd, LOCK_EX|LOCK_NB) < 0 && !fcntled) {
313 if (lockf(lockfd, F_TLOCK, 0) < 0 && !fcntled) {
318 #endif /* HAVE_FLOCK */
319 g_warning("can't lock %s\n", base);
320 if (close(lockfd) < 0)
326 g_warning("invalid lock type\n");
333 #endif /* G_OS_UNIX */
336 gint unlock_mbox(const gchar *base, gint fd, LockType type)
338 if (type == LOCK_FILE) {
341 lockfile = g_strconcat(base, ".lock", NULL);
342 if (g_unlink(lockfile) < 0) {
343 FILE_OP_ERROR(lockfile, "unlink");
350 } else if (type == LOCK_FLOCK) {
351 gboolean fcntled = FALSE;
355 fl.l_whence = SEEK_SET;
359 if (fcntl(fd, F_SETLK, &fl) == -1) {
360 g_warning("can't fnctl %s", base);
367 if (flock(fd, LOCK_UN) < 0 && !fcntled) {
371 if (lockf(fd, F_ULOCK, 0) < 0 && !fcntled) {
376 #endif /* HAVE_FLOCK */
377 g_warning("can't unlock %s\n", base);
391 g_warning("invalid lock type\n");
395 gint copy_mbox(const gchar *src, const gchar *dest)
397 return copy_file(src, dest, TRUE);
400 void empty_mbox(const gchar *mbox)
404 if ((fp = g_fopen(mbox, "wb")) == NULL) {
405 FILE_OP_ERROR(mbox, "fopen");
406 g_warning("can't truncate mailbox to zero.\n");
412 gint export_list_to_mbox(GSList *mlist, const gchar *mbox)
413 /* return values: -2 skipped, -1 error, 0 OK */
421 if (g_file_test(mbox, G_FILE_TEST_EXISTS) == TRUE) {
422 if (alertpanel_full(_("Overwrite mbox file"),
423 _("This file already exists. Do you want to overwrite it?"),
424 GTK_STOCK_CANCEL, _("Overwrite"), NULL, FALSE,
425 NULL, ALERT_WARNING, G_ALERTDEFAULT)
426 != G_ALERTALTERNATE) {
431 if ((mbox_fp = g_fopen(mbox, "wb")) == NULL) {
432 FILE_OP_ERROR(mbox, "fopen");
433 alertpanel_error(_("Could not create mbox file:\n%s\n"), mbox);
437 for (cur = mlist; cur != NULL; cur = cur->next) {
438 msginfo = (MsgInfo *)cur->data;
441 msg_fp = procmsg_open_message(msginfo);
443 procmsg_msginfo_free(msginfo);
448 msginfo->from ? msginfo->from :
449 cur_account && cur_account->address ?
450 cur_account->address : "unknown",
452 extract_address(buf);
454 fprintf(mbox_fp, "From %s %s",
455 buf, ctime(&msginfo->date_t));
459 /* write email to mboxrc */
460 while (fgets(buf, sizeof(buf), msg_fp) != NULL) {
461 /* quote any From, >From, >>From, etc., according to mbox format specs */
465 /* detect leading '>' char(s) */
466 while ((buf[offset] == '>')) {
469 if (!strncmp(buf+offset, "From ", 5))
474 /* force last line to end w/ a newline */
478 if ((buf[len] != '\n') && (buf[len] != '\r'))
479 fputc('\n', mbox_fp);
482 /* add a trailing empty line */
483 fputc('\n', mbox_fp);
486 procmsg_msginfo_free(msginfo);
494 /* read all messages in SRC, and store them into one MBOX file. */
495 /* return values: -2 skipped, -1 error, 0 OK */
496 gint export_to_mbox(FolderItem *src, const gchar *mbox)
501 g_return_val_if_fail(src != NULL, -1);
502 g_return_val_if_fail(src->folder != NULL, -1);
503 g_return_val_if_fail(mbox != NULL, -1);
505 debug_print("Exporting messages from %s into %s...\n",
508 mlist = folder_item_get_msg_list(src);
510 ret = export_list_to_mbox(mlist, mbox);
512 procmsg_msg_list_free(mlist);