move history in lib-ui
[apps/madmutt.git] / mx.c
1 /*
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>
5  *
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.
9  */
10
11 #if HAVE_CONFIG_H
12 # include "config.h"
13 #endif
14
15 #include <dirent.h>
16 #include <fcntl.h>
17 #include <sys/file.h>
18 #include <sys/stat.h>
19 #include <errno.h>
20 #include <unistd.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <ctype.h>
24 #include <utime.h>
25
26 #include <lib-lib/mem.h>
27 #include <lib-lib/ascii.h>
28 #include <lib-lib/str.h>
29 #include <lib-lib/macros.h>
30 #include <lib-lib/file.h>
31 #include <lib-lib/debug.h>
32
33 #include <lib-sys/unix.h>
34
35 #include <lib-mime/mime.h>
36
37 #include "mutt.h"
38 #include "buffy.h"
39 #include "mx.h"
40 #include "mbox.h"
41 #include "mh.h"
42 #include "sort.h"
43 #include "thread.h"
44 #include "copy.h"
45 #include "keymap.h"
46 #include "url.h"
47 #include "sidebar.h"
48 #include "compress.h"
49
50 #include <imap/imap.h>
51 #include <imap/mx_imap.h>
52
53 #include <pop/pop.h>
54 #include <pop/mx_pop.h>
55
56 #ifdef USE_NNTP
57 #include "nntp/nntp.h"
58 #include "nntp/mx_nntp.h"
59 #endif
60
61 #ifdef USE_DOTLOCK
62 #include "dotlock.h"
63 #endif
64
65 #include <lib-crypt/crypt.h>
66
67 #include "lib/list.h"
68
69 static list2_t* MailboxFormats = NULL;
70 #define MX_COMMAND(idx,cmd) ((mx_t*) MailboxFormats->data[idx])->cmd
71 #define MX_IDX(idx) (idx >= 0 && idx < MailboxFormats->length)
72
73 #define mutt_is_spool(s)  (m_strcmp(Spoolfile, s) == 0)
74
75 #ifdef USE_DOTLOCK
76 /* parameters: 
77  * path - file to lock
78  * retry - should retry if unable to lock?
79  */
80
81 static int invoke_dotlock (const char *path, int dummy, int flags, int retry)
82 {
83   char cmd[LONG_STRING + _POSIX_PATH_MAX];
84   char f[SHORT_STRING + _POSIX_PATH_MAX];
85   char r[SHORT_STRING];
86
87   if (flags & DL_FL_RETRY)
88     snprintf (r, sizeof (r), "-r %d ", retry ? MAXLOCKATTEMPT : 0);
89
90   mutt_quote_filename (f, sizeof (f), path);
91
92   snprintf (cmd, sizeof (cmd),
93             "%s %s%s%s%s%s%s%s",
94             NONULL (MuttDotlock),
95             flags & DL_FL_TRY ? "-t " : "",
96             flags & DL_FL_UNLOCK ? "-u " : "",
97             flags & DL_FL_USEPRIV ? "-p " : "",
98             flags & DL_FL_FORCE ? "-f " : "",
99             flags & DL_FL_UNLINK ? "-d " : "",
100             flags & DL_FL_RETRY ? r : "", f);
101
102   return mutt_system (cmd);
103 }
104
105 static int dotlock_file (const char *path, int fd, int retry)
106 {
107   int r;
108   int flags = DL_FL_USEPRIV | DL_FL_RETRY;
109
110   if (retry)
111     retry = 1;
112
113 retry_lock:
114   if ((r = invoke_dotlock (path, fd, flags, retry)) == DL_EX_EXIST) {
115     if (!option (OPTNOCURSES)) {
116       char msg[LONG_STRING];
117
118       snprintf (msg, sizeof (msg),
119                 _("Lock count exceeded, remove lock for %s?"), path);
120       if (retry && mutt_yesorno (msg, M_YES) == M_YES) {
121         flags |= DL_FL_FORCE;
122         retry--;
123         mutt_clear_error ();
124         goto retry_lock;
125       }
126     }
127     else {
128       mutt_error (_("Can't dotlock %s.\n"), path);
129     }
130   }
131   return (r == DL_EX_OK ? 0 : -1);
132 }
133
134 static int undotlock_file (const char *path, int fd)
135 {
136   return (invoke_dotlock (path, fd, DL_FL_USEPRIV | DL_FL_UNLOCK, 0) ==
137           DL_EX_OK ? 0 : -1);
138 }
139
140 #endif /* USE_DOTLOCK */
141
142 /* looks up index of type for path in MailboxFormats */
143 static int mx_get_idx (const char* path) {
144   int i = 0, t = 0;
145   struct stat st;
146
147   /* first, test all non-local folders to avoid stat() call */
148   for (i = 0; i < MailboxFormats->length; i++) {
149     if (!MX_COMMAND(i,local))
150       t = MX_COMMAND(i,mx_is_magic)(path, NULL);
151     if (t >= 1)
152       return (t-1);
153   }
154   if (stat (path, &st) == 0) {
155     /* if stat() succeeded, keep testing until success and
156      * pass stat() info so that we only need to do it once */
157     for (i = 0; i < MailboxFormats->length; i++) {
158       if (MX_COMMAND(i,local))
159         t = MX_COMMAND(i,mx_is_magic)(path, &st);
160       if (t >= 1)
161         return (t-1);
162     }
163   }
164   return (-1);
165 }
166
167 /* Args:
168  *      excl            if excl != 0, request an exclusive lock
169  *      dot             if dot != 0, try to dotlock the file
170  *      timeout         should retry locking?
171  */
172 int mx_lock_file (const char *path, int fd, int excl, int dot, int timeout)
173 {
174 #if defined (USE_FCNTL) || defined (USE_FLOCK)
175   int count;
176   int attempt;
177   struct stat prev_sb;
178 #endif
179   int r = 0;
180
181 #ifdef USE_FCNTL
182   struct flock lck;
183
184
185   p_clear(&lck, 1);
186   lck.l_type = excl ? F_WRLCK : F_RDLCK;
187   lck.l_whence = SEEK_SET;
188
189   count = 0;
190   attempt = 0;
191   prev_sb.st_size = 0;
192   while (fcntl (fd, F_SETLK, &lck) == -1) {
193     struct stat sb;
194
195     debug_print (1, ("fcntl errno %d.\n", errno));
196     if (errno != EAGAIN && errno != EACCES) {
197       mutt_perror ("fcntl");
198       return (-1);
199     }
200
201     if (fstat (fd, &sb) != 0)
202       sb.st_size = 0;
203
204     if (count == 0)
205       prev_sb = sb;
206
207     /* only unlock file if it is unchanged */
208     if (prev_sb.st_size == sb.st_size
209         && ++count >= (timeout ? MAXLOCKATTEMPT : 0)) {
210       if (timeout)
211         mutt_error _("Timeout exceeded while attempting fcntl lock!");
212
213       return (-1);
214     }
215
216     prev_sb = sb;
217
218     mutt_message (_("Waiting for fcntl lock... %d"), ++attempt);
219     sleep (1);
220   }
221 #endif /* USE_FCNTL */
222
223 #ifdef USE_FLOCK
224   count = 0;
225   attempt = 0;
226   while (flock (fd, (excl ? LOCK_EX : LOCK_SH) | LOCK_NB) == -1) {
227     struct stat sb;
228
229     if (errno != EWOULDBLOCK) {
230       mutt_perror ("flock");
231       r = -1;
232       break;
233     }
234
235     if (fstat (fd, &sb) != 0)
236       sb.st_size = 0;
237
238     if (count == 0)
239       prev_sb = sb;
240
241     /* only unlock file if it is unchanged */
242     if (prev_sb.st_size == sb.st_size
243         && ++count >= (timeout ? MAXLOCKATTEMPT : 0)) {
244       if (timeout)
245         mutt_error _("Timeout exceeded while attempting flock lock!");
246
247       r = -1;
248       break;
249     }
250
251     prev_sb = sb;
252
253     mutt_message (_("Waiting for flock attempt... %d"), ++attempt);
254     sleep (1);
255   }
256 #endif /* USE_FLOCK */
257
258 #ifdef USE_DOTLOCK
259   if (r == 0 && dot)
260     r = dotlock_file (path, fd, timeout);
261 #endif /* USE_DOTLOCK */
262
263   if (r == -1) {
264     /* release any other locks obtained in this routine */
265
266 #ifdef USE_FCNTL
267     lck.l_type = F_UNLCK;
268     fcntl (fd, F_SETLK, &lck);
269 #endif /* USE_FCNTL */
270
271 #ifdef USE_FLOCK
272     flock (fd, LOCK_UN);
273 #endif /* USE_FLOCK */
274
275     return (-1);
276   }
277
278   return 0;
279 }
280
281 int mx_unlock_file (const char *path, int fd, int dot)
282 {
283 #ifdef USE_FCNTL
284   struct flock unlockit;
285
286   p_clear(&unlockit, 1);
287   unlockit.l_type = F_UNLCK;
288   unlockit.l_whence = SEEK_SET;
289   fcntl (fd, F_SETLK, &unlockit);
290 #endif
291
292 #ifdef USE_FLOCK
293   flock (fd, LOCK_UN);
294 #endif
295
296 #ifdef USE_DOTLOCK
297   if (dot)
298     undotlock_file (path, fd);
299 #endif
300
301   return 0;
302 }
303
304 void mx_unlink_empty (const char *path)
305 {
306   int fd;
307
308 #ifndef USE_DOTLOCK
309   struct stat sb;
310 #endif
311
312   if ((fd = open (path, O_RDWR)) == -1)
313     return;
314
315   if (mx_lock_file (path, fd, 1, 0, 1) == -1) {
316     close (fd);
317     return;
318   }
319
320 #ifdef USE_DOTLOCK
321   invoke_dotlock (path, fd, DL_FL_UNLINK, 1);
322 #else
323   if (fstat (fd, &sb) == 0 && sb.st_size == 0)
324     unlink (path);
325 #endif
326
327   mx_unlock_file (path, fd, 0);
328   close (fd);
329 }
330
331 /* try to figure out what type of mailbox ``path'' is */
332 int mx_get_magic (const char *path) {
333   int i = 0;
334
335   if (m_strlen(path) == 0)
336     return (-1);
337   if ((i = mx_get_idx (path)) >= 0)
338     return (MX_COMMAND(i,type));
339   return (-1);
340 }
341
342 int mx_is_local (int m) {
343   if (!MX_IDX(m))
344     return (0);
345   return (MX_COMMAND(m,local));
346 }
347
348 /*
349  * set DefaultMagic to the given value
350  */
351 int mx_set_magic (const char *s)
352 {
353   if (ascii_strcasecmp (s, "mbox") == 0)
354     DefaultMagic = M_MBOX;
355   else if (ascii_strcasecmp (s, "mmdf") == 0)
356     DefaultMagic = M_MMDF;
357   else if (ascii_strcasecmp (s, "mh") == 0)
358     DefaultMagic = M_MH;
359   else if (ascii_strcasecmp (s, "maildir") == 0)
360     DefaultMagic = M_MAILDIR;
361   else
362     return (-1);
363
364   return 0;
365 }
366
367 /* mx_access: Wrapper for access, checks permissions on a given mailbox.
368  *   We may be interested in using ACL-style flags at some point, currently
369  *   we use the normal access() flags. */
370 int mx_access (const char *path, int flags)
371 {
372   int i = 0;
373
374   if ((i = mx_get_idx (path)) >= 0 && MX_COMMAND(i,mx_access))
375     return (MX_COMMAND(i,mx_access)(path,flags));
376   return (0);
377 }
378
379 static int mx_open_mailbox_append (CONTEXT * ctx, int flags)
380 {
381   struct stat sb;
382
383   /* special case for appending to compressed folders -
384    * even if we can not open them for reading */
385   if (mutt_can_append_compressed (ctx->path))
386     mutt_open_append_compressed (ctx);
387
388   ctx->append = 1;
389
390   if (mx_get_magic (ctx->path) == M_IMAP)
391     return imap_open_mailbox_append (ctx);
392
393   if (stat (ctx->path, &sb) == 0) {
394     ctx->magic = mx_get_magic (ctx->path);
395
396     switch (ctx->magic) {
397     case 0:
398       mutt_error (_("%s is not a mailbox."), ctx->path);
399       /* fall through */
400     case -1:
401       return (-1);
402     }
403   }
404   else if (errno == ENOENT) {
405     ctx->magic = DefaultMagic;
406
407     if (ctx->magic == M_MH || ctx->magic == M_MAILDIR) {
408       char tmp[_POSIX_PATH_MAX];
409
410       if (mkdir (ctx->path, S_IRWXU)) {
411         mutt_perror (ctx->path);
412         return (-1);
413       }
414
415       if (ctx->magic == M_MAILDIR) {
416         snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
417         if (mkdir (tmp, S_IRWXU)) {
418           mutt_perror (tmp);
419           rmdir (ctx->path);
420           return (-1);
421         }
422
423         snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
424         if (mkdir (tmp, S_IRWXU)) {
425           mutt_perror (tmp);
426           snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
427           rmdir (tmp);
428           rmdir (ctx->path);
429           return (-1);
430         }
431         snprintf (tmp, sizeof (tmp), "%s/tmp", ctx->path);
432         if (mkdir (tmp, S_IRWXU)) {
433           mutt_perror (tmp);
434           snprintf (tmp, sizeof (tmp), "%s/cur", ctx->path);
435           rmdir (tmp);
436           snprintf (tmp, sizeof (tmp), "%s/new", ctx->path);
437           rmdir (tmp);
438           rmdir (ctx->path);
439           return (-1);
440         }
441       }
442       else {
443         int i;
444
445         snprintf (tmp, sizeof (tmp), "%s/.mh_sequences", ctx->path);
446         if ((i = creat (tmp, S_IRWXU)) == -1) {
447           mutt_perror (tmp);
448           rmdir (ctx->path);
449           return (-1);
450         }
451         close (i);
452       }
453     }
454   }
455   else {
456     mutt_perror (ctx->path);
457     return (-1);
458   }
459
460   switch (ctx->magic) {
461   case M_MBOX:
462   case M_MMDF:
463     if ((ctx->fp =
464          safe_fopen (ctx->path, flags & M_NEWFOLDER ? "w" : "a")) == NULL
465         || mbox_lock_mailbox (ctx, 1, 1) != 0) {
466       if (!ctx->fp)
467         mutt_perror (ctx->path);
468       else {
469         mutt_error (_("Couldn't lock %s\n"), ctx->path);
470         safe_fclose (&ctx->fp);
471       }
472       return (-1);
473     }
474     fseeko (ctx->fp, 0, 2);
475     break;
476
477   case M_MH:
478   case M_MAILDIR:
479     /* nothing to do */
480     break;
481
482   default:
483     return (-1);
484   }
485
486   return 0;
487 }
488
489 /*
490  * open a mailbox and parse it
491  *
492  * Args:
493  *      flags   M_NOSORT        do not sort mailbox
494  *              M_APPEND        open mailbox for appending
495  *              M_READONLY      open mailbox in read-only mode
496  *              M_QUIET         only print error messages
497  *      ctx     if non-null, context struct to use
498  */
499 CONTEXT *mx_open_mailbox (const char *path, int flags, CONTEXT * pctx)
500 {
501   CONTEXT *ctx = pctx;
502   int rc;
503
504   if (!ctx)
505     ctx = p_new(CONTEXT, 1);
506   p_clear(ctx, 1);
507   ctx->path = m_strdup(path);
508
509   ctx->msgnotreadyet = -1;
510   ctx->collapsed = 0;
511
512   if (flags & M_QUIET)
513     ctx->quiet = 1;
514   if (flags & M_READONLY)
515     ctx->readonly = 1;
516   if (flags & M_COUNT)
517     ctx->counting = 1;
518
519   if (flags & (M_APPEND | M_NEWFOLDER)) {
520     if (mx_open_mailbox_append (ctx, flags) != 0) {
521       mx_fastclose_mailbox (ctx);
522       if (!pctx)
523         p_delete(&ctx);
524       return NULL;
525     }
526     return ctx;
527   }
528
529   if (!MX_IDX(ctx->magic-1))
530     ctx->magic = mx_get_magic (path);
531
532   if (ctx->magic == M_COMPRESSED)
533     mutt_open_read_compressed (ctx);
534
535   if (ctx->magic == 0)
536     mutt_error (_("%s is not a mailbox."), path);
537
538   if (ctx->magic == -1)
539     mutt_perror (path);
540
541   if (ctx->magic <= 0) {
542     mx_fastclose_mailbox (ctx);
543     if (!pctx)
544       p_delete(&ctx);
545     return (NULL);
546   }
547
548   /* if the user has a `push' command in their .muttrc, or in a folder-hook,
549    * it will cause the progress messages not to be displayed because
550    * mutt_refresh() will think we are in the middle of a macro.  so set a
551    * flag to indicate that we should really refresh the screen.
552    */
553   set_option (OPTFORCEREFRESH);
554
555   if (!ctx->quiet)
556     mutt_message (_("Reading %s..."), ctx->path);
557
558   rc = MX_COMMAND(ctx->magic-1,mx_open_mailbox)(ctx);
559
560   if (rc == 0) {
561     if ((flags & M_NOSORT) == 0) {
562       /* avoid unnecessary work since the mailbox is completely unthreaded
563          to begin with */
564       unset_option (OPTSORTSUBTHREADS);
565       unset_option (OPTNEEDRESCORE);
566       mutt_sort_headers (ctx, 1);
567     }
568     if (!ctx->quiet)
569       mutt_clear_error ();
570   }
571   else {
572     mx_fastclose_mailbox (ctx);
573     if (!pctx)
574       p_delete(&ctx);
575   }
576
577   unset_option (OPTFORCEREFRESH);
578   return (ctx);
579 }
580
581 /* free up memory associated with the mailbox context */
582 void mx_fastclose_mailbox (CONTEXT * ctx)
583 {
584   int i;
585
586   if (!ctx)
587     return;
588
589   if (MX_IDX(ctx->magic-1) && MX_COMMAND(ctx->magic-1,mx_fastclose_mailbox))
590     MX_COMMAND(ctx->magic-1,mx_fastclose_mailbox(ctx));
591   if (ctx->subj_hash)
592     hash_destroy (&ctx->subj_hash, NULL);
593   if (ctx->id_hash)
594     hash_destroy (&ctx->id_hash, NULL);
595   mutt_clear_threads (ctx);
596   for (i = 0; i < ctx->msgcount; i++)
597     header_delete(&ctx->hdrs[i]);
598   p_delete(&ctx->hdrs);
599   p_delete(&ctx->v2r);
600
601   if (ctx->compressinfo)
602     mutt_fast_close_compressed (ctx);
603
604   p_delete(&ctx->path);
605   p_delete(&ctx->pattern);
606   if (ctx->limit_pattern)
607     mutt_pattern_free (&ctx->limit_pattern);
608   safe_fclose (&ctx->fp);
609   p_clear(ctx, 1);
610 }
611
612 /* save changes to disk */
613 static int sync_mailbox (CONTEXT * ctx, int *index_hint)
614 {
615   int rc = -1;
616
617   if (!ctx->quiet)
618     mutt_message (_("Writing %s..."), ctx->path);
619
620   if (MX_IDX(ctx->magic-1))
621     /* the 1 is only of interest for IMAP and means EXPUNGE */
622     rc = MX_COMMAND(ctx->magic-1,mx_sync_mailbox(ctx,1,index_hint));
623
624   if (rc == 0 && ctx->compressinfo)
625     return mutt_sync_compressed (ctx);
626
627   return rc;
628 }
629
630 /* move deleted mails to the trash folder */
631 static int trash_append (CONTEXT * ctx)
632 {
633   CONTEXT *ctx_trash;
634   int i = 0;
635   struct stat st, stc;
636
637   if (!TrashPath || !ctx->deleted ||
638       (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))
639     return 0;
640
641   for (; i < ctx->msgcount && (!ctx->hdrs[i]->deleted ||
642                                ctx->hdrs[i]->appended); i++);
643   if (i == ctx->msgcount)
644     return 0;                   /* nothing to be done */
645
646   if (mutt_save_confirm (TrashPath, &st) != 0) {
647     mutt_error _("message(s) not deleted");
648
649     return -1;
650   }
651
652   if (lstat (ctx->path, &stc) == 0 && stc.st_ino == st.st_ino
653       && stc.st_dev == st.st_dev && stc.st_rdev == st.st_rdev)
654     return 0;                   /* we are in the trash folder: simple sync */
655
656   if ((ctx_trash = mx_open_mailbox (TrashPath, M_APPEND, NULL)) != NULL) {
657     for (i = 0; i < ctx->msgcount; i++)
658       if (ctx->hdrs[i]->deleted && !ctx->hdrs[i]->appended
659           && !ctx->hdrs[i]->purged
660           && mutt_append_message (ctx_trash, ctx, ctx->hdrs[i], 0, 0) == -1) {
661         mx_close_mailbox (ctx_trash, NULL);
662         return -1;
663       }
664
665     mx_close_mailbox (ctx_trash, NULL);
666   }
667   else {
668     mutt_error _("Can't open trash folder");
669
670     return -1;
671   }
672
673   return 0;
674 }
675
676 /* save changes and close mailbox */
677 static int _mx_close_mailbox (CONTEXT * ctx, int *index_hint)
678 {
679   int i, move_messages = 0, purge = 1, read_msgs = 0;
680   int check;
681   int isSpool = 0;
682   CONTEXT f;
683   char mbox[_POSIX_PATH_MAX];
684   char buf[SHORT_STRING];
685
686   if (!ctx)
687     return 0;
688
689   ctx->closing = 1;
690
691 #ifdef USE_NNTP
692   if (ctx->magic == M_NNTP) {
693     int ret;
694
695     ret = nntp_close_mailbox (ctx);
696     mx_fastclose_mailbox (ctx);
697     return ret;
698   }
699 #endif
700   if (ctx->readonly || ctx->dontwrite) {
701     /* mailbox is readonly or we don't want to write */
702     mx_fastclose_mailbox (ctx);
703     return 0;
704   }
705
706   if (ctx->append) {
707     /* mailbox was opened in write-mode */
708     if (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
709       mbox_close_mailbox (ctx);
710     else
711       mx_fastclose_mailbox (ctx);
712     return 0;
713   }
714
715   for (i = 0; i < ctx->msgcount; i++) {
716     if (!ctx->hdrs[i]->deleted && ctx->hdrs[i]->read
717         && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
718       read_msgs++;
719   }
720
721   if (read_msgs && quadoption (OPT_MOVE) != M_NO) {
722     char *p;
723
724     if ((p = mutt_find_hook (M_MBOXHOOK, ctx->path))) {
725       isSpool = 1;
726       m_strcpy(mbox, sizeof(mbox), p);
727     }
728     else {
729       m_strcpy(mbox, sizeof(mbox), NONULL(Inbox));
730       isSpool = mutt_is_spool (ctx->path) && !mutt_is_spool (mbox);
731     }
732     mutt_expand_path (mbox, sizeof (mbox));
733
734     if (isSpool) {
735       snprintf (buf, sizeof (buf), _("Move read messages to %s?"), mbox);
736       if ((move_messages = query_quadoption (OPT_MOVE, buf)) == -1) {
737         ctx->closing = 0;
738         return (-1);
739       }
740     }
741   }
742
743   /* 
744    * There is no point in asking whether or not to purge if we are
745    * just marking messages as "trash".
746    */
747   if (ctx->deleted && !(ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH))) {
748     snprintf (buf, sizeof (buf), ctx->deleted == 1
749               ? _("Purge %d deleted message?") :
750               _("Purge %d deleted messages?"), ctx->deleted);
751     if ((purge = query_quadoption (OPT_DELETE, buf)) < 0) {
752       ctx->closing = 0;
753       return (-1);
754     }
755   }
756
757   /* IMAP servers manage the OLD flag themselves */
758   if (ctx->magic != M_IMAP)
759     if (option (OPTMARKOLD)) {
760       for (i = 0; i < ctx->msgcount; i++) {
761         if (!ctx->hdrs[i]->deleted && !ctx->hdrs[i]->old)
762           mutt_set_flag (ctx, ctx->hdrs[i], M_OLD, 1);
763       }
764     }
765
766   if (move_messages) {
767     mutt_message (_("Moving read messages to %s..."), mbox);
768
769     /* try to use server-side copy first */
770     i = 1;
771
772     if (ctx->magic == M_IMAP && imap_is_magic (mbox, NULL) == M_IMAP) {
773       /* tag messages for moving, and clear old tags, if any */
774       for (i = 0; i < ctx->msgcount; i++)
775         if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted
776             && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED)))
777           ctx->hdrs[i]->tagged = 1;
778         else
779           ctx->hdrs[i]->tagged = 0;
780
781       i = imap_copy_messages (ctx, NULL, mbox, 1);
782     }
783
784     if (i == 0)                 /* success */
785       mutt_clear_error ();
786     else if (i == -1) {         /* horrible error, bail */
787       ctx->closing = 0;
788       return -1;
789     }
790     else                        /* use regular append-copy mode */
791     {
792       if (mx_open_mailbox (mbox, M_APPEND, &f) == NULL) {
793         ctx->closing = 0;
794         return -1;
795       }
796
797       for (i = 0; i < ctx->msgcount; i++) {
798         if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted
799             && !(ctx->hdrs[i]->flagged && option (OPTKEEPFLAGGED))) {
800           if (mutt_append_message (&f, ctx, ctx->hdrs[i], 0, CH_UPDATE_LEN) ==
801               0) {
802             mutt_set_flag (ctx, ctx->hdrs[i], M_DELETE, 1);
803             mutt_set_flag (ctx, ctx->hdrs[i], M_APPENDED, 1);
804           }
805           else {
806             mx_close_mailbox (&f, NULL);
807             ctx->closing = 0;
808             return -1;
809           }
810         }
811       }
812
813       mx_close_mailbox (&f, NULL);
814     }
815
816   }
817   else if (!ctx->changed && ctx->deleted == 0) {
818     mutt_message _("Mailbox is unchanged.");
819
820     mx_fastclose_mailbox (ctx);
821     return 0;
822   }
823
824   /* copy mails to the trash before expunging */
825   if (purge && ctx->deleted)
826     if (trash_append (ctx) != 0) {
827       ctx->closing = 0;
828       return -1;
829     }
830
831   /* allow IMAP to preserve the deleted flag across sessions */
832   if (ctx->magic == M_IMAP) {
833     if ((check = imap_sync_mailbox (ctx, purge, index_hint)) != 0) {
834       ctx->closing = 0;
835       return check;
836     }
837   }
838   else
839   {
840     if (!purge) {
841       for (i = 0; i < ctx->msgcount; i++)
842         ctx->hdrs[i]->deleted = 0;
843       ctx->deleted = 0;
844     }
845
846     if (ctx->changed || ctx->deleted) {
847       if ((check = sync_mailbox (ctx, index_hint)) != 0) {
848         ctx->closing = 0;
849         return check;
850       }
851     }
852   }
853
854   if (move_messages)
855     mutt_message (_("%d kept, %d moved, %d deleted."),
856                   ctx->msgcount - ctx->deleted, read_msgs, ctx->deleted);
857   else
858     mutt_message (_("%d kept, %d deleted."),
859                   ctx->msgcount - ctx->deleted, ctx->deleted);
860
861   if (ctx->msgcount == ctx->deleted &&
862       (ctx->magic == M_MMDF || ctx->magic == M_MBOX) &&
863       !mutt_is_spool (ctx->path) && !option (OPTSAVEEMPTY))
864     mx_unlink_empty (ctx->path);
865
866   if (ctx->compressinfo && mutt_slow_close_compressed (ctx))
867     return (-1);
868
869   mx_fastclose_mailbox (ctx);
870
871   return 0;
872 }
873
874 int mx_close_mailbox (CONTEXT * ctx, int *index_hint) {
875   int ret = 0;
876   if (!ctx)
877     return (0);
878   ret = _mx_close_mailbox (ctx, index_hint);
879   sidebar_set_buffystats (ctx);
880   return (ret);
881 }
882
883 /* update a Context structure's internal tables. */
884
885 void mx_update_tables (CONTEXT * ctx, int committing)
886 {
887   int i, j;
888
889   /* update memory to reflect the new state of the mailbox */
890   ctx->vcount = 0;
891   ctx->vsize = 0;
892   ctx->tagged = 0;
893   ctx->deleted = 0;
894   ctx->new = 0;
895   ctx->unread = 0;
896   ctx->changed = 0;
897   ctx->flagged = 0;
898 #define this_body ctx->hdrs[j]->content
899   for (i = 0, j = 0; i < ctx->msgcount; i++) {
900     if ((committing && (!ctx->hdrs[i]->deleted ||
901                         (ctx->magic == M_MAILDIR
902                          && option (OPTMAILDIRTRASH)))) || (!committing
903                                                             && ctx->hdrs[i]->
904                                                             active)) {
905       if (i != j) {
906         ctx->hdrs[j] = ctx->hdrs[i];
907         ctx->hdrs[i] = NULL;
908       }
909       ctx->hdrs[j]->msgno = j;
910       if (ctx->hdrs[j]->virtual != -1) {
911         ctx->v2r[ctx->vcount] = j;
912         ctx->hdrs[j]->virtual = ctx->vcount++;
913         ctx->vsize += this_body->length + this_body->offset -
914           this_body->hdr_offset;
915       }
916
917       if (committing)
918         ctx->hdrs[j]->changed = 0;
919       else if (ctx->hdrs[j]->changed)
920         ctx->changed++;
921
922       if (!committing
923           || (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH))) {
924         if (ctx->hdrs[j]->deleted)
925           ctx->deleted++;
926       }
927
928       if (ctx->hdrs[j]->tagged)
929         ctx->tagged++;
930       if (ctx->hdrs[j]->flagged)
931         ctx->flagged++;
932       if (!ctx->hdrs[j]->read) {
933         ctx->unread++;
934         if (!ctx->hdrs[j]->old)
935           ctx->new++;
936       }
937
938       j++;
939     }
940     else {
941       if (ctx->magic == M_MH || ctx->magic == M_MAILDIR)
942         ctx->size -= (ctx->hdrs[i]->content->length +
943                       ctx->hdrs[i]->content->offset -
944                       ctx->hdrs[i]->content->hdr_offset);
945       /* remove message from the hash tables */
946       if (ctx->subj_hash && ctx->hdrs[i]->env->real_subj)
947         hash_delete (ctx->subj_hash, ctx->hdrs[i]->env->real_subj,
948                      ctx->hdrs[i], NULL);
949       if (ctx->id_hash && ctx->hdrs[i]->env->message_id)
950         hash_delete (ctx->id_hash, ctx->hdrs[i]->env->message_id,
951                      ctx->hdrs[i], NULL);
952       header_delete(&ctx->hdrs[i]);
953     }
954   }
955 #undef this_body
956   ctx->msgcount = j;
957
958   /* update sidebar count */
959   sidebar_set_buffystats (ctx);
960 }
961
962
963 /* save changes to mailbox
964  *
965  * return values:
966  *      0               success
967  *      -1              error
968  */
969 static int _mx_sync_mailbox (CONTEXT * ctx, int *index_hint)
970 {
971   int rc, i;
972   int purge = 1;
973   int msgcount, deleted;
974
975   if (ctx->dontwrite) {
976     char buf[STRING], tmp[STRING];
977
978     if (km_expand_key (buf, sizeof (buf),
979                        km_find_func (MENU_MAIN, OP_TOGGLE_WRITE)))
980       snprintf (tmp, sizeof (tmp), _(" Press '%s' to toggle write"), buf);
981     else
982       m_strcpy(tmp, sizeof(tmp), _("Use 'toggle-write' to re-enable write!"));
983
984     mutt_error (_("Mailbox is marked unwritable. %s"), tmp);
985     return -1;
986   }
987   else if (ctx->readonly) {
988     mutt_error _("Mailbox is read-only.");
989
990     return -1;
991   }
992
993   if (!ctx->changed && !ctx->deleted) {
994     mutt_message _("Mailbox is unchanged.");
995
996     return (0);
997   }
998
999   if (ctx->deleted) {
1000     char buf[SHORT_STRING];
1001
1002     snprintf (buf, sizeof (buf), ctx->deleted == 1
1003               ? _("Purge %d deleted message?") :
1004               _("Purge %d deleted messages?"), ctx->deleted);
1005     if ((purge = query_quadoption (OPT_DELETE, buf)) < 0)
1006       return (-1);
1007     else if (purge == M_NO) {
1008       if (!ctx->changed)
1009         return 0;               /* nothing to do! */
1010       /* let IMAP servers hold on to D flags */
1011       if (ctx->magic != M_IMAP)
1012       {
1013         for (i = 0; i < ctx->msgcount; i++)
1014           ctx->hdrs[i]->deleted = 0;
1015         ctx->deleted = 0;
1016       }
1017     }
1018     else if (ctx->last_tag && ctx->last_tag->deleted)
1019       ctx->last_tag = NULL;     /* reset last tagged msg now useless */
1020   }
1021
1022   /* really only for IMAP - imap_sync_mailbox results in a call to
1023    * mx_update_tables, so ctx->deleted is 0 when it comes back */
1024   msgcount = ctx->msgcount;
1025   deleted = ctx->deleted;
1026
1027   if (purge && ctx->deleted) {
1028     if (trash_append (ctx) == -1)
1029       return -1;
1030   }
1031
1032   if (ctx->magic == M_IMAP)
1033     rc = imap_sync_mailbox (ctx, purge, index_hint);
1034   else
1035     rc = sync_mailbox (ctx, index_hint);
1036   if (rc == 0) {
1037     if (ctx->magic == M_IMAP && !purge)
1038       mutt_message (_("Mailbox checkpointed."));
1039
1040     else
1041       mutt_message (_("%d kept, %d deleted."), msgcount - deleted, deleted);
1042
1043     mutt_sleep (0);
1044
1045     if (ctx->msgcount == ctx->deleted &&
1046         (ctx->magic == M_MBOX || ctx->magic == M_MMDF) &&
1047         !mutt_is_spool (ctx->path) && !option (OPTSAVEEMPTY)) {
1048       unlink (ctx->path);
1049       mx_fastclose_mailbox (ctx);
1050       return 0;
1051     }
1052
1053     /* if we haven't deleted any messages, we don't need to resort */
1054     /* ... except for certain folder formats which need "unsorted" 
1055      * sort order in order to synchronize folders.
1056      * 
1057      * MH and maildir are safe.  mbox-style seems to need re-sorting,
1058      * at least with the new threading code.
1059      */
1060     if (purge || (ctx->magic != M_MAILDIR && ctx->magic != M_MH)) {
1061       /* IMAP does this automatically after handling EXPUNGE */
1062       if (ctx->magic != M_IMAP)
1063       {
1064         mx_update_tables (ctx, 1);
1065         mutt_sort_headers (ctx, 1);     /* rethread from scratch */
1066       }
1067     }
1068   }
1069
1070   return (rc);
1071 }
1072
1073 int mx_sync_mailbox (CONTEXT* ctx, int* index_hint) {
1074   int ret = _mx_sync_mailbox (ctx, index_hint);
1075   sidebar_set_buffystats (ctx);
1076   return (ret);
1077 }
1078
1079 /* args:
1080  *      dest    destintation mailbox
1081  *      hdr     message being copied (required for maildir support, because
1082  *              the filename depends on the message flags)
1083  */
1084 MESSAGE *mx_open_new_message (CONTEXT * dest, HEADER * hdr, int flags)
1085 {
1086   MESSAGE *msg;
1087   address_t *p = NULL;
1088
1089   if (!MX_IDX(dest->magic-1)) {
1090     debug_print (1, ("function unimplemented for mailbox type %d.\n", dest->magic));
1091     return (NULL);
1092   }
1093
1094   msg = p_new(MESSAGE, 1);
1095   msg->magic = dest->magic;
1096   msg->write = 1;
1097
1098   if (hdr) {
1099     msg->flags.flagged = hdr->flagged;
1100     msg->flags.replied = hdr->replied;
1101     msg->flags.read = hdr->read;
1102     msg->received = hdr->received;
1103   }
1104
1105   if (msg->received == 0)
1106     time (&msg->received);
1107
1108   if (MX_COMMAND(dest->magic-1,mx_open_new_message)(msg, dest, hdr) == 0) {
1109     if (dest->magic == M_MMDF)
1110       fputs (MMDF_SEP, msg->fp);
1111
1112     if ((msg->magic == M_MBOX || msg->magic == M_MMDF) && flags & M_ADD_FROM) {
1113       if (hdr) {
1114         if (hdr->env->return_path)
1115           p = hdr->env->return_path;
1116         else if (hdr->env->sender)
1117           p = hdr->env->sender;
1118         else
1119           p = hdr->env->from;
1120       }
1121
1122       fprintf (msg->fp, "From %s %s", p ? p->mailbox : NONULL (Username),
1123                ctime (&msg->received));
1124     }
1125   }
1126   else
1127     p_delete(&msg);
1128
1129   return msg;
1130 }
1131
1132 /* check for new mail */
1133 int mx_check_mailbox (CONTEXT * ctx, int *index_hint, int lock) {
1134   if (ctx->compressinfo)
1135     return mutt_check_mailbox_compressed (ctx);
1136
1137   if (ctx) {
1138     if (ctx->locked)
1139       lock = 0;
1140     if (MX_IDX(ctx->magic-1) && MX_COMMAND(ctx->magic-1,mx_check_mailbox))
1141       return (MX_COMMAND(ctx->magic-1,mx_check_mailbox)(ctx, index_hint, lock));
1142   }
1143
1144   debug_print (1, ("null or invalid context.\n"));
1145   return (-1);
1146
1147 }
1148
1149 /* return a stream pointer for a message */
1150 MESSAGE *mx_open_message (CONTEXT * ctx, int msgno)
1151 {
1152   MESSAGE *msg;
1153
1154   msg = p_new(MESSAGE, 1);
1155   switch (msg->magic = ctx->magic) {
1156   case M_MBOX:
1157   case M_MMDF:
1158     msg->fp = ctx->fp;
1159     break;
1160
1161   case M_MH:
1162   case M_MAILDIR:
1163     {
1164       HEADER *cur = ctx->hdrs[msgno];
1165       char path[_POSIX_PATH_MAX];
1166
1167       snprintf (path, sizeof (path), "%s/%s", ctx->path, cur->path);
1168
1169       if ((msg->fp = fopen (path, "r")) == NULL && errno == ENOENT &&
1170           ctx->magic == M_MAILDIR)
1171         msg->fp = maildir_open_find_message (ctx->path, cur->path);
1172
1173       if (msg->fp == NULL) {
1174         mutt_perror (path);
1175         debug_print (1, ("fopen: %s: %s (errno %d).\n", path, strerror (errno), errno));
1176         p_delete(&msg);
1177       }
1178     }
1179     break;
1180
1181   case M_IMAP:
1182     {
1183       if (imap_fetch_message (msg, ctx, msgno) != 0)
1184         p_delete(&msg);
1185       break;
1186     }
1187
1188   case M_POP:
1189     {
1190       if (pop_fetch_message (msg, ctx, msgno) != 0)
1191         p_delete(&msg);
1192       break;
1193     }
1194
1195 #ifdef USE_NNTP
1196   case M_NNTP:
1197     {
1198       if (nntp_fetch_message (msg, ctx, msgno) != 0)
1199         p_delete(&msg);
1200       break;
1201     }
1202 #endif /* USE_NNTP */
1203
1204   default:
1205     debug_print (1, ("function not implemented for mailbox type %d.\n", ctx->magic));
1206     p_delete(&msg);
1207     break;
1208   }
1209   return (msg);
1210 }
1211
1212 /* commit a message to a folder */
1213
1214 int mx_commit_message (MESSAGE * msg, CONTEXT * ctx) {
1215   if (!(msg->write && ctx->append)) {
1216     debug_print (1, ("msg->write = %d, ctx->append = %d\n", msg->write, ctx->append));
1217     return -1;
1218   }
1219   if (!ctx || !MX_IDX(ctx->magic-1) || !MX_COMMAND(ctx->magic-1,mx_commit_message))
1220     return (-1);
1221   return (MX_COMMAND(ctx->magic-1,mx_commit_message) (msg, ctx));
1222 }
1223
1224 /* close a pointer to a message */
1225 int mx_close_message (MESSAGE ** msg)
1226 {
1227   int r = 0;
1228
1229   if ((*msg)->magic == M_MH || (*msg)->magic == M_MAILDIR
1230       || (*msg)->magic == M_IMAP
1231       || (*msg)->magic == M_POP
1232 #ifdef USE_NNTP
1233       || (*msg)->magic == M_NNTP
1234 #endif
1235     ) {
1236     r = safe_fclose (&(*msg)->fp);
1237   }
1238   else
1239     (*msg)->fp = NULL;
1240
1241   if ((*msg)->path) {
1242     debug_print (1, ("unlinking %s\n", (*msg)->path));
1243     unlink ((*msg)->path);
1244     p_delete(&(*msg)->path);
1245   }
1246
1247   p_delete(msg);
1248   return (r);
1249 }
1250
1251 void mx_alloc_memory (CONTEXT * ctx)
1252 {
1253   int i;
1254   size_t s = MAX (sizeof (HEADER *), sizeof (int));
1255
1256   if ((ctx->hdrmax + 25) * s < ctx->hdrmax * s) {
1257     mutt_error _("Integer overflow -- can't allocate memory.");
1258
1259     sleep (1);
1260     mutt_exit (1);
1261   }
1262
1263   if (ctx->hdrs) {
1264     p_realloc(&ctx->hdrs, ctx->hdrmax += 25);
1265     p_realloc(&ctx->v2r, ctx->hdrmax);
1266   }
1267   else {
1268     ctx->hdrs = p_new(HEADER *, (ctx->hdrmax += 25));
1269     ctx->v2r = p_new(int, ctx->hdrmax);
1270   }
1271   for (i = ctx->msgcount; i < ctx->hdrmax; i++) {
1272     ctx->hdrs[i] = NULL;
1273     ctx->v2r[i] = -1;
1274   }
1275 }
1276
1277 /* this routine is called to update the counts in the context structure for
1278  * the last message header parsed.
1279  */
1280 void mx_update_context (CONTEXT * ctx, int new_messages)
1281 {
1282   HEADER *h;
1283   int msgno;
1284
1285   for (msgno = ctx->msgcount - new_messages; msgno < ctx->msgcount; msgno++) {
1286     h = ctx->hdrs[msgno];
1287
1288     /* NOTE: this _must_ be done before the check for mailcap! */
1289     h->security = crypt_query (h->content);
1290
1291     if (!ctx->pattern) {
1292       ctx->v2r[ctx->vcount] = msgno;
1293       h->virtual = ctx->vcount++;
1294     }
1295     else
1296       h->virtual = -1;
1297     h->msgno = msgno;
1298
1299     if (h->env->supersedes) {
1300       HEADER *h2;
1301
1302       if (!ctx->id_hash)
1303         ctx->id_hash = mutt_make_id_hash (ctx);
1304
1305       h2 = hash_find (ctx->id_hash, h->env->supersedes);
1306
1307       /* p_delete(&h->env->supersedes); should I ? */
1308       if (h2) {
1309         h2->superseded = 1;
1310         if (!ctx->counting && option (OPTSCORE))
1311           mutt_score_message (ctx, h2, 1);
1312       }
1313     }
1314
1315     /* add this message to the hash tables */
1316     if (ctx->id_hash && h->env->message_id)
1317       hash_insert (ctx->id_hash, h->env->message_id, h, 0);
1318     if (!ctx->counting) {
1319       if (ctx->subj_hash && h->env->real_subj)
1320         hash_insert (ctx->subj_hash, h->env->real_subj, h, 1);
1321
1322       if (option (OPTSCORE))
1323         mutt_score_message (ctx, h, 0);
1324     }
1325
1326     if (h->changed)
1327       ctx->changed = 1;
1328     if (h->flagged)
1329       ctx->flagged++;
1330     if (h->deleted)
1331       ctx->deleted++;
1332     if (!h->read) {
1333       ctx->unread++;
1334       if (!h->old)
1335         ctx->new++;
1336     }
1337   }
1338   /* update sidebar count */
1339   sidebar_set_buffystats (ctx);
1340 }
1341
1342 /*
1343  * Return:
1344  * 1 if the specified mailbox contains 0 messages.
1345  * 0 if the mailbox contains messages
1346  * -1 on error
1347  */
1348 int mx_check_empty (const char *path)
1349 {
1350   int i = 0;
1351   if ((i = mx_get_idx (path)) >= 0 && MX_COMMAND(i,mx_check_empty))
1352     return (MX_COMMAND(i,mx_check_empty)(path));
1353   errno = EINVAL;
1354   return (-1);
1355 }
1356
1357 int mx_acl_check (CONTEXT* ctx, int flag) {
1358   if (!ctx || !MX_IDX(ctx->magic-1))
1359     return (0);
1360   /* if no acl_check defined for module, assume permission is granted */
1361   if (!MX_COMMAND(ctx->magic-1,mx_acl_check))
1362     return (1);
1363   return (MX_COMMAND(ctx->magic-1,mx_acl_check)(ctx,flag));
1364 }
1365
1366 void mx_init (void) {
1367 #ifdef DEBUG
1368   int i = 0;
1369 #endif
1370   list_push_back (&MailboxFormats, (void*) mbox_reg_mx ());
1371   list_push_back (&MailboxFormats, (void*) mmdf_reg_mx ());
1372   list_push_back (&MailboxFormats, (void*) mh_reg_mx ());
1373   list_push_back (&MailboxFormats, (void*) maildir_reg_mx ());
1374   list_push_back (&MailboxFormats, (void*) imap_reg_mx ());
1375   list_push_back (&MailboxFormats, (void*) pop_reg_mx ());
1376 #ifdef USE_NNTP
1377   list_push_back (&MailboxFormats, (void*) nntp_reg_mx ());
1378 #endif
1379   list_push_back (&MailboxFormats, (void*) compress_reg_mx ());
1380 #ifdef DEBUG
1381   /* check module registration for completeness with debug versions */
1382 #define EXITWITHERR(m) do { fprintf(stderr, "error: incomplete mx module: %s is missing for type %i\n",m,i);exit(1); } while (0)
1383   for (i = 0; i < MailboxFormats->length; i++) {
1384     if (MX_COMMAND(i,type) < 1)         EXITWITHERR("type");
1385     if (!MX_COMMAND(i,mx_is_magic))     EXITWITHERR("mx_is_magic");
1386     if (!MX_COMMAND(i,mx_open_mailbox)) EXITWITHERR("mx_open_mailbox");
1387 /*    if (!MX_COMMAND(i,mx_sync_mailbox)) EXITWITHERR("mx_sync_mailbox");*/
1388   }
1389 #undef EXITWITHERR
1390 #endif /* DEBUG */
1391 }
1392
1393 int mx_rebuild_cache (void) {
1394 #ifndef USE_HCACHE
1395   mutt_error (_("Support for header caching was not build in."));
1396   return (1);
1397 #else
1398   int i = 0, magic = 0;
1399   CONTEXT* ctx = NULL;
1400   BUFFY* b = NULL;
1401
1402   if (list_empty(Incoming)) {
1403     mutt_error (_("No mailboxes defined."));
1404     return (1);
1405   }
1406
1407   for (i = 0; i < Incoming->length; i++) {
1408     b = (BUFFY*) Incoming->data[i];
1409     magic = mx_get_magic (b->path);
1410     if (magic != M_MAILDIR && magic != M_MH && magic != M_IMAP)
1411       continue;
1412     sidebar_set_current (b->path);
1413     sidebar_draw (CurrentMenu);
1414     if ((ctx = mx_open_mailbox (b->path,
1415                                 M_READONLY | M_NOSORT | M_COUNT,
1416                                 NULL)) != NULL)
1417       mx_close_mailbox (ctx, 0);
1418   }
1419   mutt_clear_error ();
1420
1421   if (Context && Context->path)
1422     sidebar_set_current (Context->path);
1423   sidebar_draw (CurrentMenu);
1424
1425   return (0);
1426 #endif
1427 }
1428
1429 void mutt_parse_mime_message (CONTEXT * ctx, HEADER * cur)
1430 {
1431   MESSAGE *msg;
1432   int flags = 0;
1433
1434   do {
1435     if (cur->content->type != TYPEMESSAGE
1436         && cur->content->type != TYPEMULTIPART)
1437       break;                     /* nothing to do */
1438
1439     if (cur->content->parts)
1440       break;                     /* The message was parsed earlier. */
1441
1442     if ((msg = mx_open_message (ctx, cur->msgno))) {
1443       mutt_parse_part (msg->fp, cur->content);
1444
1445       cur->security = crypt_query (cur->content);
1446
1447       mx_close_message (&msg);
1448     }
1449   } while (0);
1450   mutt_count_body_parts (cur, flags | M_PARTS_RECOUNT);
1451 }