2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2001 Hiroyuki Yamamoto
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)
64 gchar buf[MSGBUFSIZE];
71 g_return_val_if_fail(dest != NULL, -1);
72 g_return_val_if_fail(mbox != NULL, -1);
74 debug_print("Getting messages from %s into %s...\n", mbox, dest->path);
76 if ((mbox_fp = g_fopen(mbox, "rb")) == NULL) {
77 FILE_OP_ERROR(mbox, "fopen");
78 alertpanel_error(_("Could not open mbox file:\n%s\n"), mbox);
82 /* ignore empty lines on the head */
84 if (fgets(buf, sizeof(buf), mbox_fp) == NULL) {
85 g_warning("can't read mbox file.\n");
89 } while (buf[0] == '\n' || buf[0] == '\r');
91 if (strncmp(buf, "From ", 5) != 0) {
92 g_warning("invalid mbox format: %s\n", mbox);
97 tmp_file = get_tmp_file();
99 folder_item_update_freeze();
103 FolderItem *dropfolder;
107 if ((tmp_fp = g_fopen(tmp_file, "wb")) == NULL) {
108 FILE_OP_ERROR(tmp_file, "fopen");
109 g_warning("can't open temporary file\n");
114 if (change_file_mode_rw(tmp_fp, tmp_file) < 0) {
115 FILE_OP_ERROR(tmp_file, "chmod");
122 /* process all lines from mboxrc file */
123 while (fgets(buf, sizeof(buf), mbox_fp) != NULL) {
126 /* eof not reached, expect more lines */
129 /* eat empty lines */
130 if (buf[0] == '\n' || buf[0] == '\r') {
135 /* From separator or quoted From */
137 /* detect leading '>' char(s) */
138 while ((buf[offset] == '>')) {
141 if (!strncmp(buf+offset, "From ", 5)) {
142 /* From separator: */
144 /* expect next mbox item */
149 /* flush any eaten empty line */
150 if (empty_lines > 0) {
151 while (empty_lines-- > 0) {
152 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
156 /* store the unquoted line */
157 FPUTS_TO_TMP_ABORT_IF_FAIL(buf + 1);
162 /* flush any eaten empty line */
163 if (empty_lines > 0) {
164 while (empty_lines-- > 0) {
165 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
169 /* store the line itself */
170 FPUTS_TO_TMP_ABORT_IF_FAIL(buf);
172 /* end of mbox item or end of mbox */
174 /* flush any eaten empty line (but the last one) */
175 if (empty_lines > 0) {
176 while (--empty_lines > 0) {
177 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
181 /* more emails to expect? */
182 more = !feof(mbox_fp);
184 /* warn if email part is empty (it's the minimum check
187 g_warning("malformed mbox: %s: message %d is empty\n", mbox, msgs);
194 if (fclose(tmp_fp) == EOF) {
195 FILE_OP_ERROR(tmp_file, "fclose");
196 g_warning("can't write to temporary file\n");
203 dropfolder = folder_get_default_processing();
205 if ((msgnum = folder_item_add_msg(dropfolder, tmp_file, NULL, TRUE)) < 0) {
212 msginfo = folder_item_get_msginfo(dropfolder, msgnum);
213 if (!apply_filter || !procmsg_msginfo_filter(msginfo))
214 folder_item_move_msg(dest, msginfo);
215 procmsg_msginfo_free(msginfo);
220 folder_item_update_thaw();
224 debug_print("%d messages found.\n", msgs);
229 gint lock_mbox(const gchar *base, LockType type)
234 if (type == LOCK_FILE) {
235 gchar *lockfile, *locklink;
239 lockfile = g_strdup_printf("%s.%d", base, getpid());
240 if ((lockfp = g_fopen(lockfile, "wb")) == NULL) {
241 FILE_OP_ERROR(lockfile, "fopen");
242 g_warning("can't create lock file %s\n", lockfile);
243 g_warning("use 'flock' instead of 'file' if possible.\n");
248 fprintf(lockfp, "%d\n", getpid());
251 locklink = g_strconcat(base, ".lock", NULL);
252 while (link(lockfile, locklink) < 0) {
253 FILE_OP_ERROR(lockfile, "link");
255 g_warning("can't create %s\n", lockfile);
261 g_warning("mailbox is owned by another"
262 " process, waiting...\n");
268 } else if (type == LOCK_FLOCK) {
272 if ((lockfd = open(base, O_RDONLY)) < 0) {
274 if ((lockfd = open(base, O_RDWR)) < 0) {
276 FILE_OP_ERROR(base, "open");
280 if (flock(lockfd, LOCK_EX|LOCK_NB) < 0) {
284 if (lockf(lockfd, F_TLOCK, 0) < 0) {
289 #endif /* HAVE_FLOCK */
290 g_warning("can't lock %s\n", base);
291 if (close(lockfd) < 0)
297 g_warning("invalid lock type\n");
304 #endif /* G_OS_UNIX */
307 gint unlock_mbox(const gchar *base, gint fd, LockType type)
309 if (type == LOCK_FILE) {
312 lockfile = g_strconcat(base, ".lock", NULL);
313 if (g_unlink(lockfile) < 0) {
314 FILE_OP_ERROR(lockfile, "unlink");
321 } else if (type == LOCK_FLOCK) {
323 if (flock(fd, LOCK_UN) < 0) {
327 if (lockf(fd, F_ULOCK, 0) < 0) {
332 #endif /* HAVE_FLOCK */
333 g_warning("can't unlock %s\n", base);
347 g_warning("invalid lock type\n");
351 gint copy_mbox(const gchar *src, const gchar *dest)
353 return copy_file(src, dest, TRUE);
356 void empty_mbox(const gchar *mbox)
360 if ((fp = g_fopen(mbox, "wb")) == NULL) {
361 FILE_OP_ERROR(mbox, "fopen");
362 g_warning("can't truncate mailbox to zero.\n");
368 gint export_list_to_mbox(GSList *mlist, const gchar *mbox)
376 if (g_file_test(mbox, G_FILE_TEST_EXISTS) == TRUE) {
377 if (alertpanel_full(_("Overwrite mbox file"),
378 _("This file already exists. Do you want to overwrite it?"),
379 _("Overwrite"), GTK_STOCK_CANCEL, NULL, FALSE,
380 NULL, ALERT_WARNING, G_ALERTALTERNATE)
381 == G_ALERTALTERNATE) {
386 if ((mbox_fp = g_fopen(mbox, "wb")) == NULL) {
387 FILE_OP_ERROR(mbox, "fopen");
388 alertpanel_error(_("Could not create mbox file:\n%s\n"), mbox);
392 for (cur = mlist; cur != NULL; cur = cur->next) {
393 msginfo = (MsgInfo *)cur->data;
396 msg_fp = procmsg_open_message(msginfo);
398 procmsg_msginfo_free(msginfo);
403 msginfo->from ? msginfo->from :
404 cur_account && cur_account->address ?
405 cur_account->address : "unknown",
407 extract_address(buf);
409 fprintf(mbox_fp, "From %s %s",
410 buf, ctime(&msginfo->date_t));
414 /* write email to mboxrc */
415 while (fgets(buf, sizeof(buf), msg_fp) != NULL) {
416 /* quote any From, >From, >>From, etc., according to mbox format specs */
420 /* detect leading '>' char(s) */
421 while ((buf[offset] == '>')) {
424 if (!strncmp(buf+offset, "From ", 5))
429 /* force last line to end w/ a newline */
433 if ((buf[len] != '\n') && (buf[len] != '\r'))
434 fputc('\n', mbox_fp);
437 /* add a trailing empty line */
438 fputc('\n', mbox_fp);
441 procmsg_msginfo_free(msginfo);
449 /* read all messages in SRC, and store them into one MBOX file. */
450 gint export_to_mbox(FolderItem *src, const gchar *mbox)
454 g_return_val_if_fail(src != NULL, -1);
455 g_return_val_if_fail(src->folder != NULL, -1);
456 g_return_val_if_fail(mbox != NULL, -1);
458 debug_print("Exporting messages from %s into %s...\n",
461 mlist = folder_item_get_msg_list(src);
463 export_list_to_mbox(mlist, mbox);
465 procmsg_msg_list_free(mlist);