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