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
20 #include <lib-lib/mem.h>
21 #include <lib-lib/str.h>
22 #include <lib-lib/macros.h>
23 #include <lib-lib/file.h>
35 #include "lib/debug.h"
38 #include <sys/types.h>
58 unsigned header_parsed:1;
67 /* mh_sequences support */
69 #define MH_SEQ_UNSEEN (1 << 0)
70 #define MH_SEQ_REPLIED (1 << 1)
71 #define MH_SEQ_FLAGGED (1 << 2)
74 static int maildir_check_empty (const char*);
75 static int maildir_check_mailbox (CONTEXT*, int*, int);
76 static int mh_check_mailbox (CONTEXT*, int*, int);
78 static void mhs_alloc (struct mh_sequences *mhs, int i)
83 if (i > mhs->max || !mhs->flags) {
85 p_realloc(&mhs->flags, newmax + 1);
86 for (j = mhs->max + 1; j <= newmax; j++)
93 static void mhs_free_sequences (struct mh_sequences *mhs)
95 p_delete(&mhs->flags);
98 static short mhs_check (struct mh_sequences *mhs, int i)
100 if (!mhs->flags || i > mhs->max)
103 return mhs->flags[i];
106 static short mhs_set (struct mh_sequences *mhs, int i, short f)
110 return mhs->flags[i];
117 static short mhs_unset (struct mh_sequences *mhs, int i, short f)
121 return mhs->flags[i];
126 static void mh_read_token (char *t, int *first, int *last)
130 if ((p = strchr (t, '-'))) {
136 *first = *last = atoi (t);
139 static void mh_read_sequences (struct mh_sequences *mhs, const char *path)
150 char pathname[_POSIX_PATH_MAX];
152 snprintf (pathname, sizeof (pathname), "%s/.mh_sequences", path);
154 if (!(fp = fopen (pathname, "r")))
157 while ((buff = mutt_read_line (buff, &sz, fp, &line))) {
158 if (!(t = strtok (buff, " \t:")))
161 if (!m_strcmp(t, MhUnseen))
163 else if (!m_strcmp(t, MhFlagged))
165 else if (!m_strcmp(t, MhReplied))
167 else /* unknown sequence */
170 while ((t = strtok (NULL, " \t:"))) {
171 mh_read_token (t, &first, &last);
172 for (; first <= last; first++)
173 mhs_set (mhs, first, f);
181 int mh_buffy (const char *path)
184 struct mh_sequences mhs;
188 mh_read_sequences (&mhs, path);
189 for (i = 0; !r && i <= mhs.max; i++)
190 if (mhs_check (&mhs, i) & MH_SEQ_UNSEEN)
192 mhs_free_sequences (&mhs);
196 static int mh_mkstemp (CONTEXT * dest, FILE ** fp, char **tgt)
199 char path[_POSIX_PATH_MAX];
202 snprintf (path, _POSIX_PATH_MAX, "%s/.mutt-%s-%d-%d",
203 dest->path, NONULL (Hostname), (int) getpid (), Counter++);
205 if ((fd = open (path, O_WRONLY | O_EXCL | O_CREAT, 0666)) == -1) {
206 if (errno != EEXIST) {
212 *tgt = m_strdup(path);
217 if ((*fp = fdopen (fd, "w")) == NULL) {
227 static void mhs_write_one_sequence (FILE * fp, struct mh_sequences *mhs,
228 short f, const char *tag)
233 fprintf (fp, "%s:", tag);
238 for (i = 0; i <= mhs->max; i++) {
239 if ((mhs_check (mhs, i) & f)) {
245 else if (first >= 0) {
247 fprintf (fp, " %d", first);
249 fprintf (fp, " %d-%d", first, last);
258 fprintf (fp, " %d", first);
260 fprintf (fp, " %d-%d", first, last);
266 /* XXX - we don't currently remove deleted messages from sequences we don't know. Should we? */
268 void mh_update_sequences (CONTEXT * ctx)
272 char sequences[_POSIX_PATH_MAX];
284 char seq_unseen[STRING];
285 char seq_replied[STRING];
286 char seq_flagged[STRING];
289 struct mh_sequences mhs;
293 snprintf (seq_unseen, sizeof (seq_unseen), "%s:", NONULL (MhUnseen));
294 snprintf (seq_replied, sizeof (seq_replied), "%s:", NONULL (MhReplied));
295 snprintf (seq_flagged, sizeof (seq_flagged), "%s:", NONULL (MhFlagged));
297 if (mh_mkstemp (ctx, &nfp, &tmpfname) != 0) {
302 snprintf (sequences, sizeof (sequences), "%s/.mh_sequences", ctx->path);
305 /* first, copy unknown sequences */
306 if ((ofp = fopen (sequences, "r"))) {
307 while ((buff = mutt_read_line (buff, &s, ofp, &l))) {
308 if (!m_strncmp(buff, seq_unseen, m_strlen(seq_unseen)))
310 if (!m_strncmp(buff, seq_flagged, m_strlen(seq_flagged)))
312 if (!m_strncmp(buff, seq_replied, m_strlen(seq_replied)))
315 fprintf (nfp, "%s\n", buff);
320 /* now, update our unseen, flagged, and replied sequences */
321 for (l = 0; l < ctx->msgcount; l++) {
322 if (ctx->hdrs[l]->deleted)
325 if ((p = strrchr (ctx->hdrs[l]->path, '/')))
328 p = ctx->hdrs[l]->path;
332 if (!ctx->hdrs[l]->read) {
333 mhs_set (&mhs, i, MH_SEQ_UNSEEN);
336 if (ctx->hdrs[l]->flagged) {
337 mhs_set (&mhs, i, MH_SEQ_FLAGGED);
340 if (ctx->hdrs[l]->replied) {
341 mhs_set (&mhs, i, MH_SEQ_REPLIED);
346 /* write out the new sequences */
348 mhs_write_one_sequence (nfp, &mhs, MH_SEQ_UNSEEN, NONULL (MhUnseen));
350 mhs_write_one_sequence (nfp, &mhs, MH_SEQ_FLAGGED, NONULL (MhFlagged));
352 mhs_write_one_sequence (nfp, &mhs, MH_SEQ_REPLIED, NONULL (MhReplied));
354 mhs_free_sequences (&mhs);
357 /* try to commit the changes - no guarantee here */
361 if (safe_rename (tmpfname, sequences) != 0) {
362 /* report an error? */
369 static void mh_sequences_add_one (CONTEXT * ctx, int n, short unseen,
370 short flagged, short replied)
372 short unseen_done = 0;
373 short flagged_done = 0;
374 short replied_done = 0;
376 FILE *ofp = NULL, *nfp = NULL;
379 char sequences[_POSIX_PATH_MAX];
381 char seq_unseen[STRING];
382 char seq_replied[STRING];
383 char seq_flagged[STRING];
389 if (mh_mkstemp (ctx, &nfp, &tmpfname) == -1)
392 snprintf (seq_unseen, sizeof (seq_unseen), "%s:", NONULL (MhUnseen));
393 snprintf (seq_replied, sizeof (seq_replied), "%s:", NONULL (MhReplied));
394 snprintf (seq_flagged, sizeof (seq_flagged), "%s:", NONULL (MhFlagged));
396 snprintf (sequences, sizeof (sequences), "%s/.mh_sequences", ctx->path);
397 if ((ofp = fopen (sequences, "r"))) {
398 while ((buff = mutt_read_line (buff, &sz, ofp, &line))) {
399 if (unseen && !strncmp (buff, seq_unseen, m_strlen(seq_unseen))) {
400 fprintf (nfp, "%s %d\n", buff, n);
404 && !strncmp (buff, seq_flagged, m_strlen(seq_flagged))) {
405 fprintf (nfp, "%s %d\n", buff, n);
409 && !strncmp (buff, seq_replied, m_strlen(seq_replied))) {
410 fprintf (nfp, "%s %d\n", buff, n);
414 fprintf (nfp, "%s\n", buff);
420 if (!unseen_done && unseen)
421 fprintf (nfp, "%s: %d\n", NONULL (MhUnseen), n);
422 if (!flagged_done && flagged)
423 fprintf (nfp, "%s: %d\n", NONULL (MhFlagged), n);
424 if (!replied_done && replied)
425 fprintf (nfp, "%s: %d\n", NONULL (MhReplied), n);
430 if (safe_rename (tmpfname, sequences) != 0)
436 static void mh_update_maildir (struct maildir *md, struct mh_sequences *mhs)
442 for (; md; md = md->next) {
443 if ((p = strrchr (md->h->path, '/')))
449 f = mhs_check (mhs, i);
451 md->h->read = (f & MH_SEQ_UNSEEN) ? 0 : 1;
452 md->h->flagged = (f & MH_SEQ_FLAGGED) ? 1 : 0;
453 md->h->replied = (f & MH_SEQ_REPLIED) ? 1 : 0;
457 /* maildir support */
459 static void maildir_free_entry (struct maildir **md)
464 p_delete(&(*md)->canon_fname);
466 header_delete(&(*md)->h);
471 static void maildir_free_maildir (struct maildir **md)
473 struct maildir *p, *q;
478 for (p = *md; p; p = q) {
480 maildir_free_entry (&p);
484 static void maildir_parse_flags (HEADER * h, const char *path)
492 if ((p = strrchr (path, ':')) != NULL && m_strncmp(p + 1, "2,", 2) == 0) {
495 m_strreplace(&h->maildir_flags, p);
496 q = h->maildir_flags;
510 case 'R': /* replied */
515 case 'T': /* trashed */
528 if (q == h->maildir_flags)
529 p_delete(&h->maildir_flags);
534 static void maildir_update_mtime (CONTEXT * ctx)
536 char buf[_POSIX_PATH_MAX];
539 if (ctx->magic == M_MAILDIR) {
540 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, "cur");
541 if (stat (buf, &st) == 0)
542 ctx->mtime_cur = st.st_mtime;
543 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, "new");
546 snprintf (buf, sizeof (buf), "%s/.mh_sequences", ctx->path);
547 if (stat (buf, &st) == 0)
548 ctx->mtime_cur = st.st_mtime;
550 m_strcpy(buf, sizeof(buf), ctx->path);
553 if (stat (buf, &st) == 0)
554 ctx->mtime = st.st_mtime;
558 * Actually parse a maildir message. This may also be used to fill
559 * out a fake header structure generated by lazy maildir parsing.
561 static HEADER *maildir_parse_message (int magic, const char *fname,
562 int is_old, HEADER * _h)
568 if ((f = fopen (fname, "r")) != NULL) {
571 h->env = mutt_read_rfc822_header (f, h, 0, 0);
573 fstat (fileno (f), &st);
577 h->received = h->date_sent;
579 if (h->content->length <= 0)
580 h->content->length = st.st_size - h->content->offset;
584 if (magic == M_MAILDIR) {
586 * maildir stores its flags in the filename, so ignore the
587 * flags in the header of the message
591 maildir_parse_flags (h, fname);
599 * Note that this routine will _not_ modify the context given by
602 * It's used in the first parsing pass on maildir and MH folders.
603 * In the MH case, this means full parsing of the folder. In the
604 * maildir case, it means that we only look at flags, and create a
605 * fake HEADER structure, which may later be filled in by
606 * maildir_parse_message(), when called from
607 * maildir_delayed_parsing().
611 static int maildir_parse_entry (CONTEXT * ctx, struct maildir ***last,
612 const char *subdir, const char *fname,
613 int *count, int is_old, ino_t inode __attribute__ ((unused)))
615 struct maildir *entry;
617 char buf[_POSIX_PATH_MAX];
620 snprintf (buf, sizeof (buf), "%s/%s/%s", ctx->path, subdir, fname);
622 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, fname);
624 if (ctx->magic == M_MH)
625 h = maildir_parse_message (ctx->magic, buf, is_old, NULL);
629 maildir_parse_flags (h, buf);
635 if (!ctx->quiet && ReadInc && ((*count % ReadInc) == 0 || *count == 1))
636 mutt_message (_("Reading %s... %d"), ctx->path, *count);
640 snprintf (buf, sizeof (buf), "%s/%s", subdir, fname);
641 h->path = m_strdup(buf);
644 h->path = m_strdup(fname);
646 entry = p_new(struct maildir, 1);
648 entry->header_parsed = (ctx->magic == M_MH);
650 *last = &entry->next;
660 /* Ignore the garbage files. A valid MH message consists of only
661 * digits. Deleted message get moved to a filename with a comma before
665 int mh_valid_message (const char *s)
668 if (!isdigit ((unsigned char) *s))
674 static int maildir_parse_dir (CONTEXT * ctx, struct maildir ***last,
675 const char *subdir, int *count)
679 char buf[_POSIX_PATH_MAX];
683 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, subdir);
684 is_old = (m_strcmp("cur", subdir) == 0);
687 m_strcpy(buf, sizeof(buf), ctx->path);
689 if ((dirp = opendir (buf)) == NULL)
692 while ((de = readdir (dirp)) != NULL) {
694 if ((ctx->magic == M_MH && !mh_valid_message (de->d_name))
695 || (ctx->magic == M_MAILDIR && *de->d_name == '.'))
698 /* FOO - really ignore the return value? */
700 debug_print (2, ("parsing %s\n", de->d_name));
701 maildir_parse_entry (ctx, last, subdir, de->d_name, count, is_old,
702 #if HAVE_DIRENT_D_INO
714 static int maildir_add_to_context (CONTEXT * ctx, struct maildir *md)
716 int oldmsgcount = ctx->msgcount;
720 debug_print (2, ("considering %s\n", NONULL (md->canon_fname)));
723 debug_print (2, ("flags: %s%s%s%s%s\n", md->h->flagged ? "f" : "",
724 md->h->deleted ? "D" : "", md->h->replied ? "r" : "",
725 md->h->old ? "O" : "", md->h->read ? "R" : ""));
726 if (ctx->msgcount == ctx->hdrmax)
727 mx_alloc_memory (ctx);
729 ctx->hdrs[ctx->msgcount] = md->h;
730 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
732 md->h->content->length + md->h->content->offset -
733 md->h->content->hdr_offset;
741 if (ctx->msgcount > oldmsgcount) {
742 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
748 static int maildir_move_to_context (CONTEXT * ctx, struct maildir **md)
752 r = maildir_add_to_context (ctx, *md);
753 maildir_free_maildir (md);
758 static size_t maildir_hcache_keylen (const char *fn)
760 const char *p = strchr (fn, ':');
762 return p ? (size_t) (p - fn) : m_strlen(fn);
767 * This function does the second parsing pass for a maildir-style
770 void maildir_delayed_parsing (CONTEXT * ctx, struct maildir *md)
773 char fn[_POSIX_PATH_MAX];
779 struct timeval *when = NULL;
780 struct stat lastchanged;
783 hc = mutt_hcache_open (HeaderCache, ctx->path);
786 for (p = md, count = 0; p; p = p->next, count++) {
787 if (!(p && p->h && !p->header_parsed))
791 data = mutt_hcache_fetch (hc, p->h->path + 3, &maildir_hcache_keylen);
792 when = (struct timeval *) data;
795 if (!ctx->quiet && ReadInc && ((count % ReadInc) == 0 || count == 1))
796 mutt_message (_("Reading %s... %d"), ctx->path, count);
797 snprintf (fn, sizeof (fn), "%s/%s", ctx->path, p->h->path);
800 if (option (OPTHCACHEVERIFY)) {
801 ret = stat (fn, &lastchanged);
804 lastchanged.st_mtime = 0;
808 if (data != NULL && !ret && lastchanged.st_mtime <= when->tv_sec) {
809 p->h = mutt_hcache_restore ((unsigned char *) data, &p->h);
810 maildir_parse_flags (p->h, fn);
814 if (maildir_parse_message (ctx->magic, fn, p->h->old, p->h)) {
815 p->header_parsed = 1;
816 maildir_parse_flags (p->h, fn);
818 mutt_hcache_store (hc, p->h->path + 3, p->h, 0, &maildir_hcache_keylen);
822 header_delete(&p->h);
828 mutt_hcache_close (hc);
832 /* Read a MH/maildir style mailbox.
835 * ctx [IN/OUT] context for this mailbox
836 * subdir [IN] NULL for MH mailboxes, otherwise the subdir of the
837 * maildir mailbox to read from
839 static int _mh_read_dir (CONTEXT * ctx, const char *subdir)
842 struct mh_sequences mhs;
843 struct maildir **last;
849 maildir_update_mtime (ctx);
854 if (maildir_parse_dir (ctx, &last, subdir, &count) == -1)
857 if (ctx->magic == M_MH) {
858 mh_read_sequences (&mhs, ctx->path);
859 mh_update_maildir (md, &mhs);
860 mhs_free_sequences (&mhs);
863 if (ctx->magic == M_MAILDIR)
864 maildir_delayed_parsing (ctx, md);
866 maildir_move_to_context (ctx, &md);
870 static int mh_read_dir (CONTEXT* ctx) {
871 return (_mh_read_dir (ctx, NULL));
874 /* read a maildir style mailbox */
875 static int maildir_read_dir (CONTEXT * ctx)
877 /* maildir looks sort of like MH, except that there are two subdirectories
878 * of the main folder path from which to read messages
880 if (_mh_read_dir (ctx, "new") == -1 || _mh_read_dir (ctx, "cur") == -1)
887 * Open a new (temporary) message in an MH folder.
890 static int mh_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr __attribute__ ((unused)))
892 return mh_mkstemp (dest, &msg->fp, &msg->path);
895 int ch_compar (const void *a, const void *b)
897 return (int) (*((const char *) a) - *((const char *) b));
900 static void maildir_flags (char *dest, size_t destlen, HEADER * hdr)
905 * The maildir specification requires that all files in the cur
906 * subdirectory have the :unique string appeneded, regardless of whether
907 * or not there are any flags. If .old is set, we know that this message
908 * will end up in the cur directory, so we include it in the following
909 * test even though there is no associated flag.
913 && (hdr->flagged || hdr->replied || hdr->read || hdr->deleted
914 || hdr->old || hdr->maildir_flags)) {
915 char tmp[LONG_STRING];
917 snprintf (tmp, sizeof (tmp),
919 hdr->flagged ? "F" : "",
920 hdr->replied ? "R" : "",
921 hdr->read ? "S" : "", hdr->deleted ? "T" : "",
922 NONULL (hdr->maildir_flags));
923 if (hdr->maildir_flags)
924 qsort (tmp, m_strlen(tmp), 1, ch_compar);
925 snprintf (dest, destlen, ":2,%s", tmp);
931 * Open a new (temporary) message in a maildir folder.
933 * Note that this uses _almost_ the maildir file name format, but
934 * with a {cur,new} prefix.
938 static int maildir_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr)
941 char path[_POSIX_PATH_MAX];
946 short deleted = hdr->deleted;
950 maildir_flags (suffix, sizeof (suffix), hdr);
952 hdr->deleted = deleted;
957 if (hdr && (hdr->read || hdr->old))
958 m_strcpy(subdir, sizeof(subdir), "cur");
960 m_strcpy(subdir, sizeof(subdir), "new");
963 snprintf (path, _POSIX_PATH_MAX, "%s/tmp/%s.%ld.%u_%d.%s%s",
964 dest->path, subdir, (long) time (NULL),
965 (unsigned int) getpid (), Counter++, NONULL (Hostname), suffix);
967 debug_print (2, ("trying %s.\n", path));
970 if ((fd = open (path, O_WRONLY | O_EXCL | O_CREAT, 0666)) == -1) {
971 if (errno != EEXIST) {
977 debug_print (2, ("success.\n"));
978 msg->path = m_strdup(path);
983 if ((msg->fp = fdopen (fd, "w")) == NULL) {
984 p_delete(&msg->path);
996 * Commit a message to a maildir folder.
998 * msg->path contains the file name of a file in tmp/. We take the
999 * flags from this file's name.
1001 * ctx is the mail folder we commit to.
1003 * hdr is a header structure to which we write the message's new
1004 * file name. This is used in the mh and maildir folder synch
1005 * routines. When this routine is invoked from mx_commit_message,
1008 * msg->path looks like this:
1010 * tmp/{cur,new}.mutt-HOSTNAME-PID-COUNTER:flags
1012 * See also maildir_open_new_message().
1016 static int maildir_commit_message (MESSAGE * msg, CONTEXT * ctx, HEADER * hdr)
1020 char path[_POSIX_PATH_MAX];
1021 char full[_POSIX_PATH_MAX];
1024 if (safe_fclose (&msg->fp) != 0)
1027 /* extract the subdir */
1028 s = strrchr (msg->path, '/') + 1;
1029 m_strcpy(subdir, sizeof(subdir), s);
1031 /* extract the flags */
1032 if ((s = strchr (s, ':')))
1033 m_strcpy(suffix, sizeof(suffix), s);
1037 /* construct a new file name. */
1039 snprintf (path, _POSIX_PATH_MAX, "%s/%ld.%u_%d.%s%s", subdir,
1040 (long) time (NULL), (unsigned int) getpid (), Counter++,
1041 NONULL (Hostname), suffix);
1042 snprintf (full, _POSIX_PATH_MAX, "%s/%s", ctx->path, path);
1044 debug_print (2, ("renaming %s to %s.\n", msg->path, full));
1046 if (safe_rename (msg->path, full) == 0) {
1048 m_strreplace(&hdr->path, path);
1049 p_delete(&msg->path);
1052 * Adjust the mtime on the file to match the time at which this
1053 * message was received. Currently this is only set when copying
1054 * messages between mailboxes, so we test to ensure that it is
1057 if (msg->received) {
1060 ut.actime = msg->received;
1061 ut.modtime = msg->received;
1062 if (utime (full, &ut)) {
1064 ("maildir_commit_message(): unable to set time on file"));
1071 else if (errno != EEXIST) {
1072 mutt_perror (ctx->path);
1079 * commit a message to an MH folder.
1084 static int _mh_commit_message (MESSAGE * msg, CONTEXT * ctx, HEADER * hdr,
1090 unsigned int n, hi = 0;
1091 char path[_POSIX_PATH_MAX];
1094 if (safe_fclose (&msg->fp) != 0)
1097 if ((dirp = opendir (ctx->path)) == NULL) {
1098 mutt_perror (ctx->path);
1102 /* figure out what the next message number is */
1103 while ((de = readdir (dirp)) != NULL) {
1109 if (!isdigit ((unsigned char) *cp))
1122 * Now try to rename the file to the proper name.
1124 * Note: We may have to try multiple times, until we find a free
1130 snprintf (tmp, sizeof (tmp), "%d", hi);
1131 snprintf (path, sizeof (path), "%s/%s", ctx->path, tmp);
1132 if (safe_rename (msg->path, path) == 0) {
1134 m_strreplace(&hdr->path, tmp);
1135 p_delete(&msg->path);
1138 else if (errno != EEXIST) {
1139 mutt_perror (ctx->path);
1144 mh_sequences_add_one (ctx, hi, !msg->flags.read, msg->flags.flagged,
1145 msg->flags.replied);
1149 static int mh_commit_message (MESSAGE * msg, CONTEXT * ctx, HEADER * hdr) {
1150 return _mh_commit_message (msg, ctx, hdr, 1);
1153 /* Sync a message in an MH folder.
1155 * This code is also used for attachment deletion in maildir
1159 static int mh_rewrite_message (CONTEXT * ctx, int msgno)
1161 HEADER *h = ctx->hdrs[msgno];
1166 char oldpath[_POSIX_PATH_MAX];
1167 char newpath[_POSIX_PATH_MAX];
1168 char partpath[_POSIX_PATH_MAX];
1170 long old_body_offset = h->content->offset;
1171 long old_body_length = h->content->length;
1172 long old_hdr_lines = h->lines;
1174 if ((dest = mx_open_new_message (ctx, h, 0)) == NULL)
1177 if ((rc = mutt_copy_message (dest->fp, ctx, h,
1178 M_CM_UPDATE, CH_UPDATE | CH_UPDATE_LEN)) == 0)
1180 snprintf (oldpath, sizeof(oldpath), "%s/%s", ctx->path, h->path);
1181 m_strcpy(partpath, sizeof(partpath), h->path);
1183 if (ctx->magic == M_MAILDIR)
1184 rc = maildir_commit_message (dest, ctx, h);
1186 rc = _mh_commit_message (dest, ctx, h, 0);
1188 mx_close_message (&dest);
1196 * Try to move the new message to the old place.
1199 * This is important when we are just updating flags.
1201 * Note that there is a race condition against programs which
1202 * use the first free slot instead of the maximum message
1203 * number. Mutt does _not_ behave like this.
1205 * Anyway, if this fails, the message is in the folder, so
1206 * all what happens is that a concurrently runnung mutt will
1207 * lose flag modifications.
1210 if (ctx->magic == M_MH && rc == 0) {
1211 snprintf (newpath, _POSIX_PATH_MAX, "%s/%s", ctx->path, h->path);
1212 if ((rc = safe_rename (newpath, oldpath)) == 0)
1213 m_strreplace(&h->path, partpath);
1217 mx_close_message (&dest);
1219 if (rc == -1 && restore) {
1220 h->content->offset = old_body_offset;
1221 h->content->length = old_body_length;
1222 h->lines = old_hdr_lines;
1225 mutt_free_body (&h->content->parts);
1229 static int mh_sync_message (CONTEXT * ctx, int msgno)
1231 HEADER *h = ctx->hdrs[msgno];
1233 if (h->attach_del ||
1234 (h->env && (h->env->refs_changed || h->env->irt_changed)))
1235 if (mh_rewrite_message (ctx, msgno) != 0)
1241 static int maildir_sync_message (CONTEXT * ctx, int msgno)
1243 HEADER *h = ctx->hdrs[msgno];
1245 if (h->attach_del ||
1246 (h->env && (h->env->refs_changed || h->env->irt_changed))) {
1247 /* when doing attachment deletion/rethreading, fall back to the MH case. */
1248 if (mh_rewrite_message (ctx, msgno) != 0)
1252 /* we just have to rename the file. */
1254 char newpath[_POSIX_PATH_MAX];
1255 char partpath[_POSIX_PATH_MAX];
1256 char fullpath[_POSIX_PATH_MAX];
1257 char oldpath[_POSIX_PATH_MAX];
1261 if ((p = strrchr (h->path, '/')) == NULL) {
1262 debug_print (1, ("%s: unable to find subdir!\n", h->path));
1266 m_strcpy(newpath, sizeof(newpath), p);
1268 /* kill the previous flags */
1269 if ((p = strchr (newpath, ':')) != NULL)
1272 maildir_flags (suffix, sizeof (suffix), h);
1274 snprintf (partpath, sizeof (partpath), "%s/%s%s",
1275 (h->read || h->old) ? "cur" : "new", newpath, suffix);
1276 snprintf (fullpath, sizeof (fullpath), "%s/%s", ctx->path, partpath);
1277 snprintf (oldpath, sizeof (oldpath), "%s/%s", ctx->path, h->path);
1279 if (m_strcmp(fullpath, oldpath) == 0) {
1280 /* message hasn't really changed */
1284 /* record that the message is possibly marked as trashed on disk */
1285 h->trash = h->deleted;
1287 if (rename (oldpath, fullpath) != 0) {
1288 mutt_perror ("rename");
1291 m_strreplace(&h->path, partpath);
1296 static int mh_sync_mailbox (CONTEXT * ctx, int unused __attribute__ ((unused)), int *index_hint)
1298 char path[_POSIX_PATH_MAX], tmp[_POSIX_PATH_MAX];
1303 #endif /* USE_HCACHE */
1305 if (ctx->magic == M_MH)
1306 i = mh_check_mailbox (ctx, index_hint, 0);
1308 i = maildir_check_mailbox (ctx, index_hint, 0);
1314 if (ctx->magic == M_MAILDIR)
1315 hc = mutt_hcache_open (HeaderCache, ctx->path);
1316 #endif /* USE_HCACHE */
1318 for (i = 0; i < ctx->msgcount; i++) {
1319 if (ctx->hdrs[i]->deleted
1320 && (ctx->magic != M_MAILDIR || !option (OPTMAILDIRTRASH))) {
1321 snprintf (path, sizeof (path), "%s/%s", ctx->path, ctx->hdrs[i]->path);
1322 if (ctx->magic == M_MAILDIR
1323 || (option (OPTMHPURGE) && ctx->magic == M_MH)) {
1325 if (ctx->magic == M_MAILDIR)
1326 mutt_hcache_delete (hc, ctx->hdrs[i]->path + 3,
1327 &maildir_hcache_keylen);
1328 #endif /* USE_HCACHE */
1331 else if (ctx->magic == M_MH) {
1332 /* MH just moves files out of the way when you delete them */
1333 if (*ctx->hdrs[i]->path != ',') {
1334 snprintf (tmp, sizeof (tmp), "%s/,%s", ctx->path,
1335 ctx->hdrs[i]->path);
1342 else if (ctx->hdrs[i]->changed || ctx->hdrs[i]->attach_del ||
1343 (ctx->magic == M_MAILDIR
1344 && (option (OPTMAILDIRTRASH) || ctx->hdrs[i]->trash)
1345 && (ctx->hdrs[i]->deleted != ctx->hdrs[i]->trash))) {
1346 if (ctx->magic == M_MAILDIR) {
1347 if (maildir_sync_message (ctx, i) == -1)
1351 if (mh_sync_message (ctx, i) == -1)
1358 if (ctx->magic == M_MAILDIR)
1359 mutt_hcache_close (hc);
1360 #endif /* USE_HCACHE */
1362 if (ctx->magic == M_MH)
1363 mh_update_sequences (ctx);
1365 /* XXX race condition? */
1367 maildir_update_mtime (ctx);
1369 /* adjust indices */
1372 for (i = 0, j = 0; i < ctx->msgcount; i++) {
1373 if (!ctx->hdrs[i]->deleted
1374 || (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))
1375 ctx->hdrs[i]->index = j++;
1383 if (ctx->magic == M_MAILDIR)
1384 mutt_hcache_close (hc);
1385 #endif /* USE_HCACHE */
1389 static char *maildir_canon_filename (char *dest, const char *src, size_t l)
1393 if ((t = strrchr (src, '/')))
1396 m_strcpy(dest, l, src);
1397 if ((u = strrchr (dest, ':')))
1403 static void maildir_update_tables (CONTEXT * ctx, int *index_hint)
1409 if (Sort != SORT_ORDER) {
1412 mutt_sort_headers (ctx, 1);
1416 old_count = ctx->msgcount;
1417 for (i = 0, j = 0; i < old_count; i++) {
1418 if (ctx->hdrs[i]->active && index_hint && *index_hint == i)
1421 if (ctx->hdrs[i]->active)
1422 ctx->hdrs[i]->index = j++;
1425 mx_update_tables (ctx, 0);
1426 mutt_clear_threads (ctx);
1429 static void maildir_update_flags (CONTEXT * ctx, HEADER * o, HEADER * n)
1431 /* save the global state here so we can reset it at the
1432 * end of list block if required.
1434 int context_changed = ctx->changed;
1436 /* user didn't modify this message. alter the flags to
1437 * match the current state on disk. This may not actually
1438 * do anything, but we can't tell right now. mutt_set_flag()
1439 * will just ignore the call if the status bits are
1440 * already properly set.
1442 mutt_set_flag (ctx, o, M_FLAG, n->flagged);
1443 mutt_set_flag (ctx, o, M_REPLIED, n->replied);
1444 mutt_set_flag (ctx, o, M_READ, n->read);
1445 mutt_set_flag (ctx, o, M_OLD, n->old);
1447 /* mutt_set_flag() will set this, but we don't need to
1448 * sync the changes we made because we just updated the
1449 * context to match the current on-disk state of the
1454 /* if the mailbox was not modified before we made these
1455 * changes, unset the changed flag since nothing needs to
1458 if (!context_changed)
1463 /* This function handles arrival of new mail and reopening of
1464 * maildir folders. The basic idea here is we check to see if either
1465 * the new or cur subdirectories have changed, and if so, we scan them
1466 * for the list of files. We check for newly added messages, and
1467 * then merge the flags messages we already knew about. We don't treat
1468 * either subdirectory differently, as mail could be copied directly into
1469 * the cur directory from another agent.
1471 static int maildir_check_mailbox (CONTEXT * ctx, int *index_hint, int unused __attribute__ ((unused)))
1473 struct stat st_new; /* status of the "new" subdirectory */
1474 struct stat st_cur; /* status of the "cur" subdirectory */
1475 char buf[_POSIX_PATH_MAX];
1476 int changed = 0; /* bitmask representing which subdirectories
1477 have changed. 0x1 = new, 0x2 = cur */
1478 int occult = 0; /* messages were removed from the mailbox */
1479 int have_new = 0; /* messages were added to the mailbox */
1480 struct maildir *md; /* list of messages in the mailbox */
1481 struct maildir **last, *p;
1483 HASH *fnames; /* hash table for quickly looking up the base filename
1484 for a maildir message */
1486 if (!option (OPTCHECKNEW))
1489 snprintf (buf, sizeof (buf), "%s/new", ctx->path);
1490 if (stat (buf, &st_new) == -1)
1493 snprintf (buf, sizeof (buf), "%s/cur", ctx->path);
1494 if (stat (buf, &st_cur) == -1)
1497 /* determine which subdirectories need to be scanned */
1498 if (st_new.st_mtime > ctx->mtime)
1500 if (st_cur.st_mtime > ctx->mtime_cur)
1504 return 0; /* nothing to do */
1506 /* update the modification times on the mailbox */
1507 ctx->mtime_cur = st_cur.st_mtime;
1508 ctx->mtime = st_new.st_mtime;
1510 /* do a fast scan of just the filenames in
1511 * the subdirectories that have changed.
1516 maildir_parse_dir (ctx, &last, "new", NULL);
1518 maildir_parse_dir (ctx, &last, "cur", NULL);
1520 /* we create a hash table keyed off the canonical (sans flags) filename
1521 * of each message we scanned. This is used in the loop over the
1522 * existing messages below to do some correlation.
1524 fnames = hash_create (1031);
1526 for (p = md; p; p = p->next) {
1527 maildir_canon_filename (buf, p->h->path, sizeof (buf));
1528 p->canon_fname = m_strdup(buf);
1529 hash_insert (fnames, p->canon_fname, p, 0);
1532 /* check for modifications and adjust flags */
1533 for (i = 0; i < ctx->msgcount; i++) {
1534 ctx->hdrs[i]->active = 0;
1535 maildir_canon_filename (buf, ctx->hdrs[i]->path, sizeof (buf));
1536 p = hash_find (fnames, buf);
1538 /* message already exists, merge flags */
1539 ctx->hdrs[i]->active = 1;
1541 /* check to see if the message has moved to a different
1542 * subdirectory. If so, update the associated filename.
1544 if (m_strcmp(ctx->hdrs[i]->path, p->h->path))
1545 m_strreplace(&ctx->hdrs[i]->path, p->h->path);
1547 /* if the user hasn't modified the flags on this message, update
1548 * the flags we just detected.
1550 if (!ctx->hdrs[i]->changed)
1551 maildir_update_flags (ctx, ctx->hdrs[i], p->h);
1553 if (ctx->hdrs[i]->deleted == ctx->hdrs[i]->trash)
1554 ctx->hdrs[i]->deleted = p->h->deleted;
1555 ctx->hdrs[i]->trash = p->h->trash;
1557 /* this is a duplicate of an existing header, so remove it */
1558 header_delete(&p->h);
1560 /* This message was not in the list of messages we just scanned.
1561 * Check to see if we have enough information to know if the
1562 * message has disappeared out from underneath us.
1564 else if (((changed & 1) && (!strncmp (ctx->hdrs[i]->path, "new/", 4))) ||
1565 ((changed & 2) && (!strncmp (ctx->hdrs[i]->path, "cur/", 4)))) {
1566 /* This message disappeared, so we need to simulate a "reopen"
1567 * event. We know it disappeared because we just scanned the
1568 * subdirectory it used to reside in.
1573 /* This message resides in a subdirectory which was not
1574 * modified, so we assume that it is still present and
1577 ctx->hdrs[i]->active = 1;
1581 /* destroy the file name hash */
1582 hash_destroy (&fnames, NULL);
1584 /* If we didn't just get new mail, update the tables. */
1586 maildir_update_tables (ctx, index_hint);
1588 /* do any delayed parsing we need to do. */
1589 maildir_delayed_parsing (ctx, md);
1591 /* Incorporate new messages */
1592 have_new = maildir_move_to_context (ctx, &md);
1594 return occult ? M_REOPENED : (have_new ? M_NEW_MAIL : 0);
1598 * This function handles arrival of new mail and reopening of
1599 * mh/maildir folders. Things are getting rather complex because we
1600 * don't have a well-defined "mailbox order", so the tricks from
1601 * mbox.c and mx.c won't work here.
1603 * Don't change this code unless you _really_ understand what
1608 static int mh_check_mailbox (CONTEXT * ctx, int *index_hint, int unused __attribute__ ((unused)))
1610 char buf[_POSIX_PATH_MAX];
1611 struct stat st, st_cur;
1612 short modified = 0, have_new = 0, occult = 0;
1613 struct maildir *md, *p;
1614 struct maildir **last = NULL;
1615 struct mh_sequences mhs;
1619 if (!option (OPTCHECKNEW))
1622 m_strcpy(buf, sizeof(buf), ctx->path);
1623 if (stat (buf, &st) == -1)
1626 /* create .mh_sequences when there isn't one. */
1627 snprintf (buf, sizeof (buf), "%s/.mh_sequences", ctx->path);
1628 if ((i = stat (buf, &st_cur) == -1) && errno == ENOENT) {
1632 if (mh_mkstemp (ctx, &fp, &tmp) == 0) {
1634 if (safe_rename (tmp, buf) == -1)
1640 if (i == -1 && stat (buf, &st_cur) == -1)
1643 if (st.st_mtime > ctx->mtime || st_cur.st_mtime > ctx->mtime_cur)
1649 ctx->mtime_cur = st_cur.st_mtime;
1650 ctx->mtime = st.st_mtime;
1656 maildir_parse_dir (ctx, &last, NULL, NULL);
1657 mh_read_sequences (&mhs, ctx->path);
1658 mh_update_maildir (md, &mhs);
1659 mhs_free_sequences (&mhs);
1661 /* check for modifications and adjust flags */
1662 fnames = hash_create (1031);
1664 for (p = md; p; p = p->next)
1665 hash_insert (fnames, p->h->path, p, 0);
1667 for (i = 0; i < ctx->msgcount; i++) {
1668 ctx->hdrs[i]->active = 0;
1670 if ((p = hash_find (fnames, ctx->hdrs[i]->path)) && p->h &&
1671 (mutt_cmp_header (ctx->hdrs[i], p->h))) {
1672 ctx->hdrs[i]->active = 1;
1673 /* found the right message */
1674 if (!ctx->hdrs[i]->changed)
1675 maildir_update_flags (ctx, ctx->hdrs[i], p->h);
1677 header_delete(&p->h);
1679 else /* message has disappeared */
1683 /* destroy the file name hash */
1685 hash_destroy (&fnames, NULL);
1687 /* If we didn't just get new mail, update the tables. */
1689 maildir_update_tables (ctx, index_hint);
1691 /* Incorporate new messages */
1692 have_new = maildir_move_to_context (ctx, &md);
1694 return occult ? M_REOPENED : (have_new ? M_NEW_MAIL : 0);
1701 * These functions try to find a message in a maildir folder when it
1702 * has moved under our feet. Note that this code is rather expensive, but
1703 * then again, it's called rarely.
1706 FILE *_maildir_open_find_message (const char *folder, const char *unique,
1707 const char *subfolder)
1709 char dir[_POSIX_PATH_MAX];
1710 char tunique[_POSIX_PATH_MAX];
1711 char fname[_POSIX_PATH_MAX];
1719 snprintf (dir, sizeof (dir), "%s/%s", folder, subfolder);
1721 if ((dp = opendir (dir)) == NULL) {
1726 while ((de = readdir (dp))) {
1727 maildir_canon_filename (tunique, de->d_name, sizeof (tunique));
1729 if (!m_strcmp(tunique, unique)) {
1730 snprintf (fname, sizeof (fname), "%s/%s/%s", folder, subfolder,
1732 fp = fopen (fname, "r"); /* __FOPEN_CHECKED__ */
1744 FILE *maildir_open_find_message (const char *folder, const char *msg)
1746 char unique[_POSIX_PATH_MAX];
1749 static unsigned int new_hits = 0, cur_hits = 0; /* simple dynamic optimization */
1751 maildir_canon_filename (unique, msg, sizeof (unique));
1754 _maildir_open_find_message (folder, unique,
1755 new_hits > cur_hits ? "new" : "cur"))
1756 || errno != ENOENT) {
1757 if (new_hits < UINT_MAX && cur_hits < UINT_MAX) {
1758 new_hits += (new_hits > cur_hits ? 1 : 0);
1759 cur_hits += (new_hits > cur_hits ? 0 : 1);
1765 _maildir_open_find_message (folder, unique,
1766 new_hits > cur_hits ? "cur" : "new"))
1767 || errno != ENOENT) {
1768 if (new_hits < UINT_MAX && cur_hits < UINT_MAX) {
1769 new_hits += (new_hits > cur_hits ? 0 : 1);
1770 cur_hits += (new_hits > cur_hits ? 1 : 0);
1782 * 1 if there are no messages in the mailbox
1783 * 0 if there are messages in the mailbox
1786 static int maildir_check_empty (const char *path)
1790 int r = 1; /* assume empty until we find a message */
1791 char frealpath[_POSIX_PATH_MAX];
1794 /* Strategy here is to look for any file not beginning with a period */
1797 /* we do "cur" on the first iteration since its more likely that we'll
1798 * find old messages without having to scan both subdirs
1800 snprintf (frealpath, sizeof (frealpath), "%s/%s", path,
1801 iter == 0 ? "cur" : "new");
1802 if ((dp = opendir (frealpath)) == NULL)
1804 while ((de = readdir (dp))) {
1805 if (*de->d_name != '.') {
1812 } while (r && iter < 2);
1819 * 1 if there are no messages in the mailbox
1820 * 0 if there are messages in the mailbox
1823 int mh_check_empty (const char *path)
1827 int r = 1; /* assume empty until we find a message */
1829 if ((dp = opendir (path)) == NULL)
1831 while ((de = readdir (dp))) {
1832 if (mh_valid_message (de->d_name)) {
1842 static int mh_is_magic (const char* path, struct stat* st) {
1843 char tmp[_POSIX_PATH_MAX];
1845 if (S_ISDIR (st->st_mode)) {
1846 snprintf (tmp, sizeof (tmp), "%s/.mh_sequences", path);
1847 if (access (tmp, F_OK) == 0)
1850 snprintf (tmp, sizeof (tmp), "%s/.xmhcache", path);
1851 if (access (tmp, F_OK) == 0)
1854 snprintf (tmp, sizeof (tmp), "%s/.mew_cache", path);
1855 if (access (tmp, F_OK) == 0)
1858 snprintf (tmp, sizeof (tmp), "%s/.mew-cache", path);
1859 if (access (tmp, F_OK) == 0)
1862 snprintf (tmp, sizeof (tmp), "%s/.sylpheed_cache", path);
1863 if (access (tmp, F_OK) == 0)
1867 * ok, this isn't an mh folder, but mh mode can be used to read
1868 * Usenet news from the spool. ;-)
1871 snprintf (tmp, sizeof (tmp), "%s/.overview", path);
1872 if (access (tmp, F_OK) == 0)
1878 static int maildir_is_magic (const char* path, struct stat* st) {
1880 char tmp[_POSIX_PATH_MAX];
1882 if (S_ISDIR (st->st_mode)) {
1883 snprintf (tmp, sizeof (tmp), "%s/cur", path);
1884 if (stat (tmp, &sb) == 0 && S_ISDIR (sb.st_mode))
1890 /* routines common to maildir and mh */
1891 static mx_t* reg_mx (void) {
1892 mx_t* fmt = p_new(mx_t, 1);
1894 fmt->mx_access = access;
1895 fmt->mx_sync_mailbox = mh_sync_mailbox;
1899 static int mh_commit (MESSAGE* msg, CONTEXT* ctx) {
1900 return (mh_commit_message (msg, ctx, NULL));
1903 static int maildir_commit (MESSAGE* msg, CONTEXT* ctx) {
1904 return (maildir_commit_message (msg, ctx, NULL));
1907 mx_t* mh_reg_mx (void) {
1908 mx_t* fmt = reg_mx ();
1910 fmt->mx_check_empty = mh_check_empty;
1911 fmt->mx_is_magic = mh_is_magic;
1912 fmt->mx_open_mailbox = mh_read_dir;
1913 fmt->mx_open_new_message = mh_open_new_message;
1914 fmt->mx_check_mailbox = mh_check_mailbox;
1915 fmt->mx_commit_message = mh_commit;
1919 mx_t* maildir_reg_mx (void) {
1920 mx_t* fmt = reg_mx ();
1921 fmt->type = M_MAILDIR;
1922 fmt->mx_check_empty = maildir_check_empty;
1923 fmt->mx_is_magic = maildir_is_magic;
1924 fmt->mx_open_mailbox = maildir_read_dir;
1925 fmt->mx_open_new_message = maildir_open_new_message;
1926 fmt->mx_check_mailbox = maildir_check_mailbox;
1927 fmt->mx_commit_message = maildir_commit;