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.
40 #include "prefs_common.h"
41 #include "prefs_account.h"
44 #include "filtering.h"
46 #define MSGBUFSIZE 8192
48 #define FPUTS_TO_TMP_ABORT_IF_FAIL(s) \
50 if (fputs(s, tmp_fp) == EOF) { \
51 g_warning(_("can't write to temporary file\n")); \
60 gint proc_mbox(FolderItem *dest, const gchar *mbox)
63 gchar buf[MSGBUFSIZE], from_line[MSGBUFSIZE];
68 g_return_val_if_fail(dest != NULL, -1);
69 g_return_val_if_fail(mbox != NULL, -1);
71 debug_print("Getting messages from %s into %s...\n", mbox, dest->path);
73 if ((mbox_fp = fopen(mbox, "rb")) == NULL) {
74 FILE_OP_ERROR(mbox, "fopen");
78 /* ignore empty lines on the head */
80 if (fgets(buf, sizeof(buf), mbox_fp) == NULL) {
81 g_warning(_("can't read mbox file.\n"));
85 } while (buf[0] == '\n' || buf[0] == '\r');
87 if (strncmp(buf, "From ", 5) != 0) {
88 g_warning(_("invalid mbox format: %s\n"), mbox);
93 strcpy(from_line, buf);
94 if (fgets(buf, sizeof(buf), mbox_fp) == NULL) {
95 g_warning(_("malformed mbox: %s\n"), mbox);
100 tmp_file = get_tmp_file();
101 inbox = folder_get_default_inbox();
105 FolderItem *dropfolder;
108 gboolean is_next_msg = FALSE;
111 if ((tmp_fp = fopen(tmp_file, "wb")) == NULL) {
112 FILE_OP_ERROR(tmp_file, "fopen");
113 g_warning(_("can't open temporary file\n"));
118 if (change_file_mode_rw(tmp_fp, tmp_file) < 0)
119 FILE_OP_ERROR(tmp_file, "chmod");
121 /* convert unix From into Return-Path */
123 startp = from_line + 5;
124 endp = strchr(startp, ' ');
126 rpath = g_strdup(startp);
128 rpath = g_strndup(startp, endp - startp);
130 g_snprintf(from_line, sizeof(from_line),
131 "Return-Path: %s\n", rpath);
135 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 if (global_processing == NULL) {
210 dropfolder = filter_get_dest_folder
211 (prefs_common.fltlist, tmp_file);
213 !strcmp(dropfolder->path, FILTER_NOT_RECEIVE))
216 /* CLAWS: new filtering */
217 dropfolder = folder_get_default_processing();
221 if ((msgnum = folder_item_add_msg(dropfolder, tmp_file, TRUE)) < 0) {
228 if (global_processing) {
229 /* CLAWS: new filtering */
230 filter_message(global_processing, inbox,
235 } while (from_line[0] != '\0');
239 debug_print("%d messages found.\n", msgs);
244 gint lock_mbox(const gchar *base, LockType type)
248 if (type == LOCK_FILE) {
249 gchar *lockfile, *locklink;
253 lockfile = g_strdup_printf("%s.%d", base, getpid());
254 if ((lockfp = fopen(lockfile, "wb")) == NULL) {
255 FILE_OP_ERROR(lockfile, "fopen");
256 g_warning(_("can't create lock file %s\n"), lockfile);
257 g_warning(_("use 'flock' instead of 'file' if possible.\n"));
262 fprintf(lockfp, "%d\n", getpid());
265 locklink = g_strconcat(base, ".lock", NULL);
266 while (link(lockfile, locklink) < 0) {
267 FILE_OP_ERROR(lockfile, "link");
269 g_warning(_("can't create %s\n"), lockfile);
275 g_warning(_("mailbox is owned by another"
276 " process, waiting...\n"));
282 } else if (type == LOCK_FLOCK) {
286 if ((lockfd = open(base, O_RDONLY)) < 0) {
288 if ((lockfd = open(base, O_RDWR)) < 0) {
290 FILE_OP_ERROR(base, "open");
294 if (flock(lockfd, LOCK_EX|LOCK_NB) < 0) {
298 if (lockf(lockfd, F_TLOCK, 0) < 0) {
303 #endif /* HAVE_FLOCK */
304 g_warning(_("can't lock %s\n"), base);
305 if (close(lockfd) < 0)
311 g_warning(_("invalid lock type\n"));
318 gint unlock_mbox(const gchar *base, gint fd, LockType type)
320 if (type == LOCK_FILE) {
323 lockfile = g_strconcat(base, ".lock", NULL);
324 if (unlink(lockfile) < 0) {
325 FILE_OP_ERROR(lockfile, "unlink");
332 } else if (type == LOCK_FLOCK) {
334 if (flock(fd, LOCK_UN) < 0) {
338 if (lockf(fd, F_ULOCK, 0) < 0) {
343 #endif /* HAVE_FLOCK */
344 g_warning(_("can't unlock %s\n"), base);
358 g_warning(_("invalid lock type\n"));
362 gint copy_mbox(const gchar *src, const gchar *dest)
364 return copy_file(src, dest, TRUE);
367 void empty_mbox(const gchar *mbox)
369 if (truncate(mbox, 0) < 0) {
372 FILE_OP_ERROR(mbox, "truncate");
373 if ((fp = fopen(mbox, "wb")) == NULL) {
374 FILE_OP_ERROR(mbox, "fopen");
375 g_warning(_("can't truncate mailbox to zero.\n"));
382 /* read all messages in SRC, and store them into one MBOX file. */
383 gint export_to_mbox(FolderItem *src, const gchar *mbox)
392 g_return_val_if_fail(src != NULL, -1);
393 g_return_val_if_fail(src->folder != NULL, -1);
394 g_return_val_if_fail(mbox != NULL, -1);
396 debug_print("Exporting messages from %s into %s...\n",
399 if ((mbox_fp = fopen(mbox, "wb")) == NULL) {
400 FILE_OP_ERROR(mbox, "fopen");
404 mlist = folder_item_get_msg_list(src);
406 for (cur = mlist; cur != NULL; cur = cur->next) {
407 msginfo = (MsgInfo *)cur->data;
409 msg_fp = procmsg_open_message(msginfo);
411 procmsg_msginfo_free(msginfo);
416 msginfo->from ? msginfo->from :
417 cur_account && cur_account->address ?
418 cur_account->address : "unknown",
420 extract_address(buf);
422 fprintf(mbox_fp, "From %s %s",
423 buf, ctime(&msginfo->date_t));
425 while (fgets(buf, sizeof(buf), msg_fp) != NULL) {
426 if (!strncmp(buf, "From ", 5))
430 fputc('\n', mbox_fp);
433 procmsg_msginfo_free(msginfo);