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.
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)
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 = 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 = 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);
134 FPUTS_TO_TMP_ABORT_IF_FAIL(from_line);
135 FPUTS_TO_TMP_ABORT_IF_FAIL(buf);
140 while (fgets(buf, sizeof(buf), mbox_fp) != NULL) {
141 if (buf[0] == '\n' || buf[0] == '\r') {
148 while (!strncmp(buf, "From ", 5)) {
149 strcpy(from_line, buf);
150 if (fgets(buf, sizeof(buf), mbox_fp) == NULL) {
155 if (is_header_line(buf)) {
158 } else if (!strncmp(buf, "From ", 5)) {
160 } else if (!strncmp(buf, ">From ", 6)) {
161 g_memmove(buf, buf + 1, strlen(buf));
165 g_warning("unescaped From found:\n%s",
170 if (is_next_msg) break;
172 if (empty_line > 0) {
174 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
178 if (from_line[0] != '\0') {
179 FPUTS_TO_TMP_ABORT_IF_FAIL(from_line);
183 if (buf[0] != '\0') {
184 if (!strncmp(buf, ">From ", 6)) {
185 FPUTS_TO_TMP_ABORT_IF_FAIL(buf + 1);
187 FPUTS_TO_TMP_ABORT_IF_FAIL(buf);
193 if (empty_line > 0) {
195 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
198 if (fclose(tmp_fp) == EOF) {
199 FILE_OP_ERROR(tmp_file, "fclose");
200 g_warning("can't write to temporary file\n");
207 dropfolder = folder_get_default_processing();
209 if ((msgnum = folder_item_add_msg(dropfolder, tmp_file, NULL, TRUE)) < 0) {
216 msginfo = folder_item_get_msginfo(dropfolder, msgnum);
217 if (!procmsg_msginfo_filter(msginfo))
218 folder_item_move_msg(dest, msginfo);
219 procmsg_msginfo_free(msginfo);
222 } while (from_line[0] != '\0');
224 folder_item_update_thaw();
228 debug_print("%d messages found.\n", msgs);
233 gint lock_mbox(const gchar *base, LockType type)
237 if (type == LOCK_FILE) {
238 gchar *lockfile, *locklink;
242 lockfile = g_strdup_printf("%s.%d", base, getpid());
243 if ((lockfp = fopen(lockfile, "wb")) == NULL) {
244 FILE_OP_ERROR(lockfile, "fopen");
245 g_warning("can't create lock file %s\n", lockfile);
246 g_warning("use 'flock' instead of 'file' if possible.\n");
251 fprintf(lockfp, "%d\n", getpid());
254 locklink = g_strconcat(base, ".lock", NULL);
255 while (link(lockfile, locklink) < 0) {
256 FILE_OP_ERROR(lockfile, "link");
258 g_warning("can't create %s\n", lockfile);
264 g_warning("mailbox is owned by another"
265 " process, waiting...\n");
271 } else if (type == LOCK_FLOCK) {
275 if ((lockfd = open(base, O_RDONLY)) < 0) {
277 if ((lockfd = open(base, O_RDWR)) < 0) {
279 FILE_OP_ERROR(base, "open");
283 if (flock(lockfd, LOCK_EX|LOCK_NB) < 0) {
287 if (lockf(lockfd, F_TLOCK, 0) < 0) {
292 #endif /* HAVE_FLOCK */
293 g_warning("can't lock %s\n", base);
294 if (close(lockfd) < 0)
300 g_warning("invalid lock type\n");
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 (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)
358 if (truncate(mbox, 0) < 0) {
361 FILE_OP_ERROR(mbox, "truncate");
362 if ((fp = fopen(mbox, "wb")) == NULL) {
363 FILE_OP_ERROR(mbox, "fopen");
364 g_warning("can't truncate mailbox to zero.\n");
371 /* read all messages in SRC, and store them into one MBOX file. */
372 gint export_to_mbox(FolderItem *src, const gchar *mbox)
381 g_return_val_if_fail(src != NULL, -1);
382 g_return_val_if_fail(src->folder != NULL, -1);
383 g_return_val_if_fail(mbox != NULL, -1);
385 debug_print("Exporting messages from %s into %s...\n",
388 if ((mbox_fp = fopen(mbox, "wb")) == NULL) {
389 FILE_OP_ERROR(mbox, "fopen");
393 mlist = folder_item_get_msg_list(src);
395 for (cur = mlist; cur != NULL; cur = cur->next) {
396 msginfo = (MsgInfo *)cur->data;
398 msg_fp = procmsg_open_message(msginfo);
400 procmsg_msginfo_free(msginfo);
405 msginfo->from ? msginfo->from :
406 cur_account && cur_account->address ?
407 cur_account->address : "unknown",
409 extract_address(buf);
411 fprintf(mbox_fp, "From %s %s",
412 buf, ctime(&msginfo->date_t));
414 while (fgets(buf, sizeof(buf), msg_fp) != NULL) {
415 if (!strncmp(buf, "From ", 5))
419 fputc('\n', mbox_fp);
422 procmsg_msginfo_free(msginfo);