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