2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2002 Michael R. Elkins <me@mutt.org>
4 * Copyright (C) 1999-2000 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.
11 #include <lib-lib/lib-lib.h>
15 #include <lib-sys/unix.h>
16 #include <lib-mime/mime.h>
17 #include <lib-ui/sidebar.h>
30 #include <imap/imap.h>
31 #include <imap/mx_imap.h>
34 #include <pop/mx_pop.h>
37 #include "nntp/nntp.h"
38 #include "nntp/mx_nntp.h"
45 #include <lib-crypt/crypt.h>
49 static mx_t const *mxfmts[] = {
62 #define MX_IDX(idx) (idx >= 0 && idx < countof(mxfmts))
63 #define mutt_is_spool(s) (m_strcmp(Spoolfile, s) == 0)
68 * retry - should retry if unable to lock?
71 static int invoke_dotlock (const char *path, int flags, int retry)
73 char cmd[LONG_STRING + _POSIX_PATH_MAX];
74 char f[SHORT_STRING + _POSIX_PATH_MAX];
77 if (flags & DL_FL_RETRY)
78 snprintf (r, sizeof (r), "-r %d ", retry ? MAXLOCKATTEMPT : 0);
80 mutt_quote_filename (f, sizeof (f), path);
82 snprintf (cmd, sizeof (cmd),
85 flags & DL_FL_TRY ? "-t " : "",
86 flags & DL_FL_UNLOCK ? "-u " : "",
87 flags & DL_FL_USEPRIV ? "-p " : "",
88 flags & DL_FL_FORCE ? "-f " : "",
89 flags & DL_FL_UNLINK ? "-d " : "",
90 flags & DL_FL_RETRY ? r : "", f);
92 return mutt_system (cmd);
95 static int dotlock_file (const char *path, int retry)
98 int flags = DL_FL_USEPRIV | DL_FL_RETRY;
104 if ((r = invoke_dotlock (path, flags, retry)) == DL_EX_EXIST) {
105 if (!option (OPTNOCURSES)) {
106 char msg[LONG_STRING];
108 snprintf (msg, sizeof (msg),
109 _("Lock count exceeded, remove lock for %s?"), path);
110 if (retry && mutt_yesorno (msg, M_YES) == M_YES) {
111 flags |= DL_FL_FORCE;
118 mutt_error (_("Can't dotlock %s.\n"), path);
121 return (r == DL_EX_OK ? 0 : -1);
124 static int undotlock_file (const char *path)
126 return (invoke_dotlock (path, DL_FL_USEPRIV | DL_FL_UNLOCK, 0) ==
130 #endif /* USE_DOTLOCK */
132 /* looks up index of type for path in mxfmts */
133 static int mx_get_idx (const char* path) {
137 /* first, test all non-local folders to avoid stat() call */
138 for (i = 0; i < countof(mxfmts); i++) {
139 if (!mxfmts[i]->local)
140 t = mxfmts[i]->mx_is_magic(path, NULL);
144 if (stat (path, &st) == 0) {
145 /* if stat() succeeded, keep testing until success and
146 * pass stat() info so that we only need to do it once */
147 for (i = 0; i < countof(mxfmts); i++) {
148 if (mxfmts[i]->local)
149 t = mxfmts[i]->mx_is_magic(path, &st);
158 * excl if excl != 0, request an exclusive lock
159 * dot if dot != 0, try to dotlock the file
160 * time_out should retry locking?
162 int mx_lock_file (const char *path, int fd, int excl, int dot, int time_out)
164 #if defined (USE_FCNTL) || defined (USE_FLOCK)
176 lck.l_type = excl ? F_WRLCK : F_RDLCK;
177 lck.l_whence = SEEK_SET;
182 while (fcntl (fd, F_SETLK, &lck) == -1) {
185 if (errno != EAGAIN && errno != EACCES) {
186 mutt_perror ("fcntl");
190 if (fstat (fd, &sb) != 0)
196 /* only unlock file if it is unchanged */
197 if (prev_sb.st_size == sb.st_size
198 && ++count >= (time_out ? MAXLOCKATTEMPT : 0)) {
200 mutt_error _("Timeout exceeded while attempting fcntl lock!");
207 mutt_message (_("Waiting for fcntl lock... %d"), ++attempt);
210 #endif /* USE_FCNTL */
215 while (flock (fd, (excl ? LOCK_EX : LOCK_SH) | LOCK_NB) == -1) {
218 if (errno != EWOULDBLOCK) {
219 mutt_perror ("flock");
224 if (fstat (fd, &sb) != 0)
230 /* only unlock file if it is unchanged */
231 if (prev_sb.st_size == sb.st_size
232 && ++count >= (time_out ? MAXLOCKATTEMPT : 0)) {
234 mutt_error _("Timeout exceeded while attempting flock lock!");
242 mutt_message (_("Waiting for flock attempt... %d"), ++attempt);
245 #endif /* USE_FLOCK */
249 r = dotlock_file (path, time_out);
250 #endif /* USE_DOTLOCK */
253 /* release any other locks obtained in this routine */
256 lck.l_type = F_UNLCK;
257 fcntl (fd, F_SETLK, &lck);
258 #endif /* USE_FCNTL */
262 #endif /* USE_FLOCK */
270 int mx_unlock_file (const char *path, int fd, int dot)
273 struct flock unlockit;
275 p_clear(&unlockit, 1);
276 unlockit.l_type = F_UNLCK;
277 unlockit.l_whence = SEEK_SET;
278 fcntl (fd, F_SETLK, &unlockit);
287 undotlock_file (path);
293 static void mx_unlink_empty (const char *path)
301 if ((fd = open (path, O_RDWR)) == -1)
304 if (mx_lock_file (path, fd, 1, 0, 1) == -1) {
310 invoke_dotlock (path, DL_FL_UNLINK, 1);
312 if (fstat (fd, &sb) == 0 && sb.st_size == 0)
316 mx_unlock_file (path, fd, 0);
320 /* try to figure out what type of mailbox ``path'' is */
321 int mx_get_magic (const char *path) {
324 if (m_strlen(path) == 0)
326 if ((i = mx_get_idx (path)) >= 0)
327 return (mxfmts[i]->type);
331 int mx_is_local (int m) {
334 return (mxfmts[m]->local);
338 * set DefaultMagic to the given value
340 int mx_set_magic (const char *s)
342 if (ascii_strcasecmp (s, "mbox") == 0)
343 DefaultMagic = M_MBOX;
344 else if (ascii_strcasecmp (s, "mmdf") == 0)
345 DefaultMagic = M_MMDF;
346 else if (ascii_strcasecmp (s, "mh") == 0)
348 else if (ascii_strcasecmp (s, "maildir") == 0)
349 DefaultMagic = M_MAILDIR;
356 /* mx_access: Wrapper for access, checks permissions on a given mailbox.
357 * We may be interested in using ACL-style flags at some point, currently
358 * we use the normal access() flags. */
359 int mx_access (const char *path, int flags)
363 if ((i = mx_get_idx (path)) >= 0 && mxfmts[i]->mx_access)
364 return (mxfmts[i]->mx_access(path,flags));
368 static int mx_open_mailbox_append (CONTEXT * ctx, int flags)
372 /* special case for appending to compressed folders -
373 * even if we can not open them for reading */
374 if (mutt_can_append_compressed (ctx->path))
375 mutt_open_append_compressed (ctx);
379 if (mx_get_magic (ctx->path) == M_IMAP)
380 return imap_open_mailbox_append (ctx);
382 if (stat (ctx->path, &sb) == 0) {
383 ctx->magic = mx_get_magic (ctx->path);
385 switch (ctx->magic) {
387 mutt_error (_("%s is not a mailbox."), ctx->path);
393 else if (errno == ENOENT) {
394 ctx->magic = DefaultMagic;
396 if (ctx->magic == M_MH || ctx->magic == M_MAILDIR) {
397 char tmp[_POSIX_PATH_MAX];
399 if (mkdir (ctx->path, S_IRWXU)) {
400 mutt_perror (ctx->path);
404 if (ctx->magic == M_MAILDIR) {
405 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
406 if (mkdir (tmp, S_IRWXU)) {
412 snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
413 if (mkdir (tmp, S_IRWXU)) {
415 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
420 snprintf (tmp, sizeof (tmp), "%s/tmp", ctx->path);
421 if (mkdir (tmp, S_IRWXU)) {
423 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
425 snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
434 snprintf (tmp, sizeof (tmp), "%s/.mh_sequences", ctx->path);
435 if ((i = creat (tmp, S_IRWXU)) == -1) {
445 mutt_perror (ctx->path);
449 switch (ctx->magic) {
453 safe_fopen (ctx->path, flags & M_NEWFOLDER ? "w" : "a")) == NULL
454 || mbox_lock_mailbox (ctx, 1, 1) != 0) {
456 mutt_perror (ctx->path);
458 mutt_error (_("Couldn't lock %s\n"), ctx->path);
459 safe_fclose (&ctx->fp);
463 fseeko (ctx->fp, 0, 2);
479 * open a mailbox and parse it
482 * flags M_NOSORT do not sort mailbox
483 * M_APPEND open mailbox for appending
484 * M_READONLY open mailbox in read-only mode
485 * M_QUIET only print error messages
486 * ctx if non-null, context struct to use
488 CONTEXT *mx_open_mailbox (const char *path, int flags, CONTEXT * pctx)
494 ctx = p_new(CONTEXT, 1);
496 ctx->path = m_strdup(path);
498 ctx->msgnotreadyet = -1;
503 if (flags & M_READONLY)
508 if (flags & (M_APPEND | M_NEWFOLDER)) {
509 if (mx_open_mailbox_append (ctx, flags) != 0) {
510 mx_fastclose_mailbox (ctx);
518 if (!MX_IDX(ctx->magic-1))
519 ctx->magic = mx_get_magic (path);
521 if (ctx->magic == M_COMPRESSED)
522 mutt_open_read_compressed (ctx);
525 mutt_error (_("%s is not a mailbox."), path);
527 if (ctx->magic == -1)
530 if (ctx->magic <= 0) {
531 mx_fastclose_mailbox (ctx);
537 /* if the user has a `push' command in their .muttrc, or in a folder-hook,
538 * it will cause the progress messages not to be displayed because
539 * mutt_refresh() will think we are in the middle of a macro. so set a
540 * flag to indicate that we should really refresh the screen.
542 set_option (OPTFORCEREFRESH);
545 mutt_message (_("Reading %s..."), ctx->path);
547 rc = mxfmts[ctx->magic-1]->mx_open_mailbox(ctx);
550 if ((flags & M_NOSORT) == 0) {
551 /* avoid unnecessary work since the mailbox is completely unthreaded
553 unset_option (OPTSORTSUBTHREADS);
554 unset_option (OPTNEEDRESCORE);
555 mutt_sort_headers (ctx, 1);
561 mx_fastclose_mailbox (ctx);
566 unset_option (OPTFORCEREFRESH);
570 /* free up memory associated with the mailbox context */
571 void mx_fastclose_mailbox (CONTEXT * ctx)
578 if (MX_IDX(ctx->magic-1) && mxfmts[ctx->magic-1]->mx_fastclose_mailbox)
579 mxfmts[ctx->magic-1]->mx_fastclose_mailbox(ctx);
581 hash_destroy (&ctx->subj_hash, NULL);
583 hash_destroy (&ctx->id_hash, NULL);
584 mutt_clear_threads (ctx);
585 for (i = 0; i < ctx->msgcount; i++)
586 header_delete(&ctx->hdrs[i]);
587 p_delete(&ctx->hdrs);
590 if (ctx->compressinfo)
591 mutt_fast_close_compressed (ctx);
593 p_delete(&ctx->path);
594 p_delete(&ctx->pattern);
595 if (ctx->limit_pattern)
596 mutt_pattern_free (&ctx->limit_pattern);
597 safe_fclose (&ctx->fp);
601 /* save changes to disk */
602 static int sync_mailbox (CONTEXT * ctx, int *index_hint)
607 mutt_message (_("Writing %s..."), ctx->path);
609 if (MX_IDX(ctx->magic-1))
610 /* the 1 is only of interest for IMAP and means EXPUNGE */
611 rc = mxfmts[ctx->magic-1]->mx_sync_mailbox(ctx,1,index_hint);
613 if (rc == 0 && ctx->compressinfo)
614 return mutt_sync_compressed (ctx);
619 /* move deleted mails to the trash folder */
620 static int trash_append (CONTEXT * ctx)
626 if (!TrashPath || !ctx->deleted ||
627 (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))
630 for (; i < ctx->msgcount && (!ctx->hdrs[i]->deleted ||
631 ctx->hdrs[i]->appended); i++);
632 if (i == ctx->msgcount)
633 return 0; /* nothing to be done */
635 if (mutt_save_confirm (TrashPath, &st) != 0) {
636 mutt_error _("message(s) not deleted");
641 if (lstat (ctx->path, &stc) == 0 && stc.st_ino == st.st_ino
642 && stc.st_dev == st.st_dev && stc.st_rdev == st.st_rdev)
643 return 0; /* we are in the trash folder: simple sync */
645 if ((ctx_trash = mx_open_mailbox (TrashPath, M_APPEND, NULL)) != NULL) {
646 for (i = 0; i < ctx->msgcount; i++)
647 if (ctx->hdrs[i]->deleted && !ctx->hdrs[i]->appended
648 && !ctx->hdrs[i]->purged
649 && mutt_append_message (ctx_trash, ctx, ctx->hdrs[i], 0, 0) == -1) {
650 mx_close_mailbox (ctx_trash, NULL);
654 mx_close_mailbox (ctx_trash, NULL);
657 mutt_error _("Can't open trash folder");
665 /* save changes and close mailbox */
666 static int _mx_close_mailbox (CONTEXT * ctx, int *index_hint)
668 int i, move_messages = 0, purge = 1, read_msgs = 0;
672 char mbox[_POSIX_PATH_MAX];
673 char buf[SHORT_STRING];
681 if (ctx->magic == M_NNTP) {
684 ret = nntp_close_mailbox (ctx);
685 mx_fastclose_mailbox (ctx);
689 if (ctx->readonly || ctx->dontwrite) {
690 /* mailbox is readonly or we don't want to write */
691 mx_fastclose_mailbox (ctx);
696 /* mailbox was opened in write-mode */
697 if (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
698 mbox_close_mailbox (ctx);
700 mx_fastclose_mailbox (ctx);
704 for (i = 0; i < ctx->msgcount; i++) {
705 if (!ctx->hdrs[i]->deleted && ctx->hdrs[i]->read
706 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
710 if (read_msgs && quadoption (OPT_MOVE) != M_NO) {
713 if ((p = mutt_find_hook (M_MBOXHOOK, ctx->path))) {
715 m_strcpy(mbox, sizeof(mbox), p);
718 m_strcpy(mbox, sizeof(mbox), NONULL(Inbox));
719 isSpool = mutt_is_spool (ctx->path) && !mutt_is_spool (mbox);
721 mutt_expand_path (mbox, sizeof (mbox));
724 snprintf (buf, sizeof (buf), _("Move read messages to %s?"), mbox);
725 if ((move_messages = query_quadoption (OPT_MOVE, buf)) == -1) {
733 * There is no point in asking whether or not to purge if we are
734 * just marking messages as "trash".
736 if (ctx->deleted && !(ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH))) {
737 snprintf (buf, sizeof (buf), ctx->deleted == 1
738 ? _("Purge %d deleted message?") :
739 _("Purge %d deleted messages?"), ctx->deleted);
740 if ((purge = query_quadoption (OPT_DELETE, buf)) < 0) {
746 /* IMAP servers manage the OLD flag themselves */
747 if (ctx->magic != M_IMAP)
748 if (option (OPTMARKOLD)) {
749 for (i = 0; i < ctx->msgcount; i++) {
750 if (!ctx->hdrs[i]->deleted && !ctx->hdrs[i]->old)
751 mutt_set_flag (ctx, ctx->hdrs[i], M_OLD, 1);
756 mutt_message (_("Moving read messages to %s..."), mbox);
758 /* try to use server-side copy first */
761 if (ctx->magic == M_IMAP && imap_is_magic (mbox, NULL) == M_IMAP) {
762 /* tag messages for moving, and clear old tags, if any */
763 for (i = 0; i < ctx->msgcount; i++)
764 if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted
765 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
766 ctx->hdrs[i]->tagged = 1;
768 ctx->hdrs[i]->tagged = 0;
770 i = imap_copy_messages (ctx, NULL, mbox, 1);
773 if (i == 0) /* success */
775 else if (i == -1) { /* horrible error, bail */
779 else /* use regular append-copy mode */
781 if (mx_open_mailbox (mbox, M_APPEND, &f) == NULL) {
786 for (i = 0; i < ctx->msgcount; i++) {
787 if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted
788 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED))) {
789 if (mutt_append_message (&f, ctx, ctx->hdrs[i], 0, CH_UPDATE_LEN) ==
791 mutt_set_flag (ctx, ctx->hdrs[i], M_DELETE, 1);
792 mutt_set_flag (ctx, ctx->hdrs[i], M_APPENDED, 1);
795 mx_close_mailbox (&f, NULL);
802 mx_close_mailbox (&f, NULL);
806 else if (!ctx->changed && ctx->deleted == 0) {
807 mutt_message _("Mailbox is unchanged.");
809 mx_fastclose_mailbox (ctx);
813 /* copy mails to the trash before expunging */
814 if (purge && ctx->deleted)
815 if (trash_append (ctx) != 0) {
820 /* allow IMAP to preserve the deleted flag across sessions */
821 if (ctx->magic == M_IMAP) {
822 if ((check = imap_sync_mailbox (ctx, purge, index_hint)) != 0) {
830 for (i = 0; i < ctx->msgcount; i++)
831 ctx->hdrs[i]->deleted = 0;
835 if (ctx->changed || ctx->deleted) {
836 if ((check = sync_mailbox (ctx, index_hint)) != 0) {
844 mutt_message (_("%d kept, %d moved, %d deleted."),
845 ctx->msgcount - ctx->deleted, read_msgs, ctx->deleted);
847 mutt_message (_("%d kept, %d deleted."),
848 ctx->msgcount - ctx->deleted, ctx->deleted);
850 if (ctx->msgcount == ctx->deleted &&
851 (ctx->magic == M_MMDF || ctx->magic == M_MBOX) &&
852 !mutt_is_spool (ctx->path) && !option (OPTSAVEEMPTY))
853 mx_unlink_empty (ctx->path);
855 if (ctx->compressinfo && mutt_slow_close_compressed (ctx))
858 mx_fastclose_mailbox (ctx);
863 int mx_close_mailbox (CONTEXT * ctx, int *index_hint) {
867 ret = _mx_close_mailbox (ctx, index_hint);
868 sidebar_set_buffystats (ctx);
872 /* update a Context structure's internal tables. */
874 void mx_update_tables (CONTEXT * ctx, int committing)
878 /* update memory to reflect the new state of the mailbox */
887 #define this_body ctx->hdrs[j]->content
888 for (i = 0, j = 0; i < ctx->msgcount; i++) {
889 if ((committing && (!ctx->hdrs[i]->deleted ||
890 (ctx->magic == M_MAILDIR
891 && option (OPTMAILDIRTRASH)))) || (!committing
895 ctx->hdrs[j] = ctx->hdrs[i];
898 ctx->hdrs[j]->msgno = j;
899 if (ctx->hdrs[j]->virtual != -1) {
900 ctx->v2r[ctx->vcount] = j;
901 ctx->hdrs[j]->virtual = ctx->vcount++;
902 ctx->vsize += this_body->length + this_body->offset -
903 this_body->hdr_offset;
907 ctx->hdrs[j]->changed = 0;
908 else if (ctx->hdrs[j]->changed)
912 || (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH))) {
913 if (ctx->hdrs[j]->deleted)
917 if (ctx->hdrs[j]->tagged)
919 if (ctx->hdrs[j]->flagged)
921 if (!ctx->hdrs[j]->read) {
923 if (!ctx->hdrs[j]->old)
930 if (ctx->magic == M_MH || ctx->magic == M_MAILDIR)
931 ctx->size -= (ctx->hdrs[i]->content->length +
932 ctx->hdrs[i]->content->offset -
933 ctx->hdrs[i]->content->hdr_offset);
934 /* remove message from the hash tables */
935 if (ctx->subj_hash && ctx->hdrs[i]->env->real_subj)
936 hash_delete (ctx->subj_hash, ctx->hdrs[i]->env->real_subj,
938 if (ctx->id_hash && ctx->hdrs[i]->env->message_id)
939 hash_delete (ctx->id_hash, ctx->hdrs[i]->env->message_id,
941 header_delete(&ctx->hdrs[i]);
947 /* update sidebar count */
948 sidebar_set_buffystats (ctx);
952 /* save changes to mailbox
958 static int _mx_sync_mailbox (CONTEXT * ctx, int *index_hint)
962 int msgcount, deleted;
964 if (ctx->dontwrite) {
965 char buf[STRING], tmp[STRING];
967 if (km_expand_key (buf, sizeof (buf),
968 km_find_func (MENU_MAIN, OP_TOGGLE_WRITE)))
969 snprintf (tmp, sizeof (tmp), _(" Press '%s' to toggle write"), buf);
971 m_strcpy(tmp, sizeof(tmp), _("Use 'toggle-write' to re-enable write!"));
973 mutt_error (_("Mailbox is marked unwritable. %s"), tmp);
976 else if (ctx->readonly) {
977 mutt_error _("Mailbox is read-only.");
982 if (!ctx->changed && !ctx->deleted) {
983 mutt_message _("Mailbox is unchanged.");
989 char buf[SHORT_STRING];
991 snprintf (buf, sizeof (buf), ctx->deleted == 1
992 ? _("Purge %d deleted message?") :
993 _("Purge %d deleted messages?"), ctx->deleted);
994 if ((purge = query_quadoption (OPT_DELETE, buf)) < 0)
996 else if (purge == M_NO) {
998 return 0; /* nothing to do! */
999 /* let IMAP servers hold on to D flags */
1000 if (ctx->magic != M_IMAP)
1002 for (i = 0; i < ctx->msgcount; i++)
1003 ctx->hdrs[i]->deleted = 0;
1007 else if (ctx->last_tag && ctx->last_tag->deleted)
1008 ctx->last_tag = NULL; /* reset last tagged msg now useless */
1011 /* really only for IMAP - imap_sync_mailbox results in a call to
1012 * mx_update_tables, so ctx->deleted is 0 when it comes back */
1013 msgcount = ctx->msgcount;
1014 deleted = ctx->deleted;
1016 if (purge && ctx->deleted) {
1017 if (trash_append (ctx) == -1)
1021 if (ctx->magic == M_IMAP)
1022 rc = imap_sync_mailbox (ctx, purge, index_hint);
1024 rc = sync_mailbox (ctx, index_hint);
1026 if (ctx->magic == M_IMAP && !purge)
1027 mutt_message (_("Mailbox checkpointed."));
1030 mutt_message (_("%d kept, %d deleted."), msgcount - deleted, deleted);
1034 if (ctx->msgcount == ctx->deleted &&
1035 (ctx->magic == M_MBOX || ctx->magic == M_MMDF) &&
1036 !mutt_is_spool (ctx->path) && !option (OPTSAVEEMPTY)) {
1038 mx_fastclose_mailbox (ctx);
1042 /* if we haven't deleted any messages, we don't need to resort */
1043 /* ... except for certain folder formats which need "unsorted"
1044 * sort order in order to synchronize folders.
1046 * MH and maildir are safe. mbox-style seems to need re-sorting,
1047 * at least with the new threading code.
1049 if (purge || (ctx->magic != M_MAILDIR && ctx->magic != M_MH)) {
1050 /* IMAP does this automatically after handling EXPUNGE */
1051 if (ctx->magic != M_IMAP)
1053 mx_update_tables (ctx, 1);
1054 mutt_sort_headers (ctx, 1); /* rethread from scratch */
1062 int mx_sync_mailbox (CONTEXT* ctx, int* index_hint) {
1063 int ret = _mx_sync_mailbox (ctx, index_hint);
1064 sidebar_set_buffystats (ctx);
1069 * dest destintation mailbox
1070 * hdr message being copied (required for maildir support, because
1071 * the filename depends on the message flags)
1073 MESSAGE *mx_open_new_message (CONTEXT * dest, HEADER * hdr, int flags)
1076 address_t *p = NULL;
1078 if (!MX_IDX(dest->magic-1)) {
1082 msg = p_new(MESSAGE, 1);
1083 msg->magic = dest->magic;
1087 msg->flags.flagged = hdr->flagged;
1088 msg->flags.replied = hdr->replied;
1089 msg->flags.read = hdr->read;
1090 msg->received = hdr->received;
1093 if (msg->received == 0)
1094 time (&msg->received);
1096 if (mxfmts[dest->magic-1]->mx_open_new_message(msg, dest, hdr) == 0) {
1097 if (dest->magic == M_MMDF)
1098 fputs (MMDF_SEP, msg->fp);
1100 if ((msg->magic == M_MBOX || msg->magic == M_MMDF) && flags & M_ADD_FROM) {
1102 if (hdr->env->return_path)
1103 p = hdr->env->return_path;
1104 else if (hdr->env->sender)
1105 p = hdr->env->sender;
1110 fprintf (msg->fp, "From %s %s", p ? p->mailbox : NONULL (Username),
1111 ctime (&msg->received));
1120 /* check for new mail */
1121 int mx_check_mailbox (CONTEXT * ctx, int *index_hint, int lock) {
1122 if (ctx->compressinfo)
1123 return mutt_check_mailbox_compressed (ctx);
1128 if (MX_IDX(ctx->magic-1) && mxfmts[ctx->magic-1]->mx_check_mailbox)
1129 return (mxfmts[ctx->magic-1]->mx_check_mailbox(ctx, index_hint, lock));
1135 /* return a stream pointer for a message */
1136 MESSAGE *mx_open_message (CONTEXT * ctx, int msgno)
1140 msg = p_new(MESSAGE, 1);
1141 switch (msg->magic = ctx->magic) {
1150 HEADER *cur = ctx->hdrs[msgno];
1151 char path[_POSIX_PATH_MAX];
1153 snprintf (path, sizeof (path), "%s/%s", ctx->path, cur->path);
1155 if ((msg->fp = fopen (path, "r")) == NULL && errno == ENOENT &&
1156 ctx->magic == M_MAILDIR)
1157 msg->fp = maildir_open_find_message (ctx->path, cur->path);
1159 if (msg->fp == NULL) {
1168 if (imap_fetch_message (msg, ctx, msgno) != 0)
1175 if (pop_fetch_message (msg, ctx, msgno) != 0)
1183 if (nntp_fetch_message (msg, ctx, msgno) != 0)
1187 #endif /* USE_NNTP */
1196 /* commit a message to a folder */
1198 int mx_commit_message (MESSAGE * msg, CONTEXT * ctx) {
1199 if (!(msg->write && ctx->append)) {
1202 if (!ctx || !MX_IDX(ctx->magic-1) || !mxfmts[ctx->magic-1]->mx_commit_message)
1204 return (mxfmts[ctx->magic-1]->mx_commit_message (msg, ctx));
1207 /* close a pointer to a message */
1208 int mx_close_message (MESSAGE ** msg)
1212 if ((*msg)->magic == M_MH || (*msg)->magic == M_MAILDIR
1213 || (*msg)->magic == M_IMAP
1214 || (*msg)->magic == M_POP
1216 || (*msg)->magic == M_NNTP
1219 r = safe_fclose (&(*msg)->fp);
1225 unlink ((*msg)->path);
1226 p_delete(&(*msg)->path);
1233 void mx_alloc_memory (CONTEXT * ctx)
1236 size_t s = MAX (sizeof (HEADER *), sizeof (int));
1238 if ((ctx->hdrmax + 25) * s < ctx->hdrmax * s) {
1239 mutt_error _("Integer overflow -- can't allocate memory.");
1246 p_realloc(&ctx->hdrs, ctx->hdrmax += 25);
1247 p_realloc(&ctx->v2r, ctx->hdrmax);
1250 ctx->hdrs = p_new(HEADER *, (ctx->hdrmax += 25));
1251 ctx->v2r = p_new(int, ctx->hdrmax);
1253 for (i = ctx->msgcount; i < ctx->hdrmax; i++) {
1254 ctx->hdrs[i] = NULL;
1259 /* this routine is called to update the counts in the context structure for
1260 * the last message header parsed.
1262 void mx_update_context (CONTEXT * ctx, int new_messages)
1267 for (msgno = ctx->msgcount - new_messages; msgno < ctx->msgcount; msgno++) {
1268 h = ctx->hdrs[msgno];
1270 /* NOTE: this _must_ be done before the check for mailcap! */
1271 h->security = crypt_query (h->content);
1273 if (!ctx->pattern) {
1274 ctx->v2r[ctx->vcount] = msgno;
1275 h->virtual = ctx->vcount++;
1281 if (h->env->supersedes) {
1285 ctx->id_hash = mutt_make_id_hash (ctx);
1287 h2 = hash_find (ctx->id_hash, h->env->supersedes);
1289 /* p_delete(&h->env->supersedes); should I ? */
1292 if (!ctx->counting && option (OPTSCORE))
1293 mutt_score_message (ctx, h2, 1);
1297 /* add this message to the hash tables */
1298 if (ctx->id_hash && h->env->message_id)
1299 hash_insert (ctx->id_hash, h->env->message_id, h, 0);
1300 if (!ctx->counting) {
1301 if (ctx->subj_hash && h->env->real_subj)
1302 hash_insert (ctx->subj_hash, h->env->real_subj, h, 1);
1304 if (option (OPTSCORE))
1305 mutt_score_message (ctx, h, 0);
1320 /* update sidebar count */
1321 sidebar_set_buffystats (ctx);
1326 * 1 if the specified mailbox contains 0 messages.
1327 * 0 if the mailbox contains messages
1330 int mx_check_empty (const char *path)
1333 if ((i = mx_get_idx (path)) >= 0 && mxfmts[i]->mx_check_empty)
1334 return (mxfmts[i]->mx_check_empty(path));
1339 int mx_acl_check (CONTEXT* ctx, int flag) {
1340 if (!ctx || !MX_IDX(ctx->magic-1))
1342 /* if no acl_check defined for module, assume permission is granted */
1343 if (!mxfmts[ctx->magic-1]->mx_acl_check)
1345 return (mxfmts[ctx->magic-1]->mx_acl_check(ctx,flag));
1348 int mx_rebuild_cache (void) {
1350 mutt_error (_("Support for header caching was not build in."));
1353 int i = 0, magic = 0;
1354 CONTEXT* ctx = NULL;
1357 if (list_empty(Incoming)) {
1358 mutt_error (_("No mailboxes defined."));
1362 for (i = 0; i < Incoming->length; i++) {
1363 b = (BUFFY*) Incoming->data[i];
1364 magic = mx_get_magic (b->path);
1365 if (magic != M_MAILDIR && magic != M_MH && magic != M_IMAP)
1367 sidebar_set_current (b->path);
1369 if ((ctx = mx_open_mailbox (b->path,
1370 M_READONLY | M_NOSORT | M_COUNT,
1372 mx_close_mailbox (ctx, 0);
1374 mutt_clear_error ();
1376 if (Context && Context->path)
1377 sidebar_set_current (Context->path);
1384 void mutt_parse_mime_message (CONTEXT * ctx, HEADER * cur)
1390 if (cur->content->type != TYPEMESSAGE
1391 && cur->content->type != TYPEMULTIPART)
1392 break; /* nothing to do */
1394 if (cur->content->parts)
1395 break; /* The message was parsed earlier. */
1397 if ((msg = mx_open_message (ctx, cur->msgno))) {
1398 mutt_parse_part (msg->fp, cur->content);
1400 cur->security = crypt_query (cur->content);
1402 mx_close_message (&msg);
1405 mutt_count_body_parts (cur, flags | M_PARTS_RECOUNT);