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")); \
59 gint proc_mbox(FolderItem *dest, const gchar *mbox, GHashTable *folder_table)
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, "r")) == 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;
103 gchar *startp, *endp, *rpath;
106 gboolean is_next_msg = FALSE;
109 if ((tmp_fp = fopen(tmp_file, "w")) == NULL) {
110 FILE_OP_ERROR(tmp_file, "fopen");
111 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 */
119 startp = from_line + 5;
120 endp = strchr(startp, ' ');
122 rpath = g_strdup(startp);
124 rpath = g_strndup(startp, endp - startp);
126 g_snprintf(from_line, sizeof(from_line),
127 "Return-Path: %s\n", rpath);
130 FPUTS_TO_TMP_ABORT_IF_FAIL(from_line);
131 FPUTS_TO_TMP_ABORT_IF_FAIL(buf);
136 while (fgets(buf, sizeof(buf), mbox_fp) != NULL) {
137 if (buf[0] == '\n' || buf[0] == '\r') {
144 while (!strncmp(buf, "From ", 5)) {
145 strcpy(from_line, buf);
146 if (fgets(buf, sizeof(buf), mbox_fp) == NULL) {
151 if (is_header_line(buf)) {
154 } else if (!strncmp(buf, "From ", 5)) {
156 } else if (!strncmp(buf, ">From ", 6)) {
157 g_memmove(buf, buf + 1, strlen(buf));
161 g_warning(_("unescaped From found:\n%s"),
166 if (is_next_msg) break;
168 if (empty_line > 0) {
170 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
174 if (from_line[0] != '\0') {
175 FPUTS_TO_TMP_ABORT_IF_FAIL(from_line);
179 if (buf[0] != '\0') {
180 if (!strncmp(buf, ">From ", 6)) {
181 FPUTS_TO_TMP_ABORT_IF_FAIL(buf + 1);
183 FPUTS_TO_TMP_ABORT_IF_FAIL(buf);
189 if (empty_line > 0) {
191 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
194 if (fclose(tmp_fp) == EOF) {
195 FILE_OP_ERROR(tmp_file, "fclose");
196 g_warning(_("can't write to temporary file\n"));
203 if (prefs_filtering == NULL) {
205 dropfolder = filter_get_dest_folder
206 (prefs_common.fltlist, tmp_file);
208 !strcmp(dropfolder->path, FILTER_NOT_RECEIVE))
210 val = GPOINTER_TO_INT(g_hash_table_lookup
211 (folder_table, dropfolder));
213 folder_item_scan(dropfolder);
214 g_hash_table_insert(folder_table, dropfolder,
225 if ((msgnum = folder_item_add_msg(dropfolder, tmp_file, TRUE)) < 0) {
231 folder_item_scan(dropfolder);
233 if (prefs_filtering != NULL) {
236 filter_message(prefs_filtering, dropfolder,
237 msgnum, folder_table);
242 } while (from_line[0] != '\0');
245 debug_print(_("%d messages found.\n"), msgs);
250 gint lock_mbox(const gchar *base, LockType type)
254 if (type == LOCK_FILE) {
255 gchar *lockfile, *locklink;
259 lockfile = g_strdup_printf("%s.%d", base, getpid());
260 if ((lockfp = fopen(lockfile, "w")) == NULL) {
261 FILE_OP_ERROR(lockfile, "fopen");
262 g_warning(_("can't create lock file %s\n"), lockfile);
263 g_warning(_("use 'flock' instead of 'file' if possible.\n"));
268 fprintf(lockfp, "%d\n", getpid());
271 locklink = g_strconcat(base, ".lock", NULL);
272 while (link(lockfile, locklink) < 0) {
273 FILE_OP_ERROR(lockfile, "link");
275 g_warning(_("can't create %s\n"), lockfile);
281 g_warning(_("mailbox is owned by another"
282 " process, waiting...\n"));
288 } else if (type == LOCK_FLOCK) {
292 if ((lockfd = open(base, O_RDONLY)) < 0) {
294 if ((lockfd = open(base, O_RDWR)) < 0) {
296 FILE_OP_ERROR(base, "open");
300 if (flock(lockfd, LOCK_EX|LOCK_NB) < 0) {
304 if (lockf(lockfd, F_TLOCK, 0) < 0) {
309 #endif /* HAVE_FLOCK */
310 g_warning(_("can't lock %s\n"), base);
311 if (close(lockfd) < 0)
317 g_warning(_("invalid lock type\n"));
324 gint unlock_mbox(const gchar *base, gint fd, LockType type)
326 if (type == LOCK_FILE) {
329 lockfile = g_strconcat(base, ".lock", NULL);
330 if (unlink(lockfile) < 0) {
331 FILE_OP_ERROR(lockfile, "unlink");
338 } else if (type == LOCK_FLOCK) {
340 if (flock(fd, LOCK_UN) < 0) {
344 if (lockf(fd, F_ULOCK, 0) < 0) {
349 #endif /* HAVE_FLOCK */
350 g_warning(_("can't unlock %s\n"), base);
364 g_warning(_("invalid lock type\n"));
368 gint copy_mbox(const gchar *src, const gchar *dest)
370 return copy_file(src, dest);
373 void empty_mbox(const gchar *mbox)
375 if (truncate(mbox, 0) < 0) {
378 FILE_OP_ERROR(mbox, "truncate");
379 if ((fp = fopen(mbox, "w")) == NULL) {
380 FILE_OP_ERROR(mbox, "fopen");
381 g_warning(_("can't truncate mailbox to zero.\n"));
388 /* read all messages in SRC, and store them into one MBOX file. */
389 gint export_to_mbox(FolderItem *src, const gchar *mbox)
398 g_return_val_if_fail(src != NULL, -1);
399 g_return_val_if_fail(src->folder != NULL, -1);
400 g_return_val_if_fail(mbox != NULL, -1);
402 debug_print(_("Exporting messages from %s into %s...\n"),
405 if ((mbox_fp = fopen(mbox, "w")) == NULL) {
406 FILE_OP_ERROR(mbox, "fopen");
410 mlist = src->folder->get_msg_list(src->folder, src, TRUE);
412 for (cur = mlist; cur != NULL; cur = cur->next) {
413 msginfo = (MsgInfo *)cur->data;
415 msg_fp = procmsg_open_message(msginfo);
417 procmsg_msginfo_free(msginfo);
422 msginfo->from ? msginfo->from :
423 cur_account && cur_account->address ?
424 cur_account->address : "unknown",
426 extract_address(buf);
428 fprintf(mbox_fp, "From %s %s",
429 buf, ctime(&msginfo->date_t));
431 while (fgets(buf, sizeof(buf), msg_fp) != NULL) {
432 if (!strncmp(buf, "From ", 5))
436 fputc('\n', mbox_fp);
439 procmsg_msginfo_free(msginfo);