summaryrefslogtreecommitdiff
path: root/src/cmd/folders.rs
blob: 417203c90ed6eaae430bf4ce617c77e551457f03 (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
use std::path::{Path, PathBuf};

use protobuf::Message as _;

use crate::error::Result;
use crate::pb3::jwebmail::{FoldersReq, FoldersResp};

fn is_mailsubdir(p: &Path) -> bool {
    if !p.is_dir() {
        return false;
    }
    if !p.file_name().map_or(false, |fname| {
        fname.len() > 1 && fname.to_string_lossy().starts_with('.')
    }) {
        return false;
    }
    let mut buf = p.to_owned();
    buf.push("cur");
    if !buf.is_dir() {
        return false;
    }
    buf.pop();
    buf.push("new");
    if !buf.is_dir() {
        return false;
    }
    buf.pop();
    buf.push("tmp");
    if !buf.is_dir() {
        return false;
    }
    true
}

pub fn folders(path: PathBuf, req: &[u8]) -> Result<Vec<u8>> {
    let _ = FoldersReq::parse_from_bytes(req)?;

    let mut subdirs: Vec<_> = 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();

    subdirs.sort();

    let mut res = FoldersResp::new();
    res.folders = subdirs;
    res.write_to_bytes().map_err(|e| e.into())
}