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.
30 #include "imap/imap.h"
31 #include "imap/mx_imap.h"
36 #include "pop/mx_pop.h"
40 #include "nntp/nntp.h"
41 #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) (safe_strcmp (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;
201 while (fcntl (fd, F_SETLK, &lck) == -1) {
204 debug_print (1, ("fcntl errno %d.\n", errno));
205 if (errno != EAGAIN && errno != EACCES) {
206 mutt_perror ("fcntl");
210 if (fstat (fd, &sb) != 0)
216 /* only unlock file if it is unchanged */
217 if (prev_sb.st_size == sb.st_size
218 && ++count >= (timeout ? MAXLOCKATTEMPT : 0)) {
220 mutt_error _("Timeout exceeded while attempting fcntl lock!");
227 mutt_message (_("Waiting for fcntl lock... %d"), ++attempt);
230 #endif /* USE_FCNTL */
235 while (flock (fd, (excl ? LOCK_EX : LOCK_SH) | LOCK_NB) == -1) {
238 if (errno != EWOULDBLOCK) {
239 mutt_perror ("flock");
244 if (fstat (fd, &sb) != 0)
250 /* only unlock file if it is unchanged */
251 if (prev_sb.st_size == sb.st_size
252 && ++count >= (timeout ? MAXLOCKATTEMPT : 0)) {
254 mutt_error _("Timeout exceeded while attempting flock lock!");
262 mutt_message (_("Waiting for flock attempt... %d"), ++attempt);
265 #endif /* USE_FLOCK */
269 r = dotlock_file (path, fd, timeout);
270 #endif /* USE_DOTLOCK */
273 /* release any other locks obtained in this routine */
276 lck.l_type = F_UNLCK;
277 fcntl (fd, F_SETLK, &lck);
278 #endif /* USE_FCNTL */
282 #endif /* USE_FLOCK */
290 int mx_unlock_file (const char *path, int fd, int dot)
293 struct flock unlockit = { F_UNLCK, 0, 0, 0 };
295 memset (&unlockit, 0, sizeof (struct flock));
296 unlockit.l_type = F_UNLCK;
297 unlockit.l_whence = SEEK_SET;
298 fcntl (fd, F_SETLK, &unlockit);
307 undotlock_file (path, fd);
313 void mx_unlink_empty (const char *path)
321 if ((fd = open (path, O_RDWR)) == -1)
324 if (mx_lock_file (path, fd, 1, 0, 1) == -1) {
330 invoke_dotlock (path, fd, DL_FL_UNLINK, 1);
332 if (fstat (fd, &sb) == 0 && sb.st_size == 0)
336 mx_unlock_file (path, fd, 0);
340 /* try to figure out what type of mailbox ``path'' is */
341 int mx_get_magic (const char *path) {
344 if (safe_strlen (path) == 0)
346 if ((i = mx_get_idx (path)) >= 0)
347 return (MX_COMMAND(i,type));
351 int mx_is_local (int m) {
354 return (MX_COMMAND(m,local));
358 * set DefaultMagic to the given value
360 int mx_set_magic (const char *s)
362 if (ascii_strcasecmp (s, "mbox") == 0)
363 DefaultMagic = M_MBOX;
364 else if (ascii_strcasecmp (s, "mmdf") == 0)
365 DefaultMagic = M_MMDF;
366 else if (ascii_strcasecmp (s, "mh") == 0)
368 else if (ascii_strcasecmp (s, "maildir") == 0)
369 DefaultMagic = M_MAILDIR;
376 /* mx_access: Wrapper for access, checks permissions on a given mailbox.
377 * We may be interested in using ACL-style flags at some point, currently
378 * we use the normal access() flags. */
379 int mx_access (const char *path, int flags)
383 if ((i = mx_get_idx (path)) >= 0 && MX_COMMAND(i,mx_access))
384 return (MX_COMMAND(i,mx_access)(path,flags));
388 static int mx_open_mailbox_append (CONTEXT * ctx, int flags)
392 #ifdef USE_COMPRESSED
393 /* special case for appending to compressed folders -
394 * even if we can not open them for reading */
395 if (mutt_can_append_compressed (ctx->path))
396 mutt_open_append_compressed (ctx);
403 if (mx_get_magic (ctx->path) == M_IMAP)
404 return imap_open_mailbox_append (ctx);
408 if (stat (ctx->path, &sb) == 0) {
409 ctx->magic = mx_get_magic (ctx->path);
411 switch (ctx->magic) {
413 mutt_error (_("%s is not a mailbox."), ctx->path);
419 else if (errno == ENOENT) {
420 ctx->magic = DefaultMagic;
422 if (ctx->magic == M_MH || ctx->magic == M_MAILDIR) {
423 char tmp[_POSIX_PATH_MAX];
425 if (mkdir (ctx->path, S_IRWXU)) {
426 mutt_perror (ctx->path);
430 if (ctx->magic == M_MAILDIR) {
431 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
432 if (mkdir (tmp, S_IRWXU)) {
438 snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
439 if (mkdir (tmp, S_IRWXU)) {
441 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
446 snprintf (tmp, sizeof (tmp), "%s/tmp", ctx->path);
447 if (mkdir (tmp, S_IRWXU)) {
449 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
451 snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
460 snprintf (tmp, sizeof (tmp), "%s/.mh_sequences", ctx->path);
461 if ((i = creat (tmp, S_IRWXU)) == -1) {
471 mutt_perror (ctx->path);
475 switch (ctx->magic) {
479 safe_fopen (ctx->path, flags & M_NEWFOLDER ? "w" : "a")) == NULL
480 || mbox_lock_mailbox (ctx, 1, 1) != 0) {
482 mutt_perror (ctx->path);
484 mutt_error (_("Couldn't lock %s\n"), ctx->path);
485 safe_fclose (&ctx->fp);
489 fseek (ctx->fp, 0, 2);
505 * open a mailbox and parse it
508 * flags M_NOSORT do not sort mailbox
509 * M_APPEND open mailbox for appending
510 * M_READONLY open mailbox in read-only mode
511 * M_QUIET only print error messages
512 * ctx if non-null, context struct to use
514 CONTEXT *mx_open_mailbox (const char *path, int flags, CONTEXT * pctx)
520 ctx = safe_malloc (sizeof (CONTEXT));
521 memset (ctx, 0, sizeof (CONTEXT));
522 ctx->path = safe_strdup (path);
524 ctx->msgnotreadyet = -1;
529 if (flags & M_READONLY)
534 if (flags & (M_APPEND | M_NEWFOLDER)) {
535 if (mx_open_mailbox_append (ctx, flags) != 0) {
536 mx_fastclose_mailbox (ctx);
544 if (!MX_IDX(ctx->magic-1))
545 ctx->magic = mx_get_magic (path);
547 #ifdef USE_COMPRESSED
548 if (ctx->magic == M_COMPRESSED)
549 mutt_open_read_compressed (ctx);
553 mutt_error (_("%s is not a mailbox."), path);
555 if (ctx->magic == -1)
558 if (ctx->magic <= 0) {
559 mx_fastclose_mailbox (ctx);
565 /* if the user has a `push' command in their .muttrc, or in a folder-hook,
566 * it will cause the progress messages not to be displayed because
567 * mutt_refresh() will think we are in the middle of a macro. so set a
568 * flag to indicate that we should really refresh the screen.
570 set_option (OPTFORCEREFRESH);
573 mutt_message (_("Reading %s..."), ctx->path);
575 rc = MX_COMMAND(ctx->magic-1,mx_open_mailbox)(ctx);
578 if ((flags & M_NOSORT) == 0) {
579 /* avoid unnecessary work since the mailbox is completely unthreaded
581 unset_option (OPTSORTSUBTHREADS);
582 unset_option (OPTNEEDRESCORE);
583 mutt_sort_headers (ctx, 1);
589 mx_fastclose_mailbox (ctx);
594 unset_option (OPTFORCEREFRESH);
598 /* free up memory associated with the mailbox context */
599 void mx_fastclose_mailbox (CONTEXT * ctx)
606 if (MX_IDX(ctx->magic-1) && MX_COMMAND(ctx->magic-1,mx_fastclose_mailbox))
607 MX_COMMAND(ctx->magic-1,mx_fastclose_mailbox(ctx));
609 hash_destroy (&ctx->subj_hash, NULL);
611 hash_destroy (&ctx->id_hash, NULL);
612 mutt_clear_threads (ctx);
613 for (i = 0; i < ctx->msgcount; i++)
614 mutt_free_header (&ctx->hdrs[i]);
617 #ifdef USE_COMPRESSED
618 if (ctx->compressinfo)
619 mutt_fast_close_compressed (ctx);
622 FREE (&ctx->pattern);
623 if (ctx->limit_pattern)
624 mutt_pattern_free (&ctx->limit_pattern);
625 safe_fclose (&ctx->fp);
626 memset (ctx, 0, sizeof (CONTEXT));
629 /* save changes to disk */
630 static int sync_mailbox (CONTEXT * ctx, int *index_hint)
635 mutt_message (_("Writing %s..."), ctx->path);
637 if (MX_IDX(ctx->magic-1))
638 /* the 1 is only of interest for IMAP and means EXPUNGE */
639 rc = MX_COMMAND(ctx->magic-1,mx_sync_mailbox(ctx,1,index_hint));
641 #ifdef USE_COMPRESSED
642 if (rc == 0 && ctx->compressinfo)
643 return mutt_sync_compressed (ctx);
649 /* move deleted mails to the trash folder */
650 static int trash_append (CONTEXT * ctx)
656 if (!TrashPath || !ctx->deleted ||
657 (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))
660 for (; i < ctx->msgcount && (!ctx->hdrs[i]->deleted ||
661 ctx->hdrs[i]->appended); i++);
662 if (i == ctx->msgcount)
663 return 0; /* nothing to be done */
665 if (mutt_save_confirm (TrashPath, &st) != 0) {
666 mutt_error _("message(s) not deleted");
671 if (lstat (ctx->path, &stc) == 0 && stc.st_ino == st.st_ino
672 && stc.st_dev == st.st_dev && stc.st_rdev == st.st_rdev)
673 return 0; /* we are in the trash folder: simple sync */
675 if ((ctx_trash = mx_open_mailbox (TrashPath, M_APPEND, NULL)) != NULL) {
676 for (i = 0; i < ctx->msgcount; i++)
677 if (ctx->hdrs[i]->deleted && !ctx->hdrs[i]->appended
678 && !ctx->hdrs[i]->purged
679 && mutt_append_message (ctx_trash, ctx, ctx->hdrs[i], 0, 0) == -1) {
680 mx_close_mailbox (ctx_trash, NULL);
684 mx_close_mailbox (ctx_trash, NULL);
687 mutt_error _("Can't open trash folder");
695 /* save changes and close mailbox */
696 int mx_close_mailbox (CONTEXT * ctx, int *index_hint)
698 int i, move_messages = 0, purge = 1, read_msgs = 0;
702 char mbox[_POSIX_PATH_MAX];
703 char buf[SHORT_STRING];
711 if (ctx->magic == M_NNTP) {
714 ret = nntp_close_mailbox (ctx);
715 mx_fastclose_mailbox (ctx);
719 if (ctx->readonly || ctx->dontwrite) {
720 /* mailbox is readonly or we don't want to write */
721 mx_fastclose_mailbox (ctx);
726 /* mailbox was opened in write-mode */
727 if (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
728 mbox_close_mailbox (ctx);
730 mx_fastclose_mailbox (ctx);
734 for (i = 0; i < ctx->msgcount; i++) {
735 if (!ctx->hdrs[i]->deleted && ctx->hdrs[i]->read
736 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
740 if (read_msgs && quadoption (OPT_MOVE) != M_NO) {
743 if ((p = mutt_find_hook (M_MBOXHOOK, ctx->path))) {
745 strfcpy (mbox, p, sizeof (mbox));
748 strfcpy (mbox, NONULL (Inbox), sizeof (mbox));
749 isSpool = mutt_is_spool (ctx->path) && !mutt_is_spool (mbox);
751 mutt_expand_path (mbox, sizeof (mbox));
754 snprintf (buf, sizeof (buf), _("Move read messages to %s?"), mbox);
755 if ((move_messages = query_quadoption (OPT_MOVE, buf)) == -1) {
763 * There is no point in asking whether or not to purge if we are
764 * just marking messages as "trash".
766 if (ctx->deleted && !(ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH))) {
767 snprintf (buf, sizeof (buf), ctx->deleted == 1
768 ? _("Purge %d deleted message?") :
769 _("Purge %d deleted messages?"), ctx->deleted);
770 if ((purge = query_quadoption (OPT_DELETE, buf)) < 0) {
777 /* IMAP servers manage the OLD flag themselves */
778 if (ctx->magic != M_IMAP)
780 if (option (OPTMARKOLD)) {
781 for (i = 0; i < ctx->msgcount; i++) {
782 if (!ctx->hdrs[i]->deleted && !ctx->hdrs[i]->old)
783 mutt_set_flag (ctx, ctx->hdrs[i], M_OLD, 1);
788 mutt_message (_("Moving read messages to %s..."), mbox);
791 /* try to use server-side copy first */
794 if (ctx->magic == M_IMAP && imap_is_magic (mbox, NULL) == M_IMAP) {
795 /* tag messages for moving, and clear old tags, if any */
796 for (i = 0; i < ctx->msgcount; i++)
797 if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted
798 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
799 ctx->hdrs[i]->tagged = 1;
801 ctx->hdrs[i]->tagged = 0;
803 i = imap_copy_messages (ctx, NULL, mbox, 1);
806 if (i == 0) /* success */
808 else if (i == -1) { /* horrible error, bail */
812 else /* use regular append-copy mode */
815 if (mx_open_mailbox (mbox, M_APPEND, &f) == NULL) {
820 for (i = 0; i < ctx->msgcount; i++) {
821 if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted
822 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED))) {
823 if (mutt_append_message (&f, ctx, ctx->hdrs[i], 0, CH_UPDATE_LEN) ==
825 mutt_set_flag (ctx, ctx->hdrs[i], M_DELETE, 1);
826 mutt_set_flag (ctx, ctx->hdrs[i], M_APPENDED, 1);
829 mx_close_mailbox (&f, NULL);
836 mx_close_mailbox (&f, NULL);
840 else if (!ctx->changed && ctx->deleted == 0) {
841 mutt_message _("Mailbox is unchanged.");
843 mx_fastclose_mailbox (ctx);
847 /* copy mails to the trash before expunging */
848 if (purge && ctx->deleted)
849 if (trash_append (ctx) != 0) {
855 /* allow IMAP to preserve the deleted flag across sessions */
856 if (ctx->magic == M_IMAP) {
857 if ((check = imap_sync_mailbox (ctx, purge, index_hint)) != 0) {
866 for (i = 0; i < ctx->msgcount; i++)
867 ctx->hdrs[i]->deleted = 0;
871 if (ctx->changed || ctx->deleted) {
872 if ((check = sync_mailbox (ctx, index_hint)) != 0) {
880 mutt_message (_("%d kept, %d moved, %d deleted."),
881 ctx->msgcount - ctx->deleted, read_msgs, ctx->deleted);
883 mutt_message (_("%d kept, %d deleted."),
884 ctx->msgcount - ctx->deleted, ctx->deleted);
886 if (ctx->msgcount == ctx->deleted &&
887 (ctx->magic == M_MMDF || ctx->magic == M_MBOX) &&
888 !mutt_is_spool (ctx->path) && !option (OPTSAVEEMPTY))
889 mx_unlink_empty (ctx->path);
891 #ifdef USE_COMPRESSED
892 if (ctx->compressinfo && mutt_slow_close_compressed (ctx))
896 mx_fastclose_mailbox (ctx);
902 /* update a Context structure's internal tables. */
904 void mx_update_tables (CONTEXT * ctx, int committing)
908 /* update memory to reflect the new state of the mailbox */
917 #define this_body ctx->hdrs[j]->content
918 for (i = 0, j = 0; i < ctx->msgcount; i++) {
919 if ((committing && (!ctx->hdrs[i]->deleted ||
920 (ctx->magic == M_MAILDIR
921 && option (OPTMAILDIRTRASH)))) || (!committing
925 ctx->hdrs[j] = ctx->hdrs[i];
928 ctx->hdrs[j]->msgno = j;
929 if (ctx->hdrs[j]->virtual != -1) {
930 ctx->v2r[ctx->vcount] = j;
931 ctx->hdrs[j]->virtual = ctx->vcount++;
932 ctx->vsize += this_body->length + this_body->offset -
933 this_body->hdr_offset;
937 ctx->hdrs[j]->changed = 0;
938 else if (ctx->hdrs[j]->changed)
942 || (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH))) {
943 if (ctx->hdrs[j]->deleted)
947 if (ctx->hdrs[j]->tagged)
949 if (ctx->hdrs[j]->flagged)
951 if (!ctx->hdrs[j]->read) {
953 if (!ctx->hdrs[j]->old)
960 if (ctx->magic == M_MH || ctx->magic == M_MAILDIR)
961 ctx->size -= (ctx->hdrs[i]->content->length +
962 ctx->hdrs[i]->content->offset -
963 ctx->hdrs[i]->content->hdr_offset);
964 /* remove message from the hash tables */
965 if (ctx->subj_hash && ctx->hdrs[i]->env->real_subj)
966 hash_delete (ctx->subj_hash, ctx->hdrs[i]->env->real_subj,
968 if (ctx->id_hash && ctx->hdrs[i]->env->message_id)
969 hash_delete (ctx->id_hash, ctx->hdrs[i]->env->message_id,
971 mutt_free_header (&ctx->hdrs[i]);
979 /* save changes to mailbox
985 int mx_sync_mailbox (CONTEXT * ctx, int *index_hint)
989 int msgcount, deleted;
991 if (ctx->dontwrite) {
992 char buf[STRING], tmp[STRING];
994 if (km_expand_key (buf, sizeof (buf),
995 km_find_func (MENU_MAIN, OP_TOGGLE_WRITE)))
996 snprintf (tmp, sizeof (tmp), _(" Press '%s' to toggle write"), buf);
998 strfcpy (tmp, _("Use 'toggle-write' to re-enable write!"),
1001 mutt_error (_("Mailbox is marked unwritable. %s"), tmp);
1004 else if (ctx->readonly) {
1005 mutt_error _("Mailbox is read-only.");
1010 if (!ctx->changed && !ctx->deleted) {
1011 mutt_message _("Mailbox is unchanged.");
1017 char buf[SHORT_STRING];
1019 snprintf (buf, sizeof (buf), ctx->deleted == 1
1020 ? _("Purge %d deleted message?") :
1021 _("Purge %d deleted messages?"), ctx->deleted);
1022 if ((purge = query_quadoption (OPT_DELETE, buf)) < 0)
1024 else if (purge == M_NO) {
1026 return 0; /* nothing to do! */
1028 /* let IMAP servers hold on to D flags */
1029 if (ctx->magic != M_IMAP)
1032 for (i = 0; i < ctx->msgcount; i++)
1033 ctx->hdrs[i]->deleted = 0;
1037 else if (ctx->last_tag && ctx->last_tag->deleted)
1038 ctx->last_tag = NULL; /* reset last tagged msg now useless */
1041 /* really only for IMAP - imap_sync_mailbox results in a call to
1042 * mx_update_tables, so ctx->deleted is 0 when it comes back */
1043 msgcount = ctx->msgcount;
1044 deleted = ctx->deleted;
1046 if (purge && ctx->deleted) {
1047 if (trash_append (ctx) == -1)
1052 if (ctx->magic == M_IMAP)
1053 rc = imap_sync_mailbox (ctx, purge, index_hint);
1056 rc = sync_mailbox (ctx, index_hint);
1059 if (ctx->magic == M_IMAP && !purge)
1060 mutt_message (_("Mailbox checkpointed."));
1064 mutt_message (_("%d kept, %d deleted."), msgcount - deleted, deleted);
1068 if (ctx->msgcount == ctx->deleted &&
1069 (ctx->magic == M_MBOX || ctx->magic == M_MMDF) &&
1070 !mutt_is_spool (ctx->path) && !option (OPTSAVEEMPTY)) {
1072 mx_fastclose_mailbox (ctx);
1076 /* if we haven't deleted any messages, we don't need to resort */
1077 /* ... except for certain folder formats which need "unsorted"
1078 * sort order in order to synchronize folders.
1080 * MH and maildir are safe. mbox-style seems to need re-sorting,
1081 * at least with the new threading code.
1083 if (purge || (ctx->magic != M_MAILDIR && ctx->magic != M_MH)) {
1085 /* IMAP does this automatically after handling EXPUNGE */
1086 if (ctx->magic != M_IMAP)
1089 mx_update_tables (ctx, 1);
1090 mutt_sort_headers (ctx, 1); /* rethread from scratch */
1099 * dest destintation mailbox
1100 * hdr message being copied (required for maildir support, because
1101 * the filename depends on the message flags)
1103 MESSAGE *mx_open_new_message (CONTEXT * dest, HEADER * hdr, int flags)
1108 if (!MX_IDX(dest->magic-1)) {
1109 debug_print (1, ("function unimplemented for mailbox type %d.\n", dest->magic));
1113 msg = safe_calloc (1, sizeof (MESSAGE));
1114 msg->magic = dest->magic;
1118 msg->flags.flagged = hdr->flagged;
1119 msg->flags.replied = hdr->replied;
1120 msg->flags.read = hdr->read;
1121 msg->received = hdr->received;
1124 if (msg->received == 0)
1125 time (&msg->received);
1127 if (MX_COMMAND(dest->magic-1,mx_open_new_message)(msg, dest, hdr) == 0) {
1128 if (dest->magic == M_MMDF)
1129 fputs (MMDF_SEP, msg->fp);
1131 if ((msg->magic == M_MBOX || msg->magic == M_MMDF) && flags & M_ADD_FROM) {
1133 if (hdr->env->return_path)
1134 p = hdr->env->return_path;
1135 else if (hdr->env->sender)
1136 p = hdr->env->sender;
1141 fprintf (msg->fp, "From %s %s", p ? p->mailbox : NONULL (Username),
1142 ctime (&msg->received));
1151 /* check for new mail */
1152 int mx_check_mailbox (CONTEXT * ctx, int *index_hint, int lock)
1156 #ifdef USE_COMPRESSED
1157 if (ctx->compressinfo)
1158 return mutt_check_mailbox_compressed (ctx);
1165 switch (ctx->magic) {
1170 mutt_block_signals ();
1171 if (mbox_lock_mailbox (ctx, 0, 0) == -1) {
1172 mutt_unblock_signals ();
1177 rc = mbox_check_mailbox (ctx, index_hint);
1180 mutt_unblock_signals ();
1181 mbox_unlock_mailbox (ctx);
1188 return (mh_check_mailbox (ctx, index_hint));
1190 return (maildir_check_mailbox (ctx, index_hint));
1194 return (imap_check_mailbox (ctx, index_hint, 0));
1195 #endif /* USE_IMAP */
1199 return (pop_check_mailbox (ctx, index_hint));
1200 #endif /* USE_POP */
1204 return (nntp_check_mailbox (ctx));
1205 #endif /* USE_NNTP */
1209 debug_print (1, ("null or invalid context.\n"));
1213 /* return a stream pointer for a message */
1214 MESSAGE *mx_open_message (CONTEXT * ctx, int msgno)
1218 msg = safe_calloc (1, sizeof (MESSAGE));
1219 switch (msg->magic = ctx->magic) {
1228 HEADER *cur = ctx->hdrs[msgno];
1229 char path[_POSIX_PATH_MAX];
1231 snprintf (path, sizeof (path), "%s/%s", ctx->path, cur->path);
1233 if ((msg->fp = fopen (path, "r")) == NULL && errno == ENOENT &&
1234 ctx->magic == M_MAILDIR)
1235 msg->fp = maildir_open_find_message (ctx->path, cur->path);
1237 if (msg->fp == NULL) {
1239 debug_print (1, ("fopen: %s: %s (errno %d).\n", path, strerror (errno), errno));
1248 if (imap_fetch_message (msg, ctx, msgno) != 0)
1252 #endif /* USE_IMAP */
1257 if (pop_fetch_message (msg, ctx, msgno) != 0)
1261 #endif /* USE_POP */
1266 if (nntp_fetch_message (msg, ctx, msgno) != 0)
1270 #endif /* USE_NNTP */
1273 debug_print (1, ("function not implemented for mailbox type %d.\n", ctx->magic));
1280 /* commit a message to a folder */
1282 int mx_commit_message (MESSAGE * msg, CONTEXT * ctx)
1286 if (!(msg->write && ctx->append)) {
1287 debug_print (1, ("msg->write = %d, ctx->append = %d\n", msg->write, ctx->append));
1291 switch (msg->magic) {
1294 if (fputs (MMDF_SEP, msg->fp) == EOF)
1301 if (fputc ('\n', msg->fp) == EOF)
1309 if ((r = safe_fclose (&msg->fp)) == 0)
1310 r = imap_append_message (ctx, msg);
1317 r = maildir_commit_message (ctx, msg, NULL);
1323 r = mh_commit_message (ctx, msg, NULL);
1328 if (r == 0 && (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
1329 && (fflush (msg->fp) == EOF || fsync (fileno (msg->fp)) == -1)) {
1330 mutt_perror (_("Can't write message"));
1338 /* close a pointer to a message */
1339 int mx_close_message (MESSAGE ** msg)
1343 if ((*msg)->magic == M_MH || (*msg)->magic == M_MAILDIR
1345 || (*msg)->magic == M_IMAP
1348 || (*msg)->magic == M_POP
1351 || (*msg)->magic == M_NNTP
1354 r = safe_fclose (&(*msg)->fp);
1360 debug_print (1, ("unlinking %s\n", (*msg)->path));
1361 unlink ((*msg)->path);
1362 FREE (&(*msg)->path);
1369 void mx_alloc_memory (CONTEXT * ctx)
1372 size_t s = MAX (sizeof (HEADER *), sizeof (int));
1374 if ((ctx->hdrmax + 25) * s < ctx->hdrmax * s) {
1375 mutt_error _("Integer overflow -- can't allocate memory.");
1382 safe_realloc (&ctx->hdrs, sizeof (HEADER *) * (ctx->hdrmax += 25));
1383 safe_realloc (&ctx->v2r, sizeof (int) * ctx->hdrmax);
1386 ctx->hdrs = safe_calloc ((ctx->hdrmax += 25), sizeof (HEADER *));
1387 ctx->v2r = safe_calloc (ctx->hdrmax, sizeof (int));
1389 for (i = ctx->msgcount; i < ctx->hdrmax; i++) {
1390 ctx->hdrs[i] = NULL;
1395 /* this routine is called to update the counts in the context structure for
1396 * the last message header parsed.
1398 void mx_update_context (CONTEXT * ctx, int new_messages)
1403 for (msgno = ctx->msgcount - new_messages; msgno < ctx->msgcount; msgno++) {
1404 h = ctx->hdrs[msgno];
1407 /* NOTE: this _must_ be done before the check for mailcap! */
1408 h->security = crypt_query (h->content);
1411 if (!ctx->pattern) {
1412 ctx->v2r[ctx->vcount] = msgno;
1413 h->virtual = ctx->vcount++;
1419 if (h->env->supersedes) {
1423 ctx->id_hash = mutt_make_id_hash (ctx);
1425 h2 = hash_find (ctx->id_hash, h->env->supersedes);
1427 /* FREE (&h->env->supersedes); should I ? */
1430 if (!ctx->counting && option (OPTSCORE))
1431 mutt_score_message (ctx, h2, 1);
1435 /* add this message to the hash tables */
1436 if (ctx->id_hash && h->env->message_id)
1437 hash_insert (ctx->id_hash, h->env->message_id, h, 0);
1438 if (!ctx->counting) {
1439 if (ctx->subj_hash && h->env->real_subj)
1440 hash_insert (ctx->subj_hash, h->env->real_subj, h, 1);
1442 if (option (OPTSCORE))
1443 mutt_score_message (ctx, h, 0);
1462 * 1 if the specified mailbox contains 0 messages.
1463 * 0 if the mailbox contains messages
1466 int mx_check_empty (const char *path)
1469 if ((i = mx_get_idx (path)) >= 0 && MX_COMMAND(i,mx_check_empty))
1470 return (MX_COMMAND(i,mx_check_empty)(path));
1475 int mx_acl_check (CONTEXT* ctx, int flag) {
1476 if (!ctx || !MX_IDX(ctx->magic-1))
1478 /* if no acl_check defined for module, assume permission is granted */
1479 if (!MX_COMMAND(ctx->magic-1,mx_acl_check))
1481 return (MX_COMMAND(ctx->magic-1,mx_acl_check)(ctx,flag));
1484 void mx_init (void) {
1488 list_push_back (&MailboxFormats, (void*) mbox_reg_mx ());
1489 list_push_back (&MailboxFormats, (void*) mmdf_reg_mx ());
1490 list_push_back (&MailboxFormats, (void*) mh_reg_mx ());
1491 list_push_back (&MailboxFormats, (void*) maildir_reg_mx ());
1493 list_push_back (&MailboxFormats, (void*) imap_reg_mx ());
1496 list_push_back (&MailboxFormats, (void*) pop_reg_mx ());
1499 list_push_back (&MailboxFormats, (void*) nntp_reg_mx ());
1501 #ifdef USE_COMPRESSED
1502 list_push_back (&MailboxFormats, (void*) compress_reg_mx ());
1505 /* check module registration for completeness with debug versions */
1506 #define EXITWITHERR(m) do { fprintf(stderr, "error: incomplete mx module: %s is missing for type %i\n",m,i);exit(1); } while (0)
1507 for (i = 0; i < MailboxFormats->length; i++) {
1508 if (MX_COMMAND(i,type) < 1) EXITWITHERR("type");
1509 if (!MX_COMMAND(i,mx_is_magic)) EXITWITHERR("mx_is_magic");
1510 if (!MX_COMMAND(i,mx_open_mailbox)) EXITWITHERR("mx_open_mailbox");
1511 /* if (!MX_COMMAND(i,mx_sync_mailbox)) EXITWITHERR("mx_sync_mailbox");*/