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>
13 #include <lib-lua/lib-lua.h>
14 #include <lib-sys/unix.h>
15 #include <lib-mime/mime.h>
16 #include <lib-ui/sidebar.h>
31 #include <imap/imap.h>
34 #include <nntp/nntp.h>
37 static mx_t const *mxfmts[] = {
49 #define MX_IDX(idx) (idx >= 0 && idx < countof(mxfmts))
50 #define mutt_is_spool(s) (m_strcmp(Spoolfile, s) == 0)
54 * retry - should retry if unable to lock?
56 static int invoke_dotlock (const char *path, int flags, int retry)
58 char cmd[LONG_STRING + _POSIX_PATH_MAX];
59 char f[STRING + _POSIX_PATH_MAX];
62 if (flags & DL_FL_RETRY)
63 snprintf (r, sizeof (r), "-r %d ", retry ? MAXLOCKATTEMPT : 0);
65 mutt_quote_filename (f, sizeof (f), path);
67 snprintf(cmd, sizeof(cmd), "%s %s%s%s%s%s%s%s",
69 flags & DL_FL_TRY ? "-t " : "",
70 flags & DL_FL_UNLOCK ? "-u " : "",
71 flags & DL_FL_USEPRIV ? "-p " : "",
72 flags & DL_FL_FORCE ? "-f " : "",
73 flags & DL_FL_UNLINK ? "-d " : "",
74 flags & DL_FL_RETRY ? r : "", f);
76 return mutt_system (cmd);
79 static int dotlock_file (const char *path, int retry)
82 int flags = DL_FL_USEPRIV | DL_FL_RETRY;
88 if ((r = invoke_dotlock (path, flags, retry)) == DL_EX_EXIST) {
89 if (!option (OPTNOCURSES)) {
90 char msg[LONG_STRING];
92 snprintf (msg, sizeof (msg),
93 _("Lock count exceeded, remove lock for %s?"), path);
94 if (retry && mutt_yesorno (msg, M_YES) == M_YES) {
102 mutt_error (_("Can't dotlock %s.\n"), path);
105 return (r == DL_EX_OK ? 0 : -1);
108 static int undotlock_file (const char *path)
110 return (invoke_dotlock (path, DL_FL_USEPRIV | DL_FL_UNLOCK, 0) ==
114 /* looks up index of type for path in mxfmts */
115 static int mx_get_idx (const char* path) {
119 /* first, test all non-local folders to avoid stat() call */
120 for (i = 0; i < countof(mxfmts); i++) {
121 if (!mxfmts[i]->local)
122 t = mxfmts[i]->mx_is_magic(path, NULL);
126 if (stat (path, &st) == 0) {
127 /* if stat() succeeded, keep testing until success and
128 * pass stat() info so that we only need to do it once */
129 for (i = 0; i < countof(mxfmts); i++) {
130 if (mxfmts[i]->local)
131 t = mxfmts[i]->mx_is_magic(path, &st);
140 * excl if excl != 0, request an exclusive lock
141 * dot if dot != 0, try to dotlock the file
142 * time_out should retry locking?
144 int mx_lock_file (const char *path, int fd, int excl, int dot, int time_out)
146 #if defined (USE_FCNTL) || defined (USE_FLOCK)
158 lck.l_type = excl ? F_WRLCK : F_RDLCK;
159 lck.l_whence = SEEK_SET;
164 while (fcntl (fd, F_SETLK, &lck) == -1) {
167 if (errno != EAGAIN && errno != EACCES) {
168 mutt_perror ("fcntl");
172 if (fstat (fd, &sb) != 0)
178 /* only unlock file if it is unchanged */
179 if (prev_sb.st_size == sb.st_size
180 && ++count >= (time_out ? MAXLOCKATTEMPT : 0)) {
182 mutt_error _("Timeout exceeded while attempting fcntl lock!");
189 mutt_message (_("Waiting for fcntl lock... %d"), ++attempt);
192 #endif /* USE_FCNTL */
197 while (flock (fd, (excl ? LOCK_EX : LOCK_SH) | LOCK_NB) == -1) {
200 if (errno != EWOULDBLOCK) {
201 mutt_perror ("flock");
206 if (fstat (fd, &sb) != 0)
212 /* only unlock file if it is unchanged */
213 if (prev_sb.st_size == sb.st_size
214 && ++count >= (time_out ? MAXLOCKATTEMPT : 0)) {
216 mutt_error _("Timeout exceeded while attempting flock lock!");
224 mutt_message (_("Waiting for flock attempt... %d"), ++attempt);
227 #endif /* USE_FLOCK */
230 r = dotlock_file (path, time_out);
233 /* release any other locks obtained in this routine */
236 lck.l_type = F_UNLCK;
237 fcntl (fd, F_SETLK, &lck);
238 #endif /* USE_FCNTL */
242 #endif /* USE_FLOCK */
250 int mx_unlock_file (const char *path, int fd, int dot)
253 struct flock unlockit;
255 p_clear(&unlockit, 1);
256 unlockit.l_type = F_UNLCK;
257 unlockit.l_whence = SEEK_SET;
258 fcntl (fd, F_SETLK, &unlockit);
266 undotlock_file (path);
271 /* try to figure out what type of mailbox ``path'' is */
272 int mx_get_magic (const char *path) {
275 if (m_strlen(path) == 0)
277 if ((i = mx_get_idx (path)) >= 0)
278 return (mxfmts[i]->type);
282 int mx_is_local (int m) {
285 return (mxfmts[m]->local);
289 * set DefaultMagic to the given value
291 int mx_set_magic (const char *s)
293 if (ascii_strcasecmp (s, "mbox") == 0)
294 DefaultMagic = M_MBOX;
295 else if (ascii_strcasecmp (s, "mh") == 0)
297 else if (ascii_strcasecmp (s, "maildir") == 0)
298 DefaultMagic = M_MAILDIR;
305 /* mx_access: Wrapper for access, checks permissions on a given mailbox.
306 * We may be interested in using ACL-style flags at some point, currently
307 * we use the normal access() flags. */
308 int mx_access (const char *path, int flags)
312 if ((i = mx_get_idx (path)) >= 0 && mxfmts[i]->mx_access)
313 return (mxfmts[i]->mx_access(path,flags));
317 static int mx_open_mailbox_append (CONTEXT * ctx, int flags)
321 /* special case for appending to compressed folders -
322 * even if we can not open them for reading */
323 if (mutt_can_append_compressed (ctx->path))
324 mutt_open_append_compressed (ctx);
328 if (mx_get_magic (ctx->path) == M_IMAP)
329 return imap_open_mailbox_append (ctx);
331 if (stat (ctx->path, &sb) == 0) {
332 ctx->magic = mx_get_magic (ctx->path);
334 switch (ctx->magic) {
336 mutt_error (_("%s is not a mailbox."), ctx->path);
342 else if (errno == ENOENT) {
343 ctx->magic = DefaultMagic;
345 if (ctx->magic == M_MH || ctx->magic == M_MAILDIR) {
346 char tmp[_POSIX_PATH_MAX];
348 if (mkdir (ctx->path, S_IRWXU)) {
349 mutt_perror (ctx->path);
353 if (ctx->magic == M_MAILDIR) {
354 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
355 if (mkdir (tmp, S_IRWXU)) {
361 snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
362 if (mkdir (tmp, S_IRWXU)) {
364 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
369 snprintf (tmp, sizeof (tmp), "%s/tmp", ctx->path);
370 if (mkdir (tmp, S_IRWXU)) {
372 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
374 snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
383 snprintf (tmp, sizeof (tmp), "%s/.mh_sequences", ctx->path);
384 if ((i = creat (tmp, S_IRWXU)) == -1) {
394 mutt_perror (ctx->path);
398 switch (ctx->magic) {
401 safe_fopen (ctx->path, flags & M_NEWFOLDER ? "w" : "a")) == NULL
402 || mbox_lock_mailbox (ctx, 1, 1) != 0) {
404 mutt_perror (ctx->path);
406 mutt_error (_("Couldn't lock %s\n"), ctx->path);
411 fseeko (ctx->fp, 0, 2);
427 * open a mailbox and parse it
430 * flags M_NOSORT do not sort mailbox
431 * M_APPEND open mailbox for appending
432 * M_READONLY open mailbox in read-only mode
433 * M_QUIET only print error messages
434 * ctx if non-null, context struct to use
436 CONTEXT *mx_open_mailbox (const char *path, int flags, CONTEXT * pctx)
442 ctx = p_new(CONTEXT, 1);
444 ctx->path = m_strdup(path);
446 ctx->msgnotreadyet = -1;
451 if (flags & M_READONLY)
456 if (flags & (M_APPEND | M_NEWFOLDER)) {
457 if (mx_open_mailbox_append (ctx, flags) != 0) {
458 mx_fastclose_mailbox (ctx);
466 if (!MX_IDX(ctx->magic-1))
467 ctx->magic = mx_get_magic (path);
469 if (ctx->magic == M_COMPRESSED)
470 mutt_open_read_compressed (ctx);
473 mutt_error (_("%s is not a mailbox."), path);
475 if (ctx->magic == -1)
478 if (ctx->magic <= 0) {
479 mx_fastclose_mailbox (ctx);
485 /* if the user has a `push' command in their .muttrc, or in a folder-hook,
486 * it will cause the progress messages not to be displayed because
487 * mutt_refresh() will think we are in the middle of a macro. so set a
488 * flag to indicate that we should really refresh the screen.
490 set_option (OPTFORCEREFRESH);
493 mutt_message (_("Reading %s..."), ctx->path);
495 rc = mxfmts[ctx->magic-1]->mx_open_mailbox(ctx);
498 if ((flags & M_NOSORT) == 0) {
499 /* avoid unnecessary work since the mailbox is completely unthreaded
501 unset_option (OPTSORTSUBTHREADS);
502 unset_option (OPTNEEDRESCORE);
503 mutt_sort_headers (ctx, 1);
509 mx_fastclose_mailbox (ctx);
514 unset_option (OPTFORCEREFRESH);
518 /* free up memory associated with the mailbox context */
519 void mx_fastclose_mailbox (CONTEXT * ctx)
526 if (MX_IDX(ctx->magic-1) && mxfmts[ctx->magic-1]->mx_fastclose_mailbox)
527 mxfmts[ctx->magic-1]->mx_fastclose_mailbox(ctx);
529 hash_delete (&ctx->subj_hash, NULL);
531 hash_delete (&ctx->id_hash, NULL);
532 mutt_clear_threads (ctx);
533 for (i = 0; i < ctx->msgcount; i++)
534 header_delete(&ctx->hdrs[i]);
535 p_delete(&ctx->hdrs);
539 mutt_fast_close_compressed (ctx);
541 p_delete(&ctx->path);
542 p_delete(&ctx->pattern);
543 pattern_list_wipe(&ctx->limit_pattern);
548 /* save changes to disk */
549 static int sync_mailbox (CONTEXT * ctx, int *index_hint)
554 mutt_message (_("Writing %s..."), ctx->path);
556 if (MX_IDX(ctx->magic-1))
557 /* the 1 is only of interest for IMAP and means EXPUNGE */
558 rc = mxfmts[ctx->magic-1]->mx_sync_mailbox(ctx,1,index_hint);
560 if (rc == 0 && ctx->cinfo)
561 return mutt_sync_compressed (ctx);
566 /* move deleted mails to the trash folder */
567 static int trash_append (CONTEXT * ctx)
573 if (!TrashPath || !ctx->deleted ||
574 (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))
577 for (; i < ctx->msgcount && (!ctx->hdrs[i]->deleted ||
578 ctx->hdrs[i]->appended); i++);
579 if (i == ctx->msgcount)
580 return 0; /* nothing to be done */
582 if (mutt_save_confirm (TrashPath, &st) != 0) {
583 mutt_error _("message(s) not deleted");
588 if (lstat (ctx->path, &stc) == 0 && stc.st_ino == st.st_ino
589 && stc.st_dev == st.st_dev && stc.st_rdev == st.st_rdev)
590 return 0; /* we are in the trash folder: simple sync */
592 if ((ctx_trash = mx_open_mailbox (TrashPath, M_APPEND, NULL)) != NULL) {
593 for (i = 0; i < ctx->msgcount; i++)
594 if (ctx->hdrs[i]->deleted && !ctx->hdrs[i]->appended
595 && !ctx->hdrs[i]->purged
596 && mutt_append_message (ctx_trash, ctx, ctx->hdrs[i], 0, 0) == -1) {
597 mx_close_mailbox (ctx_trash, NULL);
601 mx_close_mailbox (ctx_trash, NULL);
604 mutt_error _("Can't open trash folder");
612 /* save changes and close mailbox */
613 static int _mx_close_mailbox (CONTEXT * ctx, int *index_hint)
615 int i, move_messages = 0, purge = 1, read_msgs = 0;
619 char mbox[_POSIX_PATH_MAX];
628 if (ctx->magic == M_NNTP) {
631 ret = nntp_close_mailbox (ctx);
632 mx_fastclose_mailbox (ctx);
636 if (ctx->readonly || ctx->dontwrite) {
637 /* mailbox is readonly or we don't want to write */
638 mx_fastclose_mailbox (ctx);
643 /* mailbox was opened in write-mode */
644 if (ctx->magic == M_MBOX)
645 mbox_close_mailbox (ctx);
647 mx_fastclose_mailbox (ctx);
651 for (i = 0; i < ctx->msgcount; i++) {
652 if (!ctx->hdrs[i]->deleted && ctx->hdrs[i]->read
653 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
657 if (read_msgs && quadoption (OPT_MOVE) != M_NO) {
660 if ((p = mutt_find_hook (M_MBOXHOOK, ctx->path))) {
662 m_strcpy(mbox, sizeof(mbox), p);
665 m_strcpy(mbox, sizeof(mbox), NONULL(Inbox));
666 isSpool = mutt_is_spool (ctx->path) && !mutt_is_spool (mbox);
668 mutt_expand_path (mbox, sizeof (mbox));
671 snprintf (buf, sizeof (buf), _("Move read messages to %s?"), mbox);
672 if ((move_messages = query_quadoption (OPT_MOVE, buf)) == -1) {
680 * There is no point in asking whether or not to purge if we are
681 * just marking messages as "trash".
683 if (ctx->deleted && !(ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH))) {
684 snprintf (buf, sizeof (buf), ctx->deleted == 1
685 ? _("Purge %d deleted message?") :
686 _("Purge %d deleted messages?"), ctx->deleted);
687 if ((purge = query_quadoption (OPT_DELETE, buf)) < 0) {
693 /* IMAP servers manage the OLD flag themselves */
694 if (ctx->magic != M_IMAP)
695 if (option (OPTMARKOLD)) {
696 for (i = 0; i < ctx->msgcount; i++) {
697 if (!ctx->hdrs[i]->deleted && !ctx->hdrs[i]->old)
698 mutt_set_flag (ctx, ctx->hdrs[i], M_OLD, 1);
703 mutt_message (_("Moving read messages to %s..."), mbox);
705 /* try to use server-side copy first */
708 if (ctx->magic == M_IMAP && imap_is_magic (mbox, NULL) == M_IMAP) {
709 /* tag messages for moving, and clear old tags, if any */
710 for (i = 0; i < ctx->msgcount; i++)
711 if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted
712 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
713 ctx->hdrs[i]->tagged = 1;
715 ctx->hdrs[i]->tagged = 0;
717 i = imap_copy_messages (ctx, NULL, mbox, 1);
720 if (i == 0) /* success */
722 else if (i == -1) { /* horrible error, bail */
726 else /* use regular append-copy mode */
728 if (mx_open_mailbox (mbox, M_APPEND, &f) == NULL) {
733 for (i = 0; i < ctx->msgcount; i++) {
734 if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted
735 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED))) {
736 if (mutt_append_message (&f, ctx, ctx->hdrs[i], 0, CH_UPDATE_LEN) ==
738 mutt_set_flag (ctx, ctx->hdrs[i], M_DELETE, 1);
739 mutt_set_flag (ctx, ctx->hdrs[i], M_APPENDED, 1);
742 mx_close_mailbox (&f, NULL);
749 mx_close_mailbox (&f, NULL);
753 else if (!ctx->changed && ctx->deleted == 0) {
754 mutt_message _("Mailbox is unchanged.");
756 mx_fastclose_mailbox (ctx);
760 /* copy mails to the trash before expunging */
761 if (purge && ctx->deleted)
762 if (trash_append (ctx) != 0) {
767 /* allow IMAP to preserve the deleted flag across sessions */
768 if (ctx->magic == M_IMAP) {
769 if ((check = imap_sync_mailbox (ctx, purge, index_hint)) != 0) {
777 for (i = 0; i < ctx->msgcount; i++)
778 ctx->hdrs[i]->deleted = 0;
782 if (ctx->changed || ctx->deleted) {
783 if ((check = sync_mailbox (ctx, index_hint)) != 0) {
791 mutt_message (_("%d kept, %d moved, %d deleted."),
792 ctx->msgcount - ctx->deleted, read_msgs, ctx->deleted);
794 mutt_message (_("%d kept, %d deleted."),
795 ctx->msgcount - ctx->deleted, ctx->deleted);
797 if (ctx->cinfo && mutt_slow_close_compressed (ctx))
800 mx_fastclose_mailbox (ctx);
805 int mx_close_mailbox (CONTEXT * ctx, int *index_hint) {
809 ret = _mx_close_mailbox (ctx, index_hint);
810 sidebar_set_buffystats (ctx);
814 /* update a Context structure's internal tables. */
816 void mx_update_tables (CONTEXT * ctx, int committing)
820 /* update memory to reflect the new state of the mailbox */
829 #define this_body ctx->hdrs[j]->content
830 for (i = 0, j = 0; i < ctx->msgcount; i++) {
831 if ((committing && (!ctx->hdrs[i]->deleted ||
832 (ctx->magic == M_MAILDIR
833 && option (OPTMAILDIRTRASH)))) || (!committing
837 ctx->hdrs[j] = ctx->hdrs[i];
840 ctx->hdrs[j]->msgno = j;
841 if (ctx->hdrs[j]->virtual != -1) {
842 ctx->v2r[ctx->vcount] = j;
843 ctx->hdrs[j]->virtual = ctx->vcount++;
844 ctx->vsize += this_body->length + this_body->offset -
845 this_body->hdr_offset;
849 ctx->hdrs[j]->changed = 0;
850 else if (ctx->hdrs[j]->changed)
854 || (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH))) {
855 if (ctx->hdrs[j]->deleted)
859 if (ctx->hdrs[j]->tagged)
861 if (ctx->hdrs[j]->flagged)
863 if (!ctx->hdrs[j]->read) {
865 if (!ctx->hdrs[j]->old)
872 if (ctx->magic == M_MH || ctx->magic == M_MAILDIR)
873 ctx->size -= (ctx->hdrs[i]->content->length +
874 ctx->hdrs[i]->content->offset -
875 ctx->hdrs[i]->content->hdr_offset);
876 /* remove message from the hash tables */
877 if (ctx->subj_hash && ctx->hdrs[i]->env->real_subj)
878 hash_remove (ctx->subj_hash, ctx->hdrs[i]->env->real_subj,
880 if (ctx->id_hash && ctx->hdrs[i]->env->message_id)
881 hash_remove (ctx->id_hash, ctx->hdrs[i]->env->message_id,
883 header_delete(&ctx->hdrs[i]);
889 /* update sidebar count */
890 sidebar_set_buffystats (ctx);
894 /* save changes to mailbox
900 static int _mx_sync_mailbox (CONTEXT * ctx, int *index_hint)
904 int msgcount, deleted;
906 if (ctx->dontwrite) {
907 char buf[STRING], tmp[STRING];
909 if (km_expand_key (buf, sizeof (buf),
910 km_find_func (MENU_MAIN, OP_TOGGLE_WRITE)))
911 snprintf (tmp, sizeof (tmp), _(" Press '%s' to toggle write"), buf);
913 m_strcpy(tmp, sizeof(tmp), _("Use 'toggle-write' to re-enable write!"));
915 mutt_error (_("Mailbox is marked unwritable. %s"), tmp);
918 else if (ctx->readonly) {
919 mutt_error _("Mailbox is read-only.");
924 if (!ctx->changed && !ctx->deleted) {
925 mutt_message _("Mailbox is unchanged.");
933 snprintf (buf, sizeof (buf), ctx->deleted == 1
934 ? _("Purge %d deleted message?") :
935 _("Purge %d deleted messages?"), ctx->deleted);
936 if ((purge = query_quadoption (OPT_DELETE, buf)) < 0)
938 else if (purge == M_NO) {
940 return 0; /* nothing to do! */
941 /* let IMAP servers hold on to D flags */
942 if (ctx->magic != M_IMAP)
944 for (i = 0; i < ctx->msgcount; i++)
945 ctx->hdrs[i]->deleted = 0;
949 else if (ctx->last_tag && ctx->last_tag->deleted)
950 ctx->last_tag = NULL; /* reset last tagged msg now useless */
953 /* really only for IMAP - imap_sync_mailbox results in a call to
954 * mx_update_tables, so ctx->deleted is 0 when it comes back */
955 msgcount = ctx->msgcount;
956 deleted = ctx->deleted;
958 if (purge && ctx->deleted) {
959 if (trash_append (ctx) == -1)
963 if (ctx->magic == M_IMAP)
964 rc = imap_sync_mailbox (ctx, purge, index_hint);
966 rc = sync_mailbox (ctx, index_hint);
968 if (ctx->magic == M_IMAP && !purge)
969 mutt_message (_("Mailbox checkpointed."));
971 mutt_message (_("%d kept, %d deleted."), msgcount - deleted, deleted);
975 /* if we haven't deleted any messages, we don't need to resort */
976 /* ... except for certain folder formats which need "unsorted"
977 * sort order in order to synchronize folders.
979 * MH and maildir are safe. mbox-style seems to need re-sorting,
980 * at least with the new threading code.
982 if (purge || (ctx->magic != M_MAILDIR && ctx->magic != M_MH)) {
983 /* IMAP does this automatically after handling EXPUNGE */
984 if (ctx->magic != M_IMAP)
986 mx_update_tables (ctx, 1);
987 mutt_sort_headers (ctx, 1); /* rethread from scratch */
995 int mx_sync_mailbox (CONTEXT* ctx, int* index_hint) {
996 int ret = _mx_sync_mailbox (ctx, index_hint);
997 sidebar_set_buffystats (ctx);
1002 * dest destintation mailbox
1003 * hdr message being copied (required for maildir support, because
1004 * the filename depends on the message flags)
1006 MESSAGE *mx_open_new_message (CONTEXT * dest, HEADER * hdr, int flags)
1009 address_t *p = NULL;
1011 if (!MX_IDX(dest->magic-1)) {
1015 msg = p_new(MESSAGE, 1);
1016 msg->magic = dest->magic;
1020 msg->flags.flagged = hdr->flagged;
1021 msg->flags.replied = hdr->replied;
1022 msg->flags.read = hdr->read;
1023 msg->received = hdr->received;
1026 if (msg->received == 0)
1027 time (&msg->received);
1029 if (mxfmts[dest->magic-1]->mx_open_new_message(msg, dest, hdr) == 0) {
1030 if (msg->magic == M_MBOX && flags & M_ADD_FROM) {
1032 if (hdr->env->return_path)
1033 p = hdr->env->return_path;
1034 else if (hdr->env->sender)
1035 p = hdr->env->sender;
1040 fprintf (msg->fp, "From %s %s", p ? p->mailbox : NONULL(MCore.username),
1041 ctime (&msg->received));
1050 /* check for new mail */
1051 int mx_check_mailbox (CONTEXT * ctx, int *index_hint, int lock) {
1053 return mutt_check_mailbox_compressed (ctx);
1058 if (MX_IDX(ctx->magic-1) && mxfmts[ctx->magic-1]->mx_check_mailbox)
1059 return (mxfmts[ctx->magic-1]->mx_check_mailbox(ctx, index_hint, lock));
1065 /* return a stream pointer for a message */
1066 MESSAGE *mx_open_message (CONTEXT * ctx, int msgno)
1070 msg = p_new(MESSAGE, 1);
1071 switch (msg->magic = ctx->magic) {
1079 HEADER *cur = ctx->hdrs[msgno];
1080 char path[_POSIX_PATH_MAX];
1082 snprintf (path, sizeof (path), "%s/%s", ctx->path, cur->path);
1084 if ((msg->fp = fopen (path, "r")) == NULL && errno == ENOENT &&
1085 ctx->magic == M_MAILDIR)
1086 msg->fp = maildir_open_find_message (ctx->path, cur->path);
1088 if (msg->fp == NULL) {
1097 if (imap_fetch_message (msg, ctx, msgno) != 0)
1104 if (pop_fetch_message (msg, ctx, msgno) != 0)
1112 if (nntp_fetch_message (msg, ctx, msgno) != 0)
1116 #endif /* USE_NNTP */
1125 /* commit a message to a folder */
1127 int mx_commit_message (MESSAGE * msg, CONTEXT * ctx) {
1128 if (!(msg->write && ctx->append)) {
1131 if (!ctx || !MX_IDX(ctx->magic-1) || !mxfmts[ctx->magic-1]->mx_commit_message)
1133 return (mxfmts[ctx->magic-1]->mx_commit_message (msg, ctx));
1136 /* close a pointer to a message */
1137 int mx_close_message (MESSAGE ** msg)
1141 if ((*msg)->magic == M_MH || (*msg)->magic == M_MAILDIR
1142 || (*msg)->magic == M_IMAP
1143 || (*msg)->magic == M_POP
1145 || (*msg)->magic == M_NNTP
1148 r = m_fclose(&(*msg)->fp);
1154 unlink ((*msg)->path);
1155 p_delete(&(*msg)->path);
1162 void mx_alloc_memory (CONTEXT * ctx)
1166 p_realloc(&ctx->hdrs, ctx->hdrmax);
1167 p_realloc(&ctx->v2r, ctx->hdrmax);
1168 p_clear(ctx->hdrs + ctx->msgcount, ctx->hdrmax - ctx->msgcount);
1169 p_clear(ctx->v2r + ctx->msgcount, ctx->hdrmax - ctx->msgcount);
1172 /* this routine is called to update the counts in the context structure for
1173 * the last message header parsed.
1175 void mx_update_context (CONTEXT * ctx, int new_messages)
1180 for (msgno = ctx->msgcount - new_messages; msgno < ctx->msgcount; msgno++) {
1181 h = ctx->hdrs[msgno];
1183 /* NOTE: this _must_ be done before the check for mailcap! */
1184 h->security = crypt_query (h->content);
1186 if (!ctx->pattern) {
1187 ctx->v2r[ctx->vcount] = msgno;
1188 h->virtual = ctx->vcount++;
1194 if (h->env->supersedes) {
1198 ctx->id_hash = mutt_make_id_hash (ctx);
1200 h2 = hash_find (ctx->id_hash, h->env->supersedes);
1202 /* p_delete(&h->env->supersedes); should I ? */
1205 if (!ctx->counting && option (OPTSCORE))
1206 mutt_score_message (ctx, h2, 1);
1210 /* add this message to the hash tables */
1211 if (ctx->id_hash && h->env->message_id)
1212 hash_insert (ctx->id_hash, h->env->message_id, h);
1213 if (!ctx->counting) {
1214 if (ctx->subj_hash && h->env->real_subj)
1215 hash_insert (ctx->subj_hash, h->env->real_subj, h);
1217 if (option (OPTSCORE))
1218 mutt_score_message (ctx, h, 0);
1233 /* update sidebar count */
1234 sidebar_set_buffystats (ctx);
1239 * 1 if the specified mailbox contains 0 messages.
1240 * 0 if the mailbox contains messages
1243 int mx_check_empty (const char *path)
1246 if ((i = mx_get_idx (path)) >= 0 && mxfmts[i]->mx_check_empty)
1247 return (mxfmts[i]->mx_check_empty(path));
1252 int mx_acl_check(CONTEXT *ctx, int flag)
1254 if (!mxfmts[ctx->magic-1]->mx_acl_check)
1256 return mxfmts[ctx->magic-1]->mx_acl_check(ctx,flag);
1259 void mutt_parse_mime_message (CONTEXT * ctx, HEADER * cur)
1265 if (cur->content->type != TYPEMESSAGE
1266 && cur->content->type != TYPEMULTIPART)
1267 break; /* nothing to do */
1269 if (cur->content->parts)
1270 break; /* The message was parsed earlier. */
1272 if ((msg = mx_open_message (ctx, cur->msgno))) {
1273 mutt_parse_part (msg->fp, cur->content);
1275 cur->security = crypt_query (cur->content);
1277 mx_close_message (&msg);
1280 mutt_count_body_parts (cur, flags | M_PARTS_RECOUNT);