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;
38 typedef struct mh_sequences {
43 /* mh_sequences support */
44 #define MH_SEQ_UNSEEN (1 << 0)
45 #define MH_SEQ_REPLIED (1 << 1)
46 #define MH_SEQ_FLAGGED (1 << 2)
49 static int maildir_check_empty (const char*);
50 static int maildir_check_mailbox (CONTEXT*, int*, int);
51 static int mh_check_mailbox (CONTEXT*, int*, int);
53 static mh_sequences *mhs_new(void)
55 mh_sequences *res = xmalloc(sizeof(mh_sequences) + 128);
60 static void mhs_ensure(mh_sequences *mhs, int i)
63 xrealloc((void *)&mhs, sizeof(mh_sequences) + mhs->size + 128);
64 p_clear(mhs->flags + mhs->size, 128);
69 static void mhs_delete(mh_sequences **mhs)
74 static short mhs_check (struct mh_sequences *mhs, int i)
76 return i > mhs->size ? 0 : mhs->flags[i];
79 static short mhs_set (struct mh_sequences *mhs, int i, short f)
86 static void mh_read_token (char *t, int *first, int *last)
90 if ((p = strchr (t, '-'))) {
96 *first = *last = atoi (t);
99 static void mh_read_sequences (struct mh_sequences *mhs, const char *path)
110 char pathname[_POSIX_PATH_MAX];
112 snprintf (pathname, sizeof (pathname), "%s/.mh_sequences", path);
114 if (!(fp = fopen (pathname, "r")))
117 while ((buff = mutt_read_line (buff, &sz, fp, &line))) {
118 if (!(t = strtok (buff, " \t:")))
121 if (!m_strcmp(t, MhUnseen))
123 else if (!m_strcmp(t, MhFlagged))
125 else if (!m_strcmp(t, MhReplied))
127 else /* unknown sequence */
130 while ((t = strtok (NULL, " \t:"))) {
131 mh_read_token (t, &first, &last);
132 for (; first <= last; first++)
133 mhs_set (mhs, first, f);
141 int mh_buffy (const char *path)
143 mh_sequences *mhs = mhs_new();
146 mh_read_sequences(mhs, path);
147 for (i = 0; i <= mhs->size; i++) {
148 if (mhs_check(mhs, i) & MH_SEQ_UNSEEN) {
158 static int mh_mkstemp (CONTEXT * dest, FILE ** fp, char **tgt)
160 static int Counter = 0;
162 char path[_POSIX_PATH_MAX];
165 snprintf (path, _POSIX_PATH_MAX, "%s/.mutt-%s-%d-%d",
166 dest->path, NONULL(mod_core.shorthost), (int) getpid (), Counter++);
168 if ((fd = open (path, O_WRONLY | O_EXCL | O_CREAT, 0666)) == -1) {
169 if (errno != EEXIST) {
175 *tgt = m_strdup(path);
180 if ((*fp = fdopen (fd, "w")) == NULL) {
190 static void mhs_write_one_sequence(FILE * fp, struct mh_sequences *mhs,
191 short f, const char *tag)
196 fprintf (fp, "%s:", tag);
201 for (i = 0; i <= mhs->size; i++) {
202 if ((mhs_check (mhs, i) & f)) {
208 else if (first >= 0) {
210 fprintf (fp, " %d", first);
212 fprintf (fp, " %d-%d", first, last);
221 fprintf (fp, " %d", first);
223 fprintf (fp, " %d-%d", first, last);
229 /* XXX - we don't currently remove deleted messages from sequences we don't know. Should we? */
231 static void mh_update_sequences (CONTEXT * ctx)
235 char sequences[_POSIX_PATH_MAX];
247 char seq_unseen[STRING];
248 char seq_replied[STRING];
249 char seq_flagged[STRING];
250 mh_sequences *mhs = mhs_new();
252 snprintf(seq_unseen, sizeof(seq_unseen), "%s:", NONULL(MhUnseen));
253 snprintf(seq_replied, sizeof(seq_replied), "%s:", NONULL(MhReplied));
254 snprintf(seq_flagged, sizeof(seq_flagged), "%s:", NONULL(MhFlagged));
256 if (mh_mkstemp (ctx, &nfp, &tmpfname) != 0) {
261 snprintf (sequences, sizeof (sequences), "%s/.mh_sequences", ctx->path);
264 /* first, copy unknown sequences */
265 if ((ofp = fopen (sequences, "r"))) {
266 while ((buff = mutt_read_line (buff, &s, ofp, &l))) {
267 if (!m_strncmp(buff, seq_unseen, m_strlen(seq_unseen)))
269 if (!m_strncmp(buff, seq_flagged, m_strlen(seq_flagged)))
271 if (!m_strncmp(buff, seq_replied, m_strlen(seq_replied)))
274 fprintf (nfp, "%s\n", buff);
279 /* now, update our unseen, flagged, and replied sequences */
280 for (l = 0; l < ctx->msgcount; l++) {
281 if (ctx->hdrs[l]->deleted)
284 if ((p = strrchr (ctx->hdrs[l]->path, '/')))
287 p = ctx->hdrs[l]->path;
291 if (!ctx->hdrs[l]->read) {
292 mhs_set(mhs, i, MH_SEQ_UNSEEN);
295 if (ctx->hdrs[l]->flagged) {
296 mhs_set(mhs, i, MH_SEQ_FLAGGED);
299 if (ctx->hdrs[l]->replied) {
300 mhs_set(mhs, i, MH_SEQ_REPLIED);
305 /* write out the new sequences */
307 mhs_write_one_sequence(nfp, mhs, MH_SEQ_UNSEEN, NONULL (MhUnseen));
309 mhs_write_one_sequence(nfp, mhs, MH_SEQ_FLAGGED, NONULL (MhFlagged));
311 mhs_write_one_sequence(nfp, mhs, MH_SEQ_REPLIED, NONULL (MhReplied));
315 /* try to commit the changes - no guarantee here */
319 if (safe_rename (tmpfname, sequences) != 0) {
320 /* report an error? */
327 static void mh_sequences_add_one (CONTEXT * ctx, int n, short unseen,
328 short flagged, short replied)
330 short unseen_done = 0;
331 short flagged_done = 0;
332 short replied_done = 0;
334 FILE *ofp = NULL, *nfp = NULL;
337 char sequences[_POSIX_PATH_MAX];
339 char seq_unseen[STRING];
340 char seq_replied[STRING];
341 char seq_flagged[STRING];
347 if (mh_mkstemp (ctx, &nfp, &tmpfname) == -1)
350 snprintf (seq_unseen, sizeof (seq_unseen), "%s:", NONULL (MhUnseen));
351 snprintf (seq_replied, sizeof (seq_replied), "%s:", NONULL (MhReplied));
352 snprintf (seq_flagged, sizeof (seq_flagged), "%s:", NONULL (MhFlagged));
354 snprintf (sequences, sizeof (sequences), "%s/.mh_sequences", ctx->path);
355 if ((ofp = fopen (sequences, "r"))) {
356 while ((buff = mutt_read_line (buff, &sz, ofp, &line))) {
357 if (unseen && !m_strncmp (buff, seq_unseen, m_strlen(seq_unseen))) {
358 fprintf (nfp, "%s %d\n", buff, n);
362 && !m_strncmp (buff, seq_flagged, m_strlen(seq_flagged))) {
363 fprintf (nfp, "%s %d\n", buff, n);
367 && !m_strncmp (buff, seq_replied, m_strlen(seq_replied))) {
368 fprintf (nfp, "%s %d\n", buff, n);
372 fprintf (nfp, "%s\n", buff);
378 if (!unseen_done && unseen)
379 fprintf (nfp, "%s: %d\n", NONULL (MhUnseen), n);
380 if (!flagged_done && flagged)
381 fprintf (nfp, "%s: %d\n", NONULL (MhFlagged), n);
382 if (!replied_done && replied)
383 fprintf (nfp, "%s: %d\n", NONULL (MhReplied), n);
388 if (safe_rename (tmpfname, sequences) != 0)
394 static void mh_update_maildir (struct maildir *md, struct mh_sequences *mhs)
400 for (; md; md = md->next) {
401 if ((p = strrchr (md->h->path, '/')))
407 f = mhs_check (mhs, i);
409 md->h->read = (f & MH_SEQ_UNSEEN) ? 0 : 1;
410 md->h->flagged = (f & MH_SEQ_FLAGGED) ? 1 : 0;
411 md->h->replied = (f & MH_SEQ_REPLIED) ? 1 : 0;
415 /* maildir support */
417 static void maildir_free_entry (struct maildir **md)
422 p_delete(&(*md)->canon_fname);
424 header_delete(&(*md)->h);
429 static void maildir_free_maildir (struct maildir **md)
431 struct maildir *p, *q;
436 for (p = *md; p; p = q) {
438 maildir_free_entry (&p);
442 static void maildir_parse_flags (HEADER * h, const char *path)
450 if ((p = strrchr (path, ':')) != NULL && m_strncmp(p + 1, "2,", 2) == 0) {
453 m_strreplace(&h->maildir_flags, p);
454 q = h->maildir_flags;
468 case 'R': /* replied */
473 case 'T': /* trashed */
486 if (q == h->maildir_flags)
487 p_delete(&h->maildir_flags);
492 static void maildir_update_mtime (CONTEXT * ctx)
494 char buf[_POSIX_PATH_MAX];
497 if (ctx->magic == M_MAILDIR) {
498 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, "cur");
499 if (stat (buf, &st) == 0)
500 ctx->mtime_cur = st.st_mtime;
501 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, "new");
504 snprintf (buf, sizeof (buf), "%s/.mh_sequences", ctx->path);
505 if (stat (buf, &st) == 0)
506 ctx->mtime_cur = st.st_mtime;
508 m_strcpy(buf, sizeof(buf), ctx->path);
511 if (stat (buf, &st) == 0)
512 ctx->mtime = st.st_mtime;
516 * Actually parse a maildir message. This may also be used to fill
517 * out a fake header structure generated by lazy maildir parsing.
519 static HEADER *maildir_parse_message (int magic, const char *fname,
520 int is_old, HEADER * _h)
526 if ((f = fopen (fname, "r")) != NULL) {
529 h->env = mutt_read_rfc822_header (f, h, 0, 0);
531 fstat (fileno (f), &st);
535 h->received = h->date_sent;
537 if (h->content->length <= 0)
538 h->content->length = st.st_size - h->content->offset;
542 if (magic == M_MAILDIR) {
544 * maildir stores its flags in the filename, so ignore the
545 * flags in the header of the message
549 maildir_parse_flags (h, fname);
557 * Note that this routine will _not_ modify the context given by
560 * It's used in the first parsing pass on maildir and MH folders.
561 * In the MH case, this means full parsing of the folder. In the
562 * maildir case, it means that we only look at flags, and create a
563 * fake HEADER structure, which may later be filled in by
564 * maildir_parse_message(), when called from
565 * maildir_delayed_parsing().
569 static int maildir_parse_entry (CONTEXT * ctx, struct maildir ***last,
570 const char *subdir, const char *fname,
571 int *count, int is_old, ino_t inode __attribute__ ((unused)))
573 struct maildir *entry;
575 char buf[_POSIX_PATH_MAX];
578 snprintf (buf, sizeof (buf), "%s/%s/%s", ctx->path, subdir, fname);
580 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, fname);
582 if (ctx->magic == M_MH)
583 h = maildir_parse_message (ctx->magic, buf, is_old, NULL);
587 maildir_parse_flags (h, buf);
593 if (!ctx->quiet && ReadInc && ((*count % ReadInc) == 0 || *count == 1))
594 mutt_message (_("Reading %s... %d"), ctx->path, *count);
598 snprintf (buf, sizeof (buf), "%s/%s", subdir, fname);
599 h->path = m_strdup(buf);
602 h->path = m_strdup(fname);
604 entry = p_new(struct maildir, 1);
606 entry->header_parsed = (ctx->magic == M_MH);
608 *last = &entry->next;
618 /* Ignore the garbage files. A valid MH message consists of only
619 * digits. Deleted message get moved to a filename with a comma before
623 int mh_valid_message (const char *s)
626 if (!isdigit ((unsigned char) *s))
632 static int maildir_parse_dir (CONTEXT * ctx, struct maildir ***last,
633 const char *subdir, int *count)
637 char buf[_POSIX_PATH_MAX];
641 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, subdir);
642 is_old = (m_strcmp("cur", subdir) == 0);
645 m_strcpy(buf, sizeof(buf), ctx->path);
647 if ((dirp = opendir (buf)) == NULL)
650 while ((de = readdir (dirp)) != NULL) {
652 if ((ctx->magic == M_MH && !mh_valid_message (de->d_name))
653 || (ctx->magic == M_MAILDIR && *de->d_name == '.'))
656 /* FOO - really ignore the return value? */
658 maildir_parse_entry (ctx, last, subdir, de->d_name, count, is_old,
659 #ifdef HAVE_DIRENT_D_INO
671 static int maildir_add_to_context (CONTEXT * ctx, struct maildir *md)
673 int oldmsgcount = ctx->msgcount;
678 if (ctx->msgcount == ctx->hdrmax)
679 mx_alloc_memory (ctx);
681 ctx->hdrs[ctx->msgcount] = md->h;
682 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
684 md->h->content->length + md->h->content->offset -
685 md->h->content->hdr_offset;
693 if (ctx->msgcount > oldmsgcount) {
694 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
700 static int maildir_move_to_context (CONTEXT * ctx, struct maildir **md)
704 r = maildir_add_to_context (ctx, *md);
705 maildir_free_maildir (md);
710 static ssize_t maildir_hcache_keylen (const char *fn)
712 return m_strchrnul(fn, ':') - fn;
717 * This function does the second parsing pass for a maildir-style
720 static void maildir_delayed_parsing (CONTEXT * ctx, struct maildir *md)
723 char fn[_POSIX_PATH_MAX];
729 struct timeval *when = NULL;
730 struct stat lastchanged;
733 hc = mutt_hcache_open(ctx->path);
736 for (p = md, count = 0; p; p = p->next, count++) {
737 if (!(p && p->h && !p->header_parsed))
741 data = mutt_hcache_fetch (hc, p->h->path + 3, &maildir_hcache_keylen);
742 when = (struct timeval *) data;
745 if (!ctx->quiet && ReadInc && ((count % ReadInc) == 0 || count == 1))
746 mutt_message (_("Reading %s... %d"), ctx->path, count);
747 snprintf (fn, sizeof (fn), "%s/%s", ctx->path, p->h->path);
750 ret = stat(fn, &lastchanged);
751 if (data && !ret && lastchanged.st_mtime <= when->tv_sec) {
752 p->h = mutt_hcache_restore(data, &p->h);
753 maildir_parse_flags (p->h, fn);
756 if (maildir_parse_message (ctx->magic, fn, p->h->old, p->h)) {
757 p->header_parsed = 1;
758 maildir_parse_flags (p->h, fn);
760 mutt_hcache_store (hc, p->h->path + 3, p->h, 0, &maildir_hcache_keylen);
763 header_delete(&p->h);
770 mutt_hcache_close (&hc);
774 /* Read a MH/maildir style mailbox.
777 * ctx [IN/OUT] context for this mailbox
778 * subdir [IN] NULL for MH mailboxes, otherwise the subdir of the
779 * maildir mailbox to read from
781 static int _mh_read_dir (CONTEXT * ctx, const char *subdir)
783 struct maildir *md = NULL, **last = &md;
786 maildir_update_mtime (ctx);
788 if (maildir_parse_dir (ctx, &last, subdir, &count) == -1)
791 if (ctx->magic == M_MH) {
792 mh_sequences *mhs = mhs_new();
793 mh_read_sequences(mhs, ctx->path);
794 mh_update_maildir(md, mhs);
798 if (ctx->magic == M_MAILDIR)
799 maildir_delayed_parsing (ctx, md);
801 maildir_move_to_context (ctx, &md);
805 static int mh_read_dir (CONTEXT* ctx) {
806 return (_mh_read_dir (ctx, NULL));
809 /* read a maildir style mailbox */
810 static int maildir_read_dir (CONTEXT * ctx)
812 /* maildir looks sort of like MH, except that there are two subdirectories
813 * of the main folder path from which to read messages
815 if (_mh_read_dir (ctx, "new") == -1 || _mh_read_dir (ctx, "cur") == -1)
822 * Open a new (temporary) message in an MH folder.
825 static int mh_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr __attribute__ ((unused)))
827 return mh_mkstemp (dest, &msg->fp, &msg->path);
830 static int ch_compar (const void *a, const void *b)
832 return (int) (*((const char *) a) - *((const char *) b));
835 static void maildir_flags (char *dest, ssize_t destlen, HEADER * hdr)
840 * The maildir specification requires that all files in the cur
841 * subdirectory have the :unique string appeneded, regardless of whether
842 * or not there are any flags. If .old is set, we know that this message
843 * will end up in the cur directory, so we include it in the following
844 * test even though there is no associated flag.
848 && (hdr->flagged || hdr->replied || hdr->read || hdr->deleted
849 || hdr->old || hdr->maildir_flags)) {
850 char tmp[LONG_STRING];
852 snprintf (tmp, sizeof (tmp),
854 hdr->flagged ? "F" : "",
855 hdr->replied ? "R" : "",
856 hdr->read ? "S" : "", hdr->deleted ? "T" : "",
857 NONULL (hdr->maildir_flags));
858 if (hdr->maildir_flags)
859 qsort (tmp, m_strlen(tmp), 1, ch_compar);
860 snprintf (dest, destlen, ":2,%s", tmp);
866 * Open a new (temporary) message in a maildir folder.
868 * Note that this uses _almost_ the maildir file name format, but
869 * with a {cur,new} prefix.
873 static int maildir_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr)
875 static int Counter = 0;
877 char path[_POSIX_PATH_MAX];
882 short deleted = hdr->deleted;
886 maildir_flags (suffix, sizeof (suffix), hdr);
888 hdr->deleted = deleted;
893 if (hdr && (hdr->read || hdr->old))
894 m_strcpy(subdir, sizeof(subdir), "cur");
896 m_strcpy(subdir, sizeof(subdir), "new");
899 snprintf (path, _POSIX_PATH_MAX, "%s/tmp/%s.%ld.%u_%d.%s%s",
900 dest->path, subdir, (long) time (NULL),
901 (unsigned int) getpid (), Counter++, NONULL (mod_core.shorthost), suffix);
904 if ((fd = open (path, O_WRONLY | O_EXCL | O_CREAT, 0666)) == -1) {
905 if (errno != EEXIST) {
910 msg->path = m_strdup(path);
915 if ((msg->fp = fdopen (fd, "w")) == NULL) {
916 p_delete(&msg->path);
928 * Commit a message to a maildir folder.
930 * msg->path contains the file name of a file in tmp/. We take the
931 * flags from this file's name.
933 * ctx is the mail folder we commit to.
935 * hdr is a header structure to which we write the message's new
936 * file name. This is used in the mh and maildir folder synch
937 * routines. When this routine is invoked from mx_commit_message,
940 * msg->path looks like this:
942 * tmp/{cur,new}.mutt-HOSTNAME-PID-COUNTER:flags
944 * See also maildir_open_new_message().
948 static int maildir_commit_message (MESSAGE * msg, CONTEXT * ctx, HEADER * hdr)
950 static int Counter = 0;
953 char path[_POSIX_PATH_MAX];
954 char full[_POSIX_PATH_MAX];
957 if (m_fclose(&msg->fp) != 0)
960 /* extract the subdir */
961 s = strrchr (msg->path, '/') + 1;
962 m_strcpy(subdir, sizeof(subdir), s);
964 /* extract the flags */
965 if ((s = strchr (s, ':')))
966 m_strcpy(suffix, sizeof(suffix), s);
970 /* construct a new file name. */
972 snprintf (path, _POSIX_PATH_MAX, "%s/%ld.%u_%d.%s%s", subdir,
973 (long) time (NULL), (unsigned int) getpid (), Counter++,
974 NONULL (mod_core.shorthost), suffix);
975 snprintf (full, _POSIX_PATH_MAX, "%s/%s", ctx->path, path);
977 if (safe_rename (msg->path, full) == 0) {
979 m_strreplace(&hdr->path, path);
980 p_delete(&msg->path);
983 * Adjust the mtime on the file to match the time at which this
984 * message was received. Currently this is only set when copying
985 * messages between mailboxes, so we test to ensure that it is
991 ut.actime = msg->received;
992 ut.modtime = msg->received;
993 if (utime (full, &ut)) {
995 ("maildir_commit_message(): unable to set time on file"));
1002 else if (errno != EEXIST) {
1003 mutt_perror (ctx->path);
1010 * commit a message to an MH folder.
1015 static int _mh_commit_message (MESSAGE * msg, CONTEXT * ctx, HEADER * hdr,
1021 unsigned int n, hi = 0;
1022 char path[_POSIX_PATH_MAX];
1025 if (m_fclose(&msg->fp) != 0)
1028 if ((dirp = opendir (ctx->path)) == NULL) {
1029 mutt_perror (ctx->path);
1033 /* figure out what the next message number is */
1034 while ((de = readdir (dirp)) != NULL) {
1040 if (!isdigit ((unsigned char) *cp))
1053 * Now try to rename the file to the proper name.
1055 * Note: We may have to try multiple times, until we find a free
1061 snprintf (tmp, sizeof (tmp), "%d", hi);
1062 snprintf (path, sizeof (path), "%s/%s", ctx->path, tmp);
1063 if (safe_rename (msg->path, path) == 0) {
1065 m_strreplace(&hdr->path, tmp);
1066 p_delete(&msg->path);
1069 else if (errno != EEXIST) {
1070 mutt_perror (ctx->path);
1075 mh_sequences_add_one (ctx, hi, !msg->flags.read, msg->flags.flagged,
1076 msg->flags.replied);
1080 static int mh_commit_message (MESSAGE * msg, CONTEXT * ctx, HEADER * hdr) {
1081 return _mh_commit_message (msg, ctx, hdr, 1);
1084 /* Sync a message in an MH folder.
1086 * This code is also used for attachment deletion in maildir
1090 static int mh_rewrite_message (CONTEXT * ctx, int msgno)
1092 HEADER *h = ctx->hdrs[msgno];
1097 char oldpath[_POSIX_PATH_MAX];
1098 char newpath[_POSIX_PATH_MAX];
1099 char partpath[_POSIX_PATH_MAX];
1101 long old_body_offset = h->content->offset;
1102 long old_body_length = h->content->length;
1103 long old_hdr_lines = h->lines;
1105 if ((dest = mx_open_new_message (ctx, h, 0)) == NULL)
1108 if ((rc = mutt_copy_message (dest->fp, ctx, h,
1109 M_CM_UPDATE, CH_UPDATE | CH_UPDATE_LEN)) == 0)
1111 snprintf (oldpath, sizeof(oldpath), "%s/%s", ctx->path, h->path);
1112 m_strcpy(partpath, sizeof(partpath), h->path);
1114 if (ctx->magic == M_MAILDIR)
1115 rc = maildir_commit_message (dest, ctx, h);
1117 rc = _mh_commit_message (dest, ctx, h, 0);
1119 mx_close_message (&dest);
1127 * Try to move the new message to the old place.
1130 * This is important when we are just updating flags.
1132 * Note that there is a race condition against programs which
1133 * use the first free slot instead of the maximum message
1134 * number. Mutt does _not_ behave like this.
1136 * Anyway, if this fails, the message is in the folder, so
1137 * all what happens is that a concurrently runnung mutt will
1138 * lose flag modifications.
1141 if (ctx->magic == M_MH && rc == 0) {
1142 snprintf (newpath, _POSIX_PATH_MAX, "%s/%s", ctx->path, h->path);
1143 if ((rc = safe_rename (newpath, oldpath)) == 0)
1144 m_strreplace(&h->path, partpath);
1148 mx_close_message (&dest);
1150 if (rc == -1 && restore) {
1151 h->content->offset = old_body_offset;
1152 h->content->length = old_body_length;
1153 h->lines = old_hdr_lines;
1156 body_list_wipe(&h->content->parts);
1160 static int mh_sync_message (CONTEXT * ctx, int msgno)
1162 HEADER *h = ctx->hdrs[msgno];
1164 if (h->attach_del ||
1165 (h->env && (h->env->refs_changed || h->env->irt_changed)))
1166 if (mh_rewrite_message (ctx, msgno) != 0)
1172 static int maildir_sync_message (CONTEXT * ctx, int msgno)
1174 HEADER *h = ctx->hdrs[msgno];
1176 if (h->attach_del ||
1177 (h->env && (h->env->refs_changed || h->env->irt_changed))) {
1178 /* when doing attachment deletion/rethreading, fall back to the MH case. */
1179 if (mh_rewrite_message (ctx, msgno) != 0)
1183 /* we just have to rename the file. */
1185 char newpath[_POSIX_PATH_MAX];
1186 char partpath[_POSIX_PATH_MAX];
1187 char fullpath[_POSIX_PATH_MAX];
1188 char oldpath[_POSIX_PATH_MAX];
1192 if ((p = strrchr (h->path, '/')) == NULL) {
1196 m_strcpy(newpath, sizeof(newpath), p);
1198 /* kill the previous flags */
1199 if ((p = strchr (newpath, ':')) != NULL)
1202 maildir_flags (suffix, sizeof (suffix), h);
1204 snprintf (partpath, sizeof (partpath), "%s/%s%s",
1205 (h->read || h->old) ? "cur" : "new", newpath, suffix);
1206 snprintf (fullpath, sizeof (fullpath), "%s/%s", ctx->path, partpath);
1207 snprintf (oldpath, sizeof (oldpath), "%s/%s", ctx->path, h->path);
1209 if (m_strcmp(fullpath, oldpath) == 0) {
1210 /* message hasn't really changed */
1214 /* record that the message is possibly marked as trashed on disk */
1215 h->trash = h->deleted;
1217 if (rename (oldpath, fullpath) != 0) {
1218 mutt_perror ("rename");
1221 m_strreplace(&h->path, partpath);
1226 static int mh_sync_mailbox (CONTEXT * ctx, int unused __attribute__ ((unused)), int *index_hint)
1228 char path[_POSIX_PATH_MAX], tmp[_POSIX_PATH_MAX];
1233 #endif /* USE_HCACHE */
1235 if (ctx->magic == M_MH)
1236 i = mh_check_mailbox (ctx, index_hint, 0);
1238 i = maildir_check_mailbox (ctx, index_hint, 0);
1244 if (ctx->magic == M_MAILDIR)
1245 hc = mutt_hcache_open(ctx->path);
1246 #endif /* USE_HCACHE */
1248 for (i = 0; i < ctx->msgcount; i++) {
1249 if (ctx->hdrs[i]->deleted
1250 && (ctx->magic != M_MAILDIR || !option (OPTMAILDIRTRASH))) {
1251 snprintf (path, sizeof (path), "%s/%s", ctx->path, ctx->hdrs[i]->path);
1252 if (ctx->magic == M_MAILDIR
1253 || (option (OPTMHPURGE) && ctx->magic == M_MH)) {
1255 if (ctx->magic == M_MAILDIR)
1256 mutt_hcache_delete (hc, ctx->hdrs[i]->path + 3,
1257 &maildir_hcache_keylen);
1258 #endif /* USE_HCACHE */
1261 else if (ctx->magic == M_MH) {
1262 /* MH just moves files out of the way when you delete them */
1263 if (*ctx->hdrs[i]->path != ',') {
1264 snprintf (tmp, sizeof (tmp), "%s/,%s", ctx->path,
1265 ctx->hdrs[i]->path);
1272 else if (ctx->hdrs[i]->changed || ctx->hdrs[i]->attach_del ||
1273 (ctx->magic == M_MAILDIR
1274 && (option (OPTMAILDIRTRASH) || ctx->hdrs[i]->trash)
1275 && (ctx->hdrs[i]->deleted != ctx->hdrs[i]->trash))) {
1276 if (ctx->magic == M_MAILDIR) {
1277 if (maildir_sync_message (ctx, i) == -1)
1281 if (mh_sync_message (ctx, i) == -1)
1288 if (ctx->magic == M_MAILDIR)
1289 mutt_hcache_close (hc);
1290 #endif /* USE_HCACHE */
1292 if (ctx->magic == M_MH)
1293 mh_update_sequences (ctx);
1295 /* XXX race condition? */
1297 maildir_update_mtime (ctx);
1299 /* adjust indices */
1302 for (i = 0, j = 0; i < ctx->msgcount; i++) {
1303 if (!ctx->hdrs[i]->deleted
1304 || (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))
1305 ctx->hdrs[i]->index = j++;
1313 if (ctx->magic == M_MAILDIR)
1314 mutt_hcache_close (hc);
1315 #endif /* USE_HCACHE */
1319 static char *maildir_canon_filename (char *dest, const char *src, ssize_t l)
1323 if ((t = strrchr (src, '/')))
1326 m_strcpy(dest, l, src);
1327 if ((u = strrchr (dest, ':')))
1333 static void maildir_update_tables (CONTEXT * ctx, int *index_hint)
1339 if (Sort != SORT_ORDER) {
1342 mutt_sort_headers (ctx, 1);
1346 old_count = ctx->msgcount;
1347 for (i = 0, j = 0; i < old_count; i++) {
1348 if (ctx->hdrs[i]->active && index_hint && *index_hint == i)
1351 if (ctx->hdrs[i]->active)
1352 ctx->hdrs[i]->index = j++;
1355 mx_update_tables (ctx, 0);
1356 mutt_clear_threads (ctx);
1359 static void maildir_update_flags (CONTEXT * ctx, HEADER * o, HEADER * n)
1361 /* save the global state here so we can reset it at the
1362 * end of list block if required.
1364 int context_changed = ctx->changed;
1366 /* user didn't modify this message. alter the flags to
1367 * match the current state on disk. This may not actually
1368 * do anything, but we can't tell right now. mutt_set_flag()
1369 * will just ignore the call if the status bits are
1370 * already properly set.
1372 mutt_set_flag (ctx, o, M_FLAG, n->flagged);
1373 mutt_set_flag (ctx, o, M_REPLIED, n->replied);
1374 mutt_set_flag (ctx, o, M_READ, n->read);
1375 mutt_set_flag (ctx, o, M_OLD, n->old);
1377 /* mutt_set_flag() will set this, but we don't need to
1378 * sync the changes we made because we just updated the
1379 * context to match the current on-disk state of the
1384 /* if the mailbox was not modified before we made these
1385 * changes, unset the changed flag since nothing needs to
1388 if (!context_changed)
1393 /* This function handles arrival of new mail and reopening of
1394 * maildir folders. The basic idea here is we check to see if either
1395 * the new or cur subdirectories have changed, and if so, we scan them
1396 * for the list of files. We check for newly added messages, and
1397 * then merge the flags messages we already knew about. We don't treat
1398 * either subdirectory differently, as mail could be copied directly into
1399 * the cur directory from another agent.
1401 static int maildir_check_mailbox (CONTEXT * ctx, int *index_hint, int unused __attribute__ ((unused)))
1403 struct stat st_new; /* status of the "new" subdirectory */
1404 struct stat st_cur; /* status of the "cur" subdirectory */
1405 char buf[_POSIX_PATH_MAX];
1406 int changed = 0; /* bitmask representing which subdirectories
1407 have changed. 0x1 = new, 0x2 = cur */
1408 int occult = 0; /* messages were removed from the mailbox */
1409 int have_new = 0; /* messages were added to the mailbox */
1410 struct maildir *md; /* list of messages in the mailbox */
1411 struct maildir **last, *p;
1413 hash_t *fnames; /* hash table for quickly looking up the base filename
1414 for a maildir message */
1416 if (!option (OPTCHECKNEW))
1419 snprintf (buf, sizeof (buf), "%s/new", ctx->path);
1420 if (stat (buf, &st_new) == -1)
1423 snprintf (buf, sizeof (buf), "%s/cur", ctx->path);
1424 if (stat (buf, &st_cur) == -1)
1427 /* determine which subdirectories need to be scanned */
1428 if (st_new.st_mtime > ctx->mtime)
1430 if (st_cur.st_mtime > ctx->mtime_cur)
1434 return 0; /* nothing to do */
1436 /* update the modification times on the mailbox */
1437 ctx->mtime_cur = st_cur.st_mtime;
1438 ctx->mtime = st_new.st_mtime;
1440 /* do a fast scan of just the filenames in
1441 * the subdirectories that have changed.
1446 maildir_parse_dir (ctx, &last, "new", NULL);
1448 maildir_parse_dir (ctx, &last, "cur", NULL);
1450 /* we create a hash table keyed off the canonical (sans flags) filename
1451 * of each message we scanned. This is used in the loop over the
1452 * existing messages below to do some correlation.
1454 fnames = hash_new (1031, 0);
1456 for (p = md; p; p = p->next) {
1457 maildir_canon_filename (buf, p->h->path, sizeof (buf));
1458 p->canon_fname = m_strdup(buf);
1459 hash_insert (fnames, p->canon_fname, p);
1462 /* check for modifications and adjust flags */
1463 for (i = 0; i < ctx->msgcount; i++) {
1464 ctx->hdrs[i]->active = 0;
1465 maildir_canon_filename (buf, ctx->hdrs[i]->path, sizeof (buf));
1466 p = hash_find (fnames, buf);
1468 /* message already exists, merge flags */
1469 ctx->hdrs[i]->active = 1;
1471 /* check to see if the message has moved to a different
1472 * subdirectory. If so, update the associated filename.
1474 if (m_strcmp(ctx->hdrs[i]->path, p->h->path))
1475 m_strreplace(&ctx->hdrs[i]->path, p->h->path);
1477 /* if the user hasn't modified the flags on this message, update
1478 * the flags we just detected.
1480 if (!ctx->hdrs[i]->changed)
1481 maildir_update_flags (ctx, ctx->hdrs[i], p->h);
1483 if (ctx->hdrs[i]->deleted == ctx->hdrs[i]->trash)
1484 ctx->hdrs[i]->deleted = p->h->deleted;
1485 ctx->hdrs[i]->trash = p->h->trash;
1487 /* this is a duplicate of an existing header, so remove it */
1488 header_delete(&p->h);
1490 /* This message was not in the list of messages we just scanned.
1491 * Check to see if we have enough information to know if the
1492 * message has disappeared out from underneath us.
1494 else if (((changed & 1) && (!m_strncmp (ctx->hdrs[i]->path, "new/", 4))) ||
1495 ((changed & 2) && (!m_strncmp (ctx->hdrs[i]->path, "cur/", 4)))) {
1496 /* This message disappeared, so we need to simulate a "reopen"
1497 * event. We know it disappeared because we just scanned the
1498 * subdirectory it used to reside in.
1503 /* This message resides in a subdirectory which was not
1504 * modified, so we assume that it is still present and
1507 ctx->hdrs[i]->active = 1;
1511 /* destroy the file name hash */
1512 hash_delete (&fnames, NULL);
1514 /* If we didn't just get new mail, update the tables. */
1516 maildir_update_tables (ctx, index_hint);
1518 /* do any delayed parsing we need to do. */
1519 maildir_delayed_parsing (ctx, md);
1521 /* Incorporate new messages */
1522 have_new = maildir_move_to_context (ctx, &md);
1524 return occult ? M_REOPENED : (have_new ? M_NEW_MAIL : 0);
1528 * This function handles arrival of new mail and reopening of
1529 * mh/maildir folders. Things are getting rather complex because we
1530 * don't have a well-defined "mailbox order", so the tricks from
1531 * mbox.c and mx.c won't work here.
1533 * Don't change this code unless you _really_ understand what
1538 static int mh_check_mailbox (CONTEXT * ctx, int *index_hint, int unused __attribute__ ((unused)))
1540 char buf[_POSIX_PATH_MAX];
1541 struct stat st, st_cur;
1542 short modified = 0, have_new = 0, occult = 0;
1543 struct maildir *md = NULL, **last = &md, *p;
1548 if (!option (OPTCHECKNEW))
1551 m_strcpy(buf, sizeof(buf), ctx->path);
1552 if (stat (buf, &st) == -1)
1555 /* create .mh_sequences when there isn't one. */
1556 snprintf (buf, sizeof (buf), "%s/.mh_sequences", ctx->path);
1557 if ((i = stat (buf, &st_cur) == -1) && errno == ENOENT) {
1561 if (mh_mkstemp (ctx, &fp, &tmp) == 0) {
1563 if (safe_rename (tmp, buf) == -1)
1569 if (i == -1 && stat (buf, &st_cur) == -1)
1572 if (st.st_mtime > ctx->mtime || st_cur.st_mtime > ctx->mtime_cur)
1578 ctx->mtime_cur = st_cur.st_mtime;
1579 ctx->mtime = st.st_mtime;
1581 maildir_parse_dir (ctx, &last, NULL, NULL);
1583 mh_read_sequences(mhs, ctx->path);
1584 mh_update_maildir(md, mhs);
1587 /* check for modifications and adjust flags */
1588 fnames = hash_new (1031, 0);
1590 for (p = md; p; p = p->next)
1591 hash_insert (fnames, p->h->path, p);
1593 for (i = 0; i < ctx->msgcount; i++) {
1594 ctx->hdrs[i]->active = 0;
1596 if ((p = hash_find (fnames, ctx->hdrs[i]->path)) && p->h &&
1597 (mutt_cmp_header (ctx->hdrs[i], p->h))) {
1598 ctx->hdrs[i]->active = 1;
1599 /* found the right message */
1600 if (!ctx->hdrs[i]->changed)
1601 maildir_update_flags (ctx, ctx->hdrs[i], p->h);
1603 header_delete(&p->h);
1605 else /* message has disappeared */
1609 /* destroy the file name hash */
1611 hash_delete (&fnames, NULL);
1613 /* If we didn't just get new mail, update the tables. */
1615 maildir_update_tables (ctx, index_hint);
1617 /* Incorporate new messages */
1618 have_new = maildir_move_to_context (ctx, &md);
1620 return occult ? M_REOPENED : (have_new ? M_NEW_MAIL : 0);
1625 * These functions try to find a message in a maildir folder when it
1626 * has moved under our feet. Note that this code is rather expensive, but
1627 * then again, it's called rarely.
1629 static FILE *_maildir_open_find_message(const char *folder, const char *unique,
1630 const char *subfolder)
1632 char dir[_POSIX_PATH_MAX];
1633 char tunique[_POSIX_PATH_MAX];
1634 char fname[_POSIX_PATH_MAX];
1642 snprintf (dir, sizeof (dir), "%s/%s", folder, subfolder);
1644 if ((dp = opendir (dir)) == NULL) {
1649 while ((de = readdir (dp))) {
1650 maildir_canon_filename (tunique, de->d_name, sizeof (tunique));
1652 if (!m_strcmp(tunique, unique)) {
1653 snprintf (fname, sizeof (fname), "%s/%s/%s", folder, subfolder,
1655 fp = fopen(fname, "r");
1667 FILE *maildir_open_find_message (const char *folder, const char *msg)
1669 char unique[_POSIX_PATH_MAX];
1672 static unsigned int new_hits = 0, cur_hits = 0; /* simple dynamic optimization */
1674 maildir_canon_filename (unique, msg, sizeof (unique));
1677 _maildir_open_find_message (folder, unique,
1678 new_hits > cur_hits ? "new" : "cur"))
1679 || errno != ENOENT) {
1680 if (new_hits < UINT_MAX && cur_hits < UINT_MAX) {
1681 new_hits += (new_hits > cur_hits ? 1 : 0);
1682 cur_hits += (new_hits > cur_hits ? 0 : 1);
1688 _maildir_open_find_message (folder, unique,
1689 new_hits > cur_hits ? "cur" : "new"))
1690 || errno != ENOENT) {
1691 if (new_hits < UINT_MAX && cur_hits < UINT_MAX) {
1692 new_hits += (new_hits > cur_hits ? 0 : 1);
1693 cur_hits += (new_hits > cur_hits ? 1 : 0);
1705 * 1 if there are no messages in the mailbox
1706 * 0 if there are messages in the mailbox
1709 static int maildir_check_empty (const char *path)
1713 int r = 1; /* assume empty until we find a message */
1714 char frealpath[_POSIX_PATH_MAX];
1717 /* Strategy here is to look for any file not beginning with a period */
1720 /* we do "cur" on the first iteration since its more likely that we'll
1721 * find old messages without having to scan both subdirs
1723 snprintf (frealpath, sizeof (frealpath), "%s/%s", path,
1724 iter == 0 ? "cur" : "new");
1725 if ((dp = opendir (frealpath)) == NULL)
1727 while ((de = readdir (dp))) {
1728 if (*de->d_name != '.') {
1735 } while (r && iter < 2);
1742 * 1 if there are no messages in the mailbox
1743 * 0 if there are messages in the mailbox
1746 static int mh_check_empty (const char *path)
1750 int r = 1; /* assume empty until we find a message */
1752 if ((dp = opendir (path)) == NULL)
1754 while ((de = readdir (dp))) {
1755 if (mh_valid_message (de->d_name)) {
1765 static int mh_is_magic(const char *path, struct stat *st)
1767 static char const * const files[] = {
1768 ".mh_sequences", ".xmhcache", ".mew_cache",
1769 ".mew-cache", ".sylpheed_cache",
1772 if (S_ISDIR(st->st_mode)) {
1773 for (int i = 0; i < countof(files); i++) {
1774 char tmp[_POSIX_PATH_MAX];
1776 snprintf(tmp, sizeof(tmp), "%s/%s", path, files[i]);
1777 if (access(tmp, F_OK) == 0)
1785 static int maildir_is_magic (const char *path, struct stat *st)
1787 if (S_ISDIR(st->st_mode)) {
1788 char tmp[_POSIX_PATH_MAX];
1791 snprintf(tmp, sizeof(tmp), "%s/cur", path);
1792 if (stat(tmp, &sb) == 0 && S_ISDIR(sb.st_mode))
1799 static int mh_commit (MESSAGE* msg, CONTEXT* ctx) {
1800 return (mh_commit_message (msg, ctx, NULL));
1803 static int maildir_commit (MESSAGE* msg, CONTEXT* ctx) {
1804 return (maildir_commit_message (msg, ctx, NULL));
1807 mx_t const maildir_mx = {
1811 maildir_check_empty,
1814 maildir_open_new_message,
1816 maildir_check_mailbox,
1822 mx_t const mh_mx = {
1829 mh_open_new_message,