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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 #include <glib/gi18n.h>
39 #include "prefs_common.h"
40 #include "prefs_account.h"
43 #include "filtering.h"
45 #define MSGBUFSIZE 8192
47 #define FPUTS_TO_TMP_ABORT_IF_FAIL(s) \
49 if (fputs(s, tmp_fp) == EOF) { \
50 g_warning("can't write to temporary file\n"); \
59 gint proc_mbox(FolderItem *dest, const gchar *mbox, gboolean apply_filter)
62 gchar buf[MSGBUFSIZE], from_line[MSGBUFSIZE];
67 g_return_val_if_fail(dest != NULL, -1);
68 g_return_val_if_fail(mbox != NULL, -1);
70 debug_print("Getting messages from %s into %s...\n", mbox, dest->path);
72 if ((mbox_fp = g_fopen(mbox, "rb")) == NULL) {
73 FILE_OP_ERROR(mbox, "fopen");
77 /* ignore empty lines on the head */
79 if (fgets(buf, sizeof(buf), mbox_fp) == NULL) {
80 g_warning("can't read mbox file.\n");
84 } while (buf[0] == '\n' || buf[0] == '\r');
86 if (strncmp(buf, "From ", 5) != 0) {
87 g_warning("invalid mbox format: %s\n", mbox);
92 strcpy(from_line, buf);
93 if (fgets(buf, sizeof(buf), mbox_fp) == NULL) {
94 g_warning("malformed mbox: %s\n", mbox);
99 tmp_file = get_tmp_file();
101 folder_item_update_freeze();
105 FolderItem *dropfolder;
107 gboolean is_next_msg = FALSE;
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");
120 /* convert unix From into Return-Path */
122 startp = from_line + 5;
123 endp = strchr(startp, ' ');
125 rpath = g_strdup(startp);
127 rpath = g_strndup(startp, endp - startp);
129 g_snprintf(from_line, sizeof(from_line),
130 "Return-Path: %s\n", rpath);
133 FPUTS_TO_TMP_ABORT_IF_FAIL(from_line);
136 FPUTS_TO_TMP_ABORT_IF_FAIL(buf);
141 while (fgets(buf, sizeof(buf), mbox_fp) != NULL) {
142 if (buf[0] == '\n' || buf[0] == '\r') {
149 while (!strncmp(buf, "From ", 5)) {
150 strcpy(from_line, buf);
151 if (fgets(buf, sizeof(buf), mbox_fp) == NULL) {
156 if (is_header_line(buf)) {
159 } else if (!strncmp(buf, "From ", 5)) {
161 } else if (!strncmp(buf, ">From ", 6)) {
162 g_memmove(buf, buf + 1, strlen(buf));
166 g_warning("unescaped From found:\n%s",
171 if (is_next_msg) break;
173 if (empty_line > 0) {
175 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
179 if (from_line[0] != '\0') {
180 FPUTS_TO_TMP_ABORT_IF_FAIL(from_line);
184 if (buf[0] != '\0') {
185 if (!strncmp(buf, ">From ", 6)) {
186 FPUTS_TO_TMP_ABORT_IF_FAIL(buf + 1);
188 FPUTS_TO_TMP_ABORT_IF_FAIL(buf);
194 if (empty_line > 0) {
196 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
199 if (fclose(tmp_fp) == EOF) {
200 FILE_OP_ERROR(tmp_file, "fclose");
201 g_warning("can't write to temporary file\n");
208 dropfolder = folder_get_default_processing();
210 if ((msgnum = folder_item_add_msg(dropfolder, tmp_file, NULL, TRUE)) < 0) {
217 msginfo = folder_item_get_msginfo(dropfolder, msgnum);
218 if (!apply_filter || !procmsg_msginfo_filter(msginfo))
219 folder_item_move_msg(dest, msginfo);
220 procmsg_msginfo_free(msginfo);
223 } while (from_line[0] != '\0');
225 folder_item_update_thaw();
229 debug_print("%d messages found.\n", msgs);
234 gint lock_mbox(const gchar *base, LockType type)
239 if (type == LOCK_FILE) {
240 gchar *lockfile, *locklink;
244 lockfile = g_strdup_printf("%s.%d", base, getpid());
245 if ((lockfp = g_fopen(lockfile, "wb")) == NULL) {
246 FILE_OP_ERROR(lockfile, "fopen");
247 g_warning("can't create lock file %s\n", lockfile);
248 g_warning("use 'flock' instead of 'file' if possible.\n");
253 fprintf(lockfp, "%d\n", getpid());
256 locklink = g_strconcat(base, ".lock", NULL);
257 while (link(lockfile, locklink) < 0) {
258 FILE_OP_ERROR(lockfile, "link");
260 g_warning("can't create %s\n", lockfile);
266 g_warning("mailbox is owned by another"
267 " process, waiting...\n");
273 } else if (type == LOCK_FLOCK) {
277 if ((lockfd = open(base, O_RDONLY)) < 0) {
279 if ((lockfd = open(base, O_RDWR)) < 0) {
281 FILE_OP_ERROR(base, "open");
285 if (flock(lockfd, LOCK_EX|LOCK_NB) < 0) {
289 if (lockf(lockfd, F_TLOCK, 0) < 0) {
294 #endif /* HAVE_FLOCK */
295 g_warning("can't lock %s\n", base);
296 if (close(lockfd) < 0)
302 g_warning("invalid lock type\n");
309 #endif /* G_OS_UNIX */
312 gint unlock_mbox(const gchar *base, gint fd, LockType type)
314 if (type == LOCK_FILE) {
317 lockfile = g_strconcat(base, ".lock", NULL);
318 if (g_unlink(lockfile) < 0) {
319 FILE_OP_ERROR(lockfile, "unlink");
326 } else if (type == LOCK_FLOCK) {
328 if (flock(fd, LOCK_UN) < 0) {
332 if (lockf(fd, F_ULOCK, 0) < 0) {
337 #endif /* HAVE_FLOCK */
338 g_warning("can't unlock %s\n", base);
352 g_warning("invalid lock type\n");
356 gint copy_mbox(const gchar *src, const gchar *dest)
358 return copy_file(src, dest, TRUE);
361 void empty_mbox(const gchar *mbox)
365 if ((fp = g_fopen(mbox, "wb")) == NULL) {
366 FILE_OP_ERROR(mbox, "fopen");
367 g_warning("can't truncate mailbox to zero.\n");
373 gint export_list_to_mbox(GSList *mlist, const gchar *mbox)
381 if ((mbox_fp = g_fopen(mbox, "wb")) == NULL) {
382 FILE_OP_ERROR(mbox, "fopen");
386 for (cur = mlist; cur != NULL; cur = cur->next) {
387 msginfo = (MsgInfo *)cur->data;
389 msg_fp = procmsg_open_message(msginfo);
391 procmsg_msginfo_free(msginfo);
396 msginfo->from ? msginfo->from :
397 cur_account && cur_account->address ?
398 cur_account->address : "unknown",
400 extract_address(buf);
402 fprintf(mbox_fp, "From %s %s",
403 buf, ctime(&msginfo->date_t));
405 while (fgets(buf, sizeof(buf), msg_fp) != NULL) {
406 if (!strncmp(buf, "From ", 5))
410 fputc('\n', mbox_fp);
413 procmsg_msginfo_free(msginfo);
421 /* read all messages in SRC, and store them into one MBOX file. */
422 gint export_to_mbox(FolderItem *src, const gchar *mbox)
426 g_return_val_if_fail(src != NULL, -1);
427 g_return_val_if_fail(src->folder != NULL, -1);
428 g_return_val_if_fail(mbox != NULL, -1);
430 debug_print("Exporting messages from %s into %s...\n",
433 mlist = folder_item_get_msg_list(src);
435 export_list_to_mbox(mlist, mbox);
437 procmsg_msg_list_free(mlist);