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