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.
15 #include <lib-lib/mem.h>
16 #include <lib-lib/ascii.h>
17 #include <lib-lib/str.h>
18 #include <lib-lib/macros.h>
19 #include <lib-lib/file.h>
21 #include <lib-mime/mime.h>
40 #include "imap/imap.h"
41 #include "imap/mx_imap.h"
46 #include "pop/mx_pop.h"
50 #include "nntp/nntp.h"
51 #include "nntp/mx_nntp.h"
58 #include "mutt_crypt.h"
61 #include "lib/debug.h"
74 static list2_t* MailboxFormats = NULL;
75 #define MX_COMMAND(idx,cmd) ((mx_t*) MailboxFormats->data[idx])->cmd
76 #define MX_IDX(idx) (idx >= 0 && idx < MailboxFormats->length)
78 #define mutt_is_spool(s) (m_strcmp(Spoolfile, s) == 0)
83 * retry - should retry if unable to lock?
88 static int invoke_dotlock (const char *path, int dummy, int flags, int retry)
90 char cmd[LONG_STRING + _POSIX_PATH_MAX];
91 char f[SHORT_STRING + _POSIX_PATH_MAX];
94 if (flags & DL_FL_RETRY)
95 snprintf (r, sizeof (r), "-r %d ", retry ? MAXLOCKATTEMPT : 0);
97 mutt_quote_filename (f, sizeof (f), path);
99 snprintf (cmd, sizeof (cmd),
101 NONULL (MuttDotlock),
102 flags & DL_FL_TRY ? "-t " : "",
103 flags & DL_FL_UNLOCK ? "-u " : "",
104 flags & DL_FL_USEPRIV ? "-p " : "",
105 flags & DL_FL_FORCE ? "-f " : "",
106 flags & DL_FL_UNLINK ? "-d " : "",
107 flags & DL_FL_RETRY ? r : "", f);
109 return mutt_system (cmd);
114 #define invoke_dotlock dotlock_invoke
118 static int dotlock_file (const char *path, int fd, int retry)
121 int flags = DL_FL_USEPRIV | DL_FL_RETRY;
127 if ((r = invoke_dotlock (path, fd, flags, retry)) == DL_EX_EXIST) {
128 if (!option (OPTNOCURSES)) {
129 char msg[LONG_STRING];
131 snprintf (msg, sizeof (msg),
132 _("Lock count exceeded, remove lock for %s?"), path);
133 if (retry && mutt_yesorno (msg, M_YES) == M_YES) {
134 flags |= DL_FL_FORCE;
141 mutt_error (_("Can't dotlock %s.\n"), path);
144 return (r == DL_EX_OK ? 0 : -1);
147 static int undotlock_file (const char *path, int fd)
149 return (invoke_dotlock (path, fd, DL_FL_USEPRIV | DL_FL_UNLOCK, 0) ==
153 #endif /* USE_DOTLOCK */
155 /* looks up index of type for path in MailboxFormats */
156 static int mx_get_idx (const char* path) {
160 /* first, test all non-local folders to avoid stat() call */
161 for (i = 0; i < MailboxFormats->length; i++) {
162 if (!MX_COMMAND(i,local))
163 t = MX_COMMAND(i,mx_is_magic)(path, NULL);
167 if (stat (path, &st) == 0) {
168 /* if stat() succeeded, keep testing until success and
169 * pass stat() info so that we only need to do it once */
170 for (i = 0; i < MailboxFormats->length; i++) {
171 if (MX_COMMAND(i,local))
172 t = MX_COMMAND(i,mx_is_magic)(path, &st);
181 * excl if excl != 0, request an exclusive lock
182 * dot if dot != 0, try to dotlock the file
183 * timeout should retry locking?
185 int mx_lock_file (const char *path, int fd, int excl, int dot, int timeout)
187 #if defined (USE_FCNTL) || defined (USE_FLOCK)
199 lck.l_type = excl ? F_WRLCK : F_RDLCK;
200 lck.l_whence = SEEK_SET;
205 while (fcntl (fd, F_SETLK, &lck) == -1) {
208 debug_print (1, ("fcntl errno %d.\n", errno));
209 if (errno != EAGAIN && errno != EACCES) {
210 mutt_perror ("fcntl");
214 if (fstat (fd, &sb) != 0)
220 /* only unlock file if it is unchanged */
221 if (prev_sb.st_size == sb.st_size
222 && ++count >= (timeout ? MAXLOCKATTEMPT : 0)) {
224 mutt_error _("Timeout exceeded while attempting fcntl lock!");
231 mutt_message (_("Waiting for fcntl lock... %d"), ++attempt);
234 #endif /* USE_FCNTL */
239 while (flock (fd, (excl ? LOCK_EX : LOCK_SH) | LOCK_NB) == -1) {
242 if (errno != EWOULDBLOCK) {
243 mutt_perror ("flock");
248 if (fstat (fd, &sb) != 0)
254 /* only unlock file if it is unchanged */
255 if (prev_sb.st_size == sb.st_size
256 && ++count >= (timeout ? MAXLOCKATTEMPT : 0)) {
258 mutt_error _("Timeout exceeded while attempting flock lock!");
266 mutt_message (_("Waiting for flock attempt... %d"), ++attempt);
269 #endif /* USE_FLOCK */
273 r = dotlock_file (path, fd, timeout);
274 #endif /* USE_DOTLOCK */
277 /* release any other locks obtained in this routine */
280 lck.l_type = F_UNLCK;
281 fcntl (fd, F_SETLK, &lck);
282 #endif /* USE_FCNTL */
286 #endif /* USE_FLOCK */
294 int mx_unlock_file (const char *path, int fd, int dot)
297 struct flock unlockit;
299 p_clear(&unlockit, 1);
300 unlockit.l_type = F_UNLCK;
301 unlockit.l_whence = SEEK_SET;
302 fcntl (fd, F_SETLK, &unlockit);
311 undotlock_file (path, fd);
317 void mx_unlink_empty (const char *path)
325 if ((fd = open (path, O_RDWR)) == -1)
328 if (mx_lock_file (path, fd, 1, 0, 1) == -1) {
334 invoke_dotlock (path, fd, DL_FL_UNLINK, 1);
336 if (fstat (fd, &sb) == 0 && sb.st_size == 0)
340 mx_unlock_file (path, fd, 0);
344 /* try to figure out what type of mailbox ``path'' is */
345 int mx_get_magic (const char *path) {
348 if (m_strlen(path) == 0)
350 if ((i = mx_get_idx (path)) >= 0)
351 return (MX_COMMAND(i,type));
355 int mx_is_local (int m) {
358 return (MX_COMMAND(m,local));
362 * set DefaultMagic to the given value
364 int mx_set_magic (const char *s)
366 if (ascii_strcasecmp (s, "mbox") == 0)
367 DefaultMagic = M_MBOX;
368 else if (ascii_strcasecmp (s, "mmdf") == 0)
369 DefaultMagic = M_MMDF;
370 else if (ascii_strcasecmp (s, "mh") == 0)
372 else if (ascii_strcasecmp (s, "maildir") == 0)
373 DefaultMagic = M_MAILDIR;
380 /* mx_access: Wrapper for access, checks permissions on a given mailbox.
381 * We may be interested in using ACL-style flags at some point, currently
382 * we use the normal access() flags. */
383 int mx_access (const char *path, int flags)
387 if ((i = mx_get_idx (path)) >= 0 && MX_COMMAND(i,mx_access))
388 return (MX_COMMAND(i,mx_access)(path,flags));
392 static int mx_open_mailbox_append (CONTEXT * ctx, int flags)
396 #ifdef USE_COMPRESSED
397 /* special case for appending to compressed folders -
398 * even if we can not open them for reading */
399 if (mutt_can_append_compressed (ctx->path))
400 mutt_open_append_compressed (ctx);
407 if (mx_get_magic (ctx->path) == M_IMAP)
408 return imap_open_mailbox_append (ctx);
412 if (stat (ctx->path, &sb) == 0) {
413 ctx->magic = mx_get_magic (ctx->path);
415 switch (ctx->magic) {
417 mutt_error (_("%s is not a mailbox."), ctx->path);
423 else if (errno == ENOENT) {
424 ctx->magic = DefaultMagic;
426 if (ctx->magic == M_MH || ctx->magic == M_MAILDIR) {
427 char tmp[_POSIX_PATH_MAX];
429 if (mkdir (ctx->path, S_IRWXU)) {
430 mutt_perror (ctx->path);
434 if (ctx->magic == M_MAILDIR) {
435 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
436 if (mkdir (tmp, S_IRWXU)) {
442 snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
443 if (mkdir (tmp, S_IRWXU)) {
445 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
450 snprintf (tmp, sizeof (tmp), "%s/tmp", ctx->path);
451 if (mkdir (tmp, S_IRWXU)) {
453 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
455 snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
464 snprintf (tmp, sizeof (tmp), "%s/.mh_sequences", ctx->path);
465 if ((i = creat (tmp, S_IRWXU)) == -1) {
475 mutt_perror (ctx->path);
479 switch (ctx->magic) {
483 safe_fopen (ctx->path, flags & M_NEWFOLDER ? "w" : "a")) == NULL
484 || mbox_lock_mailbox (ctx, 1, 1) != 0) {
486 mutt_perror (ctx->path);
488 mutt_error (_("Couldn't lock %s\n"), ctx->path);
489 safe_fclose (&ctx->fp);
493 fseeko (ctx->fp, 0, 2);
509 * open a mailbox and parse it
512 * flags M_NOSORT do not sort mailbox
513 * M_APPEND open mailbox for appending
514 * M_READONLY open mailbox in read-only mode
515 * M_QUIET only print error messages
516 * ctx if non-null, context struct to use
518 CONTEXT *mx_open_mailbox (const char *path, int flags, CONTEXT * pctx)
524 ctx = p_new(CONTEXT, 1);
526 ctx->path = m_strdup(path);
528 ctx->msgnotreadyet = -1;
533 if (flags & M_READONLY)
538 if (flags & (M_APPEND | M_NEWFOLDER)) {
539 if (mx_open_mailbox_append (ctx, flags) != 0) {
540 mx_fastclose_mailbox (ctx);
548 if (!MX_IDX(ctx->magic-1))
549 ctx->magic = mx_get_magic (path);
551 #ifdef USE_COMPRESSED
552 if (ctx->magic == M_COMPRESSED)
553 mutt_open_read_compressed (ctx);
557 mutt_error (_("%s is not a mailbox."), path);
559 if (ctx->magic == -1)
562 if (ctx->magic <= 0) {
563 mx_fastclose_mailbox (ctx);
569 /* if the user has a `push' command in their .muttrc, or in a folder-hook,
570 * it will cause the progress messages not to be displayed because
571 * mutt_refresh() will think we are in the middle of a macro. so set a
572 * flag to indicate that we should really refresh the screen.
574 set_option (OPTFORCEREFRESH);
577 mutt_message (_("Reading %s..."), ctx->path);
579 rc = MX_COMMAND(ctx->magic-1,mx_open_mailbox)(ctx);
582 if ((flags & M_NOSORT) == 0) {
583 /* avoid unnecessary work since the mailbox is completely unthreaded
585 unset_option (OPTSORTSUBTHREADS);
586 unset_option (OPTNEEDRESCORE);
587 mutt_sort_headers (ctx, 1);
593 mx_fastclose_mailbox (ctx);
598 unset_option (OPTFORCEREFRESH);
602 /* free up memory associated with the mailbox context */
603 void mx_fastclose_mailbox (CONTEXT * ctx)
610 if (MX_IDX(ctx->magic-1) && MX_COMMAND(ctx->magic-1,mx_fastclose_mailbox))
611 MX_COMMAND(ctx->magic-1,mx_fastclose_mailbox(ctx));
613 hash_destroy (&ctx->subj_hash, NULL);
615 hash_destroy (&ctx->id_hash, NULL);
616 mutt_clear_threads (ctx);
617 for (i = 0; i < ctx->msgcount; i++)
618 mutt_free_header (&ctx->hdrs[i]);
619 p_delete(&ctx->hdrs);
621 #ifdef USE_COMPRESSED
622 if (ctx->compressinfo)
623 mutt_fast_close_compressed (ctx);
625 p_delete(&ctx->path);
626 p_delete(&ctx->pattern);
627 if (ctx->limit_pattern)
628 mutt_pattern_free (&ctx->limit_pattern);
629 safe_fclose (&ctx->fp);
633 /* save changes to disk */
634 static int sync_mailbox (CONTEXT * ctx, int *index_hint)
639 mutt_message (_("Writing %s..."), ctx->path);
641 if (MX_IDX(ctx->magic-1))
642 /* the 1 is only of interest for IMAP and means EXPUNGE */
643 rc = MX_COMMAND(ctx->magic-1,mx_sync_mailbox(ctx,1,index_hint));
645 #ifdef USE_COMPRESSED
646 if (rc == 0 && ctx->compressinfo)
647 return mutt_sync_compressed (ctx);
653 /* move deleted mails to the trash folder */
654 static int trash_append (CONTEXT * ctx)
660 if (!TrashPath || !ctx->deleted ||
661 (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))
664 for (; i < ctx->msgcount && (!ctx->hdrs[i]->deleted ||
665 ctx->hdrs[i]->appended); i++);
666 if (i == ctx->msgcount)
667 return 0; /* nothing to be done */
669 if (mutt_save_confirm (TrashPath, &st) != 0) {
670 mutt_error _("message(s) not deleted");
675 if (lstat (ctx->path, &stc) == 0 && stc.st_ino == st.st_ino
676 && stc.st_dev == st.st_dev && stc.st_rdev == st.st_rdev)
677 return 0; /* we are in the trash folder: simple sync */
679 if ((ctx_trash = mx_open_mailbox (TrashPath, M_APPEND, NULL)) != NULL) {
680 for (i = 0; i < ctx->msgcount; i++)
681 if (ctx->hdrs[i]->deleted && !ctx->hdrs[i]->appended
682 && !ctx->hdrs[i]->purged
683 && mutt_append_message (ctx_trash, ctx, ctx->hdrs[i], 0, 0) == -1) {
684 mx_close_mailbox (ctx_trash, NULL);
688 mx_close_mailbox (ctx_trash, NULL);
691 mutt_error _("Can't open trash folder");
699 /* save changes and close mailbox */
700 static int _mx_close_mailbox (CONTEXT * ctx, int *index_hint)
702 int i, move_messages = 0, purge = 1, read_msgs = 0;
706 char mbox[_POSIX_PATH_MAX];
707 char buf[SHORT_STRING];
715 if (ctx->magic == M_NNTP) {
718 ret = nntp_close_mailbox (ctx);
719 mx_fastclose_mailbox (ctx);
723 if (ctx->readonly || ctx->dontwrite) {
724 /* mailbox is readonly or we don't want to write */
725 mx_fastclose_mailbox (ctx);
730 /* mailbox was opened in write-mode */
731 if (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
732 mbox_close_mailbox (ctx);
734 mx_fastclose_mailbox (ctx);
738 for (i = 0; i < ctx->msgcount; i++) {
739 if (!ctx->hdrs[i]->deleted && ctx->hdrs[i]->read
740 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
744 if (read_msgs && quadoption (OPT_MOVE) != M_NO) {
747 if ((p = mutt_find_hook (M_MBOXHOOK, ctx->path))) {
749 m_strcpy(mbox, sizeof(mbox), p);
752 m_strcpy(mbox, sizeof(mbox), NONULL(Inbox));
753 isSpool = mutt_is_spool (ctx->path) && !mutt_is_spool (mbox);
755 mutt_expand_path (mbox, sizeof (mbox));
758 snprintf (buf, sizeof (buf), _("Move read messages to %s?"), mbox);
759 if ((move_messages = query_quadoption (OPT_MOVE, buf)) == -1) {
767 * There is no point in asking whether or not to purge if we are
768 * just marking messages as "trash".
770 if (ctx->deleted && !(ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH))) {
771 snprintf (buf, sizeof (buf), ctx->deleted == 1
772 ? _("Purge %d deleted message?") :
773 _("Purge %d deleted messages?"), ctx->deleted);
774 if ((purge = query_quadoption (OPT_DELETE, buf)) < 0) {
781 /* IMAP servers manage the OLD flag themselves */
782 if (ctx->magic != M_IMAP)
784 if (option (OPTMARKOLD)) {
785 for (i = 0; i < ctx->msgcount; i++) {
786 if (!ctx->hdrs[i]->deleted && !ctx->hdrs[i]->old)
787 mutt_set_flag (ctx, ctx->hdrs[i], M_OLD, 1);
792 mutt_message (_("Moving read messages to %s..."), mbox);
795 /* try to use server-side copy first */
798 if (ctx->magic == M_IMAP && imap_is_magic (mbox, NULL) == M_IMAP) {
799 /* tag messages for moving, and clear old tags, if any */
800 for (i = 0; i < ctx->msgcount; i++)
801 if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted
802 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
803 ctx->hdrs[i]->tagged = 1;
805 ctx->hdrs[i]->tagged = 0;
807 i = imap_copy_messages (ctx, NULL, mbox, 1);
810 if (i == 0) /* success */
812 else if (i == -1) { /* horrible error, bail */
816 else /* use regular append-copy mode */
819 if (mx_open_mailbox (mbox, M_APPEND, &f) == NULL) {
824 for (i = 0; i < ctx->msgcount; i++) {
825 if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted
826 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED))) {
827 if (mutt_append_message (&f, ctx, ctx->hdrs[i], 0, CH_UPDATE_LEN) ==
829 mutt_set_flag (ctx, ctx->hdrs[i], M_DELETE, 1);
830 mutt_set_flag (ctx, ctx->hdrs[i], M_APPENDED, 1);
833 mx_close_mailbox (&f, NULL);
840 mx_close_mailbox (&f, NULL);
844 else if (!ctx->changed && ctx->deleted == 0) {
845 mutt_message _("Mailbox is unchanged.");
847 mx_fastclose_mailbox (ctx);
851 /* copy mails to the trash before expunging */
852 if (purge && ctx->deleted)
853 if (trash_append (ctx) != 0) {
859 /* allow IMAP to preserve the deleted flag across sessions */
860 if (ctx->magic == M_IMAP) {
861 if ((check = imap_sync_mailbox (ctx, purge, index_hint)) != 0) {
870 for (i = 0; i < ctx->msgcount; i++)
871 ctx->hdrs[i]->deleted = 0;
875 if (ctx->changed || ctx->deleted) {
876 if ((check = sync_mailbox (ctx, index_hint)) != 0) {
884 mutt_message (_("%d kept, %d moved, %d deleted."),
885 ctx->msgcount - ctx->deleted, read_msgs, ctx->deleted);
887 mutt_message (_("%d kept, %d deleted."),
888 ctx->msgcount - ctx->deleted, ctx->deleted);
890 if (ctx->msgcount == ctx->deleted &&
891 (ctx->magic == M_MMDF || ctx->magic == M_MBOX) &&
892 !mutt_is_spool (ctx->path) && !option (OPTSAVEEMPTY))
893 mx_unlink_empty (ctx->path);
895 #ifdef USE_COMPRESSED
896 if (ctx->compressinfo && mutt_slow_close_compressed (ctx))
900 mx_fastclose_mailbox (ctx);
905 int mx_close_mailbox (CONTEXT * ctx, int *index_hint) {
909 ret = _mx_close_mailbox (ctx, index_hint);
910 sidebar_set_buffystats (ctx);
914 /* update a Context structure's internal tables. */
916 void mx_update_tables (CONTEXT * ctx, int committing)
920 /* update memory to reflect the new state of the mailbox */
929 #define this_body ctx->hdrs[j]->content
930 for (i = 0, j = 0; i < ctx->msgcount; i++) {
931 if ((committing && (!ctx->hdrs[i]->deleted ||
932 (ctx->magic == M_MAILDIR
933 && option (OPTMAILDIRTRASH)))) || (!committing
937 ctx->hdrs[j] = ctx->hdrs[i];
940 ctx->hdrs[j]->msgno = j;
941 if (ctx->hdrs[j]->virtual != -1) {
942 ctx->v2r[ctx->vcount] = j;
943 ctx->hdrs[j]->virtual = ctx->vcount++;
944 ctx->vsize += this_body->length + this_body->offset -
945 this_body->hdr_offset;
949 ctx->hdrs[j]->changed = 0;
950 else if (ctx->hdrs[j]->changed)
954 || (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH))) {
955 if (ctx->hdrs[j]->deleted)
959 if (ctx->hdrs[j]->tagged)
961 if (ctx->hdrs[j]->flagged)
963 if (!ctx->hdrs[j]->read) {
965 if (!ctx->hdrs[j]->old)
972 if (ctx->magic == M_MH || ctx->magic == M_MAILDIR)
973 ctx->size -= (ctx->hdrs[i]->content->length +
974 ctx->hdrs[i]->content->offset -
975 ctx->hdrs[i]->content->hdr_offset);
976 /* remove message from the hash tables */
977 if (ctx->subj_hash && ctx->hdrs[i]->env->real_subj)
978 hash_delete (ctx->subj_hash, ctx->hdrs[i]->env->real_subj,
980 if (ctx->id_hash && ctx->hdrs[i]->env->message_id)
981 hash_delete (ctx->id_hash, ctx->hdrs[i]->env->message_id,
983 mutt_free_header (&ctx->hdrs[i]);
989 /* update sidebar count */
990 sidebar_set_buffystats (ctx);
994 /* save changes to mailbox
1000 static int _mx_sync_mailbox (CONTEXT * ctx, int *index_hint)
1004 int msgcount, deleted;
1006 if (ctx->dontwrite) {
1007 char buf[STRING], tmp[STRING];
1009 if (km_expand_key (buf, sizeof (buf),
1010 km_find_func (MENU_MAIN, OP_TOGGLE_WRITE)))
1011 snprintf (tmp, sizeof (tmp), _(" Press '%s' to toggle write"), buf);
1013 m_strcpy(tmp, sizeof(tmp), _("Use 'toggle-write' to re-enable write!"));
1015 mutt_error (_("Mailbox is marked unwritable. %s"), tmp);
1018 else if (ctx->readonly) {
1019 mutt_error _("Mailbox is read-only.");
1024 if (!ctx->changed && !ctx->deleted) {
1025 mutt_message _("Mailbox is unchanged.");
1031 char buf[SHORT_STRING];
1033 snprintf (buf, sizeof (buf), ctx->deleted == 1
1034 ? _("Purge %d deleted message?") :
1035 _("Purge %d deleted messages?"), ctx->deleted);
1036 if ((purge = query_quadoption (OPT_DELETE, buf)) < 0)
1038 else if (purge == M_NO) {
1040 return 0; /* nothing to do! */
1042 /* let IMAP servers hold on to D flags */
1043 if (ctx->magic != M_IMAP)
1046 for (i = 0; i < ctx->msgcount; i++)
1047 ctx->hdrs[i]->deleted = 0;
1051 else if (ctx->last_tag && ctx->last_tag->deleted)
1052 ctx->last_tag = NULL; /* reset last tagged msg now useless */
1055 /* really only for IMAP - imap_sync_mailbox results in a call to
1056 * mx_update_tables, so ctx->deleted is 0 when it comes back */
1057 msgcount = ctx->msgcount;
1058 deleted = ctx->deleted;
1060 if (purge && ctx->deleted) {
1061 if (trash_append (ctx) == -1)
1066 if (ctx->magic == M_IMAP)
1067 rc = imap_sync_mailbox (ctx, purge, index_hint);
1070 rc = sync_mailbox (ctx, index_hint);
1073 if (ctx->magic == M_IMAP && !purge)
1074 mutt_message (_("Mailbox checkpointed."));
1078 mutt_message (_("%d kept, %d deleted."), msgcount - deleted, deleted);
1082 if (ctx->msgcount == ctx->deleted &&
1083 (ctx->magic == M_MBOX || ctx->magic == M_MMDF) &&
1084 !mutt_is_spool (ctx->path) && !option (OPTSAVEEMPTY)) {
1086 mx_fastclose_mailbox (ctx);
1090 /* if we haven't deleted any messages, we don't need to resort */
1091 /* ... except for certain folder formats which need "unsorted"
1092 * sort order in order to synchronize folders.
1094 * MH and maildir are safe. mbox-style seems to need re-sorting,
1095 * at least with the new threading code.
1097 if (purge || (ctx->magic != M_MAILDIR && ctx->magic != M_MH)) {
1099 /* IMAP does this automatically after handling EXPUNGE */
1100 if (ctx->magic != M_IMAP)
1103 mx_update_tables (ctx, 1);
1104 mutt_sort_headers (ctx, 1); /* rethread from scratch */
1112 int mx_sync_mailbox (CONTEXT* ctx, int* index_hint) {
1113 int ret = _mx_sync_mailbox (ctx, index_hint);
1114 sidebar_set_buffystats (ctx);
1119 * dest destintation mailbox
1120 * hdr message being copied (required for maildir support, because
1121 * the filename depends on the message flags)
1123 MESSAGE *mx_open_new_message (CONTEXT * dest, HEADER * hdr, int flags)
1126 address_t *p = NULL;
1128 if (!MX_IDX(dest->magic-1)) {
1129 debug_print (1, ("function unimplemented for mailbox type %d.\n", dest->magic));
1133 msg = p_new(MESSAGE, 1);
1134 msg->magic = dest->magic;
1138 msg->flags.flagged = hdr->flagged;
1139 msg->flags.replied = hdr->replied;
1140 msg->flags.read = hdr->read;
1141 msg->received = hdr->received;
1144 if (msg->received == 0)
1145 time (&msg->received);
1147 if (MX_COMMAND(dest->magic-1,mx_open_new_message)(msg, dest, hdr) == 0) {
1148 if (dest->magic == M_MMDF)
1149 fputs (MMDF_SEP, msg->fp);
1151 if ((msg->magic == M_MBOX || msg->magic == M_MMDF) && flags & M_ADD_FROM) {
1153 if (hdr->env->return_path)
1154 p = hdr->env->return_path;
1155 else if (hdr->env->sender)
1156 p = hdr->env->sender;
1161 fprintf (msg->fp, "From %s %s", p ? p->mailbox : NONULL (Username),
1162 ctime (&msg->received));
1171 /* check for new mail */
1172 int mx_check_mailbox (CONTEXT * ctx, int *index_hint, int lock) {
1173 #ifdef USE_COMPRESSED
1174 if (ctx->compressinfo)
1175 return mutt_check_mailbox_compressed (ctx);
1181 if (MX_IDX(ctx->magic-1) && MX_COMMAND(ctx->magic-1,mx_check_mailbox))
1182 return (MX_COMMAND(ctx->magic-1,mx_check_mailbox)(ctx, index_hint, lock));
1185 debug_print (1, ("null or invalid context.\n"));
1190 /* return a stream pointer for a message */
1191 MESSAGE *mx_open_message (CONTEXT * ctx, int msgno)
1195 msg = p_new(MESSAGE, 1);
1196 switch (msg->magic = ctx->magic) {
1205 HEADER *cur = ctx->hdrs[msgno];
1206 char path[_POSIX_PATH_MAX];
1208 snprintf (path, sizeof (path), "%s/%s", ctx->path, cur->path);
1210 if ((msg->fp = fopen (path, "r")) == NULL && errno == ENOENT &&
1211 ctx->magic == M_MAILDIR)
1212 msg->fp = maildir_open_find_message (ctx->path, cur->path);
1214 if (msg->fp == NULL) {
1216 debug_print (1, ("fopen: %s: %s (errno %d).\n", path, strerror (errno), errno));
1225 if (imap_fetch_message (msg, ctx, msgno) != 0)
1229 #endif /* USE_IMAP */
1234 if (pop_fetch_message (msg, ctx, msgno) != 0)
1238 #endif /* USE_POP */
1243 if (nntp_fetch_message (msg, ctx, msgno) != 0)
1247 #endif /* USE_NNTP */
1250 debug_print (1, ("function not implemented for mailbox type %d.\n", ctx->magic));
1257 /* commit a message to a folder */
1259 int mx_commit_message (MESSAGE * msg, CONTEXT * ctx) {
1260 if (!(msg->write && ctx->append)) {
1261 debug_print (1, ("msg->write = %d, ctx->append = %d\n", msg->write, ctx->append));
1264 if (!ctx || !MX_IDX(ctx->magic-1) || !MX_COMMAND(ctx->magic-1,mx_commit_message))
1266 return (MX_COMMAND(ctx->magic-1,mx_commit_message) (msg, ctx));
1269 /* close a pointer to a message */
1270 int mx_close_message (MESSAGE ** msg)
1274 if ((*msg)->magic == M_MH || (*msg)->magic == M_MAILDIR
1276 || (*msg)->magic == M_IMAP
1279 || (*msg)->magic == M_POP
1282 || (*msg)->magic == M_NNTP
1285 r = safe_fclose (&(*msg)->fp);
1291 debug_print (1, ("unlinking %s\n", (*msg)->path));
1292 unlink ((*msg)->path);
1293 p_delete(&(*msg)->path);
1300 void mx_alloc_memory (CONTEXT * ctx)
1303 size_t s = MAX (sizeof (HEADER *), sizeof (int));
1305 if ((ctx->hdrmax + 25) * s < ctx->hdrmax * s) {
1306 mutt_error _("Integer overflow -- can't allocate memory.");
1313 p_realloc(&ctx->hdrs, ctx->hdrmax += 25);
1314 p_realloc(&ctx->v2r, ctx->hdrmax);
1317 ctx->hdrs = p_new(HEADER *, (ctx->hdrmax += 25));
1318 ctx->v2r = p_new(int, ctx->hdrmax);
1320 for (i = ctx->msgcount; i < ctx->hdrmax; i++) {
1321 ctx->hdrs[i] = NULL;
1326 /* this routine is called to update the counts in the context structure for
1327 * the last message header parsed.
1329 void mx_update_context (CONTEXT * ctx, int new_messages)
1334 for (msgno = ctx->msgcount - new_messages; msgno < ctx->msgcount; msgno++) {
1335 h = ctx->hdrs[msgno];
1338 /* NOTE: this _must_ be done before the check for mailcap! */
1339 h->security = crypt_query (h->content);
1342 if (!ctx->pattern) {
1343 ctx->v2r[ctx->vcount] = msgno;
1344 h->virtual = ctx->vcount++;
1350 if (h->env->supersedes) {
1354 ctx->id_hash = mutt_make_id_hash (ctx);
1356 h2 = hash_find (ctx->id_hash, h->env->supersedes);
1358 /* p_delete(&h->env->supersedes); should I ? */
1361 if (!ctx->counting && option (OPTSCORE))
1362 mutt_score_message (ctx, h2, 1);
1366 /* add this message to the hash tables */
1367 if (ctx->id_hash && h->env->message_id)
1368 hash_insert (ctx->id_hash, h->env->message_id, h, 0);
1369 if (!ctx->counting) {
1370 if (ctx->subj_hash && h->env->real_subj)
1371 hash_insert (ctx->subj_hash, h->env->real_subj, h, 1);
1373 if (option (OPTSCORE))
1374 mutt_score_message (ctx, h, 0);
1389 /* update sidebar count */
1390 sidebar_set_buffystats (ctx);
1395 * 1 if the specified mailbox contains 0 messages.
1396 * 0 if the mailbox contains messages
1399 int mx_check_empty (const char *path)
1402 if ((i = mx_get_idx (path)) >= 0 && MX_COMMAND(i,mx_check_empty))
1403 return (MX_COMMAND(i,mx_check_empty)(path));
1408 int mx_acl_check (CONTEXT* ctx, int flag) {
1409 if (!ctx || !MX_IDX(ctx->magic-1))
1411 /* if no acl_check defined for module, assume permission is granted */
1412 if (!MX_COMMAND(ctx->magic-1,mx_acl_check))
1414 return (MX_COMMAND(ctx->magic-1,mx_acl_check)(ctx,flag));
1417 void mx_init (void) {
1421 list_push_back (&MailboxFormats, (void*) mbox_reg_mx ());
1422 list_push_back (&MailboxFormats, (void*) mmdf_reg_mx ());
1423 list_push_back (&MailboxFormats, (void*) mh_reg_mx ());
1424 list_push_back (&MailboxFormats, (void*) maildir_reg_mx ());
1426 list_push_back (&MailboxFormats, (void*) imap_reg_mx ());
1429 list_push_back (&MailboxFormats, (void*) pop_reg_mx ());
1432 list_push_back (&MailboxFormats, (void*) nntp_reg_mx ());
1434 #ifdef USE_COMPRESSED
1435 list_push_back (&MailboxFormats, (void*) compress_reg_mx ());
1438 /* check module registration for completeness with debug versions */
1439 #define EXITWITHERR(m) do { fprintf(stderr, "error: incomplete mx module: %s is missing for type %i\n",m,i);exit(1); } while (0)
1440 for (i = 0; i < MailboxFormats->length; i++) {
1441 if (MX_COMMAND(i,type) < 1) EXITWITHERR("type");
1442 if (!MX_COMMAND(i,mx_is_magic)) EXITWITHERR("mx_is_magic");
1443 if (!MX_COMMAND(i,mx_open_mailbox)) EXITWITHERR("mx_open_mailbox");
1444 /* if (!MX_COMMAND(i,mx_sync_mailbox)) EXITWITHERR("mx_sync_mailbox");*/
1450 int mx_rebuild_cache (void) {
1452 mutt_error (_("Support for header caching was not build in."));
1455 int i = 0, magic = 0;
1456 CONTEXT* ctx = NULL;
1459 if (list_empty(Incoming)) {
1460 mutt_error (_("No mailboxes defined."));
1464 for (i = 0; i < Incoming->length; i++) {
1465 b = (BUFFY*) Incoming->data[i];
1466 magic = mx_get_magic (b->path);
1467 if (magic != M_MAILDIR && magic != M_MH
1473 sidebar_set_current (b->path);
1474 sidebar_draw (CurrentMenu);
1475 if ((ctx = mx_open_mailbox (b->path,
1476 M_READONLY | M_NOSORT | M_COUNT,
1478 mx_close_mailbox (ctx, 0);
1480 mutt_clear_error ();
1482 if (Context && Context->path)
1483 sidebar_set_current (Context->path);
1484 sidebar_draw (CurrentMenu);