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.
48 #include "mutt_crypt.h"
64 #define mutt_is_spool(s) (mutt_strcmp (Spoolfile, s) == 0)
69 * retry - should retry if unable to lock?
74 static int invoke_dotlock (const char *path, int dummy, int flags, int retry)
76 char cmd[LONG_STRING + _POSIX_PATH_MAX];
77 char f[SHORT_STRING + _POSIX_PATH_MAX];
80 if (flags & DL_FL_RETRY)
81 snprintf (r, sizeof (r), "-r %d ", retry ? MAXLOCKATTEMPT : 0);
83 mutt_quote_filename (f, sizeof (f), path);
85 snprintf (cmd, sizeof (cmd),
88 flags & DL_FL_TRY ? "-t " : "",
89 flags & DL_FL_UNLOCK ? "-u " : "",
90 flags & DL_FL_USEPRIV ? "-p " : "",
91 flags & DL_FL_FORCE ? "-f " : "",
92 flags & DL_FL_UNLINK ? "-d " : "",
93 flags & DL_FL_RETRY ? r : "", f);
95 return mutt_system (cmd);
100 #define invoke_dotlock dotlock_invoke
104 static int dotlock_file (const char *path, int fd, int retry)
107 int flags = DL_FL_USEPRIV | DL_FL_RETRY;
113 if ((r = invoke_dotlock (path, fd, flags, retry)) == DL_EX_EXIST) {
114 if (!option (OPTNOCURSES)) {
115 char msg[LONG_STRING];
117 snprintf (msg, sizeof (msg),
118 _("Lock count exceeded, remove lock for %s?"), path);
119 if (retry && mutt_yesorno (msg, M_YES) == M_YES) {
120 flags |= DL_FL_FORCE;
127 mutt_error (_("Can't dotlock %s.\n"), path);
130 return (r == DL_EX_OK ? 0 : -1);
133 static int undotlock_file (const char *path, int fd)
135 return (invoke_dotlock (path, fd, DL_FL_USEPRIV | DL_FL_UNLOCK, 0) ==
139 #endif /* USE_DOTLOCK */
142 * excl if excl != 0, request an exclusive lock
143 * dot if dot != 0, try to dotlock the file
144 * timeout should retry locking?
146 int mx_lock_file (const char *path, int fd, int excl, int dot, int timeout)
148 #if defined (USE_FCNTL) || defined (USE_FLOCK)
159 memset (&lck, 0, sizeof (struct flock));
160 lck.l_type = excl ? F_WRLCK : F_RDLCK;
161 lck.l_whence = SEEK_SET;
165 while (fcntl (fd, F_SETLK, &lck) == -1) {
168 dprint (1, (debugfile, "mx_lock_file(): fcntl errno %d.\n", errno));
169 if (errno != EAGAIN && errno != EACCES) {
170 mutt_perror ("fcntl");
174 if (fstat (fd, &sb) != 0)
180 /* only unlock file if it is unchanged */
181 if (prev_sb.st_size == sb.st_size
182 && ++count >= (timeout ? MAXLOCKATTEMPT : 0)) {
184 mutt_error _("Timeout exceeded while attempting fcntl lock!");
191 mutt_message (_("Waiting for fcntl lock... %d"), ++attempt);
194 #endif /* USE_FCNTL */
199 while (flock (fd, (excl ? LOCK_EX : LOCK_SH) | LOCK_NB) == -1) {
202 if (errno != EWOULDBLOCK) {
203 mutt_perror ("flock");
208 if (fstat (fd, &sb) != 0)
214 /* only unlock file if it is unchanged */
215 if (prev_sb.st_size == sb.st_size
216 && ++count >= (timeout ? MAXLOCKATTEMPT : 0)) {
218 mutt_error _("Timeout exceeded while attempting flock lock!");
226 mutt_message (_("Waiting for flock attempt... %d"), ++attempt);
229 #endif /* USE_FLOCK */
233 r = dotlock_file (path, fd, timeout);
234 #endif /* USE_DOTLOCK */
237 /* release any other locks obtained in this routine */
240 lck.l_type = F_UNLCK;
241 fcntl (fd, F_SETLK, &lck);
242 #endif /* USE_FCNTL */
246 #endif /* USE_FLOCK */
254 int mx_unlock_file (const char *path, int fd, int dot)
257 struct flock unlockit = { F_UNLCK, 0, 0, 0 };
259 memset (&unlockit, 0, sizeof (struct flock));
260 unlockit.l_type = F_UNLCK;
261 unlockit.l_whence = SEEK_SET;
262 fcntl (fd, F_SETLK, &unlockit);
271 undotlock_file (path, fd);
277 void mx_unlink_empty (const char *path)
285 if ((fd = open (path, O_RDWR)) == -1)
288 if (mx_lock_file (path, fd, 1, 0, 1) == -1) {
294 invoke_dotlock (path, fd, DL_FL_UNLINK, 1);
296 if (fstat (fd, &sb) == 0 && sb.st_size == 0)
300 mx_unlock_file (path, fd, 0);
304 /* try to figure out what type of mailbox ``path'' is
314 int mx_is_imap (const char *p)
324 scheme = url_check_scheme (p);
325 if (scheme == U_IMAP || scheme == U_IMAPS)
334 int mx_is_pop (const char *p)
341 scheme = url_check_scheme (p);
342 if (scheme == U_POP || scheme == U_POPS)
350 int mx_is_nntp (const char *p)
357 scheme = url_check_scheme (p);
358 if (scheme == U_NNTP || scheme == U_NNTPS)
365 int mx_get_magic (const char *path)
369 char tmp[_POSIX_PATH_MAX];
373 if (mx_is_imap (path))
375 #endif /* USE_IMAP */
378 if (mx_is_pop (path))
383 if (mx_is_nntp (path))
385 #endif /* USE_NNTP */
387 if (stat (path, &st) == -1) {
389 (debugfile, "mx_get_magic(): unable to stat %s: %s (errno %d).\n",
390 path, strerror (errno), errno));
394 if (S_ISDIR (st.st_mode)) {
395 /* check for maildir-style mailbox */
397 snprintf (tmp, sizeof (tmp), "%s/cur", path);
398 if (stat (tmp, &st) == 0 && S_ISDIR (st.st_mode))
401 /* check for mh-style mailbox */
403 snprintf (tmp, sizeof (tmp), "%s/.mh_sequences", path);
404 if (access (tmp, F_OK) == 0)
407 snprintf (tmp, sizeof (tmp), "%s/.xmhcache", path);
408 if (access (tmp, F_OK) == 0)
411 snprintf (tmp, sizeof (tmp), "%s/.mew_cache", path);
412 if (access (tmp, F_OK) == 0)
415 snprintf (tmp, sizeof (tmp), "%s/.mew-cache", path);
416 if (access (tmp, F_OK) == 0)
419 snprintf (tmp, sizeof (tmp), "%s/.sylpheed_cache", path);
420 if (access (tmp, F_OK) == 0)
424 * ok, this isn't an mh folder, but mh mode can be used to read
425 * Usenet news from the spool. ;-)
428 snprintf (tmp, sizeof (tmp), "%s/.overview", path);
429 if (access (tmp, F_OK) == 0)
433 else if (st.st_size == 0) {
434 /* hard to tell what zero-length files are, so assume the default magic */
435 if (DefaultMagic == M_MBOX || DefaultMagic == M_MMDF)
436 return (DefaultMagic);
440 else if ((f = fopen (path, "r")) != NULL) {
442 struct utimbuf times;
445 fgets (tmp, sizeof (tmp), f);
446 if (mutt_strncmp ("From ", tmp, 5) == 0)
448 else if (mutt_strcmp (MMDF_SEP, tmp) == 0)
452 /* need to restore the times here, the file was not really accessed,
453 * only the type was accessed. This is important, because detection
454 * of "new mail" depends on those times set correctly.
456 times.actime = st.st_atime;
457 times.modtime = st.st_mtime;
458 utime (path, ×);
464 "mx_get_magic(): unable to open file %s for reading.\n", path));
469 #ifdef USE_COMPRESSED
470 if (magic == 0 && mutt_can_read_compressed (path))
478 * set DefaultMagic to the given value
480 int mx_set_magic (const char *s)
482 if (ascii_strcasecmp (s, "mbox") == 0)
483 DefaultMagic = M_MBOX;
484 else if (ascii_strcasecmp (s, "mmdf") == 0)
485 DefaultMagic = M_MMDF;
486 else if (ascii_strcasecmp (s, "mh") == 0)
488 else if (ascii_strcasecmp (s, "maildir") == 0)
489 DefaultMagic = M_MAILDIR;
496 /* mx_access: Wrapper for access, checks permissions on a given mailbox.
497 * We may be interested in using ACL-style flags at some point, currently
498 * we use the normal access() flags. */
499 int mx_access (const char *path, int flags)
502 if (mx_is_imap (path))
503 return imap_access (path, flags);
506 return access (path, flags);
509 static int mx_open_mailbox_append (CONTEXT * ctx, int flags)
513 #ifdef USE_COMPRESSED
514 /* special case for appending to compressed folders -
515 * even if we can not open them for reading */
516 if (mutt_can_append_compressed (ctx->path))
517 mutt_open_append_compressed (ctx);
524 if (mx_is_imap (ctx->path))
525 return imap_open_mailbox_append (ctx);
529 if (stat (ctx->path, &sb) == 0) {
530 ctx->magic = mx_get_magic (ctx->path);
532 switch (ctx->magic) {
534 mutt_error (_("%s is not a mailbox."), ctx->path);
540 else if (errno == ENOENT) {
541 ctx->magic = DefaultMagic;
543 if (ctx->magic == M_MH || ctx->magic == M_MAILDIR) {
544 char tmp[_POSIX_PATH_MAX];
546 if (mkdir (ctx->path, S_IRWXU)) {
547 mutt_perror (ctx->path);
551 if (ctx->magic == M_MAILDIR) {
552 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
553 if (mkdir (tmp, S_IRWXU)) {
559 snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
560 if (mkdir (tmp, S_IRWXU)) {
562 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
567 snprintf (tmp, sizeof (tmp), "%s/tmp", ctx->path);
568 if (mkdir (tmp, S_IRWXU)) {
570 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
572 snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
581 snprintf (tmp, sizeof (tmp), "%s/.mh_sequences", ctx->path);
582 if ((i = creat (tmp, S_IRWXU)) == -1) {
592 mutt_perror (ctx->path);
596 switch (ctx->magic) {
600 safe_fopen (ctx->path, flags & M_NEWFOLDER ? "w" : "a")) == NULL
601 || mbox_lock_mailbox (ctx, 1, 1) != 0) {
603 mutt_perror (ctx->path);
605 mutt_error (_("Couldn't lock %s\n"), ctx->path);
606 safe_fclose (&ctx->fp);
610 fseek (ctx->fp, 0, 2);
626 * open a mailbox and parse it
629 * flags M_NOSORT do not sort mailbox
630 * M_APPEND open mailbox for appending
631 * M_READONLY open mailbox in read-only mode
632 * M_QUIET only print error messages
633 * ctx if non-null, context struct to use
635 CONTEXT *mx_open_mailbox (const char *path, int flags, CONTEXT * pctx)
641 ctx = safe_malloc (sizeof (CONTEXT));
642 memset (ctx, 0, sizeof (CONTEXT));
643 ctx->path = safe_strdup (path);
645 ctx->msgnotreadyet = -1;
650 if (flags & M_READONLY)
653 if (flags & (M_APPEND | M_NEWFOLDER)) {
654 if (mx_open_mailbox_append (ctx, flags) != 0) {
655 mx_fastclose_mailbox (ctx);
663 ctx->magic = mx_get_magic (path);
665 #ifdef USE_COMPRESSED
666 if (ctx->magic == M_COMPRESSED)
667 mutt_open_read_compressed (ctx);
671 mutt_error (_("%s is not a mailbox."), path);
673 if (ctx->magic == -1)
676 if (ctx->magic <= 0) {
677 mx_fastclose_mailbox (ctx);
683 /* if the user has a `push' command in their .muttrc, or in a folder-hook,
684 * it will cause the progress messages not to be displayed because
685 * mutt_refresh() will think we are in the middle of a macro. so set a
686 * flag to indicate that we should really refresh the screen.
688 set_option (OPTFORCEREFRESH);
691 mutt_message (_("Reading %s..."), ctx->path);
693 switch (ctx->magic) {
695 rc = mh_read_dir (ctx, NULL);
699 rc = maildir_read_dir (ctx);
704 rc = mbox_open_mailbox (ctx);
709 rc = imap_open_mailbox (ctx);
711 #endif /* USE_IMAP */
715 rc = pop_open_mailbox (ctx);
721 rc = nntp_open_mailbox (ctx);
723 #endif /* USE_NNTP */
731 if ((flags & M_NOSORT) == 0) {
732 /* avoid unnecessary work since the mailbox is completely unthreaded
734 unset_option (OPTSORTSUBTHREADS);
735 unset_option (OPTNEEDRESCORE);
736 mutt_sort_headers (ctx, 1);
742 mx_fastclose_mailbox (ctx);
747 unset_option (OPTFORCEREFRESH);
751 /* free up memory associated with the mailbox context */
752 void mx_fastclose_mailbox (CONTEXT * ctx)
760 if (ctx->magic == M_IMAP)
761 imap_close_mailbox (ctx);
762 #endif /* USE_IMAP */
764 if (ctx->magic == M_POP)
765 pop_close_mailbox (ctx);
768 if (ctx->magic == M_NNTP)
769 nntp_fastclose_mailbox (ctx);
770 #endif /* USE_NNTP */
772 hash_destroy (&ctx->subj_hash, NULL);
774 hash_destroy (&ctx->id_hash, NULL);
775 mutt_clear_threads (ctx);
776 for (i = 0; i < ctx->msgcount; i++)
777 mutt_free_header (&ctx->hdrs[i]);
780 #ifdef USE_COMPRESSED
781 if (ctx->compressinfo)
782 mutt_fast_close_compressed (ctx);
785 FREE (&ctx->pattern);
786 if (ctx->limit_pattern)
787 mutt_pattern_free (&ctx->limit_pattern);
788 safe_fclose (&ctx->fp);
789 memset (ctx, 0, sizeof (CONTEXT));
792 /* save changes to disk */
793 static int sync_mailbox (CONTEXT * ctx, int *index_hint)
801 mutt_message (_("Writing %s..."), ctx->path);
803 switch (ctx->magic) {
806 rc = mbox_sync_mailbox (ctx, index_hint);
808 tmp = mutt_find_mailbox (ctx->path);
814 rc = mh_sync_mailbox (ctx, index_hint);
819 /* extra argument means EXPUNGE */
820 rc = imap_sync_mailbox (ctx, 1, index_hint);
822 #endif /* USE_IMAP */
826 rc = pop_sync_mailbox (ctx, index_hint);
832 rc = nntp_sync_mailbox (ctx);
834 #endif /* USE_NNTP */
838 if (!ctx->quiet && !ctx->shutup && rc == -1)
839 mutt_error (_("Could not synchronize mailbox %s!"), ctx->path);
843 if (tmp && tmp->new == 0)
844 mutt_update_mailbox (tmp);
847 #ifdef USE_COMPRESSED
848 if (rc == 0 && ctx->compressinfo)
849 return mutt_sync_compressed (ctx);
855 /* move deleted mails to the trash folder */
856 static int trash_append (CONTEXT * ctx)
862 if (!TrashPath || !ctx->deleted ||
863 (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))
866 for (; i < ctx->msgcount && (!ctx->hdrs[i]->deleted ||
867 ctx->hdrs[i]->appended); i++);
868 if (i == ctx->msgcount)
869 return 0; /* nothing to be done */
871 if (mutt_save_confirm (TrashPath, &st) != 0) {
872 mutt_error _("message(s) not deleted");
877 if (lstat (ctx->path, &stc) == 0 && stc.st_ino == st.st_ino
878 && stc.st_dev == st.st_dev && stc.st_rdev == st.st_rdev)
879 return 0; /* we are in the trash folder: simple sync */
881 if ((ctx_trash = mx_open_mailbox (TrashPath, M_APPEND, NULL)) != NULL) {
882 for (i = 0; i < ctx->msgcount; i++)
883 if (ctx->hdrs[i]->deleted && !ctx->hdrs[i]->appended
884 && !ctx->hdrs[i]->purged
885 && mutt_append_message (ctx_trash, ctx, ctx->hdrs[i], 0, 0) == -1) {
886 mx_close_mailbox (ctx_trash, NULL);
890 mx_close_mailbox (ctx_trash, NULL);
893 mutt_error _("Can't open trash folder");
901 /* save changes and close mailbox */
902 int mx_close_mailbox (CONTEXT * ctx, int *index_hint)
904 int i, move_messages = 0, purge = 1, read_msgs = 0;
908 char mbox[_POSIX_PATH_MAX];
909 char buf[SHORT_STRING];
917 if (ctx->magic == M_NNTP) {
920 ret = nntp_close_mailbox (ctx);
921 mx_fastclose_mailbox (ctx);
925 if (ctx->readonly || ctx->dontwrite) {
926 /* mailbox is readonly or we don't want to write */
927 mx_fastclose_mailbox (ctx);
932 /* mailbox was opened in write-mode */
933 if (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
934 mbox_close_mailbox (ctx);
936 mx_fastclose_mailbox (ctx);
940 for (i = 0; i < ctx->msgcount; i++) {
941 if (!ctx->hdrs[i]->deleted && ctx->hdrs[i]->read
942 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
946 if (read_msgs && quadoption (OPT_MOVE) != M_NO) {
949 if ((p = mutt_find_hook (M_MBOXHOOK, ctx->path))) {
951 strfcpy (mbox, p, sizeof (mbox));
954 strfcpy (mbox, NONULL (Inbox), sizeof (mbox));
955 isSpool = mutt_is_spool (ctx->path) && !mutt_is_spool (mbox);
957 mutt_expand_path (mbox, sizeof (mbox));
960 snprintf (buf, sizeof (buf), _("Move read messages to %s?"), mbox);
961 if ((move_messages = query_quadoption (OPT_MOVE, buf)) == -1) {
969 * There is no point in asking whether or not to purge if we are
970 * just marking messages as "trash".
972 if (ctx->deleted && !(ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH))) {
973 snprintf (buf, sizeof (buf), ctx->deleted == 1
974 ? _("Purge %d deleted message?") :
975 _("Purge %d deleted messages?"), ctx->deleted);
976 if ((purge = query_quadoption (OPT_DELETE, buf)) < 0) {
983 /* IMAP servers manage the OLD flag themselves */
984 if (ctx->magic != M_IMAP)
986 if (option (OPTMARKOLD)) {
987 for (i = 0; i < ctx->msgcount; i++) {
988 if (!ctx->hdrs[i]->deleted && !ctx->hdrs[i]->old)
989 mutt_set_flag (ctx, ctx->hdrs[i], M_OLD, 1);
994 mutt_message (_("Moving read messages to %s..."), mbox);
997 /* try to use server-side copy first */
1000 if (ctx->magic == M_IMAP && mx_is_imap (mbox)) {
1001 /* tag messages for moving, and clear old tags, if any */
1002 for (i = 0; i < ctx->msgcount; i++)
1003 if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted
1004 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
1005 ctx->hdrs[i]->tagged = 1;
1007 ctx->hdrs[i]->tagged = 0;
1009 i = imap_copy_messages (ctx, NULL, mbox, 1);
1012 if (i == 0) /* success */
1013 mutt_clear_error ();
1014 else if (i == -1) { /* horrible error, bail */
1018 else /* use regular append-copy mode */
1021 if (mx_open_mailbox (mbox, M_APPEND, &f) == NULL) {
1026 for (i = 0; i < ctx->msgcount; i++) {
1027 if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted
1028 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED))) {
1029 if (mutt_append_message (&f, ctx, ctx->hdrs[i], 0, CH_UPDATE_LEN) ==
1031 mutt_set_flag (ctx, ctx->hdrs[i], M_DELETE, 1);
1032 mutt_set_flag (ctx, ctx->hdrs[i], M_APPENDED, 1);
1035 mx_close_mailbox (&f, NULL);
1042 mx_close_mailbox (&f, NULL);
1046 else if (!ctx->changed && ctx->deleted == 0) {
1047 mutt_message _("Mailbox is unchanged.");
1049 mx_fastclose_mailbox (ctx);
1053 /* copy mails to the trash before expunging */
1054 if (purge && ctx->deleted)
1055 if (trash_append (ctx) != 0) {
1061 /* allow IMAP to preserve the deleted flag across sessions */
1062 if (ctx->magic == M_IMAP) {
1063 if ((check = imap_sync_mailbox (ctx, purge, index_hint)) != 0) {
1072 for (i = 0; i < ctx->msgcount; i++)
1073 ctx->hdrs[i]->deleted = 0;
1077 if (ctx->changed || ctx->deleted) {
1078 if ((check = sync_mailbox (ctx, index_hint)) != 0) {
1086 mutt_message (_("%d kept, %d moved, %d deleted."),
1087 ctx->msgcount - ctx->deleted, read_msgs, ctx->deleted);
1089 mutt_message (_("%d kept, %d deleted."),
1090 ctx->msgcount - ctx->deleted, ctx->deleted);
1092 if (ctx->msgcount == ctx->deleted &&
1093 (ctx->magic == M_MMDF || ctx->magic == M_MBOX) &&
1094 !mutt_is_spool (ctx->path) && !option (OPTSAVEEMPTY))
1095 mx_unlink_empty (ctx->path);
1097 #ifdef USE_COMPRESSED
1098 if (ctx->compressinfo && mutt_slow_close_compressed (ctx))
1102 mx_fastclose_mailbox (ctx);
1108 /* update a Context structure's internal tables. */
1110 void mx_update_tables (CONTEXT * ctx, int committing)
1114 /* update memory to reflect the new state of the mailbox */
1123 #define this_body ctx->hdrs[j]->content
1124 for (i = 0, j = 0; i < ctx->msgcount; i++) {
1125 if ((committing && (!ctx->hdrs[i]->deleted ||
1126 (ctx->magic == M_MAILDIR
1127 && option (OPTMAILDIRTRASH)))) || (!committing
1131 ctx->hdrs[j] = ctx->hdrs[i];
1132 ctx->hdrs[i] = NULL;
1134 ctx->hdrs[j]->msgno = j;
1135 if (ctx->hdrs[j]->virtual != -1) {
1136 ctx->v2r[ctx->vcount] = j;
1137 ctx->hdrs[j]->virtual = ctx->vcount++;
1138 ctx->vsize += this_body->length + this_body->offset -
1139 this_body->hdr_offset;
1143 ctx->hdrs[j]->changed = 0;
1144 else if (ctx->hdrs[j]->changed)
1148 || (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH))) {
1149 if (ctx->hdrs[j]->deleted)
1153 if (ctx->hdrs[j]->tagged)
1155 if (ctx->hdrs[j]->flagged)
1157 if (!ctx->hdrs[j]->read) {
1159 if (!ctx->hdrs[j]->old)
1166 if (ctx->magic == M_MH || ctx->magic == M_MAILDIR)
1167 ctx->size -= (ctx->hdrs[i]->content->length +
1168 ctx->hdrs[i]->content->offset -
1169 ctx->hdrs[i]->content->hdr_offset);
1170 /* remove message from the hash tables */
1171 if (ctx->subj_hash && ctx->hdrs[i]->env->real_subj)
1172 hash_delete (ctx->subj_hash, ctx->hdrs[i]->env->real_subj,
1173 ctx->hdrs[i], NULL);
1174 if (ctx->id_hash && ctx->hdrs[i]->env->message_id)
1175 hash_delete (ctx->id_hash, ctx->hdrs[i]->env->message_id,
1176 ctx->hdrs[i], NULL);
1177 mutt_free_header (&ctx->hdrs[i]);
1185 /* save changes to mailbox
1191 int mx_sync_mailbox (CONTEXT * ctx, int *index_hint)
1195 int msgcount, deleted;
1197 if (ctx->dontwrite) {
1198 char buf[STRING], tmp[STRING];
1200 if (km_expand_key (buf, sizeof (buf),
1201 km_find_func (MENU_MAIN, OP_TOGGLE_WRITE)))
1202 snprintf (tmp, sizeof (tmp), _(" Press '%s' to toggle write"), buf);
1204 strfcpy (tmp, _("Use 'toggle-write' to re-enable write!"),
1207 mutt_error (_("Mailbox is marked unwritable. %s"), tmp);
1210 else if (ctx->readonly) {
1211 mutt_error _("Mailbox is read-only.");
1216 if (!ctx->changed && !ctx->deleted) {
1217 mutt_message _("Mailbox is unchanged.");
1223 char buf[SHORT_STRING];
1225 snprintf (buf, sizeof (buf), ctx->deleted == 1
1226 ? _("Purge %d deleted message?") :
1227 _("Purge %d deleted messages?"), ctx->deleted);
1228 if ((purge = query_quadoption (OPT_DELETE, buf)) < 0)
1230 else if (purge == M_NO) {
1232 return 0; /* nothing to do! */
1234 /* let IMAP servers hold on to D flags */
1235 if (ctx->magic != M_IMAP)
1238 for (i = 0; i < ctx->msgcount; i++)
1239 ctx->hdrs[i]->deleted = 0;
1243 else if (ctx->last_tag && ctx->last_tag->deleted)
1244 ctx->last_tag = NULL; /* reset last tagged msg now useless */
1247 /* really only for IMAP - imap_sync_mailbox results in a call to
1248 * mx_update_tables, so ctx->deleted is 0 when it comes back */
1249 msgcount = ctx->msgcount;
1250 deleted = ctx->deleted;
1252 if (purge && ctx->deleted) {
1253 if (trash_append (ctx) == -1)
1258 if (ctx->magic == M_IMAP)
1259 rc = imap_sync_mailbox (ctx, purge, index_hint);
1262 rc = sync_mailbox (ctx, index_hint);
1265 if (ctx->magic == M_IMAP && !purge)
1266 mutt_message (_("Mailbox checkpointed."));
1270 mutt_message (_("%d kept, %d deleted."), msgcount - deleted, deleted);
1274 if (ctx->msgcount == ctx->deleted &&
1275 (ctx->magic == M_MBOX || ctx->magic == M_MMDF) &&
1276 !mutt_is_spool (ctx->path) && !option (OPTSAVEEMPTY)) {
1278 mx_fastclose_mailbox (ctx);
1282 /* if we haven't deleted any messages, we don't need to resort */
1283 /* ... except for certain folder formats which need "unsorted"
1284 * sort order in order to synchronize folders.
1286 * MH and maildir are safe. mbox-style seems to need re-sorting,
1287 * at least with the new threading code.
1289 if (purge || (ctx->magic != M_MAILDIR && ctx->magic != M_MH)) {
1291 /* IMAP does this automatically after handling EXPUNGE */
1292 if (ctx->magic != M_IMAP)
1295 mx_update_tables (ctx, 1);
1296 mutt_sort_headers (ctx, 1); /* rethread from scratch */
1305 /* {maildir,mh}_open_new_message are in mh.c. */
1307 int mbox_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr)
1314 int imap_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr)
1316 char tmp[_POSIX_PATH_MAX];
1319 if ((msg->fp = safe_fopen (tmp, "w")) == NULL)
1321 msg->path = safe_strdup (tmp);
1327 * dest destintation mailbox
1328 * hdr message being copied (required for maildir support, because
1329 * the filename depends on the message flags)
1331 MESSAGE *mx_open_new_message (CONTEXT * dest, HEADER * hdr, int flags)
1334 int (*func) (MESSAGE *, CONTEXT *, HEADER *);
1337 switch (dest->magic) {
1340 func = mbox_open_new_message;
1343 func = maildir_open_new_message;
1346 func = mh_open_new_message;
1350 func = imap_open_new_message;
1356 "mx_open_new_message(): function unimplemented for mailbox type %d.\n",
1361 msg = safe_calloc (1, sizeof (MESSAGE));
1362 msg->magic = dest->magic;
1366 msg->flags.flagged = hdr->flagged;
1367 msg->flags.replied = hdr->replied;
1368 msg->flags.read = hdr->read;
1369 msg->received = hdr->received;
1372 if (msg->received == 0)
1373 time (&msg->received);
1375 if (func (msg, dest, hdr) == 0) {
1376 if (dest->magic == M_MMDF)
1377 fputs (MMDF_SEP, msg->fp);
1379 if ((msg->magic == M_MBOX || msg->magic == M_MMDF) && flags & M_ADD_FROM) {
1381 if (hdr->env->return_path)
1382 p = hdr->env->return_path;
1383 else if (hdr->env->sender)
1384 p = hdr->env->sender;
1389 fprintf (msg->fp, "From %s %s", p ? p->mailbox : NONULL (Username),
1390 ctime (&msg->received));
1399 /* check for new mail */
1400 int mx_check_mailbox (CONTEXT * ctx, int *index_hint, int lock)
1404 #ifdef USE_COMPRESSED
1405 if (ctx->compressinfo)
1406 return mutt_check_mailbox_compressed (ctx);
1413 switch (ctx->magic) {
1418 mutt_block_signals ();
1419 if (mbox_lock_mailbox (ctx, 0, 0) == -1) {
1420 mutt_unblock_signals ();
1425 rc = mbox_check_mailbox (ctx, index_hint);
1428 mutt_unblock_signals ();
1429 mbox_unlock_mailbox (ctx);
1436 return (mh_check_mailbox (ctx, index_hint));
1438 return (maildir_check_mailbox (ctx, index_hint));
1442 return (imap_check_mailbox (ctx, index_hint, 0));
1443 #endif /* USE_IMAP */
1447 return (pop_check_mailbox (ctx, index_hint));
1448 #endif /* USE_POP */
1452 return (nntp_check_mailbox (ctx));
1453 #endif /* USE_NNTP */
1457 dprint (1, (debugfile, "mx_check_mailbox: null or invalid context.\n"));
1461 /* return a stream pointer for a message */
1462 MESSAGE *mx_open_message (CONTEXT * ctx, int msgno)
1466 msg = safe_calloc (1, sizeof (MESSAGE));
1467 switch (msg->magic = ctx->magic) {
1476 HEADER *cur = ctx->hdrs[msgno];
1477 char path[_POSIX_PATH_MAX];
1479 snprintf (path, sizeof (path), "%s/%s", ctx->path, cur->path);
1481 if ((msg->fp = fopen (path, "r")) == NULL && errno == ENOENT &&
1482 ctx->magic == M_MAILDIR)
1483 msg->fp = maildir_open_find_message (ctx->path, cur->path);
1485 if (msg->fp == NULL) {
1487 dprint (1, (debugfile, "mx_open_message: fopen: %s: %s (errno %d).\n",
1488 path, strerror (errno), errno));
1497 if (imap_fetch_message (msg, ctx, msgno) != 0)
1501 #endif /* USE_IMAP */
1506 if (pop_fetch_message (msg, ctx, msgno) != 0)
1510 #endif /* USE_POP */
1515 if (nntp_fetch_message (msg, ctx, msgno) != 0)
1519 #endif /* USE_NNTP */
1524 "mx_open_message(): function not implemented for mailbox type %d.\n",
1532 /* commit a message to a folder */
1534 int mx_commit_message (MESSAGE * msg, CONTEXT * ctx)
1538 if (!(msg->write && ctx->append)) {
1541 "mx_commit_message(): msg->write = %d, ctx->append = %d\n",
1542 msg->write, ctx->append));
1546 switch (msg->magic) {
1549 if (fputs (MMDF_SEP, msg->fp) == EOF)
1556 if (fputc ('\n', msg->fp) == EOF)
1564 if ((r = safe_fclose (&msg->fp)) == 0)
1565 r = imap_append_message (ctx, msg);
1572 r = maildir_commit_message (ctx, msg, NULL);
1578 r = mh_commit_message (ctx, msg, NULL);
1583 if (r == 0 && (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
1584 && (fflush (msg->fp) == EOF || fsync (fileno (msg->fp)) == -1)) {
1585 mutt_perror _("Can't write message");
1593 /* close a pointer to a message */
1594 int mx_close_message (MESSAGE ** msg)
1598 if ((*msg)->magic == M_MH || (*msg)->magic == M_MAILDIR
1600 || (*msg)->magic == M_IMAP
1603 || (*msg)->magic == M_POP
1606 || (*msg)->magic == M_NNTP
1609 r = safe_fclose (&(*msg)->fp);
1615 dprint (1, (debugfile, "mx_close_message (): unlinking %s\n",
1617 unlink ((*msg)->path);
1618 FREE (&(*msg)->path);
1625 void mx_alloc_memory (CONTEXT * ctx)
1628 size_t s = MAX (sizeof (HEADER *), sizeof (int));
1630 if ((ctx->hdrmax + 25) * s < ctx->hdrmax * s) {
1631 mutt_error _("Integer overflow -- can't allocate memory.");
1638 safe_realloc (&ctx->hdrs, sizeof (HEADER *) * (ctx->hdrmax += 25));
1639 safe_realloc (&ctx->v2r, sizeof (int) * ctx->hdrmax);
1642 ctx->hdrs = safe_calloc ((ctx->hdrmax += 25), sizeof (HEADER *));
1643 ctx->v2r = safe_calloc (ctx->hdrmax, sizeof (int));
1645 for (i = ctx->msgcount; i < ctx->hdrmax; i++) {
1646 ctx->hdrs[i] = NULL;
1651 /* this routine is called to update the counts in the context structure for
1652 * the last message header parsed.
1654 void mx_update_context (CONTEXT * ctx, int new_messages)
1659 for (msgno = ctx->msgcount - new_messages; msgno < ctx->msgcount; msgno++) {
1660 h = ctx->hdrs[msgno];
1663 /* NOTE: this _must_ be done before the check for mailcap! */
1664 h->security = crypt_query (h->content);
1667 if (!ctx->pattern) {
1668 ctx->v2r[ctx->vcount] = msgno;
1669 h->virtual = ctx->vcount++;
1675 if (h->env->supersedes) {
1679 ctx->id_hash = mutt_make_id_hash (ctx);
1681 h2 = hash_find (ctx->id_hash, h->env->supersedes);
1683 /* FREE (&h->env->supersedes); should I ? */
1686 if (option (OPTSCORE))
1687 mutt_score_message (ctx, h2, 1);
1691 /* add this message to the hash tables */
1692 if (ctx->id_hash && h->env->message_id)
1693 hash_insert (ctx->id_hash, h->env->message_id, h, 0);
1694 if (ctx->subj_hash && h->env->real_subj)
1695 hash_insert (ctx->subj_hash, h->env->real_subj, h, 1);
1697 if (option (OPTSCORE))
1698 mutt_score_message (ctx, h, 0);
1716 * 1 if the specified mailbox contains 0 messages.
1717 * 0 if the mailbox contains messages
1720 int mx_check_empty (const char *path)
1722 switch (mx_get_magic (path)) {
1725 return mbox_check_empty (path);
1727 return mh_check_empty (path);
1729 return maildir_check_empty (path);