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.
53 #include "mutt_crypt.h"
69 #define mutt_is_spool(s) (mutt_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 : "",
101 return mutt_system (cmd);
106 #define invoke_dotlock dotlock_invoke
110 static int dotlock_file (const char *path, int fd, int retry)
113 int flags = DL_FL_USEPRIV | DL_FL_RETRY;
115 if (retry) retry = 1;
118 if ((r = invoke_dotlock(path, fd, flags, retry)) == DL_EX_EXIST)
120 if (!option (OPTNOCURSES))
122 char msg[LONG_STRING];
124 snprintf(msg, sizeof(msg), _("Lock count exceeded, remove lock for %s?"),
126 if(retry && mutt_yesorno(msg, M_YES) == M_YES)
128 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) == DL_EX_OK ?
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)
180 mutt_perror ("fcntl");
184 if (fstat (fd, &sb) != 0)
190 /* only unlock file if it is unchanged */
191 if (prev_sb.st_size == sb.st_size && ++count >= (timeout?MAXLOCKATTEMPT:0))
194 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)
213 mutt_perror ("flock");
218 if (fstat(fd,&sb) != 0 )
224 /* only unlock file if it is unchanged */
225 if (prev_sb.st_size == sb.st_size && ++count >= (timeout?MAXLOCKATTEMPT:0))
228 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 */
247 /* release any other locks obtained in this routine */
250 lck.l_type = F_UNLCK;
251 fcntl (fd, F_SETLK, &lck);
252 #endif /* USE_FCNTL */
256 #endif /* USE_FLOCK */
264 int mx_unlock_file (const char *path, int fd, int dot)
267 struct flock unlockit = { F_UNLCK, 0, 0, 0 };
269 memset (&unlockit, 0, sizeof (struct flock));
270 unlockit.l_type = F_UNLCK;
271 unlockit.l_whence = SEEK_SET;
272 fcntl (fd, F_SETLK, &unlockit);
281 undotlock_file (path, fd);
287 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)
304 invoke_dotlock (path, fd, DL_FL_UNLINK, 1);
306 if (fstat (fd, &sb) == 0 && sb.st_size == 0)
310 mx_unlock_file (path, fd, 0);
314 /* try to figure out what type of mailbox ``path'' is
324 int mx_is_imap(const char *p)
334 scheme = url_check_scheme (p);
335 if (scheme == U_IMAP || scheme == U_IMAPS)
344 int mx_is_pop (const char *p)
351 scheme = url_check_scheme (p);
352 if (scheme == U_POP || scheme == U_POPS)
360 int mx_is_nntp (const char *p)
367 scheme = url_check_scheme (p);
368 if (scheme == U_NNTP || scheme == U_NNTPS)
375 int mx_get_magic (const char *path)
379 char tmp[_POSIX_PATH_MAX];
385 #endif /* USE_IMAP */
388 if (mx_is_pop (path))
393 if (mx_is_nntp (path))
395 #endif /* USE_NNTP */
397 if (stat (path, &st) == -1)
399 dprint (1, (debugfile, "mx_get_magic(): unable to stat %s: %s (errno %d).\n",
400 path, strerror (errno), errno));
404 if (S_ISDIR (st.st_mode))
406 /* check for maildir-style mailbox */
408 snprintf (tmp, sizeof (tmp), "%s/cur", path);
409 if (stat (tmp, &st) == 0 && S_ISDIR (st.st_mode))
412 /* check for mh-style mailbox */
414 snprintf (tmp, sizeof (tmp), "%s/.mh_sequences", path);
415 if (access (tmp, F_OK) == 0)
418 snprintf (tmp, sizeof (tmp), "%s/.xmhcache", 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/.mew-cache", path);
427 if (access (tmp, F_OK) == 0)
430 snprintf (tmp, sizeof (tmp), "%s/.sylpheed_cache", path);
431 if (access (tmp, F_OK) == 0)
435 * ok, this isn't an mh folder, but mh mode can be used to read
436 * Usenet news from the spool. ;-)
439 snprintf (tmp, sizeof (tmp), "%s/.overview", path);
440 if (access (tmp, F_OK) == 0)
444 else if (st.st_size == 0)
446 /* hard to tell what zero-length files are, so assume the default magic */
447 if (DefaultMagic == M_MBOX || DefaultMagic == M_MMDF)
448 return (DefaultMagic);
452 else if ((f = fopen (path, "r")) != NULL)
455 struct utimbuf times;
458 fgets (tmp, sizeof (tmp), f);
459 if (mutt_strncmp ("From ", tmp, 5) == 0)
461 else if (mutt_strcmp (MMDF_SEP, tmp) == 0)
465 /* need to restore the times here, the file was not really accessed,
466 * only the type was accessed. This is important, because detection
467 * of "new mail" depends on those times set correctly.
469 times.actime = st.st_atime;
470 times.modtime = st.st_mtime;
471 utime (path, ×);
476 dprint (1, (debugfile, "mx_get_magic(): unable to open file %s for reading.\n",
482 #ifdef USE_COMPRESSED
483 if (magic == 0 && mutt_can_read_compressed (path))
491 * set DefaultMagic to the given value
493 int mx_set_magic (const char *s)
495 if (ascii_strcasecmp (s, "mbox") == 0)
496 DefaultMagic = M_MBOX;
497 else if (ascii_strcasecmp (s, "mmdf") == 0)
498 DefaultMagic = M_MMDF;
499 else if (ascii_strcasecmp (s, "mh") == 0)
501 else if (ascii_strcasecmp (s, "maildir") == 0)
502 DefaultMagic = M_MAILDIR;
509 /* mx_access: Wrapper for access, checks permissions on a given mailbox.
510 * We may be interested in using ACL-style flags at some point, currently
511 * we use the normal access() flags. */
512 int mx_access (const char* path, int flags)
515 if (mx_is_imap (path))
516 return imap_access (path, flags);
519 return access (path, flags);
522 static int mx_open_mailbox_append (CONTEXT *ctx, int flags)
526 #ifdef USE_COMPRESSED
527 /* special case for appending to compressed folders -
528 * even if we can not open them for reading */
529 if (mutt_can_append_compressed (ctx->path))
530 mutt_open_append_compressed (ctx);
537 if(mx_is_imap(ctx->path))
538 return imap_open_mailbox_append (ctx);
542 if(stat(ctx->path, &sb) == 0)
544 ctx->magic = mx_get_magic (ctx->path);
549 mutt_error (_("%s is not a mailbox."), ctx->path);
555 else if (errno == ENOENT)
557 ctx->magic = DefaultMagic;
559 if (ctx->magic == M_MH || ctx->magic == M_MAILDIR)
561 char tmp[_POSIX_PATH_MAX];
563 if (mkdir (ctx->path, S_IRWXU))
565 mutt_perror (ctx->path);
569 if (ctx->magic == M_MAILDIR)
571 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
572 if (mkdir (tmp, S_IRWXU))
579 snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
580 if (mkdir (tmp, S_IRWXU))
583 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
588 snprintf (tmp, sizeof (tmp), "%s/tmp", ctx->path);
589 if (mkdir (tmp, S_IRWXU))
592 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
594 snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
604 snprintf (tmp, sizeof (tmp), "%s/.mh_sequences", ctx->path);
605 if ((i = creat (tmp, S_IRWXU)) == -1)
617 mutt_perror (ctx->path);
625 if ((ctx->fp = safe_fopen (ctx->path, flags & M_NEWFOLDER ? "w" : "a")) == NULL ||
626 mbox_lock_mailbox (ctx, 1, 1) != 0)
629 mutt_perror (ctx->path);
632 mutt_error (_("Couldn't lock %s\n"), ctx->path);
633 safe_fclose (&ctx->fp);
637 fseek (ctx->fp, 0, 2);
653 * open a mailbox and parse it
656 * flags M_NOSORT do not sort mailbox
657 * M_APPEND open mailbox for appending
658 * M_READONLY open mailbox in read-only mode
659 * M_QUIET only print error messages
660 * ctx if non-null, context struct to use
662 CONTEXT *mx_open_mailbox (const char *path, int flags, CONTEXT *pctx)
668 ctx = safe_malloc (sizeof (CONTEXT));
669 memset (ctx, 0, sizeof (CONTEXT));
670 ctx->path = safe_strdup (path);
672 ctx->msgnotreadyet = -1;
677 if (flags & M_READONLY)
680 if (flags & (M_APPEND|M_NEWFOLDER))
682 if (mx_open_mailbox_append (ctx, flags) != 0)
684 mx_fastclose_mailbox (ctx);
692 ctx->magic = mx_get_magic (path);
694 #ifdef USE_COMPRESSED
695 if (ctx->magic == M_COMPRESSED)
696 mutt_open_read_compressed (ctx);
700 mutt_error (_("%s is not a mailbox."), path);
707 mx_fastclose_mailbox (ctx);
713 /* if the user has a `push' command in their .muttrc, or in a folder-hook,
714 * it will cause the progress messages not to be displayed because
715 * mutt_refresh() will think we are in the middle of a macro. so set a
716 * flag to indicate that we should really refresh the screen.
718 set_option (OPTFORCEREFRESH);
721 mutt_message (_("Reading %s..."), ctx->path);
726 rc = mh_read_dir (ctx, NULL);
730 rc = maildir_read_dir (ctx);
735 rc = mbox_open_mailbox (ctx);
740 rc = imap_open_mailbox (ctx);
742 #endif /* USE_IMAP */
746 rc = pop_open_mailbox (ctx);
752 rc = nntp_open_mailbox (ctx);
754 #endif /* USE_NNTP */
763 if ((flags & M_NOSORT) == 0)
765 /* avoid unnecessary work since the mailbox is completely unthreaded
767 unset_option (OPTSORTSUBTHREADS);
768 unset_option (OPTNEEDRESCORE);
769 mutt_sort_headers (ctx, 1);
776 mx_fastclose_mailbox (ctx);
781 unset_option (OPTFORCEREFRESH);
785 /* free up memory associated with the mailbox context */
786 void mx_fastclose_mailbox (CONTEXT *ctx)
794 if (ctx->magic == M_IMAP)
795 imap_close_mailbox (ctx);
796 #endif /* USE_IMAP */
798 if (ctx->magic == M_POP)
799 pop_close_mailbox (ctx);
802 if (ctx->magic == M_NNTP)
803 nntp_fastclose_mailbox (ctx);
804 #endif /* USE_NNTP */
806 hash_destroy (&ctx->subj_hash, NULL);
808 hash_destroy (&ctx->id_hash, NULL);
809 mutt_clear_threads (ctx);
810 for (i = 0; i < ctx->msgcount; i++)
811 mutt_free_header (&ctx->hdrs[i]);
814 #ifdef USE_COMPRESSED
815 if (ctx->compressinfo)
816 mutt_fast_close_compressed (ctx);
819 FREE (&ctx->pattern);
820 if (ctx->limit_pattern)
821 mutt_pattern_free (&ctx->limit_pattern);
822 safe_fclose (&ctx->fp);
823 memset (ctx, 0, sizeof (CONTEXT));
826 /* save changes to disk */
827 static int sync_mailbox (CONTEXT *ctx, int *index_hint)
835 mutt_message (_("Writing %s..."), ctx->path);
841 rc = mbox_sync_mailbox (ctx, index_hint);
843 tmp = mutt_find_mailbox (ctx->path);
849 rc = mh_sync_mailbox (ctx, index_hint);
854 /* extra argument means EXPUNGE */
855 rc = imap_sync_mailbox (ctx, 1, index_hint);
857 #endif /* USE_IMAP */
861 rc = pop_sync_mailbox (ctx, index_hint);
867 rc = nntp_sync_mailbox (ctx);
869 #endif /* USE_NNTP */
873 if (!ctx->quiet && !ctx->shutup && rc == -1)
874 mutt_error ( _("Could not synchronize mailbox %s!"), ctx->path);
878 if (tmp && tmp->new == 0)
879 mutt_update_mailbox (tmp);
882 #ifdef USE_COMPRESSED
883 if (rc == 0 && ctx->compressinfo)
884 return mutt_sync_compressed (ctx);
890 /* move deleted mails to the trash folder */
891 static int trash_append (CONTEXT *ctx)
897 if (!TrashPath || !ctx->deleted ||
898 (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))
901 for (;i < ctx->msgcount && (!ctx->hdrs[i]->deleted ||
902 ctx->hdrs[i]->appended); i++);
903 if (i == ctx->msgcount)
904 return 0; /* nothing to be done */
906 if (mutt_save_confirm (TrashPath, &st) != 0)
908 mutt_error _("message(s) not deleted");
912 if (lstat (ctx->path, &stc) == 0 && stc.st_ino == st.st_ino
913 && stc.st_dev == st.st_dev && stc.st_rdev == st.st_rdev)
914 return 0; /* we are in the trash folder: simple sync */
916 if ((ctx_trash = mx_open_mailbox (TrashPath, M_APPEND, NULL)) != NULL)
918 for (i = 0 ; i < ctx->msgcount ; i++)
919 if (ctx->hdrs[i]->deleted && !ctx->hdrs[i]->appended
920 && !ctx->hdrs[i]->purged
921 && mutt_append_message (ctx_trash, ctx, ctx->hdrs[i], 0, 0) == -1)
923 mx_close_mailbox (ctx_trash, NULL);
927 mx_close_mailbox (ctx_trash, NULL);
931 mutt_error _("Can't open trash folder");
938 /* save changes and close mailbox */
939 int mx_close_mailbox (CONTEXT *ctx, int *index_hint)
941 int i, move_messages = 0, purge = 1, read_msgs = 0;
945 char mbox[_POSIX_PATH_MAX];
946 char buf[SHORT_STRING];
953 if (ctx->magic == M_NNTP)
957 ret = nntp_close_mailbox (ctx);
958 mx_fastclose_mailbox (ctx);
962 if (ctx->readonly || ctx->dontwrite)
964 /* mailbox is readonly or we don't want to write */
965 mx_fastclose_mailbox (ctx);
971 /* mailbox was opened in write-mode */
972 if (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
973 mbox_close_mailbox (ctx);
975 mx_fastclose_mailbox (ctx);
979 for (i = 0; i < ctx->msgcount; i++)
981 if (!ctx->hdrs[i]->deleted && ctx->hdrs[i]->read
982 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
986 if (read_msgs && quadoption (OPT_MOVE) != M_NO)
990 if ((p = mutt_find_hook (M_MBOXHOOK, ctx->path)))
993 strfcpy (mbox, p, sizeof (mbox));
997 strfcpy (mbox, NONULL(Inbox), sizeof (mbox));
998 isSpool = mutt_is_spool (ctx->path) && !mutt_is_spool (mbox);
1000 mutt_expand_path (mbox, sizeof (mbox));
1004 snprintf (buf, sizeof (buf), _("Move read messages to %s?"), mbox);
1005 if ((move_messages = query_quadoption (OPT_MOVE, buf)) == -1)
1014 * There is no point in asking whether or not to purge if we are
1015 * just marking messages as "trash".
1017 if (ctx->deleted && !(ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))
1019 snprintf (buf, sizeof (buf), ctx->deleted == 1
1020 ? _("Purge %d deleted message?") : _("Purge %d deleted messages?"),
1022 if ((purge = query_quadoption (OPT_DELETE, buf)) < 0)
1030 /* IMAP servers manage the OLD flag themselves */
1031 if (ctx->magic != M_IMAP)
1033 if (option (OPTMARKOLD))
1035 for (i = 0; i < ctx->msgcount; i++)
1037 if (!ctx->hdrs[i]->deleted && !ctx->hdrs[i]->old)
1038 mutt_set_flag (ctx, ctx->hdrs[i], M_OLD, 1);
1044 mutt_message (_("Moving read messages to %s..."), mbox);
1047 /* try to use server-side copy first */
1050 if (ctx->magic == M_IMAP && mx_is_imap (mbox))
1052 /* tag messages for moving, and clear old tags, if any */
1053 for (i = 0; i < ctx->msgcount; i++)
1054 if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted
1055 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
1056 ctx->hdrs[i]->tagged = 1;
1058 ctx->hdrs[i]->tagged = 0;
1060 i = imap_copy_messages (ctx, NULL, mbox, 1);
1063 if (i == 0) /* success */
1064 mutt_clear_error ();
1065 else if (i == -1) /* horrible error, bail */
1070 else /* use regular append-copy mode */
1073 if (mx_open_mailbox (mbox, M_APPEND, &f) == NULL)
1079 for (i = 0; i < ctx->msgcount; i++)
1081 if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted
1082 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
1084 if (mutt_append_message (&f, ctx, ctx->hdrs[i], 0, CH_UPDATE_LEN) == 0)
1086 mutt_set_flag (ctx, ctx->hdrs[i], M_DELETE, 1);
1087 mutt_set_flag (ctx, ctx->hdrs[i], M_APPENDED, 1);
1091 mx_close_mailbox (&f, NULL);
1098 mx_close_mailbox (&f, NULL);
1102 else if (!ctx->changed && ctx->deleted == 0)
1104 mutt_message _("Mailbox is unchanged.");
1105 mx_fastclose_mailbox (ctx);
1109 /* copy mails to the trash before expunging */
1110 if (purge && ctx->deleted)
1111 if (trash_append (ctx) != 0)
1118 /* allow IMAP to preserve the deleted flag across sessions */
1119 if (ctx->magic == M_IMAP)
1121 if ((check = imap_sync_mailbox (ctx, purge, index_hint)) != 0)
1132 for (i = 0; i < ctx->msgcount; i++)
1133 ctx->hdrs[i]->deleted = 0;
1137 if (ctx->changed || ctx->deleted)
1139 if ((check = sync_mailbox (ctx, index_hint)) != 0)
1148 mutt_message (_("%d kept, %d moved, %d deleted."),
1149 ctx->msgcount - ctx->deleted, read_msgs, ctx->deleted);
1151 mutt_message (_("%d kept, %d deleted."),
1152 ctx->msgcount - ctx->deleted, ctx->deleted);
1154 if (ctx->msgcount == ctx->deleted &&
1155 (ctx->magic == M_MMDF || ctx->magic == M_MBOX) &&
1156 !mutt_is_spool(ctx->path) && !option (OPTSAVEEMPTY))
1157 mx_unlink_empty (ctx->path);
1159 #ifdef USE_COMPRESSED
1160 if (ctx->compressinfo && mutt_slow_close_compressed (ctx))
1164 mx_fastclose_mailbox (ctx);
1170 /* update a Context structure's internal tables. */
1172 void mx_update_tables(CONTEXT *ctx, int committing)
1176 /* update memory to reflect the new state of the mailbox */
1185 #define this_body ctx->hdrs[j]->content
1186 for (i = 0, j = 0; i < ctx->msgcount; i++)
1188 if ((committing && (!ctx->hdrs[i]->deleted ||
1189 (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))) ||
1190 (!committing && ctx->hdrs[i]->active))
1194 ctx->hdrs[j] = ctx->hdrs[i];
1195 ctx->hdrs[i] = NULL;
1197 ctx->hdrs[j]->msgno = j;
1198 if (ctx->hdrs[j]->virtual != -1)
1200 ctx->v2r[ctx->vcount] = j;
1201 ctx->hdrs[j]->virtual = ctx->vcount++;
1202 ctx->vsize += this_body->length + this_body->offset -
1203 this_body->hdr_offset;
1207 ctx->hdrs[j]->changed = 0;
1208 else if (ctx->hdrs[j]->changed)
1211 if (!committing || (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))
1213 if (ctx->hdrs[j]->deleted)
1217 if (ctx->hdrs[j]->tagged)
1219 if (ctx->hdrs[j]->flagged)
1221 if (!ctx->hdrs[j]->read)
1224 if (!ctx->hdrs[j]->old)
1232 if (ctx->magic == M_MH || ctx->magic == M_MAILDIR)
1233 ctx->size -= (ctx->hdrs[i]->content->length +
1234 ctx->hdrs[i]->content->offset -
1235 ctx->hdrs[i]->content->hdr_offset);
1236 /* remove message from the hash tables */
1237 if (ctx->subj_hash && ctx->hdrs[i]->env->real_subj)
1238 hash_delete (ctx->subj_hash, ctx->hdrs[i]->env->real_subj, ctx->hdrs[i], NULL);
1239 if (ctx->id_hash && ctx->hdrs[i]->env->message_id)
1240 hash_delete (ctx->id_hash, ctx->hdrs[i]->env->message_id, ctx->hdrs[i], NULL);
1241 mutt_free_header (&ctx->hdrs[i]);
1249 /* save changes to mailbox
1255 int mx_sync_mailbox (CONTEXT *ctx, int *index_hint)
1259 int msgcount, deleted;
1263 char buf[STRING], tmp[STRING];
1264 if (km_expand_key (buf, sizeof(buf),
1265 km_find_func (MENU_MAIN, OP_TOGGLE_WRITE)))
1266 snprintf (tmp, sizeof(tmp), _(" Press '%s' to toggle write"), buf);
1268 strfcpy (tmp, _("Use 'toggle-write' to re-enable write!"), sizeof(tmp));
1270 mutt_error (_("Mailbox is marked unwritable. %s"), tmp);
1273 else if (ctx->readonly)
1275 mutt_error _("Mailbox is read-only.");
1279 if (!ctx->changed && !ctx->deleted)
1281 mutt_message _("Mailbox is unchanged.");
1287 char buf[SHORT_STRING];
1289 snprintf (buf, sizeof (buf), ctx->deleted == 1
1290 ? _("Purge %d deleted message?") : _("Purge %d deleted messages?"),
1292 if ((purge = query_quadoption (OPT_DELETE, buf)) < 0)
1294 else if (purge == M_NO)
1297 return 0; /* nothing to do! */
1299 /* let IMAP servers hold on to D flags */
1300 if (ctx->magic != M_IMAP)
1303 for (i = 0 ; i < ctx->msgcount ; i++)
1304 ctx->hdrs[i]->deleted = 0;
1308 else if (ctx->last_tag && ctx->last_tag->deleted)
1309 ctx->last_tag = NULL; /* reset last tagged msg now useless */
1312 /* really only for IMAP - imap_sync_mailbox results in a call to
1313 * mx_update_tables, so ctx->deleted is 0 when it comes back */
1314 msgcount = ctx->msgcount;
1315 deleted = ctx->deleted;
1317 if (purge && ctx->deleted)
1319 if (trash_append (ctx) == -1)
1324 if (ctx->magic == M_IMAP)
1325 rc = imap_sync_mailbox (ctx, purge, index_hint);
1328 rc = sync_mailbox (ctx, index_hint);
1332 if (ctx->magic == M_IMAP && !purge)
1333 mutt_message _("Mailbox checkpointed.");
1336 mutt_message (_("%d kept, %d deleted."), msgcount - deleted,
1341 if (ctx->msgcount == ctx->deleted &&
1342 (ctx->magic == M_MBOX || ctx->magic == M_MMDF) &&
1343 !mutt_is_spool (ctx->path) && !option (OPTSAVEEMPTY))
1346 mx_fastclose_mailbox (ctx);
1350 /* if we haven't deleted any messages, we don't need to resort */
1351 /* ... except for certain folder formats which need "unsorted"
1352 * sort order in order to synchronize folders.
1354 * MH and maildir are safe. mbox-style seems to need re-sorting,
1355 * at least with the new threading code.
1357 if (purge || (ctx->magic != M_MAILDIR && ctx->magic != M_MH))
1360 /* IMAP does this automatically after handling EXPUNGE */
1361 if (ctx->magic != M_IMAP)
1364 mx_update_tables (ctx, 1);
1365 mutt_sort_headers (ctx, 1); /* rethread from scratch */
1374 /* {maildir,mh}_open_new_message are in mh.c. */
1376 int mbox_open_new_message (MESSAGE *msg, CONTEXT *dest, HEADER *hdr)
1383 int imap_open_new_message (MESSAGE *msg, CONTEXT *dest, HEADER *hdr)
1385 char tmp[_POSIX_PATH_MAX];
1388 if ((msg->fp = safe_fopen (tmp, "w")) == NULL)
1390 msg->path = safe_strdup(tmp);
1396 * dest destintation mailbox
1397 * hdr message being copied (required for maildir support, because
1398 * the filename depends on the message flags)
1400 MESSAGE *mx_open_new_message (CONTEXT *dest, HEADER *hdr, int flags)
1403 int (*func) (MESSAGE *, CONTEXT *, HEADER *);
1406 switch (dest->magic)
1410 func = mbox_open_new_message;
1413 func = maildir_open_new_message;
1416 func = mh_open_new_message;
1420 func = imap_open_new_message;
1424 dprint (1, (debugfile, "mx_open_new_message(): function unimplemented for mailbox type %d.\n",
1429 msg = safe_calloc (1, sizeof (MESSAGE));
1430 msg->magic = dest->magic;
1435 msg->flags.flagged = hdr->flagged;
1436 msg->flags.replied = hdr->replied;
1437 msg->flags.read = hdr->read;
1438 msg->received = hdr->received;
1441 if(msg->received == 0)
1442 time(&msg->received);
1444 if (func (msg, dest, hdr) == 0)
1446 if (dest->magic == M_MMDF)
1447 fputs (MMDF_SEP, msg->fp);
1449 if ((msg->magic == M_MBOX || msg->magic == M_MMDF) &&
1454 if (hdr->env->return_path)
1455 p = hdr->env->return_path;
1456 else if (hdr->env->sender)
1457 p = hdr->env->sender;
1462 fprintf (msg->fp, "From %s %s", p ? p->mailbox : NONULL(Username), ctime (&msg->received));
1471 /* check for new mail */
1472 int mx_check_mailbox (CONTEXT *ctx, int *index_hint, int lock)
1476 #ifdef USE_COMPRESSED
1477 if (ctx->compressinfo)
1478 return mutt_check_mailbox_compressed (ctx);
1483 if (ctx->locked) lock = 0;
1492 mutt_block_signals ();
1493 if (mbox_lock_mailbox (ctx, 0, 0) == -1)
1495 mutt_unblock_signals ();
1500 rc = mbox_check_mailbox (ctx, index_hint);
1504 mutt_unblock_signals ();
1505 mbox_unlock_mailbox (ctx);
1512 return (mh_check_mailbox (ctx, index_hint));
1514 return (maildir_check_mailbox (ctx, index_hint));
1518 return (imap_check_mailbox (ctx, index_hint, 0));
1519 #endif /* USE_IMAP */
1523 return (pop_check_mailbox (ctx, index_hint));
1524 #endif /* USE_POP */
1528 return (nntp_check_mailbox (ctx));
1529 #endif /* USE_NNTP */
1533 dprint (1, (debugfile, "mx_check_mailbox: null or invalid context.\n"));
1537 /* return a stream pointer for a message */
1538 MESSAGE *mx_open_message (CONTEXT *ctx, int msgno)
1542 msg = safe_calloc (1, sizeof (MESSAGE));
1543 switch (msg->magic = ctx->magic)
1553 HEADER *cur = ctx->hdrs[msgno];
1554 char path[_POSIX_PATH_MAX];
1556 snprintf (path, sizeof (path), "%s/%s", ctx->path, cur->path);
1558 if ((msg->fp = fopen (path, "r")) == NULL && errno == ENOENT &&
1559 ctx->magic == M_MAILDIR)
1560 msg->fp = maildir_open_find_message (ctx->path, cur->path);
1562 if (msg->fp == NULL)
1565 dprint (1, (debugfile, "mx_open_message: fopen: %s: %s (errno %d).\n",
1566 path, strerror (errno), errno));
1575 if (imap_fetch_message (msg, ctx, msgno) != 0)
1579 #endif /* USE_IMAP */
1584 if (pop_fetch_message (msg, ctx, msgno) != 0)
1588 #endif /* USE_POP */
1593 if (nntp_fetch_message (msg, ctx, msgno) != 0)
1597 #endif /* USE_NNTP */
1600 dprint (1, (debugfile, "mx_open_message(): function not implemented for mailbox type %d.\n", ctx->magic));
1607 /* commit a message to a folder */
1609 int mx_commit_message (MESSAGE *msg, CONTEXT *ctx)
1613 if (!(msg->write && ctx->append))
1615 dprint (1, (debugfile, "mx_commit_message(): msg->write = %d, ctx->append = %d\n",
1616 msg->write, ctx->append));
1624 if (fputs (MMDF_SEP, msg->fp) == EOF)
1631 if (fputc ('\n', msg->fp) == EOF)
1639 if ((r = safe_fclose (&msg->fp)) == 0)
1640 r = imap_append_message (ctx, msg);
1647 r = maildir_commit_message (ctx, msg, NULL);
1653 r = mh_commit_message (ctx, msg, NULL);
1658 if (r == 0 && (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
1659 && (fflush (msg->fp) == EOF || fsync (fileno (msg->fp)) == -1))
1661 mutt_perror _("Can't write message");
1668 /* close a pointer to a message */
1669 int mx_close_message (MESSAGE **msg)
1673 if ((*msg)->magic == M_MH || (*msg)->magic == M_MAILDIR
1675 || (*msg)->magic == M_IMAP
1678 || (*msg)->magic == M_POP
1681 || (*msg)->magic == M_NNTP
1685 r = safe_fclose (&(*msg)->fp);
1692 dprint (1, (debugfile, "mx_close_message (): unlinking %s\n",
1694 unlink ((*msg)->path);
1695 FREE (&(*msg)->path);
1702 void mx_alloc_memory (CONTEXT *ctx)
1705 size_t s = MAX (sizeof (HEADER *), sizeof (int));
1707 if ((ctx->hdrmax + 25) * s < ctx->hdrmax * s)
1709 mutt_error _("Integer overflow -- can't allocate memory.");
1716 safe_realloc (&ctx->hdrs, sizeof (HEADER *) * (ctx->hdrmax += 25));
1717 safe_realloc (&ctx->v2r, sizeof (int) * ctx->hdrmax);
1721 ctx->hdrs = safe_calloc ((ctx->hdrmax += 25), sizeof (HEADER *));
1722 ctx->v2r = safe_calloc (ctx->hdrmax, sizeof (int));
1724 for (i = ctx->msgcount ; i < ctx->hdrmax ; i++)
1726 ctx->hdrs[i] = NULL;
1731 /* this routine is called to update the counts in the context structure for
1732 * the last message header parsed.
1734 void mx_update_context (CONTEXT *ctx, int new_messages)
1739 for (msgno = ctx->msgcount - new_messages; msgno < ctx->msgcount; msgno++)
1741 h = ctx->hdrs[msgno];
1745 /* NOTE: this _must_ be done before the check for mailcap! */
1746 h->security = crypt_query (h->content);
1751 ctx->v2r[ctx->vcount] = msgno;
1752 h->virtual = ctx->vcount++;
1758 if (h->env->supersedes)
1763 ctx->id_hash = mutt_make_id_hash (ctx);
1765 h2 = hash_find (ctx->id_hash, h->env->supersedes);
1767 /* FREE (&h->env->supersedes); should I ? */
1771 if (option (OPTSCORE))
1772 mutt_score_message (ctx, h2, 1);
1776 /* add this message to the hash tables */
1777 if (ctx->id_hash && h->env->message_id)
1778 hash_insert (ctx->id_hash, h->env->message_id, h, 0);
1779 if (ctx->subj_hash && h->env->real_subj)
1780 hash_insert (ctx->subj_hash, h->env->real_subj, h, 1);
1782 if (option (OPTSCORE))
1783 mutt_score_message (ctx, h, 0);
1802 * 1 if the specified mailbox contains 0 messages.
1803 * 0 if the mailbox contains messages
1806 int mx_check_empty (const char *path)
1808 switch (mx_get_magic (path))
1812 return mbox_check_empty (path);
1814 return mh_check_empty (path);
1816 return maildir_check_empty (path);