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)
89 static void mh_read_token (char *t, int *first, int *last)
93 if ((p = strchr (t, '-'))) {
99 *first = *last = atoi (t);
102 static void mh_read_sequences (struct mh_sequences *mhs, const char *path)
113 char pathname[_POSIX_PATH_MAX];
115 snprintf (pathname, sizeof (pathname), "%s/.mh_sequences", path);
117 if (!(fp = fopen (pathname, "r")))
120 while ((buff = mutt_read_line (buff, &sz, fp, &line))) {
121 if (!(t = strtok (buff, " \t:")))
124 if (!m_strcmp(t, MhUnseen))
126 else if (!m_strcmp(t, MhFlagged))
128 else if (!m_strcmp(t, MhReplied))
130 else /* unknown sequence */
133 while ((t = strtok (NULL, " \t:"))) {
134 mh_read_token (t, &first, &last);
135 for (; first <= last; first++)
136 mhs_set (mhs, first, f);
144 int mh_buffy (const char *path)
147 struct mh_sequences mhs;
151 mh_read_sequences (&mhs, path);
152 for (i = 0; !r && i <= mhs.max; i++)
153 if (mhs_check (&mhs, i) & MH_SEQ_UNSEEN)
155 mhs_free_sequences (&mhs);
159 static int mh_mkstemp (CONTEXT * dest, FILE ** fp, char **tgt)
161 static int Counter = 0;
163 char path[_POSIX_PATH_MAX];
166 snprintf (path, _POSIX_PATH_MAX, "%s/.mutt-%s-%d-%d",
167 dest->path, NONULL(MCore.shorthost), (int) getpid (), Counter++);
169 if ((fd = open (path, O_WRONLY | O_EXCL | O_CREAT, 0666)) == -1) {
170 if (errno != EEXIST) {
176 *tgt = m_strdup(path);
181 if ((*fp = fdopen (fd, "w")) == NULL) {
191 static void mhs_write_one_sequence (FILE * fp, struct mh_sequences *mhs,
192 short f, const char *tag)
197 fprintf (fp, "%s:", tag);
202 for (i = 0; i <= mhs->max; i++) {
203 if ((mhs_check (mhs, i) & f)) {
209 else if (first >= 0) {
211 fprintf (fp, " %d", first);
213 fprintf (fp, " %d-%d", first, last);
222 fprintf (fp, " %d", first);
224 fprintf (fp, " %d-%d", first, last);
230 /* XXX - we don't currently remove deleted messages from sequences we don't know. Should we? */
232 static void mh_update_sequences (CONTEXT * ctx)
236 char sequences[_POSIX_PATH_MAX];
248 char seq_unseen[STRING];
249 char seq_replied[STRING];
250 char seq_flagged[STRING];
253 struct mh_sequences mhs;
257 snprintf (seq_unseen, sizeof (seq_unseen), "%s:", NONULL (MhUnseen));
258 snprintf (seq_replied, sizeof (seq_replied), "%s:", NONULL (MhReplied));
259 snprintf (seq_flagged, sizeof (seq_flagged), "%s:", NONULL (MhFlagged));
261 if (mh_mkstemp (ctx, &nfp, &tmpfname) != 0) {
266 snprintf (sequences, sizeof (sequences), "%s/.mh_sequences", ctx->path);
269 /* first, copy unknown sequences */
270 if ((ofp = fopen (sequences, "r"))) {
271 while ((buff = mutt_read_line (buff, &s, ofp, &l))) {
272 if (!m_strncmp(buff, seq_unseen, m_strlen(seq_unseen)))
274 if (!m_strncmp(buff, seq_flagged, m_strlen(seq_flagged)))
276 if (!m_strncmp(buff, seq_replied, m_strlen(seq_replied)))
279 fprintf (nfp, "%s\n", buff);
284 /* now, update our unseen, flagged, and replied sequences */
285 for (l = 0; l < ctx->msgcount; l++) {
286 if (ctx->hdrs[l]->deleted)
289 if ((p = strrchr (ctx->hdrs[l]->path, '/')))
292 p = ctx->hdrs[l]->path;
296 if (!ctx->hdrs[l]->read) {
297 mhs_set (&mhs, i, MH_SEQ_UNSEEN);
300 if (ctx->hdrs[l]->flagged) {
301 mhs_set (&mhs, i, MH_SEQ_FLAGGED);
304 if (ctx->hdrs[l]->replied) {
305 mhs_set (&mhs, i, MH_SEQ_REPLIED);
310 /* write out the new sequences */
312 mhs_write_one_sequence (nfp, &mhs, MH_SEQ_UNSEEN, NONULL (MhUnseen));
314 mhs_write_one_sequence (nfp, &mhs, MH_SEQ_FLAGGED, NONULL (MhFlagged));
316 mhs_write_one_sequence (nfp, &mhs, MH_SEQ_REPLIED, NONULL (MhReplied));
318 mhs_free_sequences (&mhs);
321 /* try to commit the changes - no guarantee here */
325 if (safe_rename (tmpfname, sequences) != 0) {
326 /* report an error? */
333 static void mh_sequences_add_one (CONTEXT * ctx, int n, short unseen,
334 short flagged, short replied)
336 short unseen_done = 0;
337 short flagged_done = 0;
338 short replied_done = 0;
340 FILE *ofp = NULL, *nfp = NULL;
343 char sequences[_POSIX_PATH_MAX];
345 char seq_unseen[STRING];
346 char seq_replied[STRING];
347 char seq_flagged[STRING];
353 if (mh_mkstemp (ctx, &nfp, &tmpfname) == -1)
356 snprintf (seq_unseen, sizeof (seq_unseen), "%s:", NONULL (MhUnseen));
357 snprintf (seq_replied, sizeof (seq_replied), "%s:", NONULL (MhReplied));
358 snprintf (seq_flagged, sizeof (seq_flagged), "%s:", NONULL (MhFlagged));
360 snprintf (sequences, sizeof (sequences), "%s/.mh_sequences", ctx->path);
361 if ((ofp = fopen (sequences, "r"))) {
362 while ((buff = mutt_read_line (buff, &sz, ofp, &line))) {
363 if (unseen && !m_strncmp (buff, seq_unseen, m_strlen(seq_unseen))) {
364 fprintf (nfp, "%s %d\n", buff, n);
368 && !m_strncmp (buff, seq_flagged, m_strlen(seq_flagged))) {
369 fprintf (nfp, "%s %d\n", buff, n);
373 && !m_strncmp (buff, seq_replied, m_strlen(seq_replied))) {
374 fprintf (nfp, "%s %d\n", buff, n);
378 fprintf (nfp, "%s\n", buff);
384 if (!unseen_done && unseen)
385 fprintf (nfp, "%s: %d\n", NONULL (MhUnseen), n);
386 if (!flagged_done && flagged)
387 fprintf (nfp, "%s: %d\n", NONULL (MhFlagged), n);
388 if (!replied_done && replied)
389 fprintf (nfp, "%s: %d\n", NONULL (MhReplied), n);
394 if (safe_rename (tmpfname, sequences) != 0)
400 static void mh_update_maildir (struct maildir *md, struct mh_sequences *mhs)
406 for (; md; md = md->next) {
407 if ((p = strrchr (md->h->path, '/')))
413 f = mhs_check (mhs, i);
415 md->h->read = (f & MH_SEQ_UNSEEN) ? 0 : 1;
416 md->h->flagged = (f & MH_SEQ_FLAGGED) ? 1 : 0;
417 md->h->replied = (f & MH_SEQ_REPLIED) ? 1 : 0;
421 /* maildir support */
423 static void maildir_free_entry (struct maildir **md)
428 p_delete(&(*md)->canon_fname);
430 header_delete(&(*md)->h);
435 static void maildir_free_maildir (struct maildir **md)
437 struct maildir *p, *q;
442 for (p = *md; p; p = q) {
444 maildir_free_entry (&p);
448 static void maildir_parse_flags (HEADER * h, const char *path)
456 if ((p = strrchr (path, ':')) != NULL && m_strncmp(p + 1, "2,", 2) == 0) {
459 m_strreplace(&h->maildir_flags, p);
460 q = h->maildir_flags;
474 case 'R': /* replied */
479 case 'T': /* trashed */
492 if (q == h->maildir_flags)
493 p_delete(&h->maildir_flags);
498 static void maildir_update_mtime (CONTEXT * ctx)
500 char buf[_POSIX_PATH_MAX];
503 if (ctx->magic == M_MAILDIR) {
504 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, "cur");
505 if (stat (buf, &st) == 0)
506 ctx->mtime_cur = st.st_mtime;
507 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, "new");
510 snprintf (buf, sizeof (buf), "%s/.mh_sequences", ctx->path);
511 if (stat (buf, &st) == 0)
512 ctx->mtime_cur = st.st_mtime;
514 m_strcpy(buf, sizeof(buf), ctx->path);
517 if (stat (buf, &st) == 0)
518 ctx->mtime = st.st_mtime;
522 * Actually parse a maildir message. This may also be used to fill
523 * out a fake header structure generated by lazy maildir parsing.
525 static HEADER *maildir_parse_message (int magic, const char *fname,
526 int is_old, HEADER * _h)
532 if ((f = fopen (fname, "r")) != NULL) {
535 h->env = mutt_read_rfc822_header (f, h, 0, 0);
537 fstat (fileno (f), &st);
541 h->received = h->date_sent;
543 if (h->content->length <= 0)
544 h->content->length = st.st_size - h->content->offset;
548 if (magic == M_MAILDIR) {
550 * maildir stores its flags in the filename, so ignore the
551 * flags in the header of the message
555 maildir_parse_flags (h, fname);
563 * Note that this routine will _not_ modify the context given by
566 * It's used in the first parsing pass on maildir and MH folders.
567 * In the MH case, this means full parsing of the folder. In the
568 * maildir case, it means that we only look at flags, and create a
569 * fake HEADER structure, which may later be filled in by
570 * maildir_parse_message(), when called from
571 * maildir_delayed_parsing().
575 static int maildir_parse_entry (CONTEXT * ctx, struct maildir ***last,
576 const char *subdir, const char *fname,
577 int *count, int is_old, ino_t inode __attribute__ ((unused)))
579 struct maildir *entry;
581 char buf[_POSIX_PATH_MAX];
584 snprintf (buf, sizeof (buf), "%s/%s/%s", ctx->path, subdir, fname);
586 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, fname);
588 if (ctx->magic == M_MH)
589 h = maildir_parse_message (ctx->magic, buf, is_old, NULL);
593 maildir_parse_flags (h, buf);
599 if (!ctx->quiet && ReadInc && ((*count % ReadInc) == 0 || *count == 1))
600 mutt_message (_("Reading %s... %d"), ctx->path, *count);
604 snprintf (buf, sizeof (buf), "%s/%s", subdir, fname);
605 h->path = m_strdup(buf);
608 h->path = m_strdup(fname);
610 entry = p_new(struct maildir, 1);
612 entry->header_parsed = (ctx->magic == M_MH);
614 *last = &entry->next;
624 /* Ignore the garbage files. A valid MH message consists of only
625 * digits. Deleted message get moved to a filename with a comma before
629 int mh_valid_message (const char *s)
632 if (!isdigit ((unsigned char) *s))
638 static int maildir_parse_dir (CONTEXT * ctx, struct maildir ***last,
639 const char *subdir, int *count)
643 char buf[_POSIX_PATH_MAX];
647 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, subdir);
648 is_old = (m_strcmp("cur", subdir) == 0);
651 m_strcpy(buf, sizeof(buf), ctx->path);
653 if ((dirp = opendir (buf)) == NULL)
656 while ((de = readdir (dirp)) != NULL) {
658 if ((ctx->magic == M_MH && !mh_valid_message (de->d_name))
659 || (ctx->magic == M_MAILDIR && *de->d_name == '.'))
662 /* FOO - really ignore the return value? */
664 maildir_parse_entry (ctx, last, subdir, de->d_name, count, is_old,
665 #if HAVE_DIRENT_D_INO
677 static int maildir_add_to_context (CONTEXT * ctx, struct maildir *md)
679 int oldmsgcount = ctx->msgcount;
684 if (ctx->msgcount == ctx->hdrmax)
685 mx_alloc_memory (ctx);
687 ctx->hdrs[ctx->msgcount] = md->h;
688 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
690 md->h->content->length + md->h->content->offset -
691 md->h->content->hdr_offset;
699 if (ctx->msgcount > oldmsgcount) {
700 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
706 static int maildir_move_to_context (CONTEXT * ctx, struct maildir **md)
710 r = maildir_add_to_context (ctx, *md);
711 maildir_free_maildir (md);
716 static ssize_t maildir_hcache_keylen (const char *fn)
718 return m_strchrnul(fn, ':') - fn;
723 * This function does the second parsing pass for a maildir-style
726 static void maildir_delayed_parsing (CONTEXT * ctx, struct maildir *md)
729 char fn[_POSIX_PATH_MAX];
735 struct timeval *when = NULL;
736 struct stat lastchanged;
739 hc = mutt_hcache_open (HeaderCache, ctx->path);
742 for (p = md, count = 0; p; p = p->next, count++) {
743 if (!(p && p->h && !p->header_parsed))
747 data = mutt_hcache_fetch (hc, p->h->path + 3, &maildir_hcache_keylen);
748 when = (struct timeval *) data;
751 if (!ctx->quiet && ReadInc && ((count % ReadInc) == 0 || count == 1))
752 mutt_message (_("Reading %s... %d"), ctx->path, count);
753 snprintf (fn, sizeof (fn), "%s/%s", ctx->path, p->h->path);
756 ret = stat(fn, &lastchanged);
757 if (data && !ret && lastchanged.st_mtime <= when->tv_sec) {
758 p->h = mutt_hcache_restore(data, &p->h);
759 maildir_parse_flags (p->h, fn);
762 if (maildir_parse_message (ctx->magic, fn, p->h->old, p->h)) {
763 p->header_parsed = 1;
764 maildir_parse_flags (p->h, fn);
766 mutt_hcache_store (hc, p->h->path + 3, p->h, 0, &maildir_hcache_keylen);
769 header_delete(&p->h);
776 mutt_hcache_close (&hc);
780 /* Read a MH/maildir style mailbox.
783 * ctx [IN/OUT] context for this mailbox
784 * subdir [IN] NULL for MH mailboxes, otherwise the subdir of the
785 * maildir mailbox to read from
787 static int _mh_read_dir (CONTEXT * ctx, const char *subdir)
790 struct mh_sequences mhs;
791 struct maildir **last;
797 maildir_update_mtime (ctx);
802 if (maildir_parse_dir (ctx, &last, subdir, &count) == -1)
805 if (ctx->magic == M_MH) {
806 mh_read_sequences (&mhs, ctx->path);
807 mh_update_maildir (md, &mhs);
808 mhs_free_sequences (&mhs);
811 if (ctx->magic == M_MAILDIR)
812 maildir_delayed_parsing (ctx, md);
814 maildir_move_to_context (ctx, &md);
818 static int mh_read_dir (CONTEXT* ctx) {
819 return (_mh_read_dir (ctx, NULL));
822 /* read a maildir style mailbox */
823 static int maildir_read_dir (CONTEXT * ctx)
825 /* maildir looks sort of like MH, except that there are two subdirectories
826 * of the main folder path from which to read messages
828 if (_mh_read_dir (ctx, "new") == -1 || _mh_read_dir (ctx, "cur") == -1)
835 * Open a new (temporary) message in an MH folder.
838 static int mh_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr __attribute__ ((unused)))
840 return mh_mkstemp (dest, &msg->fp, &msg->path);
843 static int ch_compar (const void *a, const void *b)
845 return (int) (*((const char *) a) - *((const char *) b));
848 static void maildir_flags (char *dest, ssize_t destlen, HEADER * hdr)
853 * The maildir specification requires that all files in the cur
854 * subdirectory have the :unique string appeneded, regardless of whether
855 * or not there are any flags. If .old is set, we know that this message
856 * will end up in the cur directory, so we include it in the following
857 * test even though there is no associated flag.
861 && (hdr->flagged || hdr->replied || hdr->read || hdr->deleted
862 || hdr->old || hdr->maildir_flags)) {
863 char tmp[LONG_STRING];
865 snprintf (tmp, sizeof (tmp),
867 hdr->flagged ? "F" : "",
868 hdr->replied ? "R" : "",
869 hdr->read ? "S" : "", hdr->deleted ? "T" : "",
870 NONULL (hdr->maildir_flags));
871 if (hdr->maildir_flags)
872 qsort (tmp, m_strlen(tmp), 1, ch_compar);
873 snprintf (dest, destlen, ":2,%s", tmp);
879 * Open a new (temporary) message in a maildir folder.
881 * Note that this uses _almost_ the maildir file name format, but
882 * with a {cur,new} prefix.
886 static int maildir_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr)
888 static int Counter = 0;
890 char path[_POSIX_PATH_MAX];
895 short deleted = hdr->deleted;
899 maildir_flags (suffix, sizeof (suffix), hdr);
901 hdr->deleted = deleted;
906 if (hdr && (hdr->read || hdr->old))
907 m_strcpy(subdir, sizeof(subdir), "cur");
909 m_strcpy(subdir, sizeof(subdir), "new");
912 snprintf (path, _POSIX_PATH_MAX, "%s/tmp/%s.%ld.%u_%d.%s%s",
913 dest->path, subdir, (long) time (NULL),
914 (unsigned int) getpid (), Counter++, NONULL (MCore.shorthost), suffix);
917 if ((fd = open (path, O_WRONLY | O_EXCL | O_CREAT, 0666)) == -1) {
918 if (errno != EEXIST) {
923 msg->path = m_strdup(path);
928 if ((msg->fp = fdopen (fd, "w")) == NULL) {
929 p_delete(&msg->path);
941 * Commit a message to a maildir folder.
943 * msg->path contains the file name of a file in tmp/. We take the
944 * flags from this file's name.
946 * ctx is the mail folder we commit to.
948 * hdr is a header structure to which we write the message's new
949 * file name. This is used in the mh and maildir folder synch
950 * routines. When this routine is invoked from mx_commit_message,
953 * msg->path looks like this:
955 * tmp/{cur,new}.mutt-HOSTNAME-PID-COUNTER:flags
957 * See also maildir_open_new_message().
961 static int maildir_commit_message (MESSAGE * msg, CONTEXT * ctx, HEADER * hdr)
963 static int Counter = 0;
966 char path[_POSIX_PATH_MAX];
967 char full[_POSIX_PATH_MAX];
970 if (m_fclose(&msg->fp) != 0)
973 /* extract the subdir */
974 s = strrchr (msg->path, '/') + 1;
975 m_strcpy(subdir, sizeof(subdir), s);
977 /* extract the flags */
978 if ((s = strchr (s, ':')))
979 m_strcpy(suffix, sizeof(suffix), s);
983 /* construct a new file name. */
985 snprintf (path, _POSIX_PATH_MAX, "%s/%ld.%u_%d.%s%s", subdir,
986 (long) time (NULL), (unsigned int) getpid (), Counter++,
987 NONULL (MCore.shorthost), suffix);
988 snprintf (full, _POSIX_PATH_MAX, "%s/%s", ctx->path, path);
990 if (safe_rename (msg->path, full) == 0) {
992 m_strreplace(&hdr->path, path);
993 p_delete(&msg->path);
996 * Adjust the mtime on the file to match the time at which this
997 * message was received. Currently this is only set when copying
998 * messages between mailboxes, so we test to ensure that it is
1001 if (msg->received) {
1004 ut.actime = msg->received;
1005 ut.modtime = msg->received;
1006 if (utime (full, &ut)) {
1008 ("maildir_commit_message(): unable to set time on file"));
1015 else if (errno != EEXIST) {
1016 mutt_perror (ctx->path);
1023 * commit a message to an MH folder.
1028 static int _mh_commit_message (MESSAGE * msg, CONTEXT * ctx, HEADER * hdr,
1034 unsigned int n, hi = 0;
1035 char path[_POSIX_PATH_MAX];
1038 if (m_fclose(&msg->fp) != 0)
1041 if ((dirp = opendir (ctx->path)) == NULL) {
1042 mutt_perror (ctx->path);
1046 /* figure out what the next message number is */
1047 while ((de = readdir (dirp)) != NULL) {
1053 if (!isdigit ((unsigned char) *cp))
1066 * Now try to rename the file to the proper name.
1068 * Note: We may have to try multiple times, until we find a free
1074 snprintf (tmp, sizeof (tmp), "%d", hi);
1075 snprintf (path, sizeof (path), "%s/%s", ctx->path, tmp);
1076 if (safe_rename (msg->path, path) == 0) {
1078 m_strreplace(&hdr->path, tmp);
1079 p_delete(&msg->path);
1082 else if (errno != EEXIST) {
1083 mutt_perror (ctx->path);
1088 mh_sequences_add_one (ctx, hi, !msg->flags.read, msg->flags.flagged,
1089 msg->flags.replied);
1093 static int mh_commit_message (MESSAGE * msg, CONTEXT * ctx, HEADER * hdr) {
1094 return _mh_commit_message (msg, ctx, hdr, 1);
1097 /* Sync a message in an MH folder.
1099 * This code is also used for attachment deletion in maildir
1103 static int mh_rewrite_message (CONTEXT * ctx, int msgno)
1105 HEADER *h = ctx->hdrs[msgno];
1110 char oldpath[_POSIX_PATH_MAX];
1111 char newpath[_POSIX_PATH_MAX];
1112 char partpath[_POSIX_PATH_MAX];
1114 long old_body_offset = h->content->offset;
1115 long old_body_length = h->content->length;
1116 long old_hdr_lines = h->lines;
1118 if ((dest = mx_open_new_message (ctx, h, 0)) == NULL)
1121 if ((rc = mutt_copy_message (dest->fp, ctx, h,
1122 M_CM_UPDATE, CH_UPDATE | CH_UPDATE_LEN)) == 0)
1124 snprintf (oldpath, sizeof(oldpath), "%s/%s", ctx->path, h->path);
1125 m_strcpy(partpath, sizeof(partpath), h->path);
1127 if (ctx->magic == M_MAILDIR)
1128 rc = maildir_commit_message (dest, ctx, h);
1130 rc = _mh_commit_message (dest, ctx, h, 0);
1132 mx_close_message (&dest);
1140 * Try to move the new message to the old place.
1143 * This is important when we are just updating flags.
1145 * Note that there is a race condition against programs which
1146 * use the first free slot instead of the maximum message
1147 * number. Mutt does _not_ behave like this.
1149 * Anyway, if this fails, the message is in the folder, so
1150 * all what happens is that a concurrently runnung mutt will
1151 * lose flag modifications.
1154 if (ctx->magic == M_MH && rc == 0) {
1155 snprintf (newpath, _POSIX_PATH_MAX, "%s/%s", ctx->path, h->path);
1156 if ((rc = safe_rename (newpath, oldpath)) == 0)
1157 m_strreplace(&h->path, partpath);
1161 mx_close_message (&dest);
1163 if (rc == -1 && restore) {
1164 h->content->offset = old_body_offset;
1165 h->content->length = old_body_length;
1166 h->lines = old_hdr_lines;
1169 body_list_wipe(&h->content->parts);
1173 static int mh_sync_message (CONTEXT * ctx, int msgno)
1175 HEADER *h = ctx->hdrs[msgno];
1177 if (h->attach_del ||
1178 (h->env && (h->env->refs_changed || h->env->irt_changed)))
1179 if (mh_rewrite_message (ctx, msgno) != 0)
1185 static int maildir_sync_message (CONTEXT * ctx, int msgno)
1187 HEADER *h = ctx->hdrs[msgno];
1189 if (h->attach_del ||
1190 (h->env && (h->env->refs_changed || h->env->irt_changed))) {
1191 /* when doing attachment deletion/rethreading, fall back to the MH case. */
1192 if (mh_rewrite_message (ctx, msgno) != 0)
1196 /* we just have to rename the file. */
1198 char newpath[_POSIX_PATH_MAX];
1199 char partpath[_POSIX_PATH_MAX];
1200 char fullpath[_POSIX_PATH_MAX];
1201 char oldpath[_POSIX_PATH_MAX];
1205 if ((p = strrchr (h->path, '/')) == NULL) {
1209 m_strcpy(newpath, sizeof(newpath), p);
1211 /* kill the previous flags */
1212 if ((p = strchr (newpath, ':')) != NULL)
1215 maildir_flags (suffix, sizeof (suffix), h);
1217 snprintf (partpath, sizeof (partpath), "%s/%s%s",
1218 (h->read || h->old) ? "cur" : "new", newpath, suffix);
1219 snprintf (fullpath, sizeof (fullpath), "%s/%s", ctx->path, partpath);
1220 snprintf (oldpath, sizeof (oldpath), "%s/%s", ctx->path, h->path);
1222 if (m_strcmp(fullpath, oldpath) == 0) {
1223 /* message hasn't really changed */
1227 /* record that the message is possibly marked as trashed on disk */
1228 h->trash = h->deleted;
1230 if (rename (oldpath, fullpath) != 0) {
1231 mutt_perror ("rename");
1234 m_strreplace(&h->path, partpath);
1239 static int mh_sync_mailbox (CONTEXT * ctx, int unused __attribute__ ((unused)), int *index_hint)
1241 char path[_POSIX_PATH_MAX], tmp[_POSIX_PATH_MAX];
1246 #endif /* USE_HCACHE */
1248 if (ctx->magic == M_MH)
1249 i = mh_check_mailbox (ctx, index_hint, 0);
1251 i = maildir_check_mailbox (ctx, index_hint, 0);
1257 if (ctx->magic == M_MAILDIR)
1258 hc = mutt_hcache_open (HeaderCache, ctx->path);
1259 #endif /* USE_HCACHE */
1261 for (i = 0; i < ctx->msgcount; i++) {
1262 if (ctx->hdrs[i]->deleted
1263 && (ctx->magic != M_MAILDIR || !option (OPTMAILDIRTRASH))) {
1264 snprintf (path, sizeof (path), "%s/%s", ctx->path, ctx->hdrs[i]->path);
1265 if (ctx->magic == M_MAILDIR
1266 || (option (OPTMHPURGE) && ctx->magic == M_MH)) {
1268 if (ctx->magic == M_MAILDIR)
1269 mutt_hcache_delete (hc, ctx->hdrs[i]->path + 3,
1270 &maildir_hcache_keylen);
1271 #endif /* USE_HCACHE */
1274 else if (ctx->magic == M_MH) {
1275 /* MH just moves files out of the way when you delete them */
1276 if (*ctx->hdrs[i]->path != ',') {
1277 snprintf (tmp, sizeof (tmp), "%s/,%s", ctx->path,
1278 ctx->hdrs[i]->path);
1285 else if (ctx->hdrs[i]->changed || ctx->hdrs[i]->attach_del ||
1286 (ctx->magic == M_MAILDIR
1287 && (option (OPTMAILDIRTRASH) || ctx->hdrs[i]->trash)
1288 && (ctx->hdrs[i]->deleted != ctx->hdrs[i]->trash))) {
1289 if (ctx->magic == M_MAILDIR) {
1290 if (maildir_sync_message (ctx, i) == -1)
1294 if (mh_sync_message (ctx, i) == -1)
1301 if (ctx->magic == M_MAILDIR)
1302 mutt_hcache_close (hc);
1303 #endif /* USE_HCACHE */
1305 if (ctx->magic == M_MH)
1306 mh_update_sequences (ctx);
1308 /* XXX race condition? */
1310 maildir_update_mtime (ctx);
1312 /* adjust indices */
1315 for (i = 0, j = 0; i < ctx->msgcount; i++) {
1316 if (!ctx->hdrs[i]->deleted
1317 || (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))
1318 ctx->hdrs[i]->index = j++;
1326 if (ctx->magic == M_MAILDIR)
1327 mutt_hcache_close (hc);
1328 #endif /* USE_HCACHE */
1332 static char *maildir_canon_filename (char *dest, const char *src, ssize_t l)
1336 if ((t = strrchr (src, '/')))
1339 m_strcpy(dest, l, src);
1340 if ((u = strrchr (dest, ':')))
1346 static void maildir_update_tables (CONTEXT * ctx, int *index_hint)
1352 if (Sort != SORT_ORDER) {
1355 mutt_sort_headers (ctx, 1);
1359 old_count = ctx->msgcount;
1360 for (i = 0, j = 0; i < old_count; i++) {
1361 if (ctx->hdrs[i]->active && index_hint && *index_hint == i)
1364 if (ctx->hdrs[i]->active)
1365 ctx->hdrs[i]->index = j++;
1368 mx_update_tables (ctx, 0);
1369 mutt_clear_threads (ctx);
1372 static void maildir_update_flags (CONTEXT * ctx, HEADER * o, HEADER * n)
1374 /* save the global state here so we can reset it at the
1375 * end of list block if required.
1377 int context_changed = ctx->changed;
1379 /* user didn't modify this message. alter the flags to
1380 * match the current state on disk. This may not actually
1381 * do anything, but we can't tell right now. mutt_set_flag()
1382 * will just ignore the call if the status bits are
1383 * already properly set.
1385 mutt_set_flag (ctx, o, M_FLAG, n->flagged);
1386 mutt_set_flag (ctx, o, M_REPLIED, n->replied);
1387 mutt_set_flag (ctx, o, M_READ, n->read);
1388 mutt_set_flag (ctx, o, M_OLD, n->old);
1390 /* mutt_set_flag() will set this, but we don't need to
1391 * sync the changes we made because we just updated the
1392 * context to match the current on-disk state of the
1397 /* if the mailbox was not modified before we made these
1398 * changes, unset the changed flag since nothing needs to
1401 if (!context_changed)
1406 /* This function handles arrival of new mail and reopening of
1407 * maildir folders. The basic idea here is we check to see if either
1408 * the new or cur subdirectories have changed, and if so, we scan them
1409 * for the list of files. We check for newly added messages, and
1410 * then merge the flags messages we already knew about. We don't treat
1411 * either subdirectory differently, as mail could be copied directly into
1412 * the cur directory from another agent.
1414 static int maildir_check_mailbox (CONTEXT * ctx, int *index_hint, int unused __attribute__ ((unused)))
1416 struct stat st_new; /* status of the "new" subdirectory */
1417 struct stat st_cur; /* status of the "cur" subdirectory */
1418 char buf[_POSIX_PATH_MAX];
1419 int changed = 0; /* bitmask representing which subdirectories
1420 have changed. 0x1 = new, 0x2 = cur */
1421 int occult = 0; /* messages were removed from the mailbox */
1422 int have_new = 0; /* messages were added to the mailbox */
1423 struct maildir *md; /* list of messages in the mailbox */
1424 struct maildir **last, *p;
1426 hash_t *fnames; /* hash table for quickly looking up the base filename
1427 for a maildir message */
1429 if (!option (OPTCHECKNEW))
1432 snprintf (buf, sizeof (buf), "%s/new", ctx->path);
1433 if (stat (buf, &st_new) == -1)
1436 snprintf (buf, sizeof (buf), "%s/cur", ctx->path);
1437 if (stat (buf, &st_cur) == -1)
1440 /* determine which subdirectories need to be scanned */
1441 if (st_new.st_mtime > ctx->mtime)
1443 if (st_cur.st_mtime > ctx->mtime_cur)
1447 return 0; /* nothing to do */
1449 /* update the modification times on the mailbox */
1450 ctx->mtime_cur = st_cur.st_mtime;
1451 ctx->mtime = st_new.st_mtime;
1453 /* do a fast scan of just the filenames in
1454 * the subdirectories that have changed.
1459 maildir_parse_dir (ctx, &last, "new", NULL);
1461 maildir_parse_dir (ctx, &last, "cur", NULL);
1463 /* we create a hash table keyed off the canonical (sans flags) filename
1464 * of each message we scanned. This is used in the loop over the
1465 * existing messages below to do some correlation.
1467 fnames = hash_new (1031, 0);
1469 for (p = md; p; p = p->next) {
1470 maildir_canon_filename (buf, p->h->path, sizeof (buf));
1471 p->canon_fname = m_strdup(buf);
1472 hash_insert (fnames, p->canon_fname, p);
1475 /* check for modifications and adjust flags */
1476 for (i = 0; i < ctx->msgcount; i++) {
1477 ctx->hdrs[i]->active = 0;
1478 maildir_canon_filename (buf, ctx->hdrs[i]->path, sizeof (buf));
1479 p = hash_find (fnames, buf);
1481 /* message already exists, merge flags */
1482 ctx->hdrs[i]->active = 1;
1484 /* check to see if the message has moved to a different
1485 * subdirectory. If so, update the associated filename.
1487 if (m_strcmp(ctx->hdrs[i]->path, p->h->path))
1488 m_strreplace(&ctx->hdrs[i]->path, p->h->path);
1490 /* if the user hasn't modified the flags on this message, update
1491 * the flags we just detected.
1493 if (!ctx->hdrs[i]->changed)
1494 maildir_update_flags (ctx, ctx->hdrs[i], p->h);
1496 if (ctx->hdrs[i]->deleted == ctx->hdrs[i]->trash)
1497 ctx->hdrs[i]->deleted = p->h->deleted;
1498 ctx->hdrs[i]->trash = p->h->trash;
1500 /* this is a duplicate of an existing header, so remove it */
1501 header_delete(&p->h);
1503 /* This message was not in the list of messages we just scanned.
1504 * Check to see if we have enough information to know if the
1505 * message has disappeared out from underneath us.
1507 else if (((changed & 1) && (!m_strncmp (ctx->hdrs[i]->path, "new/", 4))) ||
1508 ((changed & 2) && (!m_strncmp (ctx->hdrs[i]->path, "cur/", 4)))) {
1509 /* This message disappeared, so we need to simulate a "reopen"
1510 * event. We know it disappeared because we just scanned the
1511 * subdirectory it used to reside in.
1516 /* This message resides in a subdirectory which was not
1517 * modified, so we assume that it is still present and
1520 ctx->hdrs[i]->active = 1;
1524 /* destroy the file name hash */
1525 hash_delete (&fnames, NULL);
1527 /* If we didn't just get new mail, update the tables. */
1529 maildir_update_tables (ctx, index_hint);
1531 /* do any delayed parsing we need to do. */
1532 maildir_delayed_parsing (ctx, md);
1534 /* Incorporate new messages */
1535 have_new = maildir_move_to_context (ctx, &md);
1537 return occult ? M_REOPENED : (have_new ? M_NEW_MAIL : 0);
1541 * This function handles arrival of new mail and reopening of
1542 * mh/maildir folders. Things are getting rather complex because we
1543 * don't have a well-defined "mailbox order", so the tricks from
1544 * mbox.c and mx.c won't work here.
1546 * Don't change this code unless you _really_ understand what
1551 static int mh_check_mailbox (CONTEXT * ctx, int *index_hint, int unused __attribute__ ((unused)))
1553 char buf[_POSIX_PATH_MAX];
1554 struct stat st, st_cur;
1555 short modified = 0, have_new = 0, occult = 0;
1556 struct maildir *md, *p;
1557 struct maildir **last = NULL;
1558 struct mh_sequences mhs;
1562 if (!option (OPTCHECKNEW))
1565 m_strcpy(buf, sizeof(buf), ctx->path);
1566 if (stat (buf, &st) == -1)
1569 /* create .mh_sequences when there isn't one. */
1570 snprintf (buf, sizeof (buf), "%s/.mh_sequences", ctx->path);
1571 if ((i = stat (buf, &st_cur) == -1) && errno == ENOENT) {
1575 if (mh_mkstemp (ctx, &fp, &tmp) == 0) {
1577 if (safe_rename (tmp, buf) == -1)
1583 if (i == -1 && stat (buf, &st_cur) == -1)
1586 if (st.st_mtime > ctx->mtime || st_cur.st_mtime > ctx->mtime_cur)
1592 ctx->mtime_cur = st_cur.st_mtime;
1593 ctx->mtime = st.st_mtime;
1599 maildir_parse_dir (ctx, &last, NULL, NULL);
1600 mh_read_sequences (&mhs, ctx->path);
1601 mh_update_maildir (md, &mhs);
1602 mhs_free_sequences (&mhs);
1604 /* check for modifications and adjust flags */
1605 fnames = hash_new (1031, 0);
1607 for (p = md; p; p = p->next)
1608 hash_insert (fnames, p->h->path, p);
1610 for (i = 0; i < ctx->msgcount; i++) {
1611 ctx->hdrs[i]->active = 0;
1613 if ((p = hash_find (fnames, ctx->hdrs[i]->path)) && p->h &&
1614 (mutt_cmp_header (ctx->hdrs[i], p->h))) {
1615 ctx->hdrs[i]->active = 1;
1616 /* found the right message */
1617 if (!ctx->hdrs[i]->changed)
1618 maildir_update_flags (ctx, ctx->hdrs[i], p->h);
1620 header_delete(&p->h);
1622 else /* message has disappeared */
1626 /* destroy the file name hash */
1628 hash_delete (&fnames, NULL);
1630 /* If we didn't just get new mail, update the tables. */
1632 maildir_update_tables (ctx, index_hint);
1634 /* Incorporate new messages */
1635 have_new = maildir_move_to_context (ctx, &md);
1637 return occult ? M_REOPENED : (have_new ? M_NEW_MAIL : 0);
1644 * These functions try to find a message in a maildir folder when it
1645 * has moved under our feet. Note that this code is rather expensive, but
1646 * then again, it's called rarely.
1649 static FILE *_maildir_open_find_message (const char *folder, const char *unique,
1650 const char *subfolder)
1652 char dir[_POSIX_PATH_MAX];
1653 char tunique[_POSIX_PATH_MAX];
1654 char fname[_POSIX_PATH_MAX];
1662 snprintf (dir, sizeof (dir), "%s/%s", folder, subfolder);
1664 if ((dp = opendir (dir)) == NULL) {
1669 while ((de = readdir (dp))) {
1670 maildir_canon_filename (tunique, de->d_name, sizeof (tunique));
1672 if (!m_strcmp(tunique, unique)) {
1673 snprintf (fname, sizeof (fname), "%s/%s/%s", folder, subfolder,
1675 fp = fopen(fname, "r");
1687 FILE *maildir_open_find_message (const char *folder, const char *msg)
1689 char unique[_POSIX_PATH_MAX];
1692 static unsigned int new_hits = 0, cur_hits = 0; /* simple dynamic optimization */
1694 maildir_canon_filename (unique, msg, sizeof (unique));
1697 _maildir_open_find_message (folder, unique,
1698 new_hits > cur_hits ? "new" : "cur"))
1699 || errno != ENOENT) {
1700 if (new_hits < UINT_MAX && cur_hits < UINT_MAX) {
1701 new_hits += (new_hits > cur_hits ? 1 : 0);
1702 cur_hits += (new_hits > cur_hits ? 0 : 1);
1708 _maildir_open_find_message (folder, unique,
1709 new_hits > cur_hits ? "cur" : "new"))
1710 || errno != ENOENT) {
1711 if (new_hits < UINT_MAX && cur_hits < UINT_MAX) {
1712 new_hits += (new_hits > cur_hits ? 0 : 1);
1713 cur_hits += (new_hits > cur_hits ? 1 : 0);
1725 * 1 if there are no messages in the mailbox
1726 * 0 if there are messages in the mailbox
1729 static int maildir_check_empty (const char *path)
1733 int r = 1; /* assume empty until we find a message */
1734 char frealpath[_POSIX_PATH_MAX];
1737 /* Strategy here is to look for any file not beginning with a period */
1740 /* we do "cur" on the first iteration since its more likely that we'll
1741 * find old messages without having to scan both subdirs
1743 snprintf (frealpath, sizeof (frealpath), "%s/%s", path,
1744 iter == 0 ? "cur" : "new");
1745 if ((dp = opendir (frealpath)) == NULL)
1747 while ((de = readdir (dp))) {
1748 if (*de->d_name != '.') {
1755 } while (r && iter < 2);
1762 * 1 if there are no messages in the mailbox
1763 * 0 if there are messages in the mailbox
1766 static int mh_check_empty (const char *path)
1770 int r = 1; /* assume empty until we find a message */
1772 if ((dp = opendir (path)) == NULL)
1774 while ((de = readdir (dp))) {
1775 if (mh_valid_message (de->d_name)) {
1785 static int mh_is_magic (const char* path, struct stat* st) {
1786 char tmp[_POSIX_PATH_MAX];
1788 if (S_ISDIR (st->st_mode)) {
1789 snprintf (tmp, sizeof (tmp), "%s/.mh_sequences", path);
1790 if (access (tmp, F_OK) == 0)
1793 snprintf (tmp, sizeof (tmp), "%s/.xmhcache", path);
1794 if (access (tmp, F_OK) == 0)
1797 snprintf (tmp, sizeof (tmp), "%s/.mew_cache", path);
1798 if (access (tmp, F_OK) == 0)
1801 snprintf (tmp, sizeof (tmp), "%s/.mew-cache", path);
1802 if (access (tmp, F_OK) == 0)
1805 snprintf (tmp, sizeof (tmp), "%s/.sylpheed_cache", path);
1806 if (access (tmp, F_OK) == 0)
1810 * ok, this isn't an mh folder, but mh mode can be used to read
1811 * Usenet news from the spool. ;-)
1814 snprintf (tmp, sizeof (tmp), "%s/.overview", path);
1815 if (access (tmp, F_OK) == 0)
1821 static int maildir_is_magic (const char* path, struct stat* st) {
1823 char tmp[_POSIX_PATH_MAX];
1825 if (S_ISDIR (st->st_mode)) {
1826 snprintf (tmp, sizeof (tmp), "%s/cur", path);
1827 if (stat (tmp, &sb) == 0 && S_ISDIR (sb.st_mode))
1833 static int mh_commit (MESSAGE* msg, CONTEXT* ctx) {
1834 return (mh_commit_message (msg, ctx, NULL));
1837 static int maildir_commit (MESSAGE* msg, CONTEXT* ctx) {
1838 return (maildir_commit_message (msg, ctx, NULL));
1841 mx_t const maildir_mx = {
1845 maildir_check_empty,
1848 maildir_open_new_message,
1850 maildir_check_mailbox,
1856 mx_t const mh_mx = {
1863 mh_open_new_message,