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>
37 #include "imap/imap.h"
38 #include "imap/mx_imap.h"
42 #include <pop/mx_pop.h>
45 #include "nntp/nntp.h"
46 #include "nntp/mx_nntp.h"
53 #include "mutt_crypt.h"
56 #include "lib/debug.h"
69 static list2_t* MailboxFormats = NULL;
70 #define MX_COMMAND(idx,cmd) ((mx_t*) MailboxFormats->data[idx])->cmd
71 #define MX_IDX(idx) (idx >= 0 && idx < MailboxFormats->length)
73 #define mutt_is_spool(s) (m_strcmp(Spoolfile, s) == 0)
78 * retry - should retry if unable to lock?
81 static int invoke_dotlock (const char *path, int dummy, int flags, int retry)
83 char cmd[LONG_STRING + _POSIX_PATH_MAX];
84 char f[SHORT_STRING + _POSIX_PATH_MAX];
87 if (flags & DL_FL_RETRY)
88 snprintf (r, sizeof (r), "-r %d ", retry ? MAXLOCKATTEMPT : 0);
90 mutt_quote_filename (f, sizeof (f), path);
92 snprintf (cmd, sizeof (cmd),
95 flags & DL_FL_TRY ? "-t " : "",
96 flags & DL_FL_UNLOCK ? "-u " : "",
97 flags & DL_FL_USEPRIV ? "-p " : "",
98 flags & DL_FL_FORCE ? "-f " : "",
99 flags & DL_FL_UNLINK ? "-d " : "",
100 flags & DL_FL_RETRY ? r : "", f);
102 return mutt_system (cmd);
105 static int dotlock_file (const char *path, int fd, int retry)
108 int flags = DL_FL_USEPRIV | DL_FL_RETRY;
114 if ((r = invoke_dotlock (path, fd, flags, retry)) == DL_EX_EXIST) {
115 if (!option (OPTNOCURSES)) {
116 char msg[LONG_STRING];
118 snprintf (msg, sizeof (msg),
119 _("Lock count exceeded, remove lock for %s?"), path);
120 if (retry && mutt_yesorno (msg, M_YES) == M_YES) {
121 flags |= DL_FL_FORCE;
128 mutt_error (_("Can't dotlock %s.\n"), path);
131 return (r == DL_EX_OK ? 0 : -1);
134 static int undotlock_file (const char *path, int fd)
136 return (invoke_dotlock (path, fd, DL_FL_USEPRIV | DL_FL_UNLOCK, 0) ==
140 #endif /* USE_DOTLOCK */
142 /* looks up index of type for path in MailboxFormats */
143 static int mx_get_idx (const char* path) {
147 /* first, test all non-local folders to avoid stat() call */
148 for (i = 0; i < MailboxFormats->length; i++) {
149 if (!MX_COMMAND(i,local))
150 t = MX_COMMAND(i,mx_is_magic)(path, NULL);
154 if (stat (path, &st) == 0) {
155 /* if stat() succeeded, keep testing until success and
156 * pass stat() info so that we only need to do it once */
157 for (i = 0; i < MailboxFormats->length; i++) {
158 if (MX_COMMAND(i,local))
159 t = MX_COMMAND(i,mx_is_magic)(path, &st);
168 * excl if excl != 0, request an exclusive lock
169 * dot if dot != 0, try to dotlock the file
170 * timeout should retry locking?
172 int mx_lock_file (const char *path, int fd, int excl, int dot, int timeout)
174 #if defined (USE_FCNTL) || defined (USE_FLOCK)
186 lck.l_type = excl ? F_WRLCK : F_RDLCK;
187 lck.l_whence = SEEK_SET;
192 while (fcntl (fd, F_SETLK, &lck) == -1) {
195 debug_print (1, ("fcntl errno %d.\n", errno));
196 if (errno != EAGAIN && errno != EACCES) {
197 mutt_perror ("fcntl");
201 if (fstat (fd, &sb) != 0)
207 /* only unlock file if it is unchanged */
208 if (prev_sb.st_size == sb.st_size
209 && ++count >= (timeout ? MAXLOCKATTEMPT : 0)) {
211 mutt_error _("Timeout exceeded while attempting fcntl lock!");
218 mutt_message (_("Waiting for fcntl lock... %d"), ++attempt);
221 #endif /* USE_FCNTL */
226 while (flock (fd, (excl ? LOCK_EX : LOCK_SH) | LOCK_NB) == -1) {
229 if (errno != EWOULDBLOCK) {
230 mutt_perror ("flock");
235 if (fstat (fd, &sb) != 0)
241 /* only unlock file if it is unchanged */
242 if (prev_sb.st_size == sb.st_size
243 && ++count >= (timeout ? MAXLOCKATTEMPT : 0)) {
245 mutt_error _("Timeout exceeded while attempting flock lock!");
253 mutt_message (_("Waiting for flock attempt... %d"), ++attempt);
256 #endif /* USE_FLOCK */
260 r = dotlock_file (path, fd, timeout);
261 #endif /* USE_DOTLOCK */
264 /* release any other locks obtained in this routine */
267 lck.l_type = F_UNLCK;
268 fcntl (fd, F_SETLK, &lck);
269 #endif /* USE_FCNTL */
273 #endif /* USE_FLOCK */
281 int mx_unlock_file (const char *path, int fd, int dot)
284 struct flock unlockit;
286 p_clear(&unlockit, 1);
287 unlockit.l_type = F_UNLCK;
288 unlockit.l_whence = SEEK_SET;
289 fcntl (fd, F_SETLK, &unlockit);
298 undotlock_file (path, fd);
304 void mx_unlink_empty (const char *path)
312 if ((fd = open (path, O_RDWR)) == -1)
315 if (mx_lock_file (path, fd, 1, 0, 1) == -1) {
321 invoke_dotlock (path, fd, DL_FL_UNLINK, 1);
323 if (fstat (fd, &sb) == 0 && sb.st_size == 0)
327 mx_unlock_file (path, fd, 0);
331 /* try to figure out what type of mailbox ``path'' is */
332 int mx_get_magic (const char *path) {
335 if (m_strlen(path) == 0)
337 if ((i = mx_get_idx (path)) >= 0)
338 return (MX_COMMAND(i,type));
342 int mx_is_local (int m) {
345 return (MX_COMMAND(m,local));
349 * set DefaultMagic to the given value
351 int mx_set_magic (const char *s)
353 if (ascii_strcasecmp (s, "mbox") == 0)
354 DefaultMagic = M_MBOX;
355 else if (ascii_strcasecmp (s, "mmdf") == 0)
356 DefaultMagic = M_MMDF;
357 else if (ascii_strcasecmp (s, "mh") == 0)
359 else if (ascii_strcasecmp (s, "maildir") == 0)
360 DefaultMagic = M_MAILDIR;
367 /* mx_access: Wrapper for access, checks permissions on a given mailbox.
368 * We may be interested in using ACL-style flags at some point, currently
369 * we use the normal access() flags. */
370 int mx_access (const char *path, int flags)
374 if ((i = mx_get_idx (path)) >= 0 && MX_COMMAND(i,mx_access))
375 return (MX_COMMAND(i,mx_access)(path,flags));
379 static int mx_open_mailbox_append (CONTEXT * ctx, int flags)
383 /* special case for appending to compressed folders -
384 * even if we can not open them for reading */
385 if (mutt_can_append_compressed (ctx->path))
386 mutt_open_append_compressed (ctx);
392 if (mx_get_magic (ctx->path) == M_IMAP)
393 return imap_open_mailbox_append (ctx);
397 if (stat (ctx->path, &sb) == 0) {
398 ctx->magic = mx_get_magic (ctx->path);
400 switch (ctx->magic) {
402 mutt_error (_("%s is not a mailbox."), ctx->path);
408 else if (errno == ENOENT) {
409 ctx->magic = DefaultMagic;
411 if (ctx->magic == M_MH || ctx->magic == M_MAILDIR) {
412 char tmp[_POSIX_PATH_MAX];
414 if (mkdir (ctx->path, S_IRWXU)) {
415 mutt_perror (ctx->path);
419 if (ctx->magic == M_MAILDIR) {
420 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
421 if (mkdir (tmp, S_IRWXU)) {
427 snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
428 if (mkdir (tmp, S_IRWXU)) {
430 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
435 snprintf (tmp, sizeof (tmp), "%s/tmp", ctx->path);
436 if (mkdir (tmp, S_IRWXU)) {
438 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
440 snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
449 snprintf (tmp, sizeof (tmp), "%s/.mh_sequences", ctx->path);
450 if ((i = creat (tmp, S_IRWXU)) == -1) {
460 mutt_perror (ctx->path);
464 switch (ctx->magic) {
468 safe_fopen (ctx->path, flags & M_NEWFOLDER ? "w" : "a")) == NULL
469 || mbox_lock_mailbox (ctx, 1, 1) != 0) {
471 mutt_perror (ctx->path);
473 mutt_error (_("Couldn't lock %s\n"), ctx->path);
474 safe_fclose (&ctx->fp);
478 fseeko (ctx->fp, 0, 2);
494 * open a mailbox and parse it
497 * flags M_NOSORT do not sort mailbox
498 * M_APPEND open mailbox for appending
499 * M_READONLY open mailbox in read-only mode
500 * M_QUIET only print error messages
501 * ctx if non-null, context struct to use
503 CONTEXT *mx_open_mailbox (const char *path, int flags, CONTEXT * pctx)
509 ctx = p_new(CONTEXT, 1);
511 ctx->path = m_strdup(path);
513 ctx->msgnotreadyet = -1;
518 if (flags & M_READONLY)
523 if (flags & (M_APPEND | M_NEWFOLDER)) {
524 if (mx_open_mailbox_append (ctx, flags) != 0) {
525 mx_fastclose_mailbox (ctx);
533 if (!MX_IDX(ctx->magic-1))
534 ctx->magic = mx_get_magic (path);
536 if (ctx->magic == M_COMPRESSED)
537 mutt_open_read_compressed (ctx);
540 mutt_error (_("%s is not a mailbox."), path);
542 if (ctx->magic == -1)
545 if (ctx->magic <= 0) {
546 mx_fastclose_mailbox (ctx);
552 /* if the user has a `push' command in their .muttrc, or in a folder-hook,
553 * it will cause the progress messages not to be displayed because
554 * mutt_refresh() will think we are in the middle of a macro. so set a
555 * flag to indicate that we should really refresh the screen.
557 set_option (OPTFORCEREFRESH);
560 mutt_message (_("Reading %s..."), ctx->path);
562 rc = MX_COMMAND(ctx->magic-1,mx_open_mailbox)(ctx);
565 if ((flags & M_NOSORT) == 0) {
566 /* avoid unnecessary work since the mailbox is completely unthreaded
568 unset_option (OPTSORTSUBTHREADS);
569 unset_option (OPTNEEDRESCORE);
570 mutt_sort_headers (ctx, 1);
576 mx_fastclose_mailbox (ctx);
581 unset_option (OPTFORCEREFRESH);
585 /* free up memory associated with the mailbox context */
586 void mx_fastclose_mailbox (CONTEXT * ctx)
593 if (MX_IDX(ctx->magic-1) && MX_COMMAND(ctx->magic-1,mx_fastclose_mailbox))
594 MX_COMMAND(ctx->magic-1,mx_fastclose_mailbox(ctx));
596 hash_destroy (&ctx->subj_hash, NULL);
598 hash_destroy (&ctx->id_hash, NULL);
599 mutt_clear_threads (ctx);
600 for (i = 0; i < ctx->msgcount; i++)
601 mutt_free_header (&ctx->hdrs[i]);
602 p_delete(&ctx->hdrs);
605 if (ctx->compressinfo)
606 mutt_fast_close_compressed (ctx);
608 p_delete(&ctx->path);
609 p_delete(&ctx->pattern);
610 if (ctx->limit_pattern)
611 mutt_pattern_free (&ctx->limit_pattern);
612 safe_fclose (&ctx->fp);
616 /* save changes to disk */
617 static int sync_mailbox (CONTEXT * ctx, int *index_hint)
622 mutt_message (_("Writing %s..."), ctx->path);
624 if (MX_IDX(ctx->magic-1))
625 /* the 1 is only of interest for IMAP and means EXPUNGE */
626 rc = MX_COMMAND(ctx->magic-1,mx_sync_mailbox(ctx,1,index_hint));
628 if (rc == 0 && ctx->compressinfo)
629 return mutt_sync_compressed (ctx);
634 /* move deleted mails to the trash folder */
635 static int trash_append (CONTEXT * ctx)
641 if (!TrashPath || !ctx->deleted ||
642 (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))
645 for (; i < ctx->msgcount && (!ctx->hdrs[i]->deleted ||
646 ctx->hdrs[i]->appended); i++);
647 if (i == ctx->msgcount)
648 return 0; /* nothing to be done */
650 if (mutt_save_confirm (TrashPath, &st) != 0) {
651 mutt_error _("message(s) not deleted");
656 if (lstat (ctx->path, &stc) == 0 && stc.st_ino == st.st_ino
657 && stc.st_dev == st.st_dev && stc.st_rdev == st.st_rdev)
658 return 0; /* we are in the trash folder: simple sync */
660 if ((ctx_trash = mx_open_mailbox (TrashPath, M_APPEND, NULL)) != NULL) {
661 for (i = 0; i < ctx->msgcount; i++)
662 if (ctx->hdrs[i]->deleted && !ctx->hdrs[i]->appended
663 && !ctx->hdrs[i]->purged
664 && mutt_append_message (ctx_trash, ctx, ctx->hdrs[i], 0, 0) == -1) {
665 mx_close_mailbox (ctx_trash, NULL);
669 mx_close_mailbox (ctx_trash, NULL);
672 mutt_error _("Can't open trash folder");
680 /* save changes and close mailbox */
681 static int _mx_close_mailbox (CONTEXT * ctx, int *index_hint)
683 int i, move_messages = 0, purge = 1, read_msgs = 0;
687 char mbox[_POSIX_PATH_MAX];
688 char buf[SHORT_STRING];
696 if (ctx->magic == M_NNTP) {
699 ret = nntp_close_mailbox (ctx);
700 mx_fastclose_mailbox (ctx);
704 if (ctx->readonly || ctx->dontwrite) {
705 /* mailbox is readonly or we don't want to write */
706 mx_fastclose_mailbox (ctx);
711 /* mailbox was opened in write-mode */
712 if (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
713 mbox_close_mailbox (ctx);
715 mx_fastclose_mailbox (ctx);
719 for (i = 0; i < ctx->msgcount; i++) {
720 if (!ctx->hdrs[i]->deleted && ctx->hdrs[i]->read
721 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
725 if (read_msgs && quadoption (OPT_MOVE) != M_NO) {
728 if ((p = mutt_find_hook (M_MBOXHOOK, ctx->path))) {
730 m_strcpy(mbox, sizeof(mbox), p);
733 m_strcpy(mbox, sizeof(mbox), NONULL(Inbox));
734 isSpool = mutt_is_spool (ctx->path) && !mutt_is_spool (mbox);
736 mutt_expand_path (mbox, sizeof (mbox));
739 snprintf (buf, sizeof (buf), _("Move read messages to %s?"), mbox);
740 if ((move_messages = query_quadoption (OPT_MOVE, buf)) == -1) {
748 * There is no point in asking whether or not to purge if we are
749 * just marking messages as "trash".
751 if (ctx->deleted && !(ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH))) {
752 snprintf (buf, sizeof (buf), ctx->deleted == 1
753 ? _("Purge %d deleted message?") :
754 _("Purge %d deleted messages?"), ctx->deleted);
755 if ((purge = query_quadoption (OPT_DELETE, buf)) < 0) {
762 /* IMAP servers manage the OLD flag themselves */
763 if (ctx->magic != M_IMAP)
765 if (option (OPTMARKOLD)) {
766 for (i = 0; i < ctx->msgcount; i++) {
767 if (!ctx->hdrs[i]->deleted && !ctx->hdrs[i]->old)
768 mutt_set_flag (ctx, ctx->hdrs[i], M_OLD, 1);
773 mutt_message (_("Moving read messages to %s..."), mbox);
776 /* try to use server-side copy first */
779 if (ctx->magic == M_IMAP && imap_is_magic (mbox, NULL) == M_IMAP) {
780 /* tag messages for moving, and clear old tags, if any */
781 for (i = 0; i < ctx->msgcount; i++)
782 if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted
783 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
784 ctx->hdrs[i]->tagged = 1;
786 ctx->hdrs[i]->tagged = 0;
788 i = imap_copy_messages (ctx, NULL, mbox, 1);
791 if (i == 0) /* success */
793 else if (i == -1) { /* horrible error, bail */
797 else /* use regular append-copy mode */
800 if (mx_open_mailbox (mbox, M_APPEND, &f) == NULL) {
805 for (i = 0; i < ctx->msgcount; i++) {
806 if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted
807 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED))) {
808 if (mutt_append_message (&f, ctx, ctx->hdrs[i], 0, CH_UPDATE_LEN) ==
810 mutt_set_flag (ctx, ctx->hdrs[i], M_DELETE, 1);
811 mutt_set_flag (ctx, ctx->hdrs[i], M_APPENDED, 1);
814 mx_close_mailbox (&f, NULL);
821 mx_close_mailbox (&f, NULL);
825 else if (!ctx->changed && ctx->deleted == 0) {
826 mutt_message _("Mailbox is unchanged.");
828 mx_fastclose_mailbox (ctx);
832 /* copy mails to the trash before expunging */
833 if (purge && ctx->deleted)
834 if (trash_append (ctx) != 0) {
840 /* allow IMAP to preserve the deleted flag across sessions */
841 if (ctx->magic == M_IMAP) {
842 if ((check = imap_sync_mailbox (ctx, purge, index_hint)) != 0) {
851 for (i = 0; i < ctx->msgcount; i++)
852 ctx->hdrs[i]->deleted = 0;
856 if (ctx->changed || ctx->deleted) {
857 if ((check = sync_mailbox (ctx, index_hint)) != 0) {
865 mutt_message (_("%d kept, %d moved, %d deleted."),
866 ctx->msgcount - ctx->deleted, read_msgs, ctx->deleted);
868 mutt_message (_("%d kept, %d deleted."),
869 ctx->msgcount - ctx->deleted, ctx->deleted);
871 if (ctx->msgcount == ctx->deleted &&
872 (ctx->magic == M_MMDF || ctx->magic == M_MBOX) &&
873 !mutt_is_spool (ctx->path) && !option (OPTSAVEEMPTY))
874 mx_unlink_empty (ctx->path);
876 if (ctx->compressinfo && mutt_slow_close_compressed (ctx))
879 mx_fastclose_mailbox (ctx);
884 int mx_close_mailbox (CONTEXT * ctx, int *index_hint) {
888 ret = _mx_close_mailbox (ctx, index_hint);
889 sidebar_set_buffystats (ctx);
893 /* update a Context structure's internal tables. */
895 void mx_update_tables (CONTEXT * ctx, int committing)
899 /* update memory to reflect the new state of the mailbox */
908 #define this_body ctx->hdrs[j]->content
909 for (i = 0, j = 0; i < ctx->msgcount; i++) {
910 if ((committing && (!ctx->hdrs[i]->deleted ||
911 (ctx->magic == M_MAILDIR
912 && option (OPTMAILDIRTRASH)))) || (!committing
916 ctx->hdrs[j] = ctx->hdrs[i];
919 ctx->hdrs[j]->msgno = j;
920 if (ctx->hdrs[j]->virtual != -1) {
921 ctx->v2r[ctx->vcount] = j;
922 ctx->hdrs[j]->virtual = ctx->vcount++;
923 ctx->vsize += this_body->length + this_body->offset -
924 this_body->hdr_offset;
928 ctx->hdrs[j]->changed = 0;
929 else if (ctx->hdrs[j]->changed)
933 || (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH))) {
934 if (ctx->hdrs[j]->deleted)
938 if (ctx->hdrs[j]->tagged)
940 if (ctx->hdrs[j]->flagged)
942 if (!ctx->hdrs[j]->read) {
944 if (!ctx->hdrs[j]->old)
951 if (ctx->magic == M_MH || ctx->magic == M_MAILDIR)
952 ctx->size -= (ctx->hdrs[i]->content->length +
953 ctx->hdrs[i]->content->offset -
954 ctx->hdrs[i]->content->hdr_offset);
955 /* remove message from the hash tables */
956 if (ctx->subj_hash && ctx->hdrs[i]->env->real_subj)
957 hash_delete (ctx->subj_hash, ctx->hdrs[i]->env->real_subj,
959 if (ctx->id_hash && ctx->hdrs[i]->env->message_id)
960 hash_delete (ctx->id_hash, ctx->hdrs[i]->env->message_id,
962 mutt_free_header (&ctx->hdrs[i]);
968 /* update sidebar count */
969 sidebar_set_buffystats (ctx);
973 /* save changes to mailbox
979 static int _mx_sync_mailbox (CONTEXT * ctx, int *index_hint)
983 int msgcount, deleted;
985 if (ctx->dontwrite) {
986 char buf[STRING], tmp[STRING];
988 if (km_expand_key (buf, sizeof (buf),
989 km_find_func (MENU_MAIN, OP_TOGGLE_WRITE)))
990 snprintf (tmp, sizeof (tmp), _(" Press '%s' to toggle write"), buf);
992 m_strcpy(tmp, sizeof(tmp), _("Use 'toggle-write' to re-enable write!"));
994 mutt_error (_("Mailbox is marked unwritable. %s"), tmp);
997 else if (ctx->readonly) {
998 mutt_error _("Mailbox is read-only.");
1003 if (!ctx->changed && !ctx->deleted) {
1004 mutt_message _("Mailbox is unchanged.");
1010 char buf[SHORT_STRING];
1012 snprintf (buf, sizeof (buf), ctx->deleted == 1
1013 ? _("Purge %d deleted message?") :
1014 _("Purge %d deleted messages?"), ctx->deleted);
1015 if ((purge = query_quadoption (OPT_DELETE, buf)) < 0)
1017 else if (purge == M_NO) {
1019 return 0; /* nothing to do! */
1021 /* let IMAP servers hold on to D flags */
1022 if (ctx->magic != M_IMAP)
1025 for (i = 0; i < ctx->msgcount; i++)
1026 ctx->hdrs[i]->deleted = 0;
1030 else if (ctx->last_tag && ctx->last_tag->deleted)
1031 ctx->last_tag = NULL; /* reset last tagged msg now useless */
1034 /* really only for IMAP - imap_sync_mailbox results in a call to
1035 * mx_update_tables, so ctx->deleted is 0 when it comes back */
1036 msgcount = ctx->msgcount;
1037 deleted = ctx->deleted;
1039 if (purge && ctx->deleted) {
1040 if (trash_append (ctx) == -1)
1045 if (ctx->magic == M_IMAP)
1046 rc = imap_sync_mailbox (ctx, purge, index_hint);
1049 rc = sync_mailbox (ctx, index_hint);
1052 if (ctx->magic == M_IMAP && !purge)
1053 mutt_message (_("Mailbox checkpointed."));
1057 mutt_message (_("%d kept, %d deleted."), msgcount - deleted, deleted);
1061 if (ctx->msgcount == ctx->deleted &&
1062 (ctx->magic == M_MBOX || ctx->magic == M_MMDF) &&
1063 !mutt_is_spool (ctx->path) && !option (OPTSAVEEMPTY)) {
1065 mx_fastclose_mailbox (ctx);
1069 /* if we haven't deleted any messages, we don't need to resort */
1070 /* ... except for certain folder formats which need "unsorted"
1071 * sort order in order to synchronize folders.
1073 * MH and maildir are safe. mbox-style seems to need re-sorting,
1074 * at least with the new threading code.
1076 if (purge || (ctx->magic != M_MAILDIR && ctx->magic != M_MH)) {
1078 /* IMAP does this automatically after handling EXPUNGE */
1079 if (ctx->magic != M_IMAP)
1082 mx_update_tables (ctx, 1);
1083 mutt_sort_headers (ctx, 1); /* rethread from scratch */
1091 int mx_sync_mailbox (CONTEXT* ctx, int* index_hint) {
1092 int ret = _mx_sync_mailbox (ctx, index_hint);
1093 sidebar_set_buffystats (ctx);
1098 * dest destintation mailbox
1099 * hdr message being copied (required for maildir support, because
1100 * the filename depends on the message flags)
1102 MESSAGE *mx_open_new_message (CONTEXT * dest, HEADER * hdr, int flags)
1105 address_t *p = NULL;
1107 if (!MX_IDX(dest->magic-1)) {
1108 debug_print (1, ("function unimplemented for mailbox type %d.\n", dest->magic));
1112 msg = p_new(MESSAGE, 1);
1113 msg->magic = dest->magic;
1117 msg->flags.flagged = hdr->flagged;
1118 msg->flags.replied = hdr->replied;
1119 msg->flags.read = hdr->read;
1120 msg->received = hdr->received;
1123 if (msg->received == 0)
1124 time (&msg->received);
1126 if (MX_COMMAND(dest->magic-1,mx_open_new_message)(msg, dest, hdr) == 0) {
1127 if (dest->magic == M_MMDF)
1128 fputs (MMDF_SEP, msg->fp);
1130 if ((msg->magic == M_MBOX || msg->magic == M_MMDF) && flags & M_ADD_FROM) {
1132 if (hdr->env->return_path)
1133 p = hdr->env->return_path;
1134 else if (hdr->env->sender)
1135 p = hdr->env->sender;
1140 fprintf (msg->fp, "From %s %s", p ? p->mailbox : NONULL (Username),
1141 ctime (&msg->received));
1150 /* check for new mail */
1151 int mx_check_mailbox (CONTEXT * ctx, int *index_hint, int lock) {
1152 if (ctx->compressinfo)
1153 return mutt_check_mailbox_compressed (ctx);
1158 if (MX_IDX(ctx->magic-1) && MX_COMMAND(ctx->magic-1,mx_check_mailbox))
1159 return (MX_COMMAND(ctx->magic-1,mx_check_mailbox)(ctx, index_hint, lock));
1162 debug_print (1, ("null or invalid context.\n"));
1167 /* return a stream pointer for a message */
1168 MESSAGE *mx_open_message (CONTEXT * ctx, int msgno)
1172 msg = p_new(MESSAGE, 1);
1173 switch (msg->magic = ctx->magic) {
1182 HEADER *cur = ctx->hdrs[msgno];
1183 char path[_POSIX_PATH_MAX];
1185 snprintf (path, sizeof (path), "%s/%s", ctx->path, cur->path);
1187 if ((msg->fp = fopen (path, "r")) == NULL && errno == ENOENT &&
1188 ctx->magic == M_MAILDIR)
1189 msg->fp = maildir_open_find_message (ctx->path, cur->path);
1191 if (msg->fp == NULL) {
1193 debug_print (1, ("fopen: %s: %s (errno %d).\n", path, strerror (errno), errno));
1202 if (imap_fetch_message (msg, ctx, msgno) != 0)
1206 #endif /* USE_IMAP */
1210 if (pop_fetch_message (msg, ctx, msgno) != 0)
1218 if (nntp_fetch_message (msg, ctx, msgno) != 0)
1222 #endif /* USE_NNTP */
1225 debug_print (1, ("function not implemented for mailbox type %d.\n", ctx->magic));
1232 /* commit a message to a folder */
1234 int mx_commit_message (MESSAGE * msg, CONTEXT * ctx) {
1235 if (!(msg->write && ctx->append)) {
1236 debug_print (1, ("msg->write = %d, ctx->append = %d\n", msg->write, ctx->append));
1239 if (!ctx || !MX_IDX(ctx->magic-1) || !MX_COMMAND(ctx->magic-1,mx_commit_message))
1241 return (MX_COMMAND(ctx->magic-1,mx_commit_message) (msg, ctx));
1244 /* close a pointer to a message */
1245 int mx_close_message (MESSAGE ** msg)
1249 if ((*msg)->magic == M_MH || (*msg)->magic == M_MAILDIR
1251 || (*msg)->magic == M_IMAP
1253 || (*msg)->magic == M_POP
1255 || (*msg)->magic == M_NNTP
1258 r = safe_fclose (&(*msg)->fp);
1264 debug_print (1, ("unlinking %s\n", (*msg)->path));
1265 unlink ((*msg)->path);
1266 p_delete(&(*msg)->path);
1273 void mx_alloc_memory (CONTEXT * ctx)
1276 size_t s = MAX (sizeof (HEADER *), sizeof (int));
1278 if ((ctx->hdrmax + 25) * s < ctx->hdrmax * s) {
1279 mutt_error _("Integer overflow -- can't allocate memory.");
1286 p_realloc(&ctx->hdrs, ctx->hdrmax += 25);
1287 p_realloc(&ctx->v2r, ctx->hdrmax);
1290 ctx->hdrs = p_new(HEADER *, (ctx->hdrmax += 25));
1291 ctx->v2r = p_new(int, ctx->hdrmax);
1293 for (i = ctx->msgcount; i < ctx->hdrmax; i++) {
1294 ctx->hdrs[i] = NULL;
1299 /* this routine is called to update the counts in the context structure for
1300 * the last message header parsed.
1302 void mx_update_context (CONTEXT * ctx, int new_messages)
1307 for (msgno = ctx->msgcount - new_messages; msgno < ctx->msgcount; msgno++) {
1308 h = ctx->hdrs[msgno];
1310 /* NOTE: this _must_ be done before the check for mailcap! */
1311 h->security = crypt_query (h->content);
1313 if (!ctx->pattern) {
1314 ctx->v2r[ctx->vcount] = msgno;
1315 h->virtual = ctx->vcount++;
1321 if (h->env->supersedes) {
1325 ctx->id_hash = mutt_make_id_hash (ctx);
1327 h2 = hash_find (ctx->id_hash, h->env->supersedes);
1329 /* p_delete(&h->env->supersedes); should I ? */
1332 if (!ctx->counting && option (OPTSCORE))
1333 mutt_score_message (ctx, h2, 1);
1337 /* add this message to the hash tables */
1338 if (ctx->id_hash && h->env->message_id)
1339 hash_insert (ctx->id_hash, h->env->message_id, h, 0);
1340 if (!ctx->counting) {
1341 if (ctx->subj_hash && h->env->real_subj)
1342 hash_insert (ctx->subj_hash, h->env->real_subj, h, 1);
1344 if (option (OPTSCORE))
1345 mutt_score_message (ctx, h, 0);
1360 /* update sidebar count */
1361 sidebar_set_buffystats (ctx);
1366 * 1 if the specified mailbox contains 0 messages.
1367 * 0 if the mailbox contains messages
1370 int mx_check_empty (const char *path)
1373 if ((i = mx_get_idx (path)) >= 0 && MX_COMMAND(i,mx_check_empty))
1374 return (MX_COMMAND(i,mx_check_empty)(path));
1379 int mx_acl_check (CONTEXT* ctx, int flag) {
1380 if (!ctx || !MX_IDX(ctx->magic-1))
1382 /* if no acl_check defined for module, assume permission is granted */
1383 if (!MX_COMMAND(ctx->magic-1,mx_acl_check))
1385 return (MX_COMMAND(ctx->magic-1,mx_acl_check)(ctx,flag));
1388 void mx_init (void) {
1392 list_push_back (&MailboxFormats, (void*) mbox_reg_mx ());
1393 list_push_back (&MailboxFormats, (void*) mmdf_reg_mx ());
1394 list_push_back (&MailboxFormats, (void*) mh_reg_mx ());
1395 list_push_back (&MailboxFormats, (void*) maildir_reg_mx ());
1397 list_push_back (&MailboxFormats, (void*) imap_reg_mx ());
1399 list_push_back (&MailboxFormats, (void*) pop_reg_mx ());
1401 list_push_back (&MailboxFormats, (void*) nntp_reg_mx ());
1403 list_push_back (&MailboxFormats, (void*) compress_reg_mx ());
1405 /* check module registration for completeness with debug versions */
1406 #define EXITWITHERR(m) do { fprintf(stderr, "error: incomplete mx module: %s is missing for type %i\n",m,i);exit(1); } while (0)
1407 for (i = 0; i < MailboxFormats->length; i++) {
1408 if (MX_COMMAND(i,type) < 1) EXITWITHERR("type");
1409 if (!MX_COMMAND(i,mx_is_magic)) EXITWITHERR("mx_is_magic");
1410 if (!MX_COMMAND(i,mx_open_mailbox)) EXITWITHERR("mx_open_mailbox");
1411 /* if (!MX_COMMAND(i,mx_sync_mailbox)) EXITWITHERR("mx_sync_mailbox");*/
1417 int mx_rebuild_cache (void) {
1419 mutt_error (_("Support for header caching was not build in."));
1422 int i = 0, magic = 0;
1423 CONTEXT* ctx = NULL;
1426 if (list_empty(Incoming)) {
1427 mutt_error (_("No mailboxes defined."));
1431 for (i = 0; i < Incoming->length; i++) {
1432 b = (BUFFY*) Incoming->data[i];
1433 magic = mx_get_magic (b->path);
1434 if (magic != M_MAILDIR && magic != M_MH
1440 sidebar_set_current (b->path);
1441 sidebar_draw (CurrentMenu);
1442 if ((ctx = mx_open_mailbox (b->path,
1443 M_READONLY | M_NOSORT | M_COUNT,
1445 mx_close_mailbox (ctx, 0);
1447 mutt_clear_error ();
1449 if (Context && Context->path)
1450 sidebar_set_current (Context->path);
1451 sidebar_draw (CurrentMenu);