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, GHashTable *folder_table)
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"));
209 if (global_processing == NULL) {
211 dropfolder = filter_get_dest_folder
212 (prefs_common.fltlist, tmp_file);
214 !strcmp(dropfolder->path, FILTER_NOT_RECEIVE))
216 val = GPOINTER_TO_INT(g_hash_table_lookup
217 (folder_table, dropfolder));
219 g_hash_table_insert(folder_table, dropfolder,
224 /* CLAWS: new filtering */
225 dropfolder = folder_get_default_processing();
231 if ((msgnum = folder_item_add_msg(dropfolder, tmp_file, TRUE)) < 0) {
238 if (global_processing) {
239 /* CLAWS: new filtering */
241 filter_message(global_processing, inbox,
242 msgnum, folder_table);
247 } while (from_line[0] != '\0');
251 debug_print(_("%d messages found.\n"), msgs);
256 gint lock_mbox(const gchar *base, LockType type)
260 if (type == LOCK_FILE) {
261 gchar *lockfile, *locklink;
265 lockfile = g_strdup_printf("%s.%d", base, getpid());
266 if ((lockfp = fopen(lockfile, "wb")) == NULL) {
267 FILE_OP_ERROR(lockfile, "fopen");
268 g_warning(_("can't create lock file %s\n"), lockfile);
269 g_warning(_("use 'flock' instead of 'file' if possible.\n"));
274 fprintf(lockfp, "%d\n", getpid());
277 locklink = g_strconcat(base, ".lock", NULL);
278 while (link(lockfile, locklink) < 0) {
279 FILE_OP_ERROR(lockfile, "link");
281 g_warning(_("can't create %s\n"), lockfile);
287 g_warning(_("mailbox is owned by another"
288 " process, waiting...\n"));
294 } else if (type == LOCK_FLOCK) {
298 if ((lockfd = open(base, O_RDONLY)) < 0) {
300 if ((lockfd = open(base, O_RDWR)) < 0) {
302 FILE_OP_ERROR(base, "open");
306 if (flock(lockfd, LOCK_EX|LOCK_NB) < 0) {
310 if (lockf(lockfd, F_TLOCK, 0) < 0) {
315 #endif /* HAVE_FLOCK */
316 g_warning(_("can't lock %s\n"), base);
317 if (close(lockfd) < 0)
323 g_warning(_("invalid lock type\n"));
330 gint unlock_mbox(const gchar *base, gint fd, LockType type)
332 if (type == LOCK_FILE) {
335 lockfile = g_strconcat(base, ".lock", NULL);
336 if (unlink(lockfile) < 0) {
337 FILE_OP_ERROR(lockfile, "unlink");
344 } else if (type == LOCK_FLOCK) {
346 if (flock(fd, LOCK_UN) < 0) {
350 if (lockf(fd, F_ULOCK, 0) < 0) {
355 #endif /* HAVE_FLOCK */
356 g_warning(_("can't unlock %s\n"), base);
370 g_warning(_("invalid lock type\n"));
374 gint copy_mbox(const gchar *src, const gchar *dest)
376 return copy_file(src, dest, TRUE);
379 void empty_mbox(const gchar *mbox)
381 if (truncate(mbox, 0) < 0) {
384 FILE_OP_ERROR(mbox, "truncate");
385 if ((fp = fopen(mbox, "wb")) == NULL) {
386 FILE_OP_ERROR(mbox, "fopen");
387 g_warning(_("can't truncate mailbox to zero.\n"));
394 /* read all messages in SRC, and store them into one MBOX file. */
395 gint export_to_mbox(FolderItem *src, const gchar *mbox)
404 g_return_val_if_fail(src != NULL, -1);
405 g_return_val_if_fail(src->folder != NULL, -1);
406 g_return_val_if_fail(mbox != NULL, -1);
408 debug_print(_("Exporting messages from %s into %s...\n"),
411 if ((mbox_fp = fopen(mbox, "wb")) == NULL) {
412 FILE_OP_ERROR(mbox, "fopen");
416 mlist = folder_item_get_msg_list(src);
418 for (cur = mlist; cur != NULL; cur = cur->next) {
419 msginfo = (MsgInfo *)cur->data;
421 msg_fp = procmsg_open_message(msginfo);
423 procmsg_msginfo_free(msginfo);
428 msginfo->from ? msginfo->from :
429 cur_account && cur_account->address ?
430 cur_account->address : "unknown",
432 extract_address(buf);
434 fprintf(mbox_fp, "From %s %s",
435 buf, ctime(&msginfo->date_t));
437 while (fgets(buf, sizeof(buf), msg_fp) != NULL) {
438 if (!strncmp(buf, "From ", 5))
442 fputc('\n', mbox_fp);
445 procmsg_msginfo_free(msginfo);