summaryrefslogtreecommitdiffstats
path: root/smtp/src/request.rs
blob: 0a4c094c5dd6ab3eda9cc3be61279452a2d13e3e (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
use std::mem;

use new_tokio_smtp::send_mail::{self as smtp, EnvelopData, MailAddress};

use headers::{
    error::BuildInValidationError,
    header_components::Mailbox,
    headers::{Sender, _From, _To},
};
use mail::{
    error::{MailError, OtherValidationError},
    Mail,
};
use mail_internals::{
    encoder::{EncodableInHeader, EncodingBuffer},
    error::EncodingError,
    MailType,
};

use error::OtherValidationError as AnotherOtherValidationError;

/// This type contains a mail and potentially some envelop data.
///
/// It is needed as in some edge cases the smtp envelop data (i.e.
/// smtp from and smtp recipient) can not be correctly derived
/// from the mail.
///
/// The default usage is to directly turn a `Mail` into a `MailRequest`
/// by either using  `MailRequest::new`, `MailRequest::from` or `Mail::into`.
///
#[derive(Clone, Debug)]
pub struct MailRequest {
    mail: Mail,
    envelop_data: Option<EnvelopData>,
}

impl From<Mail> for MailRequest {
    fn from(mail: Mail) -> Self {
        MailRequest::new(mail)
    }
}

impl MailRequest {
    /// creates a new `MailRequest` from a `Mail` instance
    pub fn new(mail: Mail) -> Self {
        MailRequest {
            mail,
            envelop_data: None,
        }
    }

    /// create a new `MailRequest` and use custom smtp `EnvelopData`
    ///
    /// Note that envelop data comes from `new-tokio-smtp::send_mail` and
    /// is not re-exported so if you happen to run into one of the view
    /// cases where you need to set it manually just import it from
    /// `new-tokio-smtp`.
    pub fn new_with_envelop(mail: Mail, envelop: EnvelopData) -> Self {
        MailRequest {
            mail,
            envelop_data: Some(envelop),
        }
    }

    /// replace the smtp `EnvelopData`
    pub fn override_envelop(&mut self, envelop: EnvelopData) -> Option<EnvelopData> {
        mem::replace(&mut self.envelop_data, Some(envelop))
    }

    pub fn _into_mail_with_envelop(self) -> Result<(Mail, EnvelopData), MailError> {
        let envelop = if let Some(envelop) = self.envelop_data {
            envelop
        } else {
            derive_envelop_data_from_mail(&self.mail)?
        };

        Ok((self.mail, envelop))
    }

    #[cfg(not(feature = "extended-api"))]
    #[inline(always)]
    pub(crate) fn into_mail_with_envelop(self) -> Result<(Mail, EnvelopData), MailError> {
        self._into_mail_with_envelop()
    }

    /// Turns this type into the contained mail an associated envelop data.
    ///
    /// If envelop data was explicitly set it is returned.
    /// If no envelop data was explicitly given it is derived from the
    /// Mail header fields using `derive_envelop_data_from_mail`.
    #[cfg(feature = "extended-api")]
    #[inline(always)]
    pub fn into_mail_with_envelop(self) -> Result<(Mail, EnvelopData), MailError> {
        self._into_mail_with_envelop()
    }
}

fn mailaddress_from_mailbox(mailbox: &Mailbox) -> Result<MailAddress, EncodingError> {
    let email = &mailbox.email;
    let needs_smtputf8 = email.check_if_internationalized();
    let mt = if needs_smtputf8 {
        MailType::Internationalized
    } else {
        MailType::Ascii
    };
    let mut buffer = EncodingBuffer::new(mt);
    {
        let mut writer = buffer.writer();
        email.encode(&mut writer)?;
        writer.commit_partial_header();
    }
    let raw: Vec<u8> = buffer.into();
    let address = String::from_utf8(raw).expect("[BUG] encoding Email produced non utf8 data");
    Ok(MailAddress::new_unchecked(address, needs_smtputf8))
}

/// Generates envelop data based on the given Mail.
///
/// If a sender header is given smtp will use this
/// as smtp from else the single mailbox in from
/// is used as smtp from.
///
/// All `To`'s are used as smtp recipients.
///
/// **`Cc`/`Bcc` is currently no supported/has no
/// special handling**
///
/// # Error
///
/// An error is returned if there is:
///
/// - No From header
/// - No To header
/// - A From header with multiple addresses but no Sender header
///
pub fn derive_envelop_data_from_mail(mail: &Mail) -> Result<smtp::EnvelopData, MailError> {
    let headers = mail.headers();
    let smtp_from = if let Some(sender) = headers.get_single(Sender) {
        let sender = sender?;
        //TODO double check with from field
        mailaddress_from_mailbox(sender)?
    } else {
        let from = headers
            .get_single(_From)
            .ok_or(OtherValidationError::NoFrom)??;

        if from.len() > 1 {
            return Err(BuildInValidationError::MultiMailboxFromWithoutSender.into());
        }

        mailaddress_from_mailbox(from.first())?
    };

    let smtp_to = if let Some(to) = headers.get_single(_To) {
        let to = to?;
        to.try_mapped_ref(mailaddress_from_mailbox)?
    } else {
        return Err(AnotherOtherValidationError::NoTo.into());
    };

    //TODO Cc, Bcc

    Ok(EnvelopData {
        from: Some(smtp_from),
        to: smtp_to,
    })
}

#[cfg(test)]
mod test {

    mod derive_envelop_data_from_mail {
        use super::super::derive_envelop_data_from_mail;
        use headers::headers::{Sender, _From, _To};
        use mail::{test_utils::CTX, Mail, Resource};

        fn mock_resource() -> Resource {
            Resource::plain_text("abcd↓efg", CTX.unwrap())
        }

        #[test]
        fn use_sender_if_given() {
            let mut mail = Mail::new_singlepart_mail(mock_resource());

            mail.insert_headers(
                headers! {
                    Sender: "strange@caffe.test",
                    _From: ["ape@caffe.test", "epa@caffe.test"],
                    _To: ["das@ding.test"]
                }
                .unwrap(),
            );

            let envelop_data = derive_envelop_data_from_mail(&mail).unwrap();

            assert_eq!(
                envelop_data.from.as_ref().unwrap().as_str(),
                "strange@caffe.test"
            );
        }

        #[test]
        fn use_from_if_no_sender_given() {
            let mut mail = Mail::new_singlepart_mail(mock_resource());
            mail.insert_headers(
                headers! {
                    _From: ["ape@caffe.test"],
                    _To: ["das@ding.test"]
                }
                .unwrap(),
            );

            let envelop_data = derive_envelop_data_from_mail(&mail).unwrap();

            assert_eq!(
                envelop_data.from.as_ref().unwrap().as_str(),
                "ape@caffe.test"
            );
        }

        #[test]
        fn fail_if_no_sender_but_multi_mailbox_from() {
            let mut mail = Mail::new_singlepart_mail(mock_resource());
            mail.insert_headers(
                headers! {
                    _From: ["ape@caffe.test", "a@b.test"],
                    _To: ["das@ding.test"]
                }
                .unwrap(),
            );

            let envelop_data = derive_envelop_data_from_mail(&mail);

            //assert is_err
            envelop_data.unwrap_err();
        }

        #[test]
        fn use_to() {
            let mut mail = Mail::new_singlepart_mail(mock_resource());
            mail.insert_headers(
                headers! {
                    _From: ["ape@caffe.test"],
                    _To: ["das@ding.test"]
                }
                .unwrap(),
            );

            let envelop_data = derive_envelop_data_from_mail(&mail).unwrap();

            assert_eq!(envelop_data.to.first().as_str(), "das@ding.test");
        }
    }

    mod mailaddress_from_mailbox {
        use super::super::mailaddress_from_mailbox;
        use headers::{
            header_components::{Email, Mailbox},
            HeaderTryFrom,
        };

        #[test]
        #[cfg_attr(not(feature = "test-with-traceing"), ignore)]
        fn does_not_panic_with_tracing_enabled() {
            let mb = Mailbox::try_from("hy@b").unwrap();
            mailaddress_from_mailbox(&mb).unwrap();
        }

        #[test]
        fn correctly_converts_mailbox() {
            let mb = Mailbox::from(Email::new("tast@tost.test").unwrap());
            let address = mailaddress_from_mailbox(&mb).unwrap();
            assert_eq!(address.as_str(), "tast@tost.test");
            assert_eq!(address.needs_smtputf8(), false);
        }

        #[test]
        fn tracks_if_smtputf8_is_needed() {
            let mb = Mailbox::from(Email::new("tüst@tost.test").unwrap());
            let address = mailaddress_from_mailbox(&mb).unwrap();
            assert_eq!(address.as_str(), "tüst@tost.test");
            assert_eq!(address.needs_smtputf8(), true);
        }

        #[test]
        fn puny_encodes_domain_if_smtputf8_is_not_needed() {
            let mb = Mailbox::from(Email::new("tast@tüst.test").unwrap());
            let address = mailaddress_from_mailbox(&mb).unwrap();
            assert_eq!(address.as_str(), "tast@xn--tst-hoa.test");
            assert_eq!(address.needs_smtputf8(), false);
        }

        #[test]
        fn does_not_puny_encodes_domain_if_smtputf8_is_needed() {
            let mb = Mailbox::from(Email::new("töst@tüst.test").unwrap());
            let address = mailaddress_from_mailbox(&mb).unwrap();
            assert_eq!(address.as_str(), "töst@tüst.test");
            assert_eq!(address.needs_smtputf8(), true);
        }
    }
}