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/macros.h>
37 #include "imap/imap.h"
38 #include "imap/mx_imap.h"
43 #include "pop/mx_pop.h"
47 #include "nntp/nntp.h"
48 #include "nntp/mx_nntp.h"
55 #include "mutt_crypt.h"
59 #include "lib/debug.h"
72 static list2_t* MailboxFormats = NULL;
73 #define MX_COMMAND(idx,cmd) ((mx_t*) MailboxFormats->data[idx])->cmd
74 #define MX_IDX(idx) (idx >= 0 && idx < MailboxFormats->length)
76 #define mutt_is_spool(s) (str_cmp (Spoolfile, s) == 0)
81 * retry - should retry if unable to lock?
86 static int invoke_dotlock (const char *path, int dummy, int flags, int retry)
88 char cmd[LONG_STRING + _POSIX_PATH_MAX];
89 char f[SHORT_STRING + _POSIX_PATH_MAX];
92 if (flags & DL_FL_RETRY)
93 snprintf (r, sizeof (r), "-r %d ", retry ? MAXLOCKATTEMPT : 0);
95 mutt_quote_filename (f, sizeof (f), path);
97 snprintf (cmd, sizeof (cmd),
100 flags & DL_FL_TRY ? "-t " : "",
101 flags & DL_FL_UNLOCK ? "-u " : "",
102 flags & DL_FL_USEPRIV ? "-p " : "",
103 flags & DL_FL_FORCE ? "-f " : "",
104 flags & DL_FL_UNLINK ? "-d " : "",
105 flags & DL_FL_RETRY ? r : "", f);
107 return mutt_system (cmd);
112 #define invoke_dotlock dotlock_invoke
116 static int dotlock_file (const char *path, int fd, int retry)
119 int flags = DL_FL_USEPRIV | DL_FL_RETRY;
125 if ((r = invoke_dotlock (path, fd, flags, retry)) == DL_EX_EXIST) {
126 if (!option (OPTNOCURSES)) {
127 char msg[LONG_STRING];
129 snprintf (msg, sizeof (msg),
130 _("Lock count exceeded, remove lock for %s?"), path);
131 if (retry && mutt_yesorno (msg, M_YES) == M_YES) {
132 flags |= DL_FL_FORCE;
139 mutt_error (_("Can't dotlock %s.\n"), path);
142 return (r == DL_EX_OK ? 0 : -1);
145 static int undotlock_file (const char *path, int fd)
147 return (invoke_dotlock (path, fd, DL_FL_USEPRIV | DL_FL_UNLOCK, 0) ==
151 #endif /* USE_DOTLOCK */
153 /* looks up index of type for path in MailboxFormats */
154 static int mx_get_idx (const char* path) {
158 /* first, test all non-local folders to avoid stat() call */
159 for (i = 0; i < MailboxFormats->length; i++) {
160 if (!MX_COMMAND(i,local))
161 t = MX_COMMAND(i,mx_is_magic)(path, NULL);
165 if (stat (path, &st) == 0) {
166 /* if stat() succeeded, keep testing until success and
167 * pass stat() info so that we only need to do it once */
168 for (i = 0; i < MailboxFormats->length; i++) {
169 if (MX_COMMAND(i,local))
170 t = MX_COMMAND(i,mx_is_magic)(path, &st);
179 * excl if excl != 0, request an exclusive lock
180 * dot if dot != 0, try to dotlock the file
181 * timeout should retry locking?
183 int mx_lock_file (const char *path, int fd, int excl, int dot, int timeout)
185 #if defined (USE_FCNTL) || defined (USE_FLOCK)
196 memset (&lck, 0, sizeof (struct flock));
197 lck.l_type = excl ? F_WRLCK : F_RDLCK;
198 lck.l_whence = SEEK_SET;
203 while (fcntl (fd, F_SETLK, &lck) == -1) {
206 debug_print (1, ("fcntl errno %d.\n", errno));
207 if (errno != EAGAIN && errno != EACCES) {
208 mutt_perror ("fcntl");
212 if (fstat (fd, &sb) != 0)
218 /* only unlock file if it is unchanged */
219 if (prev_sb.st_size == sb.st_size
220 && ++count >= (timeout ? MAXLOCKATTEMPT : 0)) {
222 mutt_error _("Timeout exceeded while attempting fcntl lock!");
229 mutt_message (_("Waiting for fcntl lock... %d"), ++attempt);
232 #endif /* USE_FCNTL */
237 while (flock (fd, (excl ? LOCK_EX : LOCK_SH) | LOCK_NB) == -1) {
240 if (errno != EWOULDBLOCK) {
241 mutt_perror ("flock");
246 if (fstat (fd, &sb) != 0)
252 /* only unlock file if it is unchanged */
253 if (prev_sb.st_size == sb.st_size
254 && ++count >= (timeout ? MAXLOCKATTEMPT : 0)) {
256 mutt_error _("Timeout exceeded while attempting flock lock!");
264 mutt_message (_("Waiting for flock attempt... %d"), ++attempt);
267 #endif /* USE_FLOCK */
271 r = dotlock_file (path, fd, timeout);
272 #endif /* USE_DOTLOCK */
275 /* release any other locks obtained in this routine */
278 lck.l_type = F_UNLCK;
279 fcntl (fd, F_SETLK, &lck);
280 #endif /* USE_FCNTL */
284 #endif /* USE_FLOCK */
292 int mx_unlock_file (const char *path, int fd, int dot)
295 struct flock unlockit;
297 memset (&unlockit, 0, sizeof (struct flock));
298 unlockit.l_type = F_UNLCK;
299 unlockit.l_whence = SEEK_SET;
300 fcntl (fd, F_SETLK, &unlockit);
309 undotlock_file (path, fd);
315 void mx_unlink_empty (const char *path)
323 if ((fd = open (path, O_RDWR)) == -1)
326 if (mx_lock_file (path, fd, 1, 0, 1) == -1) {
332 invoke_dotlock (path, fd, DL_FL_UNLINK, 1);
334 if (fstat (fd, &sb) == 0 && sb.st_size == 0)
338 mx_unlock_file (path, fd, 0);
342 /* try to figure out what type of mailbox ``path'' is */
343 int mx_get_magic (const char *path) {
346 if (str_len (path) == 0)
348 if ((i = mx_get_idx (path)) >= 0)
349 return (MX_COMMAND(i,type));
353 int mx_is_local (int m) {
356 return (MX_COMMAND(m,local));
360 * set DefaultMagic to the given value
362 int mx_set_magic (const char *s)
364 if (ascii_strcasecmp (s, "mbox") == 0)
365 DefaultMagic = M_MBOX;
366 else if (ascii_strcasecmp (s, "mmdf") == 0)
367 DefaultMagic = M_MMDF;
368 else if (ascii_strcasecmp (s, "mh") == 0)
370 else if (ascii_strcasecmp (s, "maildir") == 0)
371 DefaultMagic = M_MAILDIR;
378 /* mx_access: Wrapper for access, checks permissions on a given mailbox.
379 * We may be interested in using ACL-style flags at some point, currently
380 * we use the normal access() flags. */
381 int mx_access (const char *path, int flags)
385 if ((i = mx_get_idx (path)) >= 0 && MX_COMMAND(i,mx_access))
386 return (MX_COMMAND(i,mx_access)(path,flags));
390 static int mx_open_mailbox_append (CONTEXT * ctx, int flags)
394 #ifdef USE_COMPRESSED
395 /* special case for appending to compressed folders -
396 * even if we can not open them for reading */
397 if (mutt_can_append_compressed (ctx->path))
398 mutt_open_append_compressed (ctx);
405 if (mx_get_magic (ctx->path) == M_IMAP)
406 return imap_open_mailbox_append (ctx);
410 if (stat (ctx->path, &sb) == 0) {
411 ctx->magic = mx_get_magic (ctx->path);
413 switch (ctx->magic) {
415 mutt_error (_("%s is not a mailbox."), ctx->path);
421 else if (errno == ENOENT) {
422 ctx->magic = DefaultMagic;
424 if (ctx->magic == M_MH || ctx->magic == M_MAILDIR) {
425 char tmp[_POSIX_PATH_MAX];
427 if (mkdir (ctx->path, S_IRWXU)) {
428 mutt_perror (ctx->path);
432 if (ctx->magic == M_MAILDIR) {
433 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
434 if (mkdir (tmp, S_IRWXU)) {
440 snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
441 if (mkdir (tmp, S_IRWXU)) {
443 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
448 snprintf (tmp, sizeof (tmp), "%s/tmp", ctx->path);
449 if (mkdir (tmp, S_IRWXU)) {
451 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
453 snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
462 snprintf (tmp, sizeof (tmp), "%s/.mh_sequences", ctx->path);
463 if ((i = creat (tmp, S_IRWXU)) == -1) {
473 mutt_perror (ctx->path);
477 switch (ctx->magic) {
481 safe_fopen (ctx->path, flags & M_NEWFOLDER ? "w" : "a")) == NULL
482 || mbox_lock_mailbox (ctx, 1, 1) != 0) {
484 mutt_perror (ctx->path);
486 mutt_error (_("Couldn't lock %s\n"), ctx->path);
487 safe_fclose (&ctx->fp);
491 fseeko (ctx->fp, 0, 2);
507 * open a mailbox and parse it
510 * flags M_NOSORT do not sort mailbox
511 * M_APPEND open mailbox for appending
512 * M_READONLY open mailbox in read-only mode
513 * M_QUIET only print error messages
514 * ctx if non-null, context struct to use
516 CONTEXT *mx_open_mailbox (const char *path, int flags, CONTEXT * pctx)
522 ctx = p_new(CONTEXT, 1);
524 ctx->path = str_dup (path);
526 ctx->msgnotreadyet = -1;
531 if (flags & M_READONLY)
536 if (flags & (M_APPEND | M_NEWFOLDER)) {
537 if (mx_open_mailbox_append (ctx, flags) != 0) {
538 mx_fastclose_mailbox (ctx);
546 if (!MX_IDX(ctx->magic-1))
547 ctx->magic = mx_get_magic (path);
549 #ifdef USE_COMPRESSED
550 if (ctx->magic == M_COMPRESSED)
551 mutt_open_read_compressed (ctx);
555 mutt_error (_("%s is not a mailbox."), path);
557 if (ctx->magic == -1)
560 if (ctx->magic <= 0) {
561 mx_fastclose_mailbox (ctx);
567 /* if the user has a `push' command in their .muttrc, or in a folder-hook,
568 * it will cause the progress messages not to be displayed because
569 * mutt_refresh() will think we are in the middle of a macro. so set a
570 * flag to indicate that we should really refresh the screen.
572 set_option (OPTFORCEREFRESH);
575 mutt_message (_("Reading %s..."), ctx->path);
577 rc = MX_COMMAND(ctx->magic-1,mx_open_mailbox)(ctx);
580 if ((flags & M_NOSORT) == 0) {
581 /* avoid unnecessary work since the mailbox is completely unthreaded
583 unset_option (OPTSORTSUBTHREADS);
584 unset_option (OPTNEEDRESCORE);
585 mutt_sort_headers (ctx, 1);
591 mx_fastclose_mailbox (ctx);
596 unset_option (OPTFORCEREFRESH);
600 /* free up memory associated with the mailbox context */
601 void mx_fastclose_mailbox (CONTEXT * ctx)
608 if (MX_IDX(ctx->magic-1) && MX_COMMAND(ctx->magic-1,mx_fastclose_mailbox))
609 MX_COMMAND(ctx->magic-1,mx_fastclose_mailbox(ctx));
611 hash_destroy (&ctx->subj_hash, NULL);
613 hash_destroy (&ctx->id_hash, NULL);
614 mutt_clear_threads (ctx);
615 for (i = 0; i < ctx->msgcount; i++)
616 mutt_free_header (&ctx->hdrs[i]);
617 p_delete(&ctx->hdrs);
619 #ifdef USE_COMPRESSED
620 if (ctx->compressinfo)
621 mutt_fast_close_compressed (ctx);
623 p_delete(&ctx->path);
624 p_delete(&ctx->pattern);
625 if (ctx->limit_pattern)
626 mutt_pattern_free (&ctx->limit_pattern);
627 safe_fclose (&ctx->fp);
628 memset (ctx, 0, sizeof (CONTEXT));
631 /* save changes to disk */
632 static int sync_mailbox (CONTEXT * ctx, int *index_hint)
637 mutt_message (_("Writing %s..."), ctx->path);
639 if (MX_IDX(ctx->magic-1))
640 /* the 1 is only of interest for IMAP and means EXPUNGE */
641 rc = MX_COMMAND(ctx->magic-1,mx_sync_mailbox(ctx,1,index_hint));
643 #ifdef USE_COMPRESSED
644 if (rc == 0 && ctx->compressinfo)
645 return mutt_sync_compressed (ctx);
651 /* move deleted mails to the trash folder */
652 static int trash_append (CONTEXT * ctx)
658 if (!TrashPath || !ctx->deleted ||
659 (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))
662 for (; i < ctx->msgcount && (!ctx->hdrs[i]->deleted ||
663 ctx->hdrs[i]->appended); i++);
664 if (i == ctx->msgcount)
665 return 0; /* nothing to be done */
667 if (mutt_save_confirm (TrashPath, &st) != 0) {
668 mutt_error _("message(s) not deleted");
673 if (lstat (ctx->path, &stc) == 0 && stc.st_ino == st.st_ino
674 && stc.st_dev == st.st_dev && stc.st_rdev == st.st_rdev)
675 return 0; /* we are in the trash folder: simple sync */
677 if ((ctx_trash = mx_open_mailbox (TrashPath, M_APPEND, NULL)) != NULL) {
678 for (i = 0; i < ctx->msgcount; i++)
679 if (ctx->hdrs[i]->deleted && !ctx->hdrs[i]->appended
680 && !ctx->hdrs[i]->purged
681 && mutt_append_message (ctx_trash, ctx, ctx->hdrs[i], 0, 0) == -1) {
682 mx_close_mailbox (ctx_trash, NULL);
686 mx_close_mailbox (ctx_trash, NULL);
689 mutt_error _("Can't open trash folder");
697 /* save changes and close mailbox */
698 static int _mx_close_mailbox (CONTEXT * ctx, int *index_hint)
700 int i, move_messages = 0, purge = 1, read_msgs = 0;
704 char mbox[_POSIX_PATH_MAX];
705 char buf[SHORT_STRING];
713 if (ctx->magic == M_NNTP) {
716 ret = nntp_close_mailbox (ctx);
717 mx_fastclose_mailbox (ctx);
721 if (ctx->readonly || ctx->dontwrite) {
722 /* mailbox is readonly or we don't want to write */
723 mx_fastclose_mailbox (ctx);
728 /* mailbox was opened in write-mode */
729 if (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
730 mbox_close_mailbox (ctx);
732 mx_fastclose_mailbox (ctx);
736 for (i = 0; i < ctx->msgcount; i++) {
737 if (!ctx->hdrs[i]->deleted && ctx->hdrs[i]->read
738 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
742 if (read_msgs && quadoption (OPT_MOVE) != M_NO) {
745 if ((p = mutt_find_hook (M_MBOXHOOK, ctx->path))) {
747 strfcpy (mbox, p, sizeof (mbox));
750 strfcpy (mbox, NONULL (Inbox), sizeof (mbox));
751 isSpool = mutt_is_spool (ctx->path) && !mutt_is_spool (mbox);
753 mutt_expand_path (mbox, sizeof (mbox));
756 snprintf (buf, sizeof (buf), _("Move read messages to %s?"), mbox);
757 if ((move_messages = query_quadoption (OPT_MOVE, buf)) == -1) {
765 * There is no point in asking whether or not to purge if we are
766 * just marking messages as "trash".
768 if (ctx->deleted && !(ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH))) {
769 snprintf (buf, sizeof (buf), ctx->deleted == 1
770 ? _("Purge %d deleted message?") :
771 _("Purge %d deleted messages?"), ctx->deleted);
772 if ((purge = query_quadoption (OPT_DELETE, buf)) < 0) {
779 /* IMAP servers manage the OLD flag themselves */
780 if (ctx->magic != M_IMAP)
782 if (option (OPTMARKOLD)) {
783 for (i = 0; i < ctx->msgcount; i++) {
784 if (!ctx->hdrs[i]->deleted && !ctx->hdrs[i]->old)
785 mutt_set_flag (ctx, ctx->hdrs[i], M_OLD, 1);
790 mutt_message (_("Moving read messages to %s..."), mbox);
793 /* try to use server-side copy first */
796 if (ctx->magic == M_IMAP && imap_is_magic (mbox, NULL) == M_IMAP) {
797 /* tag messages for moving, and clear old tags, if any */
798 for (i = 0; i < ctx->msgcount; i++)
799 if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted
800 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
801 ctx->hdrs[i]->tagged = 1;
803 ctx->hdrs[i]->tagged = 0;
805 i = imap_copy_messages (ctx, NULL, mbox, 1);
808 if (i == 0) /* success */
810 else if (i == -1) { /* horrible error, bail */
814 else /* use regular append-copy mode */
817 if (mx_open_mailbox (mbox, M_APPEND, &f) == NULL) {
822 for (i = 0; i < ctx->msgcount; i++) {
823 if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted
824 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED))) {
825 if (mutt_append_message (&f, ctx, ctx->hdrs[i], 0, CH_UPDATE_LEN) ==
827 mutt_set_flag (ctx, ctx->hdrs[i], M_DELETE, 1);
828 mutt_set_flag (ctx, ctx->hdrs[i], M_APPENDED, 1);
831 mx_close_mailbox (&f, NULL);
838 mx_close_mailbox (&f, NULL);
842 else if (!ctx->changed && ctx->deleted == 0) {
843 mutt_message _("Mailbox is unchanged.");
845 mx_fastclose_mailbox (ctx);
849 /* copy mails to the trash before expunging */
850 if (purge && ctx->deleted)
851 if (trash_append (ctx) != 0) {
857 /* allow IMAP to preserve the deleted flag across sessions */
858 if (ctx->magic == M_IMAP) {
859 if ((check = imap_sync_mailbox (ctx, purge, index_hint)) != 0) {
868 for (i = 0; i < ctx->msgcount; i++)
869 ctx->hdrs[i]->deleted = 0;
873 if (ctx->changed || ctx->deleted) {
874 if ((check = sync_mailbox (ctx, index_hint)) != 0) {
882 mutt_message (_("%d kept, %d moved, %d deleted."),
883 ctx->msgcount - ctx->deleted, read_msgs, ctx->deleted);
885 mutt_message (_("%d kept, %d deleted."),
886 ctx->msgcount - ctx->deleted, ctx->deleted);
888 if (ctx->msgcount == ctx->deleted &&
889 (ctx->magic == M_MMDF || ctx->magic == M_MBOX) &&
890 !mutt_is_spool (ctx->path) && !option (OPTSAVEEMPTY))
891 mx_unlink_empty (ctx->path);
893 #ifdef USE_COMPRESSED
894 if (ctx->compressinfo && mutt_slow_close_compressed (ctx))
898 mx_fastclose_mailbox (ctx);
903 int mx_close_mailbox (CONTEXT * ctx, int *index_hint) {
907 ret = _mx_close_mailbox (ctx, index_hint);
908 sidebar_set_buffystats (ctx);
912 /* update a Context structure's internal tables. */
914 void mx_update_tables (CONTEXT * ctx, int committing)
918 /* update memory to reflect the new state of the mailbox */
927 #define this_body ctx->hdrs[j]->content
928 for (i = 0, j = 0; i < ctx->msgcount; i++) {
929 if ((committing && (!ctx->hdrs[i]->deleted ||
930 (ctx->magic == M_MAILDIR
931 && option (OPTMAILDIRTRASH)))) || (!committing
935 ctx->hdrs[j] = ctx->hdrs[i];
938 ctx->hdrs[j]->msgno = j;
939 if (ctx->hdrs[j]->virtual != -1) {
940 ctx->v2r[ctx->vcount] = j;
941 ctx->hdrs[j]->virtual = ctx->vcount++;
942 ctx->vsize += this_body->length + this_body->offset -
943 this_body->hdr_offset;
947 ctx->hdrs[j]->changed = 0;
948 else if (ctx->hdrs[j]->changed)
952 || (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH))) {
953 if (ctx->hdrs[j]->deleted)
957 if (ctx->hdrs[j]->tagged)
959 if (ctx->hdrs[j]->flagged)
961 if (!ctx->hdrs[j]->read) {
963 if (!ctx->hdrs[j]->old)
970 if (ctx->magic == M_MH || ctx->magic == M_MAILDIR)
971 ctx->size -= (ctx->hdrs[i]->content->length +
972 ctx->hdrs[i]->content->offset -
973 ctx->hdrs[i]->content->hdr_offset);
974 /* remove message from the hash tables */
975 if (ctx->subj_hash && ctx->hdrs[i]->env->real_subj)
976 hash_delete (ctx->subj_hash, ctx->hdrs[i]->env->real_subj,
978 if (ctx->id_hash && ctx->hdrs[i]->env->message_id)
979 hash_delete (ctx->id_hash, ctx->hdrs[i]->env->message_id,
981 mutt_free_header (&ctx->hdrs[i]);
987 /* update sidebar count */
988 sidebar_set_buffystats (ctx);
992 /* save changes to mailbox
998 static int _mx_sync_mailbox (CONTEXT * ctx, int *index_hint)
1002 int msgcount, deleted;
1004 if (ctx->dontwrite) {
1005 char buf[STRING], tmp[STRING];
1007 if (km_expand_key (buf, sizeof (buf),
1008 km_find_func (MENU_MAIN, OP_TOGGLE_WRITE)))
1009 snprintf (tmp, sizeof (tmp), _(" Press '%s' to toggle write"), buf);
1011 strfcpy (tmp, _("Use 'toggle-write' to re-enable write!"),
1014 mutt_error (_("Mailbox is marked unwritable. %s"), tmp);
1017 else if (ctx->readonly) {
1018 mutt_error _("Mailbox is read-only.");
1023 if (!ctx->changed && !ctx->deleted) {
1024 mutt_message _("Mailbox is unchanged.");
1030 char buf[SHORT_STRING];
1032 snprintf (buf, sizeof (buf), ctx->deleted == 1
1033 ? _("Purge %d deleted message?") :
1034 _("Purge %d deleted messages?"), ctx->deleted);
1035 if ((purge = query_quadoption (OPT_DELETE, buf)) < 0)
1037 else if (purge == M_NO) {
1039 return 0; /* nothing to do! */
1041 /* let IMAP servers hold on to D flags */
1042 if (ctx->magic != M_IMAP)
1045 for (i = 0; i < ctx->msgcount; i++)
1046 ctx->hdrs[i]->deleted = 0;
1050 else if (ctx->last_tag && ctx->last_tag->deleted)
1051 ctx->last_tag = NULL; /* reset last tagged msg now useless */
1054 /* really only for IMAP - imap_sync_mailbox results in a call to
1055 * mx_update_tables, so ctx->deleted is 0 when it comes back */
1056 msgcount = ctx->msgcount;
1057 deleted = ctx->deleted;
1059 if (purge && ctx->deleted) {
1060 if (trash_append (ctx) == -1)
1065 if (ctx->magic == M_IMAP)
1066 rc = imap_sync_mailbox (ctx, purge, index_hint);
1069 rc = sync_mailbox (ctx, index_hint);
1072 if (ctx->magic == M_IMAP && !purge)
1073 mutt_message (_("Mailbox checkpointed."));
1077 mutt_message (_("%d kept, %d deleted."), msgcount - deleted, deleted);
1081 if (ctx->msgcount == ctx->deleted &&
1082 (ctx->magic == M_MBOX || ctx->magic == M_MMDF) &&
1083 !mutt_is_spool (ctx->path) && !option (OPTSAVEEMPTY)) {
1085 mx_fastclose_mailbox (ctx);
1089 /* if we haven't deleted any messages, we don't need to resort */
1090 /* ... except for certain folder formats which need "unsorted"
1091 * sort order in order to synchronize folders.
1093 * MH and maildir are safe. mbox-style seems to need re-sorting,
1094 * at least with the new threading code.
1096 if (purge || (ctx->magic != M_MAILDIR && ctx->magic != M_MH)) {
1098 /* IMAP does this automatically after handling EXPUNGE */
1099 if (ctx->magic != M_IMAP)
1102 mx_update_tables (ctx, 1);
1103 mutt_sort_headers (ctx, 1); /* rethread from scratch */
1111 int mx_sync_mailbox (CONTEXT* ctx, int* index_hint) {
1112 int ret = _mx_sync_mailbox (ctx, index_hint);
1113 sidebar_set_buffystats (ctx);
1118 * dest destintation mailbox
1119 * hdr message being copied (required for maildir support, because
1120 * the filename depends on the message flags)
1122 MESSAGE *mx_open_new_message (CONTEXT * dest, HEADER * hdr, int flags)
1127 if (!MX_IDX(dest->magic-1)) {
1128 debug_print (1, ("function unimplemented for mailbox type %d.\n", dest->magic));
1132 msg = p_new(MESSAGE, 1);
1133 msg->magic = dest->magic;
1137 msg->flags.flagged = hdr->flagged;
1138 msg->flags.replied = hdr->replied;
1139 msg->flags.read = hdr->read;
1140 msg->received = hdr->received;
1143 if (msg->received == 0)
1144 time (&msg->received);
1146 if (MX_COMMAND(dest->magic-1,mx_open_new_message)(msg, dest, hdr) == 0) {
1147 if (dest->magic == M_MMDF)
1148 fputs (MMDF_SEP, msg->fp);
1150 if ((msg->magic == M_MBOX || msg->magic == M_MMDF) && flags & M_ADD_FROM) {
1152 if (hdr->env->return_path)
1153 p = hdr->env->return_path;
1154 else if (hdr->env->sender)
1155 p = hdr->env->sender;
1160 fprintf (msg->fp, "From %s %s", p ? p->mailbox : NONULL (Username),
1161 ctime (&msg->received));
1170 /* check for new mail */
1171 int mx_check_mailbox (CONTEXT * ctx, int *index_hint, int lock) {
1172 #ifdef USE_COMPRESSED
1173 if (ctx->compressinfo)
1174 return mutt_check_mailbox_compressed (ctx);
1180 if (MX_IDX(ctx->magic-1) && MX_COMMAND(ctx->magic-1,mx_check_mailbox))
1181 return (MX_COMMAND(ctx->magic-1,mx_check_mailbox)(ctx, index_hint, lock));
1184 debug_print (1, ("null or invalid context.\n"));
1189 /* return a stream pointer for a message */
1190 MESSAGE *mx_open_message (CONTEXT * ctx, int msgno)
1194 msg = p_new(MESSAGE, 1);
1195 switch (msg->magic = ctx->magic) {
1204 HEADER *cur = ctx->hdrs[msgno];
1205 char path[_POSIX_PATH_MAX];
1207 snprintf (path, sizeof (path), "%s/%s", ctx->path, cur->path);
1209 if ((msg->fp = fopen (path, "r")) == NULL && errno == ENOENT &&
1210 ctx->magic == M_MAILDIR)
1211 msg->fp = maildir_open_find_message (ctx->path, cur->path);
1213 if (msg->fp == NULL) {
1215 debug_print (1, ("fopen: %s: %s (errno %d).\n", path, strerror (errno), errno));
1224 if (imap_fetch_message (msg, ctx, msgno) != 0)
1228 #endif /* USE_IMAP */
1233 if (pop_fetch_message (msg, ctx, msgno) != 0)
1237 #endif /* USE_POP */
1242 if (nntp_fetch_message (msg, ctx, msgno) != 0)
1246 #endif /* USE_NNTP */
1249 debug_print (1, ("function not implemented for mailbox type %d.\n", ctx->magic));
1256 /* commit a message to a folder */
1258 int mx_commit_message (MESSAGE * msg, CONTEXT * ctx) {
1259 if (!(msg->write && ctx->append)) {
1260 debug_print (1, ("msg->write = %d, ctx->append = %d\n", msg->write, ctx->append));
1263 if (!ctx || !MX_IDX(ctx->magic-1) || !MX_COMMAND(ctx->magic-1,mx_commit_message))
1265 return (MX_COMMAND(ctx->magic-1,mx_commit_message) (msg, ctx));
1268 /* close a pointer to a message */
1269 int mx_close_message (MESSAGE ** msg)
1273 if ((*msg)->magic == M_MH || (*msg)->magic == M_MAILDIR
1275 || (*msg)->magic == M_IMAP
1278 || (*msg)->magic == M_POP
1281 || (*msg)->magic == M_NNTP
1284 r = safe_fclose (&(*msg)->fp);
1290 debug_print (1, ("unlinking %s\n", (*msg)->path));
1291 unlink ((*msg)->path);
1292 p_delete(&(*msg)->path);
1299 void mx_alloc_memory (CONTEXT * ctx)
1302 size_t s = MAX (sizeof (HEADER *), sizeof (int));
1304 if ((ctx->hdrmax + 25) * s < ctx->hdrmax * s) {
1305 mutt_error _("Integer overflow -- can't allocate memory.");
1312 p_realloc(&ctx->hdrs, ctx->hdrmax += 25);
1313 p_realloc(&ctx->v2r, ctx->hdrmax);
1316 ctx->hdrs = p_new(HEADER *, (ctx->hdrmax += 25));
1317 ctx->v2r = p_new(int, ctx->hdrmax);
1319 for (i = ctx->msgcount; i < ctx->hdrmax; i++) {
1320 ctx->hdrs[i] = NULL;
1325 /* this routine is called to update the counts in the context structure for
1326 * the last message header parsed.
1328 void mx_update_context (CONTEXT * ctx, int new_messages)
1333 for (msgno = ctx->msgcount - new_messages; msgno < ctx->msgcount; msgno++) {
1334 h = ctx->hdrs[msgno];
1337 /* NOTE: this _must_ be done before the check for mailcap! */
1338 h->security = crypt_query (h->content);
1341 if (!ctx->pattern) {
1342 ctx->v2r[ctx->vcount] = msgno;
1343 h->virtual = ctx->vcount++;
1349 if (h->env->supersedes) {
1353 ctx->id_hash = mutt_make_id_hash (ctx);
1355 h2 = hash_find (ctx->id_hash, h->env->supersedes);
1357 /* p_delete(&h->env->supersedes); should I ? */
1360 if (!ctx->counting && option (OPTSCORE))
1361 mutt_score_message (ctx, h2, 1);
1365 /* add this message to the hash tables */
1366 if (ctx->id_hash && h->env->message_id)
1367 hash_insert (ctx->id_hash, h->env->message_id, h, 0);
1368 if (!ctx->counting) {
1369 if (ctx->subj_hash && h->env->real_subj)
1370 hash_insert (ctx->subj_hash, h->env->real_subj, h, 1);
1372 if (option (OPTSCORE))
1373 mutt_score_message (ctx, h, 0);
1388 /* update sidebar count */
1389 sidebar_set_buffystats (ctx);
1394 * 1 if the specified mailbox contains 0 messages.
1395 * 0 if the mailbox contains messages
1398 int mx_check_empty (const char *path)
1401 if ((i = mx_get_idx (path)) >= 0 && MX_COMMAND(i,mx_check_empty))
1402 return (MX_COMMAND(i,mx_check_empty)(path));
1407 int mx_acl_check (CONTEXT* ctx, int flag) {
1408 if (!ctx || !MX_IDX(ctx->magic-1))
1410 /* if no acl_check defined for module, assume permission is granted */
1411 if (!MX_COMMAND(ctx->magic-1,mx_acl_check))
1413 return (MX_COMMAND(ctx->magic-1,mx_acl_check)(ctx,flag));
1416 void mx_init (void) {
1420 list_push_back (&MailboxFormats, (void*) mbox_reg_mx ());
1421 list_push_back (&MailboxFormats, (void*) mmdf_reg_mx ());
1422 list_push_back (&MailboxFormats, (void*) mh_reg_mx ());
1423 list_push_back (&MailboxFormats, (void*) maildir_reg_mx ());
1425 list_push_back (&MailboxFormats, (void*) imap_reg_mx ());
1428 list_push_back (&MailboxFormats, (void*) pop_reg_mx ());
1431 list_push_back (&MailboxFormats, (void*) nntp_reg_mx ());
1433 #ifdef USE_COMPRESSED
1434 list_push_back (&MailboxFormats, (void*) compress_reg_mx ());
1437 /* check module registration for completeness with debug versions */
1438 #define EXITWITHERR(m) do { fprintf(stderr, "error: incomplete mx module: %s is missing for type %i\n",m,i);exit(1); } while (0)
1439 for (i = 0; i < MailboxFormats->length; i++) {
1440 if (MX_COMMAND(i,type) < 1) EXITWITHERR("type");
1441 if (!MX_COMMAND(i,mx_is_magic)) EXITWITHERR("mx_is_magic");
1442 if (!MX_COMMAND(i,mx_open_mailbox)) EXITWITHERR("mx_open_mailbox");
1443 /* if (!MX_COMMAND(i,mx_sync_mailbox)) EXITWITHERR("mx_sync_mailbox");*/
1449 int mx_rebuild_cache (void) {
1451 mutt_error (_("Support for header caching was not build in."));
1454 int i = 0, magic = 0;
1455 CONTEXT* ctx = NULL;
1458 if (list_empty(Incoming)) {
1459 mutt_error (_("No mailboxes defined."));
1463 for (i = 0; i < Incoming->length; i++) {
1464 b = (BUFFY*) Incoming->data[i];
1465 magic = mx_get_magic (b->path);
1466 if (magic != M_MAILDIR && magic != M_MH
1472 sidebar_set_current (b->path);
1473 sidebar_draw (CurrentMenu);
1474 if ((ctx = mx_open_mailbox (b->path,
1475 M_READONLY | M_NOSORT | M_COUNT,
1477 mx_close_mailbox (ctx, 0);
1479 mutt_clear_error ();
1481 if (Context && Context->path)
1482 sidebar_set_current (Context->path);
1483 sidebar_draw (CurrentMenu);