crymap 1.0.1

A simple, secure IMAP server with encrypted data at rest
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
//-
// Copyright (c) 2020, Jason Lingle
//
// This file is part of Crymap.
//
// Crymap is free software: you can  redistribute it and/or modify it under the
// terms of  the GNU General Public  License as published by  the Free Software
// Foundation, either version  3 of the License, or (at  your option) any later
// version.
//
// Crymap is distributed  in the hope that  it will be useful,  but WITHOUT ANY
// WARRANTY; without  even the implied  warranty of MERCHANTABILITY  or FITNESS
// FOR  A PARTICULAR  PURPOSE.  See the  GNU General  Public  License for  more
// details.
//
// You should have received a copy of the GNU General Public License along with
// Crymap. If not, see <http://www.gnu.org/licenses/>.

//! Support for manipulating mailbox paths and operations that manipulate whole
//! mailboxes.
//!
//! Information on file layout is found in the `mailbox` module documentation.

use std::fs;
use std::io::{self, Read};
use std::os::unix::fs::{DirBuilderExt, OpenOptionsExt};
use std::path::{Path, PathBuf};

use rand::{rngs::OsRng, Rng};
use serde::{Deserialize, Serialize};
use tempfile::TempDir;

use crate::account::model::*;
use crate::support::error::Error;
use crate::support::file_ops::{self, ErrorTransforms, IgnoreKinds};
use crate::support::safe_name::is_safe_name;

/// A lightweight reference to a mailbox by path.
///
/// This is used for most IMAP operations that take place outside the context
/// of selection (i.e. the "Authenticated State" in RFC 3501).
#[derive(Clone, Debug)]
pub struct MailboxPath {
    pub(super) name: String,
    pub(super) base_path: PathBuf,
    pub(super) data_path: PathBuf,
    pub(super) metadata_path: PathBuf,
    pub(super) socks_path: PathBuf,
    pub(super) shadow_path: PathBuf,
    pub(super) sub_path: PathBuf,
}

impl MailboxPath {
    fn from_name_and_path(
        name: String,
        base_path: PathBuf,
        shadow_path: PathBuf,
    ) -> Self {
        let data_path = base_path.join("%");
        MailboxPath {
            metadata_path: data_path.join("mailbox.toml"),
            sub_path: shadow_path.join("%subscribe"),
            socks_path: data_path.join("socks"),
            name,
            base_path,
            data_path,
            shadow_path,
        }
    }

    /// Instantiate a `MailboxPath` under the root.
    pub fn root(
        name: String,
        root: &Path,
        shadow_root: &Path,
    ) -> Result<Self, Error> {
        if !is_safe_name(&name) {
            return Err(Error::UnsafeName);
        }

        let path = root.join(&name);
        let shadow_path = shadow_root.join(&name);
        Ok(MailboxPath::from_name_and_path(name, path, shadow_path))
    }

    /// Instantiate a `MailboxPath` inferior to this one.
    pub fn child(&self, name: &str) -> Result<Self, Error> {
        if !is_safe_name(&name) {
            return Err(Error::UnsafeName);
        }

        if !self.allows_children() {
            return Err(Error::BadOperationOnInbox);
        }

        Ok(MailboxPath::from_name_and_path(
            format!("{}/{}", self.name, name),
            self.base_path.join(name),
            self.shadow_path.join(name),
        ))
    }

    /// Return this mailbox's canonical name, e.g. "INBOX" or "Archive/2020".
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Return the *current* UID validity, i.e., that which would be used if
    /// the mailbox were opened right now.
    pub fn current_uid_validity(&self) -> Result<u32, Error> {
        if !self.exists() {
            return Err(Error::NxMailbox);
        }

        parse_uid_validity(
            &nix::fcntl::readlink(&self.data_path)
                .on_not_found(Error::MailboxUnselectable)?,
        )
    }

    /// Return the UID-specific root of the mailbox data.
    pub fn scoped_data_path(&self) -> Result<PathBuf, Error> {
        Ok(
            self.scoped_data_path_for_uid_validity(
                self.current_uid_validity()?,
            ),
        )
    }

    fn scoped_data_path_for_uid_validity(&self, uid_validity: u32) -> PathBuf {
        self.base_path.join(format!("%{:x}", uid_validity))
    }

    /// Whether this mailbox allows children.
    ///
    /// INBOX is not allowed to have children due to the crazy special case RFC
    /// 3501 adds to `RENAME INBOX`.
    pub fn allows_children(&self) -> bool {
        "INBOX" != &self.name
    }

