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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
//! Module that implements parsers for all of the IMAP types.

mod literal;

#[cfg(test)]
mod tests;

use std::fmt::Debug;
use std::str::FromStr;

use chrono::{DateTime, FixedOffset, TimeZone};
use pest::{error::Error, iterators::Pair, Parser};

use crate::response::*;

use self::literal::literal_internal;

#[derive(Parser)]
#[grammar = "parser/rfc3501.pest"]

struct Rfc3501;

pub type ParseResult<T, E = Error<Rule>> = Result<T, E>;

macro_rules! parse_fail {
    ($($tt:tt)*) => {
        { error!($($tt)*); panic!(); }
    };
}

pub fn parse_capability(s: impl AsRef<str>) -> ParseResult<Capability> {
    let mut pairs = Rfc3501::parse(Rule::capability, s.as_ref())?;
    let pair = pairs.next().unwrap();
    Ok(build_capability(pair))
}

pub fn parse_streamed_response(s: impl AsRef<str>) -> ParseResult<(Response, usize)> {
    let s = s.as_ref();
    let mut pairs = match Rfc3501::parse(Rule::streamed_response, s) {
        Ok(v) => v,
        Err(e) => {
            // error!("stream failed with len {}: {}", len ,e);
            return Err(e);
        }
    };
    let pair = unwrap1(pairs.next().unwrap());
    let span = pair.as_span();
    let range = span.end() - span.start();
    let response = build_response(pair);
    Ok((response, range))
}

pub fn parse_response(s: impl AsRef<str>) -> ParseResult<Response> {
    let mut pairs = Rfc3501::parse(Rule::response, s.as_ref())?;
    let pair = pairs.next().unwrap();
    Ok(build_response(pair))
}

fn build_response(pair: Pair<Rule>) -> Response {
    assert!(matches!(pair.as_rule(), Rule::response));
    let pair = unwrap1(pair);
    match pair.as_rule() {
        Rule::response_done => build_response_done(pair),
        Rule::response_data => build_response_data(pair),
        Rule::continue_req => build_continue_req(pair),
        _ => unreachable!("{:#?}", pair),
    }
}

fn build_response_done(pair: Pair<Rule>) -> Response {
    assert!(matches!(pair.as_rule(), Rule::response_done));
    let mut pairs = pair.into_inner();
    let pair = pairs.next().unwrap();
    match pair.as_rule() {
        Rule::response_tagged => {
            let mut pairs = pair.into_inner();
            let pair = pairs.next().unwrap();
            let tag = pair.as_str().to_owned();

            let pair = pairs.next().unwrap();
            let (status, code, information) = build_resp_cond_state(pair);
            Response::Done(ResponseDone {
                tag,
                status,
                code,
                information,
            })
        }
        _ => unreachable!("{:#?}", pair),
    }
}

fn build_response_data(pair: Pair<Rule>) -> Response {
    assert!(matches!(pair.as_rule(), Rule::response_data));
    let mut pairs = pair.into_inner();
    let pair = pairs.next().unwrap();
    match pair.as_rule() {
        Rule::resp_cond_state => {
            let (status, code, information) = build_resp_cond_state(pair);
            Response::Data(ResponseData {
                status,
                code,
                information,
            })
        }
        Rule::mailbox_data => Response::MailboxData(build_mailbox_data(pair)),
        Rule::capability_data => Response::Capabilities(build_capabilities(pair)),
        Rule::message_data => {
            let mut pairs = pair.into_inner();
            let pair = pairs.next().unwrap();
            let seq: u32 = build_number(pair);

            let pair = pairs.next().unwrap();
            match pair.as_rule() {
                Rule::message_data_expunge => Response::Expunge(seq),
                Rule::message_data_fetch => {
                    let mut pairs = pair.into_inner();
                    let msg_att = pairs.next().unwrap();
                    let attrs = msg_att.into_inner().map(build_msg_att).collect();
                    Response::Fetch(seq, attrs)
                }
                _ => unreachable!("{:#?}", pair),
            }
        }
        _ => unreachable!("{:#?}", pair),
    }
}

fn build_continue_req(pair: Pair<Rule>) -> Response {
    assert!(matches!(pair.as_rule(), Rule::continue_req));
    let (code, s) = build_resp_text(unwrap1(pair));
    Response::Continue {
        code,
        information: Some(s),
    }
}

