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.
49 #include "mutt_crypt.h"
69 #define mutt_is_spool(s) (safe_strcmp (Spoolfile, s) == 0)
74 * retry - should retry if unable to lock?
79 static int invoke_dotlock (const char *path, int dummy, int flags, int retry)
81 char cmd[LONG_STRING + _POSIX_PATH_MAX];
82 char f[SHORT_STRING + _POSIX_PATH_MAX];
85 if (flags & DL_FL_RETRY)
86 snprintf (r, sizeof (r), "-r %d ", retry ? MAXLOCKATTEMPT : 0);
88 mutt_quote_filename (f, sizeof (f), path);
90 snprintf (cmd, sizeof (cmd),
93 flags & DL_FL_TRY ? "-t " : "",
94 flags & DL_FL_UNLOCK ? "-u " : "",
95 flags & DL_FL_USEPRIV ? "-p " : "",
96 flags & DL_FL_FORCE ? "-f " : "",
97 flags & DL_FL_UNLINK ? "-d " : "",
98 flags & DL_FL_RETRY ? r : "", f);
100 return mutt_system (cmd);
105 #define invoke_dotlock dotlock_invoke
109 static int dotlock_file (const char *path, int fd, int retry)
112 int flags = DL_FL_USEPRIV | DL_FL_RETRY;
118 if ((r = invoke_dotlock (path, fd, flags, retry)) == DL_EX_EXIST) {
119 if (!option (OPTNOCURSES)) {
120 char msg[LONG_STRING];
122 snprintf (msg, sizeof (msg),
123 _("Lock count exceeded, remove lock for %s?"), path);
124 if (retry && mutt_yesorno (msg, M_YES) == M_YES) {
125 flags |= DL_FL_FORCE;
132 mutt_error (_("Can't dotlock %s.\n"), path);
135 return (r == DL_EX_OK ? 0 : -1);
138 static int undotlock_file (const char *path, int fd)
140 return (invoke_dotlock (path, fd, DL_FL_USEPRIV | DL_FL_UNLOCK, 0) ==
144 #endif /* USE_DOTLOCK */
147 * excl if excl != 0, request an exclusive lock
148 * dot if dot != 0, try to dotlock the file
149 * timeout should retry locking?
151 int mx_lock_file (const char *path, int fd, int excl, int dot, int timeout)
153 #if defined (USE_FCNTL) || defined (USE_FLOCK)
164 memset (&lck, 0, sizeof (struct flock));
165 lck.l_type = excl ? F_WRLCK : F_RDLCK;
166 lck.l_whence = SEEK_SET;
170 while (fcntl (fd, F_SETLK, &lck) == -1) {
173 dprint (1, (debugfile, "mx_lock_file(): fcntl errno %d.\n", errno));
174 if (errno != EAGAIN && errno != EACCES) {
175 mutt_perror ("fcntl");
179 if (fstat (fd, &sb) != 0)
185 /* only unlock file if it is unchanged */
186 if (prev_sb.st_size == sb.st_size
187 && ++count >= (timeout ? MAXLOCKATTEMPT : 0)) {
189 mutt_error _("Timeout exceeded while attempting fcntl lock!");
196 mutt_message (_("Waiting for fcntl lock... %d"), ++attempt);
199 #endif /* USE_FCNTL */
204 while (flock (fd, (excl ? LOCK_EX : LOCK_SH) | LOCK_NB) == -1) {
207 if (errno != EWOULDBLOCK) {
208 mutt_perror ("flock");
213 if (fstat (fd, &sb) != 0)
219 /* only unlock file if it is unchanged */
220 if (prev_sb.st_size == sb.st_size
221 && ++count >= (timeout ? MAXLOCKATTEMPT : 0)) {
223 mutt_error _("Timeout exceeded while attempting flock lock!");
231 mutt_message (_("Waiting for flock attempt... %d"), ++attempt);
234 #endif /* USE_FLOCK */
238 r = dotlock_file (path, fd, timeout);
239 #endif /* USE_DOTLOCK */
242 /* release any other locks obtained in this routine */
245 lck.l_type = F_UNLCK;
246 fcntl (fd, F_SETLK, &lck);
247 #endif /* USE_FCNTL */
251 #endif /* USE_FLOCK */
259 int mx_unlock_file (const char *path, int fd, int dot)
262 struct flock unlockit = { F_UNLCK, 0, 0, 0 };
264 memset (&unlockit, 0, sizeof (struct flock));
265 unlockit.l_type = F_UNLCK;
266 unlockit.l_whence = SEEK_SET;
267 fcntl (fd, F_SETLK, &unlockit);
276 undotlock_file (path, fd);
282 void mx_unlink_empty (const char *path)
290 if ((fd = open (path, O_RDWR)) == -1)
293 if (mx_lock_file (path, fd, 1, 0, 1) == -1) {
299 invoke_dotlock (path, fd, DL_FL_UNLINK, 1);
301 if (fstat (fd, &sb) == 0 && sb.st_size == 0)
305 mx_unlock_file (path, fd, 0);
309 /* try to figure out what type of mailbox ``path'' is
318 int mx_is_imap (const char *p)
328 scheme = url_check_scheme (p);
329 if (scheme == U_IMAP || scheme == U_IMAPS)
337 int mx_is_pop (const char *p)
344 scheme = url_check_scheme (p);
345 if (scheme == U_POP || scheme == U_POPS)
353 int mx_is_nntp (const char *p)
360 scheme = url_check_scheme (p);
361 if (scheme == U_NNTP || scheme == U_NNTPS)
368 int mx_get_magic (const char *path)
372 char tmp[_POSIX_PATH_MAX];
376 if (mx_is_imap (path))
378 #endif /* USE_IMAP */
381 if (mx_is_pop (path))
386 if (mx_is_nntp (path))
388 #endif /* USE_NNTP */
390 if (stat (path, &st) == -1) {
392 (debugfile, "mx_get_magic(): unable to stat %s: %s (errno %d).\n",
393 path, strerror (errno), errno));
397 if (S_ISDIR (st.st_mode)) {
398 /* check for maildir-style mailbox */
400 snprintf (tmp, sizeof (tmp), "%s/cur", path);
401 if (stat (tmp, &st) == 0 && S_ISDIR (st.st_mode))
404 /* check for mh-style mailbox */
406 snprintf (tmp, sizeof (tmp), "%s/.mh_sequences", path);
407 if (access (tmp, F_OK) == 0)
410 snprintf (tmp, sizeof (tmp), "%s/.xmhcache", path);
411 if (access (tmp, F_OK) == 0)
414 snprintf (tmp, sizeof (tmp), "%s/.mew_cache", path);
415 if (access (tmp, F_OK) == 0)
418 snprintf (tmp, sizeof (tmp), "%s/.mew-cache", path);
419 if (access (tmp, F_OK) == 0)
422 snprintf (tmp, sizeof (tmp), "%s/.sylpheed_cache", path);
423 if (access (tmp, F_OK) == 0)
427 * ok, this isn't an mh folder, but mh mode can be used to read
428 * Usenet news from the spool. ;-)
431 snprintf (tmp, sizeof (tmp), "%s/.overview", path);
432 if (access (tmp, F_OK) == 0)
436 else if (st.st_size == 0) {
437 /* hard to tell what zero-length files are, so assume the default magic */
438 if (DefaultMagic == M_MBOX || DefaultMagic == M_MMDF)
439 return (DefaultMagic);
443 else if ((f = fopen (path, "r")) != NULL) {
445 struct utimbuf times;
448 fgets (tmp, sizeof (tmp), f);
449 if (safe_strncmp ("From ", tmp, 5) == 0)
451 else if (safe_strcmp (MMDF_SEP, tmp) == 0)
455 /* need to restore the times here, the file was not really accessed,
456 * only the type was accessed. This is important, because detection
457 * of "new mail" depends on those times set correctly.
459 times.actime = st.st_atime;
460 times.modtime = st.st_mtime;
461 utime (path, ×);
467 "mx_get_magic(): unable to open file %s for reading.\n", path));
472 #ifdef USE_COMPRESSED
473 if (magic == 0 && mutt_can_read_compressed (path))
481 * set DefaultMagic to the given value
483 int mx_set_magic (const char *s)
485 if (ascii_strcasecmp (s, "mbox") == 0)
486 DefaultMagic = M_MBOX;
487 else if (ascii_strcasecmp (s, "mmdf") == 0)
488 DefaultMagic = M_MMDF;
489 else if (ascii_strcasecmp (s, "mh") == 0)
491 else if (ascii_strcasecmp (s, "maildir") == 0)
492 DefaultMagic = M_MAILDIR;
499 /* mx_access: Wrapper for access, checks permissions on a given mailbox.
500 * We may be interested in using ACL-style flags at some point, currently
501 * we use the normal access() flags. */
502 int mx_access (const char *path, int flags)
505 if (mx_is_imap (path))
506 return imap_access (path, flags);
509 return access (path, flags);
512 static int mx_open_mailbox_append (CONTEXT * ctx, int flags)
516 #ifdef USE_COMPRESSED
517 /* special case for appending to compressed folders -
518 * even if we can not open them for reading */
519 if (mutt_can_append_compressed (ctx->path))
520 mutt_open_append_compressed (ctx);
527 if (mx_is_imap (ctx->path))
528 return imap_open_mailbox_append (ctx);
532 if (stat (ctx->path, &sb) == 0) {
533 ctx->magic = mx_get_magic (ctx->path);
535 switch (ctx->magic) {
537 mutt_error (_("%s is not a mailbox."), ctx->path);
543 else if (errno == ENOENT) {
544 ctx->magic = DefaultMagic;
546 if (ctx->magic == M_MH || ctx->magic == M_MAILDIR) {
547 char tmp[_POSIX_PATH_MAX];
549 if (mkdir (ctx->path, S_IRWXU)) {
550 mutt_perror (ctx->path);
554 if (ctx->magic == M_MAILDIR) {
555 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
556 if (mkdir (tmp, S_IRWXU)) {
562 snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
563 if (mkdir (tmp, S_IRWXU)) {
565 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
570 snprintf (tmp, sizeof (tmp), "%s/tmp", ctx->path);
571 if (mkdir (tmp, S_IRWXU)) {
573 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
575 snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
584 snprintf (tmp, sizeof (tmp), "%s/.mh_sequences", ctx->path);
585 if ((i = creat (tmp, S_IRWXU)) == -1) {
595 mutt_perror (ctx->path);
599 switch (ctx->magic) {
603 safe_fopen (ctx->path, flags & M_NEWFOLDER ? "w" : "a")) == NULL
604 || mbox_lock_mailbox (ctx, 1, 1) != 0) {
606 mutt_perror (ctx->path);
608 mutt_error (_("Couldn't lock %s\n"), ctx->path);
609 safe_fclose (&ctx->fp);
613 fseek (ctx->fp, 0, 2);
629 * open a mailbox and parse it
632 * flags M_NOSORT do not sort mailbox
633 * M_APPEND open mailbox for appending
634 * M_READONLY open mailbox in read-only mode
635 * M_QUIET only print error messages
636 * ctx if non-null, context struct to use
638 CONTEXT *mx_open_mailbox (const char *path, int flags, CONTEXT * pctx)
644 ctx = safe_malloc (sizeof (CONTEXT));
645 memset (ctx, 0, sizeof (CONTEXT));
646 ctx->path = safe_strdup (path);
648 ctx->msgnotreadyet = -1;
653 if (flags & M_READONLY)
656 if (flags & (M_APPEND | M_NEWFOLDER)) {
657 if (mx_open_mailbox_append (ctx, flags) != 0) {
658 mx_fastclose_mailbox (ctx);
666 ctx->magic = mx_get_magic (path);
668 #ifdef USE_COMPRESSED
669 if (ctx->magic == M_COMPRESSED)
670 mutt_open_read_compressed (ctx);
674 mutt_error (_("%s is not a mailbox."), path);
676 if (ctx->magic == -1)
679 if (ctx->magic <= 0) {
680 mx_fastclose_mailbox (ctx);
686 /* if the user has a `push' command in their .muttrc, or in a folder-hook,
687 * it will cause the progress messages not to be displayed because
688 * mutt_refresh() will think we are in the middle of a macro. so set a
689 * flag to indicate that we should really refresh the screen.
691 set_option (OPTFORCEREFRESH);
694 mutt_message (_("Reading %s..."), ctx->path);
696 switch (ctx->magic) {
698 rc = mh_read_dir (ctx, NULL);
702 rc = maildir_read_dir (ctx);
707 rc = mbox_open_mailbox (ctx);
712 rc = imap_open_mailbox (ctx);
714 #endif /* USE_IMAP */
718 rc = pop_open_mailbox (ctx);
724 rc = nntp_open_mailbox (ctx);
726 #endif /* USE_NNTP */
734 if ((flags & M_NOSORT) == 0) {
735 /* avoid unnecessary work since the mailbox is completely unthreaded
737 unset_option (OPTSORTSUBTHREADS);
738 unset_option (OPTNEEDRESCORE);
739 mutt_sort_headers (ctx, 1);
745 mx_fastclose_mailbox (ctx);
750 unset_option (OPTFORCEREFRESH);
754 /* free up memory associated with the mailbox context */
755 void mx_fastclose_mailbox (CONTEXT * ctx)
763 if (ctx->magic == M_IMAP)
764 imap_close_mailbox (ctx);
765 #endif /* USE_IMAP */
767 if (ctx->magic == M_POP)
768 pop_close_mailbox (ctx);
771 if (ctx->magic == M_NNTP)
772 nntp_fastclose_mailbox (ctx);
773 #endif /* USE_NNTP */
775 hash_destroy (&ctx->subj_hash, NULL);
777 hash_destroy (&ctx->id_hash, NULL);
778 mutt_clear_threads (ctx);
779 for (i = 0; i < ctx->msgcount; i++)
780 mutt_free_header (&ctx->hdrs[i]);
783 #ifdef USE_COMPRESSED
784 if (ctx->compressinfo)
785 mutt_fast_close_compressed (ctx);
788 FREE (&ctx->pattern);
789 if (ctx->limit_pattern)
790 mutt_pattern_free (&ctx->limit_pattern);
791 safe_fclose (&ctx->fp);
792 memset (ctx, 0, sizeof (CONTEXT));
795 /* save changes to disk */
796 static int sync_mailbox (CONTEXT * ctx, int *index_hint)
804 mutt_message (_("Writing %s..."), ctx->path);
806 switch (ctx->magic) {
809 rc = mbox_sync_mailbox (ctx, index_hint);
811 tmp = mutt_find_mailbox (ctx->path);
817 rc = mh_sync_mailbox (ctx, index_hint);
822 /* extra argument means EXPUNGE */
823 rc = imap_sync_mailbox (ctx, 1, index_hint);
825 #endif /* USE_IMAP */
829 rc = pop_sync_mailbox (ctx, index_hint);
835 rc = nntp_sync_mailbox (ctx);
837 #endif /* USE_NNTP */
841 if (!ctx->quiet && !ctx->shutup && rc == -1)
842 mutt_error (_("Could not synchronize mailbox %s!"), ctx->path);
846 if (tmp && tmp->new == 0)
847 mutt_update_mailbox (tmp);
850 #ifdef USE_COMPRESSED
851 if (rc == 0 && ctx->compressinfo)
852 return mutt_sync_compressed (ctx);
858 /* move deleted mails to the trash folder */
859 static int trash_append (CONTEXT * ctx)
865 if (!TrashPath || !ctx->deleted ||
866 (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))
869 for (; i < ctx->msgcount && (!ctx->hdrs[i]->deleted ||
870 ctx->hdrs[i]->appended); i++);
871 if (i == ctx->msgcount)
872 return 0; /* nothing to be done */
874 if (mutt_save_confirm (TrashPath, &st) != 0) {
875 mutt_error _("message(s) not deleted");
880 if (lstat (ctx->path, &stc) == 0 && stc.st_ino == st.st_ino
881 && stc.st_dev == st.st_dev && stc.st_rdev == st.st_rdev)
882 return 0; /* we are in the trash folder: simple sync */
884 if ((ctx_trash = mx_open_mailbox (TrashPath, M_APPEND, NULL)) != NULL) {
885 for (i = 0; i < ctx->msgcount; i++)
886 if (ctx->hdrs[i]->deleted && !ctx->hdrs[i]->appended
887 && !ctx->hdrs[i]->purged
888 && mutt_append_message (ctx_trash, ctx, ctx->hdrs[i], 0, 0) == -1) {
889 mx_close_mailbox (ctx_trash, NULL);
893 mx_close_mailbox (ctx_trash, NULL);
896 mutt_error _("Can't open trash folder");
904 /* save changes and close mailbox */
905 int mx_close_mailbox (CONTEXT * ctx, int *index_hint)
907 int i, move_messages = 0, purge = 1, read_msgs = 0;
911 char mbox[_POSIX_PATH_MAX];
912 char buf[SHORT_STRING];
920 if (ctx->magic == M_NNTP) {
923 ret = nntp_close_mailbox (ctx);
924 mx_fastclose_mailbox (ctx);
928 if (ctx->readonly || ctx->dontwrite) {
929 /* mailbox is readonly or we don't want to write */
930 mx_fastclose_mailbox (ctx);
935 /* mailbox was opened in write-mode */
936 if (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
937 mbox_close_mailbox (ctx);
939 mx_fastclose_mailbox (ctx);
943 for (i = 0; i < ctx->msgcount; i++) {
944 if (!ctx->hdrs[i]->deleted && ctx->hdrs[i]->read
945 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
949 if (read_msgs && quadoption (OPT_MOVE) != M_NO) {
952 if ((p = mutt_find_hook (M_MBOXHOOK, ctx->path))) {
954 strfcpy (mbox, p, sizeof (mbox));
957 strfcpy (mbox, NONULL (Inbox), sizeof (mbox));
958 isSpool = mutt_is_spool (ctx->path) && !mutt_is_spool (mbox);
960 mutt_expand_path (mbox, sizeof (mbox));
963 snprintf (buf, sizeof (buf), _("Move read messages to %s?"), mbox);
964 if ((move_messages = query_quadoption (OPT_MOVE, buf)) == -1) {
972 * There is no point in asking whether or not to purge if we are
973 * just marking messages as "trash".
975 if (ctx->deleted && !(ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH))) {
976 snprintf (buf, sizeof (buf), ctx->deleted == 1
977 ? _("Purge %d deleted message?") :
978 _("Purge %d deleted messages?"), ctx->deleted);
979 if ((purge = query_quadoption (OPT_DELETE, buf)) < 0) {
986 /* IMAP servers manage the OLD flag themselves */
987 if (ctx->magic != M_IMAP)
989 if (option (OPTMARKOLD)) {
990 for (i = 0; i < ctx->msgcount; i++) {
991 if (!ctx->hdrs[i]->deleted && !ctx->hdrs[i]->old)
992 mutt_set_flag (ctx, ctx->hdrs[i], M_OLD, 1);
997 mutt_message (_("Moving read messages to %s..."), mbox);
1000 /* try to use server-side copy first */
1003 if (ctx->magic == M_IMAP && mx_is_imap (mbox)) {
1004 /* tag messages for moving, and clear old tags, if any */
1005 for (i = 0; i < ctx->msgcount; i++)
1006 if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted
1007 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
1008 ctx->hdrs[i]->tagged = 1;
1010 ctx->hdrs[i]->tagged = 0;
1012 i = imap_copy_messages (ctx, NULL, mbox, 1);
1015 if (i == 0) /* success */
1016 mutt_clear_error ();
1017 else if (i == -1) { /* horrible error, bail */
1021 else /* use regular append-copy mode */
1024 if (mx_open_mailbox (mbox, M_APPEND, &f) == NULL) {
1029 for (i = 0; i < ctx->msgcount; i++) {
1030 if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted
1031 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED))) {
1032 if (mutt_append_message (&f, ctx, ctx->hdrs[i], 0, CH_UPDATE_LEN) ==
1034 mutt_set_flag (ctx, ctx->hdrs[i], M_DELETE, 1);
1035 mutt_set_flag (ctx, ctx->hdrs[i], M_APPENDED, 1);
1038 mx_close_mailbox (&f, NULL);
1045 mx_close_mailbox (&f, NULL);
1049 else if (!ctx->changed && ctx->deleted == 0) {
1050 mutt_message _("Mailbox is unchanged.");
1052 mx_fastclose_mailbox (ctx);
1056 /* copy mails to the trash before expunging */
1057 if (purge && ctx->deleted)
1058 if (trash_append (ctx) != 0) {
1064 /* allow IMAP to preserve the deleted flag across sessions */
1065 if (ctx->magic == M_IMAP) {
1066 if ((check = imap_sync_mailbox (ctx, purge, index_hint)) != 0) {
1075 for (i = 0; i < ctx->msgcount; i++)
1076 ctx->hdrs[i]->deleted = 0;
1080 if (ctx->changed || ctx->deleted) {
1081 if ((check = sync_mailbox (ctx, index_hint)) != 0) {
1089 mutt_message (_("%d kept, %d moved, %d deleted."),
1090 ctx->msgcount - ctx->deleted, read_msgs, ctx->deleted);
1092 mutt_message (_("%d kept, %d deleted."),
1093 ctx->msgcount - ctx->deleted, ctx->deleted);
1095 if (ctx->msgcount == ctx->deleted &&
1096 (ctx->magic == M_MMDF || ctx->magic == M_MBOX) &&
1097 !mutt_is_spool (ctx->path) && !option (OPTSAVEEMPTY))
1098 mx_unlink_empty (ctx->path);
1100 #ifdef USE_COMPRESSED
1101 if (ctx->compressinfo && mutt_slow_close_compressed (ctx))
1105 mx_fastclose_mailbox (ctx);
1111 /* update a Context structure's internal tables. */
1113 void mx_update_tables (CONTEXT * ctx, int committing)
1117 /* update memory to reflect the new state of the mailbox */
1126 #define this_body ctx->hdrs[j]->content
1127 for (i = 0, j = 0; i < ctx->msgcount; i++) {
1128 if ((committing && (!ctx->hdrs[i]->deleted ||
1129 (ctx->magic == M_MAILDIR
1130 && option (OPTMAILDIRTRASH)))) || (!committing
1134 ctx->hdrs[j] = ctx->hdrs[i];
1135 ctx->hdrs[i] = NULL;
1137 ctx->hdrs[j]->msgno = j;
1138 if (ctx->hdrs[j]->virtual != -1) {
1139 ctx->v2r[ctx->vcount] = j;
1140 ctx->hdrs[j]->virtual = ctx->vcount++;
1141 ctx->vsize += this_body->length + this_body->offset -
1142 this_body->hdr_offset;
1146 ctx->hdrs[j]->changed = 0;
1147 else if (ctx->hdrs[j]->changed)
1151 || (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH))) {
1152 if (ctx->hdrs[j]->deleted)
1156 if (ctx->hdrs[j]->tagged)
1158 if (ctx->hdrs[j]->flagged)
1160 if (!ctx->hdrs[j]->read) {
1162 if (!ctx->hdrs[j]->old)
1169 if (ctx->magic == M_MH || ctx->magic == M_MAILDIR)
1170 ctx->size -= (ctx->hdrs[i]->content->length +
1171 ctx->hdrs[i]->content->offset -
1172 ctx->hdrs[i]->content->hdr_offset);
1173 /* remove message from the hash tables */
1174 if (ctx->subj_hash && ctx->hdrs[i]->env->real_subj)
1175 hash_delete (ctx->subj_hash, ctx->hdrs[i]->env->real_subj,
1176 ctx->hdrs[i], NULL);
1177 if (ctx->id_hash && ctx->hdrs[i]->env->message_id)
1178 hash_delete (ctx->id_hash, ctx->hdrs[i]->env->message_id,
1179 ctx->hdrs[i], NULL);
1180 mutt_free_header (&ctx->hdrs[i]);
1188 /* save changes to mailbox
1194 int mx_sync_mailbox (CONTEXT * ctx, int *index_hint)
1198 int msgcount, deleted;
1200 if (ctx->dontwrite) {
1201 char buf[STRING], tmp[STRING];
1203 if (km_expand_key (buf, sizeof (buf),
1204 km_find_func (MENU_MAIN, OP_TOGGLE_WRITE)))
1205 snprintf (tmp, sizeof (tmp), _(" Press '%s' to toggle write"), buf);
1207 strfcpy (tmp, _("Use 'toggle-write' to re-enable write!"),
1210 mutt_error (_("Mailbox is marked unwritable. %s"), tmp);
1213 else if (ctx->readonly) {
1214 mutt_error _("Mailbox is read-only.");
1219 if (!ctx->changed && !ctx->deleted) {
1220 mutt_message _("Mailbox is unchanged.");
1226 char buf[SHORT_STRING];
1228 snprintf (buf, sizeof (buf), ctx->deleted == 1
1229 ? _("Purge %d deleted message?") :
1230 _("Purge %d deleted messages?"), ctx->deleted);
1231 if ((purge = query_quadoption (OPT_DELETE, buf)) < 0)
1233 else if (purge == M_NO) {
1235 return 0; /* nothing to do! */
1237 /* let IMAP servers hold on to D flags */
1238 if (ctx->magic != M_IMAP)
1241 for (i = 0; i < ctx->msgcount; i++)
1242 ctx->hdrs[i]->deleted = 0;
1246 else if (ctx->last_tag && ctx->last_tag->deleted)
1247 ctx->last_tag = NULL; /* reset last tagged msg now useless */
1250 /* really only for IMAP - imap_sync_mailbox results in a call to
1251 * mx_update_tables, so ctx->deleted is 0 when it comes back */
1252 msgcount = ctx->msgcount;
1253 deleted = ctx->deleted;
1255 if (purge && ctx->deleted) {
1256 if (trash_append (ctx) == -1)
1261 if (ctx->magic == M_IMAP)
1262 rc = imap_sync_mailbox (ctx, purge, index_hint);
1265 rc = sync_mailbox (ctx, index_hint);
1268 if (ctx->magic == M_IMAP && !purge)
1269 mutt_message (_("Mailbox checkpointed."));
1273 mutt_message (_("%d kept, %d deleted."), msgcount - deleted, deleted);
1277 if (ctx->msgcount == ctx->deleted &&
1278 (ctx->magic == M_MBOX || ctx->magic == M_MMDF) &&
1279 !mutt_is_spool (ctx->path) && !option (OPTSAVEEMPTY)) {
1281 mx_fastclose_mailbox (ctx);
1285 /* if we haven't deleted any messages, we don't need to resort */
1286 /* ... except for certain folder formats which need "unsorted"
1287 * sort order in order to synchronize folders.
1289 * MH and maildir are safe. mbox-style seems to need re-sorting,
1290 * at least with the new threading code.
1292 if (purge || (ctx->magic != M_MAILDIR && ctx->magic != M_MH)) {
1294 /* IMAP does this automatically after handling EXPUNGE */
1295 if (ctx->magic != M_IMAP)
1298 mx_update_tables (ctx, 1);
1299 mutt_sort_headers (ctx, 1); /* rethread from scratch */
1308 int imap_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr)
1310 char tmp[_POSIX_PATH_MAX];
1313 if ((msg->fp = safe_fopen (tmp, "w")) == NULL)
1315 msg->path = safe_strdup (tmp);
1321 * dest destintation mailbox
1322 * hdr message being copied (required for maildir support, because
1323 * the filename depends on the message flags)
1325 MESSAGE *mx_open_new_message (CONTEXT * dest, HEADER * hdr, int flags)
1328 int (*func) (MESSAGE *, CONTEXT *, HEADER *);
1331 switch (dest->magic) {
1334 func = mbox_open_new_message;
1337 func = maildir_open_new_message;
1340 func = mh_open_new_message;
1344 func = imap_open_new_message;
1350 "mx_open_new_message(): function unimplemented for mailbox type %d.\n",
1355 msg = safe_calloc (1, sizeof (MESSAGE));
1356 msg->magic = dest->magic;
1360 msg->flags.flagged = hdr->flagged;
1361 msg->flags.replied = hdr->replied;
1362 msg->flags.read = hdr->read;
1363 msg->received = hdr->received;
1366 if (msg->received == 0)
1367 time (&msg->received);
1369 if (func (msg, dest, hdr) == 0) {
1370 if (dest->magic == M_MMDF)
1371 fputs (MMDF_SEP, msg->fp);
1373 if ((msg->magic == M_MBOX || msg->magic == M_MMDF) && flags & M_ADD_FROM) {
1375 if (hdr->env->return_path)
1376 p = hdr->env->return_path;
1377 else if (hdr->env->sender)
1378 p = hdr->env->sender;
1383 fprintf (msg->fp, "From %s %s", p ? p->mailbox : NONULL (Username),
1384 ctime (&msg->received));
1393 /* check for new mail */
1394 int mx_check_mailbox (CONTEXT * ctx, int *index_hint, int lock)
1398 #ifdef USE_COMPRESSED
1399 if (ctx->compressinfo)
1400 return mutt_check_mailbox_compressed (ctx);
1407 switch (ctx->magic) {
1412 mutt_block_signals ();
1413 if (mbox_lock_mailbox (ctx, 0, 0) == -1) {
1414 mutt_unblock_signals ();
1419 rc = mbox_check_mailbox (ctx, index_hint);
1422 mutt_unblock_signals ();
1423 mbox_unlock_mailbox (ctx);
1430 return (mh_check_mailbox (ctx, index_hint));
1432 return (maildir_check_mailbox (ctx, index_hint));
1436 return (imap_check_mailbox (ctx, index_hint, 0));
1437 #endif /* USE_IMAP */
1441 return (pop_check_mailbox (ctx, index_hint));
1442 #endif /* USE_POP */
1446 return (nntp_check_mailbox (ctx));
1447 #endif /* USE_NNTP */
1451 dprint (1, (debugfile, "mx_check_mailbox: null or invalid context.\n"));
1455 /* return a stream pointer for a message */
1456 MESSAGE *mx_open_message (CONTEXT * ctx, int msgno)
1460 msg = safe_calloc (1, sizeof (MESSAGE));
1461 switch (msg->magic = ctx->magic) {
1470 HEADER *cur = ctx->hdrs[msgno];
1471 char path[_POSIX_PATH_MAX];
1473 snprintf (path, sizeof (path), "%s/%s", ctx->path, cur->path);
1475 if ((msg->fp = fopen (path, "r")) == NULL && errno == ENOENT &&
1476 ctx->magic == M_MAILDIR)
1477 msg->fp = maildir_open_find_message (ctx->path, cur->path);
1479 if (msg->fp == NULL) {
1481 dprint (1, (debugfile, "mx_open_message: fopen: %s: %s (errno %d).\n",
1482 path, strerror (errno), errno));
1491 if (imap_fetch_message (msg, ctx, msgno) != 0)
1495 #endif /* USE_IMAP */
1500 if (pop_fetch_message (msg, ctx, msgno) != 0)
1504 #endif /* USE_POP */
1509 if (nntp_fetch_message (msg, ctx, msgno) != 0)
1513 #endif /* USE_NNTP */
1518 "mx_open_message(): function not implemented for mailbox type %d.\n",
1526 /* commit a message to a folder */
1528 int mx_commit_message (MESSAGE * msg, CONTEXT * ctx)
1532 if (!(msg->write && ctx->append)) {
1535 "mx_commit_message(): msg->write = %d, ctx->append = %d\n",
1536 msg->write, ctx->append));
1540 switch (msg->magic) {
1543 if (fputs (MMDF_SEP, msg->fp) == EOF)
1550 if (fputc ('\n', msg->fp) == EOF)
1558 if ((r = safe_fclose (&msg->fp)) == 0)
1559 r = imap_append_message (ctx, msg);
1566 r = maildir_commit_message (ctx, msg, NULL);
1572 r = mh_commit_message (ctx, msg, NULL);
1577 if (r == 0 && (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
1578 && (fflush (msg->fp) == EOF || fsync (fileno (msg->fp)) == -1)) {
1579 mutt_perror _("Can't write message");
1587 /* close a pointer to a message */
1588 int mx_close_message (MESSAGE ** msg)
1592 if ((*msg)->magic == M_MH || (*msg)->magic == M_MAILDIR
1594 || (*msg)->magic == M_IMAP
1597 || (*msg)->magic == M_POP
1600 || (*msg)->magic == M_NNTP
1603 r = safe_fclose (&(*msg)->fp);
1609 dprint (1, (debugfile, "mx_close_message (): unlinking %s\n",
1611 unlink ((*msg)->path);
1612 FREE (&(*msg)->path);
1619 void mx_alloc_memory (CONTEXT * ctx)
1622 size_t s = MAX (sizeof (HEADER *), sizeof (int));
1624 if ((ctx->hdrmax + 25) * s < ctx->hdrmax * s) {
1625 mutt_error _("Integer overflow -- can't allocate memory.");
1632 safe_realloc (&ctx->hdrs, sizeof (HEADER *) * (ctx->hdrmax += 25));
1633 safe_realloc (&ctx->v2r, sizeof (int) * ctx->hdrmax);
1636 ctx->hdrs = safe_calloc ((ctx->hdrmax += 25), sizeof (HEADER *));
1637 ctx->v2r = safe_calloc (ctx->hdrmax, sizeof (int));
1639 for (i = ctx->msgcount; i < ctx->hdrmax; i++) {
1640 ctx->hdrs[i] = NULL;
1645 /* this routine is called to update the counts in the context structure for
1646 * the last message header parsed.
1648 void mx_update_context (CONTEXT * ctx, int new_messages)
1653 for (msgno = ctx->msgcount - new_messages; msgno < ctx->msgcount; msgno++) {
1654 h = ctx->hdrs[msgno];
1657 /* NOTE: this _must_ be done before the check for mailcap! */
1658 h->security = crypt_query (h->content);
1661 if (!ctx->pattern) {
1662 ctx->v2r[ctx->vcount] = msgno;
1663 h->virtual = ctx->vcount++;
1669 if (h->env->supersedes) {
1673 ctx->id_hash = mutt_make_id_hash (ctx);
1675 h2 = hash_find (ctx->id_hash, h->env->supersedes);
1677 /* FREE (&h->env->supersedes); should I ? */
1680 if (option (OPTSCORE))
1681 mutt_score_message (ctx, h2, 1);
1685 /* add this message to the hash tables */
1686 if (ctx->id_hash && h->env->message_id)
1687 hash_insert (ctx->id_hash, h->env->message_id, h, 0);
1688 if (ctx->subj_hash && h->env->real_subj)
1689 hash_insert (ctx->subj_hash, h->env->real_subj, h, 1);
1691 if (option (OPTSCORE))
1692 mutt_score_message (ctx, h, 0);
1710 * 1 if the specified mailbox contains 0 messages.
1711 * 0 if the mailbox contains messages
1714 int mx_check_empty (const char *path)
1716 switch (mx_get_magic (path)) {
1719 return mbox_check_empty (path);
1721 return mh_check_empty (path);
1723 return maildir_check_empty (path);