    /// Whether this mailbox can be selected (i.e., whether it can contain
    /// messages).
    pub fn is_selectable(&self) -> bool {
        self.data_path.is_dir()
    }

    /// Whether this mailbox exists as a physical entity, i.e., whether it is a
    /// real mailbox in the view of IMAP.
    pub fn exists(&self) -> bool {
        self.base_path.is_dir()
    }

    /// Whether this mailbox is currently subscribed.
    pub fn is_subscribed(&self) -> bool {
        self.sub_path.is_file()
    }

    /// Return an iterator to the children of this mailbox, regardless of
    /// existence status.
    pub fn children<'a>(&'a self) -> impl Iterator<Item = MailboxPath> + 'a {
        self.children_impl(&self.base_path)
    }

    fn children_impl<'a>(
        &'a self,
        path: &Path,
    ) -> impl Iterator<Item = MailboxPath> + 'a {
        fs::read_dir(path)
            .ok()
            .map(move |it| {
                it.filter_map(|r| r.ok())
                    .filter_map(|entry| entry.file_name().into_string().ok())
                    .filter_map(move |name| self.child(&name).ok())
            })
            .map(|it| {
                Box::new(it) as Box<dyn Iterator<Item = MailboxPath> + 'a>
            })
            .unwrap_or_else(|| Box::new(std::iter::empty()))
    }

    /// Create this mailbox.
    ///
    /// A `\Noselect` mailbox cannot be "created" to restore its dual-use
    /// status.
    ///
    /// Does not create parent mailboxes implicitly.
    ///
    /// Returns the MAILBOXID of the new path.
    pub fn create(
        &self,
        tmp: &Path,
        special_use: Option<MailboxAttribute>,
    ) -> Result<String, Error> {
        // Generate the UID validity by using the lower 32 bits of the
        // UNIX time. It is fine that this will wrap in 2106. To even
        // have a chance of colliding, the mail server would need to be
        // in use for 136 years.
        let uid_validity = std::time::SystemTime::now()
            .duration_since(std::time::SystemTime::UNIX_EPOCH)
            .unwrap()
            .as_secs() as u32;
        // ... but for some reason, RFC 3501 requires UIDVALIDITY to be
        // strictly ascending, despite the only sensible operation on the value
        // being equality. To maximise the time until we risk violating that
        // constraint, adjust the "zero epoch" to 2020-01-01. This gives until
        // 2156 before wrapping. We also need to avoid generating 0.
        let mut uid_validity = uid_validity.wrapping_sub(1577836800).max(1);

        // We need to avoid generating duplicate UID validity values when
        // mailboxes are created in quick succession, since otherwise the
        // identity requirements could be invalidated by renaming one mailbox,
        // then another to the former's old name.
        //
        // To do this, we simply try to create a token file in the temp
        // directory. If the UV is already taken, increment and try again
        // ("time creep"). We give up after 1000 tries, which ensures we don't
        // fail forever and also don't creep so far forward that markers needed
        // for correctness get cleaned up.
        for trie in 0.. {
            match fs::OpenOptions::new()
                .mode(0o600)
                .create_new(true)
                .write(true)
                .open(tmp.join(format!("uv-{:x}", uid_validity)))
            {
                Ok(_) => break,
                Err(e) if io::ErrorKind::AlreadyExists == e.kind() => {
                    if trie < 1000 {
                        uid_validity = uid_validity.wrapping_add(1).max(1);
                    } else {
                        return Err(Error::GaveUpInsertion);
                    }
                }
                Err(e) => return Err(e.into()),
            }
        }

        // Stage the new mailbox hierarchy inside tmp, then move the whole
        // thing in when done.
        let stage = TempDir::new_in(tmp)?;
        let stage_mbox = MailboxPath::from_name_and_path(
            String::new(),
            stage.path().to_owned(),
            // Shadow path doesn't matter
            stage.path().to_owned(),
        );
        let scoped_path =
            stage_mbox.scoped_data_path_for_uid_validity(uid_validity);
        fs::DirBuilder::new().mode(0o750).create(&scoped_path)?;
        std::os::unix::fs::symlink(
            scoped_path.file_name().unwrap(),
            &stage_mbox.data_path,
        )?;
        let mailbox_id = gen_mailbox_id();
        let full_mailbox_id = self.format_mailbox_id(&mailbox_id);
        let metadata = MailboxMetadata {
            imap: MailboxImapMetadata {
                special_use,
                mailbox_id,
            },
        };

        let metadata_toml = format!(
            "# Edit this file at your own peril!\n\
             # Crymap assumes it never changes.\n{}",
            toml::to_string_pretty(&metadata).unwrap()
        );
        file_ops::spit(
            tmp,
            &stage_mbox.metadata_path,
            false,
            0o440,
            metadata_toml.as_bytes(),
        )?;

        // Ready to go
        fs::rename(stage.into_path(), &self.base_path)
            .on_exists(Error::MailboxExists)
            .map_err(|e| match e {
                Error::Io(e)
                    if Some(nix::libc::ENOTEMPTY) == e.raw_os_error() =>
                {
                    Error::MailboxExists
                }
                e => e,
            })?;

        Ok(full_mailbox_id)
    }

    /// Create this mailbox if it does not already exist.
    pub fn create_if_nx(&self, tmp: &Path) -> Result<(), Error> {
        if !self.exists() {
            match self.create(tmp, None) {
                Err(Error::MailboxExists) => Ok(()),
                Err(e) => Err(e),
                Ok(_) => Ok(()),
            }
        } else {
            Ok(())
        }
    }

    /// Delete this mailbox, using IMAP semantics.
    ///
    /// If this mailbox is selectable, any messages it contains are destroyed
    /// and it becomes a `\Noselect` mailbox.
    ///
    /// If the mailbox has no inferiors, it is fully removed.
    ///
    /// A `MailboxHasInferiors` error is only returned if this mailbox was
    /// already `\Noselect` and it has inferiors visible to IMAP.
    pub fn delete(&self, garbage: &Path) -> Result<(), Error> {
        if &self.name == "INBOX" {
            return Err(Error::BadOperationOnInbox);
        }

        // If no children, completely remove self
        // This also handles the case where self does not exist
        if self.children().next().is_none() {
            file_ops::delete_async(&self.base_path, garbage)
                .on_not_found(Error::NxMailbox)
        } else {
            // Atomically turn into a \Noselect mailbox if we were selectable
            let selectable = match self.scoped_data_path().and_then(|p| {
                file_ops::delete_async(p, garbage).map_err(|e| e.into())
            }) {
                Ok(()) => true,
                Err(Error::MailboxUnselectable) => false,
                Err(Error::Io(e)) if io::ErrorKind::NotFound == e.kind() => {
                    false
                }
                Err(e) => return Err(e),
            };

            // Regardless of selectability, the %UV directory is gone, so
            // remove the symlink too
            let _ = fs::remove_file(&self.data_path);

            if selectable {
                // OK, silently keep existing as a \Noselect
                Ok(())
            } else {
                // We were already \Noselect, so complain
                Err(Error::MailboxHasInferiors)
            }
        }
    }

    /// Rename self to `dst`.
    ///
    /// Does not create parent mailboxes implicitly.
    ///
    /// `tmp` is used to stage files for the INBOX special case.
    pub fn rename(&self, dst: &MailboxPath, tmp: &Path) -> Result<(), Error> {
        fs::rename(&self.base_path, &dst.base_path)
            .on_not_found(Error::NxMailbox)
            .on_exists(Error::MailboxExists)
            .map_err(|e| match e {
                Error::Io(e)
                    if Some(nix::libc::ENOTEMPTY) == e.raw_os_error() =>
                {
                    Error::MailboxExists
                }
                e => e,
            })?;

        // RFC 3501 specifies a crazy special case for INBOX: A RENAME does not
        // rename it, but instead *creates* the destination and then moves all
        // *messages* into the new mailbox, while leaving all child mailboxes
        // alone.
        //
        // Due to the last clause, we forbid INBOX from having any children.
        //
        // In our implementation, we just rename INBOX and then recreate it.
        // This isn't ideal since it is non-atomic and results in a brief
        // window in which there *is no inbox*, but overall we have a choice
        // between doing that and having a period where the messages that were
        // in the inbox are not reachable by any mailbox, which is far worse.
        if &self.name == "INBOX" {
            match self.create(tmp, None) {
                Ok(_) => (),
                Err(Error::MailboxExists) => (),
                Err(e) => return Err(e),
            }
        }

        Ok(())
    }

    /// Loads and returns the metadata for this mailbox.
    pub fn metadata(&self) -> Result<MailboxMetadata, Error> {
        if !self.exists() {
            return Err(Error::NxMailbox);
        }

        let mut reader = fs::File::open(&self.metadata_path)
            .on_not_found(Error::MailboxUnselectable)?;
        let mut data = Vec::new();
        reader.read_to_end(&mut data)?;
        Ok(toml::from_slice(&data)?)
    }

    /// Return the RFC 8474 MAILBOXID for this mailbox.
    pub fn mailbox_id(&self) -> Result<String, Error> {
        let base_id = self.metadata()?.imap.mailbox_id;
        Ok(self.format_mailbox_id(&base_id))
    }

    fn format_mailbox_id(&self, base_id: &str) -> String {
        if "INBOX" == self.name {
            format!("I{}", base_id)
        } else {
            format!("M{}", base_id)
        }
    }

    /// Mark this mailbox as subscribed.
    pub fn subscribe(&self) -> Result<(), Error> {
        fs::DirBuilder::new()
            .mode(0o700)
            .recursive(true)
            .create(&self.shadow_path)
            .ignore_already_exists()?;
        fs::OpenOptions::new()
            .create(true)
            .write(true)
            .mode(0o600)
            .open(&self.sub_path)
            .map(|_| ())?;
        Ok(())
    }

    /// Mark this mailbox as unsubscribed.
    pub fn unsubscribe(&self) -> Result<(), Error> {
        Ok(fs::remove_file(&self.sub_path).ignore_not_found()?)
    }

    /// Underlying implementation for `LIST`, `XLIST`, and `LSUB`.
    ///
    /// `matcher` is used to determine  whether the mailbox's name matches.
    ///
    /// Results are pushed into `dst` in post-order, so `dst` must be reversed
    /// once the full result set is in.
    ///
    /// On success, returns whether this mailbox exists, and whether there are
    /// selected but unmatched mailboxes within it (i.e. either itself or any
    /// direct or indirect children).
    pub fn list(
        &self,
        dst: &mut Vec<ListResponse>,
        request: &ListRequest,
        matcher: &impl Fn(&str) -> bool,
    ) -> ChildListResult {
        let mut self_result = ChildListResult::default();
        let selectable = self.is_selectable();
        self_result.exists = self.exists();

        let self_matches = matcher(&self.name);

        let subscribed = (request.select_subscribed
            || request.return_subscribed)
            && self.is_subscribed();
        let special_use =
            if request.select_special_use || request.return_special_use {
                self.metadata().ok().and_then(|md| md.imap.special_use)
            } else {
                None
            };

        let mut self_selected = (!request.select_subscribed || subscribed)
            && (!request.select_special_use || special_use.is_some());
        let mut has_children = false;

        let children = self.children_impl(if request.select_subscribed {
            &self.shadow_path
        } else {
            &self.base_path
        });
        for child in children {
            let child_result = child.list(dst, request, matcher);
            self_result.selected_subscribe |= child_result.selected_subscribe;
            self_result.selected_special_use |=
                child_result.selected_special_use;
            self_result.unmatched_but_selected |=
                child_result.unmatched_but_selected;

            // RFC 5258 does not specifically define the behaviour of
            // \HasChildren and \HasNoChildren when acting on subscriptions.
            // Here, we take the position that they still refer to the *real*
            // mailboxes and not the shadow hierarchy that subscriptions have.
            has_children |= child_result.exists;
        }

        // If we aren't doing subscriptions, we filter to only existing
        // mailboxes.
        self_selected &= request.select_subscribed || self_result.exists;

        // Add an entry for self if matching and selected, or if matching and
        // unselected but we have a selected but unmatching child and
        // recursive_match is enabled.
        if self_matches
            && (self_selected
                || (request.recursive_match
                    && self_result.unmatched_but_selected))
        {
            let mut info = ListResponse {
                name: self.name.clone(),
                ..ListResponse::default()
            };

            if !self_selected && request.lsub_style {
                // If not selected but being included anyway due to
                // recursive_match, tell the client about this. For LSUB,
                // \Noselect is abused. For extended list, it is implied by the
                // fact that the returned mailbox doesn't have the attribute
                // being selected.
                info.attributes.push(MailboxAttribute::Noselect);
            } else if !self_result.exists {
                // If this mailbox doesn't exist but is a subscription, tell
                // the client unless we're doing LSUB.
                if !request.lsub_style {
                    info.attributes.push(MailboxAttribute::NonExistent);
                }
            } else if !selectable {
                // In most cases, we return \Noselect for non-selectable
                // mailboxes that do exist. The exception is LSUB, where
                // \Noselect is repurposed to mean "not subscribed, but has
                // unreported inferiors which are subscribed".
                if !request.lsub_style {
                    info.attributes.push(MailboxAttribute::Noselect);
                }
            }

            if !self.allows_children() {
                info.attributes.push(MailboxAttribute::Noinferiors);
            }

            if subscribed && request.return_subscribed {
                info.attributes.push(MailboxAttribute::Subscribed);
            }

            if request.return_children {
                if has_children {
                    info.attributes.push(MailboxAttribute::HasChildren);
                } else if self.allows_children() {
                    // Only HasNoChildren if we allow children, since otherwise
                    // we have \Noinferiors which already implies
                    // \HasNoChildren. Sending both is still OK and isn't even
                    // a "SHOULD NOT", but it's best to conform to the
                    // examples.
                    info.attributes.push(MailboxAttribute::HasNoChildren);
                }
            }

            if request.return_special_use {
                if let Some(special_use) = special_use {
                    info.attributes.push(special_use);
                }
            }

            if request.recursive_match {
                if self_result.selected_special_use {
                    info.child_info.push("SPECIAL-USE");
                }

                if self_result.selected_subscribe {
                    info.child_info.push("SUBSCRIBED");
                }

                // We've covered the unmatched child, so this can stop
                // propagating.
                self_result.unmatched_but_selected = false;
            }

            dst.push(info);
        }

        // CHILDINFO data is passed the whole way up the tree unconditionally.
        self_result.selected_special_use |=
            request.select_special_use && special_use.is_some();
        self_result.selected_subscribe |=
            request.select_subscribed && subscribed;

        if !self_matches && self_selected {
            // Notify our parent that we were selected but not matched so that
            // it can insert a spurious result if it matches but isn't
            // selected.
            self_result.unmatched_but_selected = true;
        }

        self_result
    }
}

