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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
macro_rules! def_cclass {
( $typ:ident, $test:ident) => {
#[derive(Debug, Clone, PartialEq)]
pub struct $typ(pub Vec<u8>);
impl Parsable for $typ {
fn parse(input: &[u8]) -> Result<(Self, &[u8]), ParseError> {
let mut pos: usize = 0;
let mut output: Vec<u8> = Vec::new();
while pos < input.len() && $test(input[pos]) {
output.push(input[pos]);
pos += 1;
}
if output.len() > 0 {
Ok( ($typ(output), &input[pos..]) )
}
else {
if pos >= input.len() { Err( ParseError::Eof("$typ") ) }
else { Err( ParseError::NotFound("$typ") ) }
}
}
}
impl Streamable for $typ {
fn stream<W: Write>(&self, w: &mut W) -> Result<usize, IoError> {
Ok(try!(w.write(&self.0[..])))
}
}
};
}
macro_rules! parse {
($pth:ident, $rem:ident) => {
{
$pth::parse($rem).map(|(value, r)| { $rem = r; value })
}
};
}
macro_rules! req {
($rem:ident, $bytes:expr, $input:ident) => {
let len: usize = $bytes.len();
if $rem.len() < len {
return Err(ParseError::Eof("$bytes"));
}
if &$rem[0..len] != $bytes {
return Err(ParseError::Expected($bytes.to_vec()));
}
$rem = &$rem[len..];
};
}
macro_rules! impl_display {
($t:ty) => {
impl ::std::fmt::Display for $t {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
let mut output: Vec<u8> = Vec::new();
if let Err(_) = self.stream(&mut output) {
return Err(::std::fmt::Error);
}
unsafe {
write!(f, "{}", ::std::str::from_utf8_unchecked(&*output))
}
}
}
}
}
pub mod error;
pub use self::error::ParseError;
pub mod types;
pub mod headers;
use std::io::Write;
use std::io::Error as IoError;
use buf_read_ext::BufReadExt;
use ::TryFrom;
use self::headers::{Return, Received};
use self::headers::{ResentDate, ResentFrom, ResentSender, ResentTo, ResentCc, ResentBcc,
ResentMessageId};
use self::headers::{OrigDate, From, Sender, ReplyTo, To, Cc, Bcc, MessageId, InReplyTo,
References, Subject, Comments, Keywords, OptionalField};
pub trait Parsable: Sized {
fn parse(input: &[u8]) -> Result<(Self, &[u8]), ParseError>;
}
pub trait Streamable {
fn stream<W: Write>(&self, w: &mut W) -> Result<usize, IoError>;
}
#[derive(Debug, Clone, PartialEq)]
pub struct Trace {
return_path: Option<Return>,
received: Vec<Received>
}
impl Parsable for Trace {
fn parse(input: &[u8]) -> Result<(Self, &[u8]), ParseError> {
let mut rem = input;
let maybe_return = parse!(Return, rem).ok();
let mut received: Vec<Received> = Vec::new();
while let Ok(r) = parse!(Received, rem) {
received.push(r);
}
if received.len() < 1 { return Err(ParseError::NotFound("Trace")); }
Ok((Trace {
return_path: maybe_return,
received: received,
}, rem))
}
}
impl Streamable for Trace {
fn stream<W: Write>(&self, w: &mut W) -> Result<usize, IoError> {
let mut count: usize = 0;
if let Some(ref rp) = self.return_path {
count += try!(rp.stream(w));
}
for r in &self.received {
count += try!(r.stream(w));
}
Ok(count)
}
}
impl_display!(Trace);
#[derive(Debug, Clone, PartialEq)]
pub enum ResentField {
Date(ResentDate),
From(ResentFrom),
Sender(ResentSender),
To(ResentTo),
Cc(ResentCc),
Bcc(ResentBcc),
MessageId(ResentMessageId),
}
impl Parsable for ResentField {
fn parse(input: &[u8]) -> Result<(Self, &[u8]), ParseError> {
let mut rem = input;
if let Ok(x) = parse!(ResentDate, rem) {
return Ok((ResentField::Date(x), rem));
}
if let Ok(x) = parse!(ResentFrom, rem) {
return Ok((ResentField::From(x), rem));
}
if let Ok(x) = parse!(ResentSender, rem) {
return Ok((ResentField::Sender(x), rem));
}
if let Ok(x) = parse!(ResentTo, rem) {
return Ok((ResentField::To(x), rem));
}
if let Ok(x) = parse!(ResentCc, rem) {
return Ok((ResentField::Cc(x), rem));
}
if let Ok(x) = parse!(ResentBcc, rem) {
return Ok((ResentField::Bcc(x), rem));
}
if let Ok(x) = parse!(ResentMessageId, rem) {
return Ok((ResentField::MessageId(x), rem));
}
Err(ParseError::NotFound("Resent-Field"))
}
}
impl Streamable for ResentField {
fn stream<W: Write>(&self, w: &mut W) -> Result<usize, IoError> {
match *self {
ResentField::Date(ref x) => x.stream(w),
ResentField::From(ref x) => x.stream(w),
ResentField::Sender(ref x) => x.stream(w),
ResentField::To(ref x) => x.stream(w),
ResentField::Cc(ref x) => x.stream(w),
ResentField::Bcc(ref x) => x.stream(w),
ResentField::MessageId(ref x) => x.stream(w),
}
}
}
impl_display!(ResentField);
#[derive(Debug, Clone, PartialEq)]
pub enum Field {
OrigDate(OrigDate),
From(From),
Sender(Sender),
ReplyTo(ReplyTo),
To(To),
Cc(Cc),
Bcc(Bcc),
MessageId(MessageId),
InReplyTo(InReplyTo),
References(References),
Subject(Subject),
Comments(Comments),
Keywords(Keywords),
OptionalField(OptionalField),
}
impl Parsable for Field {
fn parse(input: &[u8]) -> Result<(Self, &[u8]), ParseError> {
let mut rem = input;
if let Ok(x) = parse!(OrigDate, rem) {
return Ok((Field::OrigDate(x), rem));
}
if let Ok(x) = parse!(From, rem) {
return Ok((Field::From(x), rem));
}
if let Ok(x) = parse!(Sender, rem) {
return Ok((Field::Sender(x), rem));
}
if let Ok(x) = parse!(ReplyTo, rem) {
return Ok((Field::ReplyTo(x), rem));
}
if let Ok(x) = parse!(To, rem) {
return Ok((Field::To(x), rem));
}
if let Ok(x) = parse!(Cc, rem) {
return Ok((Field::Cc(x), rem));
}
if let Ok(x) = parse!(Bcc, rem) {
return Ok((Field::Bcc(x), rem));
}
if let Ok(x) = parse!(MessageId, rem) {
return Ok((Field::MessageId(x), rem));
}
if let Ok(x) = parse!(InReplyTo, rem) {
return Ok((Field::InReplyTo(x), rem));
}
if let Ok(x) = parse!(References, rem) {
return Ok((Field::References(x), rem));
}
if let Ok(x) = parse!(Subject, rem) {
return Ok((Field::Subject(x), rem));
}
if let Ok(x) = parse!(Comments, rem) {
return Ok((Field::Comments(x), rem));
}
if let Ok(x) = parse!(Keywords, rem) {
return Ok((Field::Keywords(x), rem));
}
if let Ok(x) = parse!(OptionalField, rem) {
return Ok((Field::OptionalField(x), rem));
}
Err(ParseError::NotFound("Field"))
}
}
impl Streamable for Field {
fn stream<W: Write>(&self, w: &mut W) -> Result<usize, IoError> {
match *self {
Field::OrigDate(ref x) => x.stream(w),
Field::From(ref x) => x.stream(w),
Field::Sender(ref x) => x.stream(w),
Field::ReplyTo(ref x) => x.stream(w),
Field::To(ref x) => x.stream(w),
Field::Cc(ref x) => x.stream(w),
Field::Bcc(ref x) => x.stream(w),
Field::MessageId(ref x) => x.stream(w),
Field::InReplyTo(ref x) => x.stream(w),
Field::References(ref x) => x.stream(w),
Field::Subject(ref x) => x.stream(w),
Field::Comments(ref x) => x.stream(w),
Field::Keywords(ref x) => x.stream(w),
Field::OptionalField(ref x) => x.stream(w),
}
}
}
impl_display!(Field);
#[derive(Debug, Clone, PartialEq)]
pub struct ResentTraceBlock {
pub trace: Trace,
pub resent_fields: Vec<ResentField>,
}
impl Parsable for ResentTraceBlock {
fn parse(input: &[u8]) -> Result<(Self, &[u8]), ParseError> {
let mut rem = input;
if let Ok(t) = parse!(Trace, rem) {
let mut fields: Vec<ResentField> = Vec::new();
while let Ok(f) = parse!(ResentField, rem) {
fields.push(f);
}
if fields.len() == 0 {
Err(ParseError::ExpectedType("Resent Field"))
} else {
Ok((ResentTraceBlock {
trace: t,
resent_fields: fields
}, rem))
}
} else {
Err(ParseError::NotFound("Resent Trace Block"))
}
}
}
impl Streamable for ResentTraceBlock {
fn stream<W: Write>(&self, w: &mut W) -> Result<usize, IoError> {
let mut count: usize = 0;
count += try!(self.trace.stream(w));
for field in &self.resent_fields {
count += try!(field.stream(w));
}
Ok(count)
}
}
impl_display!(ResentTraceBlock);
#[derive(Debug, Clone, PartialEq)]
pub struct OptTraceBlock {
pub trace: Trace,
pub opt_fields: Vec<OptionalField>,
}
impl Parsable for OptTraceBlock {
fn parse(input: &[u8]) -> Result<(Self, &[u8]), ParseError> {
let mut rem = input;
if let Ok(t) = parse!(Trace, rem) {
let mut fields: Vec<OptionalField> = Vec::new();
while let Ok(f) = parse!(OptionalField, rem) {
fields.push(f);
}
if fields.len() == 0 {
Err(ParseError::ExpectedType("Optional Field"))
} else {
Ok((OptTraceBlock {
trace: t,
opt_fields: fields
}, rem))
}
} else {
Err(ParseError::NotFound("Opt Trace Block"))
}
}
}
impl Streamable for OptTraceBlock {
fn stream<W: Write>(&self, w: &mut W) -> Result<usize, IoError> {
let mut count: usize = 0;
count += try!(self.trace.stream(w));
for field in &self.opt_fields {
count += try!(field.stream(w));
}
Ok(count)
}
}
impl_display!(OptTraceBlock);
#[derive(Debug, Clone, PartialEq)]
pub enum TraceBlock {
Resent(ResentTraceBlock),
Opt(OptTraceBlock),
}
impl Parsable for TraceBlock {
fn parse(input: &[u8]) -> Result<(Self, &[u8]), ParseError> {
let mut rem = input;
if let Ok(block) = parse!(ResentTraceBlock, rem) {
Ok((TraceBlock::Resent(block), rem))
}
else if let Ok(block) = parse!(OptTraceBlock, rem) {
Ok((TraceBlock::Opt(block), rem))
}
else {
Err(ParseError::NotFound("Trace Block"))
}
}
}
impl Streamable for TraceBlock {
fn stream<W: Write>(&self, w: &mut W) -> Result<usize, IoError> {
match *self {
TraceBlock::Resent(ref block) => block.stream(w),
TraceBlock::Opt(ref block) => block.stream(w),
}
}
}
impl_display!(TraceBlock);
#[derive(Debug, Clone, PartialEq)]
pub struct Fields {
pub trace_blocks: Vec<TraceBlock>,
pub fields: Vec<Field>,
}
impl Parsable for Fields {
fn parse(input: &[u8]) -> Result<(Self, &[u8]), ParseError> {
let mut rem = input;
let mut trace_blocks: Vec<TraceBlock> = Vec::new();
while let Ok(tb) = parse!(TraceBlock, rem) {
trace_blocks.push(tb);
}
let mut fields: Vec<Field> = Vec::new();
while let Ok(f) = parse!(Field, rem) {
fields.push(f);
}
Ok((Fields {
trace_blocks: trace_blocks,
fields: fields,
}, rem))
}
}
impl Streamable for Fields {
fn stream<W: Write>(&self, w: &mut W) -> Result<usize, IoError> {
let mut count: usize = 0;
for tb in &self.trace_blocks {
count += try!(tb.stream(w));
}
for f in &self.fields {
count += try!(f.stream(w));
}
Ok(count)
}
}
impl_display!(Fields);
#[inline]
pub fn is_text(c: u8) -> bool {
(c>=1 && c<=9) || c==11 || c==12 || (c>=14 && c<=127)
}
def_cclass!(Text, is_text);
#[derive(Debug, Clone, PartialEq)]
pub struct Body(pub Vec<u8>);
impl Parsable for Body {
fn parse(mut input: &[u8]) -> Result<(Self, &[u8]), ParseError> {
let mut body: Vec<u8> = Vec::new();
let mut line_number: usize = 0;
loop {
line_number += 1;
let mut line: Vec<u8> = Vec::new();
match input.stream_until_token(b"\r\n", &mut line) {
Err(e) => return Err(ParseError::Io(e)),
Ok((_, found)) => {
let mut rem = &*line;
if let Ok(text) = parse!(Text, rem) {
if rem.len() > 0 {
return Err(ParseError::InvalidBodyChar(rem[0]));
}
if text.0.len() > 998 {
return Err(ParseError::LineTooLong(line_number));
}
body.extend(text.0.clone());
}
if !found { break; }
else { body.extend_from_slice(b"\r\n"); }
}
}
}
Ok((Body(body), input))
}
}
impl Streamable for Body {
fn stream<W: Write>(&self, w: &mut W) -> Result<usize, IoError> {
w.write(&self.0)
}
}
impl<'a> TryFrom<&'a [u8]> for Body {
type Error = ParseError;
fn try_from(input: &'a [u8]) -> Result<Body, ParseError> {
let (out,rem) = try!(Body::parse(input));
if rem.len() > 0 {
Err(ParseError::TrailingInput("Body", input.len() - rem.len()))
} else {
Ok(out)
}
}
}
impl<'a> TryFrom<&'a str> for Body {
type Error = ParseError;
fn try_from(input: &'a str) -> Result<Body, ParseError> {
TryFrom::try_from(input.as_bytes())
}
}
impl_display!(Body);
#[derive(Debug, Clone, PartialEq)]
pub struct Message {
pub fields: Fields,
pub body: Option<Body>
}
impl Parsable for Message {
fn parse(input: &[u8]) -> Result<(Self, &[u8]), ParseError> {
let mut rem = input;
if let Ok(fields) = parse!(Fields, rem) {
if &rem[..2] != b"\r\n" {
return Ok((Message {
fields: fields,
body: None,
}, rem));
}
rem = &rem[2..];
parse!(Body, rem).map(|b| (Message {
fields: fields,
body: Some(b),
}, rem))
} else {
Err(ParseError::NotFound("Message"))
}
}
}
impl Streamable for Message {
fn stream<W: Write>(&self, w: &mut W) -> Result<usize, IoError> {
let mut count: usize = 0;
count += try!(self.fields.stream(w));
if let Some(ref body) = self.body {
count += try!(w.write(b"\r\n"));
count += try!(body.stream(w));
}
Ok(count)
}
}
impl_display!(Message);