summaryrefslogtreecommitdiff
path: root/src/cmd/folders.rs
blob: 8bf9a30ff00b4e170dc9d42bc80575d2409a4c1c (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
use std::path::Path;

use crate::cmd::MailStorage;
use crate::de_jmhoffmann_jwebmail_mailstorage::Call_Folders;

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(ms: &MailStorage, call: &mut dyn Call_Folders) -> varlink::Result<()> {
    if let Some(path) = &*ms.maildir_path.read().unwrap() {
        let mut subdirs: Vec<_> = path
            .read_dir()
            .map_err(varlink::map_context!())?
            .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();

        call.reply(subdirs)
    } else {
        call.reply_not_initialized()
    }
}