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
use std::borrow::Cow;

use bytes::{BufMut, BytesMut};
use itertools::Itertools;

use crate::decoding::Parsable;
use crate::encoding::Writable;
use crate::{error::STAGE_DECODING, NotEnoughData};
use crate::{InvalidData, ProtocolError};
use miltr_utils::ByteParsing;

/// (Silently) discard this mail without forwarding it
#[derive(Debug, Clone)]
pub struct Discard;

impl Discard {
    const CODE: u8 = b'd';
}

impl Parsable for Discard {
    const CODE: u8 = Self::CODE;

    fn parse(_buffer: BytesMut) -> Result<Self, ProtocolError> {
        Ok(Self)
    }
}

impl Writable for Discard {
    fn write(&self, _buffer: &mut BytesMut) {}

    fn len(&self) -> usize {
        0
    }

    fn code(&self) -> u8 {
        Self::CODE
    }

    fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

/// Reject this mail, informing the smtp client about it
#[derive(Debug, Clone)]
pub struct Reject;

impl Reject {
    const CODE: u8 = b'r';
}

impl Parsable for Reject {
    const CODE: u8 = Self::CODE;

    fn parse(_buffer: BytesMut) -> Result<Self, ProtocolError> {
        Ok(Self)
    }
}

impl Writable for Reject {
    fn write(&self, _buffer: &mut BytesMut) {}

    fn len(&self) -> usize {
        0
    }

    fn code(&self) -> u8 {
        Self::CODE
    }

    fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

/// Return a tempfail code to the smtp client
#[derive(Debug, Clone)]
pub struct Tempfail;

impl Tempfail {
    const CODE: u8 = b't';
}

impl Parsable for Tempfail {
    const CODE: u8 = Self::CODE;

    fn parse(_buffer: BytesMut) -> Result<Self, ProtocolError> {
        Ok(Self)
    }
}

impl Writable for Tempfail {
    fn write(&self, _buffer: &mut BytesMut) {}

    fn len(&self) -> usize {
        0
    }

    fn code(&self) -> u8 {
        Self::CODE
    }
    fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

/// Skip this mail processing
#[derive(Debug, Clone)]
pub struct Skip;

impl Skip {
    const CODE: u8 = b's';
}

impl Parsable for Skip {
    const CODE: u8 = Self::CODE;

    fn parse(_buffer: BytesMut) -> Result<Self, ProtocolError> {
        Ok(Self)
    }
}

impl Writable for Skip {
    fn write(&self, _buffer: &mut BytesMut) {}

    fn len(&self) -> usize {
        0
    }

    fn code(&self) -> u8 {
        Self::CODE
    }

    fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

const REPLY_CODE_LENGTH: usize = 3;
/// Return this status code to the smtp client
#[derive(Debug, Clone)]
pub struct Replycode {
    rcode: Code,
    xcode: Code,
    message: BytesMut,
}

impl Replycode {
    const CODE: u8 = b'y';

    /// Create a Replycode
    #[must_use]
    #[allow(clippy::similar_names)]
    pub fn new<R: Into<Code>, X: Into<Code>>(rcode: R, xcode: X, message: &str) -> Self {
        let rcode = rcode.into();
        let xcode = xcode.into();

        Self {
            rcode,
            xcode,
            message: BytesMut::from(message.as_bytes()),
        }
    }

    /// The message associated with this reply code
    #[must_use]
    pub fn message(&self) -> Cow<str> {
        String::from_utf8_lossy(&self.message)
    }

    /// The smtp return code
    #[must_use]
    pub fn rcode(&self) -> &Code {
        &self.rcode
    }

    /// The smtp enhanced return code
    #[must_use]
    pub fn xcode(&self) -> &Code {
        &self.xcode
    }
}

impl Parsable for Replycode {
    const CODE: u8 = Self::CODE;

