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
16 #include <lib-lib/lib-lib.h>
19 #include <lib-ui/curses.h>
34 unsigned header_parsed:1;
43 /* mh_sequences support */
45 #define MH_SEQ_UNSEEN (1 << 0)
46 #define MH_SEQ_REPLIED (1 << 1)
47 #define MH_SEQ_FLAGGED (1 << 2)
50 static int maildir_check_empty (const char*);
51 static int maildir_check_mailbox (CONTEXT*, int*, int);
52 static int mh_check_mailbox (CONTEXT*, int*, int);
54 static void mhs_alloc (struct mh_sequences *mhs, int i)
59 if (i > mhs->max || !mhs->flags) {
61 p_realloc(&mhs->flags, newmax + 1);
62 for (j = mhs->max + 1; j <= newmax; j++)
69 static void mhs_free_sequences (struct mh_sequences *mhs)
71 p_delete(&mhs->flags);
74 static short mhs_check (struct mh_sequences *mhs, int i)
76 if (!mhs->flags || i > mhs->max)
82 static short mhs_set (struct mh_sequences *mhs, int i, short f)
93 static short mhs_unset (struct mh_sequences *mhs, int i, short f)
102 static void mh_read_token (char *t, int *first, int *last)
106 if ((p = strchr (t, '-'))) {
112 *first = *last = atoi (t);
115 static void mh_read_sequences (struct mh_sequences *mhs, const char *path)
126 char pathname[_POSIX_PATH_MAX];
128 snprintf (pathname, sizeof (pathname), "%s/.mh_sequences", path);
130 if (!(fp = fopen (pathname, "r")))
133 while ((buff = mutt_read_line (buff, &sz, fp, &line))) {
134 if (!(t = strtok (buff, " \t:")))
137 if (!m_strcmp(t, MhUnseen))
139 else if (!m_strcmp(t, MhFlagged))
141 else if (!m_strcmp(t, MhReplied))
143 else /* unknown sequence */
146 while ((t = strtok (NULL, " \t:"))) {
147 mh_read_token (t, &first, &last);
148 for (; first <= last; first++)
149 mhs_set (mhs, first, f);
157 int mh_buffy (const char *path)
160 struct mh_sequences mhs;
164 mh_read_sequences (&mhs, path);
165 for (i = 0; !r && i <= mhs.max; i++)
166 if (mhs_check (&mhs, i) & MH_SEQ_UNSEEN)
168 mhs_free_sequences (&mhs);
172 static int mh_mkstemp (CONTEXT * dest, FILE ** fp, char **tgt)
174 static int Counter = 0;
176 char path[_POSIX_PATH_MAX];
179 snprintf (path, _POSIX_PATH_MAX, "%s/.mutt-%s-%d-%d",
180 dest->path, NONULL (Hostname), (int) getpid (), Counter++);
182 if ((fd = open (path, O_WRONLY | O_EXCL | O_CREAT, 0666)) == -1) {
183 if (errno != EEXIST) {
189 *tgt = m_strdup(path);
194 if ((*fp = fdopen (fd, "w")) == NULL) {
204 static void mhs_write_one_sequence (FILE * fp, struct mh_sequences *mhs,
205 short f, const char *tag)
210 fprintf (fp, "%s:", tag);
215 for (i = 0; i <= mhs->max; i++) {
216 if ((mhs_check (mhs, i) & f)) {
222 else if (first >= 0) {
224 fprintf (fp, " %d", first);
226 fprintf (fp, " %d-%d", first, last);
235 fprintf (fp, " %d", first);
237 fprintf (fp, " %d-%d", first, last);
243 /* XXX - we don't currently remove deleted messages from sequences we don't know. Should we? */
245 static void mh_update_sequences (CONTEXT * ctx)
249 char sequences[_POSIX_PATH_MAX];
261 char seq_unseen[STRING];
262 char seq_replied[STRING];
263 char seq_flagged[STRING];
266 struct mh_sequences mhs;
270 snprintf (seq_unseen, sizeof (seq_unseen), "%s:", NONULL (MhUnseen));
271 snprintf (seq_replied, sizeof (seq_replied), "%s:", NONULL (MhReplied));
272 snprintf (seq_flagged, sizeof (seq_flagged), "%s:", NONULL (MhFlagged));
274 if (mh_mkstemp (ctx, &nfp, &tmpfname) != 0) {
279 snprintf (sequences, sizeof (sequences), "%s/.mh_sequences", ctx->path);
282 /* first, copy unknown sequences */
283 if ((ofp = fopen (sequences, "r"))) {
284 while ((buff = mutt_read_line (buff, &s, ofp, &l))) {
285 if (!m_strncmp(buff, seq_unseen, m_strlen(seq_unseen)))
287 if (!m_strncmp(buff, seq_flagged, m_strlen(seq_flagged)))
289 if (!m_strncmp(buff, seq_replied, m_strlen(seq_replied)))
292 fprintf (nfp, "%s\n", buff);
297 /* now, update our unseen, flagged, and replied sequences */
298 for (l = 0; l < ctx->msgcount; l++) {
299 if (ctx->hdrs[l]->deleted)
302 if ((p = strrchr (ctx->hdrs[l]->path, '/')))
305 p = ctx->hdrs[l]->path;
309 if (!ctx->hdrs[l]->read) {
310 mhs_set (&mhs, i, MH_SEQ_UNSEEN);
313 if (ctx->hdrs[l]->flagged) {
314 mhs_set (&mhs, i, MH_SEQ_FLAGGED);
317 if (ctx->hdrs[l]->replied) {
318 mhs_set (&mhs, i, MH_SEQ_REPLIED);
323 /* write out the new sequences */
325 mhs_write_one_sequence (nfp, &mhs, MH_SEQ_UNSEEN, NONULL (MhUnseen));
327 mhs_write_one_sequence (nfp, &mhs, MH_SEQ_FLAGGED, NONULL (MhFlagged));
329 mhs_write_one_sequence (nfp, &mhs, MH_SEQ_REPLIED, NONULL (MhReplied));
331 mhs_free_sequences (&mhs);
334 /* try to commit the changes - no guarantee here */
338 if (safe_rename (tmpfname, sequences) != 0) {
339 /* report an error? */
346 static void mh_sequences_add_one (CONTEXT * ctx, int n, short unseen,
347 short flagged, short replied)
349 short unseen_done = 0;
350 short flagged_done = 0;
351 short replied_done = 0;
353 FILE *ofp = NULL, *nfp = NULL;
356 char sequences[_POSIX_PATH_MAX];
358 char seq_unseen[STRING];
359 char seq_replied[STRING];
360 char seq_flagged[STRING];
366 if (mh_mkstemp (ctx, &nfp, &tmpfname) == -1)
369 snprintf (seq_unseen, sizeof (seq_unseen), "%s:", NONULL (MhUnseen));
370 snprintf (seq_replied, sizeof (seq_replied), "%s:", NONULL (MhReplied));
371 snprintf (seq_flagged, sizeof (seq_flagged), "%s:", NONULL (MhFlagged));
373 snprintf (sequences, sizeof (sequences), "%s/.mh_sequences", ctx->path);
374 if ((ofp = fopen (sequences, "r"))) {
375 while ((buff = mutt_read_line (buff, &sz, ofp, &line))) {
376 if (unseen && !strncmp (buff, seq_unseen, m_strlen(seq_unseen))) {
377 fprintf (nfp, "%s %d\n", buff, n);
381 && !strncmp (buff, seq_flagged, m_strlen(seq_flagged))) {
382 fprintf (nfp, "%s %d\n", buff, n);
386 && !strncmp (buff, seq_replied, m_strlen(seq_replied))) {
387 fprintf (nfp, "%s %d\n", buff, n);
391 fprintf (nfp, "%s\n", buff);
397 if (!unseen_done && unseen)
398 fprintf (nfp, "%s: %d\n", NONULL (MhUnseen), n);
399 if (!flagged_done && flagged)
400 fprintf (nfp, "%s: %d\n", NONULL (MhFlagged), n);
401 if (!replied_done && replied)
402 fprintf (nfp, "%s: %d\n", NONULL (MhReplied), n);
407 if (safe_rename (tmpfname, sequences) != 0)
413 static void mh_update_maildir (struct maildir *md, struct mh_sequences *mhs)
419 for (; md; md = md->next) {
420 if ((p = strrchr (md->h->path, '/')))
426 f = mhs_check (mhs, i);
428 md->h->read = (f & MH_SEQ_UNSEEN) ? 0 : 1;
429 md->h->flagged = (f & MH_SEQ_FLAGGED) ? 1 : 0;
430 md->h->replied = (f & MH_SEQ_REPLIED) ? 1 : 0;
434 /* maildir support */
436 static void maildir_free_entry (struct maildir **md)
441 p_delete(&(*md)->canon_fname);
443 header_delete(&(*md)->h);
448 static void maildir_free_maildir (struct maildir **md)
450 struct maildir *p, *q;
455 for (p = *md; p; p = q) {
457 maildir_free_entry (&p);
461 static void maildir_parse_flags (HEADER * h, const char *path)
469 if ((p = strrchr (path, ':')) != NULL && m_strncmp(p + 1, "2,", 2) == 0) {
472 m_strreplace(&h->maildir_flags, p);
473 q = h->maildir_flags;
487 case 'R': /* replied */
492 case 'T': /* trashed */
505 if (q == h->maildir_flags)
506 p_delete(&h->maildir_flags);
511 static void maildir_update_mtime (CONTEXT * ctx)
513 char buf[_POSIX_PATH_MAX];
516 if (ctx->magic == M_MAILDIR) {
517 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, "cur");
518 if (stat (buf, &st) == 0)
519 ctx->mtime_cur = st.st_mtime;
520 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, "new");
523 snprintf (buf, sizeof (buf), "%s/.mh_sequences", ctx->path);
524 if (stat (buf, &st) == 0)
525 ctx->mtime_cur = st.st_mtime;
527 m_strcpy(buf, sizeof(buf), ctx->path);
530 if (stat (buf, &st) == 0)
531 ctx->mtime = st.st_mtime;
535 * Actually parse a maildir message. This may also be used to fill
536 * out a fake header structure generated by lazy maildir parsing.
538 static HEADER *maildir_parse_message (int magic, const char *fname,
539 int is_old, HEADER * _h)
545 if ((f = fopen (fname, "r")) != NULL) {
548 h->env = mutt_read_rfc822_header (f, h, 0, 0);
550 fstat (fileno (f), &st);
554 h->received = h->date_sent;
556 if (h->content->length <= 0)
557 h->content->length = st.st_size - h->content->offset;
561 if (magic == M_MAILDIR) {
563 * maildir stores its flags in the filename, so ignore the
564 * flags in the header of the message
568 maildir_parse_flags (h, fname);
576 * Note that this routine will _not_ modify the context given by
579 * It's used in the first parsing pass on maildir and MH folders.
580 * In the MH case, this means full parsing of the folder. In the
581 * maildir case, it means that we only look at flags, and create a
582 * fake HEADER structure, which may later be filled in by
583 * maildir_parse_message(), when called from
584 * maildir_delayed_parsing().
588 static int maildir_parse_entry (CONTEXT * ctx, struct maildir ***last,
589 const char *subdir, const char *fname,
590 int *count, int is_old, ino_t inode __attribute__ ((unused)))
592 struct maildir *entry;
594 char buf[_POSIX_PATH_MAX];
597 snprintf (buf, sizeof (buf), "%s/%s/%s", ctx->path, subdir, fname);
599 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, fname);
601 if (ctx->magic == M_MH)
602 h = maildir_parse_message (ctx->magic, buf, is_old, NULL);
606 maildir_parse_flags (h, buf);
612 if (!ctx->quiet && ReadInc && ((*count % ReadInc) == 0 || *count == 1))
613 mutt_message (_("Reading %s... %d"), ctx->path, *count);
617 snprintf (buf, sizeof (buf), "%s/%s", subdir, fname);
618 h->path = m_strdup(buf);
621 h->path = m_strdup(fname);
623 entry = p_new(struct maildir, 1);
625 entry->header_parsed = (ctx->magic == M_MH);
627 *last = &entry->next;
637 /* Ignore the garbage files. A valid MH message consists of only
638 * digits. Deleted message get moved to a filename with a comma before
642 int mh_valid_message (const char *s)
645 if (!isdigit ((unsigned char) *s))
651 static int maildir_parse_dir (CONTEXT * ctx, struct maildir ***last,
652 const char *subdir, int *count)
656 char buf[_POSIX_PATH_MAX];
660 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, subdir);
661 is_old = (m_strcmp("cur", subdir) == 0);
664 m_strcpy(buf, sizeof(buf), ctx->path);
666 if ((dirp = opendir (buf)) == NULL)
669 while ((de = readdir (dirp)) != NULL) {
671 if ((ctx->magic == M_MH && !mh_valid_message (de->d_name))
672 || (ctx->magic == M_MAILDIR && *de->d_name == '.'))
675 /* FOO - really ignore the return value? */
677 maildir_parse_entry (ctx, last, subdir, de->d_name, count, is_old,
678 #if HAVE_DIRENT_D_INO
690 static int maildir_add_to_context (CONTEXT * ctx, struct maildir *md)
692 int oldmsgcount = ctx->msgcount;
697 if (ctx->msgcount == ctx->hdrmax)
698 mx_alloc_memory (ctx);
700 ctx->hdrs[ctx->msgcount] = md->h;
701 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
703 md->h->content->length + md->h->content->offset -
704 md->h->content->hdr_offset;
712 if (ctx->msgcount > oldmsgcount) {
713 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
719 static int maildir_move_to_context (CONTEXT * ctx, struct maildir **md)
723 r = maildir_add_to_context (ctx, *md);
724 maildir_free_maildir (md);
729 static ssize_t maildir_hcache_keylen (const char *fn)
731 return m_strchrnul(fn, ':') - fn;
736 * This function does the second parsing pass for a maildir-style
739 static void maildir_delayed_parsing (CONTEXT * ctx, struct maildir *md)
742 char fn[_POSIX_PATH_MAX];
748 struct timeval *when = NULL;
749 struct stat lastchanged;
752 hc = mutt_hcache_open (HeaderCache, ctx->path);
755 for (p = md, count = 0; p; p = p->next, count++) {
756 if (!(p && p->h && !p->header_parsed))
760 data = mutt_hcache_fetch (hc, p->h->path + 3, &maildir_hcache_keylen);
761 when = (struct timeval *) data;
764 if (!ctx->quiet && ReadInc && ((count % ReadInc) == 0 || count == 1))
765 mutt_message (_("Reading %s... %d"), ctx->path, count);
766 snprintf (fn, sizeof (fn), "%s/%s", ctx->path, p->h->path);
769 if (option (OPTHCACHEVERIFY)) {
770 ret = stat (fn, &lastchanged);
773 lastchanged.st_mtime = 0;
777 if (data != NULL && !ret && lastchanged.st_mtime <= when->tv_sec) {
778 p->h = mutt_hcache_restore ((unsigned char *) data, &p->h);
779 maildir_parse_flags (p->h, fn);
783 if (maildir_parse_message (ctx->magic, fn, p->h->old, p->h)) {
784 p->header_parsed = 1;
785 maildir_parse_flags (p->h, fn);
787 mutt_hcache_store (hc, p->h->path + 3, p->h, 0, &maildir_hcache_keylen);
791 header_delete(&p->h);
797 mutt_hcache_close (hc);
801 /* Read a MH/maildir style mailbox.
804 * ctx [IN/OUT] context for this mailbox
805 * subdir [IN] NULL for MH mailboxes, otherwise the subdir of the
806 * maildir mailbox to read from
808 static int _mh_read_dir (CONTEXT * ctx, const char *subdir)
811 struct mh_sequences mhs;
812 struct maildir **last;
818 maildir_update_mtime (ctx);
823 if (maildir_parse_dir (ctx, &last, subdir, &count) == -1)
826 if (ctx->magic == M_MH) {
827 mh_read_sequences (&mhs, ctx->path);
828 mh_update_maildir (md, &mhs);
829 mhs_free_sequences (&mhs);
832 if (ctx->magic == M_MAILDIR)
833 maildir_delayed_parsing (ctx, md);
835 maildir_move_to_context (ctx, &md);
839 static int mh_read_dir (CONTEXT* ctx) {
840 return (_mh_read_dir (ctx, NULL));
843 /* read a maildir style mailbox */
844 static int maildir_read_dir (CONTEXT * ctx)
846 /* maildir looks sort of like MH, except that there are two subdirectories
847 * of the main folder path from which to read messages
849 if (_mh_read_dir (ctx, "new") == -1 || _mh_read_dir (ctx, "cur") == -1)
856 * Open a new (temporary) message in an MH folder.
859 static int mh_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr __attribute__ ((unused)))
861 return mh_mkstemp (dest, &msg->fp, &msg->path);
864 static int ch_compar (const void *a, const void *b)
866 return (int) (*((const char *) a) - *((const char *) b));
869 static void maildir_flags (char *dest, ssize_t destlen, HEADER * hdr)
874 * The maildir specification requires that all files in the cur
875 * subdirectory have the :unique string appeneded, regardless of whether
876 * or not there are any flags. If .old is set, we know that this message
877 * will end up in the cur directory, so we include it in the following
878 * test even though there is no associated flag.
882 && (hdr->flagged || hdr->replied || hdr->read || hdr->deleted
883 || hdr->old || hdr->maildir_flags)) {
884 char tmp[LONG_STRING];
886 snprintf (tmp, sizeof (tmp),
888 hdr->flagged ? "F" : "",
889 hdr->replied ? "R" : "",
890 hdr->read ? "S" : "", hdr->deleted ? "T" : "",
891 NONULL (hdr->maildir_flags));
892 if (hdr->maildir_flags)
893 qsort (tmp, m_strlen(tmp), 1, ch_compar);
894 snprintf (dest, destlen, ":2,%s", tmp);
900 * Open a new (temporary) message in a maildir folder.
902 * Note that this uses _almost_ the maildir file name format, but
903 * with a {cur,new} prefix.
907 static int maildir_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr)
909 static int Counter = 0;
911 char path[_POSIX_PATH_MAX];
916 short deleted = hdr->deleted;
920 maildir_flags (suffix, sizeof (suffix), hdr);
922 hdr->deleted = deleted;
927 if (hdr && (hdr->read || hdr->old))
928 m_strcpy(subdir, sizeof(subdir), "cur");
930 m_strcpy(subdir, sizeof(subdir), "new");
933 snprintf (path, _POSIX_PATH_MAX, "%s/tmp/%s.%ld.%u_%d.%s%s",
934 dest->path, subdir, (long) time (NULL),
935 (unsigned int) getpid (), Counter++, NONULL (Hostname), suffix);
938 if ((fd = open (path, O_WRONLY | O_EXCL | O_CREAT, 0666)) == -1) {
939 if (errno != EEXIST) {
944 msg->path = m_strdup(path);
949 if ((msg->fp = fdopen (fd, "w")) == NULL) {
950 p_delete(&msg->path);
962 * Commit a message to a maildir folder.
964 * msg->path contains the file name of a file in tmp/. We take the
965 * flags from this file's name.
967 * ctx is the mail folder we commit to.
969 * hdr is a header structure to which we write the message's new
970 * file name. This is used in the mh and maildir folder synch
971 * routines. When this routine is invoked from mx_commit_message,
974 * msg->path looks like this:
976 * tmp/{cur,new}.mutt-HOSTNAME-PID-COUNTER:flags
978 * See also maildir_open_new_message().
982 static int maildir_commit_message (MESSAGE * msg, CONTEXT * ctx, HEADER * hdr)
984 static int Counter = 0;
987 char path[_POSIX_PATH_MAX];
988 char full[_POSIX_PATH_MAX];
991 if (m_fclose(&msg->fp) != 0)
994 /* extract the subdir */
995 s = strrchr (msg->path, '/') + 1;
996 m_strcpy(subdir, sizeof(subdir), s);
998 /* extract the flags */
999 if ((s = strchr (s, ':')))
1000 m_strcpy(suffix, sizeof(suffix), s);
1004 /* construct a new file name. */
1006 snprintf (path, _POSIX_PATH_MAX, "%s/%ld.%u_%d.%s%s", subdir,
1007 (long) time (NULL), (unsigned int) getpid (), Counter++,
1008 NONULL (Hostname), suffix);
1009 snprintf (full, _POSIX_PATH_MAX, "%s/%s", ctx->path, path);
1011 if (safe_rename (msg->path, full) == 0) {
1013 m_strreplace(&hdr->path, path);
1014 p_delete(&msg->path);
1017 * Adjust the mtime on the file to match the time at which this
1018 * message was received. Currently this is only set when copying
1019 * messages between mailboxes, so we test to ensure that it is
1022 if (msg->received) {
1025 ut.actime = msg->received;
1026 ut.modtime = msg->received;
1027 if (utime (full, &ut)) {
1029 ("maildir_commit_message(): unable to set time on file"));
1036 else if (errno != EEXIST) {
1037 mutt_perror (ctx->path);
1044 * commit a message to an MH folder.
1049 static int _mh_commit_message (MESSAGE * msg, CONTEXT * ctx, HEADER * hdr,
1055 unsigned int n, hi = 0;
1056 char path[_POSIX_PATH_MAX];
1059 if (m_fclose(&msg->fp) != 0)
1062 if ((dirp = opendir (ctx->path)) == NULL) {
1063 mutt_perror (ctx->path);
1067 /* figure out what the next message number is */
1068 while ((de = readdir (dirp)) != NULL) {
1074 if (!isdigit ((unsigned char) *cp))
1087 * Now try to rename the file to the proper name.
1089 * Note: We may have to try multiple times, until we find a free
1095 snprintf (tmp, sizeof (tmp), "%d", hi);
1096 snprintf (path, sizeof (path), "%s/%s", ctx->path, tmp);
1097 if (safe_rename (msg->path, path) == 0) {
1099 m_strreplace(&hdr->path, tmp);
1100 p_delete(&msg->path);
1103 else if (errno != EEXIST) {
1104 mutt_perror (ctx->path);
1109 mh_sequences_add_one (ctx, hi, !msg->flags.read, msg->flags.flagged,
1110 msg->flags.replied);
1114 static int mh_commit_message (MESSAGE * msg, CONTEXT * ctx, HEADER * hdr) {
1115 return _mh_commit_message (msg, ctx, hdr, 1);
1118 /* Sync a message in an MH folder.
1120 * This code is also used for attachment deletion in maildir
1124 static int mh_rewrite_message (CONTEXT * ctx, int msgno)
1126 HEADER *h = ctx->hdrs[msgno];
1131 char oldpath[_POSIX_PATH_MAX];
1132 char newpath[_POSIX_PATH_MAX];
1133 char partpath[_POSIX_PATH_MAX];
1135 long old_body_offset = h->content->offset;
1136 long old_body_length = h->content->length;
1137 long old_hdr_lines = h->lines;
1139 if ((dest = mx_open_new_message (ctx, h, 0)) == NULL)
1142 if ((rc = mutt_copy_message (dest->fp, ctx, h,
1143 M_CM_UPDATE, CH_UPDATE | CH_UPDATE_LEN)) == 0)
1145 snprintf (oldpath, sizeof(oldpath), "%s/%s", ctx->path, h->path);
1146 m_strcpy(partpath, sizeof(partpath), h->path);
1148 if (ctx->magic == M_MAILDIR)
1149 rc = maildir_commit_message (dest, ctx, h);
1151 rc = _mh_commit_message (dest, ctx, h, 0);
1153 mx_close_message (&dest);
1161 * Try to move the new message to the old place.
1164 * This is important when we are just updating flags.
1166 * Note that there is a race condition against programs which
1167 * use the first free slot instead of the maximum message
1168 * number. Mutt does _not_ behave like this.
1170 * Anyway, if this fails, the message is in the folder, so
1171 * all what happens is that a concurrently runnung mutt will
1172 * lose flag modifications.
1175 if (ctx->magic == M_MH && rc == 0) {
1176 snprintf (newpath, _POSIX_PATH_MAX, "%s/%s", ctx->path, h->path);
1177 if ((rc = safe_rename (newpath, oldpath)) == 0)
1178 m_strreplace(&h->path, partpath);
1182 mx_close_message (&dest);
1184 if (rc == -1 && restore) {
1185 h->content->offset = old_body_offset;
1186 h->content->length = old_body_length;
1187 h->lines = old_hdr_lines;
1190 body_list_wipe(&h->content->parts);
1194 static int mh_sync_message (CONTEXT * ctx, int msgno)
1196 HEADER *h = ctx->hdrs[msgno];
1198 if (h->attach_del ||
1199 (h->env && (h->env->refs_changed || h->env->irt_changed)))
1200 if (mh_rewrite_message (ctx, msgno) != 0)
1206 static int maildir_sync_message (CONTEXT * ctx, int msgno)
1208 HEADER *h = ctx->hdrs[msgno];
1210 if (h->attach_del ||
1211 (h->env && (h->env->refs_changed || h->env->irt_changed))) {
1212 /* when doing attachment deletion/rethreading, fall back to the MH case. */
1213 if (mh_rewrite_message (ctx, msgno) != 0)
1217 /* we just have to rename the file. */
1219 char newpath[_POSIX_PATH_MAX];
1220 char partpath[_POSIX_PATH_MAX];
1221 char fullpath[_POSIX_PATH_MAX];
1222 char oldpath[_POSIX_PATH_MAX];
1226 if ((p = strrchr (h->path, '/')) == NULL) {
1230 m_strcpy(newpath, sizeof(newpath), p);
1232 /* kill the previous flags */
1233 if ((p = strchr (newpath, ':')) != NULL)
1236 maildir_flags (suffix, sizeof (suffix), h);
1238 snprintf (partpath, sizeof (partpath), "%s/%s%s",
1239 (h->read || h->old) ? "cur" : "new", newpath, suffix);
1240 snprintf (fullpath, sizeof (fullpath), "%s/%s", ctx->path, partpath);
1241 snprintf (oldpath, sizeof (oldpath), "%s/%s", ctx->path, h->path);
1243 if (m_strcmp(fullpath, oldpath) == 0) {
1244 /* message hasn't really changed */
1248 /* record that the message is possibly marked as trashed on disk */
1249 h->trash = h->deleted;
1251 if (rename (oldpath, fullpath) != 0) {
1252 mutt_perror ("rename");
1255 m_strreplace(&h->path, partpath);
1260 static int mh_sync_mailbox (CONTEXT * ctx, int unused __attribute__ ((unused)), int *index_hint)
1262 char path[_POSIX_PATH_MAX], tmp[_POSIX_PATH_MAX];
1267 #endif /* USE_HCACHE */
1269 if (ctx->magic == M_MH)
1270 i = mh_check_mailbox (ctx, index_hint, 0);
1272 i = maildir_check_mailbox (ctx, index_hint, 0);
1278 if (ctx->magic == M_MAILDIR)
1279 hc = mutt_hcache_open (HeaderCache, ctx->path);
1280 #endif /* USE_HCACHE */
1282 for (i = 0; i < ctx->msgcount; i++) {
1283 if (ctx->hdrs[i]->deleted
1284 && (ctx->magic != M_MAILDIR || !option (OPTMAILDIRTRASH))) {
1285 snprintf (path, sizeof (path), "%s/%s", ctx->path, ctx->hdrs[i]->path);
1286 if (ctx->magic == M_MAILDIR
1287 || (option (OPTMHPURGE) && ctx->magic == M_MH)) {
1289 if (ctx->magic == M_MAILDIR)
1290 mutt_hcache_delete (hc, ctx->hdrs[i]->path + 3,
1291 &maildir_hcache_keylen);
1292 #endif /* USE_HCACHE */
1295 else if (ctx->magic == M_MH) {
1296 /* MH just moves files out of the way when you delete them */
1297 if (*ctx->hdrs[i]->path != ',') {
1298 snprintf (tmp, sizeof (tmp), "%s/,%s", ctx->path,
1299 ctx->hdrs[i]->path);
1306 else if (ctx->hdrs[i]->changed || ctx->hdrs[i]->attach_del ||
1307 (ctx->magic == M_MAILDIR
1308 && (option (OPTMAILDIRTRASH) || ctx->hdrs[i]->trash)
1309 && (ctx->hdrs[i]->deleted != ctx->hdrs[i]->trash))) {
1310 if (ctx->magic == M_MAILDIR) {
1311 if (maildir_sync_message (ctx, i) == -1)
1315 if (mh_sync_message (ctx, i) == -1)
1322 if (ctx->magic == M_MAILDIR)
1323 mutt_hcache_close (hc);
1324 #endif /* USE_HCACHE */
1326 if (ctx->magic == M_MH)
1327 mh_update_sequences (ctx);
1329 /* XXX race condition? */
1331 maildir_update_mtime (ctx);
1333 /* adjust indices */
1336 for (i = 0, j = 0; i < ctx->msgcount; i++) {
1337 if (!ctx->hdrs[i]->deleted
1338 || (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))
1339 ctx->hdrs[i]->index = j++;
1347 if (ctx->magic == M_MAILDIR)
1348 mutt_hcache_close (hc);
1349 #endif /* USE_HCACHE */
1353 static char *maildir_canon_filename (char *dest, const char *src, ssize_t l)
1357 if ((t = strrchr (src, '/')))
1360 m_strcpy(dest, l, src);
1361 if ((u = strrchr (dest, ':')))
1367 static void maildir_update_tables (CONTEXT * ctx, int *index_hint)
1373 if (Sort != SORT_ORDER) {
1376 mutt_sort_headers (ctx, 1);
1380 old_count = ctx->msgcount;
1381 for (i = 0, j = 0; i < old_count; i++) {
1382 if (ctx->hdrs[i]->active && index_hint && *index_hint == i)
1385 if (ctx->hdrs[i]->active)
1386 ctx->hdrs[i]->index = j++;
1389 mx_update_tables (ctx, 0);
1390 mutt_clear_threads (ctx);
1393 static void maildir_update_flags (CONTEXT * ctx, HEADER * o, HEADER * n)
1395 /* save the global state here so we can reset it at the
1396 * end of list block if required.
1398 int context_changed = ctx->changed;
1400 /* user didn't modify this message. alter the flags to
1401 * match the current state on disk. This may not actually
1402 * do anything, but we can't tell right now. mutt_set_flag()
1403 * will just ignore the call if the status bits are
1404 * already properly set.
1406 mutt_set_flag (ctx, o, M_FLAG, n->flagged);
1407 mutt_set_flag (ctx, o, M_REPLIED, n->replied);
1408 mutt_set_flag (ctx, o, M_READ, n->read);
1409 mutt_set_flag (ctx, o, M_OLD, n->old);
1411 /* mutt_set_flag() will set this, but we don't need to
1412 * sync the changes we made because we just updated the
1413 * context to match the current on-disk state of the
1418 /* if the mailbox was not modified before we made these
1419 * changes, unset the changed flag since nothing needs to
1422 if (!context_changed)
1427 /* This function handles arrival of new mail and reopening of
1428 * maildir folders. The basic idea here is we check to see if either
1429 * the new or cur subdirectories have changed, and if so, we scan them
1430 * for the list of files. We check for newly added messages, and
1431 * then merge the flags messages we already knew about. We don't treat
1432 * either subdirectory differently, as mail could be copied directly into
1433 * the cur directory from another agent.
1435 static int maildir_check_mailbox (CONTEXT * ctx, int *index_hint, int unused __attribute__ ((unused)))
1437 struct stat st_new; /* status of the "new" subdirectory */
1438 struct stat st_cur; /* status of the "cur" subdirectory */
1439 char buf[_POSIX_PATH_MAX];
1440 int changed = 0; /* bitmask representing which subdirectories
1441 have changed. 0x1 = new, 0x2 = cur */
1442 int occult = 0; /* messages were removed from the mailbox */
1443 int have_new = 0; /* messages were added to the mailbox */
1444 struct maildir *md; /* list of messages in the mailbox */
1445 struct maildir **last, *p;
1447 HASH *fnames; /* hash table for quickly looking up the base filename
1448 for a maildir message */
1450 if (!option (OPTCHECKNEW))
1453 snprintf (buf, sizeof (buf), "%s/new", ctx->path);
1454 if (stat (buf, &st_new) == -1)
1457 snprintf (buf, sizeof (buf), "%s/cur", ctx->path);
1458 if (stat (buf, &st_cur) == -1)
1461 /* determine which subdirectories need to be scanned */
1462 if (st_new.st_mtime > ctx->mtime)
1464 if (st_cur.st_mtime > ctx->mtime_cur)
1468 return 0; /* nothing to do */
1470 /* update the modification times on the mailbox */
1471 ctx->mtime_cur = st_cur.st_mtime;
1472 ctx->mtime = st_new.st_mtime;
1474 /* do a fast scan of just the filenames in
1475 * the subdirectories that have changed.
1480 maildir_parse_dir (ctx, &last, "new", NULL);
1482 maildir_parse_dir (ctx, &last, "cur", NULL);
1484 /* we create a hash table keyed off the canonical (sans flags) filename
1485 * of each message we scanned. This is used in the loop over the
1486 * existing messages below to do some correlation.
1488 fnames = hash_create (1031);
1490 for (p = md; p; p = p->next) {
1491 maildir_canon_filename (buf, p->h->path, sizeof (buf));
1492 p->canon_fname = m_strdup(buf);
1493 hash_insert (fnames, p->canon_fname, p, 0);
1496 /* check for modifications and adjust flags */
1497 for (i = 0; i < ctx->msgcount; i++) {
1498 ctx->hdrs[i]->active = 0;
1499 maildir_canon_filename (buf, ctx->hdrs[i]->path, sizeof (buf));
1500 p = hash_find (fnames, buf);
1502 /* message already exists, merge flags */
1503 ctx->hdrs[i]->active = 1;
1505 /* check to see if the message has moved to a different
1506 * subdirectory. If so, update the associated filename.
1508 if (m_strcmp(ctx->hdrs[i]->path, p->h->path))
1509 m_strreplace(&ctx->hdrs[i]->path, p->h->path);
1511 /* if the user hasn't modified the flags on this message, update
1512 * the flags we just detected.
1514 if (!ctx->hdrs[i]->changed)
1515 maildir_update_flags (ctx, ctx->hdrs[i], p->h);
1517 if (ctx->hdrs[i]->deleted == ctx->hdrs[i]->trash)
1518 ctx->hdrs[i]->deleted = p->h->deleted;
1519 ctx->hdrs[i]->trash = p->h->trash;
1521 /* this is a duplicate of an existing header, so remove it */
1522 header_delete(&p->h);
1524 /* This message was not in the list of messages we just scanned.
1525 * Check to see if we have enough information to know if the
1526 * message has disappeared out from underneath us.
1528 else if (((changed & 1) && (!strncmp (ctx->hdrs[i]->path, "new/", 4))) ||
1529 ((changed & 2) && (!strncmp (ctx->hdrs[i]->path, "cur/", 4)))) {
1530 /* This message disappeared, so we need to simulate a "reopen"
1531 * event. We know it disappeared because we just scanned the
1532 * subdirectory it used to reside in.
1537 /* This message resides in a subdirectory which was not
1538 * modified, so we assume that it is still present and
1541 ctx->hdrs[i]->active = 1;
1545 /* destroy the file name hash */
1546 hash_destroy (&fnames, NULL);
1548 /* If we didn't just get new mail, update the tables. */
1550 maildir_update_tables (ctx, index_hint);
1552 /* do any delayed parsing we need to do. */
1553 maildir_delayed_parsing (ctx, md);
1555 /* Incorporate new messages */
1556 have_new = maildir_move_to_context (ctx, &md);
1558 return occult ? M_REOPENED : (have_new ? M_NEW_MAIL : 0);
1562 * This function handles arrival of new mail and reopening of
1563 * mh/maildir folders. Things are getting rather complex because we
1564 * don't have a well-defined "mailbox order", so the tricks from
1565 * mbox.c and mx.c won't work here.
1567 * Don't change this code unless you _really_ understand what
1572 static int mh_check_mailbox (CONTEXT * ctx, int *index_hint, int unused __attribute__ ((unused)))
1574 char buf[_POSIX_PATH_MAX];
1575 struct stat st, st_cur;
1576 short modified = 0, have_new = 0, occult = 0;
1577 struct maildir *md, *p;
1578 struct maildir **last = NULL;
1579 struct mh_sequences mhs;
1583 if (!option (OPTCHECKNEW))
1586 m_strcpy(buf, sizeof(buf), ctx->path);
1587 if (stat (buf, &st) == -1)
1590 /* create .mh_sequences when there isn't one. */
1591 snprintf (buf, sizeof (buf), "%s/.mh_sequences", ctx->path);
1592 if ((i = stat (buf, &st_cur) == -1) && errno == ENOENT) {
1596 if (mh_mkstemp (ctx, &fp, &tmp) == 0) {
1598 if (safe_rename (tmp, buf) == -1)
1604 if (i == -1 && stat (buf, &st_cur) == -1)
1607 if (st.st_mtime > ctx->mtime || st_cur.st_mtime > ctx->mtime_cur)
1613 ctx->mtime_cur = st_cur.st_mtime;
1614 ctx->mtime = st.st_mtime;
1620 maildir_parse_dir (ctx, &last, NULL, NULL);
1621 mh_read_sequences (&mhs, ctx->path);
1622 mh_update_maildir (md, &mhs);
1623 mhs_free_sequences (&mhs);
1625 /* check for modifications and adjust flags */
1626 fnames = hash_create (1031);
1628 for (p = md; p; p = p->next)
1629 hash_insert (fnames, p->h->path, p, 0);
1631 for (i = 0; i < ctx->msgcount; i++) {
1632 ctx->hdrs[i]->active = 0;
1634 if ((p = hash_find (fnames, ctx->hdrs[i]->path)) && p->h &&
1635 (mutt_cmp_header (ctx->hdrs[i], p->h))) {
1636 ctx->hdrs[i]->active = 1;
1637 /* found the right message */
1638 if (!ctx->hdrs[i]->changed)
1639 maildir_update_flags (ctx, ctx->hdrs[i], p->h);
1641 header_delete(&p->h);
1643 else /* message has disappeared */
1647 /* destroy the file name hash */
1649 hash_destroy (&fnames, NULL);
1651 /* If we didn't just get new mail, update the tables. */
1653 maildir_update_tables (ctx, index_hint);
1655 /* Incorporate new messages */
1656 have_new = maildir_move_to_context (ctx, &md);
1658 return occult ? M_REOPENED : (have_new ? M_NEW_MAIL : 0);
1665 * These functions try to find a message in a maildir folder when it
1666 * has moved under our feet. Note that this code is rather expensive, but
1667 * then again, it's called rarely.
1670 static FILE *_maildir_open_find_message (const char *folder, const char *unique,
1671 const char *subfolder)
1673 char dir[_POSIX_PATH_MAX];
1674 char tunique[_POSIX_PATH_MAX];
1675 char fname[_POSIX_PATH_MAX];
1683 snprintf (dir, sizeof (dir), "%s/%s", folder, subfolder);
1685 if ((dp = opendir (dir)) == NULL) {
1690 while ((de = readdir (dp))) {
1691 maildir_canon_filename (tunique, de->d_name, sizeof (tunique));
1693 if (!m_strcmp(tunique, unique)) {
1694 snprintf (fname, sizeof (fname), "%s/%s/%s", folder, subfolder,
1696 fp = fopen (fname, "r"); /* __FOPEN_CHECKED__ */
1708 FILE *maildir_open_find_message (const char *folder, const char *msg)
1710 char unique[_POSIX_PATH_MAX];
1713 static unsigned int new_hits = 0, cur_hits = 0; /* simple dynamic optimization */
1715 maildir_canon_filename (unique, msg, sizeof (unique));
1718 _maildir_open_find_message (folder, unique,
1719 new_hits > cur_hits ? "new" : "cur"))
1720 || errno != ENOENT) {
1721 if (new_hits < UINT_MAX && cur_hits < UINT_MAX) {
1722 new_hits += (new_hits > cur_hits ? 1 : 0);
1723 cur_hits += (new_hits > cur_hits ? 0 : 1);
1729 _maildir_open_find_message (folder, unique,
1730 new_hits > cur_hits ? "cur" : "new"))
1731 || errno != ENOENT) {
1732 if (new_hits < UINT_MAX && cur_hits < UINT_MAX) {
1733 new_hits += (new_hits > cur_hits ? 0 : 1);
1734 cur_hits += (new_hits > cur_hits ? 1 : 0);
1746 * 1 if there are no messages in the mailbox
1747 * 0 if there are messages in the mailbox
1750 static int maildir_check_empty (const char *path)
1754 int r = 1; /* assume empty until we find a message */
1755 char frealpath[_POSIX_PATH_MAX];
1758 /* Strategy here is to look for any file not beginning with a period */
1761 /* we do "cur" on the first iteration since its more likely that we'll
1762 * find old messages without having to scan both subdirs
1764 snprintf (frealpath, sizeof (frealpath), "%s/%s", path,
1765 iter == 0 ? "cur" : "new");
1766 if ((dp = opendir (frealpath)) == NULL)
1768 while ((de = readdir (dp))) {
1769 if (*de->d_name != '.') {
1776 } while (r && iter < 2);
1783 * 1 if there are no messages in the mailbox
1784 * 0 if there are messages in the mailbox
1787 static int mh_check_empty (const char *path)
1791 int r = 1; /* assume empty until we find a message */
1793 if ((dp = opendir (path)) == NULL)
1795 while ((de = readdir (dp))) {
1796 if (mh_valid_message (de->d_name)) {
1806 static int mh_is_magic (const char* path, struct stat* st) {
1807 char tmp[_POSIX_PATH_MAX];
1809 if (S_ISDIR (st->st_mode)) {
1810 snprintf (tmp, sizeof (tmp), "%s/.mh_sequences", path);
1811 if (access (tmp, F_OK) == 0)
1814 snprintf (tmp, sizeof (tmp), "%s/.xmhcache", path);
1815 if (access (tmp, F_OK) == 0)
1818 snprintf (tmp, sizeof (tmp), "%s/.mew_cache", path);
1819 if (access (tmp, F_OK) == 0)
1822 snprintf (tmp, sizeof (tmp), "%s/.mew-cache", path);
1823 if (access (tmp, F_OK) == 0)
1826 snprintf (tmp, sizeof (tmp), "%s/.sylpheed_cache", path);
1827 if (access (tmp, F_OK) == 0)
1831 * ok, this isn't an mh folder, but mh mode can be used to read
1832 * Usenet news from the spool. ;-)
1835 snprintf (tmp, sizeof (tmp), "%s/.overview", path);
1836 if (access (tmp, F_OK) == 0)
1842 static int maildir_is_magic (const char* path, struct stat* st) {
1844 char tmp[_POSIX_PATH_MAX];
1846 if (S_ISDIR (st->st_mode)) {
1847 snprintf (tmp, sizeof (tmp), "%s/cur", path);
1848 if (stat (tmp, &sb) == 0 && S_ISDIR (sb.st_mode))
1854 static int mh_commit (MESSAGE* msg, CONTEXT* ctx) {
1855 return (mh_commit_message (msg, ctx, NULL));
1858 static int maildir_commit (MESSAGE* msg, CONTEXT* ctx) {
1859 return (maildir_commit_message (msg, ctx, NULL));
1862 mx_t const maildir_mx = {
1866 maildir_check_empty,
1869 maildir_open_new_message,
1871 maildir_check_mailbox,
1877 mx_t const mh_mx = {
1884 mh_open_new_message,