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>
31 #include <imap/imap.h>
32 #include <imap/mx_imap.h>
35 #include <pop/mx_pop.h>
38 #include "nntp/nntp.h"
39 #include "nntp/mx_nntp.h"
42 #include <lib-crypt/crypt.h>
44 static mx_t const *mxfmts[] = {
57 #define MX_IDX(idx) (idx >= 0 && idx < countof(mxfmts))
58 #define mutt_is_spool(s) (m_strcmp(Spoolfile, s) == 0)
62 * retry - should retry if unable to lock?
64 static int invoke_dotlock (const char *path, int flags, int retry)
66 char cmd[LONG_STRING + _POSIX_PATH_MAX];
67 char f[SHORT_STRING + _POSIX_PATH_MAX];
70 if (flags & DL_FL_RETRY)
71 snprintf (r, sizeof (r), "-r %d ", retry ? MAXLOCKATTEMPT : 0);
73 mutt_quote_filename (f, sizeof (f), path);
75 snprintf (cmd, sizeof (cmd),
78 flags & DL_FL_TRY ? "-t " : "",
79 flags & DL_FL_UNLOCK ? "-u " : "",
80 flags & DL_FL_USEPRIV ? "-p " : "",
81 flags & DL_FL_FORCE ? "-f " : "",
82 flags & DL_FL_UNLINK ? "-d " : "",
83 flags & DL_FL_RETRY ? r : "", f);
85 return mutt_system (cmd);
88 static int dotlock_file (const char *path, int retry)
91 int flags = DL_FL_USEPRIV | DL_FL_RETRY;
97 if ((r = invoke_dotlock (path, flags, retry)) == DL_EX_EXIST) {
98 if (!option (OPTNOCURSES)) {
99 char msg[LONG_STRING];
101 snprintf (msg, sizeof (msg),
102 _("Lock count exceeded, remove lock for %s?"), path);
103 if (retry && mutt_yesorno (msg, M_YES) == M_YES) {
104 flags |= DL_FL_FORCE;
111 mutt_error (_("Can't dotlock %s.\n"), path);
114 return (r == DL_EX_OK ? 0 : -1);
117 static int undotlock_file (const char *path)
119 return (invoke_dotlock (path, DL_FL_USEPRIV | DL_FL_UNLOCK, 0) ==
123 /* looks up index of type for path in mxfmts */
124 static int mx_get_idx (const char* path) {
128 /* first, test all non-local folders to avoid stat() call */
129 for (i = 0; i < countof(mxfmts); i++) {
130 if (!mxfmts[i]->local)
131 t = mxfmts[i]->mx_is_magic(path, NULL);
135 if (stat (path, &st) == 0) {
136 /* if stat() succeeded, keep testing until success and
137 * pass stat() info so that we only need to do it once */
138 for (i = 0; i < countof(mxfmts); i++) {
139 if (mxfmts[i]->local)
140 t = mxfmts[i]->mx_is_magic(path, &st);
149 * excl if excl != 0, request an exclusive lock
150 * dot if dot != 0, try to dotlock the file
151 * time_out should retry locking?
153 int mx_lock_file (const char *path, int fd, int excl, int dot, int time_out)
155 #if defined (USE_FCNTL) || defined (USE_FLOCK)
167 lck.l_type = excl ? F_WRLCK : F_RDLCK;
168 lck.l_whence = SEEK_SET;
173 while (fcntl (fd, F_SETLK, &lck) == -1) {
176 if (errno != EAGAIN && errno != EACCES) {
177 mutt_perror ("fcntl");
181 if (fstat (fd, &sb) != 0)
187 /* only unlock file if it is unchanged */
188 if (prev_sb.st_size == sb.st_size
189 && ++count >= (time_out ? MAXLOCKATTEMPT : 0)) {
191 mutt_error _("Timeout exceeded while attempting fcntl lock!");
198 mutt_message (_("Waiting for fcntl lock... %d"), ++attempt);
201 #endif /* USE_FCNTL */
206 while (flock (fd, (excl ? LOCK_EX : LOCK_SH) | LOCK_NB) == -1) {
209 if (errno != EWOULDBLOCK) {
210 mutt_perror ("flock");
215 if (fstat (fd, &sb) != 0)
221 /* only unlock file if it is unchanged */
222 if (prev_sb.st_size == sb.st_size
223 && ++count >= (time_out ? MAXLOCKATTEMPT : 0)) {
225 mutt_error _("Timeout exceeded while attempting flock lock!");
233 mutt_message (_("Waiting for flock attempt... %d"), ++attempt);
236 #endif /* USE_FLOCK */
239 r = dotlock_file (path, time_out);
242 /* release any other locks obtained in this routine */
245 lck.l_type = F_UNLCK;
246 fcntl (fd, F_SETLK, &lck);
247 #endif /* USE_FCNTL */
251 #endif /* USE_FLOCK */
259 int mx_unlock_file (const char *path, int fd, int dot)
262 struct flock unlockit;
264 p_clear(&unlockit, 1);
265 unlockit.l_type = F_UNLCK;
266 unlockit.l_whence = SEEK_SET;
267 fcntl (fd, F_SETLK, &unlockit);
275 undotlock_file (path);
280 static void mx_unlink_empty (const char *path)
285 if ((fd = open (path, O_RDWR)) == -1)
288 if (mx_lock_file (path, fd, 1, 0, 1) == -1) {
293 invoke_dotlock(path, DL_FL_UNLINK, 1);
295 mx_unlock_file (path, fd, 0);
299 /* try to figure out what type of mailbox ``path'' is */
300 int mx_get_magic (const char *path) {
303 if (m_strlen(path) == 0)
305 if ((i = mx_get_idx (path)) >= 0)
306 return (mxfmts[i]->type);
310 int mx_is_local (int m) {
313 return (mxfmts[m]->local);
317 * set DefaultMagic to the given value
319 int mx_set_magic (const char *s)
321 if (ascii_strcasecmp (s, "mbox") == 0)
322 DefaultMagic = M_MBOX;
323 else if (ascii_strcasecmp (s, "mmdf") == 0)
324 DefaultMagic = M_MMDF;
325 else if (ascii_strcasecmp (s, "mh") == 0)
327 else if (ascii_strcasecmp (s, "maildir") == 0)
328 DefaultMagic = M_MAILDIR;
335 /* mx_access: Wrapper for access, checks permissions on a given mailbox.
336 * We may be interested in using ACL-style flags at some point, currently
337 * we use the normal access() flags. */
338 int mx_access (const char *path, int flags)
342 if ((i = mx_get_idx (path)) >= 0 && mxfmts[i]->mx_access)
343 return (mxfmts[i]->mx_access(path,flags));
347 static int mx_open_mailbox_append (CONTEXT * ctx, int flags)
351 /* special case for appending to compressed folders -
352 * even if we can not open them for reading */
353 if (mutt_can_append_compressed (ctx->path))
354 mutt_open_append_compressed (ctx);
358 if (mx_get_magic (ctx->path) == M_IMAP)
359 return imap_open_mailbox_append (ctx);
361 if (stat (ctx->path, &sb) == 0) {
362 ctx->magic = mx_get_magic (ctx->path);
364 switch (ctx->magic) {
366 mutt_error (_("%s is not a mailbox."), ctx->path);
372 else if (errno == ENOENT) {
373 ctx->magic = DefaultMagic;
375 if (ctx->magic == M_MH || ctx->magic == M_MAILDIR) {
376 char tmp[_POSIX_PATH_MAX];
378 if (mkdir (ctx->path, S_IRWXU)) {
379 mutt_perror (ctx->path);
383 if (ctx->magic == M_MAILDIR) {
384 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
385 if (mkdir (tmp, S_IRWXU)) {
391 snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
392 if (mkdir (tmp, S_IRWXU)) {
394 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
399 snprintf (tmp, sizeof (tmp), "%s/tmp", ctx->path);
400 if (mkdir (tmp, S_IRWXU)) {
402 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
404 snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
413 snprintf (tmp, sizeof (tmp), "%s/.mh_sequences", ctx->path);
414 if ((i = creat (tmp, S_IRWXU)) == -1) {
424 mutt_perror (ctx->path);
428 switch (ctx->magic) {
432 safe_fopen (ctx->path, flags & M_NEWFOLDER ? "w" : "a")) == NULL
433 || mbox_lock_mailbox (ctx, 1, 1) != 0) {
435 mutt_perror (ctx->path);
437 mutt_error (_("Couldn't lock %s\n"), ctx->path);
438 safe_fclose (&ctx->fp);
442 fseeko (ctx->fp, 0, 2);
458 * open a mailbox and parse it
461 * flags M_NOSORT do not sort mailbox
462 * M_APPEND open mailbox for appending
463 * M_READONLY open mailbox in read-only mode
464 * M_QUIET only print error messages
465 * ctx if non-null, context struct to use
467 CONTEXT *mx_open_mailbox (const char *path, int flags, CONTEXT * pctx)
473 ctx = p_new(CONTEXT, 1);
475 ctx->path = m_strdup(path);
477 ctx->msgnotreadyet = -1;
482 if (flags & M_READONLY)
487 if (flags & (M_APPEND | M_NEWFOLDER)) {
488 if (mx_open_mailbox_append (ctx, flags) != 0) {
489 mx_fastclose_mailbox (ctx);
497 if (!MX_IDX(ctx->magic-1))
498 ctx->magic = mx_get_magic (path);
500 if (ctx->magic == M_COMPRESSED)
501 mutt_open_read_compressed (ctx);
504 mutt_error (_("%s is not a mailbox."), path);
506 if (ctx->magic == -1)
509 if (ctx->magic <= 0) {
510 mx_fastclose_mailbox (ctx);
516 /* if the user has a `push' command in their .muttrc, or in a folder-hook,
517 * it will cause the progress messages not to be displayed because
518 * mutt_refresh() will think we are in the middle of a macro. so set a
519 * flag to indicate that we should really refresh the screen.
521 set_option (OPTFORCEREFRESH);
524 mutt_message (_("Reading %s..."), ctx->path);
526 rc = mxfmts[ctx->magic-1]->mx_open_mailbox(ctx);
529 if ((flags & M_NOSORT) == 0) {
530 /* avoid unnecessary work since the mailbox is completely unthreaded
532 unset_option (OPTSORTSUBTHREADS);
533 unset_option (OPTNEEDRESCORE);
534 mutt_sort_headers (ctx, 1);
540 mx_fastclose_mailbox (ctx);
545 unset_option (OPTFORCEREFRESH);
549 /* free up memory associated with the mailbox context */
550 void mx_fastclose_mailbox (CONTEXT * ctx)
557 if (MX_IDX(ctx->magic-1) && mxfmts[ctx->magic-1]->mx_fastclose_mailbox)
558 mxfmts[ctx->magic-1]->mx_fastclose_mailbox(ctx);
560 hash_destroy (&ctx->subj_hash, NULL);
562 hash_destroy (&ctx->id_hash, NULL);
563 mutt_clear_threads (ctx);
564 for (i = 0; i < ctx->msgcount; i++)
565 header_delete(&ctx->hdrs[i]);
566 p_delete(&ctx->hdrs);
569 if (ctx->compressinfo)
570 mutt_fast_close_compressed (ctx);
572 p_delete(&ctx->path);
573 p_delete(&ctx->pattern);
574 if (ctx->limit_pattern)
575 mutt_pattern_free (&ctx->limit_pattern);
576 safe_fclose (&ctx->fp);
580 /* save changes to disk */
581 static int sync_mailbox (CONTEXT * ctx, int *index_hint)
586 mutt_message (_("Writing %s..."), ctx->path);
588 if (MX_IDX(ctx->magic-1))
589 /* the 1 is only of interest for IMAP and means EXPUNGE */
590 rc = mxfmts[ctx->magic-1]->mx_sync_mailbox(ctx,1,index_hint);
592 if (rc == 0 && ctx->compressinfo)
593 return mutt_sync_compressed (ctx);
598 /* move deleted mails to the trash folder */
599 static int trash_append (CONTEXT * ctx)
605 if (!TrashPath || !ctx->deleted ||
606 (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))
609 for (; i < ctx->msgcount && (!ctx->hdrs[i]->deleted ||
610 ctx->hdrs[i]->appended); i++);
611 if (i == ctx->msgcount)
612 return 0; /* nothing to be done */
614 if (mutt_save_confirm (TrashPath, &st) != 0) {
615 mutt_error _("message(s) not deleted");
620 if (lstat (ctx->path, &stc) == 0 && stc.st_ino == st.st_ino
621 && stc.st_dev == st.st_dev && stc.st_rdev == st.st_rdev)
622 return 0; /* we are in the trash folder: simple sync */
624 if ((ctx_trash = mx_open_mailbox (TrashPath, M_APPEND, NULL)) != NULL) {
625 for (i = 0; i < ctx->msgcount; i++)
626 if (ctx->hdrs[i]->deleted && !ctx->hdrs[i]->appended
627 && !ctx->hdrs[i]->purged
628 && mutt_append_message (ctx_trash, ctx, ctx->hdrs[i], 0, 0) == -1) {
629 mx_close_mailbox (ctx_trash, NULL);
633 mx_close_mailbox (ctx_trash, NULL);
636 mutt_error _("Can't open trash folder");
644 /* save changes and close mailbox */
645 static int _mx_close_mailbox (CONTEXT * ctx, int *index_hint)
647 int i, move_messages = 0, purge = 1, read_msgs = 0;
651 char mbox[_POSIX_PATH_MAX];
652 char buf[SHORT_STRING];
660 if (ctx->magic == M_NNTP) {
663 ret = nntp_close_mailbox (ctx);
664 mx_fastclose_mailbox (ctx);
668 if (ctx->readonly || ctx->dontwrite) {
669 /* mailbox is readonly or we don't want to write */
670 mx_fastclose_mailbox (ctx);
675 /* mailbox was opened in write-mode */
676 if (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
677 mbox_close_mailbox (ctx);
679 mx_fastclose_mailbox (ctx);
683 for (i = 0; i < ctx->msgcount; i++) {
684 if (!ctx->hdrs[i]->deleted && ctx->hdrs[i]->read
685 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
689 if (read_msgs && quadoption (OPT_MOVE) != M_NO) {
692 if ((p = mutt_find_hook (M_MBOXHOOK, ctx->path))) {
694 m_strcpy(mbox, sizeof(mbox), p);
697 m_strcpy(mbox, sizeof(mbox), NONULL(Inbox));
698 isSpool = mutt_is_spool (ctx->path) && !mutt_is_spool (mbox);
700 mutt_expand_path (mbox, sizeof (mbox));
703 snprintf (buf, sizeof (buf), _("Move read messages to %s?"), mbox);
704 if ((move_messages = query_quadoption (OPT_MOVE, buf)) == -1) {
712 * There is no point in asking whether or not to purge if we are
713 * just marking messages as "trash".
715 if (ctx->deleted && !(ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH))) {
716 snprintf (buf, sizeof (buf), ctx->deleted == 1
717 ? _("Purge %d deleted message?") :
718 _("Purge %d deleted messages?"), ctx->deleted);
719 if ((purge = query_quadoption (OPT_DELETE, buf)) < 0) {
725 /* IMAP servers manage the OLD flag themselves */
726 if (ctx->magic != M_IMAP)
727 if (option (OPTMARKOLD)) {
728 for (i = 0; i < ctx->msgcount; i++) {
729 if (!ctx->hdrs[i]->deleted && !ctx->hdrs[i]->old)
730 mutt_set_flag (ctx, ctx->hdrs[i], M_OLD, 1);
735 mutt_message (_("Moving read messages to %s..."), mbox);
737 /* try to use server-side copy first */
740 if (ctx->magic == M_IMAP && imap_is_magic (mbox, NULL) == M_IMAP) {
741 /* tag messages for moving, and clear old tags, if any */
742 for (i = 0; i < ctx->msgcount; i++)
743 if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted
744 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
745 ctx->hdrs[i]->tagged = 1;
747 ctx->hdrs[i]->tagged = 0;
749 i = imap_copy_messages (ctx, NULL, mbox, 1);
752 if (i == 0) /* success */
754 else if (i == -1) { /* horrible error, bail */
758 else /* use regular append-copy mode */
760 if (mx_open_mailbox (mbox, M_APPEND, &f) == NULL) {
765 for (i = 0; i < ctx->msgcount; i++) {
766 if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted
767 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED))) {
768 if (mutt_append_message (&f, ctx, ctx->hdrs[i], 0, CH_UPDATE_LEN) ==
770 mutt_set_flag (ctx, ctx->hdrs[i], M_DELETE, 1);
771 mutt_set_flag (ctx, ctx->hdrs[i], M_APPENDED, 1);
774 mx_close_mailbox (&f, NULL);
781 mx_close_mailbox (&f, NULL);
785 else if (!ctx->changed && ctx->deleted == 0) {
786 mutt_message _("Mailbox is unchanged.");
788 mx_fastclose_mailbox (ctx);
792 /* copy mails to the trash before expunging */
793 if (purge && ctx->deleted)
794 if (trash_append (ctx) != 0) {
799 /* allow IMAP to preserve the deleted flag across sessions */
800 if (ctx->magic == M_IMAP) {
801 if ((check = imap_sync_mailbox (ctx, purge, index_hint)) != 0) {
809 for (i = 0; i < ctx->msgcount; i++)
810 ctx->hdrs[i]->deleted = 0;
814 if (ctx->changed || ctx->deleted) {
815 if ((check = sync_mailbox (ctx, index_hint)) != 0) {
823 mutt_message (_("%d kept, %d moved, %d deleted."),
824 ctx->msgcount - ctx->deleted, read_msgs, ctx->deleted);
826 mutt_message (_("%d kept, %d deleted."),
827 ctx->msgcount - ctx->deleted, ctx->deleted);
829 if (ctx->msgcount == ctx->deleted &&
830 (ctx->magic == M_MMDF || ctx->magic == M_MBOX) &&
831 !mutt_is_spool (ctx->path) && !option (OPTSAVEEMPTY))
832 mx_unlink_empty (ctx->path);
834 if (ctx->compressinfo && mutt_slow_close_compressed (ctx))
837 mx_fastclose_mailbox (ctx);
842 int mx_close_mailbox (CONTEXT * ctx, int *index_hint) {
846 ret = _mx_close_mailbox (ctx, index_hint);
847 sidebar_set_buffystats (ctx);
851 /* update a Context structure's internal tables. */
853 void mx_update_tables (CONTEXT * ctx, int committing)
857 /* update memory to reflect the new state of the mailbox */
866 #define this_body ctx->hdrs[j]->content
867 for (i = 0, j = 0; i < ctx->msgcount; i++) {
868 if ((committing && (!ctx->hdrs[i]->deleted ||
869 (ctx->magic == M_MAILDIR
870 && option (OPTMAILDIRTRASH)))) || (!committing
874 ctx->hdrs[j] = ctx->hdrs[i];
877 ctx->hdrs[j]->msgno = j;
878 if (ctx->hdrs[j]->virtual != -1) {
879 ctx->v2r[ctx->vcount] = j;
880 ctx->hdrs[j]->virtual = ctx->vcount++;
881 ctx->vsize += this_body->length + this_body->offset -
882 this_body->hdr_offset;
886 ctx->hdrs[j]->changed = 0;
887 else if (ctx->hdrs[j]->changed)
891 || (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH))) {
892 if (ctx->hdrs[j]->deleted)
896 if (ctx->hdrs[j]->tagged)
898 if (ctx->hdrs[j]->flagged)
900 if (!ctx->hdrs[j]->read) {
902 if (!ctx->hdrs[j]->old)
909 if (ctx->magic == M_MH || ctx->magic == M_MAILDIR)
910 ctx->size -= (ctx->hdrs[i]->content->length +
911 ctx->hdrs[i]->content->offset -
912 ctx->hdrs[i]->content->hdr_offset);
913 /* remove message from the hash tables */
914 if (ctx->subj_hash && ctx->hdrs[i]->env->real_subj)
915 hash_delete (ctx->subj_hash, ctx->hdrs[i]->env->real_subj,
917 if (ctx->id_hash && ctx->hdrs[i]->env->message_id)
918 hash_delete (ctx->id_hash, ctx->hdrs[i]->env->message_id,
920 header_delete(&ctx->hdrs[i]);
926 /* update sidebar count */
927 sidebar_set_buffystats (ctx);
931 /* save changes to mailbox
937 static int _mx_sync_mailbox (CONTEXT * ctx, int *index_hint)
941 int msgcount, deleted;
943 if (ctx->dontwrite) {
944 char buf[STRING], tmp[STRING];
946 if (km_expand_key (buf, sizeof (buf),
947 km_find_func (MENU_MAIN, OP_TOGGLE_WRITE)))
948 snprintf (tmp, sizeof (tmp), _(" Press '%s' to toggle write"), buf);
950 m_strcpy(tmp, sizeof(tmp), _("Use 'toggle-write' to re-enable write!"));
952 mutt_error (_("Mailbox is marked unwritable. %s"), tmp);
955 else if (ctx->readonly) {
956 mutt_error _("Mailbox is read-only.");
961 if (!ctx->changed && !ctx->deleted) {
962 mutt_message _("Mailbox is unchanged.");
968 char buf[SHORT_STRING];
970 snprintf (buf, sizeof (buf), ctx->deleted == 1
971 ? _("Purge %d deleted message?") :
972 _("Purge %d deleted messages?"), ctx->deleted);
973 if ((purge = query_quadoption (OPT_DELETE, buf)) < 0)
975 else if (purge == M_NO) {
977 return 0; /* nothing to do! */
978 /* let IMAP servers hold on to D flags */
979 if (ctx->magic != M_IMAP)
981 for (i = 0; i < ctx->msgcount; i++)
982 ctx->hdrs[i]->deleted = 0;
986 else if (ctx->last_tag && ctx->last_tag->deleted)
987 ctx->last_tag = NULL; /* reset last tagged msg now useless */
990 /* really only for IMAP - imap_sync_mailbox results in a call to
991 * mx_update_tables, so ctx->deleted is 0 when it comes back */
992 msgcount = ctx->msgcount;
993 deleted = ctx->deleted;
995 if (purge && ctx->deleted) {
996 if (trash_append (ctx) == -1)
1000 if (ctx->magic == M_IMAP)
1001 rc = imap_sync_mailbox (ctx, purge, index_hint);
1003 rc = sync_mailbox (ctx, index_hint);
1005 if (ctx->magic == M_IMAP && !purge)
1006 mutt_message (_("Mailbox checkpointed."));
1009 mutt_message (_("%d kept, %d deleted."), msgcount - deleted, deleted);
1013 if (ctx->msgcount == ctx->deleted &&
1014 (ctx->magic == M_MBOX || ctx->magic == M_MMDF) &&
1015 !mutt_is_spool (ctx->path) && !option (OPTSAVEEMPTY)) {
1017 mx_fastclose_mailbox (ctx);
1021 /* if we haven't deleted any messages, we don't need to resort */
1022 /* ... except for certain folder formats which need "unsorted"
1023 * sort order in order to synchronize folders.
1025 * MH and maildir are safe. mbox-style seems to need re-sorting,
1026 * at least with the new threading code.
1028 if (purge || (ctx->magic != M_MAILDIR && ctx->magic != M_MH)) {
1029 /* IMAP does this automatically after handling EXPUNGE */
1030 if (ctx->magic != M_IMAP)
1032 mx_update_tables (ctx, 1);
1033 mutt_sort_headers (ctx, 1); /* rethread from scratch */
1041 int mx_sync_mailbox (CONTEXT* ctx, int* index_hint) {
1042 int ret = _mx_sync_mailbox (ctx, index_hint);
1043 sidebar_set_buffystats (ctx);
1048 * dest destintation mailbox
1049 * hdr message being copied (required for maildir support, because
1050 * the filename depends on the message flags)
1052 MESSAGE *mx_open_new_message (CONTEXT * dest, HEADER * hdr, int flags)
1055 address_t *p = NULL;
1057 if (!MX_IDX(dest->magic-1)) {
1061 msg = p_new(MESSAGE, 1);
1062 msg->magic = dest->magic;
1066 msg->flags.flagged = hdr->flagged;
1067 msg->flags.replied = hdr->replied;
1068 msg->flags.read = hdr->read;
1069 msg->received = hdr->received;
1072 if (msg->received == 0)
1073 time (&msg->received);
1075 if (mxfmts[dest->magic-1]->mx_open_new_message(msg, dest, hdr) == 0) {
1076 if (dest->magic == M_MMDF)
1077 fputs (MMDF_SEP, msg->fp);
1079 if ((msg->magic == M_MBOX || msg->magic == M_MMDF) && flags & M_ADD_FROM) {
1081 if (hdr->env->return_path)
1082 p = hdr->env->return_path;
1083 else if (hdr->env->sender)
1084 p = hdr->env->sender;
1089 fprintf (msg->fp, "From %s %s", p ? p->mailbox : NONULL (Username),
1090 ctime (&msg->received));
1099 /* check for new mail */
1100 int mx_check_mailbox (CONTEXT * ctx, int *index_hint, int lock) {
1101 if (ctx->compressinfo)
1102 return mutt_check_mailbox_compressed (ctx);
1107 if (MX_IDX(ctx->magic-1) && mxfmts[ctx->magic-1]->mx_check_mailbox)
1108 return (mxfmts[ctx->magic-1]->mx_check_mailbox(ctx, index_hint, lock));
1114 /* return a stream pointer for a message */
1115 MESSAGE *mx_open_message (CONTEXT * ctx, int msgno)
1119 msg = p_new(MESSAGE, 1);
1120 switch (msg->magic = ctx->magic) {
1129 HEADER *cur = ctx->hdrs[msgno];
1130 char path[_POSIX_PATH_MAX];
1132 snprintf (path, sizeof (path), "%s/%s", ctx->path, cur->path);
1134 if ((msg->fp = fopen (path, "r")) == NULL && errno == ENOENT &&
1135 ctx->magic == M_MAILDIR)
1136 msg->fp = maildir_open_find_message (ctx->path, cur->path);
1138 if (msg->fp == NULL) {
1147 if (imap_fetch_message (msg, ctx, msgno) != 0)
1154 if (pop_fetch_message (msg, ctx, msgno) != 0)
1162 if (nntp_fetch_message (msg, ctx, msgno) != 0)
1166 #endif /* USE_NNTP */
1175 /* commit a message to a folder */
1177 int mx_commit_message (MESSAGE * msg, CONTEXT * ctx) {
1178 if (!(msg->write && ctx->append)) {
1181 if (!ctx || !MX_IDX(ctx->magic-1) || !mxfmts[ctx->magic-1]->mx_commit_message)
1183 return (mxfmts[ctx->magic-1]->mx_commit_message (msg, ctx));
1186 /* close a pointer to a message */
1187 int mx_close_message (MESSAGE ** msg)
1191 if ((*msg)->magic == M_MH || (*msg)->magic == M_MAILDIR
1192 || (*msg)->magic == M_IMAP
1193 || (*msg)->magic == M_POP
1195 || (*msg)->magic == M_NNTP
1198 r = safe_fclose (&(*msg)->fp);
1204 unlink ((*msg)->path);
1205 p_delete(&(*msg)->path);
1212 void mx_alloc_memory (CONTEXT * ctx)
1215 size_t s = MAX (sizeof (HEADER *), sizeof (int));
1217 if ((ctx->hdrmax + 25) * s < ctx->hdrmax * s) {
1218 mutt_error _("Integer overflow -- can't allocate memory.");
1225 p_realloc(&ctx->hdrs, ctx->hdrmax += 25);
1226 p_realloc(&ctx->v2r, ctx->hdrmax);
1229 ctx->hdrs = p_new(HEADER *, (ctx->hdrmax += 25));
1230 ctx->v2r = p_new(int, ctx->hdrmax);
1232 for (i = ctx->msgcount; i < ctx->hdrmax; i++) {
1233 ctx->hdrs[i] = NULL;
1238 /* this routine is called to update the counts in the context structure for
1239 * the last message header parsed.
1241 void mx_update_context (CONTEXT * ctx, int new_messages)
1246 for (msgno = ctx->msgcount - new_messages; msgno < ctx->msgcount; msgno++) {
1247 h = ctx->hdrs[msgno];
1249 /* NOTE: this _must_ be done before the check for mailcap! */
1250 h->security = crypt_query (h->content);
1252 if (!ctx->pattern) {
1253 ctx->v2r[ctx->vcount] = msgno;
1254 h->virtual = ctx->vcount++;
1260 if (h->env->supersedes) {
1264 ctx->id_hash = mutt_make_id_hash (ctx);
1266 h2 = hash_find (ctx->id_hash, h->env->supersedes);
1268 /* p_delete(&h->env->supersedes); should I ? */
1271 if (!ctx->counting && option (OPTSCORE))
1272 mutt_score_message (ctx, h2, 1);
1276 /* add this message to the hash tables */
1277 if (ctx->id_hash && h->env->message_id)
1278 hash_insert (ctx->id_hash, h->env->message_id, h, 0);
1279 if (!ctx->counting) {
1280 if (ctx->subj_hash && h->env->real_subj)
1281 hash_insert (ctx->subj_hash, h->env->real_subj, h, 1);
1283 if (option (OPTSCORE))
1284 mutt_score_message (ctx, h, 0);
1299 /* update sidebar count */
1300 sidebar_set_buffystats (ctx);
1305 * 1 if the specified mailbox contains 0 messages.
1306 * 0 if the mailbox contains messages
1309 int mx_check_empty (const char *path)
1312 if ((i = mx_get_idx (path)) >= 0 && mxfmts[i]->mx_check_empty)
1313 return (mxfmts[i]->mx_check_empty(path));
1318 int mx_acl_check (CONTEXT* ctx, int flag) {
1319 if (!ctx || !MX_IDX(ctx->magic-1))
1321 /* if no acl_check defined for module, assume permission is granted */
1322 if (!mxfmts[ctx->magic-1]->mx_acl_check)
1324 return (mxfmts[ctx->magic-1]->mx_acl_check(ctx,flag));
1327 int mx_rebuild_cache (void) {
1329 mutt_error (_("Support for header caching was not build in."));
1332 int i = 0, magic = 0;
1333 CONTEXT* ctx = NULL;
1336 if (!Incoming.len) {
1337 mutt_error (_("No mailboxes defined."));
1341 for (i = 0; i < Incoming.len; i++) {
1342 b = Incoming.arr[i];
1343 magic = mx_get_magic (b->path);
1344 if (magic != M_MAILDIR && magic != M_MH && magic != M_IMAP)
1346 sidebar_set_current (b->path);
1348 if ((ctx = mx_open_mailbox (b->path,
1349 M_READONLY | M_NOSORT | M_COUNT,
1351 mx_close_mailbox (ctx, 0);
1353 mutt_clear_error ();
1355 if (Context && Context->path)
1356 sidebar_set_current (Context->path);
1363 void mutt_parse_mime_message (CONTEXT * ctx, HEADER * cur)
1369 if (cur->content->type != TYPEMESSAGE
1370 && cur->content->type != TYPEMULTIPART)
1371 break; /* nothing to do */
1373 if (cur->content->parts)
1374 break; /* The message was parsed earlier. */
1376 if ((msg = mx_open_message (ctx, cur->msgno))) {
1377 mutt_parse_part (msg->fp, cur->content);
1379 cur->security = crypt_query (cur->content);
1381 mx_close_message (&msg);
1384 mutt_count_body_parts (cur, flags | M_PARTS_RECOUNT);