2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2002 Michael R. Elkins <me@mutt.org>
4 * Copyright (C) 1999-2002 Thomas Roessler <roessler@does-not-exist.org>
6 * This file is part of mutt-ng, see http://www.muttng.org/.
7 * It's licensed under the GNU General Public License,
8 * please see the file GPL in the top level source directory.
12 * This file contains routines specific to MH and ``maildir'' style
32 #include <sys/types.h>
52 unsigned header_parsed:1;
55 #endif /* USE_INODESORT */
64 /* mh_sequences support */
66 #define MH_SEQ_UNSEEN (1 << 0)
67 #define MH_SEQ_REPLIED (1 << 1)
68 #define MH_SEQ_FLAGGED (1 << 2)
70 static void mhs_alloc (struct mh_sequences *mhs, int i)
75 if (i > mhs->max || !mhs->flags) {
77 safe_realloc (&mhs->flags, sizeof (mhs->flags[0]) * (newmax + 1));
78 for (j = mhs->max + 1; j <= newmax; j++)
85 static void mhs_free_sequences (struct mh_sequences *mhs)
90 static short mhs_check (struct mh_sequences *mhs, int i)
92 if (!mhs->flags || i > mhs->max)
98 static short mhs_set (struct mh_sequences *mhs, int i, short f)
102 return mhs->flags[i];
109 static short mhs_unset (struct mh_sequences *mhs, int i, short f)
113 return mhs->flags[i];
118 static void mh_read_token (char *t, int *first, int *last)
122 if ((p = strchr (t, '-'))) {
128 *first = *last = atoi (t);
131 static void mh_read_sequences (struct mh_sequences *mhs, const char *path)
142 char pathname[_POSIX_PATH_MAX];
144 snprintf (pathname, sizeof (pathname), "%s/.mh_sequences", path);
146 if (!(fp = fopen (pathname, "r")))
149 while ((buff = mutt_read_line (buff, &sz, fp, &line))) {
150 if (!(t = strtok (buff, " \t:")))
153 if (!mutt_strcmp (t, MhUnseen))
155 else if (!mutt_strcmp (t, MhFlagged))
157 else if (!mutt_strcmp (t, MhReplied))
159 else /* unknown sequence */
162 while ((t = strtok (NULL, " \t:"))) {
163 mh_read_token (t, &first, &last);
164 for (; first <= last; first++)
165 mhs_set (mhs, first, f);
173 int mh_buffy (const char *path)
176 struct mh_sequences mhs;
178 memset (&mhs, 0, sizeof (mhs));
180 mh_read_sequences (&mhs, path);
181 for (i = 0; !r && i <= mhs.max; i++)
182 if (mhs_check (&mhs, i) & MH_SEQ_UNSEEN)
184 mhs_free_sequences (&mhs);
188 static int mh_mkstemp (CONTEXT * dest, FILE ** fp, char **tgt)
191 char path[_POSIX_PATH_MAX];
194 snprintf (path, _POSIX_PATH_MAX, "%s/.mutt-%s-%d-%d",
195 dest->path, NONULL (Hostname), (int) getpid (), Counter++);
197 if ((fd = open (path, O_WRONLY | O_EXCL | O_CREAT, 0666)) == -1) {
198 if (errno != EEXIST) {
204 *tgt = safe_strdup (path);
209 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)
225 fprintf (fp, "%s:", tag);
230 for (i = 0; i <= mhs->max; i++) {
231 if ((mhs_check (mhs, i) & f)) {
237 else if (first >= 0) {
239 fprintf (fp, " %d", first);
241 fprintf (fp, " %d-%d", first, last);
250 fprintf (fp, " %d", first);
252 fprintf (fp, " %d-%d", first, last);
258 /* XXX - we don't currently remove deleted messages from sequences we don't know. Should we? */
260 void mh_update_sequences (CONTEXT * ctx)
264 char sequences[_POSIX_PATH_MAX];
276 char seq_unseen[STRING];
277 char seq_replied[STRING];
278 char seq_flagged[STRING];
281 struct mh_sequences mhs;
283 memset (&mhs, 0, sizeof (mhs));
285 snprintf (seq_unseen, sizeof (seq_unseen), "%s:", NONULL (MhUnseen));
286 snprintf (seq_replied, sizeof (seq_replied), "%s:", NONULL (MhReplied));
287 snprintf (seq_flagged, sizeof (seq_flagged), "%s:", NONULL (MhFlagged));
289 if (mh_mkstemp (ctx, &nfp, &tmpfname) != 0) {
294 snprintf (sequences, sizeof (sequences), "%s/.mh_sequences", ctx->path);
297 /* first, copy unknown sequences */
298 if ((ofp = fopen (sequences, "r"))) {
299 while ((buff = mutt_read_line (buff, &s, ofp, &l))) {
300 if (!mutt_strncmp (buff, seq_unseen, mutt_strlen (seq_unseen)))
302 if (!mutt_strncmp (buff, seq_flagged, mutt_strlen (seq_flagged)))
304 if (!mutt_strncmp (buff, seq_replied, mutt_strlen (seq_replied)))
307 fprintf (nfp, "%s\n", buff);
312 /* now, update our unseen, flagged, and replied sequences */
313 for (l = 0; l < ctx->msgcount; l++) {
314 if (ctx->hdrs[l]->deleted)
317 if ((p = strrchr (ctx->hdrs[l]->path, '/')))
320 p = ctx->hdrs[l]->path;
324 if (!ctx->hdrs[l]->read) {
325 mhs_set (&mhs, i, MH_SEQ_UNSEEN);
328 if (ctx->hdrs[l]->flagged) {
329 mhs_set (&mhs, i, MH_SEQ_FLAGGED);
332 if (ctx->hdrs[l]->replied) {
333 mhs_set (&mhs, i, MH_SEQ_REPLIED);
338 /* write out the new sequences */
340 mhs_write_one_sequence (nfp, &mhs, MH_SEQ_UNSEEN, NONULL (MhUnseen));
342 mhs_write_one_sequence (nfp, &mhs, MH_SEQ_FLAGGED, NONULL (MhFlagged));
344 mhs_write_one_sequence (nfp, &mhs, MH_SEQ_REPLIED, NONULL (MhReplied));
346 mhs_free_sequences (&mhs);
349 /* try to commit the changes - no guarantee here */
353 if (safe_rename (tmpfname, sequences) != 0) {
354 /* report an error? */
361 static void mh_sequences_add_one (CONTEXT * ctx, int n, short unseen,
362 short flagged, short replied)
364 short unseen_done = 0;
365 short flagged_done = 0;
366 short replied_done = 0;
368 FILE *ofp = NULL, *nfp = NULL;
371 char sequences[_POSIX_PATH_MAX];
373 char seq_unseen[STRING];
374 char seq_replied[STRING];
375 char seq_flagged[STRING];
381 if (mh_mkstemp (ctx, &nfp, &tmpfname) == -1)
384 snprintf (seq_unseen, sizeof (seq_unseen), "%s:", NONULL (MhUnseen));
385 snprintf (seq_replied, sizeof (seq_replied), "%s:", NONULL (MhReplied));
386 snprintf (seq_flagged, sizeof (seq_flagged), "%s:", NONULL (MhFlagged));
388 snprintf (sequences, sizeof (sequences), "%s/.mh_sequences", ctx->path);
389 if ((ofp = fopen (sequences, "r"))) {
390 while ((buff = mutt_read_line (buff, &sz, ofp, &line))) {
391 if (unseen && !strncmp (buff, seq_unseen, mutt_strlen (seq_unseen))) {
392 fprintf (nfp, "%s %d\n", buff, n);
396 && !strncmp (buff, seq_flagged, mutt_strlen (seq_flagged))) {
397 fprintf (nfp, "%s %d\n", buff, n);
401 && !strncmp (buff, seq_replied, mutt_strlen (seq_replied))) {
402 fprintf (nfp, "%s %d\n", buff, n);
406 fprintf (nfp, "%s\n", buff);
412 if (!unseen_done && unseen)
413 fprintf (nfp, "%s: %d\n", NONULL (MhUnseen), n);
414 if (!flagged_done && flagged)
415 fprintf (nfp, "%s: %d\n", NONULL (MhFlagged), n);
416 if (!replied_done && replied)
417 fprintf (nfp, "%s: %d\n", NONULL (MhReplied), n);
422 if (safe_rename (tmpfname, sequences) != 0)
428 static void mh_update_maildir (struct maildir *md, struct mh_sequences *mhs)
434 for (; md; md = md->next) {
435 if ((p = strrchr (md->h->path, '/')))
441 f = mhs_check (mhs, i);
443 md->h->read = (f & MH_SEQ_UNSEEN) ? 0 : 1;
444 md->h->flagged = (f & MH_SEQ_FLAGGED) ? 1 : 0;
445 md->h->replied = (f & MH_SEQ_REPLIED) ? 1 : 0;
449 /* maildir support */
451 static void maildir_free_entry (struct maildir **md)
456 FREE (&(*md)->canon_fname);
458 mutt_free_header (&(*md)->h);
463 static void maildir_free_maildir (struct maildir **md)
465 struct maildir *p, *q;
470 for (p = *md; p; p = q) {
472 maildir_free_entry (&p);
476 static void maildir_parse_flags (HEADER * h, const char *path)
484 if ((p = strrchr (path, ':')) != NULL && mutt_strncmp (p + 1, "2,", 2) == 0) {
487 mutt_str_replace (&h->maildir_flags, p);
488 q = h->maildir_flags;
502 case 'R': /* replied */
507 case 'T': /* trashed */
520 if (q == h->maildir_flags)
521 FREE (&h->maildir_flags);
526 static void maildir_update_mtime (CONTEXT * ctx)
528 char buf[_POSIX_PATH_MAX];
531 if (ctx->magic == M_MAILDIR) {
532 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, "cur");
533 if (stat (buf, &st) == 0)
534 ctx->mtime_cur = st.st_mtime;
535 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, "new");
538 snprintf (buf, sizeof (buf), "%s/.mh_sequences", ctx->path);
539 if (stat (buf, &st) == 0)
540 ctx->mtime_cur = st.st_mtime;
542 strfcpy (buf, ctx->path, sizeof (buf));
545 if (stat (buf, &st) == 0)
546 ctx->mtime = st.st_mtime;
550 * Actually parse a maildir message. This may also be used to fill
551 * out a fake header structure generated by lazy maildir parsing.
553 static HEADER *maildir_parse_message (int magic, const char *fname,
554 int is_old, HEADER * _h)
560 if ((f = fopen (fname, "r")) != NULL) {
562 h = mutt_new_header ();
563 h->env = mutt_read_rfc822_header (f, h, 0, 0);
565 fstat (fileno (f), &st);
569 h->received = h->date_sent;
571 if (h->content->length <= 0)
572 h->content->length = st.st_size - h->content->offset;
576 if (magic == M_MAILDIR) {
578 * maildir stores its flags in the filename, so ignore the
579 * flags in the header of the message
583 maildir_parse_flags (h, fname);
591 * Note that this routine will _not_ modify the context given by
594 * It's used in the first parsing pass on maildir and MH folders.
595 * In the MH case, this means full parsing of the folder. In the
596 * maildir case, it means that we only look at flags, and create a
597 * fake HEADER structure, which may later be filled in by
598 * maildir_parse_message(), when called from
599 * maildir_delayed_parsing().
603 static int maildir_parse_entry (CONTEXT * ctx, struct maildir ***last,
604 const char *subdir, const char *fname,
605 int *count, int is_old, ino_t inode)
607 struct maildir *entry;
609 char buf[_POSIX_PATH_MAX];
612 snprintf (buf, sizeof (buf), "%s/%s/%s", ctx->path, subdir, fname);
614 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, fname);
616 if (ctx->magic == M_MH)
617 h = maildir_parse_message (ctx->magic, buf, is_old, NULL);
619 h = mutt_new_header ();
621 maildir_parse_flags (h, buf);
627 if (!ctx->quiet && ReadInc && ((*count % ReadInc) == 0 || *count == 1))
628 mutt_message (_("Reading %s... %d"), ctx->path, *count);
632 snprintf (buf, sizeof (buf), "%s/%s", subdir, fname);
633 h->path = safe_strdup (buf);
636 h->path = safe_strdup (fname);
638 entry = safe_calloc (sizeof (struct maildir), 1);
640 entry->header_parsed = (ctx->magic == M_MH);
642 entry->inode = inode;
643 #endif /* USE_INODESORT */
645 *last = &entry->next;
655 /* Ignore the garbage files. A valid MH message consists of only
656 * digits. Deleted message get moved to a filename with a comma before
660 int mh_valid_message (const char *s)
663 if (!isdigit ((unsigned char) *s))
669 static int maildir_parse_dir (CONTEXT * ctx, struct maildir ***last,
670 const char *subdir, int *count)
674 char buf[_POSIX_PATH_MAX];
678 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, subdir);
679 is_old = (mutt_strcmp ("cur", subdir) == 0);
682 strfcpy (buf, ctx->path, sizeof (buf));
684 if ((dirp = opendir (buf)) == NULL)
687 while ((de = readdir (dirp)) != NULL) {
689 if ((ctx->magic == M_MH && !mh_valid_message (de->d_name))
690 || (ctx->magic == M_MAILDIR && *de->d_name == '.'))
693 /* FOO - really ignore the return value? */
696 (debugfile, "%s:%d: parsing %s\n", __FILE__, __LINE__,
698 maildir_parse_entry (ctx, last, subdir, de->d_name, count, is_old,
706 static int maildir_add_to_context (CONTEXT * ctx, struct maildir *md)
708 int oldmsgcount = ctx->msgcount;
712 dprint (2, (debugfile, "%s:%d maildir_add_to_context(): Considering %s\n",
713 __FILE__, __LINE__, NONULL (md->canon_fname)));
718 "%s:%d Adding header structure. Flags: %s%s%s%s%s\n", __FILE__,
719 __LINE__, md->h->flagged ? "f" : "", md->h->deleted ? "D" : "",
720 md->h->replied ? "r" : "", md->h->old ? "O" : "",
721 md->h->read ? "R" : ""));
722 if (ctx->msgcount == ctx->hdrmax)
723 mx_alloc_memory (ctx);
725 ctx->hdrs[ctx->msgcount] = md->h;
726 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
728 md->h->content->length + md->h->content->offset -
729 md->h->content->hdr_offset;
737 if (ctx->msgcount > oldmsgcount) {
738 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
744 static int maildir_move_to_context (CONTEXT * ctx, struct maildir **md)
748 r = maildir_add_to_context (ctx, *md);
749 maildir_free_maildir (md);
755 * Merge two maildir lists according to the inode numbers.
757 static struct maildir *maildir_merge_inode (struct maildir *left,
758 struct maildir *right)
760 struct maildir *head;
761 struct maildir *tail;
764 if (left->inode < right->inode) {
782 while (left && right) {
783 if (left->inode < right->inode) {
805 * Sort maildir list according to inode.
807 static struct maildir *maildir_sort_inode (struct maildir *list)
809 struct maildir *left = list;
810 struct maildir *right = list;
812 if (!list || !list->next) {
817 while (list && list->next) {
819 list = list->next->next;
826 left = maildir_sort_inode (left);
827 right = maildir_sort_inode (right);
828 return maildir_merge_inode (left, right);
830 #endif /* USE_INODESORT */
833 static size_t maildir_hcache_keylen (const char *fn)
835 const char *p = strchr (fn, ':');
837 return p ? (size_t) (p - fn) : mutt_strlen (fn);
842 * This function does the second parsing pass for a maildir-style
845 void maildir_delayed_parsing (CONTEXT * ctx, struct maildir *md)
848 char fn[_POSIX_PATH_MAX];
854 struct timeval *when = NULL;
855 struct stat lastchanged;
858 hc = mutt_hcache_open (HeaderCache, ctx->path);
861 for (p = md, count = 0; p; p = p->next, count++) {
862 if (!(p && p->h && !p->header_parsed))
866 data = mutt_hcache_fetch (hc, p->h->path + 3, &maildir_hcache_keylen);
867 when = (struct timeval *) data;
870 if (!ctx->quiet && ReadInc && ((count % ReadInc) == 0 || count == 1))
871 mutt_message (_("Reading %s... %d"), ctx->path, count);
872 snprintf (fn, sizeof (fn), "%s/%s", ctx->path, p->h->path);
875 if (option (OPTHCACHEVERIFY)) {
876 ret = stat (fn, &lastchanged);
879 lastchanged.st_mtime = 0;
883 if (data != NULL && !ret && lastchanged.st_mtime <= when->tv_sec) {
884 p->h = mutt_hcache_restore ((unsigned char *) data, &p->h);
885 maildir_parse_flags (p->h, fn);
889 if (maildir_parse_message (ctx->magic, fn, p->h->old, p->h)) {
890 p->header_parsed = 1;
891 maildir_parse_flags (p->h, fn);
893 mutt_hcache_store (hc, p->h->path + 3, p->h, 0, &maildir_hcache_keylen);
897 mutt_free_header (&p->h);
903 mutt_hcache_close (hc);
907 /* Read a MH/maildir style mailbox.
910 * ctx [IN/OUT] context for this mailbox
911 * subdir [IN] NULL for MH mailboxes, otherwise the subdir of the
912 * maildir mailbox to read from
914 int mh_read_dir (CONTEXT * ctx, const char *subdir)
917 struct mh_sequences mhs;
918 struct maildir **last;
922 memset (&mhs, 0, sizeof (mhs));
924 maildir_update_mtime (ctx);
929 if (maildir_parse_dir (ctx, &last, subdir, &count) == -1)
932 if (ctx->magic == M_MH) {
933 mh_read_sequences (&mhs, ctx->path);
934 mh_update_maildir (md, &mhs);
935 mhs_free_sequences (&mhs);
939 md = maildir_sort_inode (md);
940 #endif /* USE_INODESORT */
942 if (ctx->magic == M_MAILDIR)
943 maildir_delayed_parsing (ctx, md);
945 maildir_move_to_context (ctx, &md);
949 /* read a maildir style mailbox */
950 int maildir_read_dir (CONTEXT * ctx)
952 /* maildir looks sort of like MH, except that there are two subdirectories
953 * of the main folder path from which to read messages
955 if (mh_read_dir (ctx, "new") == -1 || mh_read_dir (ctx, "cur") == -1)
962 * Open a new (temporary) message in an MH folder.
965 int mh_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr)
967 return mh_mkstemp (dest, &msg->fp, &msg->path);
970 int ch_compar (const void *a, const void *b)
972 return (int) (*((const char *) a) - *((const char *) b));
975 static void maildir_flags (char *dest, size_t destlen, HEADER * hdr)
980 * The maildir specification requires that all files in the cur
981 * subdirectory have the :unique string appeneded, regardless of whether
982 * or not there are any flags. If .old is set, we know that this message
983 * will end up in the cur directory, so we include it in the following
984 * test even though there is no associated flag.
988 && (hdr->flagged || hdr->replied || hdr->read || hdr->deleted
989 || hdr->old || hdr->maildir_flags)) {
990 char tmp[LONG_STRING];
992 snprintf (tmp, sizeof (tmp),
994 hdr->flagged ? "F" : "",
995 hdr->replied ? "R" : "",
996 hdr->read ? "S" : "", hdr->deleted ? "T" : "",
997 NONULL (hdr->maildir_flags));
998 if (hdr->maildir_flags)
999 qsort (tmp, mutt_strlen (tmp), 1, ch_compar);
1000 snprintf (dest, destlen, ":2,%s", tmp);
1006 * Open a new (temporary) message in a maildir folder.
1008 * Note that this uses _almost_ the maildir file name format, but
1009 * with a {cur,new} prefix.
1013 int maildir_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr)
1016 char path[_POSIX_PATH_MAX];
1021 short deleted = hdr->deleted;
1025 maildir_flags (suffix, sizeof (suffix), hdr);
1027 hdr->deleted = deleted;
1032 if (hdr && (hdr->read || hdr->old))
1033 strfcpy (subdir, "cur", sizeof (subdir));
1035 strfcpy (subdir, "new", sizeof (subdir));
1038 snprintf (path, _POSIX_PATH_MAX, "%s/tmp/%s.%ld.%u_%d.%s%s",
1039 dest->path, subdir, (long) time (NULL),
1040 (unsigned int) getpid (), Counter++, NONULL (Hostname), suffix);
1042 dprint (2, (debugfile, "maildir_open_new_message (): Trying %s.\n",
1046 if ((fd = open (path, O_WRONLY | O_EXCL | O_CREAT, 0666)) == -1) {
1047 if (errno != EEXIST) {
1053 dprint (2, (debugfile, "maildir_open_new_message (): Success.\n"));
1054 msg->path = safe_strdup (path);
1059 if ((msg->fp = fdopen (fd, "w")) == NULL) {
1072 * Commit a message to a maildir folder.
1074 * msg->path contains the file name of a file in tmp/. We take the
1075 * flags from this file's name.
1077 * ctx is the mail folder we commit to.
1079 * hdr is a header structure to which we write the message's new
1080 * file name. This is used in the mh and maildir folder synch
1081 * routines. When this routine is invoked from mx_commit_message,
1084 * msg->path looks like this:
1086 * tmp/{cur,new}.mutt-HOSTNAME-PID-COUNTER:flags
1088 * See also maildir_open_new_message().
1092 int maildir_commit_message (CONTEXT * ctx, MESSAGE * msg, HEADER * hdr)
1096 char path[_POSIX_PATH_MAX];
1097 char full[_POSIX_PATH_MAX];
1100 if (safe_fclose (&msg->fp) != 0)
1103 /* extract the subdir */
1104 s = strrchr (msg->path, '/') + 1;
1105 strfcpy (subdir, s, 4);
1107 /* extract the flags */
1108 if ((s = strchr (s, ':')))
1109 strfcpy (suffix, s, sizeof (suffix));
1113 /* construct a new file name. */
1115 snprintf (path, _POSIX_PATH_MAX, "%s/%ld.%u_%d.%s%s", subdir,
1116 (long) time (NULL), (unsigned int) getpid (), Counter++,
1117 NONULL (Hostname), suffix);
1118 snprintf (full, _POSIX_PATH_MAX, "%s/%s", ctx->path, path);
1120 dprint (2, (debugfile, "maildir_commit_message (): renaming %s to %s.\n",
1123 if (safe_rename (msg->path, full) == 0) {
1125 mutt_str_replace (&hdr->path, path);
1129 * Adjust the mtime on the file to match the time at which this
1130 * message was received. Currently this is only set when copying
1131 * messages between mailboxes, so we test to ensure that it is
1134 if (msg->received) {
1137 ut.actime = msg->received;
1138 ut.modtime = msg->received;
1139 if (utime (full, &ut)) {
1141 ("maildir_commit_message(): unable to set time on file"));
1148 else if (errno != EEXIST) {
1149 mutt_perror (ctx->path);
1156 * commit a message to an MH folder.
1161 static int _mh_commit_message (CONTEXT * ctx, MESSAGE * msg, HEADER * hdr,
1167 unsigned int n, hi = 0;
1168 char path[_POSIX_PATH_MAX];
1171 if (safe_fclose (&msg->fp) != 0)
1174 if ((dirp = opendir (ctx->path)) == NULL) {
1175 mutt_perror (ctx->path);
1179 /* figure out what the next message number is */
1180 while ((de = readdir (dirp)) != NULL) {
1186 if (!isdigit ((unsigned char) *cp))
1199 * Now try to rename the file to the proper name.
1201 * Note: We may have to try multiple times, until we find a free
1207 snprintf (tmp, sizeof (tmp), "%d", hi);
1208 snprintf (path, sizeof (path), "%s/%s", ctx->path, tmp);
1209 if (safe_rename (msg->path, path) == 0) {
1211 mutt_str_replace (&hdr->path, tmp);
1215 else if (errno != EEXIST) {
1216 mutt_perror (ctx->path);
1221 mh_sequences_add_one (ctx, hi, !msg->flags.read, msg->flags.flagged,
1222 msg->flags.replied);
1226 int mh_commit_message (CONTEXT * ctx, MESSAGE * msg, HEADER * hdr)
1228 return _mh_commit_message (ctx, msg, hdr, 1);
1232 /* Sync a message in an MH folder.
1234 * This code is also used for attachment deletion in maildir
1238 static int mh_rewrite_message (CONTEXT * ctx, int msgno)
1240 HEADER *h = ctx->hdrs[msgno];
1245 char oldpath[_POSIX_PATH_MAX];
1246 char newpath[_POSIX_PATH_MAX];
1247 char partpath[_POSIX_PATH_MAX];
1249 long old_body_offset = h->content->offset;
1250 long old_body_length = h->content->length;
1251 long old_hdr_lines = h->lines;
1253 if ((dest = mx_open_new_message (ctx, h, 0)) == NULL)
1256 if ((rc = mutt_copy_message (dest->fp, ctx, h,
1257 M_CM_UPDATE, CH_UPDATE | CH_UPDATE_LEN)) == 0)
1259 snprintf (oldpath, _POSIX_PATH_MAX, "%s/%s", ctx->path, h->path);
1260 strfcpy (partpath, h->path, _POSIX_PATH_MAX);
1262 if (ctx->magic == M_MAILDIR)
1263 rc = maildir_commit_message (ctx, dest, h);
1265 rc = _mh_commit_message (ctx, dest, h, 0);
1267 mx_close_message (&dest);
1275 * Try to move the new message to the old place.
1278 * This is important when we are just updating flags.
1280 * Note that there is a race condition against programs which
1281 * use the first free slot instead of the maximum message
1282 * number. Mutt does _not_ behave like this.
1284 * Anyway, if this fails, the message is in the folder, so
1285 * all what happens is that a concurrently runnung mutt will
1286 * lose flag modifications.
1289 if (ctx->magic == M_MH && rc == 0) {
1290 snprintf (newpath, _POSIX_PATH_MAX, "%s/%s", ctx->path, h->path);
1291 if ((rc = safe_rename (newpath, oldpath)) == 0)
1292 mutt_str_replace (&h->path, partpath);
1296 mx_close_message (&dest);
1298 if (rc == -1 && restore) {
1299 h->content->offset = old_body_offset;
1300 h->content->length = old_body_length;
1301 h->lines = old_hdr_lines;
1304 mutt_free_body (&h->content->parts);
1308 static int mh_sync_message (CONTEXT * ctx, int msgno)
1310 HEADER *h = ctx->hdrs[msgno];
1312 if (h->attach_del || h->refs_changed || h->irt_changed)
1313 if (mh_rewrite_message (ctx, msgno) != 0)
1319 static int maildir_sync_message (CONTEXT * ctx, int msgno)
1321 HEADER *h = ctx->hdrs[msgno];
1323 if (h->attach_del || h->refs_changed || h->irt_changed) {
1324 /* when doing attachment deletion/rethreading, fall back to the MH case. */
1325 if (mh_rewrite_message (ctx, msgno) != 0)
1329 /* we just have to rename the file. */
1331 char newpath[_POSIX_PATH_MAX];
1332 char partpath[_POSIX_PATH_MAX];
1333 char fullpath[_POSIX_PATH_MAX];
1334 char oldpath[_POSIX_PATH_MAX];
1338 if ((p = strrchr (h->path, '/')) == NULL) {
1341 "maildir_sync_message: %s: unable to find subdir!\n",
1346 strfcpy (newpath, p, sizeof (newpath));
1348 /* kill the previous flags */
1349 if ((p = strchr (newpath, ':')) != NULL)
1352 maildir_flags (suffix, sizeof (suffix), h);
1354 snprintf (partpath, sizeof (partpath), "%s/%s%s",
1355 (h->read || h->old) ? "cur" : "new", newpath, suffix);
1356 snprintf (fullpath, sizeof (fullpath), "%s/%s", ctx->path, partpath);
1357 snprintf (oldpath, sizeof (oldpath), "%s/%s", ctx->path, h->path);
1359 if (mutt_strcmp (fullpath, oldpath) == 0) {
1360 /* message hasn't really changed */
1364 /* record that the message is possibly marked as trashed on disk */
1365 h->trash = h->deleted;
1367 if (rename (oldpath, fullpath) != 0) {
1368 mutt_perror ("rename");
1371 mutt_str_replace (&h->path, partpath);
1376 int mh_sync_mailbox (CONTEXT * ctx, int *index_hint)
1378 char path[_POSIX_PATH_MAX], tmp[_POSIX_PATH_MAX];
1383 #endif /* USE_HCACHE */
1385 if (ctx->magic == M_MH)
1386 i = mh_check_mailbox (ctx, index_hint);
1388 i = maildir_check_mailbox (ctx, index_hint);
1394 if (ctx->magic == M_MAILDIR)
1395 hc = mutt_hcache_open (HeaderCache, ctx->path);
1396 #endif /* USE_HCACHE */
1398 for (i = 0; i < ctx->msgcount; i++) {
1399 if (ctx->hdrs[i]->deleted
1400 && (ctx->magic != M_MAILDIR || !option (OPTMAILDIRTRASH))) {
1401 snprintf (path, sizeof (path), "%s/%s", ctx->path, ctx->hdrs[i]->path);
1402 if (ctx->magic == M_MAILDIR
1403 || (option (OPTMHPURGE) && ctx->magic == M_MH)) {
1405 if (ctx->magic == M_MAILDIR)
1406 mutt_hcache_delete (hc, ctx->hdrs[i]->path + 3,
1407 &maildir_hcache_keylen);
1408 #endif /* USE_HCACHE */
1411 else if (ctx->magic == M_MH) {
1412 /* MH just moves files out of the way when you delete them */
1413 if (*ctx->hdrs[i]->path != ',') {
1414 snprintf (tmp, sizeof (tmp), "%s/,%s", ctx->path,
1415 ctx->hdrs[i]->path);
1422 else if (ctx->hdrs[i]->changed || ctx->hdrs[i]->attach_del ||
1423 (ctx->magic == M_MAILDIR
1424 && (option (OPTMAILDIRTRASH) || ctx->hdrs[i]->trash)
1425 && (ctx->hdrs[i]->deleted != ctx->hdrs[i]->trash))) {
1426 if (ctx->magic == M_MAILDIR) {
1427 if (maildir_sync_message (ctx, i) == -1)
1431 if (mh_sync_message (ctx, i) == -1)
1438 if (ctx->magic == M_MAILDIR)
1439 mutt_hcache_close (hc);
1440 #endif /* USE_HCACHE */
1442 if (ctx->magic == M_MH)
1443 mh_update_sequences (ctx);
1445 /* XXX race condition? */
1447 maildir_update_mtime (ctx);
1449 /* adjust indices */
1452 for (i = 0, j = 0; i < ctx->msgcount; i++) {
1453 if (!ctx->hdrs[i]->deleted
1454 || (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))
1455 ctx->hdrs[i]->index = j++;
1463 if (ctx->magic == M_MAILDIR)
1464 mutt_hcache_close (hc);
1465 #endif /* USE_HCACHE */
1469 static char *maildir_canon_filename (char *dest, const char *src, size_t l)
1473 if ((t = strrchr (src, '/')))
1476 strfcpy (dest, src, l);
1477 if ((u = strrchr (dest, ':')))
1483 static void maildir_update_tables (CONTEXT * ctx, int *index_hint)
1489 if (Sort != SORT_ORDER) {
1492 mutt_sort_headers (ctx, 1);
1496 old_count = ctx->msgcount;
1497 for (i = 0, j = 0; i < old_count; i++) {
1498 if (ctx->hdrs[i]->active && index_hint && *index_hint == i)
1501 if (ctx->hdrs[i]->active)
1502 ctx->hdrs[i]->index = j++;
1505 mx_update_tables (ctx, 0);
1506 mutt_clear_threads (ctx);
1509 static void maildir_update_flags (CONTEXT * ctx, HEADER * o, HEADER * n)
1511 /* save the global state here so we can reset it at the
1512 * end of list block if required.
1514 int context_changed = ctx->changed;
1516 /* user didn't modify this message. alter the flags to
1517 * match the current state on disk. This may not actually
1518 * do anything, but we can't tell right now. mutt_set_flag()
1519 * will just ignore the call if the status bits are
1520 * already properly set.
1522 mutt_set_flag (ctx, o, M_FLAG, n->flagged);
1523 mutt_set_flag (ctx, o, M_REPLIED, n->replied);
1524 mutt_set_flag (ctx, o, M_READ, n->read);
1525 mutt_set_flag (ctx, o, M_OLD, n->old);
1527 /* mutt_set_flag() will set this, but we don't need to
1528 * sync the changes we made because we just updated the
1529 * context to match the current on-disk state of the
1534 /* if the mailbox was not modified before we made these
1535 * changes, unset the changed flag since nothing needs to
1538 if (!context_changed)
1543 /* This function handles arrival of new mail and reopening of
1544 * maildir folders. The basic idea here is we check to see if either
1545 * the new or cur subdirectories have changed, and if so, we scan them
1546 * for the list of files. We check for newly added messages, and
1547 * then merge the flags messages we already knew about. We don't treat
1548 * either subdirectory differently, as mail could be copied directly into
1549 * the cur directory from another agent.
1551 int maildir_check_mailbox (CONTEXT * ctx, int *index_hint)
1553 struct stat st_new; /* status of the "new" subdirectory */
1554 struct stat st_cur; /* status of the "cur" subdirectory */
1555 char buf[_POSIX_PATH_MAX];
1556 int changed = 0; /* bitmask representing which subdirectories
1557 have changed. 0x1 = new, 0x2 = cur */
1558 int occult = 0; /* messages were removed from the mailbox */
1559 int have_new = 0; /* messages were added to the mailbox */
1560 struct maildir *md; /* list of messages in the mailbox */
1561 struct maildir **last, *p;
1563 HASH *fnames; /* hash table for quickly looking up the base filename
1564 for a maildir message */
1566 /* XXX seems like this check belongs in mx_check_mailbox()
1569 if (!option (OPTCHECKNEW))
1572 snprintf (buf, sizeof (buf), "%s/new", ctx->path);
1573 if (stat (buf, &st_new) == -1)
1576 snprintf (buf, sizeof (buf), "%s/cur", ctx->path);
1577 if (stat (buf, &st_cur) == -1)
1580 /* determine which subdirectories need to be scanned */
1581 if (st_new.st_mtime > ctx->mtime)
1583 if (st_cur.st_mtime > ctx->mtime_cur)
1587 return 0; /* nothing to do */
1589 /* update the modification times on the mailbox */
1590 ctx->mtime_cur = st_cur.st_mtime;
1591 ctx->mtime = st_new.st_mtime;
1593 /* do a fast scan of just the filenames in
1594 * the subdirectories that have changed.
1599 maildir_parse_dir (ctx, &last, "new", NULL);
1601 maildir_parse_dir (ctx, &last, "cur", NULL);
1603 /* we create a hash table keyed off the canonical (sans flags) filename
1604 * of each message we scanned. This is used in the loop over the
1605 * existing messages below to do some correlation.
1607 fnames = hash_create (1031);
1609 for (p = md; p; p = p->next) {
1610 maildir_canon_filename (buf, p->h->path, sizeof (buf));
1611 p->canon_fname = safe_strdup (buf);
1612 hash_insert (fnames, p->canon_fname, p, 0);
1615 /* check for modifications and adjust flags */
1616 for (i = 0; i < ctx->msgcount; i++) {
1617 ctx->hdrs[i]->active = 0;
1618 maildir_canon_filename (buf, ctx->hdrs[i]->path, sizeof (buf));
1619 p = hash_find (fnames, buf);
1621 /* message already exists, merge flags */
1622 ctx->hdrs[i]->active = 1;
1624 /* check to see if the message has moved to a different
1625 * subdirectory. If so, update the associated filename.
1627 if (mutt_strcmp (ctx->hdrs[i]->path, p->h->path))
1628 mutt_str_replace (&ctx->hdrs[i]->path, p->h->path);
1630 /* if the user hasn't modified the flags on this message, update
1631 * the flags we just detected.
1633 if (!ctx->hdrs[i]->changed)
1634 maildir_update_flags (ctx, ctx->hdrs[i], p->h);
1636 if (ctx->hdrs[i]->deleted == ctx->hdrs[i]->trash)
1637 ctx->hdrs[i]->deleted = p->h->deleted;
1638 ctx->hdrs[i]->trash = p->h->trash;
1640 /* this is a duplicate of an existing header, so remove it */
1641 mutt_free_header (&p->h);
1643 /* This message was not in the list of messages we just scanned.
1644 * Check to see if we have enough information to know if the
1645 * message has disappeared out from underneath us.
1647 else if (((changed & 1) && (!strncmp (ctx->hdrs[i]->path, "new/", 4))) ||
1648 ((changed & 2) && (!strncmp (ctx->hdrs[i]->path, "cur/", 4)))) {
1649 /* This message disappeared, so we need to simulate a "reopen"
1650 * event. We know it disappeared because we just scanned the
1651 * subdirectory it used to reside in.
1656 /* This message resides in a subdirectory which was not
1657 * modified, so we assume that it is still present and
1660 ctx->hdrs[i]->active = 1;
1664 /* destroy the file name hash */
1665 hash_destroy (&fnames, NULL);
1667 /* If we didn't just get new mail, update the tables. */
1669 maildir_update_tables (ctx, index_hint);
1671 /* do any delayed parsing we need to do. */
1672 maildir_delayed_parsing (ctx, md);
1674 /* Incorporate new messages */
1675 have_new = maildir_move_to_context (ctx, &md);
1677 return occult ? M_REOPENED : (have_new ? M_NEW_MAIL : 0);
1681 * This function handles arrival of new mail and reopening of
1682 * mh/maildir folders. Things are getting rather complex because we
1683 * don't have a well-defined "mailbox order", so the tricks from
1684 * mbox.c and mx.c won't work here.
1686 * Don't change this code unless you _really_ understand what
1691 int mh_check_mailbox (CONTEXT * ctx, int *index_hint)
1693 char buf[_POSIX_PATH_MAX];
1694 struct stat st, st_cur;
1695 short modified = 0, have_new = 0, occult = 0;
1696 struct maildir *md, *p;
1697 struct maildir **last = NULL;
1698 struct mh_sequences mhs;
1702 if (!option (OPTCHECKNEW))
1705 strfcpy (buf, ctx->path, sizeof (buf));
1706 if (stat (buf, &st) == -1)
1709 /* create .mh_sequences when there isn't one. */
1710 snprintf (buf, sizeof (buf), "%s/.mh_sequences", ctx->path);
1711 if ((i = stat (buf, &st_cur) == -1) && errno == ENOENT) {
1715 if (mh_mkstemp (ctx, &fp, &tmp) == 0) {
1717 if (safe_rename (tmp, buf) == -1)
1723 if (i == -1 && stat (buf, &st_cur) == -1)
1726 if (st.st_mtime > ctx->mtime || st_cur.st_mtime > ctx->mtime_cur)
1732 ctx->mtime_cur = st_cur.st_mtime;
1733 ctx->mtime = st.st_mtime;
1735 memset (&mhs, 0, sizeof (mhs));
1739 maildir_parse_dir (ctx, &last, NULL, NULL);
1740 mh_read_sequences (&mhs, ctx->path);
1741 mh_update_maildir (md, &mhs);
1742 mhs_free_sequences (&mhs);
1744 /* check for modifications and adjust flags */
1745 fnames = hash_create (1031);
1747 for (p = md; p; p = p->next)
1748 hash_insert (fnames, p->h->path, p, 0);
1750 for (i = 0; i < ctx->msgcount; i++) {
1751 ctx->hdrs[i]->active = 0;
1753 if ((p = hash_find (fnames, ctx->hdrs[i]->path)) && p->h &&
1754 (mbox_strict_cmp_headers (ctx->hdrs[i], p->h))) {
1755 ctx->hdrs[i]->active = 1;
1756 /* found the right message */
1757 if (!ctx->hdrs[i]->changed)
1758 maildir_update_flags (ctx, ctx->hdrs[i], p->h);
1760 mutt_free_header (&p->h);
1762 else /* message has disappeared */
1766 /* destroy the file name hash */
1768 hash_destroy (&fnames, NULL);
1770 /* If we didn't just get new mail, update the tables. */
1772 maildir_update_tables (ctx, index_hint);
1774 /* Incorporate new messages */
1775 have_new = maildir_move_to_context (ctx, &md);
1777 return occult ? M_REOPENED : (have_new ? M_NEW_MAIL : 0);
1784 * These functions try to find a message in a maildir folder when it
1785 * has moved under our feet. Note that this code is rather expensive, but
1786 * then again, it's called rarely.
1789 FILE *_maildir_open_find_message (const char *folder, const char *unique,
1790 const char *subfolder)
1792 char dir[_POSIX_PATH_MAX];
1793 char tunique[_POSIX_PATH_MAX];
1794 char fname[_POSIX_PATH_MAX];
1802 snprintf (dir, sizeof (dir), "%s/%s", folder, subfolder);
1804 if ((dp = opendir (dir)) == NULL) {
1809 while ((de = readdir (dp))) {
1810 maildir_canon_filename (tunique, de->d_name, sizeof (tunique));
1812 if (!mutt_strcmp (tunique, unique)) {
1813 snprintf (fname, sizeof (fname), "%s/%s/%s", folder, subfolder,
1815 fp = fopen (fname, "r"); /* __FOPEN_CHECKED__ */
1827 FILE *maildir_open_find_message (const char *folder, const char *msg)
1829 char unique[_POSIX_PATH_MAX];
1832 static unsigned int new_hits = 0, cur_hits = 0; /* simple dynamic optimization */
1834 maildir_canon_filename (unique, msg, sizeof (unique));
1837 _maildir_open_find_message (folder, unique,
1838 new_hits > cur_hits ? "new" : "cur"))
1839 || errno != ENOENT) {
1840 if (new_hits < UINT_MAX && cur_hits < UINT_MAX) {
1841 new_hits += (new_hits > cur_hits ? 1 : 0);
1842 cur_hits += (new_hits > cur_hits ? 0 : 1);
1848 _maildir_open_find_message (folder, unique,
1849 new_hits > cur_hits ? "cur" : "new"))
1850 || errno != ENOENT) {
1851 if (new_hits < UINT_MAX && cur_hits < UINT_MAX) {
1852 new_hits += (new_hits > cur_hits ? 0 : 1);
1853 cur_hits += (new_hits > cur_hits ? 1 : 0);
1865 * 1 if there are no messages in the mailbox
1866 * 0 if there are messages in the mailbox
1869 int maildir_check_empty (const char *path)
1873 int r = 1; /* assume empty until we find a message */
1874 char realpath[_POSIX_PATH_MAX];
1877 /* Strategy here is to look for any file not beginning with a period */
1880 /* we do "cur" on the first iteration since its more likely that we'll
1881 * find old messages without having to scan both subdirs
1883 snprintf (realpath, sizeof (realpath), "%s/%s", path,
1884 iter == 0 ? "cur" : "new");
1885 if ((dp = opendir (realpath)) == NULL)
1887 while ((de = readdir (dp))) {
1888 if (*de->d_name != '.') {
1895 } while (r && iter < 2);
1902 * 1 if there are no messages in the mailbox
1903 * 0 if there are messages in the mailbox
1906 int mh_check_empty (const char *path)
1910 int r = 1; /* assume empty until we find a message */
1912 if ((dp = opendir (path)) == NULL)
1914 while ((de = readdir (dp))) {
1915 if (mh_valid_message (de->d_name)) {