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