/// This is used internally by `MailboxPath::list`.
///
/// Its value is not meaningful outside that function, but must be `pub` here
/// since it is nonetheless returned.
#[derive(Clone, Copy, Default)]
pub struct ChildListResult {
    selected_subscribe: bool,
    selected_special_use: bool,
    unmatched_but_selected: bool,
    exists: bool,
}

/// Given a raw mailbox path, emit the parts that comprise the actual path.
///
/// This accounts for the path delimiter, empty segments, and the required
/// case-insensitivity of the root `inbox` mailbox.
///
/// It does not check for name safety.
pub fn parse_mailbox_path<'a>(
    path: &'a str,
) -> impl Iterator<Item = &'a str> + 'a {
    path.split('/')
        .filter(|s| !s.is_empty())
        .enumerate()
        .map(|(ix, s)| {
            if 0 == ix && "inbox".eq_ignore_ascii_case(s) {
                "INBOX"
            } else {
                s
            }
        })
}

/// Creates a predicate which identifies which normalised mailbox names match
/// any element of `patterns`, with pattern matching performed as per RFC 3501.
///
/// Each pattern is first normalised by `parse_mailbox_path`.
///
/// This design means that any `LIST` operation needs to fetch all mailboxes
/// and then narrow it down, instead of a more ideal recursive filtering.
/// However, the semantics of `*`, particularly the fact that it's permitted in
/// the _middle_ of the path, preclude doing that in any sane (i.e.,
/// non-exponential) way. Since we _only_ iterate actual mailboxes (and not,
/// say, all of USENET, or the user's whole home directory as UW-IMAP), this
/// shouldn't be a problem.
pub fn mailbox_path_matcher<'a>(
    patterns: impl IntoIterator<Item = &'a str>,
) -> impl Fn(&str) -> bool + 'a {
    let mut rx = "^(".to_owned();
    for (pattern_ix, pattern) in patterns.into_iter().enumerate() {
        if pattern_ix > 0 {
            rx.push('|');
        }

        for (part_ix, part) in parse_mailbox_path(pattern).enumerate() {
            if part_ix > 0 {
                rx.push('/');
            }

            let mut start = 0;
            for end in part
                .match_indices(|c| '%' == c || '*' == c)
                .map(|(ix, _)| ix)
                .chain(part.len()..=part.len())
            {
                let chunk = &part[start..end];
                start = (end + 1).min(part.len());

                rx.push_str(&regex::escape(chunk));
                match part.get(end..end + 1) {
                    Some("*") => rx.push_str(".*"),
                    Some("%") => rx.push_str("[^/]*"),
                    _ => (),
                }
            }
        }
    }
    rx.push_str(")$");

    let rx = regex::Regex::new(&rx).expect("Built invalid regex?");
    move |s| rx.is_match(s)
}

