recognise font/* and chemical/* mime types
[claws.git] / src / mbox.c
blobab19fd940b2b079265d1f56adff2830134641d50
1 /*
2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2022 the Claws Mail team and 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 3 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, see <http://www.gnu.org/licenses/>.
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #include "claws-features.h"
23 #endif
26 #include <stdio.h>
28 #ifdef USE_PTHREAD
29 #include <pthread.h>
30 #endif
32 #include "defs.h"
33 #include <glib.h>
34 #include <glib/gi18n.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <string.h>
38 #include <fcntl.h>
39 #include <sys/file.h>
40 #include <ctype.h>
41 #include <time.h>
42 #include <errno.h>
44 #include "mbox.h"
45 #include "procmsg.h"
46 #include "folder.h"
47 #include "prefs_common.h"
48 #include "prefs_account.h"
49 #include "account.h"
50 #include "utils.h"
51 #include "filtering.h"
52 #include "alertpanel.h"
53 #include "statusbar.h"
54 #include "file-utils.h"
56 #define MESSAGEBUFSIZE 8192
58 #define FPUTS_TO_TMP_ABORT_IF_FAIL(s) \
59 { \
60 lines++; \
61 if (claws_fputs(s, tmp_fp) == EOF) { \
62 g_warning("can't write to temporary file"); \
63 claws_fclose(tmp_fp); \
64 claws_fclose(mbox_fp); \
65 claws_unlink(tmp_file); \
66 g_free(tmp_file); \
67 return -1; \
68 } \
71 gint proc_mbox(FolderItem *dest, const gchar *mbox, gboolean apply_filter,
72 PrefsAccount *account)
73 /* return values: -1 error, >=0 number of msgs added */
75 FILE *mbox_fp;
76 gchar buf[MESSAGEBUFSIZE];
77 gchar *tmp_file;
78 gint msgs = 0;
79 gint lines;
80 MsgInfo *msginfo;
81 gboolean more;
82 GSList *to_filter = NULL, *filtered = NULL, *unfiltered = NULL, *cur, *to_add = NULL;
83 gboolean printed = FALSE;
84 FolderItem *dropfolder;
85 GStatBuf src_stat;
87 cm_return_val_if_fail(dest != NULL, -1);
88 cm_return_val_if_fail(mbox != NULL, -1);
90 debug_print("Getting messages from %s into %s...\n", mbox, dest->path);
92 if (g_stat(mbox, &src_stat) < 0) {
93 FILE_OP_ERROR(mbox, "g_stat");
94 alertpanel_error(_("Could not stat mbox file:\n%s\n"), mbox);
95 return -1;
98 if ((mbox_fp = claws_fopen(mbox, "rb")) == NULL) {
99 FILE_OP_ERROR(mbox, "claws_fopen");
100 alertpanel_error(_("Could not open mbox file:\n%s\n"), mbox);
101 return -1;
104 /* ignore empty lines on the head */
105 do {
106 if (claws_fgets(buf, sizeof(buf), mbox_fp) == NULL) {
107 g_warning("can't read mbox file");
108 claws_fclose(mbox_fp);
109 return -1;
111 } while (buf[0] == '\n' || buf[0] == '\r');
113 if (strncmp(buf, "From ", 5) != 0) {
114 g_warning("invalid mbox format: %s", mbox);
115 claws_fclose(mbox_fp);
116 return -1;
119 tmp_file = get_tmp_file();
121 folder_item_update_freeze();
123 if (apply_filter)
124 dropfolder = folder_get_default_processing(account->account_id);
125 else
126 dropfolder = dest;
128 do {
129 FILE *tmp_fp;
130 gint empty_lines;
131 gint msgnum;
133 if (msgs%10 == 0) {
134 long cur_offset_mb = ftell(mbox_fp) / (1024 * 1024);
135 if (printed)
136 statusbar_pop_all();
137 statusbar_print_all(
138 ngettext("Importing from mbox... (%ld MB imported)",
139 "Importing from mbox... (%ld MB imported)", cur_offset_mb), cur_offset_mb);
140 statusbar_progress_all(cur_offset_mb, src_stat.st_size / (1024*1024), 1);
141 printed=TRUE;
142 GTK_EVENTS_FLUSH();
145 if ((tmp_fp = claws_fopen(tmp_file, "wb")) == NULL) {
146 FILE_OP_ERROR(tmp_file, "claws_fopen");
147 g_warning("can't open temporary file");
148 claws_fclose(mbox_fp);
149 g_free(tmp_file);
150 return -1;
152 if (change_file_mode_rw(tmp_fp, tmp_file) < 0) {
153 FILE_OP_ERROR(tmp_file, "chmod");
156 empty_lines = 0;
157 lines = 0;
159 /* process all lines from mboxrc file */
160 while (claws_fgets(buf, sizeof(buf), mbox_fp) != NULL) {
161 int offset;
163 /* eat empty lines */
164 if (buf[0] == '\n' || buf[0] == '\r') {
165 empty_lines++;
166 continue;
169 /* From separator or quoted From */
170 offset = 0;
171 /* detect leading '>' char(s) */
172 while (buf[offset] == '>') {
173 offset++;
175 if (!strncmp(buf+offset, "From ", 5)) {
176 /* From separator: */
177 if (offset == 0) {
178 /* expect next mbox item */
179 break;
182 /* quoted From: */
183 /* flush any eaten empty line */
184 if (empty_lines > 0) {
185 while (empty_lines-- > 0) {
186 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
188 empty_lines = 0;
190 /* store the unquoted line */
191 FPUTS_TO_TMP_ABORT_IF_FAIL(buf + 1);
192 continue;
195 /* other line */
196 /* flush any eaten empty line */
197 if (empty_lines > 0) {
198 while (empty_lines-- > 0) {
199 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
201 empty_lines = 0;
203 /* store the line itself */
204 FPUTS_TO_TMP_ABORT_IF_FAIL(buf);
206 /* end of mbox item or end of mbox */
208 /* flush any eaten empty line (but the last one) */
209 if (empty_lines > 0) {
210 while (--empty_lines > 0) {
211 FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
215 /* more emails to expect? */
216 more = !claws_feof(mbox_fp);
218 /* warn if email part is empty (it's the minimum check
219 we can do */
220 if (lines == 0) {
221 g_warning("malformed mbox: %s: message %d is empty", mbox, msgs);
222 claws_fclose(tmp_fp);
223 claws_fclose(mbox_fp);
224 claws_unlink(tmp_file);
225 return -1;
228 if (claws_safe_fclose(tmp_fp) == EOF) {
229 FILE_OP_ERROR(tmp_file, "claws_fclose");
230 g_warning("can't write to temporary file");
231 claws_fclose(mbox_fp);
232 claws_unlink(tmp_file);
233 g_free(tmp_file);
234 return -1;
237 if (apply_filter) {
238 if ((msgnum = folder_item_add_msg(dropfolder, tmp_file, NULL, TRUE)) < 0) {
239 claws_fclose(mbox_fp);
240 claws_unlink(tmp_file);
241 g_free(tmp_file);
242 return -1;
244 msginfo = folder_item_get_msginfo(dropfolder, msgnum);
245 to_filter = g_slist_prepend(to_filter, msginfo);
246 } else {
247 MsgFileInfo *finfo = g_new0(MsgFileInfo, 1);
248 finfo->file = tmp_file;
250 to_add = g_slist_prepend(to_add, finfo);
251 tmp_file = get_tmp_file();
253 /* flush every 500 */
254 if (msgs > 0 && msgs % 500 == 0) {
255 folder_item_add_msgs(dropfolder, to_add, TRUE);
256 procmsg_message_file_list_free(to_add);
257 to_add = NULL;
260 msgs++;
261 } while (more);
263 if (printed) {
264 statusbar_pop_all();
265 statusbar_progress_all(0, 0, 0);
268 if (apply_filter) {
270 folder_item_set_batch(dropfolder, FALSE);
271 procmsg_msglist_filter(to_filter, account,
272 &filtered, &unfiltered, TRUE);
273 folder_item_set_batch(dropfolder, TRUE);
275 filtering_move_and_copy_msgs(to_filter);
276 for (cur = filtered; cur; cur = g_slist_next(cur)) {
277 MsgInfo *info = (MsgInfo *)cur->data;
278 procmsg_msginfo_free(&info);
281 unfiltered = g_slist_reverse(unfiltered);
282 if (unfiltered) {
283 folder_item_move_msgs(dest, unfiltered);
284 for (cur = unfiltered; cur; cur = g_slist_next(cur)) {
285 MsgInfo *info = (MsgInfo *)cur->data;
286 procmsg_msginfo_free(&info);
290 g_slist_free(unfiltered);
291 g_slist_free(filtered);
292 g_slist_free(to_filter);
293 } else if (to_add) {
294 folder_item_add_msgs(dropfolder, to_add, TRUE);
295 procmsg_message_file_list_free(to_add);
296 to_add = NULL;
299 folder_item_update_thaw();
301 g_free(tmp_file);
302 claws_fclose(mbox_fp);
303 debug_print("%d messages found.\n", msgs);
305 return msgs;
308 gint lock_mbox(const gchar *base, LockType type)
310 #ifdef G_OS_UNIX
311 gint retval = 0;
313 if (type == LOCK_FILE) {
314 gchar *lockfile, *locklink;
315 gint retry = 0;
316 FILE *lockfp;
318 lockfile = g_strdup_printf("%s.%d", base, getpid());
319 if ((lockfp = claws_fopen(lockfile, "wb")) == NULL) {
320 FILE_OP_ERROR(lockfile, "claws_fopen");
321 g_warning("can't create lock file '%s', use 'flock' instead of 'file' if possible", lockfile);
322 g_free(lockfile);
323 return -1;
326 if (fprintf(lockfp, "%d\n", getpid()) < 0) {
327 FILE_OP_ERROR(lockfile, "fprintf");
328 g_free(lockfile);
329 claws_fclose(lockfp);
330 return -1;
333 if (claws_safe_fclose(lockfp) == EOF) {
334 FILE_OP_ERROR(lockfile, "claws_fclose");
335 g_free(lockfile);
336 return -1;
339 locklink = g_strconcat(base, ".lock", NULL);
340 while (link(lockfile, locklink) < 0) {
341 FILE_OP_ERROR(lockfile, "link");
342 if (retry >= 5) {
343 g_warning("can't create '%s'", lockfile);
344 claws_unlink(lockfile);
345 g_free(locklink);
346 g_free(lockfile);
347 return -1;
349 if (retry == 0)
350 g_warning("mailbox is owned by another process, waiting");
351 retry++;
352 sleep(5);
354 claws_unlink(lockfile);
355 g_free(locklink);
356 g_free(lockfile);
357 } else if (type == LOCK_FLOCK) {
358 gint lockfd;
359 gboolean fcntled = FALSE;
360 #if HAVE_FCNTL_H && !defined(G_OS_WIN32)
361 struct flock fl;
362 fl.l_type = F_WRLCK;
363 fl.l_whence = SEEK_SET;
364 fl.l_start = 0;
365 fl.l_len = 0;
366 #endif
368 #if HAVE_FLOCK
369 if ((lockfd = g_open(base, O_RDWR, 0)) < 0) {
370 #else
371 if ((lockfd = g_open(base, O_RDWR, 0)) < 0) {
372 #endif
373 FILE_OP_ERROR(base, "open");
374 return -1;
377 #if HAVE_FCNTL_H && !defined(G_OS_WIN32)
378 if (fcntl(lockfd, F_SETLK, &fl) == -1) {
379 g_warning("can't fnctl %s (%s)", base, g_strerror(errno));
380 close(lockfd);
381 return -1;
382 } else {
383 fcntled = TRUE;
385 #endif
387 #if HAVE_FLOCK
388 if (flock(lockfd, LOCK_EX|LOCK_NB) < 0 && !fcntled) {
389 perror("flock");
390 #else
391 #if HAVE_LOCKF
392 if (lockf(lockfd, F_TLOCK, 0) < 0 && !fcntled) {
393 perror("lockf");
394 #else
396 #endif
397 #endif /* HAVE_FLOCK */
398 g_warning("can't lock %s", base);
399 if (close(lockfd) < 0)
400 perror("close");
401 return -1;
403 retval = lockfd;
404 } else {
405 g_warning("invalid lock type");
406 return -1;
409 return retval;
410 #else
411 return -1;
412 #endif /* G_OS_UNIX */
415 gint unlock_mbox(const gchar *base, gint fd, LockType type)
417 if (type == LOCK_FILE) {
418 gchar *lockfile;
420 lockfile = g_strconcat(base, ".lock", NULL);
421 if (claws_unlink(lockfile) < 0) {
422 FILE_OP_ERROR(lockfile, "unlink");
423 g_free(lockfile);
424 return -1;
426 g_free(lockfile);
428 return 0;
429 } else if (type == LOCK_FLOCK) {
430 #if HAVE_FCNTL_H && !defined(G_OS_WIN32)
431 gboolean fcntled = FALSE;
432 struct flock fl;
433 fl.l_type = F_UNLCK;
434 fl.l_whence = SEEK_SET;
435 fl.l_start = 0;
436 fl.l_len = 0;
438 if (fcntl(fd, F_SETLK, &fl) == -1) {
439 g_warning("can't fnctl %s", base);
440 } else {
441 fcntled = TRUE;
443 #endif
444 #if HAVE_FLOCK
445 if (flock(fd, LOCK_UN) < 0 && !fcntled) {
446 perror("flock");
447 #else
448 #if HAVE_LOCKF
449 if (lockf(fd, F_ULOCK, 0) < 0 && !fcntled) {
450 perror("lockf");
451 #else
453 #endif
454 #endif /* HAVE_FLOCK */
455 g_warning("can't unlock %s", base);
456 if (close(fd) < 0)
457 perror("close");
458 return -1;
461 if (close(fd) < 0) {
462 perror("close");
463 return -1;
466 return 0;
469 g_warning("invalid lock type");
470 return -1;
473 gint copy_mbox(gint srcfd, const gchar *dest)
475 FILE *dest_fp;
476 ssize_t n_read;
477 gchar buf[BUFSIZ];
478 gboolean err = FALSE;
479 int save_errno = 0;
481 if (srcfd < 0) {
482 return -1;
485 if ((dest_fp = claws_fopen(dest, "wb")) == NULL) {
486 FILE_OP_ERROR(dest, "claws_fopen");
487 return -1;
490 if (change_file_mode_rw(dest_fp, dest) < 0) {
491 FILE_OP_ERROR(dest, "chmod");
492 g_warning("can't change file mode");
495 while ((n_read = read(srcfd, buf, sizeof(buf))) > 0) {
496 if (claws_fwrite(buf, 1, n_read, dest_fp) < n_read) {
497 g_warning("writing to %s failed", dest);
498 claws_fclose(dest_fp);
499 claws_unlink(dest);
500 return -1;
504 if (save_errno != 0) {
505 g_warning("error %d reading mbox: %s", save_errno,
506 g_strerror(save_errno));
507 err = TRUE;
510 if (claws_safe_fclose(dest_fp) == EOF) {
511 FILE_OP_ERROR(dest, "claws_fclose");
512 err = TRUE;
515 if (err) {
516 claws_unlink(dest);
517 return -1;
520 return 0;
523 void empty_mbox(const gchar *mbox)
525 FILE *fp;
527 if ((fp = claws_fopen(mbox, "wb")) == NULL) {
528 FILE_OP_ERROR(mbox, "claws_fopen");
529 g_warning("can't truncate mailbox to zero");
530 return;
532 claws_safe_fclose(fp);
535 gint export_list_to_mbox(GSList *mlist, const gchar *mbox)
536 /* return values: -2 skipped, -1 error, 0 OK */
538 GSList *cur;
539 MsgInfo *msginfo;
540 FILE *msg_fp;
541 FILE *mbox_fp;
542 gchar buf[BUFFSIZE];
543 int err = 0;
545 gint msgs = 1, total = g_slist_length(mlist);
546 if (g_file_test(mbox, G_FILE_TEST_EXISTS) == TRUE) {
547 if (alertpanel_full(_("Overwrite mbox file"),
548 _("This file already exists. Do you want to overwrite it?"),
549 NULL, _("_Cancel"), NULL, _("Overwrite"), NULL, NULL,
550 ALERTFOCUS_FIRST, FALSE, NULL, ALERT_WARNING)
551 != G_ALERTALTERNATE) {
552 return -2;
556 if ((mbox_fp = claws_fopen(mbox, "wb")) == NULL) {
557 FILE_OP_ERROR(mbox, "claws_fopen");
558 alertpanel_error(_("Could not create mbox file:\n%s\n"), mbox);
559 return -1;
562 statusbar_print_all(_("Exporting to mbox..."));
563 for (cur = mlist; cur != NULL; cur = cur->next) {
564 int len;
565 gchar buft[BUFFSIZE];
566 msginfo = (MsgInfo *)cur->data;
568 msg_fp = procmsg_open_message(msginfo, TRUE);
569 if (!msg_fp) {
570 continue;
573 strncpy2(buf,
574 msginfo->from ? msginfo->from :
575 cur_account && cur_account->address ?
576 cur_account->address : "unknown",
577 sizeof(buf));
578 extract_address(buf);
580 if (fprintf(mbox_fp, "From %s %s",
581 buf, ctime_r(&msginfo->date_t, buft)) < 0) {
582 err = -1;
583 claws_fclose(msg_fp);
584 goto out;
587 buf[0] = '\0';
589 /* write email to mboxrc */
590 while (claws_fgets(buf, sizeof(buf), msg_fp) != NULL) {
591 /* quote any From, >From, >>From, etc., according to mbox format specs */
592 int offset;
594 offset = 0;
595 /* detect leading '>' char(s) */
596 while (buf[offset] == '>') {
597 offset++;
599 if (!strncmp(buf+offset, "From ", 5)) {
600 if (claws_fputc('>', mbox_fp) == EOF) {
601 err = -1;
602 claws_fclose(msg_fp);
603 goto out;
606 if (claws_fputs(buf, mbox_fp) == EOF) {
607 err = -1;
608 claws_fclose(msg_fp);
609 goto out;
613 /* force last line to end w/ a newline */
614 len = strlen(buf);
615 if (len > 0) {
616 len--;
617 if ((buf[len] != '\n') && (buf[len] != '\r')) {
618 if (claws_fputc('\n', mbox_fp) == EOF) {
619 err = -1;
620 claws_fclose(msg_fp);
621 goto out;
626 /* add a trailing empty line */
627 if (claws_fputc('\n', mbox_fp) == EOF) {
628 err = -1;
629 claws_fclose(msg_fp);
630 goto out;
633 claws_safe_fclose(msg_fp);
634 statusbar_progress_all(msgs++,total, 500);
635 if (msgs%500 == 0)
636 GTK_EVENTS_FLUSH();
639 out:
640 statusbar_progress_all(0,0,0);
641 statusbar_pop_all();
643 claws_safe_fclose(mbox_fp);
645 return err;
648 /* read all messages in SRC, and store them into one MBOX file. */
649 /* return values: -2 skipped, -1 error, 0 OK */
650 gint export_to_mbox(FolderItem *src, const gchar *mbox)
652 GSList *mlist;
653 gint ret;
655 cm_return_val_if_fail(src != NULL, -1);
656 cm_return_val_if_fail(src->folder != NULL, -1);
657 cm_return_val_if_fail(mbox != NULL, -1);
659 debug_print("Exporting messages from %s into %s...\n",
660 src->path, mbox);
662 mlist = folder_item_get_msg_list(src);
664 folder_item_update_freeze();
665 ret = export_list_to_mbox(mlist, mbox);
666 folder_item_update_thaw();
668 procmsg_msg_list_free(mlist);
670 return ret;