fn build_resp_text(pair: Pair<Rule>) -> (Option<ResponseCode>, String) {
    assert!(matches!(pair.as_rule(), Rule::resp_text));
    let mut pairs = pair.into_inner();
    let mut pair = pairs.next().unwrap();
    let mut resp_code = None;
    if let Rule::resp_text_code = pair.as_rule() {
        resp_code = build_resp_text_code(pair);
        pair = pairs.next().unwrap();
    }
    assert!(matches!(pair.as_rule(), Rule::text));
    let s = pair.as_str().to_owned();
    (resp_code, s)
}

fn build_msg_att(pair: Pair<Rule>) -> AttributeValue {
    if !matches!(pair.as_rule(), Rule::msg_att_dyn_or_stat) {
        unreachable!("{:#?}", pair);
    }

    let mut pairs = pair.into_inner();
    let pair = pairs.next().unwrap();

    match pair.as_rule() {
        Rule::msg_att_dynamic => AttributeValue::Flags(pair.into_inner().map(build_flag).collect()),
        Rule::msg_att_static => build_msg_att_static(pair),
        _ => unreachable!("{:#?}", pair),
    }
}

fn build_msg_att_static(pair: Pair<Rule>) -> AttributeValue {
    assert!(matches!(pair.as_rule(), Rule::msg_att_static));

    let mut pairs = pair.into_inner();
    let pair = pairs.next().unwrap();

    match pair.as_rule() {
        Rule::msg_att_static_internaldate => {
            AttributeValue::InternalDate(build_date_time(unwrap1(pair)))
        }
        Rule::msg_att_static_rfc822_size => AttributeValue::Rfc822Size(build_number(unwrap1(pair))),
        Rule::msg_att_static_envelope => AttributeValue::Envelope(build_envelope(unwrap1(pair))),
        // TODO: do this
        Rule::msg_att_static_body_structure => AttributeValue::BodySection(BodySection {
            section: None,
            index: None,
            data: None,
        }),
        Rule::msg_att_static_body_section => {
            let mut pairs = pair.into_inner();
            let section = None;
            pairs.next();
            let index = match pairs.peek().unwrap().as_rule() {
                Rule::number => Some(build_number(unwrap1(pairs.next().unwrap()))),
                _ => None,
            };
            let data = build_nstring(pairs.next().unwrap());
            AttributeValue::BodySection(BodySection {
                section,
                index,
                data,
            })
        }
        Rule::msg_att_static_uid => AttributeValue::Uid(build_number(unwrap1(unwrap1(pair)))),
        _ => parse_fail!("{:#?}", pair),
    }
}

fn build_section(pair: Pair<Rule>) -> () {
    assert!(matches!(pair.as_rule(), Rule::section));
}

fn build_envelope(pair: Pair<Rule>) -> Envelope {
    // TODO: do this
    let mut pairs = pair.into_inner();
    let date = build_nstring(unwrap1(pairs.next().unwrap()));
    let subject = build_nstring(unwrap1(pairs.next().unwrap()));

    let address1 = |r: Rule, pair: Pair<Rule>| -> Option<Vec<Address>> {
        assert!(matches!(pair.as_rule(), r));
        let pair = unwrap1(pair);
        match pair.as_rule() {
            Rule::nil => None,
            Rule::env_address1 => Some(pair.into_inner().map(build_address).collect()),
            _ => unreachable!("{:?}", pair),
        }
    };

    let from = address1(Rule::env_from, pairs.next().unwrap());
    let sender = address1(Rule::env_sender, pairs.next().unwrap());
    let reply_to = address1(Rule::env_reply_to, pairs.next().unwrap());
    let to = address1(Rule::env_to, pairs.next().unwrap());
    let cc = address1(Rule::env_cc, pairs.next().unwrap());
    let bcc = address1(Rule::env_bcc, pairs.next().unwrap());
    let in_reply_to = build_nstring(unwrap1(pairs.next().unwrap()));
    let message_id = build_nstring(unwrap1(pairs.next().unwrap()));

    Envelope {
        date,
        subject,
        from,
        sender,
        reply_to,
        to,
        cc,
        bcc,
        in_reply_to,
        message_id,
    }
}

