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++);
195 if ((fd = open (path, O_WRONLY | O_EXCL | O_CREAT, 0600)) == -1)
205 *tgt = safe_strdup (path);
210 if ((*fp = fdopen (fd, "w")) == NULL)
221 static void mhs_write_one_sequence (FILE * fp, struct mh_sequences *mhs,
222 short f, const char *tag)
226 fprintf (fp, "%s:", tag);
231 for (i = 0; i <= mhs->max; i++)
233 if ((mhs_check (mhs, i) & f))
243 fprintf (fp, " %d", first);
245 fprintf (fp, " %d-%d", first, last);
255 fprintf (fp, " %d", first);
257 fprintf (fp, " %d-%d", first, last);
263 /* XXX - we don't currently remove deleted messages from sequences we don't know. Should we? */
265 void mh_update_sequences (CONTEXT * ctx)
269 char sequences[_POSIX_PATH_MAX];
281 char seq_unseen[STRING];
282 char seq_replied[STRING];
283 char seq_flagged[STRING];
286 struct mh_sequences mhs;
287 memset (&mhs, 0, sizeof (mhs));
289 snprintf (seq_unseen, sizeof (seq_unseen), "%s:", NONULL (MhUnseen));
290 snprintf (seq_replied, sizeof (seq_replied), "%s:", NONULL (MhReplied));
291 snprintf (seq_flagged, sizeof (seq_flagged), "%s:", NONULL (MhFlagged));
293 if (mh_mkstemp (ctx, &nfp, &tmpfname) != 0)
299 snprintf (sequences, sizeof (sequences), "%s/.mh_sequences", ctx->path);
302 /* first, copy unknown sequences */
303 if ((ofp = fopen (sequences, "r")))
305 while ((buff = mutt_read_line (buff, &s, ofp, &l)))
307 if (!mutt_strncmp (buff, seq_unseen, mutt_strlen (seq_unseen)))
309 if (!mutt_strncmp (buff, seq_flagged, mutt_strlen (seq_flagged)))
311 if (!mutt_strncmp (buff, seq_replied, mutt_strlen (seq_replied)))
314 fprintf (nfp, "%s\n", buff);
319 /* now, update our unseen, flagged, and replied sequences */
320 for (l = 0; l < ctx->msgcount; l++)
322 if (ctx->hdrs[l]->deleted)
325 if ((p = strrchr (ctx->hdrs[l]->path, '/')))
328 p = ctx->hdrs[l]->path;
332 if (!ctx->hdrs[l]->read)
334 mhs_set (&mhs, i, MH_SEQ_UNSEEN);
337 if (ctx->hdrs[l]->flagged)
339 mhs_set (&mhs, i, MH_SEQ_FLAGGED);
342 if (ctx->hdrs[l]->replied)
344 mhs_set (&mhs, i, MH_SEQ_REPLIED);
349 /* write out the new sequences */
351 mhs_write_one_sequence (nfp, &mhs, MH_SEQ_UNSEEN, NONULL (MhUnseen));
353 mhs_write_one_sequence (nfp, &mhs, MH_SEQ_FLAGGED, NONULL (MhFlagged));
355 mhs_write_one_sequence (nfp, &mhs, MH_SEQ_REPLIED, NONULL (MhReplied));
357 mhs_free_sequences (&mhs);
360 /* try to commit the changes - no guarantee here */
364 if (safe_rename (tmpfname, sequences) != 0)
366 /* report an error? */
373 static void mh_sequences_add_one (CONTEXT * ctx, int n, short unseen,
374 short flagged, short replied)
376 short unseen_done = 0;
377 short flagged_done = 0;
378 short replied_done = 0;
380 FILE *ofp = NULL, *nfp = NULL;
383 char sequences[_POSIX_PATH_MAX];
385 char seq_unseen[STRING];
386 char seq_replied[STRING];
387 char seq_flagged[STRING];
393 if (mh_mkstemp (ctx, &nfp, &tmpfname) == -1)
396 snprintf (seq_unseen, sizeof (seq_unseen), "%s:", NONULL (MhUnseen));
397 snprintf (seq_replied, sizeof (seq_replied), "%s:", NONULL (MhReplied));
398 snprintf (seq_flagged, sizeof (seq_flagged), "%s:", NONULL (MhFlagged));
400 snprintf (sequences, sizeof (sequences), "%s/.mh_sequences", ctx->path);
401 if ((ofp = fopen (sequences, "r")))
403 while ((buff = mutt_read_line (buff, &sz, ofp, &line)))
405 if (unseen && !strncmp (buff, seq_unseen, mutt_strlen (seq_unseen)))
407 fprintf (nfp, "%s %d\n", buff, n);
411 && !strncmp (buff, seq_flagged, mutt_strlen (seq_flagged)))
413 fprintf (nfp, "%s %d\n", buff, n);
417 && !strncmp (buff, seq_replied, mutt_strlen (seq_replied)))
419 fprintf (nfp, "%s %d\n", buff, n);
423 fprintf (nfp, "%s\n", buff);
429 if (!unseen_done && unseen)
430 fprintf (nfp, "%s: %d\n", NONULL (MhUnseen), n);
431 if (!flagged_done && flagged)
432 fprintf (nfp, "%s: %d\n", NONULL (MhFlagged), n);
433 if (!replied_done && replied)
434 fprintf (nfp, "%s: %d\n", NONULL (MhReplied), n);
439 if (safe_rename (tmpfname, sequences) != 0)
445 static void mh_update_maildir (struct maildir *md, struct mh_sequences *mhs)
451 for (; md; md = md->next)
453 if ((p = strrchr (md->h->path, '/')))
459 f = mhs_check (mhs, i);
461 md->h->read = (f & MH_SEQ_UNSEEN) ? 0 : 1;
462 md->h->flagged = (f & MH_SEQ_FLAGGED) ? 1 : 0;
463 md->h->replied = (f & MH_SEQ_REPLIED) ? 1 : 0;
467 /* maildir support */
469 static void maildir_free_entry (struct maildir **md)
474 FREE (&(*md)->canon_fname);
476 mutt_free_header (&(*md)->h);
481 static void maildir_free_maildir (struct maildir **md)
483 struct maildir *p, *q;
488 for (p = *md; p; p = q)
491 maildir_free_entry (&p);
495 static void maildir_parse_flags (HEADER * h, const char *path)
503 if ((p = strrchr (path, ':')) != NULL && mutt_strncmp (p + 1, "2,", 2) == 0)
507 mutt_str_replace (&h->maildir_flags, p);
508 q = h->maildir_flags;
524 case 'R': /* replied */
529 case 'T': /* trashed */
542 if (q == h->maildir_flags)
543 FREE (&h->maildir_flags);
548 static void maildir_update_mtime (CONTEXT * ctx)
550 char buf[_POSIX_PATH_MAX];
553 if (ctx->magic == M_MAILDIR)
555 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, "cur");
556 if (stat (buf, &st) == 0)
557 ctx->mtime_cur = st.st_mtime;
558 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, "new");
562 snprintf (buf, sizeof (buf), "%s/.mh_sequences", ctx->path);
563 if (stat (buf, &st) == 0)
564 ctx->mtime_cur = st.st_mtime;
566 strfcpy (buf, ctx->path, sizeof (buf));
569 if (stat (buf, &st) == 0)
570 ctx->mtime = st.st_mtime;
574 * Actually parse a maildir message. This may also be used to fill
575 * out a fake header structure generated by lazy maildir parsing.
577 static HEADER *maildir_parse_message (int magic, const char *fname,
578 int is_old, HEADER * _h)
584 if ((f = fopen (fname, "r")) != NULL)
587 h = mutt_new_header ();
588 h->env = mutt_read_rfc822_header (f, h, 0, 0);
590 fstat (fileno (f), &st);
594 h->received = h->date_sent;
596 if (h->content->length <= 0)
597 h->content->length = st.st_size - h->content->offset;
601 if (magic == M_MAILDIR)
604 * maildir stores its flags in the filename, so ignore the
605 * flags in the header of the message
609 maildir_parse_flags (h, fname);
617 * Note that this routine will _not_ modify the context given by
620 * It's used in the first parsing pass on maildir and MH folders.
621 * In the MH case, this means full parsing of the folder. In the
622 * maildir case, it means that we only look at flags, and create a
623 * fake HEADER structure, which may later be filled in by
624 * maildir_parse_message(), when called from
625 * maildir_delayed_parsing().
629 static int maildir_parse_entry (CONTEXT * ctx, struct maildir ***last,
630 const char *subdir, const char *fname,
631 int *count, int is_old, ino_t inode)
633 struct maildir *entry;
635 char buf[_POSIX_PATH_MAX];
638 snprintf (buf, sizeof (buf), "%s/%s/%s", ctx->path, subdir, fname);
640 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, fname);
642 if (ctx->magic == M_MH)
643 h = maildir_parse_message (ctx->magic, buf, is_old, NULL);
646 h = mutt_new_header ();
648 maildir_parse_flags (h, buf);
656 if (!ctx->quiet && ReadInc && ((*count % ReadInc) == 0 || *count == 1))
657 mutt_message (_("Reading %s... %d"), ctx->path, *count);
662 snprintf (buf, sizeof (buf), "%s/%s", subdir, fname);
663 h->path = safe_strdup (buf);
666 h->path = safe_strdup (fname);
668 entry = safe_calloc (sizeof (struct maildir), 1);
670 entry->header_parsed = (ctx->magic == M_MH);
671 entry->inode = inode;
673 *last = &entry->next;
683 /* Ignore the garbage files. A valid MH message consists of only
684 * digits. Deleted message get moved to a filename with a comma before
688 int mh_valid_message (const char *s)
692 if (!isdigit ((unsigned char) *s))
698 static int maildir_parse_dir (CONTEXT * ctx, struct maildir ***last,
699 const char *subdir, int *count)
703 char buf[_POSIX_PATH_MAX];
708 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, subdir);
709 is_old = (mutt_strcmp ("cur", subdir) == 0);
712 strfcpy (buf, ctx->path, sizeof (buf));
714 if ((dirp = opendir (buf)) == NULL)
717 while ((de = readdir (dirp)) != NULL)
720 if ((ctx->magic == M_MH && !mh_valid_message (de->d_name))
721 || (ctx->magic == M_MAILDIR && *de->d_name == '.'))
724 /* FOO - really ignore the return value? */
727 (debugfile, "%s:%d: parsing %s\n", __FILE__, __LINE__,
729 maildir_parse_entry (ctx, last, subdir, de->d_name, count, is_old,
737 static int maildir_add_to_context (CONTEXT * ctx, struct maildir *md)
739 int oldmsgcount = ctx->msgcount;
744 dprint (2, (debugfile, "%s:%d maildir_add_to_context(): Considering %s\n",
745 __FILE__, __LINE__, NONULL (md->canon_fname)));
751 "%s:%d Adding header structure. Flags: %s%s%s%s%s\n", __FILE__,
752 __LINE__, md->h->flagged ? "f" : "", md->h->deleted ? "D" : "",
753 md->h->replied ? "r" : "", md->h->old ? "O" : "",
754 md->h->read ? "R" : ""));
755 if (ctx->msgcount == ctx->hdrmax)
756 mx_alloc_memory (ctx);
758 ctx->hdrs[ctx->msgcount] = md->h;
759 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
761 md->h->content->length + md->h->content->offset -
762 md->h->content->hdr_offset;
770 if (ctx->msgcount > oldmsgcount)
772 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
778 static int maildir_move_to_context (CONTEXT * ctx, struct maildir **md)
781 r = maildir_add_to_context (ctx, *md);
782 maildir_free_maildir (md);
787 * Merge two maildir lists according to the inode numbers.
789 static struct maildir* maildir_merge_inode (struct maildir *left,
790 struct maildir *right)
792 struct maildir* head;
793 struct maildir* tail;
797 if (left->inode < right->inode)
818 while (left && right)
820 if (left->inode < right->inode)
846 * Sort maildir list according to inode.
848 static struct maildir* maildir_sort_inode(struct maildir* list)
850 struct maildir* left = list;
851 struct maildir* right = list;
853 if (!list || !list->next)
859 while (list && list->next)
862 list = list->next->next;
869 left = maildir_sort_inode(left);
870 right = maildir_sort_inode(right);
871 return maildir_merge_inode(left, right);
876 * This function does the second parsing pass for a maildir-style
880 void maildir_delayed_parsing (CONTEXT * ctx, struct maildir *md)
883 char fn[_POSIX_PATH_MAX];
885 for (p = md; p; p = p->next)
886 if (p && p->h && !p->header_parsed)
888 snprintf (fn, sizeof (fn), "%s/%s", ctx->path, p->h->path);
889 if (maildir_parse_message (ctx->magic, fn, p->h->old, p->h))
890 p->header_parsed = 1;
892 mutt_free_header (&p->h);
898 /* Read a MH/maildir style mailbox.
901 * ctx [IN/OUT] context for this mailbox
902 * subdir [IN] NULL for MH mailboxes, otherwise the subdir of the
903 * maildir mailbox to read from
905 int mh_read_dir (CONTEXT * ctx, const char *subdir)
908 struct mh_sequences mhs;
909 struct maildir **last;
913 memset (&mhs, 0, sizeof (mhs));
915 maildir_update_mtime (ctx);
920 if (maildir_parse_dir (ctx, &last, subdir, &count) == -1)
923 if (ctx->magic == M_MH)
925 mh_read_sequences (&mhs, ctx->path);
926 mh_update_maildir (md, &mhs);
927 mhs_free_sequences (&mhs);
930 md = maildir_sort_inode(md);
932 if (ctx->magic == M_MAILDIR)
933 maildir_delayed_parsing (ctx, md);
935 maildir_move_to_context (ctx, &md);
939 /* read a maildir style mailbox */
940 int maildir_read_dir (CONTEXT * ctx)
942 /* maildir looks sort of like MH, except that there are two subdirectories
943 * of the main folder path from which to read messages
945 if (mh_read_dir (ctx, "new") == -1 || mh_read_dir (ctx, "cur") == -1)
952 * Open a new (temporary) message in an MH folder.
955 int mh_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr)
957 return mh_mkstemp (dest, &msg->fp, &msg->path);
960 int ch_compar (const void *a, const void *b)
962 return (int)( *((const char *) a) - *((const char *) b));
965 static void maildir_flags (char *dest, size_t destlen, HEADER * hdr)
970 * The maildir specification requires that all files in the cur
971 * subdirectory have the :unique string appeneded, regardless of whether
972 * or not there are any flags. If .old is set, we know that this message
973 * will end up in the cur directory, so we include it in the following
974 * test even though there is no associated flag.
977 if (hdr && (hdr->flagged || hdr->replied || hdr->read || hdr->deleted || hdr->old || hdr->maildir_flags))
979 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, 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];
1010 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));
1027 snprintf (path, _POSIX_PATH_MAX, "%s/tmp/%s.%ld.%d_%d.%s%s",
1028 dest->path, subdir, time (NULL), getpid (), Counter++,
1029 NONULL (Hostname), suffix);
1031 dprint (2, (debugfile, "maildir_open_new_message (): Trying %s.\n",
1034 if ((fd = open (path, O_WRONLY | O_EXCL | O_CREAT, 0600)) == -1)
1036 if (errno != EEXIST)
1044 dprint (2, (debugfile, "maildir_open_new_message (): Success.\n"));
1045 msg->path = safe_strdup (path);
1050 if ((msg->fp = fdopen (fd, "w")) == NULL)
1064 * Commit a message to a maildir folder.
1066 * msg->path contains the file name of a file in tmp/. We take the
1067 * flags from this file's name.
1069 * ctx is the mail folder we commit to.
1071 * hdr is a header structure to which we write the message's new
1072 * file name. This is used in the mh and maildir folder synch
1073 * routines. When this routine is invoked from mx_commit_message,
1076 * msg->path looks like this:
1078 * tmp/{cur,new}.mutt-HOSTNAME-PID-COUNTER:flags
1080 * See also maildir_open_new_message().
1084 int maildir_commit_message (CONTEXT * ctx, MESSAGE * msg, HEADER * hdr)
1088 char path[_POSIX_PATH_MAX];
1089 char full[_POSIX_PATH_MAX];
1092 if (safe_fclose (&msg->fp) != 0)
1095 /* extract the subdir */
1096 s = strrchr (msg->path, '/') + 1;
1097 strfcpy (subdir, s, 4);
1099 /* extract the flags */
1100 if ((s = strchr (s, ':')))
1101 strfcpy (suffix, s, sizeof (suffix));
1105 /* construct a new file name. */
1108 snprintf (path, _POSIX_PATH_MAX, "%s/%ld.%d_%d.%s%s", subdir,
1109 time (NULL), getpid (), Counter++, NONULL (Hostname), suffix);
1110 snprintf (full, _POSIX_PATH_MAX, "%s/%s", ctx->path, path);
1112 dprint (2, (debugfile, "maildir_commit_message (): renaming %s to %s.\n",
1115 if (safe_rename (msg->path, full) == 0)
1118 mutt_str_replace (&hdr->path, path);
1122 * Adjust the mtime on the file to match the time at which this
1123 * message was received. Currently this is only set when copying
1124 * messages between mailboxes, so we test to ensure that it is
1131 ut.actime = msg->received;
1132 ut.modtime = msg->received;
1133 if (utime (full, &ut))
1135 mutt_perror (_("maildir_commit_message(): unable to set time on file"));
1142 else if (errno != EEXIST)
1144 mutt_perror (ctx->path);
1151 * commit a message to an MH folder.
1156 static int _mh_commit_message (CONTEXT * ctx, MESSAGE * msg, HEADER * hdr,
1162 unsigned int n, hi = 0;
1163 char path[_POSIX_PATH_MAX];
1166 if (safe_fclose (&msg->fp) != 0)
1169 if ((dirp = opendir (ctx->path)) == NULL)
1171 mutt_perror (ctx->path);
1175 /* figure out what the next message number is */
1176 while ((de = readdir (dirp)) != NULL)
1184 if (!isdigit ((unsigned char) *cp))
1198 * Now try to rename the file to the proper name.
1200 * Note: We may have to try multiple times, until we find a free
1207 snprintf (tmp, sizeof (tmp), "%d", hi);
1208 snprintf (path, sizeof (path), "%s/%s", ctx->path, tmp);
1209 if (safe_rename (msg->path, path) == 0)
1212 mutt_str_replace (&hdr->path, tmp);
1216 else if (errno != EEXIST)
1218 mutt_perror (ctx->path);
1223 mh_sequences_add_one (ctx, hi, !msg->flags.read, msg->flags.flagged,
1224 msg->flags.replied);
1228 int mh_commit_message (CONTEXT * ctx, MESSAGE * msg, HEADER * hdr)
1230 return _mh_commit_message (ctx, msg, hdr, 1);
1234 /* Sync a message in an MH folder.
1236 * This code is also used for attachment deletion in maildir
1240 static int mh_rewrite_message (CONTEXT * ctx, int msgno)
1242 HEADER *h = ctx->hdrs[msgno];
1247 char oldpath[_POSIX_PATH_MAX];
1248 char newpath[_POSIX_PATH_MAX];
1249 char partpath[_POSIX_PATH_MAX];
1251 long old_body_offset = h->content->offset;
1252 long old_body_length = h->content->length;
1253 long old_hdr_lines = h->lines;
1255 if ((dest = mx_open_new_message (ctx, h, 0)) == NULL)
1258 if ((rc = mutt_copy_message (dest->fp, ctx, h,
1259 M_CM_UPDATE, CH_UPDATE | CH_UPDATE_LEN)) == 0)
1261 snprintf (oldpath, _POSIX_PATH_MAX, "%s/%s", ctx->path, h->path);
1262 strfcpy (partpath, h->path, _POSIX_PATH_MAX);
1264 if (ctx->magic == M_MAILDIR)
1265 rc = maildir_commit_message (ctx, dest, h);
1267 rc = _mh_commit_message (ctx, dest, h, 0);
1269 mx_close_message (&dest);
1278 * Try to move the new message to the old place.
1281 * This is important when we are just updating flags.
1283 * Note that there is a race condition against programs which
1284 * use the first free slot instead of the maximum message
1285 * number. Mutt does _not_ behave like this.
1287 * Anyway, if this fails, the message is in the folder, so
1288 * all what happens is that a concurrently runnung mutt will
1289 * lose flag modifications.
1292 if (ctx->magic == M_MH && rc == 0)
1294 snprintf (newpath, _POSIX_PATH_MAX, "%s/%s", ctx->path, h->path);
1295 if ((rc = safe_rename (newpath, oldpath)) == 0)
1296 mutt_str_replace (&h->path, partpath);
1300 mx_close_message (&dest);
1302 if (rc == -1 && restore)
1304 h->content->offset = old_body_offset;
1305 h->content->length = old_body_length;
1306 h->lines = old_hdr_lines;
1309 mutt_free_body (&h->content->parts);
1313 static int mh_sync_message (CONTEXT * ctx, int msgno)
1315 HEADER *h = ctx->hdrs[msgno];
1317 if (h->attach_del || h->refs_changed || h->irt_changed)
1318 if (mh_rewrite_message (ctx, msgno) != 0)
1324 static int maildir_sync_message (CONTEXT * ctx, int msgno)
1326 HEADER *h = ctx->hdrs[msgno];
1328 if (h->attach_del || h->refs_changed || h->irt_changed)
1330 /* when doing attachment deletion/rethreading, fall back to the MH case. */
1331 if (mh_rewrite_message (ctx, msgno) != 0)
1336 /* we just have to rename the file. */
1338 char newpath[_POSIX_PATH_MAX];
1339 char partpath[_POSIX_PATH_MAX];
1340 char fullpath[_POSIX_PATH_MAX];
1341 char oldpath[_POSIX_PATH_MAX];
1345 if ((p = strrchr (h->path, '/')) == NULL)
1349 "maildir_sync_message: %s: unable to find subdir!\n",
1354 strfcpy (newpath, p, sizeof (newpath));
1356 /* kill the previous flags */
1357 if ((p = strchr (newpath, ':')) != NULL)
1360 maildir_flags (suffix, sizeof (suffix), h);
1362 snprintf (partpath, sizeof (partpath), "%s/%s%s",
1363 (h->read || h->old) ? "cur" : "new", newpath, suffix);
1364 snprintf (fullpath, sizeof (fullpath), "%s/%s", ctx->path, partpath);
1365 snprintf (oldpath, sizeof (oldpath), "%s/%s", ctx->path, h->path);
1367 if (mutt_strcmp (fullpath, oldpath) == 0)
1369 /* message hasn't really changed */
1373 /* record that the message is possibly marked as trashed on disk */
1374 h->trash = h->deleted;
1376 if (rename (oldpath, fullpath) != 0)
1378 mutt_perror ("rename");
1381 mutt_str_replace (&h->path, partpath);
1386 int mh_sync_mailbox (CONTEXT * ctx, int *index_hint)
1388 char path[_POSIX_PATH_MAX], tmp[_POSIX_PATH_MAX];
1391 if (ctx->magic == M_MH)
1392 i = mh_check_mailbox (ctx, index_hint);
1394 i = maildir_check_mailbox (ctx, index_hint);
1399 for (i = 0; i < ctx->msgcount; i++)
1401 if (ctx->hdrs[i]->deleted
1402 && (ctx->magic != M_MAILDIR || !option (OPTMAILDIRTRASH)))
1404 snprintf (path, sizeof (path), "%s/%s", ctx->path, ctx->hdrs[i]->path);
1405 if (ctx->magic == M_MAILDIR
1406 || (option (OPTMHPURGE) && ctx->magic == M_MH))
1408 else if (ctx->magic == M_MH)
1410 /* MH just moves files out of the way when you delete them */
1411 if (*ctx->hdrs[i]->path != ',')
1413 snprintf (tmp, sizeof (tmp), "%s/,%s", ctx->path,
1414 ctx->hdrs[i]->path);
1421 else if (ctx->hdrs[i]->changed || ctx->hdrs[i]->attach_del ||
1422 (ctx->magic == M_MAILDIR
1423 && (option (OPTMAILDIRTRASH) || ctx->hdrs[i]->trash)
1424 && (ctx->hdrs[i]->deleted != ctx->hdrs[i]->trash)))
1426 if (ctx->magic == M_MAILDIR)
1428 if (maildir_sync_message (ctx, i) == -1)
1433 if (mh_sync_message (ctx, i) == -1)
1439 if (ctx->magic == M_MH)
1440 mh_update_sequences (ctx);
1442 /* XXX race condition? */
1444 maildir_update_mtime (ctx);
1446 /* adjust indices */
1450 for (i = 0, j = 0; i < ctx->msgcount; i++)
1452 if (!ctx->hdrs[i]->deleted
1453 || (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))
1454 ctx->hdrs[i]->index = j++;
1461 static char *maildir_canon_filename (char *dest, const char *src, size_t l)
1465 if ((t = strrchr (src, '/')))
1468 strfcpy (dest, src, l);
1469 if ((u = strrchr (dest, ':')))
1475 static void maildir_update_tables (CONTEXT *ctx, int *index_hint)
1481 if (Sort != SORT_ORDER)
1485 mutt_sort_headers (ctx, 1);
1489 old_count = ctx->msgcount;
1490 for (i = 0, j = 0; i < old_count; i++)
1492 if (ctx->hdrs[i]->active && index_hint && *index_hint == i)
1495 if (ctx->hdrs[i]->active)
1496 ctx->hdrs[i]->index = j++;
1499 mx_update_tables (ctx, 0);
1500 mutt_clear_threads (ctx);
1503 static void maildir_update_flags (CONTEXT *ctx, HEADER *o, HEADER *n)
1505 /* save the global state here so we can reset it at the
1506 * end of list block if required.
1508 int context_changed = ctx->changed;
1510 /* user didn't modify this message. alter the flags to
1511 * match the current state on disk. This may not actually
1512 * do anything, but we can't tell right now. mutt_set_flag()
1513 * will just ignore the call if the status bits are
1514 * already properly set.
1516 mutt_set_flag (ctx, o, M_FLAG, n->flagged);
1517 mutt_set_flag (ctx, o, M_REPLIED, n->replied);
1518 mutt_set_flag (ctx, o, M_READ, n->read);
1519 mutt_set_flag (ctx, o, M_OLD, n->old);
1521 /* mutt_set_flag() will set this, but we don't need to
1522 * sync the changes we made because we just updated the
1523 * context to match the current on-disk state of the
1528 /* if the mailbox was not modified before we made these
1529 * changes, unset the changed flag since nothing needs to
1532 if (!context_changed)
1537 /* This function handles arrival of new mail and reopening of
1538 * maildir folders. The basic idea here is we check to see if either
1539 * the new or cur subdirectories have changed, and if so, we scan them
1540 * for the list of files. We check for newly added messages, and
1541 * then merge the flags messages we already knew about. We don't treat
1542 * either subdirectory differently, as mail could be copied directly into
1543 * the cur directory from another agent.
1545 int maildir_check_mailbox (CONTEXT * ctx, int *index_hint)
1547 struct stat st_new; /* status of the "new" subdirectory */
1548 struct stat st_cur; /* status of the "cur" subdirectory */
1549 char buf[_POSIX_PATH_MAX];
1550 int changed = 0; /* bitmask representing which subdirectories
1551 have changed. 0x1 = new, 0x2 = cur */
1552 int occult = 0; /* messages were removed from the mailbox */
1553 int have_new = 0; /* messages were added to the mailbox */
1554 struct maildir *md; /* list of messages in the mailbox */
1555 struct maildir **last, *p;
1557 HASH *fnames; /* hash table for quickly looking up the base filename
1558 for a maildir message */
1560 /* XXX seems like this check belongs in mx_check_mailbox()
1563 if (!option (OPTCHECKNEW))
1566 snprintf (buf, sizeof (buf), "%s/new", ctx->path);
1567 if (stat (buf, &st_new) == -1)
1570 snprintf (buf, sizeof (buf), "%s/cur", ctx->path);
1571 if (stat (buf, &st_cur) == -1)
1574 /* determine which subdirectories need to be scanned */
1575 if (st_new.st_mtime > ctx->mtime)
1577 if (st_cur.st_mtime > ctx->mtime_cur)
1581 return 0; /* nothing to do */
1583 /* update the modification times on the mailbox */
1584 ctx->mtime_cur = st_cur.st_mtime;
1585 ctx->mtime = st_new.st_mtime;
1587 /* do a fast scan of just the filenames in
1588 * the subdirectories that have changed.
1593 maildir_parse_dir (ctx, &last, "new", NULL);
1595 maildir_parse_dir (ctx, &last, "cur", NULL);
1597 /* we create a hash table keyed off the canonical (sans flags) filename
1598 * of each message we scanned. This is used in the loop over the
1599 * existing messages below to do some correlation.
1601 fnames = hash_create (1031);
1603 for (p = md; p; p = p->next)
1605 maildir_canon_filename (buf, p->h->path, sizeof (buf));
1606 p->canon_fname = safe_strdup (buf);
1607 hash_insert (fnames, p->canon_fname, p, 0);
1610 /* check for modifications and adjust flags */
1611 for (i = 0; i < ctx->msgcount; i++)
1613 ctx->hdrs[i]->active = 0;
1614 maildir_canon_filename (buf, ctx->hdrs[i]->path, sizeof (buf));
1615 p = hash_find (fnames, buf);
1618 /* message already exists, merge flags */
1619 ctx->hdrs[i]->active = 1;
1621 /* check to see if the message has moved to a different
1622 * subdirectory. If so, update the associated filename.
1624 if (mutt_strcmp (ctx->hdrs[i]->path, p->h->path))
1625 mutt_str_replace (&ctx->hdrs[i]->path, p->h->path);
1627 /* if the user hasn't modified the flags on this message, update
1628 * the flags we just detected.
1630 if (!ctx->hdrs[i]->changed)
1631 maildir_update_flags (ctx, ctx->hdrs[i], p->h);
1633 if (ctx->hdrs[i]->deleted == ctx->hdrs[i]->trash)
1634 ctx->hdrs[i]->deleted = p->h->deleted;
1635 ctx->hdrs[i]->trash = p->h->trash;
1637 /* this is a duplicate of an existing header, so remove it */
1638 mutt_free_header (&p->h);
1640 /* This message was not in the list of messages we just scanned.
1641 * Check to see if we have enough information to know if the
1642 * message has disappeared out from underneath us.
1644 else if (((changed & 1) && (!strncmp (ctx->hdrs[i]->path, "new/", 4))) ||
1645 ((changed & 2) && (!strncmp (ctx->hdrs[i]->path, "cur/", 4))))
1647 /* This message disappeared, so we need to simulate a "reopen"
1648 * event. We know it disappeared because we just scanned the
1649 * subdirectory it used to reside in.
1655 /* This message resides in a subdirectory which was not
1656 * modified, so we assume that it is still present and
1659 ctx->hdrs[i]->active = 1;
1663 /* destroy the file name hash */
1664 hash_destroy (&fnames, NULL);
1666 /* If we didn't just get new mail, update the tables. */
1668 maildir_update_tables (ctx, index_hint);
1670 /* do any delayed parsing we need to do. */
1671 maildir_delayed_parsing (ctx, md);
1673 /* Incorporate new messages */
1674 have_new = maildir_move_to_context (ctx, &md);
1676 return occult ? M_REOPENED : (have_new ? M_NEW_MAIL : 0);
1680 * This function handles arrival of new mail and reopening of
1681 * mh/maildir folders. Things are getting rather complex because we
1682 * don't have a well-defined "mailbox order", so the tricks from
1683 * mbox.c and mx.c won't work here.
1685 * Don't change this code unless you _really_ understand what
1690 int mh_check_mailbox (CONTEXT * ctx, int *index_hint)
1692 char buf[_POSIX_PATH_MAX];
1693 struct stat st, st_cur;
1694 short modified = 0, have_new = 0, occult = 0;
1695 struct maildir *md, *p;
1696 struct maildir **last = NULL;
1697 struct mh_sequences mhs;
1701 if (!option (OPTCHECKNEW))
1704 strfcpy (buf, ctx->path, sizeof (buf));
1705 if (stat (buf, &st) == -1)
1708 /* create .mh_sequences when there isn't one. */
1709 snprintf (buf, sizeof (buf), "%s/.mh_sequences", ctx->path);
1710 if ((i = stat (buf, &st_cur) == -1) && errno == ENOENT)
1715 if (mh_mkstemp (ctx, &fp, &tmp) == 0)
1718 if (safe_rename (tmp, buf) == -1)
1724 if (i == -1 && stat (buf, &st_cur) == -1)
1727 if (st.st_mtime > ctx->mtime || st_cur.st_mtime > ctx->mtime_cur)
1733 ctx->mtime_cur = st_cur.st_mtime;
1734 ctx->mtime = st.st_mtime;
1736 memset (&mhs, 0, sizeof (mhs));
1740 maildir_parse_dir (ctx, &last, NULL, NULL);
1741 mh_read_sequences (&mhs, ctx->path);
1742 mh_update_maildir (md, &mhs);
1743 mhs_free_sequences (&mhs);
1745 /* check for modifications and adjust flags */
1746 fnames = hash_create (1031);
1748 for (p = md; p; p = p->next)
1749 hash_insert (fnames, p->h->path, p, 0);
1751 for (i = 0; i < ctx->msgcount; i++)
1753 ctx->hdrs[i]->active = 0;
1755 if ((p = hash_find (fnames, ctx->hdrs[i]->path)) && p->h &&
1756 (mbox_strict_cmp_headers (ctx->hdrs[i], p->h)))
1758 ctx->hdrs[i]->active = 1;
1759 /* found the right message */
1760 if (!ctx->hdrs[i]->changed)
1761 maildir_update_flags (ctx, ctx->hdrs[i], p->h);
1763 mutt_free_header (&p->h);
1765 else /* message has disappeared */
1769 /* destroy the file name hash */
1771 hash_destroy (&fnames, NULL);
1773 /* If we didn't just get new mail, update the tables. */
1775 maildir_update_tables (ctx, index_hint);
1777 /* Incorporate new messages */
1778 have_new = maildir_move_to_context (ctx, &md);
1780 return occult ? M_REOPENED : (have_new ? M_NEW_MAIL : 0);
1787 * These functions try to find a message in a maildir folder when it
1788 * has moved under our feet. Note that this code is rather expensive, but
1789 * then again, it's called rarely.
1792 FILE *_maildir_open_find_message (const char *folder, const char *unique,
1793 const char *subfolder)
1795 char dir[_POSIX_PATH_MAX];
1796 char tunique[_POSIX_PATH_MAX];
1797 char fname[_POSIX_PATH_MAX];
1805 snprintf (dir, sizeof (dir), "%s/%s", folder, subfolder);
1807 if ((dp = opendir (dir)) == NULL)
1813 while ((de = readdir (dp)))
1815 maildir_canon_filename (tunique, de->d_name, sizeof (tunique));
1817 if (!mutt_strcmp (tunique, unique))
1819 snprintf (fname, sizeof (fname), "%s/%s/%s", folder, subfolder,
1821 fp = fopen (fname, "r"); /* __FOPEN_CHECKED__ */
1833 FILE *maildir_open_find_message (const char *folder, const char *msg)
1835 char unique[_POSIX_PATH_MAX];
1838 static unsigned int new_hits = 0, cur_hits = 0; /* simple dynamic optimization */
1840 maildir_canon_filename (unique, msg, sizeof (unique));
1844 _maildir_open_find_message (folder, unique,
1845 new_hits > cur_hits ? "new" : "cur"))
1848 if (new_hits < UINT_MAX && cur_hits < UINT_MAX)
1850 new_hits += (new_hits > cur_hits ? 1 : 0);
1851 cur_hits += (new_hits > cur_hits ? 0 : 1);
1858 _maildir_open_find_message (folder, unique,
1859 new_hits > cur_hits ? "cur" : "new"))
1862 if (new_hits < UINT_MAX && cur_hits < UINT_MAX)
1864 new_hits += (new_hits > cur_hits ? 0 : 1);
1865 cur_hits += (new_hits > cur_hits ? 1 : 0);
1877 * 1 if there are no messages in the mailbox
1878 * 0 if there are messages in the mailbox
1881 int maildir_check_empty (const char *path)
1885 int r = 1; /* assume empty until we find a message */
1886 char realpath[_POSIX_PATH_MAX];
1889 /* Strategy here is to look for any file not beginning with a period */
1892 /* we do "cur" on the first iteration since its more likely that we'll
1893 * find old messages without having to scan both subdirs
1895 snprintf (realpath, sizeof (realpath), "%s/%s", path,
1896 iter == 0 ? "cur" : "new");
1897 if ((dp = opendir (realpath)) == NULL)
1899 while ((de = readdir (dp)))
1901 if (*de->d_name != '.')
1909 } while (r && iter < 2);
1916 * 1 if there are no messages in the mailbox
1917 * 0 if there are messages in the mailbox
1920 int mh_check_empty (const char *path)
1924 int r = 1; /* assume empty until we find a message */
1926 if ((dp = opendir (path)) == NULL)
1928 while ((de = readdir (dp)))
1930 if (mh_valid_message (de->d_name))