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 };
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 */
374 snprintf (tmp, sizeof (tmp), "%s/cur", path);
375 if (stat (tmp, &st) == 0 && S_ISDIR (st.st_mode))
378 /* check for mh-style mailbox */
380 snprintf (tmp, sizeof (tmp), "%s/.mh_sequences", path);
381 if (access (tmp, F_OK) == 0)
384 snprintf (tmp, sizeof (tmp), "%s/.xmhcache", path);
385 if (access (tmp, F_OK) == 0)
388 snprintf (tmp, sizeof (tmp), "%s/.mew_cache", path);
389 if (access (tmp, F_OK) == 0)
392 snprintf (tmp, sizeof (tmp), "%s/.mew-cache", path);
393 if (access (tmp, F_OK) == 0)
396 snprintf (tmp, sizeof (tmp), "%s/.sylpheed_cache", path);
397 if (access (tmp, F_OK) == 0)
401 * ok, this isn't an mh folder, but mh mode can be used to read
402 * Usenet news from the spool. ;-)
405 snprintf (tmp, sizeof (tmp), "%s/.overview", path);
406 if (access (tmp, F_OK) == 0)
410 else if (st.st_size == 0)
412 /* hard to tell what zero-length files are, so assume the default magic */
413 if (DefaultMagic == M_MBOX || DefaultMagic == M_MMDF)
414 return (DefaultMagic);
418 else if ((f = fopen (path, "r")) != NULL)
420 struct utimbuf times;
422 fgets (tmp, sizeof (tmp), f);
423 if (mutt_strncmp ("From ", tmp, 5) == 0)
425 else if (mutt_strcmp (MMDF_SEP, tmp) == 0)
429 if (!option(OPTCHECKMBOXSIZE))
431 /* need to restore the times here, the file was not really accessed,
432 * only the type was accessed. This is important, because detection
433 * of "new mail" depends on those times set correctly.
435 times.actime = st.st_atime;
436 times.modtime = st.st_mtime;
437 utime (path, ×);
442 dprint (1, (debugfile, "mx_get_magic(): unable to open file %s for reading.\n",
452 * set DefaultMagic to the given value
454 int mx_set_magic (const char *s)
456 if (ascii_strcasecmp (s, "mbox") == 0)
457 DefaultMagic = M_MBOX;
458 else if (ascii_strcasecmp (s, "mmdf") == 0)
459 DefaultMagic = M_MMDF;
460 else if (ascii_strcasecmp (s, "mh") == 0)
462 else if (ascii_strcasecmp (s, "maildir") == 0)
463 DefaultMagic = M_MAILDIR;
470 /* mx_access: Wrapper for access, checks permissions on a given mailbox.
471 * We may be interested in using ACL-style flags at some point, currently
472 * we use the normal access() flags. */
473 int mx_access (const char* path, int flags)
476 if (mx_is_imap (path))
477 return imap_access (path, flags);
480 return access (path, flags);
483 static int mx_open_mailbox_append (CONTEXT *ctx, int flags)
491 if(mx_is_imap(ctx->path))
492 return imap_open_mailbox_append (ctx);
496 if(stat(ctx->path, &sb) == 0)
498 ctx->magic = mx_get_magic (ctx->path);
503 mutt_error (_("%s is not a mailbox."), ctx->path);
509 else if (errno == ENOENT)
511 ctx->magic = DefaultMagic;
513 if (ctx->magic == M_MH || ctx->magic == M_MAILDIR)
515 char tmp[_POSIX_PATH_MAX];
517 if (mkdir (ctx->path, S_IRWXU))
519 mutt_perror (ctx->path);
523 if (ctx->magic == M_MAILDIR)
525 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
526 if (mkdir (tmp, S_IRWXU))
533 snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
534 if (mkdir (tmp, S_IRWXU))
537 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
542 snprintf (tmp, sizeof (tmp), "%s/tmp", ctx->path);
543 if (mkdir (tmp, S_IRWXU))
546 snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
548 snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
558 snprintf (tmp, sizeof (tmp), "%s/.mh_sequences", ctx->path);
559 if ((i = creat (tmp, S_IRWXU)) == -1)
571 mutt_perror (ctx->path);
579 if ((ctx->fp = safe_fopen (ctx->path, flags & M_NEWFOLDER ? "w" : "a")) == NULL ||
580 mbox_lock_mailbox (ctx, 1, 1) != 0)
583 mutt_perror (ctx->path);
586 mutt_error (_("Couldn't lock %s\n"), ctx->path);
587 safe_fclose (&ctx->fp);
591 fseek (ctx->fp, 0, 2);
607 * open a mailbox and parse it
610 * flags M_NOSORT do not sort mailbox
611 * M_APPEND open mailbox for appending
612 * M_READONLY open mailbox in read-only mode
613 * M_QUIET only print error messages
614 * ctx if non-null, context struct to use
616 CONTEXT *mx_open_mailbox (const char *path, int flags, CONTEXT *pctx)
622 ctx = safe_malloc (sizeof (CONTEXT));
623 memset (ctx, 0, sizeof (CONTEXT));
624 ctx->path = safe_strdup (path);
626 ctx->msgnotreadyet = -1;
629 for (rc=0; rc < RIGHTSMAX; rc++)
630 mutt_bit_set(ctx->rights,rc);
634 if (flags & M_READONLY)
637 if (flags & (M_APPEND|M_NEWFOLDER))
639 if (mx_open_mailbox_append (ctx, flags) != 0)
641 mx_fastclose_mailbox (ctx);
649 ctx->magic = mx_get_magic (path);
652 mutt_error (_("%s is not a mailbox."), path);
659 mx_fastclose_mailbox (ctx);
665 /* if the user has a `push' command in their .muttrc, or in a folder-hook,
666 * it will cause the progress messages not to be displayed because
667 * mutt_refresh() will think we are in the middle of a macro. so set a
668 * flag to indicate that we should really refresh the screen.
670 set_option (OPTFORCEREFRESH);
673 mutt_message (_("Reading %s..."), ctx->path);
678 rc = mh_read_dir (ctx, NULL);
682 rc = maildir_read_dir (ctx);
687 rc = mbox_open_mailbox (ctx);
692 rc = imap_open_mailbox (ctx);
694 #endif /* USE_IMAP */
698 rc = pop_open_mailbox (ctx);
709 if ((flags & M_NOSORT) == 0)
711 /* avoid unnecessary work since the mailbox is completely unthreaded
713 unset_option (OPTSORTSUBTHREADS);
714 unset_option (OPTNEEDRESCORE);
715 mutt_sort_headers (ctx, 1);
722 mx_fastclose_mailbox (ctx);
727 unset_option (OPTFORCEREFRESH);
731 /* free up memory associated with the mailbox context */
732 void mx_fastclose_mailbox (CONTEXT *ctx)
743 hash_destroy (&ctx->subj_hash, NULL);
745 hash_destroy (&ctx->id_hash, NULL);
746 mutt_clear_threads (ctx);
747 for (i = 0; i < ctx->msgcount; i++)
748 mutt_free_header (&ctx->hdrs[i]);
752 FREE (&ctx->pattern);
753 if (ctx->limit_pattern)
754 mutt_pattern_free (&ctx->limit_pattern);
755 safe_fclose (&ctx->fp);
756 memset (ctx, 0, sizeof (CONTEXT));
759 /* save changes to disk */
760 static int sync_mailbox (CONTEXT *ctx, int *index_hint)
766 mutt_message (_("Writing %s..."), ctx->path);
772 rc = mbox_sync_mailbox (ctx, index_hint);
773 if (option(OPTCHECKMBOXSIZE))
774 tmp = mutt_find_mailbox (ctx->path);
779 rc = mh_sync_mailbox (ctx, index_hint);
784 /* extra argument means EXPUNGE */
785 rc = imap_sync_mailbox (ctx, 1, index_hint);
787 #endif /* USE_IMAP */
791 rc = pop_sync_mailbox (ctx, index_hint);
797 if (!ctx->quiet && !ctx->shutup && rc == -1)
798 mutt_error ( _("Could not synchronize mailbox %s!"), ctx->path);
801 if (tmp && tmp->new == 0)
802 mutt_update_mailbox (tmp);
806 /* save changes and close mailbox */
807 int mx_close_mailbox (CONTEXT *ctx, int *index_hint)
809 int i, move_messages = 0, purge = 1, read_msgs = 0;
813 char mbox[_POSIX_PATH_MAX];
814 char buf[SHORT_STRING];
820 if (ctx->readonly || ctx->dontwrite)
822 /* mailbox is readonly or we don't want to write */
823 mx_fastclose_mailbox (ctx);
829 /* mailbox was opened in write-mode */
830 if (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
831 mbox_close_mailbox (ctx);
833 mx_fastclose_mailbox (ctx);
837 for (i = 0; i < ctx->msgcount; i++)
839 if (!ctx->hdrs[i]->deleted && ctx->hdrs[i]->read
840 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
844 if (read_msgs && quadoption (OPT_MOVE) != M_NO)
848 if ((p = mutt_find_hook (M_MBOXHOOK, ctx->path)))
851 strfcpy (mbox, p, sizeof (mbox));
855 strfcpy (mbox, NONULL(Inbox), sizeof (mbox));
856 isSpool = mutt_is_spool (ctx->path) && !mutt_is_spool (mbox);
859 if (isSpool && *mbox)
861 mutt_expand_path (mbox, sizeof (mbox));
862 snprintf (buf, sizeof (buf), _("Move read messages to %s?"), mbox);
863 if ((move_messages = query_quadoption (OPT_MOVE, buf)) == -1)
872 * There is no point in asking whether or not to purge if we are
873 * just marking messages as "trash".
875 if (ctx->deleted && !(ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))
877 snprintf (buf, sizeof (buf), ctx->deleted == 1
878 ? _("Purge %d deleted message?") : _("Purge %d deleted messages?"),
880 if ((purge = query_quadoption (OPT_DELETE, buf)) < 0)
887 if (option (OPTMARKOLD))
889 for (i = 0; i < ctx->msgcount; i++)
891 if (!ctx->hdrs[i]->deleted && !ctx->hdrs[i]->old && !ctx->hdrs[i]->read)
892 mutt_set_flag (ctx, ctx->hdrs[i], M_OLD, 1);
898 mutt_message (_("Moving read messages to %s..."), mbox);
901 /* try to use server-side copy first */
904 if (ctx->magic == M_IMAP && mx_is_imap (mbox))
906 /* tag messages for moving, and clear old tags, if any */
907 for (i = 0; i < ctx->msgcount; i++)
908 if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted
909 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
910 ctx->hdrs[i]->tagged = 1;
912 ctx->hdrs[i]->tagged = 0;
914 i = imap_copy_messages (ctx, NULL, mbox, 1);
917 if (i == 0) /* success */
919 else if (i == -1) /* horrible error, bail */
924 else /* use regular append-copy mode */
927 if (mx_open_mailbox (mbox, M_APPEND, &f) == NULL)
933 for (i = 0; i < ctx->msgcount; i++)
935 if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted
936 && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
938 if (mutt_append_message (&f, ctx, ctx->hdrs[i], 0, CH_UPDATE_LEN) == 0)
940 mutt_set_flag (ctx, ctx->hdrs[i], M_DELETE, 1);
944 mx_close_mailbox (&f, NULL);
951 mx_close_mailbox (&f, NULL);
955 else if (!ctx->changed && ctx->deleted == 0)
957 mutt_message _("Mailbox is unchanged.");
958 mx_fastclose_mailbox (ctx);
963 /* allow IMAP to preserve the deleted flag across sessions */
964 if (ctx->magic == M_IMAP)
966 if ((check = imap_sync_mailbox (ctx, purge, index_hint)) != 0)
977 for (i = 0; i < ctx->msgcount; i++)
978 ctx->hdrs[i]->deleted = 0;
982 if (ctx->changed || ctx->deleted)
984 if ((check = sync_mailbox (ctx, index_hint)) != 0)
993 mutt_message (_("%d kept, %d moved, %d deleted."),
994 ctx->msgcount - ctx->deleted, read_msgs, ctx->deleted);
996 mutt_message (_("%d kept, %d deleted."),
997 ctx->msgcount - ctx->deleted, ctx->deleted);
999 if (ctx->msgcount == ctx->deleted &&
1000 (ctx->magic == M_MMDF || ctx->magic == M_MBOX) &&
1001 !mutt_is_spool(ctx->path) && !option (OPTSAVEEMPTY))
1002 mx_unlink_empty (ctx->path);
1004 mx_fastclose_mailbox (ctx);
1010 /* update a Context structure's internal tables. */
1012 void mx_update_tables(CONTEXT *ctx, int committing)
1016 /* update memory to reflect the new state of the mailbox */
1025 #define this_body ctx->hdrs[j]->content
1026 for (i = 0, j = 0; i < ctx->msgcount; i++)
1028 if ((committing && (!ctx->hdrs[i]->deleted ||
1029 (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))) ||
1030 (!committing && ctx->hdrs[i]->active))
1034 ctx->hdrs[j] = ctx->hdrs[i];
1035 ctx->hdrs[i] = NULL;
1037 ctx->hdrs[j]->msgno = j;
1038 if (ctx->hdrs[j]->virtual != -1)
1040 ctx->v2r[ctx->vcount] = j;
1041 ctx->hdrs[j]->virtual = ctx->vcount++;
1042 ctx->vsize += this_body->length + this_body->offset -
1043 this_body->hdr_offset;
1047 ctx->hdrs[j]->changed = 0;
1048 else if (ctx->hdrs[j]->changed)
1051 if (!committing || (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))
1053 if (ctx->hdrs[j]->deleted)
1057 if (ctx->hdrs[j]->tagged)
1059 if (ctx->hdrs[j]->flagged)
1061 if (!ctx->hdrs[j]->read)
1064 if (!ctx->hdrs[j]->old)
1072 if (ctx->magic == M_MH || ctx->magic == M_MAILDIR)
1073 ctx->size -= (ctx->hdrs[i]->content->length +
1074 ctx->hdrs[i]->content->offset -
1075 ctx->hdrs[i]->content->hdr_offset);
1076 /* remove message from the hash tables */
1077 if (ctx->subj_hash && ctx->hdrs[i]->env->real_subj)
1078 hash_delete (ctx->subj_hash, ctx->hdrs[i]->env->real_subj, ctx->hdrs[i], NULL);
1079 if (ctx->id_hash && ctx->hdrs[i]->env->message_id)
1080 hash_delete (ctx->id_hash, ctx->hdrs[i]->env->message_id, ctx->hdrs[i], NULL);
1081 mutt_free_header (&ctx->hdrs[i]);
1089 /* save changes to mailbox
1095 int mx_sync_mailbox (CONTEXT *ctx, int *index_hint)
1099 int msgcount, deleted;
1103 char buf[STRING], tmp[STRING];
1104 if (km_expand_key (buf, sizeof(buf),
1105 km_find_func (MENU_MAIN, OP_TOGGLE_WRITE)))
1106 snprintf (tmp, sizeof(tmp), _(" Press '%s' to toggle write"), buf);
1108 strfcpy (tmp, _("Use 'toggle-write' to re-enable write!"), sizeof(tmp));
1110 mutt_error (_("Mailbox is marked unwritable. %s"), tmp);
1113 else if (ctx->readonly)
1115 mutt_error _("Mailbox is read-only.");
1119 if (!ctx->changed && !ctx->deleted)
1121 mutt_message _("Mailbox is unchanged.");
1127 char buf[SHORT_STRING];
1129 snprintf (buf, sizeof (buf), ctx->deleted == 1
1130 ? _("Purge %d deleted message?") : _("Purge %d deleted messages?"),
1132 if ((purge = query_quadoption (OPT_DELETE, buf)) < 0)
1134 else if (purge == M_NO)
1137 return 0; /* nothing to do! */
1139 /* let IMAP servers hold on to D flags */
1140 if (ctx->magic != M_IMAP)
1143 for (i = 0 ; i < ctx->msgcount ; i++)
1144 ctx->hdrs[i]->deleted = 0;
1148 else if (ctx->last_tag && ctx->last_tag->deleted)
1149 ctx->last_tag = NULL; /* reset last tagged msg now useless */
1152 /* really only for IMAP - imap_sync_mailbox results in a call to
1153 * mx_update_tables, so ctx->deleted is 0 when it comes back */
1154 msgcount = ctx->msgcount;
1155 deleted = ctx->deleted;
1158 if (ctx->magic == M_IMAP)
1159 rc = imap_sync_mailbox (ctx, purge, index_hint);
1162 rc = sync_mailbox (ctx, index_hint);
1166 if (ctx->magic == M_IMAP && !purge)
1167 mutt_message _("Mailbox checkpointed.");
1170 mutt_message (_("%d kept, %d deleted."), msgcount - deleted,
1175 if (ctx->msgcount == ctx->deleted &&
1176 (ctx->magic == M_MBOX || ctx->magic == M_MMDF) &&
1177 !mutt_is_spool (ctx->path) && !option (OPTSAVEEMPTY))
1180 mx_fastclose_mailbox (ctx);
1184 /* if we haven't deleted any messages, we don't need to resort */
1185 /* ... except for certain folder formats which need "unsorted"
1186 * sort order in order to synchronize folders.
1188 * MH and maildir are safe. mbox-style seems to need re-sorting,
1189 * at least with the new threading code.
1191 if (purge || (ctx->magic != M_MAILDIR && ctx->magic != M_MH))
1194 /* IMAP does this automatically after handling EXPUNGE */
1195 if (ctx->magic != M_IMAP)
1198 mx_update_tables (ctx, 1);
1199 mutt_sort_headers (ctx, 1); /* rethread from scratch */
1208 /* {maildir,mh}_open_new_message are in mh.c. */
1210 static int mbox_open_new_message (MESSAGE *msg, CONTEXT *dest, HEADER *hdr)
1217 static int imap_open_new_message (MESSAGE *msg, CONTEXT *dest, HEADER *hdr)
1219 char tmp[_POSIX_PATH_MAX];
1222 if ((msg->fp = safe_fopen (tmp, "w")) == NULL)
1227 msg->path = safe_strdup(tmp);
1233 * dest destintation mailbox
1234 * hdr message being copied (required for maildir support, because
1235 * the filename depends on the message flags)
1237 MESSAGE *mx_open_new_message (CONTEXT *dest, HEADER *hdr, int flags)
1240 int (*func) (MESSAGE *, CONTEXT *, HEADER *);
1243 switch (dest->magic)
1247 func = mbox_open_new_message;
1250 func = maildir_open_new_message;
1253 func = mh_open_new_message;
1257 func = imap_open_new_message;
1261 dprint (1, (debugfile, "mx_open_new_message(): function unimplemented for mailbox type %d.\n",
1266 msg = safe_calloc (1, sizeof (MESSAGE));
1267 msg->magic = dest->magic;
1272 msg->flags.flagged = hdr->flagged;
1273 msg->flags.replied = hdr->replied;
1274 msg->flags.read = hdr->read;
1275 msg->received = hdr->received;
1278 if(msg->received == 0)
1279 time(&msg->received);
1281 if (func (msg, dest, hdr) == 0)
1283 if (dest->magic == M_MMDF)
1284 fputs (MMDF_SEP, msg->fp);
1286 if ((msg->magic == M_MBOX || msg->magic == M_MMDF) &&
1291 if (hdr->env->return_path)
1292 p = hdr->env->return_path;
1293 else if (hdr->env->sender)
1294 p = hdr->env->sender;
1299 fprintf (msg->fp, "From %s %s", p ? p->mailbox : NONULL(Username), ctime (&msg->received));
1308 /* check for new mail */
1309 int mx_check_mailbox (CONTEXT *ctx, int *index_hint, int lock)
1315 if (ctx->locked) lock = 0;
1324 mutt_block_signals ();
1325 if (mbox_lock_mailbox (ctx, 0, 0) == -1)
1327 mutt_unblock_signals ();
1332 rc = mbox_check_mailbox (ctx, index_hint);
1336 mutt_unblock_signals ();
1337 mbox_unlock_mailbox (ctx);
1344 return (mh_check_mailbox (ctx, index_hint));
1346 return (maildir_check_mailbox (ctx, index_hint));
1350 return (imap_check_mailbox (ctx, index_hint, 0));
1351 #endif /* USE_IMAP */
1355 return (pop_check_mailbox (ctx, index_hint));
1356 #endif /* USE_POP */
1360 dprint (1, (debugfile, "mx_check_mailbox: null or invalid context.\n"));
1364 /* return a stream pointer for a message */
1365 MESSAGE *mx_open_message (CONTEXT *ctx, int msgno)
1369 msg = safe_calloc (1, sizeof (MESSAGE));
1370 switch (msg->magic = ctx->magic)
1380 HEADER *cur = ctx->hdrs[msgno];
1381 char path[_POSIX_PATH_MAX];
1383 snprintf (path, sizeof (path), "%s/%s", ctx->path, cur->path);
1385 if ((msg->fp = fopen (path, "r")) == NULL && errno == ENOENT &&
1386 ctx->magic == M_MAILDIR)
1387 msg->fp = maildir_open_find_message (ctx->path, cur->path);
1389 if (msg->fp == NULL)
1392 dprint (1, (debugfile, "mx_open_message: fopen: %s: %s (errno %d).\n",
1393 path, strerror (errno), errno));
1402 if (imap_fetch_message (msg, ctx, msgno) != 0)
1406 #endif /* USE_IMAP */
1411 if (pop_fetch_message (msg, ctx, msgno) != 0)
1415 #endif /* USE_POP */
1418 dprint (1, (debugfile, "mx_open_message(): function not implemented for mailbox type %d.\n", ctx->magic));
1425 /* commit a message to a folder */
1427 int mx_commit_message (MESSAGE *msg, CONTEXT *ctx)
1431 if (!(msg->write && ctx->append))
1433 dprint (1, (debugfile, "mx_commit_message(): msg->write = %d, ctx->append = %d\n",
1434 msg->write, ctx->append));
1442 if (fputs (MMDF_SEP, msg->fp) == EOF)
1449 if (fputc ('\n', msg->fp) == EOF)
1457 if ((r = safe_fclose (&msg->fp)) == 0)
1458 r = imap_append_message (ctx, msg);
1465 r = maildir_commit_message (ctx, msg, NULL);
1471 r = mh_commit_message (ctx, msg, NULL);
1476 if (r == 0 && (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
1477 && (fflush (msg->fp) == EOF || fsync (fileno (msg->fp)) == -1))
1479 mutt_perror _("Can't write message");
1486 /* close a pointer to a message */
1487 int mx_close_message (MESSAGE **msg)
1491 if ((*msg)->magic == M_MH || (*msg)->magic == M_MAILDIR
1493 || (*msg)->magic == M_IMAP
1496 || (*msg)->magic == M_POP
1500 r = safe_fclose (&(*msg)->fp);
1507 dprint (1, (debugfile, "mx_close_message (): unlinking %s\n",
1509 unlink ((*msg)->path);
1510 FREE (&(*msg)->path);
1513 FREE (msg); /* __FREE_CHECKED__ */
1517 void mx_alloc_memory (CONTEXT *ctx)
1520 size_t s = MAX (sizeof (HEADER *), sizeof (int));
1522 if ((ctx->hdrmax + 25) * s < ctx->hdrmax * s)
1524 mutt_error _("Integer overflow -- can't allocate memory.");
1531 safe_realloc (&ctx->hdrs, sizeof (HEADER *) * (ctx->hdrmax += 25));
1532 safe_realloc (&ctx->v2r, sizeof (int) * ctx->hdrmax);
1536 ctx->hdrs = safe_calloc ((ctx->hdrmax += 25), sizeof (HEADER *));
1537 ctx->v2r = safe_calloc (ctx->hdrmax, sizeof (int));
1539 for (i = ctx->msgcount ; i < ctx->hdrmax ; i++)
1541 ctx->hdrs[i] = NULL;
1546 /* this routine is called to update the counts in the context structure for
1547 * the last message header parsed.
1549 void mx_update_context (CONTEXT *ctx, int new_messages)
1554 for (msgno = ctx->msgcount - new_messages; msgno < ctx->msgcount; msgno++)
1556 h = ctx->hdrs[msgno];
1560 /* NOTE: this _must_ be done before the check for mailcap! */
1561 h->security = crypt_query (h->content);
1566 ctx->v2r[ctx->vcount] = msgno;
1567 h->virtual = ctx->vcount++;
1573 if (h->env->supersedes)
1578 ctx->id_hash = mutt_make_id_hash (ctx);
1580 h2 = hash_find (ctx->id_hash, h->env->supersedes);
1582 /* FREE (&h->env->supersedes); should I ? */
1586 if (option (OPTSCORE))
1587 mutt_score_message (ctx, h2, 1);
1591 /* add this message to the hash tables */
1592 if (ctx->id_hash && h->env->message_id)
1593 hash_insert (ctx->id_hash, h->env->message_id, h, 0);
1594 if (ctx->subj_hash && h->env->real_subj)
1595 hash_insert (ctx->subj_hash, h->env->real_subj, h, 1);
1597 if (option (OPTSCORE))
1598 mutt_score_message (ctx, h, 0);
1617 * 1 if the specified mailbox contains 0 messages.
1618 * 0 if the mailbox contains messages
1621 int mx_check_empty (const char *path)
1623 switch (mx_get_magic (path))
1627 return mbox_check_empty (path);
1629 return mh_check_empty (path);
1631 return maildir_check_empty (path);
1639 /* vim: set sw=2: */