/// Immutable metadata about a mailbox.
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct MailboxMetadata {
    pub imap: MailboxImapMetadata,
}

/// IMAP-specific immutable metadata about a mailbox.
///
/// The "general" vs "IMAP" distinction is mainly because TOML requires a top
/// level of nesting.
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct MailboxImapMetadata {
    /// If this is a special-use mailbox, the attribute representing that use.
    pub special_use: Option<MailboxAttribute>,
    /// The RFC 8474 MAILBOXID of this mailbox, sans prefix.
    ///
    /// On INBOX, this gets prefixed with "I". On any other mailbox, it gets
    /// prefixed with "M". This is needed to maintain the illusion that a
    /// RENAME of INBOX is not actually a rename.
    pub mailbox_id: String,
}

/// Parse the UID validity out of the given path.
///
/// Only the filename of the path is considered, so this will not work for
/// things inside the mailbox data directory.
pub fn parse_uid_validity(path: impl AsRef<Path>) -> Result<u32, Error> {
    let path = path.as_ref();
    let name = path
        .file_name()
        .ok_or(Error::CorruptFileLayout)?
        .to_str()
        .ok_or(Error::CorruptFileLayout)?;

    if !name.starts_with('%') {
        return Err(Error::CorruptFileLayout);
    }

    u32::from_str_radix(&name[1..], 16).map_err(|_| Error::CorruptFileLayout)
}

