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"
68 #define mutt_is_spool(s) (mutt_strcmp (Spoolfile, s) == 0)
73 * retry - should retry if unable to lock?
78 static int invoke_dotlock (const char *path, int dummy, int flags, int retry)
80 char cmd[LONG_STRING + _POSIX_PATH_MAX];
81 char f[SHORT_STRING + _POSIX_PATH_MAX];
84 if (flags & DL_FL_RETRY)
85 snprintf (r, sizeof (r), "-r %d ", retry ? MAXLOCKATTEMPT : 0);
87 mutt_quote_filename (f, sizeof (f), path);
89 snprintf (cmd, sizeof (cmd),
92 flags & DL_FL_TRY ? "-t " : "",
93 flags & DL_FL_UNLOCK ? "-u " : "",
94 flags & DL_FL_USEPRIV ? "-p " : "",
95 flags & DL_FL_FORCE ? "-f " : "",
96 flags & DL_FL_UNLINK ? "-d " : "",
97 flags & DL_FL_RETRY ? r : "", f);
99 return mutt_system (cmd);
104 #define invoke_dotlock dotlock_invoke
108 static int dotlock_file (const char *path, int fd, int retry)
111 int flags = DL_FL_USEPRIV | DL_FL_RETRY;
117 if ((r = invoke_dotlock (path, fd, flags, retry)) == DL_EX_EXIST) {
118 if (!option (OPTNOCURSES)) {
119 char msg[LONG_STRING];
121 snprintf (msg, sizeof (msg),
122 _("Lock count exceeded, remove lock for %s?"), path);
123 if (retry && mutt_yesorno (msg, M_YES) == M_YES) {
124 flags |= DL_FL_FORCE;
131 mutt_error (_("Can't dotlock %s.\n"), path);
134 return (r == DL_EX_OK ? 0 : -1);
137 static int undotlock_file (const char *path, int fd)
139 return (invoke_dotlock (path, fd, DL_FL_USEPRIV | DL_FL_UNLOCK, 0) ==
143 #endif /* USE_DOTLOCK */
146 * excl if excl != 0, request an exclusive lock
147 * dot if dot != 0, try to dotlock the file
148 * timeout should retry locking?
150 int mx_lock_file (const char *path, int fd, int excl, int dot, int timeout)
152 #if defined (USE_FCNTL) || defined (USE_FLOCK)
163 memset (&lck, 0, sizeof (struct flock));
164 lck.l_type = excl ? F_WRLCK : F_RDLCK;
165 lck.l_whence = SEEK_SET;
169 while (fcntl (fd, F_SETLK, &lck) == -1) {
172 dprint (1, (debugfile, "mx_lock_file(): fcntl errno %d.\n", errno));
173 if (errno != EAGAIN && errno != EACCES) {
174 mutt_perror ("fcntl");
178 if (fstat (fd, &sb) != 0)
184 /* only unlock file if it is unchanged */
185 if (prev_sb.st_size == sb.st_size
186 && ++count >= (timeout ? MAXLOCKATTEMPT : 0)) {
188 mutt_error _("Timeout exceeded while attempting fcntl lock!");
195 mutt_message (_("Waiting for fcntl lock... %d"), ++attempt);
198 #endif /* USE_FCNTL */
203 while (flock (fd, (excl ? LOCK_EX : LOCK_SH) | LOCK_NB) == -1) {
206 if (errno != EWOULDBLOCK) {
207 mutt_perror ("flock");
212 if (fstat (fd, &sb) != 0)
218 /* only unlock file if it is unchanged */
219 if (prev_sb.st_size == sb.st_size
220 && ++count >= (timeout ? MAXLOCKATTEMPT : 0)) {
222 mutt_error _("Timeout exceeded while attempting flock lock!");
230 mutt_message (_("Waiting for flock attempt... %d"), ++attempt);
233 #endif /* USE_FLOCK */
237 r = dotlock_file (path, fd, timeout);
238 #endif /* USE_DOTLOCK */
241 /* release any other locks obtained in this routine */
244 lck.l_type = F_UNLCK;
245 fcntl (fd, F_SETLK, &lck);
246 #endif /* USE_FCNTL */
250 #endif /* USE_FLOCK */
258 int mx_unlock_file (const char *path, int fd, int dot)
261 struct flock unlockit = { F_UNLCK, 0, 0, 0 };
263 memset (&unlockit, 0, sizeof (struct flock));
264 unlockit.l_type = F_UNLCK;
265 unlockit.l_whence = SEEK_SET;
266 fcntl (fd, F_SETLK, &unlockit);
275 undotlock_file (path, fd);
281 void mx_unlink_empty (const char *path)
289 if ((fd = open (path, O_RDWR)) == -1)
292 if (mx_lock_file (path, fd, 1, 0, 1) == -1) {
298 invoke_dotlock (path, fd, DL_FL_UNLINK, 1);
300 if (fstat (fd, &sb) == 0 && sb.st_size == 0)
304 mx_unlock_file (path, fd, 0);
308 /* 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)
338 int mx_is_pop (const char *p)
345 scheme = url_check_scheme (p);
346 if (scheme == U_POP || scheme == U_POPS)
354 int mx_is_nntp (const char *p)
361 scheme = url_check_scheme (p);
362 if (scheme == U_NNTP || scheme == U_NNTPS)
369 int mx_get_magic (const char *path)
373 char tmp[_POSIX_PATH_MAX];
377 if (mx_is_imap (path))
379 #endif /* USE_IMAP */
382 if (mx_is_pop (path))
387 if (mx_is_nntp (path))
389 #endif /* USE_NNTP */
391 if (stat (path, &st) == -1) {
393 (debugfile, "mx_get_magic(): unable to stat %s: %s (errno %d).\n",
394 path, strerror (errno), errno));
398 if (S_ISDIR (st.st_mode)) {
399 /* check for maildir-style mailbox */
401 snprintf (tmp, sizeof (tmp), "%s/cur", path);
402 if (stat (tmp, &st) == 0 && S_ISDIR (st.st_mode))
405 /* check for mh-style mailbox */
407 snprintf (tmp, sizeof (tmp), "%s/.mh_sequences", path);
408 if (access (tmp, F_OK) == 0)
411 snprintf (tmp, sizeof (tmp), "%s/.xmhcache", 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/.mew-cache", path);
420 if (access (tmp, F_OK) == 0)
423 snprintf (tmp, sizeof (tmp), "%s/.sylpheed_cache", path);
424 if (access (tmp, F_OK) == 0)
428 * ok, this isn't an mh folder, but mh mode can be used to read
429 * Usenet news from the spool. ;-)
432 snprintf (tmp, sizeof (tmp), "%s/.overview", path);
433 if (access (tmp, F_OK) == 0)
437 else if (st.st_size == 0) {
438 /* hard to tell what zero-length files are, so assume the default magic */
439 if (DefaultMagic == M_MBOX || DefaultMagic == M_MMDF)
440 return (DefaultMagic);
444 else if ((f = fopen (path, "r")) != NULL) {
446 struct utimbuf times;
449 fgets (tmp, sizeof (tmp), f);
450 if (mutt_strncmp ("From ", tmp, 5) == 0)
452 else if (mutt_strcmp (MMDF_SEP, tmp) == 0)
456 /* need to restore the times here, the file was not really accessed,
457 * only the type was accessed. This is important, because detection
458 * of "new mail" depends on those times set correctly.
460 times.actime = st.st_atime;
461 times.modtime = st.st_mtime;
462 utime (path, ×);
468 "mx_get_magic(): unable to open file %s for reading.\n", path));
473 #ifdef USE_COMPRESSED
474 if (magic == 0 && mutt_can_read_compressed (path))
482 * set DefaultMagic to the given value
484 int mx_set_magic (const char *s)
486 if (ascii_strcasecmp (s, "mbox") == 0)
487 DefaultMagic = M_MBOX;
488 else if (ascii_strcasecmp (s, "mmdf") == 0)
489 DefaultMagic = M_MMDF;
490 else if (ascii_strcasecmp (s, "mh") == 0)
492 else if (ascii_strcasecmp (s, "maildir") == 0)
493 DefaultMagic = M_MAILDIR;
500 /* mx_access: Wrapper for access, checks permissions on a given mailbox.
501 * We may be interested in using ACL-style flags at some point, currently
502 * we use the normal access() flags. */
503 int mx_access (const char *path, int flags)
506 if (mx_is_imap (path))
507 return imap_access (path, flags);
510 return access (path, flags);
513 static int mx_open_mailbox_append (CONTEXT * ctx, int flags)
517 #ifdef USE_COMPRESSED
518 /* special case for appending to compressed folders -
519 * even if we can not open them for reading */
520 if (mutt_can_append_compressed (ctx->path))
521 mutt_open_append_compressed (ctx);
528 if (mx_is_imap (ctx->path))
529 return imap_open_mailbox_append (ctx);
533 if (stat (ctx->path, &sb) == 0) {
534 ctx->magic = mx_get_magic (ctx->path);
536 switch (ctx->magic) {
538 mutt_error (_("%s is not a mailbox."), ctx->path);
544 else if (errno == ENOENT) {
545 ctx->magic = DefaultMagic;
547 if (ctx->magic == M_MH || ctx->magic == M_MAILDIR) {
548 char tmp[_POSIX_PATH_MAX];
550 if (mkdir (ctx->path, S_IRWXU)) {
551 mutt_perror (ctx->path);
555 if (ctx->magic == M_MAILDIR) {
556 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
557 if (mkdir (tmp, S_IRWXU)) {
563 snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
564 if (mkdir (tmp, S_IRWXU)) {
566 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
571 snprintf (tmp, sizeof (tmp), "%s/tmp", ctx->path);
572 if (mkdir (tmp, S_IRWXU)) {
574 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
576 snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
585 snprintf (tmp, sizeof (tmp), "%s/.mh_sequences", ctx->path);
586 if ((i = creat (tmp, S_IRWXU)) == -1) {
596 mutt_perror (ctx->path);
600 switch (ctx->magic) {
604 safe_fopen (ctx->path, flags & M_NEWFOLDER ? "w" : "a")) == NULL
605 || mbox_lock_mailbox (ctx, 1, 1) != 0) {
607 mutt_perror (ctx->path);
609 mutt_error (_("Couldn't lock %s\n"), ctx->path);
610 safe_fclose (&ctx->fp);
614 fseek (ctx->fp, 0, 2);
630 * open a mailbox and parse it
633 * flags M_NOSORT do not sort mailbox
634 * M_APPEND open mailbox for appending
635 * M_READONLY open mailbox in read-only mode
636 * M_QUIET only print error messages
637 * ctx if non-null, context struct to use
639 CONTEXT *mx_open_mailbox (const char *path, int flags, CONTEXT * pctx)
645 ctx = safe_malloc (sizeof (CONTEXT));
646 memset (ctx, 0, sizeof (CONTEXT));
647 ctx->path = safe_strdup (path);
649 ctx->msgnotreadyet = -1;
654 if (flags & M_READONLY)
657 if (flags & (M_APPEND | M_NEWFOLDER)) {
658 if (mx_open_mailbox_append (ctx, flags) != 0) {
659 mx_fastclose_mailbox (ctx);
667 ctx->magic = mx_get_magic (path);
669 #ifdef USE_COMPRESSED
670 if (ctx->magic == M_COMPRESSED)
671 mutt_open_read_compressed (ctx);
675 mutt_error (_("%s is not a mailbox."), path);
677 if (ctx->magic == -1)
680 if (ctx->magic <= 0) {
681 mx_fastclose_mailbox (ctx);
687 /* if the user has a `push' command in their .muttrc, or in a folder-hook,
688 * it will cause the progress messages not to be displayed because
689 * mutt_refresh() will think we are in the middle of a macro. so set a
690 * flag to indicate that we should really refresh the screen.
692 set_option (OPTFORCEREFRESH);
695 mutt_message (_("Reading %s..."), ctx->path);
697 switch (ctx->magic) {
699 rc = mh_read_dir (ctx, NULL);
703 rc = maildir_read_dir (ctx);
708 rc = mbox_open_mailbox (ctx);
713 rc = imap_open_mailbox (ctx);
715 #endif /* USE_IMAP */
719 rc = pop_open_mailbox (ctx);
725 rc = nntp_open_mailbox (ctx);
727 #endif /* USE_NNTP */
735 if ((flags & M_NOSORT) == 0) {
736 /* avoid unnecessary work since the mailbox is completely unthreaded
738 unset_option (OPTSORTSUBTHREADS);
739 unset_option (OPTNEEDRESCORE);
740 mutt_sort_headers (ctx, 1);
746 mx_fastclose_mailbox (ctx);
751 unset_option (OPTFORCEREFRESH);
755 /* free up memory associated with the mailbox context */
756 void mx_fastclose_mailbox (CONTEXT * ctx)
764 if (ctx->magic == M_IMAP)
765 imap_close_mailbox (ctx);
766 #endif /* USE_IMAP */
768 if (ctx->magic == M_POP)
769 pop_close_mailbox (ctx);
772 if (ctx->magic == M_NNTP)
773 nntp_fastclose_mailbox (ctx);
774 #endif /* USE_NNTP */
776 hash_destroy (&ctx->subj_hash, NULL);
778 hash_destroy (&ctx->id_hash, NULL);
779 mutt_clear_threads (ctx);
780 for (i = 0; i < ctx->msgcount; i++)
781 mutt_free_header (&ctx->hdrs[i]);
784 #ifdef USE_COMPRESSED
785 if (ctx->compressinfo)
786 mutt_fast_close_compressed (ctx);
789 FREE (&ctx->pattern);
790 if (ctx->limit_pattern)
791 mutt_pattern_free (&ctx->limit_pattern);
792 safe_fclose (&ctx->fp);
793 memset (ctx, 0, sizeof (CONTEXT));
796 /* save changes to disk */
797 static int sync_mailbox (CONTEXT * ctx, int *index_hint)
805 mutt_message (_("Writing %s..."), ctx->path);
807 switch (ctx->magic) {
810 rc = mbox_sync_mailbox (ctx, index_hint);
812 tmp = mutt_find_mailbox (ctx->path);
818 rc = mh_sync_mailbox (ctx, index_hint);
823 /* extra argument means EXPUNGE */
824 rc = imap_sync_mailbox (ctx, 1, index_hint);
826 #endif /* USE_IMAP */
830 rc = pop_sync_mailbox (ctx, index_hint);
836 rc = nntp_sync_mailbox (ctx);
838 #endif /* USE_NNTP */
842 if (!ctx->quiet && !ctx->shutup && rc == -1)
843 mutt_error (_("Could not synchronize mailbox %s!"), ctx->path);
847 if (tmp && tmp->new == 0)
848 mutt_update_mailbox (tmp);
851 #ifdef USE_COMPRESSED
852 if (rc == 0 && ctx->compressinfo)
853 return mutt_sync_compressed (ctx);
859 /* move deleted mails to the trash folder */
860 static int trash_append (CONTEXT * ctx)
866 if (!TrashPath || !ctx->deleted ||
867 (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))
870 for (; i < ctx->msgcount && (!ctx->hdrs[i]->deleted ||
871 ctx->hdrs[i]->appended); i++);
872 if (i == ctx->msgcount)
873 return 0; /* nothing to be done */
875 if (mutt_save_confirm (TrashPath, &st) != 0) {
876 mutt_error _("message(s) not deleted");
881 if (lstat (ctx->path, &stc) == 0 && stc.st_ino == st.st_ino
882 && stc.st_dev == st.st_dev && stc.st_rdev == st.st_rdev)
883 return 0; /* we are in the trash folder: simple sync */
885 if ((ctx_trash = mx_open_mailbox (TrashPath, M_APPEND, NULL)) != NULL) {
886 for (i = 0; i < ctx->msgcount; i++)
887 if (ctx->hdrs[i]->deleted && !ctx->hdrs[i]->appended
888 && !ctx->hdrs[i]->purged
889 && mutt_append_message (ctx_trash, ctx, ctx->hdrs[i], 0, 0) == -1) {
890 mx_close_mailbox (ctx_trash, NULL);
894 mx_close_mailbox (ctx_trash, NULL);
897 mutt_error _("Can't open trash folder");
905 /* save changes and close mailbox */
906 int mx_close_mailbox (CONTEXT * ctx, int *index_hint)
908 int i, move_messages = 0, purge = 1, read_msgs = 0;
912 char mbox[_POSIX_PATH_MAX];
913 char buf[SHORT_STRING];
921 if (ctx->magic == M_NNTP) {
924 ret = nntp_close_mailbox (ctx);
925 mx_fastclose_mailbox (ctx);
929 if (ctx->readonly || ctx->dontwrite) {
930 /* mailbox is readonly or we don't want to write */
931 mx_fastclose_mailbox (ctx);
936 /* mailbox was opened in write-mode */
937 if (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
938 mbox_close_mailbox (ctx);
940 mx_fastclose_mailbox (ctx);
944 for (i = 0; i < ctx->msgcount; i++) {
945 if (!ctx->hdrs[i]->deleted && ctx->hdrs[i]->read
946 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
950 if (read_msgs && quadoption (OPT_MOVE) != M_NO) {
953 if ((p = mutt_find_hook (M_MBOXHOOK, ctx->path))) {
955 strfcpy (mbox, p, sizeof (mbox));
958 strfcpy (mbox, NONULL (Inbox), sizeof (mbox));
959 isSpool = mutt_is_spool (ctx->path) && !mutt_is_spool (mbox);
961 mutt_expand_path (mbox, sizeof (mbox));
964 snprintf (buf, sizeof (buf), _("Move read messages to %s?"), mbox);
965 if ((move_messages = query_quadoption (OPT_MOVE, buf)) == -1) {
973 * There is no point in asking whether or not to purge if we are
974 * just marking messages as "trash".
976 if (ctx->deleted && !(ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH))) {
977 snprintf (buf, sizeof (buf), ctx->deleted == 1
978 ? _("Purge %d deleted message?") :
979 _("Purge %d deleted messages?"), ctx->deleted);
980 if ((purge = query_quadoption (OPT_DELETE, buf)) < 0) {
987 /* IMAP servers manage the OLD flag themselves */
988 if (ctx->magic != M_IMAP)
990 if (option (OPTMARKOLD)) {
991 for (i = 0; i < ctx->msgcount; i++) {
992 if (!ctx->hdrs[i]->deleted && !ctx->hdrs[i]->old)
993 mutt_set_flag (ctx, ctx->hdrs[i], M_OLD, 1);
998 mutt_message (_("Moving read messages to %s..."), mbox);
1001 /* try to use server-side copy first */
1004 if (ctx->magic == M_IMAP && mx_is_imap (mbox)) {
1005 /* tag messages for moving, and clear old tags, if any */
1006 for (i = 0; i < ctx->msgcount; i++)
1007 if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted
1008 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
1009 ctx->hdrs[i]->tagged = 1;
1011 ctx->hdrs[i]->tagged = 0;
1013 i = imap_copy_messages (ctx, NULL, mbox, 1);
1016 if (i == 0) /* success */
1017 mutt_clear_error ();
1018 else if (i == -1) { /* horrible error, bail */
1022 else /* use regular append-copy mode */
1025 if (mx_open_mailbox (mbox, M_APPEND, &f) == NULL) {
1030 for (i = 0; i < ctx->msgcount; i++) {
1031 if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted
1032 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED))) {
1033 if (mutt_append_message (&f, ctx, ctx->hdrs[i], 0, CH_UPDATE_LEN) ==
1035 mutt_set_flag (ctx, ctx->hdrs[i], M_DELETE, 1);
1036 mutt_set_flag (ctx, ctx->hdrs[i], M_APPENDED, 1);
1039 mx_close_mailbox (&f, NULL);
1046 mx_close_mailbox (&f, NULL);
1050 else if (!ctx->changed && ctx->deleted == 0) {
1051 mutt_message _("Mailbox is unchanged.");
1053 mx_fastclose_mailbox (ctx);
1057 /* copy mails to the trash before expunging */
1058 if (purge && ctx->deleted)
1059 if (trash_append (ctx) != 0) {
1065 /* allow IMAP to preserve the deleted flag across sessions */
1066 if (ctx->magic == M_IMAP) {
1067 if ((check = imap_sync_mailbox (ctx, purge, index_hint)) != 0) {
1076 for (i = 0; i < ctx->msgcount; i++)
1077 ctx->hdrs[i]->deleted = 0;
1081 if (ctx->changed || ctx->deleted) {
1082 if ((check = sync_mailbox (ctx, index_hint)) != 0) {
1090 mutt_message (_("%d kept, %d moved, %d deleted."),
1091 ctx->msgcount - ctx->deleted, read_msgs, ctx->deleted);
1093 mutt_message (_("%d kept, %d deleted."),
1094 ctx->msgcount - ctx->deleted, ctx->deleted);
1096 if (ctx->msgcount == ctx->deleted &&
1097 (ctx->magic == M_MMDF || ctx->magic == M_MBOX) &&
1098 !mutt_is_spool (ctx->path) && !option (OPTSAVEEMPTY))
1099 mx_unlink_empty (ctx->path);
1101 #ifdef USE_COMPRESSED
1102 if (ctx->compressinfo && mutt_slow_close_compressed (ctx))
1106 mx_fastclose_mailbox (ctx);
1112 /* update a Context structure's internal tables. */
1114 void mx_update_tables (CONTEXT * ctx, int committing)
1118 /* update memory to reflect the new state of the mailbox */
1127 #define this_body ctx->hdrs[j]->content
1128 for (i = 0, j = 0; i < ctx->msgcount; i++) {
1129 if ((committing && (!ctx->hdrs[i]->deleted ||
1130 (ctx->magic == M_MAILDIR
1131 && option (OPTMAILDIRTRASH)))) || (!committing
1135 ctx->hdrs[j] = ctx->hdrs[i];
1136 ctx->hdrs[i] = NULL;
1138 ctx->hdrs[j]->msgno = j;
1139 if (ctx->hdrs[j]->virtual != -1) {
1140 ctx->v2r[ctx->vcount] = j;
1141 ctx->hdrs[j]->virtual = ctx->vcount++;
1142 ctx->vsize += this_body->length + this_body->offset -
1143 this_body->hdr_offset;
1147 ctx->hdrs[j]->changed = 0;
1148 else if (ctx->hdrs[j]->changed)
1152 || (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH))) {
1153 if (ctx->hdrs[j]->deleted)
1157 if (ctx->hdrs[j]->tagged)
1159 if (ctx->hdrs[j]->flagged)
1161 if (!ctx->hdrs[j]->read) {
1163 if (!ctx->hdrs[j]->old)
1170 if (ctx->magic == M_MH || ctx->magic == M_MAILDIR)
1171 ctx->size -= (ctx->hdrs[i]->content->length +
1172 ctx->hdrs[i]->content->offset -
1173 ctx->hdrs[i]->content->hdr_offset);
1174 /* remove message from the hash tables */
1175 if (ctx->subj_hash && ctx->hdrs[i]->env->real_subj)
1176 hash_delete (ctx->subj_hash, ctx->hdrs[i]->env->real_subj,
1177 ctx->hdrs[i], NULL);
1178 if (ctx->id_hash && ctx->hdrs[i]->env->message_id)
1179 hash_delete (ctx->id_hash, ctx->hdrs[i]->env->message_id,
1180 ctx->hdrs[i], NULL);
1181 mutt_free_header (&ctx->hdrs[i]);
1189 /* save changes to mailbox
1195 int mx_sync_mailbox (CONTEXT * ctx, int *index_hint)
1199 int msgcount, deleted;
1201 if (ctx->dontwrite) {
1202 char buf[STRING], tmp[STRING];
1204 if (km_expand_key (buf, sizeof (buf),
1205 km_find_func (MENU_MAIN, OP_TOGGLE_WRITE)))
1206 snprintf (tmp, sizeof (tmp), _(" Press '%s' to toggle write"), buf);
1208 strfcpy (tmp, _("Use 'toggle-write' to re-enable write!"),
1211 mutt_error (_("Mailbox is marked unwritable. %s"), tmp);
1214 else if (ctx->readonly) {
1215 mutt_error _("Mailbox is read-only.");
1220 if (!ctx->changed && !ctx->deleted) {
1221 mutt_message _("Mailbox is unchanged.");
1227 char buf[SHORT_STRING];
1229 snprintf (buf, sizeof (buf), ctx->deleted == 1
1230 ? _("Purge %d deleted message?") :
1231 _("Purge %d deleted messages?"), ctx->deleted);
1232 if ((purge = query_quadoption (OPT_DELETE, buf)) < 0)
1234 else if (purge == M_NO) {
1236 return 0; /* nothing to do! */
1238 /* let IMAP servers hold on to D flags */
1239 if (ctx->magic != M_IMAP)
1242 for (i = 0; i < ctx->msgcount; i++)
1243 ctx->hdrs[i]->deleted = 0;
1247 else if (ctx->last_tag && ctx->last_tag->deleted)
1248 ctx->last_tag = NULL; /* reset last tagged msg now useless */
1251 /* really only for IMAP - imap_sync_mailbox results in a call to
1252 * mx_update_tables, so ctx->deleted is 0 when it comes back */
1253 msgcount = ctx->msgcount;
1254 deleted = ctx->deleted;
1256 if (purge && ctx->deleted) {
1257 if (trash_append (ctx) == -1)
1262 if (ctx->magic == M_IMAP)
1263 rc = imap_sync_mailbox (ctx, purge, index_hint);
1266 rc = sync_mailbox (ctx, index_hint);
1269 if (ctx->magic == M_IMAP && !purge)
1270 mutt_message (_("Mailbox checkpointed."));
1274 mutt_message (_("%d kept, %d deleted."), msgcount - deleted, deleted);
1278 if (ctx->msgcount == ctx->deleted &&
1279 (ctx->magic == M_MBOX || ctx->magic == M_MMDF) &&
1280 !mutt_is_spool (ctx->path) && !option (OPTSAVEEMPTY)) {
1282 mx_fastclose_mailbox (ctx);
1286 /* if we haven't deleted any messages, we don't need to resort */
1287 /* ... except for certain folder formats which need "unsorted"
1288 * sort order in order to synchronize folders.
1290 * MH and maildir are safe. mbox-style seems to need re-sorting,
1291 * at least with the new threading code.
1293 if (purge || (ctx->magic != M_MAILDIR && ctx->magic != M_MH)) {
1295 /* IMAP does this automatically after handling EXPUNGE */
1296 if (ctx->magic != M_IMAP)
1299 mx_update_tables (ctx, 1);
1300 mutt_sort_headers (ctx, 1); /* rethread from scratch */
1309 /* {maildir,mh}_open_new_message are in mh.c. */
1311 int mbox_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr)
1318 int imap_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr)
1320 char tmp[_POSIX_PATH_MAX];
1323 if ((msg->fp = safe_fopen (tmp, "w")) == NULL)
1325 msg->path = safe_strdup (tmp);
1331 * dest destintation mailbox
1332 * hdr message being copied (required for maildir support, because
1333 * the filename depends on the message flags)
1335 MESSAGE *mx_open_new_message (CONTEXT * dest, HEADER * hdr, int flags)
1338 int (*func) (MESSAGE *, CONTEXT *, HEADER *);
1341 switch (dest->magic) {
1344 func = mbox_open_new_message;
1347 func = maildir_open_new_message;
1350 func = mh_open_new_message;
1354 func = imap_open_new_message;
1360 "mx_open_new_message(): function unimplemented for mailbox type %d.\n",
1365 msg = safe_calloc (1, sizeof (MESSAGE));
1366 msg->magic = dest->magic;
1370 msg->flags.flagged = hdr->flagged;
1371 msg->flags.replied = hdr->replied;
1372 msg->flags.read = hdr->read;
1373 msg->received = hdr->received;
1376 if (msg->received == 0)
1377 time (&msg->received);
1379 if (func (msg, dest, hdr) == 0) {
1380 if (dest->magic == M_MMDF)
1381 fputs (MMDF_SEP, msg->fp);
1383 if ((msg->magic == M_MBOX || msg->magic == M_MMDF) && flags & M_ADD_FROM) {
1385 if (hdr->env->return_path)
1386 p = hdr->env->return_path;
1387 else if (hdr->env->sender)
1388 p = hdr->env->sender;
1393 fprintf (msg->fp, "From %s %s", p ? p->mailbox : NONULL (Username),
1394 ctime (&msg->received));
1403 /* check for new mail */
1404 int mx_check_mailbox (CONTEXT * ctx, int *index_hint, int lock)
1408 #ifdef USE_COMPRESSED
1409 if (ctx->compressinfo)
1410 return mutt_check_mailbox_compressed (ctx);
1417 switch (ctx->magic) {
1422 mutt_block_signals ();
1423 if (mbox_lock_mailbox (ctx, 0, 0) == -1) {
1424 mutt_unblock_signals ();
1429 rc = mbox_check_mailbox (ctx, index_hint);
1432 mutt_unblock_signals ();
1433 mbox_unlock_mailbox (ctx);
1440 return (mh_check_mailbox (ctx, index_hint));
1442 return (maildir_check_mailbox (ctx, index_hint));
1446 return (imap_check_mailbox (ctx, index_hint, 0));
1447 #endif /* USE_IMAP */
1451 return (pop_check_mailbox (ctx, index_hint));
1452 #endif /* USE_POP */
1456 return (nntp_check_mailbox (ctx));
1457 #endif /* USE_NNTP */
1461 dprint (1, (debugfile, "mx_check_mailbox: null or invalid context.\n"));
1465 /* return a stream pointer for a message */
1466 MESSAGE *mx_open_message (CONTEXT * ctx, int msgno)
1470 msg = safe_calloc (1, sizeof (MESSAGE));
1471 switch (msg->magic = ctx->magic) {
1480 HEADER *cur = ctx->hdrs[msgno];
1481 char path[_POSIX_PATH_MAX];
1483 snprintf (path, sizeof (path), "%s/%s", ctx->path, cur->path);
1485 if ((msg->fp = fopen (path, "r")) == NULL && errno == ENOENT &&
1486 ctx->magic == M_MAILDIR)
1487 msg->fp = maildir_open_find_message (ctx->path, cur->path);
1489 if (msg->fp == NULL) {
1491 dprint (1, (debugfile, "mx_open_message: fopen: %s: %s (errno %d).\n",
1492 path, strerror (errno), errno));
1501 if (imap_fetch_message (msg, ctx, msgno) != 0)
1505 #endif /* USE_IMAP */
1510 if (pop_fetch_message (msg, ctx, msgno) != 0)
1514 #endif /* USE_POP */
1519 if (nntp_fetch_message (msg, ctx, msgno) != 0)
1523 #endif /* USE_NNTP */
1528 "mx_open_message(): function not implemented for mailbox type %d.\n",
1536 /* commit a message to a folder */
1538 int mx_commit_message (MESSAGE * msg, CONTEXT * ctx)
1542 if (!(msg->write && ctx->append)) {
1545 "mx_commit_message(): msg->write = %d, ctx->append = %d\n",
1546 msg->write, ctx->append));
1550 switch (msg->magic) {
1553 if (fputs (MMDF_SEP, msg->fp) == EOF)
1560 if (fputc ('\n', msg->fp) == EOF)
1568 if ((r = safe_fclose (&msg->fp)) == 0)
1569 r = imap_append_message (ctx, msg);
1576 r = maildir_commit_message (ctx, msg, NULL);
1582 r = mh_commit_message (ctx, msg, NULL);
1587 if (r == 0 && (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
1588 && (fflush (msg->fp) == EOF || fsync (fileno (msg->fp)) == -1)) {
1589 mutt_perror _("Can't write message");
1597 /* close a pointer to a message */
1598 int mx_close_message (MESSAGE ** msg)
1602 if ((*msg)->magic == M_MH || (*msg)->magic == M_MAILDIR
1604 || (*msg)->magic == M_IMAP
1607 || (*msg)->magic == M_POP
1610 || (*msg)->magic == M_NNTP
1613 r = safe_fclose (&(*msg)->fp);
1619 dprint (1, (debugfile, "mx_close_message (): unlinking %s\n",
1621 unlink ((*msg)->path);
1622 FREE (&(*msg)->path);
1629 void mx_alloc_memory (CONTEXT * ctx)
1632 size_t s = MAX (sizeof (HEADER *), sizeof (int));
1634 if ((ctx->hdrmax + 25) * s < ctx->hdrmax * s) {
1635 mutt_error _("Integer overflow -- can't allocate memory.");
1642 safe_realloc (&ctx->hdrs, sizeof (HEADER *) * (ctx->hdrmax += 25));
1643 safe_realloc (&ctx->v2r, sizeof (int) * ctx->hdrmax);
1646 ctx->hdrs = safe_calloc ((ctx->hdrmax += 25), sizeof (HEADER *));
1647 ctx->v2r = safe_calloc (ctx->hdrmax, sizeof (int));
1649 for (i = ctx->msgcount; i < ctx->hdrmax; i++) {
1650 ctx->hdrs[i] = NULL;
1655 /* this routine is called to update the counts in the context structure for
1656 * the last message header parsed.
1658 void mx_update_context (CONTEXT * ctx, int new_messages)
1663 for (msgno = ctx->msgcount - new_messages; msgno < ctx->msgcount; msgno++) {
1664 h = ctx->hdrs[msgno];
1667 /* NOTE: this _must_ be done before the check for mailcap! */
1668 h->security = crypt_query (h->content);
1671 if (!ctx->pattern) {
1672 ctx->v2r[ctx->vcount] = msgno;
1673 h->virtual = ctx->vcount++;
1679 if (h->env->supersedes) {
1683 ctx->id_hash = mutt_make_id_hash (ctx);
1685 h2 = hash_find (ctx->id_hash, h->env->supersedes);
1687 /* FREE (&h->env->supersedes); should I ? */
1690 if (option (OPTSCORE))
1691 mutt_score_message (ctx, h2, 1);
1695 /* add this message to the hash tables */
1696 if (ctx->id_hash && h->env->message_id)
1697 hash_insert (ctx->id_hash, h->env->message_id, h, 0);
1698 if (ctx->subj_hash && h->env->real_subj)
1699 hash_insert (ctx->subj_hash, h->env->real_subj, h, 1);
1701 if (option (OPTSCORE))
1702 mutt_score_message (ctx, h, 0);
1720 * 1 if the specified mailbox contains 0 messages.
1721 * 0 if the mailbox contains messages
1724 int mx_check_empty (const char *path)
1726 switch (mx_get_magic (path)) {
1729 return mbox_check_empty (path);
1731 return mh_check_empty (path);
1733 return maildir_check_empty (path);