2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2002 Michael R. Elkins <me@mutt.org>
4 * Copyright (C) 1999-2000 Thomas Roessler <roessler@does-not-exist.org>
6 * This file is part of mutt-ng, see http://www.muttng.org/.
7 * It's licensed under the GNU General Public License,
8 * please see the file GPL in the top level source directory.
34 #include "imap/imap.h"
35 #include "imap/mx_imap.h"
40 #include "pop/mx_pop.h"
44 #include "nntp/nntp.h"
45 #include "nntp/mx_nntp.h"
52 #include "mutt_crypt.h"
58 #include "lib/debug.h"
71 static list2_t* MailboxFormats = NULL;
72 #define MX_COMMAND(idx,cmd) ((mx_t*) MailboxFormats->data[idx])->cmd
73 #define MX_IDX(idx) (idx >= 0 && idx < MailboxFormats->length)
75 #define mutt_is_spool(s) (str_cmp (Spoolfile, s) == 0)
80 * retry - should retry if unable to lock?
85 static int invoke_dotlock (const char *path, int dummy, int flags, int retry)
87 char cmd[LONG_STRING + _POSIX_PATH_MAX];
88 char f[SHORT_STRING + _POSIX_PATH_MAX];
91 if (flags & DL_FL_RETRY)
92 snprintf (r, sizeof (r), "-r %d ", retry ? MAXLOCKATTEMPT : 0);
94 mutt_quote_filename (f, sizeof (f), path);
96 snprintf (cmd, sizeof (cmd),
99 flags & DL_FL_TRY ? "-t " : "",
100 flags & DL_FL_UNLOCK ? "-u " : "",
101 flags & DL_FL_USEPRIV ? "-p " : "",
102 flags & DL_FL_FORCE ? "-f " : "",
103 flags & DL_FL_UNLINK ? "-d " : "",
104 flags & DL_FL_RETRY ? r : "", f);
106 return mutt_system (cmd);
111 #define invoke_dotlock dotlock_invoke
115 static int dotlock_file (const char *path, int fd, int retry)
118 int flags = DL_FL_USEPRIV | DL_FL_RETRY;
124 if ((r = invoke_dotlock (path, fd, flags, retry)) == DL_EX_EXIST) {
125 if (!option (OPTNOCURSES)) {
126 char msg[LONG_STRING];
128 snprintf (msg, sizeof (msg),
129 _("Lock count exceeded, remove lock for %s?"), path);
130 if (retry && mutt_yesorno (msg, M_YES) == M_YES) {
131 flags |= DL_FL_FORCE;
138 mutt_error (_("Can't dotlock %s.\n"), path);
141 return (r == DL_EX_OK ? 0 : -1);
144 static int undotlock_file (const char *path, int fd)
146 return (invoke_dotlock (path, fd, DL_FL_USEPRIV | DL_FL_UNLOCK, 0) ==
150 #endif /* USE_DOTLOCK */
152 /* looks up index of type for path in MailboxFormats */
153 static int mx_get_idx (const char* path) {
157 /* first, test all non-local folders to avoid stat() call */
158 for (i = 0; i < MailboxFormats->length; i++) {
159 if (!MX_COMMAND(i,local))
160 t = MX_COMMAND(i,mx_is_magic)(path, NULL);
164 if (stat (path, &st) == 0) {
165 /* if stat() succeeded, keep testing until success and
166 * pass stat() info so that we only need to do it once */
167 for (i = 0; i < MailboxFormats->length; i++) {
168 if (MX_COMMAND(i,local))
169 t = MX_COMMAND(i,mx_is_magic)(path, &st);
178 * excl if excl != 0, request an exclusive lock
179 * dot if dot != 0, try to dotlock the file
180 * timeout should retry locking?
182 int mx_lock_file (const char *path, int fd, int excl, int dot, int timeout)
184 #if defined (USE_FCNTL) || defined (USE_FLOCK)
195 memset (&lck, 0, sizeof (struct flock));
196 lck.l_type = excl ? F_WRLCK : F_RDLCK;
197 lck.l_whence = SEEK_SET;
202 while (fcntl (fd, F_SETLK, &lck) == -1) {
205 debug_print (1, ("fcntl errno %d.\n", errno));
206 if (errno != EAGAIN && errno != EACCES) {
207 mutt_perror ("fcntl");
211 if (fstat (fd, &sb) != 0)
217 /* only unlock file if it is unchanged */
218 if (prev_sb.st_size == sb.st_size
219 && ++count >= (timeout ? MAXLOCKATTEMPT : 0)) {
221 mutt_error _("Timeout exceeded while attempting fcntl lock!");
228 mutt_message (_("Waiting for fcntl lock... %d"), ++attempt);
231 #endif /* USE_FCNTL */
236 while (flock (fd, (excl ? LOCK_EX : LOCK_SH) | LOCK_NB) == -1) {
239 if (errno != EWOULDBLOCK) {
240 mutt_perror ("flock");
245 if (fstat (fd, &sb) != 0)
251 /* only unlock file if it is unchanged */
252 if (prev_sb.st_size == sb.st_size
253 && ++count >= (timeout ? MAXLOCKATTEMPT : 0)) {
255 mutt_error _("Timeout exceeded while attempting flock lock!");
263 mutt_message (_("Waiting for flock attempt... %d"), ++attempt);
266 #endif /* USE_FLOCK */
270 r = dotlock_file (path, fd, timeout);
271 #endif /* USE_DOTLOCK */
274 /* release any other locks obtained in this routine */
277 lck.l_type = F_UNLCK;
278 fcntl (fd, F_SETLK, &lck);
279 #endif /* USE_FCNTL */
283 #endif /* USE_FLOCK */
291 int mx_unlock_file (const char *path, int fd, int dot)
294 struct flock unlockit = { F_UNLCK, 0, 0, 0 };
296 memset (&unlockit, 0, sizeof (struct flock));
297 unlockit.l_type = F_UNLCK;
298 unlockit.l_whence = SEEK_SET;
299 fcntl (fd, F_SETLK, &unlockit);
308 undotlock_file (path, fd);
314 void mx_unlink_empty (const char *path)
322 if ((fd = open (path, O_RDWR)) == -1)
325 if (mx_lock_file (path, fd, 1, 0, 1) == -1) {
331 invoke_dotlock (path, fd, DL_FL_UNLINK, 1);
333 if (fstat (fd, &sb) == 0 && sb.st_size == 0)
337 mx_unlock_file (path, fd, 0);
341 /* try to figure out what type of mailbox ``path'' is */
342 int mx_get_magic (const char *path) {
345 if (str_len (path) == 0)
347 if ((i = mx_get_idx (path)) >= 0)
348 return (MX_COMMAND(i,type));
352 int mx_is_local (int m) {
355 return (MX_COMMAND(m,local));
359 * set DefaultMagic to the given value
361 int mx_set_magic (const char *s)
363 if (ascii_strcasecmp (s, "mbox") == 0)
364 DefaultMagic = M_MBOX;
365 else if (ascii_strcasecmp (s, "mmdf") == 0)
366 DefaultMagic = M_MMDF;
367 else if (ascii_strcasecmp (s, "mh") == 0)
369 else if (ascii_strcasecmp (s, "maildir") == 0)
370 DefaultMagic = M_MAILDIR;
377 /* mx_access: Wrapper for access, checks permissions on a given mailbox.
378 * We may be interested in using ACL-style flags at some point, currently
379 * we use the normal access() flags. */
380 int mx_access (const char *path, int flags)
384 if ((i = mx_get_idx (path)) >= 0 && MX_COMMAND(i,mx_access))
385 return (MX_COMMAND(i,mx_access)(path,flags));
389 static int mx_open_mailbox_append (CONTEXT * ctx, int flags)
393 #ifdef USE_COMPRESSED
394 /* special case for appending to compressed folders -
395 * even if we can not open them for reading */
396 if (mutt_can_append_compressed (ctx->path))
397 mutt_open_append_compressed (ctx);
404 if (mx_get_magic (ctx->path) == M_IMAP)
405 return imap_open_mailbox_append (ctx);
409 if (stat (ctx->path, &sb) == 0) {
410 ctx->magic = mx_get_magic (ctx->path);
412 switch (ctx->magic) {
414 mutt_error (_("%s is not a mailbox."), ctx->path);
420 else if (errno == ENOENT) {
421 ctx->magic = DefaultMagic;
423 if (ctx->magic == M_MH || ctx->magic == M_MAILDIR) {
424 char tmp[_POSIX_PATH_MAX];
426 if (mkdir (ctx->path, S_IRWXU)) {
427 mutt_perror (ctx->path);
431 if (ctx->magic == M_MAILDIR) {
432 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
433 if (mkdir (tmp, S_IRWXU)) {
439 snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
440 if (mkdir (tmp, S_IRWXU)) {
442 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
447 snprintf (tmp, sizeof (tmp), "%s/tmp", ctx->path);
448 if (mkdir (tmp, S_IRWXU)) {
450 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
452 snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
461 snprintf (tmp, sizeof (tmp), "%s/.mh_sequences", ctx->path);
462 if ((i = creat (tmp, S_IRWXU)) == -1) {
472 mutt_perror (ctx->path);
476 switch (ctx->magic) {
480 safe_fopen (ctx->path, flags & M_NEWFOLDER ? "w" : "a")) == NULL
481 || mbox_lock_mailbox (ctx, 1, 1) != 0) {
483 mutt_perror (ctx->path);
485 mutt_error (_("Couldn't lock %s\n"), ctx->path);
486 safe_fclose (&ctx->fp);
490 fseeko (ctx->fp, 0, 2);
506 * open a mailbox and parse it
509 * flags M_NOSORT do not sort mailbox
510 * M_APPEND open mailbox for appending
511 * M_READONLY open mailbox in read-only mode
512 * M_QUIET only print error messages
513 * ctx if non-null, context struct to use
515 CONTEXT *mx_open_mailbox (const char *path, int flags, CONTEXT * pctx)
521 ctx = mem_malloc (sizeof (CONTEXT));
522 memset (ctx, 0, sizeof (CONTEXT));
523 ctx->path = str_dup (path);
525 ctx->msgnotreadyet = -1;
530 if (flags & M_READONLY)
535 if (flags & (M_APPEND | M_NEWFOLDER)) {
536 if (mx_open_mailbox_append (ctx, flags) != 0) {
537 mx_fastclose_mailbox (ctx);
545 if (!MX_IDX(ctx->magic-1))
546 ctx->magic = mx_get_magic (path);
548 #ifdef USE_COMPRESSED
549 if (ctx->magic == M_COMPRESSED)
550 mutt_open_read_compressed (ctx);
554 mutt_error (_("%s is not a mailbox."), path);
556 if (ctx->magic == -1)
559 if (ctx->magic <= 0) {
560 mx_fastclose_mailbox (ctx);
566 /* if the user has a `push' command in their .muttrc, or in a folder-hook,
567 * it will cause the progress messages not to be displayed because
568 * mutt_refresh() will think we are in the middle of a macro. so set a
569 * flag to indicate that we should really refresh the screen.
571 set_option (OPTFORCEREFRESH);
574 mutt_message (_("Reading %s..."), ctx->path);
576 rc = MX_COMMAND(ctx->magic-1,mx_open_mailbox)(ctx);
579 if ((flags & M_NOSORT) == 0) {
580 /* avoid unnecessary work since the mailbox is completely unthreaded
582 unset_option (OPTSORTSUBTHREADS);
583 unset_option (OPTNEEDRESCORE);
584 mutt_sort_headers (ctx, 1);
590 mx_fastclose_mailbox (ctx);
595 unset_option (OPTFORCEREFRESH);
599 /* free up memory associated with the mailbox context */
600 void mx_fastclose_mailbox (CONTEXT * ctx)
607 if (MX_IDX(ctx->magic-1) && MX_COMMAND(ctx->magic-1,mx_fastclose_mailbox))
608 MX_COMMAND(ctx->magic-1,mx_fastclose_mailbox(ctx));
610 hash_destroy (&ctx->subj_hash, NULL);
612 hash_destroy (&ctx->id_hash, NULL);
613 mutt_clear_threads (ctx);
614 for (i = 0; i < ctx->msgcount; i++)
615 mutt_free_header (&ctx->hdrs[i]);
616 mem_free (&ctx->hdrs);
617 mem_free (&ctx->v2r);
618 #ifdef USE_COMPRESSED
619 if (ctx->compressinfo)
620 mutt_fast_close_compressed (ctx);
622 mem_free (&ctx->path);
623 mem_free (&ctx->pattern);
624 if (ctx->limit_pattern)
625 mutt_pattern_free (&ctx->limit_pattern);
626 safe_fclose (&ctx->fp);
627 memset (ctx, 0, sizeof (CONTEXT));
630 /* save changes to disk */
631 static int sync_mailbox (CONTEXT * ctx, int *index_hint)
636 mutt_message (_("Writing %s..."), ctx->path);
638 if (MX_IDX(ctx->magic-1))
639 /* the 1 is only of interest for IMAP and means EXPUNGE */
640 rc = MX_COMMAND(ctx->magic-1,mx_sync_mailbox(ctx,1,index_hint));
642 #ifdef USE_COMPRESSED
643 if (rc == 0 && ctx->compressinfo)
644 return mutt_sync_compressed (ctx);
650 /* move deleted mails to the trash folder */
651 static int trash_append (CONTEXT * ctx)
657 if (!TrashPath || !ctx->deleted ||
658 (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))
661 for (; i < ctx->msgcount && (!ctx->hdrs[i]->deleted ||
662 ctx->hdrs[i]->appended); i++);
663 if (i == ctx->msgcount)
664 return 0; /* nothing to be done */
666 if (mutt_save_confirm (TrashPath, &st) != 0) {
667 mutt_error _("message(s) not deleted");
672 if (lstat (ctx->path, &stc) == 0 && stc.st_ino == st.st_ino
673 && stc.st_dev == st.st_dev && stc.st_rdev == st.st_rdev)
674 return 0; /* we are in the trash folder: simple sync */
676 if ((ctx_trash = mx_open_mailbox (TrashPath, M_APPEND, NULL)) != NULL) {
677 for (i = 0; i < ctx->msgcount; i++)
678 if (ctx->hdrs[i]->deleted && !ctx->hdrs[i]->appended
679 && !ctx->hdrs[i]->purged
680 && mutt_append_message (ctx_trash, ctx, ctx->hdrs[i], 0, 0) == -1) {
681 mx_close_mailbox (ctx_trash, NULL);
685 mx_close_mailbox (ctx_trash, NULL);
688 mutt_error _("Can't open trash folder");
696 /* save changes and close mailbox */
697 static int _mx_close_mailbox (CONTEXT * ctx, int *index_hint)
699 int i, move_messages = 0, purge = 1, read_msgs = 0;
703 char mbox[_POSIX_PATH_MAX];
704 char buf[SHORT_STRING];
712 if (ctx->magic == M_NNTP) {
715 ret = nntp_close_mailbox (ctx);
716 mx_fastclose_mailbox (ctx);
720 if (ctx->readonly || ctx->dontwrite) {
721 /* mailbox is readonly or we don't want to write */
722 mx_fastclose_mailbox (ctx);
727 /* mailbox was opened in write-mode */
728 if (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
729 mbox_close_mailbox (ctx);
731 mx_fastclose_mailbox (ctx);
735 for (i = 0; i < ctx->msgcount; i++) {
736 if (!ctx->hdrs[i]->deleted && ctx->hdrs[i]->read
737 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
741 if (read_msgs && quadoption (OPT_MOVE) != M_NO) {
744 if ((p = mutt_find_hook (M_MBOXHOOK, ctx->path))) {
746 strfcpy (mbox, p, sizeof (mbox));
749 strfcpy (mbox, NONULL (Inbox), sizeof (mbox));
750 isSpool = mutt_is_spool (ctx->path) && !mutt_is_spool (mbox);
752 mutt_expand_path (mbox, sizeof (mbox));
755 snprintf (buf, sizeof (buf), _("Move read messages to %s?"), mbox);
756 if ((move_messages = query_quadoption (OPT_MOVE, buf)) == -1) {
764 * There is no point in asking whether or not to purge if we are
765 * just marking messages as "trash".
767 if (ctx->deleted && !(ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH))) {
768 snprintf (buf, sizeof (buf), ctx->deleted == 1
769 ? _("Purge %d deleted message?") :
770 _("Purge %d deleted messages?"), ctx->deleted);
771 if ((purge = query_quadoption (OPT_DELETE, buf)) < 0) {
778 /* IMAP servers manage the OLD flag themselves */
779 if (ctx->magic != M_IMAP)
781 if (option (OPTMARKOLD)) {
782 for (i = 0; i < ctx->msgcount; i++) {
783 if (!ctx->hdrs[i]->deleted && !ctx->hdrs[i]->old)
784 mutt_set_flag (ctx, ctx->hdrs[i], M_OLD, 1);
789 mutt_message (_("Moving read messages to %s..."), mbox);
792 /* try to use server-side copy first */
795 if (ctx->magic == M_IMAP && imap_is_magic (mbox, NULL) == M_IMAP) {
796 /* tag messages for moving, and clear old tags, if any */
797 for (i = 0; i < ctx->msgcount; i++)
798 if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted
799 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
800 ctx->hdrs[i]->tagged = 1;
802 ctx->hdrs[i]->tagged = 0;
804 i = imap_copy_messages (ctx, NULL, mbox, 1);
807 if (i == 0) /* success */
809 else if (i == -1) { /* horrible error, bail */
813 else /* use regular append-copy mode */
816 if (mx_open_mailbox (mbox, M_APPEND, &f) == NULL) {
821 for (i = 0; i < ctx->msgcount; i++) {
822 if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted
823 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED))) {
824 if (mutt_append_message (&f, ctx, ctx->hdrs[i], 0, CH_UPDATE_LEN) ==
826 mutt_set_flag (ctx, ctx->hdrs[i], M_DELETE, 1);
827 mutt_set_flag (ctx, ctx->hdrs[i], M_APPENDED, 1);
830 mx_close_mailbox (&f, NULL);
837 mx_close_mailbox (&f, NULL);
841 else if (!ctx->changed && ctx->deleted == 0) {
842 mutt_message _("Mailbox is unchanged.");
844 mx_fastclose_mailbox (ctx);
848 /* copy mails to the trash before expunging */
849 if (purge && ctx->deleted)
850 if (trash_append (ctx) != 0) {
856 /* allow IMAP to preserve the deleted flag across sessions */
857 if (ctx->magic == M_IMAP) {
858 if ((check = imap_sync_mailbox (ctx, purge, index_hint)) != 0) {
867 for (i = 0; i < ctx->msgcount; i++)
868 ctx->hdrs[i]->deleted = 0;
872 if (ctx->changed || ctx->deleted) {
873 if ((check = sync_mailbox (ctx, index_hint)) != 0) {
881 mutt_message (_("%d kept, %d moved, %d deleted."),
882 ctx->msgcount - ctx->deleted, read_msgs, ctx->deleted);
884 mutt_message (_("%d kept, %d deleted."),
885 ctx->msgcount - ctx->deleted, ctx->deleted);
887 if (ctx->msgcount == ctx->deleted &&
888 (ctx->magic == M_MMDF || ctx->magic == M_MBOX) &&
889 !mutt_is_spool (ctx->path) && !option (OPTSAVEEMPTY))
890 mx_unlink_empty (ctx->path);
892 #ifdef USE_COMPRESSED
893 if (ctx->compressinfo && mutt_slow_close_compressed (ctx))
897 mx_fastclose_mailbox (ctx);
902 int mx_close_mailbox (CONTEXT * ctx, int *index_hint) {
906 ret = _mx_close_mailbox (ctx, index_hint);
907 sidebar_set_buffystats (ctx);
911 /* update a Context structure's internal tables. */
913 void mx_update_tables (CONTEXT * ctx, int committing)
917 /* update memory to reflect the new state of the mailbox */
926 #define this_body ctx->hdrs[j]->content
927 for (i = 0, j = 0; i < ctx->msgcount; i++) {
928 if ((committing && (!ctx->hdrs[i]->deleted ||
929 (ctx->magic == M_MAILDIR
930 && option (OPTMAILDIRTRASH)))) || (!committing
934 ctx->hdrs[j] = ctx->hdrs[i];
937 ctx->hdrs[j]->msgno = j;
938 if (ctx->hdrs[j]->virtual != -1) {
939 ctx->v2r[ctx->vcount] = j;
940 ctx->hdrs[j]->virtual = ctx->vcount++;
941 ctx->vsize += this_body->length + this_body->offset -
942 this_body->hdr_offset;
946 ctx->hdrs[j]->changed = 0;
947 else if (ctx->hdrs[j]->changed)
951 || (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH))) {
952 if (ctx->hdrs[j]->deleted)
956 if (ctx->hdrs[j]->tagged)
958 if (ctx->hdrs[j]->flagged)
960 if (!ctx->hdrs[j]->read) {
962 if (!ctx->hdrs[j]->old)
969 if (ctx->magic == M_MH || ctx->magic == M_MAILDIR)
970 ctx->size -= (ctx->hdrs[i]->content->length +
971 ctx->hdrs[i]->content->offset -
972 ctx->hdrs[i]->content->hdr_offset);
973 /* remove message from the hash tables */
974 if (ctx->subj_hash && ctx->hdrs[i]->env->real_subj)
975 hash_delete (ctx->subj_hash, ctx->hdrs[i]->env->real_subj,
977 if (ctx->id_hash && ctx->hdrs[i]->env->message_id)
978 hash_delete (ctx->id_hash, ctx->hdrs[i]->env->message_id,
980 mutt_free_header (&ctx->hdrs[i]);
986 /* update sidebar count */
987 sidebar_set_buffystats (ctx);
991 /* save changes to mailbox
997 static int _mx_sync_mailbox (CONTEXT * ctx, int *index_hint)
1001 int msgcount, deleted;
1003 if (ctx->dontwrite) {
1004 char buf[STRING], tmp[STRING];
1006 if (km_expand_key (buf, sizeof (buf),
1007 km_find_func (MENU_MAIN, OP_TOGGLE_WRITE)))
1008 snprintf (tmp, sizeof (tmp), _(" Press '%s' to toggle write"), buf);
1010 strfcpy (tmp, _("Use 'toggle-write' to re-enable write!"),
1013 mutt_error (_("Mailbox is marked unwritable. %s"), tmp);
1016 else if (ctx->readonly) {
1017 mutt_error _("Mailbox is read-only.");
1022 if (!ctx->changed && !ctx->deleted) {
1023 mutt_message _("Mailbox is unchanged.");
1029 char buf[SHORT_STRING];
1031 snprintf (buf, sizeof (buf), ctx->deleted == 1
1032 ? _("Purge %d deleted message?") :
1033 _("Purge %d deleted messages?"), ctx->deleted);
1034 if ((purge = query_quadoption (OPT_DELETE, buf)) < 0)
1036 else if (purge == M_NO) {
1038 return 0; /* nothing to do! */
1040 /* let IMAP servers hold on to D flags */
1041 if (ctx->magic != M_IMAP)
1044 for (i = 0; i < ctx->msgcount; i++)
1045 ctx->hdrs[i]->deleted = 0;
1049 else if (ctx->last_tag && ctx->last_tag->deleted)
1050 ctx->last_tag = NULL; /* reset last tagged msg now useless */
1053 /* really only for IMAP - imap_sync_mailbox results in a call to
1054 * mx_update_tables, so ctx->deleted is 0 when it comes back */
1055 msgcount = ctx->msgcount;
1056 deleted = ctx->deleted;
1058 if (purge && ctx->deleted) {
1059 if (trash_append (ctx) == -1)
1064 if (ctx->magic == M_IMAP)
1065 rc = imap_sync_mailbox (ctx, purge, index_hint);
1068 rc = sync_mailbox (ctx, index_hint);
1071 if (ctx->magic == M_IMAP && !purge)
1072 mutt_message (_("Mailbox checkpointed."));
1076 mutt_message (_("%d kept, %d deleted."), msgcount - deleted, deleted);
1080 if (ctx->msgcount == ctx->deleted &&
1081 (ctx->magic == M_MBOX || ctx->magic == M_MMDF) &&
1082 !mutt_is_spool (ctx->path) && !option (OPTSAVEEMPTY)) {
1084 mx_fastclose_mailbox (ctx);
1088 /* if we haven't deleted any messages, we don't need to resort */
1089 /* ... except for certain folder formats which need "unsorted"
1090 * sort order in order to synchronize folders.
1092 * MH and maildir are safe. mbox-style seems to need re-sorting,
1093 * at least with the new threading code.
1095 if (purge || (ctx->magic != M_MAILDIR && ctx->magic != M_MH)) {
1097 /* IMAP does this automatically after handling EXPUNGE */
1098 if (ctx->magic != M_IMAP)
1101 mx_update_tables (ctx, 1);
1102 mutt_sort_headers (ctx, 1); /* rethread from scratch */
1110 int mx_sync_mailbox (CONTEXT* ctx, int* index_hint) {
1111 int ret = _mx_sync_mailbox (ctx, index_hint);
1112 sidebar_set_buffystats (ctx);
1117 * dest destintation mailbox
1118 * hdr message being copied (required for maildir support, because
1119 * the filename depends on the message flags)
1121 MESSAGE *mx_open_new_message (CONTEXT * dest, HEADER * hdr, int flags)
1126 if (!MX_IDX(dest->magic-1)) {
1127 debug_print (1, ("function unimplemented for mailbox type %d.\n", dest->magic));
1131 msg = mem_calloc (1, sizeof (MESSAGE));
1132 msg->magic = dest->magic;
1136 msg->flags.flagged = hdr->flagged;
1137 msg->flags.replied = hdr->replied;
1138 msg->flags.read = hdr->read;
1139 msg->received = hdr->received;
1142 if (msg->received == 0)
1143 time (&msg->received);
1145 if (MX_COMMAND(dest->magic-1,mx_open_new_message)(msg, dest, hdr) == 0) {
1146 if (dest->magic == M_MMDF)
1147 fputs (MMDF_SEP, msg->fp);
1149 if ((msg->magic == M_MBOX || msg->magic == M_MMDF) && flags & M_ADD_FROM) {
1151 if (hdr->env->return_path)
1152 p = hdr->env->return_path;
1153 else if (hdr->env->sender)
1154 p = hdr->env->sender;
1159 fprintf (msg->fp, "From %s %s", p ? p->mailbox : NONULL (Username),
1160 ctime (&msg->received));
1169 /* check for new mail */
1170 int mx_check_mailbox (CONTEXT * ctx, int *index_hint, int lock) {
1171 #ifdef USE_COMPRESSED
1172 if (ctx->compressinfo)
1173 return mutt_check_mailbox_compressed (ctx);
1179 if (MX_IDX(ctx->magic-1) && MX_COMMAND(ctx->magic-1,mx_check_mailbox))
1180 return (MX_COMMAND(ctx->magic-1,mx_check_mailbox)(ctx, index_hint, lock));
1183 debug_print (1, ("null or invalid context.\n"));
1188 /* return a stream pointer for a message */
1189 MESSAGE *mx_open_message (CONTEXT * ctx, int msgno)
1193 msg = mem_calloc (1, sizeof (MESSAGE));
1194 switch (msg->magic = ctx->magic) {
1203 HEADER *cur = ctx->hdrs[msgno];
1204 char path[_POSIX_PATH_MAX];
1206 snprintf (path, sizeof (path), "%s/%s", ctx->path, cur->path);
1208 if ((msg->fp = fopen (path, "r")) == NULL && errno == ENOENT &&
1209 ctx->magic == M_MAILDIR)
1210 msg->fp = maildir_open_find_message (ctx->path, cur->path);
1212 if (msg->fp == NULL) {
1214 debug_print (1, ("fopen: %s: %s (errno %d).\n", path, strerror (errno), errno));
1223 if (imap_fetch_message (msg, ctx, msgno) != 0)
1227 #endif /* USE_IMAP */
1232 if (pop_fetch_message (msg, ctx, msgno) != 0)
1236 #endif /* USE_POP */
1241 if (nntp_fetch_message (msg, ctx, msgno) != 0)
1245 #endif /* USE_NNTP */
1248 debug_print (1, ("function not implemented for mailbox type %d.\n", ctx->magic));
1255 /* commit a message to a folder */
1257 int mx_commit_message (MESSAGE * msg, CONTEXT * ctx) {
1258 if (!(msg->write && ctx->append)) {
1259 debug_print (1, ("msg->write = %d, ctx->append = %d\n", msg->write, ctx->append));
1262 if (!ctx || !MX_IDX(ctx->magic-1) || !MX_COMMAND(ctx->magic-1,mx_commit_message))
1264 return (MX_COMMAND(ctx->magic-1,mx_commit_message) (msg, ctx));
1267 /* close a pointer to a message */
1268 int mx_close_message (MESSAGE ** msg)
1272 if ((*msg)->magic == M_MH || (*msg)->magic == M_MAILDIR
1274 || (*msg)->magic == M_IMAP
1277 || (*msg)->magic == M_POP
1280 || (*msg)->magic == M_NNTP
1283 r = safe_fclose (&(*msg)->fp);
1289 debug_print (1, ("unlinking %s\n", (*msg)->path));
1290 unlink ((*msg)->path);
1291 mem_free (&(*msg)->path);
1298 void mx_alloc_memory (CONTEXT * ctx)
1301 size_t s = MAX (sizeof (HEADER *), sizeof (int));
1303 if ((ctx->hdrmax + 25) * s < ctx->hdrmax * s) {
1304 mutt_error _("Integer overflow -- can't allocate memory.");
1311 mem_realloc (&ctx->hdrs, sizeof (HEADER *) * (ctx->hdrmax += 25));
1312 mem_realloc (&ctx->v2r, sizeof (int) * ctx->hdrmax);
1315 ctx->hdrs = mem_calloc ((ctx->hdrmax += 25), sizeof (HEADER *));
1316 ctx->v2r = mem_calloc (ctx->hdrmax, sizeof (int));
1318 for (i = ctx->msgcount; i < ctx->hdrmax; i++) {
1319 ctx->hdrs[i] = NULL;
1324 /* this routine is called to update the counts in the context structure for
1325 * the last message header parsed.
1327 void mx_update_context (CONTEXT * ctx, int new_messages)
1332 for (msgno = ctx->msgcount - new_messages; msgno < ctx->msgcount; msgno++) {
1333 h = ctx->hdrs[msgno];
1336 /* NOTE: this _must_ be done before the check for mailcap! */
1337 h->security = crypt_query (h->content);
1340 if (!ctx->pattern) {
1341 ctx->v2r[ctx->vcount] = msgno;
1342 h->virtual = ctx->vcount++;
1348 if (h->env->supersedes) {
1352 ctx->id_hash = mutt_make_id_hash (ctx);
1354 h2 = hash_find (ctx->id_hash, h->env->supersedes);
1356 /* mem_free (&h->env->supersedes); should I ? */
1359 if (!ctx->counting && option (OPTSCORE))
1360 mutt_score_message (ctx, h2, 1);
1364 /* add this message to the hash tables */
1365 if (ctx->id_hash && h->env->message_id)
1366 hash_insert (ctx->id_hash, h->env->message_id, h, 0);
1367 if (!ctx->counting) {
1368 if (ctx->subj_hash && h->env->real_subj)
1369 hash_insert (ctx->subj_hash, h->env->real_subj, h, 1);
1371 if (option (OPTSCORE))
1372 mutt_score_message (ctx, h, 0);
1387 /* update sidebar count */
1388 sidebar_set_buffystats (ctx);
1393 * 1 if the specified mailbox contains 0 messages.
1394 * 0 if the mailbox contains messages
1397 int mx_check_empty (const char *path)
1400 if ((i = mx_get_idx (path)) >= 0 && MX_COMMAND(i,mx_check_empty))
1401 return (MX_COMMAND(i,mx_check_empty)(path));
1406 int mx_acl_check (CONTEXT* ctx, int flag) {
1407 if (!ctx || !MX_IDX(ctx->magic-1))
1409 /* if no acl_check defined for module, assume permission is granted */
1410 if (!MX_COMMAND(ctx->magic-1,mx_acl_check))
1412 return (MX_COMMAND(ctx->magic-1,mx_acl_check)(ctx,flag));
1415 void mx_init (void) {
1419 list_push_back (&MailboxFormats, (void*) mbox_reg_mx ());
1420 list_push_back (&MailboxFormats, (void*) mmdf_reg_mx ());
1421 list_push_back (&MailboxFormats, (void*) mh_reg_mx ());
1422 list_push_back (&MailboxFormats, (void*) maildir_reg_mx ());
1424 list_push_back (&MailboxFormats, (void*) imap_reg_mx ());
1427 list_push_back (&MailboxFormats, (void*) pop_reg_mx ());
1430 list_push_back (&MailboxFormats, (void*) nntp_reg_mx ());
1432 #ifdef USE_COMPRESSED
1433 list_push_back (&MailboxFormats, (void*) compress_reg_mx ());
1436 /* check module registration for completeness with debug versions */
1437 #define EXITWITHERR(m) do { fprintf(stderr, "error: incomplete mx module: %s is missing for type %i\n",m,i);exit(1); } while (0)
1438 for (i = 0; i < MailboxFormats->length; i++) {
1439 if (MX_COMMAND(i,type) < 1) EXITWITHERR("type");
1440 if (!MX_COMMAND(i,mx_is_magic)) EXITWITHERR("mx_is_magic");
1441 if (!MX_COMMAND(i,mx_open_mailbox)) EXITWITHERR("mx_open_mailbox");
1442 /* if (!MX_COMMAND(i,mx_sync_mailbox)) EXITWITHERR("mx_sync_mailbox");*/
1448 int mx_rebuild_cache (void) {
1450 mutt_error (_("Support for header caching was not build in."));
1453 int i = 0, magic = 0;
1454 CONTEXT* ctx = NULL;
1457 if (list_empty(Incoming)) {
1458 mutt_error (_("No mailboxes defined."));
1462 for (i = 0; i < Incoming->length; i++) {
1463 b = (BUFFY*) Incoming->data[i];
1464 magic = mx_get_magic (b->path);
1465 if (magic != M_MAILDIR && magic != M_MH
1471 sidebar_set_current (b->path);
1472 sidebar_draw (CurrentMenu);
1473 if ((ctx = mx_open_mailbox (b->path,
1474 M_READONLY | M_NOSORT | M_COUNT,
1476 mx_close_mailbox (ctx, 0);
1478 mutt_clear_error ();
1480 if (Context && Context->path)
1481 sidebar_set_current (Context->path);
1482 sidebar_draw (CurrentMenu);