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>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * This file contains routines specific to MH and ``maildir'' style
37 #include "mutt_curses.h"
40 #include <sys/types.h>
57 #define INS_SORT_THRESHOLD 6
63 unsigned header_parsed:1;
64 #ifdef HAVE_DIRENT_D_INO
66 #endif /* HAVE_DIRENT_D_INO */
82 /* mh_sequences support */
84 #define MH_SEQ_UNSEEN (1 << 0)
85 #define MH_SEQ_REPLIED (1 << 1)
86 #define MH_SEQ_FLAGGED (1 << 2)
88 static inline struct mh_data *mh_data (CONTEXT *ctx)
90 return (struct mh_data*)ctx->data;
93 static void mhs_alloc (struct mh_sequences *mhs, int i)
98 if (i > mhs->max || !mhs->flags)
101 j = mhs->flags ? mhs->max + 1 : 0;
102 safe_realloc (&mhs->flags, sizeof (mhs->flags[0]) * (newmax + 1));
110 static void mhs_free_sequences (struct mh_sequences *mhs)
115 static short mhs_check (struct mh_sequences *mhs, int i)
117 if (!mhs->flags || i > mhs->max)
120 return mhs->flags[i];
123 static short mhs_set (struct mh_sequences *mhs, int i, short f)
127 return mhs->flags[i];
134 static short mhs_unset (struct mh_sequences *mhs, int i, short f)
138 return mhs->flags[i];
143 static int mh_read_token (char *t, int *first, int *last)
146 if ((p = strchr (t, '-')))
149 if (mutt_atoi (t, first) < 0 || mutt_atoi (p, last) < 0)
154 if (mutt_atoi (t, first) < 0)
161 static int mh_read_sequences (struct mh_sequences *mhs, const char *path)
170 int first, last, rc = 0;
172 char pathname[_POSIX_PATH_MAX];
173 snprintf (pathname, sizeof (pathname), "%s/.mh_sequences", path);
175 if (!(fp = fopen (pathname, "r")))
176 return 0; /* yes, ask callers to silently ignore the error */
178 while ((buff = mutt_read_line (buff, &sz, fp, &line, 0)))
180 if (!(t = strtok (buff, " \t:")))
183 if (!mutt_strcmp (t, MhUnseen))
185 else if (!mutt_strcmp (t, MhFlagged))
187 else if (!mutt_strcmp (t, MhReplied))
189 else /* unknown sequence */
192 while ((t = strtok (NULL, " \t:")))
194 if (mh_read_token (t, &first, &last) < 0)
196 mhs_free_sequences (mhs);
200 for (; first <= last; first++)
201 mhs_set (mhs, first, f);
213 static inline mode_t mh_umask (CONTEXT* ctx)
216 struct mh_data* data = mh_data (ctx);
218 if (data && data->mh_umask)
219 return data->mh_umask;
221 if (stat (ctx->path, &st))
223 dprint (1, (debugfile, "stat failed on %s\n", ctx->path));
227 return 0777 & ~st.st_mode;
230 int mh_buffy (const char *path)
233 struct mh_sequences mhs;
234 memset (&mhs, 0, sizeof (mhs));
236 if (mh_read_sequences (&mhs, path) < 0)
238 for (i = 0; !r && i <= mhs.max; i++)
239 if (mhs_check (&mhs, i) & MH_SEQ_UNSEEN)
241 mhs_free_sequences (&mhs);
245 static int mh_mkstemp (CONTEXT * dest, FILE ** fp, char **tgt)
248 char path[_POSIX_PATH_MAX];
251 omask = umask (mh_umask (dest));
254 snprintf (path, _POSIX_PATH_MAX, "%s/.mutt-%s-%d-%d",
255 dest->path, NONULL (Hostname), (int) getpid (), Counter++);
256 if ((fd = open (path, O_WRONLY | O_EXCL | O_CREAT, 0666)) == -1)
267 *tgt = safe_strdup (path);
273 if ((*fp = fdopen (fd, "w")) == NULL)
275 FREE (tgt); /* __FREE_CHECKED__ */
284 static void mhs_write_one_sequence (FILE * fp, struct mh_sequences *mhs,
285 short f, const char *tag)
289 fprintf (fp, "%s:", tag);
294 for (i = 0; i <= mhs->max; i++)
296 if ((mhs_check (mhs, i) & f))
306 fprintf (fp, " %d", first);
308 fprintf (fp, " %d-%d", first, last);
318 fprintf (fp, " %d", first);
320 fprintf (fp, " %d-%d", first, last);
326 /* XXX - we don't currently remove deleted messages from sequences we don't know. Should we? */
328 static void mh_update_sequences (CONTEXT * ctx)
332 char sequences[_POSIX_PATH_MAX];
344 char seq_unseen[STRING];
345 char seq_replied[STRING];
346 char seq_flagged[STRING];
349 struct mh_sequences mhs;
350 memset (&mhs, 0, sizeof (mhs));
352 snprintf (seq_unseen, sizeof (seq_unseen), "%s:", NONULL (MhUnseen));
353 snprintf (seq_replied, sizeof (seq_replied), "%s:", NONULL (MhReplied));
354 snprintf (seq_flagged, sizeof (seq_flagged), "%s:", NONULL (MhFlagged));
356 if (mh_mkstemp (ctx, &nfp, &tmpfname) != 0)
362 snprintf (sequences, sizeof (sequences), "%s/.mh_sequences", ctx->path);
365 /* first, copy unknown sequences */
366 if ((ofp = fopen (sequences, "r")))
368 while ((buff = mutt_read_line (buff, &s, ofp, &l, 0)))
370 if (!mutt_strncmp (buff, seq_unseen, mutt_strlen (seq_unseen)))
372 if (!mutt_strncmp (buff, seq_flagged, mutt_strlen (seq_flagged)))
374 if (!mutt_strncmp (buff, seq_replied, mutt_strlen (seq_replied)))
377 fprintf (nfp, "%s\n", buff);
382 /* now, update our unseen, flagged, and replied sequences */
383 for (l = 0; l < ctx->msgcount; l++)
385 if (ctx->hdrs[l]->deleted)
388 if ((p = strrchr (ctx->hdrs[l]->path, '/')))
391 p = ctx->hdrs[l]->path;
393 if (mutt_atoi (p, &i) < 0)
396 if (!ctx->hdrs[l]->read)
398 mhs_set (&mhs, i, MH_SEQ_UNSEEN);
401 if (ctx->hdrs[l]->flagged)
403 mhs_set (&mhs, i, MH_SEQ_FLAGGED);
406 if (ctx->hdrs[l]->replied)
408 mhs_set (&mhs, i, MH_SEQ_REPLIED);
413 /* write out the new sequences */
415 mhs_write_one_sequence (nfp, &mhs, MH_SEQ_UNSEEN, NONULL (MhUnseen));
417 mhs_write_one_sequence (nfp, &mhs, MH_SEQ_FLAGGED, NONULL (MhFlagged));
419 mhs_write_one_sequence (nfp, &mhs, MH_SEQ_REPLIED, NONULL (MhReplied));
421 mhs_free_sequences (&mhs);
424 /* try to commit the changes - no guarantee here */
428 if (safe_rename (tmpfname, sequences) != 0)
430 /* report an error? */
437 static void mh_sequences_add_one (CONTEXT * ctx, int n, short unseen,
438 short flagged, short replied)
440 short unseen_done = 0;
441 short flagged_done = 0;
442 short replied_done = 0;
444 FILE *ofp = NULL, *nfp = NULL;
447 char sequences[_POSIX_PATH_MAX];
449 char seq_unseen[STRING];
450 char seq_replied[STRING];
451 char seq_flagged[STRING];
457 if (mh_mkstemp (ctx, &nfp, &tmpfname) == -1)
460 snprintf (seq_unseen, sizeof (seq_unseen), "%s:", NONULL (MhUnseen));
461 snprintf (seq_replied, sizeof (seq_replied), "%s:", NONULL (MhReplied));
462 snprintf (seq_flagged, sizeof (seq_flagged), "%s:", NONULL (MhFlagged));
464 snprintf (sequences, sizeof (sequences), "%s/.mh_sequences", ctx->path);
465 if ((ofp = fopen (sequences, "r")))
467 while ((buff = mutt_read_line (buff, &sz, ofp, &line, 0)))
469 if (unseen && !strncmp (buff, seq_unseen, mutt_strlen (seq_unseen)))
471 fprintf (nfp, "%s %d\n", buff, n);
475 && !strncmp (buff, seq_flagged, mutt_strlen (seq_flagged)))
477 fprintf (nfp, "%s %d\n", buff, n);
481 && !strncmp (buff, seq_replied, mutt_strlen (seq_replied)))
483 fprintf (nfp, "%s %d\n", buff, n);
487 fprintf (nfp, "%s\n", buff);
493 if (!unseen_done && unseen)
494 fprintf (nfp, "%s: %d\n", NONULL (MhUnseen), n);
495 if (!flagged_done && flagged)
496 fprintf (nfp, "%s: %d\n", NONULL (MhFlagged), n);
497 if (!replied_done && replied)
498 fprintf (nfp, "%s: %d\n", NONULL (MhReplied), n);
503 if (safe_rename (tmpfname, sequences) != 0)
509 static void mh_update_maildir (struct maildir *md, struct mh_sequences *mhs)
515 for (; md; md = md->next)
517 if ((p = strrchr (md->h->path, '/')))
522 if (mutt_atoi (p, &i) < 0)
524 f = mhs_check (mhs, i);
526 md->h->read = (f & MH_SEQ_UNSEEN) ? 0 : 1;
527 md->h->flagged = (f & MH_SEQ_FLAGGED) ? 1 : 0;
528 md->h->replied = (f & MH_SEQ_REPLIED) ? 1 : 0;
532 /* maildir support */
534 static void maildir_free_entry (struct maildir **md)
539 FREE (&(*md)->canon_fname);
541 mutt_free_header (&(*md)->h);
543 FREE (md); /* __FREE_CHECKED__ */
546 static void maildir_free_maildir (struct maildir **md)
548 struct maildir *p, *q;
553 for (p = *md; p; p = q)
556 maildir_free_entry (&p);
560 static void maildir_parse_flags (HEADER * h, const char *path)
568 if ((p = strrchr (path, ':')) != NULL && mutt_strncmp (p + 1, "2,", 2) == 0)
572 mutt_str_replace (&h->maildir_flags, p);
573 q = h->maildir_flags;
589 case 'R': /* replied */
594 case 'T': /* trashed */
607 if (q == h->maildir_flags)
608 FREE (&h->maildir_flags);
613 static void maildir_update_mtime (CONTEXT * ctx)
615 char buf[_POSIX_PATH_MAX];
617 struct mh_data *data = mh_data (ctx);
619 if (ctx->magic == M_MAILDIR)
621 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, "cur");
622 if (stat (buf, &st) == 0)
623 data->mtime_cur = st.st_mtime;
624 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, "new");
628 snprintf (buf, sizeof (buf), "%s/.mh_sequences", ctx->path);
629 if (stat (buf, &st) == 0)
630 data->mtime_cur = st.st_mtime;
632 strfcpy (buf, ctx->path, sizeof (buf));
635 if (stat (buf, &st) == 0)
636 ctx->mtime = st.st_mtime;
640 * Actually parse a maildir message. This may also be used to fill
641 * out a fake header structure generated by lazy maildir parsing.
643 static HEADER *maildir_parse_message (int magic, const char *fname,
644 int is_old, HEADER * _h)
650 if ((f = fopen (fname, "r")) != NULL)
653 h = mutt_new_header ();
654 h->env = mutt_read_rfc822_header (f, h, 0, 0);
656 fstat (fileno (f), &st);
660 h->received = h->date_sent;
662 /* always update the length since we have fresh information available. */
663 h->content->length = st.st_size - h->content->offset;
667 if (magic == M_MAILDIR)
670 * maildir stores its flags in the filename, so ignore the
671 * flags in the header of the message
675 maildir_parse_flags (h, fname);
682 /* Ignore the garbage files. A valid MH message consists of only
683 * digits. Deleted message get moved to a filename with a comma before
687 int mh_valid_message (const char *s)
691 if (!isdigit ((unsigned char) *s))
697 static int maildir_parse_dir (CONTEXT * ctx, struct maildir ***last,
698 const char *subdir, int *count,
699 progress_t *progress)
703 char buf[_POSIX_PATH_MAX];
705 struct maildir *entry;
710 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, subdir);
711 is_old = (mutt_strcmp ("cur", subdir) == 0);
714 strfcpy (buf, ctx->path, sizeof (buf));
716 if ((dirp = opendir (buf)) == NULL)
719 while ((de = readdir (dirp)) != NULL)
721 if ((ctx->magic == M_MH && !mh_valid_message (de->d_name))
722 || (ctx->magic == M_MAILDIR && *de->d_name == '.'))
725 /* FOO - really ignore the return value? */
727 (debugfile, "%s:%d: queueing %s\n", __FILE__, __LINE__,
730 h = mutt_new_header ();
732 if (ctx->magic == M_MAILDIR)
733 maildir_parse_flags (h, de->d_name);
738 if (!ctx->quiet && progress)
739 mutt_progress_update (progress, *count, -1);
744 char tmp[_POSIX_PATH_MAX];
745 snprintf (tmp, sizeof (tmp), "%s/%s", subdir, de->d_name);
746 h->path = safe_strdup (tmp);
749 h->path = safe_strdup (de->d_name);
751 entry = safe_calloc (sizeof (struct maildir), 1);
753 #ifdef HAVE_DIRENT_D_INO
754 entry->inode = de->d_ino;
755 #endif /* HAVE_DIRENT_D_INO */
757 *last = &entry->next;
765 static int maildir_add_to_context (CONTEXT * ctx, struct maildir *md)
767 int oldmsgcount = ctx->msgcount;
772 dprint (2, (debugfile, "%s:%d maildir_add_to_context(): Considering %s\n",
773 __FILE__, __LINE__, NONULL (md->canon_fname)));
779 "%s:%d Adding header structure. Flags: %s%s%s%s%s\n", __FILE__,
780 __LINE__, md->h->flagged ? "f" : "", md->h->deleted ? "D" : "",
781 md->h->replied ? "r" : "", md->h->old ? "O" : "",
782 md->h->read ? "R" : ""));
783 if (ctx->msgcount == ctx->hdrmax)
784 mx_alloc_memory (ctx);
786 ctx->hdrs[ctx->msgcount] = md->h;
787 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
789 md->h->content->length + md->h->content->offset -
790 md->h->content->hdr_offset;
798 if (ctx->msgcount > oldmsgcount)
800 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
806 static int maildir_move_to_context (CONTEXT * ctx, struct maildir **md)
809 r = maildir_add_to_context (ctx, *md);
810 maildir_free_maildir (md);
815 static size_t maildir_hcache_keylen (const char *fn)
817 const char * p = strrchr (fn, ':');
818 return p ? (size_t) (p - fn) : mutt_strlen(fn);
822 #if HAVE_DIRENT_D_INO
823 static int md_cmp_inode (struct maildir *a, struct maildir *b)
825 return a->inode - b->inode;
829 static int md_cmp_path (struct maildir *a, struct maildir *b)
831 return strcmp (a->h->path, b->h->path);
835 * Merge two maildir lists according to the inode numbers.
837 static struct maildir* maildir_merge_lists (struct maildir *left,
838 struct maildir *right,
839 int (*cmp) (struct maildir *,
842 struct maildir* head;
843 struct maildir* tail;
847 if (cmp (left, right) < 0)
868 while (left && right)
870 if (cmp (left, right) < 0)
895 static struct maildir* maildir_ins_sort (struct maildir* list,
896 int (*cmp) (struct maildir *,
899 struct maildir *tmp, *last, *ret = NULL, *back;
909 for (tmp = ret; tmp && cmp (tmp, list) <= 0; tmp = tmp->next)
925 * Sort maildir list according to inode.
927 static struct maildir* maildir_sort (struct maildir* list, size_t len,
928 int (*cmp) (struct maildir *,
931 struct maildir* left = list;
932 struct maildir* right = list;
935 if (!list || !list->next)
940 if (len != (size_t)(-1) && len <= INS_SORT_THRESHOLD)
941 return maildir_ins_sort (list, cmp);
944 while (list && list->next)
947 list = list->next->next;
955 left = maildir_sort (left, c, cmp);
956 right = maildir_sort (right, c, cmp);
957 return maildir_merge_lists (left, right, cmp);
960 /* Sorts mailbox into it's natural order.
961 * Currently only defined for MH where files are numbered.
963 static void mh_sort_natural (CONTEXT *ctx, struct maildir **md)
965 if (!ctx || !md || !*md || ctx->magic != M_MH || Sort != SORT_ORDER)
967 dprint (4, (debugfile, "maildir: sorting %s into natural order\n",
969 *md = maildir_sort (*md, (size_t) -1, md_cmp_path);
972 #if HAVE_DIRENT_D_INO
973 static struct maildir *skip_duplicates (struct maildir *p, struct maildir **last)
976 * Skip ahead to the next non-duplicate message.
978 * p should never reach NULL, because we couldn't have reached this point unless
979 * there was a message that needed to be parsed.
981 * the check for p->header_parsed is likely unnecessary since the dupes will most
982 * likely be at the head of the list. but it is present for consistency with
983 * the check at the top of the for() loop in maildir_delayed_parsing().
985 while (!p->h || p->header_parsed) {
994 * This function does the second parsing pass
996 static void maildir_delayed_parsing (CONTEXT * ctx, struct maildir **md,
997 progress_t *progress)
999 struct maildir *p, *last = NULL;
1000 char fn[_POSIX_PATH_MAX];
1002 #if HAVE_DIRENT_D_INO
1006 header_cache_t *hc = NULL;
1008 struct timeval *when = NULL;
1009 struct stat lastchanged;
1013 #if HAVE_DIRENT_D_INO
1014 #define DO_SORT() do { \
1017 dprint (4, (debugfile, "maildir: need to sort %s by inode\n", ctx->path)); \
1018 p = maildir_sort (p, (size_t) -1, md_cmp_inode); \
1024 p = skip_duplicates (p, &last); \
1025 snprintf (fn, sizeof (fn), "%s/%s", ctx->path, p->h->path); \
1029 #define DO_SORT() /* nothing */
1033 hc = mutt_hcache_open (HeaderCache, ctx->path, NULL);
1036 for (p = *md, count = 0; p; p = p->next, count++)
1038 if (! (p && p->h && !p->header_parsed))
1044 if (!ctx->quiet && progress)
1045 mutt_progress_update (progress, count, -1);
1049 snprintf (fn, sizeof (fn), "%s/%s", ctx->path, p->h->path);
1052 if (option(OPTHCACHEVERIFY))
1054 ret = stat(fn, &lastchanged);
1058 lastchanged.st_mtime = 0;
1062 if (ctx->magic == M_MH)
1063 data = mutt_hcache_fetch (hc, p->h->path, strlen);
1065 data = mutt_hcache_fetch (hc, p->h->path + 3, &maildir_hcache_keylen);
1066 when = (struct timeval *) data;
1068 if (data != NULL && !ret && lastchanged.st_mtime <= when->tv_sec)
1070 p->h = mutt_hcache_restore ((unsigned char *)data, &p->h);
1071 if (ctx->magic == M_MAILDIR)
1072 maildir_parse_flags (p->h, fn);
1076 #endif /* USE_HCACHE */
1078 if (maildir_parse_message (ctx->magic, fn, p->h->old, p->h))
1080 p->header_parsed = 1;
1082 if (ctx->magic == M_MH)
1083 mutt_hcache_store (hc, p->h->path, p->h, 0, strlen);
1085 mutt_hcache_store (hc, p->h->path + 3, p->h, 0, &maildir_hcache_keylen);
1088 mutt_free_header (&p->h);
1096 mutt_hcache_close (hc);
1101 mh_sort_natural (ctx, md);
1104 static int mh_close_mailbox (CONTEXT *ctx)
1111 /* Read a MH/maildir style mailbox.
1114 * ctx [IN/OUT] context for this mailbox
1115 * subdir [IN] NULL for MH mailboxes, otherwise the subdir of the
1116 * maildir mailbox to read from
1118 int mh_read_dir (CONTEXT * ctx, const char *subdir)
1121 struct mh_sequences mhs;
1122 struct maildir **last;
1123 struct mh_data *data;
1125 char msgbuf[STRING];
1126 progress_t progress;
1128 memset (&mhs, 0, sizeof (mhs));
1131 snprintf (msgbuf, sizeof (msgbuf), _("Scanning %s..."), ctx->path);
1132 mutt_progress_init (&progress, msgbuf, M_PROGRESS_MSG, ReadInc, 0);
1137 ctx->data = safe_calloc(sizeof (struct mh_data), 1);
1138 ctx->mx_close = mh_close_mailbox;
1140 data = mh_data (ctx);
1142 maildir_update_mtime (ctx);
1147 if (maildir_parse_dir (ctx, &last, subdir, &count, &progress) == -1)
1152 snprintf (msgbuf, sizeof (msgbuf), _("Reading %s..."), ctx->path);
1153 mutt_progress_init (&progress, msgbuf, M_PROGRESS_MSG, ReadInc, count);
1155 maildir_delayed_parsing (ctx, &md, &progress);
1157 if (ctx->magic == M_MH)
1159 if (mh_read_sequences (&mhs, ctx->path) < 0)
1161 mh_update_maildir (md, &mhs);
1162 mhs_free_sequences (&mhs);
1165 maildir_move_to_context (ctx, &md);
1167 if (!data->mh_umask)
1168 data->mh_umask = mh_umask (ctx);
1173 /* read a maildir style mailbox */
1174 int maildir_read_dir (CONTEXT * ctx)
1176 /* maildir looks sort of like MH, except that there are two subdirectories
1177 * of the main folder path from which to read messages
1179 if (mh_read_dir (ctx, "new") == -1 || mh_read_dir (ctx, "cur") == -1)
1186 * Open a new (temporary) message in an MH folder.
1189 int mh_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr)
1191 return mh_mkstemp (dest, &msg->fp, &msg->path);
1194 static int ch_compar (const void *a, const void *b)
1196 return (int)( *((const char *) a) - *((const char *) b));
1199 static void maildir_flags (char *dest, size_t destlen, HEADER * hdr)
1204 * The maildir specification requires that all files in the cur
1205 * subdirectory have the :unique string appeneded, regardless of whether
1206 * or not there are any flags. If .old is set, we know that this message
1207 * will end up in the cur directory, so we include it in the following
1208 * test even though there is no associated flag.
1211 if (hdr && (hdr->flagged || hdr->replied || hdr->read || hdr->deleted || hdr->old || hdr->maildir_flags))
1213 char tmp[LONG_STRING];
1214 snprintf (tmp, sizeof (tmp),
1216 hdr->flagged ? "F" : "",
1217 hdr->replied ? "R" : "",
1218 hdr->read ? "S" : "", hdr->deleted ? "T" : "",
1219 NONULL(hdr->maildir_flags));
1220 if (hdr->maildir_flags)
1221 qsort (tmp, strlen (tmp), 1, ch_compar);
1222 snprintf (dest, destlen, ":2,%s", tmp);
1228 * Open a new (temporary) message in a maildir folder.
1230 * Note that this uses _almost_ the maildir file name format, but
1231 * with a {cur,new} prefix.
1235 int maildir_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr)
1238 char path[_POSIX_PATH_MAX];
1245 short deleted = hdr->deleted;
1248 maildir_flags (suffix, sizeof (suffix), hdr);
1250 hdr->deleted = deleted;
1255 if (hdr && (hdr->read || hdr->old))
1256 strfcpy (subdir, "cur", sizeof (subdir));
1258 strfcpy (subdir, "new", sizeof (subdir));
1260 omask = umask (mh_umask (dest));
1263 snprintf (path, _POSIX_PATH_MAX, "%s/tmp/%s.%lld.%u_%d.%s%s",
1264 dest->path, subdir, (long long)time (NULL), (unsigned int)getpid (),
1265 Counter++, NONULL (Hostname), suffix);
1267 dprint (2, (debugfile, "maildir_open_new_message (): Trying %s.\n",
1270 if ((fd = open (path, O_WRONLY | O_EXCL | O_CREAT, 0666)) == -1)
1272 if (errno != EEXIST)
1281 dprint (2, (debugfile, "maildir_open_new_message (): Success.\n"));
1282 msg->path = safe_strdup (path);
1288 if ((msg->fp = fdopen (fd, "w")) == NULL)
1302 * Commit a message to a maildir folder.
1304 * msg->path contains the file name of a file in tmp/. We take the
1305 * flags from this file's name.
1307 * ctx is the mail folder we commit to.
1309 * hdr is a header structure to which we write the message's new
1310 * file name. This is used in the mh and maildir folder synch
1311 * routines. When this routine is invoked from mx_commit_message,
1314 * msg->path looks like this:
1316 * tmp/{cur,new}.mutt-HOSTNAME-PID-COUNTER:flags
1318 * See also maildir_open_new_message().
1322 int maildir_commit_message (CONTEXT * ctx, MESSAGE * msg, HEADER * hdr)
1326 char path[_POSIX_PATH_MAX];
1327 char full[_POSIX_PATH_MAX];
1330 if (safe_fsync_close (&msg->fp))
1332 mutt_perror (_("Could not flush message to disk"));
1336 /* extract the subdir */
1337 s = strrchr (msg->path, '/') + 1;
1338 strfcpy (subdir, s, 4);
1340 /* extract the flags */
1341 if ((s = strchr (s, ':')))
1342 strfcpy (suffix, s, sizeof (suffix));
1346 /* construct a new file name. */
1349 snprintf (path, _POSIX_PATH_MAX, "%s/%lld.%u_%d.%s%s", subdir,
1350 (long long)time (NULL), (unsigned int)getpid (), Counter++,
1351 NONULL (Hostname), suffix);
1352 snprintf (full, _POSIX_PATH_MAX, "%s/%s", ctx->path, path);
1354 dprint (2, (debugfile, "maildir_commit_message (): renaming %s to %s.\n",
1357 if (safe_rename (msg->path, full) == 0)
1360 mutt_str_replace (&hdr->path, path);
1364 * Adjust the mtime on the file to match the time at which this
1365 * message was received. Currently this is only set when copying
1366 * messages between mailboxes, so we test to ensure that it is
1373 ut.actime = msg->received;
1374 ut.modtime = msg->received;
1375 if (utime (full, &ut))
1377 mutt_perror (_("maildir_commit_message(): unable to set time on file"));
1384 else if (errno != EEXIST)
1386 mutt_perror (ctx->path);
1393 * commit a message to an MH folder.
1398 static int _mh_commit_message (CONTEXT * ctx, MESSAGE * msg, HEADER * hdr,
1404 unsigned int n, hi = 0;
1405 char path[_POSIX_PATH_MAX];
1408 if (safe_fsync_close (&msg->fp))
1410 mutt_perror (_("Could not flush message to disk"));
1414 if ((dirp = opendir (ctx->path)) == NULL)
1416 mutt_perror (ctx->path);
1420 /* figure out what the next message number is */
1421 while ((de = readdir (dirp)) != NULL)
1429 if (!isdigit ((unsigned char) *cp))
1443 * Now try to rename the file to the proper name.
1445 * Note: We may have to try multiple times, until we find a free
1452 snprintf (tmp, sizeof (tmp), "%d", hi);
1453 snprintf (path, sizeof (path), "%s/%s", ctx->path, tmp);
1454 if (safe_rename (msg->path, path) == 0)
1457 mutt_str_replace (&hdr->path, tmp);
1461 else if (errno != EEXIST)
1463 mutt_perror (ctx->path);
1468 mh_sequences_add_one (ctx, hi, !msg->flags.read, msg->flags.flagged,
1469 msg->flags.replied);
1473 int mh_commit_message (CONTEXT * ctx, MESSAGE * msg, HEADER * hdr)
1475 return _mh_commit_message (ctx, msg, hdr, 1);
1479 /* Sync a message in an MH folder.
1481 * This code is also used for attachment deletion in maildir
1485 static int mh_rewrite_message (CONTEXT * ctx, int msgno)
1487 HEADER *h = ctx->hdrs[msgno];
1492 char oldpath[_POSIX_PATH_MAX];
1493 char newpath[_POSIX_PATH_MAX];
1494 char partpath[_POSIX_PATH_MAX];
1496 long old_body_offset = h->content->offset;
1497 long old_body_length = h->content->length;
1498 long old_hdr_lines = h->lines;
1500 if ((dest = mx_open_new_message (ctx, h, 0)) == NULL)
1503 if ((rc = mutt_copy_message (dest->fp, ctx, h,
1504 M_CM_UPDATE, CH_UPDATE | CH_UPDATE_LEN)) == 0)
1506 snprintf (oldpath, _POSIX_PATH_MAX, "%s/%s", ctx->path, h->path);
1507 strfcpy (partpath, h->path, _POSIX_PATH_MAX);
1509 if (ctx->magic == M_MAILDIR)
1510 rc = maildir_commit_message (ctx, dest, h);
1512 rc = _mh_commit_message (ctx, dest, h, 0);
1514 mx_close_message (&dest);
1523 * Try to move the new message to the old place.
1526 * This is important when we are just updating flags.
1528 * Note that there is a race condition against programs which
1529 * use the first free slot instead of the maximum message
1530 * number. Mutt does _not_ behave like this.
1532 * Anyway, if this fails, the message is in the folder, so
1533 * all what happens is that a concurrently runnung mutt will
1534 * lose flag modifications.
1537 if (ctx->magic == M_MH && rc == 0)
1539 snprintf (newpath, _POSIX_PATH_MAX, "%s/%s", ctx->path, h->path);
1540 if ((rc = safe_rename (newpath, oldpath)) == 0)
1541 mutt_str_replace (&h->path, partpath);
1545 mx_close_message (&dest);
1547 if (rc == -1 && restore)
1549 h->content->offset = old_body_offset;
1550 h->content->length = old_body_length;
1551 h->lines = old_hdr_lines;
1554 mutt_free_body (&h->content->parts);
1558 static int mh_sync_message (CONTEXT * ctx, int msgno)
1560 HEADER *h = ctx->hdrs[msgno];
1562 if (h->attach_del ||
1563 (h->env && (h->env->refs_changed || h->env->irt_changed)))
1564 if (mh_rewrite_message (ctx, msgno) != 0)
1570 static int maildir_sync_message (CONTEXT * ctx, int msgno)
1572 HEADER *h = ctx->hdrs[msgno];
1574 if (h->attach_del ||
1575 (h->env && (h->env->refs_changed || h->env->irt_changed)))
1577 /* when doing attachment deletion/rethreading, fall back to the MH case. */
1578 if (mh_rewrite_message (ctx, msgno) != 0)
1583 /* we just have to rename the file. */
1585 char newpath[_POSIX_PATH_MAX];
1586 char partpath[_POSIX_PATH_MAX];
1587 char fullpath[_POSIX_PATH_MAX];
1588 char oldpath[_POSIX_PATH_MAX];
1592 if ((p = strrchr (h->path, '/')) == NULL)
1596 "maildir_sync_message: %s: unable to find subdir!\n",
1601 strfcpy (newpath, p, sizeof (newpath));
1603 /* kill the previous flags */
1604 if ((p = strchr (newpath, ':')) != NULL)
1607 maildir_flags (suffix, sizeof (suffix), h);
1609 snprintf (partpath, sizeof (partpath), "%s/%s%s",
1610 (h->read || h->old) ? "cur" : "new", newpath, suffix);
1611 snprintf (fullpath, sizeof (fullpath), "%s/%s", ctx->path, partpath);
1612 snprintf (oldpath, sizeof (oldpath), "%s/%s", ctx->path, h->path);
1614 if (mutt_strcmp (fullpath, oldpath) == 0)
1616 /* message hasn't really changed */
1620 /* record that the message is possibly marked as trashed on disk */
1621 h->trash = h->deleted;
1623 if (rename (oldpath, fullpath) != 0)
1625 mutt_perror ("rename");
1628 mutt_str_replace (&h->path, partpath);
1633 int mh_sync_mailbox (CONTEXT * ctx, int *index_hint)
1635 char path[_POSIX_PATH_MAX], tmp[_POSIX_PATH_MAX];
1638 header_cache_t *hc = NULL;
1639 #endif /* USE_HCACHE */
1640 char msgbuf[STRING];
1641 progress_t progress;
1643 if (ctx->magic == M_MH)
1644 i = mh_check_mailbox (ctx, index_hint);
1646 i = maildir_check_mailbox (ctx, index_hint);
1652 if (ctx->magic == M_MAILDIR || ctx->magic == M_MH)
1653 hc = mutt_hcache_open(HeaderCache, ctx->path, NULL);
1654 #endif /* USE_HCACHE */
1658 snprintf (msgbuf, sizeof (msgbuf), _("Writing %s..."), ctx->path);
1659 mutt_progress_init (&progress, msgbuf, M_PROGRESS_MSG, WriteInc, ctx->msgcount);
1662 for (i = 0; i < ctx->msgcount; i++)
1665 mutt_progress_update (&progress, i, -1);
1667 if (ctx->hdrs[i]->deleted
1668 && (ctx->magic != M_MAILDIR || !option (OPTMAILDIRTRASH)))
1670 snprintf (path, sizeof (path), "%s/%s", ctx->path, ctx->hdrs[i]->path);
1671 if (ctx->magic == M_MAILDIR
1672 || (option (OPTMHPURGE) && ctx->magic == M_MH))
1675 if (ctx->magic == M_MAILDIR)
1676 mutt_hcache_delete (hc, ctx->hdrs[i]->path + 3, &maildir_hcache_keylen);
1677 else if (ctx->magic == M_MH)
1678 mutt_hcache_delete (hc, ctx->hdrs[i]->path, strlen);
1679 #endif /* USE_HCACHE */
1682 else if (ctx->magic == M_MH)
1684 /* MH just moves files out of the way when you delete them */
1685 if (*ctx->hdrs[i]->path != ',')
1687 snprintf (tmp, sizeof (tmp), "%s/,%s", ctx->path,
1688 ctx->hdrs[i]->path);
1695 else if (ctx->hdrs[i]->changed || ctx->hdrs[i]->attach_del ||
1696 (ctx->magic == M_MAILDIR
1697 && (option (OPTMAILDIRTRASH) || ctx->hdrs[i]->trash)
1698 && (ctx->hdrs[i]->deleted != ctx->hdrs[i]->trash)))
1700 if (ctx->magic == M_MAILDIR)
1702 if (maildir_sync_message (ctx, i) == -1)
1707 if (mh_sync_message (ctx, i) == -1)
1713 if (ctx->hdrs[i]->changed)
1715 if (ctx->magic == M_MAILDIR)
1716 mutt_hcache_store (hc, ctx->hdrs[i]->path + 3, ctx->hdrs[i],
1717 0, &maildir_hcache_keylen);
1718 else if (ctx->magic == M_MH)
1719 mutt_hcache_store (hc, ctx->hdrs[i]->path, ctx->hdrs[i], 0, strlen);
1726 if (ctx->magic == M_MAILDIR || ctx->magic == M_MH)
1727 mutt_hcache_close (hc);
1728 #endif /* USE_HCACHE */
1730 if (ctx->magic == M_MH)
1731 mh_update_sequences (ctx);
1733 /* XXX race condition? */
1735 maildir_update_mtime (ctx);
1737 /* adjust indices */
1741 for (i = 0, j = 0; i < ctx->msgcount; i++)
1743 if (!ctx->hdrs[i]->deleted
1744 || (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))
1745 ctx->hdrs[i]->index = j++;
1753 if (ctx->magic == M_MAILDIR || ctx->magic == M_MH)
1754 mutt_hcache_close (hc);
1755 #endif /* USE_HCACHE */
1759 static char *maildir_canon_filename (char *dest, const char *src, size_t l)
1763 if ((t = strrchr (src, '/')))
1766 strfcpy (dest, src, l);
1767 if ((u = strrchr (dest, ':')))
1773 static void maildir_update_tables (CONTEXT *ctx, int *index_hint)
1779 if (Sort != SORT_ORDER)
1783 mutt_sort_headers (ctx, 1);
1787 old_count = ctx->msgcount;
1788 for (i = 0, j = 0; i < old_count; i++)
1790 if (ctx->hdrs[i]->active && index_hint && *index_hint == i)
1793 if (ctx->hdrs[i]->active)
1794 ctx->hdrs[i]->index = j++;
1797 mx_update_tables (ctx, 0);
1798 mutt_clear_threads (ctx);
1801 static void maildir_update_flags (CONTEXT *ctx, HEADER *o, HEADER *n)
1803 /* save the global state here so we can reset it at the
1804 * end of list block if required.
1806 int context_changed = ctx->changed;
1808 /* user didn't modify this message. alter the flags to match the
1809 * current state on disk. This may not actually do
1810 * anything. mutt_set_flag() will just ignore the call if the status
1811 * bits are already properly set, but it is still faster not to pass
1813 if (o->flagged != n->flagged)
1814 mutt_set_flag (ctx, o, M_FLAG, n->flagged);
1815 if (o->replied != n->replied)
1816 mutt_set_flag (ctx, o, M_REPLIED, n->replied);
1817 if (o->read != n->read)
1818 mutt_set_flag (ctx, o, M_READ, n->read);
1819 if (o->old != n->old)
1820 mutt_set_flag (ctx, o, M_OLD, n->old);
1822 /* mutt_set_flag() will set this, but we don't need to
1823 * sync the changes we made because we just updated the
1824 * context to match the current on-disk state of the
1829 /* if the mailbox was not modified before we made these
1830 * changes, unset the changed flag since nothing needs to
1833 if (!context_changed)
1838 /* This function handles arrival of new mail and reopening of
1839 * maildir folders. The basic idea here is we check to see if either
1840 * the new or cur subdirectories have changed, and if so, we scan them
1841 * for the list of files. We check for newly added messages, and
1842 * then merge the flags messages we already knew about. We don't treat
1843 * either subdirectory differently, as mail could be copied directly into
1844 * the cur directory from another agent.
1846 int maildir_check_mailbox (CONTEXT * ctx, int *index_hint)
1848 struct stat st_new; /* status of the "new" subdirectory */
1849 struct stat st_cur; /* status of the "cur" subdirectory */
1850 char buf[_POSIX_PATH_MAX];
1851 int changed = 0; /* bitmask representing which subdirectories
1852 have changed. 0x1 = new, 0x2 = cur */
1853 int occult = 0; /* messages were removed from the mailbox */
1854 int have_new = 0; /* messages were added to the mailbox */
1855 struct maildir *md; /* list of messages in the mailbox */
1856 struct maildir **last, *p;
1858 HASH *fnames; /* hash table for quickly looking up the base filename
1859 for a maildir message */
1860 struct mh_data *data = mh_data (ctx);
1862 /* XXX seems like this check belongs in mx_check_mailbox()
1865 if (!option (OPTCHECKNEW))
1868 snprintf (buf, sizeof (buf), "%s/new", ctx->path);
1869 if (stat (buf, &st_new) == -1)
1872 snprintf (buf, sizeof (buf), "%s/cur", ctx->path);
1873 if (stat (buf, &st_cur) == -1)
1876 /* determine which subdirectories need to be scanned */
1877 if (st_new.st_mtime > ctx->mtime)
1879 if (st_cur.st_mtime > data->mtime_cur)
1883 return 0; /* nothing to do */
1885 /* update the modification times on the mailbox */
1886 data->mtime_cur = st_cur.st_mtime;
1887 ctx->mtime = st_new.st_mtime;
1889 /* do a fast scan of just the filenames in
1890 * the subdirectories that have changed.
1895 maildir_parse_dir (ctx, &last, "new", NULL, NULL);
1897 maildir_parse_dir (ctx, &last, "cur", NULL, NULL);
1899 /* we create a hash table keyed off the canonical (sans flags) filename
1900 * of each message we scanned. This is used in the loop over the
1901 * existing messages below to do some correlation.
1903 fnames = hash_create (1031, 0);
1905 for (p = md; p; p = p->next)
1907 maildir_canon_filename (buf, p->h->path, sizeof (buf));
1908 p->canon_fname = safe_strdup (buf);
1909 hash_insert (fnames, p->canon_fname, p, 0);
1912 /* check for modifications and adjust flags */
1913 for (i = 0; i < ctx->msgcount; i++)
1915 ctx->hdrs[i]->active = 0;
1916 maildir_canon_filename (buf, ctx->hdrs[i]->path, sizeof (buf));
1917 p = hash_find (fnames, buf);
1920 /* message already exists, merge flags */
1921 ctx->hdrs[i]->active = 1;
1923 /* check to see if the message has moved to a different
1924 * subdirectory. If so, update the associated filename.
1926 if (mutt_strcmp (ctx->hdrs[i]->path, p->h->path))
1927 mutt_str_replace (&ctx->hdrs[i]->path, p->h->path);
1929 /* if the user hasn't modified the flags on this message, update
1930 * the flags we just detected.
1932 if (!ctx->hdrs[i]->changed)
1933 maildir_update_flags (ctx, ctx->hdrs[i], p->h);
1935 if (ctx->hdrs[i]->deleted == ctx->hdrs[i]->trash)
1936 ctx->hdrs[i]->deleted = p->h->deleted;
1937 ctx->hdrs[i]->trash = p->h->trash;
1939 /* this is a duplicate of an existing header, so remove it */
1940 mutt_free_header (&p->h);
1942 /* This message was not in the list of messages we just scanned.
1943 * Check to see if we have enough information to know if the
1944 * message has disappeared out from underneath us.
1946 else if (((changed & 1) && (!strncmp (ctx->hdrs[i]->path, "new/", 4))) ||
1947 ((changed & 2) && (!strncmp (ctx->hdrs[i]->path, "cur/", 4))))
1949 /* This message disappeared, so we need to simulate a "reopen"
1950 * event. We know it disappeared because we just scanned the
1951 * subdirectory it used to reside in.
1957 /* This message resides in a subdirectory which was not
1958 * modified, so we assume that it is still present and
1961 ctx->hdrs[i]->active = 1;
1965 /* destroy the file name hash */
1966 hash_destroy (&fnames, NULL);
1968 /* If we didn't just get new mail, update the tables. */
1970 maildir_update_tables (ctx, index_hint);
1972 /* do any delayed parsing we need to do. */
1973 maildir_delayed_parsing (ctx, &md, NULL);
1975 /* Incorporate new messages */
1976 have_new = maildir_move_to_context (ctx, &md);
1978 return occult ? M_REOPENED : (have_new ? M_NEW_MAIL : 0);
1982 * This function handles arrival of new mail and reopening of
1983 * mh/maildir folders. Things are getting rather complex because we
1984 * don't have a well-defined "mailbox order", so the tricks from
1985 * mbox.c and mx.c won't work here.
1987 * Don't change this code unless you _really_ understand what
1992 int mh_check_mailbox (CONTEXT * ctx, int *index_hint)
1994 char buf[_POSIX_PATH_MAX];
1995 struct stat st, st_cur;
1996 short modified = 0, have_new = 0, occult = 0;
1997 struct maildir *md, *p;
1998 struct maildir **last = NULL;
1999 struct mh_sequences mhs;
2002 struct mh_data *data = mh_data (ctx);
2004 if (!option (OPTCHECKNEW))
2007 strfcpy (buf, ctx->path, sizeof (buf));
2008 if (stat (buf, &st) == -1)
2011 /* create .mh_sequences when there isn't one. */
2012 snprintf (buf, sizeof (buf), "%s/.mh_sequences", ctx->path);
2013 if ((i = stat (buf, &st_cur)) == -1 && errno == ENOENT)
2018 if (mh_mkstemp (ctx, &fp, &tmp) == 0)
2021 if (safe_rename (tmp, buf) == -1)
2027 if (i == -1 && stat (buf, &st_cur) == -1)
2030 if (st.st_mtime > ctx->mtime || st_cur.st_mtime > data->mtime_cur)
2036 data->mtime_cur = st_cur.st_mtime;
2037 ctx->mtime = st.st_mtime;
2039 memset (&mhs, 0, sizeof (mhs));
2044 maildir_parse_dir (ctx, &last, NULL, NULL, NULL);
2045 maildir_delayed_parsing (ctx, &md, NULL);
2047 if (mh_read_sequences (&mhs, ctx->path) < 0)
2049 mh_update_maildir (md, &mhs);
2050 mhs_free_sequences (&mhs);
2052 /* check for modifications and adjust flags */
2053 fnames = hash_create (1031, 0);
2055 for (p = md; p; p = p->next)
2056 hash_insert (fnames, p->h->path, p, 0);
2058 for (i = 0; i < ctx->msgcount; i++)
2060 ctx->hdrs[i]->active = 0;
2062 if ((p = hash_find (fnames, ctx->hdrs[i]->path)) && p->h &&
2063 (mbox_strict_cmp_headers (ctx->hdrs[i], p->h)))
2065 ctx->hdrs[i]->active = 1;
2066 /* found the right message */
2067 if (!ctx->hdrs[i]->changed)
2068 maildir_update_flags (ctx, ctx->hdrs[i], p->h);
2070 mutt_free_header (&p->h);
2072 else /* message has disappeared */
2076 /* destroy the file name hash */
2078 hash_destroy (&fnames, NULL);
2080 /* If we didn't just get new mail, update the tables. */
2082 maildir_update_tables (ctx, index_hint);
2084 /* Incorporate new messages */
2085 have_new = maildir_move_to_context (ctx, &md);
2087 return occult ? M_REOPENED : (have_new ? M_NEW_MAIL : 0);
2094 * These functions try to find a message in a maildir folder when it
2095 * has moved under our feet. Note that this code is rather expensive, but
2096 * then again, it's called rarely.
2099 static FILE *_maildir_open_find_message (const char *folder, const char *unique,
2100 const char *subfolder)
2102 char dir[_POSIX_PATH_MAX];
2103 char tunique[_POSIX_PATH_MAX];
2104 char fname[_POSIX_PATH_MAX];
2112 snprintf (dir, sizeof (dir), "%s/%s", folder, subfolder);
2114 if ((dp = opendir (dir)) == NULL)
2120 while ((de = readdir (dp)))
2122 maildir_canon_filename (tunique, de->d_name, sizeof (tunique));
2124 if (!mutt_strcmp (tunique, unique))
2126 snprintf (fname, sizeof (fname), "%s/%s/%s", folder, subfolder,
2128 fp = fopen (fname, "r"); /* __FOPEN_CHECKED__ */
2140 FILE *maildir_open_find_message (const char *folder, const char *msg)
2142 char unique[_POSIX_PATH_MAX];
2145 static unsigned int new_hits = 0, cur_hits = 0; /* simple dynamic optimization */
2147 maildir_canon_filename (unique, msg, sizeof (unique));
2151 _maildir_open_find_message (folder, unique,
2152 new_hits > cur_hits ? "new" : "cur"))
2155 if (new_hits < UINT_MAX && cur_hits < UINT_MAX)
2157 new_hits += (new_hits > cur_hits ? 1 : 0);
2158 cur_hits += (new_hits > cur_hits ? 0 : 1);
2165 _maildir_open_find_message (folder, unique,
2166 new_hits > cur_hits ? "cur" : "new"))
2169 if (new_hits < UINT_MAX && cur_hits < UINT_MAX)
2171 new_hits += (new_hits > cur_hits ? 0 : 1);
2172 cur_hits += (new_hits > cur_hits ? 1 : 0);
2184 * 1 if there are no messages in the mailbox
2185 * 0 if there are messages in the mailbox
2188 int maildir_check_empty (const char *path)
2192 int r = 1; /* assume empty until we find a message */
2193 char realpath[_POSIX_PATH_MAX];
2196 /* Strategy here is to look for any file not beginning with a period */
2199 /* we do "cur" on the first iteration since its more likely that we'll
2200 * find old messages without having to scan both subdirs
2202 snprintf (realpath, sizeof (realpath), "%s/%s", path,
2203 iter == 0 ? "cur" : "new");
2204 if ((dp = opendir (realpath)) == NULL)
2206 while ((de = readdir (dp)))
2208 if (*de->d_name != '.')
2216 } while (r && iter < 2);
2223 * 1 if there are no messages in the mailbox
2224 * 0 if there are messages in the mailbox
2227 int mh_check_empty (const char *path)
2231 int r = 1; /* assume empty until we find a message */
2233 if ((dp = opendir (path)) == NULL)
2235 while ((de = readdir (dp)))
2237 if (mh_valid_message (de->d_name))
2248 int mx_is_maildir (const char *path)
2250 char tmp[_POSIX_PATH_MAX];
2253 snprintf (tmp, sizeof (tmp), "%s/cur", path);
2254 if (stat (tmp, &st) == 0 && S_ISDIR (st.st_mode))
2259 int mx_is_mh (const char *path)
2261 char tmp[_POSIX_PATH_MAX];
2263 snprintf (tmp, sizeof (tmp), "%s/.mh_sequences", path);
2264 if (access (tmp, F_OK) == 0)
2267 snprintf (tmp, sizeof (tmp), "%s/.xmhcache", path);
2268 if (access (tmp, F_OK) == 0)
2271 snprintf (tmp, sizeof (tmp), "%s/.mew_cache", path);
2272 if (access (tmp, F_OK) == 0)
2275 snprintf (tmp, sizeof (tmp), "%s/.mew-cache", path);
2276 if (access (tmp, F_OK) == 0)
2279 snprintf (tmp, sizeof (tmp), "%s/.sylpheed_cache", path);
2280 if (access (tmp, F_OK) == 0)
2284 * ok, this isn't an mh folder, but mh mode can be used to read
2285 * Usenet news from the spool. ;-)
2288 snprintf (tmp, sizeof (tmp), "%s/.overview", path);
2289 if (access (tmp, F_OK) == 0)