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
33 #include <sys/types.h>
53 unsigned header_parsed:1;
56 #endif /* USE_INODESORT */
65 /* mh_sequences support */
67 #define MH_SEQ_UNSEEN (1 << 0)
68 #define MH_SEQ_REPLIED (1 << 1)
69 #define MH_SEQ_FLAGGED (1 << 2)
72 static int maildir_check_empty (const char*);
74 static void mhs_alloc (struct mh_sequences *mhs, int i)
79 if (i > mhs->max || !mhs->flags) {
81 safe_realloc (&mhs->flags, sizeof (mhs->flags[0]) * (newmax + 1));
82 for (j = mhs->max + 1; j <= newmax; j++)
89 static void mhs_free_sequences (struct mh_sequences *mhs)
94 static short mhs_check (struct mh_sequences *mhs, int i)
96 if (!mhs->flags || i > mhs->max)
102 static short mhs_set (struct mh_sequences *mhs, int i, short f)
106 return mhs->flags[i];
113 static short mhs_unset (struct mh_sequences *mhs, int i, short f)
117 return mhs->flags[i];
122 static void mh_read_token (char *t, int *first, int *last)
126 if ((p = strchr (t, '-'))) {
132 *first = *last = atoi (t);
135 static void mh_read_sequences (struct mh_sequences *mhs, const char *path)
146 char pathname[_POSIX_PATH_MAX];
148 snprintf (pathname, sizeof (pathname), "%s/.mh_sequences", path);
150 if (!(fp = fopen (pathname, "r")))
153 while ((buff = mutt_read_line (buff, &sz, fp, &line))) {
154 if (!(t = strtok (buff, " \t:")))
157 if (!safe_strcmp (t, MhUnseen))
159 else if (!safe_strcmp (t, MhFlagged))
161 else if (!safe_strcmp (t, MhReplied))
163 else /* unknown sequence */
166 while ((t = strtok (NULL, " \t:"))) {
167 mh_read_token (t, &first, &last);
168 for (; first <= last; first++)
169 mhs_set (mhs, first, f);
177 int mh_buffy (const char *path)
180 struct mh_sequences mhs;
182 memset (&mhs, 0, sizeof (mhs));
184 mh_read_sequences (&mhs, path);
185 for (i = 0; !r && i <= mhs.max; i++)
186 if (mhs_check (&mhs, i) & MH_SEQ_UNSEEN)
188 mhs_free_sequences (&mhs);
192 static int mh_mkstemp (CONTEXT * dest, FILE ** fp, char **tgt)
195 char path[_POSIX_PATH_MAX];
198 snprintf (path, _POSIX_PATH_MAX, "%s/.mutt-%s-%d-%d",
199 dest->path, NONULL (Hostname), (int) getpid (), Counter++);
201 if ((fd = open (path, O_WRONLY | O_EXCL | O_CREAT, 0666)) == -1) {
202 if (errno != EEXIST) {
208 *tgt = safe_strdup (path);
213 if ((*fp = fdopen (fd, "w")) == NULL) {
223 static void mhs_write_one_sequence (FILE * fp, struct mh_sequences *mhs,
224 short f, const char *tag)
229 fprintf (fp, "%s:", tag);
234 for (i = 0; i <= mhs->max; i++) {
235 if ((mhs_check (mhs, i) & f)) {
241 else if (first >= 0) {
243 fprintf (fp, " %d", first);
245 fprintf (fp, " %d-%d", first, last);
254 fprintf (fp, " %d", first);
256 fprintf (fp, " %d-%d", first, last);
262 /* XXX - we don't currently remove deleted messages from sequences we don't know. Should we? */
264 void mh_update_sequences (CONTEXT * ctx)
268 char sequences[_POSIX_PATH_MAX];
280 char seq_unseen[STRING];
281 char seq_replied[STRING];
282 char seq_flagged[STRING];
285 struct mh_sequences mhs;
287 memset (&mhs, 0, sizeof (mhs));
289 snprintf (seq_unseen, sizeof (seq_unseen), "%s:", NONULL (MhUnseen));
290 snprintf (seq_replied, sizeof (seq_replied), "%s:", NONULL (MhReplied));
291 snprintf (seq_flagged, sizeof (seq_flagged), "%s:", NONULL (MhFlagged));
293 if (mh_mkstemp (ctx, &nfp, &tmpfname) != 0) {
298 snprintf (sequences, sizeof (sequences), "%s/.mh_sequences", ctx->path);
301 /* first, copy unknown sequences */
302 if ((ofp = fopen (sequences, "r"))) {
303 while ((buff = mutt_read_line (buff, &s, ofp, &l))) {
304 if (!safe_strncmp (buff, seq_unseen, safe_strlen (seq_unseen)))
306 if (!safe_strncmp (buff, seq_flagged, safe_strlen (seq_flagged)))
308 if (!safe_strncmp (buff, seq_replied, safe_strlen (seq_replied)))
311 fprintf (nfp, "%s\n", buff);
316 /* now, update our unseen, flagged, and replied sequences */
317 for (l = 0; l < ctx->msgcount; l++) {
318 if (ctx->hdrs[l]->deleted)
321 if ((p = strrchr (ctx->hdrs[l]->path, '/')))
324 p = ctx->hdrs[l]->path;
328 if (!ctx->hdrs[l]->read) {
329 mhs_set (&mhs, i, MH_SEQ_UNSEEN);
332 if (ctx->hdrs[l]->flagged) {
333 mhs_set (&mhs, i, MH_SEQ_FLAGGED);
336 if (ctx->hdrs[l]->replied) {
337 mhs_set (&mhs, i, MH_SEQ_REPLIED);
342 /* write out the new sequences */
344 mhs_write_one_sequence (nfp, &mhs, MH_SEQ_UNSEEN, NONULL (MhUnseen));
346 mhs_write_one_sequence (nfp, &mhs, MH_SEQ_FLAGGED, NONULL (MhFlagged));
348 mhs_write_one_sequence (nfp, &mhs, MH_SEQ_REPLIED, NONULL (MhReplied));
350 mhs_free_sequences (&mhs);
353 /* try to commit the changes - no guarantee here */
357 if (safe_rename (tmpfname, sequences) != 0) {
358 /* report an error? */
365 static void mh_sequences_add_one (CONTEXT * ctx, int n, short unseen,
366 short flagged, short replied)
368 short unseen_done = 0;
369 short flagged_done = 0;
370 short replied_done = 0;
372 FILE *ofp = NULL, *nfp = NULL;
375 char sequences[_POSIX_PATH_MAX];
377 char seq_unseen[STRING];
378 char seq_replied[STRING];
379 char seq_flagged[STRING];
385 if (mh_mkstemp (ctx, &nfp, &tmpfname) == -1)
388 snprintf (seq_unseen, sizeof (seq_unseen), "%s:", NONULL (MhUnseen));
389 snprintf (seq_replied, sizeof (seq_replied), "%s:", NONULL (MhReplied));
390 snprintf (seq_flagged, sizeof (seq_flagged), "%s:", NONULL (MhFlagged));
392 snprintf (sequences, sizeof (sequences), "%s/.mh_sequences", ctx->path);
393 if ((ofp = fopen (sequences, "r"))) {
394 while ((buff = mutt_read_line (buff, &sz, ofp, &line))) {
395 if (unseen && !strncmp (buff, seq_unseen, safe_strlen (seq_unseen))) {
396 fprintf (nfp, "%s %d\n", buff, n);
400 && !strncmp (buff, seq_flagged, safe_strlen (seq_flagged))) {
401 fprintf (nfp, "%s %d\n", buff, n);
405 && !strncmp (buff, seq_replied, safe_strlen (seq_replied))) {
406 fprintf (nfp, "%s %d\n", buff, n);
410 fprintf (nfp, "%s\n", buff);
416 if (!unseen_done && unseen)
417 fprintf (nfp, "%s: %d\n", NONULL (MhUnseen), n);
418 if (!flagged_done && flagged)
419 fprintf (nfp, "%s: %d\n", NONULL (MhFlagged), n);
420 if (!replied_done && replied)
421 fprintf (nfp, "%s: %d\n", NONULL (MhReplied), n);
426 if (safe_rename (tmpfname, sequences) != 0)
432 static void mh_update_maildir (struct maildir *md, struct mh_sequences *mhs)
438 for (; md; md = md->next) {
439 if ((p = strrchr (md->h->path, '/')))
445 f = mhs_check (mhs, i);
447 md->h->read = (f & MH_SEQ_UNSEEN) ? 0 : 1;
448 md->h->flagged = (f & MH_SEQ_FLAGGED) ? 1 : 0;
449 md->h->replied = (f & MH_SEQ_REPLIED) ? 1 : 0;
453 /* maildir support */
455 static void maildir_free_entry (struct maildir **md)
460 FREE (&(*md)->canon_fname);
462 mutt_free_header (&(*md)->h);
467 static void maildir_free_maildir (struct maildir **md)
469 struct maildir *p, *q;
474 for (p = *md; p; p = q) {
476 maildir_free_entry (&p);
480 static void maildir_parse_flags (HEADER * h, const char *path)
488 if ((p = strrchr (path, ':')) != NULL && safe_strncmp (p + 1, "2,", 2) == 0) {
491 str_replace (&h->maildir_flags, p);
492 q = h->maildir_flags;
506 case 'R': /* replied */
511 case 'T': /* trashed */
524 if (q == h->maildir_flags)
525 FREE (&h->maildir_flags);
530 static void maildir_update_mtime (CONTEXT * ctx)
532 char buf[_POSIX_PATH_MAX];
535 if (ctx->magic == M_MAILDIR) {
536 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, "cur");
537 if (stat (buf, &st) == 0)
538 ctx->mtime_cur = st.st_mtime;
539 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, "new");
542 snprintf (buf, sizeof (buf), "%s/.mh_sequences", ctx->path);
543 if (stat (buf, &st) == 0)
544 ctx->mtime_cur = st.st_mtime;
546 strfcpy (buf, ctx->path, sizeof (buf));
549 if (stat (buf, &st) == 0)
550 ctx->mtime = st.st_mtime;
554 * Actually parse a maildir message. This may also be used to fill
555 * out a fake header structure generated by lazy maildir parsing.
557 static HEADER *maildir_parse_message (int magic, const char *fname,
558 int is_old, HEADER * _h)
564 if ((f = fopen (fname, "r")) != NULL) {
566 h = mutt_new_header ();
567 h->env = mutt_read_rfc822_header (f, h, 0, 0);
569 fstat (fileno (f), &st);
573 h->received = h->date_sent;
575 if (h->content->length <= 0)
576 h->content->length = st.st_size - h->content->offset;
580 if (magic == M_MAILDIR) {
582 * maildir stores its flags in the filename, so ignore the
583 * flags in the header of the message
587 maildir_parse_flags (h, fname);
595 * Note that this routine will _not_ modify the context given by
598 * It's used in the first parsing pass on maildir and MH folders.
599 * In the MH case, this means full parsing of the folder. In the
600 * maildir case, it means that we only look at flags, and create a
601 * fake HEADER structure, which may later be filled in by
602 * maildir_parse_message(), when called from
603 * maildir_delayed_parsing().
607 static int maildir_parse_entry (CONTEXT * ctx, struct maildir ***last,
608 const char *subdir, const char *fname,
609 int *count, int is_old, ino_t inode)
611 struct maildir *entry;
613 char buf[_POSIX_PATH_MAX];
616 snprintf (buf, sizeof (buf), "%s/%s/%s", ctx->path, subdir, fname);
618 snprintf (buf, sizeof (buf), "%s/%s", ctx->path, fname);
620 if (ctx->magic == M_MH)
621 h = maildir_parse_message (ctx->magic, buf, is_old, NULL);
623 h = mutt_new_header ();
625 maildir_parse_flags (h, buf);
631 if (!ctx->quiet && ReadInc && ((*count % ReadInc) == 0 || *count == 1))
632 mutt_message (_("Reading %s... %d"), ctx->path, *count);
636 snprintf (buf, sizeof (buf), "%s/%s", subdir, fname);
637 h->path = safe_strdup (buf);
640 h->path = safe_strdup (fname);
642 entry = safe_calloc (sizeof (struct maildir), 1);
644 entry->header_parsed = (ctx->magic == M_MH);
646 entry->inode = inode;
647 #endif /* USE_INODESORT */
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 = (safe_strcmp ("cur", subdir) == 0);
686 strfcpy (buf, ctx->path, sizeof (buf));
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? */
700 (debugfile, "%s:%d: parsing %s\n", __FILE__, __LINE__,
702 maildir_parse_entry (ctx, last, subdir, de->d_name, count, is_old,
710 static int maildir_add_to_context (CONTEXT * ctx, struct maildir *md)
712 int oldmsgcount = ctx->msgcount;
716 dprint (2, (debugfile, "%s:%d maildir_add_to_context(): Considering %s\n",
717 __FILE__, __LINE__, NONULL (md->canon_fname)));
722 "%s:%d Adding header structure. Flags: %s%s%s%s%s\n", __FILE__,
723 __LINE__, md->h->flagged ? "f" : "", md->h->deleted ? "D" : "",
724 md->h->replied ? "r" : "", md->h->old ? "O" : "",
725 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);
759 * Merge two maildir lists according to the inode numbers.
761 static struct maildir *maildir_merge_inode (struct maildir *left,
762 struct maildir *right)
764 struct maildir *head;
765 struct maildir *tail;
768 if (left->inode < right->inode) {
786 while (left && right) {
787 if (left->inode < right->inode) {
809 * Sort maildir list according to inode.
811 static struct maildir *maildir_sort_inode (struct maildir *list)
813 struct maildir *left = list;
814 struct maildir *right = list;
816 if (!list || !list->next) {
821 while (list && list->next) {
823 list = list->next->next;
830 left = maildir_sort_inode (left);
831 right = maildir_sort_inode (right);
832 return maildir_merge_inode (left, right);
834 #endif /* USE_INODESORT */
837 static size_t maildir_hcache_keylen (const char *fn)
839 const char *p = strchr (fn, ':');
841 return p ? (size_t) (p - fn) : safe_strlen (fn);
846 * This function does the second parsing pass for a maildir-style
849 void maildir_delayed_parsing (CONTEXT * ctx, struct maildir *md)
852 char fn[_POSIX_PATH_MAX];
858 struct timeval *when = NULL;
859 struct stat lastchanged;
862 hc = mutt_hcache_open (HeaderCache, ctx->path);
865 for (p = md, count = 0; p; p = p->next, count++) {
866 if (!(p && p->h && !p->header_parsed))
870 data = mutt_hcache_fetch (hc, p->h->path + 3, &maildir_hcache_keylen);
871 when = (struct timeval *) data;
874 if (!ctx->quiet && ReadInc && ((count % ReadInc) == 0 || count == 1))
875 mutt_message (_("Reading %s... %d"), ctx->path, count);
876 snprintf (fn, sizeof (fn), "%s/%s", ctx->path, p->h->path);
879 if (option (OPTHCACHEVERIFY)) {
880 ret = stat (fn, &lastchanged);
883 lastchanged.st_mtime = 0;
887 if (data != NULL && !ret && lastchanged.st_mtime <= when->tv_sec) {
888 p->h = mutt_hcache_restore ((unsigned char *) data, &p->h);
889 maildir_parse_flags (p->h, fn);
893 if (maildir_parse_message (ctx->magic, fn, p->h->old, p->h)) {
894 p->header_parsed = 1;
895 maildir_parse_flags (p->h, fn);
897 mutt_hcache_store (hc, p->h->path + 3, p->h, 0, &maildir_hcache_keylen);
901 mutt_free_header (&p->h);
907 mutt_hcache_close (hc);
911 /* Read a MH/maildir style mailbox.
914 * ctx [IN/OUT] context for this mailbox
915 * subdir [IN] NULL for MH mailboxes, otherwise the subdir of the
916 * maildir mailbox to read from
918 static int _mh_read_dir (CONTEXT * ctx, const char *subdir)
921 struct mh_sequences mhs;
922 struct maildir **last;
926 memset (&mhs, 0, sizeof (mhs));
928 maildir_update_mtime (ctx);
933 if (maildir_parse_dir (ctx, &last, subdir, &count) == -1)
936 if (ctx->magic == M_MH) {
937 mh_read_sequences (&mhs, ctx->path);
938 mh_update_maildir (md, &mhs);
939 mhs_free_sequences (&mhs);
943 md = maildir_sort_inode (md);
944 #endif /* USE_INODESORT */
946 if (ctx->magic == M_MAILDIR)
947 maildir_delayed_parsing (ctx, md);
949 maildir_move_to_context (ctx, &md);
953 static int mh_read_dir (CONTEXT* ctx) {
954 return (_mh_read_dir (ctx, NULL));
957 /* read a maildir style mailbox */
958 static int maildir_read_dir (CONTEXT * ctx)
960 /* maildir looks sort of like MH, except that there are two subdirectories
961 * of the main folder path from which to read messages
963 if (_mh_read_dir (ctx, "new") == -1 || _mh_read_dir (ctx, "cur") == -1)
970 * Open a new (temporary) message in an MH folder.
973 int mh_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr)
975 return mh_mkstemp (dest, &msg->fp, &msg->path);
978 int ch_compar (const void *a, const void *b)
980 return (int) (*((const char *) a) - *((const char *) b));
983 static void maildir_flags (char *dest, size_t destlen, HEADER * hdr)
988 * The maildir specification requires that all files in the cur
989 * subdirectory have the :unique string appeneded, regardless of whether
990 * or not there are any flags. If .old is set, we know that this message
991 * will end up in the cur directory, so we include it in the following
992 * test even though there is no associated flag.
996 && (hdr->flagged || hdr->replied || hdr->read || hdr->deleted
997 || hdr->old || hdr->maildir_flags)) {
998 char tmp[LONG_STRING];
1000 snprintf (tmp, sizeof (tmp),
1002 hdr->flagged ? "F" : "",
1003 hdr->replied ? "R" : "",
1004 hdr->read ? "S" : "", hdr->deleted ? "T" : "",
1005 NONULL (hdr->maildir_flags));
1006 if (hdr->maildir_flags)
1007 qsort (tmp, safe_strlen (tmp), 1, ch_compar);
1008 snprintf (dest, destlen, ":2,%s", tmp);
1014 * Open a new (temporary) message in a maildir folder.
1016 * Note that this uses _almost_ the maildir file name format, but
1017 * with a {cur,new} prefix.
1021 int maildir_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr)
1024 char path[_POSIX_PATH_MAX];
1029 short deleted = hdr->deleted;
1033 maildir_flags (suffix, sizeof (suffix), hdr);
1035 hdr->deleted = deleted;
1040 if (hdr && (hdr->read || hdr->old))
1041 strfcpy (subdir, "cur", sizeof (subdir));
1043 strfcpy (subdir, "new", sizeof (subdir));
1046 snprintf (path, _POSIX_PATH_MAX, "%s/tmp/%s.%ld.%u_%d.%s%s",
1047 dest->path, subdir, (long) time (NULL),
1048 (unsigned int) getpid (), Counter++, NONULL (Hostname), suffix);
1050 dprint (2, (debugfile, "maildir_open_new_message (): Trying %s.\n",
1054 if ((fd = open (path, O_WRONLY | O_EXCL | O_CREAT, 0666)) == -1) {
1055 if (errno != EEXIST) {
1061 dprint (2, (debugfile, "maildir_open_new_message (): Success.\n"));
1062 msg->path = safe_strdup (path);
1067 if ((msg->fp = fdopen (fd, "w")) == NULL) {
1080 * Commit a message to a maildir folder.
1082 * msg->path contains the file name of a file in tmp/. We take the
1083 * flags from this file's name.
1085 * ctx is the mail folder we commit to.
1087 * hdr is a header structure to which we write the message's new
1088 * file name. This is used in the mh and maildir folder synch
1089 * routines. When this routine is invoked from mx_commit_message,
1092 * msg->path looks like this:
1094 * tmp/{cur,new}.mutt-HOSTNAME-PID-COUNTER:flags
1096 * See also maildir_open_new_message().
1100 int maildir_commit_message (CONTEXT * ctx, MESSAGE * msg, HEADER * hdr)
1104 char path[_POSIX_PATH_MAX];
1105 char full[_POSIX_PATH_MAX];
1108 if (safe_fclose (&msg->fp) != 0)
1111 /* extract the subdir */
1112 s = strrchr (msg->path, '/') + 1;
1113 strfcpy (subdir, s, 4);
1115 /* extract the flags */
1116 if ((s = strchr (s, ':')))
1117 strfcpy (suffix, s, sizeof (suffix));
1121 /* construct a new file name. */
1123 snprintf (path, _POSIX_PATH_MAX, "%s/%ld.%u_%d.%s%s", subdir,
1124 (long) time (NULL), (unsigned int) getpid (), Counter++,
1125 NONULL (Hostname), suffix);
1126 snprintf (full, _POSIX_PATH_MAX, "%s/%s", ctx->path, path);
1128 dprint (2, (debugfile, "maildir_commit_message (): renaming %s to %s.\n",
1131 if (safe_rename (msg->path, full) == 0) {
1133 str_replace (&hdr->path, path);
1137 * Adjust the mtime on the file to match the time at which this
1138 * message was received. Currently this is only set when copying
1139 * messages between mailboxes, so we test to ensure that it is
1142 if (msg->received) {
1145 ut.actime = msg->received;
1146 ut.modtime = msg->received;
1147 if (utime (full, &ut)) {
1149 ("maildir_commit_message(): unable to set time on file"));
1156 else if (errno != EEXIST) {
1157 mutt_perror (ctx->path);
1164 * commit a message to an MH folder.
1169 static int _mh_commit_message (CONTEXT * ctx, MESSAGE * msg, HEADER * hdr,
1175 unsigned int n, hi = 0;
1176 char path[_POSIX_PATH_MAX];
1179 if (safe_fclose (&msg->fp) != 0)
1182 if ((dirp = opendir (ctx->path)) == NULL) {
1183 mutt_perror (ctx->path);
1187 /* figure out what the next message number is */
1188 while ((de = readdir (dirp)) != NULL) {
1194 if (!isdigit ((unsigned char) *cp))
1207 * Now try to rename the file to the proper name.
1209 * Note: We may have to try multiple times, until we find a free
1215 snprintf (tmp, sizeof (tmp), "%d", hi);
1216 snprintf (path, sizeof (path), "%s/%s", ctx->path, tmp);
1217 if (safe_rename (msg->path, path) == 0) {
1219 str_replace (&hdr->path, tmp);
1223 else if (errno != EEXIST) {
1224 mutt_perror (ctx->path);
1229 mh_sequences_add_one (ctx, hi, !msg->flags.read, msg->flags.flagged,
1230 msg->flags.replied);
1234 int mh_commit_message (CONTEXT * ctx, MESSAGE * msg, HEADER * hdr)
1236 return _mh_commit_message (ctx, msg, hdr, 1);
1240 /* Sync a message in an MH folder.
1242 * This code is also used for attachment deletion in maildir
1246 static int mh_rewrite_message (CONTEXT * ctx, int msgno)
1248 HEADER *h = ctx->hdrs[msgno];
1253 char oldpath[_POSIX_PATH_MAX];
1254 char newpath[_POSIX_PATH_MAX];
1255 char partpath[_POSIX_PATH_MAX];
1257 long old_body_offset = h->content->offset;
1258 long old_body_length = h->content->length;
1259 long old_hdr_lines = h->lines;
1261 if ((dest = mx_open_new_message (ctx, h, 0)) == NULL)
1264 if ((rc = mutt_copy_message (dest->fp, ctx, h,
1265 M_CM_UPDATE, CH_UPDATE | CH_UPDATE_LEN)) == 0)
1267 snprintf (oldpath, _POSIX_PATH_MAX, "%s/%s", ctx->path, h->path);
1268 strfcpy (partpath, h->path, _POSIX_PATH_MAX);
1270 if (ctx->magic == M_MAILDIR)
1271 rc = maildir_commit_message (ctx, dest, h);
1273 rc = _mh_commit_message (ctx, dest, h, 0);
1275 mx_close_message (&dest);
1283 * Try to move the new message to the old place.
1286 * This is important when we are just updating flags.
1288 * Note that there is a race condition against programs which
1289 * use the first free slot instead of the maximum message
1290 * number. Mutt does _not_ behave like this.
1292 * Anyway, if this fails, the message is in the folder, so
1293 * all what happens is that a concurrently runnung mutt will
1294 * lose flag modifications.
1297 if (ctx->magic == M_MH && rc == 0) {
1298 snprintf (newpath, _POSIX_PATH_MAX, "%s/%s", ctx->path, h->path);
1299 if ((rc = safe_rename (newpath, oldpath)) == 0)
1300 str_replace (&h->path, partpath);
1304 mx_close_message (&dest);
1306 if (rc == -1 && restore) {
1307 h->content->offset = old_body_offset;
1308 h->content->length = old_body_length;
1309 h->lines = old_hdr_lines;
1312 mutt_free_body (&h->content->parts);
1316 static int mh_sync_message (CONTEXT * ctx, int msgno)
1318 HEADER *h = ctx->hdrs[msgno];
1320 if (h->attach_del || h->refs_changed || h->irt_changed)
1321 if (mh_rewrite_message (ctx, msgno) != 0)
1327 static int maildir_sync_message (CONTEXT * ctx, int msgno)
1329 HEADER *h = ctx->hdrs[msgno];
1331 if (h->attach_del || h->refs_changed || h->irt_changed) {
1332 /* when doing attachment deletion/rethreading, fall back to the MH case. */
1333 if (mh_rewrite_message (ctx, msgno) != 0)
1337 /* we just have to rename the file. */
1339 char newpath[_POSIX_PATH_MAX];
1340 char partpath[_POSIX_PATH_MAX];
1341 char fullpath[_POSIX_PATH_MAX];
1342 char oldpath[_POSIX_PATH_MAX];
1346 if ((p = strrchr (h->path, '/')) == NULL) {
1349 "maildir_sync_message: %s: unable to find subdir!\n",
1354 strfcpy (newpath, p, sizeof (newpath));
1356 /* kill the previous flags */
1357 if ((p = strchr (newpath, ':')) != NULL)
1360 maildir_flags (suffix, sizeof (suffix), h);
1362 snprintf (partpath, sizeof (partpath), "%s/%s%s",
1363 (h->read || h->old) ? "cur" : "new", newpath, suffix);
1364 snprintf (fullpath, sizeof (fullpath), "%s/%s", ctx->path, partpath);
1365 snprintf (oldpath, sizeof (oldpath), "%s/%s", ctx->path, h->path);
1367 if (safe_strcmp (fullpath, oldpath) == 0) {
1368 /* message hasn't really changed */
1372 /* record that the message is possibly marked as trashed on disk */
1373 h->trash = h->deleted;
1375 if (rename (oldpath, fullpath) != 0) {
1376 mutt_perror ("rename");
1379 str_replace (&h->path, partpath);
1384 static int mh_sync_mailbox (CONTEXT * ctx, int unused, int *index_hint)
1386 char path[_POSIX_PATH_MAX], tmp[_POSIX_PATH_MAX];
1391 #endif /* USE_HCACHE */
1393 if (ctx->magic == M_MH)
1394 i = mh_check_mailbox (ctx, index_hint);
1396 i = maildir_check_mailbox (ctx, index_hint);
1402 if (ctx->magic == M_MAILDIR)
1403 hc = mutt_hcache_open (HeaderCache, ctx->path);
1404 #endif /* USE_HCACHE */
1406 for (i = 0; i < ctx->msgcount; i++) {
1407 if (ctx->hdrs[i]->deleted
1408 && (ctx->magic != M_MAILDIR || !option (OPTMAILDIRTRASH))) {
1409 snprintf (path, sizeof (path), "%s/%s", ctx->path, ctx->hdrs[i]->path);
1410 if (ctx->magic == M_MAILDIR
1411 || (option (OPTMHPURGE) && ctx->magic == M_MH)) {
1413 if (ctx->magic == M_MAILDIR)
1414 mutt_hcache_delete (hc, ctx->hdrs[i]->path + 3,
1415 &maildir_hcache_keylen);
1416 #endif /* USE_HCACHE */
1419 else if (ctx->magic == M_MH) {
1420 /* MH just moves files out of the way when you delete them */
1421 if (*ctx->hdrs[i]->path != ',') {
1422 snprintf (tmp, sizeof (tmp), "%s/,%s", ctx->path,
1423 ctx->hdrs[i]->path);
1430 else if (ctx->hdrs[i]->changed || ctx->hdrs[i]->attach_del ||
1431 (ctx->magic == M_MAILDIR
1432 && (option (OPTMAILDIRTRASH) || ctx->hdrs[i]->trash)
1433 && (ctx->hdrs[i]->deleted != ctx->hdrs[i]->trash))) {
1434 if (ctx->magic == M_MAILDIR) {
1435 if (maildir_sync_message (ctx, i) == -1)
1439 if (mh_sync_message (ctx, i) == -1)
1446 if (ctx->magic == M_MAILDIR)
1447 mutt_hcache_close (hc);
1448 #endif /* USE_HCACHE */
1450 if (ctx->magic == M_MH)
1451 mh_update_sequences (ctx);
1453 /* XXX race condition? */
1455 maildir_update_mtime (ctx);
1457 /* adjust indices */
1460 for (i = 0, j = 0; i < ctx->msgcount; i++) {
1461 if (!ctx->hdrs[i]->deleted
1462 || (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))
1463 ctx->hdrs[i]->index = j++;
1471 if (ctx->magic == M_MAILDIR)
1472 mutt_hcache_close (hc);
1473 #endif /* USE_HCACHE */
1477 static char *maildir_canon_filename (char *dest, const char *src, size_t l)
1481 if ((t = strrchr (src, '/')))
1484 strfcpy (dest, src, l);
1485 if ((u = strrchr (dest, ':')))
1491 static void maildir_update_tables (CONTEXT * ctx, int *index_hint)
1497 if (Sort != SORT_ORDER) {
1500 mutt_sort_headers (ctx, 1);
1504 old_count = ctx->msgcount;
1505 for (i = 0, j = 0; i < old_count; i++) {
1506 if (ctx->hdrs[i]->active && index_hint && *index_hint == i)
1509 if (ctx->hdrs[i]->active)
1510 ctx->hdrs[i]->index = j++;
1513 mx_update_tables (ctx, 0);
1514 mutt_clear_threads (ctx);
1517 static void maildir_update_flags (CONTEXT * ctx, HEADER * o, HEADER * n)
1519 /* save the global state here so we can reset it at the
1520 * end of list block if required.
1522 int context_changed = ctx->changed;
1524 /* user didn't modify this message. alter the flags to
1525 * match the current state on disk. This may not actually
1526 * do anything, but we can't tell right now. mutt_set_flag()
1527 * will just ignore the call if the status bits are
1528 * already properly set.
1530 mutt_set_flag (ctx, o, M_FLAG, n->flagged);
1531 mutt_set_flag (ctx, o, M_REPLIED, n->replied);
1532 mutt_set_flag (ctx, o, M_READ, n->read);
1533 mutt_set_flag (ctx, o, M_OLD, n->old);
1535 /* mutt_set_flag() will set this, but we don't need to
1536 * sync the changes we made because we just updated the
1537 * context to match the current on-disk state of the
1542 /* if the mailbox was not modified before we made these
1543 * changes, unset the changed flag since nothing needs to
1546 if (!context_changed)
1551 /* This function handles arrival of new mail and reopening of
1552 * maildir folders. The basic idea here is we check to see if either
1553 * the new or cur subdirectories have changed, and if so, we scan them
1554 * for the list of files. We check for newly added messages, and
1555 * then merge the flags messages we already knew about. We don't treat
1556 * either subdirectory differently, as mail could be copied directly into
1557 * the cur directory from another agent.
1559 int maildir_check_mailbox (CONTEXT * ctx, int *index_hint)
1561 struct stat st_new; /* status of the "new" subdirectory */
1562 struct stat st_cur; /* status of the "cur" subdirectory */
1563 char buf[_POSIX_PATH_MAX];
1564 int changed = 0; /* bitmask representing which subdirectories
1565 have changed. 0x1 = new, 0x2 = cur */
1566 int occult = 0; /* messages were removed from the mailbox */
1567 int have_new = 0; /* messages were added to the mailbox */
1568 struct maildir *md; /* list of messages in the mailbox */
1569 struct maildir **last, *p;
1571 HASH *fnames; /* hash table for quickly looking up the base filename
1572 for a maildir message */
1574 /* XXX seems like this check belongs in mx_check_mailbox()
1577 if (!option (OPTCHECKNEW))
1580 snprintf (buf, sizeof (buf), "%s/new", ctx->path);
1581 if (stat (buf, &st_new) == -1)
1584 snprintf (buf, sizeof (buf), "%s/cur", ctx->path);
1585 if (stat (buf, &st_cur) == -1)
1588 /* determine which subdirectories need to be scanned */
1589 if (st_new.st_mtime > ctx->mtime)
1591 if (st_cur.st_mtime > ctx->mtime_cur)
1595 return 0; /* nothing to do */
1597 /* update the modification times on the mailbox */
1598 ctx->mtime_cur = st_cur.st_mtime;
1599 ctx->mtime = st_new.st_mtime;
1601 /* do a fast scan of just the filenames in
1602 * the subdirectories that have changed.
1607 maildir_parse_dir (ctx, &last, "new", NULL);
1609 maildir_parse_dir (ctx, &last, "cur", NULL);
1611 /* we create a hash table keyed off the canonical (sans flags) filename
1612 * of each message we scanned. This is used in the loop over the
1613 * existing messages below to do some correlation.
1615 fnames = hash_create (1031);
1617 for (p = md; p; p = p->next) {
1618 maildir_canon_filename (buf, p->h->path, sizeof (buf));
1619 p->canon_fname = safe_strdup (buf);
1620 hash_insert (fnames, p->canon_fname, p, 0);
1623 /* check for modifications and adjust flags */
1624 for (i = 0; i < ctx->msgcount; i++) {
1625 ctx->hdrs[i]->active = 0;
1626 maildir_canon_filename (buf, ctx->hdrs[i]->path, sizeof (buf));
1627 p = hash_find (fnames, buf);
1629 /* message already exists, merge flags */
1630 ctx->hdrs[i]->active = 1;
1632 /* check to see if the message has moved to a different
1633 * subdirectory. If so, update the associated filename.
1635 if (safe_strcmp (ctx->hdrs[i]->path, p->h->path))
1636 str_replace (&ctx->hdrs[i]->path, p->h->path);
1638 /* if the user hasn't modified the flags on this message, update
1639 * the flags we just detected.
1641 if (!ctx->hdrs[i]->changed)
1642 maildir_update_flags (ctx, ctx->hdrs[i], p->h);
1644 if (ctx->hdrs[i]->deleted == ctx->hdrs[i]->trash)
1645 ctx->hdrs[i]->deleted = p->h->deleted;
1646 ctx->hdrs[i]->trash = p->h->trash;
1648 /* this is a duplicate of an existing header, so remove it */
1649 mutt_free_header (&p->h);
1651 /* This message was not in the list of messages we just scanned.
1652 * Check to see if we have enough information to know if the
1653 * message has disappeared out from underneath us.
1655 else if (((changed & 1) && (!strncmp (ctx->hdrs[i]->path, "new/", 4))) ||
1656 ((changed & 2) && (!strncmp (ctx->hdrs[i]->path, "cur/", 4)))) {
1657 /* This message disappeared, so we need to simulate a "reopen"
1658 * event. We know it disappeared because we just scanned the
1659 * subdirectory it used to reside in.
1664 /* This message resides in a subdirectory which was not
1665 * modified, so we assume that it is still present and
1668 ctx->hdrs[i]->active = 1;
1672 /* destroy the file name hash */
1673 hash_destroy (&fnames, NULL);
1675 /* If we didn't just get new mail, update the tables. */
1677 maildir_update_tables (ctx, index_hint);
1679 /* do any delayed parsing we need to do. */
1680 maildir_delayed_parsing (ctx, md);
1682 /* Incorporate new messages */
1683 have_new = maildir_move_to_context (ctx, &md);
1685 return occult ? M_REOPENED : (have_new ? M_NEW_MAIL : 0);
1689 * This function handles arrival of new mail and reopening of
1690 * mh/maildir folders. Things are getting rather complex because we
1691 * don't have a well-defined "mailbox order", so the tricks from
1692 * mbox.c and mx.c won't work here.
1694 * Don't change this code unless you _really_ understand what
1699 int mh_check_mailbox (CONTEXT * ctx, int *index_hint)
1701 char buf[_POSIX_PATH_MAX];
1702 struct stat st, st_cur;
1703 short modified = 0, have_new = 0, occult = 0;
1704 struct maildir *md, *p;
1705 struct maildir **last = NULL;
1706 struct mh_sequences mhs;
1710 if (!option (OPTCHECKNEW))
1713 strfcpy (buf, ctx->path, sizeof (buf));
1714 if (stat (buf, &st) == -1)
1717 /* create .mh_sequences when there isn't one. */
1718 snprintf (buf, sizeof (buf), "%s/.mh_sequences", ctx->path);
1719 if ((i = stat (buf, &st_cur) == -1) && errno == ENOENT) {
1723 if (mh_mkstemp (ctx, &fp, &tmp) == 0) {
1725 if (safe_rename (tmp, buf) == -1)
1731 if (i == -1 && stat (buf, &st_cur) == -1)
1734 if (st.st_mtime > ctx->mtime || st_cur.st_mtime > ctx->mtime_cur)
1740 ctx->mtime_cur = st_cur.st_mtime;
1741 ctx->mtime = st.st_mtime;
1743 memset (&mhs, 0, sizeof (mhs));
1747 maildir_parse_dir (ctx, &last, NULL, NULL);
1748 mh_read_sequences (&mhs, ctx->path);
1749 mh_update_maildir (md, &mhs);
1750 mhs_free_sequences (&mhs);
1752 /* check for modifications and adjust flags */
1753 fnames = hash_create (1031);
1755 for (p = md; p; p = p->next)
1756 hash_insert (fnames, p->h->path, p, 0);
1758 for (i = 0; i < ctx->msgcount; i++) {
1759 ctx->hdrs[i]->active = 0;
1761 if ((p = hash_find (fnames, ctx->hdrs[i]->path)) && p->h &&
1762 (mbox_strict_cmp_headers (ctx->hdrs[i], p->h))) {
1763 ctx->hdrs[i]->active = 1;
1764 /* found the right message */
1765 if (!ctx->hdrs[i]->changed)
1766 maildir_update_flags (ctx, ctx->hdrs[i], p->h);
1768 mutt_free_header (&p->h);
1770 else /* message has disappeared */
1774 /* destroy the file name hash */
1776 hash_destroy (&fnames, NULL);
1778 /* If we didn't just get new mail, update the tables. */
1780 maildir_update_tables (ctx, index_hint);
1782 /* Incorporate new messages */
1783 have_new = maildir_move_to_context (ctx, &md);
1785 return occult ? M_REOPENED : (have_new ? M_NEW_MAIL : 0);
1792 * These functions try to find a message in a maildir folder when it
1793 * has moved under our feet. Note that this code is rather expensive, but
1794 * then again, it's called rarely.
1797 FILE *_maildir_open_find_message (const char *folder, const char *unique,
1798 const char *subfolder)
1800 char dir[_POSIX_PATH_MAX];
1801 char tunique[_POSIX_PATH_MAX];
1802 char fname[_POSIX_PATH_MAX];
1810 snprintf (dir, sizeof (dir), "%s/%s", folder, subfolder);
1812 if ((dp = opendir (dir)) == NULL) {
1817 while ((de = readdir (dp))) {
1818 maildir_canon_filename (tunique, de->d_name, sizeof (tunique));
1820 if (!safe_strcmp (tunique, unique)) {
1821 snprintf (fname, sizeof (fname), "%s/%s/%s", folder, subfolder,
1823 fp = fopen (fname, "r"); /* __FOPEN_CHECKED__ */
1835 FILE *maildir_open_find_message (const char *folder, const char *msg)
1837 char unique[_POSIX_PATH_MAX];
1840 static unsigned int new_hits = 0, cur_hits = 0; /* simple dynamic optimization */
1842 maildir_canon_filename (unique, msg, sizeof (unique));
1845 _maildir_open_find_message (folder, unique,
1846 new_hits > cur_hits ? "new" : "cur"))
1847 || errno != ENOENT) {
1848 if (new_hits < UINT_MAX && cur_hits < UINT_MAX) {
1849 new_hits += (new_hits > cur_hits ? 1 : 0);
1850 cur_hits += (new_hits > cur_hits ? 0 : 1);
1856 _maildir_open_find_message (folder, unique,
1857 new_hits > cur_hits ? "cur" : "new"))
1858 || errno != ENOENT) {
1859 if (new_hits < UINT_MAX && cur_hits < UINT_MAX) {
1860 new_hits += (new_hits > cur_hits ? 0 : 1);
1861 cur_hits += (new_hits > cur_hits ? 1 : 0);
1873 * 1 if there are no messages in the mailbox
1874 * 0 if there are messages in the mailbox
1877 static int maildir_check_empty (const char *path)
1881 int r = 1; /* assume empty until we find a message */
1882 char realpath[_POSIX_PATH_MAX];
1885 /* Strategy here is to look for any file not beginning with a period */
1888 /* we do "cur" on the first iteration since its more likely that we'll
1889 * find old messages without having to scan both subdirs
1891 snprintf (realpath, sizeof (realpath), "%s/%s", path,
1892 iter == 0 ? "cur" : "new");
1893 if ((dp = opendir (realpath)) == NULL)
1895 while ((de = readdir (dp))) {
1896 if (*de->d_name != '.') {
1903 } while (r && iter < 2);
1910 * 1 if there are no messages in the mailbox
1911 * 0 if there are messages in the mailbox
1914 int mh_check_empty (const char *path)
1918 int r = 1; /* assume empty until we find a message */
1920 if ((dp = opendir (path)) == NULL)
1922 while ((de = readdir (dp))) {
1923 if (mh_valid_message (de->d_name)) {
1933 static int mh_is_magic (const char* path, struct stat* st) {
1934 char tmp[_POSIX_PATH_MAX];
1936 if (S_ISDIR (st->st_mode)) {
1937 snprintf (tmp, sizeof (tmp), "%s/.mh_sequences", path);
1938 if (access (tmp, F_OK) == 0)
1941 snprintf (tmp, sizeof (tmp), "%s/.xmhcache", path);
1942 if (access (tmp, F_OK) == 0)
1945 snprintf (tmp, sizeof (tmp), "%s/.mew_cache", path);
1946 if (access (tmp, F_OK) == 0)
1949 snprintf (tmp, sizeof (tmp), "%s/.mew-cache", path);
1950 if (access (tmp, F_OK) == 0)
1953 snprintf (tmp, sizeof (tmp), "%s/.sylpheed_cache", path);
1954 if (access (tmp, F_OK) == 0)
1958 * ok, this isn't an mh folder, but mh mode can be used to read
1959 * Usenet news from the spool. ;-)
1962 snprintf (tmp, sizeof (tmp), "%s/.overview", path);
1963 if (access (tmp, F_OK) == 0)
1969 static int maildir_is_magic (const char* path, struct stat* st) {
1971 char tmp[_POSIX_PATH_MAX];
1973 if (S_ISDIR (st->st_mode)) {
1974 snprintf (tmp, sizeof (tmp), "%s/cur", path);
1975 if (stat (tmp, &sb) == 0 && S_ISDIR (sb.st_mode))
1981 /* routines common to maildir and mh */
1982 static mx_t* reg_mx (void) {
1983 mx_t* fmt = safe_calloc (1, sizeof (mx_t));
1985 fmt->mx_access = access;
1986 fmt->mx_sync_mailbox = mh_sync_mailbox;
1990 mx_t* mh_reg_mx (void) {
1991 mx_t* fmt = reg_mx ();
1993 fmt->mx_check_empty = mh_check_empty;
1994 fmt->mx_is_magic = mh_is_magic;
1995 fmt->mx_open_mailbox = mh_read_dir;
1999 mx_t* maildir_reg_mx (void) {
2000 mx_t* fmt = reg_mx ();
2001 fmt->type = M_MAILDIR;
2002 fmt->mx_check_empty = maildir_check_empty;
2003 fmt->mx_is_magic = maildir_is_magic;
2004 fmt->mx_open_mailbox = maildir_read_dir;