summaryrefslogtreecommitdiff
path: root/src/cmd/raw.rs
blob: 9fe76208fa40dff870ff53fba62a393bf6f160d3 (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
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
use std::fs::read;
use std::io::ErrorKind as IOErrKind;
use std::path::PathBuf;

use protobuf::Message as _;

use crate::cmd::open_submaildir;
use crate::error::{Error, Result};
use crate::pb3::jwebmail::mimeheader::ContentDisposition::*;
use crate::pb3::jwebmail::{MIMEHeader, RawReq, RawResp};
use crate::rfc822::parse_mail_content;

pub fn raw(md_path: PathBuf, req: &[u8]) -> Result<Vec<u8>> {
    let r = RawReq::parse_from_bytes(req)?;

    let md = open_submaildir(md_path, &r.folder);

    let mut mail = md.find(&r.mid).ok_or_else(|| {
        std::io::Error::new(IOErrKind::NotFound, format!("mail {} not found", &r.mid))
    })?;

    match r.path.as_deref() {
        Some("") | None => {
            let mut mh = MIMEHeader::new();

            mh.maintype = "message".to_owned();
            mh.subtype = "rfc822".to_owned();
            mh.file_name = Some(mail.id().to_owned());
            mh.contentdispo = CONTENT_DISPOSITION_NONE.into();

            let mut resp = RawResp::new();
            resp.header = Some(mh).into();
            resp.body = read(mail.path())?;
            resp.write_to_bytes().map_err(|e| e.into())
        }
        Some(mime_path) => {
            let path = mime_path
                .split('.')
                .map(|x| {
                    x.parse()
                        .map_err(|pe: std::num::ParseIntError| Error::PathError {
                            msg: pe.to_string(),
                            path: mime_path.to_owned(),
                        })
                })
                .collect::<Result<Vec<_>>>()?;
            let mut m = mail.parsed()?;

            if path[0] != 0 {
                return Err(Error::PathError {
                    msg: "Message must be accessed by a 0".to_owned(),
                    path: mime_path.to_owned(),
                });
            }

            for i in &path[1..] {
                match &m.ctype.mimetype {
                    x if x.starts_with("message/") => {
                        if *i != 0 {
                            return Err(Error::PathError {
                                msg: "Message must be accessed by a 0".to_owned(),
                                path: mime_path.to_owned(),
                            });
                        }
                        let s: &'static _ = m.get_body_raw()?.leak();
                        m = mailparse::parse_mail(s)?;
                    }
                    x if x.starts_with("multipart/") => {
                        if *i >= m.subparts.len() {
                            return Err(Error::PathError {
                                msg: "Out of bounds access".to_owned(),
                                path: mime_path.to_owned(),
                            });
                        }
                        m = m.subparts.swap_remove(*i);
                    }
                    _ => {
                        return Err(Error::PathError {
                            msg: "Unable to descent into leaf component".to_owned(),
                            path: mime_path.to_owned(),
                        })
                    }
                }
            }

            if m.ctype.mimetype.starts_with("multipart/") {
                return Err(Error::PathError {
                    msg: "Can not show multipart component".to_owned(),
                    path: mime_path.to_owned(),
                });
            }

            let mime_part = parse_mail_content(&m)?;
            let content = if m.ctype.mimetype.starts_with("text/") {
                m.get_body()?.into_bytes()
            } else {
                m.get_body_raw()?
            };

            let mut resp = RawResp::new();
            resp.header = Some(mime_part).into();
            resp.body = content;
            resp.write_to_bytes().map_err(|e| e.into())
        }
    }
}