2 * Copyright (C) 1996-2002,2007,2009 Michael R. Elkins <me@mutt.org>
3 * Copyright (C) 1999-2005 Thomas Roessler <roessler@does-not-exist.org>
4 * Copyright (C) 2010,2013 Michael R. Elkins <me@mutt.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * This file contains routines specific to MH and ``maildir'' style
38 #include "mutt_curses.h"
45 #include <sys/types.h>
62 #define INS_SORT_THRESHOLD 6
64 static int maildir_check_mailbox (CONTEXT * ctx, int *index_hint);
65 static int mh_check_mailbox (CONTEXT * ctx, int *index_hint);
71 unsigned header_parsed:1;
72 #ifdef HAVE_DIRENT_D_INO
74 #endif /* HAVE_DIRENT_D_INO */
86 struct timespec mtime_cur;
90 /* mh_sequences support */
92 #define MH_SEQ_UNSEEN (1 << 0)
93 #define MH_SEQ_REPLIED (1 << 1)
94 #define MH_SEQ_FLAGGED (1 << 2)
96 static inline struct mh_data *mh_data (CONTEXT *ctx)
98 return (struct mh_data*)ctx->data;
101 static void mhs_alloc (struct mh_sequences *mhs, int i)
106 if (i > mhs->max || !mhs->flags)
109 j = mhs->flags ? mhs->max + 1 : 0;
110 safe_realloc (&mhs->flags, sizeof (mhs->flags[0]) * (newmax + 1));
118 static void mhs_free_sequences (struct mh_sequences *mhs)
123 static short mhs_check (struct mh_sequences *mhs, int i)
125 if (!mhs->flags || i > mhs->max)
128 return mhs->flags[i];
131 static short mhs_set (struct mh_sequences *mhs, int i, short f)
135 return mhs->flags[i];
142 static short mhs_unset (struct mh_sequences *mhs, int i, short f)
146 return mhs->flags[i];
151 static int mh_read_token (char *t, int *first, int *last)
154 if ((p = strchr (t, '-')))
157 if (mutt_atoi (t, first) < 0 || mutt_atoi (p, last) < 0)
162 if (mutt_atoi (t, first) < 0)
169 static int mh_read_sequences (struct mh_sequences *mhs, const char *path)
178 int first, last, rc = 0;
180 BUFFER *pathname = mutt_buffer_pool_get ();
181 mutt_buffer_printf (pathname, "%s/.mh_sequences", path);
183 if (!(fp = fopen (mutt_b2s (pathname), "r")))
184 goto out; /* yes, ask callers to silently ignore the error */
186 while ((buff = mutt_read_line (buff, &sz, fp, &line, 0)))
188 if (!(t = strtok (buff, " \t:")))
191 if (!mutt_strcmp (t, MhUnseen))
193 else if (!mutt_strcmp (t, MhFlagged))
195 else if (!mutt_strcmp (t, MhReplied))
197 else /* unknown sequence */
200 while ((t = strtok (NULL, " \t:")))
202 if (mh_read_token (t, &first, &last) < 0)
204 mhs_free_sequences (mhs);
208 for (; first <= last; first++)
209 mhs_set (mhs, first, f);
216 mutt_buffer_pool_release (&pathname);
222 static inline mode_t mh_umask (CONTEXT* ctx)
225 struct mh_data* data = mh_data (ctx);
227 if (data && data->mh_umask)
228 return data->mh_umask;
230 if (stat (ctx->path, &st))
232 dprint (1, (debugfile, "stat failed on %s\n", ctx->path));
236 return 0777 & ~st.st_mode;
240 * Returns 1 if the .mh_sequences last modification time is more recent than the last visit to this mailbox
241 * Returns 0 if the modifcation time is older
242 * Returns -1 on error
244 static int mh_sequences_changed(BUFFY *b)
250 path = mutt_buffer_pool_get ();
251 mutt_buffer_printf (path, "%s/.mh_sequences", mutt_b2s (b->pathbuf));
252 if (stat (mutt_b2s (path), &sb) == 0)
253 rc = (mutt_stat_timespec_compare (&sb, MUTT_STAT_MTIME, &b->last_visited) > 0);
254 mutt_buffer_pool_release (&path);
259 * Returns 1 if the modification time on the message file is older than the last visit to this mailbox
260 * Returns 0 if the modtime is newer
261 * Returns -1 on error
263 static int mh_already_notified(BUFFY *b, int msgno)
269 path = mutt_buffer_pool_get ();
270 mutt_buffer_printf (path, "%s/%d", mutt_b2s (b->pathbuf), msgno);
271 if (stat (mutt_b2s (path), &sb) == 0)
272 rc = (mutt_stat_timespec_compare (&sb, MUTT_STAT_MTIME, &b->last_visited) <= 0);
273 mutt_buffer_pool_release (&path);
277 /* Checks new mail for a mh mailbox.
278 * check_stats: if true, also count total, new, and flagged messages.
279 * Returns 1 if the mailbox has new mail.
281 int mh_buffy (BUFFY *mailbox, int check_stats)
284 struct mh_sequences mhs;
290 /* when $mail_check_recent is set and the .mh_sequences file hasn't changed
291 * since the last mailbox visit, there is no "new mail" */
292 if (option(OPTMAILCHECKRECENT) && mh_sequences_changed(mailbox) <= 0)
298 if (! (check_new || check_stats))
301 memset (&mhs, 0, sizeof (mhs));
302 if (mh_read_sequences (&mhs, mutt_b2s (mailbox->pathbuf)) < 0)
307 mailbox->msg_count = 0;
308 mailbox->msg_unread = 0;
309 mailbox->msg_flagged = 0;
312 for (i = mhs.max; i > 0; i--)
315 (mhs_check (&mhs, i) & MH_SEQ_FLAGGED))
316 mailbox->msg_flagged++;
317 if (mhs_check (&mhs, i) & MH_SEQ_UNSEEN)
320 mailbox->msg_unread++;
323 /* if the first unseen message we encounter was in the mailbox during the
324 last visit, don't notify about it */
325 if (!option(OPTMAILCHECKRECENT) || mh_already_notified(mailbox, i) == 0)
330 /* Because we are traversing from high to low, we can stop
331 * checking for new mail after the first unseen message.
332 * Whether it resulted in "new mail" or not. */
339 mhs_free_sequences (&mhs);
343 if ((dirp = opendir (mutt_b2s (mailbox->pathbuf))) != NULL)
345 while ((de = readdir (dirp)) != NULL)
347 if (*de->d_name == '.')
349 if (mh_valid_message (de->d_name))
350 mailbox->msg_count++;
359 static int mh_mkstemp (CONTEXT * dest, FILE ** fp, char **tgt)
366 path = mutt_buffer_pool_get ();
367 omask = umask (mh_umask (dest));
370 mutt_buffer_printf (path, "%s/.mutt-%s-%d-%d",
371 dest->path, NONULL (Hostname), (int) getpid (), Counter++);
372 if ((fd = open (mutt_b2s (path), O_WRONLY | O_EXCL | O_CREAT, 0666)) == -1)
376 mutt_perror (mutt_b2s (path));
384 *tgt = safe_strdup (mutt_b2s (path));
390 if ((*fp = fdopen (fd, "w")) == NULL)
392 FREE (tgt); /* __FREE_CHECKED__ */
394 unlink (mutt_b2s (path));
400 mutt_buffer_pool_release (&path);
405 static void mhs_write_one_sequence (FILE * fp, struct mh_sequences *mhs,
406 short f, const char *tag)
410 fprintf (fp, "%s:", tag);
415 for (i = 0; i <= mhs->max; i++)
417 if ((mhs_check (mhs, i) & f))
427 fprintf (fp, " %d", first);
429 fprintf (fp, " %d-%d", first, last);
439 fprintf (fp, " %d", first);
441 fprintf (fp, " %d-%d", first, last);
447 /* XXX - we don't currently remove deleted messages from sequences we don't know. Should we? */
449 static void mh_update_sequences (CONTEXT * ctx)
453 BUFFER *sequences = NULL;
465 char seq_unseen[STRING];
466 char seq_replied[STRING];
467 char seq_flagged[STRING];
470 struct mh_sequences mhs;
471 memset (&mhs, 0, sizeof (mhs));
473 snprintf (seq_unseen, sizeof (seq_unseen), "%s:", NONULL (MhUnseen));
474 snprintf (seq_replied, sizeof (seq_replied), "%s:", NONULL (MhReplied));
475 snprintf (seq_flagged, sizeof (seq_flagged), "%s:", NONULL (MhFlagged));
477 if (mh_mkstemp (ctx, &nfp, &tmpfname) != 0)
483 sequences = mutt_buffer_pool_get ();
484 mutt_buffer_printf (sequences, "%s/.mh_sequences", ctx->path);
487 /* first, copy unknown sequences */
488 if ((ofp = fopen (mutt_b2s (sequences), "r")))
490 while ((buff = mutt_read_line (buff, &s, ofp, &l, 0)))
492 if (!mutt_strncmp (buff, seq_unseen, mutt_strlen (seq_unseen)))
494 if (!mutt_strncmp (buff, seq_flagged, mutt_strlen (seq_flagged)))
496 if (!mutt_strncmp (buff, seq_replied, mutt_strlen (seq_replied)))
499 fprintf (nfp, "%s\n", buff);
504 /* now, update our unseen, flagged, and replied sequences */
505 for (l = 0; l < ctx->msgcount; l++)
507 if (ctx->hdrs[l]->deleted)
510 if ((p = strrchr (ctx->hdrs[l]->path, '/')))
513 p = ctx->hdrs[l]->path;
515 if (mutt_atoi (p, &i) < 0)
518 if (!ctx->hdrs[l]->read)
520 mhs_set (&mhs, i, MH_SEQ_UNSEEN);
523 if (ctx->hdrs[l]->flagged)
525 mhs_set (&mhs, i, MH_SEQ_FLAGGED);
528 if (ctx->hdrs[l]->replied)
530 mhs_set (&mhs, i, MH_SEQ_REPLIED);
535 /* write out the new sequences */
537 mhs_write_one_sequence (nfp, &mhs, MH_SEQ_UNSEEN, NONULL (MhUnseen));
539 mhs_write_one_sequence (nfp, &mhs, MH_SEQ_FLAGGED, NONULL (MhFlagged));
541 mhs_write_one_sequence (nfp, &mhs, MH_SEQ_REPLIED, NONULL (MhReplied));
543 mhs_free_sequences (&mhs);
546 /* try to commit the changes - no guarantee here */
549 unlink (mutt_b2s (sequences));
550 if (safe_rename (tmpfname, mutt_b2s (sequences)) != 0)
552 /* report an error? */
555 mutt_buffer_pool_release (&sequences);
560 static void mh_sequences_add_one (CONTEXT * ctx, int n, short unseen,
561 short flagged, short replied)
563 short unseen_done = 0;
564 short flagged_done = 0;
565 short replied_done = 0;
567 FILE *ofp = NULL, *nfp = NULL;
570 BUFFER *sequences = NULL;
572 char seq_unseen[STRING];
573 char seq_replied[STRING];
574 char seq_flagged[STRING];
580 if (mh_mkstemp (ctx, &nfp, &tmpfname) == -1)
583 snprintf (seq_unseen, sizeof (seq_unseen), "%s:", NONULL (MhUnseen));
584 snprintf (seq_replied, sizeof (seq_replied), "%s:", NONULL (MhReplied));
585 snprintf (seq_flagged, sizeof (seq_flagged), "%s:", NONULL (MhFlagged));
587 sequences = mutt_buffer_pool_get ();
588 mutt_buffer_printf (sequences, "%s/.mh_sequences", ctx->path);
589 if ((ofp = fopen (mutt_b2s (sequences), "r")))
591 while ((buff = mutt_read_line (buff, &sz, ofp, &line, 0)))
593 if (unseen && !strncmp (buff, seq_unseen, mutt_strlen (seq_unseen)))
595 fprintf (nfp, "%s %d\n", buff, n);
599 && !strncmp (buff, seq_flagged, mutt_strlen (seq_flagged)))
601 fprintf (nfp, "%s %d\n", buff, n);
605 && !strncmp (buff, seq_replied, mutt_strlen (seq_replied)))
607 fprintf (nfp, "%s %d\n", buff, n);
611 fprintf (nfp, "%s\n", buff);
617 if (!unseen_done && unseen)
618 fprintf (nfp, "%s: %d\n", NONULL (MhUnseen), n);
619 if (!flagged_done && flagged)
620 fprintf (nfp, "%s: %d\n", NONULL (MhFlagged), n);
621 if (!replied_done && replied)
622 fprintf (nfp, "%s: %d\n", NONULL (MhReplied), n);
626 unlink (mutt_b2s (sequences));
627 if (safe_rename (tmpfname, mutt_b2s (sequences)) != 0)
629 mutt_buffer_pool_release (&sequences);
634 static void mh_update_maildir (struct maildir *md, struct mh_sequences *mhs)
640 for (; md; md = md->next)
642 if ((p = strrchr (md->h->path, '/')))
647 if (mutt_atoi (p, &i) < 0)
649 f = mhs_check (mhs, i);
651 md->h->read = (f & MH_SEQ_UNSEEN) ? 0 : 1;
652 md->h->flagged = (f & MH_SEQ_FLAGGED) ? 1 : 0;
653 md->h->replied = (f & MH_SEQ_REPLIED) ? 1 : 0;
657 /* maildir support */
659 static void maildir_free_entry (struct maildir **md)
664 FREE (&(*md)->canon_fname);
666 mutt_free_header (&(*md)->h);
668 FREE (md); /* __FREE_CHECKED__ */
671 static void maildir_free_maildir (struct maildir **md)
673 struct maildir *p, *q;
678 for (p = *md; p; p = q)
681 maildir_free_entry (&p);
685 static void maildir_parse_flags (HEADER * h, const char *path)
693 if ((p = strrchr (path, ':')) != NULL && mutt_strncmp (p + 1, "2,", 2) == 0)
697 mutt_str_replace (&h->maildir_flags, p);
698 q = h->maildir_flags;
714 case 'R': /* replied */
719 case 'T': /* trashed */
720 if (!h->flagged || !option(OPTFLAGSAFE))
735 if (q == h->maildir_flags)
736 FREE (&h->maildir_flags);
741 static void maildir_update_mtime (CONTEXT * ctx)
745 struct mh_data *data = mh_data (ctx);
747 buf = mutt_buffer_pool_get ();
749 if (ctx->magic == MUTT_MAILDIR)
751 mutt_buffer_printf (buf, "%s/%s", ctx->path, "cur");
752 if (stat (mutt_b2s (buf), &st) == 0)
753 mutt_get_stat_timespec (&data->mtime_cur, &st, MUTT_STAT_MTIME);
754 mutt_buffer_printf (buf, "%s/%s", ctx->path, "new");
758 mutt_buffer_printf (buf, "%s/.mh_sequences", ctx->path);
759 if (stat (mutt_b2s (buf), &st) == 0)
760 mutt_get_stat_timespec (&data->mtime_cur, &st, MUTT_STAT_MTIME);
762 mutt_buffer_strcpy (buf, ctx->path);
765 if (stat (mutt_b2s (buf), &st) == 0)
766 mutt_get_stat_timespec (&ctx->mtime, &st, MUTT_STAT_MTIME);
768 mutt_buffer_pool_release (&buf);
772 * Actually parse a maildir message. This may also be used to fill
773 * out a fake header structure generated by lazy maildir parsing.
775 static HEADER *maildir_parse_message (int magic, const char *fname,
776 int is_old, HEADER * _h)
782 if ((f = fopen (fname, "r")) != NULL)
785 h = mutt_new_header ();
786 h->env = mutt_read_rfc822_header (f, h, 0, 0);
788 fstat (fileno (f), &st);
792 h->received = h->date_sent;
794 /* always update the length since we have fresh information available. */
795 h->content->length = st.st_size - h->content->offset;
799 if (magic == MUTT_MAILDIR)
802 * maildir stores its flags in the filename, so ignore the
803 * flags in the header of the message
807 maildir_parse_flags (h, fname);
814 /* Ignore the garbage files. A valid MH message consists of only
815 * digits. Deleted message get moved to a filename with a comma before
819 int mh_valid_message (const char *s)
823 if (!isdigit ((unsigned char) *s))
829 static int maildir_parse_dir (CONTEXT * ctx, struct maildir ***last,
830 const char *subdir, int *count,
831 progress_t *progress)
836 int rc = 0, is_old = 0;
837 struct maildir *entry;
840 buf = mutt_buffer_pool_get ();
844 mutt_buffer_printf (buf, "%s/%s", ctx->path, subdir);
845 is_old = (mutt_strcmp ("cur", subdir) == 0);
848 mutt_buffer_strcpy (buf, ctx->path);
850 if ((dirp = opendir (mutt_b2s (buf))) == NULL)
856 while ((de = readdir (dirp)) != NULL)
858 if ((ctx->magic == MUTT_MH && !mh_valid_message (de->d_name))
859 || (ctx->magic == MUTT_MAILDIR && *de->d_name == '.'))
862 /* FOO - really ignore the return value? */
864 (debugfile, "%s:%d: queueing %s\n", __FILE__, __LINE__,
867 h = mutt_new_header ();
869 if (ctx->magic == MUTT_MAILDIR)
870 maildir_parse_flags (h, de->d_name);
875 if (!ctx->quiet && progress)
876 mutt_progress_update (progress, *count, -1);
881 mutt_buffer_printf (buf, "%s/%s", subdir, de->d_name);
882 h->path = safe_strdup (mutt_b2s (buf));
885 h->path = safe_strdup (de->d_name);
887 entry = safe_calloc (sizeof (struct maildir), 1);
889 #ifdef HAVE_DIRENT_D_INO
890 entry->inode = de->d_ino;
891 #endif /* HAVE_DIRENT_D_INO */
893 *last = &entry->next;
899 mutt_buffer_pool_release (&buf);
904 static int maildir_add_to_context (CONTEXT * ctx, struct maildir *md)
906 int oldmsgcount = ctx->msgcount;
911 dprint (2, (debugfile, "%s:%d maildir_add_to_context(): Considering %s\n",
912 __FILE__, __LINE__, NONULL (md->canon_fname)));
918 "%s:%d Adding header structure. Flags: %s%s%s%s%s\n", __FILE__,
919 __LINE__, md->h->flagged ? "f" : "", md->h->deleted ? "D" : "",
920 md->h->replied ? "r" : "", md->h->old ? "O" : "",
921 md->h->read ? "R" : ""));
922 if (ctx->msgcount == ctx->hdrmax)
923 mx_alloc_memory (ctx);
925 ctx->hdrs[ctx->msgcount] = md->h;
926 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
928 md->h->content->length + md->h->content->offset -
929 md->h->content->hdr_offset;
937 if (ctx->msgcount > oldmsgcount)
939 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
945 static int maildir_move_to_context (CONTEXT * ctx, struct maildir **md)
948 r = maildir_add_to_context (ctx, *md);
949 maildir_free_maildir (md);
954 static size_t maildir_hcache_keylen (const char *fn)
956 const char * p = strrchr (fn, ':');
957 return p ? (size_t) (p - fn) : mutt_strlen(fn);
961 #if HAVE_DIRENT_D_INO
962 static int md_cmp_inode (struct maildir *a, struct maildir *b)
964 return a->inode - b->inode;
968 static int md_cmp_path (struct maildir *a, struct maildir *b)
970 return strcmp (a->h->path, b->h->path);
974 * Merge two maildir lists according to the inode numbers.
976 static struct maildir* maildir_merge_lists (struct maildir *left,
977 struct maildir *right,
978 int (*cmp) (struct maildir *,
981 struct maildir* head;
982 struct maildir* tail;
986 if (cmp (left, right) < 0)
1007 while (left && right)
1009 if (cmp (left, right) < 0)
1017 right = right->next;
1034 static struct maildir* maildir_ins_sort (struct maildir* list,
1035 int (*cmp) (struct maildir *,
1038 struct maildir *tmp, *last, *ret = NULL, *back;
1048 for (tmp = ret; tmp && cmp (tmp, list) <= 0; tmp = tmp->next)
1064 * Sort maildir list according to inode.
1066 static struct maildir* maildir_sort (struct maildir* list, size_t len,
1067 int (*cmp) (struct maildir *,
1070 struct maildir* left = list;
1071 struct maildir* right = list;
1074 if (!list || !list->next)
1079 if (len != (size_t)(-1) && len <= INS_SORT_THRESHOLD)
1080 return maildir_ins_sort (list, cmp);
1083 while (list && list->next)
1085 right = right->next;
1086 list = list->next->next;
1091 right = right->next;
1094 left = maildir_sort (left, c, cmp);
1095 right = maildir_sort (right, c, cmp);
1096 return maildir_merge_lists (left, right, cmp);
1099 /* Sorts mailbox into it's natural order.
1100 * Currently only defined for MH where files are numbered.
1102 static void mh_sort_natural (CONTEXT *ctx, struct maildir **md)
1104 if (!ctx || !md || !*md || ctx->magic != MUTT_MH || Sort != SORT_ORDER)
1106 dprint (4, (debugfile, "maildir: sorting %s into natural order\n",
1108 *md = maildir_sort (*md, (size_t) -1, md_cmp_path);
1111 #if HAVE_DIRENT_D_INO
1112 static struct maildir *skip_duplicates (struct maildir *p, struct maildir **last)
1115 * Skip ahead to the next non-duplicate message.
1117 * p should never reach NULL, because we couldn't have reached this point unless
1118 * there was a message that needed to be parsed.
1120 * the check for p->header_parsed is likely unnecessary since the dupes will most
1121 * likely be at the head of the list. but it is present for consistency with
1122 * the check at the top of the for() loop in maildir_delayed_parsing().
1124 while (!p->h || p->header_parsed)
1134 * This function does the second parsing pass
1136 static void maildir_delayed_parsing (CONTEXT * ctx, struct maildir **md,
1137 progress_t *progress)
1139 struct maildir *p, *last = NULL;
1142 #if HAVE_DIRENT_D_INO
1146 header_cache_t *hc = NULL;
1148 struct timeval when;
1149 struct stat lastchanged;
1153 #if HAVE_DIRENT_D_INO
1159 dprint (4, (debugfile, "maildir: need to sort %s by inode\n", ctx->path)); \
1160 p = maildir_sort (p, (size_t) -1, md_cmp_inode); \
1166 p = skip_duplicates (p, &last); \
1167 mutt_buffer_printf (fn, "%s/%s", ctx->path, p->h->path); \
1171 #define DO_SORT() /* nothing */
1175 hc = mutt_hcache_open (HeaderCache, ctx->path, NULL);
1178 fn = mutt_buffer_pool_get ();
1180 for (p = *md, count = 0; p; p = p->next, count++)
1182 if (! (p && p->h && !p->header_parsed))
1188 if (!ctx->quiet && progress)
1189 mutt_progress_update (progress, count, -1);
1193 mutt_buffer_printf (fn, "%s/%s", ctx->path, p->h->path);
1196 if (option(OPTHCACHEVERIFY))
1198 ret = stat(mutt_b2s (fn), &lastchanged);
1202 lastchanged.st_mtime = 0;
1206 if (ctx->magic == MUTT_MH)
1207 data = mutt_hcache_fetch (hc, p->h->path, strlen);
1209 data = mutt_hcache_fetch (hc, p->h->path + 3, &maildir_hcache_keylen);
1211 memcpy (&when, data, sizeof(struct timeval));
1213 if (data != NULL && !ret && lastchanged.st_mtime <= when.tv_sec)
1215 p->h = mutt_hcache_restore ((unsigned char *)data, &p->h);
1216 if (ctx->magic == MUTT_MAILDIR)
1217 maildir_parse_flags (p->h, mutt_b2s (fn));
1221 #endif /* USE_HCACHE */
1223 if (maildir_parse_message (ctx->magic, mutt_b2s (fn), p->h->old, p->h))
1225 p->header_parsed = 1;
1227 if (ctx->magic == MUTT_MH)
1228 mutt_hcache_store (hc, p->h->path, p->h, 0, strlen, MUTT_GENERATE_UIDVALIDITY);
1230 mutt_hcache_store (hc, p->h->path + 3, p->h, 0, &maildir_hcache_keylen, MUTT_GENERATE_UIDVALIDITY);
1234 mutt_free_header (&p->h);
1237 mutt_hcache_free (&data);
1242 mutt_hcache_close (hc);
1245 mutt_buffer_pool_release (&fn);
1249 mh_sort_natural (ctx, md);
1252 static int mh_close_mailbox (CONTEXT *ctx)
1259 /* Read a MH/maildir style mailbox.
1262 * ctx [IN/OUT] context for this mailbox
1263 * subdir [IN] NULL for MH mailboxes, otherwise the subdir of the
1264 * maildir mailbox to read from
1266 static int mh_read_dir (CONTEXT * ctx, const char *subdir)
1269 struct mh_sequences mhs;
1270 struct maildir **last;
1271 struct mh_data *data;
1273 char msgbuf[STRING];
1274 progress_t progress;
1276 memset (&mhs, 0, sizeof (mhs));
1279 snprintf (msgbuf, sizeof (msgbuf), _("Scanning %s..."), ctx->path);
1280 mutt_progress_init (&progress, msgbuf, MUTT_PROGRESS_MSG, ReadInc, 0);
1285 ctx->data = safe_calloc(sizeof (struct mh_data), 1);
1287 data = mh_data (ctx);
1289 maildir_update_mtime (ctx);
1294 if (maildir_parse_dir (ctx, &last, subdir, &count, &progress) == -1)
1299 snprintf (msgbuf, sizeof (msgbuf), _("Reading %s..."), ctx->path);
1300 mutt_progress_init (&progress, msgbuf, MUTT_PROGRESS_MSG, ReadInc, count);
1302 maildir_delayed_parsing (ctx, &md, &progress);
1304 if (ctx->magic == MUTT_MH)
1306 if (mh_read_sequences (&mhs, ctx->path) < 0)
1308 maildir_free_maildir (&md);
1311 mh_update_maildir (md, &mhs);
1312 mhs_free_sequences (&mhs);
1315 maildir_move_to_context (ctx, &md);
1317 if (!data->mh_umask)
1318 data->mh_umask = mh_umask (ctx);
1323 /* read a maildir style mailbox */
1324 static int maildir_read_dir (CONTEXT * ctx)
1326 /* maildir looks sort of like MH, except that there are two subdirectories
1327 * of the main folder path from which to read messages
1329 if (mh_read_dir (ctx, "new") == -1 || mh_read_dir (ctx, "cur") == -1)
1335 static int maildir_open_mailbox (CONTEXT *ctx)
1337 return maildir_read_dir (ctx);
1340 static int maildir_open_mailbox_append (CONTEXT *ctx, int flags)
1345 tmp = mutt_buffer_pool_get ();
1347 if (flags & MUTT_APPENDNEW)
1349 if (mkdir (ctx->path, S_IRWXU))
1351 mutt_perror (ctx->path);
1355 mutt_buffer_printf (tmp, "%s/cur", ctx->path);
1356 if (mkdir (mutt_b2s (tmp), S_IRWXU))
1358 mutt_perror (mutt_b2s (tmp));
1363 mutt_buffer_printf (tmp, "%s/new", ctx->path);
1364 if (mkdir (mutt_b2s (tmp), S_IRWXU))
1366 mutt_perror (mutt_b2s (tmp));
1367 mutt_buffer_printf (tmp, "%s/cur", ctx->path);
1368 rmdir (mutt_b2s (tmp));
1373 mutt_buffer_printf (tmp, "%s/tmp", ctx->path);
1374 if (mkdir (mutt_b2s (tmp), S_IRWXU))
1376 mutt_perror (mutt_b2s (tmp));
1377 mutt_buffer_printf (tmp, "%s/cur", ctx->path);
1378 rmdir (mutt_b2s (tmp));
1379 mutt_buffer_printf (tmp, "%s/new", ctx->path);
1380 rmdir (mutt_b2s (tmp));
1389 mutt_buffer_pool_release (&tmp);
1394 static int mh_open_mailbox (CONTEXT *ctx)
1396 return mh_read_dir (ctx, NULL);
1399 static int mh_open_mailbox_append (CONTEXT *ctx, int flags)
1404 if (flags & MUTT_APPENDNEW)
1406 if (mkdir (ctx->path, S_IRWXU))
1408 mutt_perror (ctx->path);
1412 tmp = mutt_buffer_pool_get ();
1413 mutt_buffer_printf (tmp, "%s/.mh_sequences", ctx->path);
1414 if ((i = creat (mutt_b2s (tmp), S_IRWXU)) == -1)
1416 mutt_perror (mutt_b2s (tmp));
1418 mutt_buffer_pool_release (&tmp);
1422 mutt_buffer_pool_release (&tmp);
1430 * Open a new (temporary) message in an MH folder.
1433 static int mh_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr)
1435 return mh_mkstemp (dest, &msg->fp, &msg->path);
1438 static int ch_compar (const void *a, const void *b)
1440 return (int)( *((const char *) a) - *((const char *) b));
1443 static void maildir_flags (char *dest, size_t destlen, HEADER * hdr)
1448 * The maildir specification requires that all files in the cur
1449 * subdirectory have the :unique string appended, regardless of whether
1450 * or not there are any flags. If .old is set, we know that this message
1451 * will end up in the cur directory, so we include it in the following
1452 * test even though there is no associated flag.
1455 if (hdr && (hdr->flagged || hdr->replied || hdr->read || hdr->deleted || hdr->old || hdr->maildir_flags))
1457 char tmp[LONG_STRING];
1458 snprintf (tmp, sizeof (tmp),
1460 hdr->flagged ? "F" : "",
1461 hdr->replied ? "R" : "",
1462 hdr->read ? "S" : "", hdr->deleted ? "T" : "",
1463 NONULL(hdr->maildir_flags));
1464 if (hdr->maildir_flags)
1465 qsort (tmp, strlen (tmp), 1, ch_compar);
1466 snprintf (dest, destlen, ":2,%s", tmp);
1470 static int maildir_mh_open_message (CONTEXT *ctx, MESSAGE *msg, int msgno,
1473 HEADER *cur = ctx->hdrs[msgno];
1474 BUFFER *path = NULL;
1477 path = mutt_buffer_pool_get ();
1478 mutt_buffer_printf (path, "%s/%s", ctx->path, cur->path);
1480 msg->fp = fopen (mutt_b2s (path), "r");
1481 if (msg->fp == NULL && errno == ENOENT && is_maildir)
1482 msg->fp = maildir_open_find_message (ctx->path, cur->path);
1486 mutt_perror (mutt_b2s (path));
1487 dprint (1, (debugfile, "maildir_mh_open_message: fopen: %s: %s (errno %d).\n",
1488 mutt_b2s (path), strerror (errno), errno));
1492 mutt_buffer_pool_release (&path);
1497 static int maildir_open_message (CONTEXT *ctx, MESSAGE *msg, int msgno)
1499 return maildir_mh_open_message (ctx, msg, msgno, 1);
1502 static int mh_open_message (CONTEXT *ctx, MESSAGE *msg, int msgno)
1504 return maildir_mh_open_message (ctx, msg, msgno, 0);
1507 static int mh_close_message (CONTEXT *ctx, MESSAGE *msg)
1509 return safe_fclose (&msg->fp);
1513 * Open a new (temporary) message in a maildir folder.
1515 * Note that this uses _almost_ the maildir file name format, but
1516 * with a {cur,new} prefix.
1520 static int maildir_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr)
1523 BUFFER *path = NULL;
1528 path = mutt_buffer_pool_get ();
1532 short deleted = hdr->deleted;
1535 maildir_flags (suffix, sizeof (suffix), hdr);
1537 hdr->deleted = deleted;
1542 if (hdr && (hdr->read || hdr->old))
1543 strfcpy (subdir, "cur", sizeof (subdir));
1545 strfcpy (subdir, "new", sizeof (subdir));
1547 omask = umask (mh_umask (dest));
1550 mutt_buffer_printf (path, "%s/tmp/%s.%lld.%u_%d.%s%s",
1551 dest->path, subdir, (long long)time (NULL), (unsigned int)getpid (),
1552 Counter++, NONULL (Hostname), suffix);
1554 dprint (2, (debugfile, "maildir_open_new_message (): Trying %s.\n",
1557 if ((fd = open (mutt_b2s (path), O_WRONLY | O_EXCL | O_CREAT, 0666)) == -1)
1559 if (errno != EEXIST)
1562 mutt_perror (mutt_b2s (path));
1569 dprint (2, (debugfile, "maildir_open_new_message (): Success.\n"));
1570 msg->path = safe_strdup (mutt_b2s (path));
1576 if ((msg->fp = fdopen (fd, "w")) == NULL)
1580 unlink (mutt_b2s (path));
1586 mutt_buffer_pool_release (&path);
1594 * Commit a message to a maildir folder.
1596 * msg->path contains the file name of a file in tmp/. We take the
1597 * flags from this file's name.
1599 * ctx is the mail folder we commit to.
1601 * hdr is a header structure to which we write the message's new
1602 * file name. This is used in the mh and maildir folder synch
1603 * routines. When this routine is invoked from mx_commit_message,
1606 * msg->path looks like this:
1608 * tmp/{cur,new}.mutt-HOSTNAME-PID-COUNTER:flags
1610 * See also maildir_open_new_message().
1614 static int _maildir_commit_message (CONTEXT * ctx, MESSAGE * msg, HEADER * hdr)
1619 BUFFER *path = NULL, *full = NULL;
1622 if (safe_fsync_close (&msg->fp))
1624 mutt_perror (_("Could not flush message to disk"));
1628 /* extract the subdir */
1629 s = strrchr (msg->path, '/') + 1;
1630 strfcpy (subdir, s, 4);
1632 /* extract the flags */
1633 if ((s = strchr (s, ':')))
1634 strfcpy (suffix, s, sizeof (suffix));
1638 /* construct a new file name. */
1639 path = mutt_buffer_pool_get ();
1640 full = mutt_buffer_pool_get ();
1643 mutt_buffer_printf (path, "%s/%lld.%u_%d.%s%s", subdir,
1644 (long long)time (NULL), (unsigned int)getpid (), Counter++,
1645 NONULL (Hostname), suffix);
1646 mutt_buffer_printf (full, "%s/%s", ctx->path, mutt_b2s (path));
1648 dprint (2, (debugfile, "_maildir_commit_message (): renaming %s to %s.\n",
1649 msg->path, mutt_b2s (full)));
1651 if (safe_rename (msg->path, mutt_b2s (full)) == 0)
1654 mutt_str_replace (&hdr->path, mutt_b2s (path));
1658 * Adjust the mtime on the file to match the time at which this
1659 * message was received. Currently this is only set when copying
1660 * messages between mailboxes, so we test to ensure that it is
1667 ut.actime = msg->received;
1668 ut.modtime = msg->received;
1669 if (utime (mutt_b2s (full), &ut))
1671 mutt_perror (_("_maildir_commit_message(): unable to set time on file"));
1678 else if (errno != EEXIST)
1680 mutt_perror (ctx->path);
1687 mutt_buffer_pool_release (&path);
1688 mutt_buffer_pool_release (&full);
1693 static int maildir_commit_message (CONTEXT * ctx, MESSAGE * msg)
1695 return _maildir_commit_message (ctx, msg, NULL);
1699 * commit a message to an MH folder.
1704 static int _mh_commit_message (CONTEXT * ctx, MESSAGE * msg, HEADER * hdr,
1710 unsigned int n, hi = 0;
1711 BUFFER *path = NULL;
1715 if (safe_fsync_close (&msg->fp))
1717 mutt_perror (_("Could not flush message to disk"));
1721 if ((dirp = opendir (ctx->path)) == NULL)
1723 mutt_perror (ctx->path);
1727 /* figure out what the next message number is */
1728 while ((de = readdir (dirp)) != NULL)
1736 if (!isdigit ((unsigned char) *cp))
1750 * Now try to rename the file to the proper name.
1752 * Note: We may have to try multiple times, until we find a free
1756 path = mutt_buffer_pool_get ();
1761 snprintf (tmp, sizeof (tmp), "%d", hi);
1762 mutt_buffer_printf (path, "%s/%s", ctx->path, tmp);
1763 if (safe_rename (msg->path, mutt_b2s (path)) == 0)
1766 mutt_str_replace (&hdr->path, tmp);
1770 else if (errno != EEXIST)
1772 mutt_perror (ctx->path);
1778 mh_sequences_add_one (ctx, hi, !msg->flags.read, msg->flags.flagged,
1779 msg->flags.replied);
1782 mutt_buffer_pool_release (&path);
1787 static int mh_commit_message (CONTEXT * ctx, MESSAGE * msg)
1789 return _mh_commit_message (ctx, msg, NULL, 1);
1793 /* Sync a message in an MH folder.
1795 * This code is also used for attachment deletion in maildir
1799 static int mh_rewrite_message (CONTEXT * ctx, int msgno)
1801 HEADER *h = ctx->hdrs[msgno];
1806 BUFFER *oldpath = NULL;
1807 BUFFER *newpath = NULL;
1808 BUFFER *partpath = NULL;
1810 long old_body_offset = h->content->offset;
1811 long old_body_length = h->content->length;
1812 long old_hdr_lines = h->lines;
1814 if ((dest = mx_open_new_message (ctx, h, 0)) == NULL)
1817 if ((rc = mutt_copy_message (dest->fp, ctx, h,
1818 MUTT_CM_UPDATE, CH_UPDATE | CH_UPDATE_LEN)) == 0)
1820 oldpath = mutt_buffer_pool_get ();
1821 partpath = mutt_buffer_pool_get ();
1823 mutt_buffer_printf (oldpath, "%s/%s", ctx->path, h->path);
1824 mutt_buffer_strcpy (partpath, h->path);
1826 if (ctx->magic == MUTT_MAILDIR)
1827 rc = _maildir_commit_message (ctx, dest, h);
1829 rc = _mh_commit_message (ctx, dest, h, 0);
1831 mx_close_message (ctx, &dest);
1835 unlink (mutt_b2s (oldpath));
1840 * Try to move the new message to the old place.
1843 * This is important when we are just updating flags.
1845 * Note that there is a race condition against programs which
1846 * use the first free slot instead of the maximum message
1847 * number. Mutt does _not_ behave like this.
1849 * Anyway, if this fails, the message is in the folder, so
1850 * all what happens is that a concurrently running mutt will
1851 * lose flag modifications.
1854 if (ctx->magic == MUTT_MH && rc == 0)
1856 newpath = mutt_buffer_pool_get ();
1858 mutt_buffer_printf (newpath, "%s/%s", ctx->path, h->path);
1859 if ((rc = safe_rename (mutt_b2s (newpath), mutt_b2s (oldpath))) == 0)
1860 mutt_str_replace (&h->path, mutt_b2s (partpath));
1862 mutt_buffer_pool_release (&newpath);
1865 mutt_buffer_pool_release (&oldpath);
1866 mutt_buffer_pool_release (&partpath);
1869 mx_close_message (ctx, &dest);
1871 if (rc == -1 && restore)
1873 h->content->offset = old_body_offset;
1874 h->content->length = old_body_length;
1875 h->lines = old_hdr_lines;
1878 mutt_free_body (&h->content->parts);
1882 static int mh_sync_message (CONTEXT * ctx, int msgno)
1884 HEADER *h = ctx->hdrs[msgno];
1886 /* TODO: why the h->env check? */
1887 if (h->attach_del || (h->env && h->env->changed))
1889 if (mh_rewrite_message (ctx, msgno) != 0)
1891 /* TODO: why the env check? */
1893 h->env->changed = 0;
1899 static int maildir_sync_message (CONTEXT * ctx, int msgno)
1901 HEADER *h = ctx->hdrs[msgno];
1902 BUFFER *newpath = NULL,
1910 /* TODO: why the h->env check? */
1911 if (h->attach_del || (h->env && h->env->changed))
1913 /* when doing attachment deletion/rethreading, fall back to the MH case. */
1914 if (mh_rewrite_message (ctx, msgno) != 0)
1916 /* TODO: why the env check? */
1918 h->env->changed = 0;
1922 /* we just have to rename the file. */
1924 if ((p = strrchr (h->path, '/')) == NULL)
1928 "maildir_sync_message: %s: unable to find subdir!\n",
1934 newpath = mutt_buffer_pool_get ();
1935 partpath = mutt_buffer_pool_get ();
1936 fullpath = mutt_buffer_pool_get ();
1937 oldpath = mutt_buffer_pool_get ();
1939 mutt_buffer_strcpy (newpath, p);
1941 /* kill the previous flags. */
1942 if ((p = strchr (newpath->data, ':')) != NULL)
1945 newpath->dptr = p; /* fix buffer up, just to be safe */
1948 maildir_flags (suffix, sizeof (suffix), h);
1950 mutt_buffer_printf (partpath, "%s/%s%s",
1951 (h->read || h->old) ? "cur" : "new",
1952 mutt_b2s (newpath), suffix);
1953 mutt_buffer_printf (fullpath, "%s/%s", ctx->path, mutt_b2s (partpath));
1954 mutt_buffer_printf (oldpath, "%s/%s", ctx->path, h->path);
1956 if (mutt_strcmp (mutt_b2s (fullpath), mutt_b2s (oldpath)) == 0)
1958 /* message hasn't really changed */
1962 /* record that the message is possibly marked as trashed on disk */
1963 h->trash = h->deleted;
1965 if (rename (mutt_b2s (oldpath), mutt_b2s (fullpath)) != 0)
1967 mutt_perror ("rename");
1971 mutt_str_replace (&h->path, mutt_b2s (partpath));
1975 mutt_buffer_pool_release (&newpath);
1976 mutt_buffer_pool_release (&partpath);
1977 mutt_buffer_pool_release (&fullpath);
1978 mutt_buffer_pool_release (&oldpath);
1983 int mh_sync_mailbox (CONTEXT * ctx, int *index_hint)
1985 BUFFER *path = NULL, *tmp = NULL;
1988 header_cache_t *hc = NULL;
1989 #endif /* USE_HCACHE */
1990 char msgbuf[STRING];
1991 progress_t progress;
1993 if (ctx->magic == MUTT_MH)
1994 i = mh_check_mailbox (ctx, index_hint);
1996 i = maildir_check_mailbox (ctx, index_hint);
2002 if (ctx->magic == MUTT_MAILDIR || ctx->magic == MUTT_MH)
2003 hc = mutt_hcache_open(HeaderCache, ctx->path, NULL);
2004 #endif /* USE_HCACHE */
2008 snprintf (msgbuf, sizeof (msgbuf), _("Writing %s..."), ctx->path);
2009 mutt_progress_init (&progress, msgbuf, MUTT_PROGRESS_MSG, WriteInc, ctx->msgcount);
2012 path = mutt_buffer_pool_get ();
2013 tmp = mutt_buffer_pool_get ();
2015 for (i = 0; i < ctx->msgcount; i++)
2018 mutt_progress_update (&progress, i, -1);
2020 if (ctx->hdrs[i]->deleted
2021 && (ctx->magic != MUTT_MAILDIR || !option (OPTMAILDIRTRASH)))
2023 mutt_buffer_printf (path, "%s/%s", ctx->path, ctx->hdrs[i]->path);
2024 if (ctx->magic == MUTT_MAILDIR
2025 || (option (OPTMHPURGE) && ctx->magic == MUTT_MH))
2028 if (ctx->magic == MUTT_MAILDIR)
2029 mutt_hcache_delete (hc, ctx->hdrs[i]->path + 3, &maildir_hcache_keylen);
2030 else if (ctx->magic == MUTT_MH)
2031 mutt_hcache_delete (hc, ctx->hdrs[i]->path, strlen);
2032 #endif /* USE_HCACHE */
2033 unlink (mutt_b2s (path));
2035 else if (ctx->magic == MUTT_MH)
2037 /* MH just moves files out of the way when you delete them */
2038 if (*ctx->hdrs[i]->path != ',')
2040 mutt_buffer_printf (tmp, "%s/,%s", ctx->path,
2041 ctx->hdrs[i]->path);
2042 unlink (mutt_b2s (tmp));
2043 rename (mutt_b2s (path), mutt_b2s (tmp));
2048 else if (ctx->hdrs[i]->changed || ctx->hdrs[i]->attach_del ||
2049 (ctx->magic == MUTT_MAILDIR
2050 && (option (OPTMAILDIRTRASH) || ctx->hdrs[i]->trash)
2051 && (ctx->hdrs[i]->deleted != ctx->hdrs[i]->trash)))
2053 if (ctx->magic == MUTT_MAILDIR)
2055 if (maildir_sync_message (ctx, i) == -1)
2060 if (mh_sync_message (ctx, i) == -1)
2066 if (ctx->hdrs[i]->changed)
2068 if (ctx->magic == MUTT_MAILDIR)
2069 mutt_hcache_store (hc, ctx->hdrs[i]->path + 3, ctx->hdrs[i],
2070 0, &maildir_hcache_keylen, MUTT_GENERATE_UIDVALIDITY);
2071 else if (ctx->magic == MUTT_MH)
2072 mutt_hcache_store (hc, ctx->hdrs[i]->path, ctx->hdrs[i], 0, strlen, MUTT_GENERATE_UIDVALIDITY);
2078 mutt_buffer_pool_release (&path);
2079 mutt_buffer_pool_release (&tmp);
2082 if (ctx->magic == MUTT_MAILDIR || ctx->magic == MUTT_MH)
2083 mutt_hcache_close (hc);
2084 #endif /* USE_HCACHE */
2086 if (ctx->magic == MUTT_MH)
2087 mh_update_sequences (ctx);
2089 /* XXX race condition? */
2091 maildir_update_mtime (ctx);
2093 /* adjust indices */
2097 for (i = 0, j = 0; i < ctx->msgcount; i++)
2099 if (!ctx->hdrs[i]->deleted
2100 || (ctx->magic == MUTT_MAILDIR && option (OPTMAILDIRTRASH)))
2101 ctx->hdrs[i]->index = j++;
2108 mutt_buffer_pool_release (&path);
2109 mutt_buffer_pool_release (&tmp);
2111 if (ctx->magic == MUTT_MAILDIR || ctx->magic == MUTT_MH)
2112 mutt_hcache_close (hc);
2113 #endif /* USE_HCACHE */
2117 static void maildir_canon_filename (BUFFER *dest, const char *src)
2121 if ((t = strrchr (src, '/')))
2124 mutt_buffer_strcpy (dest, src);
2125 if ((u = strrchr (dest->data, ':')))
2132 static void maildir_update_tables (CONTEXT *ctx, int *index_hint)
2138 if (Sort != SORT_ORDER)
2142 mutt_sort_headers (ctx, 1);
2146 old_count = ctx->msgcount;
2147 for (i = 0, j = 0; i < old_count; i++)
2149 if (ctx->hdrs[i]->active && index_hint && *index_hint == i)
2152 if (ctx->hdrs[i]->active)
2153 ctx->hdrs[i]->index = j++;
2156 mx_update_tables (ctx, 0);
2157 mutt_clear_threads (ctx);
2160 static int maildir_update_flags (CONTEXT *ctx, HEADER *o, HEADER *n)
2162 /* save the global state here so we can reset it at the
2163 * end of list block if required.
2165 int context_changed = ctx->changed;
2168 /* user didn't modify this message. alter the flags to match the
2169 * current state on disk. This may not actually do
2170 * anything. mutt_set_flag() will just ignore the call if the status
2171 * bits are already properly set, but it is still faster not to pass
2173 if (o->flagged != n->flagged)
2174 mutt_set_flag (ctx, o, MUTT_FLAG, n->flagged);
2175 if (o->replied != n->replied)
2176 mutt_set_flag (ctx, o, MUTT_REPLIED, n->replied);
2177 if (o->read != n->read)
2178 mutt_set_flag (ctx, o, MUTT_READ, n->read);
2179 if (o->old != n->old)
2180 mutt_set_flag (ctx, o, MUTT_OLD, n->old);
2182 /* mutt_set_flag() will set this, but we don't need to
2183 * sync the changes we made because we just updated the
2184 * context to match the current on-disk state of the
2187 header_changed = o->changed;
2190 /* if the mailbox was not modified before we made these
2191 * changes, unset the changed flag since nothing needs to
2194 if (!context_changed)
2197 return header_changed;
2201 /* This function handles arrival of new mail and reopening of
2202 * maildir folders. The basic idea here is we check to see if either
2203 * the new or cur subdirectories have changed, and if so, we scan them
2204 * for the list of files. We check for newly added messages, and
2205 * then merge the flags messages we already knew about. We don't treat
2206 * either subdirectory differently, as mail could be copied directly into
2207 * the cur directory from another agent.
2209 static int maildir_check_mailbox (CONTEXT * ctx, int *index_hint)
2211 struct stat st_new; /* status of the "new" subdirectory */
2212 struct stat st_cur; /* status of the "cur" subdirectory */
2214 int changed = 0; /* bitmask representing which subdirectories
2215 have changed. 0x1 = new, 0x2 = cur */
2216 int occult = 0; /* messages were removed from the mailbox */
2217 int have_new = 0; /* messages were added to the mailbox */
2218 int flags_changed = 0; /* message flags were changed in the mailbox */
2219 struct maildir *md; /* list of messages in the mailbox */
2220 struct maildir **last, *p;
2223 HASH *fnames; /* hash table for quickly looking up the base filename
2224 for a maildir message */
2225 struct mh_data *data = mh_data (ctx);
2227 /* XXX seems like this check belongs in mx_check_mailbox()
2230 if (!option (OPTCHECKNEW))
2233 buf = mutt_buffer_pool_get ();
2234 mutt_buffer_printf (buf, "%s/new", ctx->path);
2235 if (stat (mutt_b2s (buf), &st_new) == -1)
2237 mutt_buffer_pool_release (&buf);
2241 mutt_buffer_printf (buf, "%s/cur", ctx->path);
2242 if (stat (mutt_b2s (buf), &st_cur) == -1)
2244 mutt_buffer_pool_release (&buf);
2248 /* determine which subdirectories need to be scanned */
2249 if (mutt_stat_timespec_compare (&st_new, MUTT_STAT_MTIME, &ctx->mtime) > 0)
2251 if (mutt_stat_timespec_compare (&st_cur, MUTT_STAT_MTIME, &data->mtime_cur) > 0)
2256 mutt_buffer_pool_release (&buf);
2257 return 0; /* nothing to do */
2260 /* Update the modification times on the mailbox.
2262 * The monitor code notices changes in the open mailbox too quickly.
2263 * In practice, this sometimes leads to all the new messages not being
2264 * noticed during the SAME group of mtime stat updates. To work around
2265 * the problem, don't update the stat times for a monitor caused check. */
2267 if (MonitorContextChanged)
2268 MonitorContextChanged = 0;
2272 mutt_get_stat_timespec (&data->mtime_cur, &st_cur, MUTT_STAT_MTIME);
2273 mutt_get_stat_timespec (&ctx->mtime, &st_new, MUTT_STAT_MTIME);
2276 /* do a fast scan of just the filenames in
2277 * the subdirectories that have changed.
2282 maildir_parse_dir (ctx, &last, "new", &count, NULL);
2284 maildir_parse_dir (ctx, &last, "cur", &count, NULL);
2286 /* we create a hash table keyed off the canonical (sans flags) filename
2287 * of each message we scanned. This is used in the loop over the
2288 * existing messages below to do some correlation.
2290 fnames = hash_create (count, 0);
2292 for (p = md; p; p = p->next)
2294 maildir_canon_filename (buf, p->h->path);
2295 p->canon_fname = safe_strdup (mutt_b2s (buf));
2296 hash_insert (fnames, p->canon_fname, p);
2299 /* check for modifications and adjust flags */
2300 for (i = 0; i < ctx->msgcount; i++)
2302 ctx->hdrs[i]->active = 0;
2303 maildir_canon_filename (buf, ctx->hdrs[i]->path);
2304 p = hash_find (fnames, mutt_b2s (buf));
2307 /* message already exists, merge flags */
2308 ctx->hdrs[i]->active = 1;
2310 /* check to see if the message has moved to a different
2311 * subdirectory. If so, update the associated filename.
2313 if (mutt_strcmp (ctx->hdrs[i]->path, p->h->path))
2314 mutt_str_replace (&ctx->hdrs[i]->path, p->h->path);
2316 /* if the user hasn't modified the flags on this message, update
2317 * the flags we just detected.
2319 if (!ctx->hdrs[i]->changed)
2320 if (maildir_update_flags (ctx, ctx->hdrs[i], p->h))
2323 if (ctx->hdrs[i]->deleted == ctx->hdrs[i]->trash)
2324 if (ctx->hdrs[i]->deleted != p->h->deleted)
2326 ctx->hdrs[i]->deleted = p->h->deleted;
2329 ctx->hdrs[i]->trash = p->h->trash;
2331 /* this is a duplicate of an existing header, so remove it */
2332 mutt_free_header (&p->h);
2334 /* This message was not in the list of messages we just scanned.
2335 * Check to see if we have enough information to know if the
2336 * message has disappeared out from underneath us.
2338 else if (((changed & 1) && (!strncmp (ctx->hdrs[i]->path, "new/", 4))) ||
2339 ((changed & 2) && (!strncmp (ctx->hdrs[i]->path, "cur/", 4))))
2341 /* This message disappeared, so we need to simulate a "reopen"
2342 * event. We know it disappeared because we just scanned the
2343 * subdirectory it used to reside in.
2349 /* This message resides in a subdirectory which was not
2350 * modified, so we assume that it is still present and
2353 ctx->hdrs[i]->active = 1;
2357 /* destroy the file name hash */
2358 hash_destroy (&fnames, NULL);
2360 /* If we didn't just get new mail, update the tables. */
2362 maildir_update_tables (ctx, index_hint);
2364 /* do any delayed parsing we need to do. */
2365 maildir_delayed_parsing (ctx, &md, NULL);
2367 /* Incorporate new messages */
2368 have_new = maildir_move_to_context (ctx, &md);
2370 mutt_buffer_pool_release (&buf);
2373 return MUTT_REOPENED;
2375 return MUTT_NEW_MAIL;
2382 * This function handles arrival of new mail and reopening of
2383 * mh/maildir folders. Things are getting rather complex because we
2384 * don't have a well-defined "mailbox order", so the tricks from
2385 * mbox.c and mx.c won't work here.
2387 * Don't change this code unless you _really_ understand what
2392 static int mh_check_mailbox (CONTEXT * ctx, int *index_hint)
2395 struct stat st, st_cur;
2396 short modified = 0, have_new = 0, occult = 0, flags_changed = 0;;
2397 struct maildir *md, *p;
2398 struct maildir **last = NULL;
2399 struct mh_sequences mhs;
2403 struct mh_data *data = mh_data (ctx);
2405 if (!option (OPTCHECKNEW))
2408 buf = mutt_buffer_pool_get ();
2409 mutt_buffer_strcpy (buf, ctx->path);
2410 if (stat (mutt_b2s (buf), &st) == -1)
2412 mutt_buffer_pool_release (&buf);
2416 /* create .mh_sequences when there isn't one. */
2417 mutt_buffer_printf (buf, "%s/.mh_sequences", ctx->path);
2418 if ((i = stat (mutt_b2s (buf), &st_cur)) == -1 && errno == ENOENT)
2423 if (mh_mkstemp (ctx, &fp, &tmp) == 0)
2426 if (safe_rename (tmp, mutt_b2s (buf)) == -1)
2432 if (i == -1 && stat (mutt_b2s (buf), &st_cur) == -1)
2435 mutt_buffer_pool_release (&buf);
2437 if ((mutt_stat_timespec_compare (&st, MUTT_STAT_MTIME, &ctx->mtime) > 0) ||
2438 (mutt_stat_timespec_compare (&st_cur, MUTT_STAT_MTIME, &data->mtime_cur) > 0))
2444 /* Update the modification times on the mailbox.
2446 * The monitor code notices changes in the open mailbox too quickly.
2447 * In practice, this sometimes leads to all the new messages not being
2448 * noticed during the SAME group of mtime stat updates. To work around
2449 * the problem, don't update the stat times for a monitor caused check. */
2451 if (MonitorContextChanged)
2452 MonitorContextChanged = 0;
2456 mutt_get_stat_timespec (&data->mtime_cur, &st_cur, MUTT_STAT_MTIME);
2457 mutt_get_stat_timespec (&ctx->mtime, &st, MUTT_STAT_MTIME);
2460 memset (&mhs, 0, sizeof (mhs));
2465 maildir_parse_dir (ctx, &last, NULL, &count, NULL);
2466 maildir_delayed_parsing (ctx, &md, NULL);
2468 if (mh_read_sequences (&mhs, ctx->path) < 0)
2470 mh_update_maildir (md, &mhs);
2471 mhs_free_sequences (&mhs);
2473 /* check for modifications and adjust flags */
2474 fnames = hash_create (count, 0);
2476 for (p = md; p; p = p->next)
2478 /* the hash key must survive past the header, which is freed below. */
2479 p->canon_fname = safe_strdup (p->h->path);
2480 hash_insert (fnames, p->canon_fname, p);
2483 for (i = 0; i < ctx->msgcount; i++)
2485 ctx->hdrs[i]->active = 0;
2487 if ((p = hash_find (fnames, ctx->hdrs[i]->path)) && p->h &&
2488 (mbox_strict_cmp_headers (ctx->hdrs[i], p->h)))
2490 ctx->hdrs[i]->active = 1;
2491 /* found the right message */
2492 if (!ctx->hdrs[i]->changed)
2493 if (maildir_update_flags (ctx, ctx->hdrs[i], p->h))
2496 mutt_free_header (&p->h);
2498 else /* message has disappeared */
2502 /* destroy the file name hash */
2504 hash_destroy (&fnames, NULL);
2506 /* If we didn't just get new mail, update the tables. */
2508 maildir_update_tables (ctx, index_hint);
2510 /* Incorporate new messages */
2511 have_new = maildir_move_to_context (ctx, &md);
2514 return MUTT_REOPENED;
2516 return MUTT_NEW_MAIL;
2523 static int maildir_save_to_header_cache (CONTEXT *ctx, HEADER *h)
2529 hc = mutt_hcache_open (HeaderCache, ctx->path, NULL);
2530 rc = mutt_hcache_store (hc, h->path + 3, h, 0, &maildir_hcache_keylen,
2531 MUTT_GENERATE_UIDVALIDITY);
2532 mutt_hcache_close (hc);
2538 static int mh_save_to_header_cache (CONTEXT *ctx, HEADER *h)
2544 hc = mutt_hcache_open (HeaderCache, ctx->path, NULL);
2545 rc = mutt_hcache_store (hc, h->path, h, 0, strlen, MUTT_GENERATE_UIDVALIDITY);
2546 mutt_hcache_close (hc);
2553 * These functions try to find a message in a maildir folder when it
2554 * has moved under our feet. Note that this code is rather expensive, but
2555 * then again, it's called rarely.
2558 static FILE *_maildir_open_find_message (const char *folder, const char *unique,
2559 const char *subfolder)
2561 BUFFER *dir = mutt_buffer_pool_get ();
2562 BUFFER *tunique = mutt_buffer_pool_get ();
2563 BUFFER *fname = mutt_buffer_pool_get ();
2571 mutt_buffer_printf (dir, "%s/%s", folder, subfolder);
2573 if ((dp = opendir (mutt_b2s (dir))) == NULL)
2579 while ((de = readdir (dp)))
2581 maildir_canon_filename (tunique, de->d_name);
2583 if (!mutt_strcmp (mutt_b2s (tunique), unique))
2585 mutt_buffer_printf (fname, "%s/%s/%s", folder, subfolder,
2587 fp = fopen (mutt_b2s (fname), "r"); /* __FOPEN_CHECKED__ */
2598 mutt_buffer_pool_release (&dir);
2599 mutt_buffer_pool_release (&tunique);
2600 mutt_buffer_pool_release (&fname);
2605 FILE *maildir_open_find_message (const char *folder, const char *msg)
2607 BUFFER *unique = NULL;
2610 static unsigned int new_hits = 0, cur_hits = 0; /* simple dynamic optimization */
2612 unique = mutt_buffer_pool_get ();
2613 maildir_canon_filename (unique, msg);
2615 if ((fp = _maildir_open_find_message (folder, mutt_b2s (unique),
2616 new_hits > cur_hits ? "new" : "cur"))
2619 if (new_hits < UINT_MAX && cur_hits < UINT_MAX)
2621 new_hits += (new_hits > cur_hits ? 1 : 0);
2622 cur_hits += (new_hits > cur_hits ? 0 : 1);
2627 if ((fp = _maildir_open_find_message (folder, mutt_b2s (unique),
2628 new_hits > cur_hits ? "cur" : "new"))
2631 if (new_hits < UINT_MAX && cur_hits < UINT_MAX)
2633 new_hits += (new_hits > cur_hits ? 0 : 1);
2634 cur_hits += (new_hits > cur_hits ? 1 : 0);
2643 mutt_buffer_pool_release (&unique);
2651 * 1 if there are no messages in the mailbox
2652 * 0 if there are messages in the mailbox
2655 int maildir_check_empty (const char *path)
2659 int r = 1; /* assume empty until we find a message */
2660 BUFFER *realpath = NULL;
2663 /* Strategy here is to look for any file not beginning with a period */
2665 realpath = mutt_buffer_pool_get ();
2669 /* we do "cur" on the first iteration since its more likely that we'll
2670 * find old messages without having to scan both subdirs
2672 mutt_buffer_printf (realpath, "%s/%s", path,
2673 iter == 0 ? "cur" : "new");
2674 if ((dp = opendir (mutt_b2s (realpath))) == NULL)
2679 while ((de = readdir (dp)))
2681 if (*de->d_name != '.')
2689 } while (r && iter < 2);
2692 mutt_buffer_pool_release (&realpath);
2699 * 1 if there are no messages in the mailbox
2700 * 0 if there are messages in the mailbox
2703 int mh_check_empty (const char *path)
2707 int r = 1; /* assume empty until we find a message */
2709 if ((dp = opendir (path)) == NULL)
2711 while ((de = readdir (dp)))
2713 if (mh_valid_message (de->d_name))
2724 int mx_is_maildir (const char *path)
2730 tmp = mutt_buffer_pool_get ();
2731 mutt_buffer_printf (tmp, "%s/cur", path);
2732 if (stat (mutt_b2s (tmp), &st) == 0 && S_ISDIR (st.st_mode))
2735 mutt_buffer_pool_release (&tmp);
2739 int mx_is_mh (const char *path)
2744 tmp = mutt_buffer_pool_get ();
2746 mutt_buffer_printf (tmp, "%s/.mh_sequences", path);
2747 if (access (mutt_b2s (tmp), F_OK) == 0)
2750 mutt_buffer_printf (tmp, "%s/.xmhcache", path);
2751 if (access (mutt_b2s (tmp), F_OK) == 0)
2754 mutt_buffer_printf (tmp, "%s/.mew_cache", path);
2755 if (access (mutt_b2s (tmp), F_OK) == 0)
2758 mutt_buffer_printf (tmp, "%s/.mew-cache", path);
2759 if (access (mutt_b2s (tmp), F_OK) == 0)
2762 mutt_buffer_printf (tmp, "%s/.sylpheed_cache", path);
2763 if (access (mutt_b2s (tmp), F_OK) == 0)
2767 * ok, this isn't an mh folder, but mh mode can be used to read
2768 * Usenet news from the spool. ;-)
2771 mutt_buffer_printf (tmp, "%s/.overview", path);
2772 if (access (mutt_b2s (tmp), F_OK) == 0)
2778 mutt_buffer_pool_release (&tmp);
2783 struct mx_ops mx_maildir_ops = {
2784 .open = maildir_open_mailbox,
2785 .open_append = maildir_open_mailbox_append,
2786 .close = mh_close_mailbox,
2787 .open_msg = maildir_open_message,
2788 .close_msg = mh_close_message,
2789 .commit_msg = maildir_commit_message,
2790 .open_new_msg = maildir_open_new_message,
2791 .check = maildir_check_mailbox,
2792 .sync = mh_sync_mailbox,
2793 .save_to_header_cache = maildir_save_to_header_cache,
2796 struct mx_ops mx_mh_ops = {
2797 .open = mh_open_mailbox,
2798 .open_append = mh_open_mailbox_append,
2799 .close = mh_close_mailbox,
2800 .open_msg = mh_open_message,
2801 .close_msg = mh_close_message,
2802 .commit_msg = mh_commit_message,
2803 .open_new_msg = mh_open_new_message,
2804 .check = mh_check_mailbox,
2805 .sync = mh_sync_mailbox,
2806 .save_to_header_cache = mh_save_to_header_cache,