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",
422 * set DefaultMagic to the given value
424 int mx_set_magic (const char *s)
426 if (ascii_strcasecmp (s, "mbox") == 0)
427 DefaultMagic = M_MBOX;
428 else if (ascii_strcasecmp (s, "mmdf") == 0)
429 DefaultMagic = M_MMDF;
430 else if (ascii_strcasecmp (s, "mh") == 0)
432 else if (ascii_strcasecmp (s, "maildir") == 0)
433 DefaultMagic = M_MAILDIR;
440 /* mx_access: Wrapper for access, checks permissions on a given mailbox.
441 * We may be interested in using ACL-style flags at some point, currently
442 * we use the normal access() flags. */
443 int mx_access (const char* path, int flags)
446 if (mx_is_imap (path))
447 return imap_access (path, flags);
450 return access (path, flags);
453 static int mx_open_mailbox_append (CONTEXT *ctx, int flags)
461 if(mx_is_imap(ctx->path))
462 return imap_open_mailbox_append (ctx);
466 if(stat(ctx->path, &sb) == 0)
468 ctx->magic = mx_get_magic (ctx->path);
473 mutt_error (_("%s is not a mailbox."), ctx->path);
479 else if (errno == ENOENT)
481 ctx->magic = DefaultMagic;
483 if (ctx->magic == M_MH || ctx->magic == M_MAILDIR)
485 char tmp[_POSIX_PATH_MAX];
487 if (mkdir (ctx->path, S_IRWXU))
489 mutt_perror (ctx->path);
493 if (ctx->magic == M_MAILDIR)
495 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
496 if (mkdir (tmp, S_IRWXU))
503 snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
504 if (mkdir (tmp, S_IRWXU))
507 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
512 snprintf (tmp, sizeof (tmp), "%s/tmp", ctx->path);
513 if (mkdir (tmp, S_IRWXU))
516 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
518 snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
528 snprintf (tmp, sizeof (tmp), "%s/.mh_sequences", ctx->path);
529 if ((i = creat (tmp, S_IRWXU)) == -1)
541 mutt_perror (ctx->path);
549 if ((ctx->fp = safe_fopen (ctx->path, flags & M_NEWFOLDER ? "w" : "a")) == NULL ||
550 mbox_lock_mailbox (ctx, 1, 1) != 0)
553 mutt_perror (ctx->path);
556 mutt_error (_("Couldn't lock %s\n"), ctx->path);
557 safe_fclose (&ctx->fp);
561 fseek (ctx->fp, 0, 2);
577 * open a mailbox and parse it
580 * flags M_NOSORT do not sort mailbox
581 * M_APPEND open mailbox for appending
582 * M_READONLY open mailbox in read-only mode
583 * M_QUIET only print error messages
584 * ctx if non-null, context struct to use
586 CONTEXT *mx_open_mailbox (const char *path, int flags, CONTEXT *pctx)
592 ctx = safe_malloc (sizeof (CONTEXT));
593 memset (ctx, 0, sizeof (CONTEXT));
594 ctx->path = safe_strdup (path);
596 ctx->msgnotreadyet = -1;
599 for (rc=0; rc < RIGHTSMAX; rc++)
600 mutt_bit_set(ctx->rights,rc);
604 if (flags & M_READONLY)
607 if (flags & (M_APPEND|M_NEWFOLDER))
609 if (mx_open_mailbox_append (ctx, flags) != 0)
611 mx_fastclose_mailbox (ctx);
619 ctx->magic = mx_get_magic (path);
622 mutt_error (_("%s is not a mailbox."), path);
629 mx_fastclose_mailbox (ctx);
635 /* if the user has a `push' command in their .muttrc, or in a folder-hook,
636 * it will cause the progress messages not to be displayed because
637 * mutt_refresh() will think we are in the middle of a macro. so set a
638 * flag to indicate that we should really refresh the screen.
640 set_option (OPTFORCEREFRESH);
643 mutt_message (_("Reading %s..."), ctx->path);
648 rc = mh_read_dir (ctx, NULL);
652 rc = maildir_read_dir (ctx);
657 rc = mbox_open_mailbox (ctx);
662 rc = imap_open_mailbox (ctx);
664 #endif /* USE_IMAP */
668 rc = pop_open_mailbox (ctx);
679 if ((flags & M_NOSORT) == 0)
681 /* avoid unnecessary work since the mailbox is completely unthreaded
683 unset_option (OPTSORTSUBTHREADS);
684 unset_option (OPTNEEDRESCORE);
685 mutt_sort_headers (ctx, 1);
692 mx_fastclose_mailbox (ctx);
697 unset_option (OPTFORCEREFRESH);
701 /* free up memory associated with the mailbox context */
702 void mx_fastclose_mailbox (CONTEXT *ctx)
713 hash_destroy (&ctx->subj_hash, NULL);
715 hash_destroy (&ctx->id_hash, NULL);
716 mutt_clear_threads (ctx);
717 for (i = 0; i < ctx->msgcount; i++)
718 mutt_free_header (&ctx->hdrs[i]);
722 FREE (&ctx->pattern);
723 if (ctx->limit_pattern)
724 mutt_pattern_free (&ctx->limit_pattern);
725 safe_fclose (&ctx->fp);
726 memset (ctx, 0, sizeof (CONTEXT));
729 /* save changes to disk */
730 static int sync_mailbox (CONTEXT *ctx, int *index_hint)
736 mutt_message (_("Writing %s..."), ctx->path);
742 rc = mbox_sync_mailbox (ctx, index_hint);
743 if (option(OPTCHECKMBOXSIZE))
744 tmp = mutt_find_mailbox (ctx->path);
749 rc = mh_sync_mailbox (ctx, index_hint);
754 /* extra argument means EXPUNGE */
755 rc = imap_sync_mailbox (ctx, 1, index_hint);
757 #endif /* USE_IMAP */
761 rc = pop_sync_mailbox (ctx, index_hint);
767 if (!ctx->quiet && !ctx->shutup && rc == -1)
768 mutt_error ( _("Could not synchronize mailbox %s!"), ctx->path);
771 if (tmp && tmp->new == 0)
772 mutt_update_mailbox (tmp);
776 /* save changes and close mailbox */
777 int mx_close_mailbox (CONTEXT *ctx, int *index_hint)
779 int i, move_messages = 0, purge = 1, read_msgs = 0;
783 char mbox[_POSIX_PATH_MAX];
784 char buf[SHORT_STRING];
790 if (ctx->readonly || ctx->dontwrite)
792 /* mailbox is readonly or we don't want to write */
793 mx_fastclose_mailbox (ctx);
799 /* mailbox was opened in write-mode */
800 if (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
801 mbox_close_mailbox (ctx);
803 mx_fastclose_mailbox (ctx);
807 for (i = 0; i < ctx->msgcount; i++)
809 if (!ctx->hdrs[i]->deleted && ctx->hdrs[i]->read
810 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
814 if (read_msgs && quadoption (OPT_MOVE) != M_NO)
818 if ((p = mutt_find_hook (M_MBOXHOOK, ctx->path)))
821 strfcpy (mbox, p, sizeof (mbox));
825 strfcpy (mbox, NONULL(Inbox), sizeof (mbox));
826 isSpool = mutt_is_spool (ctx->path) && !mutt_is_spool (mbox);
829 if (isSpool && *mbox)
831 mutt_expand_path (mbox, sizeof (mbox));
832 snprintf (buf, sizeof (buf), _("Move read messages to %s?"), mbox);
833 if ((move_messages = query_quadoption (OPT_MOVE, buf)) == -1)
842 * There is no point in asking whether or not to purge if we are
843 * just marking messages as "trash".
845 if (ctx->deleted && !(ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))
847 snprintf (buf, sizeof (buf), ctx->deleted == 1
848 ? _("Purge %d deleted message?") : _("Purge %d deleted messages?"),
850 if ((purge = query_quadoption (OPT_DELETE, buf)) < 0)
857 if (option (OPTMARKOLD))
859 for (i = 0; i < ctx->msgcount; i++)
861 if (!ctx->hdrs[i]->deleted && !ctx->hdrs[i]->old && !ctx->hdrs[i]->read)
862 mutt_set_flag (ctx, ctx->hdrs[i], M_OLD, 1);
869 mutt_message (_("Moving read messages to %s..."), mbox);
872 /* try to use server-side copy first */
875 if (ctx->magic == M_IMAP && mx_is_imap (mbox))
877 /* tag messages for moving, and clear old tags, if any */
878 for (i = 0; i < ctx->msgcount; i++)
879 if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted
880 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
881 ctx->hdrs[i]->tagged = 1;
883 ctx->hdrs[i]->tagged = 0;
885 i = imap_copy_messages (ctx, NULL, mbox, 1);
888 if (i == 0) /* success */
890 else if (i == -1) /* horrible error, bail */
895 else /* use regular append-copy mode */
898 if (mx_open_mailbox (mbox, M_APPEND, &f) == NULL)
904 for (i = 0; i < ctx->msgcount; i++)
906 if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted
907 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
909 if (mutt_append_message (&f, ctx, ctx->hdrs[i], 0, CH_UPDATE_LEN) == 0)
911 mutt_set_flag (ctx, ctx->hdrs[i], M_DELETE, 1);
915 mx_close_mailbox (&f, NULL);
922 mx_close_mailbox (&f, NULL);
926 else if (!ctx->changed && ctx->deleted == 0)
929 mutt_message _("Mailbox is unchanged.");
930 mx_fastclose_mailbox (ctx);
935 /* allow IMAP to preserve the deleted flag across sessions */
936 if (ctx->magic == M_IMAP)
938 if ((check = imap_sync_mailbox (ctx, purge, index_hint)) != 0)
949 for (i = 0; i < ctx->msgcount; i++)
950 ctx->hdrs[i]->deleted = 0;
954 if (ctx->changed || ctx->deleted)
956 if ((check = sync_mailbox (ctx, index_hint)) != 0)
967 mutt_message (_("%d kept, %d moved, %d deleted."),
968 ctx->msgcount - ctx->deleted, read_msgs, ctx->deleted);
970 mutt_message (_("%d kept, %d deleted."),
971 ctx->msgcount - ctx->deleted, ctx->deleted);
974 if (ctx->msgcount == ctx->deleted &&
975 (ctx->magic == M_MMDF || ctx->magic == M_MBOX) &&
976 !mutt_is_spool(ctx->path) && !option (OPTSAVEEMPTY))
977 mx_unlink_empty (ctx->path);
979 mx_fastclose_mailbox (ctx);
985 /* update a Context structure's internal tables. */
987 void mx_update_tables(CONTEXT *ctx, int committing)
991 /* update memory to reflect the new state of the mailbox */
1000 #define this_body ctx->hdrs[j]->content
1001 for (i = 0, j = 0; i < ctx->msgcount; i++)
1003 if ((committing && (!ctx->hdrs[i]->deleted ||
1004 (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))) ||
1005 (!committing && ctx->hdrs[i]->active))
1009 ctx->hdrs[j] = ctx->hdrs[i];
1010 ctx->hdrs[i] = NULL;
1012 ctx->hdrs[j]->msgno = j;
1013 if (ctx->hdrs[j]->virtual != -1)
1015 ctx->v2r[ctx->vcount] = j;
1016 ctx->hdrs[j]->virtual = ctx->vcount++;
1017 ctx->vsize += this_body->length + this_body->offset -
1018 this_body->hdr_offset;
1022 ctx->hdrs[j]->changed = 0;
1023 else if (ctx->hdrs[j]->changed)
1026 if (!committing || (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))
1028 if (ctx->hdrs[j]->deleted)
1032 if (ctx->hdrs[j]->tagged)
1034 if (ctx->hdrs[j]->flagged)
1036 if (!ctx->hdrs[j]->read)
1039 if (!ctx->hdrs[j]->old)
1047 if (ctx->magic == M_MH || ctx->magic == M_MAILDIR)
1048 ctx->size -= (ctx->hdrs[i]->content->length +
1049 ctx->hdrs[i]->content->offset -
1050 ctx->hdrs[i]->content->hdr_offset);
1051 /* remove message from the hash tables */
1052 if (ctx->subj_hash && ctx->hdrs[i]->env->real_subj)
1053 hash_delete (ctx->subj_hash, ctx->hdrs[i]->env->real_subj, ctx->hdrs[i], NULL);
1054 if (ctx->id_hash && ctx->hdrs[i]->env->message_id)
1055 hash_delete (ctx->id_hash, ctx->hdrs[i]->env->message_id, ctx->hdrs[i], NULL);
1056 mutt_free_header (&ctx->hdrs[i]);
1064 /* save changes to mailbox
1070 int mx_sync_mailbox (CONTEXT *ctx, int *index_hint)
1074 int msgcount, deleted;
1078 char buf[STRING], tmp[STRING];
1079 if (km_expand_key (buf, sizeof(buf),
1080 km_find_func (MENU_MAIN, OP_TOGGLE_WRITE)))
1081 snprintf (tmp, sizeof(tmp), _(" Press '%s' to toggle write"), buf);
1083 strfcpy (tmp, _("Use 'toggle-write' to re-enable write!"), sizeof(tmp));
1085 mutt_error (_("Mailbox is marked unwritable. %s"), tmp);
1088 else if (ctx->readonly)
1090 mutt_error _("Mailbox is read-only.");
1094 if (!ctx->changed && !ctx->deleted)
1097 mutt_message _("Mailbox is unchanged.");
1103 char buf[SHORT_STRING];
1105 snprintf (buf, sizeof (buf), ctx->deleted == 1
1106 ? _("Purge %d deleted message?") : _("Purge %d deleted messages?"),
1108 if ((purge = query_quadoption (OPT_DELETE, buf)) < 0)
1110 else if (purge == M_NO)
1113 return 0; /* nothing to do! */
1115 /* let IMAP servers hold on to D flags */
1116 if (ctx->magic != M_IMAP)
1119 for (i = 0 ; i < ctx->msgcount ; i++)
1120 ctx->hdrs[i]->deleted = 0;
1124 else if (ctx->last_tag && ctx->last_tag->deleted)
1125 ctx->last_tag = NULL; /* reset last tagged msg now useless */
1128 /* really only for IMAP - imap_sync_mailbox results in a call to
1129 * mx_update_tables, so ctx->deleted is 0 when it comes back */
1130 msgcount = ctx->msgcount;
1131 deleted = ctx->deleted;
1134 if (ctx->magic == M_IMAP)
1135 rc = imap_sync_mailbox (ctx, purge, index_hint);
1138 rc = sync_mailbox (ctx, index_hint);
1142 if (ctx->magic == M_IMAP && !purge)
1145 mutt_message _("Mailbox checkpointed.");
1151 mutt_message (_("%d kept, %d deleted."), msgcount - deleted,
1157 if (ctx->msgcount == ctx->deleted &&
1158 (ctx->magic == M_MBOX || ctx->magic == M_MMDF) &&
1159 !mutt_is_spool (ctx->path) && !option (OPTSAVEEMPTY))
1162 mx_fastclose_mailbox (ctx);
1166 /* if we haven't deleted any messages, we don't need to resort */
1167 /* ... except for certain folder formats which need "unsorted"
1168 * sort order in order to synchronize folders.
1170 * MH and maildir are safe. mbox-style seems to need re-sorting,
1171 * at least with the new threading code.
1173 if (purge || (ctx->magic != M_MAILDIR && ctx->magic != M_MH))
1176 /* IMAP does this automatically after handling EXPUNGE */
1177 if (ctx->magic != M_IMAP)
1180 mx_update_tables (ctx, 1);
1181 mutt_sort_headers (ctx, 1); /* rethread from scratch */
1190 /* {maildir,mh}_open_new_message are in mh.c. */
1192 static int mbox_open_new_message (MESSAGE *msg, CONTEXT *dest, HEADER *hdr)
1199 static int imap_open_new_message (MESSAGE *msg, CONTEXT *dest, HEADER *hdr)
1201 char tmp[_POSIX_PATH_MAX];
1204 if ((msg->fp = safe_fopen (tmp, "w")) == NULL)
1209 msg->path = safe_strdup(tmp);
1215 * dest destintation mailbox
1216 * hdr message being copied (required for maildir support, because
1217 * the filename depends on the message flags)
1219 MESSAGE *mx_open_new_message (CONTEXT *dest, HEADER *hdr, int flags)
1222 int (*func) (MESSAGE *, CONTEXT *, HEADER *);
1225 switch (dest->magic)
1229 func = mbox_open_new_message;
1232 func = maildir_open_new_message;
1235 func = mh_open_new_message;
1239 func = imap_open_new_message;
1243 dprint (1, (debugfile, "mx_open_new_message(): function unimplemented for mailbox type %d.\n",
1248 msg = safe_calloc (1, sizeof (MESSAGE));
1249 msg->magic = dest->magic;
1254 msg->flags.flagged = hdr->flagged;
1255 msg->flags.replied = hdr->replied;
1256 msg->flags.read = hdr->read;
1257 msg->received = hdr->received;
1260 if(msg->received == 0)
1261 time(&msg->received);
1263 if (func (msg, dest, hdr) == 0)
1265 if (dest->magic == M_MMDF)
1266 fputs (MMDF_SEP, msg->fp);
1268 if ((msg->magic == M_MBOX || msg->magic == M_MMDF) &&
1273 if (hdr->env->return_path)
1274 p = hdr->env->return_path;
1275 else if (hdr->env->sender)
1276 p = hdr->env->sender;
1281 fprintf (msg->fp, "From %s %s", p ? p->mailbox : NONULL(Username), ctime (&msg->received));
1290 /* check for new mail */
1291 int mx_check_mailbox (CONTEXT *ctx, int *index_hint, int lock)
1297 if (ctx->locked) lock = 0;
1306 mutt_block_signals ();
1307 if (mbox_lock_mailbox (ctx, 0, 0) == -1)
1309 mutt_unblock_signals ();
1314 rc = mbox_check_mailbox (ctx, index_hint);
1318 mutt_unblock_signals ();
1319 mbox_unlock_mailbox (ctx);
1326 return (mh_check_mailbox (ctx, index_hint));
1328 return (maildir_check_mailbox (ctx, index_hint));
1332 return (imap_check_mailbox (ctx, index_hint, 0));
1333 #endif /* USE_IMAP */
1337 return (pop_check_mailbox (ctx, index_hint));
1338 #endif /* USE_POP */
1342 dprint (1, (debugfile, "mx_check_mailbox: null or invalid context.\n"));
1346 /* return a stream pointer for a message */
1347 MESSAGE *mx_open_message (CONTEXT *ctx, int msgno)
1351 msg = safe_calloc (1, sizeof (MESSAGE));
1352 switch (msg->magic = ctx->magic)
1362 HEADER *cur = ctx->hdrs[msgno];
1363 char path[_POSIX_PATH_MAX];
1365 snprintf (path, sizeof (path), "%s/%s", ctx->path, cur->path);
1367 if ((msg->fp = fopen (path, "r")) == NULL && errno == ENOENT &&
1368 ctx->magic == M_MAILDIR)
1369 msg->fp = maildir_open_find_message (ctx->path, cur->path);
1371 if (msg->fp == NULL)
1374 dprint (1, (debugfile, "mx_open_message: fopen: %s: %s (errno %d).\n",
1375 path, strerror (errno), errno));
1384 if (imap_fetch_message (msg, ctx, msgno) != 0)
1388 #endif /* USE_IMAP */
1393 if (pop_fetch_message (msg, ctx, msgno) != 0)
1397 #endif /* USE_POP */
1400 dprint (1, (debugfile, "mx_open_message(): function not implemented for mailbox type %d.\n", ctx->magic));
1407 /* commit a message to a folder */
1409 int mx_commit_message (MESSAGE *msg, CONTEXT *ctx)
1413 if (!(msg->write && ctx->append))
1415 dprint (1, (debugfile, "mx_commit_message(): msg->write = %d, ctx->append = %d\n",
1416 msg->write, ctx->append));
1424 if (fputs (MMDF_SEP, msg->fp) == EOF)
1431 if (fputc ('\n', msg->fp) == EOF)
1439 if ((r = safe_fclose (&msg->fp)) == 0)
1440 r = imap_append_message (ctx, msg);
1447 r = maildir_commit_message (ctx, msg, NULL);
1453 r = mh_commit_message (ctx, msg, NULL);
1458 if (r == 0 && (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
1459 && (fflush (msg->fp) == EOF || fsync (fileno (msg->fp)) == -1))
1461 mutt_perror _("Can't write message");
1468 /* close a pointer to a message */
1469 int mx_close_message (MESSAGE **msg)
1473 if ((*msg)->magic == M_MH || (*msg)->magic == M_MAILDIR
1475 || (*msg)->magic == M_IMAP
1478 || (*msg)->magic == M_POP
1482 r = safe_fclose (&(*msg)->fp);
1489 dprint (1, (debugfile, "mx_close_message (): unlinking %s\n",
1491 unlink ((*msg)->path);
1492 FREE (&(*msg)->path);
1495 FREE (msg); /* __FREE_CHECKED__ */
1499 void mx_alloc_memory (CONTEXT *ctx)
1502 size_t s = MAX (sizeof (HEADER *), sizeof (int));
1504 if ((ctx->hdrmax + 25) * s < ctx->hdrmax * s)
1506 mutt_error _("Integer overflow -- can't allocate memory.");
1513 safe_realloc (&ctx->hdrs, sizeof (HEADER *) * (ctx->hdrmax += 25));
1514 safe_realloc (&ctx->v2r, sizeof (int) * ctx->hdrmax);
1518 ctx->hdrs = safe_calloc ((ctx->hdrmax += 25), sizeof (HEADER *));
1519 ctx->v2r = safe_calloc (ctx->hdrmax, sizeof (int));
1521 for (i = ctx->msgcount ; i < ctx->hdrmax ; i++)
1523 ctx->hdrs[i] = NULL;
1528 /* this routine is called to update the counts in the context structure for
1529 * the last message header parsed.
1531 void mx_update_context (CONTEXT *ctx, int new_messages)
1536 for (msgno = ctx->msgcount - new_messages; msgno < ctx->msgcount; msgno++)
1538 h = ctx->hdrs[msgno];
1542 /* NOTE: this _must_ be done before the check for mailcap! */
1543 h->security = crypt_query (h->content);
1548 ctx->v2r[ctx->vcount] = msgno;
1549 h->virtual = ctx->vcount++;
1555 if (h->env->supersedes)
1560 ctx->id_hash = mutt_make_id_hash (ctx);
1562 h2 = hash_find (ctx->id_hash, h->env->supersedes);
1564 /* FREE (&h->env->supersedes); should I ? */
1568 if (option (OPTSCORE))
1569 mutt_score_message (ctx, h2, 1);
1573 /* add this message to the hash tables */
1574 if (ctx->id_hash && h->env->message_id)
1575 hash_insert (ctx->id_hash, h->env->message_id, h, 0);
1576 if (ctx->subj_hash && h->env->real_subj)
1577 hash_insert (ctx->subj_hash, h->env->real_subj, h, 1);
1579 if (option (OPTSCORE))
1580 mutt_score_message (ctx, h, 0);
1599 * 1 if the specified mailbox contains 0 messages.
1600 * 0 if the mailbox contains messages
1603 int mx_check_empty (const char *path)
1605 switch (mx_get_magic (path))
1609 return mbox_check_empty (path);
1611 return mh_check_empty (path);
1613 return maildir_check_empty (path);
1621 /* vim: set sw=2: */