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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
use serde::{Deserialize, Serialize};
use std::time::Duration;

#[derive(Serialize, Deserialize, Copy, Clone, Debug, PartialEq)]
pub struct SmtpClientTimeouts {
    #[serde(
        default = "SmtpClientTimeouts::default_connect_timeout",
        with = "duration_serde"
    )]
    pub connect_timeout: Duration,

    #[serde(
        default = "SmtpClientTimeouts::default_ehlo_timeout",
        with = "duration_serde"
    )]
    pub ehlo_timeout: Duration,

    #[serde(
        default = "SmtpClientTimeouts::default_mail_from_timeout",
        with = "duration_serde"
    )]
    pub mail_from_timeout: Duration,

    #[serde(
        default = "SmtpClientTimeouts::default_rcpt_to_timeout",
        with = "duration_serde"
    )]
    pub rcpt_to_timeout: Duration,

    #[serde(
        default = "SmtpClientTimeouts::default_data_timeout",
        with = "duration_serde"
    )]
    pub data_timeout: Duration,
    #[serde(
        default = "SmtpClientTimeouts::default_data_dot_timeout",
        with = "duration_serde"
    )]
    pub data_dot_timeout: Duration,
    #[serde(
        default = "SmtpClientTimeouts::default_rset_timeout",
        with = "duration_serde"
    )]
    pub rset_timeout: Duration,

    #[serde(
        default = "SmtpClientTimeouts::default_idle_timeout",
        with = "duration_serde"
    )]
    pub idle_timeout: Duration,

    #[serde(
        default = "SmtpClientTimeouts::default_starttls_timeout",
        with = "duration_serde"
    )]
    pub starttls_timeout: Duration,

    #[serde(
        default = "SmtpClientTimeouts::default_auth_timeout",
        with = "duration_serde"
    )]
    pub auth_timeout: Duration,
}

impl Default for SmtpClientTimeouts {
    fn default() -> Self {
        Self {
            connect_timeout: Self::default_connect_timeout(),
            ehlo_timeout: Self::default_ehlo_timeout(),
            mail_from_timeout: Self::default_mail_from_timeout(),
            rcpt_to_timeout: Self::default_rcpt_to_timeout(),
            data_timeout: Self::default_data_timeout(),
            data_dot_timeout: Self::default_data_dot_timeout(),
            rset_timeout: Self::default_rset_timeout(),
            idle_timeout: Self::default_idle_timeout(),
            starttls_timeout: Self::default_starttls_timeout(),
            auth_timeout: Self::default_auth_timeout(),
        }
    }
}

impl SmtpClientTimeouts {
    fn default_connect_timeout() -> Duration {
        Duration::from_secs(60)
    }
    fn default_auth_timeout() -> Duration {
        Duration::from_secs(60)
    }
    fn default_ehlo_timeout() -> Duration {
        Duration::from_secs(300)
    }
    fn default_mail_from_timeout() -> Duration {
        Duration::from_secs(300)
    }
    fn default_rcpt_to_timeout() -> Duration {
        Duration::from_secs(300)
    }
    fn default_data_timeout() -> Duration {
        Duration::from_secs(300)
    }
    fn default_data_dot_timeout() -> Duration {
        Duration::from_secs(300)
    }
    fn default_rset_timeout() -> Duration {
        Duration::from_secs(5)
    }
    fn default_idle_timeout() -> Duration {
        Duration::from_secs(5)
    }
    fn default_starttls_timeout() -> Duration {
        Duration::from_secs(5)
    }

    pub fn short_timeouts() -> Self {
        let short = Duration::from_secs(20);
        Self {
            connect_timeout: short,
            ehlo_timeout: short,
            mail_from_timeout: short,
            rcpt_to_timeout: short,
            data_timeout: short,
            data_dot_timeout: short,
            rset_timeout: short,
            idle_timeout: short,
            starttls_timeout: short,
            auth_timeout: short,
        }
    }

    /// Compute theoretical maximum lifetime of a single message send
    pub fn total_message_send_duration(&self) -> Duration {
        self.connect_timeout
            + self.ehlo_timeout
            + self.auth_timeout
            + self.mail_from_timeout
            + self.rcpt_to_timeout
            + self.data_timeout
            + self.data_dot_timeout
            + self.starttls_timeout
            + self.idle_timeout
    }
}

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone, Hash)]
pub struct Response {
    pub code: u16,
    pub enhanced_code: Option<EnhancedStatusCode>,
    #[serde(serialize_with = "as_single_line")]
    pub content: String,
    pub command: Option<String>,
}

impl Response {
    pub fn to_single_line(&self) -> String {
        let mut line = format!("{} ", self.code);

        if let Some(enh) = &self.enhanced_code {
            line.push_str(&format!("{}.{}.{} ", enh.class, enh.subject, enh.detail));
        }

        line.push_str(&remove_line_break(&self.content));

        line
    }

    pub fn is_transient(&self) -> bool {
        self.code >= 400 && self.code < 500
    }

    pub fn is_permanent(&self) -> bool {
        self.code >= 500 && self.code < 600
    }