fn build_resp_cond_state(pair: Pair<Rule>) -> (Status, Option<ResponseCode>, Option<String>) {
    if !matches!(pair.as_rule(), Rule::resp_cond_state) {
        unreachable!("{:#?}", pair);
    }

    let mut pairs = pair.into_inner();
    let pair = pairs.next().unwrap();
    let status = build_status(pair);
    let mut code = None;
    let mut information = None;

    let pair = pairs.next().unwrap();
    let pairs = pair.into_inner();
    for pair in pairs {
        match pair.as_rule() {
            Rule::resp_text_code => code = build_resp_text_code(pair),
            Rule::text => information = Some(pair.as_str().to_owned()),
            _ => unreachable!("{:#?}", pair),
        }
    }

    (status, code, information)
}

fn build_resp_text_code(pair: Pair<Rule>) -> Option<ResponseCode> {
    if !matches!(pair.as_rule(), Rule::resp_text_code) {
        unreachable!("{:#?}", pair);
    }

    let mut pairs = pair.into_inner();
    let pair = pairs.next()?;
    Some(match pair.as_rule() {
        Rule::capability_data => ResponseCode::Capabilities(build_capabilities(pair)),
        Rule::resp_text_code_readwrite => ResponseCode::ReadWrite,
        Rule::resp_text_code_uidvalidity => ResponseCode::UidValidity(build_number(unwrap1(pair))),
        Rule::resp_text_code_uidnext => ResponseCode::UidNext(build_number(unwrap1(pair))),
        Rule::resp_text_code_unseen => ResponseCode::Unseen(build_number(unwrap1(pair))),
        // TODO: maybe have an actual type for these flags instead of just string
        Rule::resp_text_code_permanentflags => {
            ResponseCode::PermanentFlags(pair.into_inner().map(|p| p.as_str().to_owned()).collect())
        }
        Rule::resp_text_code_other => {
            let mut pairs = pair.into_inner();
            let pair = pairs.next().unwrap();
            let a = pair.as_str().to_owned();
            let mut b = None;
            if let Some(pair) = pairs.next() {
                b = Some(pair.as_str().to_owned());
            }
            ResponseCode::Other(a, b)
        }
        _ => unreachable!("{:#?}", pair),
    })
}

fn build_capability(pair: Pair<Rule>) -> Capability {
    if !matches!(pair.as_rule(), Rule::capability) {
        unreachable!("{:#?}", pair);
    }

    let mut pairs = pair.into_inner();
    let pair = pairs.next().unwrap();
    match pair.as_rule() {
        Rule::auth_type => Capability::Auth(pair.as_str().to_uppercase().to_owned()),
        Rule::atom => match pair.as_str() {
            "IMAP4rev1" => Capability::Imap4rev1,
            s => Capability::Atom(s.to_uppercase().to_owned()),
        },
        _ => unreachable!("{:?}", pair),
    }
}

fn build_capabilities(pair: Pair<Rule>) -> Vec<Capability> {
    if !matches!(pair.as_rule(), Rule::capability_data) {
        unreachable!("{:#?}", pair);
    }
    pair.into_inner().map(build_capability).collect()
}

fn build_status(pair: Pair<Rule>) -> Status {
    match pair.as_rule() {
        Rule::resp_status => match pair.as_str().to_uppercase().as_str() {
            "OK" => Status::Ok,
            "NO" => Status::No,
            "BAD" => Status::Bad,
            s => unreachable!("invalid status {:?}", s),
        },
        _ => unreachable!("{:?}", pair),
    }
}

fn build_flag_list(pair: Pair<Rule>) -> Vec<MailboxFlag> {
    if !matches!(pair.as_rule(), Rule::flag_list) {
        unreachable!("{:#?}", pair);
    }

    pair.into_inner().map(build_flag).collect()
}

fn build_flag(mut pair: Pair<Rule>) -> MailboxFlag {
    if matches!(pair.as_rule(), Rule::flag_fetch) {
        let mut pairs = pair.into_inner();
        pair = pairs.next().unwrap();

        if matches!(pair.as_rule(), Rule::flag_fetch_recent) {
            return MailboxFlag::Recent;
        }
    }

    if !matches!(pair.as_rule(), Rule::flag) {
        unreachable!("{:#?}", pair);
    }

    match pair.as_str() {
        "\\Answered" => MailboxFlag::Answered,
        "\\Flagged" => MailboxFlag::Flagged,
        "\\Deleted" => MailboxFlag::Deleted,
        "\\Seen" => MailboxFlag::Seen,
        "\\Draft" => MailboxFlag::Draft,
        // s if s.starts_with("\\") => MailboxFlag::Ext(s.to_owned()),
        // TODO: what??
        s => MailboxFlag::Ext(s.to_owned()),
    }
}

