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);
159 return (t-1); /* use type as index for array */
165 * excl if excl != 0, request an exclusive lock
166 * dot if dot != 0, try to dotlock the file
167 * timeout should retry locking?
169 int mx_lock_file (const char *path, int fd, int excl, int dot, int timeout)
171 #if defined (USE_FCNTL) || defined (USE_FLOCK)
182 memset (&lck, 0, sizeof (struct flock));
183 lck.l_type = excl ? F_WRLCK : F_RDLCK;
184 lck.l_whence = SEEK_SET;
188 while (fcntl (fd, F_SETLK, &lck) == -1) {
191 dprint (1, (debugfile, "mx_lock_file(): fcntl errno %d.\n", errno));
192 if (errno != EAGAIN && errno != EACCES) {
193 mutt_perror ("fcntl");
197 if (fstat (fd, &sb) != 0)
203 /* only unlock file if it is unchanged */
204 if (prev_sb.st_size == sb.st_size
205 && ++count >= (timeout ? MAXLOCKATTEMPT : 0)) {
207 mutt_error _("Timeout exceeded while attempting fcntl lock!");
214 mutt_message (_("Waiting for fcntl lock... %d"), ++attempt);
217 #endif /* USE_FCNTL */
222 while (flock (fd, (excl ? LOCK_EX : LOCK_SH) | LOCK_NB) == -1) {
225 if (errno != EWOULDBLOCK) {
226 mutt_perror ("flock");
231 if (fstat (fd, &sb) != 0)
237 /* only unlock file if it is unchanged */
238 if (prev_sb.st_size == sb.st_size
239 && ++count >= (timeout ? MAXLOCKATTEMPT : 0)) {
241 mutt_error _("Timeout exceeded while attempting flock lock!");
249 mutt_message (_("Waiting for flock attempt... %d"), ++attempt);
252 #endif /* USE_FLOCK */
256 r = dotlock_file (path, fd, timeout);
257 #endif /* USE_DOTLOCK */
260 /* release any other locks obtained in this routine */
263 lck.l_type = F_UNLCK;
264 fcntl (fd, F_SETLK, &lck);
265 #endif /* USE_FCNTL */
269 #endif /* USE_FLOCK */
277 int mx_unlock_file (const char *path, int fd, int dot)
280 struct flock unlockit = { F_UNLCK, 0, 0, 0 };
282 memset (&unlockit, 0, sizeof (struct flock));
283 unlockit.l_type = F_UNLCK;
284 unlockit.l_whence = SEEK_SET;
285 fcntl (fd, F_SETLK, &unlockit);
294 undotlock_file (path, fd);
300 void mx_unlink_empty (const char *path)
308 if ((fd = open (path, O_RDWR)) == -1)
311 if (mx_lock_file (path, fd, 1, 0, 1) == -1) {
317 invoke_dotlock (path, fd, DL_FL_UNLINK, 1);
319 if (fstat (fd, &sb) == 0 && sb.st_size == 0)
323 mx_unlock_file (path, fd, 0);
327 /* try to figure out what type of mailbox ``path'' is */
328 int mx_get_magic (const char *path) {
331 if (safe_strlen (path) == 0)
333 if ((i = mx_get_idx (path)) >= 0)
334 return (MX_COMMAND(i,type));
339 * set DefaultMagic to the given value
341 int mx_set_magic (const char *s)
343 if (ascii_strcasecmp (s, "mbox") == 0)
344 DefaultMagic = M_MBOX;
345 else if (ascii_strcasecmp (s, "mmdf") == 0)
346 DefaultMagic = M_MMDF;
347 else if (ascii_strcasecmp (s, "mh") == 0)
349 else if (ascii_strcasecmp (s, "maildir") == 0)
350 DefaultMagic = M_MAILDIR;
357 /* mx_access: Wrapper for access, checks permissions on a given mailbox.
358 * We may be interested in using ACL-style flags at some point, currently
359 * we use the normal access() flags. */
360 int mx_access (const char *path, int flags)
364 if ((i = mx_get_idx (path)) >= 0 && MX_COMMAND(i,mx_access))
365 return (MX_COMMAND(i,mx_access)(path,flags));
369 static int mx_open_mailbox_append (CONTEXT * ctx, int flags)
373 #ifdef USE_COMPRESSED
374 /* special case for appending to compressed folders -
375 * even if we can not open them for reading */
376 if (mutt_can_append_compressed (ctx->path))
377 mutt_open_append_compressed (ctx);
384 if (mx_get_magic (ctx->path) == M_IMAP)
385 return imap_open_mailbox_append (ctx);
389 if (stat (ctx->path, &sb) == 0) {
390 ctx->magic = mx_get_magic (ctx->path);
392 switch (ctx->magic) {
394 mutt_error (_("%s is not a mailbox."), ctx->path);
400 else if (errno == ENOENT) {
401 ctx->magic = DefaultMagic;
403 if (ctx->magic == M_MH || ctx->magic == M_MAILDIR) {
404 char tmp[_POSIX_PATH_MAX];
406 if (mkdir (ctx->path, S_IRWXU)) {
407 mutt_perror (ctx->path);
411 if (ctx->magic == M_MAILDIR) {
412 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
413 if (mkdir (tmp, S_IRWXU)) {
419 snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
420 if (mkdir (tmp, S_IRWXU)) {
422 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
427 snprintf (tmp, sizeof (tmp), "%s/tmp", ctx->path);
428 if (mkdir (tmp, S_IRWXU)) {
430 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
432 snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
441 snprintf (tmp, sizeof (tmp), "%s/.mh_sequences", ctx->path);
442 if ((i = creat (tmp, S_IRWXU)) == -1) {
452 mutt_perror (ctx->path);
456 switch (ctx->magic) {
460 safe_fopen (ctx->path, flags & M_NEWFOLDER ? "w" : "a")) == NULL
461 || mbox_lock_mailbox (ctx, 1, 1) != 0) {
463 mutt_perror (ctx->path);
465 mutt_error (_("Couldn't lock %s\n"), ctx->path);
466 safe_fclose (&ctx->fp);
470 fseek (ctx->fp, 0, 2);
486 * open a mailbox and parse it
489 * flags M_NOSORT do not sort mailbox
490 * M_APPEND open mailbox for appending
491 * M_READONLY open mailbox in read-only mode
492 * M_QUIET only print error messages
493 * ctx if non-null, context struct to use
495 CONTEXT *mx_open_mailbox (const char *path, int flags, CONTEXT * pctx)
501 ctx = safe_malloc (sizeof (CONTEXT));
502 memset (ctx, 0, sizeof (CONTEXT));
503 ctx->path = safe_strdup (path);
505 ctx->msgnotreadyet = -1;
510 if (flags & M_READONLY)
513 if (flags & (M_APPEND | M_NEWFOLDER)) {
514 if (mx_open_mailbox_append (ctx, flags) != 0) {
515 mx_fastclose_mailbox (ctx);
523 ctx->magic = mx_get_magic (path);
525 #ifdef USE_COMPRESSED
526 if (ctx->magic == M_COMPRESSED)
527 mutt_open_read_compressed (ctx);
531 mutt_error (_("%s is not a mailbox."), path);
533 if (ctx->magic == -1)
536 if (ctx->magic <= 0) {
537 mx_fastclose_mailbox (ctx);
543 /* if the user has a `push' command in their .muttrc, or in a folder-hook,
544 * it will cause the progress messages not to be displayed because
545 * mutt_refresh() will think we are in the middle of a macro. so set a
546 * flag to indicate that we should really refresh the screen.
548 set_option (OPTFORCEREFRESH);
551 mutt_message (_("Reading %s..."), ctx->path);
553 rc = MX_COMMAND(ctx->magic-1,mx_open_mailbox)(ctx);
556 if ((flags & M_NOSORT) == 0) {
557 /* avoid unnecessary work since the mailbox is completely unthreaded
559 unset_option (OPTSORTSUBTHREADS);
560 unset_option (OPTNEEDRESCORE);
561 mutt_sort_headers (ctx, 1);
567 mx_fastclose_mailbox (ctx);
572 unset_option (OPTFORCEREFRESH);
576 /* free up memory associated with the mailbox context */
577 void mx_fastclose_mailbox (CONTEXT * ctx)
585 if (ctx->magic == M_IMAP)
586 imap_close_mailbox (ctx);
587 #endif /* USE_IMAP */
589 if (ctx->magic == M_POP)
590 pop_close_mailbox (ctx);
593 if (ctx->magic == M_NNTP)
594 nntp_fastclose_mailbox (ctx);
595 #endif /* USE_NNTP */
597 hash_destroy (&ctx->subj_hash, NULL);
599 hash_destroy (&ctx->id_hash, NULL);
600 mutt_clear_threads (ctx);
601 for (i = 0; i < ctx->msgcount; i++)
602 mutt_free_header (&ctx->hdrs[i]);
605 #ifdef USE_COMPRESSED
606 if (ctx->compressinfo)
607 mutt_fast_close_compressed (ctx);
610 FREE (&ctx->pattern);
611 if (ctx->limit_pattern)
612 mutt_pattern_free (&ctx->limit_pattern);
613 safe_fclose (&ctx->fp);
614 memset (ctx, 0, sizeof (CONTEXT));
617 /* save changes to disk */
618 static int sync_mailbox (CONTEXT * ctx, int *index_hint)
626 mutt_message (_("Writing %s..."), ctx->path);
628 switch (ctx->magic) {
631 rc = mbox_sync_mailbox (ctx, index_hint);
633 tmp = mutt_find_mailbox (ctx->path);
639 rc = mh_sync_mailbox (ctx, index_hint);
644 /* extra argument means EXPUNGE */
645 rc = imap_sync_mailbox (ctx, 1, index_hint);
647 #endif /* USE_IMAP */
651 rc = pop_sync_mailbox (ctx, index_hint);
657 rc = nntp_sync_mailbox (ctx);
659 #endif /* USE_NNTP */
663 if (!ctx->quiet && !ctx->shutup && rc == -1)
664 mutt_error (_("Could not synchronize mailbox %s!"), ctx->path);
668 if (tmp && tmp->new == 0)
669 mutt_update_mailbox (tmp);
672 #ifdef USE_COMPRESSED
673 if (rc == 0 && ctx->compressinfo)
674 return mutt_sync_compressed (ctx);
680 /* move deleted mails to the trash folder */
681 static int trash_append (CONTEXT * ctx)
687 if (!TrashPath || !ctx->deleted ||
688 (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))
691 for (; i < ctx->msgcount && (!ctx->hdrs[i]->deleted ||
692 ctx->hdrs[i]->appended); i++);
693 if (i == ctx->msgcount)
694 return 0; /* nothing to be done */
696 if (mutt_save_confirm (TrashPath, &st) != 0) {
697 mutt_error _("message(s) not deleted");
702 if (lstat (ctx->path, &stc) == 0 && stc.st_ino == st.st_ino
703 && stc.st_dev == st.st_dev && stc.st_rdev == st.st_rdev)
704 return 0; /* we are in the trash folder: simple sync */
706 if ((ctx_trash = mx_open_mailbox (TrashPath, M_APPEND, NULL)) != NULL) {
707 for (i = 0; i < ctx->msgcount; i++)
708 if (ctx->hdrs[i]->deleted && !ctx->hdrs[i]->appended
709 && !ctx->hdrs[i]->purged
710 && mutt_append_message (ctx_trash, ctx, ctx->hdrs[i], 0, 0) == -1) {
711 mx_close_mailbox (ctx_trash, NULL);
715 mx_close_mailbox (ctx_trash, NULL);
718 mutt_error _("Can't open trash folder");
726 /* save changes and close mailbox */
727 int mx_close_mailbox (CONTEXT * ctx, int *index_hint)
729 int i, move_messages = 0, purge = 1, read_msgs = 0;
733 char mbox[_POSIX_PATH_MAX];
734 char buf[SHORT_STRING];
742 if (ctx->magic == M_NNTP) {
745 ret = nntp_close_mailbox (ctx);
746 mx_fastclose_mailbox (ctx);
750 if (ctx->readonly || ctx->dontwrite) {
751 /* mailbox is readonly or we don't want to write */
752 mx_fastclose_mailbox (ctx);
757 /* mailbox was opened in write-mode */
758 if (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
759 mbox_close_mailbox (ctx);
761 mx_fastclose_mailbox (ctx);
765 for (i = 0; i < ctx->msgcount; i++) {
766 if (!ctx->hdrs[i]->deleted && ctx->hdrs[i]->read
767 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
771 if (read_msgs && quadoption (OPT_MOVE) != M_NO) {
774 if ((p = mutt_find_hook (M_MBOXHOOK, ctx->path))) {
776 strfcpy (mbox, p, sizeof (mbox));
779 strfcpy (mbox, NONULL (Inbox), sizeof (mbox));
780 isSpool = mutt_is_spool (ctx->path) && !mutt_is_spool (mbox);
782 mutt_expand_path (mbox, sizeof (mbox));
785 snprintf (buf, sizeof (buf), _("Move read messages to %s?"), mbox);
786 if ((move_messages = query_quadoption (OPT_MOVE, buf)) == -1) {
794 * There is no point in asking whether or not to purge if we are
795 * just marking messages as "trash".
797 if (ctx->deleted && !(ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH))) {
798 snprintf (buf, sizeof (buf), ctx->deleted == 1
799 ? _("Purge %d deleted message?") :
800 _("Purge %d deleted messages?"), ctx->deleted);
801 if ((purge = query_quadoption (OPT_DELETE, buf)) < 0) {
808 /* IMAP servers manage the OLD flag themselves */
809 if (ctx->magic != M_IMAP)
811 if (option (OPTMARKOLD)) {
812 for (i = 0; i < ctx->msgcount; i++) {
813 if (!ctx->hdrs[i]->deleted && !ctx->hdrs[i]->old)
814 mutt_set_flag (ctx, ctx->hdrs[i], M_OLD, 1);
819 mutt_message (_("Moving read messages to %s..."), mbox);
822 /* try to use server-side copy first */
825 if (ctx->magic == M_IMAP && mx_get_magic (mbox) == M_IMAP) {
826 /* tag messages for moving, and clear old tags, if any */
827 for (i = 0; i < ctx->msgcount; i++)
828 if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted
829 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
830 ctx->hdrs[i]->tagged = 1;
832 ctx->hdrs[i]->tagged = 0;
834 i = imap_copy_messages (ctx, NULL, mbox, 1);
837 if (i == 0) /* success */
839 else if (i == -1) { /* horrible error, bail */
843 else /* use regular append-copy mode */
846 if (mx_open_mailbox (mbox, M_APPEND, &f) == NULL) {
851 for (i = 0; i < ctx->msgcount; i++) {
852 if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted
853 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED))) {
854 if (mutt_append_message (&f, ctx, ctx->hdrs[i], 0, CH_UPDATE_LEN) ==
856 mutt_set_flag (ctx, ctx->hdrs[i], M_DELETE, 1);
857 mutt_set_flag (ctx, ctx->hdrs[i], M_APPENDED, 1);
860 mx_close_mailbox (&f, NULL);
867 mx_close_mailbox (&f, NULL);
871 else if (!ctx->changed && ctx->deleted == 0) {
872 mutt_message _("Mailbox is unchanged.");
874 mx_fastclose_mailbox (ctx);
878 /* copy mails to the trash before expunging */
879 if (purge && ctx->deleted)
880 if (trash_append (ctx) != 0) {
886 /* allow IMAP to preserve the deleted flag across sessions */
887 if (ctx->magic == M_IMAP) {
888 if ((check = imap_sync_mailbox (ctx, purge, index_hint)) != 0) {
897 for (i = 0; i < ctx->msgcount; i++)
898 ctx->hdrs[i]->deleted = 0;
902 if (ctx->changed || ctx->deleted) {
903 if ((check = sync_mailbox (ctx, index_hint)) != 0) {
911 mutt_message (_("%d kept, %d moved, %d deleted."),
912 ctx->msgcount - ctx->deleted, read_msgs, ctx->deleted);
914 mutt_message (_("%d kept, %d deleted."),
915 ctx->msgcount - ctx->deleted, ctx->deleted);
917 if (ctx->msgcount == ctx->deleted &&
918 (ctx->magic == M_MMDF || ctx->magic == M_MBOX) &&
919 !mutt_is_spool (ctx->path) && !option (OPTSAVEEMPTY))
920 mx_unlink_empty (ctx->path);
922 #ifdef USE_COMPRESSED
923 if (ctx->compressinfo && mutt_slow_close_compressed (ctx))
927 mx_fastclose_mailbox (ctx);
933 /* update a Context structure's internal tables. */
935 void mx_update_tables (CONTEXT * ctx, int committing)
939 /* update memory to reflect the new state of the mailbox */
948 #define this_body ctx->hdrs[j]->content
949 for (i = 0, j = 0; i < ctx->msgcount; i++) {
950 if ((committing && (!ctx->hdrs[i]->deleted ||
951 (ctx->magic == M_MAILDIR
952 && option (OPTMAILDIRTRASH)))) || (!committing
956 ctx->hdrs[j] = ctx->hdrs[i];
959 ctx->hdrs[j]->msgno = j;
960 if (ctx->hdrs[j]->virtual != -1) {
961 ctx->v2r[ctx->vcount] = j;
962 ctx->hdrs[j]->virtual = ctx->vcount++;
963 ctx->vsize += this_body->length + this_body->offset -
964 this_body->hdr_offset;
968 ctx->hdrs[j]->changed = 0;
969 else if (ctx->hdrs[j]->changed)
973 || (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH))) {
974 if (ctx->hdrs[j]->deleted)
978 if (ctx->hdrs[j]->tagged)
980 if (ctx->hdrs[j]->flagged)
982 if (!ctx->hdrs[j]->read) {
984 if (!ctx->hdrs[j]->old)
991 if (ctx->magic == M_MH || ctx->magic == M_MAILDIR)
992 ctx->size -= (ctx->hdrs[i]->content->length +
993 ctx->hdrs[i]->content->offset -
994 ctx->hdrs[i]->content->hdr_offset);
995 /* remove message from the hash tables */
996 if (ctx->subj_hash && ctx->hdrs[i]->env->real_subj)
997 hash_delete (ctx->subj_hash, ctx->hdrs[i]->env->real_subj,
999 if (ctx->id_hash && ctx->hdrs[i]->env->message_id)
1000 hash_delete (ctx->id_hash, ctx->hdrs[i]->env->message_id,
1001 ctx->hdrs[i], NULL);
1002 mutt_free_header (&ctx->hdrs[i]);
1010 /* save changes to mailbox
1016 int mx_sync_mailbox (CONTEXT * ctx, int *index_hint)
1020 int msgcount, deleted;
1022 if (ctx->dontwrite) {
1023 char buf[STRING], tmp[STRING];
1025 if (km_expand_key (buf, sizeof (buf),
1026 km_find_func (MENU_MAIN, OP_TOGGLE_WRITE)))
1027 snprintf (tmp, sizeof (tmp), _(" Press '%s' to toggle write"), buf);
1029 strfcpy (tmp, _("Use 'toggle-write' to re-enable write!"),
1032 mutt_error (_("Mailbox is marked unwritable. %s"), tmp);
1035 else if (ctx->readonly) {
1036 mutt_error _("Mailbox is read-only.");
1041 if (!ctx->changed && !ctx->deleted) {
1042 mutt_message _("Mailbox is unchanged.");
1048 char buf[SHORT_STRING];
1050 snprintf (buf, sizeof (buf), ctx->deleted == 1
1051 ? _("Purge %d deleted message?") :
1052 _("Purge %d deleted messages?"), ctx->deleted);
1053 if ((purge = query_quadoption (OPT_DELETE, buf)) < 0)
1055 else if (purge == M_NO) {
1057 return 0; /* nothing to do! */
1059 /* let IMAP servers hold on to D flags */
1060 if (ctx->magic != M_IMAP)
1063 for (i = 0; i < ctx->msgcount; i++)
1064 ctx->hdrs[i]->deleted = 0;
1068 else if (ctx->last_tag && ctx->last_tag->deleted)
1069 ctx->last_tag = NULL; /* reset last tagged msg now useless */
1072 /* really only for IMAP - imap_sync_mailbox results in a call to
1073 * mx_update_tables, so ctx->deleted is 0 when it comes back */
1074 msgcount = ctx->msgcount;
1075 deleted = ctx->deleted;
1077 if (purge && ctx->deleted) {
1078 if (trash_append (ctx) == -1)
1083 if (ctx->magic == M_IMAP)
1084 rc = imap_sync_mailbox (ctx, purge, index_hint);
1087 rc = sync_mailbox (ctx, index_hint);
1090 if (ctx->magic == M_IMAP && !purge)
1091 mutt_message (_("Mailbox checkpointed."));
1095 mutt_message (_("%d kept, %d deleted."), msgcount - deleted, deleted);
1099 if (ctx->msgcount == ctx->deleted &&
1100 (ctx->magic == M_MBOX || ctx->magic == M_MMDF) &&
1101 !mutt_is_spool (ctx->path) && !option (OPTSAVEEMPTY)) {
1103 mx_fastclose_mailbox (ctx);
1107 /* if we haven't deleted any messages, we don't need to resort */
1108 /* ... except for certain folder formats which need "unsorted"
1109 * sort order in order to synchronize folders.
1111 * MH and maildir are safe. mbox-style seems to need re-sorting,
1112 * at least with the new threading code.
1114 if (purge || (ctx->magic != M_MAILDIR && ctx->magic != M_MH)) {
1116 /* IMAP does this automatically after handling EXPUNGE */
1117 if (ctx->magic != M_IMAP)
1120 mx_update_tables (ctx, 1);
1121 mutt_sort_headers (ctx, 1); /* rethread from scratch */
1130 int imap_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr)
1132 char tmp[_POSIX_PATH_MAX];
1135 if ((msg->fp = safe_fopen (tmp, "w")) == NULL)
1137 msg->path = safe_strdup (tmp);
1143 * dest destintation mailbox
1144 * hdr message being copied (required for maildir support, because
1145 * the filename depends on the message flags)
1147 MESSAGE *mx_open_new_message (CONTEXT * dest, HEADER * hdr, int flags)
1150 int (*func) (MESSAGE *, CONTEXT *, HEADER *);
1153 switch (dest->magic) {
1156 func = mbox_open_new_message;
1159 func = maildir_open_new_message;
1162 func = mh_open_new_message;
1166 func = imap_open_new_message;
1172 "mx_open_new_message(): function unimplemented for mailbox type %d.\n",
1177 msg = safe_calloc (1, sizeof (MESSAGE));
1178 msg->magic = dest->magic;
1182 msg->flags.flagged = hdr->flagged;
1183 msg->flags.replied = hdr->replied;
1184 msg->flags.read = hdr->read;
1185 msg->received = hdr->received;
1188 if (msg->received == 0)
1189 time (&msg->received);
1191 if (func (msg, dest, hdr) == 0) {
1192 if (dest->magic == M_MMDF)
1193 fputs (MMDF_SEP, msg->fp);
1195 if ((msg->magic == M_MBOX || msg->magic == M_MMDF) && flags & M_ADD_FROM) {
1197 if (hdr->env->return_path)
1198 p = hdr->env->return_path;
1199 else if (hdr->env->sender)
1200 p = hdr->env->sender;
1205 fprintf (msg->fp, "From %s %s", p ? p->mailbox : NONULL (Username),
1206 ctime (&msg->received));
1215 /* check for new mail */
1216 int mx_check_mailbox (CONTEXT * ctx, int *index_hint, int lock)
1220 #ifdef USE_COMPRESSED
1221 if (ctx->compressinfo)
1222 return mutt_check_mailbox_compressed (ctx);
1229 switch (ctx->magic) {
1234 mutt_block_signals ();
1235 if (mbox_lock_mailbox (ctx, 0, 0) == -1) {
1236 mutt_unblock_signals ();
1241 rc = mbox_check_mailbox (ctx, index_hint);
1244 mutt_unblock_signals ();
1245 mbox_unlock_mailbox (ctx);
1252 return (mh_check_mailbox (ctx, index_hint));
1254 return (maildir_check_mailbox (ctx, index_hint));
1258 return (imap_check_mailbox (ctx, index_hint, 0));
1259 #endif /* USE_IMAP */
1263 return (pop_check_mailbox (ctx, index_hint));
1264 #endif /* USE_POP */
1268 return (nntp_check_mailbox (ctx));
1269 #endif /* USE_NNTP */
1273 dprint (1, (debugfile, "mx_check_mailbox: null or invalid context.\n"));
1277 /* return a stream pointer for a message */
1278 MESSAGE *mx_open_message (CONTEXT * ctx, int msgno)
1282 msg = safe_calloc (1, sizeof (MESSAGE));
1283 switch (msg->magic = ctx->magic) {
1292 HEADER *cur = ctx->hdrs[msgno];
1293 char path[_POSIX_PATH_MAX];
1295 snprintf (path, sizeof (path), "%s/%s", ctx->path, cur->path);
1297 if ((msg->fp = fopen (path, "r")) == NULL && errno == ENOENT &&
1298 ctx->magic == M_MAILDIR)
1299 msg->fp = maildir_open_find_message (ctx->path, cur->path);
1301 if (msg->fp == NULL) {
1303 dprint (1, (debugfile, "mx_open_message: fopen: %s: %s (errno %d).\n",
1304 path, strerror (errno), errno));
1313 if (imap_fetch_message (msg, ctx, msgno) != 0)
1317 #endif /* USE_IMAP */
1322 if (pop_fetch_message (msg, ctx, msgno) != 0)
1326 #endif /* USE_POP */
1331 if (nntp_fetch_message (msg, ctx, msgno) != 0)
1335 #endif /* USE_NNTP */
1340 "mx_open_message(): function not implemented for mailbox type %d.\n",
1348 /* commit a message to a folder */
1350 int mx_commit_message (MESSAGE * msg, CONTEXT * ctx)
1354 if (!(msg->write && ctx->append)) {
1357 "mx_commit_message(): msg->write = %d, ctx->append = %d\n",
1358 msg->write, ctx->append));
1362 switch (msg->magic) {
1365 if (fputs (MMDF_SEP, msg->fp) == EOF)
1372 if (fputc ('\n', msg->fp) == EOF)
1380 if ((r = safe_fclose (&msg->fp)) == 0)
1381 r = imap_append_message (ctx, msg);
1388 r = maildir_commit_message (ctx, msg, NULL);
1394 r = mh_commit_message (ctx, msg, NULL);
1399 if (r == 0 && (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
1400 && (fflush (msg->fp) == EOF || fsync (fileno (msg->fp)) == -1)) {
1401 mutt_perror (_("Can't write message"));
1409 /* close a pointer to a message */
1410 int mx_close_message (MESSAGE ** msg)
1414 if ((*msg)->magic == M_MH || (*msg)->magic == M_MAILDIR
1416 || (*msg)->magic == M_IMAP
1419 || (*msg)->magic == M_POP
1422 || (*msg)->magic == M_NNTP
1425 r = safe_fclose (&(*msg)->fp);
1431 dprint (1, (debugfile, "mx_close_message (): unlinking %s\n",
1433 unlink ((*msg)->path);
1434 FREE (&(*msg)->path);
1441 void mx_alloc_memory (CONTEXT * ctx)
1444 size_t s = MAX (sizeof (HEADER *), sizeof (int));
1446 if ((ctx->hdrmax + 25) * s < ctx->hdrmax * s) {
1447 mutt_error _("Integer overflow -- can't allocate memory.");
1454 safe_realloc (&ctx->hdrs, sizeof (HEADER *) * (ctx->hdrmax += 25));
1455 safe_realloc (&ctx->v2r, sizeof (int) * ctx->hdrmax);
1458 ctx->hdrs = safe_calloc ((ctx->hdrmax += 25), sizeof (HEADER *));
1459 ctx->v2r = safe_calloc (ctx->hdrmax, sizeof (int));
1461 for (i = ctx->msgcount; i < ctx->hdrmax; i++) {
1462 ctx->hdrs[i] = NULL;
1467 /* this routine is called to update the counts in the context structure for
1468 * the last message header parsed.
1470 void mx_update_context (CONTEXT * ctx, int new_messages)
1475 for (msgno = ctx->msgcount - new_messages; msgno < ctx->msgcount; msgno++) {
1476 h = ctx->hdrs[msgno];
1479 /* NOTE: this _must_ be done before the check for mailcap! */
1480 h->security = crypt_query (h->content);
1483 if (!ctx->pattern) {
1484 ctx->v2r[ctx->vcount] = msgno;
1485 h->virtual = ctx->vcount++;
1491 if (h->env->supersedes) {
1495 ctx->id_hash = mutt_make_id_hash (ctx);
1497 h2 = hash_find (ctx->id_hash, h->env->supersedes);
1499 /* FREE (&h->env->supersedes); should I ? */
1502 if (option (OPTSCORE))
1503 mutt_score_message (ctx, h2, 1);
1507 /* add this message to the hash tables */
1508 if (ctx->id_hash && h->env->message_id)
1509 hash_insert (ctx->id_hash, h->env->message_id, h, 0);
1510 if (ctx->subj_hash && h->env->real_subj)
1511 hash_insert (ctx->subj_hash, h->env->real_subj, h, 1);
1513 if (option (OPTSCORE))
1514 mutt_score_message (ctx, h, 0);
1532 * 1 if the specified mailbox contains 0 messages.
1533 * 0 if the mailbox contains messages
1536 int mx_check_empty (const char *path)
1539 if ((i = mx_get_idx (path)) >= 0 && MX_COMMAND(i,mx_check_empty))
1540 return (MX_COMMAND(i,mx_check_empty)(path));
1545 void mx_init (void) {
1549 list_push_back (&MailboxFormats, (void*) mbox_reg_mx ());
1550 list_push_back (&MailboxFormats, (void*) mmdf_reg_mx ());
1551 list_push_back (&MailboxFormats, (void*) mh_reg_mx ());
1552 list_push_back (&MailboxFormats, (void*) maildir_reg_mx ());
1554 list_push_back (&MailboxFormats, (void*) imap_reg_mx ());
1557 list_push_back (&MailboxFormats, (void*) pop_reg_mx ());
1560 list_push_back (&MailboxFormats, (void*) nntp_reg_mx ());
1562 #ifdef USE_COMPRESSED
1563 list_push_back (&MailboxFormats, (void*) compress_reg_mx ());
1566 /* check module registration for completeness with debug versions */
1567 #define EXITWITHERR(m) do { fprintf(stderr, "error: incomplete mx module: %s is missing for type %i\n",m,i);exit(1); } while (0)
1568 for (i = 0; i < MailboxFormats->length; i++) {
1569 if (MX_COMMAND(i,type) < 1) EXITWITHERR("type");
1570 if (!MX_COMMAND(i,mx_is_magic)) EXITWITHERR("mx_is_magic");
1571 if (!MX_COMMAND(i,mx_open_mailbox)) EXITWITHERR("mx_open_mailbox");