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];
66 g_return_val_if_fail(dest != NULL, -1);
67 g_return_val_if_fail(mbox != NULL, -1);
69 debug_print("Getting messages from %s into %s...\n", mbox, dest->path);
71 if ((mbox_fp = fopen(mbox, "rb")) == NULL) {
72 FILE_OP_ERROR(mbox, "fopen");
76 /* ignore empty lines on the head */
78 if (fgets(buf, sizeof(buf), mbox_fp) == NULL) {
79 g_warning(_("can't read mbox file.\n"));
83 } while (buf[0] == '\n' || buf[0] == '\r');
85 if (strncmp(buf, "From ", 5) != 0) {
86 g_warning(_("invalid mbox format: %s\n"), mbox);
91 strcpy(from_line, buf);
92 if (fgets(buf, sizeof(buf), mbox_fp) == NULL) {
93 g_warning(_("malformed mbox: %s\n"), mbox);
98 tmp_file = get_tmp_file();
102 FolderItem *dropfolder;
105 gboolean is_next_msg = FALSE;
108 if ((tmp_fp = fopen(tmp_file, "wb")) == NULL) {
109 FILE_OP_ERROR(tmp_file, "fopen");
110 g_warning(_("can't open temporary file\n"));
115 if (change_file_mode_rw(tmp_fp, tmp_file) < 0)
116 FILE_OP_ERROR(tmp_file, "chmod");
118 /* convert unix From into Return-Path */
120 startp = from_line + 5;
121 endp = strchr(startp, ' ');
123 rpath = g_strdup(startp);
125 rpath = g_strndup(startp, endp - startp);
127 g_snprintf(from_line, sizeof(from_line),
128 "Return-Path: %s\n", rpath);
132 FPUTS_TO_TMP_ABORT_IF_FAIL(from_line);
133 FPUTS_TO_TMP_ABORT_IF_FAIL(buf);
138 while (fgets(buf, sizeof(buf), mbox_fp) != NULL) {
139 if (buf[0] == '\n' || buf[0] == '\r') {
146 while (!strncmp(buf, "From ", 5)) {
147 strcpy(from_line, buf);
148 if (fgets(buf, sizeof(buf), mbox_fp) == NULL) {
153 if (is_header_line(buf)) {
156 } else if (!strncmp(buf, "From ", 5)) {
158 } else if (!strncmp(buf, ">From ", 6)) {
159 g_memmove(buf, buf + 1, strlen(buf));
163 g_warning(_("unescaped From found:\n%s"),
168 if (is_next_msg) break;
170 if (empty_line > 0) {
172 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
176 if (from_line[0] != '\0') {
177 FPUTS_TO_TMP_ABORT_IF_FAIL(from_line);
181 if (buf[0] != '\0') {
182 if (!strncmp(buf, ">From ", 6)) {
183 FPUTS_TO_TMP_ABORT_IF_FAIL(buf + 1);
185 FPUTS_TO_TMP_ABORT_IF_FAIL(buf);
191 if (empty_line > 0) {
193 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
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, TRUE)) < 0) {
214 filter_message(global_processing, dest,
218 } while (from_line[0] != '\0');
222 debug_print("%d messages found.\n", msgs);
227 gint lock_mbox(const gchar *base, LockType type)
231 if (type == LOCK_FILE) {
232 gchar *lockfile, *locklink;
236 lockfile = g_strdup_printf("%s.%d", base, getpid());
237 if ((lockfp = fopen(lockfile, "wb")) == NULL) {
238 FILE_OP_ERROR(lockfile, "fopen");
239 g_warning(_("can't create lock file %s\n"), lockfile);
240 g_warning(_("use 'flock' instead of 'file' if possible.\n"));
245 fprintf(lockfp, "%d\n", getpid());
248 locklink = g_strconcat(base, ".lock", NULL);
249 while (link(lockfile, locklink) < 0) {
250 FILE_OP_ERROR(lockfile, "link");
252 g_warning(_("can't create %s\n"), lockfile);
258 g_warning(_("mailbox is owned by another"
259 " process, waiting...\n"));
265 } else if (type == LOCK_FLOCK) {
269 if ((lockfd = open(base, O_RDONLY)) < 0) {
271 if ((lockfd = open(base, O_RDWR)) < 0) {
273 FILE_OP_ERROR(base, "open");
277 if (flock(lockfd, LOCK_EX|LOCK_NB) < 0) {
281 if (lockf(lockfd, F_TLOCK, 0) < 0) {
286 #endif /* HAVE_FLOCK */
287 g_warning(_("can't lock %s\n"), base);
288 if (close(lockfd) < 0)
294 g_warning(_("invalid lock type\n"));
301 gint unlock_mbox(const gchar *base, gint fd, LockType type)
303 if (type == LOCK_FILE) {
306 lockfile = g_strconcat(base, ".lock", NULL);
307 if (unlink(lockfile) < 0) {
308 FILE_OP_ERROR(lockfile, "unlink");
315 } else if (type == LOCK_FLOCK) {
317 if (flock(fd, LOCK_UN) < 0) {
321 if (lockf(fd, F_ULOCK, 0) < 0) {
326 #endif /* HAVE_FLOCK */
327 g_warning(_("can't unlock %s\n"), base);
341 g_warning(_("invalid lock type\n"));
345 gint copy_mbox(const gchar *src, const gchar *dest)
347 return copy_file(src, dest, TRUE);
350 void empty_mbox(const gchar *mbox)
352 if (truncate(mbox, 0) < 0) {
355 FILE_OP_ERROR(mbox, "truncate");
356 if ((fp = fopen(mbox, "wb")) == NULL) {
357 FILE_OP_ERROR(mbox, "fopen");
358 g_warning(_("can't truncate mailbox to zero.\n"));
365 /* read all messages in SRC, and store them into one MBOX file. */
366 gint export_to_mbox(FolderItem *src, const gchar *mbox)
375 g_return_val_if_fail(src != NULL, -1);
376 g_return_val_if_fail(src->folder != NULL, -1);
377 g_return_val_if_fail(mbox != NULL, -1);
379 debug_print("Exporting messages from %s into %s...\n",
382 if ((mbox_fp = fopen(mbox, "wb")) == NULL) {
383 FILE_OP_ERROR(mbox, "fopen");
387 mlist = folder_item_get_msg_list(src);
389 for (cur = mlist; cur != NULL; cur = cur->next) {
390 msginfo = (MsgInfo *)cur->data;
392 msg_fp = procmsg_open_message(msginfo);
394 procmsg_msginfo_free(msginfo);
399 msginfo->from ? msginfo->from :
400 cur_account && cur_account->address ?
401 cur_account->address : "unknown",
403 extract_address(buf);
405 fprintf(mbox_fp, "From %s %s",
406 buf, ctime(&msginfo->date_t));
408 while (fgets(buf, sizeof(buf), msg_fp) != NULL) {
409 if (!strncmp(buf, "From ", 5))
413 fputc('\n', mbox_fp);
416 procmsg_msginfo_free(msginfo);