2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2002 Michael R. Elkins <me@mutt.org>
4 * Copyright (C) 1999-2002 Thomas Roessler <roessler@does-not-exist.org>
6 * This file is part of mutt-ng, see http://www.muttng.org/.
7 * It's licensed under the GNU General Public License,
8 * please see the file GPL in the top level source directory.
12 * This file contains routines specific to MH and ``maildir'' style
32 #include <sys/types.h>
48 unsigned header_parsed:1;
58 /* mh_sequences support */
60 #define MH_SEQ_UNSEEN (1 << 0)
61 #define MH_SEQ_REPLIED (1 << 1)
62 #define MH_SEQ_FLAGGED (1 << 2)
64 static void mhs_alloc (struct mh_sequences *mhs, int i)
69 if (i > mhs->max || !mhs->flags) {
71 safe_realloc (&mhs->flags, sizeof (mhs->flags[0]) * (newmax + 1));
72 for (j = mhs->max + 1; j <= newmax; j++)
79 static void mhs_free_sequences (struct mh_sequences *mhs)
84 static short mhs_check (struct mh_sequences *mhs, int i)
86 if (!mhs->flags || i > mhs->max)
92 static short mhs_set (struct mh_sequences *mhs, int i, short f)
103 static short mhs_unset (struct mh_sequences *mhs, int i, short f)
107 return mhs->flags[i];
112 static void mh_read_token (char *t, int *first, int *last)
116 if ((p = strchr (t, '-'))) {
122 *first = *last = atoi (t);
125 static void mh_read_sequences (struct mh_sequences *mhs, const char *path)
136 char pathname[_POSIX_PATH_MAX];
138 snprintf (pathname, sizeof (pathname), "%s/.mh_sequences", path);
140 if (!(fp = fopen (pathname, "r")))
143 while ((buff = mutt_read_line (buff, &sz, fp, &line))) {
144 if (!(t = strtok (buff, " \t:")))
147 if (!mutt_strcmp (t, MhUnseen))
149 else if (!mutt_strcmp (t, MhFlagged))
151 else if (!mutt_strcmp (t, MhReplied))
153 else /* unknown sequence */
156 while ((t = strtok (NULL, " \t:"))) {
157 mh_read_token (t, &first, &last);
158 for (; first <= last; first++)
159 mhs_set (mhs, first, f);
167 int mh_buffy (const char *path)
170 struct mh_sequences mhs;
172 memset (&mhs, 0, sizeof (mhs));
174 mh_read_sequences (&mhs, path);
175 for (i = 0; !r && i <= mhs.max; i++)
176 if (mhs_check (&mhs, i) & MH_SEQ_UNSEEN)
178 mhs_free_sequences (&mhs);
182 static int mh_mkstemp (CONTEXT * dest, FILE ** fp, char **tgt)
185 char path[_POSIX_PATH_MAX];
188 snprintf (path, _POSIX_PATH_MAX, "%s/.mutt-%s-%d-%d",
189 dest->path, NONULL (Hostname), (int) getpid (), Counter++);
191 if ((fd = open (path, O_WRONLY | O_EXCL | O_CREAT, 0666)) == -1) {
192 if (errno != EEXIST) {
198 *tgt = safe_strdup (path);
203 if ((*fp = fdopen (fd, "w")) == NULL) {
213 static void mhs_write_one_sequence (FILE * fp, struct mh_sequences *mhs,
214 short f, const char *tag)
219 fprintf (fp, "%s:", tag);
224 for (i = 0; i <= mhs->max; i++) {
225 if ((mhs_check (mhs, i) & f)) {
231 else if (first >= 0) {
233 fprintf (fp, " %d", first);
235 fprintf (fp, " %d-%d", first, last);
244 fprintf (fp, " %d", first);
246 fprintf (fp, " %d-%d", first, last);
252 /* XXX - we don't currently remove deleted messages from sequences we don't know. Should we? */
254 void mh_update_sequences (CONTEXT * ctx)
258 char sequences[_POSIX_PATH_MAX];
270 char seq_unseen[STRING];
271 char seq_replied[STRING];
272 char seq_flagged[STRING];
275 struct mh_sequences mhs;
277 memset (&mhs, 0, sizeof (mhs));
279 snprintf (seq_unseen, sizeof (seq_unseen), "%s:", NONULL (MhUnseen));
280 snprintf (seq_replied, sizeof (seq_replied), "%s:", NONULL (MhReplied));
281 snprintf (seq_flagged, sizeof (seq_flagged), "%s:", NONULL (MhFlagged));
283 if (mh_mkstemp (ctx, &nfp, &tmpfname) != 0) {
288 snprintf (sequences, sizeof (sequences), "%s/.mh_sequences", ctx->path);
291 /* first, copy unknown sequences */
292 if ((ofp = fopen (sequences, "r"))) {
293 while ((buff = mutt_read_line (buff, &s, ofp, &l))) {
294 if (!mutt_strncmp (buff, seq_unseen, mutt_strlen (seq_unseen)))
296 if (!mutt_strncmp (buff, seq_flagged, mutt_strlen (seq_flagged)))
298 if (!mutt_strncmp (buff, seq_replied, mutt_strlen (seq_replied)))
301 fprintf (nfp, "%s\n", buff);
306 /* now, update our unseen, flagged, and replied sequences */
307 for (l = 0; l < ctx->msgcount; l++) {
308 if (ctx->hdrs[l]->deleted)
311 if ((p = strrchr (ctx->hdrs[l]->path, '/')))
314 p = ctx->hdrs[l]->path;
318 if (!ctx->hdrs[l]->read) {
319 mhs_set (&mhs, i, MH_SEQ_UNSEEN);
322 if (ctx->hdrs[l]->flagged) {
323 mhs_set (&mhs, i, MH_SEQ_FLAGGED);
326 if (ctx->hdrs[l]->replied) {
327 mhs_set (&mhs, i, MH_SEQ_REPLIED);
332 /* write out the new sequences */
334 mhs_write_one_sequence (nfp, &mhs, MH_SEQ_UNSEEN, NONULL (MhUnseen));
336 mhs_write_one_sequence (nfp, &mhs, MH_SEQ_FLAGGED, NONULL (MhFlagged));
338 mhs_write_one_sequence (nfp, &mhs, MH_SEQ_REPLIED, NONULL (MhReplied));
340 mhs_free_sequences (&mhs);
343 /* try to commit the changes - no guarantee here */
347 if (safe_rename (tmpfname, sequences) != 0) {
348 /* report an error? */
355 static void mh_sequences_add_one (CONTEXT * ctx, int n, short unseen,
356 short flagged, short replied)
358 short unseen_done = 0;
359 short flagged_done = 0;
360 short replied_done = 0;
362 FILE *ofp = NULL, *nfp = NULL;
365 char sequences[_POSIX_PATH_MAX];
367 char seq_unseen[STRING];
368 char seq_replied[STRING];
369 char seq_flagged[STRING];
375 if (mh_mkstemp (ctx, &nfp, &tmpfname) == -1)
378 snprintf (seq_unseen, sizeof (seq_unseen), "%s:", NONULL (MhUnseen));
379 snprintf (seq_replied, sizeof (seq_replied), "%s:", NONULL (MhReplied));
380 snprintf (seq_flagged, sizeof (seq_flagged), "%s:", NONULL (MhFlagged));
382 snprintf (sequences, sizeof (sequences), "%s/.mh_sequences", ctx->path);
383 if ((ofp = fopen (sequences, "r"))) {
384 while ((buff = mutt_read_line (buff, &sz, ofp, &line))) {
385 if (unseen && !strncmp (buff, seq_unseen, mutt_strlen (seq_unseen))) {
386 fprintf (nfp, "%s %d\n", buff, n);
390 && !strncmp (buff, seq_flagged, mutt_strlen (seq_flagged))) {
391 fprintf (nfp, "%s %d\n", buff, n);
395 && !strncmp (buff, seq_replied, mutt_strlen (seq_replied))) {
396 fprintf (nfp, "%s %d\n", buff, n);
400 fprintf (nfp, "%s\n", buff);
406 if (!unseen_done && unseen)
407 fprintf (nfp, "%s: %d\n", NONULL (MhUnseen), n);
408 if (!flagged_done && flagged)
409 fprintf (nfp, "%s: %d\n", NONULL (MhFlagged), n);
410 if (!replied_done && replied)
411 fprintf (nfp, "%s: %d\n", NONULL (MhReplied), n);
416 if (safe_rename (tmpfname, sequences) != 0)
422 static void mh_update_maildir (struct maildir *md, struct mh_sequences *mhs)
428 for (; md; md = md->next) {
429 if ((p = strrchr (md->h->path, '/')))
435 f = mhs_check (mhs, i);
437 md->h->read = (f & MH_SEQ_UNSEEN) ? 0 : 1;
438 md->h->flagged = (f & MH_SEQ_FLAGGED) ? 1 : 0;
439 md->h->replied = (f & MH_SEQ_REPLIED) ? 1 : 0;
443 /* maildir support */
445 static void maildir_free_entry (struct maildir **md)
450 FREE (&(*md)->canon_fname);
452 mutt_free_header (&(*md)->h);
457 static void maildir_free_maildir (struct maildir **md)
459 struct maildir *p, *q;
464 for (p = *md; p; p = q) {
466 maildir_free_entry (&p);
470 static void maildir_parse_flags (HEADER * h, const char *path)
478 if ((p = strrchr (path, ':')) != NULL && mutt_strncmp (p + 1, "2,", 2) == 0) {
481 mutt_str_replace (&h->maildir_flags, p);
482 q = h->maildir_flags;
496 case 'R': /* replied */
501 case 'T': /* trashed */
514 if (q == h->maildir_flags)
515 FREE (&h->maildir_flags);
520 static void maildir_update_mtime (CONTEXT * ctx)
522 char buf[_POSIX_PATH_MAX];
525 if (ctx->magic == M_MAILDIR) {
526 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, "cur");
527 if (stat (buf, &st) == 0)
528 ctx->mtime_cur = st.st_mtime;
529 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, "new");
532 snprintf (buf, sizeof (buf), "%s/.mh_sequences", ctx->path);
533 if (stat (buf, &st) == 0)
534 ctx->mtime_cur = st.st_mtime;
536 strfcpy (buf, ctx->path, sizeof (buf));
539 if (stat (buf, &st) == 0)
540 ctx->mtime = st.st_mtime;
544 * Actually parse a maildir message. This may also be used to fill
545 * out a fake header structure generated by lazy maildir parsing.
547 static HEADER *maildir_parse_message (int magic, const char *fname,
548 int is_old, HEADER * _h)
554 if ((f = fopen (fname, "r")) != NULL) {
556 h = mutt_new_header ();
557 h->env = mutt_read_rfc822_header (f, h, 0, 0);
559 fstat (fileno (f), &st);
563 h->received = h->date_sent;
565 if (h->content->length <= 0)
566 h->content->length = st.st_size - h->content->offset;
570 if (magic == M_MAILDIR) {
572 * maildir stores its flags in the filename, so ignore the
573 * flags in the header of the message
577 maildir_parse_flags (h, fname);
585 * Note that this routine will _not_ modify the context given by
588 * It's used in the first parsing pass on maildir and MH folders.
589 * In the MH case, this means full parsing of the folder. In the
590 * maildir case, it means that we only look at flags, and create a
591 * fake HEADER structure, which may later be filled in by
592 * maildir_parse_message(), when called from
593 * maildir_delayed_parsing().
597 static int maildir_parse_entry (CONTEXT * ctx, struct maildir ***last,
598 const char *subdir, const char *fname,
599 int *count, int is_old, ino_t inode)
601 struct maildir *entry;
603 char buf[_POSIX_PATH_MAX];
606 snprintf (buf, sizeof (buf), "%s/%s/%s", ctx->path, subdir, fname);
608 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, fname);
610 if (ctx->magic == M_MH)
611 h = maildir_parse_message (ctx->magic, buf, is_old, NULL);
613 h = mutt_new_header ();
615 maildir_parse_flags (h, buf);
621 if (!ctx->quiet && ReadInc && ((*count % ReadInc) == 0 || *count == 1))
622 mutt_message (_("Reading %s... %d"), ctx->path, *count);
626 snprintf (buf, sizeof (buf), "%s/%s", subdir, fname);
627 h->path = safe_strdup (buf);
630 h->path = safe_strdup (fname);
632 entry = safe_calloc (sizeof (struct maildir), 1);
634 entry->header_parsed = (ctx->magic == M_MH);
635 entry->inode = inode;
637 *last = &entry->next;
647 /* Ignore the garbage files. A valid MH message consists of only
648 * digits. Deleted message get moved to a filename with a comma before
652 int mh_valid_message (const char *s)
655 if (!isdigit ((unsigned char) *s))
661 static int maildir_parse_dir (CONTEXT * ctx, struct maildir ***last,
662 const char *subdir, int *count)
666 char buf[_POSIX_PATH_MAX];
670 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, subdir);
671 is_old = (mutt_strcmp ("cur", subdir) == 0);
674 strfcpy (buf, ctx->path, sizeof (buf));
676 if ((dirp = opendir (buf)) == NULL)
679 while ((de = readdir (dirp)) != NULL) {
681 if ((ctx->magic == M_MH && !mh_valid_message (de->d_name))
682 || (ctx->magic == M_MAILDIR && *de->d_name == '.'))
685 /* FOO - really ignore the return value? */
688 (debugfile, "%s:%d: parsing %s\n", __FILE__, __LINE__,
690 maildir_parse_entry (ctx, last, subdir, de->d_name, count, is_old,
698 static int maildir_add_to_context (CONTEXT * ctx, struct maildir *md)
700 int oldmsgcount = ctx->msgcount;
704 dprint (2, (debugfile, "%s:%d maildir_add_to_context(): Considering %s\n",
705 __FILE__, __LINE__, NONULL (md->canon_fname)));
710 "%s:%d Adding header structure. Flags: %s%s%s%s%s\n", __FILE__,
711 __LINE__, md->h->flagged ? "f" : "", md->h->deleted ? "D" : "",
712 md->h->replied ? "r" : "", md->h->old ? "O" : "",
713 md->h->read ? "R" : ""));
714 if (ctx->msgcount == ctx->hdrmax)
715 mx_alloc_memory (ctx);
717 ctx->hdrs[ctx->msgcount] = md->h;
718 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
720 md->h->content->length + md->h->content->offset -
721 md->h->content->hdr_offset;
729 if (ctx->msgcount > oldmsgcount) {
730 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
736 static int maildir_move_to_context (CONTEXT * ctx, struct maildir **md)
740 r = maildir_add_to_context (ctx, *md);
741 maildir_free_maildir (md);
746 * Merge two maildir lists according to the inode numbers.
748 static struct maildir *maildir_merge_inode (struct maildir *left,
749 struct maildir *right)
751 struct maildir *head;
752 struct maildir *tail;
755 if (left->inode < right->inode) {
773 while (left && right) {
774 if (left->inode < right->inode) {
796 * Sort maildir list according to inode.
798 static struct maildir *maildir_sort_inode (struct maildir *list)
800 struct maildir *left = list;
801 struct maildir *right = list;
803 if (!list || !list->next) {
808 while (list && list->next) {
810 list = list->next->next;
817 left = maildir_sort_inode (left);
818 right = maildir_sort_inode (right);
819 return maildir_merge_inode (left, right);
823 static size_t maildir_hcache_keylen (const char *fn)
825 const char *p = strchr (fn, ':');
827 return p ? (size_t) (p - fn) : mutt_strlen (fn);
832 * This function does the second parsing pass for a maildir-style
835 void maildir_delayed_parsing (CONTEXT * ctx, struct maildir *md)
838 char fn[_POSIX_PATH_MAX];
844 struct timeval *when = NULL;
845 struct stat lastchanged;
848 hc = mutt_hcache_open (HeaderCache, ctx->path);
851 for (p = md, count = 0; p; p = p->next, count++) {
852 if (!(p && p->h && !p->header_parsed))
856 data = mutt_hcache_fetch (hc, p->h->path + 3, &maildir_hcache_keylen);
857 when = (struct timeval *) data;
860 if (!ctx->quiet && ReadInc && ((count % ReadInc) == 0 || count == 1))
861 mutt_message (_("Reading %s... %d"), ctx->path, count);
862 snprintf (fn, sizeof (fn), "%s/%s", ctx->path, p->h->path);
865 if (option (OPTHCACHEVERIFY)) {
866 ret = stat (fn, &lastchanged);
869 lastchanged.st_mtime = 0;
873 if (data != NULL && !ret && lastchanged.st_mtime <= when->tv_sec) {
874 p->h = mutt_hcache_restore ((unsigned char *) data, &p->h);
875 maildir_parse_flags (p->h, fn);
879 if (maildir_parse_message (ctx->magic, fn, p->h->old, p->h)) {
880 p->header_parsed = 1;
881 maildir_parse_flags (p->h, fn);
883 mutt_hcache_store (hc, p->h->path + 3, p->h, 0, &maildir_hcache_keylen);
887 mutt_free_header (&p->h);
893 mutt_hcache_close (hc);
897 /* Read a MH/maildir style mailbox.
900 * ctx [IN/OUT] context for this mailbox
901 * subdir [IN] NULL for MH mailboxes, otherwise the subdir of the
902 * maildir mailbox to read from
904 int mh_read_dir (CONTEXT * ctx, const char *subdir)
907 struct mh_sequences mhs;
908 struct maildir **last;
912 memset (&mhs, 0, sizeof (mhs));
914 maildir_update_mtime (ctx);
919 if (maildir_parse_dir (ctx, &last, subdir, &count) == -1)
922 if (ctx->magic == M_MH) {
923 mh_read_sequences (&mhs, ctx->path);
924 mh_update_maildir (md, &mhs);
925 mhs_free_sequences (&mhs);
928 md = maildir_sort_inode (md);
930 if (ctx->magic == M_MAILDIR)
931 maildir_delayed_parsing (ctx, md);
933 maildir_move_to_context (ctx, &md);
937 /* read a maildir style mailbox */
938 int maildir_read_dir (CONTEXT * ctx)
940 /* maildir looks sort of like MH, except that there are two subdirectories
941 * of the main folder path from which to read messages
943 if (mh_read_dir (ctx, "new") == -1 || mh_read_dir (ctx, "cur") == -1)
950 * Open a new (temporary) message in an MH folder.
953 int mh_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr)
955 return mh_mkstemp (dest, &msg->fp, &msg->path);
958 int ch_compar (const void *a, const void *b)
960 return (int) (*((const char *) a) - *((const char *) b));
963 static void maildir_flags (char *dest, size_t destlen, HEADER * hdr)
968 * The maildir specification requires that all files in the cur
969 * subdirectory have the :unique string appeneded, regardless of whether
970 * or not there are any flags. If .old is set, we know that this message
971 * will end up in the cur directory, so we include it in the following
972 * test even though there is no associated flag.
976 && (hdr->flagged || hdr->replied || hdr->read || hdr->deleted
977 || hdr->old || hdr->maildir_flags)) {
978 char tmp[LONG_STRING];
980 snprintf (tmp, sizeof (tmp),
982 hdr->flagged ? "F" : "",
983 hdr->replied ? "R" : "",
984 hdr->read ? "S" : "", hdr->deleted ? "T" : "",
985 NONULL (hdr->maildir_flags));
986 if (hdr->maildir_flags)
987 qsort (tmp, mutt_strlen (tmp), 1, ch_compar);
988 snprintf (dest, destlen, ":2,%s", tmp);
994 * Open a new (temporary) message in a maildir folder.
996 * Note that this uses _almost_ the maildir file name format, but
997 * with a {cur,new} prefix.
1001 int maildir_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr)
1004 char path[_POSIX_PATH_MAX];
1009 short deleted = hdr->deleted;
1013 maildir_flags (suffix, sizeof (suffix), hdr);
1015 hdr->deleted = deleted;
1020 if (hdr && (hdr->read || hdr->old))
1021 strfcpy (subdir, "cur", sizeof (subdir));
1023 strfcpy (subdir, "new", sizeof (subdir));
1026 snprintf (path, _POSIX_PATH_MAX, "%s/tmp/%s.%ld.%u_%d.%s%s",
1027 dest->path, subdir, (long) time (NULL),
1028 (unsigned int) getpid (), Counter++, NONULL (Hostname), suffix);
1030 dprint (2, (debugfile, "maildir_open_new_message (): Trying %s.\n",
1034 if ((fd = open (path, O_WRONLY | O_EXCL | O_CREAT, 0666)) == -1) {
1035 if (errno != EEXIST) {
1041 dprint (2, (debugfile, "maildir_open_new_message (): Success.\n"));
1042 msg->path = safe_strdup (path);
1047 if ((msg->fp = fdopen (fd, "w")) == NULL) {
1060 * Commit a message to a maildir folder.
1062 * msg->path contains the file name of a file in tmp/. We take the
1063 * flags from this file's name.
1065 * ctx is the mail folder we commit to.
1067 * hdr is a header structure to which we write the message's new
1068 * file name. This is used in the mh and maildir folder synch
1069 * routines. When this routine is invoked from mx_commit_message,
1072 * msg->path looks like this:
1074 * tmp/{cur,new}.mutt-HOSTNAME-PID-COUNTER:flags
1076 * See also maildir_open_new_message().
1080 int maildir_commit_message (CONTEXT * ctx, MESSAGE * msg, HEADER * hdr)
1084 char path[_POSIX_PATH_MAX];
1085 char full[_POSIX_PATH_MAX];
1088 if (safe_fclose (&msg->fp) != 0)
1091 /* extract the subdir */
1092 s = strrchr (msg->path, '/') + 1;
1093 strfcpy (subdir, s, 4);
1095 /* extract the flags */
1096 if ((s = strchr (s, ':')))
1097 strfcpy (suffix, s, sizeof (suffix));
1101 /* construct a new file name. */
1103 snprintf (path, _POSIX_PATH_MAX, "%s/%ld.%u_%d.%s%s", subdir,
1104 (long) time (NULL), (unsigned int) getpid (), Counter++,
1105 NONULL (Hostname), suffix);
1106 snprintf (full, _POSIX_PATH_MAX, "%s/%s", ctx->path, path);
1108 dprint (2, (debugfile, "maildir_commit_message (): renaming %s to %s.\n",
1111 if (safe_rename (msg->path, full) == 0) {
1113 mutt_str_replace (&hdr->path, path);
1117 * Adjust the mtime on the file to match the time at which this
1118 * message was received. Currently this is only set when copying
1119 * messages between mailboxes, so we test to ensure that it is
1122 if (msg->received) {
1125 ut.actime = msg->received;
1126 ut.modtime = msg->received;
1127 if (utime (full, &ut)) {
1129 ("maildir_commit_message(): unable to set time on file"));
1136 else if (errno != EEXIST) {
1137 mutt_perror (ctx->path);
1144 * commit a message to an MH folder.
1149 static int _mh_commit_message (CONTEXT * ctx, MESSAGE * msg, HEADER * hdr,
1155 unsigned int n, hi = 0;
1156 char path[_POSIX_PATH_MAX];
1159 if (safe_fclose (&msg->fp) != 0)
1162 if ((dirp = opendir (ctx->path)) == NULL) {
1163 mutt_perror (ctx->path);
1167 /* figure out what the next message number is */
1168 while ((de = readdir (dirp)) != NULL) {
1174 if (!isdigit ((unsigned char) *cp))
1187 * Now try to rename the file to the proper name.
1189 * Note: We may have to try multiple times, until we find a free
1195 snprintf (tmp, sizeof (tmp), "%d", hi);
1196 snprintf (path, sizeof (path), "%s/%s", ctx->path, tmp);
1197 if (safe_rename (msg->path, path) == 0) {
1199 mutt_str_replace (&hdr->path, tmp);
1203 else if (errno != EEXIST) {
1204 mutt_perror (ctx->path);
1209 mh_sequences_add_one (ctx, hi, !msg->flags.read, msg->flags.flagged,
1210 msg->flags.replied);
1214 int mh_commit_message (CONTEXT * ctx, MESSAGE * msg, HEADER * hdr)
1216 return _mh_commit_message (ctx, msg, hdr, 1);
1220 /* Sync a message in an MH folder.
1222 * This code is also used for attachment deletion in maildir
1226 static int mh_rewrite_message (CONTEXT * ctx, int msgno)
1228 HEADER *h = ctx->hdrs[msgno];
1233 char oldpath[_POSIX_PATH_MAX];
1234 char newpath[_POSIX_PATH_MAX];
1235 char partpath[_POSIX_PATH_MAX];
1237 long old_body_offset = h->content->offset;
1238 long old_body_length = h->content->length;
1239 long old_hdr_lines = h->lines;
1241 if ((dest = mx_open_new_message (ctx, h, 0)) == NULL)
1244 if ((rc = mutt_copy_message (dest->fp, ctx, h,
1245 M_CM_UPDATE, CH_UPDATE | CH_UPDATE_LEN)) == 0)
1247 snprintf (oldpath, _POSIX_PATH_MAX, "%s/%s", ctx->path, h->path);
1248 strfcpy (partpath, h->path, _POSIX_PATH_MAX);
1250 if (ctx->magic == M_MAILDIR)
1251 rc = maildir_commit_message (ctx, dest, h);
1253 rc = _mh_commit_message (ctx, dest, h, 0);
1255 mx_close_message (&dest);
1263 * Try to move the new message to the old place.
1266 * This is important when we are just updating flags.
1268 * Note that there is a race condition against programs which
1269 * use the first free slot instead of the maximum message
1270 * number. Mutt does _not_ behave like this.
1272 * Anyway, if this fails, the message is in the folder, so
1273 * all what happens is that a concurrently runnung mutt will
1274 * lose flag modifications.
1277 if (ctx->magic == M_MH && rc == 0) {
1278 snprintf (newpath, _POSIX_PATH_MAX, "%s/%s", ctx->path, h->path);
1279 if ((rc = safe_rename (newpath, oldpath)) == 0)
1280 mutt_str_replace (&h->path, partpath);
1284 mx_close_message (&dest);
1286 if (rc == -1 && restore) {
1287 h->content->offset = old_body_offset;
1288 h->content->length = old_body_length;
1289 h->lines = old_hdr_lines;
1292 mutt_free_body (&h->content->parts);
1296 static int mh_sync_message (CONTEXT * ctx, int msgno)
1298 HEADER *h = ctx->hdrs[msgno];
1300 if (h->attach_del || h->refs_changed || h->irt_changed)
1301 if (mh_rewrite_message (ctx, msgno) != 0)
1307 static int maildir_sync_message (CONTEXT * ctx, int msgno)
1309 HEADER *h = ctx->hdrs[msgno];
1311 if (h->attach_del || h->refs_changed || h->irt_changed) {
1312 /* when doing attachment deletion/rethreading, fall back to the MH case. */
1313 if (mh_rewrite_message (ctx, msgno) != 0)
1317 /* we just have to rename the file. */
1319 char newpath[_POSIX_PATH_MAX];
1320 char partpath[_POSIX_PATH_MAX];
1321 char fullpath[_POSIX_PATH_MAX];
1322 char oldpath[_POSIX_PATH_MAX];
1326 if ((p = strrchr (h->path, '/')) == NULL) {
1329 "maildir_sync_message: %s: unable to find subdir!\n",
1334 strfcpy (newpath, p, sizeof (newpath));
1336 /* kill the previous flags */
1337 if ((p = strchr (newpath, ':')) != NULL)
1340 maildir_flags (suffix, sizeof (suffix), h);
1342 snprintf (partpath, sizeof (partpath), "%s/%s%s",
1343 (h->read || h->old) ? "cur" : "new", newpath, suffix);
1344 snprintf (fullpath, sizeof (fullpath), "%s/%s", ctx->path, partpath);
1345 snprintf (oldpath, sizeof (oldpath), "%s/%s", ctx->path, h->path);
1347 if (mutt_strcmp (fullpath, oldpath) == 0) {
1348 /* message hasn't really changed */
1352 /* record that the message is possibly marked as trashed on disk */
1353 h->trash = h->deleted;
1355 if (rename (oldpath, fullpath) != 0) {
1356 mutt_perror ("rename");
1359 mutt_str_replace (&h->path, partpath);
1364 int mh_sync_mailbox (CONTEXT * ctx, int *index_hint)
1366 char path[_POSIX_PATH_MAX], tmp[_POSIX_PATH_MAX];
1371 #endif /* USE_HCACHE */
1373 if (ctx->magic == M_MH)
1374 i = mh_check_mailbox (ctx, index_hint);
1376 i = maildir_check_mailbox (ctx, index_hint);
1382 if (ctx->magic == M_MAILDIR)
1383 hc = mutt_hcache_open (HeaderCache, ctx->path);
1384 #endif /* USE_HCACHE */
1386 for (i = 0; i < ctx->msgcount; i++) {
1387 if (ctx->hdrs[i]->deleted
1388 && (ctx->magic != M_MAILDIR || !option (OPTMAILDIRTRASH))) {
1389 snprintf (path, sizeof (path), "%s/%s", ctx->path, ctx->hdrs[i]->path);
1390 if (ctx->magic == M_MAILDIR
1391 || (option (OPTMHPURGE) && ctx->magic == M_MH)) {
1393 if (ctx->magic == M_MAILDIR)
1394 mutt_hcache_delete (hc, ctx->hdrs[i]->path + 3,
1395 &maildir_hcache_keylen);
1396 #endif /* USE_HCACHE */
1399 else if (ctx->magic == M_MH) {
1400 /* MH just moves files out of the way when you delete them */
1401 if (*ctx->hdrs[i]->path != ',') {
1402 snprintf (tmp, sizeof (tmp), "%s/,%s", ctx->path,
1403 ctx->hdrs[i]->path);
1410 else if (ctx->hdrs[i]->changed || ctx->hdrs[i]->attach_del ||
1411 (ctx->magic == M_MAILDIR
1412 && (option (OPTMAILDIRTRASH) || ctx->hdrs[i]->trash)
1413 && (ctx->hdrs[i]->deleted != ctx->hdrs[i]->trash))) {
1414 if (ctx->magic == M_MAILDIR) {
1415 if (maildir_sync_message (ctx, i) == -1)
1419 if (mh_sync_message (ctx, i) == -1)
1426 if (ctx->magic == M_MAILDIR)
1427 mutt_hcache_close (hc);
1428 #endif /* USE_HCACHE */
1430 if (ctx->magic == M_MH)
1431 mh_update_sequences (ctx);
1433 /* XXX race condition? */
1435 maildir_update_mtime (ctx);
1437 /* adjust indices */
1440 for (i = 0, j = 0; i < ctx->msgcount; i++) {
1441 if (!ctx->hdrs[i]->deleted
1442 || (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))
1443 ctx->hdrs[i]->index = j++;
1451 if (ctx->magic == M_MAILDIR)
1452 mutt_hcache_close (hc);
1453 #endif /* USE_HCACHE */
1457 static char *maildir_canon_filename (char *dest, const char *src, size_t l)
1461 if ((t = strrchr (src, '/')))
1464 strfcpy (dest, src, l);
1465 if ((u = strrchr (dest, ':')))
1471 static void maildir_update_tables (CONTEXT * ctx, int *index_hint)
1477 if (Sort != SORT_ORDER) {
1480 mutt_sort_headers (ctx, 1);
1484 old_count = ctx->msgcount;
1485 for (i = 0, j = 0; i < old_count; i++) {
1486 if (ctx->hdrs[i]->active && index_hint && *index_hint == i)
1489 if (ctx->hdrs[i]->active)
1490 ctx->hdrs[i]->index = j++;
1493 mx_update_tables (ctx, 0);
1494 mutt_clear_threads (ctx);
1497 static void maildir_update_flags (CONTEXT * ctx, HEADER * o, HEADER * n)
1499 /* save the global state here so we can reset it at the
1500 * end of list block if required.
1502 int context_changed = ctx->changed;
1504 /* user didn't modify this message. alter the flags to
1505 * match the current state on disk. This may not actually
1506 * do anything, but we can't tell right now. mutt_set_flag()
1507 * will just ignore the call if the status bits are
1508 * already properly set.
1510 mutt_set_flag (ctx, o, M_FLAG, n->flagged);
1511 mutt_set_flag (ctx, o, M_REPLIED, n->replied);
1512 mutt_set_flag (ctx, o, M_READ, n->read);
1513 mutt_set_flag (ctx, o, M_OLD, n->old);
1515 /* mutt_set_flag() will set this, but we don't need to
1516 * sync the changes we made because we just updated the
1517 * context to match the current on-disk state of the
1522 /* if the mailbox was not modified before we made these
1523 * changes, unset the changed flag since nothing needs to
1526 if (!context_changed)
1531 /* This function handles arrival of new mail and reopening of
1532 * maildir folders. The basic idea here is we check to see if either
1533 * the new or cur subdirectories have changed, and if so, we scan them
1534 * for the list of files. We check for newly added messages, and
1535 * then merge the flags messages we already knew about. We don't treat
1536 * either subdirectory differently, as mail could be copied directly into
1537 * the cur directory from another agent.
1539 int maildir_check_mailbox (CONTEXT * ctx, int *index_hint)
1541 struct stat st_new; /* status of the "new" subdirectory */
1542 struct stat st_cur; /* status of the "cur" subdirectory */
1543 char buf[_POSIX_PATH_MAX];
1544 int changed = 0; /* bitmask representing which subdirectories
1545 have changed. 0x1 = new, 0x2 = cur */
1546 int occult = 0; /* messages were removed from the mailbox */
1547 int have_new = 0; /* messages were added to the mailbox */
1548 struct maildir *md; /* list of messages in the mailbox */
1549 struct maildir **last, *p;
1551 HASH *fnames; /* hash table for quickly looking up the base filename
1552 for a maildir message */
1554 /* XXX seems like this check belongs in mx_check_mailbox()
1557 if (!option (OPTCHECKNEW))
1560 snprintf (buf, sizeof (buf), "%s/new", ctx->path);
1561 if (stat (buf, &st_new) == -1)
1564 snprintf (buf, sizeof (buf), "%s/cur", ctx->path);
1565 if (stat (buf, &st_cur) == -1)
1568 /* determine which subdirectories need to be scanned */
1569 if (st_new.st_mtime > ctx->mtime)
1571 if (st_cur.st_mtime > ctx->mtime_cur)
1575 return 0; /* nothing to do */
1577 /* update the modification times on the mailbox */
1578 ctx->mtime_cur = st_cur.st_mtime;
1579 ctx->mtime = st_new.st_mtime;
1581 /* do a fast scan of just the filenames in
1582 * the subdirectories that have changed.
1587 maildir_parse_dir (ctx, &last, "new", NULL);
1589 maildir_parse_dir (ctx, &last, "cur", NULL);
1591 /* we create a hash table keyed off the canonical (sans flags) filename
1592 * of each message we scanned. This is used in the loop over the
1593 * existing messages below to do some correlation.
1595 fnames = hash_create (1031);
1597 for (p = md; p; p = p->next) {
1598 maildir_canon_filename (buf, p->h->path, sizeof (buf));
1599 p->canon_fname = safe_strdup (buf);
1600 hash_insert (fnames, p->canon_fname, p, 0);
1603 /* check for modifications and adjust flags */
1604 for (i = 0; i < ctx->msgcount; i++) {
1605 ctx->hdrs[i]->active = 0;
1606 maildir_canon_filename (buf, ctx->hdrs[i]->path, sizeof (buf));
1607 p = hash_find (fnames, buf);
1609 /* message already exists, merge flags */
1610 ctx->hdrs[i]->active = 1;
1612 /* check to see if the message has moved to a different
1613 * subdirectory. If so, update the associated filename.
1615 if (mutt_strcmp (ctx->hdrs[i]->path, p->h->path))
1616 mutt_str_replace (&ctx->hdrs[i]->path, p->h->path);
1618 /* if the user hasn't modified the flags on this message, update
1619 * the flags we just detected.
1621 if (!ctx->hdrs[i]->changed)
1622 maildir_update_flags (ctx, ctx->hdrs[i], p->h);
1624 if (ctx->hdrs[i]->deleted == ctx->hdrs[i]->trash)
1625 ctx->hdrs[i]->deleted = p->h->deleted;
1626 ctx->hdrs[i]->trash = p->h->trash;
1628 /* this is a duplicate of an existing header, so remove it */
1629 mutt_free_header (&p->h);
1631 /* This message was not in the list of messages we just scanned.
1632 * Check to see if we have enough information to know if the
1633 * message has disappeared out from underneath us.
1635 else if (((changed & 1) && (!strncmp (ctx->hdrs[i]->path, "new/", 4))) ||
1636 ((changed & 2) && (!strncmp (ctx->hdrs[i]->path, "cur/", 4)))) {
1637 /* This message disappeared, so we need to simulate a "reopen"
1638 * event. We know it disappeared because we just scanned the
1639 * subdirectory it used to reside in.
1644 /* This message resides in a subdirectory which was not
1645 * modified, so we assume that it is still present and
1648 ctx->hdrs[i]->active = 1;
1652 /* destroy the file name hash */
1653 hash_destroy (&fnames, NULL);
1655 /* If we didn't just get new mail, update the tables. */
1657 maildir_update_tables (ctx, index_hint);
1659 /* do any delayed parsing we need to do. */
1660 maildir_delayed_parsing (ctx, md);
1662 /* Incorporate new messages */
1663 have_new = maildir_move_to_context (ctx, &md);
1665 return occult ? M_REOPENED : (have_new ? M_NEW_MAIL : 0);
1669 * This function handles arrival of new mail and reopening of
1670 * mh/maildir folders. Things are getting rather complex because we
1671 * don't have a well-defined "mailbox order", so the tricks from
1672 * mbox.c and mx.c won't work here.
1674 * Don't change this code unless you _really_ understand what
1679 int mh_check_mailbox (CONTEXT * ctx, int *index_hint)
1681 char buf[_POSIX_PATH_MAX];
1682 struct stat st, st_cur;
1683 short modified = 0, have_new = 0, occult = 0;
1684 struct maildir *md, *p;
1685 struct maildir **last = NULL;
1686 struct mh_sequences mhs;
1690 if (!option (OPTCHECKNEW))
1693 strfcpy (buf, ctx->path, sizeof (buf));
1694 if (stat (buf, &st) == -1)
1697 /* create .mh_sequences when there isn't one. */
1698 snprintf (buf, sizeof (buf), "%s/.mh_sequences", ctx->path);
1699 if ((i = stat (buf, &st_cur) == -1) && errno == ENOENT) {
1703 if (mh_mkstemp (ctx, &fp, &tmp) == 0) {
1705 if (safe_rename (tmp, buf) == -1)
1711 if (i == -1 && stat (buf, &st_cur) == -1)
1714 if (st.st_mtime > ctx->mtime || st_cur.st_mtime > ctx->mtime_cur)
1720 ctx->mtime_cur = st_cur.st_mtime;
1721 ctx->mtime = st.st_mtime;
1723 memset (&mhs, 0, sizeof (mhs));
1727 maildir_parse_dir (ctx, &last, NULL, NULL);
1728 mh_read_sequences (&mhs, ctx->path);
1729 mh_update_maildir (md, &mhs);
1730 mhs_free_sequences (&mhs);
1732 /* check for modifications and adjust flags */
1733 fnames = hash_create (1031);
1735 for (p = md; p; p = p->next)
1736 hash_insert (fnames, p->h->path, p, 0);
1738 for (i = 0; i < ctx->msgcount; i++) {
1739 ctx->hdrs[i]->active = 0;
1741 if ((p = hash_find (fnames, ctx->hdrs[i]->path)) && p->h &&
1742 (mbox_strict_cmp_headers (ctx->hdrs[i], p->h))) {
1743 ctx->hdrs[i]->active = 1;
1744 /* found the right message */
1745 if (!ctx->hdrs[i]->changed)
1746 maildir_update_flags (ctx, ctx->hdrs[i], p->h);
1748 mutt_free_header (&p->h);
1750 else /* message has disappeared */
1754 /* destroy the file name hash */
1756 hash_destroy (&fnames, NULL);
1758 /* If we didn't just get new mail, update the tables. */
1760 maildir_update_tables (ctx, index_hint);
1762 /* Incorporate new messages */
1763 have_new = maildir_move_to_context (ctx, &md);
1765 return occult ? M_REOPENED : (have_new ? M_NEW_MAIL : 0);
1772 * These functions try to find a message in a maildir folder when it
1773 * has moved under our feet. Note that this code is rather expensive, but
1774 * then again, it's called rarely.
1777 FILE *_maildir_open_find_message (const char *folder, const char *unique,
1778 const char *subfolder)
1780 char dir[_POSIX_PATH_MAX];
1781 char tunique[_POSIX_PATH_MAX];
1782 char fname[_POSIX_PATH_MAX];
1790 snprintf (dir, sizeof (dir), "%s/%s", folder, subfolder);
1792 if ((dp = opendir (dir)) == NULL) {
1797 while ((de = readdir (dp))) {
1798 maildir_canon_filename (tunique, de->d_name, sizeof (tunique));
1800 if (!mutt_strcmp (tunique, unique)) {
1801 snprintf (fname, sizeof (fname), "%s/%s/%s", folder, subfolder,
1803 fp = fopen (fname, "r"); /* __FOPEN_CHECKED__ */
1815 FILE *maildir_open_find_message (const char *folder, const char *msg)
1817 char unique[_POSIX_PATH_MAX];
1820 static unsigned int new_hits = 0, cur_hits = 0; /* simple dynamic optimization */
1822 maildir_canon_filename (unique, msg, sizeof (unique));
1825 _maildir_open_find_message (folder, unique,
1826 new_hits > cur_hits ? "new" : "cur"))
1827 || errno != ENOENT) {
1828 if (new_hits < UINT_MAX && cur_hits < UINT_MAX) {
1829 new_hits += (new_hits > cur_hits ? 1 : 0);
1830 cur_hits += (new_hits > cur_hits ? 0 : 1);
1836 _maildir_open_find_message (folder, unique,
1837 new_hits > cur_hits ? "cur" : "new"))
1838 || errno != ENOENT) {
1839 if (new_hits < UINT_MAX && cur_hits < UINT_MAX) {
1840 new_hits += (new_hits > cur_hits ? 0 : 1);
1841 cur_hits += (new_hits > cur_hits ? 1 : 0);
1853 * 1 if there are no messages in the mailbox
1854 * 0 if there are messages in the mailbox
1857 int maildir_check_empty (const char *path)
1861 int r = 1; /* assume empty until we find a message */
1862 char realpath[_POSIX_PATH_MAX];
1865 /* Strategy here is to look for any file not beginning with a period */
1868 /* we do "cur" on the first iteration since its more likely that we'll
1869 * find old messages without having to scan both subdirs
1871 snprintf (realpath, sizeof (realpath), "%s/%s", path,
1872 iter == 0 ? "cur" : "new");
1873 if ((dp = opendir (realpath)) == NULL)
1875 while ((de = readdir (dp))) {
1876 if (*de->d_name != '.') {
1883 } while (r && iter < 2);
1890 * 1 if there are no messages in the mailbox
1891 * 0 if there are messages in the mailbox
1894 int mh_check_empty (const char *path)
1898 int r = 1; /* assume empty until we find a message */
1900 if ((dp = opendir (path)) == NULL)
1902 while ((de = readdir (dp))) {
1903 if (mh_valid_message (de->d_name)) {