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
33 #include <sys/types.h>
50 unsigned header_parsed:1;
61 /* mh_sequences support */
63 #define MH_SEQ_UNSEEN (1 << 0)
64 #define MH_SEQ_REPLIED (1 << 1)
65 #define MH_SEQ_FLAGGED (1 << 2)
67 static void mhs_alloc (struct mh_sequences *mhs, int i)
72 if (i > mhs->max || !mhs->flags)
75 safe_realloc (&mhs->flags, sizeof (mhs->flags[0]) * (newmax + 1));
76 for (j = mhs->max + 1; j <= newmax; j++)
83 static void mhs_free_sequences (struct mh_sequences *mhs)
88 static short mhs_check (struct mh_sequences *mhs, int i)
90 if (!mhs->flags || i > mhs->max)
96 static short mhs_set (struct mh_sequences *mhs, int i, short f)
100 return mhs->flags[i];
107 static short mhs_unset (struct mh_sequences *mhs, int i, short f)
111 return mhs->flags[i];
116 static void mh_read_token (char *t, int *first, int *last)
119 if ((p = strchr (t, '-')))
126 *first = *last = atoi (t);
129 static void mh_read_sequences (struct mh_sequences *mhs, const char *path)
140 char pathname[_POSIX_PATH_MAX];
141 snprintf (pathname, sizeof (pathname), "%s/.mh_sequences", path);
143 if (!(fp = fopen (pathname, "r")))
146 while ((buff = mutt_read_line (buff, &sz, fp, &line)))
148 if (!(t = strtok (buff, " \t:")))
151 if (!mutt_strcmp (t, MhUnseen))
153 else if (!mutt_strcmp (t, MhFlagged))
155 else if (!mutt_strcmp (t, MhReplied))
157 else /* unknown sequence */
160 while ((t = strtok (NULL, " \t:")))
162 mh_read_token (t, &first, &last);
163 for (; first <= last; first++)
164 mhs_set (mhs, first, f);
172 int mh_buffy (const char *path)
175 struct mh_sequences mhs;
176 memset (&mhs, 0, sizeof (mhs));
178 mh_read_sequences (&mhs, path);
179 for (i = 0; !r && i <= mhs.max; i++)
180 if (mhs_check (&mhs, i) & MH_SEQ_UNSEEN)
182 mhs_free_sequences (&mhs);
186 static int mh_mkstemp (CONTEXT * dest, FILE ** fp, char **tgt)
189 char path[_POSIX_PATH_MAX];
193 snprintf (path, _POSIX_PATH_MAX, "%s/.mutt-%s-%d-%d",
194 dest->path, NONULL (Hostname), (int) getpid (), Counter++);
196 if ((fd = open (path, O_WRONLY | O_EXCL | O_CREAT, 0666)) == -1)
206 *tgt = safe_strdup (path);
211 if ((*fp = fdopen (fd, "w")) == NULL)
222 static void mhs_write_one_sequence (FILE * fp, struct mh_sequences *mhs,
223 short f, const char *tag)
227 fprintf (fp, "%s:", tag);
232 for (i = 0; i <= mhs->max; i++)
234 if ((mhs_check (mhs, i) & f))
244 fprintf (fp, " %d", first);
246 fprintf (fp, " %d-%d", first, last);
256 fprintf (fp, " %d", first);
258 fprintf (fp, " %d-%d", first, last);
264 /* XXX - we don't currently remove deleted messages from sequences we don't know. Should we? */
266 void mh_update_sequences (CONTEXT * ctx)
270 char sequences[_POSIX_PATH_MAX];
282 char seq_unseen[STRING];
283 char seq_replied[STRING];
284 char seq_flagged[STRING];
287 struct mh_sequences mhs;
288 memset (&mhs, 0, sizeof (mhs));
290 snprintf (seq_unseen, sizeof (seq_unseen), "%s:", NONULL (MhUnseen));
291 snprintf (seq_replied, sizeof (seq_replied), "%s:", NONULL (MhReplied));
292 snprintf (seq_flagged, sizeof (seq_flagged), "%s:", NONULL (MhFlagged));
294 if (mh_mkstemp (ctx, &nfp, &tmpfname) != 0)
300 snprintf (sequences, sizeof (sequences), "%s/.mh_sequences", ctx->path);
303 /* first, copy unknown sequences */
304 if ((ofp = fopen (sequences, "r")))
306 while ((buff = mutt_read_line (buff, &s, ofp, &l)))
308 if (!mutt_strncmp (buff, seq_unseen, mutt_strlen (seq_unseen)))
310 if (!mutt_strncmp (buff, seq_flagged, mutt_strlen (seq_flagged)))
312 if (!mutt_strncmp (buff, seq_replied, mutt_strlen (seq_replied)))
315 fprintf (nfp, "%s\n", buff);
320 /* now, update our unseen, flagged, and replied sequences */
321 for (l = 0; l < ctx->msgcount; l++)
323 if (ctx->hdrs[l]->deleted)
326 if ((p = strrchr (ctx->hdrs[l]->path, '/')))
329 p = ctx->hdrs[l]->path;
333 if (!ctx->hdrs[l]->read)
335 mhs_set (&mhs, i, MH_SEQ_UNSEEN);
338 if (ctx->hdrs[l]->flagged)
340 mhs_set (&mhs, i, MH_SEQ_FLAGGED);
343 if (ctx->hdrs[l]->replied)
345 mhs_set (&mhs, i, MH_SEQ_REPLIED);
350 /* write out the new sequences */
352 mhs_write_one_sequence (nfp, &mhs, MH_SEQ_UNSEEN, NONULL (MhUnseen));
354 mhs_write_one_sequence (nfp, &mhs, MH_SEQ_FLAGGED, NONULL (MhFlagged));
356 mhs_write_one_sequence (nfp, &mhs, MH_SEQ_REPLIED, NONULL (MhReplied));
358 mhs_free_sequences (&mhs);
361 /* try to commit the changes - no guarantee here */
365 if (safe_rename (tmpfname, sequences) != 0)
367 /* report an error? */
374 static void mh_sequences_add_one (CONTEXT * ctx, int n, short unseen,
375 short flagged, short replied)
377 short unseen_done = 0;
378 short flagged_done = 0;
379 short replied_done = 0;
381 FILE *ofp = NULL, *nfp = NULL;
384 char sequences[_POSIX_PATH_MAX];
386 char seq_unseen[STRING];
387 char seq_replied[STRING];
388 char seq_flagged[STRING];
394 if (mh_mkstemp (ctx, &nfp, &tmpfname) == -1)
397 snprintf (seq_unseen, sizeof (seq_unseen), "%s:", NONULL (MhUnseen));
398 snprintf (seq_replied, sizeof (seq_replied), "%s:", NONULL (MhReplied));
399 snprintf (seq_flagged, sizeof (seq_flagged), "%s:", NONULL (MhFlagged));
401 snprintf (sequences, sizeof (sequences), "%s/.mh_sequences", ctx->path);
402 if ((ofp = fopen (sequences, "r")))
404 while ((buff = mutt_read_line (buff, &sz, ofp, &line)))
406 if (unseen && !strncmp (buff, seq_unseen, mutt_strlen (seq_unseen)))
408 fprintf (nfp, "%s %d\n", buff, n);
412 && !strncmp (buff, seq_flagged, mutt_strlen (seq_flagged)))
414 fprintf (nfp, "%s %d\n", buff, n);
418 && !strncmp (buff, seq_replied, mutt_strlen (seq_replied)))
420 fprintf (nfp, "%s %d\n", buff, n);
424 fprintf (nfp, "%s\n", buff);
430 if (!unseen_done && unseen)
431 fprintf (nfp, "%s: %d\n", NONULL (MhUnseen), n);
432 if (!flagged_done && flagged)
433 fprintf (nfp, "%s: %d\n", NONULL (MhFlagged), n);
434 if (!replied_done && replied)
435 fprintf (nfp, "%s: %d\n", NONULL (MhReplied), n);
440 if (safe_rename (tmpfname, sequences) != 0)
446 static void mh_update_maildir (struct maildir *md, struct mh_sequences *mhs)
452 for (; md; md = md->next)
454 if ((p = strrchr (md->h->path, '/')))
460 f = mhs_check (mhs, i);
462 md->h->read = (f & MH_SEQ_UNSEEN) ? 0 : 1;
463 md->h->flagged = (f & MH_SEQ_FLAGGED) ? 1 : 0;
464 md->h->replied = (f & MH_SEQ_REPLIED) ? 1 : 0;
468 /* maildir support */
470 static void maildir_free_entry (struct maildir **md)
475 FREE (&(*md)->canon_fname);
477 mutt_free_header (&(*md)->h);
482 static void maildir_free_maildir (struct maildir **md)
484 struct maildir *p, *q;
489 for (p = *md; p; p = q)
492 maildir_free_entry (&p);
496 static void maildir_parse_flags (HEADER * h, const char *path)
504 if ((p = strrchr (path, ':')) != NULL && mutt_strncmp (p + 1, "2,", 2) == 0)
508 mutt_str_replace (&h->maildir_flags, p);
509 q = h->maildir_flags;
525 case 'R': /* replied */
530 case 'T': /* trashed */
543 if (q == h->maildir_flags)
544 FREE (&h->maildir_flags);
549 static void maildir_update_mtime (CONTEXT * ctx)
551 char buf[_POSIX_PATH_MAX];
554 if (ctx->magic == M_MAILDIR)
556 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, "cur");
557 if (stat (buf, &st) == 0)
558 ctx->mtime_cur = st.st_mtime;
559 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, "new");
563 snprintf (buf, sizeof (buf), "%s/.mh_sequences", ctx->path);
564 if (stat (buf, &st) == 0)
565 ctx->mtime_cur = st.st_mtime;
567 strfcpy (buf, ctx->path, sizeof (buf));
570 if (stat (buf, &st) == 0)
571 ctx->mtime = st.st_mtime;
575 * Actually parse a maildir message. This may also be used to fill
576 * out a fake header structure generated by lazy maildir parsing.
578 static HEADER *maildir_parse_message (int magic, const char *fname,
579 int is_old, HEADER * _h)
585 if ((f = fopen (fname, "r")) != NULL)
588 h = mutt_new_header ();
589 h->env = mutt_read_rfc822_header (f, h, 0, 0);
591 fstat (fileno (f), &st);
595 h->received = h->date_sent;
597 if (h->content->length <= 0)
598 h->content->length = st.st_size - h->content->offset;
602 if (magic == M_MAILDIR)
605 * maildir stores its flags in the filename, so ignore the
606 * flags in the header of the message
610 maildir_parse_flags (h, fname);
618 * Note that this routine will _not_ modify the context given by
621 * It's used in the first parsing pass on maildir and MH folders.
622 * In the MH case, this means full parsing of the folder. In the
623 * maildir case, it means that we only look at flags, and create a
624 * fake HEADER structure, which may later be filled in by
625 * maildir_parse_message(), when called from
626 * maildir_delayed_parsing().
630 static int maildir_parse_entry (CONTEXT * ctx, struct maildir ***last,
631 const char *subdir, const char *fname,
632 int *count, int is_old, ino_t inode)
634 struct maildir *entry;
636 char buf[_POSIX_PATH_MAX];
639 snprintf (buf, sizeof (buf), "%s/%s/%s", ctx->path, subdir, fname);
641 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, fname);
643 if (ctx->magic == M_MH)
644 h = maildir_parse_message (ctx->magic, buf, is_old, NULL);
647 h = mutt_new_header ();
649 maildir_parse_flags (h, buf);
657 if (!ctx->quiet && ReadInc && ((*count % ReadInc) == 0 || *count == 1))
658 mutt_message (_("Reading %s... %d"), ctx->path, *count);
663 snprintf (buf, sizeof (buf), "%s/%s", subdir, fname);
664 h->path = safe_strdup (buf);
667 h->path = safe_strdup (fname);
669 entry = safe_calloc (sizeof (struct maildir), 1);
671 entry->header_parsed = (ctx->magic == M_MH);
672 entry->inode = inode;
674 *last = &entry->next;
684 /* Ignore the garbage files. A valid MH message consists of only
685 * digits. Deleted message get moved to a filename with a comma before
689 int mh_valid_message (const char *s)
693 if (!isdigit ((unsigned char) *s))
699 static int maildir_parse_dir (CONTEXT * ctx, struct maildir ***last,
700 const char *subdir, int *count)
704 char buf[_POSIX_PATH_MAX];
709 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, subdir);
710 is_old = (mutt_strcmp ("cur", subdir) == 0);
713 strfcpy (buf, ctx->path, sizeof (buf));
715 if ((dirp = opendir (buf)) == NULL)
718 while ((de = readdir (dirp)) != NULL)
721 if ((ctx->magic == M_MH && !mh_valid_message (de->d_name))
722 || (ctx->magic == M_MAILDIR && *de->d_name == '.'))
725 /* FOO - really ignore the return value? */
728 (debugfile, "%s:%d: parsing %s\n", __FILE__, __LINE__,
730 maildir_parse_entry (ctx, last, subdir, de->d_name, count, is_old,
738 static int maildir_add_to_context (CONTEXT * ctx, struct maildir *md)
740 int oldmsgcount = ctx->msgcount;
745 dprint (2, (debugfile, "%s:%d maildir_add_to_context(): Considering %s\n",
746 __FILE__, __LINE__, NONULL (md->canon_fname)));
752 "%s:%d Adding header structure. Flags: %s%s%s%s%s\n", __FILE__,
753 __LINE__, md->h->flagged ? "f" : "", md->h->deleted ? "D" : "",
754 md->h->replied ? "r" : "", md->h->old ? "O" : "",
755 md->h->read ? "R" : ""));
756 if (ctx->msgcount == ctx->hdrmax)
757 mx_alloc_memory (ctx);
759 ctx->hdrs[ctx->msgcount] = md->h;
760 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
762 md->h->content->length + md->h->content->offset -
763 md->h->content->hdr_offset;
771 if (ctx->msgcount > oldmsgcount)
773 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
779 static int maildir_move_to_context (CONTEXT * ctx, struct maildir **md)
782 r = maildir_add_to_context (ctx, *md);
783 maildir_free_maildir (md);
788 * Merge two maildir lists according to the inode numbers.
790 static struct maildir* maildir_merge_inode (struct maildir *left,
791 struct maildir *right)
793 struct maildir* head;
794 struct maildir* tail;
798 if (left->inode < right->inode)
819 while (left && right)
821 if (left->inode < right->inode)
847 * Sort maildir list according to inode.
849 static struct maildir* maildir_sort_inode(struct maildir* list)
851 struct maildir* left = list;
852 struct maildir* right = list;
854 if (!list || !list->next)
860 while (list && list->next)
863 list = list->next->next;
870 left = maildir_sort_inode(left);
871 right = maildir_sort_inode(right);
872 return maildir_merge_inode(left, right);
876 static size_t maildir_hcache_keylen (const char *fn)
878 const char * p = strchr (fn, ':');
879 return p ? (size_t) (p - fn) : mutt_strlen(fn);
884 * This function does the second parsing pass for a maildir-style
887 void maildir_delayed_parsing (CONTEXT * ctx, struct maildir *md)
890 char fn[_POSIX_PATH_MAX];
897 struct timeval *when = NULL;
898 struct stat lastchanged;
901 hc = mutt_hcache_open (HeaderCache, ctx->path);
904 for (p = md, count = 0; p; p = p->next, count++)
906 if (! (p && p->h && !p->header_parsed))
910 data = mutt_hcache_fetch (hc, p->h->path + 3, &maildir_hcache_keylen);
911 when = (struct timeval *) data;
914 snprintf (fn, sizeof (fn), "%s/%s", ctx->path, p->h->path);
917 if (option(OPTHCACHEVERIFY)) {
918 ret = stat(fn, &lastchanged);
920 lastchanged.st_mtime = 0;
924 if (data != NULL && !ret && lastchanged.st_mtime <= when->tv_sec)
926 p->h = mutt_hcache_restore ((unsigned char *)data, &p->h);
927 maildir_parse_flags (p->h, fn);
930 if (maildir_parse_message (ctx->magic, fn, p->h->old, p->h))
932 p->header_parsed = 1;
933 maildir_parse_flags (p->h, fn);
935 mutt_hcache_store (hc, p->h->path + 3, p->h, 0, &maildir_hcache_keylen);
938 mutt_free_header (&p->h);
944 mutt_hcache_close (hc);
948 /* Read a MH/maildir style mailbox.
951 * ctx [IN/OUT] context for this mailbox
952 * subdir [IN] NULL for MH mailboxes, otherwise the subdir of the
953 * maildir mailbox to read from
955 int mh_read_dir (CONTEXT * ctx, const char *subdir)
958 struct mh_sequences mhs;
959 struct maildir **last;
963 memset (&mhs, 0, sizeof (mhs));
965 maildir_update_mtime (ctx);
970 if (maildir_parse_dir (ctx, &last, subdir, &count) == -1)
973 if (ctx->magic == M_MH)
975 mh_read_sequences (&mhs, ctx->path);
976 mh_update_maildir (md, &mhs);
977 mhs_free_sequences (&mhs);
980 md = maildir_sort_inode(md);
982 if (ctx->magic == M_MAILDIR)
983 maildir_delayed_parsing (ctx, md);
985 maildir_move_to_context (ctx, &md);
989 /* read a maildir style mailbox */
990 int maildir_read_dir (CONTEXT * ctx)
992 /* maildir looks sort of like MH, except that there are two subdirectories
993 * of the main folder path from which to read messages
995 if (mh_read_dir (ctx, "new") == -1 || mh_read_dir (ctx, "cur") == -1)
1002 * Open a new (temporary) message in an MH folder.
1005 int mh_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr)
1007 return mh_mkstemp (dest, &msg->fp, &msg->path);
1010 int ch_compar (const void *a, const void *b)
1012 return (int)( *((const char *) a) - *((const char *) b));
1015 static void maildir_flags (char *dest, size_t destlen, HEADER * hdr)
1020 * The maildir specification requires that all files in the cur
1021 * subdirectory have the :unique string appeneded, regardless of whether
1022 * or not there are any flags. If .old is set, we know that this message
1023 * will end up in the cur directory, so we include it in the following
1024 * test even though there is no associated flag.
1027 if (hdr && (hdr->flagged || hdr->replied || hdr->read || hdr->deleted || hdr->old || hdr->maildir_flags))
1029 char tmp[LONG_STRING];
1030 snprintf (tmp, sizeof (tmp),
1032 hdr->flagged ? "F" : "",
1033 hdr->replied ? "R" : "",
1034 hdr->read ? "S" : "", hdr->deleted ? "T" : "",
1035 NONULL(hdr->maildir_flags));
1036 if (hdr->maildir_flags)
1037 qsort (tmp, strlen (tmp), 1, ch_compar);
1038 snprintf (dest, destlen, ":2,%s", tmp);
1044 * Open a new (temporary) message in a maildir folder.
1046 * Note that this uses _almost_ the maildir file name format, but
1047 * with a {cur,new} prefix.
1051 int maildir_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr)
1054 char path[_POSIX_PATH_MAX];
1060 short deleted = hdr->deleted;
1063 maildir_flags (suffix, sizeof (suffix), hdr);
1065 hdr->deleted = deleted;
1070 if (hdr && (hdr->read || hdr->old))
1071 strfcpy (subdir, "cur", sizeof (subdir));
1073 strfcpy (subdir, "new", sizeof (subdir));
1077 snprintf (path, _POSIX_PATH_MAX, "%s/tmp/%s.%ld.%d_%d.%s%s",
1078 dest->path, subdir, time (NULL), getpid (), Counter++,
1079 NONULL (Hostname), suffix);
1081 dprint (2, (debugfile, "maildir_open_new_message (): Trying %s.\n",
1085 if ((fd = open (path, O_WRONLY | O_EXCL | O_CREAT, 0666)) == -1)
1087 if (errno != EEXIST)
1095 dprint (2, (debugfile, "maildir_open_new_message (): Success.\n"));
1096 msg->path = safe_strdup (path);
1101 if ((msg->fp = fdopen (fd, "w")) == NULL)
1115 * Commit a message to a maildir folder.
1117 * msg->path contains the file name of a file in tmp/. We take the
1118 * flags from this file's name.
1120 * ctx is the mail folder we commit to.
1122 * hdr is a header structure to which we write the message's new
1123 * file name. This is used in the mh and maildir folder synch
1124 * routines. When this routine is invoked from mx_commit_message,
1127 * msg->path looks like this:
1129 * tmp/{cur,new}.mutt-HOSTNAME-PID-COUNTER:flags
1131 * See also maildir_open_new_message().
1135 int maildir_commit_message (CONTEXT * ctx, MESSAGE * msg, HEADER * hdr)
1139 char path[_POSIX_PATH_MAX];
1140 char full[_POSIX_PATH_MAX];
1143 if (safe_fclose (&msg->fp) != 0)
1146 /* extract the subdir */
1147 s = strrchr (msg->path, '/') + 1;
1148 strfcpy (subdir, s, 4);
1150 /* extract the flags */
1151 if ((s = strchr (s, ':')))
1152 strfcpy (suffix, s, sizeof (suffix));
1156 /* construct a new file name. */
1159 snprintf (path, _POSIX_PATH_MAX, "%s/%ld.%d_%d.%s%s", subdir,
1160 time (NULL), getpid (), Counter++, NONULL (Hostname), suffix);
1161 snprintf (full, _POSIX_PATH_MAX, "%s/%s", ctx->path, path);
1163 dprint (2, (debugfile, "maildir_commit_message (): renaming %s to %s.\n",
1166 if (safe_rename (msg->path, full) == 0)
1169 mutt_str_replace (&hdr->path, path);
1173 * Adjust the mtime on the file to match the time at which this
1174 * message was received. Currently this is only set when copying
1175 * messages between mailboxes, so we test to ensure that it is
1182 ut.actime = msg->received;
1183 ut.modtime = msg->received;
1184 if (utime (full, &ut))
1186 mutt_perror (_("maildir_commit_message(): unable to set time on file"));
1193 else if (errno != EEXIST)
1195 mutt_perror (ctx->path);
1202 * commit a message to an MH folder.
1207 static int _mh_commit_message (CONTEXT * ctx, MESSAGE * msg, HEADER * hdr,
1213 unsigned int n, hi = 0;
1214 char path[_POSIX_PATH_MAX];
1217 if (safe_fclose (&msg->fp) != 0)
1220 if ((dirp = opendir (ctx->path)) == NULL)
1222 mutt_perror (ctx->path);
1226 /* figure out what the next message number is */
1227 while ((de = readdir (dirp)) != NULL)
1235 if (!isdigit ((unsigned char) *cp))
1249 * Now try to rename the file to the proper name.
1251 * Note: We may have to try multiple times, until we find a free
1258 snprintf (tmp, sizeof (tmp), "%d", hi);
1259 snprintf (path, sizeof (path), "%s/%s", ctx->path, tmp);
1260 if (safe_rename (msg->path, path) == 0)
1263 mutt_str_replace (&hdr->path, tmp);
1267 else if (errno != EEXIST)
1269 mutt_perror (ctx->path);
1274 mh_sequences_add_one (ctx, hi, !msg->flags.read, msg->flags.flagged,
1275 msg->flags.replied);
1279 int mh_commit_message (CONTEXT * ctx, MESSAGE * msg, HEADER * hdr)
1281 return _mh_commit_message (ctx, msg, hdr, 1);
1285 /* Sync a message in an MH folder.
1287 * This code is also used for attachment deletion in maildir
1291 static int mh_rewrite_message (CONTEXT * ctx, int msgno)
1293 HEADER *h = ctx->hdrs[msgno];
1298 char oldpath[_POSIX_PATH_MAX];
1299 char newpath[_POSIX_PATH_MAX];
1300 char partpath[_POSIX_PATH_MAX];
1302 long old_body_offset = h->content->offset;
1303 long old_body_length = h->content->length;
1304 long old_hdr_lines = h->lines;
1306 if ((dest = mx_open_new_message (ctx, h, 0)) == NULL)
1309 if ((rc = mutt_copy_message (dest->fp, ctx, h,
1310 M_CM_UPDATE, CH_UPDATE | CH_UPDATE_LEN)) == 0)
1312 snprintf (oldpath, _POSIX_PATH_MAX, "%s/%s", ctx->path, h->path);
1313 strfcpy (partpath, h->path, _POSIX_PATH_MAX);
1315 if (ctx->magic == M_MAILDIR)
1316 rc = maildir_commit_message (ctx, dest, h);
1318 rc = _mh_commit_message (ctx, dest, h, 0);
1320 mx_close_message (&dest);
1329 * Try to move the new message to the old place.
1332 * This is important when we are just updating flags.
1334 * Note that there is a race condition against programs which
1335 * use the first free slot instead of the maximum message
1336 * number. Mutt does _not_ behave like this.
1338 * Anyway, if this fails, the message is in the folder, so
1339 * all what happens is that a concurrently runnung mutt will
1340 * lose flag modifications.
1343 if (ctx->magic == M_MH && rc == 0)
1345 snprintf (newpath, _POSIX_PATH_MAX, "%s/%s", ctx->path, h->path);
1346 if ((rc = safe_rename (newpath, oldpath)) == 0)
1347 mutt_str_replace (&h->path, partpath);
1351 mx_close_message (&dest);
1353 if (rc == -1 && restore)
1355 h->content->offset = old_body_offset;
1356 h->content->length = old_body_length;
1357 h->lines = old_hdr_lines;
1360 mutt_free_body (&h->content->parts);
1364 static int mh_sync_message (CONTEXT * ctx, int msgno)
1366 HEADER *h = ctx->hdrs[msgno];
1368 if (h->attach_del || h->refs_changed || h->irt_changed)
1369 if (mh_rewrite_message (ctx, msgno) != 0)
1375 static int maildir_sync_message (CONTEXT * ctx, int msgno)
1377 HEADER *h = ctx->hdrs[msgno];
1379 if (h->attach_del || h->refs_changed || h->irt_changed)
1381 /* when doing attachment deletion/rethreading, fall back to the MH case. */
1382 if (mh_rewrite_message (ctx, msgno) != 0)
1387 /* we just have to rename the file. */
1389 char newpath[_POSIX_PATH_MAX];
1390 char partpath[_POSIX_PATH_MAX];
1391 char fullpath[_POSIX_PATH_MAX];
1392 char oldpath[_POSIX_PATH_MAX];
1396 if ((p = strrchr (h->path, '/')) == NULL)
1400 "maildir_sync_message: %s: unable to find subdir!\n",
1405 strfcpy (newpath, p, sizeof (newpath));
1407 /* kill the previous flags */
1408 if ((p = strchr (newpath, ':')) != NULL)
1411 maildir_flags (suffix, sizeof (suffix), h);
1413 snprintf (partpath, sizeof (partpath), "%s/%s%s",
1414 (h->read || h->old) ? "cur" : "new", newpath, suffix);
1415 snprintf (fullpath, sizeof (fullpath), "%s/%s", ctx->path, partpath);
1416 snprintf (oldpath, sizeof (oldpath), "%s/%s", ctx->path, h->path);
1418 if (mutt_strcmp (fullpath, oldpath) == 0)
1420 /* message hasn't really changed */
1424 /* record that the message is possibly marked as trashed on disk */
1425 h->trash = h->deleted;
1427 if (rename (oldpath, fullpath) != 0)
1429 mutt_perror ("rename");
1432 mutt_str_replace (&h->path, partpath);
1437 int mh_sync_mailbox (CONTEXT * ctx, int *index_hint)
1439 char path[_POSIX_PATH_MAX], tmp[_POSIX_PATH_MAX];
1443 #endif /* USE_HCACHE */
1445 if (ctx->magic == M_MH)
1446 i = mh_check_mailbox (ctx, index_hint);
1448 i = maildir_check_mailbox (ctx, index_hint);
1454 if (ctx->magic == M_MAILDIR)
1455 hc = mutt_hcache_open(HeaderCache, ctx->path);
1456 #endif /* USE_HCACHE */
1458 for (i = 0; i < ctx->msgcount; i++)
1460 if (ctx->hdrs[i]->deleted
1461 && (ctx->magic != M_MAILDIR || !option (OPTMAILDIRTRASH)))
1463 snprintf (path, sizeof (path), "%s/%s", ctx->path, ctx->hdrs[i]->path);
1464 if (ctx->magic == M_MAILDIR
1465 || (option (OPTMHPURGE) && ctx->magic == M_MH))
1468 if (ctx->magic == M_MAILDIR)
1469 mutt_hcache_delete (hc, ctx->hdrs[i]->path + 3, &maildir_hcache_keylen);
1470 #endif /* USE_HCACHE */
1473 else if (ctx->magic == M_MH)
1475 /* MH just moves files out of the way when you delete them */
1476 if (*ctx->hdrs[i]->path != ',')
1478 snprintf (tmp, sizeof (tmp), "%s/,%s", ctx->path,
1479 ctx->hdrs[i]->path);
1486 else if (ctx->hdrs[i]->changed || ctx->hdrs[i]->attach_del ||
1487 (ctx->magic == M_MAILDIR
1488 && (option (OPTMAILDIRTRASH) || ctx->hdrs[i]->trash)
1489 && (ctx->hdrs[i]->deleted != ctx->hdrs[i]->trash)))
1491 if (ctx->magic == M_MAILDIR)
1493 if (maildir_sync_message (ctx, i) == -1)
1498 if (mh_sync_message (ctx, i) == -1)
1505 if (ctx->magic == M_MAILDIR)
1506 mutt_hcache_close (hc);
1507 #endif /* USE_HCACHE */
1509 if (ctx->magic == M_MH)
1510 mh_update_sequences (ctx);
1512 /* XXX race condition? */
1514 maildir_update_mtime (ctx);
1516 /* adjust indices */
1520 for (i = 0, j = 0; i < ctx->msgcount; i++)
1522 if (!ctx->hdrs[i]->deleted
1523 || (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))
1524 ctx->hdrs[i]->index = j++;
1532 if (ctx->magic == M_MAILDIR)
1533 mutt_hcache_close (hc);
1534 #endif /* USE_HCACHE */
1538 static char *maildir_canon_filename (char *dest, const char *src, size_t l)
1542 if ((t = strrchr (src, '/')))
1545 strfcpy (dest, src, l);
1546 if ((u = strrchr (dest, ':')))
1552 static void maildir_update_tables (CONTEXT *ctx, int *index_hint)
1558 if (Sort != SORT_ORDER)
1562 mutt_sort_headers (ctx, 1);
1566 old_count = ctx->msgcount;
1567 for (i = 0, j = 0; i < old_count; i++)
1569 if (ctx->hdrs[i]->active && index_hint && *index_hint == i)
1572 if (ctx->hdrs[i]->active)
1573 ctx->hdrs[i]->index = j++;
1576 mx_update_tables (ctx, 0);
1577 mutt_clear_threads (ctx);
1580 static void maildir_update_flags (CONTEXT *ctx, HEADER *o, HEADER *n)
1582 /* save the global state here so we can reset it at the
1583 * end of list block if required.
1585 int context_changed = ctx->changed;
1587 /* user didn't modify this message. alter the flags to
1588 * match the current state on disk. This may not actually
1589 * do anything, but we can't tell right now. mutt_set_flag()
1590 * will just ignore the call if the status bits are
1591 * already properly set.
1593 mutt_set_flag (ctx, o, M_FLAG, n->flagged);
1594 mutt_set_flag (ctx, o, M_REPLIED, n->replied);
1595 mutt_set_flag (ctx, o, M_READ, n->read);
1596 mutt_set_flag (ctx, o, M_OLD, n->old);
1598 /* mutt_set_flag() will set this, but we don't need to
1599 * sync the changes we made because we just updated the
1600 * context to match the current on-disk state of the
1605 /* if the mailbox was not modified before we made these
1606 * changes, unset the changed flag since nothing needs to
1609 if (!context_changed)
1614 /* This function handles arrival of new mail and reopening of
1615 * maildir folders. The basic idea here is we check to see if either
1616 * the new or cur subdirectories have changed, and if so, we scan them
1617 * for the list of files. We check for newly added messages, and
1618 * then merge the flags messages we already knew about. We don't treat
1619 * either subdirectory differently, as mail could be copied directly into
1620 * the cur directory from another agent.
1622 int maildir_check_mailbox (CONTEXT * ctx, int *index_hint)
1624 struct stat st_new; /* status of the "new" subdirectory */
1625 struct stat st_cur; /* status of the "cur" subdirectory */
1626 char buf[_POSIX_PATH_MAX];
1627 int changed = 0; /* bitmask representing which subdirectories
1628 have changed. 0x1 = new, 0x2 = cur */
1629 int occult = 0; /* messages were removed from the mailbox */
1630 int have_new = 0; /* messages were added to the mailbox */
1631 struct maildir *md; /* list of messages in the mailbox */
1632 struct maildir **last, *p;
1634 HASH *fnames; /* hash table for quickly looking up the base filename
1635 for a maildir message */
1637 /* XXX seems like this check belongs in mx_check_mailbox()
1640 if (!option (OPTCHECKNEW))
1643 snprintf (buf, sizeof (buf), "%s/new", ctx->path);
1644 if (stat (buf, &st_new) == -1)
1647 snprintf (buf, sizeof (buf), "%s/cur", ctx->path);
1648 if (stat (buf, &st_cur) == -1)
1651 /* determine which subdirectories need to be scanned */
1652 if (st_new.st_mtime > ctx->mtime)
1654 if (st_cur.st_mtime > ctx->mtime_cur)
1658 return 0; /* nothing to do */
1660 /* update the modification times on the mailbox */
1661 ctx->mtime_cur = st_cur.st_mtime;
1662 ctx->mtime = st_new.st_mtime;
1664 /* do a fast scan of just the filenames in
1665 * the subdirectories that have changed.
1670 maildir_parse_dir (ctx, &last, "new", NULL);
1672 maildir_parse_dir (ctx, &last, "cur", NULL);
1674 /* we create a hash table keyed off the canonical (sans flags) filename
1675 * of each message we scanned. This is used in the loop over the
1676 * existing messages below to do some correlation.
1678 fnames = hash_create (1031);
1680 for (p = md; p; p = p->next)
1682 maildir_canon_filename (buf, p->h->path, sizeof (buf));
1683 p->canon_fname = safe_strdup (buf);
1684 hash_insert (fnames, p->canon_fname, p, 0);
1687 /* check for modifications and adjust flags */
1688 for (i = 0; i < ctx->msgcount; i++)
1690 ctx->hdrs[i]->active = 0;
1691 maildir_canon_filename (buf, ctx->hdrs[i]->path, sizeof (buf));
1692 p = hash_find (fnames, buf);
1695 /* message already exists, merge flags */
1696 ctx->hdrs[i]->active = 1;
1698 /* check to see if the message has moved to a different
1699 * subdirectory. If so, update the associated filename.
1701 if (mutt_strcmp (ctx->hdrs[i]->path, p->h->path))
1702 mutt_str_replace (&ctx->hdrs[i]->path, p->h->path);
1704 /* if the user hasn't modified the flags on this message, update
1705 * the flags we just detected.
1707 if (!ctx->hdrs[i]->changed)
1708 maildir_update_flags (ctx, ctx->hdrs[i], p->h);
1710 if (ctx->hdrs[i]->deleted == ctx->hdrs[i]->trash)
1711 ctx->hdrs[i]->deleted = p->h->deleted;
1712 ctx->hdrs[i]->trash = p->h->trash;
1714 /* this is a duplicate of an existing header, so remove it */
1715 mutt_free_header (&p->h);
1717 /* This message was not in the list of messages we just scanned.
1718 * Check to see if we have enough information to know if the
1719 * message has disappeared out from underneath us.
1721 else if (((changed & 1) && (!strncmp (ctx->hdrs[i]->path, "new/", 4))) ||
1722 ((changed & 2) && (!strncmp (ctx->hdrs[i]->path, "cur/", 4))))
1724 /* This message disappeared, so we need to simulate a "reopen"
1725 * event. We know it disappeared because we just scanned the
1726 * subdirectory it used to reside in.
1732 /* This message resides in a subdirectory which was not
1733 * modified, so we assume that it is still present and
1736 ctx->hdrs[i]->active = 1;
1740 /* destroy the file name hash */
1741 hash_destroy (&fnames, NULL);
1743 /* If we didn't just get new mail, update the tables. */
1745 maildir_update_tables (ctx, index_hint);
1747 /* do any delayed parsing we need to do. */
1748 maildir_delayed_parsing (ctx, md);
1750 /* Incorporate new messages */
1751 have_new = maildir_move_to_context (ctx, &md);
1753 return occult ? M_REOPENED : (have_new ? M_NEW_MAIL : 0);
1757 * This function handles arrival of new mail and reopening of
1758 * mh/maildir folders. Things are getting rather complex because we
1759 * don't have a well-defined "mailbox order", so the tricks from
1760 * mbox.c and mx.c won't work here.
1762 * Don't change this code unless you _really_ understand what
1767 int mh_check_mailbox (CONTEXT * ctx, int *index_hint)
1769 char buf[_POSIX_PATH_MAX];
1770 struct stat st, st_cur;
1771 short modified = 0, have_new = 0, occult = 0;
1772 struct maildir *md, *p;
1773 struct maildir **last = NULL;
1774 struct mh_sequences mhs;
1778 if (!option (OPTCHECKNEW))
1781 strfcpy (buf, ctx->path, sizeof (buf));
1782 if (stat (buf, &st) == -1)
1785 /* create .mh_sequences when there isn't one. */
1786 snprintf (buf, sizeof (buf), "%s/.mh_sequences", ctx->path);
1787 if ((i = stat (buf, &st_cur) == -1) && errno == ENOENT)
1792 if (mh_mkstemp (ctx, &fp, &tmp) == 0)
1795 if (safe_rename (tmp, buf) == -1)
1801 if (i == -1 && stat (buf, &st_cur) == -1)
1804 if (st.st_mtime > ctx->mtime || st_cur.st_mtime > ctx->mtime_cur)
1810 ctx->mtime_cur = st_cur.st_mtime;
1811 ctx->mtime = st.st_mtime;
1813 memset (&mhs, 0, sizeof (mhs));
1817 maildir_parse_dir (ctx, &last, NULL, NULL);
1818 mh_read_sequences (&mhs, ctx->path);
1819 mh_update_maildir (md, &mhs);
1820 mhs_free_sequences (&mhs);
1822 /* check for modifications and adjust flags */
1823 fnames = hash_create (1031);
1825 for (p = md; p; p = p->next)
1826 hash_insert (fnames, p->h->path, p, 0);
1828 for (i = 0; i < ctx->msgcount; i++)
1830 ctx->hdrs[i]->active = 0;
1832 if ((p = hash_find (fnames, ctx->hdrs[i]->path)) && p->h &&
1833 (mbox_strict_cmp_headers (ctx->hdrs[i], p->h)))
1835 ctx->hdrs[i]->active = 1;
1836 /* found the right message */
1837 if (!ctx->hdrs[i]->changed)
1838 maildir_update_flags (ctx, ctx->hdrs[i], p->h);
1840 mutt_free_header (&p->h);
1842 else /* message has disappeared */
1846 /* destroy the file name hash */
1848 hash_destroy (&fnames, NULL);
1850 /* If we didn't just get new mail, update the tables. */
1852 maildir_update_tables (ctx, index_hint);
1854 /* Incorporate new messages */
1855 have_new = maildir_move_to_context (ctx, &md);
1857 return occult ? M_REOPENED : (have_new ? M_NEW_MAIL : 0);
1864 * These functions try to find a message in a maildir folder when it
1865 * has moved under our feet. Note that this code is rather expensive, but
1866 * then again, it's called rarely.
1869 FILE *_maildir_open_find_message (const char *folder, const char *unique,
1870 const char *subfolder)
1872 char dir[_POSIX_PATH_MAX];
1873 char tunique[_POSIX_PATH_MAX];
1874 char fname[_POSIX_PATH_MAX];
1882 snprintf (dir, sizeof (dir), "%s/%s", folder, subfolder);
1884 if ((dp = opendir (dir)) == NULL)
1890 while ((de = readdir (dp)))
1892 maildir_canon_filename (tunique, de->d_name, sizeof (tunique));
1894 if (!mutt_strcmp (tunique, unique))
1896 snprintf (fname, sizeof (fname), "%s/%s/%s", folder, subfolder,
1898 fp = fopen (fname, "r"); /* __FOPEN_CHECKED__ */
1910 FILE *maildir_open_find_message (const char *folder, const char *msg)
1912 char unique[_POSIX_PATH_MAX];
1915 static unsigned int new_hits = 0, cur_hits = 0; /* simple dynamic optimization */
1917 maildir_canon_filename (unique, msg, sizeof (unique));
1921 _maildir_open_find_message (folder, unique,
1922 new_hits > cur_hits ? "new" : "cur"))
1925 if (new_hits < UINT_MAX && cur_hits < UINT_MAX)
1927 new_hits += (new_hits > cur_hits ? 1 : 0);
1928 cur_hits += (new_hits > cur_hits ? 0 : 1);
1935 _maildir_open_find_message (folder, unique,
1936 new_hits > cur_hits ? "cur" : "new"))
1939 if (new_hits < UINT_MAX && cur_hits < UINT_MAX)
1941 new_hits += (new_hits > cur_hits ? 0 : 1);
1942 cur_hits += (new_hits > cur_hits ? 1 : 0);
1954 * 1 if there are no messages in the mailbox
1955 * 0 if there are messages in the mailbox
1958 int maildir_check_empty (const char *path)
1962 int r = 1; /* assume empty until we find a message */
1963 char realpath[_POSIX_PATH_MAX];
1966 /* Strategy here is to look for any file not beginning with a period */
1969 /* we do "cur" on the first iteration since its more likely that we'll
1970 * find old messages without having to scan both subdirs
1972 snprintf (realpath, sizeof (realpath), "%s/%s", path,
1973 iter == 0 ? "cur" : "new");
1974 if ((dp = opendir (realpath)) == NULL)
1976 while ((de = readdir (dp)))
1978 if (*de->d_name != '.')
1986 } while (r && iter < 2);
1993 * 1 if there are no messages in the mailbox
1994 * 0 if there are messages in the mailbox
1997 int mh_check_empty (const char *path)
2001 int r = 1; /* assume empty until we find a message */
2003 if ((dp = opendir (path)) == NULL)
2005 while ((de = readdir (dp)))
2007 if (mh_valid_message (de->d_name))