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