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
28 #include <sys/types.h>
44 unsigned header_parsed:1;
54 /* mh_sequences support */
56 #define MH_SEQ_UNSEEN (1 << 0)
57 #define MH_SEQ_REPLIED (1 << 1)
58 #define MH_SEQ_FLAGGED (1 << 2)
60 static void mhs_alloc (struct mh_sequences *mhs, int i)
65 if (i > mhs->max || !mhs->flags) {
67 safe_realloc (&mhs->flags, sizeof (mhs->flags[0]) * (newmax + 1));
68 for (j = mhs->max + 1; j <= newmax; j++)
75 static void mhs_free_sequences (struct mh_sequences *mhs)
80 static short mhs_check (struct mh_sequences *mhs, int i)
82 if (!mhs->flags || i > mhs->max)
88 static short mhs_set (struct mh_sequences *mhs, int i, short f)
99 static short mhs_unset (struct mh_sequences *mhs, int i, short f)
103 return mhs->flags[i];
108 static void mh_read_token (char *t, int *first, int *last)
112 if ((p = strchr (t, '-'))) {
118 *first = *last = atoi (t);
121 static void mh_read_sequences (struct mh_sequences *mhs, const char *path)
132 char pathname[_POSIX_PATH_MAX];
134 snprintf (pathname, sizeof (pathname), "%s/.mh_sequences", path);
136 if (!(fp = fopen (pathname, "r")))
139 while ((buff = mutt_read_line (buff, &sz, fp, &line))) {
140 if (!(t = strtok (buff, " \t:")))
143 if (!mutt_strcmp (t, MhUnseen))
145 else if (!mutt_strcmp (t, MhFlagged))
147 else if (!mutt_strcmp (t, MhReplied))
149 else /* unknown sequence */
152 while ((t = strtok (NULL, " \t:"))) {
153 mh_read_token (t, &first, &last);
154 for (; first <= last; first++)
155 mhs_set (mhs, first, f);
163 int mh_buffy (const char *path)
166 struct mh_sequences mhs;
168 memset (&mhs, 0, sizeof (mhs));
170 mh_read_sequences (&mhs, path);
171 for (i = 0; !r && i <= mhs.max; i++)
172 if (mhs_check (&mhs, i) & MH_SEQ_UNSEEN)
174 mhs_free_sequences (&mhs);
178 static int mh_mkstemp (CONTEXT * dest, FILE ** fp, char **tgt)
181 char path[_POSIX_PATH_MAX];
184 snprintf (path, _POSIX_PATH_MAX, "%s/.mutt-%s-%d-%d",
185 dest->path, NONULL (Hostname), (int) getpid (), Counter++);
187 if ((fd = open (path, O_WRONLY | O_EXCL | O_CREAT, 0666)) == -1) {
188 if (errno != EEXIST) {
194 *tgt = safe_strdup (path);
199 if ((*fp = fdopen (fd, "w")) == NULL) {
209 static void mhs_write_one_sequence (FILE * fp, struct mh_sequences *mhs,
210 short f, const char *tag)
215 fprintf (fp, "%s:", tag);
220 for (i = 0; i <= mhs->max; i++) {
221 if ((mhs_check (mhs, i) & f)) {
227 else if (first >= 0) {
229 fprintf (fp, " %d", first);
231 fprintf (fp, " %d-%d", first, last);
240 fprintf (fp, " %d", first);
242 fprintf (fp, " %d-%d", first, last);
248 /* XXX - we don't currently remove deleted messages from sequences we don't know. Should we? */
250 void mh_update_sequences (CONTEXT * ctx)
254 char sequences[_POSIX_PATH_MAX];
266 char seq_unseen[STRING];
267 char seq_replied[STRING];
268 char seq_flagged[STRING];
271 struct mh_sequences mhs;
273 memset (&mhs, 0, sizeof (mhs));
275 snprintf (seq_unseen, sizeof (seq_unseen), "%s:", NONULL (MhUnseen));
276 snprintf (seq_replied, sizeof (seq_replied), "%s:", NONULL (MhReplied));
277 snprintf (seq_flagged, sizeof (seq_flagged), "%s:", NONULL (MhFlagged));
279 if (mh_mkstemp (ctx, &nfp, &tmpfname) != 0) {
284 snprintf (sequences, sizeof (sequences), "%s/.mh_sequences", ctx->path);
287 /* first, copy unknown sequences */
288 if ((ofp = fopen (sequences, "r"))) {
289 while ((buff = mutt_read_line (buff, &s, ofp, &l))) {
290 if (!mutt_strncmp (buff, seq_unseen, mutt_strlen (seq_unseen)))
292 if (!mutt_strncmp (buff, seq_flagged, mutt_strlen (seq_flagged)))
294 if (!mutt_strncmp (buff, seq_replied, mutt_strlen (seq_replied)))
297 fprintf (nfp, "%s\n", buff);
302 /* now, update our unseen, flagged, and replied sequences */
303 for (l = 0; l < ctx->msgcount; l++) {
304 if (ctx->hdrs[l]->deleted)
307 if ((p = strrchr (ctx->hdrs[l]->path, '/')))
310 p = ctx->hdrs[l]->path;
314 if (!ctx->hdrs[l]->read) {
315 mhs_set (&mhs, i, MH_SEQ_UNSEEN);
318 if (ctx->hdrs[l]->flagged) {
319 mhs_set (&mhs, i, MH_SEQ_FLAGGED);
322 if (ctx->hdrs[l]->replied) {
323 mhs_set (&mhs, i, MH_SEQ_REPLIED);
328 /* write out the new sequences */
330 mhs_write_one_sequence (nfp, &mhs, MH_SEQ_UNSEEN, NONULL (MhUnseen));
332 mhs_write_one_sequence (nfp, &mhs, MH_SEQ_FLAGGED, NONULL (MhFlagged));
334 mhs_write_one_sequence (nfp, &mhs, MH_SEQ_REPLIED, NONULL (MhReplied));
336 mhs_free_sequences (&mhs);
339 /* try to commit the changes - no guarantee here */
343 if (safe_rename (tmpfname, sequences) != 0) {
344 /* report an error? */
351 static void mh_sequences_add_one (CONTEXT * ctx, int n, short unseen,
352 short flagged, short replied)
354 short unseen_done = 0;
355 short flagged_done = 0;
356 short replied_done = 0;
358 FILE *ofp = NULL, *nfp = NULL;
361 char sequences[_POSIX_PATH_MAX];
363 char seq_unseen[STRING];
364 char seq_replied[STRING];
365 char seq_flagged[STRING];
371 if (mh_mkstemp (ctx, &nfp, &tmpfname) == -1)
374 snprintf (seq_unseen, sizeof (seq_unseen), "%s:", NONULL (MhUnseen));
375 snprintf (seq_replied, sizeof (seq_replied), "%s:", NONULL (MhReplied));
376 snprintf (seq_flagged, sizeof (seq_flagged), "%s:", NONULL (MhFlagged));
378 snprintf (sequences, sizeof (sequences), "%s/.mh_sequences", ctx->path);
379 if ((ofp = fopen (sequences, "r"))) {
380 while ((buff = mutt_read_line (buff, &sz, ofp, &line))) {
381 if (unseen && !strncmp (buff, seq_unseen, mutt_strlen (seq_unseen))) {
382 fprintf (nfp, "%s %d\n", buff, n);
386 && !strncmp (buff, seq_flagged, mutt_strlen (seq_flagged))) {
387 fprintf (nfp, "%s %d\n", buff, n);
391 && !strncmp (buff, seq_replied, mutt_strlen (seq_replied))) {
392 fprintf (nfp, "%s %d\n", buff, n);
396 fprintf (nfp, "%s\n", buff);
402 if (!unseen_done && unseen)
403 fprintf (nfp, "%s: %d\n", NONULL (MhUnseen), n);
404 if (!flagged_done && flagged)
405 fprintf (nfp, "%s: %d\n", NONULL (MhFlagged), n);
406 if (!replied_done && replied)
407 fprintf (nfp, "%s: %d\n", NONULL (MhReplied), n);
412 if (safe_rename (tmpfname, sequences) != 0)
418 static void mh_update_maildir (struct maildir *md, struct mh_sequences *mhs)
424 for (; md; md = md->next) {
425 if ((p = strrchr (md->h->path, '/')))
431 f = mhs_check (mhs, i);
433 md->h->read = (f & MH_SEQ_UNSEEN) ? 0 : 1;
434 md->h->flagged = (f & MH_SEQ_FLAGGED) ? 1 : 0;
435 md->h->replied = (f & MH_SEQ_REPLIED) ? 1 : 0;
439 /* maildir support */
441 static void maildir_free_entry (struct maildir **md)
446 FREE (&(*md)->canon_fname);
448 mutt_free_header (&(*md)->h);
453 static void maildir_free_maildir (struct maildir **md)
455 struct maildir *p, *q;
460 for (p = *md; p; p = q) {
462 maildir_free_entry (&p);
466 static void maildir_parse_flags (HEADER * h, const char *path)
474 if ((p = strrchr (path, ':')) != NULL && mutt_strncmp (p + 1, "2,", 2) == 0) {
477 mutt_str_replace (&h->maildir_flags, p);
478 q = h->maildir_flags;
492 case 'R': /* replied */
497 case 'T': /* trashed */
510 if (q == h->maildir_flags)
511 FREE (&h->maildir_flags);
516 static void maildir_update_mtime (CONTEXT * ctx)
518 char buf[_POSIX_PATH_MAX];
521 if (ctx->magic == M_MAILDIR) {
522 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, "cur");
523 if (stat (buf, &st) == 0)
524 ctx->mtime_cur = st.st_mtime;
525 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, "new");
528 snprintf (buf, sizeof (buf), "%s/.mh_sequences", ctx->path);
529 if (stat (buf, &st) == 0)
530 ctx->mtime_cur = st.st_mtime;
532 strfcpy (buf, ctx->path, sizeof (buf));
535 if (stat (buf, &st) == 0)
536 ctx->mtime = st.st_mtime;
540 * Actually parse a maildir message. This may also be used to fill
541 * out a fake header structure generated by lazy maildir parsing.
543 static HEADER *maildir_parse_message (int magic, const char *fname,
544 int is_old, HEADER * _h)
550 if ((f = fopen (fname, "r")) != NULL) {
552 h = mutt_new_header ();
553 h->env = mutt_read_rfc822_header (f, h, 0, 0);
555 fstat (fileno (f), &st);
559 h->received = h->date_sent;
561 if (h->content->length <= 0)
562 h->content->length = st.st_size - h->content->offset;
566 if (magic == M_MAILDIR) {
568 * maildir stores its flags in the filename, so ignore the
569 * flags in the header of the message
573 maildir_parse_flags (h, fname);
581 * Note that this routine will _not_ modify the context given by
584 * It's used in the first parsing pass on maildir and MH folders.
585 * In the MH case, this means full parsing of the folder. In the
586 * maildir case, it means that we only look at flags, and create a
587 * fake HEADER structure, which may later be filled in by
588 * maildir_parse_message(), when called from
589 * maildir_delayed_parsing().
593 static int maildir_parse_entry (CONTEXT * ctx, struct maildir ***last,
594 const char *subdir, const char *fname,
595 int *count, int is_old, ino_t inode)
597 struct maildir *entry;
599 char buf[_POSIX_PATH_MAX];
602 snprintf (buf, sizeof (buf), "%s/%s/%s", ctx->path, subdir, fname);
604 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, fname);
606 if (ctx->magic == M_MH)
607 h = maildir_parse_message (ctx->magic, buf, is_old, NULL);
609 h = mutt_new_header ();
611 maildir_parse_flags (h, buf);
617 if (!ctx->quiet && ReadInc && ((*count % ReadInc) == 0 || *count == 1))
618 mutt_message (_("Reading %s... %d"), ctx->path, *count);
622 snprintf (buf, sizeof (buf), "%s/%s", subdir, fname);
623 h->path = safe_strdup (buf);
626 h->path = safe_strdup (fname);
628 entry = safe_calloc (sizeof (struct maildir), 1);
630 entry->header_parsed = (ctx->magic == M_MH);
631 entry->inode = inode;
633 *last = &entry->next;
643 /* Ignore the garbage files. A valid MH message consists of only
644 * digits. Deleted message get moved to a filename with a comma before
648 int mh_valid_message (const char *s)
651 if (!isdigit ((unsigned char) *s))
657 static int maildir_parse_dir (CONTEXT * ctx, struct maildir ***last,
658 const char *subdir, int *count)
662 char buf[_POSIX_PATH_MAX];
666 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, subdir);
667 is_old = (mutt_strcmp ("cur", subdir) == 0);
670 strfcpy (buf, ctx->path, sizeof (buf));
672 if ((dirp = opendir (buf)) == NULL)
675 while ((de = readdir (dirp)) != NULL) {
677 if ((ctx->magic == M_MH && !mh_valid_message (de->d_name))
678 || (ctx->magic == M_MAILDIR && *de->d_name == '.'))
681 /* FOO - really ignore the return value? */
684 (debugfile, "%s:%d: parsing %s\n", __FILE__, __LINE__,
686 maildir_parse_entry (ctx, last, subdir, de->d_name, count, is_old,
694 static int maildir_add_to_context (CONTEXT * ctx, struct maildir *md)
696 int oldmsgcount = ctx->msgcount;
700 dprint (2, (debugfile, "%s:%d maildir_add_to_context(): Considering %s\n",
701 __FILE__, __LINE__, NONULL (md->canon_fname)));
706 "%s:%d Adding header structure. Flags: %s%s%s%s%s\n", __FILE__,
707 __LINE__, md->h->flagged ? "f" : "", md->h->deleted ? "D" : "",
708 md->h->replied ? "r" : "", md->h->old ? "O" : "",
709 md->h->read ? "R" : ""));
710 if (ctx->msgcount == ctx->hdrmax)
711 mx_alloc_memory (ctx);
713 ctx->hdrs[ctx->msgcount] = md->h;
714 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
716 md->h->content->length + md->h->content->offset -
717 md->h->content->hdr_offset;
725 if (ctx->msgcount > oldmsgcount) {
726 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
732 static int maildir_move_to_context (CONTEXT * ctx, struct maildir **md)
736 r = maildir_add_to_context (ctx, *md);
737 maildir_free_maildir (md);
742 * Merge two maildir lists according to the inode numbers.
744 static struct maildir *maildir_merge_inode (struct maildir *left,
745 struct maildir *right)
747 struct maildir *head;
748 struct maildir *tail;
751 if (left->inode < right->inode) {
769 while (left && right) {
770 if (left->inode < right->inode) {
792 * Sort maildir list according to inode.
794 static struct maildir *maildir_sort_inode (struct maildir *list)
796 struct maildir *left = list;
797 struct maildir *right = list;
799 if (!list || !list->next) {
804 while (list && list->next) {
806 list = list->next->next;
813 left = maildir_sort_inode (left);
814 right = maildir_sort_inode (right);
815 return maildir_merge_inode (left, right);
819 static size_t maildir_hcache_keylen (const char *fn)
821 const char *p = strchr (fn, ':');
823 return p ? (size_t) (p - fn) : mutt_strlen (fn);
828 * This function does the second parsing pass for a maildir-style
831 void maildir_delayed_parsing (CONTEXT * ctx, struct maildir *md)
834 char fn[_POSIX_PATH_MAX];
840 struct timeval *when = NULL;
841 struct stat lastchanged;
844 hc = mutt_hcache_open (HeaderCache, ctx->path);
847 for (p = md, count = 0; p; p = p->next, count++) {
848 if (!(p && p->h && !p->header_parsed))
852 data = mutt_hcache_fetch (hc, p->h->path + 3, &maildir_hcache_keylen);
853 when = (struct timeval *) data;
856 if (!ctx->quiet && ReadInc && ((count % ReadInc) == 0 || count == 1))
857 mutt_message (_("Reading %s... %d"), ctx->path, count);
858 snprintf (fn, sizeof (fn), "%s/%s", ctx->path, p->h->path);
861 if (option (OPTHCACHEVERIFY)) {
862 ret = stat (fn, &lastchanged);
865 lastchanged.st_mtime = 0;
869 if (data != NULL && !ret && lastchanged.st_mtime <= when->tv_sec) {
870 p->h = mutt_hcache_restore ((unsigned char *) data, &p->h);
871 maildir_parse_flags (p->h, fn);
875 if (maildir_parse_message (ctx->magic, fn, p->h->old, p->h)) {
876 p->header_parsed = 1;
877 maildir_parse_flags (p->h, fn);
879 mutt_hcache_store (hc, p->h->path + 3, p->h, 0, &maildir_hcache_keylen);
883 mutt_free_header (&p->h);
889 mutt_hcache_close (hc);
893 /* Read a MH/maildir style mailbox.
896 * ctx [IN/OUT] context for this mailbox
897 * subdir [IN] NULL for MH mailboxes, otherwise the subdir of the
898 * maildir mailbox to read from
900 int mh_read_dir (CONTEXT * ctx, const char *subdir)
903 struct mh_sequences mhs;
904 struct maildir **last;
908 memset (&mhs, 0, sizeof (mhs));
910 maildir_update_mtime (ctx);
915 if (maildir_parse_dir (ctx, &last, subdir, &count) == -1)
918 if (ctx->magic == M_MH) {
919 mh_read_sequences (&mhs, ctx->path);
920 mh_update_maildir (md, &mhs);
921 mhs_free_sequences (&mhs);
924 md = maildir_sort_inode (md);
926 if (ctx->magic == M_MAILDIR)
927 maildir_delayed_parsing (ctx, md);
929 maildir_move_to_context (ctx, &md);
933 /* read a maildir style mailbox */
934 int maildir_read_dir (CONTEXT * ctx)
936 /* maildir looks sort of like MH, except that there are two subdirectories
937 * of the main folder path from which to read messages
939 if (mh_read_dir (ctx, "new") == -1 || mh_read_dir (ctx, "cur") == -1)
946 * Open a new (temporary) message in an MH folder.
949 int mh_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr)
951 return mh_mkstemp (dest, &msg->fp, &msg->path);
954 int ch_compar (const void *a, const void *b)
956 return (int) (*((const char *) a) - *((const char *) b));
959 static void maildir_flags (char *dest, size_t destlen, HEADER * hdr)
964 * The maildir specification requires that all files in the cur
965 * subdirectory have the :unique string appeneded, regardless of whether
966 * or not there are any flags. If .old is set, we know that this message
967 * will end up in the cur directory, so we include it in the following
968 * test even though there is no associated flag.
972 && (hdr->flagged || hdr->replied || hdr->read || hdr->deleted
973 || hdr->old || hdr->maildir_flags)) {
974 char tmp[LONG_STRING];
976 snprintf (tmp, sizeof (tmp),
978 hdr->flagged ? "F" : "",
979 hdr->replied ? "R" : "",
980 hdr->read ? "S" : "", hdr->deleted ? "T" : "",
981 NONULL (hdr->maildir_flags));
982 if (hdr->maildir_flags)
983 qsort (tmp, strlen (tmp), 1, ch_compar);
984 snprintf (dest, destlen, ":2,%s", tmp);
990 * Open a new (temporary) message in a maildir folder.
992 * Note that this uses _almost_ the maildir file name format, but
993 * with a {cur,new} prefix.
997 int maildir_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr)
1000 char path[_POSIX_PATH_MAX];
1005 short deleted = hdr->deleted;
1009 maildir_flags (suffix, sizeof (suffix), hdr);
1011 hdr->deleted = deleted;
1016 if (hdr && (hdr->read || hdr->old))
1017 strfcpy (subdir, "cur", sizeof (subdir));
1019 strfcpy (subdir, "new", sizeof (subdir));
1022 snprintf (path, _POSIX_PATH_MAX, "%s/tmp/%s.%ld.%u_%d.%s%s",
1023 dest->path, subdir, (long) time (NULL),
1024 (unsigned int) getpid (), Counter++, NONULL (Hostname), suffix);
1026 dprint (2, (debugfile, "maildir_open_new_message (): Trying %s.\n",
1030 if ((fd = open (path, O_WRONLY | O_EXCL | O_CREAT, 0666)) == -1) {
1031 if (errno != EEXIST) {
1037 dprint (2, (debugfile, "maildir_open_new_message (): Success.\n"));
1038 msg->path = safe_strdup (path);
1043 if ((msg->fp = fdopen (fd, "w")) == NULL) {
1056 * Commit a message to a maildir folder.
1058 * msg->path contains the file name of a file in tmp/. We take the
1059 * flags from this file's name.
1061 * ctx is the mail folder we commit to.
1063 * hdr is a header structure to which we write the message's new
1064 * file name. This is used in the mh and maildir folder synch
1065 * routines. When this routine is invoked from mx_commit_message,
1068 * msg->path looks like this:
1070 * tmp/{cur,new}.mutt-HOSTNAME-PID-COUNTER:flags
1072 * See also maildir_open_new_message().
1076 int maildir_commit_message (CONTEXT * ctx, MESSAGE * msg, HEADER * hdr)
1080 char path[_POSIX_PATH_MAX];
1081 char full[_POSIX_PATH_MAX];
1084 if (safe_fclose (&msg->fp) != 0)
1087 /* extract the subdir */
1088 s = strrchr (msg->path, '/') + 1;
1089 strfcpy (subdir, s, 4);
1091 /* extract the flags */
1092 if ((s = strchr (s, ':')))
1093 strfcpy (suffix, s, sizeof (suffix));
1097 /* construct a new file name. */
1099 snprintf (path, _POSIX_PATH_MAX, "%s/%ld.%u_%d.%s%s", subdir,
1100 (long) time (NULL), (unsigned int) getpid (), Counter++,
1101 NONULL (Hostname), suffix);
1102 snprintf (full, _POSIX_PATH_MAX, "%s/%s", ctx->path, path);
1104 dprint (2, (debugfile, "maildir_commit_message (): renaming %s to %s.\n",
1107 if (safe_rename (msg->path, full) == 0) {
1109 mutt_str_replace (&hdr->path, path);
1113 * Adjust the mtime on the file to match the time at which this
1114 * message was received. Currently this is only set when copying
1115 * messages between mailboxes, so we test to ensure that it is
1118 if (msg->received) {
1121 ut.actime = msg->received;
1122 ut.modtime = msg->received;
1123 if (utime (full, &ut)) {
1125 ("maildir_commit_message(): unable to set time on file"));
1132 else if (errno != EEXIST) {
1133 mutt_perror (ctx->path);
1140 * commit a message to an MH folder.
1145 static int _mh_commit_message (CONTEXT * ctx, MESSAGE * msg, HEADER * hdr,
1151 unsigned int n, hi = 0;
1152 char path[_POSIX_PATH_MAX];
1155 if (safe_fclose (&msg->fp) != 0)
1158 if ((dirp = opendir (ctx->path)) == NULL) {
1159 mutt_perror (ctx->path);
1163 /* figure out what the next message number is */
1164 while ((de = readdir (dirp)) != NULL) {
1170 if (!isdigit ((unsigned char) *cp))
1183 * Now try to rename the file to the proper name.
1185 * Note: We may have to try multiple times, until we find a free
1191 snprintf (tmp, sizeof (tmp), "%d", hi);
1192 snprintf (path, sizeof (path), "%s/%s", ctx->path, tmp);
1193 if (safe_rename (msg->path, path) == 0) {
1195 mutt_str_replace (&hdr->path, tmp);
1199 else if (errno != EEXIST) {
1200 mutt_perror (ctx->path);
1205 mh_sequences_add_one (ctx, hi, !msg->flags.read, msg->flags.flagged,
1206 msg->flags.replied);
1210 int mh_commit_message (CONTEXT * ctx, MESSAGE * msg, HEADER * hdr)
1212 return _mh_commit_message (ctx, msg, hdr, 1);
1216 /* Sync a message in an MH folder.
1218 * This code is also used for attachment deletion in maildir
1222 static int mh_rewrite_message (CONTEXT * ctx, int msgno)
1224 HEADER *h = ctx->hdrs[msgno];
1229 char oldpath[_POSIX_PATH_MAX];
1230 char newpath[_POSIX_PATH_MAX];
1231 char partpath[_POSIX_PATH_MAX];
1233 long old_body_offset = h->content->offset;
1234 long old_body_length = h->content->length;
1235 long old_hdr_lines = h->lines;
1237 if ((dest = mx_open_new_message (ctx, h, 0)) == NULL)
1240 if ((rc = mutt_copy_message (dest->fp, ctx, h,
1241 M_CM_UPDATE, CH_UPDATE | CH_UPDATE_LEN)) == 0)
1243 snprintf (oldpath, _POSIX_PATH_MAX, "%s/%s", ctx->path, h->path);
1244 strfcpy (partpath, h->path, _POSIX_PATH_MAX);
1246 if (ctx->magic == M_MAILDIR)
1247 rc = maildir_commit_message (ctx, dest, h);
1249 rc = _mh_commit_message (ctx, dest, h, 0);
1251 mx_close_message (&dest);
1259 * Try to move the new message to the old place.
1262 * This is important when we are just updating flags.
1264 * Note that there is a race condition against programs which
1265 * use the first free slot instead of the maximum message
1266 * number. Mutt does _not_ behave like this.
1268 * Anyway, if this fails, the message is in the folder, so
1269 * all what happens is that a concurrently runnung mutt will
1270 * lose flag modifications.
1273 if (ctx->magic == M_MH && rc == 0) {
1274 snprintf (newpath, _POSIX_PATH_MAX, "%s/%s", ctx->path, h->path);
1275 if ((rc = safe_rename (newpath, oldpath)) == 0)
1276 mutt_str_replace (&h->path, partpath);
1280 mx_close_message (&dest);
1282 if (rc == -1 && restore) {
1283 h->content->offset = old_body_offset;
1284 h->content->length = old_body_length;
1285 h->lines = old_hdr_lines;
1288 mutt_free_body (&h->content->parts);
1292 static int mh_sync_message (CONTEXT * ctx, int msgno)
1294 HEADER *h = ctx->hdrs[msgno];
1296 if (h->attach_del || h->refs_changed || h->irt_changed)
1297 if (mh_rewrite_message (ctx, msgno) != 0)
1303 static int maildir_sync_message (CONTEXT * ctx, int msgno)
1305 HEADER *h = ctx->hdrs[msgno];
1307 if (h->attach_del || h->refs_changed || h->irt_changed) {
1308 /* when doing attachment deletion/rethreading, fall back to the MH case. */
1309 if (mh_rewrite_message (ctx, msgno) != 0)
1313 /* we just have to rename the file. */
1315 char newpath[_POSIX_PATH_MAX];
1316 char partpath[_POSIX_PATH_MAX];
1317 char fullpath[_POSIX_PATH_MAX];
1318 char oldpath[_POSIX_PATH_MAX];
1322 if ((p = strrchr (h->path, '/')) == NULL) {
1325 "maildir_sync_message: %s: unable to find subdir!\n",
1330 strfcpy (newpath, p, sizeof (newpath));
1332 /* kill the previous flags */
1333 if ((p = strchr (newpath, ':')) != NULL)
1336 maildir_flags (suffix, sizeof (suffix), h);
1338 snprintf (partpath, sizeof (partpath), "%s/%s%s",
1339 (h->read || h->old) ? "cur" : "new", newpath, suffix);
1340 snprintf (fullpath, sizeof (fullpath), "%s/%s", ctx->path, partpath);
1341 snprintf (oldpath, sizeof (oldpath), "%s/%s", ctx->path, h->path);
1343 if (mutt_strcmp (fullpath, oldpath) == 0) {
1344 /* message hasn't really changed */
1348 /* record that the message is possibly marked as trashed on disk */
1349 h->trash = h->deleted;
1351 if (rename (oldpath, fullpath) != 0) {
1352 mutt_perror ("rename");
1355 mutt_str_replace (&h->path, partpath);
1360 int mh_sync_mailbox (CONTEXT * ctx, int *index_hint)
1362 char path[_POSIX_PATH_MAX], tmp[_POSIX_PATH_MAX];
1367 #endif /* USE_HCACHE */
1369 if (ctx->magic == M_MH)
1370 i = mh_check_mailbox (ctx, index_hint);
1372 i = maildir_check_mailbox (ctx, index_hint);
1378 if (ctx->magic == M_MAILDIR)
1379 hc = mutt_hcache_open (HeaderCache, ctx->path);
1380 #endif /* USE_HCACHE */
1382 for (i = 0; i < ctx->msgcount; i++) {
1383 if (ctx->hdrs[i]->deleted
1384 && (ctx->magic != M_MAILDIR || !option (OPTMAILDIRTRASH))) {
1385 snprintf (path, sizeof (path), "%s/%s", ctx->path, ctx->hdrs[i]->path);
1386 if (ctx->magic == M_MAILDIR
1387 || (option (OPTMHPURGE) && ctx->magic == M_MH)) {
1389 if (ctx->magic == M_MAILDIR)
1390 mutt_hcache_delete (hc, ctx->hdrs[i]->path + 3,
1391 &maildir_hcache_keylen);
1392 #endif /* USE_HCACHE */
1395 else if (ctx->magic == M_MH) {
1396 /* MH just moves files out of the way when you delete them */
1397 if (*ctx->hdrs[i]->path != ',') {
1398 snprintf (tmp, sizeof (tmp), "%s/,%s", ctx->path,
1399 ctx->hdrs[i]->path);
1406 else if (ctx->hdrs[i]->changed || ctx->hdrs[i]->attach_del ||
1407 (ctx->magic == M_MAILDIR
1408 && (option (OPTMAILDIRTRASH) || ctx->hdrs[i]->trash)
1409 && (ctx->hdrs[i]->deleted != ctx->hdrs[i]->trash))) {
1410 if (ctx->magic == M_MAILDIR) {
1411 if (maildir_sync_message (ctx, i) == -1)
1415 if (mh_sync_message (ctx, i) == -1)
1422 if (ctx->magic == M_MAILDIR)
1423 mutt_hcache_close (hc);
1424 #endif /* USE_HCACHE */
1426 if (ctx->magic == M_MH)
1427 mh_update_sequences (ctx);
1429 /* XXX race condition? */
1431 maildir_update_mtime (ctx);
1433 /* adjust indices */
1436 for (i = 0, j = 0; i < ctx->msgcount; i++) {
1437 if (!ctx->hdrs[i]->deleted
1438 || (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))
1439 ctx->hdrs[i]->index = j++;
1447 if (ctx->magic == M_MAILDIR)
1448 mutt_hcache_close (hc);
1449 #endif /* USE_HCACHE */
1453 static char *maildir_canon_filename (char *dest, const char *src, size_t l)
1457 if ((t = strrchr (src, '/')))
1460 strfcpy (dest, src, l);
1461 if ((u = strrchr (dest, ':')))
1467 static void maildir_update_tables (CONTEXT * ctx, int *index_hint)
1473 if (Sort != SORT_ORDER) {
1476 mutt_sort_headers (ctx, 1);
1480 old_count = ctx->msgcount;
1481 for (i = 0, j = 0; i < old_count; i++) {
1482 if (ctx->hdrs[i]->active && index_hint && *index_hint == i)
1485 if (ctx->hdrs[i]->active)
1486 ctx->hdrs[i]->index = j++;
1489 mx_update_tables (ctx, 0);
1490 mutt_clear_threads (ctx);
1493 static void maildir_update_flags (CONTEXT * ctx, HEADER * o, HEADER * n)
1495 /* save the global state here so we can reset it at the
1496 * end of list block if required.
1498 int context_changed = ctx->changed;
1500 /* user didn't modify this message. alter the flags to
1501 * match the current state on disk. This may not actually
1502 * do anything, but we can't tell right now. mutt_set_flag()
1503 * will just ignore the call if the status bits are
1504 * already properly set.
1506 mutt_set_flag (ctx, o, M_FLAG, n->flagged);
1507 mutt_set_flag (ctx, o, M_REPLIED, n->replied);
1508 mutt_set_flag (ctx, o, M_READ, n->read);
1509 mutt_set_flag (ctx, o, M_OLD, n->old);
1511 /* mutt_set_flag() will set this, but we don't need to
1512 * sync the changes we made because we just updated the
1513 * context to match the current on-disk state of the
1518 /* if the mailbox was not modified before we made these
1519 * changes, unset the changed flag since nothing needs to
1522 if (!context_changed)
1527 /* This function handles arrival of new mail and reopening of
1528 * maildir folders. The basic idea here is we check to see if either
1529 * the new or cur subdirectories have changed, and if so, we scan them
1530 * for the list of files. We check for newly added messages, and
1531 * then merge the flags messages we already knew about. We don't treat
1532 * either subdirectory differently, as mail could be copied directly into
1533 * the cur directory from another agent.
1535 int maildir_check_mailbox (CONTEXT * ctx, int *index_hint)
1537 struct stat st_new; /* status of the "new" subdirectory */
1538 struct stat st_cur; /* status of the "cur" subdirectory */
1539 char buf[_POSIX_PATH_MAX];
1540 int changed = 0; /* bitmask representing which subdirectories
1541 have changed. 0x1 = new, 0x2 = cur */
1542 int occult = 0; /* messages were removed from the mailbox */
1543 int have_new = 0; /* messages were added to the mailbox */
1544 struct maildir *md; /* list of messages in the mailbox */
1545 struct maildir **last, *p;
1547 HASH *fnames; /* hash table for quickly looking up the base filename
1548 for a maildir message */
1550 /* XXX seems like this check belongs in mx_check_mailbox()
1553 if (!option (OPTCHECKNEW))
1556 snprintf (buf, sizeof (buf), "%s/new", ctx->path);
1557 if (stat (buf, &st_new) == -1)
1560 snprintf (buf, sizeof (buf), "%s/cur", ctx->path);
1561 if (stat (buf, &st_cur) == -1)
1564 /* determine which subdirectories need to be scanned */
1565 if (st_new.st_mtime > ctx->mtime)
1567 if (st_cur.st_mtime > ctx->mtime_cur)
1571 return 0; /* nothing to do */
1573 /* update the modification times on the mailbox */
1574 ctx->mtime_cur = st_cur.st_mtime;
1575 ctx->mtime = st_new.st_mtime;
1577 /* do a fast scan of just the filenames in
1578 * the subdirectories that have changed.
1583 maildir_parse_dir (ctx, &last, "new", NULL);
1585 maildir_parse_dir (ctx, &last, "cur", NULL);
1587 /* we create a hash table keyed off the canonical (sans flags) filename
1588 * of each message we scanned. This is used in the loop over the
1589 * existing messages below to do some correlation.
1591 fnames = hash_create (1031);
1593 for (p = md; p; p = p->next) {
1594 maildir_canon_filename (buf, p->h->path, sizeof (buf));
1595 p->canon_fname = safe_strdup (buf);
1596 hash_insert (fnames, p->canon_fname, p, 0);
1599 /* check for modifications and adjust flags */
1600 for (i = 0; i < ctx->msgcount; i++) {
1601 ctx->hdrs[i]->active = 0;
1602 maildir_canon_filename (buf, ctx->hdrs[i]->path, sizeof (buf));
1603 p = hash_find (fnames, buf);
1605 /* message already exists, merge flags */
1606 ctx->hdrs[i]->active = 1;
1608 /* check to see if the message has moved to a different
1609 * subdirectory. If so, update the associated filename.
1611 if (mutt_strcmp (ctx->hdrs[i]->path, p->h->path))
1612 mutt_str_replace (&ctx->hdrs[i]->path, p->h->path);
1614 /* if the user hasn't modified the flags on this message, update
1615 * the flags we just detected.
1617 if (!ctx->hdrs[i]->changed)
1618 maildir_update_flags (ctx, ctx->hdrs[i], p->h);
1620 if (ctx->hdrs[i]->deleted == ctx->hdrs[i]->trash)
1621 ctx->hdrs[i]->deleted = p->h->deleted;
1622 ctx->hdrs[i]->trash = p->h->trash;
1624 /* this is a duplicate of an existing header, so remove it */
1625 mutt_free_header (&p->h);
1627 /* This message was not in the list of messages we just scanned.
1628 * Check to see if we have enough information to know if the
1629 * message has disappeared out from underneath us.
1631 else if (((changed & 1) && (!strncmp (ctx->hdrs[i]->path, "new/", 4))) ||
1632 ((changed & 2) && (!strncmp (ctx->hdrs[i]->path, "cur/", 4)))) {
1633 /* This message disappeared, so we need to simulate a "reopen"
1634 * event. We know it disappeared because we just scanned the
1635 * subdirectory it used to reside in.
1640 /* This message resides in a subdirectory which was not
1641 * modified, so we assume that it is still present and
1644 ctx->hdrs[i]->active = 1;
1648 /* destroy the file name hash */
1649 hash_destroy (&fnames, NULL);
1651 /* If we didn't just get new mail, update the tables. */
1653 maildir_update_tables (ctx, index_hint);
1655 /* do any delayed parsing we need to do. */
1656 maildir_delayed_parsing (ctx, md);
1658 /* Incorporate new messages */
1659 have_new = maildir_move_to_context (ctx, &md);
1661 return occult ? M_REOPENED : (have_new ? M_NEW_MAIL : 0);
1665 * This function handles arrival of new mail and reopening of
1666 * mh/maildir folders. Things are getting rather complex because we
1667 * don't have a well-defined "mailbox order", so the tricks from
1668 * mbox.c and mx.c won't work here.
1670 * Don't change this code unless you _really_ understand what
1675 int mh_check_mailbox (CONTEXT * ctx, int *index_hint)
1677 char buf[_POSIX_PATH_MAX];
1678 struct stat st, st_cur;
1679 short modified = 0, have_new = 0, occult = 0;
1680 struct maildir *md, *p;
1681 struct maildir **last = NULL;
1682 struct mh_sequences mhs;
1686 if (!option (OPTCHECKNEW))
1689 strfcpy (buf, ctx->path, sizeof (buf));
1690 if (stat (buf, &st) == -1)
1693 /* create .mh_sequences when there isn't one. */
1694 snprintf (buf, sizeof (buf), "%s/.mh_sequences", ctx->path);
1695 if ((i = stat (buf, &st_cur) == -1) && errno == ENOENT) {
1699 if (mh_mkstemp (ctx, &fp, &tmp) == 0) {
1701 if (safe_rename (tmp, buf) == -1)
1707 if (i == -1 && stat (buf, &st_cur) == -1)
1710 if (st.st_mtime > ctx->mtime || st_cur.st_mtime > ctx->mtime_cur)
1716 ctx->mtime_cur = st_cur.st_mtime;
1717 ctx->mtime = st.st_mtime;
1719 memset (&mhs, 0, sizeof (mhs));
1723 maildir_parse_dir (ctx, &last, NULL, NULL);
1724 mh_read_sequences (&mhs, ctx->path);
1725 mh_update_maildir (md, &mhs);
1726 mhs_free_sequences (&mhs);
1728 /* check for modifications and adjust flags */
1729 fnames = hash_create (1031);
1731 for (p = md; p; p = p->next)
1732 hash_insert (fnames, p->h->path, p, 0);
1734 for (i = 0; i < ctx->msgcount; i++) {
1735 ctx->hdrs[i]->active = 0;
1737 if ((p = hash_find (fnames, ctx->hdrs[i]->path)) && p->h &&
1738 (mbox_strict_cmp_headers (ctx->hdrs[i], p->h))) {
1739 ctx->hdrs[i]->active = 1;
1740 /* found the right message */
1741 if (!ctx->hdrs[i]->changed)
1742 maildir_update_flags (ctx, ctx->hdrs[i], p->h);
1744 mutt_free_header (&p->h);
1746 else /* message has disappeared */
1750 /* destroy the file name hash */
1752 hash_destroy (&fnames, NULL);
1754 /* If we didn't just get new mail, update the tables. */
1756 maildir_update_tables (ctx, index_hint);
1758 /* Incorporate new messages */
1759 have_new = maildir_move_to_context (ctx, &md);
1761 return occult ? M_REOPENED : (have_new ? M_NEW_MAIL : 0);
1768 * These functions try to find a message in a maildir folder when it
1769 * has moved under our feet. Note that this code is rather expensive, but
1770 * then again, it's called rarely.
1773 FILE *_maildir_open_find_message (const char *folder, const char *unique,
1774 const char *subfolder)
1776 char dir[_POSIX_PATH_MAX];
1777 char tunique[_POSIX_PATH_MAX];
1778 char fname[_POSIX_PATH_MAX];
1786 snprintf (dir, sizeof (dir), "%s/%s", folder, subfolder);
1788 if ((dp = opendir (dir)) == NULL) {
1793 while ((de = readdir (dp))) {
1794 maildir_canon_filename (tunique, de->d_name, sizeof (tunique));
1796 if (!mutt_strcmp (tunique, unique)) {
1797 snprintf (fname, sizeof (fname), "%s/%s/%s", folder, subfolder,
1799 fp = fopen (fname, "r"); /* __FOPEN_CHECKED__ */
1811 FILE *maildir_open_find_message (const char *folder, const char *msg)
1813 char unique[_POSIX_PATH_MAX];
1816 static unsigned int new_hits = 0, cur_hits = 0; /* simple dynamic optimization */
1818 maildir_canon_filename (unique, msg, sizeof (unique));
1821 _maildir_open_find_message (folder, unique,
1822 new_hits > cur_hits ? "new" : "cur"))
1823 || errno != ENOENT) {
1824 if (new_hits < UINT_MAX && cur_hits < UINT_MAX) {
1825 new_hits += (new_hits > cur_hits ? 1 : 0);
1826 cur_hits += (new_hits > cur_hits ? 0 : 1);
1832 _maildir_open_find_message (folder, unique,
1833 new_hits > cur_hits ? "cur" : "new"))
1834 || errno != ENOENT) {
1835 if (new_hits < UINT_MAX && cur_hits < UINT_MAX) {
1836 new_hits += (new_hits > cur_hits ? 0 : 1);
1837 cur_hits += (new_hits > cur_hits ? 1 : 0);
1849 * 1 if there are no messages in the mailbox
1850 * 0 if there are messages in the mailbox
1853 int maildir_check_empty (const char *path)
1857 int r = 1; /* assume empty until we find a message */
1858 char realpath[_POSIX_PATH_MAX];
1861 /* Strategy here is to look for any file not beginning with a period */
1864 /* we do "cur" on the first iteration since its more likely that we'll
1865 * find old messages without having to scan both subdirs
1867 snprintf (realpath, sizeof (realpath), "%s/%s", path,
1868 iter == 0 ? "cur" : "new");
1869 if ((dp = opendir (realpath)) == NULL)
1871 while ((de = readdir (dp))) {
1872 if (*de->d_name != '.') {
1879 } while (r && iter < 2);
1886 * 1 if there are no messages in the mailbox
1887 * 0 if there are messages in the mailbox
1890 int mh_check_empty (const char *path)
1894 int r = 1; /* assume empty until we find a message */
1896 if ((dp = opendir (path)) == NULL)
1898 while ((de = readdir (dp))) {
1899 if (mh_valid_message (de->d_name)) {