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
|
use std::io::ErrorKind as IOErrKind;
use std::path::PathBuf;
use maildir::Maildir;
use serde::Serialize as _;
use serde::Serializer as _;
mod count;
mod folders;
mod list;
mod raw;
use crate::error::Result;
use crate::rfc822::{MIMEHeader, Mail, TopMailHeader};
pub use count::{count, CountInfo};
pub use folders::folders;
pub use list::list;
pub use raw::raw;
pub enum Return {
Read(Mail),
Raw(MIMEHeader, Vec<u8>),
List(Vec<TopMailHeader>),
Folders(Vec<String>),
Count(CountInfo),
Search(Vec<Mail>),
Move,
}
pub fn serialize_to<W>(res: Result<Return>, mut write: W) -> std::io::Result<()>
where
W: std::io::Write + Copy,
{
let ser = &mut serde_json::Serializer::new(write);
match res {
Err(e) => {
e.serialize(ser)?;
std::process::exit(3)
}
Ok(r) => {
match r {
Return::Folders(fs) => fs.serialize(ser),
Return::Count(ci) => ci.serialize(ser),
Return::List(tmhs) => tmhs.serialize(ser),
Return::Search(ms) => ms.serialize(ser),
Return::Read(m) => m.serialize(ser),
Return::Raw(mh, b) => {
let r = match mh.serialize(ser) {
Ok(x) => x,
Err(e) => return Err(e.into()),
};
write.write_all(b"\n")?;
write.write_all(&b)?;
Ok(r)
}
Return::Move => ser.serialize_unit(),
}?;
Ok(())
}
}
}
pub fn open_submaildir(mut path: PathBuf, sub: &str) -> Maildir {
if sub != "" {
path.push(String::from(".") + sub);
}
Maildir::from(path)
}
pub fn read(md: &Maildir, mid: &str) -> Result<Mail> {
md.add_flags(mid, "S")?;
let mut mail = md.find(mid).ok_or_else(|| {
std::io::Error::new(IOErrKind::NotFound, format!("mail {} not found", mid))
})?;
Ok(mail.parsed()?.try_into()?)
}
pub fn move_mail(p: PathBuf, mid: &str, from_f: &str, to_f: &str) -> Result<()> {
let from = open_submaildir(p.clone(), from_f);
let to = open_submaildir(p, to_f);
from.move_to(mid, &to).map_err(|e| e.into())
}
pub fn search(_md: &Maildir, _pattern: &str) -> Result<Vec<Mail>> {
todo!()
}
|