2 * Copyright (C) 1996-2002 Michael R. Elkins <me@mutt.org>
3 * Copyright (C) 1999-2000 Thomas Roessler <roessler@does-not-exist.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
49 #include "mutt_crypt.h"
65 #define mutt_is_spool(s) (mutt_strcmp (Spoolfile, s) == 0)
70 * retry - should retry if unable to lock?
75 static int invoke_dotlock (const char *path, int dummy, int flags, int retry)
77 char cmd[LONG_STRING + _POSIX_PATH_MAX];
78 char f[SHORT_STRING + _POSIX_PATH_MAX];
81 if (flags & DL_FL_RETRY)
82 snprintf (r, sizeof (r), "-r %d ", retry ? MAXLOCKATTEMPT : 0);
84 mutt_quote_filename (f, sizeof (f), path);
86 snprintf (cmd, sizeof (cmd),
89 flags & DL_FL_TRY ? "-t " : "",
90 flags & DL_FL_UNLOCK ? "-u " : "",
91 flags & DL_FL_USEPRIV ? "-p " : "",
92 flags & DL_FL_FORCE ? "-f " : "",
93 flags & DL_FL_UNLINK ? "-d " : "",
94 flags & DL_FL_RETRY ? r : "",
97 return mutt_system (cmd);
102 #define invoke_dotlock dotlock_invoke
106 static int dotlock_file (const char *path, int fd, int retry)
109 int flags = DL_FL_USEPRIV | DL_FL_RETRY;
111 if (retry) retry = 1;
114 if ((r = invoke_dotlock(path, fd, flags, retry)) == DL_EX_EXIST)
116 if (!option (OPTNOCURSES))
118 char msg[LONG_STRING];
120 snprintf(msg, sizeof(msg), _("Lock count exceeded, remove lock for %s?"),
122 if(retry && mutt_yesorno(msg, M_YES) == M_YES)
124 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) == DL_EX_OK ?
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)
176 mutt_perror ("fcntl");
180 if (fstat (fd, &sb) != 0)
186 /* only unlock file if it is unchanged */
187 if (prev_sb.st_size == sb.st_size && ++count >= (timeout?MAXLOCKATTEMPT:0))
190 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)
209 mutt_perror ("flock");
214 if (fstat(fd,&sb) != 0 )
220 /* only unlock file if it is unchanged */
221 if (prev_sb.st_size == sb.st_size && ++count >= (timeout?MAXLOCKATTEMPT:0))
224 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 */
243 /* release any other locks obtained in this routine */
246 lck.l_type = F_UNLCK;
247 fcntl (fd, F_SETLK, &lck);
248 #endif /* USE_FCNTL */
252 #endif /* USE_FLOCK */
260 int mx_unlock_file (const char *path, int fd, int dot)
263 struct flock unlockit = { F_UNLCK, 0, 0, 0 };
265 memset (&unlockit, 0, sizeof (struct flock));
266 unlockit.l_type = F_UNLCK;
267 unlockit.l_whence = SEEK_SET;
268 fcntl (fd, F_SETLK, &unlockit);
277 undotlock_file (path, fd);
283 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)
300 invoke_dotlock (path, fd, DL_FL_UNLINK, 1);
302 if (fstat (fd, &sb) == 0 && sb.st_size == 0)
306 mx_unlock_file (path, fd, 0);
310 /* try to figure out what type of mailbox ``path'' is
320 int mx_is_imap(const char *p)
330 scheme = url_check_scheme (p);
331 if (scheme == U_IMAP || scheme == U_IMAPS)
340 int mx_is_pop (const char *p)
347 scheme = url_check_scheme (p);
348 if (scheme == U_POP || scheme == U_POPS)
356 int mx_is_nntp (const char *p)
363 scheme = url_check_scheme (p);
364 if (scheme == U_NNTP || scheme == U_NNTPS)
371 int mx_get_magic (const char *path)
375 char tmp[_POSIX_PATH_MAX];
381 #endif /* USE_IMAP */
384 if (mx_is_pop (path))
389 if (mx_is_nntp (path))
391 #endif /* USE_NNTP */
393 if (stat (path, &st) == -1)
395 dprint (1, (debugfile, "mx_get_magic(): unable to stat %s: %s (errno %d).\n",
396 path, strerror (errno), errno));
400 if (S_ISDIR (st.st_mode))
402 /* check for maildir-style mailbox */
404 snprintf (tmp, sizeof (tmp), "%s/cur", path);
405 if (stat (tmp, &st) == 0 && S_ISDIR (st.st_mode))
408 /* check for mh-style mailbox */
410 snprintf (tmp, sizeof (tmp), "%s/.mh_sequences", path);
411 if (access (tmp, F_OK) == 0)
414 snprintf (tmp, sizeof (tmp), "%s/.xmhcache", 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/.mew-cache", path);
423 if (access (tmp, F_OK) == 0)
426 snprintf (tmp, sizeof (tmp), "%s/.sylpheed_cache", path);
427 if (access (tmp, F_OK) == 0)
431 * ok, this isn't an mh folder, but mh mode can be used to read
432 * Usenet news from the spool. ;-)
435 snprintf (tmp, sizeof (tmp), "%s/.overview", path);
436 if (access (tmp, F_OK) == 0)
440 else if (st.st_size == 0)
442 /* hard to tell what zero-length files are, so assume the default magic */
443 if (DefaultMagic == M_MBOX || DefaultMagic == M_MMDF)
444 return (DefaultMagic);
448 else if ((f = fopen (path, "r")) != NULL)
451 struct utimbuf times;
454 fgets (tmp, sizeof (tmp), f);
455 if (mutt_strncmp ("From ", tmp, 5) == 0)
457 else if (mutt_strcmp (MMDF_SEP, tmp) == 0)
461 /* need to restore the times here, the file was not really accessed,
462 * only the type was accessed. This is important, because detection
463 * of "new mail" depends on those times set correctly.
465 times.actime = st.st_atime;
466 times.modtime = st.st_mtime;
467 utime (path, ×);
472 dprint (1, (debugfile, "mx_get_magic(): unable to open file %s for reading.\n",
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)
521 if(mx_is_imap(ctx->path))
522 return imap_open_mailbox_append (ctx);
526 if(stat(ctx->path, &sb) == 0)
528 ctx->magic = mx_get_magic (ctx->path);
533 mutt_error (_("%s is not a mailbox."), ctx->path);
539 else if (errno == ENOENT)
541 ctx->magic = DefaultMagic;
543 if (ctx->magic == M_MH || ctx->magic == M_MAILDIR)
545 char tmp[_POSIX_PATH_MAX];
547 if (mkdir (ctx->path, S_IRWXU))
549 mutt_perror (ctx->path);
553 if (ctx->magic == M_MAILDIR)
555 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
556 if (mkdir (tmp, S_IRWXU))
563 snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
564 if (mkdir (tmp, S_IRWXU))
567 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
572 snprintf (tmp, sizeof (tmp), "%s/tmp", ctx->path);
573 if (mkdir (tmp, S_IRWXU))
576 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
578 snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
588 snprintf (tmp, sizeof (tmp), "%s/.mh_sequences", ctx->path);
589 if ((i = creat (tmp, S_IRWXU)) == -1)
601 mutt_perror (ctx->path);
609 if ((ctx->fp = safe_fopen (ctx->path, flags & M_NEWFOLDER ? "w" : "a")) == NULL ||
610 mbox_lock_mailbox (ctx, 1, 1) != 0)
613 mutt_perror (ctx->path);
616 mutt_error (_("Couldn't lock %s\n"), ctx->path);
617 safe_fclose (&ctx->fp);
621 fseek (ctx->fp, 0, 2);
637 * open a mailbox and parse it
640 * flags M_NOSORT do not sort mailbox
641 * M_APPEND open mailbox for appending
642 * M_READONLY open mailbox in read-only mode
643 * M_QUIET only print error messages
644 * ctx if non-null, context struct to use
646 CONTEXT *mx_open_mailbox (const char *path, int flags, CONTEXT *pctx)
652 ctx = safe_malloc (sizeof (CONTEXT));
653 memset (ctx, 0, sizeof (CONTEXT));
654 ctx->path = safe_strdup (path);
656 ctx->msgnotreadyet = -1;
661 if (flags & M_READONLY)
664 if (flags & (M_APPEND|M_NEWFOLDER))
666 if (mx_open_mailbox_append (ctx, flags) != 0)
668 mx_fastclose_mailbox (ctx);
676 ctx->magic = mx_get_magic (path);
679 mutt_error (_("%s is not a mailbox."), path);
686 mx_fastclose_mailbox (ctx);
692 /* if the user has a `push' command in their .muttrc, or in a folder-hook,
693 * it will cause the progress messages not to be displayed because
694 * mutt_refresh() will think we are in the middle of a macro. so set a
695 * flag to indicate that we should really refresh the screen.
697 set_option (OPTFORCEREFRESH);
700 mutt_message (_("Reading %s..."), ctx->path);
705 rc = mh_read_dir (ctx, NULL);
709 rc = maildir_read_dir (ctx);
714 rc = mbox_open_mailbox (ctx);
719 rc = imap_open_mailbox (ctx);
721 #endif /* USE_IMAP */
725 rc = pop_open_mailbox (ctx);
731 rc = nntp_open_mailbox (ctx);
733 #endif /* USE_NNTP */
742 if ((flags & M_NOSORT) == 0)
744 /* avoid unnecessary work since the mailbox is completely unthreaded
746 unset_option (OPTSORTSUBTHREADS);
747 unset_option (OPTNEEDRESCORE);
748 mutt_sort_headers (ctx, 1);
755 mx_fastclose_mailbox (ctx);
760 unset_option (OPTFORCEREFRESH);
764 /* free up memory associated with the mailbox context */
765 void mx_fastclose_mailbox (CONTEXT *ctx)
773 if (ctx->magic == M_IMAP)
774 imap_close_mailbox (ctx);
775 #endif /* USE_IMAP */
777 if (ctx->magic == M_POP)
778 pop_close_mailbox (ctx);
781 if (ctx->magic == M_NNTP)
782 nntp_fastclose_mailbox (ctx);
783 #endif /* USE_NNTP */
785 hash_destroy (&ctx->subj_hash, NULL);
787 hash_destroy (&ctx->id_hash, NULL);
788 mutt_clear_threads (ctx);
789 for (i = 0; i < ctx->msgcount; i++)
790 mutt_free_header (&ctx->hdrs[i]);
794 FREE (&ctx->pattern);
795 if (ctx->limit_pattern)
796 mutt_pattern_free (&ctx->limit_pattern);
797 safe_fclose (&ctx->fp);
798 memset (ctx, 0, sizeof (CONTEXT));
801 /* save changes to disk */
802 static int sync_mailbox (CONTEXT *ctx, int *index_hint)
810 mutt_message (_("Writing %s..."), ctx->path);
816 rc = mbox_sync_mailbox (ctx, index_hint);
818 tmp = mutt_find_mailbox (ctx->path);
824 rc = mh_sync_mailbox (ctx, index_hint);
829 /* extra argument means EXPUNGE */
830 rc = imap_sync_mailbox (ctx, 1, index_hint);
832 #endif /* USE_IMAP */
836 rc = pop_sync_mailbox (ctx, index_hint);
842 rc = nntp_sync_mailbox (ctx);
844 #endif /* USE_NNTP */
848 if (!ctx->quiet && !ctx->shutup && rc == -1)
849 mutt_error ( _("Could not synchronize mailbox %s!"), ctx->path);
853 if (tmp && tmp->new == 0)
854 mutt_update_mailbox (tmp);
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)
877 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)
887 for (i = 0 ; i < ctx->msgcount ; i++)
888 if (ctx->hdrs[i]->deleted && !ctx->hdrs[i]->appended
889 && !ctx->hdrs[i]->purged
890 && mutt_append_message (ctx_trash, ctx, ctx->hdrs[i], 0, 0) == -1)
892 mx_close_mailbox (ctx_trash, NULL);
896 mx_close_mailbox (ctx_trash, NULL);
900 mutt_error _("Can't open trash folder");
907 /* save changes and close mailbox */
908 int mx_close_mailbox (CONTEXT *ctx, int *index_hint)
910 int i, move_messages = 0, purge = 1, read_msgs = 0;
914 char mbox[_POSIX_PATH_MAX];
915 char buf[SHORT_STRING];
922 if (ctx->magic == M_NNTP)
926 ret = nntp_close_mailbox (ctx);
927 mx_fastclose_mailbox (ctx);
931 if (ctx->readonly || ctx->dontwrite)
933 /* mailbox is readonly or we don't want to write */
934 mx_fastclose_mailbox (ctx);
940 /* mailbox was opened in write-mode */
941 if (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
942 mbox_close_mailbox (ctx);
944 mx_fastclose_mailbox (ctx);
948 for (i = 0; i < ctx->msgcount; i++)
950 if (!ctx->hdrs[i]->deleted && ctx->hdrs[i]->read
951 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
955 if (read_msgs && quadoption (OPT_MOVE) != M_NO)
959 if ((p = mutt_find_hook (M_MBOXHOOK, ctx->path)))
962 strfcpy (mbox, p, sizeof (mbox));
966 strfcpy (mbox, NONULL(Inbox), sizeof (mbox));
967 isSpool = mutt_is_spool (ctx->path) && !mutt_is_spool (mbox);
969 mutt_expand_path (mbox, sizeof (mbox));
973 snprintf (buf, sizeof (buf), _("Move read messages to %s?"), mbox);
974 if ((move_messages = query_quadoption (OPT_MOVE, buf)) == -1)
983 * There is no point in asking whether or not to purge if we are
984 * just marking messages as "trash".
986 if (ctx->deleted && !(ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))
988 snprintf (buf, sizeof (buf), ctx->deleted == 1
989 ? _("Purge %d deleted message?") : _("Purge %d deleted messages?"),
991 if ((purge = query_quadoption (OPT_DELETE, buf)) < 0)
999 /* IMAP servers manage the OLD flag themselves */
1000 if (ctx->magic != M_IMAP)
1002 if (option (OPTMARKOLD))
1004 for (i = 0; i < ctx->msgcount; i++)
1006 if (!ctx->hdrs[i]->deleted && !ctx->hdrs[i]->old)
1007 mutt_set_flag (ctx, ctx->hdrs[i], M_OLD, 1);
1013 mutt_message (_("Moving read messages to %s..."), mbox);
1016 /* try to use server-side copy first */
1019 if (ctx->magic == M_IMAP && mx_is_imap (mbox))
1021 /* tag messages for moving, and clear old tags, if any */
1022 for (i = 0; i < ctx->msgcount; i++)
1023 if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted
1024 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
1025 ctx->hdrs[i]->tagged = 1;
1027 ctx->hdrs[i]->tagged = 0;
1029 i = imap_copy_messages (ctx, NULL, mbox, 1);
1032 if (i == 0) /* success */
1033 mutt_clear_error ();
1034 else if (i == -1) /* horrible error, bail */
1039 else /* use regular append-copy mode */
1042 if (mx_open_mailbox (mbox, M_APPEND, &f) == NULL)
1048 for (i = 0; i < ctx->msgcount; i++)
1050 if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted
1051 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
1053 if (mutt_append_message (&f, ctx, ctx->hdrs[i], 0, CH_UPDATE_LEN) == 0)
1055 mutt_set_flag (ctx, ctx->hdrs[i], M_DELETE, 1);
1056 mutt_set_flag (ctx, ctx->hdrs[i], M_APPENDED, 1);
1060 mx_close_mailbox (&f, NULL);
1067 mx_close_mailbox (&f, NULL);
1071 else if (!ctx->changed && ctx->deleted == 0)
1073 mutt_message _("Mailbox is unchanged.");
1074 mx_fastclose_mailbox (ctx);
1078 /* copy mails to the trash before expunging */
1079 if (purge && ctx->deleted)
1080 if (trash_append (ctx) != 0)
1087 /* allow IMAP to preserve the deleted flag across sessions */
1088 if (ctx->magic == M_IMAP)
1090 if ((check = imap_sync_mailbox (ctx, purge, index_hint)) != 0)
1101 for (i = 0; i < ctx->msgcount; i++)
1102 ctx->hdrs[i]->deleted = 0;
1106 if (ctx->changed || ctx->deleted)
1108 if ((check = sync_mailbox (ctx, index_hint)) != 0)
1117 mutt_message (_("%d kept, %d moved, %d deleted."),
1118 ctx->msgcount - ctx->deleted, read_msgs, ctx->deleted);
1120 mutt_message (_("%d kept, %d deleted."),
1121 ctx->msgcount - ctx->deleted, ctx->deleted);
1123 if (ctx->msgcount == ctx->deleted &&
1124 (ctx->magic == M_MMDF || ctx->magic == M_MBOX) &&
1125 !mutt_is_spool(ctx->path) && !option (OPTSAVEEMPTY))
1126 mx_unlink_empty (ctx->path);
1128 mx_fastclose_mailbox (ctx);
1134 /* update a Context structure's internal tables. */
1136 void mx_update_tables(CONTEXT *ctx, int committing)
1140 /* update memory to reflect the new state of the mailbox */
1149 #define this_body ctx->hdrs[j]->content
1150 for (i = 0, j = 0; i < ctx->msgcount; i++)
1152 if ((committing && (!ctx->hdrs[i]->deleted ||
1153 (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))) ||
1154 (!committing && ctx->hdrs[i]->active))
1158 ctx->hdrs[j] = ctx->hdrs[i];
1159 ctx->hdrs[i] = NULL;
1161 ctx->hdrs[j]->msgno = j;
1162 if (ctx->hdrs[j]->virtual != -1)
1164 ctx->v2r[ctx->vcount] = j;
1165 ctx->hdrs[j]->virtual = ctx->vcount++;
1166 ctx->vsize += this_body->length + this_body->offset -
1167 this_body->hdr_offset;
1171 ctx->hdrs[j]->changed = 0;
1172 else if (ctx->hdrs[j]->changed)
1175 if (!committing || (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))
1177 if (ctx->hdrs[j]->deleted)
1181 if (ctx->hdrs[j]->tagged)
1183 if (ctx->hdrs[j]->flagged)
1185 if (!ctx->hdrs[j]->read)
1188 if (!ctx->hdrs[j]->old)
1196 if (ctx->magic == M_MH || ctx->magic == M_MAILDIR)
1197 ctx->size -= (ctx->hdrs[i]->content->length +
1198 ctx->hdrs[i]->content->offset -
1199 ctx->hdrs[i]->content->hdr_offset);
1200 /* remove message from the hash tables */
1201 if (ctx->subj_hash && ctx->hdrs[i]->env->real_subj)
1202 hash_delete (ctx->subj_hash, ctx->hdrs[i]->env->real_subj, ctx->hdrs[i], NULL);
1203 if (ctx->id_hash && ctx->hdrs[i]->env->message_id)
1204 hash_delete (ctx->id_hash, ctx->hdrs[i]->env->message_id, ctx->hdrs[i], NULL);
1205 mutt_free_header (&ctx->hdrs[i]);
1213 /* save changes to mailbox
1219 int mx_sync_mailbox (CONTEXT *ctx, int *index_hint)
1223 int msgcount, deleted;
1227 char buf[STRING], tmp[STRING];
1228 if (km_expand_key (buf, sizeof(buf),
1229 km_find_func (MENU_MAIN, OP_TOGGLE_WRITE)))
1230 snprintf (tmp, sizeof(tmp), _(" Press '%s' to toggle write"), buf);
1232 strfcpy (tmp, _("Use 'toggle-write' to re-enable write!"), sizeof(tmp));
1234 mutt_error (_("Mailbox is marked unwritable. %s"), tmp);
1237 else if (ctx->readonly)
1239 mutt_error _("Mailbox is read-only.");
1243 if (!ctx->changed && !ctx->deleted)
1245 mutt_message _("Mailbox is unchanged.");
1251 char buf[SHORT_STRING];
1253 snprintf (buf, sizeof (buf), ctx->deleted == 1
1254 ? _("Purge %d deleted message?") : _("Purge %d deleted messages?"),
1256 if ((purge = query_quadoption (OPT_DELETE, buf)) < 0)
1258 else if (purge == M_NO)
1261 return 0; /* nothing to do! */
1263 /* let IMAP servers hold on to D flags */
1264 if (ctx->magic != M_IMAP)
1267 for (i = 0 ; i < ctx->msgcount ; i++)
1268 ctx->hdrs[i]->deleted = 0;
1274 /* really only for IMAP - imap_sync_mailbox results in a call to
1275 * mx_update_tables, so ctx->deleted is 0 when it comes back */
1276 msgcount = ctx->msgcount;
1277 deleted = ctx->deleted;
1279 if (purge && ctx->deleted)
1281 if (trash_append (ctx) == -1)
1286 if (ctx->magic == M_IMAP)
1287 rc = imap_sync_mailbox (ctx, purge, index_hint);
1290 rc = sync_mailbox (ctx, index_hint);
1294 if (ctx->magic == M_IMAP && !purge)
1295 mutt_message _("Mailbox checkpointed.");
1298 mutt_message (_("%d kept, %d deleted."), msgcount - deleted,
1303 if (ctx->msgcount == ctx->deleted &&
1304 (ctx->magic == M_MBOX || ctx->magic == M_MMDF) &&
1305 !mutt_is_spool (ctx->path) && !option (OPTSAVEEMPTY))
1308 mx_fastclose_mailbox (ctx);
1312 /* if we haven't deleted any messages, we don't need to resort */
1313 /* ... except for certain folder formats which need "unsorted"
1314 * sort order in order to synchronize folders.
1316 * MH and maildir are safe. mbox-style seems to need re-sorting,
1317 * at least with the new threading code.
1319 if (purge || (ctx->magic != M_MAILDIR && ctx->magic != M_MH))
1322 /* IMAP does this automatically after handling EXPUNGE */
1323 if (ctx->magic != M_IMAP)
1326 mx_update_tables (ctx, 1);
1327 mutt_sort_headers (ctx, 1); /* rethread from scratch */
1336 /* {maildir,mh}_open_new_message are in mh.c. */
1338 int mbox_open_new_message (MESSAGE *msg, CONTEXT *dest, HEADER *hdr)
1345 int imap_open_new_message (MESSAGE *msg, CONTEXT *dest, HEADER *hdr)
1347 char tmp[_POSIX_PATH_MAX];
1350 if ((msg->fp = safe_fopen (tmp, "w")) == NULL)
1352 msg->path = safe_strdup(tmp);
1358 * dest destintation mailbox
1359 * hdr message being copied (required for maildir support, because
1360 * the filename depends on the message flags)
1362 MESSAGE *mx_open_new_message (CONTEXT *dest, HEADER *hdr, int flags)
1365 int (*func) (MESSAGE *, CONTEXT *, HEADER *);
1368 switch (dest->magic)
1372 func = mbox_open_new_message;
1375 func = maildir_open_new_message;
1378 func = mh_open_new_message;
1382 func = imap_open_new_message;
1386 dprint (1, (debugfile, "mx_open_new_message(): function unimplemented for mailbox type %d.\n",
1391 msg = safe_calloc (1, sizeof (MESSAGE));
1392 msg->magic = dest->magic;
1397 msg->flags.flagged = hdr->flagged;
1398 msg->flags.replied = hdr->replied;
1399 msg->flags.read = hdr->read;
1400 msg->received = hdr->received;
1403 if(msg->received == 0)
1404 time(&msg->received);
1406 if (func (msg, dest, hdr) == 0)
1408 if (dest->magic == M_MMDF)
1409 fputs (MMDF_SEP, msg->fp);
1411 if ((msg->magic == M_MBOX || msg->magic == M_MMDF) &&
1416 if (hdr->env->return_path)
1417 p = hdr->env->return_path;
1418 else if (hdr->env->sender)
1419 p = hdr->env->sender;
1424 fprintf (msg->fp, "From %s %s", p ? p->mailbox : NONULL(Username), ctime (&msg->received));
1433 /* check for new mail */
1434 int mx_check_mailbox (CONTEXT *ctx, int *index_hint, int lock)
1440 if (ctx->locked) lock = 0;
1449 mutt_block_signals ();
1450 if (mbox_lock_mailbox (ctx, 0, 0) == -1)
1452 mutt_unblock_signals ();
1457 rc = mbox_check_mailbox (ctx, index_hint);
1461 mutt_unblock_signals ();
1462 mbox_unlock_mailbox (ctx);
1469 return (mh_check_mailbox (ctx, index_hint));
1471 return (maildir_check_mailbox (ctx, index_hint));
1475 return (imap_check_mailbox (ctx, index_hint, 0));
1476 #endif /* USE_IMAP */
1480 return (pop_check_mailbox (ctx, index_hint));
1481 #endif /* USE_POP */
1485 return (nntp_check_mailbox (ctx));
1486 #endif /* USE_NNTP */
1490 dprint (1, (debugfile, "mx_check_mailbox: null or invalid context.\n"));
1494 /* return a stream pointer for a message */
1495 MESSAGE *mx_open_message (CONTEXT *ctx, int msgno)
1499 msg = safe_calloc (1, sizeof (MESSAGE));
1500 switch (msg->magic = ctx->magic)
1510 HEADER *cur = ctx->hdrs[msgno];
1511 char path[_POSIX_PATH_MAX];
1513 snprintf (path, sizeof (path), "%s/%s", ctx->path, cur->path);
1515 if ((msg->fp = fopen (path, "r")) == NULL && errno == ENOENT &&
1516 ctx->magic == M_MAILDIR)
1517 msg->fp = maildir_open_find_message (ctx->path, cur->path);
1519 if (msg->fp == NULL)
1522 dprint (1, (debugfile, "mx_open_message: fopen: %s: %s (errno %d).\n",
1523 path, strerror (errno), errno));
1532 if (imap_fetch_message (msg, ctx, msgno) != 0)
1536 #endif /* USE_IMAP */
1541 if (pop_fetch_message (msg, ctx, msgno) != 0)
1545 #endif /* USE_POP */
1550 if (nntp_fetch_message (msg, ctx, msgno) != 0)
1554 #endif /* USE_NNTP */
1557 dprint (1, (debugfile, "mx_open_message(): function not implemented for mailbox type %d.\n", ctx->magic));
1564 /* commit a message to a folder */
1566 int mx_commit_message (MESSAGE *msg, CONTEXT *ctx)
1570 if (!(msg->write && ctx->append))
1572 dprint (1, (debugfile, "mx_commit_message(): msg->write = %d, ctx->append = %d\n",
1573 msg->write, ctx->append));
1581 if (fputs (MMDF_SEP, msg->fp) == EOF)
1588 if (fputc ('\n', msg->fp) == EOF)
1596 if ((r = safe_fclose (&msg->fp)) == 0)
1597 r = imap_append_message (ctx, msg);
1604 r = maildir_commit_message (ctx, msg, NULL);
1610 r = mh_commit_message (ctx, msg, NULL);
1615 if (r == 0 && (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
1616 && (fflush (msg->fp) == EOF || fsync (fileno (msg->fp)) == -1))
1618 mutt_perror _("Can't write message");
1625 /* close a pointer to a message */
1626 int mx_close_message (MESSAGE **msg)
1630 if ((*msg)->magic == M_MH || (*msg)->magic == M_MAILDIR
1632 || (*msg)->magic == M_IMAP
1635 || (*msg)->magic == M_POP
1638 || (*msg)->magic == M_NNTP
1642 r = safe_fclose (&(*msg)->fp);
1649 dprint (1, (debugfile, "mx_close_message (): unlinking %s\n",
1651 unlink ((*msg)->path);
1652 FREE (&(*msg)->path);
1659 void mx_alloc_memory (CONTEXT *ctx)
1662 size_t s = MAX (sizeof (HEADER *), sizeof (int));
1664 if ((ctx->hdrmax + 25) * s < ctx->hdrmax * s)
1666 mutt_error _("Integer overflow -- can't allocate memory.");
1673 safe_realloc (&ctx->hdrs, sizeof (HEADER *) * (ctx->hdrmax += 25));
1674 safe_realloc (&ctx->v2r, sizeof (int) * ctx->hdrmax);
1678 ctx->hdrs = safe_calloc ((ctx->hdrmax += 25), sizeof (HEADER *));
1679 ctx->v2r = safe_calloc (ctx->hdrmax, sizeof (int));
1681 for (i = ctx->msgcount ; i < ctx->hdrmax ; i++)
1683 ctx->hdrs[i] = NULL;
1688 /* this routine is called to update the counts in the context structure for
1689 * the last message header parsed.
1691 void mx_update_context (CONTEXT *ctx, int new_messages)
1696 for (msgno = ctx->msgcount - new_messages; msgno < ctx->msgcount; msgno++)
1698 h = ctx->hdrs[msgno];
1702 /* NOTE: this _must_ be done before the check for mailcap! */
1703 h->security = crypt_query (h->content);
1708 ctx->v2r[ctx->vcount] = msgno;
1709 h->virtual = ctx->vcount++;
1715 if (h->env->supersedes)
1720 ctx->id_hash = mutt_make_id_hash (ctx);
1722 h2 = hash_find (ctx->id_hash, h->env->supersedes);
1724 /* FREE (&h->env->supersedes); should I ? */
1728 if (option (OPTSCORE))
1729 mutt_score_message (ctx, h2, 1);
1733 /* add this message to the hash tables */
1734 if (ctx->id_hash && h->env->message_id)
1735 hash_insert (ctx->id_hash, h->env->message_id, h, 0);
1736 if (ctx->subj_hash && h->env->real_subj)
1737 hash_insert (ctx->subj_hash, h->env->real_subj, h, 1);
1739 if (option (OPTSCORE))
1740 mutt_score_message (ctx, h, 0);
1759 * 1 if the specified mailbox contains 0 messages.
1760 * 0 if the mailbox contains messages
1763 int mx_check_empty (const char *path)
1765 switch (mx_get_magic (path))
1769 return mbox_check_empty (path);
1771 return mh_check_empty (path);
1773 return maildir_check_empty (path);