NeoMutt  2025-12-11-236-g400a5f
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
mbox.c
Go to the documentation of this file.
1
25
36
37#include "config.h"
38#include <errno.h>
39#include <fcntl.h>
40#include <inttypes.h>
41#include <stdbool.h>
42#include <stdio.h>
43#include <string.h>
44#include <sys/stat.h>
45#include <sys/types.h>
46#include <time.h>
47#include <unistd.h>
48#include <utime.h>
49#include "mutt/lib.h"
50#include "address/lib.h"
51#include "config/lib.h"
52#include "email/lib.h"
53#include "core/lib.h"
54#include "mutt.h"
55#include "lib.h"
56#include "progress/lib.h"
57#include "globals.h"
58#include "muttlib.h"
59#include "mx.h"
60
64struct MUpdate
65{
66 bool valid;
67 LOFF_T hdr;
68 LOFF_T body;
69 long lines;
70 LOFF_T length;
71};
72
76static void mbox_adata_free(void **ptr)
77{
78 if (!ptr || !*ptr)
79 return;
80
81 struct MboxAccountData *m = *ptr;
82
84 FREE(ptr);
85}
86
91static struct MboxAccountData *mbox_adata_new(void)
92{
93 return MUTT_MEM_CALLOC(1, struct MboxAccountData);
94}
95
102static int init_mailbox(struct Mailbox *m)
103{
104 if (!m || !m->account)
105 return -1;
106 if ((m->type != MUTT_MBOX) && (m->type != MUTT_MMDF))
107 return -1;
108 if (m->account->adata)
109 return 0;
110
113 return 0;
114}
115
121static struct MboxAccountData *mbox_adata_get(struct Mailbox *m)
122{
123 if (init_mailbox(m) == -1)
124 return NULL;
125 return m->account->adata;
126}
127
136static int mbox_lock_mailbox(struct Mailbox *m, bool excl, bool retry)
137{
139 if (!adata)
140 return -1;
141
142 int rc = mutt_file_lock(fileno(adata->fp), excl, retry);
143 if (rc == 0)
144 {
145 adata->locked = true;
146 }
147 else if (retry && !excl)
148 {
149 m->readonly = true;
150 return 0;
151 }
152
153 return rc;
154}
155
160static void mbox_unlock_mailbox(struct Mailbox *m)
161{
163 if (!adata)
164 return;
165
166 if (adata->locked)
167 {
168 fflush(adata->fp);
169
170 mutt_file_unlock(fileno(adata->fp));
171 adata->locked = false;
172 }
173}
174
181{
182 if (!m)
183 return MX_OPEN_ERROR;
184
186 if (!adata)
187 return MX_OPEN_ERROR;
188
189 char buf[8192] = { 0 };
190 char return_path[1024] = { 0 };
191 int count = 0;
192 int lines;
193 time_t t = 0;
194 LOFF_T loc, tmploc;
195 struct Email *e = NULL;
196 struct stat st = { 0 };
197 struct Progress *progress = NULL;
199
200 if (stat(mailbox_path(m), &st) == -1)
201 {
202 mutt_perror("%s", mailbox_path(m));
203 goto fail;
204 }
207 m->size = st.st_size;
208
209 buf[sizeof(buf) - 1] = '\0';
210
211 if (m->verbose)
212 {
213 progress = progress_new(MUTT_PROGRESS_READ, 0);
214 progress_set_message(progress, _("Reading %s..."), mailbox_path(m));
215 }
216
217 while (true)
218 {
219 if (!fgets(buf, sizeof(buf) - 1, adata->fp))
220 break;
221
222 if (SigInt)
223 break;
224
225 if (mutt_str_equal(buf, MMDF_SEP))
226 {
227 loc = ftello(adata->fp);
228 if (loc < 0)
229 goto fail;
230
231 count++;
232 progress_update(progress, count, (int) (loc / (m->size / 100 + 1)));
233
235 e = email_new();
236 m->emails[m->msg_count] = e;
237 e->offset = loc;
238 e->index = m->msg_count;
239
240 if (!fgets(buf, sizeof(buf) - 1, adata->fp))
241 {
242 /* TODO: memory leak??? */
243 mutt_debug(LL_DEBUG1, "unexpected EOF\n");
244 break;
245 }
246
247 return_path[0] = '\0';
248
249 if (!is_from(buf, return_path, sizeof(return_path), &t))
250 {
251 if (!mutt_file_seek(adata->fp, loc, SEEK_SET))
252 {
253 mutt_error(_("Mailbox is corrupt"));
254 goto fail;
255 }
256 }
257 else
258 {
259 e->received = t - mutt_date_local_tz(t);
260 }
261
262 e->env = mutt_rfc822_read_header(adata->fp, e, false, false);
263
264 loc = ftello(adata->fp);
265 if (loc < 0)
266 goto fail;
267
268 if ((e->body->length > 0) && (e->lines > 0))
269 {
270 tmploc = loc + e->body->length;
271
272 if ((tmploc > 0) && (tmploc < m->size))
273 {
274 if (!mutt_file_seek(adata->fp, tmploc, SEEK_SET) ||
275 !fgets(buf, sizeof(buf) - 1, adata->fp) || !mutt_str_equal(MMDF_SEP, buf))
276 {
277 (void) mutt_file_seek(adata->fp, loc, SEEK_SET);
278 e->body->length = -1;
279 }
280 }
281 else
282 {
283 e->body->length = -1;
284 }
285 }
286 else
287 {
288 e->body->length = -1;
289 }
290
291 if (e->body->length < 0)
292 {
293 lines = -1;
294 do
295 {
296 loc = ftello(adata->fp);
297 if (loc < 0)
298 goto fail;
299 if (!fgets(buf, sizeof(buf) - 1, adata->fp))
300 break;
301 lines++;
302 } while (!mutt_str_equal(buf, MMDF_SEP));
303
304 e->lines = lines;
305 e->body->length = loc - e->body->offset;
306 }
307
308 if (TAILQ_EMPTY(&e->env->return_path) && return_path[0])
309 mutt_addrlist_parse(&e->env->return_path, return_path);
310
311 if (TAILQ_EMPTY(&e->env->from))
312 mutt_addrlist_copy(&e->env->from, &e->env->return_path, false);
313
314 m->msg_count++;
315 }
316 else
317 {
318 mutt_debug(LL_DEBUG1, "corrupt mailbox\n");
319 mutt_error(_("Mailbox is corrupt"));
320 goto fail;
321 }
322 }
323
324 if (SigInt)
325 {
326 SigInt = false;
327 rc = MX_OPEN_ABORT; /* action aborted */
328 goto fail;
329 }
330
331 rc = MX_OPEN_OK;
332fail:
333 progress_free(&progress);
334 return rc;
335}
336
349{
350 if (!m)
351 return MX_OPEN_ERROR;
352
354 if (!adata)
355 return MX_OPEN_ERROR;
356
357 struct stat st = { 0 };
358 char buf[8192] = { 0 };
359 char return_path[256] = { 0 };
360 struct Email *e_cur = NULL;
361 time_t t = 0;
362 int count = 0, lines = 0;
363 LOFF_T loc;
364 struct Progress *progress = NULL;
366
367 /* Save information about the folder at the time we opened it. */
368 if (stat(mailbox_path(m), &st) == -1)
369 {
370 mutt_perror("%s", mailbox_path(m));
371 goto fail;
372 }
373
374 m->size = st.st_size;
377
378 if (!m->readonly)
379 m->readonly = access(mailbox_path(m), W_OK) ? true : false;
380
381 if (m->verbose)
382 {
383 progress = progress_new(MUTT_PROGRESS_READ, 0);
384 progress_set_message(progress, _("Reading %s..."), mailbox_path(m));
385 }
386
387 loc = ftello(adata->fp);
388 if (loc < 0)
389 {
390 mutt_debug(LL_DEBUG1, "ftello: %s (errno %d)\n", strerror(errno), errno);
391 loc = 0;
392 }
393
394 while ((fgets(buf, sizeof(buf), adata->fp)) && !SigInt)
395 {
396 if (is_from(buf, return_path, sizeof(return_path), &t))
397 {
398 /* Save the Content-Length of the previous message */
399 if (count > 0)
400 {
401 struct Email *e = m->emails[m->msg_count - 1];
402 if (e->body->length < 0)
403 {
404 e->body->length = loc - e->body->offset - 1;
405 if (e->body->length < 0)
406 e->body->length = 0;
407 }
408 if (e->lines == 0)
409 e->lines = lines ? lines - 1 : 0;
410 }
411
412 count++;
413
414 progress_update(progress, count, (int) (ftello(adata->fp) / (m->size / 100 + 1)));
415
417
418 m->emails[m->msg_count] = email_new();
419 e_cur = m->emails[m->msg_count];
420 e_cur->received = t - mutt_date_local_tz(t);
421 e_cur->offset = loc;
422 e_cur->index = m->msg_count;
423
424 e_cur->env = mutt_rfc822_read_header(adata->fp, e_cur, false, false);
425
426 /* if we know how long this message is, either just skip over the body,
427 * or if we don't know how many lines there are, count them now (this will
428 * save time by not having to search for the next message marker). */
429 if (e_cur->body->length > 0)
430 {
431 LOFF_T tmploc;
432
433 loc = ftello(adata->fp);
434 if (loc < 0)
435 {
436 mutt_debug(LL_DEBUG1, "ftello: %s (errno %d)\n", strerror(errno), errno);
437 loc = 0;
438 }
439
440 /* The test below avoids a potential integer overflow if the
441 * content-length is huge (thus necessarily invalid). */
442 tmploc = (e_cur->body->length < m->size) ? (loc + e_cur->body->length + 1) : -1;
443
444 if ((tmploc > 0) && (tmploc < m->size))
445 {
446 /* check to see if the content-length looks valid. we expect to
447 * to see a valid message separator at this point in the stream */
448 if (!mutt_file_seek(adata->fp, tmploc, SEEK_SET) ||
449 !fgets(buf, sizeof(buf), adata->fp) || !mutt_str_startswith(buf, "From "))
450 {
451 mutt_debug(LL_DEBUG1, "bad content-length in message %d (cl=" OFF_T_FMT ")\n",
452 e_cur->index, e_cur->body->length);
453 mutt_debug(LL_DEBUG1, " LINE: %s", buf);
454 /* nope, return the previous position */
455 (void) mutt_file_seek(adata->fp, loc, SEEK_SET);
456 e_cur->body->length = -1;
457 }
458 }
459 else if (tmploc != m->size)
460 {
461 /* content-length would put us past the end of the file, so it
462 * must be wrong */
463 e_cur->body->length = -1;
464 }
465
466 if (e_cur->body->length != -1)
467 {
468 /* good content-length. check to see if we know how many lines
469 * are in this message. */
470 if (e_cur->lines == 0)
471 {
472 int cl = e_cur->body->length;
473
474 /* count the number of lines in this message */
475 (void) mutt_file_seek(adata->fp, loc, SEEK_SET);
476 while (cl-- > 0)
477 {
478 if (fgetc(adata->fp) == '\n')
479 e_cur->lines++;
480 }
481 }
482
483 /* return to the offset of the next message separator */
484 (void) mutt_file_seek(adata->fp, tmploc, SEEK_SET);
485 }
486 }
487
488 m->msg_count++;
489
490 if (TAILQ_EMPTY(&e_cur->env->return_path) && return_path[0])
491 {
492 mutt_addrlist_parse(&e_cur->env->return_path, return_path);
493 }
494
495 if (TAILQ_EMPTY(&e_cur->env->from))
496 mutt_addrlist_copy(&e_cur->env->from, &e_cur->env->return_path, false);
497
498 lines = 0;
499 }
500 else
501 {
502 lines++;
503 }
504
505 loc = ftello(adata->fp);
506 }
507
508 /* Only set the content-length of the previous message if we have read more
509 * than one message during _this_ invocation. If this routine is called
510 * when new mail is received, we need to make sure not to clobber what
511 * previously was the last message since the headers may be sorted. */
512 if (count > 0)
513 {
514 struct Email *e = m->emails[m->msg_count - 1];
515 if (e->body->length < 0)
516 {
517 e->body->length = ftello(adata->fp) - e->body->offset - 1;
518 if (e->body->length < 0)
519 e->body->length = 0;
520 }
521
522 if (e->lines == 0)
523 e->lines = lines ? lines - 1 : 0;
524 }
525
526 if (SigInt)
527 {
528 SigInt = false;
529 rc = MX_OPEN_ABORT;
530 goto fail; /* action aborted */
531 }
532
533 rc = MX_OPEN_OK;
534fail:
535 progress_free(&progress);
536 return rc;
537}
538
545static int reopen_mailbox(struct Mailbox *m)
546{
547 if (!m)
548 return -1;
549
551 if (!adata)
552 return -1;
553
554 bool (*cmp_headers)(const struct Email *, const struct Email *) = NULL;
555 struct Email **e_old = NULL;
556 int old_msg_count;
557 bool msg_mod = false;
558 int rc = -1;
559
560 /* silent operations */
561 m->verbose = false;
562
563 /* our heuristics require the old mailbox to be unsorted */
564 const enum EmailSortType c_sort = cs_subset_sort(NeoMutt->sub, "sort");
565 if (c_sort != EMAIL_SORT_UNSORTED)
566 {
569 cs_subset_str_native_set(NeoMutt->sub, "sort", c_sort, NULL);
570 }
571
572 e_old = NULL;
573 old_msg_count = 0;
574
575 /* simulate a close */
579 FREE(&m->v2r);
580 if (m->readonly)
581 {
582 for (int i = 0; i < m->msg_count; i++)
583 email_free(&(m->emails[i])); /* nothing to do! */
584 FREE(&m->emails);
585 }
586 else
587 {
588 /* save the old headers */
589 old_msg_count = m->msg_count;
590 e_old = m->emails;
591 m->emails = NULL;
592 }
593
594 m->email_max = 0; /* force allocation of new headers */
595 m->msg_count = 0;
596 m->vcount = 0;
597 m->msg_tagged = 0;
598 m->msg_deleted = 0;
599 m->msg_new = 0;
600 m->msg_unread = 0;
601 m->msg_flagged = 0;
602 m->changed = false;
603 m->id_hash = NULL;
604 m->subj_hash = NULL;
606
607 switch (m->type)
608 {
609 case MUTT_MBOX:
610 case MUTT_MMDF:
611 cmp_headers = email_cmp_strict;
612 mutt_file_fclose(&adata->fp);
613 adata->fp = mutt_file_fopen(mailbox_path(m), "r");
614 if (!adata->fp)
615 rc = -1;
616 else if (m->type == MUTT_MBOX)
617 rc = mbox_parse_mailbox(m);
618 else
619 rc = mmdf_parse_mailbox(m);
620 break;
621
622 default:
623 rc = -1;
624 break;
625 }
626
627 if (rc == -1)
628 {
629 /* free the old headers */
630 for (int i = 0; i < old_msg_count; i++)
631 email_free(&(e_old[i]));
632 FREE(&e_old);
633
634 m->verbose = true;
635 return -1;
636 }
637
638 mutt_file_touch_atime(fileno(adata->fp));
639
640 /* now try to recover the old flags */
641
642 if (!m->readonly)
643 {
644 for (int i = 0; i < m->msg_count; i++)
645 {
646 bool found = false;
647
648 /* some messages have been deleted, and new messages have been
649 * appended at the end; the heuristic is that old messages have then
650 * "advanced" towards the beginning of the folder, so we begin the
651 * search at index "i" */
652 int j;
653 for (j = i; j < old_msg_count; j++)
654 {
655 if (!e_old[j])
656 continue;
657 if (cmp_headers(m->emails[i], e_old[j]))
658 {
659 found = true;
660 break;
661 }
662 }
663 if (!found)
664 {
665 for (j = 0; (j < i) && (j < old_msg_count); j++)
666 {
667 if (!e_old[j])
668 continue;
669 if (cmp_headers(m->emails[i], e_old[j]))
670 {
671 found = true;
672 break;
673 }
674 }
675 }
676
677 if (found)
678 {
679 m->changed = true;
680 if (e_old[j]->changed)
681 {
682 /* Only update the flags if the old header was changed;
683 * otherwise, the header may have been modified externally,
684 * and we don't want to lose _those_ changes */
685 mutt_set_flag(m, m->emails[i], MUTT_FLAG, e_old[j]->flagged, true);
686 mutt_set_flag(m, m->emails[i], MUTT_REPLIED, e_old[j]->replied, true);
687 mutt_set_flag(m, m->emails[i], MUTT_OLD, e_old[j]->old, true);
688 mutt_set_flag(m, m->emails[i], MUTT_READ, e_old[j]->read, true);
689 }
690 mutt_set_flag(m, m->emails[i], MUTT_DELETE, e_old[j]->deleted, true);
691 mutt_set_flag(m, m->emails[i], MUTT_PURGE, e_old[j]->purge, true);
692 mutt_set_flag(m, m->emails[i], MUTT_TAG, e_old[j]->tagged, true);
693
694 /* we don't need this header any more */
695 email_free(&(e_old[j]));
696 }
697 }
698
699 /* free the remaining old emails */
700 for (int j = 0; j < old_msg_count; j++)
701 {
702 if (e_old[j])
703 {
704 email_free(&(e_old[j]));
705 msg_mod = true;
706 }
707 }
708 FREE(&e_old);
709 }
710
712 m->verbose = true;
713
714 return (m->changed || msg_mod) ? MX_STATUS_REOPENED : MX_STATUS_NEW_MAIL;
715}
716
723static bool mbox_has_new(struct Mailbox *m)
724{
725 for (int i = 0; i < m->msg_count; i++)
726 {
727 struct Email *e = m->emails[i];
728 if (!e)
729 break;
730 if (!e->deleted && !e->read && !e->old)
731 return true;
732 }
733 return false;
734}
735
744void mbox_reset_atime(struct Mailbox *m, struct stat *st)
745{
746 struct stat st2 = { 0 };
747 if (!st)
748 {
749 if (stat(mailbox_path(m), &st2) < 0)
750 return;
751 st = &st2;
752 }
753
754 struct utimbuf utimebuf = { 0 };
755 utimebuf.actime = st->st_atime;
756 utimebuf.modtime = st->st_mtime;
757
758 /* When $mbox_check_recent is set, existing new mail is ignored, so do not
759 * reset the atime to mtime-1 to signal new mail. */
760 const bool c_mail_check_recent = cs_subset_bool(NeoMutt->sub, "mail_check_recent");
761 if (!c_mail_check_recent && (utimebuf.actime >= utimebuf.modtime) && mbox_has_new(m))
762 {
763 utimebuf.actime = utimebuf.modtime - 1;
764 }
765
766 utime(mailbox_path(m), &utimebuf);
767}
768
772static bool mbox_ac_owns_path(struct Account *a, const char *path)
773{
774 if ((a->type != MUTT_MBOX) && (a->type != MUTT_MMDF))
775 return false;
776
777 struct Mailbox **mp = ARRAY_FIRST(&a->mailboxes);
778 if (!mp)
779 return false;
780
781 return mutt_str_equal(mailbox_path(*mp), path);
782}
783
787static bool mbox_ac_add(struct Account *a, struct Mailbox *m)
788{
789 return true;
790}
791
799static FILE *mbox_open_readwrite(struct Mailbox *m)
800{
801 FILE *fp = mutt_file_fopen(mailbox_path(m), "r+");
802 if (fp)
803 m->readonly = false;
804 return fp;
805}
806
814static FILE *mbox_open_readonly(struct Mailbox *m)
815{
816 FILE *fp = mutt_file_fopen(mailbox_path(m), "r");
817 if (fp)
818 m->readonly = true;
819 return fp;
820}
821
826{
827 if (init_mailbox(m) != 0)
828 return MX_OPEN_ERROR;
829
831 if (!adata)
832 return MX_OPEN_ERROR;
833
834 adata->fp = m->readonly ? NULL : mbox_open_readwrite(m);
835 if (!adata->fp)
836 {
837 adata->fp = mbox_open_readonly(m);
838 }
839 if (!adata->fp)
840 {
841 mutt_perror("%s", mailbox_path(m));
842 return MX_OPEN_ERROR;
843 }
844
846 if (mbox_lock_mailbox(m, false, true) == -1)
847 {
849 return MX_OPEN_ERROR;
850 }
851
852 m->has_new = true;
854 if (m->type == MUTT_MBOX)
855 rc = mbox_parse_mailbox(m);
856 else if (m->type == MUTT_MMDF)
857 rc = mmdf_parse_mailbox(m);
858 else
859 rc = MX_OPEN_ERROR;
860
861 if (!mbox_has_new(m))
862 m->has_new = false;
863 clearerr(adata->fp); // Clear the EOF flag
864 mutt_file_touch_atime(fileno(adata->fp));
865
868 return rc;
869}
870
874static bool mbox_mbox_open_append(struct Mailbox *m, OpenMailboxFlags flags)
875{
876 if (init_mailbox(m) != 0)
877 return false;
878
880 if (!adata)
881 return false;
882
883 if (!adata->fp)
884 {
885 // create dir recursively
886 char *tmp_path = mutt_path_dirname(mailbox_path(m));
887 if (mutt_file_mkdir(tmp_path, S_IRWXU) == -1)
888 {
889 mutt_perror("%s", mailbox_path(m));
890 FREE(&tmp_path);
891 return false;
892 }
893 FREE(&tmp_path);
894
895 adata->fp = mutt_file_fopen(mailbox_path(m), "a+");
896 if (!adata->fp)
897 {
898 mutt_perror("%s", mailbox_path(m));
899 return false;
900 }
901
902 if (mbox_lock_mailbox(m, true, true) != false)
903 {
904 mutt_error(_("Couldn't lock %s"), mailbox_path(m));
906 return false;
907 }
908 }
909
910 if (!mutt_file_seek(adata->fp, 0, SEEK_END))
911 {
913 return false;
914 }
915
916 return true;
917}
918
926static enum MxStatus mbox_mbox_check(struct Mailbox *m)
927{
929 if (!adata)
930 return MX_STATUS_ERROR;
931
932 if (!adata->fp)
933 {
934 if (mbox_mbox_open(m) != MX_OPEN_OK)
935 return MX_STATUS_ERROR;
937 }
938 if (!adata->fp)
939 return MX_STATUS_ERROR;
940
941 struct stat st = { 0 };
942 bool unlock = false;
943 bool modified = false;
944
945 if (stat(mailbox_path(m), &st) == 0)
946 {
947 if ((mutt_file_stat_timespec_compare(&st, MUTT_STAT_MTIME, &adata->mtime) == 0) &&
948 (st.st_size == m->size))
949 {
950 return MX_STATUS_OK;
951 }
952
953 if (st.st_size == m->size)
954 {
955 /* the file was touched, but it is still the same length, so just exit */
957 return MX_STATUS_OK;
958 }
959
960 if (st.st_size > m->size)
961 {
962 /* lock the file if it isn't already */
963 if (!adata->locked)
964 {
966 if (mbox_lock_mailbox(m, false, false) == -1)
967 {
969 /* we couldn't lock the mailbox, but nothing serious happened:
970 * probably the new mail arrived: no reason to wait till we can
971 * parse it: we'll get it on the next pass */
972 return MX_STATUS_LOCKED;
973 }
974 unlock = 1;
975 }
976
977 /* Check to make sure that the only change to the mailbox is that
978 * message(s) were appended to this file. My heuristic is that we should
979 * see the message separator at *exactly* what used to be the end of the
980 * folder. */
981 char buf[1024] = { 0 };
982 if (!mutt_file_seek(adata->fp, m->size, SEEK_SET))
983 {
984 goto error;
985 }
986 if (fgets(buf, sizeof(buf), adata->fp))
987 {
988 if (((m->type == MUTT_MBOX) && mutt_str_startswith(buf, "From ")) ||
989 ((m->type == MUTT_MMDF) && mutt_str_equal(buf, MMDF_SEP)))
990 {
991 if (!mutt_file_seek(adata->fp, m->size, SEEK_SET))
992 {
993 goto error;
994 }
995
996 int old_msg_count = m->msg_count;
997 if (m->type == MUTT_MBOX)
999 else
1001
1002 if (m->msg_count > old_msg_count)
1004
1005 /* Only unlock the folder if it was locked inside of this routine.
1006 * It may have been locked elsewhere, like in
1007 * mutt_checkpoint_mailbox(). */
1008 if (unlock)
1009 {
1012 }
1013
1014 return MX_STATUS_NEW_MAIL; /* signal that new mail arrived */
1015 }
1016 else
1017 {
1018 modified = true;
1019 }
1020 }
1021 else
1022 {
1023 mutt_debug(LL_DEBUG1, "fgets returned NULL\n");
1024 modified = true;
1025 }
1026 }
1027 else
1028 {
1029 modified = true;
1030 }
1031 }
1032
1033 if (modified)
1034 {
1035 if (reopen_mailbox(m) != -1)
1036 {
1038 if (unlock)
1039 {
1042 }
1043 return MX_STATUS_REOPENED;
1044 }
1045 }
1046
1047 /* fatal error */
1048
1049error:
1051 mx_fastclose_mailbox(m, false);
1053 mutt_error(_("Mailbox was corrupted"));
1054 return MX_STATUS_ERROR;
1055}
1056
1060static enum MxStatus mbox_mbox_sync(struct Mailbox *m)
1061{
1063 if (!adata)
1064 return MX_STATUS_ERROR;
1065
1066 struct Buffer *tempfile = NULL;
1067 char buf[32] = { 0 };
1068 int j;
1069 bool unlink_tempfile = false;
1070 bool need_sort = false; /* flag to resort mailbox if new mail arrives */
1071 int first = -1; /* first message to be written */
1072 LOFF_T offset; /* location in mailbox to write changed messages */
1073 struct stat st = { 0 };
1074 struct MUpdate *new_offset = NULL;
1075 struct MUpdate *old_offset = NULL;
1076 FILE *fp = NULL;
1077 struct Progress *progress = NULL;
1078 enum MxStatus rc = MX_STATUS_ERROR;
1079
1080 /* sort message by their position in the mailbox on disk */
1081 const enum EmailSortType c_sort = cs_subset_sort(NeoMutt->sub, "sort");
1082 if (c_sort != EMAIL_SORT_UNSORTED)
1083 {
1085 need_sort = true;
1086 }
1087
1088 /* need to open the file for writing in such a way that it does not truncate
1089 * the file, so use read-write mode. */
1090 adata->fp = freopen(mailbox_path(m), "r+", adata->fp);
1091 if (!adata->fp)
1092 {
1093 mx_fastclose_mailbox(m, false);
1094 mutt_error(_("Fatal error! Could not reopen mailbox!"));
1095 goto fatal;
1096 }
1097
1099
1100 if (mbox_lock_mailbox(m, true, true) == -1)
1101 {
1103 mutt_error(_("Unable to lock mailbox"));
1104 goto bail;
1105 }
1106
1107 /* Check to make sure that the file hasn't changed on disk */
1108 enum MxStatus check = mbox_mbox_check(m);
1109 if ((check == MX_STATUS_NEW_MAIL) || (check == MX_STATUS_REOPENED))
1110 {
1111 /* new mail arrived, or mailbox reopened */
1112 rc = check;
1113 goto bail;
1114 }
1115 else if (check < 0)
1116 {
1117 goto fatal;
1118 }
1119
1120 /* Create a temporary file to write the new version of the mailbox in. */
1121 tempfile = buf_pool_get();
1122 buf_mktemp(tempfile);
1123 int fd = open(buf_string(tempfile), O_WRONLY | O_EXCL | O_CREAT, 0600);
1124 if ((fd == -1) || !(fp = fdopen(fd, "w")))
1125 {
1126 if (fd != -1)
1127 {
1128 close(fd);
1129 unlink_tempfile = true;
1130 }
1131 mutt_error(_("Could not create temporary file"));
1132 goto bail;
1133 }
1134 unlink_tempfile = true;
1135
1136 /* find the first deleted/changed message. we save a lot of time by only
1137 * rewriting the mailbox from the point where it has actually changed. */
1138 int i = 0;
1139 for (; (i < m->msg_count) && !m->emails[i]->deleted &&
1140 !m->emails[i]->changed && !m->emails[i]->attach_del;
1141 i++)
1142 {
1143 }
1144 if (i == m->msg_count)
1145 {
1146 /* this means m->changed or m->msg_deleted was set, but no
1147 * messages were found to be changed or deleted. This should
1148 * never happen, is we presume it is a bug in neomutt. */
1149 mutt_error(_("sync: mbox modified, but no modified messages (report this bug)"));
1150 mutt_debug(LL_DEBUG1, "no modified messages\n");
1151 goto bail;
1152 }
1153
1154 /* save the index of the first changed/deleted message */
1155 first = i;
1156 /* where to start overwriting */
1157 offset = m->emails[i]->offset;
1158
1159 /* the offset stored in the header does not include the MMDF_SEP, so make
1160 * sure we seek to the correct location */
1161 if (m->type == MUTT_MMDF)
1162 offset -= (sizeof(MMDF_SEP) - 1);
1163
1164 /* allocate space for the new offsets */
1165 new_offset = MUTT_MEM_CALLOC(m->msg_count - first, struct MUpdate);
1166 old_offset = MUTT_MEM_CALLOC(m->msg_count - first, struct MUpdate);
1167
1168 if (m->verbose)
1169 {
1171 progress_set_message(progress, _("Writing %s..."), mailbox_path(m));
1172 }
1173
1174 for (i = first, j = 0; i < m->msg_count; i++)
1175 {
1176 progress_update(progress, i, i / (m->msg_count / 100 + 1));
1177 /* back up some information which is needed to restore offsets when
1178 * something fails. */
1179
1180 old_offset[i - first].valid = true;
1181 old_offset[i - first].hdr = m->emails[i]->offset;
1182 old_offset[i - first].body = m->emails[i]->body->offset;
1183 old_offset[i - first].lines = m->emails[i]->lines;
1184 old_offset[i - first].length = m->emails[i]->body->length;
1185
1186 if (!m->emails[i]->deleted)
1187 {
1188 j++;
1189
1190 if (m->type == MUTT_MMDF)
1191 {
1192 if (fputs(MMDF_SEP, fp) == EOF)
1193 {
1194 mutt_perror("%s", buf_string(tempfile));
1195 goto bail;
1196 }
1197 }
1198
1199 /* save the new offset for this message. we add 'offset' because the
1200 * temporary file only contains saved message which are located after
1201 * 'offset' in the real mailbox */
1202 new_offset[i - first].hdr = ftello(fp) + offset;
1203
1204 struct Message *msg = mx_msg_open(m, m->emails[i]);
1205 const int rc2 = mutt_copy_message(fp, m->emails[i], msg, MUTT_CM_UPDATE,
1207 mx_msg_close(m, &msg);
1208 if (rc2 != 0)
1209 {
1210 mutt_perror("%s", buf_string(tempfile));
1211 goto bail;
1212 }
1213
1214 /* Since messages could have been deleted, the offsets stored in memory
1215 * will be wrong, so update what we can, which is the offset of this
1216 * message, and the offset of the body. If this is a multipart message,
1217 * we just flush the in memory cache so that the message will be reparsed
1218 * if the user accesses it later. */
1219 new_offset[i - first].body = ftello(fp) - m->emails[i]->body->length + offset;
1220 mutt_body_free(&m->emails[i]->body->parts);
1221
1222 if (m->type == MUTT_MMDF)
1223 {
1224 if (fputs(MMDF_SEP, fp) == EOF)
1225 {
1226 mutt_perror("%s", buf_string(tempfile));
1227 goto bail;
1228 }
1229 }
1230 else
1231 {
1232 if (fputs("\n", fp) == EOF)
1233 {
1234 mutt_perror("%s", buf_string(tempfile));
1235 goto bail;
1236 }
1237 }
1238 }
1239 }
1240
1241 if (mutt_file_fclose(&fp) != 0)
1242 {
1243 mutt_debug(LL_DEBUG1, "mutt_file_fclose (&) returned non-zero\n");
1244 mutt_perror("%s", buf_string(tempfile));
1245 goto bail;
1246 }
1247
1248 /* Save the state of this folder. */
1249 if (stat(mailbox_path(m), &st) == -1)
1250 {
1251 mutt_perror("%s", mailbox_path(m));
1252 goto bail;
1253 }
1254
1255 unlink_tempfile = false;
1256
1257 fp = mutt_file_fopen(buf_string(tempfile), "r");
1258 if (!fp)
1259 {
1261 mx_fastclose_mailbox(m, false);
1262 mutt_debug(LL_DEBUG1, "unable to reopen temp copy of mailbox!\n");
1263 mutt_perror("%s", buf_string(tempfile));
1264 FREE(&new_offset);
1265 FREE(&old_offset);
1266 goto fatal;
1267 }
1268
1269 if (!mutt_file_seek(adata->fp, offset, SEEK_SET) || /* seek the append location */
1270 /* do a sanity check to make sure the mailbox looks ok */
1271 !fgets(buf, sizeof(buf), adata->fp) ||
1272 ((m->type == MUTT_MBOX) && !mutt_str_startswith(buf, "From ")) ||
1273 ((m->type == MUTT_MMDF) && !mutt_str_equal(MMDF_SEP, buf)))
1274 {
1275 mutt_debug(LL_DEBUG1, "message not in expected position\n");
1276 mutt_debug(LL_DEBUG1, " LINE: %s\n", buf);
1277 i = -1;
1278 }
1279 else
1280 {
1281 if (!mutt_file_seek(adata->fp, offset, SEEK_SET)) /* return to proper offset */
1282 {
1283 i = -1;
1284 }
1285 else
1286 {
1287 /* copy the temp mailbox back into place starting at the first
1288 * change/deleted message */
1289 if (m->verbose)
1290 mutt_message(_("Committing changes..."));
1291 i = mutt_file_copy_stream(fp, adata->fp);
1292
1293 if (ferror(adata->fp))
1294 i = -1;
1295 }
1296 if (i >= 0)
1297 {
1298 m->size = ftello(adata->fp); /* update the mailbox->size of the mailbox */
1299 if ((m->size < 0) || (ftruncate(fileno(adata->fp), m->size) != 0))
1300 {
1301 i = -1;
1302 mutt_debug(LL_DEBUG1, "ftruncate() failed\n");
1303 }
1304 }
1305 }
1306
1309
1310 if ((mutt_file_fclose(&adata->fp) != 0) || (i == -1))
1311 {
1312 /* error occurred while writing the mailbox back, so keep the temp copy around */
1313
1314 struct Buffer *savefile = buf_pool_get();
1315
1316 const char *const c_tmp_dir = cs_subset_path(NeoMutt->sub, "tmp_dir");
1317 buf_printf(savefile, "%s/neomutt.%s-%s-%u", NONULL(c_tmp_dir),
1318 NONULL(NeoMutt->username), NONULL(ShortHostname), (unsigned int) getpid());
1319 rename(buf_string(tempfile), buf_string(savefile));
1321 mx_fastclose_mailbox(m, false);
1322 pretty_mailbox(savefile);
1323 mutt_error(_("Write failed! Saved partial mailbox to %s"), buf_string(savefile));
1324 buf_pool_release(&savefile);
1325 FREE(&new_offset);
1326 FREE(&old_offset);
1327 goto fatal;
1328 }
1329
1330 /* Restore the previous access/modification times */
1331 mbox_reset_atime(m, &st);
1332
1333 /* reopen the mailbox in read-only mode */
1334 adata->fp = mbox_open_readwrite(m);
1335 if (!adata->fp)
1336 {
1337 adata->fp = mbox_open_readonly(m);
1338 }
1339 if (!adata->fp)
1340 {
1341 unlink(buf_string(tempfile));
1343 mx_fastclose_mailbox(m, false);
1344 mutt_error(_("Fatal error! Could not reopen mailbox!"));
1345 FREE(&new_offset);
1346 FREE(&old_offset);
1347 goto fatal;
1348 }
1349
1350 /* update the offsets of the rewritten messages */
1351 for (i = first, j = first; i < m->msg_count; i++)
1352 {
1353 if (!m->emails[i]->deleted)
1354 {
1355 m->emails[i]->offset = new_offset[i - first].hdr;
1356 m->emails[i]->body->hdr_offset = new_offset[i - first].hdr;
1357 m->emails[i]->body->offset = new_offset[i - first].body;
1358 m->emails[i]->index = j++;
1359 }
1360 }
1361 FREE(&new_offset);
1362 FREE(&old_offset);
1363 unlink(buf_string(tempfile)); /* remove partial copy of the mailbox */
1364 buf_pool_release(&tempfile);
1366
1367 const bool c_check_mbox_size = cs_subset_bool(NeoMutt->sub, "check_mbox_size");
1368 if (c_check_mbox_size)
1369 {
1370 struct Mailbox *m_tmp = mailbox_find(mailbox_path(m));
1371 if (m_tmp && !m_tmp->has_new)
1372 mailbox_update(m_tmp);
1373 }
1374
1375 progress_free(&progress);
1376 return 0; /* signal success */
1377
1378bail: /* Come here in case of disaster */
1379
1380 mutt_file_fclose(&fp);
1381
1382 if (tempfile && unlink_tempfile)
1383 unlink(buf_string(tempfile));
1384
1385 /* restore offsets, as far as they are valid */
1386 if ((first >= 0) && old_offset)
1387 {
1388 for (i = first; (i < m->msg_count) && old_offset[i - first].valid; i++)
1389 {
1390 m->emails[i]->offset = old_offset[i - first].hdr;
1391 m->emails[i]->body->hdr_offset = old_offset[i - first].hdr;
1392 m->emails[i]->body->offset = old_offset[i - first].body;
1393 m->emails[i]->lines = old_offset[i - first].lines;
1394 m->emails[i]->body->length = old_offset[i - first].length;
1395 }
1396 }
1397
1398 /* this is ok to call even if we haven't locked anything */
1400
1402 FREE(&new_offset);
1403 FREE(&old_offset);
1404
1405 adata->fp = freopen(mailbox_path(m), "r", adata->fp);
1406 if (!adata->fp)
1407 {
1408 mutt_error(_("Could not reopen mailbox"));
1409 mx_fastclose_mailbox(m, false);
1410 goto fatal;
1411 }
1412
1414 if (need_sort)
1415 {
1416 /* if the mailbox was reopened, the thread tree will be invalid so make
1417 * sure to start threading from scratch. */
1419 }
1420
1421fatal:
1422 buf_pool_release(&tempfile);
1423 progress_free(&progress);
1424 return rc;
1425}
1426
1430static enum MxStatus mbox_mbox_close(struct Mailbox *m)
1431{
1433 if (!adata)
1434 return MX_STATUS_ERROR;
1435
1436 if (!adata->fp)
1437 return MX_STATUS_OK;
1438
1439 if (adata->append)
1440 {
1441 mutt_file_unlock(fileno(adata->fp));
1443 }
1444
1445 mutt_file_fclose(&adata->fp);
1446
1447 /* fix up the times so mailbox won't get confused */
1448 if (m->peekonly && !buf_is_empty(&m->pathbuf) &&
1449 (mutt_file_timespec_compare(&adata->mtime, &adata->atime) > 0))
1450 {
1451#ifdef HAVE_UTIMENSAT
1452 struct timespec ts[2] = { { 0 }, { 0 } };
1453 ts[0] = adata->atime;
1454 ts[1] = adata->mtime;
1455 utimensat(AT_FDCWD, m->path, ts, 0);
1456#else
1457 struct utimbuf ut = { 0 };
1458 ut.actime = adata->atime.tv_sec;
1459 ut.modtime = adata->mtime.tv_sec;
1460 utime(mailbox_path(m), &ut);
1461#endif
1462 }
1463
1464 return MX_STATUS_OK;
1465}
1466
1470static bool mbox_msg_open(struct Mailbox *m, struct Message *msg, struct Email *e)
1471{
1473 if (!adata)
1474 return false;
1475
1476 msg->fp = mutt_file_fopen(mailbox_path(m), "r");
1477 if (!msg->fp)
1478 return false;
1479
1480 return true;
1481}
1482
1486static bool mbox_msg_open_new(struct Mailbox *m, struct Message *msg, const struct Email *e)
1487{
1489 if (!adata)
1490 return false;
1491
1492 msg->fp = adata->fp;
1493 return true;
1494}
1495
1499static int mbox_msg_commit(struct Mailbox *m, struct Message *msg)
1500{
1501 if (fputc('\n', msg->fp) == EOF)
1502 return -1;
1503
1504 if ((fflush(msg->fp) == EOF) || (fsync(fileno(msg->fp)) == -1))
1505 {
1506 mutt_perror(_("Can't write message"));
1507 return -1;
1508 }
1509
1510 return 0;
1511}
1512
1516static int mbox_msg_close(struct Mailbox *m, struct Message *msg)
1517{
1518 if (msg->write)
1519 msg->fp = NULL;
1520 else
1521 mutt_file_fclose(&msg->fp);
1522
1523 return 0;
1524}
1525
1531static int mbox_msg_padding_size(struct Mailbox *m)
1532{
1533 return 1;
1534}
1535
1539enum MailboxType mbox_path_probe(const char *path, const struct stat *st)
1540{
1541 if (!st)
1542 return MUTT_UNKNOWN;
1543
1544 if (S_ISDIR(st->st_mode))
1545 return MUTT_UNKNOWN;
1546
1547 if (st->st_size == 0)
1548 return MUTT_MBOX;
1549
1550 FILE *fp = mutt_file_fopen(path, "r");
1551 if (!fp)
1552 return MUTT_UNKNOWN;
1553
1554 int ch;
1555 while ((ch = fgetc(fp)) != EOF)
1556 {
1557 /* Some mailbox creation tools erroneously append a blank line to
1558 * a file before appending a mail message. This allows neomutt to
1559 * detect type for and thus open those files. */
1560 if ((ch != '\n') && (ch != '\r'))
1561 {
1562 ungetc(ch, fp);
1563 break;
1564 }
1565 }
1566
1568 char tmp[256] = { 0 };
1569 if (fgets(tmp, sizeof(tmp), fp))
1570 {
1571 if (mutt_str_startswith(tmp, "From "))
1572 type = MUTT_MBOX;
1573 else if (mutt_str_equal(tmp, MMDF_SEP))
1574 type = MUTT_MMDF;
1575 }
1577
1578 const bool c_check_mbox_size = cs_subset_bool(NeoMutt->sub, "check_mbox_size");
1579 if (!c_check_mbox_size)
1580 {
1581 /* need to restore the times here, the file was not really accessed,
1582 * only the type was accessed. This is important, because detection
1583 * of "new mail" depends on those times set correctly. */
1584#ifdef HAVE_UTIMENSAT
1585 struct timespec ts[2] = { { 0 }, { 0 } };
1588 utimensat(AT_FDCWD, path, ts, 0);
1589#else
1590 struct utimbuf times = { 0 };
1591 times.actime = st->st_atime;
1592 times.modtime = st->st_mtime;
1593 utime(path, &times);
1594#endif
1595 }
1596
1597 return type;
1598}
1599
1603static int mbox_path_canon(struct Buffer *path)
1604{
1605 mutt_path_canon(path, NeoMutt->home_dir, false);
1606 return 0;
1607}
1608
1612static int mbox_path_is_empty(struct Buffer *path)
1613{
1614 return mutt_file_check_empty(buf_string(path));
1615}
1616
1620static int mmdf_msg_commit(struct Mailbox *m, struct Message *msg)
1621{
1622 if (fputs(MMDF_SEP, msg->fp) == EOF)
1623 return -1;
1624
1625 if ((fflush(msg->fp) == EOF) || (fsync(fileno(msg->fp)) == -1))
1626 {
1627 mutt_perror(_("Can't write message"));
1628 return -1;
1629 }
1630
1631 return 0;
1632}
1633
1639static int mmdf_msg_padding_size(struct Mailbox *m)
1640{
1641 return 10;
1642}
1643
1647static enum MxStatus mbox_mbox_check_stats(struct Mailbox *m, uint8_t flags)
1648{
1649 struct stat st = { 0 };
1650 if (stat(mailbox_path(m), &st) != 0)
1651 return MX_STATUS_ERROR;
1652
1653 bool new_or_changed;
1654
1655 const bool c_check_mbox_size = cs_subset_bool(NeoMutt->sub, "check_mbox_size");
1656 if (c_check_mbox_size)
1657 {
1658 new_or_changed = (st.st_size > m->size);
1659 }
1660 else
1661 {
1662 new_or_changed =
1664 (m->newly_created &&
1667 }
1668
1669 if (new_or_changed)
1670 {
1671 const bool c_mail_check_recent = cs_subset_bool(NeoMutt->sub, "mail_check_recent");
1672 if (!c_mail_check_recent ||
1674 {
1675 m->has_new = true;
1676 }
1677 }
1678 else if (c_check_mbox_size)
1679 {
1680 /* some other program has deleted mail from the folder */
1681 m->size = (off_t) st.st_size;
1682 }
1683
1684 if (m->newly_created && ((st.st_ctime != st.st_mtime) || (st.st_ctime != st.st_atime)))
1685 m->newly_created = false;
1686
1687 if (flags & MUTT_MAILBOX_CHECK_STATS)
1688 {
1691 &adata->stats_last_checked) > 0)
1692 {
1693 bool old_peek = m->peekonly;
1695 mx_mbox_close(m);
1696 m->peekonly = old_peek;
1697 mutt_time_now(&adata->stats_last_checked);
1698 }
1699 }
1700
1701 if (m->has_new || m->msg_new)
1702 return MX_STATUS_NEW_MAIL;
1703 return MX_STATUS_OK;
1704}
1705
1709const struct MxOps MxMboxOps = {
1710 // clang-format off
1711 .type = MUTT_MBOX,
1712 .name = "mbox",
1713 .is_local = true,
1714 .ac_owns_path = mbox_ac_owns_path,
1715 .ac_add = mbox_ac_add,
1716 .mbox_open = mbox_mbox_open,
1717 .mbox_open_append = mbox_mbox_open_append,
1718 .mbox_check = mbox_mbox_check,
1719 .mbox_check_stats = mbox_mbox_check_stats,
1720 .mbox_sync = mbox_mbox_sync,
1721 .mbox_close = mbox_mbox_close,
1722 .msg_open = mbox_msg_open,
1723 .msg_open_new = mbox_msg_open_new,
1724 .msg_commit = mbox_msg_commit,
1725 .msg_close = mbox_msg_close,
1726 .msg_padding_size = mbox_msg_padding_size,
1727 .msg_save_hcache = NULL,
1728 .tags_edit = NULL,
1729 .tags_commit = NULL,
1730 .path_probe = mbox_path_probe,
1731 .path_canon = mbox_path_canon,
1732 .path_is_empty = mbox_path_is_empty,
1733 // clang-format on
1734};
1735
1739const struct MxOps MxMmdfOps = {
1740 // clang-format off
1741 .type = MUTT_MMDF,
1742 .name = "mmdf",
1743 .is_local = true,
1744 .ac_owns_path = mbox_ac_owns_path,
1745 .ac_add = mbox_ac_add,
1746 .mbox_open = mbox_mbox_open,
1747 .mbox_open_append = mbox_mbox_open_append,
1748 .mbox_check = mbox_mbox_check,
1749 .mbox_check_stats = mbox_mbox_check_stats,
1750 .mbox_sync = mbox_mbox_sync,
1751 .mbox_close = mbox_mbox_close,
1752 .msg_open = mbox_msg_open,
1753 .msg_open_new = mbox_msg_open_new,
1754 .msg_commit = mmdf_msg_commit,
1755 .msg_close = mbox_msg_close,
1756 .msg_padding_size = mmdf_msg_padding_size,
1757 .msg_save_hcache = NULL,
1758 .tags_edit = NULL,
1759 .tags_commit = NULL,
1760 .path_probe = mbox_path_probe,
1761 .path_canon = mbox_path_canon,
1762 .path_is_empty = mbox_path_is_empty,
1763 // clang-format on
1764};
void mutt_addrlist_copy(struct AddressList *dst, const struct AddressList *src, bool prune)
Copy a list of addresses into another list.
Definition address.c:765
int mutt_addrlist_parse(struct AddressList *al, const char *s)
Parse a list of email addresses.
Definition address.c:480
Email Address Handling.
#define ARRAY_FIRST(head)
Convenience method to get the first element.
Definition array.h:136
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition buffer.c:161
bool buf_is_empty(const struct Buffer *buf)
Is the Buffer empty?
Definition buffer.c:291
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition buffer.h:96
const char * cs_subset_path(const struct ConfigSubset *sub, const char *name)
Get a path config item by name.
Definition helpers.c:168
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition helpers.c:47
short cs_subset_sort(const struct ConfigSubset *sub, const char *name)
Get a sort config item by name.
Definition helpers.c:266
Convenience wrapper for the config headers.
int mutt_copy_message(FILE *fp_out, struct Email *e, struct Message *msg, CopyMessageFlags cmflags, CopyHeaderFlags chflags, int wraplen)
Copy a message from a Mailbox.
Definition copy_email.c:914
#define MUTT_CM_UPDATE
Update structs on sync.
Definition copy_email.h:42
#define CH_UPDATE
Update the status and x-status fields?
Definition copy_email.h:56
#define CH_FROM
Retain the "From " message separator?
Definition copy_email.h:60
#define CH_UPDATE_LEN
Update Lines: and Content-Length:
Definition copy_email.h:66
Convenience wrapper for the core headers.
void mailbox_update(struct Mailbox *m)
Get the mailbox's current size.
Definition mailbox.c:214
void mailbox_changed(struct Mailbox *m, enum NotifyMailbox action)
Notify observers of a change to a Mailbox.
Definition mailbox.c:232
struct Mailbox * mailbox_find(const char *path)
Find the mailbox with a given path.
Definition mailbox.c:151
@ NT_MAILBOX_RESORT
Email list needs resorting.
Definition mailbox.h:180
@ NT_MAILBOX_INVALID
Email list was changed.
Definition mailbox.h:179
@ NT_MAILBOX_UPDATE
Update internal tables.
Definition mailbox.h:181
static const char * mailbox_path(const struct Mailbox *m)
Get the Mailbox's path string.
Definition mailbox.h:213
MailboxType
Supported mailbox formats.
Definition mailbox.h:40
@ MUTT_MMDF
'mmdf' Mailbox type
Definition mailbox.h:45
@ MUTT_MBOX
'mbox' Mailbox type
Definition mailbox.h:44
@ MUTT_UNKNOWN
Mailbox wasn't recognised.
Definition mailbox.h:43
void mutt_body_free(struct Body **ptr)
Free a Body.
Definition body.c:58
bool email_cmp_strict(const struct Email *e1, const struct Email *e2)
Strictly compare message emails.
Definition email.c:96
struct Email * email_new(void)
Create a new Email.
Definition email.c:77
void email_free(struct Email **ptr)
Free an Email.
Definition email.c:46
void mutt_make_label_hash(struct Mailbox *m)
Create a Hash Table to store the labels.
Definition header.c:405
Structs that make up an email.
struct Envelope * mutt_rfc822_read_header(FILE *fp, struct Email *e, bool user_hdrs, bool weed)
Parses an RFC822 header.
Definition parse.c:1204
void mutt_sort_unsorted(struct Mailbox *m)
Sort emails by their disk order.
Definition sort.c:448
EmailSortType
Methods for sorting Emails.
Definition sort.h:53
@ EMAIL_SORT_UNSORTED
Sort by the order the messages appear in the mailbox.
Definition sort.h:64
void mutt_file_get_stat_timespec(struct timespec *dest, struct stat *st, enum MuttStatType type)
Read the stat() time into a time value.
Definition file.c:1469
int mutt_file_copy_stream(FILE *fp_in, FILE *fp_out)
Copy the contents of one file into another.
Definition file.c:222
int mutt_file_stat_compare(struct stat *st1, enum MuttStatType st1_type, struct stat *st2, enum MuttStatType st2_type)
Compare two stat infos.
Definition file.c:1531
void mutt_file_touch_atime(int fd)
Set the access time to current time.
Definition file.c:965
int mutt_file_check_empty(const char *path)
Is the mailbox empty.
Definition file.c:1326
int mutt_file_mkdir(const char *path, mode_t mode)
Recursively create directories.
Definition file.c:848
int mutt_file_lock(int fd, bool excl, bool timeout)
(Try to) Lock a file using fcntl()
Definition file.c:1092
int mutt_file_timespec_compare(struct timespec *a, struct timespec *b)
Compare to time values.
Definition file.c:1447
int mutt_file_unlock(int fd)
Unlock a file previously locked by mutt_file_lock()
Definition file.c:1139
bool mutt_file_seek(FILE *fp, LOFF_T offset, int whence)
Wrapper for fseeko with error handling.
Definition file.c:652
int mutt_file_stat_timespec_compare(struct stat *st, enum MuttStatType type, struct timespec *b)
Compare stat info with a time value.
Definition file.c:1509
#define mutt_file_fclose(FP)
Definition file.h:139
#define mutt_file_fopen(PATH, MODE)
Definition file.h:138
@ MUTT_STAT_CTIME
File/dir's ctime - creation time.
Definition file.h:55
@ MUTT_STAT_ATIME
File/dir's atime - last accessed time.
Definition file.h:53
@ MUTT_STAT_MTIME
File/dir's mtime - last modified time.
Definition file.h:54
void mutt_set_flag(struct Mailbox *m, struct Email *e, enum MessageType flag, bool bf, bool upd_mbox)
Set a flag on an email.
Definition flags.c:54
bool is_from(const char *s, char *path, size_t pathlen, time_t *tp)
Is a string a 'From' header line?
Definition from.c:49
char * ShortHostname
Short version of the hostname.
Definition globals.c:37
Global variables.
static void mbox_adata_free(void **ptr)
Free the private Account data - Implements Account::adata_free() -.
Definition mbox.c:76
#define mutt_error(...)
Definition logging2.h:94
#define mutt_message(...)
Definition logging2.h:93
#define mutt_debug(LEVEL,...)
Definition logging2.h:91
#define mutt_perror(...)
Definition logging2.h:95
static bool mbox_ac_add(struct Account *a, struct Mailbox *m)
Add a Mailbox to an Account - Implements MxOps::ac_add() -.
Definition mbox.c:787
static bool mbox_ac_owns_path(struct Account *a, const char *path)
Check whether an Account owns a Mailbox path - Implements MxOps::ac_owns_path() -.
Definition mbox.c:772
const struct MxOps MxMboxOps
Mbox Mailbox - Implements MxOps -.
Definition mbox.c:1709
const struct MxOps MxMmdfOps
MMDF Mailbox - Implements MxOps -.
Definition mbox.c:1739
static enum MxStatus mbox_mbox_check_stats(struct Mailbox *m, uint8_t flags)
Check the Mailbox statistics - Implements MxOps::mbox_check_stats() -.
Definition mbox.c:1647
static enum MxStatus mbox_mbox_check(struct Mailbox *m)
Check for new mail - Implements MxOps::mbox_check() -.
Definition mbox.c:926
static enum MxStatus mbox_mbox_close(struct Mailbox *m)
Close a Mailbox - Implements MxOps::mbox_close() -.
Definition mbox.c:1430
static bool mbox_mbox_open_append(struct Mailbox *m, OpenMailboxFlags flags)
Open a Mailbox for appending - Implements MxOps::mbox_open_append() -.
Definition mbox.c:874
static enum MxOpenReturns mbox_mbox_open(struct Mailbox *m)
Open a Mailbox - Implements MxOps::mbox_open() -.
Definition mbox.c:825
static enum MxStatus mbox_mbox_sync(struct Mailbox *m)
Save changes to the Mailbox - Implements MxOps::mbox_sync() -.
Definition mbox.c:1060
static int mbox_msg_close(struct Mailbox *m, struct Message *msg)
Close an email - Implements MxOps::msg_close() -.
Definition mbox.c:1516
static int mbox_msg_commit(struct Mailbox *m, struct Message *msg)
Save changes to an email - Implements MxOps::msg_commit() -.
Definition mbox.c:1499
static int mmdf_msg_commit(struct Mailbox *m, struct Message *msg)
Save changes to an email - Implements MxOps::msg_commit() -.
Definition mbox.c:1620
static bool mbox_msg_open_new(struct Mailbox *m, struct Message *msg, const struct Email *e)
Open a new message in a Mailbox - Implements MxOps::msg_open_new() -.
Definition mbox.c:1486
static bool mbox_msg_open(struct Mailbox *m, struct Message *msg, struct Email *e)
Open an email message in a Mailbox - Implements MxOps::msg_open() -.
Definition mbox.c:1470
static int mbox_msg_padding_size(struct Mailbox *m)
Bytes of padding between messages - Implements MxOps::msg_padding_size() -.
Definition mbox.c:1531
static int mmdf_msg_padding_size(struct Mailbox *m)
Bytes of padding between messages - Implements MxOps::msg_padding_size() -.
Definition mbox.c:1639
static int mbox_path_canon(struct Buffer *path)
Canonicalise a Mailbox path - Implements MxOps::path_canon() -.
Definition mbox.c:1603
static int mbox_path_is_empty(struct Buffer *path)
Is the mailbox empty - Implements MxOps::path_is_empty() -.
Definition mbox.c:1612
enum MailboxType mbox_path_probe(const char *path, const struct stat *st)
Is this an mbox Mailbox?
Definition mbox.c:1539
void mutt_hash_free(struct HashTable **ptr)
Free a hash table.
Definition hash.c:459
@ LL_DEBUG1
Log at debug level 1.
Definition logging2.h:45
Mbox local mailbox type.
#define MMDF_SEP
Definition lib.h:63
static enum MxOpenReturns mbox_parse_mailbox(struct Mailbox *m)
Read a mailbox from disk.
Definition mbox.c:348
static struct MboxAccountData * mbox_adata_new(void)
Create a new MboxAccountData struct.
Definition mbox.c:91
static bool mbox_has_new(struct Mailbox *m)
Does the mailbox have new mail.
Definition mbox.c:723
static int mbox_lock_mailbox(struct Mailbox *m, bool excl, bool retry)
Lock a mailbox.
Definition mbox.c:136
static struct MboxAccountData * mbox_adata_get(struct Mailbox *m)
Get the private data associated with a Mailbox.
Definition mbox.c:121
static int init_mailbox(struct Mailbox *m)
Add Mbox data to the Mailbox.
Definition mbox.c:102
static FILE * mbox_open_readwrite(struct Mailbox *m)
Open an mbox read-write.
Definition mbox.c:799
static FILE * mbox_open_readonly(struct Mailbox *m)
Open an mbox read-only.
Definition mbox.c:814
static void mbox_unlock_mailbox(struct Mailbox *m)
Unlock a mailbox.
Definition mbox.c:160
static enum MxOpenReturns mmdf_parse_mailbox(struct Mailbox *m)
Read a mailbox in MMDF format.
Definition mbox.c:180
void mbox_reset_atime(struct Mailbox *m, struct stat *st)
Reset the access time on the mailbox file.
Definition mbox.c:744
static int reopen_mailbox(struct Mailbox *m)
Close and reopen a mailbox.
Definition mbox.c:545
#define FREE(x)
Free memory and set the pointer to NULL.
Definition memory.h:68
#define MUTT_MEM_CALLOC(n, type)
Definition memory.h:52
int mutt_date_local_tz(time_t t)
Calculate the local timezone in seconds east of UTC.
Definition date.c:220
void mutt_time_now(struct timespec *tp)
Set the provided time field to the current time.
Definition date.c:481
Convenience wrapper for the library headers.
#define _(a)
Definition message.h:28
bool mutt_path_canon(struct Buffer *path, const char *homedir, bool is_dir)
Create the canonical version of a path.
Definition path.c:248
char * mutt_path_dirname(const char *path)
Return a path up to, but not including, the final '/'.
Definition path.c:312
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition string.c:662
size_t mutt_str_startswith(const char *str, const char *prefix)
Check whether a string starts with a prefix.
Definition string.c:234
Many unsorted constants and some structs.
@ MUTT_READ
Messages that have been read.
Definition mutt.h:91
@ MUTT_OLD
Old messages.
Definition mutt.h:89
@ MUTT_PURGE
Messages to be purged (bypass trash)
Definition mutt.h:95
@ MUTT_TAG
Tagged messages.
Definition mutt.h:98
@ MUTT_FLAG
Flagged messages.
Definition mutt.h:97
@ MUTT_DELETE
Messages to be deleted.
Definition mutt.h:93
@ MUTT_REPLIED
Messages that have been replied to.
Definition mutt.h:90
void pretty_mailbox(struct Buffer *buf)
Shorten a mailbox path using '~' or '='.
Definition muttlib.c:427
Some miscellaneous functions.
void mx_alloc_memory(struct Mailbox *m, int req_size)
Create storage for the emails.
Definition mx.c:1208
int mx_msg_close(struct Mailbox *m, struct Message **ptr)
Close a message.
Definition mx.c:1182
void mx_fastclose_mailbox(struct Mailbox *m, bool keep_account)
Free up memory associated with the Mailbox.
Definition mx.c:411
bool mx_mbox_open(struct Mailbox *m, OpenMailboxFlags flags)
Open a mailbox and parse it.
Definition mx.c:285
struct Message * mx_msg_open(struct Mailbox *m, struct Email *e)
Return a stream pointer for a message.
Definition mx.c:1136
enum MxStatus mx_mbox_close(struct Mailbox *m)
Save changes and close mailbox.
Definition mx.c:595
API for mailboxes.
uint8_t OpenMailboxFlags
Flags for mutt_open_mailbox(), e.g. MUTT_NOSORT.
Definition mxapi.h:38
#define MUTT_MAILBOX_CHECK_STATS
Ignore mail_check_stats and calculate statistics (used by <check-stats>)
Definition mxapi.h:51
#define MUTT_QUIET
Do not print any messages.
Definition mxapi.h:43
MxOpenReturns
Return values for mbox_open()
Definition mxapi.h:72
@ MX_OPEN_ERROR
Open failed with an error.
Definition mxapi.h:74
@ MX_OPEN_ABORT
Open was aborted.
Definition mxapi.h:75
@ MX_OPEN_OK
Open succeeded.
Definition mxapi.h:73
#define MUTT_PEEK
Revert atime back after taking a look (if applicable)
Definition mxapi.h:44
#define MUTT_NOSORT
Do not sort the mailbox after opening it.
Definition mxapi.h:40
MxStatus
Return values from mbox_check(), mbox_check_stats(), mbox_sync(), and mbox_close()
Definition mxapi.h:59
@ MX_STATUS_LOCKED
Couldn't lock the Mailbox.
Definition mxapi.h:63
@ MX_STATUS_ERROR
An error occurred.
Definition mxapi.h:60
@ MX_STATUS_OK
No changes.
Definition mxapi.h:61
@ MX_STATUS_REOPENED
Mailbox was reopened.
Definition mxapi.h:64
@ MX_STATUS_NEW_MAIL
New mail received in Mailbox.
Definition mxapi.h:62
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition pool.c:91
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition pool.c:111
Progress Bar.
@ MUTT_PROGRESS_READ
Progress tracks elements, according to $read_inc
Definition lib.h:84
@ MUTT_PROGRESS_WRITE
Progress tracks elements, according to $write_inc
Definition lib.h:85
struct Progress * progress_new(enum ProgressType type, size_t size)
Create a new Progress Bar.
Definition progress.c:139
void progress_free(struct Progress **ptr)
Free a Progress Bar.
Definition progress.c:110
void progress_set_message(struct Progress *progress, const char *fmt,...) __attribute__((__format__(__printf__
bool progress_update(struct Progress *progress, size_t pos, int percent)
Update the state of the progress bar.
Definition progress.c:80
#define TAILQ_EMPTY(head)
Definition queue.h:778
volatile sig_atomic_t SigInt
true after SIGINT is received
Definition signal.c:68
void mutt_sig_block(void)
Block signals during critical operations.
Definition signal.c:227
void mutt_sig_unblock(void)
Restore previously blocked signals.
Definition signal.c:245
#define NONULL(x)
Definition string2.h:44
A group of associated Mailboxes.
Definition account.h:36
enum MailboxType type
Type of Mailboxes this Account contains.
Definition account.h:37
struct MailboxArray mailboxes
All Mailboxes.
Definition account.h:40
void(* adata_free)(void **ptr)
Definition account.h:53
void * adata
Private data (for Mailbox backends)
Definition account.h:42
struct Body * parts
parts of a multipart or message/rfc822
Definition body.h:73
LOFF_T offset
offset where the actual data begins
Definition body.h:52
LOFF_T length
length (in bytes) of attachment
Definition body.h:53
long hdr_offset
Offset in stream where the headers begin.
Definition body.h:81
String manipulation buffer.
Definition buffer.h:36
The envelope/body of an email.
Definition email.h:39
bool read
Email is read.
Definition email.h:50
bool purge
Skip trash folder when deleting.
Definition email.h:79
struct Envelope * env
Envelope information.
Definition email.h:68
int lines
How many lines in the body of this message?
Definition email.h:62
struct Body * body
List of MIME parts.
Definition email.h:69
bool old
Email is seen, but unread.
Definition email.h:49
bool changed
Email has been edited.
Definition email.h:77
LOFF_T offset
Where in the stream does this message begin?
Definition email.h:71
bool attach_del
Has an attachment marked for deletion.
Definition email.h:99
bool flagged
Marked important?
Definition email.h:47
bool replied
Email has been replied to.
Definition email.h:51
bool deleted
Email is deleted.
Definition email.h:78
int index
The absolute (unsorted) message number.
Definition email.h:110
bool tagged
Email is tagged.
Definition email.h:107
time_t received
Time when the message was placed in the mailbox.
Definition email.h:61
struct AddressList return_path
Return path for the Email.
Definition envelope.h:58
struct AddressList from
Email's 'From' list.
Definition envelope.h:59
Store of new offsets, used by mutt_sync_mailbox()
Definition mbox.c:65
long lines
Number of lines.
Definition mbox.c:69
LOFF_T hdr
Header offset.
Definition mbox.c:67
LOFF_T length
Total length.
Definition mbox.c:70
LOFF_T body
Body offset.
Definition mbox.c:68
bool valid
Is this entry valid?
Definition mbox.c:66
A mailbox.
Definition mailbox.h:78
int vcount
The number of virtual messages.
Definition mailbox.h:98
bool changed
Mailbox has been modified.
Definition mailbox.h:109
bool has_new
Mailbox has new mail.
Definition mailbox.h:84
int * v2r
Mapping from virtual to real msgno.
Definition mailbox.h:97
int msg_new
Number of new messages.
Definition mailbox.h:91
int msg_count
Total number of messages.
Definition mailbox.h:87
int email_max
Size of emails array.
Definition mailbox.h:96
enum MailboxType type
Mailbox type.
Definition mailbox.h:101
bool newly_created
Mbox or mmdf just popped into existence.
Definition mailbox.h:102
struct HashTable * subj_hash
Hash Table: "subject" -> Email.
Definition mailbox.h:123
struct Email ** emails
Array of Emails.
Definition mailbox.h:95
struct HashTable * id_hash
Hash Table: "message-id" -> Email.
Definition mailbox.h:122
struct Buffer pathbuf
Path of the Mailbox.
Definition mailbox.h:79
bool peekonly
Just taking a glance, revert atime.
Definition mailbox.h:113
int msg_deleted
Number of deleted messages.
Definition mailbox.h:92
struct Account * account
Account that owns this Mailbox.
Definition mailbox.h:126
off_t size
Size of the Mailbox.
Definition mailbox.h:83
struct HashTable * label_hash
Hash Table: "x-labels" -> Email.
Definition mailbox.h:124
int msg_flagged
Number of flagged messages.
Definition mailbox.h:89
struct timespec last_visited
Time of last exit from this mailbox.
Definition mailbox.h:103
bool readonly
Don't allow changes to the mailbox.
Definition mailbox.h:115
int msg_tagged
How many messages are tagged?
Definition mailbox.h:93
bool verbose
Display status messages?
Definition mailbox.h:116
int msg_unread
Number of unread messages.
Definition mailbox.h:88
Mbox-specific Account data -.
Definition lib.h:50
FILE * fp
Mailbox file.
Definition lib.h:51
bool locked
is the mailbox locked?
Definition lib.h:56
struct timespec atime
File's last-access time.
Definition lib.h:53
struct timespec mtime
Time Mailbox was last changed.
Definition lib.h:52
A local copy of an email.
Definition message.h:34
FILE * fp
pointer to the message data
Definition message.h:35
bool write
nonzero if message is open for writing
Definition message.h:38
Definition mxapi.h:87
Container for Accounts, Notifications.
Definition neomutt.h:128
char * username
User's login name.
Definition neomutt.h:142
char * home_dir
User's home directory.
Definition neomutt.h:141
struct ConfigSubset * sub
Inherited config items.
Definition neomutt.h:134
int cs_subset_str_native_set(const struct ConfigSubset *sub, const char *name, intptr_t value, struct Buffer *err)
Natively set the value of a string config item.
Definition subset.c:303
#define buf_mktemp(buf)
Definition tmp.h:33