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];
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();
103 FolderItem *dropfolder;
106 gboolean is_next_msg = FALSE;
109 if ((tmp_fp = fopen(tmp_file, "wb")) == NULL) {
110 FILE_OP_ERROR(tmp_file, "fopen");
111 g_warning(_("can't open temporary file\n"));
116 if (change_file_mode_rw(tmp_fp, tmp_file) < 0)
117 FILE_OP_ERROR(tmp_file, "chmod");
119 /* convert unix From into Return-Path */
121 startp = from_line + 5;
122 endp = strchr(startp, ' ');
124 rpath = g_strdup(startp);
126 rpath = g_strndup(startp, endp - startp);
128 g_snprintf(from_line, sizeof(from_line),
129 "Return-Path: %s\n", rpath);
133 FPUTS_TO_TMP_ABORT_IF_FAIL(from_line);
134 FPUTS_TO_TMP_ABORT_IF_FAIL(buf);
139 while (fgets(buf, sizeof(buf), mbox_fp) != NULL) {
140 if (buf[0] == '\n' || buf[0] == '\r') {
147 while (!strncmp(buf, "From ", 5)) {
148 strcpy(from_line, buf);
149 if (fgets(buf, sizeof(buf), mbox_fp) == NULL) {
154 if (is_header_line(buf)) {
157 } else if (!strncmp(buf, "From ", 5)) {
159 } else if (!strncmp(buf, ">From ", 6)) {
160 g_memmove(buf, buf + 1, strlen(buf));
164 g_warning(_("unescaped From found:\n%s"),
169 if (is_next_msg) break;
171 if (empty_line > 0) {
173 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
177 if (from_line[0] != '\0') {
178 FPUTS_TO_TMP_ABORT_IF_FAIL(from_line);
182 if (buf[0] != '\0') {
183 if (!strncmp(buf, ">From ", 6)) {
184 FPUTS_TO_TMP_ABORT_IF_FAIL(buf + 1);
186 FPUTS_TO_TMP_ABORT_IF_FAIL(buf);
192 if (empty_line > 0) {
194 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
197 if (fclose(tmp_fp) == EOF) {
198 FILE_OP_ERROR(tmp_file, "fclose");
199 g_warning(_("can't write to temporary file\n"));
206 if (global_processing == NULL) {
208 dropfolder = filter_get_dest_folder
209 (prefs_common.fltlist, tmp_file);
211 !strcmp(dropfolder->path, FILTER_NOT_RECEIVE))
214 /* CLAWS: new filtering */
215 dropfolder = folder_get_default_processing();
219 if ((msgnum = folder_item_add_msg(dropfolder, tmp_file, TRUE)) < 0) {
226 if (global_processing) {
227 /* CLAWS: new filtering */
228 filter_message(global_processing, dest,
233 } while (from_line[0] != '\0');
237 debug_print("%d messages found.\n", msgs);
242 gint lock_mbox(const gchar *base, LockType type)
246 if (type == LOCK_FILE) {
247 gchar *lockfile, *locklink;
251 lockfile = g_strdup_printf("%s.%d", base, getpid());
252 if ((lockfp = fopen(lockfile, "wb")) == NULL) {
253 FILE_OP_ERROR(lockfile, "fopen");
254 g_warning(_("can't create lock file %s\n"), lockfile);
255 g_warning(_("use 'flock' instead of 'file' if possible.\n"));
260 fprintf(lockfp, "%d\n", getpid());
263 locklink = g_strconcat(base, ".lock", NULL);
264 while (link(lockfile, locklink) < 0) {
265 FILE_OP_ERROR(lockfile, "link");
267 g_warning(_("can't create %s\n"), lockfile);
273 g_warning(_("mailbox is owned by another"
274 " process, waiting...\n"));
280 } else if (type == LOCK_FLOCK) {
284 if ((lockfd = open(base, O_RDONLY)) < 0) {
286 if ((lockfd = open(base, O_RDWR)) < 0) {
288 FILE_OP_ERROR(base, "open");
292 if (flock(lockfd, LOCK_EX|LOCK_NB) < 0) {
296 if (lockf(lockfd, F_TLOCK, 0) < 0) {
301 #endif /* HAVE_FLOCK */
302 g_warning(_("can't lock %s\n"), base);
303 if (close(lockfd) < 0)
309 g_warning(_("invalid lock type\n"));
316 gint unlock_mbox(const gchar *base, gint fd, LockType type)
318 if (type == LOCK_FILE) {
321 lockfile = g_strconcat(base, ".lock", NULL);
322 if (unlink(lockfile) < 0) {
323 FILE_OP_ERROR(lockfile, "unlink");
330 } else if (type == LOCK_FLOCK) {
332 if (flock(fd, LOCK_UN) < 0) {
336 if (lockf(fd, F_ULOCK, 0) < 0) {
341 #endif /* HAVE_FLOCK */
342 g_warning(_("can't unlock %s\n"), base);
356 g_warning(_("invalid lock type\n"));
360 gint copy_mbox(const gchar *src, const gchar *dest)
362 return copy_file(src, dest, TRUE);
365 void empty_mbox(const gchar *mbox)
367 if (truncate(mbox, 0) < 0) {
370 FILE_OP_ERROR(mbox, "truncate");
371 if ((fp = fopen(mbox, "wb")) == NULL) {
372 FILE_OP_ERROR(mbox, "fopen");
373 g_warning(_("can't truncate mailbox to zero.\n"));
380 /* read all messages in SRC, and store them into one MBOX file. */
381 gint export_to_mbox(FolderItem *src, const gchar *mbox)
390 g_return_val_if_fail(src != NULL, -1);
391 g_return_val_if_fail(src->folder != NULL, -1);
392 g_return_val_if_fail(mbox != NULL, -1);
394 debug_print("Exporting messages from %s into %s...\n",
397 if ((mbox_fp = fopen(mbox, "wb")) == NULL) {
398 FILE_OP_ERROR(mbox, "fopen");
402 mlist = folder_item_get_msg_list(src);
404 for (cur = mlist; cur != NULL; cur = cur->next) {
405 msginfo = (MsgInfo *)cur->data;
407 msg_fp = procmsg_open_message(msginfo);
409 procmsg_msginfo_free(msginfo);
414 msginfo->from ? msginfo->from :
415 cur_account && cur_account->address ?
416 cur_account->address : "unknown",
418 extract_address(buf);
420 fprintf(mbox_fp, "From %s %s",
421 buf, ctime(&msginfo->date_t));
423 while (fgets(buf, sizeof(buf), msg_fp) != NULL) {
424 if (!strncmp(buf, "From ", 5))
428 fputc('\n', mbox_fp);
431 procmsg_msginfo_free(msginfo);