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 (t, last) < 0)
154 if (mutt_atoi (t, first) < 0)
161 static int mh_read_sequences (struct mh_sequences *mhs, const char *path)
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)
244 static int mh_mkstemp (CONTEXT * dest, FILE ** fp, char **tgt)
247 char path[_POSIX_PATH_MAX];
250 omask = umask (mh_umask (dest));
253 snprintf (path, _POSIX_PATH_MAX, "%s/.mutt-%s-%d-%d",
254 dest->path, NONULL (Hostname), (int) getpid (), Counter++);
255 if ((fd = open (path, O_WRONLY | O_EXCL | O_CREAT, 0666)) == -1)
266 *tgt = safe_strdup (path);
272 if ((*fp = fdopen (fd, "w")) == NULL)
274 FREE (tgt); /* __FREE_CHECKED__ */
283 static void mhs_write_one_sequence (FILE * fp, struct mh_sequences *mhs,
284 short f, const char *tag)
288 fprintf (fp, "%s:", tag);
293 for (i = 0; i <= mhs->max; i++)
295 if ((mhs_check (mhs, i) & f))
305 fprintf (fp, " %d", first);
307 fprintf (fp, " %d-%d", first, last);
317 fprintf (fp, " %d", first);
319 fprintf (fp, " %d-%d", first, last);
325 /* XXX - we don't currently remove deleted messages from sequences we don't know. Should we? */
327 static void mh_update_sequences (CONTEXT * ctx)
331 char sequences[_POSIX_PATH_MAX];
343 char seq_unseen[STRING];
344 char seq_replied[STRING];
345 char seq_flagged[STRING];
348 struct mh_sequences mhs;
349 memset (&mhs, 0, sizeof (mhs));
351 snprintf (seq_unseen, sizeof (seq_unseen), "%s:", NONULL (MhUnseen));
352 snprintf (seq_replied, sizeof (seq_replied), "%s:", NONULL (MhReplied));
353 snprintf (seq_flagged, sizeof (seq_flagged), "%s:", NONULL (MhFlagged));
355 if (mh_mkstemp (ctx, &nfp, &tmpfname) != 0)
361 snprintf (sequences, sizeof (sequences), "%s/.mh_sequences", ctx->path);
364 /* first, copy unknown sequences */
365 if ((ofp = fopen (sequences, "r")))
367 while ((buff = mutt_read_line (buff, &s, ofp, &l, 0)))
369 if (!mutt_strncmp (buff, seq_unseen, mutt_strlen (seq_unseen)))
371 if (!mutt_strncmp (buff, seq_flagged, mutt_strlen (seq_flagged)))
373 if (!mutt_strncmp (buff, seq_replied, mutt_strlen (seq_replied)))
376 fprintf (nfp, "%s\n", buff);
381 /* now, update our unseen, flagged, and replied sequences */
382 for (l = 0; l < ctx->msgcount; l++)
384 if (ctx->hdrs[l]->deleted)
387 if ((p = strrchr (ctx->hdrs[l]->path, '/')))
390 p = ctx->hdrs[l]->path;
392 if (mutt_atoi (p, &i) < 0)
395 if (!ctx->hdrs[l]->read)
397 mhs_set (&mhs, i, MH_SEQ_UNSEEN);
400 if (ctx->hdrs[l]->flagged)
402 mhs_set (&mhs, i, MH_SEQ_FLAGGED);
405 if (ctx->hdrs[l]->replied)
407 mhs_set (&mhs, i, MH_SEQ_REPLIED);
412 /* write out the new sequences */
414 mhs_write_one_sequence (nfp, &mhs, MH_SEQ_UNSEEN, NONULL (MhUnseen));
416 mhs_write_one_sequence (nfp, &mhs, MH_SEQ_FLAGGED, NONULL (MhFlagged));
418 mhs_write_one_sequence (nfp, &mhs, MH_SEQ_REPLIED, NONULL (MhReplied));
420 mhs_free_sequences (&mhs);
423 /* try to commit the changes - no guarantee here */
427 if (safe_rename (tmpfname, sequences) != 0)
429 /* report an error? */
436 static void mh_sequences_add_one (CONTEXT * ctx, int n, short unseen,
437 short flagged, short replied)
439 short unseen_done = 0;
440 short flagged_done = 0;
441 short replied_done = 0;
443 FILE *ofp = NULL, *nfp = NULL;
446 char sequences[_POSIX_PATH_MAX];
448 char seq_unseen[STRING];
449 char seq_replied[STRING];
450 char seq_flagged[STRING];
456 if (mh_mkstemp (ctx, &nfp, &tmpfname) == -1)
459 snprintf (seq_unseen, sizeof (seq_unseen), "%s:", NONULL (MhUnseen));
460 snprintf (seq_replied, sizeof (seq_replied), "%s:", NONULL (MhReplied));
461 snprintf (seq_flagged, sizeof (seq_flagged), "%s:", NONULL (MhFlagged));
463 snprintf (sequences, sizeof (sequences), "%s/.mh_sequences", ctx->path);
464 if ((ofp = fopen (sequences, "r")))
466 while ((buff = mutt_read_line (buff, &sz, ofp, &line, 0)))
468 if (unseen && !strncmp (buff, seq_unseen, mutt_strlen (seq_unseen)))
470 fprintf (nfp, "%s %d\n", buff, n);
474 && !strncmp (buff, seq_flagged, mutt_strlen (seq_flagged)))
476 fprintf (nfp, "%s %d\n", buff, n);
480 && !strncmp (buff, seq_replied, mutt_strlen (seq_replied)))
482 fprintf (nfp, "%s %d\n", buff, n);
486 fprintf (nfp, "%s\n", buff);
492 if (!unseen_done && unseen)
493 fprintf (nfp, "%s: %d\n", NONULL (MhUnseen), n);
494 if (!flagged_done && flagged)
495 fprintf (nfp, "%s: %d\n", NONULL (MhFlagged), n);
496 if (!replied_done && replied)
497 fprintf (nfp, "%s: %d\n", NONULL (MhReplied), n);
502 if (safe_rename (tmpfname, sequences) != 0)
508 static void mh_update_maildir (struct maildir *md, struct mh_sequences *mhs)
514 for (; md; md = md->next)
516 if ((p = strrchr (md->h->path, '/')))
521 if (mutt_atoi (p, &i) < 0)
523 f = mhs_check (mhs, i);
525 md->h->read = (f & MH_SEQ_UNSEEN) ? 0 : 1;
526 md->h->flagged = (f & MH_SEQ_FLAGGED) ? 1 : 0;
527 md->h->replied = (f & MH_SEQ_REPLIED) ? 1 : 0;
531 /* maildir support */
533 static void maildir_free_entry (struct maildir **md)
538 FREE (&(*md)->canon_fname);
540 mutt_free_header (&(*md)->h);
542 FREE (md); /* __FREE_CHECKED__ */
545 static void maildir_free_maildir (struct maildir **md)
547 struct maildir *p, *q;
552 for (p = *md; p; p = q)
555 maildir_free_entry (&p);
559 static void maildir_parse_flags (HEADER * h, const char *path)
567 if ((p = strrchr (path, ':')) != NULL && mutt_strncmp (p + 1, "2,", 2) == 0)
571 mutt_str_replace (&h->maildir_flags, p);
572 q = h->maildir_flags;
588 case 'R': /* replied */
593 case 'T': /* trashed */
606 if (q == h->maildir_flags)
607 FREE (&h->maildir_flags);
612 static void maildir_update_mtime (CONTEXT * ctx)
614 char buf[_POSIX_PATH_MAX];
616 struct mh_data *data = mh_data (ctx);
618 if (ctx->magic == M_MAILDIR)
620 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, "cur");
621 if (stat (buf, &st) == 0)
622 data->mtime_cur = st.st_mtime;
623 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, "new");
627 snprintf (buf, sizeof (buf), "%s/.mh_sequences", ctx->path);
628 if (stat (buf, &st) == 0)
629 data->mtime_cur = st.st_mtime;
631 strfcpy (buf, ctx->path, sizeof (buf));
634 if (stat (buf, &st) == 0)
635 ctx->mtime = st.st_mtime;
639 * Actually parse a maildir message. This may also be used to fill
640 * out a fake header structure generated by lazy maildir parsing.
642 static HEADER *maildir_parse_message (int magic, const char *fname,
643 int is_old, HEADER * _h)
649 if ((f = fopen (fname, "r")) != NULL)
652 h = mutt_new_header ();
653 h->env = mutt_read_rfc822_header (f, h, 0, 0);
655 fstat (fileno (f), &st);
659 h->received = h->date_sent;
661 if (h->content->length <= 0)
662 h->content->length = st.st_size - h->content->offset;
666 if (magic == M_MAILDIR)
669 * maildir stores its flags in the filename, so ignore the
670 * flags in the header of the message
674 maildir_parse_flags (h, fname);
681 /* Ignore the garbage files. A valid MH message consists of only
682 * digits. Deleted message get moved to a filename with a comma before
686 int mh_valid_message (const char *s)
690 if (!isdigit ((unsigned char) *s))
696 static int maildir_parse_dir (CONTEXT * ctx, struct maildir ***last,
697 const char *subdir, int *count,
698 progress_t *progress)
702 char buf[_POSIX_PATH_MAX];
704 struct maildir *entry;
709 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, subdir);
710 is_old = (mutt_strcmp ("cur", subdir) == 0);
713 strfcpy (buf, ctx->path, sizeof (buf));
715 if ((dirp = opendir (buf)) == NULL)
718 while ((de = readdir (dirp)) != NULL)
720 if ((ctx->magic == M_MH && !mh_valid_message (de->d_name))
721 || (ctx->magic == M_MAILDIR && *de->d_name == '.'))
724 /* FOO - really ignore the return value? */
726 (debugfile, "%s:%d: queueing %s\n", __FILE__, __LINE__,
729 h = mutt_new_header ();
731 if (ctx->magic == M_MAILDIR)
732 maildir_parse_flags (h, de->d_name);
737 if (!ctx->quiet && progress)
738 mutt_progress_update (progress, *count, -1);
743 char tmp[_POSIX_PATH_MAX];
744 snprintf (tmp, sizeof (tmp), "%s/%s", subdir, de->d_name);
745 h->path = safe_strdup (tmp);
748 h->path = safe_strdup (de->d_name);
750 entry = safe_calloc (sizeof (struct maildir), 1);
752 #ifdef HAVE_DIRENT_D_INO
753 entry->inode = de->d_ino;
754 #endif /* HAVE_DIRENT_D_INO */
756 *last = &entry->next;
764 static int maildir_add_to_context (CONTEXT * ctx, struct maildir *md)
766 int oldmsgcount = ctx->msgcount;
771 dprint (2, (debugfile, "%s:%d maildir_add_to_context(): Considering %s\n",
772 __FILE__, __LINE__, NONULL (md->canon_fname)));
778 "%s:%d Adding header structure. Flags: %s%s%s%s%s\n", __FILE__,
779 __LINE__, md->h->flagged ? "f" : "", md->h->deleted ? "D" : "",
780 md->h->replied ? "r" : "", md->h->old ? "O" : "",
781 md->h->read ? "R" : ""));
782 if (ctx->msgcount == ctx->hdrmax)
783 mx_alloc_memory (ctx);
785 ctx->hdrs[ctx->msgcount] = md->h;
786 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
788 md->h->content->length + md->h->content->offset -
789 md->h->content->hdr_offset;
797 if (ctx->msgcount > oldmsgcount)
799 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
805 static int maildir_move_to_context (CONTEXT * ctx, struct maildir **md)
808 r = maildir_add_to_context (ctx, *md);
809 maildir_free_maildir (md);
814 static size_t maildir_hcache_keylen (const char *fn)
816 const char * p = strrchr (fn, ':');
817 return p ? (size_t) (p - fn) : mutt_strlen(fn);
821 #if HAVE_DIRENT_D_INO
822 static int md_cmp_inode (struct maildir *a, struct maildir *b)
824 return a->inode - b->inode;
828 static int md_cmp_path (struct maildir *a, struct maildir *b)
830 return strcmp (a->h->path, b->h->path);
834 * Merge two maildir lists according to the inode numbers.
836 static struct maildir* maildir_merge_lists (struct maildir *left,
837 struct maildir *right,
838 int (*cmp) (struct maildir *,
841 struct maildir* head;
842 struct maildir* tail;
846 if (cmp (left, right) < 0)
867 while (left && right)
869 if (cmp (left, right) < 0)
894 static struct maildir* maildir_ins_sort (struct maildir* list,
895 int (*cmp) (struct maildir *,
898 struct maildir *tmp, *last, *ret = NULL, *back;
908 for (tmp = ret; tmp && cmp (tmp, list) <= 0; tmp = tmp->next)
924 * Sort maildir list according to inode.
926 static struct maildir* maildir_sort (struct maildir* list, size_t len,
927 int (*cmp) (struct maildir *,
930 struct maildir* left = list;
931 struct maildir* right = list;
934 if (!list || !list->next)
939 if (len != (size_t)(-1) && len <= INS_SORT_THRESHOLD)
940 return maildir_ins_sort (list, cmp);
943 while (list && list->next)
946 list = list->next->next;
954 left = maildir_sort (left, c, cmp);
955 right = maildir_sort (right, c, cmp);
956 return maildir_merge_lists (left, right, cmp);
959 /* Sorts mailbox into it's natural order.
960 * Currently only defined for MH where files are numbered.
962 static void mh_sort_natural (CONTEXT *ctx, struct maildir **md)
964 if (!ctx || !md || !*md || ctx->magic != M_MH || Sort != SORT_ORDER)
966 dprint (4, (debugfile, "maildir: sorting %s into natural order\n",
968 *md = maildir_sort (*md, (size_t) -1, md_cmp_path);
971 #if HAVE_DIRENT_D_INO
972 static struct maildir *skip_duplicates (struct maildir *p, struct maildir **last)
975 * Skip ahead to the next non-duplicate message.
977 * p should never reach NULL, because we couldn't have reached this point unless
978 * there was a message that needed to be parsed.
980 * the check for p->header_parsed is likely unnecessary since the dupes will most
981 * likely be at the head of the list. but it is present for consistency with
982 * the check at the top of the for() loop in maildir_delayed_parsing().
984 while (!p->h || p->header_parsed) {
993 * This function does the second parsing pass
995 static void maildir_delayed_parsing (CONTEXT * ctx, struct maildir **md,
996 progress_t *progress)
998 struct maildir *p, *last = NULL;
999 char fn[_POSIX_PATH_MAX];
1001 #if HAVE_DIRENT_D_INO
1005 header_cache_t *hc = NULL;
1007 struct timeval *when = NULL;
1008 struct stat lastchanged;
1012 #if HAVE_DIRENT_D_INO
1013 #define DO_SORT() do { \
1016 dprint (4, (debugfile, "maildir: need to sort %s by inode\n", ctx->path)); \
1017 p = maildir_sort (p, (size_t) -1, md_cmp_inode); \
1023 p = skip_duplicates (p, &last); \
1024 snprintf (fn, sizeof (fn), "%s/%s", ctx->path, p->h->path); \
1028 #define DO_SORT() /* nothing */
1032 hc = mutt_hcache_open (HeaderCache, ctx->path, NULL);
1035 for (p = *md, count = 0; p; p = p->next, count++)
1037 if (! (p && p->h && !p->header_parsed))
1043 if (!ctx->quiet && progress)
1044 mutt_progress_update (progress, count, -1);
1048 snprintf (fn, sizeof (fn), "%s/%s", ctx->path, p->h->path);
1051 if (option(OPTHCACHEVERIFY))
1053 ret = stat(fn, &lastchanged);
1057 lastchanged.st_mtime = 0;
1061 if (ctx->magic == M_MH)
1062 data = mutt_hcache_fetch (hc, p->h->path, strlen);
1064 data = mutt_hcache_fetch (hc, p->h->path + 3, &maildir_hcache_keylen);
1065 when = (struct timeval *) data;
1067 if (data != NULL && !ret && lastchanged.st_mtime <= when->tv_sec)
1069 p->h = mutt_hcache_restore ((unsigned char *)data, &p->h);
1070 if (ctx->magic == M_MAILDIR)
1071 maildir_parse_flags (p->h, fn);
1075 #endif /* USE_HCACHE */
1077 if (maildir_parse_message (ctx->magic, fn, p->h->old, p->h))
1079 p->header_parsed = 1;
1081 if (ctx->magic == M_MH)
1082 mutt_hcache_store (hc, p->h->path, p->h, 0, strlen);
1084 mutt_hcache_store (hc, p->h->path + 3, p->h, 0, &maildir_hcache_keylen);
1087 mutt_free_header (&p->h);
1095 mutt_hcache_close (hc);
1100 mh_sort_natural (ctx, md);
1103 static int mh_close_mailbox (CONTEXT *ctx)
1110 /* Read a MH/maildir style mailbox.
1113 * ctx [IN/OUT] context for this mailbox
1114 * subdir [IN] NULL for MH mailboxes, otherwise the subdir of the
1115 * maildir mailbox to read from
1117 int mh_read_dir (CONTEXT * ctx, const char *subdir)
1120 struct mh_sequences mhs;
1121 struct maildir **last;
1122 struct mh_data *data;
1124 char msgbuf[STRING];
1125 progress_t progress;
1127 memset (&mhs, 0, sizeof (mhs));
1130 snprintf (msgbuf, sizeof (msgbuf), _("Scanning %s..."), ctx->path);
1131 mutt_progress_init (&progress, msgbuf, M_PROGRESS_MSG, ReadInc, 0);
1136 ctx->data = safe_calloc(sizeof (struct mh_data), 1);
1137 ctx->mx_close = mh_close_mailbox;
1139 data = mh_data (ctx);
1141 maildir_update_mtime (ctx);
1146 if (maildir_parse_dir (ctx, &last, subdir, &count, &progress) == -1)
1151 snprintf (msgbuf, sizeof (msgbuf), _("Reading %s..."), ctx->path);
1152 mutt_progress_init (&progress, msgbuf, M_PROGRESS_MSG, ReadInc, count);
1154 maildir_delayed_parsing (ctx, &md, &progress);
1156 if (ctx->magic == M_MH)
1158 if (mh_read_sequences (&mhs, ctx->path) >= 0)
1160 mh_update_maildir (md, &mhs);
1161 mhs_free_sequences (&mhs);
1164 maildir_move_to_context (ctx, &md);
1166 if (!data->mh_umask)
1167 data->mh_umask = mh_umask (ctx);
1172 /* read a maildir style mailbox */
1173 int maildir_read_dir (CONTEXT * ctx)
1175 /* maildir looks sort of like MH, except that there are two subdirectories
1176 * of the main folder path from which to read messages
1178 if (mh_read_dir (ctx, "new") == -1 || mh_read_dir (ctx, "cur") == -1)
1185 * Open a new (temporary) message in an MH folder.
1188 int mh_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr)
1190 return mh_mkstemp (dest, &msg->fp, &msg->path);
1193 static int ch_compar (const void *a, const void *b)
1195 return (int)( *((const char *) a) - *((const char *) b));
1198 static void maildir_flags (char *dest, size_t destlen, HEADER * hdr)
1203 * The maildir specification requires that all files in the cur
1204 * subdirectory have the :unique string appeneded, regardless of whether
1205 * or not there are any flags. If .old is set, we know that this message
1206 * will end up in the cur directory, so we include it in the following
1207 * test even though there is no associated flag.
1210 if (hdr && (hdr->flagged || hdr->replied || hdr->read || hdr->deleted || hdr->old || hdr->maildir_flags))
1212 char tmp[LONG_STRING];
1213 snprintf (tmp, sizeof (tmp),
1215 hdr->flagged ? "F" : "",
1216 hdr->replied ? "R" : "",
1217 hdr->read ? "S" : "", hdr->deleted ? "T" : "",
1218 NONULL(hdr->maildir_flags));
1219 if (hdr->maildir_flags)
1220 qsort (tmp, strlen (tmp), 1, ch_compar);
1221 snprintf (dest, destlen, ":2,%s", tmp);
1227 * Open a new (temporary) message in a maildir folder.
1229 * Note that this uses _almost_ the maildir file name format, but
1230 * with a {cur,new} prefix.
1234 int maildir_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr)
1237 char path[_POSIX_PATH_MAX];
1244 short deleted = hdr->deleted;
1247 maildir_flags (suffix, sizeof (suffix), hdr);
1249 hdr->deleted = deleted;
1254 if (hdr && (hdr->read || hdr->old))
1255 strfcpy (subdir, "cur", sizeof (subdir));
1257 strfcpy (subdir, "new", sizeof (subdir));
1259 omask = umask (mh_umask (dest));
1262 snprintf (path, _POSIX_PATH_MAX, "%s/tmp/%s.%lld.%u_%d.%s%s",
1263 dest->path, subdir, (long long)time (NULL), (unsigned int)getpid (),
1264 Counter++, NONULL (Hostname), suffix);
1266 dprint (2, (debugfile, "maildir_open_new_message (): Trying %s.\n",
1269 if ((fd = open (path, O_WRONLY | O_EXCL | O_CREAT, 0666)) == -1)
1271 if (errno != EEXIST)
1280 dprint (2, (debugfile, "maildir_open_new_message (): Success.\n"));
1281 msg->path = safe_strdup (path);
1287 if ((msg->fp = fdopen (fd, "w")) == NULL)
1301 * Commit a message to a maildir folder.
1303 * msg->path contains the file name of a file in tmp/. We take the
1304 * flags from this file's name.
1306 * ctx is the mail folder we commit to.
1308 * hdr is a header structure to which we write the message's new
1309 * file name. This is used in the mh and maildir folder synch
1310 * routines. When this routine is invoked from mx_commit_message,
1313 * msg->path looks like this:
1315 * tmp/{cur,new}.mutt-HOSTNAME-PID-COUNTER:flags
1317 * See also maildir_open_new_message().
1321 int maildir_commit_message (CONTEXT * ctx, MESSAGE * msg, HEADER * hdr)
1325 char path[_POSIX_PATH_MAX];
1326 char full[_POSIX_PATH_MAX];
1329 if (safe_fsync_close (&msg->fp))
1331 mutt_perror (_("Could not flush message to disk"));
1335 /* extract the subdir */
1336 s = strrchr (msg->path, '/') + 1;
1337 strfcpy (subdir, s, 4);
1339 /* extract the flags */
1340 if ((s = strchr (s, ':')))
1341 strfcpy (suffix, s, sizeof (suffix));
1345 /* construct a new file name. */
1348 snprintf (path, _POSIX_PATH_MAX, "%s/%lld.%u_%d.%s%s", subdir,
1349 (long long)time (NULL), (unsigned int)getpid (), Counter++,
1350 NONULL (Hostname), suffix);
1351 snprintf (full, _POSIX_PATH_MAX, "%s/%s", ctx->path, path);
1353 dprint (2, (debugfile, "maildir_commit_message (): renaming %s to %s.\n",
1356 if (safe_rename (msg->path, full) == 0)
1359 mutt_str_replace (&hdr->path, path);
1363 * Adjust the mtime on the file to match the time at which this
1364 * message was received. Currently this is only set when copying
1365 * messages between mailboxes, so we test to ensure that it is
1372 ut.actime = msg->received;
1373 ut.modtime = msg->received;
1374 if (utime (full, &ut))
1376 mutt_perror (_("maildir_commit_message(): unable to set time on file"));
1383 else if (errno != EEXIST)
1385 mutt_perror (ctx->path);
1392 * commit a message to an MH folder.
1397 static int _mh_commit_message (CONTEXT * ctx, MESSAGE * msg, HEADER * hdr,
1403 unsigned int n, hi = 0;
1404 char path[_POSIX_PATH_MAX];
1407 if (safe_fsync_close (&msg->fp))
1409 mutt_perror (_("Could not flush message to disk"));
1413 if ((dirp = opendir (ctx->path)) == NULL)
1415 mutt_perror (ctx->path);
1419 /* figure out what the next message number is */
1420 while ((de = readdir (dirp)) != NULL)
1428 if (!isdigit ((unsigned char) *cp))
1442 * Now try to rename the file to the proper name.
1444 * Note: We may have to try multiple times, until we find a free
1451 snprintf (tmp, sizeof (tmp), "%d", hi);
1452 snprintf (path, sizeof (path), "%s/%s", ctx->path, tmp);
1453 if (safe_rename (msg->path, path) == 0)
1456 mutt_str_replace (&hdr->path, tmp);
1460 else if (errno != EEXIST)
1462 mutt_perror (ctx->path);
1467 mh_sequences_add_one (ctx, hi, !msg->flags.read, msg->flags.flagged,
1468 msg->flags.replied);
1472 int mh_commit_message (CONTEXT * ctx, MESSAGE * msg, HEADER * hdr)
1474 return _mh_commit_message (ctx, msg, hdr, 1);
1478 /* Sync a message in an MH folder.
1480 * This code is also used for attachment deletion in maildir
1484 static int mh_rewrite_message (CONTEXT * ctx, int msgno)
1486 HEADER *h = ctx->hdrs[msgno];
1491 char oldpath[_POSIX_PATH_MAX];
1492 char newpath[_POSIX_PATH_MAX];
1493 char partpath[_POSIX_PATH_MAX];
1495 long old_body_offset = h->content->offset;
1496 long old_body_length = h->content->length;
1497 long old_hdr_lines = h->lines;
1499 if ((dest = mx_open_new_message (ctx, h, 0)) == NULL)
1502 if ((rc = mutt_copy_message (dest->fp, ctx, h,
1503 M_CM_UPDATE, CH_UPDATE | CH_UPDATE_LEN)) == 0)
1505 snprintf (oldpath, _POSIX_PATH_MAX, "%s/%s", ctx->path, h->path);
1506 strfcpy (partpath, h->path, _POSIX_PATH_MAX);
1508 if (ctx->magic == M_MAILDIR)
1509 rc = maildir_commit_message (ctx, dest, h);
1511 rc = _mh_commit_message (ctx, dest, h, 0);
1513 mx_close_message (&dest);
1522 * Try to move the new message to the old place.
1525 * This is important when we are just updating flags.
1527 * Note that there is a race condition against programs which
1528 * use the first free slot instead of the maximum message
1529 * number. Mutt does _not_ behave like this.
1531 * Anyway, if this fails, the message is in the folder, so
1532 * all what happens is that a concurrently runnung mutt will
1533 * lose flag modifications.
1536 if (ctx->magic == M_MH && rc == 0)
1538 snprintf (newpath, _POSIX_PATH_MAX, "%s/%s", ctx->path, h->path);
1539 if ((rc = safe_rename (newpath, oldpath)) == 0)
1540 mutt_str_replace (&h->path, partpath);
1544 mx_close_message (&dest);
1546 if (rc == -1 && restore)
1548 h->content->offset = old_body_offset;
1549 h->content->length = old_body_length;
1550 h->lines = old_hdr_lines;
1553 mutt_free_body (&h->content->parts);
1557 static int mh_sync_message (CONTEXT * ctx, int msgno)
1559 HEADER *h = ctx->hdrs[msgno];
1561 if (h->attach_del ||
1562 (h->env && (h->env->refs_changed || h->env->irt_changed)))
1563 if (mh_rewrite_message (ctx, msgno) != 0)
1569 static int maildir_sync_message (CONTEXT * ctx, int msgno)
1571 HEADER *h = ctx->hdrs[msgno];
1573 if (h->attach_del ||
1574 (h->env && (h->env->refs_changed || h->env->irt_changed)))
1576 /* when doing attachment deletion/rethreading, fall back to the MH case. */
1577 if (mh_rewrite_message (ctx, msgno) != 0)
1582 /* we just have to rename the file. */
1584 char newpath[_POSIX_PATH_MAX];
1585 char partpath[_POSIX_PATH_MAX];
1586 char fullpath[_POSIX_PATH_MAX];
1587 char oldpath[_POSIX_PATH_MAX];
1591 if ((p = strrchr (h->path, '/')) == NULL)
1595 "maildir_sync_message: %s: unable to find subdir!\n",
1600 strfcpy (newpath, p, sizeof (newpath));
1602 /* kill the previous flags */
1603 if ((p = strchr (newpath, ':')) != NULL)
1606 maildir_flags (suffix, sizeof (suffix), h);
1608 snprintf (partpath, sizeof (partpath), "%s/%s%s",
1609 (h->read || h->old) ? "cur" : "new", newpath, suffix);
1610 snprintf (fullpath, sizeof (fullpath), "%s/%s", ctx->path, partpath);
1611 snprintf (oldpath, sizeof (oldpath), "%s/%s", ctx->path, h->path);
1613 if (mutt_strcmp (fullpath, oldpath) == 0)
1615 /* message hasn't really changed */
1619 /* record that the message is possibly marked as trashed on disk */
1620 h->trash = h->deleted;
1622 if (rename (oldpath, fullpath) != 0)
1624 mutt_perror ("rename");
1627 mutt_str_replace (&h->path, partpath);
1632 int mh_sync_mailbox (CONTEXT * ctx, int *index_hint)
1634 char path[_POSIX_PATH_MAX], tmp[_POSIX_PATH_MAX];
1637 header_cache_t *hc = NULL;
1638 #endif /* USE_HCACHE */
1639 char msgbuf[STRING];
1640 progress_t progress;
1642 if (ctx->magic == M_MH)
1643 i = mh_check_mailbox (ctx, index_hint);
1645 i = maildir_check_mailbox (ctx, index_hint);
1651 if (ctx->magic == M_MAILDIR || ctx->magic == M_MH)
1652 hc = mutt_hcache_open(HeaderCache, ctx->path, NULL);
1653 #endif /* USE_HCACHE */
1657 snprintf (msgbuf, sizeof (msgbuf), _("Writing %s..."), ctx->path);
1658 mutt_progress_init (&progress, msgbuf, M_PROGRESS_MSG, WriteInc, ctx->msgcount);
1661 for (i = 0; i < ctx->msgcount; i++)
1664 mutt_progress_update (&progress, i, -1);
1666 if (ctx->hdrs[i]->deleted
1667 && (ctx->magic != M_MAILDIR || !option (OPTMAILDIRTRASH)))
1669 snprintf (path, sizeof (path), "%s/%s", ctx->path, ctx->hdrs[i]->path);
1670 if (ctx->magic == M_MAILDIR
1671 || (option (OPTMHPURGE) && ctx->magic == M_MH))
1674 if (ctx->magic == M_MAILDIR)
1675 mutt_hcache_delete (hc, ctx->hdrs[i]->path + 3, &maildir_hcache_keylen);
1676 else if (ctx->magic == M_MH)
1677 mutt_hcache_delete (hc, ctx->hdrs[i]->path, strlen);
1678 #endif /* USE_HCACHE */
1681 else if (ctx->magic == M_MH)
1683 /* MH just moves files out of the way when you delete them */
1684 if (*ctx->hdrs[i]->path != ',')
1686 snprintf (tmp, sizeof (tmp), "%s/,%s", ctx->path,
1687 ctx->hdrs[i]->path);
1694 else if (ctx->hdrs[i]->changed || ctx->hdrs[i]->attach_del ||
1695 (ctx->magic == M_MAILDIR
1696 && (option (OPTMAILDIRTRASH) || ctx->hdrs[i]->trash)
1697 && (ctx->hdrs[i]->deleted != ctx->hdrs[i]->trash)))
1699 if (ctx->magic == M_MAILDIR)
1701 if (maildir_sync_message (ctx, i) == -1)
1706 if (mh_sync_message (ctx, i) == -1)
1712 if (ctx->hdrs[i]->changed)
1714 if (ctx->magic == M_MAILDIR)
1715 mutt_hcache_store (hc, ctx->hdrs[i]->path + 3, ctx->hdrs[i],
1716 0, &maildir_hcache_keylen);
1717 else if (ctx->magic == M_MH)
1718 mutt_hcache_store (hc, ctx->hdrs[i]->path, ctx->hdrs[i], 0, strlen);
1725 if (ctx->magic == M_MAILDIR || ctx->magic == M_MH)
1726 mutt_hcache_close (hc);
1727 #endif /* USE_HCACHE */
1729 if (ctx->magic == M_MH)
1730 mh_update_sequences (ctx);
1732 /* XXX race condition? */
1734 maildir_update_mtime (ctx);
1736 /* adjust indices */
1740 for (i = 0, j = 0; i < ctx->msgcount; i++)
1742 if (!ctx->hdrs[i]->deleted
1743 || (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))
1744 ctx->hdrs[i]->index = j++;
1752 if (ctx->magic == M_MAILDIR || ctx->magic == M_MH)
1753 mutt_hcache_close (hc);
1754 #endif /* USE_HCACHE */
1758 static char *maildir_canon_filename (char *dest, const char *src, size_t l)
1762 if ((t = strrchr (src, '/')))
1765 strfcpy (dest, src, l);
1766 if ((u = strrchr (dest, ':')))
1772 static void maildir_update_tables (CONTEXT *ctx, int *index_hint)
1778 if (Sort != SORT_ORDER)
1782 mutt_sort_headers (ctx, 1);
1786 old_count = ctx->msgcount;
1787 for (i = 0, j = 0; i < old_count; i++)
1789 if (ctx->hdrs[i]->active && index_hint && *index_hint == i)
1792 if (ctx->hdrs[i]->active)
1793 ctx->hdrs[i]->index = j++;
1796 mx_update_tables (ctx, 0);
1797 mutt_clear_threads (ctx);
1800 static void maildir_update_flags (CONTEXT *ctx, HEADER *o, HEADER *n)
1802 /* save the global state here so we can reset it at the
1803 * end of list block if required.
1805 int context_changed = ctx->changed;
1807 /* user didn't modify this message. alter the flags to match the
1808 * current state on disk. This may not actually do
1809 * anything. mutt_set_flag() will just ignore the call if the status
1810 * bits are already properly set, but it is still faster not to pass
1812 if (o->flagged != n->flagged)
1813 mutt_set_flag (ctx, o, M_FLAG, n->flagged);
1814 if (o->replied != n->replied)
1815 mutt_set_flag (ctx, o, M_REPLIED, n->replied);
1816 if (o->read != n->read)
1817 mutt_set_flag (ctx, o, M_READ, n->read);
1818 if (o->old != n->old)
1819 mutt_set_flag (ctx, o, M_OLD, n->old);
1821 /* mutt_set_flag() will set this, but we don't need to
1822 * sync the changes we made because we just updated the
1823 * context to match the current on-disk state of the
1828 /* if the mailbox was not modified before we made these
1829 * changes, unset the changed flag since nothing needs to
1832 if (!context_changed)
1837 /* This function handles arrival of new mail and reopening of
1838 * maildir folders. The basic idea here is we check to see if either
1839 * the new or cur subdirectories have changed, and if so, we scan them
1840 * for the list of files. We check for newly added messages, and
1841 * then merge the flags messages we already knew about. We don't treat
1842 * either subdirectory differently, as mail could be copied directly into
1843 * the cur directory from another agent.
1845 int maildir_check_mailbox (CONTEXT * ctx, int *index_hint)
1847 struct stat st_new; /* status of the "new" subdirectory */
1848 struct stat st_cur; /* status of the "cur" subdirectory */
1849 char buf[_POSIX_PATH_MAX];
1850 int changed = 0; /* bitmask representing which subdirectories
1851 have changed. 0x1 = new, 0x2 = cur */
1852 int occult = 0; /* messages were removed from the mailbox */
1853 int have_new = 0; /* messages were added to the mailbox */
1854 struct maildir *md; /* list of messages in the mailbox */
1855 struct maildir **last, *p;
1857 HASH *fnames; /* hash table for quickly looking up the base filename
1858 for a maildir message */
1859 struct mh_data *data = mh_data (ctx);
1861 /* XXX seems like this check belongs in mx_check_mailbox()
1864 if (!option (OPTCHECKNEW))
1867 snprintf (buf, sizeof (buf), "%s/new", ctx->path);
1868 if (stat (buf, &st_new) == -1)
1871 snprintf (buf, sizeof (buf), "%s/cur", ctx->path);
1872 if (stat (buf, &st_cur) == -1)
1875 /* determine which subdirectories need to be scanned */
1876 if (st_new.st_mtime > ctx->mtime)
1878 if (st_cur.st_mtime > data->mtime_cur)
1882 return 0; /* nothing to do */
1884 /* update the modification times on the mailbox */
1885 data->mtime_cur = st_cur.st_mtime;
1886 ctx->mtime = st_new.st_mtime;
1888 /* do a fast scan of just the filenames in
1889 * the subdirectories that have changed.
1894 maildir_parse_dir (ctx, &last, "new", NULL, NULL);
1896 maildir_parse_dir (ctx, &last, "cur", NULL, NULL);
1898 /* we create a hash table keyed off the canonical (sans flags) filename
1899 * of each message we scanned. This is used in the loop over the
1900 * existing messages below to do some correlation.
1902 fnames = hash_create (1031, 0);
1904 for (p = md; p; p = p->next)
1906 maildir_canon_filename (buf, p->h->path, sizeof (buf));
1907 p->canon_fname = safe_strdup (buf);
1908 hash_insert (fnames, p->canon_fname, p, 0);
1911 /* check for modifications and adjust flags */
1912 for (i = 0; i < ctx->msgcount; i++)
1914 ctx->hdrs[i]->active = 0;
1915 maildir_canon_filename (buf, ctx->hdrs[i]->path, sizeof (buf));
1916 p = hash_find (fnames, buf);
1919 /* message already exists, merge flags */
1920 ctx->hdrs[i]->active = 1;
1922 /* check to see if the message has moved to a different
1923 * subdirectory. If so, update the associated filename.
1925 if (mutt_strcmp (ctx->hdrs[i]->path, p->h->path))
1926 mutt_str_replace (&ctx->hdrs[i]->path, p->h->path);
1928 /* if the user hasn't modified the flags on this message, update
1929 * the flags we just detected.
1931 if (!ctx->hdrs[i]->changed)
1932 maildir_update_flags (ctx, ctx->hdrs[i], p->h);
1934 if (ctx->hdrs[i]->deleted == ctx->hdrs[i]->trash)
1935 ctx->hdrs[i]->deleted = p->h->deleted;
1936 ctx->hdrs[i]->trash = p->h->trash;
1938 /* this is a duplicate of an existing header, so remove it */
1939 mutt_free_header (&p->h);
1941 /* This message was not in the list of messages we just scanned.
1942 * Check to see if we have enough information to know if the
1943 * message has disappeared out from underneath us.
1945 else if (((changed & 1) && (!strncmp (ctx->hdrs[i]->path, "new/", 4))) ||
1946 ((changed & 2) && (!strncmp (ctx->hdrs[i]->path, "cur/", 4))))
1948 /* This message disappeared, so we need to simulate a "reopen"
1949 * event. We know it disappeared because we just scanned the
1950 * subdirectory it used to reside in.
1956 /* This message resides in a subdirectory which was not
1957 * modified, so we assume that it is still present and
1960 ctx->hdrs[i]->active = 1;
1964 /* destroy the file name hash */
1965 hash_destroy (&fnames, NULL);
1967 /* If we didn't just get new mail, update the tables. */
1969 maildir_update_tables (ctx, index_hint);
1971 /* do any delayed parsing we need to do. */
1972 maildir_delayed_parsing (ctx, &md, NULL);
1974 /* Incorporate new messages */
1975 have_new = maildir_move_to_context (ctx, &md);
1977 return occult ? M_REOPENED : (have_new ? M_NEW_MAIL : 0);
1981 * This function handles arrival of new mail and reopening of
1982 * mh/maildir folders. Things are getting rather complex because we
1983 * don't have a well-defined "mailbox order", so the tricks from
1984 * mbox.c and mx.c won't work here.
1986 * Don't change this code unless you _really_ understand what
1991 int mh_check_mailbox (CONTEXT * ctx, int *index_hint)
1993 char buf[_POSIX_PATH_MAX];
1994 struct stat st, st_cur;
1995 short modified = 0, have_new = 0, occult = 0;
1996 struct maildir *md, *p;
1997 struct maildir **last = NULL;
1998 struct mh_sequences mhs;
2001 struct mh_data *data = mh_data (ctx);
2003 if (!option (OPTCHECKNEW))
2006 strfcpy (buf, ctx->path, sizeof (buf));
2007 if (stat (buf, &st) == -1)
2010 /* create .mh_sequences when there isn't one. */
2011 snprintf (buf, sizeof (buf), "%s/.mh_sequences", ctx->path);
2012 if ((i = stat (buf, &st_cur)) == -1 && errno == ENOENT)
2017 if (mh_mkstemp (ctx, &fp, &tmp) == 0)
2020 if (safe_rename (tmp, buf) == -1)
2026 if (i == -1 && stat (buf, &st_cur) == -1)
2029 if (st.st_mtime > ctx->mtime || st_cur.st_mtime > data->mtime_cur)
2035 data->mtime_cur = st_cur.st_mtime;
2036 ctx->mtime = st.st_mtime;
2038 memset (&mhs, 0, sizeof (mhs));
2043 maildir_parse_dir (ctx, &last, NULL, NULL, NULL);
2044 maildir_delayed_parsing (ctx, &md, NULL);
2046 if (mh_read_sequences (&mhs, ctx->path) < 0)
2048 mh_update_maildir (md, &mhs);
2049 mhs_free_sequences (&mhs);
2051 /* check for modifications and adjust flags */
2052 fnames = hash_create (1031, 0);
2054 for (p = md; p; p = p->next)
2055 hash_insert (fnames, p->h->path, p, 0);
2057 for (i = 0; i < ctx->msgcount; i++)
2059 ctx->hdrs[i]->active = 0;
2061 if ((p = hash_find (fnames, ctx->hdrs[i]->path)) && p->h &&
2062 (mbox_strict_cmp_headers (ctx->hdrs[i], p->h)))
2064 ctx->hdrs[i]->active = 1;
2065 /* found the right message */
2066 if (!ctx->hdrs[i]->changed)
2067 maildir_update_flags (ctx, ctx->hdrs[i], p->h);
2069 mutt_free_header (&p->h);
2071 else /* message has disappeared */
2075 /* destroy the file name hash */
2077 hash_destroy (&fnames, NULL);
2079 /* If we didn't just get new mail, update the tables. */
2081 maildir_update_tables (ctx, index_hint);
2083 /* Incorporate new messages */
2084 have_new = maildir_move_to_context (ctx, &md);
2086 return occult ? M_REOPENED : (have_new ? M_NEW_MAIL : 0);
2093 * These functions try to find a message in a maildir folder when it
2094 * has moved under our feet. Note that this code is rather expensive, but
2095 * then again, it's called rarely.
2098 static FILE *_maildir_open_find_message (const char *folder, const char *unique,
2099 const char *subfolder)
2101 char dir[_POSIX_PATH_MAX];
2102 char tunique[_POSIX_PATH_MAX];
2103 char fname[_POSIX_PATH_MAX];
2111 snprintf (dir, sizeof (dir), "%s/%s", folder, subfolder);
2113 if ((dp = opendir (dir)) == NULL)
2119 while ((de = readdir (dp)))
2121 maildir_canon_filename (tunique, de->d_name, sizeof (tunique));
2123 if (!mutt_strcmp (tunique, unique))
2125 snprintf (fname, sizeof (fname), "%s/%s/%s", folder, subfolder,
2127 fp = fopen (fname, "r"); /* __FOPEN_CHECKED__ */
2139 FILE *maildir_open_find_message (const char *folder, const char *msg)
2141 char unique[_POSIX_PATH_MAX];
2144 static unsigned int new_hits = 0, cur_hits = 0; /* simple dynamic optimization */
2146 maildir_canon_filename (unique, msg, sizeof (unique));
2150 _maildir_open_find_message (folder, unique,
2151 new_hits > cur_hits ? "new" : "cur"))
2154 if (new_hits < UINT_MAX && cur_hits < UINT_MAX)
2156 new_hits += (new_hits > cur_hits ? 1 : 0);
2157 cur_hits += (new_hits > cur_hits ? 0 : 1);
2164 _maildir_open_find_message (folder, unique,
2165 new_hits > cur_hits ? "cur" : "new"))
2168 if (new_hits < UINT_MAX && cur_hits < UINT_MAX)
2170 new_hits += (new_hits > cur_hits ? 0 : 1);
2171 cur_hits += (new_hits > cur_hits ? 1 : 0);
2183 * 1 if there are no messages in the mailbox
2184 * 0 if there are messages in the mailbox
2187 int maildir_check_empty (const char *path)
2191 int r = 1; /* assume empty until we find a message */
2192 char realpath[_POSIX_PATH_MAX];
2195 /* Strategy here is to look for any file not beginning with a period */
2198 /* we do "cur" on the first iteration since its more likely that we'll
2199 * find old messages without having to scan both subdirs
2201 snprintf (realpath, sizeof (realpath), "%s/%s", path,
2202 iter == 0 ? "cur" : "new");
2203 if ((dp = opendir (realpath)) == NULL)
2205 while ((de = readdir (dp)))
2207 if (*de->d_name != '.')
2215 } while (r && iter < 2);
2222 * 1 if there are no messages in the mailbox
2223 * 0 if there are messages in the mailbox
2226 int mh_check_empty (const char *path)
2230 int r = 1; /* assume empty until we find a message */
2232 if ((dp = opendir (path)) == NULL)
2234 while ((de = readdir (dp)))
2236 if (mh_valid_message (de->d_name))
2247 int mx_is_maildir (const char *path)
2249 char tmp[_POSIX_PATH_MAX];
2252 snprintf (tmp, sizeof (tmp), "%s/cur", path);
2253 if (stat (tmp, &st) == 0 && S_ISDIR (st.st_mode))
2258 int mx_is_mh (const char *path)
2260 char tmp[_POSIX_PATH_MAX];
2262 snprintf (tmp, sizeof (tmp), "%s/.mh_sequences", path);
2263 if (access (tmp, F_OK) == 0)
2266 snprintf (tmp, sizeof (tmp), "%s/.xmhcache", path);
2267 if (access (tmp, F_OK) == 0)
2270 snprintf (tmp, sizeof (tmp), "%s/.mew_cache", path);
2271 if (access (tmp, F_OK) == 0)
2274 snprintf (tmp, sizeof (tmp), "%s/.mew-cache", path);
2275 if (access (tmp, F_OK) == 0)
2278 snprintf (tmp, sizeof (tmp), "%s/.sylpheed_cache", path);
2279 if (access (tmp, F_OK) == 0)
2283 * ok, this isn't an mh folder, but mh mode can be used to read
2284 * Usenet news from the spool. ;-)
2287 snprintf (tmp, sizeof (tmp), "%s/.overview", path);
2288 if (access (tmp, F_OK) == 0)