fn gen_mailbox_id() -> String {
    // 15 bytes = 120 bits of entropy, roughly the same as a V4 UUID, but
    // shorter since we can use base64 encoding. This outputs a 20 character
    // string, and then we have the extra 1-character prefix for a total of 21
    // character ids. Using 15 bytes means there is no padding, which is
    // important since the padding characters are not allowed in object ids.
    // (The set of allowed characters is exactly the set of "URL Safe" base64.)
    let data: [u8; 15] = OsRng.gen();
    base64::encode_config(&data, base64::URL_SAFE)
}

#[cfg(test)]
mod test {
    use std::iter;

    use super::*;

    #[test]
    fn test_parse_mailbox_path() {
        fn p(p: &'static str) -> Vec<&'static str> {
            parse_mailbox_path(p).collect()
        }

        assert_eq!(vec!["INBOX"], p("inbox"));
        assert_eq!(vec!["INBOX", "foo"], p("Inbox/foo"));
        assert_eq!(vec!["bar"], p("/bar"));
        assert_eq!(vec!["bar"], p("bar/"));
        assert_eq!(vec!["foo", "bar"], p("foo//bar"));
        assert_eq!(vec!["foo", "InBoX"], p("foo/InBoX"));
    }

    #[test]
    fn test_mailbox_patterns() {
        fn matches(pat: &str, mb: &str) -> bool {
            mailbox_path_matcher(iter::once(pat))(mb)
        }

        assert!(matches("*", "INBOX"));
        assert!(matches("%", "INBOX"));

        assert!(matches("INB*X", "INBOX"));
        assert!(matches("INB*X", "INB/BOX"));
        assert!(!matches("INB*X", "INBOX/plugh"));
        assert!(!matches("INB*X", "foo/INBOX"));
        assert!(matches("INB%X", "INBOX"));
        assert!(!matches("INB%X", "INB/BOX"));
        assert!(!matches("INB%X", "INBOX/plugh"));

        assert!(matches("INB*", "INBOX"));
        assert!(matches("INB*", "INBOX/plugh"));
        assert!(matches("INB%", "INBOX"));
        assert!(!matches("INB%", "INBOX/plugh"));
        assert!(!matches("INB%", "foo/INBOX"));

        assert!(matches("*X", "INBOX"));
        assert!(matches("*X", "foo/boX"));
        assert!(matches("%X", "INBOX"));
        assert!(!matches("%X", "foo/boX"));

        assert!(matches("foo/bar", "foo/bar"));
        assert!(!matches("foo/bar", "foo/bar/baz"));
        assert!(!matches("foo/*", "foo"));
        assert!(matches("foo/*", "foo/bar"));
        assert!(matches("foo/*", "foo/bar/baz"));
        assert!(matches("foo/%", "foo/bar"));
        assert!(!matches("foo/%", "foo/bar/baz"));

        assert!(matches("inbox", "INBOX"));
    }
}