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-lua/lib-lua.h>
16 #include <lib-sys/unix.h>
17 #include <lib-mime/mime.h>
18 #include <lib-ui/sidebar.h>
34 #include <imap/imap.h>
38 #include <nntp/nntp.h>
41 static mx_t const *mxfmts[] = {
53 #define MX_IDX(idx) (idx >= 0 && idx < countof(mxfmts))
54 #define mutt_is_spool(s) (m_strcmp(Spoolfile, s) == 0)
58 * retry - should retry if unable to lock?
60 static int invoke_dotlock (const char *path, int flags, int retry)
62 char cmd[LONG_STRING + _POSIX_PATH_MAX];
63 char f[STRING + _POSIX_PATH_MAX];
66 if (flags & DL_FL_RETRY)
67 snprintf (r, sizeof (r), "-r %d ", retry ? MAXLOCKATTEMPT : 0);
69 mutt_quote_filename (f, sizeof (f), path);
71 snprintf(cmd, sizeof(cmd), "%s %s%s%s%s%s%s%s",
73 flags & DL_FL_TRY ? "-t " : "",
74 flags & DL_FL_UNLOCK ? "-u " : "",
75 flags & DL_FL_USEPRIV ? "-p " : "",
76 flags & DL_FL_FORCE ? "-f " : "",
77 flags & DL_FL_UNLINK ? "-d " : "",
78 flags & DL_FL_RETRY ? r : "", f);
80 return mutt_system (cmd);
83 static int dotlock_file (const char *path, int retry)
86 int flags = DL_FL_USEPRIV | DL_FL_RETRY;
92 if ((r = invoke_dotlock (path, flags, retry)) == DL_EX_EXIST) {
93 if (!option (OPTNOCURSES)) {
94 char msg[LONG_STRING];
96 snprintf (msg, sizeof (msg),
97 _("Lock count exceeded, remove lock for %s?"), path);
98 if (retry && mutt_yesorno (msg, M_YES) == M_YES) {
106 mutt_error (_("Can't dotlock %s.\n"), path);
109 return (r == DL_EX_OK ? 0 : -1);
112 static int undotlock_file (const char *path)
114 return (invoke_dotlock (path, DL_FL_USEPRIV | DL_FL_UNLOCK, 0) ==
118 /* looks up index of type for path in mxfmts */
119 static int mx_get_idx (const char* path) {
123 /* first, test all non-local folders to avoid stat() call */
124 for (i = 0; i < countof(mxfmts); i++) {
125 if (!mxfmts[i]->local)
126 t = mxfmts[i]->mx_is_magic(path, NULL);
130 if (stat (path, &st) == 0) {
131 /* if stat() succeeded, keep testing until success and
132 * pass stat() info so that we only need to do it once */
133 for (i = 0; i < countof(mxfmts); i++) {
134 if (mxfmts[i]->local)
135 t = mxfmts[i]->mx_is_magic(path, &st);
144 * excl if excl != 0, request an exclusive lock
145 * dot if dot != 0, try to dotlock the file
146 * time_out should retry locking?
148 int mx_lock_file (const char *path, int fd, int excl, int dot, int time_out)
150 #if defined (USE_FCNTL) || defined (USE_FLOCK)
162 lck.l_type = excl ? F_WRLCK : F_RDLCK;
163 lck.l_whence = SEEK_SET;
168 while (fcntl (fd, F_SETLK, &lck) == -1) {
171 if (errno != EAGAIN && errno != EACCES) {
172 mutt_perror ("fcntl");
176 if (fstat (fd, &sb) != 0)
182 /* only unlock file if it is unchanged */
183 if (prev_sb.st_size == sb.st_size
184 && ++count >= (time_out ? MAXLOCKATTEMPT : 0)) {
186 mutt_error _("Timeout exceeded while attempting fcntl lock!");
193 mutt_message (_("Waiting for fcntl lock... %d"), ++attempt);
196 #endif /* USE_FCNTL */
201 while (flock (fd, (excl ? LOCK_EX : LOCK_SH) | LOCK_NB) == -1) {
204 if (errno != EWOULDBLOCK) {
205 mutt_perror ("flock");
210 if (fstat (fd, &sb) != 0)
216 /* only unlock file if it is unchanged */
217 if (prev_sb.st_size == sb.st_size
218 && ++count >= (time_out ? MAXLOCKATTEMPT : 0)) {
220 mutt_error _("Timeout exceeded while attempting flock lock!");
228 mutt_message (_("Waiting for flock attempt... %d"), ++attempt);
231 #endif /* USE_FLOCK */
234 r = dotlock_file (path, time_out);
237 /* release any other locks obtained in this routine */
240 lck.l_type = F_UNLCK;
241 fcntl (fd, F_SETLK, &lck);
242 #endif /* USE_FCNTL */
246 #endif /* USE_FLOCK */
254 int mx_unlock_file (const char *path, int fd, int dot)
257 struct flock unlockit;
259 p_clear(&unlockit, 1);
260 unlockit.l_type = F_UNLCK;
261 unlockit.l_whence = SEEK_SET;
262 fcntl (fd, F_SETLK, &unlockit);
270 undotlock_file (path);
275 /* try to figure out what type of mailbox ``path'' is */
276 int mx_get_magic (const char *path) {
279 if (m_strlen(path) == 0)
281 if ((i = mx_get_idx (path)) >= 0)
282 return (mxfmts[i]->type);
286 int mx_is_local (int m) {
289 return (mxfmts[m]->local);
293 * set DefaultMagic to the given value
295 int mx_set_magic (const char *s)
297 if (ascii_strcasecmp (s, "mbox") == 0)
298 DefaultMagic = M_MBOX;
299 else if (ascii_strcasecmp (s, "mh") == 0)
301 else if (ascii_strcasecmp (s, "maildir") == 0)
302 DefaultMagic = M_MAILDIR;
309 /* mx_access: Wrapper for access, checks permissions on a given mailbox.
310 * We may be interested in using ACL-style flags at some point, currently
311 * we use the normal access() flags. */
312 int mx_access (const char *path, int flags)
316 if ((i = mx_get_idx (path)) >= 0 && mxfmts[i]->mx_access)
317 return (mxfmts[i]->mx_access(path,flags));
321 static int mx_open_mailbox_append (CONTEXT * ctx, int flags)
325 /* special case for appending to compressed folders -
326 * even if we can not open them for reading */
327 if (mutt_can_append_compressed (ctx->path))
328 mutt_open_append_compressed (ctx);
332 if (mx_get_magic (ctx->path) == M_IMAP)
333 return imap_open_mailbox_append (ctx);
335 if (stat (ctx->path, &sb) == 0) {
336 ctx->magic = mx_get_magic (ctx->path);
338 switch (ctx->magic) {
340 mutt_error (_("%s is not a mailbox."), ctx->path);
346 else if (errno == ENOENT) {
347 ctx->magic = DefaultMagic;
349 if (ctx->magic == M_MH || ctx->magic == M_MAILDIR) {
350 char tmp[_POSIX_PATH_MAX];
352 if (mkdir (ctx->path, S_IRWXU)) {
353 mutt_perror (ctx->path);
357 if (ctx->magic == M_MAILDIR) {
358 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
359 if (mkdir (tmp, S_IRWXU)) {
365 snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
366 if (mkdir (tmp, S_IRWXU)) {
368 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
373 snprintf (tmp, sizeof (tmp), "%s/tmp", ctx->path);
374 if (mkdir (tmp, S_IRWXU)) {
376 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
378 snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
387 snprintf (tmp, sizeof (tmp), "%s/.mh_sequences", ctx->path);
388 if ((i = creat (tmp, S_IRWXU)) == -1) {
398 mutt_perror (ctx->path);
402 switch (ctx->magic) {
405 safe_fopen (ctx->path, flags & M_NEWFOLDER ? "w" : "a")) == NULL
406 || mbox_lock_mailbox (ctx, 1, 1) != 0) {
408 mutt_perror (ctx->path);
410 mutt_error (_("Couldn't lock %s\n"), ctx->path);
415 fseeko (ctx->fp, 0, 2);
431 * open a mailbox and parse it
434 * flags M_NOSORT do not sort mailbox
435 * M_APPEND open mailbox for appending
436 * M_READONLY open mailbox in read-only mode
437 * M_QUIET only print error messages
438 * ctx if non-null, context struct to use
440 CONTEXT *mx_open_mailbox (const char *path, int flags, CONTEXT * pctx)
446 ctx = p_new(CONTEXT, 1);
448 ctx->path = m_strdup(path);
450 ctx->msgnotreadyet = -1;
455 if (flags & M_READONLY)
460 if (flags & (M_APPEND | M_NEWFOLDER)) {
461 if (mx_open_mailbox_append (ctx, flags) != 0) {
462 mx_fastclose_mailbox (ctx);
470 if (!MX_IDX(ctx->magic-1))
471 ctx->magic = mx_get_magic (path);
473 if (ctx->magic == M_COMPRESSED)
474 mutt_open_read_compressed (ctx);
477 mutt_error (_("%s is not a mailbox."), path);
479 if (ctx->magic == -1)
482 if (ctx->magic <= 0) {
483 mx_fastclose_mailbox (ctx);
489 /* if the user has a `push' command in their .muttrc, or in a folder-hook,
490 * it will cause the progress messages not to be displayed because
491 * mutt_refresh() will think we are in the middle of a macro. so set a
492 * flag to indicate that we should really refresh the screen.
494 set_option (OPTFORCEREFRESH);
497 mutt_message (_("Reading %s..."), ctx->path);
499 rc = mxfmts[ctx->magic-1]->mx_open_mailbox(ctx);
502 if ((flags & M_NOSORT) == 0) {
503 /* avoid unnecessary work since the mailbox is completely unthreaded
505 unset_option (OPTSORTSUBTHREADS);
506 unset_option (OPTNEEDRESCORE);
507 mutt_sort_headers (ctx, 1);
513 mx_fastclose_mailbox (ctx);
518 unset_option (OPTFORCEREFRESH);
522 /* free up memory associated with the mailbox context */
523 void mx_fastclose_mailbox (CONTEXT * ctx)
530 if (MX_IDX(ctx->magic-1) && mxfmts[ctx->magic-1]->mx_fastclose_mailbox)
531 mxfmts[ctx->magic-1]->mx_fastclose_mailbox(ctx);
533 hash_delete (&ctx->subj_hash, NULL);
535 hash_delete (&ctx->id_hash, NULL);
536 mutt_clear_threads (ctx);
537 for (i = 0; i < ctx->msgcount; i++)
538 header_delete(&ctx->hdrs[i]);
539 p_delete(&ctx->hdrs);
543 mutt_fast_close_compressed (ctx);
545 p_delete(&ctx->path);
546 p_delete(&ctx->pattern);
547 pattern_list_wipe(&ctx->limit_pattern);
552 /* save changes to disk */
553 static int sync_mailbox (CONTEXT * ctx, int *index_hint)
558 mutt_message (_("Writing %s..."), ctx->path);
560 if (MX_IDX(ctx->magic-1))
561 /* the 1 is only of interest for IMAP and means EXPUNGE */
562 rc = mxfmts[ctx->magic-1]->mx_sync_mailbox(ctx,1,index_hint);
564 if (rc == 0 && ctx->cinfo)
565 return mutt_sync_compressed (ctx);
570 /* move deleted mails to the trash folder */
571 static int trash_append (CONTEXT * ctx)
577 if (!TrashPath || !ctx->deleted ||
578 (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))
581 for (; i < ctx->msgcount && (!ctx->hdrs[i]->deleted ||
582 ctx->hdrs[i]->appended); i++);
583 if (i == ctx->msgcount)
584 return 0; /* nothing to be done */
586 if (mutt_save_confirm (TrashPath, &st) != 0) {
587 mutt_error _("message(s) not deleted");
592 if (lstat (ctx->path, &stc) == 0 && stc.st_ino == st.st_ino
593 && stc.st_dev == st.st_dev && stc.st_rdev == st.st_rdev)
594 return 0; /* we are in the trash folder: simple sync */
596 if ((ctx_trash = mx_open_mailbox (TrashPath, M_APPEND, NULL)) != NULL) {
597 for (i = 0; i < ctx->msgcount; i++)
598 if (ctx->hdrs[i]->deleted && !ctx->hdrs[i]->appended
599 && !ctx->hdrs[i]->purged
600 && mutt_append_message (ctx_trash, ctx, ctx->hdrs[i], 0, 0) == -1) {
601 mx_close_mailbox (ctx_trash, NULL);
605 mx_close_mailbox (ctx_trash, NULL);
608 mutt_error _("Can't open trash folder");
616 /* save changes and close mailbox */
617 static int _mx_close_mailbox (CONTEXT * ctx, int *index_hint)
619 int i, move_messages = 0, purge = 1, read_msgs = 0;
623 char mbox[_POSIX_PATH_MAX];
632 if (ctx->magic == M_NNTP) {
635 ret = nntp_close_mailbox (ctx);
636 mx_fastclose_mailbox (ctx);
640 if (ctx->readonly || ctx->dontwrite) {
641 /* mailbox is readonly or we don't want to write */
642 mx_fastclose_mailbox (ctx);
647 /* mailbox was opened in write-mode */
648 if (ctx->magic == M_MBOX)
649 mbox_close_mailbox (ctx);
651 mx_fastclose_mailbox (ctx);
655 for (i = 0; i < ctx->msgcount; i++) {
656 if (!ctx->hdrs[i]->deleted && ctx->hdrs[i]->read
657 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
661 if (read_msgs && quadoption (OPT_MOVE) != M_NO) {
664 if ((p = mutt_find_hook (M_MBOXHOOK, ctx->path))) {
666 m_strcpy(mbox, sizeof(mbox), p);
669 m_strcpy(mbox, sizeof(mbox), NONULL(Inbox));
670 isSpool = mutt_is_spool (ctx->path) && !mutt_is_spool (mbox);
672 mutt_expand_path (mbox, sizeof (mbox));
675 snprintf (buf, sizeof (buf), _("Move read messages to %s?"), mbox);
676 if ((move_messages = query_quadoption (OPT_MOVE, buf)) == -1) {
684 * There is no point in asking whether or not to purge if we are
685 * just marking messages as "trash".
687 if (ctx->deleted && !(ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH))) {
688 snprintf (buf, sizeof (buf), ctx->deleted == 1
689 ? _("Purge %d deleted message?") :
690 _("Purge %d deleted messages?"), ctx->deleted);
691 if ((purge = query_quadoption (OPT_DELETE, buf)) < 0) {
697 /* IMAP servers manage the OLD flag themselves */
698 if (ctx->magic != M_IMAP)
699 if (option (OPTMARKOLD)) {
700 for (i = 0; i < ctx->msgcount; i++) {
701 if (!ctx->hdrs[i]->deleted && !ctx->hdrs[i]->old)
702 mutt_set_flag (ctx, ctx->hdrs[i], M_OLD, 1);
707 mutt_message (_("Moving read messages to %s..."), mbox);
709 /* try to use server-side copy first */
712 if (ctx->magic == M_IMAP && imap_is_magic (mbox, NULL) == M_IMAP) {
713 /* tag messages for moving, and clear old tags, if any */
714 for (i = 0; i < ctx->msgcount; i++)
715 if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted
716 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
717 ctx->hdrs[i]->tagged = 1;
719 ctx->hdrs[i]->tagged = 0;
721 i = imap_copy_messages (ctx, NULL, mbox, 1);
724 if (i == 0) /* success */
726 else if (i == -1) { /* horrible error, bail */
730 else /* use regular append-copy mode */
732 if (mx_open_mailbox (mbox, M_APPEND, &f) == NULL) {
737 for (i = 0; i < ctx->msgcount; i++) {
738 if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted
739 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED))) {
740 if (mutt_append_message (&f, ctx, ctx->hdrs[i], 0, CH_UPDATE_LEN) ==
742 mutt_set_flag (ctx, ctx->hdrs[i], M_DELETE, 1);
743 mutt_set_flag (ctx, ctx->hdrs[i], M_APPENDED, 1);
746 mx_close_mailbox (&f, NULL);
753 mx_close_mailbox (&f, NULL);
757 else if (!ctx->changed && ctx->deleted == 0) {
758 mutt_message _("Mailbox is unchanged.");
760 mx_fastclose_mailbox (ctx);
764 /* copy mails to the trash before expunging */
765 if (purge && ctx->deleted)
766 if (trash_append (ctx) != 0) {
771 /* allow IMAP to preserve the deleted flag across sessions */
772 if (ctx->magic == M_IMAP) {
773 if ((check = imap_sync_mailbox (ctx, purge, index_hint)) != 0) {
781 for (i = 0; i < ctx->msgcount; i++)
782 ctx->hdrs[i]->deleted = 0;
786 if (ctx->changed || ctx->deleted) {
787 if ((check = sync_mailbox (ctx, index_hint)) != 0) {
795 mutt_message (_("%d kept, %d moved, %d deleted."),
796 ctx->msgcount - ctx->deleted, read_msgs, ctx->deleted);
798 mutt_message (_("%d kept, %d deleted."),
799 ctx->msgcount - ctx->deleted, ctx->deleted);
801 if (ctx->cinfo && mutt_slow_close_compressed (ctx))
804 mx_fastclose_mailbox (ctx);
809 int mx_close_mailbox (CONTEXT * ctx, int *index_hint) {
813 ret = _mx_close_mailbox (ctx, index_hint);
814 sidebar_set_buffystats (ctx);
818 /* update a Context structure's internal tables. */
820 void mx_update_tables (CONTEXT * ctx, int committing)
824 /* update memory to reflect the new state of the mailbox */
833 #define this_body ctx->hdrs[j]->content
834 for (i = 0, j = 0; i < ctx->msgcount; i++) {
835 if ((committing && (!ctx->hdrs[i]->deleted ||
836 (ctx->magic == M_MAILDIR
837 && option (OPTMAILDIRTRASH)))) || (!committing
841 ctx->hdrs[j] = ctx->hdrs[i];
844 ctx->hdrs[j]->msgno = j;
845 if (ctx->hdrs[j]->virtual != -1) {
846 ctx->v2r[ctx->vcount] = j;
847 ctx->hdrs[j]->virtual = ctx->vcount++;
848 ctx->vsize += this_body->length + this_body->offset -
849 this_body->hdr_offset;
853 ctx->hdrs[j]->changed = 0;
854 else if (ctx->hdrs[j]->changed)
858 || (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH))) {
859 if (ctx->hdrs[j]->deleted)
863 if (ctx->hdrs[j]->tagged)
865 if (ctx->hdrs[j]->flagged)
867 if (!ctx->hdrs[j]->read) {
869 if (!ctx->hdrs[j]->old)
876 if (ctx->magic == M_MH || ctx->magic == M_MAILDIR)
877 ctx->size -= (ctx->hdrs[i]->content->length +
878 ctx->hdrs[i]->content->offset -
879 ctx->hdrs[i]->content->hdr_offset);
880 /* remove message from the hash tables */
881 if (ctx->subj_hash && ctx->hdrs[i]->env->real_subj)
882 hash_remove (ctx->subj_hash, ctx->hdrs[i]->env->real_subj,
884 if (ctx->id_hash && ctx->hdrs[i]->env->message_id)
885 hash_remove (ctx->id_hash, ctx->hdrs[i]->env->message_id,
887 header_delete(&ctx->hdrs[i]);
893 /* update sidebar count */
894 sidebar_set_buffystats (ctx);
898 /* save changes to mailbox
904 static int _mx_sync_mailbox (CONTEXT * ctx, int *index_hint)
908 int msgcount, deleted;
910 if (ctx->dontwrite) {
911 char buf[STRING], tmp[STRING];
913 if (km_expand_key (buf, sizeof (buf),
914 km_find_func (MENU_MAIN, OP_TOGGLE_WRITE)))
915 snprintf (tmp, sizeof (tmp), _(" Press '%s' to toggle write"), buf);
917 m_strcpy(tmp, sizeof(tmp), _("Use 'toggle-write' to re-enable write!"));
919 mutt_error (_("Mailbox is marked unwritable. %s"), tmp);
922 else if (ctx->readonly) {
923 mutt_error _("Mailbox is read-only.");
928 if (!ctx->changed && !ctx->deleted) {
929 mutt_message _("Mailbox is unchanged.");
937 snprintf (buf, sizeof (buf), ctx->deleted == 1
938 ? _("Purge %d deleted message?") :
939 _("Purge %d deleted messages?"), ctx->deleted);
940 if ((purge = query_quadoption (OPT_DELETE, buf)) < 0)
942 else if (purge == M_NO) {
944 return 0; /* nothing to do! */
945 /* let IMAP servers hold on to D flags */
946 if (ctx->magic != M_IMAP)
948 for (i = 0; i < ctx->msgcount; i++)
949 ctx->hdrs[i]->deleted = 0;
953 else if (ctx->last_tag && ctx->last_tag->deleted)
954 ctx->last_tag = NULL; /* reset last tagged msg now useless */
957 /* really only for IMAP - imap_sync_mailbox results in a call to
958 * mx_update_tables, so ctx->deleted is 0 when it comes back */
959 msgcount = ctx->msgcount;
960 deleted = ctx->deleted;
962 if (purge && ctx->deleted) {
963 if (trash_append (ctx) == -1)
967 if (ctx->magic == M_IMAP)
968 rc = imap_sync_mailbox (ctx, purge, index_hint);
970 rc = sync_mailbox (ctx, index_hint);
972 if (ctx->magic == M_IMAP && !purge)
973 mutt_message (_("Mailbox checkpointed."));
975 mutt_message (_("%d kept, %d deleted."), msgcount - deleted, deleted);
979 /* if we haven't deleted any messages, we don't need to resort */
980 /* ... except for certain folder formats which need "unsorted"
981 * sort order in order to synchronize folders.
983 * MH and maildir are safe. mbox-style seems to need re-sorting,
984 * at least with the new threading code.
986 if (purge || (ctx->magic != M_MAILDIR && ctx->magic != M_MH)) {
987 /* IMAP does this automatically after handling EXPUNGE */
988 if (ctx->magic != M_IMAP)
990 mx_update_tables (ctx, 1);
991 mutt_sort_headers (ctx, 1); /* rethread from scratch */
999 int mx_sync_mailbox (CONTEXT* ctx, int* index_hint) {
1000 int ret = _mx_sync_mailbox (ctx, index_hint);
1001 sidebar_set_buffystats (ctx);
1006 * dest destintation mailbox
1007 * hdr message being copied (required for maildir support, because
1008 * the filename depends on the message flags)
1010 MESSAGE *mx_open_new_message (CONTEXT * dest, HEADER * hdr, int flags)
1013 address_t *p = NULL;
1015 if (!MX_IDX(dest->magic-1)) {
1019 msg = p_new(MESSAGE, 1);
1020 msg->magic = dest->magic;
1024 msg->flags.flagged = hdr->flagged;
1025 msg->flags.replied = hdr->replied;
1026 msg->flags.read = hdr->read;
1027 msg->received = hdr->received;
1030 if (msg->received == 0)
1031 time (&msg->received);
1033 if (mxfmts[dest->magic-1]->mx_open_new_message(msg, dest, hdr) == 0) {
1034 if (msg->magic == M_MBOX && flags & M_ADD_FROM) {
1036 if (hdr->env->return_path)
1037 p = hdr->env->return_path;
1038 else if (hdr->env->sender)
1039 p = hdr->env->sender;
1044 fprintf (msg->fp, "From %s %s", p ? p->mailbox : NONULL(MCore.username),
1045 ctime (&msg->received));
1054 /* check for new mail */
1055 int mx_check_mailbox (CONTEXT * ctx, int *index_hint, int lock) {
1057 return mutt_check_mailbox_compressed (ctx);
1062 if (MX_IDX(ctx->magic-1) && mxfmts[ctx->magic-1]->mx_check_mailbox)
1063 return (mxfmts[ctx->magic-1]->mx_check_mailbox(ctx, index_hint, lock));
1069 /* return a stream pointer for a message */
1070 MESSAGE *mx_open_message (CONTEXT * ctx, int msgno)
1074 msg = p_new(MESSAGE, 1);
1075 switch (msg->magic = ctx->magic) {
1083 HEADER *cur = ctx->hdrs[msgno];
1084 char path[_POSIX_PATH_MAX];
1086 snprintf (path, sizeof (path), "%s/%s", ctx->path, cur->path);
1088 if ((msg->fp = fopen (path, "r")) == NULL && errno == ENOENT &&
1089 ctx->magic == M_MAILDIR)
1090 msg->fp = maildir_open_find_message (ctx->path, cur->path);
1092 if (msg->fp == NULL) {
1101 if (imap_fetch_message (msg, ctx, msgno) != 0)
1108 if (pop_fetch_message (msg, ctx, msgno) != 0)
1116 if (nntp_fetch_message (msg, ctx, msgno) != 0)
1120 #endif /* USE_NNTP */
1129 /* commit a message to a folder */
1131 int mx_commit_message (MESSAGE * msg, CONTEXT * ctx) {
1132 if (!(msg->write && ctx->append)) {
1135 if (!ctx || !MX_IDX(ctx->magic-1) || !mxfmts[ctx->magic-1]->mx_commit_message)
1137 return (mxfmts[ctx->magic-1]->mx_commit_message (msg, ctx));
1140 /* close a pointer to a message */
1141 int mx_close_message (MESSAGE ** msg)
1145 if ((*msg)->magic == M_MH || (*msg)->magic == M_MAILDIR
1146 || (*msg)->magic == M_IMAP
1147 || (*msg)->magic == M_POP
1149 || (*msg)->magic == M_NNTP
1152 r = m_fclose(&(*msg)->fp);
1158 unlink ((*msg)->path);
1159 p_delete(&(*msg)->path);
1166 void mx_alloc_memory (CONTEXT * ctx)
1170 p_realloc(&ctx->hdrs, ctx->hdrmax);
1171 p_realloc(&ctx->v2r, ctx->hdrmax);
1172 p_clear(ctx->hdrs + ctx->msgcount, ctx->hdrmax - ctx->msgcount);
1173 p_clear(ctx->v2r + ctx->msgcount, ctx->hdrmax - ctx->msgcount);
1176 /* this routine is called to update the counts in the context structure for
1177 * the last message header parsed.
1179 void mx_update_context (CONTEXT * ctx, int new_messages)
1184 for (msgno = ctx->msgcount - new_messages; msgno < ctx->msgcount; msgno++) {
1185 h = ctx->hdrs[msgno];
1187 /* NOTE: this _must_ be done before the check for mailcap! */
1188 h->security = crypt_query (h->content);
1190 if (!ctx->pattern) {
1191 ctx->v2r[ctx->vcount] = msgno;
1192 h->virtual = ctx->vcount++;
1198 if (h->env->supersedes) {
1202 ctx->id_hash = mutt_make_id_hash (ctx);
1204 h2 = hash_find (ctx->id_hash, h->env->supersedes);
1206 /* p_delete(&h->env->supersedes); should I ? */
1209 if (!ctx->counting && option (OPTSCORE))
1210 mutt_score_message (ctx, h2, 1);
1214 /* add this message to the hash tables */
1215 if (ctx->id_hash && h->env->message_id)
1216 hash_insert (ctx->id_hash, h->env->message_id, h);
1217 if (!ctx->counting) {
1218 if (ctx->subj_hash && h->env->real_subj)
1219 hash_insert (ctx->subj_hash, h->env->real_subj, h);
1221 if (option (OPTSCORE))
1222 mutt_score_message (ctx, h, 0);
1237 /* update sidebar count */
1238 sidebar_set_buffystats (ctx);
1243 * 1 if the specified mailbox contains 0 messages.
1244 * 0 if the mailbox contains messages
1247 int mx_check_empty (const char *path)
1250 if ((i = mx_get_idx (path)) >= 0 && mxfmts[i]->mx_check_empty)
1251 return (mxfmts[i]->mx_check_empty(path));
1256 int mx_acl_check(CONTEXT *ctx, int flag)
1258 if (!mxfmts[ctx->magic-1]->mx_acl_check)
1260 return mxfmts[ctx->magic-1]->mx_acl_check(ctx,flag);
1263 void mutt_parse_mime_message (CONTEXT * ctx, HEADER * cur)
1269 if (cur->content->type != TYPEMESSAGE
1270 && cur->content->type != TYPEMULTIPART)
1271 break; /* nothing to do */
1273 if (cur->content->parts)
1274 break; /* The message was parsed earlier. */
1276 if ((msg = mx_open_message (ctx, cur->msgno))) {
1277 mutt_parse_part (msg->fp, cur->content);
1279 cur->security = crypt_query (cur->content);
1281 mx_close_message (&msg);
1284 mutt_count_body_parts (cur, flags | M_PARTS_RECOUNT);