2 * Copyright (C) 1996-2002 Michael R. Elkins <me@mutt.org>
3 * Copyright (C) 1999-2003 Thomas Roessler <roessler@does-not-exist.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
47 #include "mutt_crypt.h"
61 #define mutt_is_spool(s) (mutt_strcmp (Spoolfile, s) == 0)
66 * retry - should retry if unable to lock?
71 static int invoke_dotlock (const char *path, int dummy, int flags, int retry)
73 char cmd[LONG_STRING + _POSIX_PATH_MAX];
74 char f[SHORT_STRING + _POSIX_PATH_MAX];
77 if (flags & DL_FL_RETRY)
78 snprintf (r, sizeof (r), "-r %d ", retry ? MAXLOCKATTEMPT : 0);
80 mutt_quote_filename (f, sizeof (f), path);
82 snprintf (cmd, sizeof (cmd),
85 flags & DL_FL_TRY ? "-t " : "",
86 flags & DL_FL_UNLOCK ? "-u " : "",
87 flags & DL_FL_USEPRIV ? "-p " : "",
88 flags & DL_FL_FORCE ? "-f " : "",
89 flags & DL_FL_UNLINK ? "-d " : "",
90 flags & DL_FL_RETRY ? r : "",
93 return mutt_system (cmd);
98 #define invoke_dotlock dotlock_invoke
102 static int dotlock_file (const char *path, int fd, int retry)
105 int flags = DL_FL_USEPRIV | DL_FL_RETRY;
107 if (retry) retry = 1;
110 if ((r = invoke_dotlock(path, fd, flags, retry)) == DL_EX_EXIST)
112 if (!option (OPTNOCURSES))
114 char msg[LONG_STRING];
116 snprintf(msg, sizeof(msg), _("Lock count exceeded, remove lock for %s?"),
118 if(retry && mutt_yesorno(msg, M_YES) == M_YES)
120 flags |= DL_FL_FORCE;
128 mutt_error ( _("Can't dotlock %s.\n"), path);
131 return (r == DL_EX_OK ? 0 : -1);
134 static int undotlock_file (const char *path, int fd)
136 return (invoke_dotlock(path, fd, DL_FL_USEPRIV | DL_FL_UNLOCK, 0) == DL_EX_OK ?
140 #endif /* USE_DOTLOCK */
143 * excl if excl != 0, request an exclusive lock
144 * dot if dot != 0, try to dotlock the file
145 * timeout should retry locking?
147 int mx_lock_file (const char *path, int fd, int excl, int dot, int timeout)
149 #if defined (USE_FCNTL) || defined (USE_FLOCK)
152 struct stat sb = { 0 }, prev_sb = { 0 }; /* silence gcc warnings */
159 memset (&lck, 0, sizeof (struct flock));
160 lck.l_type = excl ? F_WRLCK : F_RDLCK;
161 lck.l_whence = SEEK_SET;
165 while (fcntl (fd, F_SETLK, &lck) == -1)
167 dprint(1,(debugfile, "mx_lock_file(): fcntl errno %d.\n", errno));
168 if (errno != EAGAIN && errno != EACCES)
170 mutt_perror ("fcntl");
174 if (fstat (fd, &sb) != 0)
180 /* only unlock file if it is unchanged */
181 if (prev_sb.st_size == sb.st_size && ++count >= (timeout?MAXLOCKATTEMPT:0))
184 mutt_error _("Timeout exceeded while attempting fcntl lock!");
190 mutt_message (_("Waiting for fcntl lock... %d"), ++attempt);
193 #endif /* USE_FCNTL */
198 while (flock (fd, (excl ? LOCK_EX : LOCK_SH) | LOCK_NB) == -1)
200 if (errno != EWOULDBLOCK)
202 mutt_perror ("flock");
207 if (fstat(fd, &sb) != 0)
213 /* only unlock file if it is unchanged */
214 if (prev_sb.st_size == sb.st_size && ++count >= (timeout?MAXLOCKATTEMPT:0))
217 mutt_error _("Timeout exceeded while attempting flock lock!");
224 mutt_message (_("Waiting for flock attempt... %d"), ++attempt);
227 #endif /* USE_FLOCK */
231 r = dotlock_file (path, fd, timeout);
232 #endif /* USE_DOTLOCK */
236 /* release any other locks obtained in this routine */
239 lck.l_type = F_UNLCK;
240 fcntl (fd, F_SETLK, &lck);
241 #endif /* USE_FCNTL */
245 #endif /* USE_FLOCK */
251 int mx_unlock_file (const char *path, int fd, int dot)
254 struct flock unlockit = { F_UNLCK, 0, 0, 0, 0 };
256 memset (&unlockit, 0, sizeof (struct flock));
257 unlockit.l_type = F_UNLCK;
258 unlockit.l_whence = SEEK_SET;
259 fcntl (fd, F_SETLK, &unlockit);
268 undotlock_file (path, fd);
274 static void mx_unlink_empty (const char *path)
281 if ((fd = open (path, O_RDWR)) == -1)
284 if (mx_lock_file (path, fd, 1, 0, 1) == -1)
291 invoke_dotlock (path, fd, DL_FL_UNLINK, 1);
293 if (fstat (fd, &sb) == 0 && sb.st_size == 0)
297 mx_unlock_file (path, fd, 0);
301 /* try to figure out what type of mailbox ``path'' is
311 int mx_is_imap(const char *p)
321 scheme = url_check_scheme (p);
322 if (scheme == U_IMAP || scheme == U_IMAPS)
331 int mx_is_pop (const char *p)
338 scheme = url_check_scheme (p);
339 if (scheme == U_POP || scheme == U_POPS)
346 int mx_get_magic (const char *path)
350 char tmp[_POSIX_PATH_MAX];
356 #endif /* USE_IMAP */
359 if (mx_is_pop (path))
363 if (stat (path, &st) == -1)
365 dprint (1, (debugfile, "mx_get_magic(): unable to stat %s: %s (errno %d).\n",
366 path, strerror (errno), errno));
370 if (S_ISDIR (st.st_mode))
372 /* check for maildir-style mailbox */
373 if (mx_is_maildir (path))
376 /* check for mh-style mailbox */
380 else if (st.st_size == 0)
382 /* hard to tell what zero-length files are, so assume the default magic */
383 if (DefaultMagic == M_MBOX || DefaultMagic == M_MMDF)
384 return (DefaultMagic);
388 else if ((f = fopen (path, "r")) != NULL)
390 struct utimbuf times;
392 fgets (tmp, sizeof (tmp), f);
393 if (mutt_strncmp ("From ", tmp, 5) == 0)
395 else if (mutt_strcmp (MMDF_SEP, tmp) == 0)
399 if (!option(OPTCHECKMBOXSIZE))
401 /* need to restore the times here, the file was not really accessed,
402 * only the type was accessed. This is important, because detection
403 * of "new mail" depends on those times set correctly.
405 times.actime = st.st_atime;
406 times.modtime = st.st_mtime;
407 utime (path, ×);
412 dprint (1, (debugfile, "mx_get_magic(): unable to open file %s for reading.\n",
421 * set DefaultMagic to the given value
423 int mx_set_magic (const char *s)
425 if (ascii_strcasecmp (s, "mbox") == 0)
426 DefaultMagic = M_MBOX;
427 else if (ascii_strcasecmp (s, "mmdf") == 0)
428 DefaultMagic = M_MMDF;
429 else if (ascii_strcasecmp (s, "mh") == 0)
431 else if (ascii_strcasecmp (s, "maildir") == 0)
432 DefaultMagic = M_MAILDIR;
439 /* mx_access: Wrapper for access, checks permissions on a given mailbox.
440 * We may be interested in using ACL-style flags at some point, currently
441 * we use the normal access() flags. */
442 int mx_access (const char* path, int flags)
445 if (mx_is_imap (path))
446 return imap_access (path, flags);
449 return access (path, flags);
452 static int mx_open_mailbox_append (CONTEXT *ctx, int flags)
460 if(mx_is_imap(ctx->path))
461 return imap_open_mailbox_append (ctx);
465 if(stat(ctx->path, &sb) == 0)
467 ctx->magic = mx_get_magic (ctx->path);
472 mutt_error (_("%s is not a mailbox."), ctx->path);
478 else if (errno == ENOENT)
480 ctx->magic = DefaultMagic;
482 if (ctx->magic == M_MH || ctx->magic == M_MAILDIR)
484 char tmp[_POSIX_PATH_MAX];
486 if (mkdir (ctx->path, S_IRWXU))
488 mutt_perror (ctx->path);
492 if (ctx->magic == M_MAILDIR)
494 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
495 if (mkdir (tmp, S_IRWXU))
502 snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
503 if (mkdir (tmp, S_IRWXU))
506 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
511 snprintf (tmp, sizeof (tmp), "%s/tmp", ctx->path);
512 if (mkdir (tmp, S_IRWXU))
515 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
517 snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
527 snprintf (tmp, sizeof (tmp), "%s/.mh_sequences", ctx->path);
528 if ((i = creat (tmp, S_IRWXU)) == -1)
540 mutt_perror (ctx->path);
548 if ((ctx->fp = safe_fopen (ctx->path, flags & M_NEWFOLDER ? "w" : "a")) == NULL ||
549 mbox_lock_mailbox (ctx, 1, 1) != 0)
552 mutt_perror (ctx->path);
555 mutt_error (_("Couldn't lock %s\n"), ctx->path);
556 safe_fclose (&ctx->fp);
560 fseek (ctx->fp, 0, 2);
576 * open a mailbox and parse it
579 * flags M_NOSORT do not sort mailbox
580 * M_APPEND open mailbox for appending
581 * M_READONLY open mailbox in read-only mode
582 * M_QUIET only print error messages
583 * ctx if non-null, context struct to use
585 CONTEXT *mx_open_mailbox (const char *path, int flags, CONTEXT *pctx)
591 ctx = safe_malloc (sizeof (CONTEXT));
592 memset (ctx, 0, sizeof (CONTEXT));
593 ctx->path = safe_strdup (path);
595 ctx->msgnotreadyet = -1;
598 for (rc=0; rc < RIGHTSMAX; rc++)
599 mutt_bit_set(ctx->rights,rc);
603 if (flags & M_READONLY)
606 if (flags & (M_APPEND|M_NEWFOLDER))
608 if (mx_open_mailbox_append (ctx, flags) != 0)
610 mx_fastclose_mailbox (ctx);
618 ctx->magic = mx_get_magic (path);
621 mutt_error (_("%s is not a mailbox."), path);
628 mx_fastclose_mailbox (ctx);
634 /* if the user has a `push' command in their .muttrc, or in a folder-hook,
635 * it will cause the progress messages not to be displayed because
636 * mutt_refresh() will think we are in the middle of a macro. so set a
637 * flag to indicate that we should really refresh the screen.
639 set_option (OPTFORCEREFRESH);
642 mutt_message (_("Reading %s..."), ctx->path);
647 rc = mh_read_dir (ctx, NULL);
651 rc = maildir_read_dir (ctx);
656 rc = mbox_open_mailbox (ctx);
661 rc = imap_open_mailbox (ctx);
663 #endif /* USE_IMAP */
667 rc = pop_open_mailbox (ctx);
678 if ((flags & M_NOSORT) == 0)
680 /* avoid unnecessary work since the mailbox is completely unthreaded
682 unset_option (OPTSORTSUBTHREADS);
683 unset_option (OPTNEEDRESCORE);
684 mutt_sort_headers (ctx, 1);
691 mx_fastclose_mailbox (ctx);
696 unset_option (OPTFORCEREFRESH);
700 /* free up memory associated with the mailbox context */
701 void mx_fastclose_mailbox (CONTEXT *ctx)
708 /* never announce that a mailbox we've just left has new mail. #3290
709 * XXX: really belongs in mx_close_mailbox, but this is a nice hook point */
710 mutt_buffy_setnotified(ctx->path);
716 hash_destroy (&ctx->subj_hash, NULL);
718 hash_destroy (&ctx->id_hash, NULL);
719 mutt_clear_threads (ctx);
720 for (i = 0; i < ctx->msgcount; i++)
721 mutt_free_header (&ctx->hdrs[i]);
725 FREE (&ctx->pattern);
726 if (ctx->limit_pattern)
727 mutt_pattern_free (&ctx->limit_pattern);
728 safe_fclose (&ctx->fp);
729 memset (ctx, 0, sizeof (CONTEXT));
732 /* save changes to disk */
733 static int sync_mailbox (CONTEXT *ctx, int *index_hint)
739 mutt_message (_("Writing %s..."), ctx->path);
745 rc = mbox_sync_mailbox (ctx, index_hint);
746 if (option(OPTCHECKMBOXSIZE))
747 tmp = mutt_find_mailbox (ctx->path);
752 rc = mh_sync_mailbox (ctx, index_hint);
757 /* extra argument means EXPUNGE */
758 rc = imap_sync_mailbox (ctx, 1, index_hint);
760 #endif /* USE_IMAP */
764 rc = pop_sync_mailbox (ctx, index_hint);
770 if (!ctx->quiet && !ctx->shutup && rc == -1)
771 mutt_error ( _("Could not synchronize mailbox %s!"), ctx->path);
774 if (tmp && tmp->new == 0)
775 mutt_update_mailbox (tmp);
779 /* save changes and close mailbox */
780 int mx_close_mailbox (CONTEXT *ctx, int *index_hint)
782 int i, move_messages = 0, purge = 1, read_msgs = 0;
786 char mbox[_POSIX_PATH_MAX];
787 char buf[SHORT_STRING];
793 if (ctx->readonly || ctx->dontwrite)
795 /* mailbox is readonly or we don't want to write */
796 mx_fastclose_mailbox (ctx);
802 /* mailbox was opened in write-mode */
803 if (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
804 mbox_close_mailbox (ctx);
806 mx_fastclose_mailbox (ctx);
810 for (i = 0; i < ctx->msgcount; i++)
812 if (!ctx->hdrs[i]->deleted && ctx->hdrs[i]->read
813 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
817 if (read_msgs && quadoption (OPT_MOVE) != M_NO)
821 if ((p = mutt_find_hook (M_MBOXHOOK, ctx->path)))
824 strfcpy (mbox, p, sizeof (mbox));
828 strfcpy (mbox, NONULL(Inbox), sizeof (mbox));
829 isSpool = mutt_is_spool (ctx->path) && !mutt_is_spool (mbox);
832 if (isSpool && *mbox)
834 mutt_expand_path (mbox, sizeof (mbox));
835 snprintf (buf, sizeof (buf), _("Move read messages to %s?"), mbox);
836 if ((move_messages = query_quadoption (OPT_MOVE, buf)) == -1)
845 * There is no point in asking whether or not to purge if we are
846 * just marking messages as "trash".
848 if (ctx->deleted && !(ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))
850 snprintf (buf, sizeof (buf), ctx->deleted == 1
851 ? _("Purge %d deleted message?") : _("Purge %d deleted messages?"),
853 if ((purge = query_quadoption (OPT_DELETE, buf)) < 0)
860 if (option (OPTMARKOLD))
862 for (i = 0; i < ctx->msgcount; i++)
864 if (!ctx->hdrs[i]->deleted && !ctx->hdrs[i]->old && !ctx->hdrs[i]->read)
865 mutt_set_flag (ctx, ctx->hdrs[i], M_OLD, 1);
872 mutt_message (_("Moving read messages to %s..."), mbox);
875 /* try to use server-side copy first */
878 if (ctx->magic == M_IMAP && mx_is_imap (mbox))
880 /* tag messages for moving, and clear old tags, if any */
881 for (i = 0; i < ctx->msgcount; i++)
882 if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted
883 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
884 ctx->hdrs[i]->tagged = 1;
886 ctx->hdrs[i]->tagged = 0;
888 i = imap_copy_messages (ctx, NULL, mbox, 1);
891 if (i == 0) /* success */
893 else if (i == -1) /* horrible error, bail */
898 else /* use regular append-copy mode */
901 if (mx_open_mailbox (mbox, M_APPEND, &f) == NULL)
907 for (i = 0; i < ctx->msgcount; i++)
909 if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted
910 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
912 if (mutt_append_message (&f, ctx, ctx->hdrs[i], 0, CH_UPDATE_LEN) == 0)
914 mutt_set_flag (ctx, ctx->hdrs[i], M_DELETE, 1);
918 mx_close_mailbox (&f, NULL);
925 mx_close_mailbox (&f, NULL);
929 else if (!ctx->changed && ctx->deleted == 0)
932 mutt_message _("Mailbox is unchanged.");
933 if (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
934 mbox_reset_atime (ctx, NULL);
935 mx_fastclose_mailbox (ctx);
940 /* allow IMAP to preserve the deleted flag across sessions */
941 if (ctx->magic == M_IMAP)
943 if ((check = imap_sync_mailbox (ctx, purge, index_hint)) != 0)
954 for (i = 0; i < ctx->msgcount; i++)
955 ctx->hdrs[i]->deleted = 0;
959 if (ctx->changed || ctx->deleted)
961 if ((check = sync_mailbox (ctx, index_hint)) != 0)
972 mutt_message (_("%d kept, %d moved, %d deleted."),
973 ctx->msgcount - ctx->deleted, read_msgs, ctx->deleted);
975 mutt_message (_("%d kept, %d deleted."),
976 ctx->msgcount - ctx->deleted, ctx->deleted);
979 if (ctx->msgcount == ctx->deleted &&
980 (ctx->magic == M_MMDF || ctx->magic == M_MBOX) &&
981 !mutt_is_spool(ctx->path) && !option (OPTSAVEEMPTY))
982 mx_unlink_empty (ctx->path);
984 mx_fastclose_mailbox (ctx);
990 /* update a Context structure's internal tables. */
992 void mx_update_tables(CONTEXT *ctx, int committing)
996 /* update memory to reflect the new state of the mailbox */
1005 #define this_body ctx->hdrs[j]->content
1006 for (i = 0, j = 0; i < ctx->msgcount; i++)
1008 if ((committing && (!ctx->hdrs[i]->deleted ||
1009 (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))) ||
1010 (!committing && ctx->hdrs[i]->active))
1014 ctx->hdrs[j] = ctx->hdrs[i];
1015 ctx->hdrs[i] = NULL;
1017 ctx->hdrs[j]->msgno = j;
1018 if (ctx->hdrs[j]->virtual != -1)
1020 ctx->v2r[ctx->vcount] = j;
1021 ctx->hdrs[j]->virtual = ctx->vcount++;
1022 ctx->vsize += this_body->length + this_body->offset -
1023 this_body->hdr_offset;
1027 ctx->hdrs[j]->changed = 0;
1028 else if (ctx->hdrs[j]->changed)
1031 if (!committing || (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))
1033 if (ctx->hdrs[j]->deleted)
1037 if (ctx->hdrs[j]->tagged)
1039 if (ctx->hdrs[j]->flagged)
1041 if (!ctx->hdrs[j]->read)
1044 if (!ctx->hdrs[j]->old)
1052 if (ctx->magic == M_MH || ctx->magic == M_MAILDIR)
1053 ctx->size -= (ctx->hdrs[i]->content->length +
1054 ctx->hdrs[i]->content->offset -
1055 ctx->hdrs[i]->content->hdr_offset);
1056 /* remove message from the hash tables */
1057 if (ctx->subj_hash && ctx->hdrs[i]->env->real_subj)
1058 hash_delete (ctx->subj_hash, ctx->hdrs[i]->env->real_subj, ctx->hdrs[i], NULL);
1059 if (ctx->id_hash && ctx->hdrs[i]->env->message_id)
1060 hash_delete (ctx->id_hash, ctx->hdrs[i]->env->message_id, ctx->hdrs[i], NULL);
1061 mutt_free_header (&ctx->hdrs[i]);
1069 /* save changes to mailbox
1075 int mx_sync_mailbox (CONTEXT *ctx, int *index_hint)
1079 int msgcount, deleted;
1083 char buf[STRING], tmp[STRING];
1084 if (km_expand_key (buf, sizeof(buf),
1085 km_find_func (MENU_MAIN, OP_TOGGLE_WRITE)))
1086 snprintf (tmp, sizeof(tmp), _(" Press '%s' to toggle write"), buf);
1088 strfcpy (tmp, _("Use 'toggle-write' to re-enable write!"), sizeof(tmp));
1090 mutt_error (_("Mailbox is marked unwritable. %s"), tmp);
1093 else if (ctx->readonly)
1095 mutt_error _("Mailbox is read-only.");
1099 if (!ctx->changed && !ctx->deleted)
1102 mutt_message _("Mailbox is unchanged.");
1108 char buf[SHORT_STRING];
1110 snprintf (buf, sizeof (buf), ctx->deleted == 1
1111 ? _("Purge %d deleted message?") : _("Purge %d deleted messages?"),
1113 if ((purge = query_quadoption (OPT_DELETE, buf)) < 0)
1115 else if (purge == M_NO)
1118 return 0; /* nothing to do! */
1119 /* let IMAP servers hold on to D flags */
1120 if (ctx->magic != M_IMAP)
1122 for (i = 0 ; i < ctx->msgcount ; i++)
1123 ctx->hdrs[i]->deleted = 0;
1127 else if (ctx->last_tag && ctx->last_tag->deleted)
1128 ctx->last_tag = NULL; /* reset last tagged msg now useless */
1131 /* really only for IMAP - imap_sync_mailbox results in a call to
1132 * mx_update_tables, so ctx->deleted is 0 when it comes back */
1133 msgcount = ctx->msgcount;
1134 deleted = ctx->deleted;
1137 if (ctx->magic == M_IMAP)
1138 rc = imap_sync_mailbox (ctx, purge, index_hint);
1141 rc = sync_mailbox (ctx, index_hint);
1145 if (ctx->magic == M_IMAP && !purge)
1148 mutt_message _("Mailbox checkpointed.");
1154 mutt_message (_("%d kept, %d deleted."), msgcount - deleted,
1160 if (ctx->msgcount == ctx->deleted &&
1161 (ctx->magic == M_MBOX || ctx->magic == M_MMDF) &&
1162 !mutt_is_spool (ctx->path) && !option (OPTSAVEEMPTY))
1165 mx_fastclose_mailbox (ctx);
1169 /* if we haven't deleted any messages, we don't need to resort */
1170 /* ... except for certain folder formats which need "unsorted"
1171 * sort order in order to synchronize folders.
1173 * MH and maildir are safe. mbox-style seems to need re-sorting,
1174 * at least with the new threading code.
1176 if (purge || (ctx->magic != M_MAILDIR && ctx->magic != M_MH))
1178 /* IMAP does this automatically after handling EXPUNGE */
1179 if (ctx->magic != M_IMAP)
1181 mx_update_tables (ctx, 1);
1182 mutt_sort_headers (ctx, 1); /* rethread from scratch */
1191 /* {maildir,mh}_open_new_message are in mh.c. */
1193 static int mbox_open_new_message (MESSAGE *msg, CONTEXT *dest, HEADER *hdr)
1200 static int imap_open_new_message (MESSAGE *msg, CONTEXT *dest, HEADER *hdr)
1202 char tmp[_POSIX_PATH_MAX];
1204 mutt_mktemp (tmp, sizeof (tmp));
1205 if ((msg->fp = safe_fopen (tmp, "w")) == NULL)
1210 msg->path = safe_strdup(tmp);
1216 * dest destintation mailbox
1217 * hdr message being copied (required for maildir support, because
1218 * the filename depends on the message flags)
1220 MESSAGE *mx_open_new_message (CONTEXT *dest, HEADER *hdr, int flags)
1223 int (*func) (MESSAGE *, CONTEXT *, HEADER *);
1226 switch (dest->magic)
1230 func = mbox_open_new_message;
1233 func = maildir_open_new_message;
1236 func = mh_open_new_message;
1240 func = imap_open_new_message;
1244 dprint (1, (debugfile, "mx_open_new_message(): function unimplemented for mailbox type %d.\n",
1249 msg = safe_calloc (1, sizeof (MESSAGE));
1250 msg->magic = dest->magic;
1255 msg->flags.flagged = hdr->flagged;
1256 msg->flags.replied = hdr->replied;
1257 msg->flags.read = hdr->read;
1258 msg->received = hdr->received;
1261 if(msg->received == 0)
1262 time(&msg->received);
1264 if (func (msg, dest, hdr) == 0)
1266 if (dest->magic == M_MMDF)
1267 fputs (MMDF_SEP, msg->fp);
1269 if ((msg->magic == M_MBOX || msg->magic == M_MMDF) &&
1274 if (hdr->env->return_path)
1275 p = hdr->env->return_path;
1276 else if (hdr->env->sender)
1277 p = hdr->env->sender;
1282 fprintf (msg->fp, "From %s %s", p ? p->mailbox : NONULL(Username), ctime (&msg->received));
1291 /* check for new mail */
1292 int mx_check_mailbox (CONTEXT *ctx, int *index_hint, int lock)
1298 if (ctx->locked) lock = 0;
1307 mutt_block_signals ();
1308 if (mbox_lock_mailbox (ctx, 0, 0) == -1)
1310 mutt_unblock_signals ();
1315 rc = mbox_check_mailbox (ctx, index_hint);
1319 mutt_unblock_signals ();
1320 mbox_unlock_mailbox (ctx);
1327 return (mh_check_mailbox (ctx, index_hint));
1329 return (maildir_check_mailbox (ctx, index_hint));
1333 /* caller expects that mailbox may change */
1334 imap_allow_reopen (ctx);
1335 rc = imap_check_mailbox (ctx, index_hint, 0);
1336 imap_disallow_reopen (ctx);
1338 #endif /* USE_IMAP */
1342 return (pop_check_mailbox (ctx, index_hint));
1343 #endif /* USE_POP */
1347 dprint (1, (debugfile, "mx_check_mailbox: null or invalid context.\n"));
1351 /* return a stream pointer for a message */
1352 MESSAGE *mx_open_message (CONTEXT *ctx, int msgno)
1356 msg = safe_calloc (1, sizeof (MESSAGE));
1357 switch (msg->magic = ctx->magic)
1367 HEADER *cur = ctx->hdrs[msgno];
1368 char path[_POSIX_PATH_MAX];
1370 snprintf (path, sizeof (path), "%s/%s", ctx->path, cur->path);
1372 if ((msg->fp = fopen (path, "r")) == NULL && errno == ENOENT &&
1373 ctx->magic == M_MAILDIR)
1374 msg->fp = maildir_open_find_message (ctx->path, cur->path);
1376 if (msg->fp == NULL)
1379 dprint (1, (debugfile, "mx_open_message: fopen: %s: %s (errno %d).\n",
1380 path, strerror (errno), errno));
1389 if (imap_fetch_message (msg, ctx, msgno) != 0)
1393 #endif /* USE_IMAP */
1398 if (pop_fetch_message (msg, ctx, msgno) != 0)
1402 #endif /* USE_POP */
1405 dprint (1, (debugfile, "mx_open_message(): function not implemented for mailbox type %d.\n", ctx->magic));
1412 /* commit a message to a folder */
1414 int mx_commit_message (MESSAGE *msg, CONTEXT *ctx)
1418 if (!(msg->write && ctx->append))
1420 dprint (1, (debugfile, "mx_commit_message(): msg->write = %d, ctx->append = %d\n",
1421 msg->write, ctx->append));
1429 if (fputs (MMDF_SEP, msg->fp) == EOF)
1436 if (fputc ('\n', msg->fp) == EOF)
1444 if ((r = safe_fclose (&msg->fp)) == 0)
1445 r = imap_append_message (ctx, msg);
1452 r = maildir_commit_message (ctx, msg, NULL);
1458 r = mh_commit_message (ctx, msg, NULL);
1463 if (r == 0 && (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
1464 && (fflush (msg->fp) == EOF || fsync (fileno (msg->fp)) == -1))
1466 mutt_perror _("Can't write message");
1473 /* close a pointer to a message */
1474 int mx_close_message (MESSAGE **msg)
1478 if ((*msg)->magic == M_MH || (*msg)->magic == M_MAILDIR
1479 || (*msg)->magic == M_IMAP || (*msg)->magic == M_POP)
1481 r = safe_fclose (&(*msg)->fp);
1488 dprint (1, (debugfile, "mx_close_message (): unlinking %s\n",
1490 unlink ((*msg)->path);
1491 FREE (&(*msg)->path);
1494 FREE (msg); /* __FREE_CHECKED__ */
1498 void mx_alloc_memory (CONTEXT *ctx)
1501 size_t s = MAX (sizeof (HEADER *), sizeof (int));
1503 if ((ctx->hdrmax + 25) * s < ctx->hdrmax * s)
1505 mutt_error _("Integer overflow -- can't allocate memory.");
1512 safe_realloc (&ctx->hdrs, sizeof (HEADER *) * (ctx->hdrmax += 25));
1513 safe_realloc (&ctx->v2r, sizeof (int) * ctx->hdrmax);
1517 ctx->hdrs = safe_calloc ((ctx->hdrmax += 25), sizeof (HEADER *));
1518 ctx->v2r = safe_calloc (ctx->hdrmax, sizeof (int));
1520 for (i = ctx->msgcount ; i < ctx->hdrmax ; i++)
1522 ctx->hdrs[i] = NULL;
1527 /* this routine is called to update the counts in the context structure for
1528 * the last message header parsed.
1530 void mx_update_context (CONTEXT *ctx, int new_messages)
1535 for (msgno = ctx->msgcount - new_messages; msgno < ctx->msgcount; msgno++)
1537 h = ctx->hdrs[msgno];
1541 /* NOTE: this _must_ be done before the check for mailcap! */
1542 h->security = crypt_query (h->content);
1547 ctx->v2r[ctx->vcount] = msgno;
1548 h->virtual = ctx->vcount++;
1554 if (h->env->supersedes)
1559 ctx->id_hash = mutt_make_id_hash (ctx);
1561 h2 = hash_find (ctx->id_hash, h->env->supersedes);
1563 /* FREE (&h->env->supersedes); should I ? */
1567 if (option (OPTSCORE))
1568 mutt_score_message (ctx, h2, 1);
1572 /* add this message to the hash tables */
1573 if (ctx->id_hash && h->env->message_id)
1574 hash_insert (ctx->id_hash, h->env->message_id, h, 0);
1575 if (ctx->subj_hash && h->env->real_subj)
1576 hash_insert (ctx->subj_hash, h->env->real_subj, h, 1);
1578 if (option (OPTSCORE))
1579 mutt_score_message (ctx, h, 0);
1598 * 1 if the specified mailbox contains 0 messages.
1599 * 0 if the mailbox contains messages
1602 int mx_check_empty (const char *path)
1604 switch (mx_get_magic (path))
1608 return mbox_check_empty (path);
1610 return mh_check_empty (path);
1612 return maildir_check_empty (path);
1620 /* vim: set sw=2: */