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.
57 #include "mutt_crypt.h"
73 #define mutt_is_spool(s) (mutt_strcmp (Spoolfile, s) == 0)
78 * retry - should retry if unable to lock?
83 static int invoke_dotlock (const char *path, int dummy, int flags, int retry)
85 char cmd[LONG_STRING + _POSIX_PATH_MAX];
86 char f[SHORT_STRING + _POSIX_PATH_MAX];
89 if (flags & DL_FL_RETRY)
90 snprintf (r, sizeof (r), "-r %d ", retry ? MAXLOCKATTEMPT : 0);
92 mutt_quote_filename (f, sizeof (f), path);
94 snprintf (cmd, sizeof (cmd),
97 flags & DL_FL_TRY ? "-t " : "",
98 flags & DL_FL_UNLOCK ? "-u " : "",
99 flags & DL_FL_USEPRIV ? "-p " : "",
100 flags & DL_FL_FORCE ? "-f " : "",
101 flags & DL_FL_UNLINK ? "-d " : "",
102 flags & DL_FL_RETRY ? r : "", f);
104 return mutt_system (cmd);
109 #define invoke_dotlock dotlock_invoke
113 static int dotlock_file (const char *path, int fd, int retry)
116 int flags = DL_FL_USEPRIV | DL_FL_RETRY;
122 if ((r = invoke_dotlock (path, fd, flags, retry)) == DL_EX_EXIST) {
123 if (!option (OPTNOCURSES)) {
124 char msg[LONG_STRING];
126 snprintf (msg, sizeof (msg),
127 _("Lock count exceeded, remove lock for %s?"), path);
128 if (retry && mutt_yesorno (msg, M_YES) == M_YES) {
129 flags |= DL_FL_FORCE;
136 mutt_error (_("Can't dotlock %s.\n"), path);
139 return (r == DL_EX_OK ? 0 : -1);
142 static int undotlock_file (const char *path, int fd)
144 return (invoke_dotlock (path, fd, DL_FL_USEPRIV | DL_FL_UNLOCK, 0) ==
148 #endif /* USE_DOTLOCK */
151 * excl if excl != 0, request an exclusive lock
152 * dot if dot != 0, try to dotlock the file
153 * timeout should retry locking?
155 int mx_lock_file (const char *path, int fd, int excl, int dot, int timeout)
157 #if defined (USE_FCNTL) || defined (USE_FLOCK)
168 memset (&lck, 0, sizeof (struct flock));
169 lck.l_type = excl ? F_WRLCK : F_RDLCK;
170 lck.l_whence = SEEK_SET;
174 while (fcntl (fd, F_SETLK, &lck) == -1) {
177 dprint (1, (debugfile, "mx_lock_file(): fcntl errno %d.\n", errno));
178 if (errno != EAGAIN && errno != EACCES) {
179 mutt_perror ("fcntl");
183 if (fstat (fd, &sb) != 0)
189 /* only unlock file if it is unchanged */
190 if (prev_sb.st_size == sb.st_size
191 && ++count >= (timeout ? MAXLOCKATTEMPT : 0)) {
193 mutt_error _("Timeout exceeded while attempting fcntl lock!");
200 mutt_message (_("Waiting for fcntl lock... %d"), ++attempt);
203 #endif /* USE_FCNTL */
208 while (flock (fd, (excl ? LOCK_EX : LOCK_SH) | LOCK_NB) == -1) {
211 if (errno != EWOULDBLOCK) {
212 mutt_perror ("flock");
217 if (fstat (fd, &sb) != 0)
223 /* only unlock file if it is unchanged */
224 if (prev_sb.st_size == sb.st_size
225 && ++count >= (timeout ? MAXLOCKATTEMPT : 0)) {
227 mutt_error _("Timeout exceeded while attempting flock lock!");
235 mutt_message (_("Waiting for flock attempt... %d"), ++attempt);
238 #endif /* USE_FLOCK */
242 r = dotlock_file (path, fd, timeout);
243 #endif /* USE_DOTLOCK */
246 /* release any other locks obtained in this routine */
249 lck.l_type = F_UNLCK;
250 fcntl (fd, F_SETLK, &lck);
251 #endif /* USE_FCNTL */
255 #endif /* USE_FLOCK */
263 int mx_unlock_file (const char *path, int fd, int dot)
266 struct flock unlockit = { F_UNLCK, 0, 0, 0 };
268 memset (&unlockit, 0, sizeof (struct flock));
269 unlockit.l_type = F_UNLCK;
270 unlockit.l_whence = SEEK_SET;
271 fcntl (fd, F_SETLK, &unlockit);
280 undotlock_file (path, fd);
286 void mx_unlink_empty (const char *path)
294 if ((fd = open (path, O_RDWR)) == -1)
297 if (mx_lock_file (path, fd, 1, 0, 1) == -1) {
303 invoke_dotlock (path, fd, DL_FL_UNLINK, 1);
305 if (fstat (fd, &sb) == 0 && sb.st_size == 0)
309 mx_unlock_file (path, fd, 0);
313 /* try to figure out what type of mailbox ``path'' is
323 int mx_is_imap (const char *p)
333 scheme = url_check_scheme (p);
334 if (scheme == U_IMAP || scheme == U_IMAPS)
343 int mx_is_pop (const char *p)
350 scheme = url_check_scheme (p);
351 if (scheme == U_POP || scheme == U_POPS)
359 int mx_is_nntp (const char *p)
366 scheme = url_check_scheme (p);
367 if (scheme == U_NNTP || scheme == U_NNTPS)
374 int mx_get_magic (const char *path)
378 char tmp[_POSIX_PATH_MAX];
382 if (mx_is_imap (path))
384 #endif /* USE_IMAP */
387 if (mx_is_pop (path))
392 if (mx_is_nntp (path))
394 #endif /* USE_NNTP */
396 if (stat (path, &st) == -1) {
398 (debugfile, "mx_get_magic(): unable to stat %s: %s (errno %d).\n",
399 path, strerror (errno), errno));
403 if (S_ISDIR (st.st_mode)) {
404 /* check for maildir-style mailbox */
406 snprintf (tmp, sizeof (tmp), "%s/cur", path);
407 if (stat (tmp, &st) == 0 && S_ISDIR (st.st_mode))
410 /* check for mh-style mailbox */
412 snprintf (tmp, sizeof (tmp), "%s/.mh_sequences", path);
413 if (access (tmp, F_OK) == 0)
416 snprintf (tmp, sizeof (tmp), "%s/.xmhcache", path);
417 if (access (tmp, F_OK) == 0)
420 snprintf (tmp, sizeof (tmp), "%s/.mew_cache", path);
421 if (access (tmp, F_OK) == 0)
424 snprintf (tmp, sizeof (tmp), "%s/.mew-cache", path);
425 if (access (tmp, F_OK) == 0)
428 snprintf (tmp, sizeof (tmp), "%s/.sylpheed_cache", path);
429 if (access (tmp, F_OK) == 0)
433 * ok, this isn't an mh folder, but mh mode can be used to read
434 * Usenet news from the spool. ;-)
437 snprintf (tmp, sizeof (tmp), "%s/.overview", path);
438 if (access (tmp, F_OK) == 0)
442 else if (st.st_size == 0) {
443 /* hard to tell what zero-length files are, so assume the default magic */
444 if (DefaultMagic == M_MBOX || DefaultMagic == M_MMDF)
445 return (DefaultMagic);
449 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, ×);
473 "mx_get_magic(): unable to open file %s for reading.\n", path));
478 #ifdef USE_COMPRESSED
479 if (magic == 0 && mutt_can_read_compressed (path))
487 * set DefaultMagic to the given value
489 int mx_set_magic (const char *s)
491 if (ascii_strcasecmp (s, "mbox") == 0)
492 DefaultMagic = M_MBOX;
493 else if (ascii_strcasecmp (s, "mmdf") == 0)
494 DefaultMagic = M_MMDF;
495 else if (ascii_strcasecmp (s, "mh") == 0)
497 else if (ascii_strcasecmp (s, "maildir") == 0)
498 DefaultMagic = M_MAILDIR;
505 /* mx_access: Wrapper for access, checks permissions on a given mailbox.
506 * We may be interested in using ACL-style flags at some point, currently
507 * we use the normal access() flags. */
508 int mx_access (const char *path, int flags)
511 if (mx_is_imap (path))
512 return imap_access (path, flags);
515 return access (path, flags);
518 static int mx_open_mailbox_append (CONTEXT * ctx, int flags)
522 #ifdef USE_COMPRESSED
523 /* special case for appending to compressed folders -
524 * even if we can not open them for reading */
525 if (mutt_can_append_compressed (ctx->path))
526 mutt_open_append_compressed (ctx);
533 if (mx_is_imap (ctx->path))
534 return imap_open_mailbox_append (ctx);
538 if (stat (ctx->path, &sb) == 0) {
539 ctx->magic = mx_get_magic (ctx->path);
541 switch (ctx->magic) {
543 mutt_error (_("%s is not a mailbox."), ctx->path);
549 else if (errno == ENOENT) {
550 ctx->magic = DefaultMagic;
552 if (ctx->magic == M_MH || ctx->magic == M_MAILDIR) {
553 char tmp[_POSIX_PATH_MAX];
555 if (mkdir (ctx->path, S_IRWXU)) {
556 mutt_perror (ctx->path);
560 if (ctx->magic == M_MAILDIR) {
561 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
562 if (mkdir (tmp, S_IRWXU)) {
568 snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
569 if (mkdir (tmp, S_IRWXU)) {
571 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
576 snprintf (tmp, sizeof (tmp), "%s/tmp", ctx->path);
577 if (mkdir (tmp, S_IRWXU)) {
579 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
581 snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
590 snprintf (tmp, sizeof (tmp), "%s/.mh_sequences", ctx->path);
591 if ((i = creat (tmp, S_IRWXU)) == -1) {
601 mutt_perror (ctx->path);
605 switch (ctx->magic) {
609 safe_fopen (ctx->path, flags & M_NEWFOLDER ? "w" : "a")) == NULL
610 || mbox_lock_mailbox (ctx, 1, 1) != 0) {
612 mutt_perror (ctx->path);
614 mutt_error (_("Couldn't lock %s\n"), ctx->path);
615 safe_fclose (&ctx->fp);
619 fseek (ctx->fp, 0, 2);
635 * open a mailbox and parse it
638 * flags M_NOSORT do not sort mailbox
639 * M_APPEND open mailbox for appending
640 * M_READONLY open mailbox in read-only mode
641 * M_QUIET only print error messages
642 * ctx if non-null, context struct to use
644 CONTEXT *mx_open_mailbox (const char *path, int flags, CONTEXT * pctx)
650 ctx = safe_malloc (sizeof (CONTEXT));
651 memset (ctx, 0, sizeof (CONTEXT));
652 ctx->path = safe_strdup (path);
654 ctx->msgnotreadyet = -1;
659 if (flags & M_READONLY)
662 if (flags & (M_APPEND | M_NEWFOLDER)) {
663 if (mx_open_mailbox_append (ctx, flags) != 0) {
664 mx_fastclose_mailbox (ctx);
672 ctx->magic = mx_get_magic (path);
674 #ifdef USE_COMPRESSED
675 if (ctx->magic == M_COMPRESSED)
676 mutt_open_read_compressed (ctx);
680 mutt_error (_("%s is not a mailbox."), path);
682 if (ctx->magic == -1)
685 if (ctx->magic <= 0) {
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);
702 switch (ctx->magic) {
704 rc = mh_read_dir (ctx, NULL);
708 rc = maildir_read_dir (ctx);
713 rc = mbox_open_mailbox (ctx);
718 rc = imap_open_mailbox (ctx);
720 #endif /* USE_IMAP */
724 rc = pop_open_mailbox (ctx);
730 rc = nntp_open_mailbox (ctx);
732 #endif /* USE_NNTP */
740 if ((flags & M_NOSORT) == 0) {
741 /* avoid unnecessary work since the mailbox is completely unthreaded
743 unset_option (OPTSORTSUBTHREADS);
744 unset_option (OPTNEEDRESCORE);
745 mutt_sort_headers (ctx, 1);
751 mx_fastclose_mailbox (ctx);
756 unset_option (OPTFORCEREFRESH);
760 /* free up memory associated with the mailbox context */
761 void mx_fastclose_mailbox (CONTEXT * ctx)
769 if (ctx->magic == M_IMAP)
770 imap_close_mailbox (ctx);
771 #endif /* USE_IMAP */
773 if (ctx->magic == M_POP)
774 pop_close_mailbox (ctx);
777 if (ctx->magic == M_NNTP)
778 nntp_fastclose_mailbox (ctx);
779 #endif /* USE_NNTP */
781 hash_destroy (&ctx->subj_hash, NULL);
783 hash_destroy (&ctx->id_hash, NULL);
784 mutt_clear_threads (ctx);
785 for (i = 0; i < ctx->msgcount; i++)
786 mutt_free_header (&ctx->hdrs[i]);
789 #ifdef USE_COMPRESSED
790 if (ctx->compressinfo)
791 mutt_fast_close_compressed (ctx);
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);
812 switch (ctx->magic) {
815 rc = mbox_sync_mailbox (ctx, index_hint);
817 tmp = mutt_find_mailbox (ctx->path);
823 rc = mh_sync_mailbox (ctx, index_hint);
828 /* extra argument means EXPUNGE */
829 rc = imap_sync_mailbox (ctx, 1, index_hint);
831 #endif /* USE_IMAP */
835 rc = pop_sync_mailbox (ctx, index_hint);
841 rc = nntp_sync_mailbox (ctx);
843 #endif /* USE_NNTP */
847 if (!ctx->quiet && !ctx->shutup && rc == -1)
848 mutt_error (_("Could not synchronize mailbox %s!"), ctx->path);
852 if (tmp && tmp->new == 0)
853 mutt_update_mailbox (tmp);
856 #ifdef USE_COMPRESSED
857 if (rc == 0 && ctx->compressinfo)
858 return mutt_sync_compressed (ctx);
864 /* move deleted mails to the trash folder */
865 static int trash_append (CONTEXT * ctx)
871 if (!TrashPath || !ctx->deleted ||
872 (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))
875 for (; i < ctx->msgcount && (!ctx->hdrs[i]->deleted ||
876 ctx->hdrs[i]->appended); i++);
877 if (i == ctx->msgcount)
878 return 0; /* nothing to be done */
880 if (mutt_save_confirm (TrashPath, &st) != 0) {
881 mutt_error _("message(s) not deleted");
886 if (lstat (ctx->path, &stc) == 0 && stc.st_ino == st.st_ino
887 && stc.st_dev == st.st_dev && stc.st_rdev == st.st_rdev)
888 return 0; /* we are in the trash folder: simple sync */
890 if ((ctx_trash = mx_open_mailbox (TrashPath, M_APPEND, NULL)) != NULL) {
891 for (i = 0; i < ctx->msgcount; i++)
892 if (ctx->hdrs[i]->deleted && !ctx->hdrs[i]->appended
893 && !ctx->hdrs[i]->purged
894 && mutt_append_message (ctx_trash, ctx, ctx->hdrs[i], 0, 0) == -1) {
895 mx_close_mailbox (ctx_trash, NULL);
899 mx_close_mailbox (ctx_trash, NULL);
902 mutt_error _("Can't open trash folder");
910 /* save changes and close mailbox */
911 int mx_close_mailbox (CONTEXT * ctx, int *index_hint)
913 int i, move_messages = 0, purge = 1, read_msgs = 0;
917 char mbox[_POSIX_PATH_MAX];
918 char buf[SHORT_STRING];
926 if (ctx->magic == M_NNTP) {
929 ret = nntp_close_mailbox (ctx);
930 mx_fastclose_mailbox (ctx);
934 if (ctx->readonly || ctx->dontwrite) {
935 /* mailbox is readonly or we don't want to write */
936 mx_fastclose_mailbox (ctx);
941 /* mailbox was opened in write-mode */
942 if (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
943 mbox_close_mailbox (ctx);
945 mx_fastclose_mailbox (ctx);
949 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) {
958 if ((p = mutt_find_hook (M_MBOXHOOK, ctx->path))) {
960 strfcpy (mbox, p, sizeof (mbox));
963 strfcpy (mbox, NONULL (Inbox), sizeof (mbox));
964 isSpool = mutt_is_spool (ctx->path) && !mutt_is_spool (mbox);
966 mutt_expand_path (mbox, sizeof (mbox));
969 snprintf (buf, sizeof (buf), _("Move read messages to %s?"), mbox);
970 if ((move_messages = query_quadoption (OPT_MOVE, buf)) == -1) {
978 * There is no point in asking whether or not to purge if we are
979 * just marking messages as "trash".
981 if (ctx->deleted && !(ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH))) {
982 snprintf (buf, sizeof (buf), ctx->deleted == 1
983 ? _("Purge %d deleted message?") :
984 _("Purge %d deleted messages?"), ctx->deleted);
985 if ((purge = query_quadoption (OPT_DELETE, buf)) < 0) {
992 /* IMAP servers manage the OLD flag themselves */
993 if (ctx->magic != M_IMAP)
995 if (option (OPTMARKOLD)) {
996 for (i = 0; i < ctx->msgcount; i++) {
997 if (!ctx->hdrs[i]->deleted && !ctx->hdrs[i]->old)
998 mutt_set_flag (ctx, ctx->hdrs[i], M_OLD, 1);
1002 if (move_messages) {
1003 mutt_message (_("Moving read messages to %s..."), mbox);
1006 /* try to use server-side copy first */
1009 if (ctx->magic == M_IMAP && mx_is_imap (mbox)) {
1010 /* tag messages for moving, and clear old tags, if any */
1011 for (i = 0; i < ctx->msgcount; i++)
1012 if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted
1013 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
1014 ctx->hdrs[i]->tagged = 1;
1016 ctx->hdrs[i]->tagged = 0;
1018 i = imap_copy_messages (ctx, NULL, mbox, 1);
1021 if (i == 0) /* success */
1022 mutt_clear_error ();
1023 else if (i == -1) { /* horrible error, bail */
1027 else /* use regular append-copy mode */
1030 if (mx_open_mailbox (mbox, M_APPEND, &f) == NULL) {
1035 for (i = 0; i < ctx->msgcount; i++) {
1036 if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted
1037 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED))) {
1038 if (mutt_append_message (&f, ctx, ctx->hdrs[i], 0, CH_UPDATE_LEN) ==
1040 mutt_set_flag (ctx, ctx->hdrs[i], M_DELETE, 1);
1041 mutt_set_flag (ctx, ctx->hdrs[i], M_APPENDED, 1);
1044 mx_close_mailbox (&f, NULL);
1051 mx_close_mailbox (&f, NULL);
1055 else if (!ctx->changed && ctx->deleted == 0) {
1056 mutt_message _("Mailbox is unchanged.");
1058 mx_fastclose_mailbox (ctx);
1062 /* copy mails to the trash before expunging */
1063 if (purge && ctx->deleted)
1064 if (trash_append (ctx) != 0) {
1070 /* allow IMAP to preserve the deleted flag across sessions */
1071 if (ctx->magic == M_IMAP) {
1072 if ((check = imap_sync_mailbox (ctx, purge, index_hint)) != 0) {
1081 for (i = 0; i < ctx->msgcount; i++)
1082 ctx->hdrs[i]->deleted = 0;
1086 if (ctx->changed || ctx->deleted) {
1087 if ((check = sync_mailbox (ctx, index_hint)) != 0) {
1095 mutt_message (_("%d kept, %d moved, %d deleted."),
1096 ctx->msgcount - ctx->deleted, read_msgs, ctx->deleted);
1098 mutt_message (_("%d kept, %d deleted."),
1099 ctx->msgcount - ctx->deleted, ctx->deleted);
1101 if (ctx->msgcount == ctx->deleted &&
1102 (ctx->magic == M_MMDF || ctx->magic == M_MBOX) &&
1103 !mutt_is_spool (ctx->path) && !option (OPTSAVEEMPTY))
1104 mx_unlink_empty (ctx->path);
1106 #ifdef USE_COMPRESSED
1107 if (ctx->compressinfo && mutt_slow_close_compressed (ctx))
1111 mx_fastclose_mailbox (ctx);
1117 /* update a Context structure's internal tables. */
1119 void mx_update_tables (CONTEXT * ctx, int committing)
1123 /* update memory to reflect the new state of the mailbox */
1132 #define this_body ctx->hdrs[j]->content
1133 for (i = 0, j = 0; i < ctx->msgcount; i++) {
1134 if ((committing && (!ctx->hdrs[i]->deleted ||
1135 (ctx->magic == M_MAILDIR
1136 && option (OPTMAILDIRTRASH)))) || (!committing
1140 ctx->hdrs[j] = ctx->hdrs[i];
1141 ctx->hdrs[i] = NULL;
1143 ctx->hdrs[j]->msgno = j;
1144 if (ctx->hdrs[j]->virtual != -1) {
1145 ctx->v2r[ctx->vcount] = j;
1146 ctx->hdrs[j]->virtual = ctx->vcount++;
1147 ctx->vsize += this_body->length + this_body->offset -
1148 this_body->hdr_offset;
1152 ctx->hdrs[j]->changed = 0;
1153 else if (ctx->hdrs[j]->changed)
1157 || (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH))) {
1158 if (ctx->hdrs[j]->deleted)
1162 if (ctx->hdrs[j]->tagged)
1164 if (ctx->hdrs[j]->flagged)
1166 if (!ctx->hdrs[j]->read) {
1168 if (!ctx->hdrs[j]->old)
1175 if (ctx->magic == M_MH || ctx->magic == M_MAILDIR)
1176 ctx->size -= (ctx->hdrs[i]->content->length +
1177 ctx->hdrs[i]->content->offset -
1178 ctx->hdrs[i]->content->hdr_offset);
1179 /* remove message from the hash tables */
1180 if (ctx->subj_hash && ctx->hdrs[i]->env->real_subj)
1181 hash_delete (ctx->subj_hash, ctx->hdrs[i]->env->real_subj,
1182 ctx->hdrs[i], NULL);
1183 if (ctx->id_hash && ctx->hdrs[i]->env->message_id)
1184 hash_delete (ctx->id_hash, ctx->hdrs[i]->env->message_id,
1185 ctx->hdrs[i], NULL);
1186 mutt_free_header (&ctx->hdrs[i]);
1194 /* save changes to mailbox
1200 int mx_sync_mailbox (CONTEXT * ctx, int *index_hint)
1204 int msgcount, deleted;
1206 if (ctx->dontwrite) {
1207 char buf[STRING], tmp[STRING];
1209 if (km_expand_key (buf, sizeof (buf),
1210 km_find_func (MENU_MAIN, OP_TOGGLE_WRITE)))
1211 snprintf (tmp, sizeof (tmp), _(" Press '%s' to toggle write"), buf);
1213 strfcpy (tmp, _("Use 'toggle-write' to re-enable write!"),
1216 mutt_error (_("Mailbox is marked unwritable. %s"), tmp);
1219 else if (ctx->readonly) {
1220 mutt_error _("Mailbox is read-only.");
1225 if (!ctx->changed && !ctx->deleted) {
1226 mutt_message _("Mailbox is unchanged.");
1232 char buf[SHORT_STRING];
1234 snprintf (buf, sizeof (buf), ctx->deleted == 1
1235 ? _("Purge %d deleted message?") :
1236 _("Purge %d deleted messages?"), ctx->deleted);
1237 if ((purge = query_quadoption (OPT_DELETE, buf)) < 0)
1239 else if (purge == M_NO) {
1241 return 0; /* nothing to do! */
1243 /* let IMAP servers hold on to D flags */
1244 if (ctx->magic != M_IMAP)
1247 for (i = 0; i < ctx->msgcount; i++)
1248 ctx->hdrs[i]->deleted = 0;
1252 else if (ctx->last_tag && ctx->last_tag->deleted)
1253 ctx->last_tag = NULL; /* reset last tagged msg now useless */
1256 /* really only for IMAP - imap_sync_mailbox results in a call to
1257 * mx_update_tables, so ctx->deleted is 0 when it comes back */
1258 msgcount = ctx->msgcount;
1259 deleted = ctx->deleted;
1261 if (purge && ctx->deleted) {
1262 if (trash_append (ctx) == -1)
1267 if (ctx->magic == M_IMAP)
1268 rc = imap_sync_mailbox (ctx, purge, index_hint);
1271 rc = sync_mailbox (ctx, index_hint);
1274 if (ctx->magic == M_IMAP && !purge)
1275 mutt_message (_("Mailbox checkpointed."));
1279 mutt_message (_("%d kept, %d deleted."), msgcount - deleted, deleted);
1283 if (ctx->msgcount == ctx->deleted &&
1284 (ctx->magic == M_MBOX || ctx->magic == M_MMDF) &&
1285 !mutt_is_spool (ctx->path) && !option (OPTSAVEEMPTY)) {
1287 mx_fastclose_mailbox (ctx);
1291 /* if we haven't deleted any messages, we don't need to resort */
1292 /* ... except for certain folder formats which need "unsorted"
1293 * sort order in order to synchronize folders.
1295 * MH and maildir are safe. mbox-style seems to need re-sorting,
1296 * at least with the new threading code.
1298 if (purge || (ctx->magic != M_MAILDIR && ctx->magic != M_MH)) {
1300 /* IMAP does this automatically after handling EXPUNGE */
1301 if (ctx->magic != M_IMAP)
1304 mx_update_tables (ctx, 1);
1305 mutt_sort_headers (ctx, 1); /* rethread from scratch */
1314 /* {maildir,mh}_open_new_message are in mh.c. */
1316 int mbox_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr)
1323 int imap_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr)
1325 char tmp[_POSIX_PATH_MAX];
1328 if ((msg->fp = safe_fopen (tmp, "w")) == NULL)
1330 msg->path = safe_strdup (tmp);
1336 * dest destintation mailbox
1337 * hdr message being copied (required for maildir support, because
1338 * the filename depends on the message flags)
1340 MESSAGE *mx_open_new_message (CONTEXT * dest, HEADER * hdr, int flags)
1343 int (*func) (MESSAGE *, CONTEXT *, HEADER *);
1346 switch (dest->magic) {
1349 func = mbox_open_new_message;
1352 func = maildir_open_new_message;
1355 func = mh_open_new_message;
1359 func = imap_open_new_message;
1365 "mx_open_new_message(): function unimplemented for mailbox type %d.\n",
1370 msg = safe_calloc (1, sizeof (MESSAGE));
1371 msg->magic = dest->magic;
1375 msg->flags.flagged = hdr->flagged;
1376 msg->flags.replied = hdr->replied;
1377 msg->flags.read = hdr->read;
1378 msg->received = hdr->received;
1381 if (msg->received == 0)
1382 time (&msg->received);
1384 if (func (msg, dest, hdr) == 0) {
1385 if (dest->magic == M_MMDF)
1386 fputs (MMDF_SEP, msg->fp);
1388 if ((msg->magic == M_MBOX || msg->magic == M_MMDF) && flags & M_ADD_FROM) {
1390 if (hdr->env->return_path)
1391 p = hdr->env->return_path;
1392 else if (hdr->env->sender)
1393 p = hdr->env->sender;
1398 fprintf (msg->fp, "From %s %s", p ? p->mailbox : NONULL (Username),
1399 ctime (&msg->received));
1408 /* check for new mail */
1409 int mx_check_mailbox (CONTEXT * ctx, int *index_hint, int lock)
1413 #ifdef USE_COMPRESSED
1414 if (ctx->compressinfo)
1415 return mutt_check_mailbox_compressed (ctx);
1422 switch (ctx->magic) {
1427 mutt_block_signals ();
1428 if (mbox_lock_mailbox (ctx, 0, 0) == -1) {
1429 mutt_unblock_signals ();
1434 rc = mbox_check_mailbox (ctx, index_hint);
1437 mutt_unblock_signals ();
1438 mbox_unlock_mailbox (ctx);
1445 return (mh_check_mailbox (ctx, index_hint));
1447 return (maildir_check_mailbox (ctx, index_hint));
1451 return (imap_check_mailbox (ctx, index_hint, 0));
1452 #endif /* USE_IMAP */
1456 return (pop_check_mailbox (ctx, index_hint));
1457 #endif /* USE_POP */
1461 return (nntp_check_mailbox (ctx));
1462 #endif /* USE_NNTP */
1466 dprint (1, (debugfile, "mx_check_mailbox: null or invalid context.\n"));
1470 /* return a stream pointer for a message */
1471 MESSAGE *mx_open_message (CONTEXT * ctx, int msgno)
1475 msg = safe_calloc (1, sizeof (MESSAGE));
1476 switch (msg->magic = ctx->magic) {
1485 HEADER *cur = ctx->hdrs[msgno];
1486 char path[_POSIX_PATH_MAX];
1488 snprintf (path, sizeof (path), "%s/%s", ctx->path, cur->path);
1490 if ((msg->fp = fopen (path, "r")) == NULL && errno == ENOENT &&
1491 ctx->magic == M_MAILDIR)
1492 msg->fp = maildir_open_find_message (ctx->path, cur->path);
1494 if (msg->fp == NULL) {
1496 dprint (1, (debugfile, "mx_open_message: fopen: %s: %s (errno %d).\n",
1497 path, strerror (errno), errno));
1506 if (imap_fetch_message (msg, ctx, msgno) != 0)
1510 #endif /* USE_IMAP */
1515 if (pop_fetch_message (msg, ctx, msgno) != 0)
1519 #endif /* USE_POP */
1524 if (nntp_fetch_message (msg, ctx, msgno) != 0)
1528 #endif /* USE_NNTP */
1533 "mx_open_message(): function not implemented for mailbox type %d.\n",
1541 /* commit a message to a folder */
1543 int mx_commit_message (MESSAGE * msg, CONTEXT * ctx)
1547 if (!(msg->write && ctx->append)) {
1550 "mx_commit_message(): msg->write = %d, ctx->append = %d\n",
1551 msg->write, ctx->append));
1555 switch (msg->magic) {
1558 if (fputs (MMDF_SEP, msg->fp) == EOF)
1565 if (fputc ('\n', msg->fp) == EOF)
1573 if ((r = safe_fclose (&msg->fp)) == 0)
1574 r = imap_append_message (ctx, msg);
1581 r = maildir_commit_message (ctx, msg, NULL);
1587 r = mh_commit_message (ctx, msg, NULL);
1592 if (r == 0 && (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
1593 && (fflush (msg->fp) == EOF || fsync (fileno (msg->fp)) == -1)) {
1594 mutt_perror _("Can't write message");
1602 /* close a pointer to a message */
1603 int mx_close_message (MESSAGE ** msg)
1607 if ((*msg)->magic == M_MH || (*msg)->magic == M_MAILDIR
1609 || (*msg)->magic == M_IMAP
1612 || (*msg)->magic == M_POP
1615 || (*msg)->magic == M_NNTP
1618 r = safe_fclose (&(*msg)->fp);
1624 dprint (1, (debugfile, "mx_close_message (): unlinking %s\n",
1626 unlink ((*msg)->path);
1627 FREE (&(*msg)->path);
1634 void mx_alloc_memory (CONTEXT * ctx)
1637 size_t s = MAX (sizeof (HEADER *), sizeof (int));
1639 if ((ctx->hdrmax + 25) * s < ctx->hdrmax * s) {
1640 mutt_error _("Integer overflow -- can't allocate memory.");
1647 safe_realloc (&ctx->hdrs, sizeof (HEADER *) * (ctx->hdrmax += 25));
1648 safe_realloc (&ctx->v2r, sizeof (int) * ctx->hdrmax);
1651 ctx->hdrs = safe_calloc ((ctx->hdrmax += 25), sizeof (HEADER *));
1652 ctx->v2r = safe_calloc (ctx->hdrmax, sizeof (int));
1654 for (i = ctx->msgcount; i < ctx->hdrmax; i++) {
1655 ctx->hdrs[i] = NULL;
1660 /* this routine is called to update the counts in the context structure for
1661 * the last message header parsed.
1663 void mx_update_context (CONTEXT * ctx, int new_messages)
1668 for (msgno = ctx->msgcount - new_messages; msgno < ctx->msgcount; msgno++) {
1669 h = ctx->hdrs[msgno];
1672 /* NOTE: this _must_ be done before the check for mailcap! */
1673 h->security = crypt_query (h->content);
1676 if (!ctx->pattern) {
1677 ctx->v2r[ctx->vcount] = msgno;
1678 h->virtual = ctx->vcount++;
1684 if (h->env->supersedes) {
1688 ctx->id_hash = mutt_make_id_hash (ctx);
1690 h2 = hash_find (ctx->id_hash, h->env->supersedes);
1692 /* FREE (&h->env->supersedes); should I ? */
1695 if (option (OPTSCORE))
1696 mutt_score_message (ctx, h2, 1);
1700 /* add this message to the hash tables */
1701 if (ctx->id_hash && h->env->message_id)
1702 hash_insert (ctx->id_hash, h->env->message_id, h, 0);
1703 if (ctx->subj_hash && h->env->real_subj)
1704 hash_insert (ctx->subj_hash, h->env->real_subj, h, 1);
1706 if (option (OPTSCORE))
1707 mutt_score_message (ctx, h, 0);
1725 * 1 if the specified mailbox contains 0 messages.
1726 * 0 if the mailbox contains messages
1729 int mx_check_empty (const char *path)
1731 switch (mx_get_magic (path)) {
1734 return mbox_check_empty (path);
1736 return mh_check_empty (path);
1738 return maildir_check_empty (path);