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
37 #include <sys/types.h>
54 unsigned header_parsed:1;
65 /* mh_sequences support */
67 #define MH_SEQ_UNSEEN (1 << 0)
68 #define MH_SEQ_REPLIED (1 << 1)
69 #define MH_SEQ_FLAGGED (1 << 2)
71 static void mhs_alloc (struct mh_sequences *mhs, int i)
76 if (i > mhs->max || !mhs->flags)
79 safe_realloc (&mhs->flags, sizeof (mhs->flags[0]) * (newmax + 1));
80 for (j = mhs->max + 1; j <= newmax; j++)
87 static void mhs_free_sequences (struct mh_sequences *mhs)
92 static short mhs_check (struct mh_sequences *mhs, int i)
94 if (!mhs->flags || i > mhs->max)
100 static short mhs_set (struct mh_sequences *mhs, int i, short f)
104 return mhs->flags[i];
111 static short mhs_unset (struct mh_sequences *mhs, int i, short f)
115 return mhs->flags[i];
120 static void mh_read_token (char *t, int *first, int *last)
123 if ((p = strchr (t, '-')))
130 *first = *last = atoi (t);
133 static void mh_read_sequences (struct mh_sequences *mhs, const char *path)
144 char pathname[_POSIX_PATH_MAX];
145 snprintf (pathname, sizeof (pathname), "%s/.mh_sequences", path);
147 if (!(fp = fopen (pathname, "r")))
150 while ((buff = mutt_read_line (buff, &sz, fp, &line)))
152 if (!(t = strtok (buff, " \t:")))
155 if (!mutt_strcmp (t, MhUnseen))
157 else if (!mutt_strcmp (t, MhFlagged))
159 else if (!mutt_strcmp (t, MhReplied))
161 else /* unknown sequence */
164 while ((t = strtok (NULL, " \t:")))
166 mh_read_token (t, &first, &last);
167 for (; first <= last; first++)
168 mhs_set (mhs, first, f);
176 int mh_buffy (const char *path)
179 struct mh_sequences mhs;
180 memset (&mhs, 0, sizeof (mhs));
182 mh_read_sequences (&mhs, path);
183 for (i = 0; !r && i <= mhs.max; i++)
184 if (mhs_check (&mhs, i) & MH_SEQ_UNSEEN)
186 mhs_free_sequences (&mhs);
190 static int mh_mkstemp (CONTEXT * dest, FILE ** fp, char **tgt)
193 char path[_POSIX_PATH_MAX];
197 snprintf (path, _POSIX_PATH_MAX, "%s/.mutt-%s-%d-%d",
198 dest->path, NONULL (Hostname), (int) getpid (), Counter++);
200 if ((fd = open (path, O_WRONLY | O_EXCL | O_CREAT, 0666)) == -1)
210 *tgt = safe_strdup (path);
215 if ((*fp = fdopen (fd, "w")) == NULL)
226 static void mhs_write_one_sequence (FILE * fp, struct mh_sequences *mhs,
227 short f, const char *tag)
231 fprintf (fp, "%s:", tag);
236 for (i = 0; i <= mhs->max; i++)
238 if ((mhs_check (mhs, i) & f))
248 fprintf (fp, " %d", first);
250 fprintf (fp, " %d-%d", first, last);
260 fprintf (fp, " %d", first);
262 fprintf (fp, " %d-%d", first, last);
268 /* XXX - we don't currently remove deleted messages from sequences we don't know. Should we? */
270 void mh_update_sequences (CONTEXT * ctx)
274 char sequences[_POSIX_PATH_MAX];
286 char seq_unseen[STRING];
287 char seq_replied[STRING];
288 char seq_flagged[STRING];
291 struct mh_sequences mhs;
292 memset (&mhs, 0, sizeof (mhs));
294 snprintf (seq_unseen, sizeof (seq_unseen), "%s:", NONULL (MhUnseen));
295 snprintf (seq_replied, sizeof (seq_replied), "%s:", NONULL (MhReplied));
296 snprintf (seq_flagged, sizeof (seq_flagged), "%s:", NONULL (MhFlagged));
298 if (mh_mkstemp (ctx, &nfp, &tmpfname) != 0)
304 snprintf (sequences, sizeof (sequences), "%s/.mh_sequences", ctx->path);
307 /* first, copy unknown sequences */
308 if ((ofp = fopen (sequences, "r")))
310 while ((buff = mutt_read_line (buff, &s, ofp, &l)))
312 if (!mutt_strncmp (buff, seq_unseen, mutt_strlen (seq_unseen)))
314 if (!mutt_strncmp (buff, seq_flagged, mutt_strlen (seq_flagged)))
316 if (!mutt_strncmp (buff, seq_replied, mutt_strlen (seq_replied)))
319 fprintf (nfp, "%s\n", buff);
324 /* now, update our unseen, flagged, and replied sequences */
325 for (l = 0; l < ctx->msgcount; l++)
327 if (ctx->hdrs[l]->deleted)
330 if ((p = strrchr (ctx->hdrs[l]->path, '/')))
333 p = ctx->hdrs[l]->path;
337 if (!ctx->hdrs[l]->read)
339 mhs_set (&mhs, i, MH_SEQ_UNSEEN);
342 if (ctx->hdrs[l]->flagged)
344 mhs_set (&mhs, i, MH_SEQ_FLAGGED);
347 if (ctx->hdrs[l]->replied)
349 mhs_set (&mhs, i, MH_SEQ_REPLIED);
354 /* write out the new sequences */
356 mhs_write_one_sequence (nfp, &mhs, MH_SEQ_UNSEEN, NONULL (MhUnseen));
358 mhs_write_one_sequence (nfp, &mhs, MH_SEQ_FLAGGED, NONULL (MhFlagged));
360 mhs_write_one_sequence (nfp, &mhs, MH_SEQ_REPLIED, NONULL (MhReplied));
362 mhs_free_sequences (&mhs);
365 /* try to commit the changes - no guarantee here */
369 if (safe_rename (tmpfname, sequences) != 0)
371 /* report an error? */
378 static void mh_sequences_add_one (CONTEXT * ctx, int n, short unseen,
379 short flagged, short replied)
381 short unseen_done = 0;
382 short flagged_done = 0;
383 short replied_done = 0;
385 FILE *ofp = NULL, *nfp = NULL;
388 char sequences[_POSIX_PATH_MAX];
390 char seq_unseen[STRING];
391 char seq_replied[STRING];
392 char seq_flagged[STRING];
398 if (mh_mkstemp (ctx, &nfp, &tmpfname) == -1)
401 snprintf (seq_unseen, sizeof (seq_unseen), "%s:", NONULL (MhUnseen));
402 snprintf (seq_replied, sizeof (seq_replied), "%s:", NONULL (MhReplied));
403 snprintf (seq_flagged, sizeof (seq_flagged), "%s:", NONULL (MhFlagged));
405 snprintf (sequences, sizeof (sequences), "%s/.mh_sequences", ctx->path);
406 if ((ofp = fopen (sequences, "r")))
408 while ((buff = mutt_read_line (buff, &sz, ofp, &line)))
410 if (unseen && !strncmp (buff, seq_unseen, mutt_strlen (seq_unseen)))
412 fprintf (nfp, "%s %d\n", buff, n);
416 && !strncmp (buff, seq_flagged, mutt_strlen (seq_flagged)))
418 fprintf (nfp, "%s %d\n", buff, n);
422 && !strncmp (buff, seq_replied, mutt_strlen (seq_replied)))
424 fprintf (nfp, "%s %d\n", buff, n);
428 fprintf (nfp, "%s\n", buff);
434 if (!unseen_done && unseen)
435 fprintf (nfp, "%s: %d\n", NONULL (MhUnseen), n);
436 if (!flagged_done && flagged)
437 fprintf (nfp, "%s: %d\n", NONULL (MhFlagged), n);
438 if (!replied_done && replied)
439 fprintf (nfp, "%s: %d\n", NONULL (MhReplied), n);
444 if (safe_rename (tmpfname, sequences) != 0)
450 static void mh_update_maildir (struct maildir *md, struct mh_sequences *mhs)
456 for (; md; md = md->next)
458 if ((p = strrchr (md->h->path, '/')))
464 f = mhs_check (mhs, i);
466 md->h->read = (f & MH_SEQ_UNSEEN) ? 0 : 1;
467 md->h->flagged = (f & MH_SEQ_FLAGGED) ? 1 : 0;
468 md->h->replied = (f & MH_SEQ_REPLIED) ? 1 : 0;
472 /* maildir support */
474 static void maildir_free_entry (struct maildir **md)
479 FREE (&(*md)->canon_fname);
481 mutt_free_header (&(*md)->h);
486 static void maildir_free_maildir (struct maildir **md)
488 struct maildir *p, *q;
493 for (p = *md; p; p = q)
496 maildir_free_entry (&p);
500 static void maildir_parse_flags (HEADER * h, const char *path)
508 if ((p = strrchr (path, ':')) != NULL && mutt_strncmp (p + 1, "2,", 2) == 0)
512 mutt_str_replace (&h->maildir_flags, p);
513 q = h->maildir_flags;
529 case 'R': /* replied */
534 case 'T': /* trashed */
547 if (q == h->maildir_flags)
548 FREE (&h->maildir_flags);
553 static void maildir_update_mtime (CONTEXT * ctx)
555 char buf[_POSIX_PATH_MAX];
558 if (ctx->magic == M_MAILDIR)
560 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, "cur");
561 if (stat (buf, &st) == 0)
562 ctx->mtime_cur = st.st_mtime;
563 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, "new");
567 snprintf (buf, sizeof (buf), "%s/.mh_sequences", ctx->path);
568 if (stat (buf, &st) == 0)
569 ctx->mtime_cur = st.st_mtime;
571 strfcpy (buf, ctx->path, sizeof (buf));
574 if (stat (buf, &st) == 0)
575 ctx->mtime = st.st_mtime;
579 * Actually parse a maildir message. This may also be used to fill
580 * out a fake header structure generated by lazy maildir parsing.
582 static HEADER *maildir_parse_message (int magic, const char *fname,
583 int is_old, HEADER * _h)
589 if ((f = fopen (fname, "r")) != NULL)
592 h = mutt_new_header ();
593 h->env = mutt_read_rfc822_header (f, h, 0, 0);
595 fstat (fileno (f), &st);
599 h->received = h->date_sent;
601 if (h->content->length <= 0)
602 h->content->length = st.st_size - h->content->offset;
606 if (magic == M_MAILDIR)
609 * maildir stores its flags in the filename, so ignore the
610 * flags in the header of the message
614 maildir_parse_flags (h, fname);
622 * Note that this routine will _not_ modify the context given by
625 * It's used in the first parsing pass on maildir and MH folders.
626 * In the MH case, this means full parsing of the folder. In the
627 * maildir case, it means that we only look at flags, and create a
628 * fake HEADER structure, which may later be filled in by
629 * maildir_parse_message(), when called from
630 * maildir_delayed_parsing().
634 static int maildir_parse_entry (CONTEXT * ctx, struct maildir ***last,
635 const char *subdir, const char *fname,
636 int *count, int is_old, ino_t inode)
638 struct maildir *entry;
640 char buf[_POSIX_PATH_MAX];
643 snprintf (buf, sizeof (buf), "%s/%s/%s", ctx->path, subdir, fname);
645 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, fname);
647 if (ctx->magic == M_MH)
648 h = maildir_parse_message (ctx->magic, buf, is_old, NULL);
651 h = mutt_new_header ();
653 maildir_parse_flags (h, buf);
661 if (!ctx->quiet && ReadInc && ((*count % ReadInc) == 0 || *count == 1))
662 mutt_message (_("Reading %s... %d"), ctx->path, *count);
667 snprintf (buf, sizeof (buf), "%s/%s", subdir, fname);
668 h->path = safe_strdup (buf);
671 h->path = safe_strdup (fname);
673 entry = safe_calloc (sizeof (struct maildir), 1);
675 entry->header_parsed = (ctx->magic == M_MH);
676 entry->inode = inode;
678 *last = &entry->next;
688 /* Ignore the garbage files. A valid MH message consists of only
689 * digits. Deleted message get moved to a filename with a comma before
693 int mh_valid_message (const char *s)
697 if (!isdigit ((unsigned char) *s))
703 static int maildir_parse_dir (CONTEXT * ctx, struct maildir ***last,
704 const char *subdir, int *count)
708 char buf[_POSIX_PATH_MAX];
713 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, subdir);
714 is_old = (mutt_strcmp ("cur", subdir) == 0);
717 strfcpy (buf, ctx->path, sizeof (buf));
719 if ((dirp = opendir (buf)) == NULL)
722 while ((de = readdir (dirp)) != NULL)
725 if ((ctx->magic == M_MH && !mh_valid_message (de->d_name))
726 || (ctx->magic == M_MAILDIR && *de->d_name == '.'))
729 /* FOO - really ignore the return value? */
732 (debugfile, "%s:%d: parsing %s\n", __FILE__, __LINE__,
734 maildir_parse_entry (ctx, last, subdir, de->d_name, count, is_old,
742 static int maildir_add_to_context (CONTEXT * ctx, struct maildir *md)
744 int oldmsgcount = ctx->msgcount;
749 dprint (2, (debugfile, "%s:%d maildir_add_to_context(): Considering %s\n",
750 __FILE__, __LINE__, NONULL (md->canon_fname)));
756 "%s:%d Adding header structure. Flags: %s%s%s%s%s\n", __FILE__,
757 __LINE__, md->h->flagged ? "f" : "", md->h->deleted ? "D" : "",
758 md->h->replied ? "r" : "", md->h->old ? "O" : "",
759 md->h->read ? "R" : ""));
760 if (ctx->msgcount == ctx->hdrmax)
761 mx_alloc_memory (ctx);
763 ctx->hdrs[ctx->msgcount] = md->h;
764 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
766 md->h->content->length + md->h->content->offset -
767 md->h->content->hdr_offset;
775 if (ctx->msgcount > oldmsgcount)
777 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
783 static int maildir_move_to_context (CONTEXT * ctx, struct maildir **md)
786 r = maildir_add_to_context (ctx, *md);
787 maildir_free_maildir (md);
792 * Merge two maildir lists according to the inode numbers.
794 static struct maildir* maildir_merge_inode (struct maildir *left,
795 struct maildir *right)
797 struct maildir* head;
798 struct maildir* tail;
802 if (left->inode < right->inode)
823 while (left && right)
825 if (left->inode < right->inode)
851 * Sort maildir list according to inode.
853 static struct maildir* maildir_sort_inode(struct maildir* list)
855 struct maildir* left = list;
856 struct maildir* right = list;
858 if (!list || !list->next)
864 while (list && list->next)
867 list = list->next->next;
874 left = maildir_sort_inode(left);
875 right = maildir_sort_inode(right);
876 return maildir_merge_inode(left, right);
880 static size_t maildir_hcache_keylen (const char *fn)
882 const char * p = strchr (fn, ':');
883 return p ? (size_t) (p - fn) : mutt_strlen(fn);
888 * This function does the second parsing pass for a maildir-style
891 void maildir_delayed_parsing (CONTEXT * ctx, struct maildir *md)
894 char fn[_POSIX_PATH_MAX];
901 struct timeval *when = NULL;
902 struct stat lastchanged;
905 hc = mutt_hcache_open (HeaderCache, ctx->path);
908 for (p = md, count = 0; p; p = p->next, count++)
910 if (! (p && p->h && !p->header_parsed))
914 data = mutt_hcache_fetch (hc, p->h->path + 3, &maildir_hcache_keylen);
915 when = (struct timeval *) data;
918 if (!ctx->quiet && ReadInc && ((count % ReadInc) == 0 || count == 1))
919 mutt_message (_("Reading %s... %d"), ctx->path, count);
920 snprintf (fn, sizeof (fn), "%s/%s", ctx->path, p->h->path);
923 if (option(OPTHCACHEVERIFY)) {
924 ret = stat(fn, &lastchanged);
926 lastchanged.st_mtime = 0;
930 if (data != NULL && !ret && lastchanged.st_mtime <= when->tv_sec)
932 p->h = mutt_hcache_restore ((unsigned char *)data, &p->h);
933 maildir_parse_flags (p->h, fn);
936 if (maildir_parse_message (ctx->magic, fn, p->h->old, p->h))
938 p->header_parsed = 1;
939 maildir_parse_flags (p->h, fn);
941 mutt_hcache_store (hc, p->h->path + 3, p->h, 0, &maildir_hcache_keylen);
944 mutt_free_header (&p->h);
950 mutt_hcache_close (hc);
954 /* Read a MH/maildir style mailbox.
957 * ctx [IN/OUT] context for this mailbox
958 * subdir [IN] NULL for MH mailboxes, otherwise the subdir of the
959 * maildir mailbox to read from
961 int mh_read_dir (CONTEXT * ctx, const char *subdir)
964 struct mh_sequences mhs;
965 struct maildir **last;
969 memset (&mhs, 0, sizeof (mhs));
971 maildir_update_mtime (ctx);
976 if (maildir_parse_dir (ctx, &last, subdir, &count) == -1)
979 if (ctx->magic == M_MH)
981 mh_read_sequences (&mhs, ctx->path);
982 mh_update_maildir (md, &mhs);
983 mhs_free_sequences (&mhs);
986 md = maildir_sort_inode(md);
988 if (ctx->magic == M_MAILDIR)
989 maildir_delayed_parsing (ctx, md);
991 maildir_move_to_context (ctx, &md);
995 /* read a maildir style mailbox */
996 int maildir_read_dir (CONTEXT * ctx)
998 /* maildir looks sort of like MH, except that there are two subdirectories
999 * of the main folder path from which to read messages
1001 if (mh_read_dir (ctx, "new") == -1 || mh_read_dir (ctx, "cur") == -1)
1008 * Open a new (temporary) message in an MH folder.
1011 int mh_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr)
1013 return mh_mkstemp (dest, &msg->fp, &msg->path);
1016 int ch_compar (const void *a, const void *b)
1018 return (int)( *((const char *) a) - *((const char *) b));
1021 static void maildir_flags (char *dest, size_t destlen, HEADER * hdr)
1026 * The maildir specification requires that all files in the cur
1027 * subdirectory have the :unique string appeneded, regardless of whether
1028 * or not there are any flags. If .old is set, we know that this message
1029 * will end up in the cur directory, so we include it in the following
1030 * test even though there is no associated flag.
1033 if (hdr && (hdr->flagged || hdr->replied || hdr->read || hdr->deleted || hdr->old || hdr->maildir_flags))
1035 char tmp[LONG_STRING];
1036 snprintf (tmp, sizeof (tmp),
1038 hdr->flagged ? "F" : "",
1039 hdr->replied ? "R" : "",
1040 hdr->read ? "S" : "", hdr->deleted ? "T" : "",
1041 NONULL(hdr->maildir_flags));
1042 if (hdr->maildir_flags)
1043 qsort (tmp, strlen (tmp), 1, ch_compar);
1044 snprintf (dest, destlen, ":2,%s", tmp);
1050 * Open a new (temporary) message in a maildir folder.
1052 * Note that this uses _almost_ the maildir file name format, but
1053 * with a {cur,new} prefix.
1057 int maildir_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr)
1060 char path[_POSIX_PATH_MAX];
1066 short deleted = hdr->deleted;
1069 maildir_flags (suffix, sizeof (suffix), hdr);
1071 hdr->deleted = deleted;
1076 if (hdr && (hdr->read || hdr->old))
1077 strfcpy (subdir, "cur", sizeof (subdir));
1079 strfcpy (subdir, "new", sizeof (subdir));
1083 snprintf (path, _POSIX_PATH_MAX, "%s/tmp/%s.%ld.%u_%d.%s%s",
1084 dest->path, subdir, time (NULL), (unsigned int)getpid (),
1085 Counter++, NONULL (Hostname), suffix);
1087 dprint (2, (debugfile, "maildir_open_new_message (): Trying %s.\n",
1091 if ((fd = open (path, O_WRONLY | O_EXCL | O_CREAT, 0666)) == -1)
1093 if (errno != EEXIST)
1101 dprint (2, (debugfile, "maildir_open_new_message (): Success.\n"));
1102 msg->path = safe_strdup (path);
1107 if ((msg->fp = fdopen (fd, "w")) == NULL)
1121 * Commit a message to a maildir folder.
1123 * msg->path contains the file name of a file in tmp/. We take the
1124 * flags from this file's name.
1126 * ctx is the mail folder we commit to.
1128 * hdr is a header structure to which we write the message's new
1129 * file name. This is used in the mh and maildir folder synch
1130 * routines. When this routine is invoked from mx_commit_message,
1133 * msg->path looks like this:
1135 * tmp/{cur,new}.mutt-HOSTNAME-PID-COUNTER:flags
1137 * See also maildir_open_new_message().
1141 int maildir_commit_message (CONTEXT * ctx, MESSAGE * msg, HEADER * hdr)
1145 char path[_POSIX_PATH_MAX];
1146 char full[_POSIX_PATH_MAX];
1149 if (safe_fclose (&msg->fp) != 0)
1152 /* extract the subdir */
1153 s = strrchr (msg->path, '/') + 1;
1154 strfcpy (subdir, s, 4);
1156 /* extract the flags */
1157 if ((s = strchr (s, ':')))
1158 strfcpy (suffix, s, sizeof (suffix));
1162 /* construct a new file name. */
1165 snprintf (path, _POSIX_PATH_MAX, "%s/%ld.%u_%d.%s%s", subdir,
1166 time (NULL), (unsigned int)getpid (), Counter++,
1167 NONULL (Hostname), suffix);
1168 snprintf (full, _POSIX_PATH_MAX, "%s/%s", ctx->path, path);
1170 dprint (2, (debugfile, "maildir_commit_message (): renaming %s to %s.\n",
1173 if (safe_rename (msg->path, full) == 0)
1176 mutt_str_replace (&hdr->path, path);
1180 * Adjust the mtime on the file to match the time at which this
1181 * message was received. Currently this is only set when copying
1182 * messages between mailboxes, so we test to ensure that it is
1189 ut.actime = msg->received;
1190 ut.modtime = msg->received;
1191 if (utime (full, &ut))
1193 mutt_perror (_("maildir_commit_message(): unable to set time on file"));
1200 else if (errno != EEXIST)
1202 mutt_perror (ctx->path);
1209 * commit a message to an MH folder.
1214 static int _mh_commit_message (CONTEXT * ctx, MESSAGE * msg, HEADER * hdr,
1220 unsigned int n, hi = 0;
1221 char path[_POSIX_PATH_MAX];
1224 if (safe_fclose (&msg->fp) != 0)
1227 if ((dirp = opendir (ctx->path)) == NULL)
1229 mutt_perror (ctx->path);
1233 /* figure out what the next message number is */
1234 while ((de = readdir (dirp)) != NULL)
1242 if (!isdigit ((unsigned char) *cp))
1256 * Now try to rename the file to the proper name.
1258 * Note: We may have to try multiple times, until we find a free
1265 snprintf (tmp, sizeof (tmp), "%d", hi);
1266 snprintf (path, sizeof (path), "%s/%s", ctx->path, tmp);
1267 if (safe_rename (msg->path, path) == 0)
1270 mutt_str_replace (&hdr->path, tmp);
1274 else if (errno != EEXIST)
1276 mutt_perror (ctx->path);
1281 mh_sequences_add_one (ctx, hi, !msg->flags.read, msg->flags.flagged,
1282 msg->flags.replied);
1286 int mh_commit_message (CONTEXT * ctx, MESSAGE * msg, HEADER * hdr)
1288 return _mh_commit_message (ctx, msg, hdr, 1);
1292 /* Sync a message in an MH folder.
1294 * This code is also used for attachment deletion in maildir
1298 static int mh_rewrite_message (CONTEXT * ctx, int msgno)
1300 HEADER *h = ctx->hdrs[msgno];
1305 char oldpath[_POSIX_PATH_MAX];
1306 char newpath[_POSIX_PATH_MAX];
1307 char partpath[_POSIX_PATH_MAX];
1309 long old_body_offset = h->content->offset;
1310 long old_body_length = h->content->length;
1311 long old_hdr_lines = h->lines;
1313 if ((dest = mx_open_new_message (ctx, h, 0)) == NULL)
1316 if ((rc = mutt_copy_message (dest->fp, ctx, h,
1317 M_CM_UPDATE, CH_UPDATE | CH_UPDATE_LEN)) == 0)
1319 snprintf (oldpath, _POSIX_PATH_MAX, "%s/%s", ctx->path, h->path);
1320 strfcpy (partpath, h->path, _POSIX_PATH_MAX);
1322 if (ctx->magic == M_MAILDIR)
1323 rc = maildir_commit_message (ctx, dest, h);
1325 rc = _mh_commit_message (ctx, dest, h, 0);
1327 mx_close_message (&dest);
1336 * Try to move the new message to the old place.
1339 * This is important when we are just updating flags.
1341 * Note that there is a race condition against programs which
1342 * use the first free slot instead of the maximum message
1343 * number. Mutt does _not_ behave like this.
1345 * Anyway, if this fails, the message is in the folder, so
1346 * all what happens is that a concurrently runnung mutt will
1347 * lose flag modifications.
1350 if (ctx->magic == M_MH && rc == 0)
1352 snprintf (newpath, _POSIX_PATH_MAX, "%s/%s", ctx->path, h->path);
1353 if ((rc = safe_rename (newpath, oldpath)) == 0)
1354 mutt_str_replace (&h->path, partpath);
1358 mx_close_message (&dest);
1360 if (rc == -1 && restore)
1362 h->content->offset = old_body_offset;
1363 h->content->length = old_body_length;
1364 h->lines = old_hdr_lines;
1367 mutt_free_body (&h->content->parts);
1371 static int mh_sync_message (CONTEXT * ctx, int msgno)
1373 HEADER *h = ctx->hdrs[msgno];
1375 if (h->attach_del || h->refs_changed || h->irt_changed)
1376 if (mh_rewrite_message (ctx, msgno) != 0)
1382 static int maildir_sync_message (CONTEXT * ctx, int msgno)
1384 HEADER *h = ctx->hdrs[msgno];
1386 if (h->attach_del || h->refs_changed || h->irt_changed)
1388 /* when doing attachment deletion/rethreading, fall back to the MH case. */
1389 if (mh_rewrite_message (ctx, msgno) != 0)
1394 /* we just have to rename the file. */
1396 char newpath[_POSIX_PATH_MAX];
1397 char partpath[_POSIX_PATH_MAX];
1398 char fullpath[_POSIX_PATH_MAX];
1399 char oldpath[_POSIX_PATH_MAX];
1403 if ((p = strrchr (h->path, '/')) == NULL)
1407 "maildir_sync_message: %s: unable to find subdir!\n",
1412 strfcpy (newpath, p, sizeof (newpath));
1414 /* kill the previous flags */
1415 if ((p = strchr (newpath, ':')) != NULL)
1418 maildir_flags (suffix, sizeof (suffix), h);
1420 snprintf (partpath, sizeof (partpath), "%s/%s%s",
1421 (h->read || h->old) ? "cur" : "new", newpath, suffix);
1422 snprintf (fullpath, sizeof (fullpath), "%s/%s", ctx->path, partpath);
1423 snprintf (oldpath, sizeof (oldpath), "%s/%s", ctx->path, h->path);
1425 if (mutt_strcmp (fullpath, oldpath) == 0)
1427 /* message hasn't really changed */
1431 /* record that the message is possibly marked as trashed on disk */
1432 h->trash = h->deleted;
1434 if (rename (oldpath, fullpath) != 0)
1436 mutt_perror ("rename");
1439 mutt_str_replace (&h->path, partpath);
1444 int mh_sync_mailbox (CONTEXT * ctx, int *index_hint)
1446 char path[_POSIX_PATH_MAX], tmp[_POSIX_PATH_MAX];
1450 #endif /* USE_HCACHE */
1452 if (ctx->magic == M_MH)
1453 i = mh_check_mailbox (ctx, index_hint);
1455 i = maildir_check_mailbox (ctx, index_hint);
1461 if (ctx->magic == M_MAILDIR)
1462 hc = mutt_hcache_open(HeaderCache, ctx->path);
1463 #endif /* USE_HCACHE */
1465 for (i = 0; i < ctx->msgcount; i++)
1467 if (ctx->hdrs[i]->deleted
1468 && (ctx->magic != M_MAILDIR || !option (OPTMAILDIRTRASH)))
1470 snprintf (path, sizeof (path), "%s/%s", ctx->path, ctx->hdrs[i]->path);
1471 if (ctx->magic == M_MAILDIR
1472 || (option (OPTMHPURGE) && ctx->magic == M_MH))
1475 if (ctx->magic == M_MAILDIR)
1476 mutt_hcache_delete (hc, ctx->hdrs[i]->path + 3, &maildir_hcache_keylen);
1477 #endif /* USE_HCACHE */
1480 else if (ctx->magic == M_MH)
1482 /* MH just moves files out of the way when you delete them */
1483 if (*ctx->hdrs[i]->path != ',')
1485 snprintf (tmp, sizeof (tmp), "%s/,%s", ctx->path,
1486 ctx->hdrs[i]->path);
1493 else if (ctx->hdrs[i]->changed || ctx->hdrs[i]->attach_del ||
1494 (ctx->magic == M_MAILDIR
1495 && (option (OPTMAILDIRTRASH) || ctx->hdrs[i]->trash)
1496 && (ctx->hdrs[i]->deleted != ctx->hdrs[i]->trash)))
1498 if (ctx->magic == M_MAILDIR)
1500 if (maildir_sync_message (ctx, i) == -1)
1505 if (mh_sync_message (ctx, i) == -1)
1512 if (ctx->magic == M_MAILDIR)
1513 mutt_hcache_close (hc);
1514 #endif /* USE_HCACHE */
1516 if (ctx->magic == M_MH)
1517 mh_update_sequences (ctx);
1519 /* XXX race condition? */
1521 maildir_update_mtime (ctx);
1523 /* adjust indices */
1527 for (i = 0, j = 0; i < ctx->msgcount; i++)
1529 if (!ctx->hdrs[i]->deleted
1530 || (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))
1531 ctx->hdrs[i]->index = j++;
1539 if (ctx->magic == M_MAILDIR)
1540 mutt_hcache_close (hc);
1541 #endif /* USE_HCACHE */
1545 static char *maildir_canon_filename (char *dest, const char *src, size_t l)
1549 if ((t = strrchr (src, '/')))
1552 strfcpy (dest, src, l);
1553 if ((u = strrchr (dest, ':')))
1559 static void maildir_update_tables (CONTEXT *ctx, int *index_hint)
1565 if (Sort != SORT_ORDER)
1569 mutt_sort_headers (ctx, 1);
1573 old_count = ctx->msgcount;
1574 for (i = 0, j = 0; i < old_count; i++)
1576 if (ctx->hdrs[i]->active && index_hint && *index_hint == i)
1579 if (ctx->hdrs[i]->active)
1580 ctx->hdrs[i]->index = j++;
1583 mx_update_tables (ctx, 0);
1584 mutt_clear_threads (ctx);
1587 static void maildir_update_flags (CONTEXT *ctx, HEADER *o, HEADER *n)
1589 /* save the global state here so we can reset it at the
1590 * end of list block if required.
1592 int context_changed = ctx->changed;
1594 /* user didn't modify this message. alter the flags to
1595 * match the current state on disk. This may not actually
1596 * do anything, but we can't tell right now. mutt_set_flag()
1597 * will just ignore the call if the status bits are
1598 * already properly set.
1600 mutt_set_flag (ctx, o, M_FLAG, n->flagged);
1601 mutt_set_flag (ctx, o, M_REPLIED, n->replied);
1602 mutt_set_flag (ctx, o, M_READ, n->read);
1603 mutt_set_flag (ctx, o, M_OLD, n->old);
1605 /* mutt_set_flag() will set this, but we don't need to
1606 * sync the changes we made because we just updated the
1607 * context to match the current on-disk state of the
1612 /* if the mailbox was not modified before we made these
1613 * changes, unset the changed flag since nothing needs to
1616 if (!context_changed)
1621 /* This function handles arrival of new mail and reopening of
1622 * maildir folders. The basic idea here is we check to see if either
1623 * the new or cur subdirectories have changed, and if so, we scan them
1624 * for the list of files. We check for newly added messages, and
1625 * then merge the flags messages we already knew about. We don't treat
1626 * either subdirectory differently, as mail could be copied directly into
1627 * the cur directory from another agent.
1629 int maildir_check_mailbox (CONTEXT * ctx, int *index_hint)
1631 struct stat st_new; /* status of the "new" subdirectory */
1632 struct stat st_cur; /* status of the "cur" subdirectory */
1633 char buf[_POSIX_PATH_MAX];
1634 int changed = 0; /* bitmask representing which subdirectories
1635 have changed. 0x1 = new, 0x2 = cur */
1636 int occult = 0; /* messages were removed from the mailbox */
1637 int have_new = 0; /* messages were added to the mailbox */
1638 struct maildir *md; /* list of messages in the mailbox */
1639 struct maildir **last, *p;
1641 HASH *fnames; /* hash table for quickly looking up the base filename
1642 for a maildir message */
1644 /* XXX seems like this check belongs in mx_check_mailbox()
1647 if (!option (OPTCHECKNEW))
1650 snprintf (buf, sizeof (buf), "%s/new", ctx->path);
1651 if (stat (buf, &st_new) == -1)
1654 snprintf (buf, sizeof (buf), "%s/cur", ctx->path);
1655 if (stat (buf, &st_cur) == -1)
1658 /* determine which subdirectories need to be scanned */
1659 if (st_new.st_mtime > ctx->mtime)
1661 if (st_cur.st_mtime > ctx->mtime_cur)
1665 return 0; /* nothing to do */
1667 /* update the modification times on the mailbox */
1668 ctx->mtime_cur = st_cur.st_mtime;
1669 ctx->mtime = st_new.st_mtime;
1671 /* do a fast scan of just the filenames in
1672 * the subdirectories that have changed.
1677 maildir_parse_dir (ctx, &last, "new", NULL);
1679 maildir_parse_dir (ctx, &last, "cur", NULL);
1681 /* we create a hash table keyed off the canonical (sans flags) filename
1682 * of each message we scanned. This is used in the loop over the
1683 * existing messages below to do some correlation.
1685 fnames = hash_create (1031);
1687 for (p = md; p; p = p->next)
1689 maildir_canon_filename (buf, p->h->path, sizeof (buf));
1690 p->canon_fname = safe_strdup (buf);
1691 hash_insert (fnames, p->canon_fname, p, 0);
1694 /* check for modifications and adjust flags */
1695 for (i = 0; i < ctx->msgcount; i++)
1697 ctx->hdrs[i]->active = 0;
1698 maildir_canon_filename (buf, ctx->hdrs[i]->path, sizeof (buf));
1699 p = hash_find (fnames, buf);
1702 /* message already exists, merge flags */
1703 ctx->hdrs[i]->active = 1;
1705 /* check to see if the message has moved to a different
1706 * subdirectory. If so, update the associated filename.
1708 if (mutt_strcmp (ctx->hdrs[i]->path, p->h->path))
1709 mutt_str_replace (&ctx->hdrs[i]->path, p->h->path);
1711 /* if the user hasn't modified the flags on this message, update
1712 * the flags we just detected.
1714 if (!ctx->hdrs[i]->changed)
1715 maildir_update_flags (ctx, ctx->hdrs[i], p->h);
1717 if (ctx->hdrs[i]->deleted == ctx->hdrs[i]->trash)
1718 ctx->hdrs[i]->deleted = p->h->deleted;
1719 ctx->hdrs[i]->trash = p->h->trash;
1721 /* this is a duplicate of an existing header, so remove it */
1722 mutt_free_header (&p->h);
1724 /* This message was not in the list of messages we just scanned.
1725 * Check to see if we have enough information to know if the
1726 * message has disappeared out from underneath us.
1728 else if (((changed & 1) && (!strncmp (ctx->hdrs[i]->path, "new/", 4))) ||
1729 ((changed & 2) && (!strncmp (ctx->hdrs[i]->path, "cur/", 4))))
1731 /* This message disappeared, so we need to simulate a "reopen"
1732 * event. We know it disappeared because we just scanned the
1733 * subdirectory it used to reside in.
1739 /* This message resides in a subdirectory which was not
1740 * modified, so we assume that it is still present and
1743 ctx->hdrs[i]->active = 1;
1747 /* destroy the file name hash */
1748 hash_destroy (&fnames, NULL);
1750 /* If we didn't just get new mail, update the tables. */
1752 maildir_update_tables (ctx, index_hint);
1754 /* do any delayed parsing we need to do. */
1755 maildir_delayed_parsing (ctx, md);
1757 /* Incorporate new messages */
1758 have_new = maildir_move_to_context (ctx, &md);
1760 return occult ? M_REOPENED : (have_new ? M_NEW_MAIL : 0);
1764 * This function handles arrival of new mail and reopening of
1765 * mh/maildir folders. Things are getting rather complex because we
1766 * don't have a well-defined "mailbox order", so the tricks from
1767 * mbox.c and mx.c won't work here.
1769 * Don't change this code unless you _really_ understand what
1774 int mh_check_mailbox (CONTEXT * ctx, int *index_hint)
1776 char buf[_POSIX_PATH_MAX];
1777 struct stat st, st_cur;
1778 short modified = 0, have_new = 0, occult = 0;
1779 struct maildir *md, *p;
1780 struct maildir **last = NULL;
1781 struct mh_sequences mhs;
1785 if (!option (OPTCHECKNEW))
1788 strfcpy (buf, ctx->path, sizeof (buf));
1789 if (stat (buf, &st) == -1)
1792 /* create .mh_sequences when there isn't one. */
1793 snprintf (buf, sizeof (buf), "%s/.mh_sequences", ctx->path);
1794 if ((i = stat (buf, &st_cur) == -1) && errno == ENOENT)
1799 if (mh_mkstemp (ctx, &fp, &tmp) == 0)
1802 if (safe_rename (tmp, buf) == -1)
1808 if (i == -1 && stat (buf, &st_cur) == -1)
1811 if (st.st_mtime > ctx->mtime || st_cur.st_mtime > ctx->mtime_cur)
1817 ctx->mtime_cur = st_cur.st_mtime;
1818 ctx->mtime = st.st_mtime;
1820 memset (&mhs, 0, sizeof (mhs));
1824 maildir_parse_dir (ctx, &last, NULL, NULL);
1825 mh_read_sequences (&mhs, ctx->path);
1826 mh_update_maildir (md, &mhs);
1827 mhs_free_sequences (&mhs);
1829 /* check for modifications and adjust flags */
1830 fnames = hash_create (1031);
1832 for (p = md; p; p = p->next)
1833 hash_insert (fnames, p->h->path, p, 0);
1835 for (i = 0; i < ctx->msgcount; i++)
1837 ctx->hdrs[i]->active = 0;
1839 if ((p = hash_find (fnames, ctx->hdrs[i]->path)) && p->h &&
1840 (mbox_strict_cmp_headers (ctx->hdrs[i], p->h)))
1842 ctx->hdrs[i]->active = 1;
1843 /* found the right message */
1844 if (!ctx->hdrs[i]->changed)
1845 maildir_update_flags (ctx, ctx->hdrs[i], p->h);
1847 mutt_free_header (&p->h);
1849 else /* message has disappeared */
1853 /* destroy the file name hash */
1855 hash_destroy (&fnames, NULL);
1857 /* If we didn't just get new mail, update the tables. */
1859 maildir_update_tables (ctx, index_hint);
1861 /* Incorporate new messages */
1862 have_new = maildir_move_to_context (ctx, &md);
1864 return occult ? M_REOPENED : (have_new ? M_NEW_MAIL : 0);
1871 * These functions try to find a message in a maildir folder when it
1872 * has moved under our feet. Note that this code is rather expensive, but
1873 * then again, it's called rarely.
1876 FILE *_maildir_open_find_message (const char *folder, const char *unique,
1877 const char *subfolder)
1879 char dir[_POSIX_PATH_MAX];
1880 char tunique[_POSIX_PATH_MAX];
1881 char fname[_POSIX_PATH_MAX];
1889 snprintf (dir, sizeof (dir), "%s/%s", folder, subfolder);
1891 if ((dp = opendir (dir)) == NULL)
1897 while ((de = readdir (dp)))
1899 maildir_canon_filename (tunique, de->d_name, sizeof (tunique));
1901 if (!mutt_strcmp (tunique, unique))
1903 snprintf (fname, sizeof (fname), "%s/%s/%s", folder, subfolder,
1905 fp = fopen (fname, "r"); /* __FOPEN_CHECKED__ */
1917 FILE *maildir_open_find_message (const char *folder, const char *msg)
1919 char unique[_POSIX_PATH_MAX];
1922 static unsigned int new_hits = 0, cur_hits = 0; /* simple dynamic optimization */
1924 maildir_canon_filename (unique, msg, sizeof (unique));
1928 _maildir_open_find_message (folder, unique,
1929 new_hits > cur_hits ? "new" : "cur"))
1932 if (new_hits < UINT_MAX && cur_hits < UINT_MAX)
1934 new_hits += (new_hits > cur_hits ? 1 : 0);
1935 cur_hits += (new_hits > cur_hits ? 0 : 1);
1942 _maildir_open_find_message (folder, unique,
1943 new_hits > cur_hits ? "cur" : "new"))
1946 if (new_hits < UINT_MAX && cur_hits < UINT_MAX)
1948 new_hits += (new_hits > cur_hits ? 0 : 1);
1949 cur_hits += (new_hits > cur_hits ? 1 : 0);
1961 * 1 if there are no messages in the mailbox
1962 * 0 if there are messages in the mailbox
1965 int maildir_check_empty (const char *path)
1969 int r = 1; /* assume empty until we find a message */
1970 char realpath[_POSIX_PATH_MAX];
1973 /* Strategy here is to look for any file not beginning with a period */
1976 /* we do "cur" on the first iteration since its more likely that we'll
1977 * find old messages without having to scan both subdirs
1979 snprintf (realpath, sizeof (realpath), "%s/%s", path,
1980 iter == 0 ? "cur" : "new");
1981 if ((dp = opendir (realpath)) == NULL)
1983 while ((de = readdir (dp)))
1985 if (*de->d_name != '.')
1993 } while (r && iter < 2);
2000 * 1 if there are no messages in the mailbox
2001 * 0 if there are messages in the mailbox
2004 int mh_check_empty (const char *path)
2008 int r = 1; /* assume empty until we find a message */
2010 if ((dp = opendir (path)) == NULL)
2012 while ((de = readdir (dp)))
2014 if (mh_valid_message (de->d_name))