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"
70 static list2_t* MailboxFormats = NULL;
71 #define MX_COMMAND(idx,cmd) ((mx_t*) MailboxFormats->data[idx])->cmd
72 #define MX_IDX(idx) (idx >= 0 && idx < MailboxFormats->length)
74 #define mutt_is_spool(s) (safe_strcmp (Spoolfile, s) == 0)
79 * retry - should retry if unable to lock?
84 static int invoke_dotlock (const char *path, int dummy, int flags, int retry)
86 char cmd[LONG_STRING + _POSIX_PATH_MAX];
87 char f[SHORT_STRING + _POSIX_PATH_MAX];
90 if (flags & DL_FL_RETRY)
91 snprintf (r, sizeof (r), "-r %d ", retry ? MAXLOCKATTEMPT : 0);
93 mutt_quote_filename (f, sizeof (f), path);
95 snprintf (cmd, sizeof (cmd),
98 flags & DL_FL_TRY ? "-t " : "",
99 flags & DL_FL_UNLOCK ? "-u " : "",
100 flags & DL_FL_USEPRIV ? "-p " : "",
101 flags & DL_FL_FORCE ? "-f " : "",
102 flags & DL_FL_UNLINK ? "-d " : "",
103 flags & DL_FL_RETRY ? r : "", f);
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;
123 if ((r = invoke_dotlock (path, fd, flags, retry)) == DL_EX_EXIST) {
124 if (!option (OPTNOCURSES)) {
125 char msg[LONG_STRING];
127 snprintf (msg, sizeof (msg),
128 _("Lock count exceeded, remove lock for %s?"), path);
129 if (retry && mutt_yesorno (msg, M_YES) == M_YES) {
130 flags |= DL_FL_FORCE;
137 mutt_error (_("Can't dotlock %s.\n"), path);
140 return (r == DL_EX_OK ? 0 : -1);
143 static int undotlock_file (const char *path, int fd)
145 return (invoke_dotlock (path, fd, DL_FL_USEPRIV | DL_FL_UNLOCK, 0) ==
149 #endif /* USE_DOTLOCK */
151 /* looks up index of type for path in MailboxFormats */
152 static int mx_get_idx (const char* path) {
156 /* first, test all non-local folders to avoid stat() call */
157 for (i = 0; i < MailboxFormats->length; i++) {
158 if (!MX_COMMAND(i,local))
159 t = MX_COMMAND(i,mx_is_magic)(path, NULL);
163 if (stat (path, &st) == 0) {
164 /* if stat() succeeded, keep testing until success and
165 * pass stat() info so that we only need to do it once */
166 for (i = 0; i < MailboxFormats->length; i++) {
167 if (MX_COMMAND(i,local))
168 t = MX_COMMAND(i,mx_is_magic)(path, &st);
177 * excl if excl != 0, request an exclusive lock
178 * dot if dot != 0, try to dotlock the file
179 * timeout should retry locking?
181 int mx_lock_file (const char *path, int fd, int excl, int dot, int timeout)
183 #if defined (USE_FCNTL) || defined (USE_FLOCK)
194 memset (&lck, 0, sizeof (struct flock));
195 lck.l_type = excl ? F_WRLCK : F_RDLCK;
196 lck.l_whence = SEEK_SET;
200 while (fcntl (fd, F_SETLK, &lck) == -1) {
203 dprint (1, (debugfile, "mx_lock_file(): fcntl errno %d.\n", errno));
204 if (errno != EAGAIN && errno != EACCES) {
205 mutt_perror ("fcntl");
209 if (fstat (fd, &sb) != 0)
215 /* only unlock file if it is unchanged */
216 if (prev_sb.st_size == sb.st_size
217 && ++count >= (timeout ? MAXLOCKATTEMPT : 0)) {
219 mutt_error _("Timeout exceeded while attempting fcntl lock!");
226 mutt_message (_("Waiting for fcntl lock... %d"), ++attempt);
229 #endif /* USE_FCNTL */
234 while (flock (fd, (excl ? LOCK_EX : LOCK_SH) | LOCK_NB) == -1) {
237 if (errno != EWOULDBLOCK) {
238 mutt_perror ("flock");
243 if (fstat (fd, &sb) != 0)
249 /* only unlock file if it is unchanged */
250 if (prev_sb.st_size == sb.st_size
251 && ++count >= (timeout ? MAXLOCKATTEMPT : 0)) {
253 mutt_error _("Timeout exceeded while attempting flock lock!");
261 mutt_message (_("Waiting for flock attempt... %d"), ++attempt);
264 #endif /* USE_FLOCK */
268 r = dotlock_file (path, fd, timeout);
269 #endif /* USE_DOTLOCK */
272 /* release any other locks obtained in this routine */
275 lck.l_type = F_UNLCK;
276 fcntl (fd, F_SETLK, &lck);
277 #endif /* USE_FCNTL */
281 #endif /* USE_FLOCK */
289 int mx_unlock_file (const char *path, int fd, int dot)
292 struct flock unlockit = { F_UNLCK, 0, 0, 0 };
294 memset (&unlockit, 0, sizeof (struct flock));
295 unlockit.l_type = F_UNLCK;
296 unlockit.l_whence = SEEK_SET;
297 fcntl (fd, F_SETLK, &unlockit);
306 undotlock_file (path, fd);
312 void mx_unlink_empty (const char *path)
320 if ((fd = open (path, O_RDWR)) == -1)
323 if (mx_lock_file (path, fd, 1, 0, 1) == -1) {
329 invoke_dotlock (path, fd, DL_FL_UNLINK, 1);
331 if (fstat (fd, &sb) == 0 && sb.st_size == 0)
335 mx_unlock_file (path, fd, 0);
339 /* try to figure out what type of mailbox ``path'' is */
340 int mx_get_magic (const char *path) {
343 if (safe_strlen (path) == 0)
345 if ((i = mx_get_idx (path)) >= 0)
346 return (MX_COMMAND(i,type));
350 int mx_is_local (int m) {
353 return (MX_COMMAND(m,local));
357 * set DefaultMagic to the given value
359 int mx_set_magic (const char *s)
361 if (ascii_strcasecmp (s, "mbox") == 0)
362 DefaultMagic = M_MBOX;
363 else if (ascii_strcasecmp (s, "mmdf") == 0)
364 DefaultMagic = M_MMDF;
365 else if (ascii_strcasecmp (s, "mh") == 0)
367 else if (ascii_strcasecmp (s, "maildir") == 0)
368 DefaultMagic = M_MAILDIR;
375 /* mx_access: Wrapper for access, checks permissions on a given mailbox.
376 * We may be interested in using ACL-style flags at some point, currently
377 * we use the normal access() flags. */
378 int mx_access (const char *path, int flags)
382 if ((i = mx_get_idx (path)) >= 0 && MX_COMMAND(i,mx_access))
383 return (MX_COMMAND(i,mx_access)(path,flags));
387 static int mx_open_mailbox_append (CONTEXT * ctx, int flags)
391 #ifdef USE_COMPRESSED
392 /* special case for appending to compressed folders -
393 * even if we can not open them for reading */
394 if (mutt_can_append_compressed (ctx->path))
395 mutt_open_append_compressed (ctx);
402 if (mx_get_magic (ctx->path) == M_IMAP)
403 return imap_open_mailbox_append (ctx);
407 if (stat (ctx->path, &sb) == 0) {
408 ctx->magic = mx_get_magic (ctx->path);
410 switch (ctx->magic) {
412 mutt_error (_("%s is not a mailbox."), ctx->path);
418 else if (errno == ENOENT) {
419 ctx->magic = DefaultMagic;
421 if (ctx->magic == M_MH || ctx->magic == M_MAILDIR) {
422 char tmp[_POSIX_PATH_MAX];
424 if (mkdir (ctx->path, S_IRWXU)) {
425 mutt_perror (ctx->path);
429 if (ctx->magic == M_MAILDIR) {
430 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
431 if (mkdir (tmp, S_IRWXU)) {
437 snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
438 if (mkdir (tmp, S_IRWXU)) {
440 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
445 snprintf (tmp, sizeof (tmp), "%s/tmp", ctx->path);
446 if (mkdir (tmp, S_IRWXU)) {
448 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
450 snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
459 snprintf (tmp, sizeof (tmp), "%s/.mh_sequences", ctx->path);
460 if ((i = creat (tmp, S_IRWXU)) == -1) {
470 mutt_perror (ctx->path);
474 switch (ctx->magic) {
478 safe_fopen (ctx->path, flags & M_NEWFOLDER ? "w" : "a")) == NULL
479 || mbox_lock_mailbox (ctx, 1, 1) != 0) {
481 mutt_perror (ctx->path);
483 mutt_error (_("Couldn't lock %s\n"), ctx->path);
484 safe_fclose (&ctx->fp);
488 fseek (ctx->fp, 0, 2);
504 * open a mailbox and parse it
507 * flags M_NOSORT do not sort mailbox
508 * M_APPEND open mailbox for appending
509 * M_READONLY open mailbox in read-only mode
510 * M_QUIET only print error messages
511 * ctx if non-null, context struct to use
513 CONTEXT *mx_open_mailbox (const char *path, int flags, CONTEXT * pctx)
519 ctx = safe_malloc (sizeof (CONTEXT));
520 memset (ctx, 0, sizeof (CONTEXT));
521 ctx->path = safe_strdup (path);
523 ctx->msgnotreadyet = -1;
528 if (flags & M_READONLY)
533 if (flags & (M_APPEND | M_NEWFOLDER)) {
534 if (mx_open_mailbox_append (ctx, flags) != 0) {
535 mx_fastclose_mailbox (ctx);
543 if (!MX_IDX(ctx->magic-1))
544 ctx->magic = mx_get_magic (path);
546 #ifdef USE_COMPRESSED
547 if (ctx->magic == M_COMPRESSED)
548 mutt_open_read_compressed (ctx);
552 mutt_error (_("%s is not a mailbox."), path);
554 if (ctx->magic == -1)
557 if (ctx->magic <= 0) {
558 mx_fastclose_mailbox (ctx);
564 /* if the user has a `push' command in their .muttrc, or in a folder-hook,
565 * it will cause the progress messages not to be displayed because
566 * mutt_refresh() will think we are in the middle of a macro. so set a
567 * flag to indicate that we should really refresh the screen.
569 set_option (OPTFORCEREFRESH);
572 mutt_message (_("Reading %s..."), ctx->path);
574 rc = MX_COMMAND(ctx->magic-1,mx_open_mailbox)(ctx);
577 if ((flags & M_NOSORT) == 0) {
578 /* avoid unnecessary work since the mailbox is completely unthreaded
580 unset_option (OPTSORTSUBTHREADS);
581 unset_option (OPTNEEDRESCORE);
582 mutt_sort_headers (ctx, 1);
588 mx_fastclose_mailbox (ctx);
593 unset_option (OPTFORCEREFRESH);
597 /* free up memory associated with the mailbox context */
598 void mx_fastclose_mailbox (CONTEXT * ctx)
605 if (MX_IDX(ctx->magic-1) && MX_COMMAND(ctx->magic-1,mx_fastclose_mailbox))
606 MX_COMMAND(ctx->magic-1,mx_fastclose_mailbox(ctx));
608 hash_destroy (&ctx->subj_hash, NULL);
610 hash_destroy (&ctx->id_hash, NULL);
611 mutt_clear_threads (ctx);
612 for (i = 0; i < ctx->msgcount; i++)
613 mutt_free_header (&ctx->hdrs[i]);
616 #ifdef USE_COMPRESSED
617 if (ctx->compressinfo)
618 mutt_fast_close_compressed (ctx);
621 FREE (&ctx->pattern);
622 if (ctx->limit_pattern)
623 mutt_pattern_free (&ctx->limit_pattern);
624 safe_fclose (&ctx->fp);
625 memset (ctx, 0, sizeof (CONTEXT));
628 /* save changes to disk */
629 static int sync_mailbox (CONTEXT * ctx, int *index_hint)
634 mutt_message (_("Writing %s..."), ctx->path);
636 if (MX_IDX(ctx->magic-1))
637 /* the 1 is only of interest for IMAP and means EXPUNGE */
638 rc = MX_COMMAND(ctx->magic-1,mx_sync_mailbox(ctx,1,index_hint));
640 #ifdef USE_COMPRESSED
641 if (rc == 0 && ctx->compressinfo)
642 return mutt_sync_compressed (ctx);
648 /* move deleted mails to the trash folder */
649 static int trash_append (CONTEXT * ctx)
655 if (!TrashPath || !ctx->deleted ||
656 (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))
659 for (; i < ctx->msgcount && (!ctx->hdrs[i]->deleted ||
660 ctx->hdrs[i]->appended); i++);
661 if (i == ctx->msgcount)
662 return 0; /* nothing to be done */
664 if (mutt_save_confirm (TrashPath, &st) != 0) {
665 mutt_error _("message(s) not deleted");
670 if (lstat (ctx->path, &stc) == 0 && stc.st_ino == st.st_ino
671 && stc.st_dev == st.st_dev && stc.st_rdev == st.st_rdev)
672 return 0; /* we are in the trash folder: simple sync */
674 if ((ctx_trash = mx_open_mailbox (TrashPath, M_APPEND, NULL)) != NULL) {
675 for (i = 0; i < ctx->msgcount; i++)
676 if (ctx->hdrs[i]->deleted && !ctx->hdrs[i]->appended
677 && !ctx->hdrs[i]->purged
678 && mutt_append_message (ctx_trash, ctx, ctx->hdrs[i], 0, 0) == -1) {
679 mx_close_mailbox (ctx_trash, NULL);
683 mx_close_mailbox (ctx_trash, NULL);
686 mutt_error _("Can't open trash folder");
694 /* save changes and close mailbox */
695 int mx_close_mailbox (CONTEXT * ctx, int *index_hint)
697 int i, move_messages = 0, purge = 1, read_msgs = 0;
701 char mbox[_POSIX_PATH_MAX];
702 char buf[SHORT_STRING];
710 if (ctx->magic == M_NNTP) {
713 ret = nntp_close_mailbox (ctx);
714 mx_fastclose_mailbox (ctx);
718 if (ctx->readonly || ctx->dontwrite) {
719 /* mailbox is readonly or we don't want to write */
720 mx_fastclose_mailbox (ctx);
725 /* mailbox was opened in write-mode */
726 if (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
727 mbox_close_mailbox (ctx);
729 mx_fastclose_mailbox (ctx);
733 for (i = 0; i < ctx->msgcount; i++) {
734 if (!ctx->hdrs[i]->deleted && ctx->hdrs[i]->read
735 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
739 if (read_msgs && quadoption (OPT_MOVE) != M_NO) {
742 if ((p = mutt_find_hook (M_MBOXHOOK, ctx->path))) {
744 strfcpy (mbox, p, sizeof (mbox));
747 strfcpy (mbox, NONULL (Inbox), sizeof (mbox));
748 isSpool = mutt_is_spool (ctx->path) && !mutt_is_spool (mbox);
750 mutt_expand_path (mbox, sizeof (mbox));
753 snprintf (buf, sizeof (buf), _("Move read messages to %s?"), mbox);
754 if ((move_messages = query_quadoption (OPT_MOVE, buf)) == -1) {
762 * There is no point in asking whether or not to purge if we are
763 * just marking messages as "trash".
765 if (ctx->deleted && !(ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH))) {
766 snprintf (buf, sizeof (buf), ctx->deleted == 1
767 ? _("Purge %d deleted message?") :
768 _("Purge %d deleted messages?"), ctx->deleted);
769 if ((purge = query_quadoption (OPT_DELETE, buf)) < 0) {
776 /* IMAP servers manage the OLD flag themselves */
777 if (ctx->magic != M_IMAP)
779 if (option (OPTMARKOLD)) {
780 for (i = 0; i < ctx->msgcount; i++) {
781 if (!ctx->hdrs[i]->deleted && !ctx->hdrs[i]->old)
782 mutt_set_flag (ctx, ctx->hdrs[i], M_OLD, 1);
787 mutt_message (_("Moving read messages to %s..."), mbox);
790 /* try to use server-side copy first */
793 if (ctx->magic == M_IMAP && imap_is_magic (mbox, NULL) == M_IMAP) {
794 /* tag messages for moving, and clear old tags, if any */
795 for (i = 0; i < ctx->msgcount; i++)
796 if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted
797 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
798 ctx->hdrs[i]->tagged = 1;
800 ctx->hdrs[i]->tagged = 0;
802 i = imap_copy_messages (ctx, NULL, mbox, 1);
805 if (i == 0) /* success */
807 else if (i == -1) { /* horrible error, bail */
811 else /* use regular append-copy mode */
814 if (mx_open_mailbox (mbox, M_APPEND, &f) == NULL) {
819 for (i = 0; i < ctx->msgcount; i++) {
820 if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted
821 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED))) {
822 if (mutt_append_message (&f, ctx, ctx->hdrs[i], 0, CH_UPDATE_LEN) ==
824 mutt_set_flag (ctx, ctx->hdrs[i], M_DELETE, 1);
825 mutt_set_flag (ctx, ctx->hdrs[i], M_APPENDED, 1);
828 mx_close_mailbox (&f, NULL);
835 mx_close_mailbox (&f, NULL);
839 else if (!ctx->changed && ctx->deleted == 0) {
840 mutt_message _("Mailbox is unchanged.");
842 mx_fastclose_mailbox (ctx);
846 /* copy mails to the trash before expunging */
847 if (purge && ctx->deleted)
848 if (trash_append (ctx) != 0) {
854 /* allow IMAP to preserve the deleted flag across sessions */
855 if (ctx->magic == M_IMAP) {
856 if ((check = imap_sync_mailbox (ctx, purge, index_hint)) != 0) {
865 for (i = 0; i < ctx->msgcount; i++)
866 ctx->hdrs[i]->deleted = 0;
870 if (ctx->changed || ctx->deleted) {
871 if ((check = sync_mailbox (ctx, index_hint)) != 0) {
879 mutt_message (_("%d kept, %d moved, %d deleted."),
880 ctx->msgcount - ctx->deleted, read_msgs, ctx->deleted);
882 mutt_message (_("%d kept, %d deleted."),
883 ctx->msgcount - ctx->deleted, ctx->deleted);
885 if (ctx->msgcount == ctx->deleted &&
886 (ctx->magic == M_MMDF || ctx->magic == M_MBOX) &&
887 !mutt_is_spool (ctx->path) && !option (OPTSAVEEMPTY))
888 mx_unlink_empty (ctx->path);
890 #ifdef USE_COMPRESSED
891 if (ctx->compressinfo && mutt_slow_close_compressed (ctx))
895 mx_fastclose_mailbox (ctx);
901 /* update a Context structure's internal tables. */
903 void mx_update_tables (CONTEXT * ctx, int committing)
907 /* update memory to reflect the new state of the mailbox */
916 #define this_body ctx->hdrs[j]->content
917 for (i = 0, j = 0; i < ctx->msgcount; i++) {
918 if ((committing && (!ctx->hdrs[i]->deleted ||
919 (ctx->magic == M_MAILDIR
920 && option (OPTMAILDIRTRASH)))) || (!committing
924 ctx->hdrs[j] = ctx->hdrs[i];
927 ctx->hdrs[j]->msgno = j;
928 if (ctx->hdrs[j]->virtual != -1) {
929 ctx->v2r[ctx->vcount] = j;
930 ctx->hdrs[j]->virtual = ctx->vcount++;
931 ctx->vsize += this_body->length + this_body->offset -
932 this_body->hdr_offset;
936 ctx->hdrs[j]->changed = 0;
937 else if (ctx->hdrs[j]->changed)
941 || (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH))) {
942 if (ctx->hdrs[j]->deleted)
946 if (ctx->hdrs[j]->tagged)
948 if (ctx->hdrs[j]->flagged)
950 if (!ctx->hdrs[j]->read) {
952 if (!ctx->hdrs[j]->old)
959 if (ctx->magic == M_MH || ctx->magic == M_MAILDIR)
960 ctx->size -= (ctx->hdrs[i]->content->length +
961 ctx->hdrs[i]->content->offset -
962 ctx->hdrs[i]->content->hdr_offset);
963 /* remove message from the hash tables */
964 if (ctx->subj_hash && ctx->hdrs[i]->env->real_subj)
965 hash_delete (ctx->subj_hash, ctx->hdrs[i]->env->real_subj,
967 if (ctx->id_hash && ctx->hdrs[i]->env->message_id)
968 hash_delete (ctx->id_hash, ctx->hdrs[i]->env->message_id,
970 mutt_free_header (&ctx->hdrs[i]);
978 /* save changes to mailbox
984 int mx_sync_mailbox (CONTEXT * ctx, int *index_hint)
988 int msgcount, deleted;
990 if (ctx->dontwrite) {
991 char buf[STRING], tmp[STRING];
993 if (km_expand_key (buf, sizeof (buf),
994 km_find_func (MENU_MAIN, OP_TOGGLE_WRITE)))
995 snprintf (tmp, sizeof (tmp), _(" Press '%s' to toggle write"), buf);
997 strfcpy (tmp, _("Use 'toggle-write' to re-enable write!"),
1000 mutt_error (_("Mailbox is marked unwritable. %s"), tmp);
1003 else if (ctx->readonly) {
1004 mutt_error _("Mailbox is read-only.");
1009 if (!ctx->changed && !ctx->deleted) {
1010 mutt_message _("Mailbox is unchanged.");
1016 char buf[SHORT_STRING];
1018 snprintf (buf, sizeof (buf), ctx->deleted == 1
1019 ? _("Purge %d deleted message?") :
1020 _("Purge %d deleted messages?"), ctx->deleted);
1021 if ((purge = query_quadoption (OPT_DELETE, buf)) < 0)
1023 else if (purge == M_NO) {
1025 return 0; /* nothing to do! */
1027 /* let IMAP servers hold on to D flags */
1028 if (ctx->magic != M_IMAP)
1031 for (i = 0; i < ctx->msgcount; i++)
1032 ctx->hdrs[i]->deleted = 0;
1036 else if (ctx->last_tag && ctx->last_tag->deleted)
1037 ctx->last_tag = NULL; /* reset last tagged msg now useless */
1040 /* really only for IMAP - imap_sync_mailbox results in a call to
1041 * mx_update_tables, so ctx->deleted is 0 when it comes back */
1042 msgcount = ctx->msgcount;
1043 deleted = ctx->deleted;
1045 if (purge && ctx->deleted) {
1046 if (trash_append (ctx) == -1)
1051 if (ctx->magic == M_IMAP)
1052 rc = imap_sync_mailbox (ctx, purge, index_hint);
1055 rc = sync_mailbox (ctx, index_hint);
1058 if (ctx->magic == M_IMAP && !purge)
1059 mutt_message (_("Mailbox checkpointed."));
1063 mutt_message (_("%d kept, %d deleted."), msgcount - deleted, deleted);
1067 if (ctx->msgcount == ctx->deleted &&
1068 (ctx->magic == M_MBOX || ctx->magic == M_MMDF) &&
1069 !mutt_is_spool (ctx->path) && !option (OPTSAVEEMPTY)) {
1071 mx_fastclose_mailbox (ctx);
1075 /* if we haven't deleted any messages, we don't need to resort */
1076 /* ... except for certain folder formats which need "unsorted"
1077 * sort order in order to synchronize folders.
1079 * MH and maildir are safe. mbox-style seems to need re-sorting,
1080 * at least with the new threading code.
1082 if (purge || (ctx->magic != M_MAILDIR && ctx->magic != M_MH)) {
1084 /* IMAP does this automatically after handling EXPUNGE */
1085 if (ctx->magic != M_IMAP)
1088 mx_update_tables (ctx, 1);
1089 mutt_sort_headers (ctx, 1); /* rethread from scratch */
1098 int imap_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr)
1100 char tmp[_POSIX_PATH_MAX];
1103 if ((msg->fp = safe_fopen (tmp, "w")) == NULL)
1105 msg->path = safe_strdup (tmp);
1111 * dest destintation mailbox
1112 * hdr message being copied (required for maildir support, because
1113 * the filename depends on the message flags)
1115 MESSAGE *mx_open_new_message (CONTEXT * dest, HEADER * hdr, int flags)
1118 int (*func) (MESSAGE *, CONTEXT *, HEADER *);
1121 switch (dest->magic) {
1124 func = mbox_open_new_message;
1127 func = maildir_open_new_message;
1130 func = mh_open_new_message;
1134 func = imap_open_new_message;
1140 "mx_open_new_message(): function unimplemented for mailbox type %d.\n",
1145 msg = safe_calloc (1, sizeof (MESSAGE));
1146 msg->magic = dest->magic;
1150 msg->flags.flagged = hdr->flagged;
1151 msg->flags.replied = hdr->replied;
1152 msg->flags.read = hdr->read;
1153 msg->received = hdr->received;
1156 if (msg->received == 0)
1157 time (&msg->received);
1159 if (func (msg, dest, hdr) == 0) {
1160 if (dest->magic == M_MMDF)
1161 fputs (MMDF_SEP, msg->fp);
1163 if ((msg->magic == M_MBOX || msg->magic == M_MMDF) && flags & M_ADD_FROM) {
1165 if (hdr->env->return_path)
1166 p = hdr->env->return_path;
1167 else if (hdr->env->sender)
1168 p = hdr->env->sender;
1173 fprintf (msg->fp, "From %s %s", p ? p->mailbox : NONULL (Username),
1174 ctime (&msg->received));
1183 /* check for new mail */
1184 int mx_check_mailbox (CONTEXT * ctx, int *index_hint, int lock)
1188 #ifdef USE_COMPRESSED
1189 if (ctx->compressinfo)
1190 return mutt_check_mailbox_compressed (ctx);
1197 switch (ctx->magic) {
1202 mutt_block_signals ();
1203 if (mbox_lock_mailbox (ctx, 0, 0) == -1) {
1204 mutt_unblock_signals ();
1209 rc = mbox_check_mailbox (ctx, index_hint);
1212 mutt_unblock_signals ();
1213 mbox_unlock_mailbox (ctx);
1220 return (mh_check_mailbox (ctx, index_hint));
1222 return (maildir_check_mailbox (ctx, index_hint));
1226 return (imap_check_mailbox (ctx, index_hint, 0));
1227 #endif /* USE_IMAP */
1231 return (pop_check_mailbox (ctx, index_hint));
1232 #endif /* USE_POP */
1236 return (nntp_check_mailbox (ctx));
1237 #endif /* USE_NNTP */
1241 dprint (1, (debugfile, "mx_check_mailbox: null or invalid context.\n"));
1245 /* return a stream pointer for a message */
1246 MESSAGE *mx_open_message (CONTEXT * ctx, int msgno)
1250 msg = safe_calloc (1, sizeof (MESSAGE));
1251 switch (msg->magic = ctx->magic) {
1260 HEADER *cur = ctx->hdrs[msgno];
1261 char path[_POSIX_PATH_MAX];
1263 snprintf (path, sizeof (path), "%s/%s", ctx->path, cur->path);
1265 if ((msg->fp = fopen (path, "r")) == NULL && errno == ENOENT &&
1266 ctx->magic == M_MAILDIR)
1267 msg->fp = maildir_open_find_message (ctx->path, cur->path);
1269 if (msg->fp == NULL) {
1271 dprint (1, (debugfile, "mx_open_message: fopen: %s: %s (errno %d).\n",
1272 path, strerror (errno), errno));
1281 if (imap_fetch_message (msg, ctx, msgno) != 0)
1285 #endif /* USE_IMAP */
1290 if (pop_fetch_message (msg, ctx, msgno) != 0)
1294 #endif /* USE_POP */
1299 if (nntp_fetch_message (msg, ctx, msgno) != 0)
1303 #endif /* USE_NNTP */
1308 "mx_open_message(): function not implemented for mailbox type %d.\n",
1316 /* commit a message to a folder */
1318 int mx_commit_message (MESSAGE * msg, CONTEXT * ctx)
1322 if (!(msg->write && ctx->append)) {
1325 "mx_commit_message(): msg->write = %d, ctx->append = %d\n",
1326 msg->write, ctx->append));
1330 switch (msg->magic) {
1333 if (fputs (MMDF_SEP, msg->fp) == EOF)
1340 if (fputc ('\n', msg->fp) == EOF)
1348 if ((r = safe_fclose (&msg->fp)) == 0)
1349 r = imap_append_message (ctx, msg);
1356 r = maildir_commit_message (ctx, msg, NULL);
1362 r = mh_commit_message (ctx, msg, NULL);
1367 if (r == 0 && (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
1368 && (fflush (msg->fp) == EOF || fsync (fileno (msg->fp)) == -1)) {
1369 mutt_perror (_("Can't write message"));
1377 /* close a pointer to a message */
1378 int mx_close_message (MESSAGE ** msg)
1382 if ((*msg)->magic == M_MH || (*msg)->magic == M_MAILDIR
1384 || (*msg)->magic == M_IMAP
1387 || (*msg)->magic == M_POP
1390 || (*msg)->magic == M_NNTP
1393 r = safe_fclose (&(*msg)->fp);
1399 dprint (1, (debugfile, "mx_close_message (): unlinking %s\n",
1401 unlink ((*msg)->path);
1402 FREE (&(*msg)->path);
1409 void mx_alloc_memory (CONTEXT * ctx)
1412 size_t s = MAX (sizeof (HEADER *), sizeof (int));
1414 if ((ctx->hdrmax + 25) * s < ctx->hdrmax * s) {
1415 mutt_error _("Integer overflow -- can't allocate memory.");
1422 safe_realloc (&ctx->hdrs, sizeof (HEADER *) * (ctx->hdrmax += 25));
1423 safe_realloc (&ctx->v2r, sizeof (int) * ctx->hdrmax);
1426 ctx->hdrs = safe_calloc ((ctx->hdrmax += 25), sizeof (HEADER *));
1427 ctx->v2r = safe_calloc (ctx->hdrmax, sizeof (int));
1429 for (i = ctx->msgcount; i < ctx->hdrmax; i++) {
1430 ctx->hdrs[i] = NULL;
1435 /* this routine is called to update the counts in the context structure for
1436 * the last message header parsed.
1438 void mx_update_context (CONTEXT * ctx, int new_messages)
1443 for (msgno = ctx->msgcount - new_messages; msgno < ctx->msgcount; msgno++) {
1444 h = ctx->hdrs[msgno];
1447 /* NOTE: this _must_ be done before the check for mailcap! */
1448 h->security = crypt_query (h->content);
1451 if (!ctx->pattern) {
1452 ctx->v2r[ctx->vcount] = msgno;
1453 h->virtual = ctx->vcount++;
1459 if (h->env->supersedes) {
1463 ctx->id_hash = mutt_make_id_hash (ctx);
1465 h2 = hash_find (ctx->id_hash, h->env->supersedes);
1467 /* FREE (&h->env->supersedes); should I ? */
1470 if (!ctx->counting && option (OPTSCORE))
1471 mutt_score_message (ctx, h2, 1);
1475 /* add this message to the hash tables */
1476 if (ctx->id_hash && h->env->message_id)
1477 hash_insert (ctx->id_hash, h->env->message_id, h, 0);
1478 if (!ctx->counting) {
1479 if (ctx->subj_hash && h->env->real_subj)
1480 hash_insert (ctx->subj_hash, h->env->real_subj, h, 1);
1482 if (option (OPTSCORE))
1483 mutt_score_message (ctx, h, 0);
1502 * 1 if the specified mailbox contains 0 messages.
1503 * 0 if the mailbox contains messages
1506 int mx_check_empty (const char *path)
1509 if ((i = mx_get_idx (path)) >= 0 && MX_COMMAND(i,mx_check_empty))
1510 return (MX_COMMAND(i,mx_check_empty)(path));
1515 int mx_acl_check (CONTEXT* ctx, int flag) {
1516 if (!ctx || !MX_IDX(ctx->magic-1))
1518 /* if no acl_check defined for module, assume permission is granted */
1519 if (!MX_COMMAND(ctx->magic-1,mx_acl_check))
1521 return (MX_COMMAND(ctx->magic-1,mx_acl_check)(ctx,flag));
1524 void mx_init (void) {
1528 list_push_back (&MailboxFormats, (void*) mbox_reg_mx ());
1529 list_push_back (&MailboxFormats, (void*) mmdf_reg_mx ());
1530 list_push_back (&MailboxFormats, (void*) mh_reg_mx ());
1531 list_push_back (&MailboxFormats, (void*) maildir_reg_mx ());
1533 list_push_back (&MailboxFormats, (void*) imap_reg_mx ());
1536 list_push_back (&MailboxFormats, (void*) pop_reg_mx ());
1539 list_push_back (&MailboxFormats, (void*) nntp_reg_mx ());
1541 #ifdef USE_COMPRESSED
1542 list_push_back (&MailboxFormats, (void*) compress_reg_mx ());
1545 /* check module registration for completeness with debug versions */
1546 #define EXITWITHERR(m) do { fprintf(stderr, "error: incomplete mx module: %s is missing for type %i\n",m,i);exit(1); } while (0)
1547 for (i = 0; i < MailboxFormats->length; i++) {
1548 if (MX_COMMAND(i,type) < 1) EXITWITHERR("type");
1549 if (!MX_COMMAND(i,mx_is_magic)) EXITWITHERR("mx_is_magic");
1550 if (!MX_COMMAND(i,mx_open_mailbox)) EXITWITHERR("mx_open_mailbox");
1551 /* if (!MX_COMMAND(i,mx_sync_mailbox)) EXITWITHERR("mx_sync_mailbox");*/