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
21 #include <sys/types.h>
38 #include <lib-lib/mem.h>
39 #include <lib-lib/str.h>
40 #include <lib-lib/macros.h>
41 #include <lib-lib/file.h>
42 #include <lib-lib/debug.h>
57 unsigned header_parsed:1;
66 /* mh_sequences support */
68 #define MH_SEQ_UNSEEN (1 << 0)
69 #define MH_SEQ_REPLIED (1 << 1)
70 #define MH_SEQ_FLAGGED (1 << 2)
73 static int maildir_check_empty (const char*);
74 static int maildir_check_mailbox (CONTEXT*, int*, int);
75 static int mh_check_mailbox (CONTEXT*, int*, int);
77 static void mhs_alloc (struct mh_sequences *mhs, int i)
82 if (i > mhs->max || !mhs->flags) {
84 p_realloc(&mhs->flags, newmax + 1);
85 for (j = mhs->max + 1; j <= newmax; j++)
92 static void mhs_free_sequences (struct mh_sequences *mhs)
94 p_delete(&mhs->flags);
97 static short mhs_check (struct mh_sequences *mhs, int i)
99 if (!mhs->flags || i > mhs->max)
102 return mhs->flags[i];
105 static short mhs_set (struct mh_sequences *mhs, int i, short f)
109 return mhs->flags[i];
116 static short mhs_unset (struct mh_sequences *mhs, int i, short f)
120 return mhs->flags[i];
125 static void mh_read_token (char *t, int *first, int *last)
129 if ((p = strchr (t, '-'))) {
135 *first = *last = atoi (t);
138 static void mh_read_sequences (struct mh_sequences *mhs, const char *path)
149 char pathname[_POSIX_PATH_MAX];
151 snprintf (pathname, sizeof (pathname), "%s/.mh_sequences", path);
153 if (!(fp = fopen (pathname, "r")))
156 while ((buff = mutt_read_line (buff, &sz, fp, &line))) {
157 if (!(t = strtok (buff, " \t:")))
160 if (!m_strcmp(t, MhUnseen))
162 else if (!m_strcmp(t, MhFlagged))
164 else if (!m_strcmp(t, MhReplied))
166 else /* unknown sequence */
169 while ((t = strtok (NULL, " \t:"))) {
170 mh_read_token (t, &first, &last);
171 for (; first <= last; first++)
172 mhs_set (mhs, first, f);
180 int mh_buffy (const char *path)
183 struct mh_sequences mhs;
187 mh_read_sequences (&mhs, path);
188 for (i = 0; !r && i <= mhs.max; i++)
189 if (mhs_check (&mhs, i) & MH_SEQ_UNSEEN)
191 mhs_free_sequences (&mhs);
195 static int mh_mkstemp (CONTEXT * dest, FILE ** fp, char **tgt)
198 char path[_POSIX_PATH_MAX];
201 snprintf (path, _POSIX_PATH_MAX, "%s/.mutt-%s-%d-%d",
202 dest->path, NONULL (Hostname), (int) getpid (), Counter++);
204 if ((fd = open (path, O_WRONLY | O_EXCL | O_CREAT, 0666)) == -1) {
205 if (errno != EEXIST) {
211 *tgt = m_strdup(path);
216 if ((*fp = fdopen (fd, "w")) == NULL) {
226 static void mhs_write_one_sequence (FILE * fp, struct mh_sequences *mhs,
227 short f, const char *tag)
232 fprintf (fp, "%s:", tag);
237 for (i = 0; i <= mhs->max; i++) {
238 if ((mhs_check (mhs, i) & f)) {
244 else if (first >= 0) {
246 fprintf (fp, " %d", first);
248 fprintf (fp, " %d-%d", first, last);
257 fprintf (fp, " %d", first);
259 fprintf (fp, " %d-%d", first, last);
265 /* XXX - we don't currently remove deleted messages from sequences we don't know. Should we? */
267 void mh_update_sequences (CONTEXT * ctx)
271 char sequences[_POSIX_PATH_MAX];
283 char seq_unseen[STRING];
284 char seq_replied[STRING];
285 char seq_flagged[STRING];
288 struct mh_sequences mhs;
292 snprintf (seq_unseen, sizeof (seq_unseen), "%s:", NONULL (MhUnseen));
293 snprintf (seq_replied, sizeof (seq_replied), "%s:", NONULL (MhReplied));
294 snprintf (seq_flagged, sizeof (seq_flagged), "%s:", NONULL (MhFlagged));
296 if (mh_mkstemp (ctx, &nfp, &tmpfname) != 0) {
301 snprintf (sequences, sizeof (sequences), "%s/.mh_sequences", ctx->path);
304 /* first, copy unknown sequences */
305 if ((ofp = fopen (sequences, "r"))) {
306 while ((buff = mutt_read_line (buff, &s, ofp, &l))) {
307 if (!m_strncmp(buff, seq_unseen, m_strlen(seq_unseen)))
309 if (!m_strncmp(buff, seq_flagged, m_strlen(seq_flagged)))
311 if (!m_strncmp(buff, seq_replied, m_strlen(seq_replied)))
314 fprintf (nfp, "%s\n", buff);
319 /* now, update our unseen, flagged, and replied sequences */
320 for (l = 0; l < ctx->msgcount; l++) {
321 if (ctx->hdrs[l]->deleted)
324 if ((p = strrchr (ctx->hdrs[l]->path, '/')))
327 p = ctx->hdrs[l]->path;
331 if (!ctx->hdrs[l]->read) {
332 mhs_set (&mhs, i, MH_SEQ_UNSEEN);
335 if (ctx->hdrs[l]->flagged) {
336 mhs_set (&mhs, i, MH_SEQ_FLAGGED);
339 if (ctx->hdrs[l]->replied) {
340 mhs_set (&mhs, i, MH_SEQ_REPLIED);
345 /* write out the new sequences */
347 mhs_write_one_sequence (nfp, &mhs, MH_SEQ_UNSEEN, NONULL (MhUnseen));
349 mhs_write_one_sequence (nfp, &mhs, MH_SEQ_FLAGGED, NONULL (MhFlagged));
351 mhs_write_one_sequence (nfp, &mhs, MH_SEQ_REPLIED, NONULL (MhReplied));
353 mhs_free_sequences (&mhs);
356 /* try to commit the changes - no guarantee here */
360 if (safe_rename (tmpfname, sequences) != 0) {
361 /* report an error? */
368 static void mh_sequences_add_one (CONTEXT * ctx, int n, short unseen,
369 short flagged, short replied)
371 short unseen_done = 0;
372 short flagged_done = 0;
373 short replied_done = 0;
375 FILE *ofp = NULL, *nfp = NULL;
378 char sequences[_POSIX_PATH_MAX];
380 char seq_unseen[STRING];
381 char seq_replied[STRING];
382 char seq_flagged[STRING];
388 if (mh_mkstemp (ctx, &nfp, &tmpfname) == -1)
391 snprintf (seq_unseen, sizeof (seq_unseen), "%s:", NONULL (MhUnseen));
392 snprintf (seq_replied, sizeof (seq_replied), "%s:", NONULL (MhReplied));
393 snprintf (seq_flagged, sizeof (seq_flagged), "%s:", NONULL (MhFlagged));
395 snprintf (sequences, sizeof (sequences), "%s/.mh_sequences", ctx->path);
396 if ((ofp = fopen (sequences, "r"))) {
397 while ((buff = mutt_read_line (buff, &sz, ofp, &line))) {
398 if (unseen && !strncmp (buff, seq_unseen, m_strlen(seq_unseen))) {
399 fprintf (nfp, "%s %d\n", buff, n);
403 && !strncmp (buff, seq_flagged, m_strlen(seq_flagged))) {
404 fprintf (nfp, "%s %d\n", buff, n);
408 && !strncmp (buff, seq_replied, m_strlen(seq_replied))) {
409 fprintf (nfp, "%s %d\n", buff, n);
413 fprintf (nfp, "%s\n", buff);
419 if (!unseen_done && unseen)
420 fprintf (nfp, "%s: %d\n", NONULL (MhUnseen), n);
421 if (!flagged_done && flagged)
422 fprintf (nfp, "%s: %d\n", NONULL (MhFlagged), n);
423 if (!replied_done && replied)
424 fprintf (nfp, "%s: %d\n", NONULL (MhReplied), n);
429 if (safe_rename (tmpfname, sequences) != 0)
435 static void mh_update_maildir (struct maildir *md, struct mh_sequences *mhs)
441 for (; md; md = md->next) {
442 if ((p = strrchr (md->h->path, '/')))
448 f = mhs_check (mhs, i);
450 md->h->read = (f & MH_SEQ_UNSEEN) ? 0 : 1;
451 md->h->flagged = (f & MH_SEQ_FLAGGED) ? 1 : 0;
452 md->h->replied = (f & MH_SEQ_REPLIED) ? 1 : 0;
456 /* maildir support */
458 static void maildir_free_entry (struct maildir **md)
463 p_delete(&(*md)->canon_fname);
465 header_delete(&(*md)->h);
470 static void maildir_free_maildir (struct maildir **md)
472 struct maildir *p, *q;
477 for (p = *md; p; p = q) {
479 maildir_free_entry (&p);
483 static void maildir_parse_flags (HEADER * h, const char *path)
491 if ((p = strrchr (path, ':')) != NULL && m_strncmp(p + 1, "2,", 2) == 0) {
494 m_strreplace(&h->maildir_flags, p);
495 q = h->maildir_flags;
509 case 'R': /* replied */
514 case 'T': /* trashed */
527 if (q == h->maildir_flags)
528 p_delete(&h->maildir_flags);
533 static void maildir_update_mtime (CONTEXT * ctx)
535 char buf[_POSIX_PATH_MAX];
538 if (ctx->magic == M_MAILDIR) {
539 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, "cur");
540 if (stat (buf, &st) == 0)
541 ctx->mtime_cur = st.st_mtime;
542 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, "new");
545 snprintf (buf, sizeof (buf), "%s/.mh_sequences", ctx->path);
546 if (stat (buf, &st) == 0)
547 ctx->mtime_cur = st.st_mtime;
549 m_strcpy(buf, sizeof(buf), ctx->path);
552 if (stat (buf, &st) == 0)
553 ctx->mtime = st.st_mtime;
557 * Actually parse a maildir message. This may also be used to fill
558 * out a fake header structure generated by lazy maildir parsing.
560 static HEADER *maildir_parse_message (int magic, const char *fname,
561 int is_old, HEADER * _h)
567 if ((f = fopen (fname, "r")) != NULL) {
570 h->env = mutt_read_rfc822_header (f, h, 0, 0);
572 fstat (fileno (f), &st);
576 h->received = h->date_sent;
578 if (h->content->length <= 0)
579 h->content->length = st.st_size - h->content->offset;
583 if (magic == M_MAILDIR) {
585 * maildir stores its flags in the filename, so ignore the
586 * flags in the header of the message
590 maildir_parse_flags (h, fname);
598 * Note that this routine will _not_ modify the context given by
601 * It's used in the first parsing pass on maildir and MH folders.
602 * In the MH case, this means full parsing of the folder. In the
603 * maildir case, it means that we only look at flags, and create a
604 * fake HEADER structure, which may later be filled in by
605 * maildir_parse_message(), when called from
606 * maildir_delayed_parsing().
610 static int maildir_parse_entry (CONTEXT * ctx, struct maildir ***last,
611 const char *subdir, const char *fname,
612 int *count, int is_old, ino_t inode __attribute__ ((unused)))
614 struct maildir *entry;
616 char buf[_POSIX_PATH_MAX];
619 snprintf (buf, sizeof (buf), "%s/%s/%s", ctx->path, subdir, fname);
621 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, fname);
623 if (ctx->magic == M_MH)
624 h = maildir_parse_message (ctx->magic, buf, is_old, NULL);
628 maildir_parse_flags (h, buf);
634 if (!ctx->quiet && ReadInc && ((*count % ReadInc) == 0 || *count == 1))
635 mutt_message (_("Reading %s... %d"), ctx->path, *count);
639 snprintf (buf, sizeof (buf), "%s/%s", subdir, fname);
640 h->path = m_strdup(buf);
643 h->path = m_strdup(fname);
645 entry = p_new(struct maildir, 1);
647 entry->header_parsed = (ctx->magic == M_MH);
649 *last = &entry->next;
659 /* Ignore the garbage files. A valid MH message consists of only
660 * digits. Deleted message get moved to a filename with a comma before
664 int mh_valid_message (const char *s)
667 if (!isdigit ((unsigned char) *s))
673 static int maildir_parse_dir (CONTEXT * ctx, struct maildir ***last,
674 const char *subdir, int *count)
678 char buf[_POSIX_PATH_MAX];
682 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, subdir);
683 is_old = (m_strcmp("cur", subdir) == 0);
686 m_strcpy(buf, sizeof(buf), ctx->path);
688 if ((dirp = opendir (buf)) == NULL)
691 while ((de = readdir (dirp)) != NULL) {
693 if ((ctx->magic == M_MH && !mh_valid_message (de->d_name))
694 || (ctx->magic == M_MAILDIR && *de->d_name == '.'))
697 /* FOO - really ignore the return value? */
699 debug_print (2, ("parsing %s\n", de->d_name));
700 maildir_parse_entry (ctx, last, subdir, de->d_name, count, is_old,
701 #if HAVE_DIRENT_D_INO
713 static int maildir_add_to_context (CONTEXT * ctx, struct maildir *md)
715 int oldmsgcount = ctx->msgcount;
719 debug_print (2, ("considering %s\n", NONULL (md->canon_fname)));
722 debug_print (2, ("flags: %s%s%s%s%s\n", md->h->flagged ? "f" : "",
723 md->h->deleted ? "D" : "", md->h->replied ? "r" : "",
724 md->h->old ? "O" : "", md->h->read ? "R" : ""));
725 if (ctx->msgcount == ctx->hdrmax)
726 mx_alloc_memory (ctx);
728 ctx->hdrs[ctx->msgcount] = md->h;
729 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
731 md->h->content->length + md->h->content->offset -
732 md->h->content->hdr_offset;
740 if (ctx->msgcount > oldmsgcount) {
741 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
747 static int maildir_move_to_context (CONTEXT * ctx, struct maildir **md)
751 r = maildir_add_to_context (ctx, *md);
752 maildir_free_maildir (md);
757 static ssize_t maildir_hcache_keylen (const char *fn)
759 return m_strchrnul(fn, ':') - fn;
764 * This function does the second parsing pass for a maildir-style
767 void maildir_delayed_parsing (CONTEXT * ctx, struct maildir *md)
770 char fn[_POSIX_PATH_MAX];
776 struct timeval *when = NULL;
777 struct stat lastchanged;
780 hc = mutt_hcache_open (HeaderCache, ctx->path);
783 for (p = md, count = 0; p; p = p->next, count++) {
784 if (!(p && p->h && !p->header_parsed))
788 data = mutt_hcache_fetch (hc, p->h->path + 3, &maildir_hcache_keylen);
789 when = (struct timeval *) data;
792 if (!ctx->quiet && ReadInc && ((count % ReadInc) == 0 || count == 1))
793 mutt_message (_("Reading %s... %d"), ctx->path, count);
794 snprintf (fn, sizeof (fn), "%s/%s", ctx->path, p->h->path);
797 if (option (OPTHCACHEVERIFY)) {
798 ret = stat (fn, &lastchanged);
801 lastchanged.st_mtime = 0;
805 if (data != NULL && !ret && lastchanged.st_mtime <= when->tv_sec) {
806 p->h = mutt_hcache_restore ((unsigned char *) data, &p->h);
807 maildir_parse_flags (p->h, fn);
811 if (maildir_parse_message (ctx->magic, fn, p->h->old, p->h)) {
812 p->header_parsed = 1;
813 maildir_parse_flags (p->h, fn);
815 mutt_hcache_store (hc, p->h->path + 3, p->h, 0, &maildir_hcache_keylen);
819 header_delete(&p->h);
825 mutt_hcache_close (hc);
829 /* Read a MH/maildir style mailbox.
832 * ctx [IN/OUT] context for this mailbox
833 * subdir [IN] NULL for MH mailboxes, otherwise the subdir of the
834 * maildir mailbox to read from
836 static int _mh_read_dir (CONTEXT * ctx, const char *subdir)
839 struct mh_sequences mhs;
840 struct maildir **last;
846 maildir_update_mtime (ctx);
851 if (maildir_parse_dir (ctx, &last, subdir, &count) == -1)
854 if (ctx->magic == M_MH) {
855 mh_read_sequences (&mhs, ctx->path);
856 mh_update_maildir (md, &mhs);
857 mhs_free_sequences (&mhs);
860 if (ctx->magic == M_MAILDIR)
861 maildir_delayed_parsing (ctx, md);
863 maildir_move_to_context (ctx, &md);
867 static int mh_read_dir (CONTEXT* ctx) {
868 return (_mh_read_dir (ctx, NULL));
871 /* read a maildir style mailbox */
872 static int maildir_read_dir (CONTEXT * ctx)
874 /* maildir looks sort of like MH, except that there are two subdirectories
875 * of the main folder path from which to read messages
877 if (_mh_read_dir (ctx, "new") == -1 || _mh_read_dir (ctx, "cur") == -1)
884 * Open a new (temporary) message in an MH folder.
887 static int mh_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr __attribute__ ((unused)))
889 return mh_mkstemp (dest, &msg->fp, &msg->path);
892 int ch_compar (const void *a, const void *b)
894 return (int) (*((const char *) a) - *((const char *) b));
897 static void maildir_flags (char *dest, ssize_t destlen, HEADER * hdr)
902 * The maildir specification requires that all files in the cur
903 * subdirectory have the :unique string appeneded, regardless of whether
904 * or not there are any flags. If .old is set, we know that this message
905 * will end up in the cur directory, so we include it in the following
906 * test even though there is no associated flag.
910 && (hdr->flagged || hdr->replied || hdr->read || hdr->deleted
911 || hdr->old || hdr->maildir_flags)) {
912 char tmp[LONG_STRING];
914 snprintf (tmp, sizeof (tmp),
916 hdr->flagged ? "F" : "",
917 hdr->replied ? "R" : "",
918 hdr->read ? "S" : "", hdr->deleted ? "T" : "",
919 NONULL (hdr->maildir_flags));
920 if (hdr->maildir_flags)
921 qsort (tmp, m_strlen(tmp), 1, ch_compar);
922 snprintf (dest, destlen, ":2,%s", tmp);
928 * Open a new (temporary) message in a maildir folder.
930 * Note that this uses _almost_ the maildir file name format, but
931 * with a {cur,new} prefix.
935 static int maildir_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr)
938 char path[_POSIX_PATH_MAX];
943 short deleted = hdr->deleted;
947 maildir_flags (suffix, sizeof (suffix), hdr);
949 hdr->deleted = deleted;
954 if (hdr && (hdr->read || hdr->old))
955 m_strcpy(subdir, sizeof(subdir), "cur");
957 m_strcpy(subdir, sizeof(subdir), "new");
960 snprintf (path, _POSIX_PATH_MAX, "%s/tmp/%s.%ld.%u_%d.%s%s",
961 dest->path, subdir, (long) time (NULL),
962 (unsigned int) getpid (), Counter++, NONULL (Hostname), suffix);
964 debug_print (2, ("trying %s.\n", path));
967 if ((fd = open (path, O_WRONLY | O_EXCL | O_CREAT, 0666)) == -1) {
968 if (errno != EEXIST) {
974 debug_print (2, ("success.\n"));
975 msg->path = m_strdup(path);
980 if ((msg->fp = fdopen (fd, "w")) == NULL) {
981 p_delete(&msg->path);
993 * Commit a message to a maildir folder.
995 * msg->path contains the file name of a file in tmp/. We take the
996 * flags from this file's name.
998 * ctx is the mail folder we commit to.
1000 * hdr is a header structure to which we write the message's new
1001 * file name. This is used in the mh and maildir folder synch
1002 * routines. When this routine is invoked from mx_commit_message,
1005 * msg->path looks like this:
1007 * tmp/{cur,new}.mutt-HOSTNAME-PID-COUNTER:flags
1009 * See also maildir_open_new_message().
1013 static int maildir_commit_message (MESSAGE * msg, CONTEXT * ctx, HEADER * hdr)
1017 char path[_POSIX_PATH_MAX];
1018 char full[_POSIX_PATH_MAX];
1021 if (safe_fclose (&msg->fp) != 0)
1024 /* extract the subdir */
1025 s = strrchr (msg->path, '/') + 1;
1026 m_strcpy(subdir, sizeof(subdir), s);
1028 /* extract the flags */
1029 if ((s = strchr (s, ':')))
1030 m_strcpy(suffix, sizeof(suffix), s);
1034 /* construct a new file name. */
1036 snprintf (path, _POSIX_PATH_MAX, "%s/%ld.%u_%d.%s%s", subdir,
1037 (long) time (NULL), (unsigned int) getpid (), Counter++,
1038 NONULL (Hostname), suffix);
1039 snprintf (full, _POSIX_PATH_MAX, "%s/%s", ctx->path, path);
1041 debug_print (2, ("renaming %s to %s.\n", msg->path, full));
1043 if (safe_rename (msg->path, full) == 0) {
1045 m_strreplace(&hdr->path, path);
1046 p_delete(&msg->path);
1049 * Adjust the mtime on the file to match the time at which this
1050 * message was received. Currently this is only set when copying
1051 * messages between mailboxes, so we test to ensure that it is
1054 if (msg->received) {
1057 ut.actime = msg->received;
1058 ut.modtime = msg->received;
1059 if (utime (full, &ut)) {
1061 ("maildir_commit_message(): unable to set time on file"));
1068 else if (errno != EEXIST) {
1069 mutt_perror (ctx->path);
1076 * commit a message to an MH folder.
1081 static int _mh_commit_message (MESSAGE * msg, CONTEXT * ctx, HEADER * hdr,
1087 unsigned int n, hi = 0;
1088 char path[_POSIX_PATH_MAX];
1091 if (safe_fclose (&msg->fp) != 0)
1094 if ((dirp = opendir (ctx->path)) == NULL) {
1095 mutt_perror (ctx->path);
1099 /* figure out what the next message number is */
1100 while ((de = readdir (dirp)) != NULL) {
1106 if (!isdigit ((unsigned char) *cp))
1119 * Now try to rename the file to the proper name.
1121 * Note: We may have to try multiple times, until we find a free
1127 snprintf (tmp, sizeof (tmp), "%d", hi);
1128 snprintf (path, sizeof (path), "%s/%s", ctx->path, tmp);
1129 if (safe_rename (msg->path, path) == 0) {
1131 m_strreplace(&hdr->path, tmp);
1132 p_delete(&msg->path);
1135 else if (errno != EEXIST) {
1136 mutt_perror (ctx->path);
1141 mh_sequences_add_one (ctx, hi, !msg->flags.read, msg->flags.flagged,
1142 msg->flags.replied);
1146 static int mh_commit_message (MESSAGE * msg, CONTEXT * ctx, HEADER * hdr) {
1147 return _mh_commit_message (msg, ctx, hdr, 1);
1150 /* Sync a message in an MH folder.
1152 * This code is also used for attachment deletion in maildir
1156 static int mh_rewrite_message (CONTEXT * ctx, int msgno)
1158 HEADER *h = ctx->hdrs[msgno];
1163 char oldpath[_POSIX_PATH_MAX];
1164 char newpath[_POSIX_PATH_MAX];
1165 char partpath[_POSIX_PATH_MAX];
1167 long old_body_offset = h->content->offset;
1168 long old_body_length = h->content->length;
1169 long old_hdr_lines = h->lines;
1171 if ((dest = mx_open_new_message (ctx, h, 0)) == NULL)
1174 if ((rc = mutt_copy_message (dest->fp, ctx, h,
1175 M_CM_UPDATE, CH_UPDATE | CH_UPDATE_LEN)) == 0)
1177 snprintf (oldpath, sizeof(oldpath), "%s/%s", ctx->path, h->path);
1178 m_strcpy(partpath, sizeof(partpath), h->path);
1180 if (ctx->magic == M_MAILDIR)
1181 rc = maildir_commit_message (dest, ctx, h);
1183 rc = _mh_commit_message (dest, ctx, h, 0);
1185 mx_close_message (&dest);
1193 * Try to move the new message to the old place.
1196 * This is important when we are just updating flags.
1198 * Note that there is a race condition against programs which
1199 * use the first free slot instead of the maximum message
1200 * number. Mutt does _not_ behave like this.
1202 * Anyway, if this fails, the message is in the folder, so
1203 * all what happens is that a concurrently runnung mutt will
1204 * lose flag modifications.
1207 if (ctx->magic == M_MH && rc == 0) {
1208 snprintf (newpath, _POSIX_PATH_MAX, "%s/%s", ctx->path, h->path);
1209 if ((rc = safe_rename (newpath, oldpath)) == 0)
1210 m_strreplace(&h->path, partpath);
1214 mx_close_message (&dest);
1216 if (rc == -1 && restore) {
1217 h->content->offset = old_body_offset;
1218 h->content->length = old_body_length;
1219 h->lines = old_hdr_lines;
1222 mutt_free_body (&h->content->parts);
1226 static int mh_sync_message (CONTEXT * ctx, int msgno)
1228 HEADER *h = ctx->hdrs[msgno];
1230 if (h->attach_del ||
1231 (h->env && (h->env->refs_changed || h->env->irt_changed)))
1232 if (mh_rewrite_message (ctx, msgno) != 0)
1238 static int maildir_sync_message (CONTEXT * ctx, int msgno)
1240 HEADER *h = ctx->hdrs[msgno];
1242 if (h->attach_del ||
1243 (h->env && (h->env->refs_changed || h->env->irt_changed))) {
1244 /* when doing attachment deletion/rethreading, fall back to the MH case. */
1245 if (mh_rewrite_message (ctx, msgno) != 0)
1249 /* we just have to rename the file. */
1251 char newpath[_POSIX_PATH_MAX];
1252 char partpath[_POSIX_PATH_MAX];
1253 char fullpath[_POSIX_PATH_MAX];
1254 char oldpath[_POSIX_PATH_MAX];
1258 if ((p = strrchr (h->path, '/')) == NULL) {
1259 debug_print (1, ("%s: unable to find subdir!\n", h->path));
1263 m_strcpy(newpath, sizeof(newpath), p);
1265 /* kill the previous flags */
1266 if ((p = strchr (newpath, ':')) != NULL)
1269 maildir_flags (suffix, sizeof (suffix), h);
1271 snprintf (partpath, sizeof (partpath), "%s/%s%s",
1272 (h->read || h->old) ? "cur" : "new", newpath, suffix);
1273 snprintf (fullpath, sizeof (fullpath), "%s/%s", ctx->path, partpath);
1274 snprintf (oldpath, sizeof (oldpath), "%s/%s", ctx->path, h->path);
1276 if (m_strcmp(fullpath, oldpath) == 0) {
1277 /* message hasn't really changed */
1281 /* record that the message is possibly marked as trashed on disk */
1282 h->trash = h->deleted;
1284 if (rename (oldpath, fullpath) != 0) {
1285 mutt_perror ("rename");
1288 m_strreplace(&h->path, partpath);
1293 static int mh_sync_mailbox (CONTEXT * ctx, int unused __attribute__ ((unused)), int *index_hint)
1295 char path[_POSIX_PATH_MAX], tmp[_POSIX_PATH_MAX];
1300 #endif /* USE_HCACHE */
1302 if (ctx->magic == M_MH)
1303 i = mh_check_mailbox (ctx, index_hint, 0);
1305 i = maildir_check_mailbox (ctx, index_hint, 0);
1311 if (ctx->magic == M_MAILDIR)
1312 hc = mutt_hcache_open (HeaderCache, ctx->path);
1313 #endif /* USE_HCACHE */
1315 for (i = 0; i < ctx->msgcount; i++) {
1316 if (ctx->hdrs[i]->deleted
1317 && (ctx->magic != M_MAILDIR || !option (OPTMAILDIRTRASH))) {
1318 snprintf (path, sizeof (path), "%s/%s", ctx->path, ctx->hdrs[i]->path);
1319 if (ctx->magic == M_MAILDIR
1320 || (option (OPTMHPURGE) && ctx->magic == M_MH)) {
1322 if (ctx->magic == M_MAILDIR)
1323 mutt_hcache_delete (hc, ctx->hdrs[i]->path + 3,
1324 &maildir_hcache_keylen);
1325 #endif /* USE_HCACHE */
1328 else if (ctx->magic == M_MH) {
1329 /* MH just moves files out of the way when you delete them */
1330 if (*ctx->hdrs[i]->path != ',') {
1331 snprintf (tmp, sizeof (tmp), "%s/,%s", ctx->path,
1332 ctx->hdrs[i]->path);
1339 else if (ctx->hdrs[i]->changed || ctx->hdrs[i]->attach_del ||
1340 (ctx->magic == M_MAILDIR
1341 && (option (OPTMAILDIRTRASH) || ctx->hdrs[i]->trash)
1342 && (ctx->hdrs[i]->deleted != ctx->hdrs[i]->trash))) {
1343 if (ctx->magic == M_MAILDIR) {
1344 if (maildir_sync_message (ctx, i) == -1)
1348 if (mh_sync_message (ctx, i) == -1)
1355 if (ctx->magic == M_MAILDIR)
1356 mutt_hcache_close (hc);
1357 #endif /* USE_HCACHE */
1359 if (ctx->magic == M_MH)
1360 mh_update_sequences (ctx);
1362 /* XXX race condition? */
1364 maildir_update_mtime (ctx);
1366 /* adjust indices */
1369 for (i = 0, j = 0; i < ctx->msgcount; i++) {
1370 if (!ctx->hdrs[i]->deleted
1371 || (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))
1372 ctx->hdrs[i]->index = j++;
1380 if (ctx->magic == M_MAILDIR)
1381 mutt_hcache_close (hc);
1382 #endif /* USE_HCACHE */
1386 static char *maildir_canon_filename (char *dest, const char *src, ssize_t l)
1390 if ((t = strrchr (src, '/')))
1393 m_strcpy(dest, l, src);
1394 if ((u = strrchr (dest, ':')))
1400 static void maildir_update_tables (CONTEXT * ctx, int *index_hint)
1406 if (Sort != SORT_ORDER) {
1409 mutt_sort_headers (ctx, 1);
1413 old_count = ctx->msgcount;
1414 for (i = 0, j = 0; i < old_count; i++) {
1415 if (ctx->hdrs[i]->active && index_hint && *index_hint == i)
1418 if (ctx->hdrs[i]->active)
1419 ctx->hdrs[i]->index = j++;
1422 mx_update_tables (ctx, 0);
1423 mutt_clear_threads (ctx);
1426 static void maildir_update_flags (CONTEXT * ctx, HEADER * o, HEADER * n)
1428 /* save the global state here so we can reset it at the
1429 * end of list block if required.
1431 int context_changed = ctx->changed;
1433 /* user didn't modify this message. alter the flags to
1434 * match the current state on disk. This may not actually
1435 * do anything, but we can't tell right now. mutt_set_flag()
1436 * will just ignore the call if the status bits are
1437 * already properly set.
1439 mutt_set_flag (ctx, o, M_FLAG, n->flagged);
1440 mutt_set_flag (ctx, o, M_REPLIED, n->replied);
1441 mutt_set_flag (ctx, o, M_READ, n->read);
1442 mutt_set_flag (ctx, o, M_OLD, n->old);
1444 /* mutt_set_flag() will set this, but we don't need to
1445 * sync the changes we made because we just updated the
1446 * context to match the current on-disk state of the
1451 /* if the mailbox was not modified before we made these
1452 * changes, unset the changed flag since nothing needs to
1455 if (!context_changed)
1460 /* This function handles arrival of new mail and reopening of
1461 * maildir folders. The basic idea here is we check to see if either
1462 * the new or cur subdirectories have changed, and if so, we scan them
1463 * for the list of files. We check for newly added messages, and
1464 * then merge the flags messages we already knew about. We don't treat
1465 * either subdirectory differently, as mail could be copied directly into
1466 * the cur directory from another agent.
1468 static int maildir_check_mailbox (CONTEXT * ctx, int *index_hint, int unused __attribute__ ((unused)))
1470 struct stat st_new; /* status of the "new" subdirectory */
1471 struct stat st_cur; /* status of the "cur" subdirectory */
1472 char buf[_POSIX_PATH_MAX];
1473 int changed = 0; /* bitmask representing which subdirectories
1474 have changed. 0x1 = new, 0x2 = cur */
1475 int occult = 0; /* messages were removed from the mailbox */
1476 int have_new = 0; /* messages were added to the mailbox */
1477 struct maildir *md; /* list of messages in the mailbox */
1478 struct maildir **last, *p;
1480 HASH *fnames; /* hash table for quickly looking up the base filename
1481 for a maildir message */
1483 if (!option (OPTCHECKNEW))
1486 snprintf (buf, sizeof (buf), "%s/new", ctx->path);
1487 if (stat (buf, &st_new) == -1)
1490 snprintf (buf, sizeof (buf), "%s/cur", ctx->path);
1491 if (stat (buf, &st_cur) == -1)
1494 /* determine which subdirectories need to be scanned */
1495 if (st_new.st_mtime > ctx->mtime)
1497 if (st_cur.st_mtime > ctx->mtime_cur)
1501 return 0; /* nothing to do */
1503 /* update the modification times on the mailbox */
1504 ctx->mtime_cur = st_cur.st_mtime;
1505 ctx->mtime = st_new.st_mtime;
1507 /* do a fast scan of just the filenames in
1508 * the subdirectories that have changed.
1513 maildir_parse_dir (ctx, &last, "new", NULL);
1515 maildir_parse_dir (ctx, &last, "cur", NULL);
1517 /* we create a hash table keyed off the canonical (sans flags) filename
1518 * of each message we scanned. This is used in the loop over the
1519 * existing messages below to do some correlation.
1521 fnames = hash_create (1031);
1523 for (p = md; p; p = p->next) {
1524 maildir_canon_filename (buf, p->h->path, sizeof (buf));
1525 p->canon_fname = m_strdup(buf);
1526 hash_insert (fnames, p->canon_fname, p, 0);
1529 /* check for modifications and adjust flags */
1530 for (i = 0; i < ctx->msgcount; i++) {
1531 ctx->hdrs[i]->active = 0;
1532 maildir_canon_filename (buf, ctx->hdrs[i]->path, sizeof (buf));
1533 p = hash_find (fnames, buf);
1535 /* message already exists, merge flags */
1536 ctx->hdrs[i]->active = 1;
1538 /* check to see if the message has moved to a different
1539 * subdirectory. If so, update the associated filename.
1541 if (m_strcmp(ctx->hdrs[i]->path, p->h->path))
1542 m_strreplace(&ctx->hdrs[i]->path, p->h->path);
1544 /* if the user hasn't modified the flags on this message, update
1545 * the flags we just detected.
1547 if (!ctx->hdrs[i]->changed)
1548 maildir_update_flags (ctx, ctx->hdrs[i], p->h);
1550 if (ctx->hdrs[i]->deleted == ctx->hdrs[i]->trash)
1551 ctx->hdrs[i]->deleted = p->h->deleted;
1552 ctx->hdrs[i]->trash = p->h->trash;
1554 /* this is a duplicate of an existing header, so remove it */
1555 header_delete(&p->h);
1557 /* This message was not in the list of messages we just scanned.
1558 * Check to see if we have enough information to know if the
1559 * message has disappeared out from underneath us.
1561 else if (((changed & 1) && (!strncmp (ctx->hdrs[i]->path, "new/", 4))) ||
1562 ((changed & 2) && (!strncmp (ctx->hdrs[i]->path, "cur/", 4)))) {
1563 /* This message disappeared, so we need to simulate a "reopen"
1564 * event. We know it disappeared because we just scanned the
1565 * subdirectory it used to reside in.
1570 /* This message resides in a subdirectory which was not
1571 * modified, so we assume that it is still present and
1574 ctx->hdrs[i]->active = 1;
1578 /* destroy the file name hash */
1579 hash_destroy (&fnames, NULL);
1581 /* If we didn't just get new mail, update the tables. */
1583 maildir_update_tables (ctx, index_hint);
1585 /* do any delayed parsing we need to do. */
1586 maildir_delayed_parsing (ctx, md);
1588 /* Incorporate new messages */
1589 have_new = maildir_move_to_context (ctx, &md);
1591 return occult ? M_REOPENED : (have_new ? M_NEW_MAIL : 0);
1595 * This function handles arrival of new mail and reopening of
1596 * mh/maildir folders. Things are getting rather complex because we
1597 * don't have a well-defined "mailbox order", so the tricks from
1598 * mbox.c and mx.c won't work here.
1600 * Don't change this code unless you _really_ understand what
1605 static int mh_check_mailbox (CONTEXT * ctx, int *index_hint, int unused __attribute__ ((unused)))
1607 char buf[_POSIX_PATH_MAX];
1608 struct stat st, st_cur;
1609 short modified = 0, have_new = 0, occult = 0;
1610 struct maildir *md, *p;
1611 struct maildir **last = NULL;
1612 struct mh_sequences mhs;
1616 if (!option (OPTCHECKNEW))
1619 m_strcpy(buf, sizeof(buf), ctx->path);
1620 if (stat (buf, &st) == -1)
1623 /* create .mh_sequences when there isn't one. */
1624 snprintf (buf, sizeof (buf), "%s/.mh_sequences", ctx->path);
1625 if ((i = stat (buf, &st_cur) == -1) && errno == ENOENT) {
1629 if (mh_mkstemp (ctx, &fp, &tmp) == 0) {
1631 if (safe_rename (tmp, buf) == -1)
1637 if (i == -1 && stat (buf, &st_cur) == -1)
1640 if (st.st_mtime > ctx->mtime || st_cur.st_mtime > ctx->mtime_cur)
1646 ctx->mtime_cur = st_cur.st_mtime;
1647 ctx->mtime = st.st_mtime;
1653 maildir_parse_dir (ctx, &last, NULL, NULL);
1654 mh_read_sequences (&mhs, ctx->path);
1655 mh_update_maildir (md, &mhs);
1656 mhs_free_sequences (&mhs);
1658 /* check for modifications and adjust flags */
1659 fnames = hash_create (1031);
1661 for (p = md; p; p = p->next)
1662 hash_insert (fnames, p->h->path, p, 0);
1664 for (i = 0; i < ctx->msgcount; i++) {
1665 ctx->hdrs[i]->active = 0;
1667 if ((p = hash_find (fnames, ctx->hdrs[i]->path)) && p->h &&
1668 (mutt_cmp_header (ctx->hdrs[i], p->h))) {
1669 ctx->hdrs[i]->active = 1;
1670 /* found the right message */
1671 if (!ctx->hdrs[i]->changed)
1672 maildir_update_flags (ctx, ctx->hdrs[i], p->h);
1674 header_delete(&p->h);
1676 else /* message has disappeared */
1680 /* destroy the file name hash */
1682 hash_destroy (&fnames, NULL);
1684 /* If we didn't just get new mail, update the tables. */
1686 maildir_update_tables (ctx, index_hint);
1688 /* Incorporate new messages */
1689 have_new = maildir_move_to_context (ctx, &md);
1691 return occult ? M_REOPENED : (have_new ? M_NEW_MAIL : 0);
1698 * These functions try to find a message in a maildir folder when it
1699 * has moved under our feet. Note that this code is rather expensive, but
1700 * then again, it's called rarely.
1703 FILE *_maildir_open_find_message (const char *folder, const char *unique,
1704 const char *subfolder)
1706 char dir[_POSIX_PATH_MAX];
1707 char tunique[_POSIX_PATH_MAX];
1708 char fname[_POSIX_PATH_MAX];
1716 snprintf (dir, sizeof (dir), "%s/%s", folder, subfolder);
1718 if ((dp = opendir (dir)) == NULL) {
1723 while ((de = readdir (dp))) {
1724 maildir_canon_filename (tunique, de->d_name, sizeof (tunique));
1726 if (!m_strcmp(tunique, unique)) {
1727 snprintf (fname, sizeof (fname), "%s/%s/%s", folder, subfolder,
1729 fp = fopen (fname, "r"); /* __FOPEN_CHECKED__ */
1741 FILE *maildir_open_find_message (const char *folder, const char *msg)
1743 char unique[_POSIX_PATH_MAX];
1746 static unsigned int new_hits = 0, cur_hits = 0; /* simple dynamic optimization */
1748 maildir_canon_filename (unique, msg, sizeof (unique));
1751 _maildir_open_find_message (folder, unique,
1752 new_hits > cur_hits ? "new" : "cur"))
1753 || errno != ENOENT) {
1754 if (new_hits < UINT_MAX && cur_hits < UINT_MAX) {
1755 new_hits += (new_hits > cur_hits ? 1 : 0);
1756 cur_hits += (new_hits > cur_hits ? 0 : 1);
1762 _maildir_open_find_message (folder, unique,
1763 new_hits > cur_hits ? "cur" : "new"))
1764 || errno != ENOENT) {
1765 if (new_hits < UINT_MAX && cur_hits < UINT_MAX) {
1766 new_hits += (new_hits > cur_hits ? 0 : 1);
1767 cur_hits += (new_hits > cur_hits ? 1 : 0);
1779 * 1 if there are no messages in the mailbox
1780 * 0 if there are messages in the mailbox
1783 static int maildir_check_empty (const char *path)
1787 int r = 1; /* assume empty until we find a message */
1788 char frealpath[_POSIX_PATH_MAX];
1791 /* Strategy here is to look for any file not beginning with a period */
1794 /* we do "cur" on the first iteration since its more likely that we'll
1795 * find old messages without having to scan both subdirs
1797 snprintf (frealpath, sizeof (frealpath), "%s/%s", path,
1798 iter == 0 ? "cur" : "new");
1799 if ((dp = opendir (frealpath)) == NULL)
1801 while ((de = readdir (dp))) {
1802 if (*de->d_name != '.') {
1809 } while (r && iter < 2);
1816 * 1 if there are no messages in the mailbox
1817 * 0 if there are messages in the mailbox
1820 int mh_check_empty (const char *path)
1824 int r = 1; /* assume empty until we find a message */
1826 if ((dp = opendir (path)) == NULL)
1828 while ((de = readdir (dp))) {
1829 if (mh_valid_message (de->d_name)) {
1839 static int mh_is_magic (const char* path, struct stat* st) {
1840 char tmp[_POSIX_PATH_MAX];
1842 if (S_ISDIR (st->st_mode)) {
1843 snprintf (tmp, sizeof (tmp), "%s/.mh_sequences", path);
1844 if (access (tmp, F_OK) == 0)
1847 snprintf (tmp, sizeof (tmp), "%s/.xmhcache", path);
1848 if (access (tmp, F_OK) == 0)
1851 snprintf (tmp, sizeof (tmp), "%s/.mew_cache", path);
1852 if (access (tmp, F_OK) == 0)
1855 snprintf (tmp, sizeof (tmp), "%s/.mew-cache", path);
1856 if (access (tmp, F_OK) == 0)
1859 snprintf (tmp, sizeof (tmp), "%s/.sylpheed_cache", path);
1860 if (access (tmp, F_OK) == 0)
1864 * ok, this isn't an mh folder, but mh mode can be used to read
1865 * Usenet news from the spool. ;-)
1868 snprintf (tmp, sizeof (tmp), "%s/.overview", path);
1869 if (access (tmp, F_OK) == 0)
1875 static int maildir_is_magic (const char* path, struct stat* st) {
1877 char tmp[_POSIX_PATH_MAX];
1879 if (S_ISDIR (st->st_mode)) {
1880 snprintf (tmp, sizeof (tmp), "%s/cur", path);
1881 if (stat (tmp, &sb) == 0 && S_ISDIR (sb.st_mode))
1887 /* routines common to maildir and mh */
1888 static mx_t* reg_mx (void) {
1889 mx_t* fmt = p_new(mx_t, 1);
1891 fmt->mx_access = access;
1892 fmt->mx_sync_mailbox = mh_sync_mailbox;
1896 static int mh_commit (MESSAGE* msg, CONTEXT* ctx) {
1897 return (mh_commit_message (msg, ctx, NULL));
1900 static int maildir_commit (MESSAGE* msg, CONTEXT* ctx) {
1901 return (maildir_commit_message (msg, ctx, NULL));
1904 mx_t* mh_reg_mx (void) {
1905 mx_t* fmt = reg_mx ();
1907 fmt->mx_check_empty = mh_check_empty;
1908 fmt->mx_is_magic = mh_is_magic;
1909 fmt->mx_open_mailbox = mh_read_dir;
1910 fmt->mx_open_new_message = mh_open_new_message;
1911 fmt->mx_check_mailbox = mh_check_mailbox;
1912 fmt->mx_commit_message = mh_commit;
1916 mx_t* maildir_reg_mx (void) {
1917 mx_t* fmt = reg_mx ();
1918 fmt->type = M_MAILDIR;
1919 fmt->mx_check_empty = maildir_check_empty;
1920 fmt->mx_is_magic = maildir_is_magic;
1921 fmt->mx_open_mailbox = maildir_read_dir;
1922 fmt->mx_open_new_message = maildir_open_new_message;
1923 fmt->mx_check_mailbox = maildir_check_mailbox;
1924 fmt->mx_commit_message = maildir_commit;