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.
30 #include "imap/imap.h"
31 #include "imap/mx_imap.h"
36 #include "pop/mx_pop.h"
40 #include "nntp/nntp.h"
41 #include "nntp/mx_nntp.h"
52 #include "mutt_crypt.h"
72 static list2_t* MailboxFormats = NULL;
73 #define MX_COMMAND(idx,cmd) ((mx_t*) MailboxFormats->data[idx])->cmd
75 #define mutt_is_spool(s) (safe_strcmp (Spoolfile, s) == 0)
80 * retry - should retry if unable to lock?
85 static int invoke_dotlock (const char *path, int dummy, int flags, int retry)
87 char cmd[LONG_STRING + _POSIX_PATH_MAX];
88 char f[SHORT_STRING + _POSIX_PATH_MAX];
91 if (flags & DL_FL_RETRY)
92 snprintf (r, sizeof (r), "-r %d ", retry ? MAXLOCKATTEMPT : 0);
94 mutt_quote_filename (f, sizeof (f), path);
96 snprintf (cmd, sizeof (cmd),
99 flags & DL_FL_TRY ? "-t " : "",
100 flags & DL_FL_UNLOCK ? "-u " : "",
101 flags & DL_FL_USEPRIV ? "-p " : "",
102 flags & DL_FL_FORCE ? "-f " : "",
103 flags & DL_FL_UNLINK ? "-d " : "",
104 flags & DL_FL_RETRY ? r : "", f);
106 return mutt_system (cmd);
111 #define invoke_dotlock dotlock_invoke
115 static int dotlock_file (const char *path, int fd, int retry)
118 int flags = DL_FL_USEPRIV | DL_FL_RETRY;
124 if ((r = invoke_dotlock (path, fd, flags, retry)) == DL_EX_EXIST) {
125 if (!option (OPTNOCURSES)) {
126 char msg[LONG_STRING];
128 snprintf (msg, sizeof (msg),
129 _("Lock count exceeded, remove lock for %s?"), path);
130 if (retry && mutt_yesorno (msg, M_YES) == M_YES) {
131 flags |= DL_FL_FORCE;
138 mutt_error (_("Can't dotlock %s.\n"), path);
141 return (r == DL_EX_OK ? 0 : -1);
144 static int undotlock_file (const char *path, int fd)
146 return (invoke_dotlock (path, fd, DL_FL_USEPRIV | DL_FL_UNLOCK, 0) ==
150 #endif /* USE_DOTLOCK */
152 /* looks up index of type for path in MailboxFormats */
153 static int mx_get_idx (const char* path) {
156 for (i = 0; i < MailboxFormats->length; i++) {
157 t = MX_COMMAND(i,mx_is_magic)(path);
158 fprintf (stderr, " test %s for %i == %i\n", NONULL(path), i, t);
160 return (t-1); /* use type as index for array */
166 * excl if excl != 0, request an exclusive lock
167 * dot if dot != 0, try to dotlock the file
168 * timeout should retry locking?
170 int mx_lock_file (const char *path, int fd, int excl, int dot, int timeout)
172 #if defined (USE_FCNTL) || defined (USE_FLOCK)
183 memset (&lck, 0, sizeof (struct flock));
184 lck.l_type = excl ? F_WRLCK : F_RDLCK;
185 lck.l_whence = SEEK_SET;
189 while (fcntl (fd, F_SETLK, &lck) == -1) {
192 dprint (1, (debugfile, "mx_lock_file(): fcntl errno %d.\n", errno));
193 if (errno != EAGAIN && errno != EACCES) {
194 mutt_perror ("fcntl");
198 if (fstat (fd, &sb) != 0)
204 /* only unlock file if it is unchanged */
205 if (prev_sb.st_size == sb.st_size
206 && ++count >= (timeout ? MAXLOCKATTEMPT : 0)) {
208 mutt_error _("Timeout exceeded while attempting fcntl lock!");
215 mutt_message (_("Waiting for fcntl lock... %d"), ++attempt);
218 #endif /* USE_FCNTL */
223 while (flock (fd, (excl ? LOCK_EX : LOCK_SH) | LOCK_NB) == -1) {
226 if (errno != EWOULDBLOCK) {
227 mutt_perror ("flock");
232 if (fstat (fd, &sb) != 0)
238 /* only unlock file if it is unchanged */
239 if (prev_sb.st_size == sb.st_size
240 && ++count >= (timeout ? MAXLOCKATTEMPT : 0)) {
242 mutt_error _("Timeout exceeded while attempting flock lock!");
250 mutt_message (_("Waiting for flock attempt... %d"), ++attempt);
253 #endif /* USE_FLOCK */
257 r = dotlock_file (path, fd, timeout);
258 #endif /* USE_DOTLOCK */
261 /* release any other locks obtained in this routine */
264 lck.l_type = F_UNLCK;
265 fcntl (fd, F_SETLK, &lck);
266 #endif /* USE_FCNTL */
270 #endif /* USE_FLOCK */
278 int mx_unlock_file (const char *path, int fd, int dot)
281 struct flock unlockit = { F_UNLCK, 0, 0, 0 };
283 memset (&unlockit, 0, sizeof (struct flock));
284 unlockit.l_type = F_UNLCK;
285 unlockit.l_whence = SEEK_SET;
286 fcntl (fd, F_SETLK, &unlockit);
295 undotlock_file (path, fd);
301 void mx_unlink_empty (const char *path)
309 if ((fd = open (path, O_RDWR)) == -1)
312 if (mx_lock_file (path, fd, 1, 0, 1) == -1) {
318 invoke_dotlock (path, fd, DL_FL_UNLINK, 1);
320 if (fstat (fd, &sb) == 0 && sb.st_size == 0)
324 mx_unlock_file (path, fd, 0);
328 /* try to figure out what type of mailbox ``path'' is */
329 int mx_get_magic (const char *path) {
332 if (safe_strlen (path) == 0)
334 if ((i = mx_get_idx (path)) >= 0) {
335 fprintf (stderr, "%s is %i\n", NONULL(path), i);
336 return (MX_COMMAND(i,type));
342 * set DefaultMagic to the given value
344 int mx_set_magic (const char *s)
346 if (ascii_strcasecmp (s, "mbox") == 0)
347 DefaultMagic = M_MBOX;
348 else if (ascii_strcasecmp (s, "mmdf") == 0)
349 DefaultMagic = M_MMDF;
350 else if (ascii_strcasecmp (s, "mh") == 0)
352 else if (ascii_strcasecmp (s, "maildir") == 0)
353 DefaultMagic = M_MAILDIR;
360 /* mx_access: Wrapper for access, checks permissions on a given mailbox.
361 * We may be interested in using ACL-style flags at some point, currently
362 * we use the normal access() flags. */
363 int mx_access (const char *path, int flags)
367 if ((i = mx_get_idx (path)) >= 0 && MX_COMMAND(i,mx_access))
368 return (MX_COMMAND(i,mx_access)(path,flags));
372 static int mx_open_mailbox_append (CONTEXT * ctx, int flags)
376 #ifdef USE_COMPRESSED
377 /* special case for appending to compressed folders -
378 * even if we can not open them for reading */
379 if (mutt_can_append_compressed (ctx->path))
380 mutt_open_append_compressed (ctx);
387 if (mx_get_magic (ctx->path) == M_IMAP)
388 return imap_open_mailbox_append (ctx);
392 if (stat (ctx->path, &sb) == 0) {
393 ctx->magic = mx_get_magic (ctx->path);
395 switch (ctx->magic) {
397 mutt_error (_("%s is not a mailbox."), ctx->path);
403 else if (errno == ENOENT) {
404 ctx->magic = DefaultMagic;
406 if (ctx->magic == M_MH || ctx->magic == M_MAILDIR) {
407 char tmp[_POSIX_PATH_MAX];
409 if (mkdir (ctx->path, S_IRWXU)) {
410 mutt_perror (ctx->path);
414 if (ctx->magic == M_MAILDIR) {
415 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
416 if (mkdir (tmp, S_IRWXU)) {
422 snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
423 if (mkdir (tmp, S_IRWXU)) {
425 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
430 snprintf (tmp, sizeof (tmp), "%s/tmp", ctx->path);
431 if (mkdir (tmp, S_IRWXU)) {
433 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
435 snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
444 snprintf (tmp, sizeof (tmp), "%s/.mh_sequences", ctx->path);
445 if ((i = creat (tmp, S_IRWXU)) == -1) {
455 mutt_perror (ctx->path);
459 switch (ctx->magic) {
463 safe_fopen (ctx->path, flags & M_NEWFOLDER ? "w" : "a")) == NULL
464 || mbox_lock_mailbox (ctx, 1, 1) != 0) {
466 mutt_perror (ctx->path);
468 mutt_error (_("Couldn't lock %s\n"), ctx->path);
469 safe_fclose (&ctx->fp);
473 fseek (ctx->fp, 0, 2);
489 * open a mailbox and parse it
492 * flags M_NOSORT do not sort mailbox
493 * M_APPEND open mailbox for appending
494 * M_READONLY open mailbox in read-only mode
495 * M_QUIET only print error messages
496 * ctx if non-null, context struct to use
498 CONTEXT *mx_open_mailbox (const char *path, int flags, CONTEXT * pctx)
504 ctx = safe_malloc (sizeof (CONTEXT));
505 memset (ctx, 0, sizeof (CONTEXT));
506 ctx->path = safe_strdup (path);
508 ctx->msgnotreadyet = -1;
513 if (flags & M_READONLY)
516 if (flags & (M_APPEND | M_NEWFOLDER)) {
517 if (mx_open_mailbox_append (ctx, flags) != 0) {
518 mx_fastclose_mailbox (ctx);
526 ctx->magic = mx_get_magic (path);
528 if (ctx->magic == M_COMPRESSED)
529 mutt_open_read_compressed (ctx);
532 mutt_error (_("%s is not a mailbox."), path);
534 if (ctx->magic == -1)
537 if (ctx->magic <= 0) {
538 mx_fastclose_mailbox (ctx);
544 /* if the user has a `push' command in their .muttrc, or in a folder-hook,
545 * it will cause the progress messages not to be displayed because
546 * mutt_refresh() will think we are in the middle of a macro. so set a
547 * flag to indicate that we should really refresh the screen.
549 set_option (OPTFORCEREFRESH);
552 mutt_message (_("Reading %s..."), ctx->path);
554 if ((rc = mx_get_idx (ctx->path)) >= 0)
555 rc = MX_COMMAND(rc,mx_open_mailbox)(ctx);
558 if ((flags & M_NOSORT) == 0) {
559 /* avoid unnecessary work since the mailbox is completely unthreaded
561 unset_option (OPTSORTSUBTHREADS);
562 unset_option (OPTNEEDRESCORE);
563 mutt_sort_headers (ctx, 1);
569 mx_fastclose_mailbox (ctx);
574 unset_option (OPTFORCEREFRESH);
578 /* free up memory associated with the mailbox context */
579 void mx_fastclose_mailbox (CONTEXT * ctx)
587 if (ctx->magic == M_IMAP)
588 imap_close_mailbox (ctx);
589 #endif /* USE_IMAP */
591 if (ctx->magic == M_POP)
592 pop_close_mailbox (ctx);
595 if (ctx->magic == M_NNTP)
596 nntp_fastclose_mailbox (ctx);
597 #endif /* USE_NNTP */
599 hash_destroy (&ctx->subj_hash, NULL);
601 hash_destroy (&ctx->id_hash, NULL);
602 mutt_clear_threads (ctx);
603 for (i = 0; i < ctx->msgcount; i++)
604 mutt_free_header (&ctx->hdrs[i]);
607 #ifdef USE_COMPRESSED
608 if (ctx->compressinfo)
609 mutt_fast_close_compressed (ctx);
612 FREE (&ctx->pattern);
613 if (ctx->limit_pattern)
614 mutt_pattern_free (&ctx->limit_pattern);
615 safe_fclose (&ctx->fp);
616 memset (ctx, 0, sizeof (CONTEXT));
619 /* save changes to disk */
620 static int sync_mailbox (CONTEXT * ctx, int *index_hint)
628 mutt_message (_("Writing %s..."), ctx->path);
630 switch (ctx->magic) {
633 rc = mbox_sync_mailbox (ctx, index_hint);
635 tmp = mutt_find_mailbox (ctx->path);
641 rc = mh_sync_mailbox (ctx, index_hint);
646 /* extra argument means EXPUNGE */
647 rc = imap_sync_mailbox (ctx, 1, index_hint);
649 #endif /* USE_IMAP */
653 rc = pop_sync_mailbox (ctx, index_hint);
659 rc = nntp_sync_mailbox (ctx);
661 #endif /* USE_NNTP */
665 if (!ctx->quiet && !ctx->shutup && rc == -1)
666 mutt_error (_("Could not synchronize mailbox %s!"), ctx->path);
670 if (tmp && tmp->new == 0)
671 mutt_update_mailbox (tmp);
674 #ifdef USE_COMPRESSED
675 if (rc == 0 && ctx->compressinfo)
676 return mutt_sync_compressed (ctx);
682 /* move deleted mails to the trash folder */
683 static int trash_append (CONTEXT * ctx)
689 if (!TrashPath || !ctx->deleted ||
690 (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))
693 for (; i < ctx->msgcount && (!ctx->hdrs[i]->deleted ||
694 ctx->hdrs[i]->appended); i++);
695 if (i == ctx->msgcount)
696 return 0; /* nothing to be done */
698 if (mutt_save_confirm (TrashPath, &st) != 0) {
699 mutt_error _("message(s) not deleted");
704 if (lstat (ctx->path, &stc) == 0 && stc.st_ino == st.st_ino
705 && stc.st_dev == st.st_dev && stc.st_rdev == st.st_rdev)
706 return 0; /* we are in the trash folder: simple sync */
708 if ((ctx_trash = mx_open_mailbox (TrashPath, M_APPEND, NULL)) != NULL) {
709 for (i = 0; i < ctx->msgcount; i++)
710 if (ctx->hdrs[i]->deleted && !ctx->hdrs[i]->appended
711 && !ctx->hdrs[i]->purged
712 && mutt_append_message (ctx_trash, ctx, ctx->hdrs[i], 0, 0) == -1) {
713 mx_close_mailbox (ctx_trash, NULL);
717 mx_close_mailbox (ctx_trash, NULL);
720 mutt_error _("Can't open trash folder");
728 /* save changes and close mailbox */
729 int mx_close_mailbox (CONTEXT * ctx, int *index_hint)
731 int i, move_messages = 0, purge = 1, read_msgs = 0;
735 char mbox[_POSIX_PATH_MAX];
736 char buf[SHORT_STRING];
744 if (ctx->magic == M_NNTP) {
747 ret = nntp_close_mailbox (ctx);
748 mx_fastclose_mailbox (ctx);
752 if (ctx->readonly || ctx->dontwrite) {
753 /* mailbox is readonly or we don't want to write */
754 mx_fastclose_mailbox (ctx);
759 /* mailbox was opened in write-mode */
760 if (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
761 mbox_close_mailbox (ctx);
763 mx_fastclose_mailbox (ctx);
767 for (i = 0; i < ctx->msgcount; i++) {
768 if (!ctx->hdrs[i]->deleted && ctx->hdrs[i]->read
769 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
773 if (read_msgs && quadoption (OPT_MOVE) != M_NO) {
776 if ((p = mutt_find_hook (M_MBOXHOOK, ctx->path))) {
778 strfcpy (mbox, p, sizeof (mbox));
781 strfcpy (mbox, NONULL (Inbox), sizeof (mbox));
782 isSpool = mutt_is_spool (ctx->path) && !mutt_is_spool (mbox);
784 mutt_expand_path (mbox, sizeof (mbox));
787 snprintf (buf, sizeof (buf), _("Move read messages to %s?"), mbox);
788 if ((move_messages = query_quadoption (OPT_MOVE, buf)) == -1) {
796 * There is no point in asking whether or not to purge if we are
797 * just marking messages as "trash".
799 if (ctx->deleted && !(ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH))) {
800 snprintf (buf, sizeof (buf), ctx->deleted == 1
801 ? _("Purge %d deleted message?") :
802 _("Purge %d deleted messages?"), ctx->deleted);
803 if ((purge = query_quadoption (OPT_DELETE, buf)) < 0) {
810 /* IMAP servers manage the OLD flag themselves */
811 if (ctx->magic != M_IMAP)
813 if (option (OPTMARKOLD)) {
814 for (i = 0; i < ctx->msgcount; i++) {
815 if (!ctx->hdrs[i]->deleted && !ctx->hdrs[i]->old)
816 mutt_set_flag (ctx, ctx->hdrs[i], M_OLD, 1);
821 mutt_message (_("Moving read messages to %s..."), mbox);
824 /* try to use server-side copy first */
827 if (ctx->magic == M_IMAP && mx_get_magic (mbox) == M_IMAP) {
828 /* tag messages for moving, and clear old tags, if any */
829 for (i = 0; i < ctx->msgcount; i++)
830 if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted
831 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
832 ctx->hdrs[i]->tagged = 1;
834 ctx->hdrs[i]->tagged = 0;
836 i = imap_copy_messages (ctx, NULL, mbox, 1);
839 if (i == 0) /* success */
841 else if (i == -1) { /* horrible error, bail */
845 else /* use regular append-copy mode */
848 if (mx_open_mailbox (mbox, M_APPEND, &f) == NULL) {
853 for (i = 0; i < ctx->msgcount; i++) {
854 if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted
855 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED))) {
856 if (mutt_append_message (&f, ctx, ctx->hdrs[i], 0, CH_UPDATE_LEN) ==
858 mutt_set_flag (ctx, ctx->hdrs[i], M_DELETE, 1);
859 mutt_set_flag (ctx, ctx->hdrs[i], M_APPENDED, 1);
862 mx_close_mailbox (&f, NULL);
869 mx_close_mailbox (&f, NULL);
873 else if (!ctx->changed && ctx->deleted == 0) {
874 mutt_message _("Mailbox is unchanged.");
876 mx_fastclose_mailbox (ctx);
880 /* copy mails to the trash before expunging */
881 if (purge && ctx->deleted)
882 if (trash_append (ctx) != 0) {
888 /* allow IMAP to preserve the deleted flag across sessions */
889 if (ctx->magic == M_IMAP) {
890 if ((check = imap_sync_mailbox (ctx, purge, index_hint)) != 0) {
899 for (i = 0; i < ctx->msgcount; i++)
900 ctx->hdrs[i]->deleted = 0;
904 if (ctx->changed || ctx->deleted) {
905 if ((check = sync_mailbox (ctx, index_hint)) != 0) {
913 mutt_message (_("%d kept, %d moved, %d deleted."),
914 ctx->msgcount - ctx->deleted, read_msgs, ctx->deleted);
916 mutt_message (_("%d kept, %d deleted."),
917 ctx->msgcount - ctx->deleted, ctx->deleted);
919 if (ctx->msgcount == ctx->deleted &&
920 (ctx->magic == M_MMDF || ctx->magic == M_MBOX) &&
921 !mutt_is_spool (ctx->path) && !option (OPTSAVEEMPTY))
922 mx_unlink_empty (ctx->path);
924 #ifdef USE_COMPRESSED
925 if (ctx->compressinfo && mutt_slow_close_compressed (ctx))
929 mx_fastclose_mailbox (ctx);
935 /* update a Context structure's internal tables. */
937 void mx_update_tables (CONTEXT * ctx, int committing)
941 /* update memory to reflect the new state of the mailbox */
950 #define this_body ctx->hdrs[j]->content
951 for (i = 0, j = 0; i < ctx->msgcount; i++) {
952 if ((committing && (!ctx->hdrs[i]->deleted ||
953 (ctx->magic == M_MAILDIR
954 && option (OPTMAILDIRTRASH)))) || (!committing
958 ctx->hdrs[j] = ctx->hdrs[i];
961 ctx->hdrs[j]->msgno = j;
962 if (ctx->hdrs[j]->virtual != -1) {
963 ctx->v2r[ctx->vcount] = j;
964 ctx->hdrs[j]->virtual = ctx->vcount++;
965 ctx->vsize += this_body->length + this_body->offset -
966 this_body->hdr_offset;
970 ctx->hdrs[j]->changed = 0;
971 else if (ctx->hdrs[j]->changed)
975 || (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH))) {
976 if (ctx->hdrs[j]->deleted)
980 if (ctx->hdrs[j]->tagged)
982 if (ctx->hdrs[j]->flagged)
984 if (!ctx->hdrs[j]->read) {
986 if (!ctx->hdrs[j]->old)
993 if (ctx->magic == M_MH || ctx->magic == M_MAILDIR)
994 ctx->size -= (ctx->hdrs[i]->content->length +
995 ctx->hdrs[i]->content->offset -
996 ctx->hdrs[i]->content->hdr_offset);
997 /* remove message from the hash tables */
998 if (ctx->subj_hash && ctx->hdrs[i]->env->real_subj)
999 hash_delete (ctx->subj_hash, ctx->hdrs[i]->env->real_subj,
1000 ctx->hdrs[i], NULL);
1001 if (ctx->id_hash && ctx->hdrs[i]->env->message_id)
1002 hash_delete (ctx->id_hash, ctx->hdrs[i]->env->message_id,
1003 ctx->hdrs[i], NULL);
1004 mutt_free_header (&ctx->hdrs[i]);
1012 /* save changes to mailbox
1018 int mx_sync_mailbox (CONTEXT * ctx, int *index_hint)
1022 int msgcount, deleted;
1024 if (ctx->dontwrite) {
1025 char buf[STRING], tmp[STRING];
1027 if (km_expand_key (buf, sizeof (buf),
1028 km_find_func (MENU_MAIN, OP_TOGGLE_WRITE)))
1029 snprintf (tmp, sizeof (tmp), _(" Press '%s' to toggle write"), buf);
1031 strfcpy (tmp, _("Use 'toggle-write' to re-enable write!"),
1034 mutt_error (_("Mailbox is marked unwritable. %s"), tmp);
1037 else if (ctx->readonly) {
1038 mutt_error _("Mailbox is read-only.");
1043 if (!ctx->changed && !ctx->deleted) {
1044 mutt_message _("Mailbox is unchanged.");
1050 char buf[SHORT_STRING];
1052 snprintf (buf, sizeof (buf), ctx->deleted == 1
1053 ? _("Purge %d deleted message?") :
1054 _("Purge %d deleted messages?"), ctx->deleted);
1055 if ((purge = query_quadoption (OPT_DELETE, buf)) < 0)
1057 else if (purge == M_NO) {
1059 return 0; /* nothing to do! */
1061 /* let IMAP servers hold on to D flags */
1062 if (ctx->magic != M_IMAP)
1065 for (i = 0; i < ctx->msgcount; i++)
1066 ctx->hdrs[i]->deleted = 0;
1070 else if (ctx->last_tag && ctx->last_tag->deleted)
1071 ctx->last_tag = NULL; /* reset last tagged msg now useless */
1074 /* really only for IMAP - imap_sync_mailbox results in a call to
1075 * mx_update_tables, so ctx->deleted is 0 when it comes back */
1076 msgcount = ctx->msgcount;
1077 deleted = ctx->deleted;
1079 if (purge && ctx->deleted) {
1080 if (trash_append (ctx) == -1)
1085 if (ctx->magic == M_IMAP)
1086 rc = imap_sync_mailbox (ctx, purge, index_hint);
1089 rc = sync_mailbox (ctx, index_hint);
1092 if (ctx->magic == M_IMAP && !purge)
1093 mutt_message (_("Mailbox checkpointed."));
1097 mutt_message (_("%d kept, %d deleted."), msgcount - deleted, deleted);
1101 if (ctx->msgcount == ctx->deleted &&
1102 (ctx->magic == M_MBOX || ctx->magic == M_MMDF) &&
1103 !mutt_is_spool (ctx->path) && !option (OPTSAVEEMPTY)) {
1105 mx_fastclose_mailbox (ctx);
1109 /* if we haven't deleted any messages, we don't need to resort */
1110 /* ... except for certain folder formats which need "unsorted"
1111 * sort order in order to synchronize folders.
1113 * MH and maildir are safe. mbox-style seems to need re-sorting,
1114 * at least with the new threading code.
1116 if (purge || (ctx->magic != M_MAILDIR && ctx->magic != M_MH)) {
1118 /* IMAP does this automatically after handling EXPUNGE */
1119 if (ctx->magic != M_IMAP)
1122 mx_update_tables (ctx, 1);
1123 mutt_sort_headers (ctx, 1); /* rethread from scratch */
1132 int imap_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr)
1134 char tmp[_POSIX_PATH_MAX];
1137 if ((msg->fp = safe_fopen (tmp, "w")) == NULL)
1139 msg->path = safe_strdup (tmp);
1145 * dest destintation mailbox
1146 * hdr message being copied (required for maildir support, because
1147 * the filename depends on the message flags)
1149 MESSAGE *mx_open_new_message (CONTEXT * dest, HEADER * hdr, int flags)
1152 int (*func) (MESSAGE *, CONTEXT *, HEADER *);
1155 switch (dest->magic) {
1158 func = mbox_open_new_message;
1161 func = maildir_open_new_message;
1164 func = mh_open_new_message;
1168 func = imap_open_new_message;
1174 "mx_open_new_message(): function unimplemented for mailbox type %d.\n",
1179 msg = safe_calloc (1, sizeof (MESSAGE));
1180 msg->magic = dest->magic;
1184 msg->flags.flagged = hdr->flagged;
1185 msg->flags.replied = hdr->replied;
1186 msg->flags.read = hdr->read;
1187 msg->received = hdr->received;
1190 if (msg->received == 0)
1191 time (&msg->received);
1193 if (func (msg, dest, hdr) == 0) {
1194 if (dest->magic == M_MMDF)
1195 fputs (MMDF_SEP, msg->fp);
1197 if ((msg->magic == M_MBOX || msg->magic == M_MMDF) && flags & M_ADD_FROM) {
1199 if (hdr->env->return_path)
1200 p = hdr->env->return_path;
1201 else if (hdr->env->sender)
1202 p = hdr->env->sender;
1207 fprintf (msg->fp, "From %s %s", p ? p->mailbox : NONULL (Username),
1208 ctime (&msg->received));
1217 /* check for new mail */
1218 int mx_check_mailbox (CONTEXT * ctx, int *index_hint, int lock)
1222 #ifdef USE_COMPRESSED
1223 if (ctx->compressinfo)
1224 return mutt_check_mailbox_compressed (ctx);
1231 switch (ctx->magic) {
1236 mutt_block_signals ();
1237 if (mbox_lock_mailbox (ctx, 0, 0) == -1) {
1238 mutt_unblock_signals ();
1243 rc = mbox_check_mailbox (ctx, index_hint);
1246 mutt_unblock_signals ();
1247 mbox_unlock_mailbox (ctx);
1254 return (mh_check_mailbox (ctx, index_hint));
1256 return (maildir_check_mailbox (ctx, index_hint));
1260 return (imap_check_mailbox (ctx, index_hint, 0));
1261 #endif /* USE_IMAP */
1265 return (pop_check_mailbox (ctx, index_hint));
1266 #endif /* USE_POP */
1270 return (nntp_check_mailbox (ctx));
1271 #endif /* USE_NNTP */
1275 dprint (1, (debugfile, "mx_check_mailbox: null or invalid context.\n"));
1279 /* return a stream pointer for a message */
1280 MESSAGE *mx_open_message (CONTEXT * ctx, int msgno)
1284 msg = safe_calloc (1, sizeof (MESSAGE));
1285 switch (msg->magic = ctx->magic) {
1294 HEADER *cur = ctx->hdrs[msgno];
1295 char path[_POSIX_PATH_MAX];
1297 snprintf (path, sizeof (path), "%s/%s", ctx->path, cur->path);
1299 if ((msg->fp = fopen (path, "r")) == NULL && errno == ENOENT &&
1300 ctx->magic == M_MAILDIR)
1301 msg->fp = maildir_open_find_message (ctx->path, cur->path);
1303 if (msg->fp == NULL) {
1305 dprint (1, (debugfile, "mx_open_message: fopen: %s: %s (errno %d).\n",
1306 path, strerror (errno), errno));
1315 if (imap_fetch_message (msg, ctx, msgno) != 0)
1319 #endif /* USE_IMAP */
1324 if (pop_fetch_message (msg, ctx, msgno) != 0)
1328 #endif /* USE_POP */
1333 if (nntp_fetch_message (msg, ctx, msgno) != 0)
1337 #endif /* USE_NNTP */
1342 "mx_open_message(): function not implemented for mailbox type %d.\n",
1350 /* commit a message to a folder */
1352 int mx_commit_message (MESSAGE * msg, CONTEXT * ctx)
1356 if (!(msg->write && ctx->append)) {
1359 "mx_commit_message(): msg->write = %d, ctx->append = %d\n",
1360 msg->write, ctx->append));
1364 switch (msg->magic) {
1367 if (fputs (MMDF_SEP, msg->fp) == EOF)
1374 if (fputc ('\n', msg->fp) == EOF)
1382 if ((r = safe_fclose (&msg->fp)) == 0)
1383 r = imap_append_message (ctx, msg);
1390 r = maildir_commit_message (ctx, msg, NULL);
1396 r = mh_commit_message (ctx, msg, NULL);
1401 if (r == 0 && (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
1402 && (fflush (msg->fp) == EOF || fsync (fileno (msg->fp)) == -1)) {
1403 mutt_perror (_("Can't write message"));
1411 /* close a pointer to a message */
1412 int mx_close_message (MESSAGE ** msg)
1416 if ((*msg)->magic == M_MH || (*msg)->magic == M_MAILDIR
1418 || (*msg)->magic == M_IMAP
1421 || (*msg)->magic == M_POP
1424 || (*msg)->magic == M_NNTP
1427 r = safe_fclose (&(*msg)->fp);
1433 dprint (1, (debugfile, "mx_close_message (): unlinking %s\n",
1435 unlink ((*msg)->path);
1436 FREE (&(*msg)->path);
1443 void mx_alloc_memory (CONTEXT * ctx)
1446 size_t s = MAX (sizeof (HEADER *), sizeof (int));
1448 if ((ctx->hdrmax + 25) * s < ctx->hdrmax * s) {
1449 mutt_error _("Integer overflow -- can't allocate memory.");
1456 safe_realloc (&ctx->hdrs, sizeof (HEADER *) * (ctx->hdrmax += 25));
1457 safe_realloc (&ctx->v2r, sizeof (int) * ctx->hdrmax);
1460 ctx->hdrs = safe_calloc ((ctx->hdrmax += 25), sizeof (HEADER *));
1461 ctx->v2r = safe_calloc (ctx->hdrmax, sizeof (int));
1463 for (i = ctx->msgcount; i < ctx->hdrmax; i++) {
1464 ctx->hdrs[i] = NULL;
1469 /* this routine is called to update the counts in the context structure for
1470 * the last message header parsed.
1472 void mx_update_context (CONTEXT * ctx, int new_messages)
1477 for (msgno = ctx->msgcount - new_messages; msgno < ctx->msgcount; msgno++) {
1478 h = ctx->hdrs[msgno];
1481 /* NOTE: this _must_ be done before the check for mailcap! */
1482 h->security = crypt_query (h->content);
1485 if (!ctx->pattern) {
1486 ctx->v2r[ctx->vcount] = msgno;
1487 h->virtual = ctx->vcount++;
1493 if (h->env->supersedes) {
1497 ctx->id_hash = mutt_make_id_hash (ctx);
1499 h2 = hash_find (ctx->id_hash, h->env->supersedes);
1501 /* FREE (&h->env->supersedes); should I ? */
1504 if (option (OPTSCORE))
1505 mutt_score_message (ctx, h2, 1);
1509 /* add this message to the hash tables */
1510 if (ctx->id_hash && h->env->message_id)
1511 hash_insert (ctx->id_hash, h->env->message_id, h, 0);
1512 if (ctx->subj_hash && h->env->real_subj)
1513 hash_insert (ctx->subj_hash, h->env->real_subj, h, 1);
1515 if (option (OPTSCORE))
1516 mutt_score_message (ctx, h, 0);
1534 * 1 if the specified mailbox contains 0 messages.
1535 * 0 if the mailbox contains messages
1538 int mx_check_empty (const char *path)
1541 if ((i = mx_get_idx (path)) >= 0 && MX_COMMAND(i,mx_check_empty))
1542 return (MX_COMMAND(i,mx_check_empty)(path));
1547 void mx_init (void) {
1551 list_push_back (&MailboxFormats, (void*) mbox_reg_mx ());
1552 list_push_back (&MailboxFormats, (void*) mmdf_reg_mx ());
1553 list_push_back (&MailboxFormats, (void*) mh_reg_mx ());
1554 list_push_back (&MailboxFormats, (void*) maildir_reg_mx ());
1556 list_push_back (&MailboxFormats, (void*) imap_reg_mx ());
1559 list_push_back (&MailboxFormats, (void*) pop_reg_mx ());
1562 list_push_back (&MailboxFormats, (void*) nntp_reg_mx ());
1564 #ifdef USE_COMPRESSED
1565 list_push_back (&MailboxFormats, (void*) compress_reg_mx ());
1568 /* check module registration for completeness with debug versions */
1569 #define EXITWITHERR(m) do { fprintf(stderr, "error: incomplete mx module: %s is missing for type %i\n",m,i);exit(1); } while (0)
1570 for (i = 0; i < MailboxFormats->length; i++) {
1571 if (MX_COMMAND(i,type) < 1) EXITWITHERR("type");
1572 if (!MX_COMMAND(i,mx_is_magic)) EXITWITHERR("mx_is_magic");
1573 if (!MX_COMMAND(i,mx_open_mailbox)) EXITWITHERR("mx_open_mailbox");