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/lib-ui.h>
33 #include <imap/imap.h>
36 static mx_t const *mxfmts[] = {
45 #define MX_IDX(idx) (idx >= 0 && idx < countof(mxfmts))
46 #define mutt_is_spool(s) (m_strcmp(Spoolfile, s) == 0)
48 static int dotlock_file(const char *path, int retry)
50 char lockfile[_POSIX_PATH_MAX];
51 snprintf(lockfile, sizeof(lockfile), "%s.lock", path);
53 if (lockfile_create(lockfile, retry ? 1 : 0, 0)) {
55 mutt_error (_("Can't dotlock %s.\n"), lockfile);
60 static int undotlock_file (const char *path)
62 char lockfile[_POSIX_PATH_MAX];
63 snprintf(lockfile, sizeof(lockfile), "%s.lock", path);
64 return lockfile_remove(lockfile);
67 /* looks up index of type for path in mxfmts */
68 static int mx_get_idx (const char* path) {
72 /* first, test all non-local folders to avoid stat() call */
73 for (i = 0; i < countof(mxfmts); i++) {
74 if (!mxfmts[i]->local)
75 t = mxfmts[i]->mx_is_magic(path, NULL);
79 if (stat (path, &st) == 0) {
80 /* if stat() succeeded, keep testing until success and
81 * pass stat() info so that we only need to do it once */
82 for (i = 0; i < countof(mxfmts); i++) {
84 t = mxfmts[i]->mx_is_magic(path, &st);
93 * excl if excl != 0, request an exclusive lock
94 * dot if dot != 0, try to dotlock the file
95 * time_out should retry locking?
97 int mx_lock_file (const char *path, int fd, int excl, int dot, int time_out)
99 #if defined (USE_FCNTL) || defined (USE_FLOCK)
110 lck.l_type = excl ? F_WRLCK : F_RDLCK;
111 lck.l_whence = SEEK_SET;
116 while (fcntl (fd, F_SETLK, &lck) == -1) {
119 if (errno != EAGAIN && errno != EACCES) {
120 mutt_perror ("fcntl");
124 if (fstat (fd, &sb) != 0)
130 /* only unlock file if it is unchanged */
131 if (prev_sb.st_size == sb.st_size
132 && ++count >= (time_out ? MAXLOCKATTEMPT : 0)) {
134 mutt_error _("Timeout exceeded while attempting fcntl lock!");
141 mutt_message (_("Waiting for fcntl lock... %d"), ++attempt);
144 #endif /* USE_FCNTL */
149 while (flock (fd, (excl ? LOCK_EX : LOCK_SH) | LOCK_NB) == -1) {
152 if (errno != EWOULDBLOCK) {
153 mutt_perror ("flock");
158 if (fstat (fd, &sb) != 0)
164 /* only unlock file if it is unchanged */
165 if (prev_sb.st_size == sb.st_size
166 && ++count >= (time_out ? MAXLOCKATTEMPT : 0)) {
168 mutt_error _("Timeout exceeded while attempting flock lock!");
176 mutt_message (_("Waiting for flock attempt... %d"), ++attempt);
179 #endif /* USE_FLOCK */
182 r = dotlock_file(path, time_out);
185 /* release any other locks obtained in this routine */
188 lck.l_type = F_UNLCK;
189 fcntl (fd, F_SETLK, &lck);
190 #endif /* USE_FCNTL */
194 #endif /* USE_FLOCK */
202 int mx_unlock_file (const char *path, int fd, int dot)
205 struct flock unlockit;
207 p_clear(&unlockit, 1);
208 unlockit.l_type = F_UNLCK;
209 unlockit.l_whence = SEEK_SET;
210 fcntl (fd, F_SETLK, &unlockit);
218 undotlock_file (path);
223 /* try to figure out what type of mailbox ``path'' is */
224 int mx_get_magic (const char *path) {
227 if (m_strlen(path) == 0)
229 if ((i = mx_get_idx (path)) >= 0)
230 return (mxfmts[i]->type);
234 int mx_is_local (int m) {
237 return (mxfmts[m]->local);
241 * set DefaultMagic to the given value
243 int mx_set_magic (const char *s)
245 if (ascii_strcasecmp (s, "mbox") == 0)
246 DefaultMagic = M_MBOX;
247 else if (ascii_strcasecmp (s, "mh") == 0)
249 else if (ascii_strcasecmp (s, "maildir") == 0)
250 DefaultMagic = M_MAILDIR;
257 /* mx_access: Wrapper for access, checks permissions on a given mailbox.
258 * We may be interested in using ACL-style flags at some point, currently
259 * we use the normal access() flags. */
260 int mx_access (const char *path, int flags)
264 if ((i = mx_get_idx (path)) >= 0 && mxfmts[i]->mx_access)
265 return (mxfmts[i]->mx_access(path,flags));
269 static int mx_open_mailbox_append (CONTEXT * ctx, int flags)
273 /* special case for appending to compressed folders -
274 * even if we can not open them for reading */
275 if (mutt_can_append_compressed (ctx->path))
276 mutt_open_append_compressed (ctx);
280 if (mx_get_magic (ctx->path) == M_IMAP)
281 return imap_open_mailbox_append (ctx);
283 if (stat (ctx->path, &sb) == 0) {
284 ctx->magic = mx_get_magic (ctx->path);
286 switch (ctx->magic) {
288 mutt_error (_("%s is not a mailbox."), ctx->path);
294 else if (errno == ENOENT) {
295 ctx->magic = DefaultMagic;
297 if (ctx->magic == M_MH || ctx->magic == M_MAILDIR) {
298 char tmp[_POSIX_PATH_MAX];
300 if (mkdir (ctx->path, S_IRWXU)) {
301 mutt_perror (ctx->path);
305 if (ctx->magic == M_MAILDIR) {
306 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
307 if (mkdir (tmp, S_IRWXU)) {
313 snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
314 if (mkdir (tmp, S_IRWXU)) {
316 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
321 snprintf (tmp, sizeof (tmp), "%s/tmp", ctx->path);
322 if (mkdir (tmp, S_IRWXU)) {
324 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
326 snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
335 snprintf (tmp, sizeof (tmp), "%s/.mh_sequences", ctx->path);
336 if ((i = creat (tmp, S_IRWXU)) == -1) {
346 mutt_perror (ctx->path);
350 switch (ctx->magic) {
353 safe_fopen (ctx->path, flags & M_NEWFOLDER ? "w" : "a")) == NULL
354 || mbox_lock_mailbox (ctx, 1, 1) != 0) {
356 mutt_perror (ctx->path);
358 mutt_error (_("Couldn't lock %s\n"), ctx->path);
363 fseeko (ctx->fp, 0, 2);
379 * open a mailbox and parse it
382 * flags M_NOSORT do not sort mailbox
383 * M_APPEND open mailbox for appending
384 * M_READONLY open mailbox in read-only mode
385 * M_QUIET only print error messages
386 * ctx if non-null, context struct to use
388 CONTEXT *mx_open_mailbox (const char *path, int flags, CONTEXT * pctx)
394 ctx = p_new(CONTEXT, 1);
396 ctx->path = m_strdup(path);
398 ctx->msgnotreadyet = -1;
403 if (flags & M_READONLY)
408 if (flags & (M_APPEND | M_NEWFOLDER)) {
409 if (mx_open_mailbox_append (ctx, flags) != 0) {
410 mx_fastclose_mailbox (ctx);
418 if (!MX_IDX(ctx->magic-1))
419 ctx->magic = mx_get_magic (path);
421 if (ctx->magic == M_COMPRESSED)
422 mutt_open_read_compressed (ctx);
425 mutt_error (_("%s is not a mailbox."), path);
427 if (ctx->magic == -1)
430 if (ctx->magic <= 0) {
431 mx_fastclose_mailbox (ctx);
437 /* if the user has a `push' command in their .muttrc, or in a folder-hook,
438 * it will cause the progress messages not to be displayed because
439 * mutt_refresh() will think we are in the middle of a macro. so set a
440 * flag to indicate that we should really refresh the screen.
442 set_option (OPTFORCEREFRESH);
445 mutt_message (_("Reading %s..."), ctx->path);
447 rc = mxfmts[ctx->magic-1]->mx_open_mailbox(ctx);
450 if ((flags & M_NOSORT) == 0) {
451 /* avoid unnecessary work since the mailbox is completely unthreaded
453 unset_option (OPTSORTSUBTHREADS);
454 unset_option (OPTNEEDRESCORE);
455 mutt_sort_headers (ctx, 1);
461 mx_fastclose_mailbox (ctx);
466 unset_option (OPTFORCEREFRESH);
470 /* free up memory associated with the mailbox context */
471 void mx_fastclose_mailbox (CONTEXT * ctx)
478 if (MX_IDX(ctx->magic-1) && mxfmts[ctx->magic-1]->mx_fastclose_mailbox)
479 mxfmts[ctx->magic-1]->mx_fastclose_mailbox(ctx);
481 hash_delete (&ctx->subj_hash, NULL);
483 hash_delete (&ctx->id_hash, NULL);
484 mutt_clear_threads (ctx);
485 for (i = 0; i < ctx->msgcount; i++)
486 header_delete(&ctx->hdrs[i]);
487 p_delete(&ctx->hdrs);
491 mutt_fast_close_compressed (ctx);
493 p_delete(&ctx->path);
494 p_delete(&ctx->pattern);
495 pattern_list_wipe(&ctx->limit_pattern);
500 /* save changes to disk */
501 static int sync_mailbox (CONTEXT * ctx, int *index_hint)
506 mutt_message (_("Writing %s..."), ctx->path);
508 if (MX_IDX(ctx->magic-1))
509 /* the 1 is only of interest for IMAP and means EXPUNGE */
510 rc = mxfmts[ctx->magic-1]->mx_sync_mailbox(ctx,1,index_hint);
512 if (rc == 0 && ctx->cinfo)
513 return mutt_sync_compressed (ctx);
518 /* move deleted mails to the trash folder */
519 static int trash_append (CONTEXT * ctx)
525 if (!TrashPath || !ctx->deleted ||
526 (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))
529 for (; i < ctx->msgcount && (!ctx->hdrs[i]->deleted ||
530 ctx->hdrs[i]->appended); i++);
531 if (i == ctx->msgcount)
532 return 0; /* nothing to be done */
534 if (mutt_save_confirm (TrashPath, &st) != 0) {
535 mutt_error _("message(s) not deleted");
540 if (lstat (ctx->path, &stc) == 0 && stc.st_ino == st.st_ino
541 && stc.st_dev == st.st_dev && stc.st_rdev == st.st_rdev)
542 return 0; /* we are in the trash folder: simple sync */
544 if ((ctx_trash = mx_open_mailbox (TrashPath, M_APPEND, NULL)) != NULL) {
545 for (i = 0; i < ctx->msgcount; i++)
546 if (ctx->hdrs[i]->deleted && !ctx->hdrs[i]->appended
547 && !ctx->hdrs[i]->purged
548 && mutt_append_message (ctx_trash, ctx, ctx->hdrs[i], 0, 0) == -1) {
549 mx_close_mailbox (ctx_trash, NULL);
553 mx_close_mailbox (ctx_trash, NULL);
556 mutt_error _("Can't open trash folder");
564 /* save changes and close mailbox */
565 static int _mx_close_mailbox (CONTEXT * ctx, int *index_hint)
567 int i, move_messages = 0, purge = 1, read_msgs = 0;
571 char mbox[_POSIX_PATH_MAX];
579 if (ctx->readonly || ctx->dontwrite) {
580 /* mailbox is readonly or we don't want to write */
581 mx_fastclose_mailbox (ctx);
586 /* mailbox was opened in write-mode */
587 if (ctx->magic == M_MBOX)
588 mbox_close_mailbox (ctx);
590 mx_fastclose_mailbox (ctx);
594 for (i = 0; i < ctx->msgcount; i++) {
595 if (!ctx->hdrs[i]->deleted && ctx->hdrs[i]->read
596 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
600 if (read_msgs && quadoption (OPT_MOVE) != M_NO) {
601 m_strcpy(mbox, sizeof(mbox), NONULL(Inbox));
602 isSpool = mutt_is_spool (ctx->path) && !mutt_is_spool (mbox);
603 mutt_expand_path (mbox, sizeof (mbox));
606 snprintf (buf, sizeof (buf), _("Move read messages to %s?"), mbox);
607 if ((move_messages = query_quadoption (OPT_MOVE, buf)) == -1) {
615 * There is no point in asking whether or not to purge if we are
616 * just marking messages as "trash".
618 if (ctx->deleted && !(ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH))) {
619 snprintf (buf, sizeof (buf), ctx->deleted == 1
620 ? _("Purge %d deleted message?") :
621 _("Purge %d deleted messages?"), ctx->deleted);
622 if ((purge = query_quadoption (OPT_DELETE, buf)) < 0) {
628 /* IMAP servers manage the OLD flag themselves */
629 if (ctx->magic != M_IMAP)
630 if (option (OPTMARKOLD)) {
631 for (i = 0; i < ctx->msgcount; i++) {
632 if (!ctx->hdrs[i]->deleted && !ctx->hdrs[i]->old)
633 mutt_set_flag (ctx, ctx->hdrs[i], M_OLD, 1);
638 mutt_message (_("Moving read messages to %s..."), mbox);
640 /* try to use server-side copy first */
643 if (ctx->magic == M_IMAP && imap_is_magic (mbox, NULL) == M_IMAP) {
644 /* tag messages for moving, and clear old tags, if any */
645 for (i = 0; i < ctx->msgcount; i++)
646 if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted
647 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
648 ctx->hdrs[i]->tagged = 1;
650 ctx->hdrs[i]->tagged = 0;
652 i = imap_copy_messages (ctx, NULL, mbox, 1);
655 if (i == 0) /* success */
657 else if (i == -1) { /* horrible error, bail */
661 else /* use regular append-copy mode */
663 if (mx_open_mailbox (mbox, M_APPEND, &f) == NULL) {
668 for (i = 0; i < ctx->msgcount; i++) {
669 if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted
670 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED))) {
671 if (mutt_append_message (&f, ctx, ctx->hdrs[i], 0, CH_UPDATE_LEN) ==
673 mutt_set_flag (ctx, ctx->hdrs[i], M_DELETE, 1);
674 mutt_set_flag (ctx, ctx->hdrs[i], M_APPENDED, 1);
677 mx_close_mailbox (&f, NULL);
684 mx_close_mailbox (&f, NULL);
688 else if (!ctx->changed && ctx->deleted == 0) {
689 mutt_message _("Mailbox is unchanged.");
691 mx_fastclose_mailbox (ctx);
695 /* copy mails to the trash before expunging */
696 if (purge && ctx->deleted)
697 if (trash_append (ctx) != 0) {
702 /* allow IMAP to preserve the deleted flag across sessions */
703 if (ctx->magic == M_IMAP) {
704 if ((check = imap_sync_mailbox (ctx, purge, index_hint)) != 0) {
712 for (i = 0; i < ctx->msgcount; i++)
713 ctx->hdrs[i]->deleted = 0;
717 if (ctx->changed || ctx->deleted) {
718 if ((check = sync_mailbox (ctx, index_hint)) != 0) {
726 mutt_message (_("%d kept, %d moved, %d deleted."),
727 ctx->msgcount - ctx->deleted, read_msgs, ctx->deleted);
729 mutt_message (_("%d kept, %d deleted."),
730 ctx->msgcount - ctx->deleted, ctx->deleted);
732 if (ctx->cinfo && mutt_slow_close_compressed (ctx))
735 mx_fastclose_mailbox (ctx);
740 int mx_close_mailbox (CONTEXT * ctx, int *index_hint) {
744 ret = _mx_close_mailbox (ctx, index_hint);
745 sidebar_set_buffystats (ctx);
749 /* update a Context structure's internal tables. */
751 void mx_update_tables (CONTEXT * ctx, int committing)
755 /* update memory to reflect the new state of the mailbox */
764 #define this_body ctx->hdrs[j]->content
765 for (i = 0, j = 0; i < ctx->msgcount; i++) {
766 if ((committing && (!ctx->hdrs[i]->deleted ||
767 (ctx->magic == M_MAILDIR
768 && option (OPTMAILDIRTRASH)))) || (!committing
772 ctx->hdrs[j] = ctx->hdrs[i];
775 ctx->hdrs[j]->msgno = j;
776 if (ctx->hdrs[j]->virtual != -1) {
777 ctx->v2r[ctx->vcount] = j;
778 ctx->hdrs[j]->virtual = ctx->vcount++;
779 ctx->vsize += this_body->length + this_body->offset -
780 this_body->hdr_offset;
784 ctx->hdrs[j]->changed = 0;
785 else if (ctx->hdrs[j]->changed)
789 || (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH))) {
790 if (ctx->hdrs[j]->deleted)
794 if (ctx->hdrs[j]->tagged)
796 if (ctx->hdrs[j]->flagged)
798 if (!ctx->hdrs[j]->read) {
800 if (!ctx->hdrs[j]->old)
807 if (ctx->magic == M_MH || ctx->magic == M_MAILDIR)
808 ctx->size -= (ctx->hdrs[i]->content->length +
809 ctx->hdrs[i]->content->offset -
810 ctx->hdrs[i]->content->hdr_offset);
811 /* remove message from the hash tables */
812 if (ctx->subj_hash && ctx->hdrs[i]->env->real_subj)
813 hash_remove (ctx->subj_hash, ctx->hdrs[i]->env->real_subj,
815 if (ctx->id_hash && ctx->hdrs[i]->env->message_id)
816 hash_remove (ctx->id_hash, ctx->hdrs[i]->env->message_id,
818 header_delete(&ctx->hdrs[i]);
824 /* update sidebar count */
825 sidebar_set_buffystats (ctx);
829 /* save changes to mailbox
835 static int _mx_sync_mailbox (CONTEXT * ctx, int *index_hint)
839 int msgcount, deleted;
841 if (ctx->dontwrite) {
842 char buf[STRING], tmp[STRING];
844 if (km_expand_key (buf, sizeof (buf),
845 km_find_func (MENU_MAIN, OP_TOGGLE_WRITE)))
846 snprintf (tmp, sizeof (tmp), _(" Press '%s' to toggle write"), buf);
848 m_strcpy(tmp, sizeof(tmp), _("Use 'toggle-write' to re-enable write!"));
850 mutt_error (_("Mailbox is marked unwritable. %s"), tmp);
853 else if (ctx->readonly) {
854 mutt_error _("Mailbox is read-only.");
859 if (!ctx->changed && !ctx->deleted) {
860 mutt_message _("Mailbox is unchanged.");
868 snprintf (buf, sizeof (buf), ctx->deleted == 1
869 ? _("Purge %d deleted message?") :
870 _("Purge %d deleted messages?"), ctx->deleted);
871 if ((purge = query_quadoption (OPT_DELETE, buf)) < 0)
873 else if (purge == M_NO) {
875 return 0; /* nothing to do! */
876 /* let IMAP servers hold on to D flags */
877 if (ctx->magic != M_IMAP)
879 for (i = 0; i < ctx->msgcount; i++)
880 ctx->hdrs[i]->deleted = 0;
884 else if (ctx->last_tag && ctx->last_tag->deleted)
885 ctx->last_tag = NULL; /* reset last tagged msg now useless */
888 /* really only for IMAP - imap_sync_mailbox results in a call to
889 * mx_update_tables, so ctx->deleted is 0 when it comes back */
890 msgcount = ctx->msgcount;
891 deleted = ctx->deleted;
893 if (purge && ctx->deleted) {
894 if (trash_append (ctx) == -1)
898 if (ctx->magic == M_IMAP)
899 rc = imap_sync_mailbox (ctx, purge, index_hint);
901 rc = sync_mailbox (ctx, index_hint);
903 if (ctx->magic == M_IMAP && !purge)
904 mutt_message (_("Mailbox checkpointed."));
906 mutt_message (_("%d kept, %d deleted."), msgcount - deleted, deleted);
910 /* if we haven't deleted any messages, we don't need to resort */
911 /* ... except for certain folder formats which need "unsorted"
912 * sort order in order to synchronize folders.
914 * MH and maildir are safe. mbox-style seems to need re-sorting,
915 * at least with the new threading code.
917 if (purge || (ctx->magic != M_MAILDIR && ctx->magic != M_MH)) {
918 /* IMAP does this automatically after handling EXPUNGE */
919 if (ctx->magic != M_IMAP)
921 mx_update_tables (ctx, 1);
922 mutt_sort_headers (ctx, 1); /* rethread from scratch */
930 int mx_sync_mailbox (CONTEXT* ctx, int* index_hint) {
931 int ret = _mx_sync_mailbox (ctx, index_hint);
932 sidebar_set_buffystats (ctx);
937 * dest destintation mailbox
938 * hdr message being copied (required for maildir support, because
939 * the filename depends on the message flags)
941 MESSAGE *mx_open_new_message (CONTEXT * dest, HEADER * hdr, int flags)
946 if (!MX_IDX(dest->magic-1)) {
950 msg = p_new(MESSAGE, 1);
951 msg->magic = dest->magic;
955 msg->flags.flagged = hdr->flagged;
956 msg->flags.replied = hdr->replied;
957 msg->flags.read = hdr->read;
958 msg->received = hdr->received;
961 if (msg->received == 0)
962 time (&msg->received);
964 if (mxfmts[dest->magic-1]->mx_open_new_message(msg, dest, hdr) == 0) {
965 if (msg->magic == M_MBOX && flags & M_ADD_FROM) {
967 if (hdr->env->return_path)
968 p = hdr->env->return_path;
969 else if (hdr->env->sender)
970 p = hdr->env->sender;
975 fprintf (msg->fp, "From %s %s", p ? p->mailbox : NONULL(mod_core.username),
976 ctime (&msg->received));
985 /* check for new mail */
986 int mx_check_mailbox (CONTEXT * ctx, int *index_hint, int lock) {
988 return mutt_check_mailbox_compressed (ctx);
993 if (MX_IDX(ctx->magic-1) && mxfmts[ctx->magic-1]->mx_check_mailbox)
994 return (mxfmts[ctx->magic-1]->mx_check_mailbox(ctx, index_hint, lock));
1000 /* return a stream pointer for a message */
1001 MESSAGE *mx_open_message (CONTEXT * ctx, int msgno)
1005 msg = p_new(MESSAGE, 1);
1006 switch (msg->magic = ctx->magic) {
1014 HEADER *cur = ctx->hdrs[msgno];
1015 char path[_POSIX_PATH_MAX];
1017 snprintf (path, sizeof (path), "%s/%s", ctx->path, cur->path);
1019 if ((msg->fp = fopen (path, "r")) == NULL && errno == ENOENT &&
1020 ctx->magic == M_MAILDIR)
1021 msg->fp = maildir_open_find_message (ctx->path, cur->path);
1023 if (msg->fp == NULL) {
1032 if (imap_fetch_message (msg, ctx, msgno) != 0)
1039 if (pop_fetch_message (msg, ctx, msgno) != 0)
1051 /* commit a message to a folder */
1053 int mx_commit_message (MESSAGE * msg, CONTEXT * ctx) {
1054 if (!(msg->write && ctx->append)) {
1057 if (!ctx || !MX_IDX(ctx->magic-1) || !mxfmts[ctx->magic-1]->mx_commit_message)
1059 return (mxfmts[ctx->magic-1]->mx_commit_message (msg, ctx));
1062 /* close a pointer to a message */
1063 int mx_close_message (MESSAGE ** msg)
1067 if ((*msg)->magic == M_MH || (*msg)->magic == M_MAILDIR
1068 || (*msg)->magic == M_IMAP || (*msg)->magic == M_POP)
1070 r = m_fclose(&(*msg)->fp);
1076 unlink ((*msg)->path);
1077 p_delete(&(*msg)->path);
1084 void mx_alloc_memory (CONTEXT * ctx)
1088 p_realloc(&ctx->hdrs, ctx->hdrmax);
1089 p_realloc(&ctx->v2r, ctx->hdrmax);
1090 p_clear(ctx->hdrs + ctx->msgcount, ctx->hdrmax - ctx->msgcount);
1091 p_clear(ctx->v2r + ctx->msgcount, ctx->hdrmax - ctx->msgcount);
1094 /* this routine is called to update the counts in the context structure for
1095 * the last message header parsed.
1097 void mx_update_context (CONTEXT * ctx, int new_messages)
1102 for (msgno = ctx->msgcount - new_messages; msgno < ctx->msgcount; msgno++) {
1103 h = ctx->hdrs[msgno];
1105 /* NOTE: this _must_ be done before the check for mailcap! */
1106 h->security = crypt_query (h->content);
1108 if (!ctx->pattern) {
1109 ctx->v2r[ctx->vcount] = msgno;
1110 h->virtual = ctx->vcount++;
1116 if (h->env->supersedes) {
1120 ctx->id_hash = mutt_make_id_hash (ctx);
1122 h2 = hash_find (ctx->id_hash, h->env->supersedes);
1124 /* p_delete(&h->env->supersedes); should I ? */
1127 if (!ctx->counting && mod_score.enable)
1128 mutt_score_message (ctx, h2, 1);
1132 /* add this message to the hash tables */
1133 if (ctx->id_hash && h->env->message_id)
1134 hash_insert (ctx->id_hash, h->env->message_id, h);
1135 if (!ctx->counting) {
1136 if (ctx->subj_hash && h->env->real_subj)
1137 hash_insert (ctx->subj_hash, h->env->real_subj, h);
1139 if (mod_score.enable)
1140 mutt_score_message (ctx, h, 0);
1155 /* update sidebar count */
1156 sidebar_set_buffystats (ctx);
1161 * 1 if the specified mailbox contains 0 messages.
1162 * 0 if the mailbox contains messages
1165 int mx_check_empty (const char *path)
1168 if ((i = mx_get_idx (path)) >= 0 && mxfmts[i]->mx_check_empty)
1169 return (mxfmts[i]->mx_check_empty(path));
1174 int mx_acl_check(CONTEXT *ctx, int flag)
1176 if (!mxfmts[ctx->magic-1]->mx_acl_check)
1178 return mxfmts[ctx->magic-1]->mx_acl_check(ctx,flag);
1181 void mutt_parse_mime_message (CONTEXT * ctx, HEADER * cur)
1187 if (cur->content->type != TYPEMESSAGE
1188 && cur->content->type != TYPEMULTIPART)
1189 break; /* nothing to do */
1191 if (cur->content->parts)
1192 break; /* The message was parsed earlier. */
1194 if ((msg = mx_open_message (ctx, cur->msgno))) {
1195 mutt_parse_part (msg->fp, cur->content);
1197 cur->security = crypt_query (cur->content);
1199 mx_close_message (&msg);
1202 mutt_count_body_parts (cur, flags | M_PARTS_RECOUNT);