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 #ifdef USE_COMPRESSED
529 if (ctx->magic == M_COMPRESSED)
530 mutt_open_read_compressed (ctx);
534 mutt_error (_("%s is not a mailbox."), path);
536 if (ctx->magic == -1)
539 if (ctx->magic <= 0) {
540 mx_fastclose_mailbox (ctx);
546 /* if the user has a `push' command in their .muttrc, or in a folder-hook,
547 * it will cause the progress messages not to be displayed because
548 * mutt_refresh() will think we are in the middle of a macro. so set a
549 * flag to indicate that we should really refresh the screen.
551 set_option (OPTFORCEREFRESH);
554 mutt_message (_("Reading %s..."), ctx->path);
556 if ((rc = mx_get_idx (ctx->path)) >= 0)
557 rc = MX_COMMAND(rc,mx_open_mailbox)(ctx);
560 if ((flags & M_NOSORT) == 0) {
561 /* avoid unnecessary work since the mailbox is completely unthreaded
563 unset_option (OPTSORTSUBTHREADS);
564 unset_option (OPTNEEDRESCORE);
565 mutt_sort_headers (ctx, 1);
571 mx_fastclose_mailbox (ctx);
576 unset_option (OPTFORCEREFRESH);
580 /* free up memory associated with the mailbox context */
581 void mx_fastclose_mailbox (CONTEXT * ctx)
589 if (ctx->magic == M_IMAP)
590 imap_close_mailbox (ctx);
591 #endif /* USE_IMAP */
593 if (ctx->magic == M_POP)
594 pop_close_mailbox (ctx);
597 if (ctx->magic == M_NNTP)
598 nntp_fastclose_mailbox (ctx);
599 #endif /* USE_NNTP */
601 hash_destroy (&ctx->subj_hash, NULL);
603 hash_destroy (&ctx->id_hash, NULL);
604 mutt_clear_threads (ctx);
605 for (i = 0; i < ctx->msgcount; i++)
606 mutt_free_header (&ctx->hdrs[i]);
609 #ifdef USE_COMPRESSED
610 if (ctx->compressinfo)
611 mutt_fast_close_compressed (ctx);
614 FREE (&ctx->pattern);
615 if (ctx->limit_pattern)
616 mutt_pattern_free (&ctx->limit_pattern);
617 safe_fclose (&ctx->fp);
618 memset (ctx, 0, sizeof (CONTEXT));
621 /* save changes to disk */
622 static int sync_mailbox (CONTEXT * ctx, int *index_hint)
630 mutt_message (_("Writing %s..."), ctx->path);
632 switch (ctx->magic) {
635 rc = mbox_sync_mailbox (ctx, index_hint);
637 tmp = mutt_find_mailbox (ctx->path);
643 rc = mh_sync_mailbox (ctx, index_hint);
648 /* extra argument means EXPUNGE */
649 rc = imap_sync_mailbox (ctx, 1, index_hint);
651 #endif /* USE_IMAP */
655 rc = pop_sync_mailbox (ctx, index_hint);
661 rc = nntp_sync_mailbox (ctx);
663 #endif /* USE_NNTP */
667 if (!ctx->quiet && !ctx->shutup && rc == -1)
668 mutt_error (_("Could not synchronize mailbox %s!"), ctx->path);
672 if (tmp && tmp->new == 0)
673 mutt_update_mailbox (tmp);
676 #ifdef USE_COMPRESSED
677 if (rc == 0 && ctx->compressinfo)
678 return mutt_sync_compressed (ctx);
684 /* move deleted mails to the trash folder */
685 static int trash_append (CONTEXT * ctx)
691 if (!TrashPath || !ctx->deleted ||
692 (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))
695 for (; i < ctx->msgcount && (!ctx->hdrs[i]->deleted ||
696 ctx->hdrs[i]->appended); i++);
697 if (i == ctx->msgcount)
698 return 0; /* nothing to be done */
700 if (mutt_save_confirm (TrashPath, &st) != 0) {
701 mutt_error _("message(s) not deleted");
706 if (lstat (ctx->path, &stc) == 0 && stc.st_ino == st.st_ino
707 && stc.st_dev == st.st_dev && stc.st_rdev == st.st_rdev)
708 return 0; /* we are in the trash folder: simple sync */
710 if ((ctx_trash = mx_open_mailbox (TrashPath, M_APPEND, NULL)) != NULL) {
711 for (i = 0; i < ctx->msgcount; i++)
712 if (ctx->hdrs[i]->deleted && !ctx->hdrs[i]->appended
713 && !ctx->hdrs[i]->purged
714 && mutt_append_message (ctx_trash, ctx, ctx->hdrs[i], 0, 0) == -1) {
715 mx_close_mailbox (ctx_trash, NULL);
719 mx_close_mailbox (ctx_trash, NULL);
722 mutt_error _("Can't open trash folder");
730 /* save changes and close mailbox */
731 int mx_close_mailbox (CONTEXT * ctx, int *index_hint)
733 int i, move_messages = 0, purge = 1, read_msgs = 0;
737 char mbox[_POSIX_PATH_MAX];
738 char buf[SHORT_STRING];
746 if (ctx->magic == M_NNTP) {
749 ret = nntp_close_mailbox (ctx);
750 mx_fastclose_mailbox (ctx);
754 if (ctx->readonly || ctx->dontwrite) {
755 /* mailbox is readonly or we don't want to write */
756 mx_fastclose_mailbox (ctx);
761 /* mailbox was opened in write-mode */
762 if (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
763 mbox_close_mailbox (ctx);
765 mx_fastclose_mailbox (ctx);
769 for (i = 0; i < ctx->msgcount; i++) {
770 if (!ctx->hdrs[i]->deleted && ctx->hdrs[i]->read
771 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
775 if (read_msgs && quadoption (OPT_MOVE) != M_NO) {
778 if ((p = mutt_find_hook (M_MBOXHOOK, ctx->path))) {
780 strfcpy (mbox, p, sizeof (mbox));
783 strfcpy (mbox, NONULL (Inbox), sizeof (mbox));
784 isSpool = mutt_is_spool (ctx->path) && !mutt_is_spool (mbox);
786 mutt_expand_path (mbox, sizeof (mbox));
789 snprintf (buf, sizeof (buf), _("Move read messages to %s?"), mbox);
790 if ((move_messages = query_quadoption (OPT_MOVE, buf)) == -1) {
798 * There is no point in asking whether or not to purge if we are
799 * just marking messages as "trash".
801 if (ctx->deleted && !(ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH))) {
802 snprintf (buf, sizeof (buf), ctx->deleted == 1
803 ? _("Purge %d deleted message?") :
804 _("Purge %d deleted messages?"), ctx->deleted);
805 if ((purge = query_quadoption (OPT_DELETE, buf)) < 0) {
812 /* IMAP servers manage the OLD flag themselves */
813 if (ctx->magic != M_IMAP)
815 if (option (OPTMARKOLD)) {
816 for (i = 0; i < ctx->msgcount; i++) {
817 if (!ctx->hdrs[i]->deleted && !ctx->hdrs[i]->old)
818 mutt_set_flag (ctx, ctx->hdrs[i], M_OLD, 1);
823 mutt_message (_("Moving read messages to %s..."), mbox);
826 /* try to use server-side copy first */
829 if (ctx->magic == M_IMAP && mx_get_magic (mbox) == M_IMAP) {
830 /* tag messages for moving, and clear old tags, if any */
831 for (i = 0; i < ctx->msgcount; i++)
832 if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted
833 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
834 ctx->hdrs[i]->tagged = 1;
836 ctx->hdrs[i]->tagged = 0;
838 i = imap_copy_messages (ctx, NULL, mbox, 1);
841 if (i == 0) /* success */
843 else if (i == -1) { /* horrible error, bail */
847 else /* use regular append-copy mode */
850 if (mx_open_mailbox (mbox, M_APPEND, &f) == NULL) {
855 for (i = 0; i < ctx->msgcount; i++) {
856 if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted
857 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED))) {
858 if (mutt_append_message (&f, ctx, ctx->hdrs[i], 0, CH_UPDATE_LEN) ==
860 mutt_set_flag (ctx, ctx->hdrs[i], M_DELETE, 1);
861 mutt_set_flag (ctx, ctx->hdrs[i], M_APPENDED, 1);
864 mx_close_mailbox (&f, NULL);
871 mx_close_mailbox (&f, NULL);
875 else if (!ctx->changed && ctx->deleted == 0) {
876 mutt_message _("Mailbox is unchanged.");
878 mx_fastclose_mailbox (ctx);
882 /* copy mails to the trash before expunging */
883 if (purge && ctx->deleted)
884 if (trash_append (ctx) != 0) {
890 /* allow IMAP to preserve the deleted flag across sessions */
891 if (ctx->magic == M_IMAP) {
892 if ((check = imap_sync_mailbox (ctx, purge, index_hint)) != 0) {
901 for (i = 0; i < ctx->msgcount; i++)
902 ctx->hdrs[i]->deleted = 0;
906 if (ctx->changed || ctx->deleted) {
907 if ((check = sync_mailbox (ctx, index_hint)) != 0) {
915 mutt_message (_("%d kept, %d moved, %d deleted."),
916 ctx->msgcount - ctx->deleted, read_msgs, ctx->deleted);
918 mutt_message (_("%d kept, %d deleted."),
919 ctx->msgcount - ctx->deleted, ctx->deleted);
921 if (ctx->msgcount == ctx->deleted &&
922 (ctx->magic == M_MMDF || ctx->magic == M_MBOX) &&
923 !mutt_is_spool (ctx->path) && !option (OPTSAVEEMPTY))
924 mx_unlink_empty (ctx->path);
926 #ifdef USE_COMPRESSED
927 if (ctx->compressinfo && mutt_slow_close_compressed (ctx))
931 mx_fastclose_mailbox (ctx);
937 /* update a Context structure's internal tables. */
939 void mx_update_tables (CONTEXT * ctx, int committing)
943 /* update memory to reflect the new state of the mailbox */
952 #define this_body ctx->hdrs[j]->content
953 for (i = 0, j = 0; i < ctx->msgcount; i++) {
954 if ((committing && (!ctx->hdrs[i]->deleted ||
955 (ctx->magic == M_MAILDIR
956 && option (OPTMAILDIRTRASH)))) || (!committing
960 ctx->hdrs[j] = ctx->hdrs[i];
963 ctx->hdrs[j]->msgno = j;
964 if (ctx->hdrs[j]->virtual != -1) {
965 ctx->v2r[ctx->vcount] = j;
966 ctx->hdrs[j]->virtual = ctx->vcount++;
967 ctx->vsize += this_body->length + this_body->offset -
968 this_body->hdr_offset;
972 ctx->hdrs[j]->changed = 0;
973 else if (ctx->hdrs[j]->changed)
977 || (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH))) {
978 if (ctx->hdrs[j]->deleted)
982 if (ctx->hdrs[j]->tagged)
984 if (ctx->hdrs[j]->flagged)
986 if (!ctx->hdrs[j]->read) {
988 if (!ctx->hdrs[j]->old)
995 if (ctx->magic == M_MH || ctx->magic == M_MAILDIR)
996 ctx->size -= (ctx->hdrs[i]->content->length +
997 ctx->hdrs[i]->content->offset -
998 ctx->hdrs[i]->content->hdr_offset);
999 /* remove message from the hash tables */
1000 if (ctx->subj_hash && ctx->hdrs[i]->env->real_subj)
1001 hash_delete (ctx->subj_hash, ctx->hdrs[i]->env->real_subj,
1002 ctx->hdrs[i], NULL);
1003 if (ctx->id_hash && ctx->hdrs[i]->env->message_id)
1004 hash_delete (ctx->id_hash, ctx->hdrs[i]->env->message_id,
1005 ctx->hdrs[i], NULL);
1006 mutt_free_header (&ctx->hdrs[i]);
1014 /* save changes to mailbox
1020 int mx_sync_mailbox (CONTEXT * ctx, int *index_hint)
1024 int msgcount, deleted;
1026 if (ctx->dontwrite) {
1027 char buf[STRING], tmp[STRING];
1029 if (km_expand_key (buf, sizeof (buf),
1030 km_find_func (MENU_MAIN, OP_TOGGLE_WRITE)))
1031 snprintf (tmp, sizeof (tmp), _(" Press '%s' to toggle write"), buf);
1033 strfcpy (tmp, _("Use 'toggle-write' to re-enable write!"),
1036 mutt_error (_("Mailbox is marked unwritable. %s"), tmp);
1039 else if (ctx->readonly) {
1040 mutt_error _("Mailbox is read-only.");
1045 if (!ctx->changed && !ctx->deleted) {
1046 mutt_message _("Mailbox is unchanged.");
1052 char buf[SHORT_STRING];
1054 snprintf (buf, sizeof (buf), ctx->deleted == 1
1055 ? _("Purge %d deleted message?") :
1056 _("Purge %d deleted messages?"), ctx->deleted);
1057 if ((purge = query_quadoption (OPT_DELETE, buf)) < 0)
1059 else if (purge == M_NO) {
1061 return 0; /* nothing to do! */
1063 /* let IMAP servers hold on to D flags */
1064 if (ctx->magic != M_IMAP)
1067 for (i = 0; i < ctx->msgcount; i++)
1068 ctx->hdrs[i]->deleted = 0;
1072 else if (ctx->last_tag && ctx->last_tag->deleted)
1073 ctx->last_tag = NULL; /* reset last tagged msg now useless */
1076 /* really only for IMAP - imap_sync_mailbox results in a call to
1077 * mx_update_tables, so ctx->deleted is 0 when it comes back */
1078 msgcount = ctx->msgcount;
1079 deleted = ctx->deleted;
1081 if (purge && ctx->deleted) {
1082 if (trash_append (ctx) == -1)
1087 if (ctx->magic == M_IMAP)
1088 rc = imap_sync_mailbox (ctx, purge, index_hint);
1091 rc = sync_mailbox (ctx, index_hint);
1094 if (ctx->magic == M_IMAP && !purge)
1095 mutt_message (_("Mailbox checkpointed."));
1099 mutt_message (_("%d kept, %d deleted."), msgcount - deleted, deleted);
1103 if (ctx->msgcount == ctx->deleted &&
1104 (ctx->magic == M_MBOX || ctx->magic == M_MMDF) &&
1105 !mutt_is_spool (ctx->path) && !option (OPTSAVEEMPTY)) {
1107 mx_fastclose_mailbox (ctx);
1111 /* if we haven't deleted any messages, we don't need to resort */
1112 /* ... except for certain folder formats which need "unsorted"
1113 * sort order in order to synchronize folders.
1115 * MH and maildir are safe. mbox-style seems to need re-sorting,
1116 * at least with the new threading code.
1118 if (purge || (ctx->magic != M_MAILDIR && ctx->magic != M_MH)) {
1120 /* IMAP does this automatically after handling EXPUNGE */
1121 if (ctx->magic != M_IMAP)
1124 mx_update_tables (ctx, 1);
1125 mutt_sort_headers (ctx, 1); /* rethread from scratch */
1134 int imap_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr)
1136 char tmp[_POSIX_PATH_MAX];
1139 if ((msg->fp = safe_fopen (tmp, "w")) == NULL)
1141 msg->path = safe_strdup (tmp);
1147 * dest destintation mailbox
1148 * hdr message being copied (required for maildir support, because
1149 * the filename depends on the message flags)
1151 MESSAGE *mx_open_new_message (CONTEXT * dest, HEADER * hdr, int flags)
1154 int (*func) (MESSAGE *, CONTEXT *, HEADER *);
1157 switch (dest->magic) {
1160 func = mbox_open_new_message;
1163 func = maildir_open_new_message;
1166 func = mh_open_new_message;
1170 func = imap_open_new_message;
1176 "mx_open_new_message(): function unimplemented for mailbox type %d.\n",
1181 msg = safe_calloc (1, sizeof (MESSAGE));
1182 msg->magic = dest->magic;
1186 msg->flags.flagged = hdr->flagged;
1187 msg->flags.replied = hdr->replied;
1188 msg->flags.read = hdr->read;
1189 msg->received = hdr->received;
1192 if (msg->received == 0)
1193 time (&msg->received);
1195 if (func (msg, dest, hdr) == 0) {
1196 if (dest->magic == M_MMDF)
1197 fputs (MMDF_SEP, msg->fp);
1199 if ((msg->magic == M_MBOX || msg->magic == M_MMDF) && flags & M_ADD_FROM) {
1201 if (hdr->env->return_path)
1202 p = hdr->env->return_path;
1203 else if (hdr->env->sender)
1204 p = hdr->env->sender;
1209 fprintf (msg->fp, "From %s %s", p ? p->mailbox : NONULL (Username),
1210 ctime (&msg->received));
1219 /* check for new mail */
1220 int mx_check_mailbox (CONTEXT * ctx, int *index_hint, int lock)
1224 #ifdef USE_COMPRESSED
1225 if (ctx->compressinfo)
1226 return mutt_check_mailbox_compressed (ctx);
1233 switch (ctx->magic) {
1238 mutt_block_signals ();
1239 if (mbox_lock_mailbox (ctx, 0, 0) == -1) {
1240 mutt_unblock_signals ();
1245 rc = mbox_check_mailbox (ctx, index_hint);
1248 mutt_unblock_signals ();
1249 mbox_unlock_mailbox (ctx);
1256 return (mh_check_mailbox (ctx, index_hint));
1258 return (maildir_check_mailbox (ctx, index_hint));
1262 return (imap_check_mailbox (ctx, index_hint, 0));
1263 #endif /* USE_IMAP */
1267 return (pop_check_mailbox (ctx, index_hint));
1268 #endif /* USE_POP */
1272 return (nntp_check_mailbox (ctx));
1273 #endif /* USE_NNTP */
1277 dprint (1, (debugfile, "mx_check_mailbox: null or invalid context.\n"));
1281 /* return a stream pointer for a message */
1282 MESSAGE *mx_open_message (CONTEXT * ctx, int msgno)
1286 msg = safe_calloc (1, sizeof (MESSAGE));
1287 switch (msg->magic = ctx->magic) {
1296 HEADER *cur = ctx->hdrs[msgno];
1297 char path[_POSIX_PATH_MAX];
1299 snprintf (path, sizeof (path), "%s/%s", ctx->path, cur->path);
1301 if ((msg->fp = fopen (path, "r")) == NULL && errno == ENOENT &&
1302 ctx->magic == M_MAILDIR)
1303 msg->fp = maildir_open_find_message (ctx->path, cur->path);
1305 if (msg->fp == NULL) {
1307 dprint (1, (debugfile, "mx_open_message: fopen: %s: %s (errno %d).\n",
1308 path, strerror (errno), errno));
1317 if (imap_fetch_message (msg, ctx, msgno) != 0)
1321 #endif /* USE_IMAP */
1326 if (pop_fetch_message (msg, ctx, msgno) != 0)
1330 #endif /* USE_POP */
1335 if (nntp_fetch_message (msg, ctx, msgno) != 0)
1339 #endif /* USE_NNTP */
1344 "mx_open_message(): function not implemented for mailbox type %d.\n",
1352 /* commit a message to a folder */
1354 int mx_commit_message (MESSAGE * msg, CONTEXT * ctx)
1358 if (!(msg->write && ctx->append)) {
1361 "mx_commit_message(): msg->write = %d, ctx->append = %d\n",
1362 msg->write, ctx->append));
1366 switch (msg->magic) {
1369 if (fputs (MMDF_SEP, msg->fp) == EOF)
1376 if (fputc ('\n', msg->fp) == EOF)
1384 if ((r = safe_fclose (&msg->fp)) == 0)
1385 r = imap_append_message (ctx, msg);
1392 r = maildir_commit_message (ctx, msg, NULL);
1398 r = mh_commit_message (ctx, msg, NULL);
1403 if (r == 0 && (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
1404 && (fflush (msg->fp) == EOF || fsync (fileno (msg->fp)) == -1)) {
1405 mutt_perror (_("Can't write message"));
1413 /* close a pointer to a message */
1414 int mx_close_message (MESSAGE ** msg)
1418 if ((*msg)->magic == M_MH || (*msg)->magic == M_MAILDIR
1420 || (*msg)->magic == M_IMAP
1423 || (*msg)->magic == M_POP
1426 || (*msg)->magic == M_NNTP
1429 r = safe_fclose (&(*msg)->fp);
1435 dprint (1, (debugfile, "mx_close_message (): unlinking %s\n",
1437 unlink ((*msg)->path);
1438 FREE (&(*msg)->path);
1445 void mx_alloc_memory (CONTEXT * ctx)
1448 size_t s = MAX (sizeof (HEADER *), sizeof (int));
1450 if ((ctx->hdrmax + 25) * s < ctx->hdrmax * s) {
1451 mutt_error _("Integer overflow -- can't allocate memory.");
1458 safe_realloc (&ctx->hdrs, sizeof (HEADER *) * (ctx->hdrmax += 25));
1459 safe_realloc (&ctx->v2r, sizeof (int) * ctx->hdrmax);
1462 ctx->hdrs = safe_calloc ((ctx->hdrmax += 25), sizeof (HEADER *));
1463 ctx->v2r = safe_calloc (ctx->hdrmax, sizeof (int));
1465 for (i = ctx->msgcount; i < ctx->hdrmax; i++) {
1466 ctx->hdrs[i] = NULL;
1471 /* this routine is called to update the counts in the context structure for
1472 * the last message header parsed.
1474 void mx_update_context (CONTEXT * ctx, int new_messages)
1479 for (msgno = ctx->msgcount - new_messages; msgno < ctx->msgcount; msgno++) {
1480 h = ctx->hdrs[msgno];
1483 /* NOTE: this _must_ be done before the check for mailcap! */
1484 h->security = crypt_query (h->content);
1487 if (!ctx->pattern) {
1488 ctx->v2r[ctx->vcount] = msgno;
1489 h->virtual = ctx->vcount++;
1495 if (h->env->supersedes) {
1499 ctx->id_hash = mutt_make_id_hash (ctx);
1501 h2 = hash_find (ctx->id_hash, h->env->supersedes);
1503 /* FREE (&h->env->supersedes); should I ? */
1506 if (option (OPTSCORE))
1507 mutt_score_message (ctx, h2, 1);
1511 /* add this message to the hash tables */
1512 if (ctx->id_hash && h->env->message_id)
1513 hash_insert (ctx->id_hash, h->env->message_id, h, 0);
1514 if (ctx->subj_hash && h->env->real_subj)
1515 hash_insert (ctx->subj_hash, h->env->real_subj, h, 1);
1517 if (option (OPTSCORE))
1518 mutt_score_message (ctx, h, 0);
1536 * 1 if the specified mailbox contains 0 messages.
1537 * 0 if the mailbox contains messages
1540 int mx_check_empty (const char *path)
1543 if ((i = mx_get_idx (path)) >= 0 && MX_COMMAND(i,mx_check_empty))
1544 return (MX_COMMAND(i,mx_check_empty)(path));
1549 void mx_init (void) {
1553 list_push_back (&MailboxFormats, (void*) mbox_reg_mx ());
1554 list_push_back (&MailboxFormats, (void*) mmdf_reg_mx ());
1555 list_push_back (&MailboxFormats, (void*) mh_reg_mx ());
1556 list_push_back (&MailboxFormats, (void*) maildir_reg_mx ());
1558 list_push_back (&MailboxFormats, (void*) imap_reg_mx ());
1561 list_push_back (&MailboxFormats, (void*) pop_reg_mx ());
1564 list_push_back (&MailboxFormats, (void*) nntp_reg_mx ());
1566 #ifdef USE_COMPRESSED
1567 list_push_back (&MailboxFormats, (void*) compress_reg_mx ());
1570 /* check module registration for completeness with debug versions */
1571 #define EXITWITHERR(m) do { fprintf(stderr, "error: incomplete mx module: %s is missing for type %i\n",m,i);exit(1); } while (0)
1572 for (i = 0; i < MailboxFormats->length; i++) {
1573 if (MX_COMMAND(i,type) < 1) EXITWITHERR("type");
1574 if (!MX_COMMAND(i,mx_is_magic)) EXITWITHERR("mx_is_magic");
1575 if (!MX_COMMAND(i,mx_open_mailbox)) EXITWITHERR("mx_open_mailbox");