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 if (!ctx->quiet && ReadInc && ((count % ReadInc) == 0 || count == 1))
915 mutt_message (_("Reading %s... %d"), ctx->path, count);
916 snprintf (fn, sizeof (fn), "%s/%s", ctx->path, p->h->path);
919 if (option(OPTHCACHEVERIFY)) {
920 ret = stat(fn, &lastchanged);
922 lastchanged.st_mtime = 0;
926 if (data != NULL && !ret && lastchanged.st_mtime <= when->tv_sec)
928 p->h = mutt_hcache_restore ((unsigned char *)data, &p->h);
929 maildir_parse_flags (p->h, fn);
932 if (maildir_parse_message (ctx->magic, fn, p->h->old, p->h))
934 p->header_parsed = 1;
935 maildir_parse_flags (p->h, fn);
937 mutt_hcache_store (hc, p->h->path + 3, p->h, 0, &maildir_hcache_keylen);
940 mutt_free_header (&p->h);
946 mutt_hcache_close (hc);
950 /* Read a MH/maildir style mailbox.
953 * ctx [IN/OUT] context for this mailbox
954 * subdir [IN] NULL for MH mailboxes, otherwise the subdir of the
955 * maildir mailbox to read from
957 int mh_read_dir (CONTEXT * ctx, const char *subdir)
960 struct mh_sequences mhs;
961 struct maildir **last;
965 memset (&mhs, 0, sizeof (mhs));
967 maildir_update_mtime (ctx);
972 if (maildir_parse_dir (ctx, &last, subdir, &count) == -1)
975 if (ctx->magic == M_MH)
977 mh_read_sequences (&mhs, ctx->path);
978 mh_update_maildir (md, &mhs);
979 mhs_free_sequences (&mhs);
982 md = maildir_sort_inode(md);
984 if (ctx->magic == M_MAILDIR)
985 maildir_delayed_parsing (ctx, md);
987 maildir_move_to_context (ctx, &md);
991 /* read a maildir style mailbox */
992 int maildir_read_dir (CONTEXT * ctx)
994 /* maildir looks sort of like MH, except that there are two subdirectories
995 * of the main folder path from which to read messages
997 if (mh_read_dir (ctx, "new") == -1 || mh_read_dir (ctx, "cur") == -1)
1004 * Open a new (temporary) message in an MH folder.
1007 int mh_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr)
1009 return mh_mkstemp (dest, &msg->fp, &msg->path);
1012 int ch_compar (const void *a, const void *b)
1014 return (int)( *((const char *) a) - *((const char *) b));
1017 static void maildir_flags (char *dest, size_t destlen, HEADER * hdr)
1022 * The maildir specification requires that all files in the cur
1023 * subdirectory have the :unique string appeneded, regardless of whether
1024 * or not there are any flags. If .old is set, we know that this message
1025 * will end up in the cur directory, so we include it in the following
1026 * test even though there is no associated flag.
1029 if (hdr && (hdr->flagged || hdr->replied || hdr->read || hdr->deleted || hdr->old || hdr->maildir_flags))
1031 char tmp[LONG_STRING];
1032 snprintf (tmp, sizeof (tmp),
1034 hdr->flagged ? "F" : "",
1035 hdr->replied ? "R" : "",
1036 hdr->read ? "S" : "", hdr->deleted ? "T" : "",
1037 NONULL(hdr->maildir_flags));
1038 if (hdr->maildir_flags)
1039 qsort (tmp, strlen (tmp), 1, ch_compar);
1040 snprintf (dest, destlen, ":2,%s", tmp);
1046 * Open a new (temporary) message in a maildir folder.
1048 * Note that this uses _almost_ the maildir file name format, but
1049 * with a {cur,new} prefix.
1053 int maildir_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr)
1056 char path[_POSIX_PATH_MAX];
1062 short deleted = hdr->deleted;
1065 maildir_flags (suffix, sizeof (suffix), hdr);
1067 hdr->deleted = deleted;
1072 if (hdr && (hdr->read || hdr->old))
1073 strfcpy (subdir, "cur", sizeof (subdir));
1075 strfcpy (subdir, "new", sizeof (subdir));
1079 snprintf (path, _POSIX_PATH_MAX, "%s/tmp/%s.%ld.%u_%d.%s%s",
1080 dest->path, subdir, time (NULL), (unsigned int)getpid (),
1081 Counter++, NONULL (Hostname), suffix);
1083 dprint (2, (debugfile, "maildir_open_new_message (): Trying %s.\n",
1087 if ((fd = open (path, O_WRONLY | O_EXCL | O_CREAT, 0666)) == -1)
1089 if (errno != EEXIST)
1097 dprint (2, (debugfile, "maildir_open_new_message (): Success.\n"));
1098 msg->path = safe_strdup (path);
1103 if ((msg->fp = fdopen (fd, "w")) == NULL)
1117 * Commit a message to a maildir folder.
1119 * msg->path contains the file name of a file in tmp/. We take the
1120 * flags from this file's name.
1122 * ctx is the mail folder we commit to.
1124 * hdr is a header structure to which we write the message's new
1125 * file name. This is used in the mh and maildir folder synch
1126 * routines. When this routine is invoked from mx_commit_message,
1129 * msg->path looks like this:
1131 * tmp/{cur,new}.mutt-HOSTNAME-PID-COUNTER:flags
1133 * See also maildir_open_new_message().
1137 int maildir_commit_message (CONTEXT * ctx, MESSAGE * msg, HEADER * hdr)
1141 char path[_POSIX_PATH_MAX];
1142 char full[_POSIX_PATH_MAX];
1145 if (safe_fclose (&msg->fp) != 0)
1148 /* extract the subdir */
1149 s = strrchr (msg->path, '/') + 1;
1150 strfcpy (subdir, s, 4);
1152 /* extract the flags */
1153 if ((s = strchr (s, ':')))
1154 strfcpy (suffix, s, sizeof (suffix));
1158 /* construct a new file name. */
1161 snprintf (path, _POSIX_PATH_MAX, "%s/%ld.%u_%d.%s%s", subdir,
1162 time (NULL), (unsigned int)getpid (), Counter++,
1163 NONULL (Hostname), suffix);
1164 snprintf (full, _POSIX_PATH_MAX, "%s/%s", ctx->path, path);
1166 dprint (2, (debugfile, "maildir_commit_message (): renaming %s to %s.\n",
1169 if (safe_rename (msg->path, full) == 0)
1172 mutt_str_replace (&hdr->path, path);
1176 * Adjust the mtime on the file to match the time at which this
1177 * message was received. Currently this is only set when copying
1178 * messages between mailboxes, so we test to ensure that it is
1185 ut.actime = msg->received;
1186 ut.modtime = msg->received;
1187 if (utime (full, &ut))
1189 mutt_perror (_("maildir_commit_message(): unable to set time on file"));
1196 else if (errno != EEXIST)
1198 mutt_perror (ctx->path);
1205 * commit a message to an MH folder.
1210 static int _mh_commit_message (CONTEXT * ctx, MESSAGE * msg, HEADER * hdr,
1216 unsigned int n, hi = 0;
1217 char path[_POSIX_PATH_MAX];
1220 if (safe_fclose (&msg->fp) != 0)
1223 if ((dirp = opendir (ctx->path)) == NULL)
1225 mutt_perror (ctx->path);
1229 /* figure out what the next message number is */
1230 while ((de = readdir (dirp)) != NULL)
1238 if (!isdigit ((unsigned char) *cp))
1252 * Now try to rename the file to the proper name.
1254 * Note: We may have to try multiple times, until we find a free
1261 snprintf (tmp, sizeof (tmp), "%d", hi);
1262 snprintf (path, sizeof (path), "%s/%s", ctx->path, tmp);
1263 if (safe_rename (msg->path, path) == 0)
1266 mutt_str_replace (&hdr->path, tmp);
1270 else if (errno != EEXIST)
1272 mutt_perror (ctx->path);
1277 mh_sequences_add_one (ctx, hi, !msg->flags.read, msg->flags.flagged,
1278 msg->flags.replied);
1282 int mh_commit_message (CONTEXT * ctx, MESSAGE * msg, HEADER * hdr)
1284 return _mh_commit_message (ctx, msg, hdr, 1);
1288 /* Sync a message in an MH folder.
1290 * This code is also used for attachment deletion in maildir
1294 static int mh_rewrite_message (CONTEXT * ctx, int msgno)
1296 HEADER *h = ctx->hdrs[msgno];
1301 char oldpath[_POSIX_PATH_MAX];
1302 char newpath[_POSIX_PATH_MAX];
1303 char partpath[_POSIX_PATH_MAX];
1305 long old_body_offset = h->content->offset;
1306 long old_body_length = h->content->length;
1307 long old_hdr_lines = h->lines;
1309 if ((dest = mx_open_new_message (ctx, h, 0)) == NULL)
1312 if ((rc = mutt_copy_message (dest->fp, ctx, h,
1313 M_CM_UPDATE, CH_UPDATE | CH_UPDATE_LEN)) == 0)
1315 snprintf (oldpath, _POSIX_PATH_MAX, "%s/%s", ctx->path, h->path);
1316 strfcpy (partpath, h->path, _POSIX_PATH_MAX);
1318 if (ctx->magic == M_MAILDIR)
1319 rc = maildir_commit_message (ctx, dest, h);
1321 rc = _mh_commit_message (ctx, dest, h, 0);
1323 mx_close_message (&dest);
1332 * Try to move the new message to the old place.
1335 * This is important when we are just updating flags.
1337 * Note that there is a race condition against programs which
1338 * use the first free slot instead of the maximum message
1339 * number. Mutt does _not_ behave like this.
1341 * Anyway, if this fails, the message is in the folder, so
1342 * all what happens is that a concurrently runnung mutt will
1343 * lose flag modifications.
1346 if (ctx->magic == M_MH && rc == 0)
1348 snprintf (newpath, _POSIX_PATH_MAX, "%s/%s", ctx->path, h->path);
1349 if ((rc = safe_rename (newpath, oldpath)) == 0)
1350 mutt_str_replace (&h->path, partpath);
1354 mx_close_message (&dest);
1356 if (rc == -1 && restore)
1358 h->content->offset = old_body_offset;
1359 h->content->length = old_body_length;
1360 h->lines = old_hdr_lines;
1363 mutt_free_body (&h->content->parts);
1367 static int mh_sync_message (CONTEXT * ctx, int msgno)
1369 HEADER *h = ctx->hdrs[msgno];
1371 if (h->attach_del || h->refs_changed || h->irt_changed)
1372 if (mh_rewrite_message (ctx, msgno) != 0)
1378 static int maildir_sync_message (CONTEXT * ctx, int msgno)
1380 HEADER *h = ctx->hdrs[msgno];
1382 if (h->attach_del || h->refs_changed || h->irt_changed)
1384 /* when doing attachment deletion/rethreading, fall back to the MH case. */
1385 if (mh_rewrite_message (ctx, msgno) != 0)
1390 /* we just have to rename the file. */
1392 char newpath[_POSIX_PATH_MAX];
1393 char partpath[_POSIX_PATH_MAX];
1394 char fullpath[_POSIX_PATH_MAX];
1395 char oldpath[_POSIX_PATH_MAX];
1399 if ((p = strrchr (h->path, '/')) == NULL)
1403 "maildir_sync_message: %s: unable to find subdir!\n",
1408 strfcpy (newpath, p, sizeof (newpath));
1410 /* kill the previous flags */
1411 if ((p = strchr (newpath, ':')) != NULL)
1414 maildir_flags (suffix, sizeof (suffix), h);
1416 snprintf (partpath, sizeof (partpath), "%s/%s%s",
1417 (h->read || h->old) ? "cur" : "new", newpath, suffix);
1418 snprintf (fullpath, sizeof (fullpath), "%s/%s", ctx->path, partpath);
1419 snprintf (oldpath, sizeof (oldpath), "%s/%s", ctx->path, h->path);
1421 if (mutt_strcmp (fullpath, oldpath) == 0)
1423 /* message hasn't really changed */
1427 /* record that the message is possibly marked as trashed on disk */
1428 h->trash = h->deleted;
1430 if (rename (oldpath, fullpath) != 0)
1432 mutt_perror ("rename");
1435 mutt_str_replace (&h->path, partpath);
1440 int mh_sync_mailbox (CONTEXT * ctx, int *index_hint)
1442 char path[_POSIX_PATH_MAX], tmp[_POSIX_PATH_MAX];
1446 #endif /* USE_HCACHE */
1448 if (ctx->magic == M_MH)
1449 i = mh_check_mailbox (ctx, index_hint);
1451 i = maildir_check_mailbox (ctx, index_hint);
1457 if (ctx->magic == M_MAILDIR)
1458 hc = mutt_hcache_open(HeaderCache, ctx->path);
1459 #endif /* USE_HCACHE */
1461 for (i = 0; i < ctx->msgcount; i++)
1463 if (ctx->hdrs[i]->deleted
1464 && (ctx->magic != M_MAILDIR || !option (OPTMAILDIRTRASH)))
1466 snprintf (path, sizeof (path), "%s/%s", ctx->path, ctx->hdrs[i]->path);
1467 if (ctx->magic == M_MAILDIR
1468 || (option (OPTMHPURGE) && ctx->magic == M_MH))
1471 if (ctx->magic == M_MAILDIR)
1472 mutt_hcache_delete (hc, ctx->hdrs[i]->path + 3, &maildir_hcache_keylen);
1473 #endif /* USE_HCACHE */
1476 else if (ctx->magic == M_MH)
1478 /* MH just moves files out of the way when you delete them */
1479 if (*ctx->hdrs[i]->path != ',')
1481 snprintf (tmp, sizeof (tmp), "%s/,%s", ctx->path,
1482 ctx->hdrs[i]->path);
1489 else if (ctx->hdrs[i]->changed || ctx->hdrs[i]->attach_del ||
1490 (ctx->magic == M_MAILDIR
1491 && (option (OPTMAILDIRTRASH) || ctx->hdrs[i]->trash)
1492 && (ctx->hdrs[i]->deleted != ctx->hdrs[i]->trash)))
1494 if (ctx->magic == M_MAILDIR)
1496 if (maildir_sync_message (ctx, i) == -1)
1501 if (mh_sync_message (ctx, i) == -1)
1508 if (ctx->magic == M_MAILDIR)
1509 mutt_hcache_close (hc);
1510 #endif /* USE_HCACHE */
1512 if (ctx->magic == M_MH)
1513 mh_update_sequences (ctx);
1515 /* XXX race condition? */
1517 maildir_update_mtime (ctx);
1519 /* adjust indices */
1523 for (i = 0, j = 0; i < ctx->msgcount; i++)
1525 if (!ctx->hdrs[i]->deleted
1526 || (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))
1527 ctx->hdrs[i]->index = j++;
1535 if (ctx->magic == M_MAILDIR)
1536 mutt_hcache_close (hc);
1537 #endif /* USE_HCACHE */
1541 static char *maildir_canon_filename (char *dest, const char *src, size_t l)
1545 if ((t = strrchr (src, '/')))
1548 strfcpy (dest, src, l);
1549 if ((u = strrchr (dest, ':')))
1555 static void maildir_update_tables (CONTEXT *ctx, int *index_hint)
1561 if (Sort != SORT_ORDER)
1565 mutt_sort_headers (ctx, 1);
1569 old_count = ctx->msgcount;
1570 for (i = 0, j = 0; i < old_count; i++)
1572 if (ctx->hdrs[i]->active && index_hint && *index_hint == i)
1575 if (ctx->hdrs[i]->active)
1576 ctx->hdrs[i]->index = j++;
1579 mx_update_tables (ctx, 0);
1580 mutt_clear_threads (ctx);
1583 static void maildir_update_flags (CONTEXT *ctx, HEADER *o, HEADER *n)
1585 /* save the global state here so we can reset it at the
1586 * end of list block if required.
1588 int context_changed = ctx->changed;
1590 /* user didn't modify this message. alter the flags to
1591 * match the current state on disk. This may not actually
1592 * do anything, but we can't tell right now. mutt_set_flag()
1593 * will just ignore the call if the status bits are
1594 * already properly set.
1596 mutt_set_flag (ctx, o, M_FLAG, n->flagged);
1597 mutt_set_flag (ctx, o, M_REPLIED, n->replied);
1598 mutt_set_flag (ctx, o, M_READ, n->read);
1599 mutt_set_flag (ctx, o, M_OLD, n->old);
1601 /* mutt_set_flag() will set this, but we don't need to
1602 * sync the changes we made because we just updated the
1603 * context to match the current on-disk state of the
1608 /* if the mailbox was not modified before we made these
1609 * changes, unset the changed flag since nothing needs to
1612 if (!context_changed)
1617 /* This function handles arrival of new mail and reopening of
1618 * maildir folders. The basic idea here is we check to see if either
1619 * the new or cur subdirectories have changed, and if so, we scan them
1620 * for the list of files. We check for newly added messages, and
1621 * then merge the flags messages we already knew about. We don't treat
1622 * either subdirectory differently, as mail could be copied directly into
1623 * the cur directory from another agent.
1625 int maildir_check_mailbox (CONTEXT * ctx, int *index_hint)
1627 struct stat st_new; /* status of the "new" subdirectory */
1628 struct stat st_cur; /* status of the "cur" subdirectory */
1629 char buf[_POSIX_PATH_MAX];
1630 int changed = 0; /* bitmask representing which subdirectories
1631 have changed. 0x1 = new, 0x2 = cur */
1632 int occult = 0; /* messages were removed from the mailbox */
1633 int have_new = 0; /* messages were added to the mailbox */
1634 struct maildir *md; /* list of messages in the mailbox */
1635 struct maildir **last, *p;
1637 HASH *fnames; /* hash table for quickly looking up the base filename
1638 for a maildir message */
1640 /* XXX seems like this check belongs in mx_check_mailbox()
1643 if (!option (OPTCHECKNEW))
1646 snprintf (buf, sizeof (buf), "%s/new", ctx->path);
1647 if (stat (buf, &st_new) == -1)
1650 snprintf (buf, sizeof (buf), "%s/cur", ctx->path);
1651 if (stat (buf, &st_cur) == -1)
1654 /* determine which subdirectories need to be scanned */
1655 if (st_new.st_mtime > ctx->mtime)
1657 if (st_cur.st_mtime > ctx->mtime_cur)
1661 return 0; /* nothing to do */
1663 /* update the modification times on the mailbox */
1664 ctx->mtime_cur = st_cur.st_mtime;
1665 ctx->mtime = st_new.st_mtime;
1667 /* do a fast scan of just the filenames in
1668 * the subdirectories that have changed.
1673 maildir_parse_dir (ctx, &last, "new", NULL);
1675 maildir_parse_dir (ctx, &last, "cur", NULL);
1677 /* we create a hash table keyed off the canonical (sans flags) filename
1678 * of each message we scanned. This is used in the loop over the
1679 * existing messages below to do some correlation.
1681 fnames = hash_create (1031);
1683 for (p = md; p; p = p->next)
1685 maildir_canon_filename (buf, p->h->path, sizeof (buf));
1686 p->canon_fname = safe_strdup (buf);
1687 hash_insert (fnames, p->canon_fname, p, 0);
1690 /* check for modifications and adjust flags */
1691 for (i = 0; i < ctx->msgcount; i++)
1693 ctx->hdrs[i]->active = 0;
1694 maildir_canon_filename (buf, ctx->hdrs[i]->path, sizeof (buf));
1695 p = hash_find (fnames, buf);
1698 /* message already exists, merge flags */
1699 ctx->hdrs[i]->active = 1;
1701 /* check to see if the message has moved to a different
1702 * subdirectory. If so, update the associated filename.
1704 if (mutt_strcmp (ctx->hdrs[i]->path, p->h->path))
1705 mutt_str_replace (&ctx->hdrs[i]->path, p->h->path);
1707 /* if the user hasn't modified the flags on this message, update
1708 * the flags we just detected.
1710 if (!ctx->hdrs[i]->changed)
1711 maildir_update_flags (ctx, ctx->hdrs[i], p->h);
1713 if (ctx->hdrs[i]->deleted == ctx->hdrs[i]->trash)
1714 ctx->hdrs[i]->deleted = p->h->deleted;
1715 ctx->hdrs[i]->trash = p->h->trash;
1717 /* this is a duplicate of an existing header, so remove it */
1718 mutt_free_header (&p->h);
1720 /* This message was not in the list of messages we just scanned.
1721 * Check to see if we have enough information to know if the
1722 * message has disappeared out from underneath us.
1724 else if (((changed & 1) && (!strncmp (ctx->hdrs[i]->path, "new/", 4))) ||
1725 ((changed & 2) && (!strncmp (ctx->hdrs[i]->path, "cur/", 4))))
1727 /* This message disappeared, so we need to simulate a "reopen"
1728 * event. We know it disappeared because we just scanned the
1729 * subdirectory it used to reside in.
1735 /* This message resides in a subdirectory which was not
1736 * modified, so we assume that it is still present and
1739 ctx->hdrs[i]->active = 1;
1743 /* destroy the file name hash */
1744 hash_destroy (&fnames, NULL);
1746 /* If we didn't just get new mail, update the tables. */
1748 maildir_update_tables (ctx, index_hint);
1750 /* do any delayed parsing we need to do. */
1751 maildir_delayed_parsing (ctx, md);
1753 /* Incorporate new messages */
1754 have_new = maildir_move_to_context (ctx, &md);
1756 return occult ? M_REOPENED : (have_new ? M_NEW_MAIL : 0);
1760 * This function handles arrival of new mail and reopening of
1761 * mh/maildir folders. Things are getting rather complex because we
1762 * don't have a well-defined "mailbox order", so the tricks from
1763 * mbox.c and mx.c won't work here.
1765 * Don't change this code unless you _really_ understand what
1770 int mh_check_mailbox (CONTEXT * ctx, int *index_hint)
1772 char buf[_POSIX_PATH_MAX];
1773 struct stat st, st_cur;
1774 short modified = 0, have_new = 0, occult = 0;
1775 struct maildir *md, *p;
1776 struct maildir **last = NULL;
1777 struct mh_sequences mhs;
1781 if (!option (OPTCHECKNEW))
1784 strfcpy (buf, ctx->path, sizeof (buf));
1785 if (stat (buf, &st) == -1)
1788 /* create .mh_sequences when there isn't one. */
1789 snprintf (buf, sizeof (buf), "%s/.mh_sequences", ctx->path);
1790 if ((i = stat (buf, &st_cur) == -1) && errno == ENOENT)
1795 if (mh_mkstemp (ctx, &fp, &tmp) == 0)
1798 if (safe_rename (tmp, buf) == -1)
1804 if (i == -1 && stat (buf, &st_cur) == -1)
1807 if (st.st_mtime > ctx->mtime || st_cur.st_mtime > ctx->mtime_cur)
1813 ctx->mtime_cur = st_cur.st_mtime;
1814 ctx->mtime = st.st_mtime;
1816 memset (&mhs, 0, sizeof (mhs));
1820 maildir_parse_dir (ctx, &last, NULL, NULL);
1821 mh_read_sequences (&mhs, ctx->path);
1822 mh_update_maildir (md, &mhs);
1823 mhs_free_sequences (&mhs);
1825 /* check for modifications and adjust flags */
1826 fnames = hash_create (1031);
1828 for (p = md; p; p = p->next)
1829 hash_insert (fnames, p->h->path, p, 0);
1831 for (i = 0; i < ctx->msgcount; i++)
1833 ctx->hdrs[i]->active = 0;
1835 if ((p = hash_find (fnames, ctx->hdrs[i]->path)) && p->h &&
1836 (mbox_strict_cmp_headers (ctx->hdrs[i], p->h)))
1838 ctx->hdrs[i]->active = 1;
1839 /* found the right message */
1840 if (!ctx->hdrs[i]->changed)
1841 maildir_update_flags (ctx, ctx->hdrs[i], p->h);
1843 mutt_free_header (&p->h);
1845 else /* message has disappeared */
1849 /* destroy the file name hash */
1851 hash_destroy (&fnames, NULL);
1853 /* If we didn't just get new mail, update the tables. */
1855 maildir_update_tables (ctx, index_hint);
1857 /* Incorporate new messages */
1858 have_new = maildir_move_to_context (ctx, &md);
1860 return occult ? M_REOPENED : (have_new ? M_NEW_MAIL : 0);
1867 * These functions try to find a message in a maildir folder when it
1868 * has moved under our feet. Note that this code is rather expensive, but
1869 * then again, it's called rarely.
1872 FILE *_maildir_open_find_message (const char *folder, const char *unique,
1873 const char *subfolder)
1875 char dir[_POSIX_PATH_MAX];
1876 char tunique[_POSIX_PATH_MAX];
1877 char fname[_POSIX_PATH_MAX];
1885 snprintf (dir, sizeof (dir), "%s/%s", folder, subfolder);
1887 if ((dp = opendir (dir)) == NULL)
1893 while ((de = readdir (dp)))
1895 maildir_canon_filename (tunique, de->d_name, sizeof (tunique));
1897 if (!mutt_strcmp (tunique, unique))
1899 snprintf (fname, sizeof (fname), "%s/%s/%s", folder, subfolder,
1901 fp = fopen (fname, "r"); /* __FOPEN_CHECKED__ */
1913 FILE *maildir_open_find_message (const char *folder, const char *msg)
1915 char unique[_POSIX_PATH_MAX];
1918 static unsigned int new_hits = 0, cur_hits = 0; /* simple dynamic optimization */
1920 maildir_canon_filename (unique, msg, sizeof (unique));
1924 _maildir_open_find_message (folder, unique,
1925 new_hits > cur_hits ? "new" : "cur"))
1928 if (new_hits < UINT_MAX && cur_hits < UINT_MAX)
1930 new_hits += (new_hits > cur_hits ? 1 : 0);
1931 cur_hits += (new_hits > cur_hits ? 0 : 1);
1938 _maildir_open_find_message (folder, unique,
1939 new_hits > cur_hits ? "cur" : "new"))
1942 if (new_hits < UINT_MAX && cur_hits < UINT_MAX)
1944 new_hits += (new_hits > cur_hits ? 0 : 1);
1945 cur_hits += (new_hits > cur_hits ? 1 : 0);
1957 * 1 if there are no messages in the mailbox
1958 * 0 if there are messages in the mailbox
1961 int maildir_check_empty (const char *path)
1965 int r = 1; /* assume empty until we find a message */
1966 char realpath[_POSIX_PATH_MAX];
1969 /* Strategy here is to look for any file not beginning with a period */
1972 /* we do "cur" on the first iteration since its more likely that we'll
1973 * find old messages without having to scan both subdirs
1975 snprintf (realpath, sizeof (realpath), "%s/%s", path,
1976 iter == 0 ? "cur" : "new");
1977 if ((dp = opendir (realpath)) == NULL)
1979 while ((de = readdir (dp)))
1981 if (*de->d_name != '.')
1989 } while (r && iter < 2);
1996 * 1 if there are no messages in the mailbox
1997 * 0 if there are messages in the mailbox
2000 int mh_check_empty (const char *path)
2004 int r = 1; /* assume empty until we find a message */
2006 if ((dp = opendir (path)) == NULL)
2008 while ((de = readdir (dp)))
2010 if (mh_valid_message (de->d_name))