2 * Copyright (C) 1996-2002 Michael R. Elkins <me@mutt.org>
3 * Copyright (C) 1999-2002 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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
21 * This file contains routines specific to MH and ``maildir'' style
49 unsigned header_parsed:1;
59 /* mh_sequences support */
61 #define MH_SEQ_UNSEEN (1 << 0)
62 #define MH_SEQ_REPLIED (1 << 1)
63 #define MH_SEQ_FLAGGED (1 << 2)
65 static void mhs_alloc (struct mh_sequences *mhs, int i)
70 if (i > mhs->max || !mhs->flags)
73 safe_realloc (&mhs->flags, sizeof (mhs->flags[0]) * (newmax + 1));
74 for (j = mhs->max + 1; j <= newmax; j++)
81 static void mhs_free_sequences (struct mh_sequences *mhs)
86 static short mhs_check (struct mh_sequences *mhs, int i)
88 if (!mhs->flags || i > mhs->max)
94 static short mhs_set (struct mh_sequences *mhs, int i, short f)
105 static short mhs_unset (struct mh_sequences *mhs, int i, short f)
109 return mhs->flags[i];
114 static void mh_read_token (char *t, int *first, int *last)
117 if ((p = strchr (t, '-')))
124 *first = *last = atoi (t);
127 static void mh_read_sequences (struct mh_sequences *mhs, const char *path)
138 char pathname[_POSIX_PATH_MAX];
139 snprintf (pathname, sizeof (pathname), "%s/.mh_sequences", path);
141 if (!(fp = fopen (pathname, "r")))
144 while ((buff = mutt_read_line (buff, &sz, fp, &line)))
146 if (!(t = strtok (buff, " \t:")))
149 if (!mutt_strcmp (t, MhUnseen))
151 else if (!mutt_strcmp (t, MhFlagged))
153 else if (!mutt_strcmp (t, MhReplied))
155 else /* unknown sequence */
158 while ((t = strtok (NULL, " \t:")))
160 mh_read_token (t, &first, &last);
161 for (; first <= last; first++)
162 mhs_set (mhs, first, f);
170 int mh_buffy (const char *path)
173 struct mh_sequences mhs;
174 memset (&mhs, 0, sizeof (mhs));
176 mh_read_sequences (&mhs, path);
177 for (i = 0; !r && i <= mhs.max; i++)
178 if (mhs_check (&mhs, i) & MH_SEQ_UNSEEN)
180 mhs_free_sequences (&mhs);
184 static int mh_mkstemp (CONTEXT * dest, FILE ** fp, char **tgt)
187 char path[_POSIX_PATH_MAX];
191 snprintf (path, _POSIX_PATH_MAX, "%s/.mutt-%s-%d-%d",
192 dest->path, NONULL (Hostname), (int) getpid (), Counter++);
193 if ((fd = open (path, O_WRONLY | O_EXCL | O_CREAT, 0600)) == -1)
203 *tgt = safe_strdup (path);
208 if ((*fp = fdopen (fd, "w")) == NULL)
219 static void mhs_write_one_sequence (FILE * fp, struct mh_sequences *mhs,
220 short f, const char *tag)
224 fprintf (fp, "%s:", tag);
229 for (i = 0; i <= mhs->max; i++)
231 if ((mhs_check (mhs, i) & f))
241 fprintf (fp, " %d", first);
243 fprintf (fp, " %d-%d", first, last);
253 fprintf (fp, " %d", first);
255 fprintf (fp, " %d-%d", first, last);
261 /* XXX - we don't currently remove deleted messages from sequences we don't know. Should we? */
263 void mh_update_sequences (CONTEXT * ctx)
267 char sequences[_POSIX_PATH_MAX];
279 char seq_unseen[STRING];
280 char seq_replied[STRING];
281 char seq_flagged[STRING];
284 struct mh_sequences mhs;
285 memset (&mhs, 0, sizeof (mhs));
287 snprintf (seq_unseen, sizeof (seq_unseen), "%s:", NONULL (MhUnseen));
288 snprintf (seq_replied, sizeof (seq_replied), "%s:", NONULL (MhReplied));
289 snprintf (seq_flagged, sizeof (seq_flagged), "%s:", NONULL (MhFlagged));
291 if (mh_mkstemp (ctx, &nfp, &tmpfname) != 0)
297 snprintf (sequences, sizeof (sequences), "%s/.mh_sequences", ctx->path);
300 /* first, copy unknown sequences */
301 if ((ofp = fopen (sequences, "r")))
303 while ((buff = mutt_read_line (buff, &s, ofp, &l)))
305 if (!mutt_strncmp (buff, seq_unseen, mutt_strlen (seq_unseen)))
307 if (!mutt_strncmp (buff, seq_flagged, mutt_strlen (seq_flagged)))
309 if (!mutt_strncmp (buff, seq_replied, mutt_strlen (seq_replied)))
312 fprintf (nfp, "%s\n", buff);
317 /* now, update our unseen, flagged, and replied sequences */
318 for (l = 0; l < ctx->msgcount; l++)
320 if (ctx->hdrs[l]->deleted)
323 if ((p = strrchr (ctx->hdrs[l]->path, '/')))
326 p = ctx->hdrs[l]->path;
330 if (!ctx->hdrs[l]->read)
332 mhs_set (&mhs, i, MH_SEQ_UNSEEN);
335 if (ctx->hdrs[l]->flagged)
337 mhs_set (&mhs, i, MH_SEQ_FLAGGED);
340 if (ctx->hdrs[l]->replied)
342 mhs_set (&mhs, i, MH_SEQ_REPLIED);
347 /* write out the new sequences */
349 mhs_write_one_sequence (nfp, &mhs, MH_SEQ_UNSEEN, NONULL (MhUnseen));
351 mhs_write_one_sequence (nfp, &mhs, MH_SEQ_FLAGGED, NONULL (MhFlagged));
353 mhs_write_one_sequence (nfp, &mhs, MH_SEQ_REPLIED, NONULL (MhReplied));
355 mhs_free_sequences (&mhs);
358 /* try to commit the changes - no guarantee here */
362 if (safe_rename (tmpfname, sequences) != 0)
364 /* report an error? */
371 static void mh_sequences_add_one (CONTEXT * ctx, int n, short unseen,
372 short flagged, short replied)
374 short unseen_done = 0;
375 short flagged_done = 0;
376 short replied_done = 0;
378 FILE *ofp = NULL, *nfp = NULL;
381 char sequences[_POSIX_PATH_MAX];
383 char seq_unseen[STRING];
384 char seq_replied[STRING];
385 char seq_flagged[STRING];
391 if (mh_mkstemp (ctx, &nfp, &tmpfname) == -1)
394 snprintf (seq_unseen, sizeof (seq_unseen), "%s:", NONULL (MhUnseen));
395 snprintf (seq_replied, sizeof (seq_replied), "%s:", NONULL (MhReplied));
396 snprintf (seq_flagged, sizeof (seq_flagged), "%s:", NONULL (MhFlagged));
398 snprintf (sequences, sizeof (sequences), "%s/.mh_sequences", ctx->path);
399 if ((ofp = fopen (sequences, "r")))
401 while ((buff = mutt_read_line (buff, &sz, ofp, &line)))
403 if (unseen && !strncmp (buff, seq_unseen, mutt_strlen (seq_unseen)))
405 fprintf (nfp, "%s %d\n", buff, n);
409 && !strncmp (buff, seq_flagged, mutt_strlen (seq_flagged)))
411 fprintf (nfp, "%s %d\n", buff, n);
415 && !strncmp (buff, seq_replied, mutt_strlen (seq_replied)))
417 fprintf (nfp, "%s %d\n", buff, n);
421 fprintf (nfp, "%s\n", buff);
427 if (!unseen_done && unseen)
428 fprintf (nfp, "%s: %d\n", NONULL (MhUnseen), n);
429 if (!flagged_done && flagged)
430 fprintf (nfp, "%s: %d\n", NONULL (MhFlagged), n);
431 if (!replied_done && replied)
432 fprintf (nfp, "%s: %d\n", NONULL (MhReplied), n);
437 if (safe_rename (tmpfname, sequences) != 0)
443 static void mh_update_maildir (struct maildir *md, struct mh_sequences *mhs)
449 for (; md; md = md->next)
451 if ((p = strrchr (md->h->path, '/')))
457 f = mhs_check (mhs, i);
459 md->h->read = (f & MH_SEQ_UNSEEN) ? 0 : 1;
460 md->h->flagged = (f & MH_SEQ_FLAGGED) ? 1 : 0;
461 md->h->replied = (f & MH_SEQ_REPLIED) ? 1 : 0;
465 /* maildir support */
467 static void maildir_free_entry (struct maildir **md)
472 FREE (&(*md)->canon_fname);
474 mutt_free_header (&(*md)->h);
479 static void maildir_free_maildir (struct maildir **md)
481 struct maildir *p, *q;
486 for (p = *md; p; p = q)
489 maildir_free_entry (&p);
493 static void maildir_parse_flags (HEADER * h, const char *path)
501 if ((p = strrchr (path, ':')) != NULL && mutt_strncmp (p + 1, "2,", 2) == 0)
505 mutt_str_replace (&h->maildir_flags, p);
506 q = h->maildir_flags;
522 case 'R': /* replied */
527 case 'T': /* trashed */
540 if (q == h->maildir_flags)
541 FREE (&h->maildir_flags);
546 static void maildir_update_mtime (CONTEXT * ctx)
548 char buf[_POSIX_PATH_MAX];
551 if (ctx->magic == M_MAILDIR)
553 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, "cur");
554 if (stat (buf, &st) == 0)
555 ctx->mtime_cur = st.st_mtime;
556 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, "new");
560 snprintf (buf, sizeof (buf), "%s/.mh_sequences", ctx->path);
561 if (stat (buf, &st) == 0)
562 ctx->mtime_cur = st.st_mtime;
564 strfcpy (buf, ctx->path, sizeof (buf));
567 if (stat (buf, &st) == 0)
568 ctx->mtime = st.st_mtime;
572 * Actually parse a maildir message. This may also be used to fill
573 * out a fake header structure generated by lazy maildir parsing.
575 static HEADER *maildir_parse_message (int magic, const char *fname,
576 int is_old, HEADER * _h)
582 if ((f = fopen (fname, "r")) != NULL)
585 h = mutt_new_header ();
586 h->env = mutt_read_rfc822_header (f, h, 0, 0);
588 fstat (fileno (f), &st);
592 h->received = h->date_sent;
594 if (h->content->length <= 0)
595 h->content->length = st.st_size - h->content->offset;
599 if (magic == M_MAILDIR)
602 * maildir stores its flags in the filename, so ignore the
603 * flags in the header of the message
607 maildir_parse_flags (h, fname);
615 * Note that this routine will _not_ modify the context given by
618 * It's used in the first parsing pass on maildir and MH folders.
619 * In the MH case, this means full parsing of the folder. In the
620 * maildir case, it means that we only look at flags, and create a
621 * fake HEADER structure, which may later be filled in by
622 * maildir_parse_message(), when called from
623 * maildir_delayed_parsing().
627 static int maildir_parse_entry (CONTEXT * ctx, struct maildir ***last,
628 const char *subdir, const char *fname,
629 int *count, int is_old)
631 struct maildir *entry;
633 char buf[_POSIX_PATH_MAX];
636 snprintf (buf, sizeof (buf), "%s/%s/%s", ctx->path, subdir, fname);
638 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, fname);
640 if (ctx->magic == M_MH)
641 h = maildir_parse_message (ctx->magic, buf, is_old, NULL);
644 h = mutt_new_header ();
646 maildir_parse_flags (h, buf);
654 if (!ctx->quiet && ReadInc && ((*count % ReadInc) == 0 || *count == 1))
655 mutt_message (_("Reading %s... %d"), ctx->path, *count);
660 snprintf (buf, sizeof (buf), "%s/%s", subdir, fname);
661 h->path = safe_strdup (buf);
664 h->path = safe_strdup (fname);
666 entry = safe_calloc (sizeof (struct maildir), 1);
668 entry->header_parsed = (ctx->magic == M_MH);
670 *last = &entry->next;
680 /* Ignore the garbage files. A valid MH message consists of only
681 * digits. Deleted message get moved to a filename with a comma before
685 int mh_valid_message (const char *s)
689 if (!isdigit ((unsigned char) *s))
695 static int maildir_parse_dir (CONTEXT * ctx, struct maildir ***last,
696 const char *subdir, int *count)
700 char buf[_POSIX_PATH_MAX];
705 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, subdir);
706 is_old = (mutt_strcmp ("cur", subdir) == 0);
709 strfcpy (buf, ctx->path, sizeof (buf));
711 if ((dirp = opendir (buf)) == NULL)
714 while ((de = readdir (dirp)) != NULL)
717 if ((ctx->magic == M_MH && !mh_valid_message (de->d_name))
718 || (ctx->magic == M_MAILDIR && *de->d_name == '.'))
721 /* FOO - really ignore the return value? */
724 (debugfile, "%s:%d: parsing %s\n", __FILE__, __LINE__,
726 maildir_parse_entry (ctx, last, subdir, de->d_name, count, is_old);
733 static int maildir_add_to_context (CONTEXT * ctx, struct maildir *md)
735 int oldmsgcount = ctx->msgcount;
740 dprint (2, (debugfile, "%s:%d maildir_add_to_context(): Considering %s\n",
741 __FILE__, __LINE__, NONULL (md->canon_fname)));
747 "%s:%d Adding header structure. Flags: %s%s%s%s%s\n", __FILE__,
748 __LINE__, md->h->flagged ? "f" : "", md->h->deleted ? "D" : "",
749 md->h->replied ? "r" : "", md->h->old ? "O" : "",
750 md->h->read ? "R" : ""));
751 if (ctx->msgcount == ctx->hdrmax)
752 mx_alloc_memory (ctx);
754 ctx->hdrs[ctx->msgcount] = md->h;
755 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
757 md->h->content->length + md->h->content->offset -
758 md->h->content->hdr_offset;
766 if (ctx->msgcount > oldmsgcount)
768 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
774 static int maildir_move_to_context (CONTEXT * ctx, struct maildir **md)
777 r = maildir_add_to_context (ctx, *md);
778 maildir_free_maildir (md);
784 * This function does the second parsing pass for a maildir-style
788 void maildir_delayed_parsing (CONTEXT * ctx, struct maildir *md)
791 char fn[_POSIX_PATH_MAX];
793 for (p = md; p; p = p->next)
794 if (p && p->h && !p->header_parsed)
796 snprintf (fn, sizeof (fn), "%s/%s", ctx->path, p->h->path);
797 if (maildir_parse_message (ctx->magic, fn, p->h->old, p->h))
798 p->header_parsed = 1;
800 mutt_free_header (&p->h);
806 /* Read a MH/maildir style mailbox.
809 * ctx [IN/OUT] context for this mailbox
810 * subdir [IN] NULL for MH mailboxes, otherwise the subdir of the
811 * maildir mailbox to read from
813 int mh_read_dir (CONTEXT * ctx, const char *subdir)
816 struct mh_sequences mhs;
817 struct maildir **last;
821 memset (&mhs, 0, sizeof (mhs));
823 maildir_update_mtime (ctx);
828 if (maildir_parse_dir (ctx, &last, subdir, &count) == -1)
831 if (ctx->magic == M_MH)
833 mh_read_sequences (&mhs, ctx->path);
834 mh_update_maildir (md, &mhs);
835 mhs_free_sequences (&mhs);
838 if (ctx->magic == M_MAILDIR)
839 maildir_delayed_parsing (ctx, md);
841 maildir_move_to_context (ctx, &md);
845 /* read a maildir style mailbox */
846 int maildir_read_dir (CONTEXT * ctx)
848 /* maildir looks sort of like MH, except that there are two subdirectories
849 * of the main folder path from which to read messages
851 if (mh_read_dir (ctx, "new") == -1 || mh_read_dir (ctx, "cur") == -1)
858 * Open a new (temporary) message in an MH folder.
861 int mh_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr)
863 return mh_mkstemp (dest, &msg->fp, &msg->path);
866 int ch_compar (const void *a, const void *b)
868 return (int)( *((const char *) a) - *((const char *) b));
871 static void maildir_flags (char *dest, size_t destlen, HEADER * hdr)
876 * The maildir specification requires that all files in the cur
877 * subdirectory have the :unique string appeneded, regardless of whether
878 * or not there are any flags. If .old is set, we know that this message
879 * will end up in the cur directory, so we include it in the following
880 * test even though there is no associated flag.
883 if (hdr && (hdr->flagged || hdr->replied || hdr->read || hdr->deleted || hdr->old || hdr->maildir_flags))
885 char tmp[LONG_STRING];
886 snprintf (tmp, sizeof (tmp),
888 hdr->flagged ? "F" : "",
889 hdr->replied ? "R" : "",
890 hdr->read ? "S" : "", hdr->deleted ? "T" : "",
891 NONULL(hdr->maildir_flags));
892 if (hdr->maildir_flags)
893 qsort (tmp, strlen (tmp), 1, ch_compar);
894 snprintf (dest, destlen, ":2,%s", tmp);
900 * Open a new (temporary) message in a maildir folder.
902 * Note that this uses _almost_ the maildir file name format, but
903 * with a {cur,new} prefix.
907 int maildir_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr)
910 char path[_POSIX_PATH_MAX];
916 short deleted = hdr->deleted;
919 maildir_flags (suffix, sizeof (suffix), hdr);
921 hdr->deleted = deleted;
926 if (hdr && (hdr->read || hdr->old))
927 strfcpy (subdir, "cur", sizeof (subdir));
929 strfcpy (subdir, "new", sizeof (subdir));
933 snprintf (path, _POSIX_PATH_MAX, "%s/tmp/%s.%ld.%d_%d.%s%s",
934 dest->path, subdir, time (NULL), getpid (), Counter++,
935 NONULL (Hostname), suffix);
937 dprint (2, (debugfile, "maildir_open_new_message (): Trying %s.\n",
940 if ((fd = open (path, O_WRONLY | O_EXCL | O_CREAT, 0600)) == -1)
950 dprint (2, (debugfile, "maildir_open_new_message (): Success.\n"));
951 msg->path = safe_strdup (path);
956 if ((msg->fp = fdopen (fd, "w")) == NULL)
970 * Commit a message to a maildir folder.
972 * msg->path contains the file name of a file in tmp/. We take the
973 * flags from this file's name.
975 * ctx is the mail folder we commit to.
977 * hdr is a header structure to which we write the message's new
978 * file name. This is used in the mh and maildir folder synch
979 * routines. When this routine is invoked from mx_commit_message,
982 * msg->path looks like this:
984 * tmp/{cur,new}.mutt-HOSTNAME-PID-COUNTER:flags
986 * See also maildir_open_new_message().
990 int maildir_commit_message (CONTEXT * ctx, MESSAGE * msg, HEADER * hdr)
994 char path[_POSIX_PATH_MAX];
995 char full[_POSIX_PATH_MAX];
998 if (safe_fclose (&msg->fp) != 0)
1001 /* extract the subdir */
1002 s = strrchr (msg->path, '/') + 1;
1003 strfcpy (subdir, s, 4);
1005 /* extract the flags */
1006 if ((s = strchr (s, ':')))
1007 strfcpy (suffix, s, sizeof (suffix));
1011 /* construct a new file name. */
1014 snprintf (path, _POSIX_PATH_MAX, "%s/%ld.%d_%d.%s%s", subdir,
1015 time (NULL), getpid (), Counter++, NONULL (Hostname), suffix);
1016 snprintf (full, _POSIX_PATH_MAX, "%s/%s", ctx->path, path);
1018 dprint (2, (debugfile, "maildir_commit_message (): renaming %s to %s.\n",
1021 if (safe_rename (msg->path, full) == 0)
1024 mutt_str_replace (&hdr->path, path);
1028 * Adjust the mtime on the file to match the time at which this
1029 * message was received. Currently this is only set when copying
1030 * messages between mailboxes, so we test to ensure that it is
1037 ut.actime = msg->received;
1038 ut.modtime = msg->received;
1039 if (utime (full, &ut))
1041 mutt_perror (_("maildir_commit_message(): unable to set time on file"));
1048 else if (errno != EEXIST)
1050 mutt_perror (ctx->path);
1057 * commit a message to an MH folder.
1062 static int _mh_commit_message (CONTEXT * ctx, MESSAGE * msg, HEADER * hdr,
1068 unsigned int n, hi = 0;
1069 char path[_POSIX_PATH_MAX];
1072 if (safe_fclose (&msg->fp) != 0)
1075 if ((dirp = opendir (ctx->path)) == NULL)
1077 mutt_perror (ctx->path);
1081 /* figure out what the next message number is */
1082 while ((de = readdir (dirp)) != NULL)
1090 if (!isdigit ((unsigned char) *cp))
1104 * Now try to rename the file to the proper name.
1106 * Note: We may have to try multiple times, until we find a free
1113 snprintf (tmp, sizeof (tmp), "%d", hi);
1114 snprintf (path, sizeof (path), "%s/%s", ctx->path, tmp);
1115 if (safe_rename (msg->path, path) == 0)
1118 mutt_str_replace (&hdr->path, tmp);
1122 else if (errno != EEXIST)
1124 mutt_perror (ctx->path);
1129 mh_sequences_add_one (ctx, hi, !msg->flags.read, msg->flags.flagged,
1130 msg->flags.replied);
1134 int mh_commit_message (CONTEXT * ctx, MESSAGE * msg, HEADER * hdr)
1136 return _mh_commit_message (ctx, msg, hdr, 1);
1140 /* Sync a message in an MH folder.
1142 * This code is also used for attachment deletion in maildir
1146 static int mh_rewrite_message (CONTEXT * ctx, int msgno)
1148 HEADER *h = ctx->hdrs[msgno];
1153 char oldpath[_POSIX_PATH_MAX];
1154 char newpath[_POSIX_PATH_MAX];
1155 char partpath[_POSIX_PATH_MAX];
1157 long old_body_offset = h->content->offset;
1158 long old_body_length = h->content->length;
1159 long old_hdr_lines = h->lines;
1161 if ((dest = mx_open_new_message (ctx, h, 0)) == NULL)
1164 if ((rc = mutt_copy_message (dest->fp, ctx, h,
1165 M_CM_UPDATE, CH_UPDATE | CH_UPDATE_LEN)) == 0)
1167 snprintf (oldpath, _POSIX_PATH_MAX, "%s/%s", ctx->path, h->path);
1168 strfcpy (partpath, h->path, _POSIX_PATH_MAX);
1170 if (ctx->magic == M_MAILDIR)
1171 rc = maildir_commit_message (ctx, dest, h);
1173 rc = _mh_commit_message (ctx, dest, h, 0);
1175 mx_close_message (&dest);
1184 * Try to move the new message to the old place.
1187 * This is important when we are just updating flags.
1189 * Note that there is a race condition against programs which
1190 * use the first free slot instead of the maximum message
1191 * number. Mutt does _not_ behave like this.
1193 * Anyway, if this fails, the message is in the folder, so
1194 * all what happens is that a concurrently runnung mutt will
1195 * lose flag modifications.
1198 if (ctx->magic == M_MH && rc == 0)
1200 snprintf (newpath, _POSIX_PATH_MAX, "%s/%s", ctx->path, h->path);
1201 if ((rc = safe_rename (newpath, oldpath)) == 0)
1202 mutt_str_replace (&h->path, partpath);
1206 mx_close_message (&dest);
1208 if (rc == -1 && restore)
1210 h->content->offset = old_body_offset;
1211 h->content->length = old_body_length;
1212 h->lines = old_hdr_lines;
1215 mutt_free_body (&h->content->parts);
1219 static int mh_sync_message (CONTEXT * ctx, int msgno)
1221 HEADER *h = ctx->hdrs[msgno];
1224 if (mh_rewrite_message (ctx, msgno) != 0)
1230 static int maildir_sync_message (CONTEXT * ctx, int msgno)
1232 HEADER *h = ctx->hdrs[msgno];
1236 /* when doing attachment deletion, fall back to the MH case. */
1237 if (mh_rewrite_message (ctx, msgno) != 0)
1242 /* we just have to rename the file. */
1244 char newpath[_POSIX_PATH_MAX];
1245 char partpath[_POSIX_PATH_MAX];
1246 char fullpath[_POSIX_PATH_MAX];
1247 char oldpath[_POSIX_PATH_MAX];
1251 if ((p = strrchr (h->path, '/')) == NULL)
1255 "maildir_sync_message: %s: unable to find subdir!\n",
1260 strfcpy (newpath, p, sizeof (newpath));
1262 /* kill the previous flags */
1263 if ((p = strchr (newpath, ':')) != NULL)
1266 maildir_flags (suffix, sizeof (suffix), h);
1268 snprintf (partpath, sizeof (partpath), "%s/%s%s",
1269 (h->read || h->old) ? "cur" : "new", newpath, suffix);
1270 snprintf (fullpath, sizeof (fullpath), "%s/%s", ctx->path, partpath);
1271 snprintf (oldpath, sizeof (oldpath), "%s/%s", ctx->path, h->path);
1273 if (mutt_strcmp (fullpath, oldpath) == 0)
1275 /* message hasn't really changed */
1279 /* record that the message is possibly marked as trashed on disk */
1280 h->trash = h->deleted;
1282 if (rename (oldpath, fullpath) != 0)
1284 mutt_perror ("rename");
1287 mutt_str_replace (&h->path, partpath);
1292 int mh_sync_mailbox (CONTEXT * ctx, int *index_hint)
1294 char path[_POSIX_PATH_MAX], tmp[_POSIX_PATH_MAX];
1297 if (ctx->magic == M_MH)
1298 i = mh_check_mailbox (ctx, index_hint);
1300 i = maildir_check_mailbox (ctx, index_hint);
1305 for (i = 0; i < ctx->msgcount; i++)
1307 if (ctx->hdrs[i]->deleted
1308 && (ctx->magic != M_MAILDIR || !option (OPTMAILDIRTRASH)))
1310 snprintf (path, sizeof (path), "%s/%s", ctx->path, ctx->hdrs[i]->path);
1311 if (ctx->magic == M_MAILDIR
1312 || (option (OPTMHPURGE) && ctx->magic == M_MH))
1314 else if (ctx->magic == M_MH)
1316 /* MH just moves files out of the way when you delete them */
1317 if (*ctx->hdrs[i]->path != ',')
1319 snprintf (tmp, sizeof (tmp), "%s/,%s", ctx->path,
1320 ctx->hdrs[i]->path);
1327 else if (ctx->hdrs[i]->changed || ctx->hdrs[i]->attach_del ||
1328 (ctx->magic == M_MAILDIR
1329 && (option (OPTMAILDIRTRASH) || ctx->hdrs[i]->trash)
1330 && (ctx->hdrs[i]->deleted != ctx->hdrs[i]->trash)))
1332 if (ctx->magic == M_MAILDIR)
1334 if (maildir_sync_message (ctx, i) == -1)
1339 if (mh_sync_message (ctx, i) == -1)
1345 if (ctx->magic == M_MH)
1346 mh_update_sequences (ctx);
1348 /* XXX race condition? */
1350 maildir_update_mtime (ctx);
1352 /* adjust indices */
1356 for (i = 0, j = 0; i < ctx->msgcount; i++)
1358 if (!ctx->hdrs[i]->deleted
1359 || (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))
1360 ctx->hdrs[i]->index = j++;
1367 static char *maildir_canon_filename (char *dest, const char *src, size_t l)
1371 if ((t = strrchr (src, '/')))
1374 strfcpy (dest, src, l);
1375 if ((u = strrchr (dest, ':')))
1381 static void maildir_update_tables (CONTEXT *ctx, int *index_hint)
1387 if (Sort != SORT_ORDER)
1391 mutt_sort_headers (ctx, 1);
1395 old_count = ctx->msgcount;
1396 for (i = 0, j = 0; i < old_count; i++)
1398 if (ctx->hdrs[i]->active && index_hint && *index_hint == i)
1401 if (ctx->hdrs[i]->active)
1402 ctx->hdrs[i]->index = j++;
1405 mx_update_tables (ctx, 0);
1406 mutt_clear_threads (ctx);
1409 static void maildir_update_flags (CONTEXT *ctx, HEADER *o, HEADER *n)
1411 /* save the global state here so we can reset it at the
1412 * end of list block if required.
1414 int context_changed = ctx->changed;
1416 /* user didn't modify this message. alter the flags to
1417 * match the current state on disk. This may not actually
1418 * do anything, but we can't tell right now. mutt_set_flag()
1419 * will just ignore the call if the status bits are
1420 * already properly set.
1422 mutt_set_flag (ctx, o, M_FLAG, n->flagged);
1423 mutt_set_flag (ctx, o, M_REPLIED, n->replied);
1424 mutt_set_flag (ctx, o, M_READ, n->read);
1425 mutt_set_flag (ctx, o, M_OLD, n->old);
1427 /* mutt_set_flag() will set this, but we don't need to
1428 * sync the changes we made because we just updated the
1429 * context to match the current on-disk state of the
1434 /* if the mailbox was not modified before we made these
1435 * changes, unset the changed flag since nothing needs to
1438 if (!context_changed)
1443 /* This function handles arrival of new mail and reopening of
1444 * maildir folders. The basic idea here is we check to see if either
1445 * the new or cur subdirectories have changed, and if so, we scan them
1446 * for the list of files. We check for newly added messages, and
1447 * then merge the flags messages we already knew about. We don't treat
1448 * either subdirectory differently, as mail could be copied directly into
1449 * the cur directory from another agent.
1451 int maildir_check_mailbox (CONTEXT * ctx, int *index_hint)
1453 struct stat st_new; /* status of the "new" subdirectory */
1454 struct stat st_cur; /* status of the "cur" subdirectory */
1455 char buf[_POSIX_PATH_MAX];
1456 int changed = 0; /* bitmask representing which subdirectories
1457 have changed. 0x1 = new, 0x2 = cur */
1458 int occult = 0; /* messages were removed from the mailbox */
1459 int have_new = 0; /* messages were added to the mailbox */
1460 struct maildir *md; /* list of messages in the mailbox */
1461 struct maildir **last, *p;
1463 HASH *fnames; /* hash table for quickly looking up the base filename
1464 for a maildir message */
1466 /* XXX seems like this check belongs in mx_check_mailbox()
1469 if (!option (OPTCHECKNEW))
1472 snprintf (buf, sizeof (buf), "%s/new", ctx->path);
1473 if (stat (buf, &st_new) == -1)
1476 snprintf (buf, sizeof (buf), "%s/cur", ctx->path);
1477 if (stat (buf, &st_cur) == -1)
1480 /* determine which subdirectories need to be scanned */
1481 if (st_new.st_mtime > ctx->mtime)
1483 if (st_cur.st_mtime > ctx->mtime_cur)
1487 return 0; /* nothing to do */
1489 /* update the modification times on the mailbox */
1490 ctx->mtime_cur = st_cur.st_mtime;
1491 ctx->mtime = st_new.st_mtime;
1493 /* do a fast scan of just the filenames in
1494 * the subdirectories that have changed.
1499 maildir_parse_dir (ctx, &last, "new", NULL);
1501 maildir_parse_dir (ctx, &last, "cur", NULL);
1503 /* we create a hash table keyed off the canonical (sans flags) filename
1504 * of each message we scanned. This is used in the loop over the
1505 * existing messages below to do some correlation.
1507 fnames = hash_create (1031);
1509 for (p = md; p; p = p->next)
1511 maildir_canon_filename (buf, p->h->path, sizeof (buf));
1512 p->canon_fname = safe_strdup (buf);
1513 hash_insert (fnames, p->canon_fname, p, 0);
1516 /* check for modifications and adjust flags */
1517 for (i = 0; i < ctx->msgcount; i++)
1519 ctx->hdrs[i]->active = 0;
1520 maildir_canon_filename (buf, ctx->hdrs[i]->path, sizeof (buf));
1521 p = hash_find (fnames, buf);
1524 /* message already exists, merge flags */
1525 ctx->hdrs[i]->active = 1;
1527 /* check to see if the message has moved to a different
1528 * subdirectory. If so, update the associated filename.
1530 if (mutt_strcmp (ctx->hdrs[i]->path, p->h->path))
1531 mutt_str_replace (&ctx->hdrs[i]->path, p->h->path);
1533 /* if the user hasn't modified the flags on this message, update
1534 * the flags we just detected.
1536 if (!ctx->hdrs[i]->changed)
1537 maildir_update_flags (ctx, ctx->hdrs[i], p->h);
1539 if (ctx->hdrs[i]->deleted == ctx->hdrs[i]->trash)
1540 ctx->hdrs[i]->deleted = p->h->deleted;
1541 ctx->hdrs[i]->trash = p->h->trash;
1543 /* this is a duplicate of an existing header, so remove it */
1544 mutt_free_header (&p->h);
1546 /* This message was not in the list of messages we just scanned.
1547 * Check to see if we have enough information to know if the
1548 * message has disappeared out from underneath us.
1550 else if (((changed & 1) && (!strncmp (ctx->hdrs[i]->path, "new/", 4))) ||
1551 ((changed & 2) && (!strncmp (ctx->hdrs[i]->path, "cur/", 4))))
1553 /* This message disappeared, so we need to simulate a "reopen"
1554 * event. We know it disappeared because we just scanned the
1555 * subdirectory it used to reside in.
1561 /* This message resides in a subdirectory which was not
1562 * modified, so we assume that it is still present and
1565 ctx->hdrs[i]->active = 1;
1569 /* destroy the file name hash */
1570 hash_destroy (&fnames, NULL);
1572 /* If we didn't just get new mail, update the tables. */
1574 maildir_update_tables (ctx, index_hint);
1576 /* do any delayed parsing we need to do. */
1577 maildir_delayed_parsing (ctx, md);
1579 /* Incorporate new messages */
1580 have_new = maildir_move_to_context (ctx, &md);
1582 return occult ? M_REOPENED : (have_new ? M_NEW_MAIL : 0);
1586 * This function handles arrival of new mail and reopening of
1587 * mh/maildir folders. Things are getting rather complex because we
1588 * don't have a well-defined "mailbox order", so the tricks from
1589 * mbox.c and mx.c won't work here.
1591 * Don't change this code unless you _really_ understand what
1596 int mh_check_mailbox (CONTEXT * ctx, int *index_hint)
1598 char buf[_POSIX_PATH_MAX];
1599 struct stat st, st_cur;
1600 short modified = 0, have_new = 0, occult = 0;
1601 struct maildir *md, *p;
1602 struct maildir **last = NULL;
1603 struct mh_sequences mhs;
1607 if (!option (OPTCHECKNEW))
1610 strfcpy (buf, ctx->path, sizeof (buf));
1611 if (stat (buf, &st) == -1)
1614 /* create .mh_sequences when there isn't one. */
1615 snprintf (buf, sizeof (buf), "%s/.mh_sequences", ctx->path);
1616 if ((i = stat (buf, &st_cur) == -1) && errno == ENOENT)
1621 if (mh_mkstemp (ctx, &fp, &tmp) == 0)
1624 if (safe_rename (tmp, buf) == -1)
1630 if (i == -1 && stat (buf, &st_cur) == -1)
1633 if (st.st_mtime > ctx->mtime || st_cur.st_mtime > ctx->mtime_cur)
1639 ctx->mtime_cur = st_cur.st_mtime;
1640 ctx->mtime = st.st_mtime;
1642 memset (&mhs, 0, sizeof (mhs));
1646 maildir_parse_dir (ctx, &last, NULL, NULL);
1647 mh_read_sequences (&mhs, ctx->path);
1648 mh_update_maildir (md, &mhs);
1649 mhs_free_sequences (&mhs);
1651 /* check for modifications and adjust flags */
1652 fnames = hash_create (1031);
1654 for (p = md; p; p = p->next)
1655 hash_insert (fnames, p->h->path, p, 0);
1657 for (i = 0; i < ctx->msgcount; i++)
1659 ctx->hdrs[i]->active = 0;
1661 if ((p = hash_find (fnames, ctx->hdrs[i]->path)) && p->h &&
1662 (mbox_strict_cmp_headers (ctx->hdrs[i], p->h)))
1664 ctx->hdrs[i]->active = 1;
1665 /* found the right message */
1666 if (!ctx->hdrs[i]->changed)
1667 maildir_update_flags (ctx, ctx->hdrs[i], p->h);
1669 mutt_free_header (&p->h);
1671 else /* message has disappeared */
1675 /* destroy the file name hash */
1677 hash_destroy (&fnames, NULL);
1679 /* If we didn't just get new mail, update the tables. */
1681 maildir_update_tables (ctx, index_hint);
1683 /* Incorporate new messages */
1684 have_new = maildir_move_to_context (ctx, &md);
1686 return occult ? M_REOPENED : (have_new ? M_NEW_MAIL : 0);
1693 * These functions try to find a message in a maildir folder when it
1694 * has moved under our feet. Note that this code is rather expensive, but
1695 * then again, it's called rarely.
1698 FILE *_maildir_open_find_message (const char *folder, const char *unique,
1699 const char *subfolder)
1701 char dir[_POSIX_PATH_MAX];
1702 char tunique[_POSIX_PATH_MAX];
1703 char fname[_POSIX_PATH_MAX];
1711 snprintf (dir, sizeof (dir), "%s/%s", folder, subfolder);
1713 if ((dp = opendir (dir)) == NULL)
1719 while ((de = readdir (dp)))
1721 maildir_canon_filename (tunique, de->d_name, sizeof (tunique));
1723 if (!mutt_strcmp (tunique, unique))
1725 snprintf (fname, sizeof (fname), "%s/%s/%s", folder, subfolder,
1727 fp = fopen (fname, "r"); /* __FOPEN_CHECKED__ */
1739 FILE *maildir_open_find_message (const char *folder, const char *msg)
1741 char unique[_POSIX_PATH_MAX];
1744 static unsigned int new_hits = 0, cur_hits = 0; /* simple dynamic optimization */
1746 maildir_canon_filename (unique, msg, sizeof (unique));
1750 _maildir_open_find_message (folder, unique,
1751 new_hits > cur_hits ? "new" : "cur"))
1754 if (new_hits < UINT_MAX && cur_hits < UINT_MAX)
1756 new_hits += (new_hits > cur_hits ? 1 : 0);
1757 cur_hits += (new_hits > cur_hits ? 0 : 1);
1764 _maildir_open_find_message (folder, unique,
1765 new_hits > cur_hits ? "cur" : "new"))
1768 if (new_hits < UINT_MAX && cur_hits < UINT_MAX)
1770 new_hits += (new_hits > cur_hits ? 0 : 1);
1771 cur_hits += (new_hits > cur_hits ? 1 : 0);
1783 * 1 if there are no messages in the mailbox
1784 * 0 if there are messages in the mailbox
1787 int maildir_check_empty (const char *path)
1791 int r = 1; /* assume empty until we find a message */
1792 char realpath[_POSIX_PATH_MAX];
1795 /* Strategy here is to look for any file not beginning with a period */
1798 /* we do "cur" on the first iteration since its more likely that we'll
1799 * find old messages without having to scan both subdirs
1801 snprintf (realpath, sizeof (realpath), "%s/%s", path,
1802 iter == 0 ? "cur" : "new");
1803 if ((dp = opendir (realpath)) == NULL)
1805 while ((de = readdir (dp)))
1807 if (*de->d_name != '.')
1815 } while (r && iter < 2);
1822 * 1 if there are no messages in the mailbox
1823 * 0 if there are messages in the mailbox
1826 int mh_check_empty (const char *path)
1830 int r = 1; /* assume empty until we find a message */
1832 if ((dp = opendir (path)) == NULL)
1834 while ((de = readdir (dp)))
1836 if (mh_valid_message (de->d_name))