summaryrefslogtreecommitdiff
path: root/src/cmd/raw.rs
blob: 0a4df869c3c5ab25c1d2037876d5d9e790496e3a (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
107
108
109
110
use base64::prelude::BASE64_STANDARD;
use base64::Engine;

use crate::cmd::{open_submaildir, MailStorage};
use crate::de_jmhoffmann_jwebmail_mailstorage::MIMEHeader_content_dispo as CD;
use crate::de_jmhoffmann_jwebmail_mailstorage::{Call_Raw, MIMEHeader, MIMEHeader_mime_type};
use crate::rfc822::parse_mail_content;

pub fn raw(
    ms: &MailStorage,
    call: &mut dyn Call_Raw,
    folder: String,
    mid: String,
    path: Option<String>,
) -> varlink::Result<()> {
    if let Some(p) = ms.maildir_path.read().unwrap().clone() {
        let md = open_submaildir(p, &folder);

        if let Some(mut mail) = md.find(&mid) {
            match path.as_deref() {
                Some("") | None => call.reply(
                    MIMEHeader {
                        mime_type: MIMEHeader_mime_type {
                            main_type: "message".to_owned(),
                            sub_type: "rfc822".to_owned(),
                        },
                        file_name: Some(mail.id().to_owned()),
                        content_dispo: CD::none,
                    },
                    BASE64_STANDARD
                        .encode(std::fs::read(mail.path()).map_err(varlink::map_context!())?),
                ),
                Some(mime_path) => {
                    if let Ok(path) = mime_path
                        .split('.')
                        .map(|x| x.parse())
                        .collect::<std::result::Result<Vec<usize>, std::num::ParseIntError>>()
                    {
                        let mut s;
                        let mut m = mail.parsed().unwrap();

                        if path[0] != 0 {
                            return call.reply_invalid_path_in_mail(
                                folder,
                                mid,
                                mime_path.to_owned(),
                            );
                        }

                        for i in &path[1..] {
                            match &m.ctype.mimetype {
                                x if x.starts_with("message/") => {
                                    if *i != 0 {
                                        return call.reply_invalid_path_in_mail(
                                            folder,
                                            mid,
                                            mime_path.to_owned(),
                                        );
                                    }
                                    s = m.get_body_raw().unwrap();
                                    m = mailparse::parse_mail(&s).unwrap();
                                }
                                x if x.starts_with("multipart/") => {
                                    if *i >= m.subparts.len() {
                                        return call.reply_invalid_path_in_mail(
                                            folder,
                                            mid,
                                            mime_path.to_owned(),
                                        );
                                    }
                                    m = m.subparts.swap_remove(*i);
                                }
                                _ => {
                                    return call.reply_invalid_path_in_mail(
                                        folder,
                                        mid,
                                        mime_path.to_owned(),
                                    );
                                }
                            }
                        }

                        if m.ctype.mimetype.starts_with("multipart/") {
                            return call.reply_invalid_path_in_mail(
                                folder,
                                mid,
                                mime_path.to_owned(),
                            );
                        }

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

                        call.reply(mime_part, BASE64_STANDARD.encode(content))
                    } else {
                        call.reply_invalid_path_in_mail(folder, mid, mime_path.to_owned())
                    }
                }
            }
        } else {
            call.reply_invalid_mid(folder, mid)
        }
    } else {
        call.reply_not_initialized()
    }
}