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>
39 #include "prefs_common.h"
40 #include "prefs_account.h"
43 #include "filtering.h"
44 #include "alertpanel.h"
46 #define MSGBUFSIZE 8192
48 #define FPUTS_TO_TMP_ABORT_IF_FAIL(s) \
51 if (fputs(s, tmp_fp) == EOF) { \
52 g_warning("can't write to temporary file\n"); \
61 gint proc_mbox(FolderItem *dest, const gchar *mbox, gboolean apply_filter)
62 /* return values: -1 error, >=0 number of msgs added */
65 gchar buf[MSGBUFSIZE];
71 GSList *to_filter = NULL, *cur;
73 g_return_val_if_fail(dest != NULL, -1);
74 g_return_val_if_fail(mbox != NULL, -1);
76 debug_print("Getting messages from %s into %s...\n", mbox, dest->path);
78 if ((mbox_fp = g_fopen(mbox, "rb")) == NULL) {
79 FILE_OP_ERROR(mbox, "fopen");
80 alertpanel_error(_("Could not open mbox file:\n%s\n"), mbox);
84 /* ignore empty lines on the head */
86 if (fgets(buf, sizeof(buf), mbox_fp) == NULL) {
87 g_warning("can't read mbox file.\n");
91 } while (buf[0] == '\n' || buf[0] == '\r');
93 if (strncmp(buf, "From ", 5) != 0) {
94 g_warning("invalid mbox format: %s\n", mbox);
99 tmp_file = get_tmp_file();
101 folder_item_update_freeze();
105 FolderItem *dropfolder;
109 if ((tmp_fp = g_fopen(tmp_file, "wb")) == NULL) {
110 FILE_OP_ERROR(tmp_file, "fopen");
111 g_warning("can't open temporary file\n");
116 if (change_file_mode_rw(tmp_fp, tmp_file) < 0) {
117 FILE_OP_ERROR(tmp_file, "chmod");
124 /* process all lines from mboxrc file */
125 while (fgets(buf, sizeof(buf), mbox_fp) != NULL) {
128 /* eof not reached, expect more lines */
131 /* eat empty lines */
132 if (buf[0] == '\n' || buf[0] == '\r') {
137 /* From separator or quoted From */
139 /* detect leading '>' char(s) */
140 while ((buf[offset] == '>')) {
143 if (!strncmp(buf+offset, "From ", 5)) {
144 /* From separator: */
146 /* expect next mbox item */
151 /* flush any eaten empty line */
152 if (empty_lines > 0) {
153 while (empty_lines-- > 0) {
154 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
158 /* store the unquoted line */
159 FPUTS_TO_TMP_ABORT_IF_FAIL(buf + 1);
164 /* flush any eaten empty line */
165 if (empty_lines > 0) {
166 while (empty_lines-- > 0) {
167 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
171 /* store the line itself */
172 FPUTS_TO_TMP_ABORT_IF_FAIL(buf);
174 /* end of mbox item or end of mbox */
176 /* flush any eaten empty line (but the last one) */
177 if (empty_lines > 0) {
178 while (--empty_lines > 0) {
179 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
183 /* more emails to expect? */
184 more = !feof(mbox_fp);
186 /* warn if email part is empty (it's the minimum check
189 g_warning("malformed mbox: %s: message %d is empty\n", mbox, msgs);
196 if (fclose(tmp_fp) == EOF) {
197 FILE_OP_ERROR(tmp_file, "fclose");
198 g_warning("can't write to temporary file\n");
205 dropfolder = folder_get_default_processing();
207 if ((msgnum = folder_item_add_msg(dropfolder, tmp_file, NULL, TRUE)) < 0) {
214 msginfo = folder_item_get_msginfo(dropfolder, msgnum);
215 if (!apply_filter || !procmsg_msginfo_filter(msginfo)) {
216 folder_item_move_msg(dest, msginfo);
217 procmsg_msginfo_free(msginfo);
219 to_filter = g_slist_append(to_filter, msginfo);
224 filtering_move_and_copy_msgs(to_filter);
225 for (cur = to_filter; cur; cur = g_slist_next(cur)) {
226 MsgInfo *info = (MsgInfo *)cur->data;
227 procmsg_msginfo_free(info);
230 folder_item_update_thaw();
234 debug_print("%d messages found.\n", msgs);
239 gint lock_mbox(const gchar *base, LockType type)
244 if (type == LOCK_FILE) {
245 gchar *lockfile, *locklink;
249 lockfile = g_strdup_printf("%s.%d", base, getpid());
250 if ((lockfp = g_fopen(lockfile, "wb")) == NULL) {
251 FILE_OP_ERROR(lockfile, "fopen");
252 g_warning("can't create lock file %s\n", lockfile);
253 g_warning("use 'flock' instead of 'file' if possible.\n");
258 fprintf(lockfp, "%d\n", getpid());
261 locklink = g_strconcat(base, ".lock", NULL);
262 while (link(lockfile, locklink) < 0) {
263 FILE_OP_ERROR(lockfile, "link");
265 g_warning("can't create %s\n", lockfile);
271 g_warning("mailbox is owned by another"
272 " process, waiting...\n");
278 } else if (type == LOCK_FLOCK) {
282 if ((lockfd = open(base, O_RDONLY)) < 0) {
284 if ((lockfd = open(base, O_RDWR)) < 0) {
286 FILE_OP_ERROR(base, "open");
290 if (flock(lockfd, LOCK_EX|LOCK_NB) < 0) {
294 if (lockf(lockfd, F_TLOCK, 0) < 0) {
299 #endif /* HAVE_FLOCK */
300 g_warning("can't lock %s\n", base);
301 if (close(lockfd) < 0)
307 g_warning("invalid lock type\n");
314 #endif /* G_OS_UNIX */
317 gint unlock_mbox(const gchar *base, gint fd, LockType type)
319 if (type == LOCK_FILE) {
322 lockfile = g_strconcat(base, ".lock", NULL);
323 if (g_unlink(lockfile) < 0) {
324 FILE_OP_ERROR(lockfile, "unlink");
331 } else if (type == LOCK_FLOCK) {
333 if (flock(fd, LOCK_UN) < 0) {
337 if (lockf(fd, F_ULOCK, 0) < 0) {
342 #endif /* HAVE_FLOCK */
343 g_warning("can't unlock %s\n", base);
357 g_warning("invalid lock type\n");
361 gint copy_mbox(const gchar *src, const gchar *dest)
363 return copy_file(src, dest, TRUE);
366 void empty_mbox(const gchar *mbox)
370 if ((fp = g_fopen(mbox, "wb")) == NULL) {
371 FILE_OP_ERROR(mbox, "fopen");
372 g_warning("can't truncate mailbox to zero.\n");
378 gint export_list_to_mbox(GSList *mlist, const gchar *mbox)
379 /* return values: -2 skipped, -1 error, 0 OK */
387 if (g_file_test(mbox, G_FILE_TEST_EXISTS) == TRUE) {
388 if (alertpanel_full(_("Overwrite mbox file"),
389 _("This file already exists. Do you want to overwrite it?"),
390 _("Overwrite"), GTK_STOCK_CANCEL, NULL, FALSE,
391 NULL, ALERT_WARNING, G_ALERTALTERNATE)
392 == G_ALERTALTERNATE) {
397 if ((mbox_fp = g_fopen(mbox, "wb")) == NULL) {
398 FILE_OP_ERROR(mbox, "fopen");
399 alertpanel_error(_("Could not create mbox file:\n%s\n"), mbox);
403 for (cur = mlist; cur != NULL; cur = cur->next) {
404 msginfo = (MsgInfo *)cur->data;
407 msg_fp = procmsg_open_message(msginfo);
409 procmsg_msginfo_free(msginfo);
414 msginfo->from ? msginfo->from :
415 cur_account && cur_account->address ?
416 cur_account->address : "unknown",
418 extract_address(buf);
420 fprintf(mbox_fp, "From %s %s",
421 buf, ctime(&msginfo->date_t));
425 /* write email to mboxrc */
426 while (fgets(buf, sizeof(buf), msg_fp) != NULL) {
427 /* quote any From, >From, >>From, etc., according to mbox format specs */
431 /* detect leading '>' char(s) */
432 while ((buf[offset] == '>')) {
435 if (!strncmp(buf+offset, "From ", 5))
440 /* force last line to end w/ a newline */
444 if ((buf[len] != '\n') && (buf[len] != '\r'))
445 fputc('\n', mbox_fp);
448 /* add a trailing empty line */
449 fputc('\n', mbox_fp);
452 procmsg_msginfo_free(msginfo);
460 /* read all messages in SRC, and store them into one MBOX file. */
461 /* return values: -2 skipped, -1 error, 0 OK */
462 gint export_to_mbox(FolderItem *src, const gchar *mbox)
467 g_return_val_if_fail(src != NULL, -1);
468 g_return_val_if_fail(src->folder != NULL, -1);
469 g_return_val_if_fail(mbox != NULL, -1);
471 debug_print("Exporting messages from %s into %s...\n",
474 mlist = folder_item_get_msg_list(src);
476 ret = export_list_to_mbox(mlist, mbox);
478 procmsg_msg_list_free(mlist);