NeoMutt  2022-04-29-178-g3b62e6
Teaching an old dog new tricks
DOXYGEN
maildir.c
Go to the documentation of this file.
1
34#include "config.h"
35#include <dirent.h>
36#include <errno.h>
37#include <fcntl.h>
38#include <inttypes.h>
39#include <limits.h>
40#include <stdbool.h>
41#include <stdio.h>
42#include <stdlib.h>
43#include <string.h>
44#include <sys/stat.h>
45#include <unistd.h>
46#include <utime.h>
47#include "private.h"
48#include "mutt/lib.h"
49#include "config/lib.h"
50#include "email/lib.h"
51#include "core/lib.h"
52#include "lib.h"
53#include "progress/lib.h"
54#include "copy.h"
55#include "edata.h"
56#include "mdata.h"
57#include "mdemail.h"
58#include "mutt_globals.h"
59#include "mx.h"
60#ifdef USE_INOTIFY
61#include "monitor.h"
62#endif
63#ifdef USE_HCACHE
64#include "hcache/lib.h"
65#endif
66#ifdef USE_NOTMUCH
67#include "notmuch/lib.h"
68#endif
69
70struct Progress;
71
72// Flags for maildir_mbox_check()
73#define MMC_NO_DIRS 0
74#define MMC_NEW_DIR (1 << 0)
75#define MMC_CUR_DIR (1 << 1)
76
86static void maildir_check_dir(struct Mailbox *m, const char *dir_name,
87 bool check_new, bool check_stats)
88{
89 DIR *dirp = NULL;
90 struct dirent *de = NULL;
91 char *p = NULL;
92 struct stat st = { 0 };
93
94 struct Buffer *path = mutt_buffer_pool_get();
95 struct Buffer *msgpath = mutt_buffer_pool_get();
96 mutt_buffer_printf(path, "%s/%s", mailbox_path(m), dir_name);
97
98 /* when $mail_check_recent is set, if the new/ directory hasn't been modified since
99 * the user last exited the m, then we know there is no recent mail. */
100 const bool c_mail_check_recent = cs_subset_bool(NeoMutt->sub, "mail_check_recent");
101 if (check_new && c_mail_check_recent)
102 {
103 if ((stat(mutt_buffer_string(path), &st) == 0) &&
105 {
106 check_new = false;
107 }
108 }
109
110 if (!(check_new || check_stats))
111 goto cleanup;
112
113 dirp = opendir(mutt_buffer_string(path));
114 if (!dirp)
115 {
116 m->type = MUTT_UNKNOWN;
117 goto cleanup;
118 }
119
120 while ((de = readdir(dirp)))
121 {
122 if (*de->d_name == '.')
123 continue;
124
125 p = strstr(de->d_name, ":2,");
126 if (p && strchr(p + 3, 'T'))
127 continue;
128
129 if (check_stats)
130 {
131 m->msg_count++;
132 if (p && strchr(p + 3, 'F'))
133 m->msg_flagged++;
134 }
135 if (!p || !strchr(p + 3, 'S'))
136 {
137 if (check_stats)
138 m->msg_unread++;
139 if (check_new)
140 {
141 if (c_mail_check_recent)
142 {
143 mutt_buffer_printf(msgpath, "%s/%s", mutt_buffer_string(path), de->d_name);
144 /* ensure this message was received since leaving this m */
145 if ((stat(mutt_buffer_string(msgpath), &st) == 0) &&
147 {
148 continue;
149 }
150 }
151 m->has_new = true;
152 check_new = false;
153 m->msg_new++;
154 if (!check_stats)
155 break;
156 }
157 }
158 }
159
160 closedir(dirp);
161
162cleanup:
164 mutt_buffer_pool_release(&msgpath);
165}
166
175static int ch_compare(const void *a, const void *b)
176{
177 return (int) (*((const char *) a) - *((const char *) b));
178}
179
186void maildir_gen_flags(char *dest, size_t destlen, struct Email *e)
187{
188 *dest = '\0';
189
190 const char *flags = NULL;
191
193 if (edata)
194 flags = edata->maildir_flags;
195
196 /* The maildir specification requires that all files in the cur
197 * subdirectory have the :unique string appended, regardless of whether
198 * or not there are any flags. If .old is set, we know that this message
199 * will end up in the cur directory, so we include it in the following
200 * test even though there is no associated flag. */
201
202 if (e->flagged || e->replied || e->read || e->deleted || e->old || flags)
203 {
204 char tmp[1024] = { 0 };
205 snprintf(tmp, sizeof(tmp), "%s%s%s%s%s", e->flagged ? "F" : "", e->replied ? "R" : "",
206 e->read ? "S" : "", e->deleted ? "T" : "", NONULL(flags));
207 if (flags)
208 qsort(tmp, strlen(tmp), 1, ch_compare);
209 snprintf(dest, destlen, ":2,%s", tmp);
210 }
211}
212
237int maildir_commit_message(struct Mailbox *m, struct Message *msg, struct Email *e)
238{
239 char subdir[4] = { 0 };
240 char suffix[16] = { 0 };
241 int rc = 0;
242
243 if (mutt_file_fsync_close(&msg->fp))
244 {
245 mutt_perror(_("Could not flush message to disk"));
246 return -1;
247 }
248
249 /* extract the subdir */
250 char *s = strrchr(msg->path, '/') + 1;
251 mutt_str_copy(subdir, s, 4);
252
253 /* extract the flags */
254 s = strchr(s, ':');
255 if (s)
256 mutt_str_copy(suffix, s, sizeof(suffix));
257 else
258 suffix[0] = '\0';
259
260 /* construct a new file name. */
261 struct Buffer *path = mutt_buffer_pool_get();
262 struct Buffer *full = mutt_buffer_pool_get();
263 while (true)
264 {
265 mutt_buffer_printf(path, "%s/%lld.R%" PRIu64 ".%s%s", subdir,
266 (long long) mutt_date_epoch(), mutt_rand64(),
267 NONULL(ShortHostname), suffix);
268 mutt_buffer_printf(full, "%s/%s", mailbox_path(m), mutt_buffer_string(path));
269
270 mutt_debug(LL_DEBUG2, "renaming %s to %s\n", msg->path, mutt_buffer_string(full));
271
272 if (mutt_file_safe_rename(msg->path, mutt_buffer_string(full)) == 0)
273 {
274 /* Adjust the mtime on the file to match the time at which this
275 * message was received. Currently this is only set when copying
276 * messages between mailboxes, so we test to ensure that it is
277 * actually set. */
278 if (msg->received != 0)
279 {
280 struct utimbuf ut;
281 int rc_utime;
282
283 ut.actime = msg->received;
284 ut.modtime = msg->received;
285 do
286 {
287 rc_utime = utime(mutt_buffer_string(full), &ut);
288 } while ((rc_utime == -1) && (errno == EINTR));
289 if (rc_utime == -1)
290 {
291 mutt_perror(_("maildir_commit_message(): unable to set time on file"));
292 rc = -1;
293 goto cleanup;
294 }
295 }
296
297#ifdef USE_NOTMUCH
298 if (m->type == MUTT_NOTMUCH)
300#endif
301 if (e)
304 FREE(&msg->path);
305
306 goto cleanup;
307 }
308 else if (errno != EEXIST)
309 {
311 rc = -1;
312 goto cleanup;
313 }
314 }
315
316cleanup:
319
320 return rc;
321}
322
330int maildir_rewrite_message(struct Mailbox *m, int msgno)
331{
332 if (!m || !m->emails || (msgno >= m->msg_count))
333 return -1;
334
335 struct Email *e = m->emails[msgno];
336 if (!e)
337 return -1;
338
339 bool restore = true;
340
341 long old_body_offset = e->body->offset;
342 long old_body_length = e->body->length;
343 long old_hdr_lines = e->lines;
344
345 struct Message *src = mx_msg_open(m, msgno);
346 struct Message *dest = mx_msg_open_new(m, e, MUTT_MSG_NO_FLAGS);
347 if (!src || !dest)
348 return -1;
349
350 int rc = mutt_copy_message(dest->fp, e, src, MUTT_CM_UPDATE, CH_UPDATE | CH_UPDATE_LEN, 0);
351 if (rc == 0)
352 {
353 char oldpath[PATH_MAX] = { 0 };
354 char partpath[PATH_MAX] = { 0 };
355 snprintf(oldpath, sizeof(oldpath), "%s/%s", mailbox_path(m), e->path);
356 mutt_str_copy(partpath, e->path, sizeof(partpath));
357
358 rc = maildir_commit_message(m, dest, e);
359
360 if (rc == 0)
361 {
362 unlink(oldpath);
363 restore = false;
364 }
365 }
366 mx_msg_close(m, &src);
367 mx_msg_close(m, &dest);
368
369 if ((rc == -1) && restore)
370 {
371 e->body->offset = old_body_offset;
372 e->body->length = old_body_length;
373 e->lines = old_hdr_lines;
374 }
375
377 return rc;
378}
379
387int maildir_sync_message(struct Mailbox *m, int msgno)
388{
389 if (!m || !m->emails || (msgno >= m->msg_count))
390 return -1;
391
392 struct Email *e = m->emails[msgno];
393 if (!e)
394 return -1;
395
396 struct Buffer *newpath = NULL;
397 struct Buffer *partpath = NULL;
398 struct Buffer *fullpath = NULL;
399 struct Buffer *oldpath = NULL;
400 char suffix[16] = { 0 };
401 int rc = 0;
402
403 /* TODO: why the e->env check? */
404 if (e->attach_del || (e->env && e->env->changed))
405 {
406 /* when doing attachment deletion/rethreading, fall back to the MH case. */
407 if (maildir_rewrite_message(m, msgno) != 0)
408 return -1;
409 /* TODO: why the env check? */
410 if (e->env)
411 e->env->changed = 0;
412 }
413 else
414 {
415 /* we just have to rename the file. */
416
417 char *p = strrchr(e->path, '/');
418 if (!p)
419 {
420 mutt_debug(LL_DEBUG1, "%s: unable to find subdir!\n", e->path);
421 return -1;
422 }
423 p++;
424 newpath = mutt_buffer_pool_get();
425 partpath = mutt_buffer_pool_get();
426 fullpath = mutt_buffer_pool_get();
427 oldpath = mutt_buffer_pool_get();
428
429 mutt_buffer_strcpy(newpath, p);
430
431 /* kill the previous flags */
432 p = strchr(newpath->data, ':');
433 if (p)
434 {
435 *p = '\0';
436 newpath->dptr = p; /* fix buffer up, just to be safe */
437 }
438
439 maildir_gen_flags(suffix, sizeof(suffix), e);
440
441 mutt_buffer_printf(partpath, "%s/%s%s", (e->read || e->old) ? "cur" : "new",
442 mutt_buffer_string(newpath), suffix);
443 mutt_buffer_printf(fullpath, "%s/%s", mailbox_path(m), mutt_buffer_string(partpath));
444 mutt_buffer_printf(oldpath, "%s/%s", mailbox_path(m), e->path);
445
447 {
448 /* message hasn't really changed */
449 goto cleanup;
450 }
451
452 /* record that the message is possibly marked as trashed on disk */
453 e->trash = e->deleted;
454
455 if (rename(mutt_buffer_string(oldpath), mutt_buffer_string(fullpath)) != 0)
456 {
457 mutt_perror("rename");
458 rc = -1;
459 goto cleanup;
460 }
462 }
463
464cleanup:
465 mutt_buffer_pool_release(&newpath);
466 mutt_buffer_pool_release(&partpath);
467 mutt_buffer_pool_release(&fullpath);
468 mutt_buffer_pool_release(&oldpath);
469
470 return rc;
471}
472
478{
479 char buf[PATH_MAX] = { 0 };
480 struct stat st = { 0 };
482
483 snprintf(buf, sizeof(buf), "%s/%s", mailbox_path(m), "cur");
484 if (stat(buf, &st) == 0)
486 snprintf(buf, sizeof(buf), "%s/%s", mailbox_path(m), "new");
487
488 if (stat(buf, &st) == 0)
490}
491
495static int maildir_cmp_inode(const void *a, const void *b)
496{
497 const struct MdEmail *ma = *(struct MdEmail **) a;
498 const struct MdEmail *mb = *(struct MdEmail **) b;
499
500 return ma->inode - mb->inode;
501}
502
513int maildir_parse_dir(struct Mailbox *m, struct MdEmailArray *mda,
514 const char *subdir, struct Progress *progress)
515{
516 struct dirent *de = NULL;
517 int rc = 0;
518 bool is_old = false;
519 struct MdEmail *entry = NULL;
520 struct Email *e = NULL;
521
522 struct Buffer *buf = mutt_buffer_pool_get();
523
524 mutt_buffer_printf(buf, "%s/%s", mailbox_path(m), subdir);
525 const bool c_mark_old = cs_subset_bool(NeoMutt->sub, "mark_old");
526 is_old = c_mark_old ? mutt_str_equal("cur", subdir) : false;
527
528 DIR *dirp = opendir(mutt_buffer_string(buf));
529 if (!dirp)
530 {
531 rc = -1;
532 goto cleanup;
533 }
534
535 while (((de = readdir(dirp))) && !SigInt)
536 {
537 if (*de->d_name == '.')
538 continue;
539
540 /* FOO - really ignore the return value? */
541 mutt_debug(LL_DEBUG2, "queueing %s\n", de->d_name);
542
543 e = email_new();
546
547 e->old = is_old;
548 maildir_parse_flags(e, de->d_name);
549
550 if (m->verbose && progress)
551 progress_update(progress, ARRAY_SIZE(mda) + 1, -1);
552
553 mutt_buffer_printf(buf, "%s/%s", subdir, de->d_name);
554 e->path = mutt_buffer_strdup(buf);
555
556 entry = maildir_entry_new();
557 entry->email = e;
558 entry->inode = de->d_ino;
559 ARRAY_ADD(mda, entry);
560 }
561
562 closedir(dirp);
563
564 if (SigInt)
565 {
566 SigInt = false;
567 return -2; /* action aborted */
568 }
569
571
572cleanup:
574
575 return rc;
576}
577
585size_t maildir_hcache_keylen(const char *fn)
586{
587 const char *p = strrchr(fn, ':');
588 return p ? (size_t) (p - fn) : mutt_str_len(fn);
589}
590
597void maildir_delayed_parsing(struct Mailbox *m, struct MdEmailArray *mda,
598 struct Progress *progress)
599{
600 char fn[PATH_MAX] = { 0 };
601
602#ifdef USE_HCACHE
603 const char *const c_header_cache = cs_subset_path(NeoMutt->sub, "header_cache");
604 struct HeaderCache *hc = mutt_hcache_open(c_header_cache, mailbox_path(m), NULL);
605#endif
606
607 struct MdEmail *md = NULL;
608 struct MdEmail **mdp = NULL;
609 ARRAY_FOREACH(mdp, mda)
610 {
611 md = *mdp;
612 if (!md || !md->email || md->header_parsed)
613 continue;
614
615 if (m->verbose && progress)
616 progress_update(progress, ARRAY_FOREACH_IDX, -1);
617
618 snprintf(fn, sizeof(fn), "%s/%s", mailbox_path(m), md->email->path);
619
620#ifdef USE_HCACHE
621 struct stat st_lastchanged = { 0 };
622 int rc = 0;
623 const bool c_maildir_header_cache_verify = cs_subset_bool(NeoMutt->sub, "maildir_header_cache_verify");
624 if (c_maildir_header_cache_verify)
625 {
626 rc = stat(fn, &st_lastchanged);
627 }
628
629 const char *key = md->email->path + 3;
630 size_t keylen = maildir_hcache_keylen(key);
631 struct HCacheEntry hce = mutt_hcache_fetch(hc, key, keylen, 0);
632
633 if (hce.email && (rc == 0) && (st_lastchanged.st_mtime <= hce.uidvalidity))
634 {
637 hce.email->old = md->email->old;
638 hce.email->path = mutt_str_dup(md->email->path);
639 email_free(&md->email);
640 md->email = hce.email;
641 maildir_parse_flags(md->email, fn);
642 }
643 else
644#endif
645 {
646 if (maildir_parse_message(m->type, fn, md->email->old, md->email))
647 {
648 md->header_parsed = true;
649#ifdef USE_HCACHE
650 key = md->email->path + 3;
651 keylen = maildir_hcache_keylen(key);
652 mutt_hcache_store(hc, key, keylen, md->email, 0);
653#endif
654 }
655 else
656 email_free(&md->email);
657 }
658 }
659#ifdef USE_HCACHE
661#endif
662}
663
671int maildir_read_dir(struct Mailbox *m, const char *subdir)
672{
673 if (!m)
674 return -1;
675
676 struct Progress *progress = NULL;
677
678 if (m->verbose)
679 {
680 char msg[PATH_MAX] = { 0 };
681 snprintf(msg, sizeof(msg), _("Scanning %s..."), mailbox_path(m));
682 progress = progress_new(msg, MUTT_PROGRESS_READ, 0);
683 }
684
686 if (!mdata)
687 {
689 m->mdata = mdata;
691 }
692
693 struct MdEmailArray mda = ARRAY_HEAD_INITIALIZER;
694 int rc = maildir_parse_dir(m, &mda, subdir, progress);
695 progress_free(&progress);
696 if (rc < 0)
697 return -1;
698
699 if (m->verbose)
700 {
701 char msg[PATH_MAX] = { 0 };
702 snprintf(msg, sizeof(msg), _("Reading %s..."), mailbox_path(m));
703 progress = progress_new(msg, MUTT_PROGRESS_READ, ARRAY_SIZE(&mda));
704 }
705 maildir_delayed_parsing(m, &mda, progress);
706 progress_free(&progress);
707
709
710 if (!mdata->mh_umask)
711 mdata->mh_umask = mh_umask(m);
712
713 return 0;
714}
715
725void maildir_canon_filename(struct Buffer *dest, const char *src)
726{
727 if (!dest || !src)
728 return;
729
730 char *t = strrchr(src, '/');
731 if (t)
732 src = t + 1;
733
734 mutt_buffer_strcpy(dest, src);
735 char *u = strpbrk(dest->data, ",:");
736 if (u)
737 {
738 *u = '\0';
739 dest->dptr = u;
740 }
741}
742
755static FILE *maildir_open_find_message_dir(const char *folder, const char *unique,
756 const char *subfolder, char **newname)
757{
758 struct Buffer *dir = mutt_buffer_pool_get();
759 struct Buffer *tunique = mutt_buffer_pool_get();
760 struct Buffer *fname = mutt_buffer_pool_get();
761
762 struct dirent *de = NULL;
763
764 FILE *fp = NULL;
765 int oe = ENOENT;
766
767 mutt_buffer_printf(dir, "%s/%s", folder, subfolder);
768
769 DIR *dp = opendir(mutt_buffer_string(dir));
770 if (!dp)
771 {
772 errno = ENOENT;
773 goto cleanup;
774 }
775
776 while ((de = readdir(dp)))
777 {
778 maildir_canon_filename(tunique, de->d_name);
779
780 if (mutt_str_equal(mutt_buffer_string(tunique), unique))
781 {
782 mutt_buffer_printf(fname, "%s/%s/%s", folder, subfolder, de->d_name);
783 fp = fopen(mutt_buffer_string(fname), "r");
784 oe = errno;
785 break;
786 }
787 }
788
789 closedir(dp);
790
791 if (newname && fp)
792 *newname = mutt_buffer_strdup(fname);
793
794 errno = oe;
795
796cleanup:
798 mutt_buffer_pool_release(&tunique);
800
801 return fp;
802}
803
809void maildir_parse_flags(struct Email *e, const char *path)
810{
811 char *q = NULL;
812
813 e->flagged = false;
814 e->read = false;
815 e->replied = false;
816
818
819 char *p = strrchr(path, ':');
820 if (p && mutt_str_startswith(p + 1, "2,"))
821 {
822 p += 3;
823
824 mutt_str_replace(&edata->maildir_flags, p);
825 q = edata->maildir_flags;
826
827 while (*p)
828 {
829 switch (*p)
830 {
831 case 'F':
832 e->flagged = true;
833 break;
834
835 case 'R': /* replied */
836 e->replied = true;
837 break;
838
839 case 'S': /* seen */
840 e->read = true;
841 break;
842
843 case 'T': /* trashed */
844 {
845 const bool c_flag_safe = cs_subset_bool(NeoMutt->sub, "flag_safe");
846 if (!e->flagged || !c_flag_safe)
847 {
848 e->trash = true;
849 e->deleted = true;
850 }
851 break;
852 }
853
854 default:
855 *q++ = *p;
856 break;
857 }
858 p++;
859 }
860 }
861
862 if (q == edata->maildir_flags)
863 FREE(&edata->maildir_flags);
864 else if (q)
865 *q = '\0';
866}
867
881struct Email *maildir_parse_stream(enum MailboxType type, FILE *fp,
882 const char *fname, bool is_old, struct Email *e)
883{
884 const long size = mutt_file_get_size_fp(fp);
885 if (size == 0)
886 {
887 return NULL;
888 }
889
890 if (!e)
891 {
892 e = email_new();
895 }
896 e->env = mutt_rfc822_read_header(fp, e, false, false);
897
898 if (e->received == 0)
899 e->received = e->date_sent;
900
901 /* always update the length since we have fresh information available. */
902 e->body->length = size - e->body->offset;
903
904 e->index = -1;
905
906 if (type == MUTT_MAILDIR)
907 {
908 /* maildir stores its flags in the filename, so ignore the
909 * flags in the header of the message */
910
911 e->old = is_old;
912 maildir_parse_flags(e, fname);
913 }
914 return e;
915}
916
928struct Email *maildir_parse_message(enum MailboxType type, const char *fname,
929 bool is_old, struct Email *e)
930{
931 FILE *fp = fopen(fname, "r");
932 if (!fp)
933 return NULL;
934
935 struct Email *e_res = maildir_parse_stream(type, fp, fname, is_old, e);
936 mutt_file_fclose(&fp);
937 return e_res;
938}
939
949{
950 struct Email *e = m->emails[msgno];
951 if (!e)
952 return false;
953
954 const bool c_maildir_trash = cs_subset_bool(NeoMutt->sub, "maildir_trash");
955 if (e->deleted && !c_maildir_trash)
956 {
957 char path[PATH_MAX] = { 0 };
958 snprintf(path, sizeof(path), "%s/%s", mailbox_path(m), e->path);
959#ifdef USE_HCACHE
960 if (hc)
961 {
962 const char *key = e->path + 3;
963 size_t keylen = maildir_hcache_keylen(key);
964 mutt_hcache_delete_record(hc, key, keylen);
965 }
966#endif
967 unlink(path);
968 }
969 else if (e->changed || e->attach_del ||
970 ((c_maildir_trash || e->trash) && (e->deleted != e->trash)))
971 {
972 if (maildir_sync_message(m, msgno) == -1)
973 return false;
974 }
975
976#ifdef USE_HCACHE
977 if (hc && e->changed)
978 {
979 const char *key = e->path + 3;
980 size_t keylen = maildir_hcache_keylen(key);
981 mutt_hcache_store(hc, key, keylen, e, 0);
982 }
983#endif
984
985 return true;
986}
987
995FILE *maildir_open_find_message(const char *folder, const char *msg, char **newname)
996{
997 static unsigned int new_hits = 0, cur_hits = 0; /* simple dynamic optimization */
998
999 struct Buffer *unique = mutt_buffer_pool_get();
1000 maildir_canon_filename(unique, msg);
1001
1002 FILE *fp = maildir_open_find_message_dir(folder, mutt_buffer_string(unique),
1003 (new_hits > cur_hits) ? "new" : "cur", newname);
1004 if (fp || (errno != ENOENT))
1005 {
1006 if ((new_hits < UINT_MAX) && (cur_hits < UINT_MAX))
1007 {
1008 new_hits += ((new_hits > cur_hits) ? 1 : 0);
1009 cur_hits += ((new_hits > cur_hits) ? 0 : 1);
1010 }
1011
1012 goto cleanup;
1013 }
1015 (new_hits > cur_hits) ? "cur" : "new", newname);
1016 if (fp || (errno != ENOENT))
1017 {
1018 if ((new_hits < UINT_MAX) && (cur_hits < UINT_MAX))
1019 {
1020 new_hits += ((new_hits > cur_hits) ? 0 : 1);
1021 cur_hits += ((new_hits > cur_hits) ? 1 : 0);
1022 }
1023
1024 goto cleanup;
1025 }
1026
1027 fp = NULL;
1028
1029cleanup:
1030 mutt_buffer_pool_release(&unique);
1031
1032 return fp;
1033}
1034
1042int maildir_check_empty(const char *path)
1043{
1044 DIR *dp = NULL;
1045 struct dirent *de = NULL;
1046 int rc = 1; /* assume empty until we find a message */
1047 char realpath[PATH_MAX] = { 0 };
1048 int iter = 0;
1049
1050 /* Strategy here is to look for any file not beginning with a period */
1051
1052 do
1053 {
1054 /* we do "cur" on the first iteration since it's more likely that we'll
1055 * find old messages without having to scan both subdirs */
1056 snprintf(realpath, sizeof(realpath), "%s/%s", path, (iter == 0) ? "cur" : "new");
1057 dp = opendir(realpath);
1058 if (!dp)
1059 return -1;
1060 while ((de = readdir(dp)))
1061 {
1062 if (*de->d_name != '.')
1063 {
1064 rc = 0;
1065 break;
1066 }
1067 }
1068 closedir(dp);
1069 iter++;
1070 } while (rc && iter < 2);
1071
1072 return rc;
1073}
1074
1078bool maildir_ac_owns_path(struct Account *a, const char *path)
1079{
1080 return true;
1081}
1082
1086bool maildir_ac_add(struct Account *a, struct Mailbox *m)
1087{
1088 return true;
1089}
1090
1095{
1096 /* maildir looks sort of like MH, except that there are two subdirectories
1097 * of the main folder path from which to read messages */
1098 if ((maildir_read_dir(m, "new") == -1) || (maildir_read_dir(m, "cur") == -1))
1099 return MX_OPEN_ERROR;
1100
1101 return MX_OPEN_OK;
1102}
1103
1108{
1109 if (!(flags & (MUTT_APPEND | MUTT_APPENDNEW | MUTT_NEWFOLDER)))
1110 {
1111 return true;
1112 }
1113
1114 errno = 0;
1115 if ((mutt_file_mkdir(mailbox_path(m), S_IRWXU) != 0) && (errno != EEXIST))
1116 {
1118 return false;
1119 }
1120
1121 char tmp[PATH_MAX] = { 0 };
1122 snprintf(tmp, sizeof(tmp), "%s/cur", mailbox_path(m));
1123 errno = 0;
1124 if ((mkdir(tmp, S_IRWXU) != 0) && (errno != EEXIST))
1125 {
1126 mutt_perror(tmp);
1127 rmdir(mailbox_path(m));
1128 return false;
1129 }
1130
1131 snprintf(tmp, sizeof(tmp), "%s/new", mailbox_path(m));
1132 errno = 0;
1133 if ((mkdir(tmp, S_IRWXU) != 0) && (errno != EEXIST))
1134 {
1135 mutt_perror(tmp);
1136 snprintf(tmp, sizeof(tmp), "%s/cur", mailbox_path(m));
1137 rmdir(tmp);
1138 rmdir(mailbox_path(m));
1139 return false;
1140 }
1141
1142 snprintf(tmp, sizeof(tmp), "%s/tmp", mailbox_path(m));
1143 errno = 0;
1144 if ((mkdir(tmp, S_IRWXU) != 0) && (errno != EEXIST))
1145 {
1146 mutt_perror(tmp);
1147 snprintf(tmp, sizeof(tmp), "%s/cur", mailbox_path(m));
1148 rmdir(tmp);
1149 snprintf(tmp, sizeof(tmp), "%s/new", mailbox_path(m));
1150 rmdir(tmp);
1151 rmdir(mailbox_path(m));
1152 return false;
1153 }
1154
1155 return true;
1156}
1157
1169{
1170 struct stat st_new = { 0 }; /* status of the "new" subdirectory */
1171 struct stat st_cur = { 0 }; /* status of the "cur" subdirectory */
1172 int changed = MMC_NO_DIRS; /* which subdirectories have changed */
1173 bool occult = false; /* messages were removed from the mailbox */
1174 int num_new = 0; /* number of new messages added to the mailbox */
1175 bool flags_changed = false; /* message flags were changed in the mailbox */
1176 struct HashTable *fnames = NULL; /* hash table for quickly looking up the base filename
1177 for a maildir message */
1179
1180 /* XXX seems like this check belongs in mx_mbox_check() rather than here. */
1181 const bool c_check_new = cs_subset_bool(NeoMutt->sub, "check_new");
1182 if (!c_check_new)
1183 return MX_STATUS_OK;
1184
1185 struct Buffer *buf = mutt_buffer_pool_get();
1186 mutt_buffer_printf(buf, "%s/new", mailbox_path(m));
1187 if (stat(mutt_buffer_string(buf), &st_new) == -1)
1188 {
1190 return MX_STATUS_ERROR;
1191 }
1192
1193 mutt_buffer_printf(buf, "%s/cur", mailbox_path(m));
1194 if (stat(mutt_buffer_string(buf), &st_cur) == -1)
1195 {
1197 return MX_STATUS_ERROR;
1198 }
1199
1200 /* determine which subdirectories need to be scanned */
1202 changed = MMC_NEW_DIR;
1204 changed |= MMC_CUR_DIR;
1205
1206 if (changed == MMC_NO_DIRS)
1207 {
1209 return MX_STATUS_OK; /* nothing to do */
1210 }
1211
1212 /* Update the modification times on the mailbox.
1213 *
1214 * The monitor code notices changes in the open mailbox too quickly.
1215 * In practice, this sometimes leads to all the new messages not being
1216 * noticed during the SAME group of mtime stat updates. To work around
1217 * the problem, don't update the stat times for a monitor caused check. */
1218#ifdef USE_INOTIFY
1220 MonitorContextChanged = false;
1221 else
1222#endif
1223 {
1226 }
1227
1228 /* do a fast scan of just the filenames in
1229 * the subdirectories that have changed. */
1230 struct MdEmailArray mda = ARRAY_HEAD_INITIALIZER;
1231 if (changed & MMC_NEW_DIR)
1232 maildir_parse_dir(m, &mda, "new", NULL);
1233 if (changed & MMC_CUR_DIR)
1234 maildir_parse_dir(m, &mda, "cur", NULL);
1235
1236 /* we create a hash table keyed off the canonical (sans flags) filename
1237 * of each message we scanned. This is used in the loop over the
1238 * existing messages below to do some correlation. */
1240
1241 struct MdEmail *md = NULL;
1242 struct MdEmail **mdp = NULL;
1243 ARRAY_FOREACH(mdp, &mda)
1244 {
1245 md = *mdp;
1248 mutt_hash_insert(fnames, md->canon_fname, md);
1249 }
1250
1251 /* check for modifications and adjust flags */
1252 for (int i = 0; i < m->msg_count; i++)
1253 {
1254 struct Email *e = m->emails[i];
1255 if (!e)
1256 break;
1257
1258 e->active = false;
1260 md = mutt_hash_find(fnames, mutt_buffer_string(buf));
1261 if (md && md->email)
1262 {
1263 /* message already exists, merge flags */
1264 e->active = true;
1265
1266 /* check to see if the message has moved to a different
1267 * subdirectory. If so, update the associated filename. */
1268 if (!mutt_str_equal(e->path, md->email->path))
1269 mutt_str_replace(&e->path, md->email->path);
1270
1271 /* if the user hasn't modified the flags on this message, update
1272 * the flags we just detected. */
1273 if (!e->changed)
1274 if (maildir_update_flags(m, e, md->email))
1275 flags_changed = true;
1276
1277 if (e->deleted == e->trash)
1278 {
1279 if (e->deleted != md->email->deleted)
1280 {
1281 e->deleted = md->email->deleted;
1282 flags_changed = true;
1283 }
1284 }
1285 e->trash = md->email->trash;
1286
1287 /* this is a duplicate of an existing email, so remove it */
1288 email_free(&md->email);
1289 }
1290 /* This message was not in the list of messages we just scanned.
1291 * Check to see if we have enough information to know if the
1292 * message has disappeared out from underneath us. */
1293 else if (((changed & MMC_NEW_DIR) && mutt_strn_equal(e->path, "new/", 4)) ||
1294 ((changed & MMC_CUR_DIR) && mutt_strn_equal(e->path, "cur/", 4)))
1295 {
1296 /* This message disappeared, so we need to simulate a "reopen"
1297 * event. We know it disappeared because we just scanned the
1298 * subdirectory it used to reside in. */
1299 occult = true;
1300 e->deleted = true;
1301 e->purge = true;
1302 }
1303 else
1304 {
1305 /* This message resides in a subdirectory which was not
1306 * modified, so we assume that it is still present and
1307 * unchanged. */
1308 e->active = true;
1309 }
1310 }
1311
1312 /* destroy the file name hash */
1313 mutt_hash_free(&fnames);
1314
1315 /* If we didn't just get new mail, update the tables. */
1316 if (occult)
1318
1319 /* do any delayed parsing we need to do. */
1320 maildir_delayed_parsing(m, &mda, NULL);
1321
1322 /* Incorporate new messages */
1323 num_new = maildir_move_to_mailbox(m, &mda);
1324 if (num_new > 0)
1325 {
1327 m->changed = true;
1328 }
1329
1331
1332 ARRAY_FREE(&mda);
1333 if (occult)
1334 return MX_STATUS_REOPENED;
1335 if (num_new > 0)
1336 return MX_STATUS_NEW_MAIL;
1337 if (flags_changed)
1338 return MX_STATUS_FLAGS;
1339 return MX_STATUS_OK;
1340}
1341
1345static enum MxStatus maildir_mbox_check_stats(struct Mailbox *m, uint8_t flags)
1346{
1347 bool check_stats = flags & MUTT_MAILBOX_CHECK_FORCE_STATS;
1348 bool check_new = true;
1349
1350 if (check_stats)
1351 {
1352 m->msg_count = 0;
1353 m->msg_unread = 0;
1354 m->msg_flagged = 0;
1355 m->msg_new = 0;
1356 }
1357
1358 maildir_check_dir(m, "new", check_new, check_stats);
1359
1360 const bool c_maildir_check_cur = cs_subset_bool(NeoMutt->sub, "maildir_check_cur");
1361 check_new = !m->has_new && c_maildir_check_cur;
1362 if (check_new || check_stats)
1363 maildir_check_dir(m, "cur", check_new, check_stats);
1364
1366}
1367
1375{
1376 enum MxStatus check = maildir_mbox_check(m);
1377 if (check == MX_STATUS_ERROR)
1378 return check;
1379
1380 struct HeaderCache *hc = NULL;
1381#ifdef USE_HCACHE
1382 const char *const c_header_cache = cs_subset_path(NeoMutt->sub, "header_cache");
1383 if (m->type == MUTT_MAILDIR)
1384 hc = mutt_hcache_open(c_header_cache, mailbox_path(m), NULL);
1385#endif
1386
1387 struct Progress *progress = NULL;
1388 if (m->verbose)
1389 {
1390 char msg[PATH_MAX] = { 0 };
1391 snprintf(msg, sizeof(msg), _("Writing %s..."), mailbox_path(m));
1392 progress = progress_new(msg, MUTT_PROGRESS_WRITE, m->msg_count);
1393 }
1394
1395 for (int i = 0; i < m->msg_count; i++)
1396 {
1397 if (m->verbose)
1398 progress_update(progress, i, -1);
1399
1400 if (!maildir_sync_mailbox_message(m, i, hc))
1401 {
1402 progress_free(&progress);
1403 goto err;
1404 }
1405 }
1406 progress_free(&progress);
1407
1408#ifdef USE_HCACHE
1409 if (m->type == MUTT_MAILDIR)
1411#endif
1412
1413 /* XXX race condition? */
1414
1416
1417 /* adjust indices */
1418
1419 if (m->msg_deleted)
1420 {
1421 for (int i = 0, j = 0; i < m->msg_count; i++)
1422 {
1423 struct Email *e = m->emails[i];
1424 if (!e)
1425 break;
1426
1427 const bool c_maildir_trash = cs_subset_bool(NeoMutt->sub, "maildir_trash");
1428 if (!e->deleted || c_maildir_trash)
1429 e->index = j++;
1430 }
1431 }
1432
1433 return check;
1434
1435err:
1436#ifdef USE_HCACHE
1437 if (m->type == MUTT_MAILDIR)
1439#endif
1440 return MX_STATUS_ERROR;
1441}
1442
1448{
1449 return MX_STATUS_OK;
1450}
1451
1455static bool maildir_msg_open(struct Mailbox *m, struct Message *msg, int msgno)
1456{
1457 struct Email *e = m->emails[msgno];
1458 if (!e)
1459 return false;
1460
1461 char path[PATH_MAX] = { 0 };
1462
1463 snprintf(path, sizeof(path), "%s/%s", mailbox_path(m), e->path);
1464
1465 msg->fp = fopen(path, "r");
1466 if (!msg->fp && (errno == ENOENT))
1467 msg->fp = maildir_open_find_message(mailbox_path(m), e->path, NULL);
1468
1469 if (!msg->fp)
1470 {
1472 mutt_debug(LL_DEBUG1, "fopen: %s: %s (errno %d)\n", path, strerror(errno), errno);
1473 return false;
1474 }
1475
1476 return true;
1477}
1478
1487bool maildir_msg_open_new(struct Mailbox *m, struct Message *msg, const struct Email *e)
1488{
1489 int fd;
1490 char path[PATH_MAX] = { 0 };
1491 char suffix[16] = { 0 };
1492 char subdir[16] = { 0 };
1493
1494 if (e)
1495 {
1496 struct Email tmp = *e;
1497 tmp.deleted = false;
1498 tmp.edata = NULL;
1499 maildir_gen_flags(suffix, sizeof(suffix), &tmp);
1500 }
1501 else
1502 *suffix = '\0';
1503
1504 if (e && (e->read || e->old))
1505 mutt_str_copy(subdir, "cur", sizeof(subdir));
1506 else
1507 mutt_str_copy(subdir, "new", sizeof(subdir));
1508
1509 mode_t omask = umask(mh_umask(m));
1510 while (true)
1511 {
1512 snprintf(path, sizeof(path), "%s/tmp/%s.%lld.R%" PRIu64 ".%s%s",
1513 mailbox_path(m), subdir, (long long) mutt_date_epoch(),
1514 mutt_rand64(), NONULL(ShortHostname), suffix);
1515
1516 mutt_debug(LL_DEBUG2, "Trying %s\n", path);
1517
1518 fd = open(path, O_WRONLY | O_EXCL | O_CREAT, 0666);
1519 if (fd == -1)
1520 {
1521 if (errno != EEXIST)
1522 {
1523 umask(omask);
1525 return false;
1526 }
1527 }
1528 else
1529 {
1530 mutt_debug(LL_DEBUG2, "Success\n");
1531 msg->path = mutt_str_dup(path);
1532 break;
1533 }
1534 }
1535 umask(omask);
1536
1537 msg->fp = fdopen(fd, "w");
1538 if (!msg->fp)
1539 {
1540 FREE(&msg->path);
1541 close(fd);
1542 unlink(path);
1543 return false;
1544 }
1545
1546 return true;
1547}
1548
1552static int maildir_msg_commit(struct Mailbox *m, struct Message *msg)
1553{
1554 return maildir_commit_message(m, msg, NULL);
1555}
1556
1562int maildir_msg_close(struct Mailbox *m, struct Message *msg)
1563{
1564 return mutt_file_fclose(&msg->fp);
1565}
1566
1570static int maildir_msg_save_hcache(struct Mailbox *m, struct Email *e)
1571{
1572 int rc = 0;
1573#ifdef USE_HCACHE
1574 const char *const c_header_cache = cs_subset_path(NeoMutt->sub, "header_cache");
1575 struct HeaderCache *hc = mutt_hcache_open(c_header_cache, mailbox_path(m), NULL);
1576 char *key = e->path + 3;
1577 int keylen = maildir_hcache_keylen(key);
1578 rc = mutt_hcache_store(hc, key, keylen, e, 0);
1580#endif
1581 return rc;
1582}
1583
1587int maildir_path_canon(char *buf, size_t buflen)
1588{
1589 mutt_path_canon(buf, buflen, HomeDir, true);
1590 return 0;
1591}
1592
1596int maildir_path_parent(char *buf, size_t buflen)
1597{
1598 if (mutt_path_parent(buf))
1599 return 0;
1600
1601 if (buf[0] == '~')
1602 mutt_path_canon(buf, buflen, HomeDir, true);
1603
1604 if (mutt_path_parent(buf))
1605 return 0;
1606
1607 return -1;
1608}
1609
1613int maildir_path_pretty(char *buf, size_t buflen, const char *folder)
1614{
1615 if (mutt_path_abbr_folder(buf, folder))
1616 return 0;
1617
1618 if (mutt_path_pretty(buf, buflen, HomeDir, false))
1619 return 0;
1620
1621 return -1;
1622}
1623
1627static enum MailboxType maildir_path_probe(const char *path, const struct stat *st)
1628{
1629 if (!st || !S_ISDIR(st->st_mode))
1630 return MUTT_UNKNOWN;
1631
1632 char cur[PATH_MAX] = { 0 };
1633 snprintf(cur, sizeof(cur), "%s/cur", path);
1634
1635 struct stat st_cur = { 0 };
1636 if ((stat(cur, &st_cur) == 0) && S_ISDIR(st_cur.st_mode))
1637 return MUTT_MAILDIR;
1638
1639 return MUTT_UNKNOWN;
1640}
1641
1646 // clang-format off
1647 .type = MUTT_MAILDIR,
1648 .name = "maildir",
1649 .is_local = true,
1650 .ac_owns_path = maildir_ac_owns_path,
1651 .ac_add = maildir_ac_add,
1652 .mbox_open = maildir_mbox_open,
1653 .mbox_open_append = maildir_mbox_open_append,
1654 .mbox_check = maildir_mbox_check,
1655 .mbox_check_stats = maildir_mbox_check_stats,
1656 .mbox_sync = maildir_mbox_sync,
1657 .mbox_close = maildir_mbox_close,
1658 .msg_open = maildir_msg_open,
1659 .msg_open_new = maildir_msg_open_new,
1660 .msg_commit = maildir_msg_commit,
1661 .msg_close = maildir_msg_close,
1662 .msg_padding_size = NULL,
1663 .msg_save_hcache = maildir_msg_save_hcache,
1664 .tags_edit = NULL,
1665 .tags_commit = NULL,
1666 .path_probe = maildir_path_probe,
1667 .path_canon = maildir_path_canon,
1668 .path_pretty = maildir_path_pretty,
1669 .path_parent = maildir_path_parent,
1670 .path_is_empty = maildir_check_empty,
1671 // clang-format on
1672};
#define ARRAY_SORT(head, fn)
Sort an array.
Definition: array.h:277
#define ARRAY_ADD(head, elem)
Add an element at the end of the array.
Definition: array.h:155
#define ARRAY_FOREACH(elem, head)
Iterate over all elements of the array.
Definition: array.h:211
#define ARRAY_SIZE(head)
The number of elements stored.
Definition: array.h:86
#define ARRAY_FREE(head)
Release all memory.
Definition: array.h:203
#define ARRAY_HEAD_INITIALIZER
Static initializer for arrays.
Definition: array.h:57
size_t mutt_buffer_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition: buffer.c:310
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:158
char * mutt_buffer_strdup(const struct Buffer *buf)
Copy a Buffer's string.
Definition: buffer.c:430
static const char * mutt_buffer_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:77
const char * cs_subset_path(const struct ConfigSubset *sub, const char *name)
Get a path config item by name.
Definition: helpers.c:194
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:73
Convenience wrapper for the config headers.
char * HomeDir
User's home directory.
Definition: mutt_globals.h:49
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:864
Duplicate the structure of an entire email.
#define MUTT_CM_UPDATE
Update structs on sync.
Definition: copy.h:40
#define CH_UPDATE
Update the status and x-status fields?
Definition: copy.h:52
#define CH_UPDATE_LEN
Update Lines: and Content-Length:
Definition: copy.h:62
Convenience wrapper for the core headers.
time_t mutt_date_epoch(void)
Return the number of seconds since the Unix epoch.
Definition: date.c:427
void mutt_body_free(struct Body **ptr)
Free a Body.
Definition: body.c:57
struct Email * email_new(void)
Create a new Email.
Definition: email.c:78
void email_free(struct Email **ptr)
Free an Email.
Definition: email.c:44
Structs that make up an email.
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:1611
int mutt_file_safe_rename(const char *src, const char *target)
NFS-safe renaming of files.
Definition: file.c:344
int mutt_file_fclose(FILE **fp)
Close a FILE handle (and NULL the pointer)
Definition: file.c:152
int mutt_file_mkdir(const char *path, mode_t mode)
Recursively create directories.
Definition: file.c:930
long mutt_file_get_size_fp(FILE *fp)
Get the size of a file.
Definition: file.c:1569
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:1651
int mutt_file_fsync_close(FILE **fp)
Flush the data, before closing a file (and NULL the pointer)
Definition: file.c:168
@ MUTT_STAT_CTIME
File/dir's ctime - creation time.
Definition: file.h:64
@ MUTT_STAT_MTIME
File/dir's mtime - last modified time.
Definition: file.h:63
#define mutt_debug(LEVEL,...)
Definition: logging.h:84
#define mutt_perror(...)
Definition: logging.h:88
bool maildir_ac_add(struct Account *a, struct Mailbox *m)
Add a Mailbox to an Account - Implements MxOps::ac_add() -.
Definition: maildir.c:1086
bool maildir_ac_owns_path(struct Account *a, const char *path)
Check whether an Account own a Mailbox path - Implements MxOps::ac_owns_path() -.
Definition: maildir.c:1078
struct MxOps MxMaildirOps
Maildir Mailbox - Implements MxOps -.
Definition: maildir.c:1645
static enum MxStatus maildir_mbox_check_stats(struct Mailbox *m, uint8_t flags)
Check the Mailbox statistics - Implements MxOps::mbox_check_stats() -.
Definition: maildir.c:1345
enum MxStatus maildir_mbox_check(struct Mailbox *m)
Check for new mail - Implements MxOps::mbox_check() -.
Definition: maildir.c:1168
enum MxStatus maildir_mbox_close(struct Mailbox *m)
Close a Mailbox - Implements MxOps::mbox_close() -.
Definition: maildir.c:1447
static bool maildir_mbox_open_append(struct Mailbox *m, OpenMailboxFlags flags)
Open a Mailbox for appending - Implements MxOps::mbox_open_append() -.
Definition: maildir.c:1107
static enum MxOpenReturns maildir_mbox_open(struct Mailbox *m)
Open a Mailbox - Implements MxOps::mbox_open() -.
Definition: maildir.c:1094
enum MxStatus maildir_mbox_sync(struct Mailbox *m)
Save changes to the Mailbox - Implements MxOps::mbox_sync() -.
Definition: maildir.c:1374
int maildir_msg_close(struct Mailbox *m, struct Message *msg)
Close an email - Implements MxOps::msg_close() -.
Definition: maildir.c:1562
static int maildir_msg_commit(struct Mailbox *m, struct Message *msg)
Save changes to an email - Implements MxOps::msg_commit() -.
Definition: maildir.c:1552
bool maildir_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: maildir.c:1487
static bool maildir_msg_open(struct Mailbox *m, struct Message *msg, int msgno)
Open an email message in a Mailbox - Implements MxOps::msg_open() -.
Definition: maildir.c:1455
static int maildir_msg_save_hcache(struct Mailbox *m, struct Email *e)
Save message to the header cache - Implements MxOps::msg_save_hcache() -.
Definition: maildir.c:1570
int maildir_path_canon(char *buf, size_t buflen)
Canonicalise a Mailbox path - Implements MxOps::path_canon() -.
Definition: maildir.c:1587
int maildir_path_parent(char *buf, size_t buflen)
Find the parent of a Mailbox path - Implements MxOps::path_parent() -.
Definition: maildir.c:1596
int maildir_path_pretty(char *buf, size_t buflen, const char *folder)
Abbreviate a Mailbox path - Implements MxOps::path_pretty() -.
Definition: maildir.c:1613
static enum MailboxType maildir_path_probe(const char *path, const struct stat *st)
Is this a Maildir Mailbox? - Implements MxOps::path_probe() -.
Definition: maildir.c:1627
static int maildir_cmp_inode(const void *a, const void *b)
Compare two Maildirs by inode number - Implements sort_t -.
Definition: maildir.c:495
struct HashElem * mutt_hash_insert(struct HashTable *table, const char *strkey, void *data)
Add a new element to the Hash Table (with string keys)
Definition: hash.c:335
void * mutt_hash_find(const struct HashTable *table, const char *strkey)
Find the HashElem data in a Hash Table element using a key.
Definition: hash.c:362
struct HashTable * mutt_hash_new(size_t num_elems, HashFlags flags)
Create a new Hash Table (with string keys)
Definition: hash.c:259
void mutt_hash_free(struct HashTable **ptr)
Free a hash table.
Definition: hash.c:457
#define MUTT_HASH_NO_FLAGS
No flags are set.
Definition: hash.h:109
Header cache multiplexor.
int mutt_hcache_store(struct HeaderCache *hc, const char *key, size_t keylen, struct Email *e, uint32_t uidvalidity)
Multiplexor for StoreOps::store.
Definition: hcache.c:552
void mutt_hcache_close(struct HeaderCache *hc)
Multiplexor for StoreOps::close.
Definition: hcache.c:432
struct HeaderCache * mutt_hcache_open(const char *path, const char *folder, hcache_namer_t namer)
Multiplexor for StoreOps::open.
Definition: hcache.c:332
int mutt_hcache_delete_record(struct HeaderCache *hc, const char *key, size_t keylen)
Multiplexor for StoreOps::delete_record.
Definition: hcache.c:631
static struct Email * restore(const unsigned char *d)
Restore an Email from data retrieved from the cache.
Definition: hcache.c:143
struct HCacheEntry mutt_hcache_fetch(struct HeaderCache *hc, const char *key, size_t keylen, uint32_t uidvalidity)
Multiplexor for StoreOps::fetch.
Definition: hcache.c:456
@ LL_DEBUG2
Log at debug level 2.
Definition: logging.h:41
@ LL_DEBUG1
Log at debug level 1.
Definition: logging.h:40
void mailbox_changed(struct Mailbox *m, enum NotifyMailbox action)
Notify observers of a change to a Mailbox.
Definition: mailbox.c:222
@ NT_MAILBOX_RESORT
Email list needs resorting.
Definition: mailbox.h:178
@ NT_MAILBOX_INVALID
Email list was changed.
Definition: mailbox.h:177
static const char * mailbox_path(const struct Mailbox *m)
Get the Mailbox's path string.
Definition: mailbox.h:211
MailboxType
Supported mailbox formats.
Definition: mailbox.h:41
@ MUTT_NOTMUCH
'Notmuch' (virtual) Mailbox type
Definition: mailbox.h:51
@ MUTT_UNKNOWN
Mailbox wasn't recognised.
Definition: mailbox.h:44
@ MUTT_MAILDIR
'Maildir' Mailbox type
Definition: mailbox.h:48
struct MaildirEmailData * maildir_edata_get(struct Email *e)
Get the private data for this Email.
Definition: edata.c:64
void maildir_edata_free(void **ptr)
Free the private Email data - Implements Email::edata_free()
Definition: edata.c:38
struct MaildirEmailData * maildir_edata_new(void)
Create a new MaildirEmailData object.
Definition: edata.c:53
bool maildir_update_flags(struct Mailbox *m, struct Email *e_old, struct Email *e_new)
Update the mailbox flags.
Definition: shared.c:122
void maildir_mdata_free(void **ptr)
Free the private Mailbox data - Implements Mailbox::mdata_free()
Definition: mdata.c:37
struct MaildirMboxData * maildir_mdata_get(struct Mailbox *m)
Get the private data for this Mailbox.
Definition: mdata.c:58
struct MaildirMboxData * maildir_mdata_new(void)
Create a new MaildirMboxData object.
Definition: mdata.c:47
mode_t mh_umask(struct Mailbox *m)
Create a umask from the mailbox directory.
Definition: shared.c:52
int maildir_move_to_mailbox(struct Mailbox *m, struct MdEmailArray *mda)
Copy the Maildir list to the Mailbox.
Definition: shared.c:75
size_t maildir_hcache_keylen(const char *fn)
Calculate the length of the Maildir path.
Definition: maildir.c:585
static int ch_compare(const void *a, const void *b)
qsort() callback to sort characters
Definition: maildir.c:175
int maildir_parse_dir(struct Mailbox *m, struct MdEmailArray *mda, const char *subdir, struct Progress *progress)
Read a Maildir mailbox.
Definition: maildir.c:513
void maildir_update_mtime(struct Mailbox *m)
Update our record of the Maildir modification time.
Definition: maildir.c:477
int maildir_sync_message(struct Mailbox *m, int msgno)
Sync an email to a Maildir folder.
Definition: maildir.c:387
int maildir_rewrite_message(struct Mailbox *m, int msgno)
Sync a message in an MH folder.
Definition: maildir.c:330
FILE * maildir_open_find_message(const char *folder, const char *msg, char **newname)
Find a new.
Definition: maildir.c:995
int maildir_commit_message(struct Mailbox *m, struct Message *msg, struct Email *e)
Commit a message to a maildir folder.
Definition: maildir.c:237
#define MMC_CUR_DIR
'cur' directory changed
Definition: maildir.c:75
struct Email * maildir_parse_message(enum MailboxType type, const char *fname, bool is_old, struct Email *e)
Actually parse a maildir message.
Definition: maildir.c:928
int maildir_read_dir(struct Mailbox *m, const char *subdir)
Read a Maildir style mailbox.
Definition: maildir.c:671
static FILE * maildir_open_find_message_dir(const char *folder, const char *unique, const char *subfolder, char **newname)
Find a message in a maildir folder.
Definition: maildir.c:755
#define MMC_NO_DIRS
No directories changed.
Definition: maildir.c:73
static void maildir_check_dir(struct Mailbox *m, const char *dir_name, bool check_new, bool check_stats)
Check for new mail / mail counts.
Definition: maildir.c:86
#define MMC_NEW_DIR
'new' directory changed
Definition: maildir.c:74
int maildir_check_empty(const char *path)
Is the mailbox empty.
Definition: maildir.c:1042
void maildir_gen_flags(char *dest, size_t destlen, struct Email *e)
Generate the Maildir flags for an email.
Definition: maildir.c:186
struct Email * maildir_parse_stream(enum MailboxType type, FILE *fp, const char *fname, bool is_old, struct Email *e)
Parse a Maildir message.
Definition: maildir.c:881
void maildir_delayed_parsing(struct Mailbox *m, struct MdEmailArray *mda, struct Progress *progress)
This function does the second parsing pass.
Definition: maildir.c:597
void maildir_canon_filename(struct Buffer *dest, const char *src)
Generate the canonical filename for a Maildir folder.
Definition: maildir.c:725
bool maildir_sync_mailbox_message(struct Mailbox *m, int msgno, struct HeaderCache *hc)
Save changes to the mailbox.
Definition: maildir.c:948
void maildir_parse_flags(struct Email *e, const char *path)
Parse Maildir file flags.
Definition: maildir.c:809
struct MdEmail * maildir_entry_new(void)
Create a new Maildir entry.
Definition: mdemail.c:39
Maildir Email helper.
#define FREE(x)
Definition: memory.h:43
bool MonitorContextChanged
true after the current mailbox has changed
Definition: monitor.c:51
Monitor files for changes.
Convenience wrapper for the library headers.
#define _(a)
Definition: message.h:28
bool mutt_path_abbr_folder(char *buf, const char *folder)
Create a folder abbreviation.
Definition: path.c:490
bool mutt_path_pretty(char *buf, size_t buflen, const char *homedir, bool is_dir)
Tidy a filesystem path.
Definition: path.c:186
bool mutt_path_canon(char *buf, size_t buflen, const char *homedir, bool is_dir)
Create the canonical version of a path.
Definition: path.c:285
bool mutt_path_parent(char *buf)
Find the parent of a path.
Definition: path.c:458
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:250
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:784
bool mutt_strn_equal(const char *a, const char *b, size_t num)
Check for equality of two strings (to a maximum), safely.
Definition: string.c:473
size_t mutt_str_startswith(const char *str, const char *prefix)
Check whether a string starts with a prefix.
Definition: string.c:227
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:544
size_t mutt_str_copy(char *dest, const char *src, size_t dsize)
Copy a string into a buffer (guaranteeing NUL-termination)
Definition: string.c:629
char * mutt_str_replace(char **p, const char *s)
Replace one string with another.
Definition: string.c:326
#define PATH_MAX
Definition: mutt.h:40
Hundreds of global variables to back the user variables.
char * ShortHostname
Short version of the hostname.
Definition: mutt_globals.h:50
SIG_ATOMIC_VOLATILE_T SigInt
true after SIGINT is received
Definition: mutt_globals.h:69
int mx_msg_close(struct Mailbox *m, struct Message **msg)
Close a message.
Definition: mx.c:1193
struct Message * mx_msg_open(struct Mailbox *m, int msgno)
Return a stream pointer for a message.
Definition: mx.c:1147
struct Message * mx_msg_open_new(struct Mailbox *m, const struct Email *e, MsgOpenFlags flags)
Open a new message.
Definition: mx.c:1057
API for mailboxes.
#define MUTT_MSG_NO_FLAGS
No flags are set.
Definition: mx.h:42
uint8_t OpenMailboxFlags
Flags for mutt_open_mailbox(), e.g. MUTT_NOSORT.
Definition: mxapi.h:60
#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:66
#define MUTT_APPEND
Open mailbox for appending messages.
Definition: mxapi.h:63
MxOpenReturns
Return values for mbox_open()
Definition: mxapi.h:97
@ MX_OPEN_ERROR
Open failed with an error.
Definition: mxapi.h:99
@ MX_OPEN_OK
Open succeeded.
Definition: mxapi.h:98
#define MUTT_MAILBOX_CHECK_FORCE_STATS
Ignore MailboxType and calculate statistics.
Definition: mxapi.h:76
#define MUTT_APPENDNEW
Set in mx_open_mailbox_append if the mailbox doesn't exist.
Definition: mxapi.h:70
MxStatus
Return values from mbox_check(), mbox_check_stats(), mbox_snc(), and mbox_close()
Definition: mxapi.h:84
@ MX_STATUS_ERROR
An error occurred.
Definition: mxapi.h:85
@ MX_STATUS_OK
No changes.
Definition: mxapi.h:86
@ MX_STATUS_FLAGS
Nondestructive flags change (IMAP)
Definition: mxapi.h:90
@ MX_STATUS_REOPENED
Mailbox was reopened.
Definition: mxapi.h:89
@ MX_STATUS_NEW_MAIL
New mail received in Mailbox.
Definition: mxapi.h:87
Notmuch virtual mailbox type.
int nm_update_filename(struct Mailbox *m, const char *old_file, const char *new_file, struct Email *e)
Change the filename.
Definition: notmuch.c:1736
Notmuch-specific Mailbox data.
struct Envelope * mutt_rfc822_read_header(FILE *fp, struct Email *e, bool user_hdrs, bool weed)
Parses an RFC822 header.
Definition: parse.c:1158
void mutt_buffer_pool_release(struct Buffer **pbuf)
Free a Buffer from the pool.
Definition: pool.c:112
struct Buffer * mutt_buffer_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:101
Pop-specific Email data.
Progress bar.
@ MUTT_PROGRESS_READ
Progress tracks elements, according to $read_inc
Definition: lib.h:49
@ MUTT_PROGRESS_WRITE
Progress tracks elements, according to $write_inc
Definition: lib.h:50
void progress_free(struct Progress **ptr)
Free a Progress Bar.
Definition: progress.c:86
bool progress_update(struct Progress *progress, size_t pos, int percent)
Update the state of the progress bar.
Definition: progress.c:73
struct Progress * progress_new(const char *msg, enum ProgressType type, size_t size)
Create a new Progress Bar.
Definition: progress.c:118
uint64_t mutt_rand64(void)
Create a 64-bit random number.
Definition: random.c:129
GUI display the mailboxes in a side panel.
Key value store.
#define NONULL(x)
Definition: string2.h:37
A group of associated Mailboxes.
Definition: account.h:37
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
String manipulation buffer.
Definition: buffer.h:34
char * dptr
Current read/write position.
Definition: buffer.h:36
char * data
Pointer to data.
Definition: buffer.h:35
The envelope/body of an email.
Definition: email.h:37
bool read
Email is read.
Definition: email.h:48
bool purge
Skip trash folder when deleting.
Definition: email.h:77
struct Envelope * env
Envelope information.
Definition: email.h:66
void * edata
Driver-specific data.
Definition: email.h:72
int lines
How many lines in the body of this message?
Definition: email.h:60
struct Body * body
List of MIME parts.
Definition: email.h:67
bool active
Message is not to be removed.
Definition: email.h:74
bool old
Email is seen, but unread.
Definition: email.h:47
void(* edata_free)(void **ptr)
Free the private data attached to the Email.
Definition: email.h:87
bool changed
Email has been edited.
Definition: email.h:75
bool attach_del
Has an attachment marked for deletion.
Definition: email.h:99
bool flagged
Marked important?
Definition: email.h:45
time_t date_sent
Time when the message was sent (UTC)
Definition: email.h:58
bool replied
Email has been replied to.
Definition: email.h:49
int msgno
Number displayed to the user.
Definition: email.h:111
char * path
Path of Email (for local Mailboxes)
Definition: email.h:68
bool deleted
Email is deleted.
Definition: email.h:76
int index
The absolute (unsorted) message number.
Definition: email.h:110
bool trash
Message is marked as trashed on disk (used by the maildir_trash option)
Definition: email.h:51
time_t received
Time when the message was placed in the mailbox.
Definition: email.h:59
unsigned char changed
Changed fields, e.g. MUTT_ENV_CHANGED_SUBJECT.
Definition: envelope.h:92
Wrapper for Email retrieved from the header cache.
Definition: lib.h:98
uint32_t uidvalidity
IMAP-specific UIDVALIDITY.
Definition: lib.h:99
struct Email * email
Retrieved email.
Definition: lib.h:101
A Hash Table.
Definition: hash.h:97
Header cache structure.
Definition: lib.h:87
char * folder
Folder name.
Definition: lib.h:88
A mailbox.
Definition: mailbox.h:79
void(* mdata_free)(void **ptr)
Free the private data attached to the Mailbox.
Definition: mailbox.h:143
bool changed
Mailbox has been modified.
Definition: mailbox.h:111
bool has_new
Mailbox has new mail.
Definition: mailbox.h:85
struct timespec mtime
Time Mailbox was last changed.
Definition: mailbox.h:104
int msg_new
Number of new messages.
Definition: mailbox.h:92
int msg_count
Total number of messages.
Definition: mailbox.h:88
enum MailboxType type
Mailbox type.
Definition: mailbox.h:102
void * mdata
Driver specific data.
Definition: mailbox.h:133
struct Email ** emails
Array of Emails.
Definition: mailbox.h:96
int msg_deleted
Number of deleted messages.
Definition: mailbox.h:93
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:105
bool verbose
Display status messages?
Definition: mailbox.h:115
int msg_unread
Number of unread messages.
Definition: mailbox.h:89
Maildir-specific Email data -.
Definition: edata.h:34
Maildir-specific Mailbox data -.
Definition: mdata.h:35
mode_t mh_umask
umask to use when creating files
Definition: mdata.h:37
struct timespec mtime_cur
Timestamp of the 'cur' dir.
Definition: mdata.h:36
A Maildir Email helper.
Definition: mdemail.h:34
bool header_parsed
Definition: mdemail.h:37
char * canon_fname
Definition: mdemail.h:36
struct Email * email
Definition: mdemail.h:35
ino_t inode
Definition: mdemail.h:38
A local copy of an email.
Definition: mxapi.h:43
FILE * fp
pointer to the message data
Definition: mxapi.h:44
char * path
path to temp file
Definition: mxapi.h:45
char * committed_path
the final path generated by mx_msg_commit()
Definition: mxapi.h:46
time_t received
Time at which this message was received.
Definition: mxapi.h:55
Definition: mxapi.h:112
enum MailboxType type
Mailbox type, e.g. MUTT_IMAP.
Definition: mxapi.h:113
Container for Accounts, Notifications.
Definition: neomutt.h:37
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39