]> granicus.if.org Git - neomutt/blob - mx.c
Fix mutt_write_mime_body() application/pgp-encrypted handling
[neomutt] / mx.c
1 /**
2  * @file
3  * Mailbox multiplexor
4  *
5  * @authors
6  * Copyright (C) 1996-2002,2010,2013 Michael R. Elkins <me@mutt.org>
7  * Copyright (C) 1999-2003 Thomas Roessler <roessler@does-not-exist.org>
8  * Copyright (C) 2016-2018 Richard Russon <rich@flatcap.org>
9  * Copyright (C) 2019 Pietro Cerutti <gahr@gahr.ch>
10  *
11  * @copyright
12  * This program is free software: you can redistribute it and/or modify it under
13  * the terms of the GNU General Public License as published by the Free Software
14  * Foundation, either version 2 of the License, or (at your option) any later
15  * version.
16  *
17  * This program is distributed in the hope that it will be useful, but WITHOUT
18  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
20  * details.
21  *
22  * You should have received a copy of the GNU General Public License along with
23  * this program.  If not, see <http://www.gnu.org/licenses/>.
24  */
25
26 /**
27  * @page mx Mailbox multiplexor
28  *
29  * Mailbox multiplexor
30  */
31
32 #include "config.h"
33 #include <errno.h>
34 #include <limits.h>
35 #include <stdbool.h>
36 #include <string.h>
37 #include <sys/stat.h>
38 #include <unistd.h>
39 #include "mutt/mutt.h"
40 #include "address/lib.h"
41 #include "email/lib.h"
42 #include "core/lib.h"
43 #include "mutt.h"
44 #include "mx.h"
45 #include "alias.h"
46 #include "context.h"
47 #include "copy.h"
48 #include "globals.h"
49 #include "hook.h"
50 #include "keymap.h"
51 #include "maildir/lib.h"
52 #include "mbox/mbox.h"
53 #include "mutt_header.h"
54 #include "mutt_logging.h"
55 #include "mutt_mailbox.h"
56 #include "muttlib.h"
57 #include "opcodes.h"
58 #include "options.h"
59 #include "protos.h"
60 #include "sort.h"
61 #ifdef USE_COMPRESSED
62 #include "compress.h"
63 #endif
64 #ifdef USE_IMAP
65 #include "imap/imap.h"
66 #endif
67 #ifdef USE_POP
68 #include "pop/pop.h"
69 #endif
70 #ifdef USE_NNTP
71 #include "nntp/nntp.h"
72 #endif
73 #ifdef USE_NOTMUCH
74 #include "notmuch/mutt_notmuch.h"
75 #endif
76 #ifdef ENABLE_NLS
77 #include <libintl.h>
78 #endif
79
80 /* These Config Variables are only used in mx.c */
81 unsigned char C_CatchupNewsgroup; ///< Config: (nntp) Mark all articles as read when leaving a newsgroup
82 bool C_KeepFlagged; ///< Config: Don't move flagged messages from #C_Spoolfile to #C_Mbox
83 unsigned char C_MboxType; ///< Config: Default type for creating new mailboxes
84 unsigned char C_Move; ///< Config: Move emails from #C_Spoolfile to #C_Mbox when read
85 char *C_Trash;        ///< Config: Folder to put deleted emails
86
87 // clang-format off
88 static struct Mapping MagicMap[] = {
89   { "mbox",    MUTT_MBOX,    },
90   { "MMDF",    MUTT_MMDF,    },
91   { "MH",      MUTT_MH,      },
92   { "Maildir", MUTT_MAILDIR, },
93   { NULL,      0,            },
94 };
95 // clang-format on
96
97 struct EnumDef MagicDef = {
98   "mbox_type",
99   4,
100   (struct Mapping *) &MagicMap,
101 };
102
103 /**
104  * mx_ops - All the Mailbox backends
105  */
106 static const struct MxOps *mx_ops[] = {
107 /* These mailboxes can be recognised by their Url scheme */
108 #ifdef USE_IMAP
109   &MxImapOps,
110 #endif
111 #ifdef USE_NOTMUCH
112   &MxNotmuchOps,
113 #endif
114 #ifdef USE_POP
115   &MxPopOps,
116 #endif
117 #ifdef USE_NNTP
118   &MxNntpOps,
119 #endif
120
121   /* Local mailboxes */
122   &MxMaildirOps,
123   &MxMboxOps,
124   &MxMhOps,
125   &MxMmdfOps,
126
127 /* If everything else fails... */
128 #ifdef USE_COMPRESSED
129   &MxCompOps,
130 #endif
131   NULL,
132 };
133
134 /**
135  * mx_get_ops - Get mailbox operations
136  * @param magic Mailbox magic number
137  * @retval ptr  Mailbox function
138  * @retval NULL Error
139  */
140 const struct MxOps *mx_get_ops(enum MailboxType magic)
141 {
142   for (const struct MxOps **ops = mx_ops; *ops; ops++)
143     if ((*ops)->magic == magic)
144       return *ops;
145
146   return NULL;
147 }
148
149 /**
150  * mutt_is_spool - Is this the spoolfile?
151  * @param str Name to check
152  * @retval true It is the spoolfile
153  */
154 static bool mutt_is_spool(const char *str)
155 {
156   return mutt_str_strcmp(C_Spoolfile, str) == 0;
157 }
158
159 /**
160  * mx_access - Wrapper for access, checks permissions on a given mailbox
161  * @param path  Path of mailbox
162  * @param flags Flags, e.g. W_OK
163  * @retval  0 Success, allowed
164  * @retval <0 Failure, not allowed
165  *
166  * We may be interested in using ACL-style flags at some point, currently we
167  * use the normal access() flags.
168  */
169 int mx_access(const char *path, int flags)
170 {
171 #ifdef USE_IMAP
172   if (imap_path_probe(path, NULL) == MUTT_IMAP)
173     return imap_access(path);
174 #endif
175
176   return access(path, flags);
177 }
178
179 /**
180  * mx_open_mailbox_append - Open a mailbox for appending
181  * @param m     Mailbox
182  * @param flags Flags, see #OpenMailboxFlags
183  * @retval  0 Success
184  * @retval -1 Failure
185  */
186 static int mx_open_mailbox_append(struct Mailbox *m, OpenMailboxFlags flags)
187 {
188   if (!m)
189     return -1;
190
191   struct stat sb;
192
193   m->append = true;
194   if ((m->magic == MUTT_UNKNOWN) || (m->magic == MUTT_MAILBOX_ERROR))
195   {
196     m->magic = mx_path_probe(mailbox_path(m), NULL);
197
198     if (m->magic == MUTT_UNKNOWN)
199     {
200       if (flags & (MUTT_APPEND | MUTT_NEWFOLDER))
201       {
202         m->magic = MUTT_MAILBOX_ERROR;
203       }
204       else
205       {
206         mutt_error(_("%s is not a mailbox"), mailbox_path(m));
207         return -1;
208       }
209     }
210
211     if (m->magic == MUTT_MAILBOX_ERROR)
212     {
213       if (stat(mailbox_path(m), &sb) == -1)
214       {
215         if (errno == ENOENT)
216         {
217 #ifdef USE_COMPRESSED
218           if (mutt_comp_can_append(m))
219             m->magic = MUTT_COMPRESSED;
220           else
221 #endif
222             m->magic = C_MboxType;
223           flags |= MUTT_APPENDNEW;
224         }
225         else
226         {
227           mutt_perror(mailbox_path(m));
228           return -1;
229         }
230       }
231       else
232         return -1;
233     }
234
235     m->mx_ops = mx_get_ops(m->magic);
236   }
237
238   if (!m->mx_ops || !m->mx_ops->mbox_open_append)
239     return -1;
240
241   int rc = m->mx_ops->mbox_open_append(m, flags);
242   m->opened++;
243   return rc;
244 }
245
246 /**
247  * mx_mbox_open - Open a mailbox and parse it
248  * @param m     Mailbox to open
249  * @param flags Flags, see #OpenMailboxFlags
250  * @retval ptr  Mailbox context
251  * @retval NULL Error
252  */
253 struct Context *mx_mbox_open(struct Mailbox *m, OpenMailboxFlags flags)
254 {
255   if (!m)
256     return NULL;
257
258   struct Context *ctx = ctx_new();
259   ctx->mailbox = m;
260
261   struct EventContext ev_ctx = { ctx };
262   notify_send(ctx->notify, NT_CONTEXT, NT_CONTEXT_OPEN, IP & ev_ctx);
263
264   // If the Mailbox is closed, Context->mailbox must be set to NULL
265   notify_observer_add(m->notify, NT_MAILBOX, 0, ctx_mailbox_observer, IP ctx);
266
267   if ((m->magic == MUTT_UNKNOWN) && (flags & (MUTT_NEWFOLDER | MUTT_APPEND)))
268   {
269     m->magic = C_MboxType;
270     m->mx_ops = mx_get_ops(m->magic);
271   }
272
273   if (!m->account)
274   {
275     struct Account *a = mx_ac_find(m);
276     bool new_account = false;
277     if (!a)
278     {
279       a = account_new(NULL, NeoMutt->sub);
280       a->magic = m->magic;
281       new_account = true;
282     }
283     if (mx_ac_add(a, m) < 0)
284     {
285       ctx_free(&ctx);
286       if (new_account)
287       {
288         FREE(&a);
289       }
290       return NULL;
291     }
292     if (new_account)
293     {
294       neomutt_account_add(NeoMutt, a);
295     }
296   }
297
298   ctx->msg_not_read_yet = -1;
299   ctx->collapsed = false;
300
301   m->size = 0;
302   m->msg_unread = 0;
303   m->msg_flagged = 0;
304   m->rights = MUTT_ACL_ALL;
305
306   m->quiet = (flags & MUTT_QUIET);
307   if (flags & MUTT_READONLY)
308     m->readonly = true;
309   m->peekonly = (flags & MUTT_PEEK);
310
311   if (flags & (MUTT_APPEND | MUTT_NEWFOLDER))
312   {
313     if (mx_open_mailbox_append(ctx->mailbox, flags) != 0)
314     {
315       mx_fastclose_mailbox(m);
316       ctx_free(&ctx);
317       return NULL;
318     }
319     return ctx;
320   }
321
322   if (m->magic == MUTT_UNKNOWN)
323   {
324     m->magic = mx_path_probe(mailbox_path(m), NULL);
325     m->mx_ops = mx_get_ops(m->magic);
326   }
327
328   if ((m->magic == MUTT_UNKNOWN) || (m->magic == MUTT_MAILBOX_ERROR) || !m->mx_ops)
329   {
330     if (m->magic == MUTT_MAILBOX_ERROR)
331       mutt_perror(mailbox_path(m));
332     else if ((m->magic == MUTT_UNKNOWN) || !m->mx_ops)
333       mutt_error(_("%s is not a mailbox"), mailbox_path(m));
334
335     mx_fastclose_mailbox(m);
336     ctx_free(&ctx);
337     return NULL;
338   }
339
340   mutt_make_label_hash(m);
341
342   /* if the user has a 'push' command in their .neomuttrc, or in a folder-hook,
343    * it will cause the progress messages not to be displayed because
344    * mutt_refresh() will think we are in the middle of a macro.  so set a
345    * flag to indicate that we should really refresh the screen.  */
346   OptForceRefresh = true;
347
348   if (!m->quiet)
349     mutt_message(_("Reading %s..."), mailbox_path(m));
350
351   int rc = m->mx_ops->mbox_open(ctx->mailbox);
352   m->opened++;
353   if (rc == 0)
354     ctx_update(ctx);
355
356   if ((rc == 0) || (rc == -2))
357   {
358     if ((flags & MUTT_NOSORT) == 0)
359     {
360       /* avoid unnecessary work since the mailbox is completely unthreaded
361        * to begin with */
362       OptSortSubthreads = false;
363       OptNeedRescore = false;
364     }
365     if (!m->quiet)
366       mutt_clear_error();
367     if (rc == -2)
368     {
369       mutt_error(_("Reading from %s interrupted..."), mailbox_path(m));
370       mutt_sort_headers(ctx, true);
371     }
372   }
373   else
374   {
375     mx_fastclose_mailbox(m);
376     ctx_free(&ctx);
377     return NULL;
378   }
379
380   OptForceRefresh = false;
381
382   return ctx;
383 }
384
385 /**
386  * mx_fastclose_mailbox - free up memory associated with the Mailbox
387  * @param m Mailbox
388  */
389 void mx_fastclose_mailbox(struct Mailbox *m)
390 {
391   if (!m)
392     return;
393
394   m->opened--;
395   if (m->opened != 0)
396     return;
397
398   /* never announce that a mailbox we've just left has new mail. #3290
399    * TODO: really belongs in mx_mbox_close, but this is a nice hook point */
400   if (!m->peekonly)
401     mutt_mailbox_set_notified(m);
402
403   if (m->mx_ops)
404     m->mx_ops->mbox_close(m);
405
406   mailbox_changed(m, MBN_CLOSED);
407
408   mutt_hash_free(&m->subj_hash);
409   mutt_hash_free(&m->id_hash);
410   mutt_hash_free(&m->label_hash);
411
412   if (m->emails)
413   {
414     for (int i = 0; i < m->msg_count; i++)
415       email_free(&m->emails[i]);
416     FREE(&m->emails);
417   }
418   FREE(&m->v2r);
419 }
420
421 /**
422  * sync_mailbox - save changes to disk
423  * @param m          Mailbox
424  * @param index_hint Current email
425  * @retval  0 Success
426  * @retval -1 Failure
427  */
428 static int sync_mailbox(struct Mailbox *m, int *index_hint)
429 {
430   if (!m || !m->mx_ops || !m->mx_ops->mbox_sync)
431     return -1;
432
433   if (!m->quiet)
434   {
435     /* L10N: Displayed before/as a mailbox is being synced */
436     mutt_message(_("Writing %s..."), mailbox_path(m));
437   }
438
439   int rc = m->mx_ops->mbox_sync(m, index_hint);
440   if ((rc != 0) && !m->quiet)
441   {
442     /* L10N: Displayed if a mailbox sync fails */
443     mutt_error(_("Unable to write %s"), mailbox_path(m));
444   }
445
446   return rc;
447 }
448
449 /**
450  * trash_append - move deleted mails to the trash folder
451  * @param m Mailbox
452  * @retval  0 Success
453  * @retval -1 Failure
454  */
455 static int trash_append(struct Mailbox *m)
456 {
457   if (!m)
458     return -1;
459
460   struct stat st, stc;
461   int opt_confappend, rc;
462
463   if (!C_Trash || (m->msg_deleted == 0) || ((m->magic == MUTT_MAILDIR) && C_MaildirTrash))
464   {
465     return 0;
466   }
467
468   int delmsgcount = 0;
469   int first_del = -1;
470   for (int i = 0; i < m->msg_count; i++)
471   {
472     if (m->emails[i]->deleted && (!m->emails[i]->purge))
473     {
474       if (first_del < 0)
475         first_del = i;
476       delmsgcount++;
477     }
478   }
479
480   if (delmsgcount == 0)
481     return 0; /* nothing to be done */
482
483   /* avoid the "append messages" prompt */
484   opt_confappend = C_Confirmappend;
485   if (opt_confappend)
486     C_Confirmappend = false;
487   rc = mutt_save_confirm(C_Trash, &st);
488   if (opt_confappend)
489     C_Confirmappend = true;
490   if (rc != 0)
491   {
492     /* L10N: Although we know the precise number of messages, we do not show it to the user.
493        So feel free to use a "generic plural" as plural translation if your language has one. */
494     mutt_error(ngettext("message not deleted", "messages not deleted", delmsgcount));
495     return -1;
496   }
497
498   if ((lstat(mailbox_path(m), &stc) == 0) && (stc.st_ino == st.st_ino) &&
499       (stc.st_dev == st.st_dev) && (stc.st_rdev == st.st_rdev))
500   {
501     return 0; /* we are in the trash folder: simple sync */
502   }
503
504 #ifdef USE_IMAP
505   if ((m->magic == MUTT_IMAP) && (imap_path_probe(C_Trash, NULL) == MUTT_IMAP))
506   {
507     if (imap_fast_trash(m, C_Trash) == 0)
508       return 0;
509   }
510 #endif
511
512   struct Mailbox *m_trash = mx_path_resolve(C_Trash);
513   struct Context *ctx_trash = mx_mbox_open(m_trash, MUTT_OPEN_NO_FLAGS);
514   if (ctx_trash)
515   {
516     bool old_append = m_trash->append;
517     m_trash->append = true;
518
519     /* continue from initial scan above */
520     for (int i = first_del; i < m->msg_count; i++)
521     {
522       if (m->emails[i]->deleted && (!m->emails[i]->purge))
523       {
524         if (mutt_append_message(ctx_trash->mailbox, m, m->emails[i],
525                                 MUTT_CM_NO_FLAGS, CH_NO_FLAGS) == -1)
526         {
527           m_trash->append = old_append;
528           mx_mbox_close(&ctx_trash);
529           return -1;
530         }
531       }
532     }
533
534     m_trash->append = old_append;
535     mx_mbox_close(&ctx_trash);
536   }
537   else
538   {
539     mutt_error(_("Can't open trash folder"));
540     mailbox_free(&m_trash);
541     return -1;
542   }
543
544   return 0;
545 }
546
547 /**
548  * mx_mbox_close - Save changes and close mailbox
549  * @param[out] ptr Mailbox
550  * @retval  0 Success
551  * @retval -1 Failure
552  *
553  * @note Context will be freed after it's closed
554  */
555 int mx_mbox_close(struct Context **ptr)
556 {
557   if (!ptr || !*ptr)
558     return 0;
559
560   struct Context *ctx = *ptr;
561   if (!ctx || !ctx->mailbox)
562     return -1;
563
564   struct Mailbox *m = ctx->mailbox;
565
566   int i, read_msgs = 0;
567   enum QuadOption move_messages = MUTT_NO;
568   enum QuadOption purge = MUTT_YES;
569   char mbox[PATH_MAX];
570   char buf[PATH_MAX + 64];
571
572   if (m->readonly || m->dontwrite || m->append)
573   {
574     mx_fastclose_mailbox(m);
575     ctx_free(ptr);
576     return 0;
577   }
578
579 #ifdef USE_NNTP
580   if ((m->msg_unread != 0) && (m->magic == MUTT_NNTP))
581   {
582     struct NntpMboxData *mdata = m->mdata;
583
584     if (mdata && mdata->adata && mdata->group)
585     {
586       enum QuadOption ans =
587           query_quadoption(C_CatchupNewsgroup, _("Mark all articles read?"));
588       if (ans == MUTT_ABORT)
589         return -1;
590       else if (ans == MUTT_YES)
591         mutt_newsgroup_catchup(m, mdata->adata, mdata->group);
592     }
593   }
594 #endif
595
596   for (i = 0; i < m->msg_count; i++)
597   {
598     if (!m->emails[i])
599       break;
600     if (!m->emails[i]->deleted && m->emails[i]->read && !(m->emails[i]->flagged && C_KeepFlagged))
601     {
602       read_msgs++;
603     }
604   }
605
606 #ifdef USE_NNTP
607   /* don't need to move articles from newsgroup */
608   if (m->magic == MUTT_NNTP)
609     read_msgs = 0;
610 #endif
611
612   if ((read_msgs != 0) && (C_Move != MUTT_NO))
613   {
614     bool is_spool;
615     char *p = mutt_find_hook(MUTT_MBOX_HOOK, mailbox_path(m));
616     if (p)
617     {
618       is_spool = true;
619       mutt_str_strfcpy(mbox, p, sizeof(mbox));
620     }
621     else
622     {
623       mutt_str_strfcpy(mbox, C_Mbox, sizeof(mbox));
624       is_spool = mutt_is_spool(mailbox_path(m)) && !mutt_is_spool(mbox);
625     }
626
627     if (is_spool && (mbox[0] != '\0'))
628     {
629       mutt_expand_path(mbox, sizeof(mbox));
630       snprintf(buf, sizeof(buf),
631                /* L10N: The first argument is the number of read messages to be
632                   moved, the second argument is the target mailbox. */
633                ngettext("Move %d read message to %s?", "Move %d read messages to %s?", read_msgs),
634                read_msgs, mbox);
635       move_messages = query_quadoption(C_Move, buf);
636       if (move_messages == MUTT_ABORT)
637         return -1;
638     }
639   }
640
641   /* There is no point in asking whether or not to purge if we are
642    * just marking messages as "trash".  */
643   if ((m->msg_deleted != 0) && !((m->magic == MUTT_MAILDIR) && C_MaildirTrash))
644   {
645     snprintf(buf, sizeof(buf),
646              ngettext("Purge %d deleted message?", "Purge %d deleted messages?", m->msg_deleted),
647              m->msg_deleted);
648     purge = query_quadoption(C_Delete, buf);
649     if (purge == MUTT_ABORT)
650       return -1;
651   }
652
653   if (C_MarkOld)
654   {
655     for (i = 0; i < m->msg_count; i++)
656     {
657       if (!m->emails[i]->deleted && !m->emails[i]->old && !m->emails[i]->read)
658         mutt_set_flag(m, m->emails[i], MUTT_OLD, true);
659     }
660   }
661
662   if (move_messages)
663   {
664     if (!m->quiet)
665       mutt_message(_("Moving read messages to %s..."), mbox);
666
667 #ifdef USE_IMAP
668     /* try to use server-side copy first */
669     i = 1;
670
671     if ((m->magic == MUTT_IMAP) && (imap_path_probe(mbox, NULL) == MUTT_IMAP))
672     {
673       /* tag messages for moving, and clear old tags, if any */
674       for (i = 0; i < m->msg_count; i++)
675       {
676         if (m->emails[i]->read && !m->emails[i]->deleted &&
677             !(m->emails[i]->flagged && C_KeepFlagged))
678         {
679           m->emails[i]->tagged = true;
680         }
681         else
682         {
683           m->emails[i]->tagged = false;
684         }
685       }
686
687       i = imap_copy_messages(ctx->mailbox, NULL, mbox, true);
688     }
689
690     if (i == 0) /* success */
691       mutt_clear_error();
692     else if (i == -1) /* horrible error, bail */
693       return -1;
694     else /* use regular append-copy mode */
695 #endif
696     {
697       struct Mailbox *m_read = mx_path_resolve(mbox);
698       struct Context *ctx_read = mx_mbox_open(m_read, MUTT_APPEND);
699       if (!ctx_read)
700       {
701         mailbox_free(&m_read);
702         return -1;
703       }
704
705       for (i = 0; i < m->msg_count; i++)
706       {
707         if (m->emails[i]->read && !m->emails[i]->deleted &&
708             !(m->emails[i]->flagged && C_KeepFlagged))
709         {
710           if (mutt_append_message(ctx_read->mailbox, ctx->mailbox, m->emails[i],
711                                   MUTT_CM_NO_FLAGS, CH_UPDATE_LEN) == 0)
712           {
713             mutt_set_flag(m, m->emails[i], MUTT_DELETE, true);
714             mutt_set_flag(m, m->emails[i], MUTT_PURGE, true);
715           }
716           else
717           {
718             mx_mbox_close(&ctx_read);
719             return -1;
720           }
721         }
722       }
723
724       mx_mbox_close(&ctx_read);
725     }
726   }
727   else if (!m->changed && (m->msg_deleted == 0))
728   {
729     if (!m->quiet)
730       mutt_message(_("Mailbox is unchanged"));
731     if ((m->magic == MUTT_MBOX) || (m->magic == MUTT_MMDF))
732       mbox_reset_atime(m, NULL);
733     mx_fastclose_mailbox(m);
734     ctx_free(ptr);
735     return 0;
736   }
737
738   /* copy mails to the trash before expunging */
739   if (purge && (m->msg_deleted != 0) && (mutt_str_strcmp(mailbox_path(m), C_Trash) != 0))
740   {
741     if (trash_append(ctx->mailbox) != 0)
742       return -1;
743   }
744
745 #ifdef USE_IMAP
746   /* allow IMAP to preserve the deleted flag across sessions */
747   if (m->magic == MUTT_IMAP)
748   {
749     int check = imap_sync_mailbox(ctx->mailbox, (purge != MUTT_NO), true);
750     if (check != 0)
751       return check;
752   }
753   else
754 #endif
755   {
756     if (purge == MUTT_NO)
757     {
758       for (i = 0; i < m->msg_count; i++)
759       {
760         m->emails[i]->deleted = false;
761         m->emails[i]->purge = false;
762       }
763       m->msg_deleted = 0;
764     }
765
766     if (m->changed || (m->msg_deleted != 0))
767     {
768       int check = sync_mailbox(ctx->mailbox, NULL);
769       if (check != 0)
770         return check;
771     }
772   }
773
774   if (!m->quiet)
775   {
776     if (move_messages)
777     {
778       mutt_message(_("%d kept, %d moved, %d deleted"),
779                    m->msg_count - m->msg_deleted, read_msgs, m->msg_deleted);
780     }
781     else
782       mutt_message(_("%d kept, %d deleted"), m->msg_count - m->msg_deleted, m->msg_deleted);
783   }
784
785   if ((m->msg_count == m->msg_deleted) &&
786       ((m->magic == MUTT_MMDF) || (m->magic == MUTT_MBOX)) &&
787       !mutt_is_spool(mailbox_path(m)) && !C_SaveEmpty)
788   {
789     mutt_file_unlink_empty(mailbox_path(m));
790   }
791
792 #ifdef USE_SIDEBAR
793   if ((purge == MUTT_YES) && (m->msg_deleted != 0))
794   {
795     for (i = 0; i < m->msg_count; i++)
796     {
797       if (m->emails[i]->deleted && !m->emails[i]->read)
798       {
799         m->msg_unread--;
800         if (!m->emails[i]->old)
801           m->msg_new--;
802       }
803       if (m->emails[i]->deleted && m->emails[i]->flagged)
804         m->msg_flagged--;
805     }
806   }
807 #endif
808
809   mx_fastclose_mailbox(m);
810   FREE(ptr);
811
812   return 0;
813 }
814
815 /**
816  * mx_mbox_sync - Save changes to mailbox
817  * @param[in]  m          Mailbox
818  * @param[out] index_hint Currently selected Email
819  * @retval  0 Success
820  * @retval -1 Error
821  */
822 int mx_mbox_sync(struct Mailbox *m, int *index_hint)
823 {
824   if (!m)
825     return -1;
826
827   int rc;
828   int purge = 1;
829   int msgcount, deleted;
830
831   if (m->dontwrite)
832   {
833     char buf[256], tmp[256];
834     if (km_expand_key(buf, sizeof(buf), km_find_func(MENU_MAIN, OP_TOGGLE_WRITE)))
835       snprintf(tmp, sizeof(tmp), _(" Press '%s' to toggle write"), buf);
836     else
837       mutt_str_strfcpy(tmp, _("Use 'toggle-write' to re-enable write"), sizeof(tmp));
838
839     mutt_error(_("Mailbox is marked unwritable. %s"), tmp);
840     return -1;
841   }
842   else if (m->readonly)
843   {
844     mutt_error(_("Mailbox is read-only"));
845     return -1;
846   }
847
848   if (!m->changed && (m->msg_deleted == 0))
849   {
850     if (!m->quiet)
851       mutt_message(_("Mailbox is unchanged"));
852     return 0;
853   }
854
855   if (m->msg_deleted != 0)
856   {
857     char buf[128];
858
859     snprintf(buf, sizeof(buf),
860              ngettext("Purge %d deleted message?", "Purge %d deleted messages?", m->msg_deleted),
861              m->msg_deleted);
862     purge = query_quadoption(C_Delete, buf);
863     if (purge == MUTT_ABORT)
864       return -1;
865     else if (purge == MUTT_NO)
866     {
867       if (!m->changed)
868         return 0; /* nothing to do! */
869       /* let IMAP servers hold on to D flags */
870       if (m->magic != MUTT_IMAP)
871       {
872         for (int i = 0; i < m->msg_count; i++)
873         {
874           m->emails[i]->deleted = false;
875           m->emails[i]->purge = false;
876         }
877         m->msg_deleted = 0;
878       }
879     }
880     mailbox_changed(m, MBN_UNTAG);
881   }
882
883   /* really only for IMAP - imap_sync_mailbox results in a call to
884    * ctx_update_tables, so m->msg_deleted is 0 when it comes back */
885   msgcount = m->msg_count;
886   deleted = m->msg_deleted;
887
888   if (purge && (m->msg_deleted != 0) && (mutt_str_strcmp(mailbox_path(m), C_Trash) != 0))
889   {
890     if (trash_append(m) != 0)
891       return -1;
892   }
893
894 #ifdef USE_IMAP
895   if (m->magic == MUTT_IMAP)
896     rc = imap_sync_mailbox(m, purge, false);
897   else
898 #endif
899     rc = sync_mailbox(m, index_hint);
900   if (rc == 0)
901   {
902 #ifdef USE_IMAP
903     if ((m->magic == MUTT_IMAP) && !purge)
904     {
905       if (!m->quiet)
906         mutt_message(_("Mailbox checkpointed"));
907     }
908     else
909 #endif
910     {
911       if (!m->quiet)
912         mutt_message(_("%d kept, %d deleted"), msgcount - deleted, deleted);
913     }
914
915     mutt_sleep(0);
916
917     if ((m->msg_count == m->msg_deleted) &&
918         ((m->magic == MUTT_MBOX) || (m->magic == MUTT_MMDF)) &&
919         !mutt_is_spool(mailbox_path(m)) && !C_SaveEmpty)
920     {
921       unlink(mailbox_path(m));
922       mx_fastclose_mailbox(m);
923       return 0;
924     }
925
926     /* if we haven't deleted any messages, we don't need to resort */
927     /* ... except for certain folder formats which need "unsorted"
928      * sort order in order to synchronize folders.
929      *
930      * MH and maildir are safe.  mbox-style seems to need re-sorting,
931      * at least with the new threading code.  */
932     if (purge || ((m->magic != MUTT_MAILDIR) && (m->magic != MUTT_MH)))
933     {
934       /* IMAP does this automatically after handling EXPUNGE */
935       if (m->magic != MUTT_IMAP)
936       {
937         mailbox_changed(m, MBN_UPDATE);
938         mailbox_changed(m, MBN_RESORT);
939       }
940     }
941   }
942
943   return rc;
944 }
945
946 /**
947  * mx_msg_open_new - Open a new message
948  * @param m     Destination mailbox
949  * @param e     Message being copied (required for maildir support, because the filename depends on the message flags)
950  * @param flags Flags, see #MsgOpenFlags
951  * @retval ptr New Message
952  */
953 struct Message *mx_msg_open_new(struct Mailbox *m, struct Email *e, MsgOpenFlags flags)
954 {
955   if (!m)
956     return NULL;
957
958   struct Address *p = NULL;
959   struct Message *msg = NULL;
960
961   if (!m->mx_ops || !m->mx_ops->msg_open_new)
962   {
963     mutt_debug(LL_DEBUG1, "function unimplemented for mailbox type %d\n", m->magic);
964     return NULL;
965   }
966
967   msg = mutt_mem_calloc(1, sizeof(struct Message));
968   msg->write = true;
969
970   if (e)
971   {
972     msg->flags.flagged = e->flagged;
973     msg->flags.replied = e->replied;
974     msg->flags.read = e->read;
975     msg->flags.draft = (flags & MUTT_SET_DRAFT);
976     msg->received = e->received;
977   }
978
979   if (msg->received == 0)
980     msg->received = mutt_date_epoch();
981
982   if (m->mx_ops->msg_open_new(m, msg, e) == 0)
983   {
984     if (m->magic == MUTT_MMDF)
985       fputs(MMDF_SEP, msg->fp);
986
987     if (((m->magic == MUTT_MBOX) || (m->magic == MUTT_MMDF)) && flags & MUTT_ADD_FROM)
988     {
989       if (e)
990       {
991         p = TAILQ_FIRST(&e->env->return_path);
992         if (!p)
993           p = TAILQ_FIRST(&e->env->sender);
994         if (!p)
995           p = TAILQ_FIRST(&e->env->from);
996       }
997
998       char buf[64] = { 0 };
999       mutt_date_localtime_format(buf, sizeof(buf), "%a %b %e %H:%M:%S %Y\n", msg->received);
1000       fprintf(msg->fp, "From %s %s", p ? p->mailbox : NONULL(Username), buf);
1001     }
1002   }
1003   else
1004     FREE(&msg);
1005
1006   return msg;
1007 }
1008
1009 /**
1010  * mx_mbox_check - Check for new mail - Wrapper for MxOps::mbox_check()
1011  * @param m          Mailbox
1012  * @param index_hint Current email
1013  * @retval >0 Success, e.g. #MUTT_NEW_MAIL
1014  * @retval  0 Success, no change
1015  * @retval -1 Failure
1016  */
1017 int mx_mbox_check(struct Mailbox *m, int *index_hint)
1018 {
1019   if (!m || !m->mx_ops)
1020     return -1;
1021
1022   int rc = m->mx_ops->mbox_check(m, index_hint);
1023   if ((rc == MUTT_NEW_MAIL) || (rc == MUTT_REOPENED))
1024     mailbox_changed(m, MBN_INVALID);
1025
1026   return rc;
1027 }
1028
1029 /**
1030  * mx_msg_open - return a stream pointer for a message
1031  * @param m   Mailbox
1032  * @param msgno Message number
1033  * @retval ptr  Message
1034  * @retval NULL Error
1035  */
1036 struct Message *mx_msg_open(struct Mailbox *m, int msgno)
1037 {
1038   if (!m)
1039     return NULL;
1040
1041   struct Message *msg = NULL;
1042
1043   if (!m->mx_ops || !m->mx_ops->msg_open)
1044   {
1045     mutt_debug(LL_DEBUG1, "function not implemented for mailbox type %d\n", m->magic);
1046     return NULL;
1047   }
1048
1049   msg = mutt_mem_calloc(1, sizeof(struct Message));
1050   if (m->mx_ops->msg_open(m, msg, msgno) < 0)
1051     FREE(&msg);
1052
1053   return msg;
1054 }
1055
1056 /**
1057  * mx_msg_commit - Commit a message to a folder - Wrapper for MxOps::msg_commit()
1058  * @param m   Mailbox
1059  * @param msg Message to commit
1060  * @retval  0 Success
1061  * @retval -1 Failure
1062  */
1063 int mx_msg_commit(struct Mailbox *m, struct Message *msg)
1064 {
1065   if (!m || !m->mx_ops || !m->mx_ops->msg_commit)
1066     return -1;
1067
1068   if (!(msg->write && m->append))
1069   {
1070     mutt_debug(LL_DEBUG1, "msg->write = %d, m->append = %d\n", msg->write, m->append);
1071     return -1;
1072   }
1073
1074   return m->mx_ops->msg_commit(m, msg);
1075 }
1076
1077 /**
1078  * mx_msg_close - Close a message
1079  * @param[in]  m   Mailbox
1080  * @param[out] msg Message to close
1081  * @retval  0 Success
1082  * @retval -1 Failure
1083  */
1084 int mx_msg_close(struct Mailbox *m, struct Message **msg)
1085 {
1086   if (!m || !msg || !*msg)
1087     return 0;
1088
1089   int rc = 0;
1090
1091   if (m->mx_ops && m->mx_ops->msg_close)
1092     rc = m->mx_ops->msg_close(m, *msg);
1093
1094   if ((*msg)->path)
1095   {
1096     mutt_debug(LL_DEBUG1, "unlinking %s\n", (*msg)->path);
1097     unlink((*msg)->path);
1098     FREE(&(*msg)->path);
1099   }
1100
1101   FREE(&(*msg)->committed_path);
1102   FREE(msg);
1103   return rc;
1104 }
1105
1106 /**
1107  * mx_alloc_memory - Create storage for the emails
1108  * @param m Mailbox
1109  */
1110 void mx_alloc_memory(struct Mailbox *m)
1111 {
1112   size_t s = MAX(sizeof(struct Email *), sizeof(int));
1113
1114   if ((m->email_max + 25) * s < m->email_max * s)
1115   {
1116     mutt_error(_("Out of memory"));
1117     mutt_exit(1);
1118   }
1119
1120   m->email_max += 25;
1121   if (m->emails)
1122   {
1123     mutt_mem_realloc(&m->emails, sizeof(struct Email *) * m->email_max);
1124     mutt_mem_realloc(&m->v2r, sizeof(int) * m->email_max);
1125   }
1126   else
1127   {
1128     m->emails = mutt_mem_calloc(m->email_max, sizeof(struct Email *));
1129     m->v2r = mutt_mem_calloc(m->email_max, sizeof(int));
1130   }
1131   for (int i = m->msg_count; i < m->email_max; i++)
1132   {
1133     m->emails[i] = NULL;
1134     m->v2r[i] = -1;
1135   }
1136 }
1137
1138 /**
1139  * mx_check_empty - Is the mailbox empty
1140  * @param path Mailbox to check
1141  * @retval 1 Mailbox is empty
1142  * @retval 0 Mailbox contains mail
1143  * @retval -1 Error
1144  */
1145 int mx_check_empty(const char *path)
1146 {
1147   switch (mx_path_probe(path, NULL))
1148   {
1149     case MUTT_MBOX:
1150     case MUTT_MMDF:
1151       return mutt_file_check_empty(path);
1152     case MUTT_MH:
1153       return mh_check_empty(path);
1154     case MUTT_MAILDIR:
1155       return maildir_check_empty(path);
1156 #ifdef USE_IMAP
1157     case MUTT_IMAP:
1158     {
1159       int rc = imap_path_status(path, false);
1160       if (rc < 0)
1161         return -1;
1162       else if (rc == 0)
1163         return 1;
1164       else
1165         return 0;
1166     }
1167 #endif
1168     default:
1169       errno = EINVAL;
1170       return -1;
1171   }
1172   /* not reached */
1173 }
1174
1175 /**
1176  * mx_tags_edit - start the tag editor of the mailbox
1177  * @param m      Mailbox
1178  * @param tags   Existing tags
1179  * @param buf    Buffer for the results
1180  * @param buflen Length of the buffer
1181  * @retval -1 Error
1182  * @retval 0  No valid user input
1183  * @retval 1  Buffer set
1184  */
1185 int mx_tags_edit(struct Mailbox *m, const char *tags, char *buf, size_t buflen)
1186 {
1187   if (!m)
1188     return -1;
1189
1190   if (m->mx_ops->tags_edit)
1191     return m->mx_ops->tags_edit(m, tags, buf, buflen);
1192
1193   mutt_message(_("Folder doesn't support tagging, aborting"));
1194   return -1;
1195 }
1196
1197 /**
1198  * mx_tags_commit - Save tags to the Mailbox - Wrapper for MxOps::tags_commit()
1199  * @param m    Mailbox
1200  * @param e    Email
1201  * @param tags Tags to save
1202  * @retval  0 Success
1203  * @retval -1 Failure
1204  */
1205 int mx_tags_commit(struct Mailbox *m, struct Email *e, char *tags)
1206 {
1207   if (!m)
1208     return -1;
1209
1210   if (m->mx_ops->tags_commit)
1211     return m->mx_ops->tags_commit(m, e, tags);
1212
1213   mutt_message(_("Folder doesn't support tagging, aborting"));
1214   return -1;
1215 }
1216
1217 /**
1218  * mx_tags_is_supported - return true if mailbox support tagging
1219  * @param m Mailbox
1220  * @retval true Tagging is supported
1221  */
1222 bool mx_tags_is_supported(struct Mailbox *m)
1223 {
1224   return m && m->mx_ops->tags_commit && m->mx_ops->tags_edit;
1225 }
1226
1227 /**
1228  * mx_path_probe - Find a mailbox that understands a path
1229  * @param[in]  path  Path to examine
1230  * @param[out] st    stat buffer (OPTIONAL, for local mailboxes)
1231  * @retval num Type, e.g. #MUTT_IMAP
1232  */
1233 enum MailboxType mx_path_probe(const char *path, struct stat *st)
1234 {
1235   if (!path)
1236     return MUTT_UNKNOWN;
1237
1238   static const struct MxOps *no_stat[] = {
1239 #ifdef USE_IMAP
1240     &MxImapOps,
1241 #endif
1242 #ifdef USE_NOTMUCH
1243     &MxNotmuchOps,
1244 #endif
1245 #ifdef USE_POP
1246     &MxPopOps,
1247 #endif
1248 #ifdef USE_NNTP
1249     &MxNntpOps,
1250 #endif
1251   };
1252
1253   static const struct MxOps *with_stat[] = {
1254     &MxMaildirOps, &MxMboxOps, &MxMhOps, &MxMmdfOps,
1255 #ifdef USE_COMPRESSED
1256     &MxCompOps,
1257 #endif
1258   };
1259
1260   enum MailboxType rc;
1261
1262   for (size_t i = 0; i < mutt_array_size(no_stat); i++)
1263   {
1264     rc = no_stat[i]->path_probe(path, NULL);
1265     if (rc != MUTT_UNKNOWN)
1266       return rc;
1267   }
1268
1269   struct stat st2 = { 0 };
1270   if (!st)
1271     st = &st2;
1272
1273   if (stat(path, st) != 0)
1274   {
1275     mutt_debug(LL_DEBUG1, "unable to stat %s: %s (errno %d)\n", path, strerror(errno), errno);
1276     return MUTT_UNKNOWN;
1277   }
1278
1279   for (size_t i = 0; i < mutt_array_size(with_stat); i++)
1280   {
1281     rc = with_stat[i]->path_probe(path, st);
1282     if (rc != MUTT_UNKNOWN)
1283       return rc;
1284   }
1285
1286   return rc;
1287 }
1288
1289 /**
1290  * mx_path_canon - Canonicalise a mailbox path - Wrapper for MxOps::path_canon()
1291  */
1292 int mx_path_canon(char *buf, size_t buflen, const char *folder, enum MailboxType *magic)
1293 {
1294   if (!buf)
1295     return -1;
1296
1297   for (size_t i = 0; i < 3; i++)
1298   {
1299     /* Look for !! ! - < > or ^ followed by / or NUL */
1300     if ((buf[0] == '!') && (buf[1] == '!'))
1301     {
1302       if (((buf[2] == '/') || (buf[2] == '\0')))
1303       {
1304         mutt_str_inline_replace(buf, buflen, 2, LastFolder);
1305       }
1306     }
1307     else if ((buf[0] == '+') || (buf[0] == '='))
1308     {
1309       size_t folder_len = mutt_str_strlen(folder);
1310       if ((folder_len > 0) && (folder[folder_len - 1] != '/'))
1311       {
1312         buf[0] = '/';
1313         mutt_str_inline_replace(buf, buflen, 0, folder);
1314       }
1315       else
1316       {
1317         mutt_str_inline_replace(buf, buflen, 1, folder);
1318       }
1319     }
1320     else if ((buf[1] == '/') || (buf[1] == '\0'))
1321     {
1322       if (buf[0] == '!')
1323       {
1324         mutt_str_inline_replace(buf, buflen, 1, C_Spoolfile);
1325       }
1326       else if (buf[0] == '-')
1327       {
1328         mutt_str_inline_replace(buf, buflen, 1, LastFolder);
1329       }
1330       else if (buf[0] == '<')
1331       {
1332         mutt_str_inline_replace(buf, buflen, 1, C_Record);
1333       }
1334       else if (buf[0] == '>')
1335       {
1336         mutt_str_inline_replace(buf, buflen, 1, C_Mbox);
1337       }
1338       else if (buf[0] == '^')
1339       {
1340         mutt_str_inline_replace(buf, buflen, 1, CurrentFolder);
1341       }
1342       else if (buf[0] == '~')
1343       {
1344         mutt_str_inline_replace(buf, buflen, 1, HomeDir);
1345       }
1346     }
1347     else if (buf[0] == '@')
1348     {
1349       /* elm compatibility, @ expands alias to user name */
1350       struct AddressList *al = mutt_alias_lookup(buf + 1);
1351       if (TAILQ_EMPTY(al))
1352         break;
1353
1354       struct Email *e = email_new();
1355       e->env = mutt_env_new();
1356       mutt_addrlist_copy(&e->env->from, al, false);
1357       mutt_addrlist_copy(&e->env->to, al, false);
1358       mutt_default_save(buf, buflen, e);
1359       email_free(&e);
1360       break;
1361     }
1362     else
1363     {
1364       break;
1365     }
1366   }
1367
1368   // if (!folder) //XXX - use inherited version, or pass NULL to backend?
1369   //   return -1;
1370
1371   enum MailboxType magic2 = mx_path_probe(buf, NULL);
1372   if (magic)
1373     *magic = magic2;
1374   const struct MxOps *ops = mx_get_ops(magic2);
1375   if (!ops || !ops->path_canon)
1376     return -1;
1377
1378   if (ops->path_canon(buf, buflen) < 0)
1379   {
1380     mutt_path_canon(buf, buflen, HomeDir);
1381   }
1382
1383   return 0;
1384 }
1385
1386 /**
1387  * mx_path_canon2 - XXX
1388  * canonicalise the path to realpath
1389  */
1390 int mx_path_canon2(struct Mailbox *m, const char *folder)
1391 {
1392   if (!m)
1393     return -1;
1394
1395   char buf[PATH_MAX];
1396
1397   if (m->realpath)
1398     mutt_str_strfcpy(buf, m->realpath, sizeof(buf));
1399   else
1400     mutt_str_strfcpy(buf, mailbox_path(m), sizeof(buf));
1401
1402   int rc = mx_path_canon(buf, sizeof(buf), folder, &m->magic);
1403
1404   mutt_str_replace(&m->realpath, buf);
1405
1406   if (rc >= 0)
1407   {
1408     m->mx_ops = mx_get_ops(m->magic);
1409     mutt_buffer_strcpy(&m->pathbuf, m->realpath);
1410   }
1411
1412   return rc;
1413 }
1414
1415 /**
1416  * mx_path_pretty - Abbreviate a mailbox path - Wrapper for MxOps::path_pretty()
1417  */
1418 int mx_path_pretty(char *buf, size_t buflen, const char *folder)
1419 {
1420   enum MailboxType magic = mx_path_probe(buf, NULL);
1421   const struct MxOps *ops = mx_get_ops(magic);
1422   if (!ops)
1423     return -1;
1424
1425   if (!ops->path_canon)
1426     return -1;
1427
1428   if (ops->path_canon(buf, buflen) < 0)
1429     return -1;
1430
1431   if (!ops->path_pretty)
1432     return -1;
1433
1434   if (ops->path_pretty(buf, buflen, folder) < 0)
1435     return -1;
1436
1437   return 0;
1438 }
1439
1440 /**
1441  * mx_path_parent - Find the parent of a mailbox path - Wrapper for MxOps::path_parent()
1442  */
1443 int mx_path_parent(char *buf, size_t buflen)
1444 {
1445   if (!buf)
1446     return -1;
1447
1448   return 0;
1449 }
1450
1451 /**
1452  * mx_msg_padding_size - Bytes of padding between messages - Wrapper for MxOps::msg_padding_size()
1453  * @param m Mailbox
1454  * @retval num Number of bytes of padding
1455  *
1456  * mmdf and mbox add separators, which leads a small discrepancy when computing
1457  * vsize for a limited view.
1458  */
1459 int mx_msg_padding_size(struct Mailbox *m)
1460 {
1461   if (!m || !m->mx_ops || !m->mx_ops->msg_padding_size)
1462     return 0;
1463
1464   return m->mx_ops->msg_padding_size(m);
1465 }
1466
1467 /**
1468  * mx_ac_find - XXX
1469  */
1470 struct Account *mx_ac_find(struct Mailbox *m)
1471 {
1472   if (!m || !m->mx_ops)
1473     return NULL;
1474
1475   struct Account *np = NULL;
1476   TAILQ_FOREACH(np, &NeoMutt->accounts, entries)
1477   {
1478     if (np->magic != m->magic)
1479       continue;
1480
1481     if (m->mx_ops->ac_find(np, m->realpath))
1482       return np;
1483   }
1484
1485   return NULL;
1486 }
1487
1488 /**
1489  * mx_mbox_find - XXX
1490  *
1491  * find a mailbox on an account
1492  */
1493 struct Mailbox *mx_mbox_find(struct Account *a, const char *path)
1494 {
1495   if (!a || !path)
1496     return NULL;
1497
1498   struct MailboxNode *np = NULL;
1499   STAILQ_FOREACH(np, &a->mailboxes, entries)
1500   {
1501     if (mutt_str_strcmp(np->mailbox->realpath, path) == 0)
1502       return np->mailbox;
1503   }
1504
1505   return NULL;
1506 }
1507
1508 /**
1509  * mx_mbox_find2 - XXX
1510  *
1511  * find a mailbox on an account
1512  */
1513 struct Mailbox *mx_mbox_find2(const char *path)
1514 {
1515   if (!path)
1516     return NULL;
1517
1518   char buf[PATH_MAX];
1519   mutt_str_strfcpy(buf, path, sizeof(buf));
1520   mx_path_canon(buf, sizeof(buf), C_Folder, NULL);
1521
1522   struct Account *np = NULL;
1523   TAILQ_FOREACH(np, &NeoMutt->accounts, entries)
1524   {
1525     struct Mailbox *m = mx_mbox_find(np, buf);
1526     if (m)
1527       return m;
1528   }
1529
1530   return NULL;
1531 }
1532
1533 /**
1534  * mx_path_resolve - XXX
1535  */
1536 struct Mailbox *mx_path_resolve(const char *path)
1537 {
1538   if (!path)
1539     return NULL;
1540
1541   struct Mailbox *m = mx_mbox_find2(path);
1542   if (m)
1543     return m;
1544
1545   m = mailbox_new();
1546   m->flags = MB_HIDDEN;
1547   mutt_buffer_strcpy(&m->pathbuf, path);
1548   mx_path_canon2(m, C_Folder);
1549
1550   return m;
1551 }
1552
1553 /**
1554  * mx_ac_add - Add a Mailbox to an Account - Wrapper for MxOps::ac_add()
1555  */
1556 int mx_ac_add(struct Account *a, struct Mailbox *m)
1557 {
1558   if (!a || !m || !m->mx_ops || !m->mx_ops->ac_add)
1559     return -1;
1560
1561   if (m->mx_ops->ac_add(a, m) < 0)
1562     return -1;
1563
1564   account_mailbox_add(a, m);
1565   return 0;
1566 }
1567
1568 /**
1569  * mx_ac_remove - Remove a Mailbox from an Account and delete Account if empty
1570  * @param m Mailbox to remove
1571  */
1572 int mx_ac_remove(struct Mailbox *m)
1573 {
1574   if (!m || !m->account)
1575     return -1;
1576
1577   account_mailbox_remove(m->account, m);
1578   if (STAILQ_EMPTY(&m->account->mailboxes))
1579   {
1580     neomutt_account_remove(NeoMutt, m->account);
1581   }
1582   return 0;
1583 }
1584
1585 /**
1586  * mx_mbox_check_stats - Check the statistics for a mailbox - Wrapper for MxOps::mbox_check_stats()
1587  */
1588 int mx_mbox_check_stats(struct Mailbox *m, int flags)
1589 {
1590   if (!m)
1591     return -1;
1592
1593   return m->mx_ops->mbox_check_stats(m, flags);
1594 }
1595
1596 /**
1597  * mx_save_hcache - Save message to the header cache - Wrapper for MxOps::msg_save_hcache()
1598  * @param m Mailbox
1599  * @param e Email
1600  * @retval  0 Success
1601  * @retval -1 Failure
1602  *
1603  * Write a single header out to the header cache.
1604  */
1605 int mx_save_hcache(struct Mailbox *m, struct Email *e)
1606 {
1607   if (!m->mx_ops || !m->mx_ops->msg_save_hcache)
1608     return 0;
1609
1610   return m->mx_ops->msg_save_hcache(m, e);
1611 }