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 : "",
105 return mutt_system (cmd);
110 #define invoke_dotlock dotlock_invoke
114 static int dotlock_file (const char *path, int fd, int retry)
117 int flags = DL_FL_USEPRIV | DL_FL_RETRY;
119 if (retry) retry = 1;
122 if ((r = invoke_dotlock(path, fd, flags, retry)) == DL_EX_EXIST)
124 if (!option (OPTNOCURSES))
126 char msg[LONG_STRING];
128 snprintf(msg, sizeof(msg), _("Lock count exceeded, remove lock for %s?"),
130 if(retry && mutt_yesorno(msg, M_YES) == M_YES)
132 flags |= DL_FL_FORCE;
140 mutt_error ( _("Can't dotlock %s.\n"), path);
143 return (r == DL_EX_OK ? 0 : -1);
146 static int undotlock_file (const char *path, int fd)
148 return (invoke_dotlock(path, fd, DL_FL_USEPRIV | DL_FL_UNLOCK, 0) == DL_EX_OK ?
152 #endif /* USE_DOTLOCK */
155 * excl if excl != 0, request an exclusive lock
156 * dot if dot != 0, try to dotlock the file
157 * timeout should retry locking?
159 int mx_lock_file (const char *path, int fd, int excl, int dot, int timeout)
161 #if defined (USE_FCNTL) || defined (USE_FLOCK)
172 memset (&lck, 0, sizeof (struct flock));
173 lck.l_type = excl ? F_WRLCK : F_RDLCK;
174 lck.l_whence = SEEK_SET;
178 while (fcntl (fd, F_SETLK, &lck) == -1)
181 dprint(1,(debugfile, "mx_lock_file(): fcntl errno %d.\n", errno));
182 if (errno != EAGAIN && errno != EACCES)
184 mutt_perror ("fcntl");
188 if (fstat (fd, &sb) != 0)
194 /* only unlock file if it is unchanged */
195 if (prev_sb.st_size == sb.st_size && ++count >= (timeout?MAXLOCKATTEMPT:0))
198 mutt_error _("Timeout exceeded while attempting fcntl lock!");
204 mutt_message (_("Waiting for fcntl lock... %d"), ++attempt);
207 #endif /* USE_FCNTL */
212 while (flock (fd, (excl ? LOCK_EX : LOCK_SH) | LOCK_NB) == -1)
215 if (errno != EWOULDBLOCK)
217 mutt_perror ("flock");
222 if (fstat(fd,&sb) != 0 )
228 /* only unlock file if it is unchanged */
229 if (prev_sb.st_size == sb.st_size && ++count >= (timeout?MAXLOCKATTEMPT:0))
232 mutt_error _("Timeout exceeded while attempting flock lock!");
239 mutt_message (_("Waiting for flock attempt... %d"), ++attempt);
242 #endif /* USE_FLOCK */
246 r = dotlock_file (path, fd, timeout);
247 #endif /* USE_DOTLOCK */
251 /* release any other locks obtained in this routine */
254 lck.l_type = F_UNLCK;
255 fcntl (fd, F_SETLK, &lck);
256 #endif /* USE_FCNTL */
260 #endif /* USE_FLOCK */
268 int mx_unlock_file (const char *path, int fd, int dot)
271 struct flock unlockit = { F_UNLCK, 0, 0, 0 };
273 memset (&unlockit, 0, sizeof (struct flock));
274 unlockit.l_type = F_UNLCK;
275 unlockit.l_whence = SEEK_SET;
276 fcntl (fd, F_SETLK, &unlockit);
285 undotlock_file (path, fd);
291 void mx_unlink_empty (const char *path)
298 if ((fd = open (path, O_RDWR)) == -1)
301 if (mx_lock_file (path, fd, 1, 0, 1) == -1)
308 invoke_dotlock (path, fd, DL_FL_UNLINK, 1);
310 if (fstat (fd, &sb) == 0 && sb.st_size == 0)
314 mx_unlock_file (path, fd, 0);
318 /* try to figure out what type of mailbox ``path'' is
328 int mx_is_imap(const char *p)
338 scheme = url_check_scheme (p);
339 if (scheme == U_IMAP || scheme == U_IMAPS)
348 int mx_is_pop (const char *p)
355 scheme = url_check_scheme (p);
356 if (scheme == U_POP || scheme == U_POPS)
364 int mx_is_nntp (const char *p)
371 scheme = url_check_scheme (p);
372 if (scheme == U_NNTP || scheme == U_NNTPS)
379 int mx_get_magic (const char *path)
383 char tmp[_POSIX_PATH_MAX];
389 #endif /* USE_IMAP */
392 if (mx_is_pop (path))
397 if (mx_is_nntp (path))
399 #endif /* USE_NNTP */
401 if (stat (path, &st) == -1)
403 dprint (1, (debugfile, "mx_get_magic(): unable to stat %s: %s (errno %d).\n",
404 path, strerror (errno), errno));
408 if (S_ISDIR (st.st_mode))
410 /* check for maildir-style mailbox */
412 snprintf (tmp, sizeof (tmp), "%s/cur", path);
413 if (stat (tmp, &st) == 0 && S_ISDIR (st.st_mode))
416 /* check for mh-style mailbox */
418 snprintf (tmp, sizeof (tmp), "%s/.mh_sequences", path);
419 if (access (tmp, F_OK) == 0)
422 snprintf (tmp, sizeof (tmp), "%s/.xmhcache", 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/.mew-cache", path);
431 if (access (tmp, F_OK) == 0)
434 snprintf (tmp, sizeof (tmp), "%s/.sylpheed_cache", path);
435 if (access (tmp, F_OK) == 0)
439 * ok, this isn't an mh folder, but mh mode can be used to read
440 * Usenet news from the spool. ;-)
443 snprintf (tmp, sizeof (tmp), "%s/.overview", path);
444 if (access (tmp, F_OK) == 0)
448 else if (st.st_size == 0)
450 /* hard to tell what zero-length files are, so assume the default magic */
451 if (DefaultMagic == M_MBOX || DefaultMagic == M_MMDF)
452 return (DefaultMagic);
456 else if ((f = fopen (path, "r")) != NULL)
459 struct utimbuf times;
462 fgets (tmp, sizeof (tmp), f);
463 if (mutt_strncmp ("From ", tmp, 5) == 0)
465 else if (mutt_strcmp (MMDF_SEP, tmp) == 0)
469 /* need to restore the times here, the file was not really accessed,
470 * only the type was accessed. This is important, because detection
471 * of "new mail" depends on those times set correctly.
473 times.actime = st.st_atime;
474 times.modtime = st.st_mtime;
475 utime (path, ×);
480 dprint (1, (debugfile, "mx_get_magic(): unable to open file %s for reading.\n",
486 #ifdef USE_COMPRESSED
487 if (magic == 0 && mutt_can_read_compressed (path))
495 * set DefaultMagic to the given value
497 int mx_set_magic (const char *s)
499 if (ascii_strcasecmp (s, "mbox") == 0)
500 DefaultMagic = M_MBOX;
501 else if (ascii_strcasecmp (s, "mmdf") == 0)
502 DefaultMagic = M_MMDF;
503 else if (ascii_strcasecmp (s, "mh") == 0)
505 else if (ascii_strcasecmp (s, "maildir") == 0)
506 DefaultMagic = M_MAILDIR;
513 /* mx_access: Wrapper for access, checks permissions on a given mailbox.
514 * We may be interested in using ACL-style flags at some point, currently
515 * we use the normal access() flags. */
516 int mx_access (const char* path, int flags)
519 if (mx_is_imap (path))
520 return imap_access (path, flags);
523 return access (path, flags);
526 static int mx_open_mailbox_append (CONTEXT *ctx, int flags)
530 #ifdef USE_COMPRESSED
531 /* special case for appending to compressed folders -
532 * even if we can not open them for reading */
533 if (mutt_can_append_compressed (ctx->path))
534 mutt_open_append_compressed (ctx);
541 if(mx_is_imap(ctx->path))
542 return imap_open_mailbox_append (ctx);
546 if(stat(ctx->path, &sb) == 0)
548 ctx->magic = mx_get_magic (ctx->path);
553 mutt_error (_("%s is not a mailbox."), ctx->path);
559 else if (errno == ENOENT)
561 ctx->magic = DefaultMagic;
563 if (ctx->magic == M_MH || ctx->magic == M_MAILDIR)
565 char tmp[_POSIX_PATH_MAX];
567 if (mkdir (ctx->path, S_IRWXU))
569 mutt_perror (ctx->path);
573 if (ctx->magic == M_MAILDIR)
575 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
576 if (mkdir (tmp, S_IRWXU))
583 snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
584 if (mkdir (tmp, S_IRWXU))
587 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
592 snprintf (tmp, sizeof (tmp), "%s/tmp", ctx->path);
593 if (mkdir (tmp, S_IRWXU))
596 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
598 snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
608 snprintf (tmp, sizeof (tmp), "%s/.mh_sequences", ctx->path);
609 if ((i = creat (tmp, S_IRWXU)) == -1)
621 mutt_perror (ctx->path);
629 if ((ctx->fp = safe_fopen (ctx->path, flags & M_NEWFOLDER ? "w" : "a")) == NULL ||
630 mbox_lock_mailbox (ctx, 1, 1) != 0)
633 mutt_perror (ctx->path);
636 mutt_error (_("Couldn't lock %s\n"), ctx->path);
637 safe_fclose (&ctx->fp);
641 fseek (ctx->fp, 0, 2);
657 * open a mailbox and parse it
660 * flags M_NOSORT do not sort mailbox
661 * M_APPEND open mailbox for appending
662 * M_READONLY open mailbox in read-only mode
663 * M_QUIET only print error messages
664 * ctx if non-null, context struct to use
666 CONTEXT *mx_open_mailbox (const char *path, int flags, CONTEXT *pctx)
672 ctx = safe_malloc (sizeof (CONTEXT));
673 memset (ctx, 0, sizeof (CONTEXT));
674 ctx->path = safe_strdup (path);
676 ctx->msgnotreadyet = -1;
681 if (flags & M_READONLY)
684 if (flags & (M_APPEND|M_NEWFOLDER))
686 if (mx_open_mailbox_append (ctx, flags) != 0)
688 mx_fastclose_mailbox (ctx);
696 ctx->magic = mx_get_magic (path);
698 #ifdef USE_COMPRESSED
699 if (ctx->magic == M_COMPRESSED)
700 mutt_open_read_compressed (ctx);
704 mutt_error (_("%s is not a mailbox."), path);
711 mx_fastclose_mailbox (ctx);
717 /* if the user has a `push' command in their .muttrc, or in a folder-hook,
718 * it will cause the progress messages not to be displayed because
719 * mutt_refresh() will think we are in the middle of a macro. so set a
720 * flag to indicate that we should really refresh the screen.
722 set_option (OPTFORCEREFRESH);
725 mutt_message (_("Reading %s..."), ctx->path);
730 rc = mh_read_dir (ctx, NULL);
734 rc = maildir_read_dir (ctx);
739 rc = mbox_open_mailbox (ctx);
744 rc = imap_open_mailbox (ctx);
746 #endif /* USE_IMAP */
750 rc = pop_open_mailbox (ctx);
756 rc = nntp_open_mailbox (ctx);
758 #endif /* USE_NNTP */
767 if ((flags & M_NOSORT) == 0)
769 /* avoid unnecessary work since the mailbox is completely unthreaded
771 unset_option (OPTSORTSUBTHREADS);
772 unset_option (OPTNEEDRESCORE);
773 mutt_sort_headers (ctx, 1);
780 mx_fastclose_mailbox (ctx);
785 unset_option (OPTFORCEREFRESH);
789 /* free up memory associated with the mailbox context */
790 void mx_fastclose_mailbox (CONTEXT *ctx)
798 if (ctx->magic == M_IMAP)
799 imap_close_mailbox (ctx);
800 #endif /* USE_IMAP */
802 if (ctx->magic == M_POP)
803 pop_close_mailbox (ctx);
806 if (ctx->magic == M_NNTP)
807 nntp_fastclose_mailbox (ctx);
808 #endif /* USE_NNTP */
810 hash_destroy (&ctx->subj_hash, NULL);
812 hash_destroy (&ctx->id_hash, NULL);
813 mutt_clear_threads (ctx);
814 for (i = 0; i < ctx->msgcount; i++)
815 mutt_free_header (&ctx->hdrs[i]);
818 #ifdef USE_COMPRESSED
819 if (ctx->compressinfo)
820 mutt_fast_close_compressed (ctx);
823 FREE (&ctx->pattern);
824 if (ctx->limit_pattern)
825 mutt_pattern_free (&ctx->limit_pattern);
826 safe_fclose (&ctx->fp);
827 memset (ctx, 0, sizeof (CONTEXT));
830 /* save changes to disk */
831 static int sync_mailbox (CONTEXT *ctx, int *index_hint)
839 mutt_message (_("Writing %s..."), ctx->path);
845 rc = mbox_sync_mailbox (ctx, index_hint);
847 tmp = mutt_find_mailbox (ctx->path);
853 rc = mh_sync_mailbox (ctx, index_hint);
858 /* extra argument means EXPUNGE */
859 rc = imap_sync_mailbox (ctx, 1, index_hint);
861 #endif /* USE_IMAP */
865 rc = pop_sync_mailbox (ctx, index_hint);
871 rc = nntp_sync_mailbox (ctx);
873 #endif /* USE_NNTP */
877 if (!ctx->quiet && !ctx->shutup && rc == -1)
878 mutt_error ( _("Could not synchronize mailbox %s!"), ctx->path);
882 if (tmp && tmp->new == 0)
883 mutt_update_mailbox (tmp);
886 #ifdef USE_COMPRESSED
887 if (rc == 0 && ctx->compressinfo)
888 return mutt_sync_compressed (ctx);
894 /* move deleted mails to the trash folder */
895 static int trash_append (CONTEXT *ctx)
901 if (!TrashPath || !ctx->deleted ||
902 (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))
905 for (;i < ctx->msgcount && (!ctx->hdrs[i]->deleted ||
906 ctx->hdrs[i]->appended); i++);
907 if (i == ctx->msgcount)
908 return 0; /* nothing to be done */
910 if (mutt_save_confirm (TrashPath, &st) != 0)
912 mutt_error _("message(s) not deleted");
916 if (lstat (ctx->path, &stc) == 0 && stc.st_ino == st.st_ino
917 && stc.st_dev == st.st_dev && stc.st_rdev == st.st_rdev)
918 return 0; /* we are in the trash folder: simple sync */
920 if ((ctx_trash = mx_open_mailbox (TrashPath, M_APPEND, NULL)) != NULL)
922 for (i = 0 ; i < ctx->msgcount ; i++)
923 if (ctx->hdrs[i]->deleted && !ctx->hdrs[i]->appended
924 && !ctx->hdrs[i]->purged
925 && mutt_append_message (ctx_trash, ctx, ctx->hdrs[i], 0, 0) == -1)
927 mx_close_mailbox (ctx_trash, NULL);
931 mx_close_mailbox (ctx_trash, NULL);
935 mutt_error _("Can't open trash folder");
942 /* save changes and close mailbox */
943 int mx_close_mailbox (CONTEXT *ctx, int *index_hint)
945 int i, move_messages = 0, purge = 1, read_msgs = 0;
949 char mbox[_POSIX_PATH_MAX];
950 char buf[SHORT_STRING];
957 if (ctx->magic == M_NNTP)
961 ret = nntp_close_mailbox (ctx);
962 mx_fastclose_mailbox (ctx);
966 if (ctx->readonly || ctx->dontwrite)
968 /* mailbox is readonly or we don't want to write */
969 mx_fastclose_mailbox (ctx);
975 /* mailbox was opened in write-mode */
976 if (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
977 mbox_close_mailbox (ctx);
979 mx_fastclose_mailbox (ctx);
983 for (i = 0; i < ctx->msgcount; i++)
985 if (!ctx->hdrs[i]->deleted && ctx->hdrs[i]->read
986 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
990 if (read_msgs && quadoption (OPT_MOVE) != M_NO)
994 if ((p = mutt_find_hook (M_MBOXHOOK, ctx->path)))
997 strfcpy (mbox, p, sizeof (mbox));
1001 strfcpy (mbox, NONULL(Inbox), sizeof (mbox));
1002 isSpool = mutt_is_spool (ctx->path) && !mutt_is_spool (mbox);
1004 mutt_expand_path (mbox, sizeof (mbox));
1008 snprintf (buf, sizeof (buf), _("Move read messages to %s?"), mbox);
1009 if ((move_messages = query_quadoption (OPT_MOVE, buf)) == -1)
1018 * There is no point in asking whether or not to purge if we are
1019 * just marking messages as "trash".
1021 if (ctx->deleted && !(ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))
1023 snprintf (buf, sizeof (buf), ctx->deleted == 1
1024 ? _("Purge %d deleted message?") : _("Purge %d deleted messages?"),
1026 if ((purge = query_quadoption (OPT_DELETE, buf)) < 0)
1034 /* IMAP servers manage the OLD flag themselves */
1035 if (ctx->magic != M_IMAP)
1037 if (option (OPTMARKOLD))
1039 for (i = 0; i < ctx->msgcount; i++)
1041 if (!ctx->hdrs[i]->deleted && !ctx->hdrs[i]->old)
1042 mutt_set_flag (ctx, ctx->hdrs[i], M_OLD, 1);
1048 mutt_message (_("Moving read messages to %s..."), mbox);
1051 /* try to use server-side copy first */
1054 if (ctx->magic == M_IMAP && mx_is_imap (mbox))
1056 /* tag messages for moving, and clear old tags, if any */
1057 for (i = 0; i < ctx->msgcount; i++)
1058 if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted
1059 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
1060 ctx->hdrs[i]->tagged = 1;
1062 ctx->hdrs[i]->tagged = 0;
1064 i = imap_copy_messages (ctx, NULL, mbox, 1);
1067 if (i == 0) /* success */
1068 mutt_clear_error ();
1069 else if (i == -1) /* horrible error, bail */
1074 else /* use regular append-copy mode */
1077 if (mx_open_mailbox (mbox, M_APPEND, &f) == NULL)
1083 for (i = 0; i < ctx->msgcount; i++)
1085 if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted
1086 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
1088 if (mutt_append_message (&f, ctx, ctx->hdrs[i], 0, CH_UPDATE_LEN) == 0)
1090 mutt_set_flag (ctx, ctx->hdrs[i], M_DELETE, 1);
1091 mutt_set_flag (ctx, ctx->hdrs[i], M_APPENDED, 1);
1095 mx_close_mailbox (&f, NULL);
1102 mx_close_mailbox (&f, NULL);
1106 else if (!ctx->changed && ctx->deleted == 0)
1108 mutt_message _("Mailbox is unchanged.");
1109 mx_fastclose_mailbox (ctx);
1113 /* copy mails to the trash before expunging */
1114 if (purge && ctx->deleted)
1115 if (trash_append (ctx) != 0)
1122 /* allow IMAP to preserve the deleted flag across sessions */
1123 if (ctx->magic == M_IMAP)
1125 if ((check = imap_sync_mailbox (ctx, purge, index_hint)) != 0)
1136 for (i = 0; i < ctx->msgcount; i++)
1137 ctx->hdrs[i]->deleted = 0;
1141 if (ctx->changed || ctx->deleted)
1143 if ((check = sync_mailbox (ctx, index_hint)) != 0)
1152 mutt_message (_("%d kept, %d moved, %d deleted."),
1153 ctx->msgcount - ctx->deleted, read_msgs, ctx->deleted);
1155 mutt_message (_("%d kept, %d deleted."),
1156 ctx->msgcount - ctx->deleted, ctx->deleted);
1158 if (ctx->msgcount == ctx->deleted &&
1159 (ctx->magic == M_MMDF || ctx->magic == M_MBOX) &&
1160 !mutt_is_spool(ctx->path) && !option (OPTSAVEEMPTY))
1161 mx_unlink_empty (ctx->path);
1163 #ifdef USE_COMPRESSED
1164 if (ctx->compressinfo && mutt_slow_close_compressed (ctx))
1168 mx_fastclose_mailbox (ctx);
1174 /* update a Context structure's internal tables. */
1176 void mx_update_tables(CONTEXT *ctx, int committing)
1180 /* update memory to reflect the new state of the mailbox */
1189 #define this_body ctx->hdrs[j]->content
1190 for (i = 0, j = 0; i < ctx->msgcount; i++)
1192 if ((committing && (!ctx->hdrs[i]->deleted ||
1193 (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))) ||
1194 (!committing && ctx->hdrs[i]->active))
1198 ctx->hdrs[j] = ctx->hdrs[i];
1199 ctx->hdrs[i] = NULL;
1201 ctx->hdrs[j]->msgno = j;
1202 if (ctx->hdrs[j]->virtual != -1)
1204 ctx->v2r[ctx->vcount] = j;
1205 ctx->hdrs[j]->virtual = ctx->vcount++;
1206 ctx->vsize += this_body->length + this_body->offset -
1207 this_body->hdr_offset;
1211 ctx->hdrs[j]->changed = 0;
1212 else if (ctx->hdrs[j]->changed)
1215 if (!committing || (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))
1217 if (ctx->hdrs[j]->deleted)
1221 if (ctx->hdrs[j]->tagged)
1223 if (ctx->hdrs[j]->flagged)
1225 if (!ctx->hdrs[j]->read)
1228 if (!ctx->hdrs[j]->old)
1236 if (ctx->magic == M_MH || ctx->magic == M_MAILDIR)
1237 ctx->size -= (ctx->hdrs[i]->content->length +
1238 ctx->hdrs[i]->content->offset -
1239 ctx->hdrs[i]->content->hdr_offset);
1240 /* remove message from the hash tables */
1241 if (ctx->subj_hash && ctx->hdrs[i]->env->real_subj)
1242 hash_delete (ctx->subj_hash, ctx->hdrs[i]->env->real_subj, ctx->hdrs[i], NULL);
1243 if (ctx->id_hash && ctx->hdrs[i]->env->message_id)
1244 hash_delete (ctx->id_hash, ctx->hdrs[i]->env->message_id, ctx->hdrs[i], NULL);
1245 mutt_free_header (&ctx->hdrs[i]);
1253 /* save changes to mailbox
1259 int mx_sync_mailbox (CONTEXT *ctx, int *index_hint)
1263 int msgcount, deleted;
1267 char buf[STRING], tmp[STRING];
1268 if (km_expand_key (buf, sizeof(buf),
1269 km_find_func (MENU_MAIN, OP_TOGGLE_WRITE)))
1270 snprintf (tmp, sizeof(tmp), _(" Press '%s' to toggle write"), buf);
1272 strfcpy (tmp, _("Use 'toggle-write' to re-enable write!"), sizeof(tmp));
1274 mutt_error (_("Mailbox is marked unwritable. %s"), tmp);
1277 else if (ctx->readonly)
1279 mutt_error _("Mailbox is read-only.");
1283 if (!ctx->changed && !ctx->deleted)
1285 mutt_message _("Mailbox is unchanged.");
1291 char buf[SHORT_STRING];
1293 snprintf (buf, sizeof (buf), ctx->deleted == 1
1294 ? _("Purge %d deleted message?") : _("Purge %d deleted messages?"),
1296 if ((purge = query_quadoption (OPT_DELETE, buf)) < 0)
1298 else if (purge == M_NO)
1301 return 0; /* nothing to do! */
1303 /* let IMAP servers hold on to D flags */
1304 if (ctx->magic != M_IMAP)
1307 for (i = 0 ; i < ctx->msgcount ; i++)
1308 ctx->hdrs[i]->deleted = 0;
1312 else if (ctx->last_tag && ctx->last_tag->deleted)
1313 ctx->last_tag = NULL; /* reset last tagged msg now useless */
1316 /* really only for IMAP - imap_sync_mailbox results in a call to
1317 * mx_update_tables, so ctx->deleted is 0 when it comes back */
1318 msgcount = ctx->msgcount;
1319 deleted = ctx->deleted;
1321 if (purge && ctx->deleted)
1323 if (trash_append (ctx) == -1)
1328 if (ctx->magic == M_IMAP)
1329 rc = imap_sync_mailbox (ctx, purge, index_hint);
1332 rc = sync_mailbox (ctx, index_hint);
1336 if (ctx->magic == M_IMAP && !purge)
1337 mutt_message _("Mailbox checkpointed.");
1340 mutt_message (_("%d kept, %d deleted."), msgcount - deleted,
1345 if (ctx->msgcount == ctx->deleted &&
1346 (ctx->magic == M_MBOX || ctx->magic == M_MMDF) &&
1347 !mutt_is_spool (ctx->path) && !option (OPTSAVEEMPTY))
1350 mx_fastclose_mailbox (ctx);
1354 /* if we haven't deleted any messages, we don't need to resort */
1355 /* ... except for certain folder formats which need "unsorted"
1356 * sort order in order to synchronize folders.
1358 * MH and maildir are safe. mbox-style seems to need re-sorting,
1359 * at least with the new threading code.
1361 if (purge || (ctx->magic != M_MAILDIR && ctx->magic != M_MH))
1364 /* IMAP does this automatically after handling EXPUNGE */
1365 if (ctx->magic != M_IMAP)
1368 mx_update_tables (ctx, 1);
1369 mutt_sort_headers (ctx, 1); /* rethread from scratch */
1378 /* {maildir,mh}_open_new_message are in mh.c. */
1380 int mbox_open_new_message (MESSAGE *msg, CONTEXT *dest, HEADER *hdr)
1387 int imap_open_new_message (MESSAGE *msg, CONTEXT *dest, HEADER *hdr)
1389 char tmp[_POSIX_PATH_MAX];
1392 if ((msg->fp = safe_fopen (tmp, "w")) == NULL)
1394 msg->path = safe_strdup(tmp);
1400 * dest destintation mailbox
1401 * hdr message being copied (required for maildir support, because
1402 * the filename depends on the message flags)
1404 MESSAGE *mx_open_new_message (CONTEXT *dest, HEADER *hdr, int flags)
1407 int (*func) (MESSAGE *, CONTEXT *, HEADER *);
1410 switch (dest->magic)
1414 func = mbox_open_new_message;
1417 func = maildir_open_new_message;
1420 func = mh_open_new_message;
1424 func = imap_open_new_message;
1428 dprint (1, (debugfile, "mx_open_new_message(): function unimplemented for mailbox type %d.\n",
1433 msg = safe_calloc (1, sizeof (MESSAGE));
1434 msg->magic = dest->magic;
1439 msg->flags.flagged = hdr->flagged;
1440 msg->flags.replied = hdr->replied;
1441 msg->flags.read = hdr->read;
1442 msg->received = hdr->received;
1445 if(msg->received == 0)
1446 time(&msg->received);
1448 if (func (msg, dest, hdr) == 0)
1450 if (dest->magic == M_MMDF)
1451 fputs (MMDF_SEP, msg->fp);
1453 if ((msg->magic == M_MBOX || msg->magic == M_MMDF) &&
1458 if (hdr->env->return_path)
1459 p = hdr->env->return_path;
1460 else if (hdr->env->sender)
1461 p = hdr->env->sender;
1466 fprintf (msg->fp, "From %s %s", p ? p->mailbox : NONULL(Username), ctime (&msg->received));
1475 /* check for new mail */
1476 int mx_check_mailbox (CONTEXT *ctx, int *index_hint, int lock)
1480 #ifdef USE_COMPRESSED
1481 if (ctx->compressinfo)
1482 return mutt_check_mailbox_compressed (ctx);
1487 if (ctx->locked) lock = 0;
1496 mutt_block_signals ();
1497 if (mbox_lock_mailbox (ctx, 0, 0) == -1)
1499 mutt_unblock_signals ();
1504 rc = mbox_check_mailbox (ctx, index_hint);
1508 mutt_unblock_signals ();
1509 mbox_unlock_mailbox (ctx);
1516 return (mh_check_mailbox (ctx, index_hint));
1518 return (maildir_check_mailbox (ctx, index_hint));
1522 return (imap_check_mailbox (ctx, index_hint, 0));
1523 #endif /* USE_IMAP */
1527 return (pop_check_mailbox (ctx, index_hint));
1528 #endif /* USE_POP */
1532 return (nntp_check_mailbox (ctx));
1533 #endif /* USE_NNTP */
1537 dprint (1, (debugfile, "mx_check_mailbox: null or invalid context.\n"));
1541 /* return a stream pointer for a message */
1542 MESSAGE *mx_open_message (CONTEXT *ctx, int msgno)
1546 msg = safe_calloc (1, sizeof (MESSAGE));
1547 switch (msg->magic = ctx->magic)
1557 HEADER *cur = ctx->hdrs[msgno];
1558 char path[_POSIX_PATH_MAX];
1560 snprintf (path, sizeof (path), "%s/%s", ctx->path, cur->path);
1562 if ((msg->fp = fopen (path, "r")) == NULL && errno == ENOENT &&
1563 ctx->magic == M_MAILDIR)
1564 msg->fp = maildir_open_find_message (ctx->path, cur->path);
1566 if (msg->fp == NULL)
1569 dprint (1, (debugfile, "mx_open_message: fopen: %s: %s (errno %d).\n",
1570 path, strerror (errno), errno));
1579 if (imap_fetch_message (msg, ctx, msgno) != 0)
1583 #endif /* USE_IMAP */
1588 if (pop_fetch_message (msg, ctx, msgno) != 0)
1592 #endif /* USE_POP */
1597 if (nntp_fetch_message (msg, ctx, msgno) != 0)
1601 #endif /* USE_NNTP */
1604 dprint (1, (debugfile, "mx_open_message(): function not implemented for mailbox type %d.\n", ctx->magic));
1611 /* commit a message to a folder */
1613 int mx_commit_message (MESSAGE *msg, CONTEXT *ctx)
1617 if (!(msg->write && ctx->append))
1619 dprint (1, (debugfile, "mx_commit_message(): msg->write = %d, ctx->append = %d\n",
1620 msg->write, ctx->append));
1628 if (fputs (MMDF_SEP, msg->fp) == EOF)
1635 if (fputc ('\n', msg->fp) == EOF)
1643 if ((r = safe_fclose (&msg->fp)) == 0)
1644 r = imap_append_message (ctx, msg);
1651 r = maildir_commit_message (ctx, msg, NULL);
1657 r = mh_commit_message (ctx, msg, NULL);
1662 if (r == 0 && (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
1663 && (fflush (msg->fp) == EOF || fsync (fileno (msg->fp)) == -1))
1665 mutt_perror _("Can't write message");
1672 /* close a pointer to a message */
1673 int mx_close_message (MESSAGE **msg)
1677 if ((*msg)->magic == M_MH || (*msg)->magic == M_MAILDIR
1679 || (*msg)->magic == M_IMAP
1682 || (*msg)->magic == M_POP
1685 || (*msg)->magic == M_NNTP
1689 r = safe_fclose (&(*msg)->fp);
1696 dprint (1, (debugfile, "mx_close_message (): unlinking %s\n",
1698 unlink ((*msg)->path);
1699 FREE (&(*msg)->path);
1706 void mx_alloc_memory (CONTEXT *ctx)
1709 size_t s = MAX (sizeof (HEADER *), sizeof (int));
1711 if ((ctx->hdrmax + 25) * s < ctx->hdrmax * s)
1713 mutt_error _("Integer overflow -- can't allocate memory.");
1720 safe_realloc (&ctx->hdrs, sizeof (HEADER *) * (ctx->hdrmax += 25));
1721 safe_realloc (&ctx->v2r, sizeof (int) * ctx->hdrmax);
1725 ctx->hdrs = safe_calloc ((ctx->hdrmax += 25), sizeof (HEADER *));
1726 ctx->v2r = safe_calloc (ctx->hdrmax, sizeof (int));
1728 for (i = ctx->msgcount ; i < ctx->hdrmax ; i++)
1730 ctx->hdrs[i] = NULL;
1735 /* this routine is called to update the counts in the context structure for
1736 * the last message header parsed.
1738 void mx_update_context (CONTEXT *ctx, int new_messages)
1743 for (msgno = ctx->msgcount - new_messages; msgno < ctx->msgcount; msgno++)
1745 h = ctx->hdrs[msgno];
1749 /* NOTE: this _must_ be done before the check for mailcap! */
1750 h->security = crypt_query (h->content);
1755 ctx->v2r[ctx->vcount] = msgno;
1756 h->virtual = ctx->vcount++;
1762 if (h->env->supersedes)
1767 ctx->id_hash = mutt_make_id_hash (ctx);
1769 h2 = hash_find (ctx->id_hash, h->env->supersedes);
1771 /* FREE (&h->env->supersedes); should I ? */
1775 if (option (OPTSCORE))
1776 mutt_score_message (ctx, h2, 1);
1780 /* add this message to the hash tables */
1781 if (ctx->id_hash && h->env->message_id)
1782 hash_insert (ctx->id_hash, h->env->message_id, h, 0);
1783 if (ctx->subj_hash && h->env->real_subj)
1784 hash_insert (ctx->subj_hash, h->env->real_subj, h, 1);
1786 if (option (OPTSCORE))
1787 mutt_score_message (ctx, h, 0);
1806 * 1 if the specified mailbox contains 0 messages.
1807 * 0 if the mailbox contains messages
1810 int mx_check_empty (const char *path)
1812 switch (mx_get_magic (path))
1816 return mbox_check_empty (path);
1818 return mh_check_empty (path);
1820 return maildir_check_empty (path);