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.
26 #include <lib-lib/lib-lib.h>
28 #include <lib-sys/unix.h>
30 #include <lib-mime/mime.h>
32 #include <lib-ui/sidebar.h>
45 #include <imap/imap.h>
46 #include <imap/mx_imap.h>
49 #include <pop/mx_pop.h>
52 #include "nntp/nntp.h"
53 #include "nntp/mx_nntp.h"
60 #include <lib-crypt/crypt.h>
64 static list2_t* MailboxFormats = NULL;
65 #define MX_COMMAND(idx,cmd) ((mx_t*) MailboxFormats->data[idx])->cmd
66 #define MX_IDX(idx) (idx >= 0 && idx < MailboxFormats->length)
68 #define mutt_is_spool(s) (m_strcmp(Spoolfile, s) == 0)
73 * retry - should retry if unable to lock?
76 static int invoke_dotlock (const char *path, int dummy, int flags, int retry)
78 char cmd[LONG_STRING + _POSIX_PATH_MAX];
79 char f[SHORT_STRING + _POSIX_PATH_MAX];
82 if (flags & DL_FL_RETRY)
83 snprintf (r, sizeof (r), "-r %d ", retry ? MAXLOCKATTEMPT : 0);
85 mutt_quote_filename (f, sizeof (f), path);
87 snprintf (cmd, sizeof (cmd),
90 flags & DL_FL_TRY ? "-t " : "",
91 flags & DL_FL_UNLOCK ? "-u " : "",
92 flags & DL_FL_USEPRIV ? "-p " : "",
93 flags & DL_FL_FORCE ? "-f " : "",
94 flags & DL_FL_UNLINK ? "-d " : "",
95 flags & DL_FL_RETRY ? r : "", f);
97 return mutt_system (cmd);
100 static int dotlock_file (const char *path, int fd, int retry)
103 int flags = DL_FL_USEPRIV | DL_FL_RETRY;
109 if ((r = invoke_dotlock (path, fd, flags, retry)) == DL_EX_EXIST) {
110 if (!option (OPTNOCURSES)) {
111 char msg[LONG_STRING];
113 snprintf (msg, sizeof (msg),
114 _("Lock count exceeded, remove lock for %s?"), path);
115 if (retry && mutt_yesorno (msg, M_YES) == M_YES) {
116 flags |= DL_FL_FORCE;
123 mutt_error (_("Can't dotlock %s.\n"), path);
126 return (r == DL_EX_OK ? 0 : -1);
129 static int undotlock_file (const char *path, int fd)
131 return (invoke_dotlock (path, fd, DL_FL_USEPRIV | DL_FL_UNLOCK, 0) ==
135 #endif /* USE_DOTLOCK */
137 /* looks up index of type for path in MailboxFormats */
138 static int mx_get_idx (const char* path) {
142 /* first, test all non-local folders to avoid stat() call */
143 for (i = 0; i < MailboxFormats->length; i++) {
144 if (!MX_COMMAND(i,local))
145 t = MX_COMMAND(i,mx_is_magic)(path, NULL);
149 if (stat (path, &st) == 0) {
150 /* if stat() succeeded, keep testing until success and
151 * pass stat() info so that we only need to do it once */
152 for (i = 0; i < MailboxFormats->length; i++) {
153 if (MX_COMMAND(i,local))
154 t = MX_COMMAND(i,mx_is_magic)(path, &st);
163 * excl if excl != 0, request an exclusive lock
164 * dot if dot != 0, try to dotlock the file
165 * time_out should retry locking?
167 int mx_lock_file (const char *path, int fd, int excl, int dot, int time_out)
169 #if defined (USE_FCNTL) || defined (USE_FLOCK)
181 lck.l_type = excl ? F_WRLCK : F_RDLCK;
182 lck.l_whence = SEEK_SET;
187 while (fcntl (fd, F_SETLK, &lck) == -1) {
190 if (errno != EAGAIN && errno != EACCES) {
191 mutt_perror ("fcntl");
195 if (fstat (fd, &sb) != 0)
201 /* only unlock file if it is unchanged */
202 if (prev_sb.st_size == sb.st_size
203 && ++count >= (time_out ? MAXLOCKATTEMPT : 0)) {
205 mutt_error _("Timeout exceeded while attempting fcntl lock!");
212 mutt_message (_("Waiting for fcntl lock... %d"), ++attempt);
215 #endif /* USE_FCNTL */
220 while (flock (fd, (excl ? LOCK_EX : LOCK_SH) | LOCK_NB) == -1) {
223 if (errno != EWOULDBLOCK) {
224 mutt_perror ("flock");
229 if (fstat (fd, &sb) != 0)
235 /* only unlock file if it is unchanged */
236 if (prev_sb.st_size == sb.st_size
237 && ++count >= (time_out ? MAXLOCKATTEMPT : 0)) {
239 mutt_error _("Timeout exceeded while attempting flock lock!");
247 mutt_message (_("Waiting for flock attempt... %d"), ++attempt);
250 #endif /* USE_FLOCK */
254 r = dotlock_file (path, fd, time_out);
255 #endif /* USE_DOTLOCK */
258 /* release any other locks obtained in this routine */
261 lck.l_type = F_UNLCK;
262 fcntl (fd, F_SETLK, &lck);
263 #endif /* USE_FCNTL */
267 #endif /* USE_FLOCK */
275 int mx_unlock_file (const char *path, int fd, int dot)
278 struct flock unlockit;
280 p_clear(&unlockit, 1);
281 unlockit.l_type = F_UNLCK;
282 unlockit.l_whence = SEEK_SET;
283 fcntl (fd, F_SETLK, &unlockit);
292 undotlock_file (path, fd);
298 void mx_unlink_empty (const char *path)
306 if ((fd = open (path, O_RDWR)) == -1)
309 if (mx_lock_file (path, fd, 1, 0, 1) == -1) {
315 invoke_dotlock (path, fd, DL_FL_UNLINK, 1);
317 if (fstat (fd, &sb) == 0 && sb.st_size == 0)
321 mx_unlock_file (path, fd, 0);
325 /* try to figure out what type of mailbox ``path'' is */
326 int mx_get_magic (const char *path) {
329 if (m_strlen(path) == 0)
331 if ((i = mx_get_idx (path)) >= 0)
332 return (MX_COMMAND(i,type));
336 int mx_is_local (int m) {
339 return (MX_COMMAND(m,local));
343 * set DefaultMagic to the given value
345 int mx_set_magic (const char *s)
347 if (ascii_strcasecmp (s, "mbox") == 0)
348 DefaultMagic = M_MBOX;
349 else if (ascii_strcasecmp (s, "mmdf") == 0)
350 DefaultMagic = M_MMDF;
351 else if (ascii_strcasecmp (s, "mh") == 0)
353 else if (ascii_strcasecmp (s, "maildir") == 0)
354 DefaultMagic = M_MAILDIR;
361 /* mx_access: Wrapper for access, checks permissions on a given mailbox.
362 * We may be interested in using ACL-style flags at some point, currently
363 * we use the normal access() flags. */
364 int mx_access (const char *path, int flags)
368 if ((i = mx_get_idx (path)) >= 0 && MX_COMMAND(i,mx_access))
369 return (MX_COMMAND(i,mx_access)(path,flags));
373 static int mx_open_mailbox_append (CONTEXT * ctx, int flags)
377 /* special case for appending to compressed folders -
378 * even if we can not open them for reading */
379 if (mutt_can_append_compressed (ctx->path))
380 mutt_open_append_compressed (ctx);
384 if (mx_get_magic (ctx->path) == M_IMAP)
385 return imap_open_mailbox_append (ctx);
387 if (stat (ctx->path, &sb) == 0) {
388 ctx->magic = mx_get_magic (ctx->path);
390 switch (ctx->magic) {
392 mutt_error (_("%s is not a mailbox."), ctx->path);
398 else if (errno == ENOENT) {
399 ctx->magic = DefaultMagic;
401 if (ctx->magic == M_MH || ctx->magic == M_MAILDIR) {
402 char tmp[_POSIX_PATH_MAX];
404 if (mkdir (ctx->path, S_IRWXU)) {
405 mutt_perror (ctx->path);
409 if (ctx->magic == M_MAILDIR) {
410 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
411 if (mkdir (tmp, S_IRWXU)) {
417 snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
418 if (mkdir (tmp, S_IRWXU)) {
420 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
425 snprintf (tmp, sizeof (tmp), "%s/tmp", ctx->path);
426 if (mkdir (tmp, S_IRWXU)) {
428 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
430 snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
439 snprintf (tmp, sizeof (tmp), "%s/.mh_sequences", ctx->path);
440 if ((i = creat (tmp, S_IRWXU)) == -1) {
450 mutt_perror (ctx->path);
454 switch (ctx->magic) {
458 safe_fopen (ctx->path, flags & M_NEWFOLDER ? "w" : "a")) == NULL
459 || mbox_lock_mailbox (ctx, 1, 1) != 0) {
461 mutt_perror (ctx->path);
463 mutt_error (_("Couldn't lock %s\n"), ctx->path);
464 safe_fclose (&ctx->fp);
468 fseeko (ctx->fp, 0, 2);
484 * open a mailbox and parse it
487 * flags M_NOSORT do not sort mailbox
488 * M_APPEND open mailbox for appending
489 * M_READONLY open mailbox in read-only mode
490 * M_QUIET only print error messages
491 * ctx if non-null, context struct to use
493 CONTEXT *mx_open_mailbox (const char *path, int flags, CONTEXT * pctx)
499 ctx = p_new(CONTEXT, 1);
501 ctx->path = m_strdup(path);
503 ctx->msgnotreadyet = -1;
508 if (flags & M_READONLY)
513 if (flags & (M_APPEND | M_NEWFOLDER)) {
514 if (mx_open_mailbox_append (ctx, flags) != 0) {
515 mx_fastclose_mailbox (ctx);
523 if (!MX_IDX(ctx->magic-1))
524 ctx->magic = mx_get_magic (path);
526 if (ctx->magic == M_COMPRESSED)
527 mutt_open_read_compressed (ctx);
530 mutt_error (_("%s is not a mailbox."), path);
532 if (ctx->magic == -1)
535 if (ctx->magic <= 0) {
536 mx_fastclose_mailbox (ctx);
542 /* if the user has a `push' command in their .muttrc, or in a folder-hook,
543 * it will cause the progress messages not to be displayed because
544 * mutt_refresh() will think we are in the middle of a macro. so set a
545 * flag to indicate that we should really refresh the screen.
547 set_option (OPTFORCEREFRESH);
550 mutt_message (_("Reading %s..."), ctx->path);
552 rc = MX_COMMAND(ctx->magic-1,mx_open_mailbox)(ctx);
555 if ((flags & M_NOSORT) == 0) {
556 /* avoid unnecessary work since the mailbox is completely unthreaded
558 unset_option (OPTSORTSUBTHREADS);
559 unset_option (OPTNEEDRESCORE);
560 mutt_sort_headers (ctx, 1);
566 mx_fastclose_mailbox (ctx);
571 unset_option (OPTFORCEREFRESH);
575 /* free up memory associated with the mailbox context */
576 void mx_fastclose_mailbox (CONTEXT * ctx)
583 if (MX_IDX(ctx->magic-1) && MX_COMMAND(ctx->magic-1,mx_fastclose_mailbox))
584 MX_COMMAND(ctx->magic-1,mx_fastclose_mailbox(ctx));
586 hash_destroy (&ctx->subj_hash, NULL);
588 hash_destroy (&ctx->id_hash, NULL);
589 mutt_clear_threads (ctx);
590 for (i = 0; i < ctx->msgcount; i++)
591 header_delete(&ctx->hdrs[i]);
592 p_delete(&ctx->hdrs);
595 if (ctx->compressinfo)
596 mutt_fast_close_compressed (ctx);
598 p_delete(&ctx->path);
599 p_delete(&ctx->pattern);
600 if (ctx->limit_pattern)
601 mutt_pattern_free (&ctx->limit_pattern);
602 safe_fclose (&ctx->fp);
606 /* save changes to disk */
607 static int sync_mailbox (CONTEXT * ctx, int *index_hint)
612 mutt_message (_("Writing %s..."), ctx->path);
614 if (MX_IDX(ctx->magic-1))
615 /* the 1 is only of interest for IMAP and means EXPUNGE */
616 rc = MX_COMMAND(ctx->magic-1,mx_sync_mailbox(ctx,1,index_hint));
618 if (rc == 0 && ctx->compressinfo)
619 return mutt_sync_compressed (ctx);
624 /* move deleted mails to the trash folder */
625 static int trash_append (CONTEXT * ctx)
631 if (!TrashPath || !ctx->deleted ||
632 (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))
635 for (; i < ctx->msgcount && (!ctx->hdrs[i]->deleted ||
636 ctx->hdrs[i]->appended); i++);
637 if (i == ctx->msgcount)
638 return 0; /* nothing to be done */
640 if (mutt_save_confirm (TrashPath, &st) != 0) {
641 mutt_error _("message(s) not deleted");
646 if (lstat (ctx->path, &stc) == 0 && stc.st_ino == st.st_ino
647 && stc.st_dev == st.st_dev && stc.st_rdev == st.st_rdev)
648 return 0; /* we are in the trash folder: simple sync */
650 if ((ctx_trash = mx_open_mailbox (TrashPath, M_APPEND, NULL)) != NULL) {
651 for (i = 0; i < ctx->msgcount; i++)
652 if (ctx->hdrs[i]->deleted && !ctx->hdrs[i]->appended
653 && !ctx->hdrs[i]->purged
654 && mutt_append_message (ctx_trash, ctx, ctx->hdrs[i], 0, 0) == -1) {
655 mx_close_mailbox (ctx_trash, NULL);
659 mx_close_mailbox (ctx_trash, NULL);
662 mutt_error _("Can't open trash folder");
670 /* save changes and close mailbox */
671 static int _mx_close_mailbox (CONTEXT * ctx, int *index_hint)
673 int i, move_messages = 0, purge = 1, read_msgs = 0;
677 char mbox[_POSIX_PATH_MAX];
678 char buf[SHORT_STRING];
686 if (ctx->magic == M_NNTP) {
689 ret = nntp_close_mailbox (ctx);
690 mx_fastclose_mailbox (ctx);
694 if (ctx->readonly || ctx->dontwrite) {
695 /* mailbox is readonly or we don't want to write */
696 mx_fastclose_mailbox (ctx);
701 /* mailbox was opened in write-mode */
702 if (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
703 mbox_close_mailbox (ctx);
705 mx_fastclose_mailbox (ctx);
709 for (i = 0; i < ctx->msgcount; i++) {
710 if (!ctx->hdrs[i]->deleted && ctx->hdrs[i]->read
711 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
715 if (read_msgs && quadoption (OPT_MOVE) != M_NO) {
718 if ((p = mutt_find_hook (M_MBOXHOOK, ctx->path))) {
720 m_strcpy(mbox, sizeof(mbox), p);
723 m_strcpy(mbox, sizeof(mbox), NONULL(Inbox));
724 isSpool = mutt_is_spool (ctx->path) && !mutt_is_spool (mbox);
726 mutt_expand_path (mbox, sizeof (mbox));
729 snprintf (buf, sizeof (buf), _("Move read messages to %s?"), mbox);
730 if ((move_messages = query_quadoption (OPT_MOVE, buf)) == -1) {
738 * There is no point in asking whether or not to purge if we are
739 * just marking messages as "trash".
741 if (ctx->deleted && !(ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH))) {
742 snprintf (buf, sizeof (buf), ctx->deleted == 1
743 ? _("Purge %d deleted message?") :
744 _("Purge %d deleted messages?"), ctx->deleted);
745 if ((purge = query_quadoption (OPT_DELETE, buf)) < 0) {
751 /* IMAP servers manage the OLD flag themselves */
752 if (ctx->magic != M_IMAP)
753 if (option (OPTMARKOLD)) {
754 for (i = 0; i < ctx->msgcount; i++) {
755 if (!ctx->hdrs[i]->deleted && !ctx->hdrs[i]->old)
756 mutt_set_flag (ctx, ctx->hdrs[i], M_OLD, 1);
761 mutt_message (_("Moving read messages to %s..."), mbox);
763 /* try to use server-side copy first */
766 if (ctx->magic == M_IMAP && imap_is_magic (mbox, NULL) == M_IMAP) {
767 /* tag messages for moving, and clear old tags, if any */
768 for (i = 0; i < ctx->msgcount; i++)
769 if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted
770 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
771 ctx->hdrs[i]->tagged = 1;
773 ctx->hdrs[i]->tagged = 0;
775 i = imap_copy_messages (ctx, NULL, mbox, 1);
778 if (i == 0) /* success */
780 else if (i == -1) { /* horrible error, bail */
784 else /* use regular append-copy mode */
786 if (mx_open_mailbox (mbox, M_APPEND, &f) == NULL) {
791 for (i = 0; i < ctx->msgcount; i++) {
792 if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted
793 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED))) {
794 if (mutt_append_message (&f, ctx, ctx->hdrs[i], 0, CH_UPDATE_LEN) ==
796 mutt_set_flag (ctx, ctx->hdrs[i], M_DELETE, 1);
797 mutt_set_flag (ctx, ctx->hdrs[i], M_APPENDED, 1);
800 mx_close_mailbox (&f, NULL);
807 mx_close_mailbox (&f, NULL);
811 else if (!ctx->changed && ctx->deleted == 0) {
812 mutt_message _("Mailbox is unchanged.");
814 mx_fastclose_mailbox (ctx);
818 /* copy mails to the trash before expunging */
819 if (purge && ctx->deleted)
820 if (trash_append (ctx) != 0) {
825 /* allow IMAP to preserve the deleted flag across sessions */
826 if (ctx->magic == M_IMAP) {
827 if ((check = imap_sync_mailbox (ctx, purge, index_hint)) != 0) {
835 for (i = 0; i < ctx->msgcount; i++)
836 ctx->hdrs[i]->deleted = 0;
840 if (ctx->changed || ctx->deleted) {
841 if ((check = sync_mailbox (ctx, index_hint)) != 0) {
849 mutt_message (_("%d kept, %d moved, %d deleted."),
850 ctx->msgcount - ctx->deleted, read_msgs, ctx->deleted);
852 mutt_message (_("%d kept, %d deleted."),
853 ctx->msgcount - ctx->deleted, ctx->deleted);
855 if (ctx->msgcount == ctx->deleted &&
856 (ctx->magic == M_MMDF || ctx->magic == M_MBOX) &&
857 !mutt_is_spool (ctx->path) && !option (OPTSAVEEMPTY))
858 mx_unlink_empty (ctx->path);
860 if (ctx->compressinfo && mutt_slow_close_compressed (ctx))
863 mx_fastclose_mailbox (ctx);
868 int mx_close_mailbox (CONTEXT * ctx, int *index_hint) {
872 ret = _mx_close_mailbox (ctx, index_hint);
873 sidebar_set_buffystats (ctx);
877 /* update a Context structure's internal tables. */
879 void mx_update_tables (CONTEXT * ctx, int committing)
883 /* update memory to reflect the new state of the mailbox */
892 #define this_body ctx->hdrs[j]->content
893 for (i = 0, j = 0; i < ctx->msgcount; i++) {
894 if ((committing && (!ctx->hdrs[i]->deleted ||
895 (ctx->magic == M_MAILDIR
896 && option (OPTMAILDIRTRASH)))) || (!committing
900 ctx->hdrs[j] = ctx->hdrs[i];
903 ctx->hdrs[j]->msgno = j;
904 if (ctx->hdrs[j]->virtual != -1) {
905 ctx->v2r[ctx->vcount] = j;
906 ctx->hdrs[j]->virtual = ctx->vcount++;
907 ctx->vsize += this_body->length + this_body->offset -
908 this_body->hdr_offset;
912 ctx->hdrs[j]->changed = 0;
913 else if (ctx->hdrs[j]->changed)
917 || (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH))) {
918 if (ctx->hdrs[j]->deleted)
922 if (ctx->hdrs[j]->tagged)
924 if (ctx->hdrs[j]->flagged)
926 if (!ctx->hdrs[j]->read) {
928 if (!ctx->hdrs[j]->old)
935 if (ctx->magic == M_MH || ctx->magic == M_MAILDIR)
936 ctx->size -= (ctx->hdrs[i]->content->length +
937 ctx->hdrs[i]->content->offset -
938 ctx->hdrs[i]->content->hdr_offset);
939 /* remove message from the hash tables */
940 if (ctx->subj_hash && ctx->hdrs[i]->env->real_subj)
941 hash_delete (ctx->subj_hash, ctx->hdrs[i]->env->real_subj,
943 if (ctx->id_hash && ctx->hdrs[i]->env->message_id)
944 hash_delete (ctx->id_hash, ctx->hdrs[i]->env->message_id,
946 header_delete(&ctx->hdrs[i]);
952 /* update sidebar count */
953 sidebar_set_buffystats (ctx);
957 /* save changes to mailbox
963 static int _mx_sync_mailbox (CONTEXT * ctx, int *index_hint)
967 int msgcount, deleted;
969 if (ctx->dontwrite) {
970 char buf[STRING], tmp[STRING];
972 if (km_expand_key (buf, sizeof (buf),
973 km_find_func (MENU_MAIN, OP_TOGGLE_WRITE)))
974 snprintf (tmp, sizeof (tmp), _(" Press '%s' to toggle write"), buf);
976 m_strcpy(tmp, sizeof(tmp), _("Use 'toggle-write' to re-enable write!"));
978 mutt_error (_("Mailbox is marked unwritable. %s"), tmp);
981 else if (ctx->readonly) {
982 mutt_error _("Mailbox is read-only.");
987 if (!ctx->changed && !ctx->deleted) {
988 mutt_message _("Mailbox is unchanged.");
994 char buf[SHORT_STRING];
996 snprintf (buf, sizeof (buf), ctx->deleted == 1
997 ? _("Purge %d deleted message?") :
998 _("Purge %d deleted messages?"), ctx->deleted);
999 if ((purge = query_quadoption (OPT_DELETE, buf)) < 0)
1001 else if (purge == M_NO) {
1003 return 0; /* nothing to do! */
1004 /* let IMAP servers hold on to D flags */
1005 if (ctx->magic != M_IMAP)
1007 for (i = 0; i < ctx->msgcount; i++)
1008 ctx->hdrs[i]->deleted = 0;
1012 else if (ctx->last_tag && ctx->last_tag->deleted)
1013 ctx->last_tag = NULL; /* reset last tagged msg now useless */
1016 /* really only for IMAP - imap_sync_mailbox results in a call to
1017 * mx_update_tables, so ctx->deleted is 0 when it comes back */
1018 msgcount = ctx->msgcount;
1019 deleted = ctx->deleted;
1021 if (purge && ctx->deleted) {
1022 if (trash_append (ctx) == -1)
1026 if (ctx->magic == M_IMAP)
1027 rc = imap_sync_mailbox (ctx, purge, index_hint);
1029 rc = sync_mailbox (ctx, index_hint);
1031 if (ctx->magic == M_IMAP && !purge)
1032 mutt_message (_("Mailbox checkpointed."));
1035 mutt_message (_("%d kept, %d deleted."), msgcount - deleted, deleted);
1039 if (ctx->msgcount == ctx->deleted &&
1040 (ctx->magic == M_MBOX || ctx->magic == M_MMDF) &&
1041 !mutt_is_spool (ctx->path) && !option (OPTSAVEEMPTY)) {
1043 mx_fastclose_mailbox (ctx);
1047 /* if we haven't deleted any messages, we don't need to resort */
1048 /* ... except for certain folder formats which need "unsorted"
1049 * sort order in order to synchronize folders.
1051 * MH and maildir are safe. mbox-style seems to need re-sorting,
1052 * at least with the new threading code.
1054 if (purge || (ctx->magic != M_MAILDIR && ctx->magic != M_MH)) {
1055 /* IMAP does this automatically after handling EXPUNGE */
1056 if (ctx->magic != M_IMAP)
1058 mx_update_tables (ctx, 1);
1059 mutt_sort_headers (ctx, 1); /* rethread from scratch */
1067 int mx_sync_mailbox (CONTEXT* ctx, int* index_hint) {
1068 int ret = _mx_sync_mailbox (ctx, index_hint);
1069 sidebar_set_buffystats (ctx);
1074 * dest destintation mailbox
1075 * hdr message being copied (required for maildir support, because
1076 * the filename depends on the message flags)
1078 MESSAGE *mx_open_new_message (CONTEXT * dest, HEADER * hdr, int flags)
1081 address_t *p = NULL;
1083 if (!MX_IDX(dest->magic-1)) {
1087 msg = p_new(MESSAGE, 1);
1088 msg->magic = dest->magic;
1092 msg->flags.flagged = hdr->flagged;
1093 msg->flags.replied = hdr->replied;
1094 msg->flags.read = hdr->read;
1095 msg->received = hdr->received;
1098 if (msg->received == 0)
1099 time (&msg->received);
1101 if (MX_COMMAND(dest->magic-1,mx_open_new_message)(msg, dest, hdr) == 0) {
1102 if (dest->magic == M_MMDF)
1103 fputs (MMDF_SEP, msg->fp);
1105 if ((msg->magic == M_MBOX || msg->magic == M_MMDF) && flags & M_ADD_FROM) {
1107 if (hdr->env->return_path)
1108 p = hdr->env->return_path;
1109 else if (hdr->env->sender)
1110 p = hdr->env->sender;
1115 fprintf (msg->fp, "From %s %s", p ? p->mailbox : NONULL (Username),
1116 ctime (&msg->received));
1125 /* check for new mail */
1126 int mx_check_mailbox (CONTEXT * ctx, int *index_hint, int lock) {
1127 if (ctx->compressinfo)
1128 return mutt_check_mailbox_compressed (ctx);
1133 if (MX_IDX(ctx->magic-1) && MX_COMMAND(ctx->magic-1,mx_check_mailbox))
1134 return (MX_COMMAND(ctx->magic-1,mx_check_mailbox)(ctx, index_hint, lock));
1140 /* return a stream pointer for a message */
1141 MESSAGE *mx_open_message (CONTEXT * ctx, int msgno)
1145 msg = p_new(MESSAGE, 1);
1146 switch (msg->magic = ctx->magic) {
1155 HEADER *cur = ctx->hdrs[msgno];
1156 char path[_POSIX_PATH_MAX];
1158 snprintf (path, sizeof (path), "%s/%s", ctx->path, cur->path);
1160 if ((msg->fp = fopen (path, "r")) == NULL && errno == ENOENT &&
1161 ctx->magic == M_MAILDIR)
1162 msg->fp = maildir_open_find_message (ctx->path, cur->path);
1164 if (msg->fp == NULL) {
1173 if (imap_fetch_message (msg, ctx, msgno) != 0)
1180 if (pop_fetch_message (msg, ctx, msgno) != 0)
1188 if (nntp_fetch_message (msg, ctx, msgno) != 0)
1192 #endif /* USE_NNTP */
1201 /* commit a message to a folder */
1203 int mx_commit_message (MESSAGE * msg, CONTEXT * ctx) {
1204 if (!(msg->write && ctx->append)) {
1207 if (!ctx || !MX_IDX(ctx->magic-1) || !MX_COMMAND(ctx->magic-1,mx_commit_message))
1209 return (MX_COMMAND(ctx->magic-1,mx_commit_message) (msg, ctx));
1212 /* close a pointer to a message */
1213 int mx_close_message (MESSAGE ** msg)
1217 if ((*msg)->magic == M_MH || (*msg)->magic == M_MAILDIR
1218 || (*msg)->magic == M_IMAP
1219 || (*msg)->magic == M_POP
1221 || (*msg)->magic == M_NNTP
1224 r = safe_fclose (&(*msg)->fp);
1230 unlink ((*msg)->path);
1231 p_delete(&(*msg)->path);
1238 void mx_alloc_memory (CONTEXT * ctx)
1241 size_t s = MAX (sizeof (HEADER *), sizeof (int));
1243 if ((ctx->hdrmax + 25) * s < ctx->hdrmax * s) {
1244 mutt_error _("Integer overflow -- can't allocate memory.");
1251 p_realloc(&ctx->hdrs, ctx->hdrmax += 25);
1252 p_realloc(&ctx->v2r, ctx->hdrmax);
1255 ctx->hdrs = p_new(HEADER *, (ctx->hdrmax += 25));
1256 ctx->v2r = p_new(int, ctx->hdrmax);
1258 for (i = ctx->msgcount; i < ctx->hdrmax; i++) {
1259 ctx->hdrs[i] = NULL;
1264 /* this routine is called to update the counts in the context structure for
1265 * the last message header parsed.
1267 void mx_update_context (CONTEXT * ctx, int new_messages)
1272 for (msgno = ctx->msgcount - new_messages; msgno < ctx->msgcount; msgno++) {
1273 h = ctx->hdrs[msgno];
1275 /* NOTE: this _must_ be done before the check for mailcap! */
1276 h->security = crypt_query (h->content);
1278 if (!ctx->pattern) {
1279 ctx->v2r[ctx->vcount] = msgno;
1280 h->virtual = ctx->vcount++;
1286 if (h->env->supersedes) {
1290 ctx->id_hash = mutt_make_id_hash (ctx);
1292 h2 = hash_find (ctx->id_hash, h->env->supersedes);
1294 /* p_delete(&h->env->supersedes); should I ? */
1297 if (!ctx->counting && option (OPTSCORE))
1298 mutt_score_message (ctx, h2, 1);
1302 /* add this message to the hash tables */
1303 if (ctx->id_hash && h->env->message_id)
1304 hash_insert (ctx->id_hash, h->env->message_id, h, 0);
1305 if (!ctx->counting) {
1306 if (ctx->subj_hash && h->env->real_subj)
1307 hash_insert (ctx->subj_hash, h->env->real_subj, h, 1);
1309 if (option (OPTSCORE))
1310 mutt_score_message (ctx, h, 0);
1325 /* update sidebar count */
1326 sidebar_set_buffystats (ctx);
1331 * 1 if the specified mailbox contains 0 messages.
1332 * 0 if the mailbox contains messages
1335 int mx_check_empty (const char *path)
1338 if ((i = mx_get_idx (path)) >= 0 && MX_COMMAND(i,mx_check_empty))
1339 return (MX_COMMAND(i,mx_check_empty)(path));
1344 int mx_acl_check (CONTEXT* ctx, int flag) {
1345 if (!ctx || !MX_IDX(ctx->magic-1))
1347 /* if no acl_check defined for module, assume permission is granted */
1348 if (!MX_COMMAND(ctx->magic-1,mx_acl_check))
1350 return (MX_COMMAND(ctx->magic-1,mx_acl_check)(ctx,flag));
1353 void mx_init (void) {
1354 list_push_back (&MailboxFormats, (void*) mbox_reg_mx ());
1355 list_push_back (&MailboxFormats, (void*) mmdf_reg_mx ());
1356 list_push_back (&MailboxFormats, (void*) mh_reg_mx ());
1357 list_push_back (&MailboxFormats, (void*) maildir_reg_mx ());
1358 list_push_back (&MailboxFormats, (void*) imap_reg_mx ());
1359 list_push_back (&MailboxFormats, (void*) pop_reg_mx ());
1361 list_push_back (&MailboxFormats, (void*) nntp_reg_mx ());
1363 list_push_back (&MailboxFormats, (void*) compress_reg_mx ());
1366 int mx_rebuild_cache (void) {
1368 mutt_error (_("Support for header caching was not build in."));
1371 int i = 0, magic = 0;
1372 CONTEXT* ctx = NULL;
1375 if (list_empty(Incoming)) {
1376 mutt_error (_("No mailboxes defined."));
1380 for (i = 0; i < Incoming->length; i++) {
1381 b = (BUFFY*) Incoming->data[i];
1382 magic = mx_get_magic (b->path);
1383 if (magic != M_MAILDIR && magic != M_MH && magic != M_IMAP)
1385 sidebar_set_current (b->path);
1386 sidebar_draw (CurrentMenu);
1387 if ((ctx = mx_open_mailbox (b->path,
1388 M_READONLY | M_NOSORT | M_COUNT,
1390 mx_close_mailbox (ctx, 0);
1392 mutt_clear_error ();
1394 if (Context && Context->path)
1395 sidebar_set_current (Context->path);
1396 sidebar_draw (CurrentMenu);
1402 void mutt_parse_mime_message (CONTEXT * ctx, HEADER * cur)
1408 if (cur->content->type != TYPEMESSAGE
1409 && cur->content->type != TYPEMULTIPART)
1410 break; /* nothing to do */
1412 if (cur->content->parts)
1413 break; /* The message was parsed earlier. */
1415 if ((msg = mx_open_message (ctx, cur->msgno))) {
1416 mutt_parse_part (msg->fp, cur->content);
1418 cur->security = crypt_query (cur->content);
1420 mx_close_message (&msg);
1423 mutt_count_body_parts (cur, flags | M_PARTS_RECOUNT);