2.10.0 unleashed
[claws.git] / src / mbox.c
blob5263d505b2d6e8ce1d3bc85bdf82595d3c0dcde7
1 /*
2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2007 Hiroyuki Yamamoto and the Claws Mail team
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #endif
25 #define _GNU_SOURCE
26 #include <stdio.h>
28 #include "defs.h"
29 #include <glib.h>
30 #include <glib/gi18n.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <string.h>
34 #include <fcntl.h>
35 #include <sys/file.h>
36 #include <ctype.h>
37 #include <time.h>
38 #include <errno.h>
40 #include "mbox.h"
41 #include "procmsg.h"
42 #include "folder.h"
43 #include "prefs_common.h"
44 #include "prefs_account.h"
45 #include "account.h"
46 #include "utils.h"
47 #include "filtering.h"
48 #include "alertpanel.h"
49 #include "statusbar.h"
51 #define MSGBUFSIZE 8192
53 #ifdef HAVE_FGETS_UNLOCKED
54 #define SC_FGETS fgets_unlocked
55 #define SC_FPUTS fputs_unlocked
56 #define SC_FPUTC fputc_unlocked
57 #else
58 #define SC_FGETS fgets
59 #define SC_FPUTS fputs
60 #define SC_FPUTC fputc
61 #endif
63 #define FPUTS_TO_TMP_ABORT_IF_FAIL(s) \
64 { \
65 lines++; \
66 if (fputs(s, tmp_fp) == EOF) { \
67 g_warning("can't write to temporary file\n"); \
68 fclose(tmp_fp); \
69 fclose(mbox_fp); \
70 g_unlink(tmp_file); \
71 g_free(tmp_file); \
72 return -1; \
73 } \
76 gint proc_mbox(FolderItem *dest, const gchar *mbox, gboolean apply_filter,
77 PrefsAccount *account)
78 /* return values: -1 error, >=0 number of msgs added */
80 FILE *mbox_fp;
81 gchar buf[MSGBUFSIZE];
82 gchar *tmp_file;
83 gint msgs = 0;
84 gint lines;
85 MsgInfo *msginfo;
86 gboolean more;
87 GSList *to_filter = NULL, *filtered = NULL, *unfiltered = NULL, *cur, *to_add = NULL;
88 gboolean printed = FALSE;
89 FolderItem *dropfolder;
91 g_return_val_if_fail(dest != NULL, -1);
92 g_return_val_if_fail(mbox != NULL, -1);
94 debug_print("Getting messages from %s into %s...\n", mbox, dest->path);
96 if ((mbox_fp = g_fopen(mbox, "rb")) == NULL) {
97 FILE_OP_ERROR(mbox, "fopen");
98 alertpanel_error(_("Could not open mbox file:\n%s\n"), mbox);
99 return -1;
102 /* ignore empty lines on the head */
103 do {
104 if (fgets(buf, sizeof(buf), mbox_fp) == NULL) {
105 g_warning("can't read mbox file.\n");
106 fclose(mbox_fp);
107 return -1;
109 } while (buf[0] == '\n' || buf[0] == '\r');
111 if (strncmp(buf, "From ", 5) != 0) {
112 g_warning("invalid mbox format: %s\n", mbox);
113 fclose(mbox_fp);
114 return -1;
117 tmp_file = get_tmp_file();
119 folder_item_update_freeze();
121 if (apply_filter)
122 dropfolder = folder_get_default_processing();
123 else
124 dropfolder = dest;
126 do {
127 FILE *tmp_fp;
128 gint empty_lines;
129 gint msgnum;
131 if (msgs > 0 && msgs%500 == 0) {
132 if (printed)
133 statusbar_pop_all();
134 statusbar_print_all(_("Importing from mbox... (%d mails imported)"), msgs);
135 printed=TRUE;
136 GTK_EVENTS_FLUSH();
139 if ((tmp_fp = g_fopen(tmp_file, "wb")) == NULL) {
140 FILE_OP_ERROR(tmp_file, "fopen");
141 g_warning("can't open temporary file\n");
142 fclose(mbox_fp);
143 g_free(tmp_file);
144 return -1;
146 if (change_file_mode_rw(tmp_fp, tmp_file) < 0) {
147 FILE_OP_ERROR(tmp_file, "chmod");
150 empty_lines = 0;
151 lines = 0;
152 more = FALSE;
154 /* process all lines from mboxrc file */
155 while (fgets(buf, sizeof(buf), mbox_fp) != NULL) {
156 int offset;
158 /* eof not reached, expect more lines */
159 more = TRUE;
161 /* eat empty lines */
162 if (buf[0] == '\n' || buf[0] == '\r') {
163 empty_lines++;
164 continue;
167 /* From separator or quoted From */
168 offset = 0;
169 /* detect leading '>' char(s) */
170 while ((buf[offset] == '>')) {
171 offset++;
173 if (!strncmp(buf+offset, "From ", 5)) {
174 /* From separator: */
175 if (offset == 0) {
176 /* expect next mbox item */
177 break;
180 /* quoted From: */
181 /* flush any eaten empty line */
182 if (empty_lines > 0) {
183 while (empty_lines-- > 0) {
184 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
186 empty_lines = 0;
188 /* store the unquoted line */
189 FPUTS_TO_TMP_ABORT_IF_FAIL(buf + 1);
190 continue;
193 /* other line */
194 /* flush any eaten empty line */
195 if (empty_lines > 0) {
196 while (empty_lines-- > 0) {
197 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
199 empty_lines = 0;
201 /* store the line itself */
202 FPUTS_TO_TMP_ABORT_IF_FAIL(buf);
204 /* end of mbox item or end of mbox */
206 /* flush any eaten empty line (but the last one) */
207 if (empty_lines > 0) {
208 while (--empty_lines > 0) {
209 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
213 /* more emails to expect? */
214 more = !feof(mbox_fp);
216 /* warn if email part is empty (it's the minimum check
217 we can do */
218 if (lines == 0) {
219 g_warning("malformed mbox: %s: message %d is empty\n", mbox, msgs);
220 fclose(tmp_fp);
221 fclose(mbox_fp);
222 g_unlink(tmp_file);
223 return -1;
226 if (fclose(tmp_fp) == EOF) {
227 FILE_OP_ERROR(tmp_file, "fclose");
228 g_warning("can't write to temporary file\n");
229 fclose(mbox_fp);
230 g_unlink(tmp_file);
231 g_free(tmp_file);
232 return -1;
235 if (apply_filter) {
236 if ((msgnum = folder_item_add_msg(dropfolder, tmp_file, NULL, TRUE)) < 0) {
237 fclose(mbox_fp);
238 g_unlink(tmp_file);
239 g_free(tmp_file);
240 return -1;
242 msginfo = folder_item_get_msginfo(dropfolder, msgnum);
243 to_filter = g_slist_prepend(to_filter, msginfo);
244 } else {
245 MsgFileInfo *finfo = g_new0(MsgFileInfo, 1);
246 finfo->file = tmp_file;
248 to_add = g_slist_prepend(to_add, finfo);
249 tmp_file = get_tmp_file();
251 /* flush every 500 */
252 if (msgs > 0 && msgs % 500 == 0) {
253 folder_item_add_msgs(dropfolder, to_add, TRUE);
254 procmsg_message_file_list_free(to_add);
255 to_add = NULL;
258 msgs++;
259 } while (more);
261 if (printed)
262 statusbar_pop_all();
264 if (apply_filter) {
265 procmsg_msglist_filter(to_filter, account,
266 &filtered, &unfiltered, TRUE);
268 filtering_move_and_copy_msgs(to_filter);
269 for (cur = filtered; cur; cur = g_slist_next(cur)) {
270 MsgInfo *info = (MsgInfo *)cur->data;
271 procmsg_msginfo_free(info);
274 unfiltered = g_slist_reverse(unfiltered);
275 folder_item_move_msgs(dest, unfiltered);
276 for (cur = unfiltered; cur; cur = g_slist_next(cur)) {
277 MsgInfo *info = (MsgInfo *)cur->data;
278 procmsg_msginfo_free(info);
281 g_slist_free(unfiltered);
282 g_slist_free(filtered);
283 g_slist_free(to_filter);
284 } else if (to_add) {
285 folder_item_add_msgs(dropfolder, to_add, TRUE);
286 procmsg_message_file_list_free(to_add);
287 to_add = NULL;
290 folder_item_update_thaw();
292 g_free(tmp_file);
293 fclose(mbox_fp);
294 debug_print("%d messages found.\n", msgs);
296 return msgs;
299 gint lock_mbox(const gchar *base, LockType type)
301 #ifdef G_OS_UNIX
302 gint retval = 0;
304 if (type == LOCK_FILE) {
305 gchar *lockfile, *locklink;
306 gint retry = 0;
307 FILE *lockfp;
309 lockfile = g_strdup_printf("%s.%d", base, getpid());
310 if ((lockfp = g_fopen(lockfile, "wb")) == NULL) {
311 FILE_OP_ERROR(lockfile, "fopen");
312 g_warning("can't create lock file %s\n", lockfile);
313 g_warning("use 'flock' instead of 'file' if possible.\n");
314 g_free(lockfile);
315 return -1;
318 fprintf(lockfp, "%d\n", getpid());
319 fclose(lockfp);
321 locklink = g_strconcat(base, ".lock", NULL);
322 while (link(lockfile, locklink) < 0) {
323 FILE_OP_ERROR(lockfile, "link");
324 if (retry >= 5) {
325 g_warning("can't create %s\n", lockfile);
326 g_unlink(lockfile);
327 g_free(lockfile);
328 return -1;
330 if (retry == 0)
331 g_warning("mailbox is owned by another"
332 " process, waiting...\n");
333 retry++;
334 sleep(5);
336 g_unlink(lockfile);
337 g_free(lockfile);
338 } else if (type == LOCK_FLOCK) {
339 gint lockfd;
340 gboolean fcntled = FALSE;
341 #if HAVE_FCNTL_H && !defined(G_OS_WIN32)
342 struct flock fl;
343 fl.l_type = F_WRLCK;
344 fl.l_whence = SEEK_SET;
345 fl.l_start = 0;
346 fl.l_len = 0;
347 #endif
349 #if HAVE_FLOCK
350 if ((lockfd = open(base, O_RDWR)) < 0) {
351 #else
352 if ((lockfd = open(base, O_RDWR)) < 0) {
353 #endif
354 FILE_OP_ERROR(base, "open");
355 return -1;
358 #if HAVE_FCNTL_H && !defined(G_OS_WIN32)
359 if (fcntl(lockfd, F_SETLK, &fl) == -1) {
360 g_warning("can't fnctl %s (%s)", base, strerror(errno));
361 return -1;
362 } else {
363 fcntled = TRUE;
365 #endif
367 #if HAVE_FLOCK
368 if (flock(lockfd, LOCK_EX|LOCK_NB) < 0 && !fcntled) {
369 perror("flock");
370 #else
371 #if HAVE_LOCKF
372 if (lockf(lockfd, F_TLOCK, 0) < 0 && !fcntled) {
373 perror("lockf");
374 #else
376 #endif
377 #endif /* HAVE_FLOCK */
378 g_warning("can't lock %s\n", base);
379 if (close(lockfd) < 0)
380 perror("close");
381 return -1;
383 retval = lockfd;
384 } else {
385 g_warning("invalid lock type\n");
386 return -1;
389 return retval;
390 #else
391 return -1;
392 #endif /* G_OS_UNIX */
395 gint unlock_mbox(const gchar *base, gint fd, LockType type)
397 if (type == LOCK_FILE) {
398 gchar *lockfile;
400 lockfile = g_strconcat(base, ".lock", NULL);
401 if (g_unlink(lockfile) < 0) {
402 FILE_OP_ERROR(lockfile, "unlink");
403 g_free(lockfile);
404 return -1;
406 g_free(lockfile);
408 return 0;
409 } else if (type == LOCK_FLOCK) {
410 gboolean fcntled = FALSE;
411 #if HAVE_FCNTL_H && !defined(G_OS_WIN32)
412 struct flock fl;
413 fl.l_type = F_UNLCK;
414 fl.l_whence = SEEK_SET;
415 fl.l_start = 0;
416 fl.l_len = 0;
418 if (fcntl(fd, F_SETLK, &fl) == -1) {
419 g_warning("can't fnctl %s", base);
420 } else {
421 fcntled = TRUE;
423 #endif
424 #if HAVE_FLOCK
425 if (flock(fd, LOCK_UN) < 0 && !fcntled) {
426 perror("flock");
427 #else
428 #if HAVE_LOCKF
429 if (lockf(fd, F_ULOCK, 0) < 0 && !fcntled) {
430 perror("lockf");
431 #else
433 #endif
434 #endif /* HAVE_FLOCK */
435 g_warning("can't unlock %s\n", base);
436 if (close(fd) < 0)
437 perror("close");
438 return -1;
441 if (close(fd) < 0) {
442 perror("close");
443 return -1;
446 return 0;
449 g_warning("invalid lock type\n");
450 return -1;
453 gint copy_mbox(gint srcfd, const gchar *dest)
455 FILE *dest_fp;
456 ssize_t n_read;
457 gchar buf[BUFSIZ];
458 gboolean err = FALSE;
459 int save_errno = 0;
461 if (srcfd < 0) {
462 return -1;
465 if ((dest_fp = g_fopen(dest, "wb")) == NULL) {
466 FILE_OP_ERROR(dest, "fopen");
467 return -1;
470 if (change_file_mode_rw(dest_fp, dest) < 0) {
471 FILE_OP_ERROR(dest, "chmod");
472 g_warning("can't change file mode\n");
475 while ((n_read = read(srcfd, buf, sizeof(buf))) > 0) {
476 if (n_read == -1 && errno != 0) {
477 save_errno = errno;
478 break;
480 if (fwrite(buf, 1, n_read, dest_fp) < n_read) {
481 g_warning("writing to %s failed.\n", dest);
482 fclose(dest_fp);
483 g_unlink(dest);
484 return -1;
488 if (save_errno != 0) {
489 g_warning("error %d reading mbox: %s\n", save_errno,
490 strerror(save_errno));
491 err = TRUE;
494 if (fclose(dest_fp) == EOF) {
495 FILE_OP_ERROR(dest, "fclose");
496 err = TRUE;
499 if (err) {
500 g_unlink(dest);
501 return -1;
504 return 0;
507 void empty_mbox(const gchar *mbox)
509 FILE *fp;
511 if ((fp = g_fopen(mbox, "wb")) == NULL) {
512 FILE_OP_ERROR(mbox, "fopen");
513 g_warning("can't truncate mailbox to zero.\n");
514 return;
516 fclose(fp);
519 gint export_list_to_mbox(GSList *mlist, const gchar *mbox)
520 /* return values: -2 skipped, -1 error, 0 OK */
522 GSList *cur;
523 MsgInfo *msginfo;
524 FILE *msg_fp;
525 FILE *mbox_fp;
526 gchar buf[BUFFSIZE];
527 gint msgs = 1, total = g_slist_length(mlist);
528 if (g_file_test(mbox, G_FILE_TEST_EXISTS) == TRUE) {
529 if (alertpanel_full(_("Overwrite mbox file"),
530 _("This file already exists. Do you want to overwrite it?"),
531 GTK_STOCK_CANCEL, _("Overwrite"), NULL, FALSE,
532 NULL, ALERT_WARNING, G_ALERTDEFAULT)
533 != G_ALERTALTERNATE) {
534 return -2;
538 if ((mbox_fp = g_fopen(mbox, "wb")) == NULL) {
539 FILE_OP_ERROR(mbox, "fopen");
540 alertpanel_error(_("Could not create mbox file:\n%s\n"), mbox);
541 return -1;
544 #ifdef HAVE_FGETS_UNLOCKED
545 flockfile(mbox_fp);
546 #endif
548 statusbar_print_all(_("Exporting to mbox..."));
549 for (cur = mlist; cur != NULL; cur = cur->next) {
550 int len;
551 gchar buft[BUFFSIZE];
552 msginfo = (MsgInfo *)cur->data;
554 msg_fp = procmsg_open_message(msginfo);
555 if (!msg_fp) {
556 continue;
559 #ifdef HAVE_FGETS_UNLOCKED
560 flockfile(msg_fp);
561 #endif
562 strncpy2(buf,
563 msginfo->from ? msginfo->from :
564 cur_account && cur_account->address ?
565 cur_account->address : "unknown",
566 sizeof(buf));
567 extract_address(buf);
569 fprintf(mbox_fp, "From %s %s",
570 buf, ctime_r(&msginfo->date_t, buft));
572 buf[0] = '\0';
574 /* write email to mboxrc */
575 while (SC_FGETS(buf, sizeof(buf), msg_fp) != NULL) {
576 /* quote any From, >From, >>From, etc., according to mbox format specs */
577 int offset;
579 offset = 0;
580 /* detect leading '>' char(s) */
581 while ((buf[offset] == '>')) {
582 offset++;
584 if (!strncmp(buf+offset, "From ", 5))
585 SC_FPUTC('>', mbox_fp);
586 SC_FPUTS(buf, mbox_fp);
589 /* force last line to end w/ a newline */
590 len = strlen(buf);
591 if (len > 0) {
592 len--;
593 if ((buf[len] != '\n') && (buf[len] != '\r'))
594 SC_FPUTC('\n', mbox_fp);
597 /* add a trailing empty line */
598 SC_FPUTC('\n', mbox_fp);
600 #ifdef HAVE_FGETS_UNLOCKED
601 funlockfile(msg_fp);
602 #endif
603 fclose(msg_fp);
604 statusbar_progress_all(msgs++,total, 500);
605 if (msgs%500 == 0)
606 GTK_EVENTS_FLUSH();
608 statusbar_progress_all(0,0,0);
609 statusbar_pop_all();
611 #ifdef HAVE_FGETS_UNLOCKED
612 funlockfile(mbox_fp);
613 #endif
614 fclose(mbox_fp);
616 return 0;
619 /* read all messages in SRC, and store them into one MBOX file. */
620 /* return values: -2 skipped, -1 error, 0 OK */
621 gint export_to_mbox(FolderItem *src, const gchar *mbox)
623 GSList *mlist;
624 gint ret;
626 g_return_val_if_fail(src != NULL, -1);
627 g_return_val_if_fail(src->folder != NULL, -1);
628 g_return_val_if_fail(mbox != NULL, -1);
630 debug_print("Exporting messages from %s into %s...\n",
631 src->path, mbox);
633 mlist = folder_item_get_msg_list(src);
635 folder_item_update_freeze();
636 ret = export_list_to_mbox(mlist, mbox);
637 folder_item_update_thaw();
639 procmsg_msg_list_free(mlist);
641 return ret;