54548b07fe3ec4506cbdfb08495e7446ee3bdc85
[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 #ifdef USE_IMAP
1098 int imap_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr)
1099 {
1100   char tmp[_POSIX_PATH_MAX];
1101
1102   mutt_mktemp (tmp);
1103   if ((msg->fp = safe_fopen (tmp, "w")) == NULL)
1104     return (-1);
1105   msg->path = safe_strdup (tmp);
1106   return 0;
1107 }
1108 #endif
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   int (*func) (MESSAGE *, CONTEXT *, HEADER *);
1119   ADDRESS *p = NULL;
1120
1121   switch (dest->magic) {
1122   case M_MMDF:
1123   case M_MBOX:
1124     func = mbox_open_new_message;
1125     break;
1126   case M_MAILDIR:
1127     func = maildir_open_new_message;
1128     break;
1129   case M_MH:
1130     func = mh_open_new_message;
1131     break;
1132 #ifdef USE_IMAP
1133   case M_IMAP:
1134     func = imap_open_new_message;
1135     break;
1136 #endif
1137   default:
1138     dprint (1,
1139             (debugfile,
1140              "mx_open_new_message(): function unimplemented for mailbox type %d.\n",
1141              dest->magic));
1142     return (NULL);
1143   }
1144
1145   msg = safe_calloc (1, sizeof (MESSAGE));
1146   msg->magic = dest->magic;
1147   msg->write = 1;
1148
1149   if (hdr) {
1150     msg->flags.flagged = hdr->flagged;
1151     msg->flags.replied = hdr->replied;
1152     msg->flags.read = hdr->read;
1153     msg->received = hdr->received;
1154   }
1155
1156   if (msg->received == 0)
1157     time (&msg->received);
1158
1159   if (func (msg, dest, hdr) == 0) {
1160     if (dest->magic == M_MMDF)
1161       fputs (MMDF_SEP, msg->fp);
1162
1163     if ((msg->magic == M_MBOX || msg->magic == M_MMDF) && flags & M_ADD_FROM) {
1164       if (hdr) {
1165         if (hdr->env->return_path)
1166           p = hdr->env->return_path;
1167         else if (hdr->env->sender)
1168           p = hdr->env->sender;
1169         else
1170           p = hdr->env->from;
1171       }
1172
1173       fprintf (msg->fp, "From %s %s", p ? p->mailbox : NONULL (Username),
1174                ctime (&msg->received));
1175     }
1176   }
1177   else
1178     FREE (&msg);
1179
1180   return msg;
1181 }
1182
1183 /* check for new mail */
1184 int mx_check_mailbox (CONTEXT * ctx, int *index_hint, int lock)
1185 {
1186   int rc;
1187
1188 #ifdef USE_COMPRESSED
1189   if (ctx->compressinfo)
1190     return mutt_check_mailbox_compressed (ctx);
1191 #endif
1192
1193   if (ctx) {
1194     if (ctx->locked)
1195       lock = 0;
1196
1197     switch (ctx->magic) {
1198     case M_MBOX:
1199     case M_MMDF:
1200
1201       if (lock) {
1202         mutt_block_signals ();
1203         if (mbox_lock_mailbox (ctx, 0, 0) == -1) {
1204           mutt_unblock_signals ();
1205           return M_LOCKED;
1206         }
1207       }
1208
1209       rc = mbox_check_mailbox (ctx, index_hint);
1210
1211       if (lock) {
1212         mutt_unblock_signals ();
1213         mbox_unlock_mailbox (ctx);
1214       }
1215
1216       return rc;
1217
1218
1219     case M_MH:
1220       return (mh_check_mailbox (ctx, index_hint));
1221     case M_MAILDIR:
1222       return (maildir_check_mailbox (ctx, index_hint));
1223
1224 #ifdef USE_IMAP
1225     case M_IMAP:
1226       return (imap_check_mailbox (ctx, index_hint, 0));
1227 #endif /* USE_IMAP */
1228
1229 #ifdef USE_POP
1230     case M_POP:
1231       return (pop_check_mailbox (ctx, index_hint));
1232 #endif /* USE_POP */
1233
1234 #ifdef USE_NNTP
1235     case M_NNTP:
1236       return (nntp_check_mailbox (ctx));
1237 #endif /* USE_NNTP */
1238     }
1239   }
1240
1241   dprint (1, (debugfile, "mx_check_mailbox: null or invalid context.\n"));
1242   return (-1);
1243 }
1244
1245 /* return a stream pointer for a message */
1246 MESSAGE *mx_open_message (CONTEXT * ctx, int msgno)
1247 {
1248   MESSAGE *msg;
1249
1250   msg = safe_calloc (1, sizeof (MESSAGE));
1251   switch (msg->magic = ctx->magic) {
1252   case M_MBOX:
1253   case M_MMDF:
1254     msg->fp = ctx->fp;
1255     break;
1256
1257   case M_MH:
1258   case M_MAILDIR:
1259     {
1260       HEADER *cur = ctx->hdrs[msgno];
1261       char path[_POSIX_PATH_MAX];
1262
1263       snprintf (path, sizeof (path), "%s/%s", ctx->path, cur->path);
1264
1265       if ((msg->fp = fopen (path, "r")) == NULL && errno == ENOENT &&
1266           ctx->magic == M_MAILDIR)
1267         msg->fp = maildir_open_find_message (ctx->path, cur->path);
1268
1269       if (msg->fp == NULL) {
1270         mutt_perror (path);
1271         dprint (1, (debugfile, "mx_open_message: fopen: %s: %s (errno %d).\n",
1272                     path, strerror (errno), errno));
1273         FREE (&msg);
1274       }
1275     }
1276     break;
1277
1278 #ifdef USE_IMAP
1279   case M_IMAP:
1280     {
1281       if (imap_fetch_message (msg, ctx, msgno) != 0)
1282         FREE (&msg);
1283       break;
1284     }
1285 #endif /* USE_IMAP */
1286
1287 #ifdef USE_POP
1288   case M_POP:
1289     {
1290       if (pop_fetch_message (msg, ctx, msgno) != 0)
1291         FREE (&msg);
1292       break;
1293     }
1294 #endif /* USE_POP */
1295
1296 #ifdef USE_NNTP
1297   case M_NNTP:
1298     {
1299       if (nntp_fetch_message (msg, ctx, msgno) != 0)
1300         FREE (&msg);
1301       break;
1302     }
1303 #endif /* USE_NNTP */
1304
1305   default:
1306     dprint (1,
1307             (debugfile,
1308              "mx_open_message(): function not implemented for mailbox type %d.\n",
1309              ctx->magic));
1310     FREE (&msg);
1311     break;
1312   }
1313   return (msg);
1314 }
1315
1316 /* commit a message to a folder */
1317
1318 int mx_commit_message (MESSAGE * msg, CONTEXT * ctx)
1319 {
1320   int r = 0;
1321
1322   if (!(msg->write && ctx->append)) {
1323     dprint (1,
1324             (debugfile,
1325              "mx_commit_message(): msg->write = %d, ctx->append = %d\n",
1326              msg->write, ctx->append));
1327     return -1;
1328   }
1329
1330   switch (msg->magic) {
1331   case M_MMDF:
1332     {
1333       if (fputs (MMDF_SEP, msg->fp) == EOF)
1334         r = -1;
1335       break;
1336     }
1337
1338   case M_MBOX:
1339     {
1340       if (fputc ('\n', msg->fp) == EOF)
1341         r = -1;
1342       break;
1343     }
1344
1345 #ifdef USE_IMAP
1346   case M_IMAP:
1347     {
1348       if ((r = safe_fclose (&msg->fp)) == 0)
1349         r = imap_append_message (ctx, msg);
1350       break;
1351     }
1352 #endif
1353
1354   case M_MAILDIR:
1355     {
1356       r = maildir_commit_message (ctx, msg, NULL);
1357       break;
1358     }
1359
1360   case M_MH:
1361     {
1362       r = mh_commit_message (ctx, msg, NULL);
1363       break;
1364     }
1365   }
1366
1367   if (r == 0 && (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
1368       && (fflush (msg->fp) == EOF || fsync (fileno (msg->fp)) == -1)) {
1369     mutt_perror (_("Can't write message"));
1370
1371     r = -1;
1372   }
1373
1374   return r;
1375 }
1376
1377 /* close a pointer to a message */
1378 int mx_close_message (MESSAGE ** msg)
1379 {
1380   int r = 0;
1381
1382   if ((*msg)->magic == M_MH || (*msg)->magic == M_MAILDIR
1383 #ifdef USE_IMAP
1384       || (*msg)->magic == M_IMAP
1385 #endif
1386 #ifdef USE_POP
1387       || (*msg)->magic == M_POP
1388 #endif
1389 #ifdef USE_NNTP
1390       || (*msg)->magic == M_NNTP
1391 #endif
1392     ) {
1393     r = safe_fclose (&(*msg)->fp);
1394   }
1395   else
1396     (*msg)->fp = NULL;
1397
1398   if ((*msg)->path) {
1399     dprint (1, (debugfile, "mx_close_message (): unlinking %s\n",
1400                 (*msg)->path));
1401     unlink ((*msg)->path);
1402     FREE (&(*msg)->path);
1403   }
1404
1405   FREE (msg);
1406   return (r);
1407 }
1408
1409 void mx_alloc_memory (CONTEXT * ctx)
1410 {
1411   int i;
1412   size_t s = MAX (sizeof (HEADER *), sizeof (int));
1413
1414   if ((ctx->hdrmax + 25) * s < ctx->hdrmax * s) {
1415     mutt_error _("Integer overflow -- can't allocate memory.");
1416
1417     sleep (1);
1418     mutt_exit (1);
1419   }
1420
1421   if (ctx->hdrs) {
1422     safe_realloc (&ctx->hdrs, sizeof (HEADER *) * (ctx->hdrmax += 25));
1423     safe_realloc (&ctx->v2r, sizeof (int) * ctx->hdrmax);
1424   }
1425   else {
1426     ctx->hdrs = safe_calloc ((ctx->hdrmax += 25), sizeof (HEADER *));
1427     ctx->v2r = safe_calloc (ctx->hdrmax, sizeof (int));
1428   }
1429   for (i = ctx->msgcount; i < ctx->hdrmax; i++) {
1430     ctx->hdrs[i] = NULL;
1431     ctx->v2r[i] = -1;
1432   }
1433 }
1434
1435 /* this routine is called to update the counts in the context structure for
1436  * the last message header parsed.
1437  */
1438 void mx_update_context (CONTEXT * ctx, int new_messages)
1439 {
1440   HEADER *h;
1441   int msgno;
1442
1443   for (msgno = ctx->msgcount - new_messages; msgno < ctx->msgcount; msgno++) {
1444     h = ctx->hdrs[msgno];
1445
1446     if (WithCrypto) {
1447       /* NOTE: this _must_ be done before the check for mailcap! */
1448       h->security = crypt_query (h->content);
1449     }
1450
1451     if (!ctx->pattern) {
1452       ctx->v2r[ctx->vcount] = msgno;
1453       h->virtual = ctx->vcount++;
1454     }
1455     else
1456       h->virtual = -1;
1457     h->msgno = msgno;
1458
1459     if (h->env->supersedes) {
1460       HEADER *h2;
1461
1462       if (!ctx->id_hash)
1463         ctx->id_hash = mutt_make_id_hash (ctx);
1464
1465       h2 = hash_find (ctx->id_hash, h->env->supersedes);
1466
1467       /* FREE (&h->env->supersedes); should I ? */
1468       if (h2) {
1469         h2->superseded = 1;
1470         if (!ctx->counting && option (OPTSCORE))
1471           mutt_score_message (ctx, h2, 1);
1472       }
1473     }
1474
1475     /* add this message to the hash tables */
1476     if (ctx->id_hash && h->env->message_id)
1477       hash_insert (ctx->id_hash, h->env->message_id, h, 0);
1478     if (!ctx->counting) {
1479       if (ctx->subj_hash && h->env->real_subj)
1480         hash_insert (ctx->subj_hash, h->env->real_subj, h, 1);
1481
1482       if (option (OPTSCORE))
1483         mutt_score_message (ctx, h, 0);
1484     }
1485
1486     if (h->changed)
1487       ctx->changed = 1;
1488     if (h->flagged)
1489       ctx->flagged++;
1490     if (h->deleted)
1491       ctx->deleted++;
1492     if (!h->read) {
1493       ctx->unread++;
1494       if (!h->old)
1495         ctx->new++;
1496     }
1497   }
1498 }
1499
1500 /*
1501  * Return:
1502  * 1 if the specified mailbox contains 0 messages.
1503  * 0 if the mailbox contains messages
1504  * -1 on error
1505  */
1506 int mx_check_empty (const char *path)
1507 {
1508   int i = 0;
1509   if ((i = mx_get_idx (path)) >= 0 && MX_COMMAND(i,mx_check_empty))
1510     return (MX_COMMAND(i,mx_check_empty)(path));
1511   errno = EINVAL;
1512   return (-1);
1513 }
1514
1515 int mx_acl_check (CONTEXT* ctx, int flag) {
1516   if (!ctx || !MX_IDX(ctx->magic-1))
1517     return (0);
1518   /* if no acl_check defined for module, assume permission is granted */
1519   if (!MX_COMMAND(ctx->magic-1,mx_acl_check))
1520     return (1);
1521   return (MX_COMMAND(ctx->magic-1,mx_acl_check)(ctx,flag));
1522 }
1523
1524 void mx_init (void) {
1525 #ifdef DEBUG
1526   int i = 0;
1527 #endif
1528   list_push_back (&MailboxFormats, (void*) mbox_reg_mx ());
1529   list_push_back (&MailboxFormats, (void*) mmdf_reg_mx ());
1530   list_push_back (&MailboxFormats, (void*) mh_reg_mx ());
1531   list_push_back (&MailboxFormats, (void*) maildir_reg_mx ());
1532 #ifdef USE_IMAP
1533   list_push_back (&MailboxFormats, (void*) imap_reg_mx ());
1534 #endif
1535 #ifdef USE_POP
1536   list_push_back (&MailboxFormats, (void*) pop_reg_mx ());
1537 #endif
1538 #ifdef USE_NNTP
1539   list_push_back (&MailboxFormats, (void*) nntp_reg_mx ());
1540 #endif
1541 #ifdef USE_COMPRESSED
1542   list_push_back (&MailboxFormats, (void*) compress_reg_mx ());
1543 #endif
1544 #ifdef DEBUG
1545   /* check module registration for completeness with debug versions */
1546 #define EXITWITHERR(m) do { fprintf(stderr, "error: incomplete mx module: %s is missing for type %i\n",m,i);exit(1); } while (0)
1547   for (i = 0; i < MailboxFormats->length; i++) {
1548     if (MX_COMMAND(i,type) < 1)         EXITWITHERR("type");
1549     if (!MX_COMMAND(i,mx_is_magic))     EXITWITHERR("mx_is_magic");
1550     if (!MX_COMMAND(i,mx_open_mailbox)) EXITWITHERR("mx_open_mailbox");
1551 /*    if (!MX_COMMAND(i,mx_sync_mailbox)) EXITWITHERR("mx_sync_mailbox");*/
1552   }
1553 #undef EXITWITHERR
1554 #endif /* DEBUG */
1555 }