blob: b50daa9c47c440dc59df8855129cba373a3ee1ae (
plain)
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
|
use std::collections::BTreeSet;
use std::ffi::{OsStr, OsString};
use std::path::{Path, PathBuf};
use std::sync::LazyLock;
use protobuf::Message as _;
use crate::error::Result;
use crate::pb3::jwebmail::{FoldersReq, FoldersResp};
static REQUIRED_MAILDIR_DIRS: LazyLock<BTreeSet<OsString>> = LazyLock::new(|| {
[
OsString::from("cur"),
"new".into(),
"tmp".into(),
"maildirfolder".into(),
]
.into()
});
fn is_mailsubdir(p: &Path) -> bool {
p.is_dir()
&& p.file_name()
.map_or(false, |fname| fname.to_string_lossy().starts_with('.'))
&& p.read_dir()
.map(|dir| {
dir.filter_map(|child| {
child
.ok()
.and_then(|dir_entry| dir_entry.path().file_name().map(OsStr::to_owned))
})
.collect::<BTreeSet<_>>()
.is_superset(&*REQUIRED_MAILDIR_DIRS)
})
.unwrap_or_default()
}
pub fn folders(path: PathBuf, req: &[u8]) -> Result<Vec<u8>> {
let _ = FoldersReq::parse_from_bytes(req)?;
let subdirs = path
.read_dir()?
.filter_map(|d| d.ok())
.filter(|d| is_mailsubdir(&d.path()))
.filter_map(|d| Some(d.path().file_name()?.to_string_lossy()[1..].to_owned()))
.collect();
let mut res = FoldersResp::new();
res.folders = subdirs;
res.write_to_bytes().map_err(|e| e.into())
}
|