fn build_mailbox_data(pair: Pair<Rule>) -> MailboxData {
    assert!(matches!(pair.as_rule(), Rule::mailbox_data));

    let mut pairs = pair.into_inner();
    let pair = pairs.next().unwrap();
    match pair.as_rule() {
        Rule::mailbox_data_exists => MailboxData::Exists(build_number(unwrap1(pair))),
        Rule::mailbox_data_flags => {
            let mut pairs = pair.into_inner();
            let pair = pairs.next().unwrap();
            let flags = build_flag_list(pair);
            MailboxData::Flags(flags)
        }
        Rule::mailbox_data_recent => MailboxData::Recent(build_number(unwrap1(pair))),
        Rule::mailbox_data_list => {
            let mut pairs = pair.into_inner();
            let pair = pairs.next().unwrap();
            let (flags, delimiter, name) = build_mailbox_list(pair);
            MailboxData::List {
                flags,
                delimiter,
                name,
            }
        }
        Rule::mailbox_data_search => {
            let uids = pair.into_inner().map(build_number).collect();
            MailboxData::Search(uids)
        }
        _ => unreachable!("{:#?}", pair),
    }
}

fn build_mailbox_list(pair: Pair<Rule>) -> (Vec<String>, Option<String>, String) {
    assert!(matches!(pair.as_rule(), Rule::mailbox_list));

    let mut pairs = pair.into_inner();
    let mut pair = pairs.next().unwrap();

    // let mut flags = Vec::new();
    let flags = if let Rule::mailbox_list_flags = pair.as_rule() {
        let pairs_ = pair.into_inner();
        let mut flags = Vec::new();
        for pair in pairs_ {
            flags.extend(build_mbx_list_flags(pair));
        }
        pair = pairs.next().unwrap();
        flags
    } else {
        Vec::new()
    };

    assert!(matches!(pair.as_rule(), Rule::mailbox_list_string));
    let s = build_nstring(unwrap1(pair));

    pair = pairs.next().unwrap();
    assert!(matches!(pair.as_rule(), Rule::mailbox));
    let mailbox = if pair.as_str().to_lowercase() == "inbox" {
        pair.as_str().to_owned()
    } else {
        build_astring(unwrap1(pair))
    };

    (flags, s, mailbox)
}

fn build_mbx_list_flags(pair: Pair<Rule>) -> Vec<String> {
    assert!(matches!(pair.as_rule(), Rule::mbx_list_flags));
    pair.into_inner()
        .map(|pair| pair.as_str().to_owned())
        .collect()
}

/// Unwraps a singleton pair (a pair that only has one element in its `inner` list)
fn unwrap1(pair: Pair<Rule>) -> Pair<Rule> {
    let mut pairs = pair.into_inner();
    pairs.next().unwrap()
}

/// Extracts a numerical type, generic over anything that could possibly be read as a number
// TODO: should probably restrict this to a few cases
fn build_number<T>(pair: Pair<Rule>) -> T
where
    T: FromStr,
    T::Err: Debug,
{
    assert!(matches!(pair.as_rule(), Rule::nz_number | Rule::number));
    pair.as_str().parse::<T>().unwrap()
}

fn build_astring(pair: Pair<Rule>) -> String {
    assert!(matches!(pair.as_rule(), Rule::astring));
    let pair_str = pair.as_str().to_owned();
    let mut pairs = pair.into_inner();
    let rule = pairs.peek().map(|p| p.as_rule());
    if let Some(Rule::string) = rule {
        let pair = pairs.next().unwrap();
        build_string(pair)
    } else {
        pair_str
    }
}

fn build_nstring(pair: Pair<Rule>) -> Option<String> {
    assert!(matches!(pair.as_rule(), Rule::nstring));
    let pair = unwrap1(pair);
    match pair.as_rule() {
        Rule::nil => None,
        Rule::string => Some(build_string(pair)),
        _ => unreachable!(),
    }
}

