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>
32 #include <imap/imap.h>
35 #include <nntp/nntp.h>
38 static mx_t const *mxfmts[] = {
50 #define MX_IDX(idx) (idx >= 0 && idx < countof(mxfmts))
51 #define mutt_is_spool(s) (m_strcmp(Spoolfile, s) == 0)
55 * retry - should retry if unable to lock?
57 static int invoke_dotlock (const char *path, int flags, int retry)
59 char cmd[LONG_STRING + _POSIX_PATH_MAX];
60 char f[STRING + _POSIX_PATH_MAX];
63 if (flags & DL_FL_RETRY)
64 snprintf (r, sizeof (r), "-r %d ", retry ? MAXLOCKATTEMPT : 0);
66 mutt_quote_filename (f, sizeof (f), path);
68 snprintf(cmd, sizeof(cmd), "%s %s%s%s%s%s%s%s",
70 flags & DL_FL_TRY ? "-t " : "",
71 flags & DL_FL_UNLOCK ? "-u " : "",
72 flags & DL_FL_USEPRIV ? "-p " : "",
73 flags & DL_FL_FORCE ? "-f " : "",
74 flags & DL_FL_UNLINK ? "-d " : "",
75 flags & DL_FL_RETRY ? r : "", f);
77 return mutt_system (cmd);
80 static int dotlock_file (const char *path, int retry)
83 int flags = DL_FL_USEPRIV | DL_FL_RETRY;
89 if ((r = invoke_dotlock (path, flags, retry)) == DL_EX_EXIST) {
90 if (!option (OPTNOCURSES)) {
91 char msg[LONG_STRING];
93 snprintf (msg, sizeof (msg),
94 _("Lock count exceeded, remove lock for %s?"), path);
95 if (retry && mutt_yesorno (msg, M_YES) == M_YES) {
103 mutt_error (_("Can't dotlock %s.\n"), path);
106 return (r == DL_EX_OK ? 0 : -1);
109 static int undotlock_file (const char *path)
111 return (invoke_dotlock (path, DL_FL_USEPRIV | DL_FL_UNLOCK, 0) ==
115 /* looks up index of type for path in mxfmts */
116 static int mx_get_idx (const char* path) {
120 /* first, test all non-local folders to avoid stat() call */
121 for (i = 0; i < countof(mxfmts); i++) {
122 if (!mxfmts[i]->local)
123 t = mxfmts[i]->mx_is_magic(path, NULL);
127 if (stat (path, &st) == 0) {
128 /* if stat() succeeded, keep testing until success and
129 * pass stat() info so that we only need to do it once */
130 for (i = 0; i < countof(mxfmts); i++) {
131 if (mxfmts[i]->local)
132 t = mxfmts[i]->mx_is_magic(path, &st);
141 * excl if excl != 0, request an exclusive lock
142 * dot if dot != 0, try to dotlock the file
143 * time_out should retry locking?
145 int mx_lock_file (const char *path, int fd, int excl, int dot, int time_out)
147 #if defined (USE_FCNTL) || defined (USE_FLOCK)
159 lck.l_type = excl ? F_WRLCK : F_RDLCK;
160 lck.l_whence = SEEK_SET;
165 while (fcntl (fd, F_SETLK, &lck) == -1) {
168 if (errno != EAGAIN && errno != EACCES) {
169 mutt_perror ("fcntl");
173 if (fstat (fd, &sb) != 0)
179 /* only unlock file if it is unchanged */
180 if (prev_sb.st_size == sb.st_size
181 && ++count >= (time_out ? MAXLOCKATTEMPT : 0)) {
183 mutt_error _("Timeout exceeded while attempting fcntl lock!");
190 mutt_message (_("Waiting for fcntl lock... %d"), ++attempt);
193 #endif /* USE_FCNTL */
198 while (flock (fd, (excl ? LOCK_EX : LOCK_SH) | LOCK_NB) == -1) {
201 if (errno != EWOULDBLOCK) {
202 mutt_perror ("flock");
207 if (fstat (fd, &sb) != 0)
213 /* only unlock file if it is unchanged */
214 if (prev_sb.st_size == sb.st_size
215 && ++count >= (time_out ? MAXLOCKATTEMPT : 0)) {
217 mutt_error _("Timeout exceeded while attempting flock lock!");
225 mutt_message (_("Waiting for flock attempt... %d"), ++attempt);
228 #endif /* USE_FLOCK */
231 r = dotlock_file (path, time_out);
234 /* release any other locks obtained in this routine */
237 lck.l_type = F_UNLCK;
238 fcntl (fd, F_SETLK, &lck);
239 #endif /* USE_FCNTL */
243 #endif /* USE_FLOCK */
251 int mx_unlock_file (const char *path, int fd, int dot)
254 struct flock unlockit;
256 p_clear(&unlockit, 1);
257 unlockit.l_type = F_UNLCK;
258 unlockit.l_whence = SEEK_SET;
259 fcntl (fd, F_SETLK, &unlockit);
267 undotlock_file (path);
272 /* try to figure out what type of mailbox ``path'' is */
273 int mx_get_magic (const char *path) {
276 if (m_strlen(path) == 0)
278 if ((i = mx_get_idx (path)) >= 0)
279 return (mxfmts[i]->type);
283 int mx_is_local (int m) {
286 return (mxfmts[m]->local);
290 * set DefaultMagic to the given value
292 int mx_set_magic (const char *s)
294 if (ascii_strcasecmp (s, "mbox") == 0)
295 DefaultMagic = M_MBOX;
296 else if (ascii_strcasecmp (s, "mh") == 0)
298 else if (ascii_strcasecmp (s, "maildir") == 0)
299 DefaultMagic = M_MAILDIR;
306 /* mx_access: Wrapper for access, checks permissions on a given mailbox.
307 * We may be interested in using ACL-style flags at some point, currently
308 * we use the normal access() flags. */
309 int mx_access (const char *path, int flags)
313 if ((i = mx_get_idx (path)) >= 0 && mxfmts[i]->mx_access)
314 return (mxfmts[i]->mx_access(path,flags));
318 static int mx_open_mailbox_append (CONTEXT * ctx, int flags)
322 /* special case for appending to compressed folders -
323 * even if we can not open them for reading */
324 if (mutt_can_append_compressed (ctx->path))
325 mutt_open_append_compressed (ctx);
329 if (mx_get_magic (ctx->path) == M_IMAP)
330 return imap_open_mailbox_append (ctx);
332 if (stat (ctx->path, &sb) == 0) {
333 ctx->magic = mx_get_magic (ctx->path);
335 switch (ctx->magic) {
337 mutt_error (_("%s is not a mailbox."), ctx->path);
343 else if (errno == ENOENT) {
344 ctx->magic = DefaultMagic;
346 if (ctx->magic == M_MH || ctx->magic == M_MAILDIR) {
347 char tmp[_POSIX_PATH_MAX];
349 if (mkdir (ctx->path, S_IRWXU)) {
350 mutt_perror (ctx->path);
354 if (ctx->magic == M_MAILDIR) {
355 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
356 if (mkdir (tmp, S_IRWXU)) {
362 snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
363 if (mkdir (tmp, S_IRWXU)) {
365 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
370 snprintf (tmp, sizeof (tmp), "%s/tmp", ctx->path);
371 if (mkdir (tmp, S_IRWXU)) {
373 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
375 snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
384 snprintf (tmp, sizeof (tmp), "%s/.mh_sequences", ctx->path);
385 if ((i = creat (tmp, S_IRWXU)) == -1) {
395 mutt_perror (ctx->path);
399 switch (ctx->magic) {
402 safe_fopen (ctx->path, flags & M_NEWFOLDER ? "w" : "a")) == NULL
403 || mbox_lock_mailbox (ctx, 1, 1) != 0) {
405 mutt_perror (ctx->path);
407 mutt_error (_("Couldn't lock %s\n"), ctx->path);
412 fseeko (ctx->fp, 0, 2);
428 * open a mailbox and parse it
431 * flags M_NOSORT do not sort mailbox
432 * M_APPEND open mailbox for appending
433 * M_READONLY open mailbox in read-only mode
434 * M_QUIET only print error messages
435 * ctx if non-null, context struct to use
437 CONTEXT *mx_open_mailbox (const char *path, int flags, CONTEXT * pctx)
443 ctx = p_new(CONTEXT, 1);
445 ctx->path = m_strdup(path);
447 ctx->msgnotreadyet = -1;
452 if (flags & M_READONLY)
457 if (flags & (M_APPEND | M_NEWFOLDER)) {
458 if (mx_open_mailbox_append (ctx, flags) != 0) {
459 mx_fastclose_mailbox (ctx);
467 if (!MX_IDX(ctx->magic-1))
468 ctx->magic = mx_get_magic (path);
470 if (ctx->magic == M_COMPRESSED)
471 mutt_open_read_compressed (ctx);
474 mutt_error (_("%s is not a mailbox."), path);
476 if (ctx->magic == -1)
479 if (ctx->magic <= 0) {
480 mx_fastclose_mailbox (ctx);
486 /* if the user has a `push' command in their .muttrc, or in a folder-hook,
487 * it will cause the progress messages not to be displayed because
488 * mutt_refresh() will think we are in the middle of a macro. so set a
489 * flag to indicate that we should really refresh the screen.
491 set_option (OPTFORCEREFRESH);
494 mutt_message (_("Reading %s..."), ctx->path);
496 rc = mxfmts[ctx->magic-1]->mx_open_mailbox(ctx);
499 if ((flags & M_NOSORT) == 0) {
500 /* avoid unnecessary work since the mailbox is completely unthreaded
502 unset_option (OPTSORTSUBTHREADS);
503 unset_option (OPTNEEDRESCORE);
504 mutt_sort_headers (ctx, 1);
510 mx_fastclose_mailbox (ctx);
515 unset_option (OPTFORCEREFRESH);
519 /* free up memory associated with the mailbox context */
520 void mx_fastclose_mailbox (CONTEXT * ctx)
527 if (MX_IDX(ctx->magic-1) && mxfmts[ctx->magic-1]->mx_fastclose_mailbox)
528 mxfmts[ctx->magic-1]->mx_fastclose_mailbox(ctx);
530 hash_delete (&ctx->subj_hash, NULL);
532 hash_delete (&ctx->id_hash, NULL);
533 mutt_clear_threads (ctx);
534 for (i = 0; i < ctx->msgcount; i++)
535 header_delete(&ctx->hdrs[i]);
536 p_delete(&ctx->hdrs);
540 mutt_fast_close_compressed (ctx);
542 p_delete(&ctx->path);
543 p_delete(&ctx->pattern);
544 pattern_list_wipe(&ctx->limit_pattern);
549 /* save changes to disk */
550 static int sync_mailbox (CONTEXT * ctx, int *index_hint)
555 mutt_message (_("Writing %s..."), ctx->path);
557 if (MX_IDX(ctx->magic-1))
558 /* the 1 is only of interest for IMAP and means EXPUNGE */
559 rc = mxfmts[ctx->magic-1]->mx_sync_mailbox(ctx,1,index_hint);
561 if (rc == 0 && ctx->cinfo)
562 return mutt_sync_compressed (ctx);
567 /* move deleted mails to the trash folder */
568 static int trash_append (CONTEXT * ctx)
574 if (!TrashPath || !ctx->deleted ||
575 (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))
578 for (; i < ctx->msgcount && (!ctx->hdrs[i]->deleted ||
579 ctx->hdrs[i]->appended); i++);
580 if (i == ctx->msgcount)
581 return 0; /* nothing to be done */
583 if (mutt_save_confirm (TrashPath, &st) != 0) {
584 mutt_error _("message(s) not deleted");
589 if (lstat (ctx->path, &stc) == 0 && stc.st_ino == st.st_ino
590 && stc.st_dev == st.st_dev && stc.st_rdev == st.st_rdev)
591 return 0; /* we are in the trash folder: simple sync */
593 if ((ctx_trash = mx_open_mailbox (TrashPath, M_APPEND, NULL)) != NULL) {
594 for (i = 0; i < ctx->msgcount; i++)
595 if (ctx->hdrs[i]->deleted && !ctx->hdrs[i]->appended
596 && !ctx->hdrs[i]->purged
597 && mutt_append_message (ctx_trash, ctx, ctx->hdrs[i], 0, 0) == -1) {
598 mx_close_mailbox (ctx_trash, NULL);
602 mx_close_mailbox (ctx_trash, NULL);
605 mutt_error _("Can't open trash folder");
613 /* save changes and close mailbox */
614 static int _mx_close_mailbox (CONTEXT * ctx, int *index_hint)
616 int i, move_messages = 0, purge = 1, read_msgs = 0;
620 char mbox[_POSIX_PATH_MAX];
629 if (ctx->magic == M_NNTP) {
632 ret = nntp_close_mailbox (ctx);
633 mx_fastclose_mailbox (ctx);
637 if (ctx->readonly || ctx->dontwrite) {
638 /* mailbox is readonly or we don't want to write */
639 mx_fastclose_mailbox (ctx);
644 /* mailbox was opened in write-mode */
645 if (ctx->magic == M_MBOX)
646 mbox_close_mailbox (ctx);
648 mx_fastclose_mailbox (ctx);
652 for (i = 0; i < ctx->msgcount; i++) {
653 if (!ctx->hdrs[i]->deleted && ctx->hdrs[i]->read
654 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
658 if (read_msgs && quadoption (OPT_MOVE) != M_NO) {
661 if ((p = mutt_find_hook (M_MBOXHOOK, ctx->path))) {
663 m_strcpy(mbox, sizeof(mbox), p);
666 m_strcpy(mbox, sizeof(mbox), NONULL(Inbox));
667 isSpool = mutt_is_spool (ctx->path) && !mutt_is_spool (mbox);
669 mutt_expand_path (mbox, sizeof (mbox));
672 snprintf (buf, sizeof (buf), _("Move read messages to %s?"), mbox);
673 if ((move_messages = query_quadoption (OPT_MOVE, buf)) == -1) {
681 * There is no point in asking whether or not to purge if we are
682 * just marking messages as "trash".
684 if (ctx->deleted && !(ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH))) {
685 snprintf (buf, sizeof (buf), ctx->deleted == 1
686 ? _("Purge %d deleted message?") :
687 _("Purge %d deleted messages?"), ctx->deleted);
688 if ((purge = query_quadoption (OPT_DELETE, buf)) < 0) {
694 /* IMAP servers manage the OLD flag themselves */
695 if (ctx->magic != M_IMAP)
696 if (option (OPTMARKOLD)) {
697 for (i = 0; i < ctx->msgcount; i++) {
698 if (!ctx->hdrs[i]->deleted && !ctx->hdrs[i]->old)
699 mutt_set_flag (ctx, ctx->hdrs[i], M_OLD, 1);
704 mutt_message (_("Moving read messages to %s..."), mbox);
706 /* try to use server-side copy first */
709 if (ctx->magic == M_IMAP && imap_is_magic (mbox, NULL) == M_IMAP) {
710 /* tag messages for moving, and clear old tags, if any */
711 for (i = 0; i < ctx->msgcount; i++)
712 if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted
713 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
714 ctx->hdrs[i]->tagged = 1;
716 ctx->hdrs[i]->tagged = 0;
718 i = imap_copy_messages (ctx, NULL, mbox, 1);
721 if (i == 0) /* success */
723 else if (i == -1) { /* horrible error, bail */
727 else /* use regular append-copy mode */
729 if (mx_open_mailbox (mbox, M_APPEND, &f) == NULL) {
734 for (i = 0; i < ctx->msgcount; i++) {
735 if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted
736 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED))) {
737 if (mutt_append_message (&f, ctx, ctx->hdrs[i], 0, CH_UPDATE_LEN) ==
739 mutt_set_flag (ctx, ctx->hdrs[i], M_DELETE, 1);
740 mutt_set_flag (ctx, ctx->hdrs[i], M_APPENDED, 1);
743 mx_close_mailbox (&f, NULL);
750 mx_close_mailbox (&f, NULL);
754 else if (!ctx->changed && ctx->deleted == 0) {
755 mutt_message _("Mailbox is unchanged.");
757 mx_fastclose_mailbox (ctx);
761 /* copy mails to the trash before expunging */
762 if (purge && ctx->deleted)
763 if (trash_append (ctx) != 0) {
768 /* allow IMAP to preserve the deleted flag across sessions */
769 if (ctx->magic == M_IMAP) {
770 if ((check = imap_sync_mailbox (ctx, purge, index_hint)) != 0) {
778 for (i = 0; i < ctx->msgcount; i++)
779 ctx->hdrs[i]->deleted = 0;
783 if (ctx->changed || ctx->deleted) {
784 if ((check = sync_mailbox (ctx, index_hint)) != 0) {
792 mutt_message (_("%d kept, %d moved, %d deleted."),
793 ctx->msgcount - ctx->deleted, read_msgs, ctx->deleted);
795 mutt_message (_("%d kept, %d deleted."),
796 ctx->msgcount - ctx->deleted, ctx->deleted);
798 if (ctx->cinfo && mutt_slow_close_compressed (ctx))
801 mx_fastclose_mailbox (ctx);
806 int mx_close_mailbox (CONTEXT * ctx, int *index_hint) {
810 ret = _mx_close_mailbox (ctx, index_hint);
811 sidebar_set_buffystats (ctx);
815 /* update a Context structure's internal tables. */
817 void mx_update_tables (CONTEXT * ctx, int committing)
821 /* update memory to reflect the new state of the mailbox */
830 #define this_body ctx->hdrs[j]->content
831 for (i = 0, j = 0; i < ctx->msgcount; i++) {
832 if ((committing && (!ctx->hdrs[i]->deleted ||
833 (ctx->magic == M_MAILDIR
834 && option (OPTMAILDIRTRASH)))) || (!committing
838 ctx->hdrs[j] = ctx->hdrs[i];
841 ctx->hdrs[j]->msgno = j;
842 if (ctx->hdrs[j]->virtual != -1) {
843 ctx->v2r[ctx->vcount] = j;
844 ctx->hdrs[j]->virtual = ctx->vcount++;
845 ctx->vsize += this_body->length + this_body->offset -
846 this_body->hdr_offset;
850 ctx->hdrs[j]->changed = 0;
851 else if (ctx->hdrs[j]->changed)
855 || (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH))) {
856 if (ctx->hdrs[j]->deleted)
860 if (ctx->hdrs[j]->tagged)
862 if (ctx->hdrs[j]->flagged)
864 if (!ctx->hdrs[j]->read) {
866 if (!ctx->hdrs[j]->old)
873 if (ctx->magic == M_MH || ctx->magic == M_MAILDIR)
874 ctx->size -= (ctx->hdrs[i]->content->length +
875 ctx->hdrs[i]->content->offset -
876 ctx->hdrs[i]->content->hdr_offset);
877 /* remove message from the hash tables */
878 if (ctx->subj_hash && ctx->hdrs[i]->env->real_subj)
879 hash_remove (ctx->subj_hash, ctx->hdrs[i]->env->real_subj,
881 if (ctx->id_hash && ctx->hdrs[i]->env->message_id)
882 hash_remove (ctx->id_hash, ctx->hdrs[i]->env->message_id,
884 header_delete(&ctx->hdrs[i]);
890 /* update sidebar count */
891 sidebar_set_buffystats (ctx);
895 /* save changes to mailbox
901 static int _mx_sync_mailbox (CONTEXT * ctx, int *index_hint)
905 int msgcount, deleted;
907 if (ctx->dontwrite) {
908 char buf[STRING], tmp[STRING];
910 if (km_expand_key (buf, sizeof (buf),
911 km_find_func (MENU_MAIN, OP_TOGGLE_WRITE)))
912 snprintf (tmp, sizeof (tmp), _(" Press '%s' to toggle write"), buf);
914 m_strcpy(tmp, sizeof(tmp), _("Use 'toggle-write' to re-enable write!"));
916 mutt_error (_("Mailbox is marked unwritable. %s"), tmp);
919 else if (ctx->readonly) {
920 mutt_error _("Mailbox is read-only.");
925 if (!ctx->changed && !ctx->deleted) {
926 mutt_message _("Mailbox is unchanged.");
934 snprintf (buf, sizeof (buf), ctx->deleted == 1
935 ? _("Purge %d deleted message?") :
936 _("Purge %d deleted messages?"), ctx->deleted);
937 if ((purge = query_quadoption (OPT_DELETE, buf)) < 0)
939 else if (purge == M_NO) {
941 return 0; /* nothing to do! */
942 /* let IMAP servers hold on to D flags */
943 if (ctx->magic != M_IMAP)
945 for (i = 0; i < ctx->msgcount; i++)
946 ctx->hdrs[i]->deleted = 0;
950 else if (ctx->last_tag && ctx->last_tag->deleted)
951 ctx->last_tag = NULL; /* reset last tagged msg now useless */
954 /* really only for IMAP - imap_sync_mailbox results in a call to
955 * mx_update_tables, so ctx->deleted is 0 when it comes back */
956 msgcount = ctx->msgcount;
957 deleted = ctx->deleted;
959 if (purge && ctx->deleted) {
960 if (trash_append (ctx) == -1)
964 if (ctx->magic == M_IMAP)
965 rc = imap_sync_mailbox (ctx, purge, index_hint);
967 rc = sync_mailbox (ctx, index_hint);
969 if (ctx->magic == M_IMAP && !purge)
970 mutt_message (_("Mailbox checkpointed."));
972 mutt_message (_("%d kept, %d deleted."), msgcount - deleted, deleted);
976 /* if we haven't deleted any messages, we don't need to resort */
977 /* ... except for certain folder formats which need "unsorted"
978 * sort order in order to synchronize folders.
980 * MH and maildir are safe. mbox-style seems to need re-sorting,
981 * at least with the new threading code.
983 if (purge || (ctx->magic != M_MAILDIR && ctx->magic != M_MH)) {
984 /* IMAP does this automatically after handling EXPUNGE */
985 if (ctx->magic != M_IMAP)
987 mx_update_tables (ctx, 1);
988 mutt_sort_headers (ctx, 1); /* rethread from scratch */
996 int mx_sync_mailbox (CONTEXT* ctx, int* index_hint) {
997 int ret = _mx_sync_mailbox (ctx, index_hint);
998 sidebar_set_buffystats (ctx);
1003 * dest destintation mailbox
1004 * hdr message being copied (required for maildir support, because
1005 * the filename depends on the message flags)
1007 MESSAGE *mx_open_new_message (CONTEXT * dest, HEADER * hdr, int flags)
1010 address_t *p = NULL;
1012 if (!MX_IDX(dest->magic-1)) {
1016 msg = p_new(MESSAGE, 1);
1017 msg->magic = dest->magic;
1021 msg->flags.flagged = hdr->flagged;
1022 msg->flags.replied = hdr->replied;
1023 msg->flags.read = hdr->read;
1024 msg->received = hdr->received;
1027 if (msg->received == 0)
1028 time (&msg->received);
1030 if (mxfmts[dest->magic-1]->mx_open_new_message(msg, dest, hdr) == 0) {
1031 if (msg->magic == M_MBOX && flags & M_ADD_FROM) {
1033 if (hdr->env->return_path)
1034 p = hdr->env->return_path;
1035 else if (hdr->env->sender)
1036 p = hdr->env->sender;
1041 fprintf (msg->fp, "From %s %s", p ? p->mailbox : NONULL(MCore.username),
1042 ctime (&msg->received));
1051 /* check for new mail */
1052 int mx_check_mailbox (CONTEXT * ctx, int *index_hint, int lock) {
1054 return mutt_check_mailbox_compressed (ctx);
1059 if (MX_IDX(ctx->magic-1) && mxfmts[ctx->magic-1]->mx_check_mailbox)
1060 return (mxfmts[ctx->magic-1]->mx_check_mailbox(ctx, index_hint, lock));
1066 /* return a stream pointer for a message */
1067 MESSAGE *mx_open_message (CONTEXT * ctx, int msgno)
1071 msg = p_new(MESSAGE, 1);
1072 switch (msg->magic = ctx->magic) {
1080 HEADER *cur = ctx->hdrs[msgno];
1081 char path[_POSIX_PATH_MAX];
1083 snprintf (path, sizeof (path), "%s/%s", ctx->path, cur->path);
1085 if ((msg->fp = fopen (path, "r")) == NULL && errno == ENOENT &&
1086 ctx->magic == M_MAILDIR)
1087 msg->fp = maildir_open_find_message (ctx->path, cur->path);
1089 if (msg->fp == NULL) {
1098 if (imap_fetch_message (msg, ctx, msgno) != 0)
1105 if (pop_fetch_message (msg, ctx, msgno) != 0)
1113 if (nntp_fetch_message (msg, ctx, msgno) != 0)
1117 #endif /* USE_NNTP */
1126 /* commit a message to a folder */
1128 int mx_commit_message (MESSAGE * msg, CONTEXT * ctx) {
1129 if (!(msg->write && ctx->append)) {
1132 if (!ctx || !MX_IDX(ctx->magic-1) || !mxfmts[ctx->magic-1]->mx_commit_message)
1134 return (mxfmts[ctx->magic-1]->mx_commit_message (msg, ctx));
1137 /* close a pointer to a message */
1138 int mx_close_message (MESSAGE ** msg)
1142 if ((*msg)->magic == M_MH || (*msg)->magic == M_MAILDIR
1143 || (*msg)->magic == M_IMAP
1144 || (*msg)->magic == M_POP
1146 || (*msg)->magic == M_NNTP
1149 r = m_fclose(&(*msg)->fp);
1155 unlink ((*msg)->path);
1156 p_delete(&(*msg)->path);
1163 void mx_alloc_memory (CONTEXT * ctx)
1167 p_realloc(&ctx->hdrs, ctx->hdrmax);
1168 p_realloc(&ctx->v2r, ctx->hdrmax);
1169 p_clear(ctx->hdrs + ctx->msgcount, ctx->hdrmax - ctx->msgcount);
1170 p_clear(ctx->v2r + ctx->msgcount, ctx->hdrmax - ctx->msgcount);
1173 /* this routine is called to update the counts in the context structure for
1174 * the last message header parsed.
1176 void mx_update_context (CONTEXT * ctx, int new_messages)
1181 for (msgno = ctx->msgcount - new_messages; msgno < ctx->msgcount; msgno++) {
1182 h = ctx->hdrs[msgno];
1184 /* NOTE: this _must_ be done before the check for mailcap! */
1185 h->security = crypt_query (h->content);
1187 if (!ctx->pattern) {
1188 ctx->v2r[ctx->vcount] = msgno;
1189 h->virtual = ctx->vcount++;
1195 if (h->env->supersedes) {
1199 ctx->id_hash = mutt_make_id_hash (ctx);
1201 h2 = hash_find (ctx->id_hash, h->env->supersedes);
1203 /* p_delete(&h->env->supersedes); should I ? */
1206 if (!ctx->counting && mod_score.enable)
1207 mutt_score_message (ctx, h2, 1);
1211 /* add this message to the hash tables */
1212 if (ctx->id_hash && h->env->message_id)
1213 hash_insert (ctx->id_hash, h->env->message_id, h);
1214 if (!ctx->counting) {
1215 if (ctx->subj_hash && h->env->real_subj)
1216 hash_insert (ctx->subj_hash, h->env->real_subj, h);
1218 if (mod_score.enable)
1219 mutt_score_message (ctx, h, 0);
1234 /* update sidebar count */
1235 sidebar_set_buffystats (ctx);
1240 * 1 if the specified mailbox contains 0 messages.
1241 * 0 if the mailbox contains messages
1244 int mx_check_empty (const char *path)
1247 if ((i = mx_get_idx (path)) >= 0 && mxfmts[i]->mx_check_empty)
1248 return (mxfmts[i]->mx_check_empty(path));
1253 int mx_acl_check(CONTEXT *ctx, int flag)
1255 if (!mxfmts[ctx->magic-1]->mx_acl_check)
1257 return mxfmts[ctx->magic-1]->mx_acl_check(ctx,flag);
1260 void mutt_parse_mime_message (CONTEXT * ctx, HEADER * cur)
1266 if (cur->content->type != TYPEMESSAGE
1267 && cur->content->type != TYPEMULTIPART)
1268 break; /* nothing to do */
1270 if (cur->content->parts)
1271 break; /* The message was parsed earlier. */
1273 if ((msg = mx_open_message (ctx, cur->msgno))) {
1274 mutt_parse_part (msg->fp, cur->content);
1276 cur->security = crypt_query (cur->content);
1278 mx_close_message (&msg);
1281 mutt_count_body_parts (cur, flags | M_PARTS_RECOUNT);