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