    pub fn with_code_and_message(code: u16, message: &str) -> Self {
        let lines: Vec<&str> = message.lines().collect();

        let mut builder = ResponseBuilder::new(&ResponseLine {
            code,
            content: lines[0],
            is_final: lines.len() == 1,
        });

        for (n, line) in lines.iter().enumerate().skip(1) {
            builder
                .add_line(&ResponseLine {
                    code,
                    content: line,
                    is_final: n == lines.len() - 1,
                })
                .ok();
        }

        builder.build(None)
    }
}

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone, Copy, Hash)]
pub struct EnhancedStatusCode {
    pub class: u8,
    pub subject: u16,
    pub detail: u16,
}

fn parse_enhanced_status_code(line: &str) -> Option<(EnhancedStatusCode, &str)> {
    let mut fields = line.splitn(3, '.');
    let class = fields.next()?.parse::<u8>().ok()?;
    if !matches!(class, 2 | 4 | 5) {
        // No other classes are defined
        return None;
    }
    let subject = fields.next()?.parse::<u16>().ok()?;

    let remainder = fields.next()?;
    let mut fields = remainder.splitn(2, ' ');
    let detail = fields.next()?.parse::<u16>().ok()?;
    let remainder = fields.next()?;

    Some((
        EnhancedStatusCode {
            class,
            subject,
            detail,
        },
        remainder,
    ))
}

fn remove_line_break(data: &str) -> String {
    let data = data.as_bytes();
    let mut normalized = Vec::with_capacity(data.len());
    let mut last_idx = 0;

    for i in memchr::memchr2_iter(b'\r', b'\n', data) {
        match data[i] {
            b'\r' => {
                normalized.extend_from_slice(&data[last_idx..i]);
                if data.get(i + 1).copied() != Some(b'\n') {
                    normalized.push(b' ');
                }
            }
            b'\n' => {
                normalized.extend_from_slice(&data[last_idx..i]);
                normalized.push(b' ');
            }
            _ => unreachable!(),
        }
        last_idx = i + 1;
    }

    normalized.extend_from_slice(&data[last_idx..]);
    // This is safe because data comes from str, which is
    // guaranteed to be valid utf8, and all we're manipulating
    // above is whitespace which won't invalidate the utf8
    // byte sequences in the data byte array
    unsafe { String::from_utf8_unchecked(normalized) }
}

#[derive(Debug, PartialEq, Eq)]
pub(crate) struct ResponseLine<'a> {
    pub code: u16,
    pub is_final: bool,
    pub content: &'a str,
}

impl<'a> ResponseLine<'a> {
    /// Reconsitute the original line that we parsed
    fn to_original_line(&self) -> String {
        format!(
            "{}{}{}",
            self.code,
            if self.is_final { " " } else { "-" },
            self.content
        )
    }
}

pub(crate) struct ResponseBuilder {
    pub code: u16,
    pub enhanced_code: Option<EnhancedStatusCode>,
    pub content: String,
}

impl ResponseBuilder {
    pub fn new(parsed: &ResponseLine) -> Self {
        let code = parsed.code;
        let (enhanced_code, content) = match parse_enhanced_status_code(parsed.content) {
            Some((enhanced, content)) => (Some(enhanced), content.to_string()),
            None => (None, parsed.content.to_string()),
        };

        Self {
            code,
            enhanced_code,
            content,
        }
    }

    pub fn add_line(&mut self, parsed: &ResponseLine) -> Result<(), String> {
        if parsed.code != self.code {
            return Err(parsed.to_original_line());
        }

        self.content.push('\n');

        let mut content = parsed.content;

        if let Some(enh) = &self.enhanced_code {
            let prefix = format!("{}.{}.{} ", enh.class, enh.subject, enh.detail);
            if let Some(remainder) = parsed.content.strip_prefix(&prefix) {
                content = remainder;
            }
        }

        self.content.push_str(content);
        Ok(())
    }

    pub fn build(self, command: Option<String>) -> Response {
        Response {
            code: self.code,
            content: self.content,
            enhanced_code: self.enhanced_code,
            command,
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn remove_crlf() {
        fn remove(s: &str, expect: &str) {
            assert_eq!(remove_line_break(s), expect, "input: {s:?}");
        }

        remove("hello\r\nthere\r\n", "hello there ");
        remove("hello\r", "hello ");
        remove("hello\nthere\r\n", "hello there ");
        remove("hello\r\nthere\n", "hello there ");
        remove("hello\r\r\r\nthere\n", "hello   there ");
    }

    #[test]
    fn response_parsing() {
        assert_eq!(
            parse_enhanced_status_code("2.0.1 w00t"),
            Some((
                EnhancedStatusCode {
                    class: 2,
                    subject: 0,
                    detail: 1
                },
                "w00t"
            ))
        );

        assert_eq!(parse_enhanced_status_code("3.0.0 w00t"), None);

        assert_eq!(parse_enhanced_status_code("2.0.0.1 w00t"), None);

        assert_eq!(parse_enhanced_status_code("2.0.0.1w00t"), None);
    }
}

fn as_single_line<S>(content: &String, serializer: S) -> Result<S::Ok, S::Error>
where
    S: serde::Serializer,
{
    serializer.serialize_str(&remove_line_break(content))
}