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 if ((rc = mx_get_idx (ctx->path)) >= 0)
554 rc = MX_COMMAND(rc,mx_open_mailbox)(ctx);
557 if ((flags & M_NOSORT) == 0) {
558 /* avoid unnecessary work since the mailbox is completely unthreaded
560 unset_option (OPTSORTSUBTHREADS);
561 unset_option (OPTNEEDRESCORE);
562 mutt_sort_headers (ctx, 1);
568 mx_fastclose_mailbox (ctx);
573 unset_option (OPTFORCEREFRESH);
577 /* free up memory associated with the mailbox context */
578 void mx_fastclose_mailbox (CONTEXT * ctx)
586 if (ctx->magic == M_IMAP)
587 imap_close_mailbox (ctx);
588 #endif /* USE_IMAP */
590 if (ctx->magic == M_POP)
591 pop_close_mailbox (ctx);
594 if (ctx->magic == M_NNTP)
595 nntp_fastclose_mailbox (ctx);
596 #endif /* USE_NNTP */
598 hash_destroy (&ctx->subj_hash, NULL);
600 hash_destroy (&ctx->id_hash, NULL);
601 mutt_clear_threads (ctx);
602 for (i = 0; i < ctx->msgcount; i++)
603 mutt_free_header (&ctx->hdrs[i]);
606 #ifdef USE_COMPRESSED
607 if (ctx->compressinfo)
608 mutt_fast_close_compressed (ctx);
611 FREE (&ctx->pattern);
612 if (ctx->limit_pattern)
613 mutt_pattern_free (&ctx->limit_pattern);
614 safe_fclose (&ctx->fp);
615 memset (ctx, 0, sizeof (CONTEXT));
618 /* save changes to disk */
619 static int sync_mailbox (CONTEXT * ctx, int *index_hint)
627 mutt_message (_("Writing %s..."), ctx->path);
629 switch (ctx->magic) {
632 rc = mbox_sync_mailbox (ctx, index_hint);
634 tmp = mutt_find_mailbox (ctx->path);
640 rc = mh_sync_mailbox (ctx, index_hint);
645 /* extra argument means EXPUNGE */
646 rc = imap_sync_mailbox (ctx, 1, index_hint);
648 #endif /* USE_IMAP */
652 rc = pop_sync_mailbox (ctx, index_hint);
658 rc = nntp_sync_mailbox (ctx);
660 #endif /* USE_NNTP */
664 if (!ctx->quiet && !ctx->shutup && rc == -1)
665 mutt_error (_("Could not synchronize mailbox %s!"), ctx->path);
669 if (tmp && tmp->new == 0)
670 mutt_update_mailbox (tmp);
673 #ifdef USE_COMPRESSED
674 if (rc == 0 && ctx->compressinfo)
675 return mutt_sync_compressed (ctx);
681 /* move deleted mails to the trash folder */
682 static int trash_append (CONTEXT * ctx)
688 if (!TrashPath || !ctx->deleted ||
689 (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))
692 for (; i < ctx->msgcount && (!ctx->hdrs[i]->deleted ||
693 ctx->hdrs[i]->appended); i++);
694 if (i == ctx->msgcount)
695 return 0; /* nothing to be done */
697 if (mutt_save_confirm (TrashPath, &st) != 0) {
698 mutt_error _("message(s) not deleted");
703 if (lstat (ctx->path, &stc) == 0 && stc.st_ino == st.st_ino
704 && stc.st_dev == st.st_dev && stc.st_rdev == st.st_rdev)
705 return 0; /* we are in the trash folder: simple sync */
707 if ((ctx_trash = mx_open_mailbox (TrashPath, M_APPEND, NULL)) != NULL) {
708 for (i = 0; i < ctx->msgcount; i++)
709 if (ctx->hdrs[i]->deleted && !ctx->hdrs[i]->appended
710 && !ctx->hdrs[i]->purged
711 && mutt_append_message (ctx_trash, ctx, ctx->hdrs[i], 0, 0) == -1) {
712 mx_close_mailbox (ctx_trash, NULL);
716 mx_close_mailbox (ctx_trash, NULL);
719 mutt_error _("Can't open trash folder");
727 /* save changes and close mailbox */
728 int mx_close_mailbox (CONTEXT * ctx, int *index_hint)
730 int i, move_messages = 0, purge = 1, read_msgs = 0;
734 char mbox[_POSIX_PATH_MAX];
735 char buf[SHORT_STRING];
743 if (ctx->magic == M_NNTP) {
746 ret = nntp_close_mailbox (ctx);
747 mx_fastclose_mailbox (ctx);
751 if (ctx->readonly || ctx->dontwrite) {
752 /* mailbox is readonly or we don't want to write */
753 mx_fastclose_mailbox (ctx);
758 /* mailbox was opened in write-mode */
759 if (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
760 mbox_close_mailbox (ctx);
762 mx_fastclose_mailbox (ctx);
766 for (i = 0; i < ctx->msgcount; i++) {
767 if (!ctx->hdrs[i]->deleted && ctx->hdrs[i]->read
768 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
772 if (read_msgs && quadoption (OPT_MOVE) != M_NO) {
775 if ((p = mutt_find_hook (M_MBOXHOOK, ctx->path))) {
777 strfcpy (mbox, p, sizeof (mbox));
780 strfcpy (mbox, NONULL (Inbox), sizeof (mbox));
781 isSpool = mutt_is_spool (ctx->path) && !mutt_is_spool (mbox);
783 mutt_expand_path (mbox, sizeof (mbox));
786 snprintf (buf, sizeof (buf), _("Move read messages to %s?"), mbox);
787 if ((move_messages = query_quadoption (OPT_MOVE, buf)) == -1) {
795 * There is no point in asking whether or not to purge if we are
796 * just marking messages as "trash".
798 if (ctx->deleted && !(ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH))) {
799 snprintf (buf, sizeof (buf), ctx->deleted == 1
800 ? _("Purge %d deleted message?") :
801 _("Purge %d deleted messages?"), ctx->deleted);
802 if ((purge = query_quadoption (OPT_DELETE, buf)) < 0) {
809 /* IMAP servers manage the OLD flag themselves */
810 if (ctx->magic != M_IMAP)
812 if (option (OPTMARKOLD)) {
813 for (i = 0; i < ctx->msgcount; i++) {
814 if (!ctx->hdrs[i]->deleted && !ctx->hdrs[i]->old)
815 mutt_set_flag (ctx, ctx->hdrs[i], M_OLD, 1);
820 mutt_message (_("Moving read messages to %s..."), mbox);
823 /* try to use server-side copy first */
826 if (ctx->magic == M_IMAP && mx_get_magic (mbox) == M_IMAP) {
827 /* tag messages for moving, and clear old tags, if any */
828 for (i = 0; i < ctx->msgcount; i++)
829 if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted
830 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
831 ctx->hdrs[i]->tagged = 1;
833 ctx->hdrs[i]->tagged = 0;
835 i = imap_copy_messages (ctx, NULL, mbox, 1);
838 if (i == 0) /* success */
840 else if (i == -1) { /* horrible error, bail */
844 else /* use regular append-copy mode */
847 if (mx_open_mailbox (mbox, M_APPEND, &f) == NULL) {
852 for (i = 0; i < ctx->msgcount; i++) {
853 if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted
854 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED))) {
855 if (mutt_append_message (&f, ctx, ctx->hdrs[i], 0, CH_UPDATE_LEN) ==
857 mutt_set_flag (ctx, ctx->hdrs[i], M_DELETE, 1);
858 mutt_set_flag (ctx, ctx->hdrs[i], M_APPENDED, 1);
861 mx_close_mailbox (&f, NULL);
868 mx_close_mailbox (&f, NULL);
872 else if (!ctx->changed && ctx->deleted == 0) {
873 mutt_message _("Mailbox is unchanged.");
875 mx_fastclose_mailbox (ctx);
879 /* copy mails to the trash before expunging */
880 if (purge && ctx->deleted)
881 if (trash_append (ctx) != 0) {
887 /* allow IMAP to preserve the deleted flag across sessions */
888 if (ctx->magic == M_IMAP) {
889 if ((check = imap_sync_mailbox (ctx, purge, index_hint)) != 0) {
898 for (i = 0; i < ctx->msgcount; i++)
899 ctx->hdrs[i]->deleted = 0;
903 if (ctx->changed || ctx->deleted) {
904 if ((check = sync_mailbox (ctx, index_hint)) != 0) {
912 mutt_message (_("%d kept, %d moved, %d deleted."),
913 ctx->msgcount - ctx->deleted, read_msgs, ctx->deleted);
915 mutt_message (_("%d kept, %d deleted."),
916 ctx->msgcount - ctx->deleted, ctx->deleted);
918 if (ctx->msgcount == ctx->deleted &&
919 (ctx->magic == M_MMDF || ctx->magic == M_MBOX) &&
920 !mutt_is_spool (ctx->path) && !option (OPTSAVEEMPTY))
921 mx_unlink_empty (ctx->path);
923 #ifdef USE_COMPRESSED
924 if (ctx->compressinfo && mutt_slow_close_compressed (ctx))
928 mx_fastclose_mailbox (ctx);
934 /* update a Context structure's internal tables. */
936 void mx_update_tables (CONTEXT * ctx, int committing)
940 /* update memory to reflect the new state of the mailbox */
949 #define this_body ctx->hdrs[j]->content
950 for (i = 0, j = 0; i < ctx->msgcount; i++) {
951 if ((committing && (!ctx->hdrs[i]->deleted ||
952 (ctx->magic == M_MAILDIR
953 && option (OPTMAILDIRTRASH)))) || (!committing
957 ctx->hdrs[j] = ctx->hdrs[i];
960 ctx->hdrs[j]->msgno = j;
961 if (ctx->hdrs[j]->virtual != -1) {
962 ctx->v2r[ctx->vcount] = j;
963 ctx->hdrs[j]->virtual = ctx->vcount++;
964 ctx->vsize += this_body->length + this_body->offset -
965 this_body->hdr_offset;
969 ctx->hdrs[j]->changed = 0;
970 else if (ctx->hdrs[j]->changed)
974 || (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH))) {
975 if (ctx->hdrs[j]->deleted)
979 if (ctx->hdrs[j]->tagged)
981 if (ctx->hdrs[j]->flagged)
983 if (!ctx->hdrs[j]->read) {
985 if (!ctx->hdrs[j]->old)
992 if (ctx->magic == M_MH || ctx->magic == M_MAILDIR)
993 ctx->size -= (ctx->hdrs[i]->content->length +
994 ctx->hdrs[i]->content->offset -
995 ctx->hdrs[i]->content->hdr_offset);
996 /* remove message from the hash tables */
997 if (ctx->subj_hash && ctx->hdrs[i]->env->real_subj)
998 hash_delete (ctx->subj_hash, ctx->hdrs[i]->env->real_subj,
1000 if (ctx->id_hash && ctx->hdrs[i]->env->message_id)
1001 hash_delete (ctx->id_hash, ctx->hdrs[i]->env->message_id,
1002 ctx->hdrs[i], NULL);
1003 mutt_free_header (&ctx->hdrs[i]);
1011 /* save changes to mailbox
1017 int mx_sync_mailbox (CONTEXT * ctx, int *index_hint)
1021 int msgcount, deleted;
1023 if (ctx->dontwrite) {
1024 char buf[STRING], tmp[STRING];
1026 if (km_expand_key (buf, sizeof (buf),
1027 km_find_func (MENU_MAIN, OP_TOGGLE_WRITE)))
1028 snprintf (tmp, sizeof (tmp), _(" Press '%s' to toggle write"), buf);
1030 strfcpy (tmp, _("Use 'toggle-write' to re-enable write!"),
1033 mutt_error (_("Mailbox is marked unwritable. %s"), tmp);
1036 else if (ctx->readonly) {
1037 mutt_error _("Mailbox is read-only.");
1042 if (!ctx->changed && !ctx->deleted) {
1043 mutt_message _("Mailbox is unchanged.");
1049 char buf[SHORT_STRING];
1051 snprintf (buf, sizeof (buf), ctx->deleted == 1
1052 ? _("Purge %d deleted message?") :
1053 _("Purge %d deleted messages?"), ctx->deleted);
1054 if ((purge = query_quadoption (OPT_DELETE, buf)) < 0)
1056 else if (purge == M_NO) {
1058 return 0; /* nothing to do! */
1060 /* let IMAP servers hold on to D flags */
1061 if (ctx->magic != M_IMAP)
1064 for (i = 0; i < ctx->msgcount; i++)
1065 ctx->hdrs[i]->deleted = 0;
1069 else if (ctx->last_tag && ctx->last_tag->deleted)
1070 ctx->last_tag = NULL; /* reset last tagged msg now useless */
1073 /* really only for IMAP - imap_sync_mailbox results in a call to
1074 * mx_update_tables, so ctx->deleted is 0 when it comes back */
1075 msgcount = ctx->msgcount;
1076 deleted = ctx->deleted;
1078 if (purge && ctx->deleted) {
1079 if (trash_append (ctx) == -1)
1084 if (ctx->magic == M_IMAP)
1085 rc = imap_sync_mailbox (ctx, purge, index_hint);
1088 rc = sync_mailbox (ctx, index_hint);
1091 if (ctx->magic == M_IMAP && !purge)
1092 mutt_message (_("Mailbox checkpointed."));
1096 mutt_message (_("%d kept, %d deleted."), msgcount - deleted, deleted);
1100 if (ctx->msgcount == ctx->deleted &&
1101 (ctx->magic == M_MBOX || ctx->magic == M_MMDF) &&
1102 !mutt_is_spool (ctx->path) && !option (OPTSAVEEMPTY)) {
1104 mx_fastclose_mailbox (ctx);
1108 /* if we haven't deleted any messages, we don't need to resort */
1109 /* ... except for certain folder formats which need "unsorted"
1110 * sort order in order to synchronize folders.
1112 * MH and maildir are safe. mbox-style seems to need re-sorting,
1113 * at least with the new threading code.
1115 if (purge || (ctx->magic != M_MAILDIR && ctx->magic != M_MH)) {
1117 /* IMAP does this automatically after handling EXPUNGE */
1118 if (ctx->magic != M_IMAP)
1121 mx_update_tables (ctx, 1);
1122 mutt_sort_headers (ctx, 1); /* rethread from scratch */
1131 int imap_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr)
1133 char tmp[_POSIX_PATH_MAX];
1136 if ((msg->fp = safe_fopen (tmp, "w")) == NULL)
1138 msg->path = safe_strdup (tmp);
1144 * dest destintation mailbox
1145 * hdr message being copied (required for maildir support, because
1146 * the filename depends on the message flags)
1148 MESSAGE *mx_open_new_message (CONTEXT * dest, HEADER * hdr, int flags)
1151 int (*func) (MESSAGE *, CONTEXT *, HEADER *);
1154 switch (dest->magic) {
1157 func = mbox_open_new_message;
1160 func = maildir_open_new_message;
1163 func = mh_open_new_message;
1167 func = imap_open_new_message;
1173 "mx_open_new_message(): function unimplemented for mailbox type %d.\n",
1178 msg = safe_calloc (1, sizeof (MESSAGE));
1179 msg->magic = dest->magic;
1183 msg->flags.flagged = hdr->flagged;
1184 msg->flags.replied = hdr->replied;
1185 msg->flags.read = hdr->read;
1186 msg->received = hdr->received;
1189 if (msg->received == 0)
1190 time (&msg->received);
1192 if (func (msg, dest, hdr) == 0) {
1193 if (dest->magic == M_MMDF)
1194 fputs (MMDF_SEP, msg->fp);
1196 if ((msg->magic == M_MBOX || msg->magic == M_MMDF) && flags & M_ADD_FROM) {
1198 if (hdr->env->return_path)
1199 p = hdr->env->return_path;
1200 else if (hdr->env->sender)
1201 p = hdr->env->sender;
1206 fprintf (msg->fp, "From %s %s", p ? p->mailbox : NONULL (Username),
1207 ctime (&msg->received));
1216 /* check for new mail */
1217 int mx_check_mailbox (CONTEXT * ctx, int *index_hint, int lock)
1221 #ifdef USE_COMPRESSED
1222 if (ctx->compressinfo)
1223 return mutt_check_mailbox_compressed (ctx);
1230 switch (ctx->magic) {
1235 mutt_block_signals ();
1236 if (mbox_lock_mailbox (ctx, 0, 0) == -1) {
1237 mutt_unblock_signals ();
1242 rc = mbox_check_mailbox (ctx, index_hint);
1245 mutt_unblock_signals ();
1246 mbox_unlock_mailbox (ctx);
1253 return (mh_check_mailbox (ctx, index_hint));
1255 return (maildir_check_mailbox (ctx, index_hint));
1259 return (imap_check_mailbox (ctx, index_hint, 0));
1260 #endif /* USE_IMAP */
1264 return (pop_check_mailbox (ctx, index_hint));
1265 #endif /* USE_POP */
1269 return (nntp_check_mailbox (ctx));
1270 #endif /* USE_NNTP */
1274 dprint (1, (debugfile, "mx_check_mailbox: null or invalid context.\n"));
1278 /* return a stream pointer for a message */
1279 MESSAGE *mx_open_message (CONTEXT * ctx, int msgno)
1283 msg = safe_calloc (1, sizeof (MESSAGE));
1284 switch (msg->magic = ctx->magic) {
1293 HEADER *cur = ctx->hdrs[msgno];
1294 char path[_POSIX_PATH_MAX];
1296 snprintf (path, sizeof (path), "%s/%s", ctx->path, cur->path);
1298 if ((msg->fp = fopen (path, "r")) == NULL && errno == ENOENT &&
1299 ctx->magic == M_MAILDIR)
1300 msg->fp = maildir_open_find_message (ctx->path, cur->path);
1302 if (msg->fp == NULL) {
1304 dprint (1, (debugfile, "mx_open_message: fopen: %s: %s (errno %d).\n",
1305 path, strerror (errno), errno));
1314 if (imap_fetch_message (msg, ctx, msgno) != 0)
1318 #endif /* USE_IMAP */
1323 if (pop_fetch_message (msg, ctx, msgno) != 0)
1327 #endif /* USE_POP */
1332 if (nntp_fetch_message (msg, ctx, msgno) != 0)
1336 #endif /* USE_NNTP */
1341 "mx_open_message(): function not implemented for mailbox type %d.\n",
1349 /* commit a message to a folder */
1351 int mx_commit_message (MESSAGE * msg, CONTEXT * ctx)
1355 if (!(msg->write && ctx->append)) {
1358 "mx_commit_message(): msg->write = %d, ctx->append = %d\n",
1359 msg->write, ctx->append));
1363 switch (msg->magic) {
1366 if (fputs (MMDF_SEP, msg->fp) == EOF)
1373 if (fputc ('\n', msg->fp) == EOF)
1381 if ((r = safe_fclose (&msg->fp)) == 0)
1382 r = imap_append_message (ctx, msg);
1389 r = maildir_commit_message (ctx, msg, NULL);
1395 r = mh_commit_message (ctx, msg, NULL);
1400 if (r == 0 && (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
1401 && (fflush (msg->fp) == EOF || fsync (fileno (msg->fp)) == -1)) {
1402 mutt_perror (_("Can't write message"));
1410 /* close a pointer to a message */
1411 int mx_close_message (MESSAGE ** msg)
1415 if ((*msg)->magic == M_MH || (*msg)->magic == M_MAILDIR
1417 || (*msg)->magic == M_IMAP
1420 || (*msg)->magic == M_POP
1423 || (*msg)->magic == M_NNTP
1426 r = safe_fclose (&(*msg)->fp);
1432 dprint (1, (debugfile, "mx_close_message (): unlinking %s\n",
1434 unlink ((*msg)->path);
1435 FREE (&(*msg)->path);
1442 void mx_alloc_memory (CONTEXT * ctx)
1445 size_t s = MAX (sizeof (HEADER *), sizeof (int));
1447 if ((ctx->hdrmax + 25) * s < ctx->hdrmax * s) {
1448 mutt_error _("Integer overflow -- can't allocate memory.");
1455 safe_realloc (&ctx->hdrs, sizeof (HEADER *) * (ctx->hdrmax += 25));
1456 safe_realloc (&ctx->v2r, sizeof (int) * ctx->hdrmax);
1459 ctx->hdrs = safe_calloc ((ctx->hdrmax += 25), sizeof (HEADER *));
1460 ctx->v2r = safe_calloc (ctx->hdrmax, sizeof (int));
1462 for (i = ctx->msgcount; i < ctx->hdrmax; i++) {
1463 ctx->hdrs[i] = NULL;
1468 /* this routine is called to update the counts in the context structure for
1469 * the last message header parsed.
1471 void mx_update_context (CONTEXT * ctx, int new_messages)
1476 for (msgno = ctx->msgcount - new_messages; msgno < ctx->msgcount; msgno++) {
1477 h = ctx->hdrs[msgno];
1480 /* NOTE: this _must_ be done before the check for mailcap! */
1481 h->security = crypt_query (h->content);
1484 if (!ctx->pattern) {
1485 ctx->v2r[ctx->vcount] = msgno;
1486 h->virtual = ctx->vcount++;
1492 if (h->env->supersedes) {
1496 ctx->id_hash = mutt_make_id_hash (ctx);
1498 h2 = hash_find (ctx->id_hash, h->env->supersedes);
1500 /* FREE (&h->env->supersedes); should I ? */
1503 if (option (OPTSCORE))
1504 mutt_score_message (ctx, h2, 1);
1508 /* add this message to the hash tables */
1509 if (ctx->id_hash && h->env->message_id)
1510 hash_insert (ctx->id_hash, h->env->message_id, h, 0);
1511 if (ctx->subj_hash && h->env->real_subj)
1512 hash_insert (ctx->subj_hash, h->env->real_subj, h, 1);
1514 if (option (OPTSCORE))
1515 mutt_score_message (ctx, h, 0);
1533 * 1 if the specified mailbox contains 0 messages.
1534 * 0 if the mailbox contains messages
1537 int mx_check_empty (const char *path)
1540 if ((i = mx_get_idx (path)) >= 0 && MX_COMMAND(i,mx_check_empty))
1541 return (MX_COMMAND(i,mx_check_empty)(path));
1546 void mx_init (void) {
1550 list_push_back (&MailboxFormats, (void*) mbox_reg_mx ());
1551 list_push_back (&MailboxFormats, (void*) mmdf_reg_mx ());
1552 list_push_back (&MailboxFormats, (void*) mh_reg_mx ());
1553 list_push_back (&MailboxFormats, (void*) maildir_reg_mx ());
1555 list_push_back (&MailboxFormats, (void*) imap_reg_mx ());
1558 list_push_back (&MailboxFormats, (void*) pop_reg_mx ());
1561 list_push_back (&MailboxFormats, (void*) nntp_reg_mx ());
1563 #ifdef USE_COMPRESSED
1564 list_push_back (&MailboxFormats, (void*) compress_reg_mx ());
1567 /* check module registration for completeness with debug versions */
1568 #define EXITWITHERR(m) do { fprintf(stderr, "error: incomplete mx module: %s is missing for type %i\n",m,i);exit(1); } while (0)
1569 for (i = 0; i < MailboxFormats->length; i++) {
1570 if (MX_COMMAND(i,type) < 1) EXITWITHERR("type");
1571 if (!MX_COMMAND(i,mx_is_magic)) EXITWITHERR("mx_is_magic");
1572 if (!MX_COMMAND(i,mx_open_mailbox)) EXITWITHERR("mx_open_mailbox");