    // rcode and xcode are just named that in the docs. Keeping it consistent.
    #[allow(clippy::similar_names)]
    fn parse(mut buffer: BytesMut) -> Result<Self, ProtocolError> {
        #[allow(clippy::similar_names)]
        let Some(rcode) = buffer.delimited(0) else {
            return Err(NotEnoughData::new(
                STAGE_DECODING,
                "Replycode",
                "Missing nullbyte delimiter after rcode",
                1,
                0,
                buffer,
            )
            .into());
        };
        let rcode = Code::parse(rcode)?;

        let Some(xcode) = buffer.delimited(0) else {
            return Err(NotEnoughData::new(
                STAGE_DECODING,
                "Replycode",
                "Missing nullbyte delimiter after xcode",
                1,
                0,
                buffer,
            )
            .into());
        };
        let xcode = Code::parse(xcode)?;

        let Some(message) = buffer.delimited(0) else {
            return Err(NotEnoughData::new(
                STAGE_DECODING,
                "Replycode",
                "Missing nullbyte delimiter after message",
                1,
                0,
                buffer,
            )
            .into());
        };

        Ok(Self {
            rcode,
            xcode,
            message,
        })
    }
}

impl Writable for Replycode {
    fn write(&self, buffer: &mut BytesMut) {
        buffer.put_slice(self.rcode.as_bytes());
        buffer.put_u8(0);
        buffer.put_slice(self.xcode.as_bytes());
        buffer.put_u8(0);
        buffer.put_slice(&self.message);
        buffer.put_u8(0);
    }

    fn len(&self) -> usize {
        self.rcode.len() + 1 + self.xcode.len() + 1 + self.message.len() + 1
    }

    fn code(&self) -> u8 {
        Self::CODE
    }
    fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

#[derive(Debug, Clone)]
pub struct Code {
    code: [u16; REPLY_CODE_LENGTH],
    bytes: BytesMut,
}

impl From<[u16; REPLY_CODE_LENGTH]> for Code {
    fn from(code: [u16; REPLY_CODE_LENGTH]) -> Self {
        Self::new(code)
    }
}

impl Code {
    pub fn new(code: [u16; REPLY_CODE_LENGTH]) -> Self {
        Self {
            code,
            bytes: BytesMut::from_iter(code.iter().map(ToString::to_string).join(".").as_bytes()),
        }
    }

    fn parse(buffer: BytesMut) -> Result<Self, InvalidData> {
        let mut positions = buffer.iter().positions(|&c| c == b'.');
        let mut code: [u16; 3] = [0_u16; REPLY_CODE_LENGTH];

        let mut start = 0;
        for c_code in code.iter_mut().take(REPLY_CODE_LENGTH - 1) {
            let Some(end) = positions.next() else {
                return Err(InvalidData {
                    msg: "missing '.' delimiter in code",
                    offending_bytes: buffer,
                });
            };
            let raw = &buffer[start..end];
            let Ok(number) = String::from_utf8_lossy(raw).parse() else {
                return Err(InvalidData {
                    msg: "invalid u16 in code",
                    offending_bytes: buffer,
                });
            };

            *c_code = number;
            start = end + 1;
        }
        let raw = &buffer[start..buffer.len()];
        let Ok(number) = String::from_utf8_lossy(raw).parse() else {
            return Err(InvalidData {
                msg: "invalid u16 in code",
                offending_bytes: buffer,
            });
        };

        code[REPLY_CODE_LENGTH - 1] = number;

        Ok(Self {
            code,
            bytes: buffer,
        })
    }

    /// The status code
    #[must_use]
    pub fn code(&self) -> [u16; REPLY_CODE_LENGTH] {
        self.code
    }

    fn as_bytes(&self) -> &[u8] {
        &self.bytes
    }

    fn len(&self) -> usize {
        self.bytes.len()
    }
}

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

    #[test]
    fn test_rcode_valid() {
        let input = BytesMut::from_iter(b"1.20.3");
        let code = Code::parse(input).expect("Failed parsing input");

        assert_eq!(code.code, [1, 20, 3]);

        println!("{:?}", code.bytes);
        assert_eq!(6, code.bytes.len());
    }

    #[test]
    fn test_rcode_invalid() {
        let input = BytesMut::from_iter(b"1.23");
        let _code = Code::parse(input).expect_err("Parsing did not error on invalid");
    }
}