/// Extracts a string-type, discarding the surrounding quotes and unescaping the escaped characters
fn build_string(pair: Pair<Rule>) -> String {
    assert!(matches!(pair.as_rule(), Rule::string));
    let pair = unwrap1(pair);

    match pair.as_rule() {
        Rule::literal => build_literal(pair),
        // TODO: escaping stuff?
        Rule::quoted => pair
            .as_str()
            .trim_start_matches("\"")
            .trim_end_matches("\"")
            .replace("\\\"", "\"")
            .to_owned(),
        _ => unreachable!(),
    }
}

fn parse_literal(s: impl AsRef<str>) -> ParseResult<String> {
    let mut pairs = Rfc3501::parse(Rule::literal, s.as_ref())?;
    let pair = pairs.next().unwrap();
    Ok(build_literal(pair))
}

fn build_literal(pair: Pair<Rule>) -> String {
    assert!(matches!(pair.as_rule(), Rule::literal));

    let mut pairs = pair.into_inner();
    let _ = pairs.next().unwrap();
    let literal_str = pairs.next().unwrap();
    literal_str.as_str().to_owned()
}

fn parse_zone(s: impl AsRef<str>) -> ParseResult<FixedOffset> {
    let mut pairs = Rfc3501::parse(Rule::zone, s.as_ref())?;
    let pair = pairs.next().unwrap();
    Ok(build_zone(pair))
}

fn build_zone(pair: Pair<Rule>) -> FixedOffset {
    assert!(matches!(pair.as_rule(), Rule::zone));
    let n = pair.as_str().parse::<i32>().unwrap();
    let sign = if n != 0 { n / n.abs() } else { 1 };
    let h = n.abs() / 100;
    let m = n.abs() % 100;
    FixedOffset::east(sign * (h * 60 + m) * 60)
}

fn build_date_time(pair: Pair<Rule>) -> DateTime<FixedOffset> {
    assert!(matches!(pair.as_rule(), Rule::date_time));

    let mut pairs = pair.into_inner();
    let pair = pairs.next().unwrap();
    assert!(matches!(pair.as_rule(), Rule::date_day_fixed));
    let day = pair.as_str().trim().parse::<u32>().unwrap();

    let pair = pairs.next().unwrap();
    assert!(matches!(pair.as_rule(), Rule::date_month));
    let month = match pair.as_str() {
        "Jan" => 1,
        "Feb" => 2,
        "Mar" => 3,
        "Apr" => 4,
        "May" => 5,
        "Jun" => 6,
        "Jul" => 7,
        "Aug" => 8,
        "Sep" => 9,
        "Oct" => 10,
        "Nov" => 11,
        "Dec" => 12,
        _ => unreachable!(),
    };

    let pair = pairs.next().unwrap();
    assert!(matches!(pair.as_rule(), Rule::date_year));
    let year = pair.as_str().trim().parse::<i32>().unwrap();

    let pair = pairs.next().unwrap();
    assert!(matches!(pair.as_rule(), Rule::time));
    let mut parts = pair.as_str().split(':');
    let hour = parts.next().unwrap().parse::<u32>().unwrap();
    let minute = parts.next().unwrap().parse::<u32>().unwrap();
    let second = parts.next().unwrap().parse::<u32>().unwrap();

    let pair = pairs.next().unwrap();
    assert!(matches!(pair.as_rule(), Rule::zone));
    let zone = build_zone(pair);

    zone.ymd(year, month, day).and_hms(hour, minute, second)
}

fn build_address(pair: Pair<Rule>) -> Address {
    assert!(matches!(pair.as_rule(), Rule::address));

    let mut pairs = pair.into_inner();
    let pair = pairs.next().unwrap();
    assert!(matches!(pair.as_rule(), Rule::addr_name));
    let name = build_nstring(unwrap1(pair));

    let pair = pairs.next().unwrap();
    assert!(matches!(pair.as_rule(), Rule::addr_adl));
    let adl = build_nstring(unwrap1(pair));

    let pair = pairs.next().unwrap();
    assert!(matches!(pair.as_rule(), Rule::addr_mailbox));
    let mailbox = build_nstring(unwrap1(pair));

    let pair = pairs.next().unwrap();
    assert!(matches!(pair.as_rule(), Rule::addr_host));
    let host = build_nstring(unwrap1(pair));

    Address {
        name,
        adl,
        mailbox,
        host,
    }
}