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
use std::ops::RangeInclusive;

use chrono::{DateTime, FixedOffset};

use super::bytes::Bytes;

pub type Atom = Bytes;

#[derive(Clone, Debug)]
pub struct Tag(pub Bytes);

#[derive(Debug)]
#[non_exhaustive]
pub enum Response {
    Capabilities(Vec<Capability>),
    Continue(ResponseText),
    Condition(Condition),
    Done(ResponseDone),
    MailboxData(MailboxData),
    Fetch(u32, Vec<MessageAttribute>),
    Expunge(u32),
    Fatal(Condition),
    Tagged(Tag, Condition),
}

#[derive(Debug)]
pub struct ResponseText {
    pub code: Option<ResponseCode>,
    pub info: Bytes,
}

#[derive(Debug)]
pub enum MessageAttribute {
    BodySection,
    BodyStructure,
    Envelope(Envelope),
    Flags(Vec<Flag>),
    InternalDate(DateTime<FixedOffset>),
    ModSeq(u64), // RFC 4551, section 3.3.2
    Rfc822(Option<String>),
    Rfc822Header(Option<String>),
    Rfc822Size(u32),
    Rfc822Text(Option<String>),
    Uid(u32),
}

#[derive(Debug)]
pub struct Envelope {
    pub date: Option<Bytes>,
    pub subject: Option<Bytes>,
    pub from: Option<Vec<Address>>,
    pub sender: Option<Vec<Address>>,
    pub reply_to: Option<Vec<Address>>,
    pub to: Option<Vec<Address>>,
    pub cc: Option<Vec<Address>>,
    pub bcc: Option<Vec<Address>>,
    pub in_reply_to: Option<Bytes>,
    pub message_id: Option<Bytes>,
}

#[derive(Debug)]
pub struct Address {
    pub name: Option<Bytes>,
    pub adl: Option<Bytes>,
    pub mailbox: Option<Bytes>,
    pub host: Option<Bytes>,
}

#[derive(Debug)]
pub struct ResponseDone {
    pub tag: Tag,
    pub status: Status,
    pub code: Option<ResponseCode>,
    pub info: Option<Bytes>,
}

#[derive(Debug)]
pub struct Condition {
    pub status: Status,
    pub code: Option<ResponseCode>,
    pub info: Bytes,
}

#[derive(Debug)]
pub enum Status {
    Ok,
    No,
    Bad,
    PreAuth,
    Bye,
}

#[derive(Debug)]
#[non_exhaustive]
pub enum ResponseCode {
    Alert,
    BadCharset(Option<Vec<Bytes>>),
    Capabilities(Vec<Capability>),
    HighestModSeq(u64), // RFC 4551, section 3.1.1
    Parse,
    PermanentFlags(Vec<Bytes>),
    ReadOnly,
    ReadWrite,
    TryCreate,
    UidNext(u32),
    UidValidity(u32),
    Unseen(u32),
    AppendUid(u32, Vec<UidSetMember>),
    CopyUid(u32, Vec<UidSetMember>, Vec<UidSetMember>),
    UidNotSticky,
    Other(Bytes, Option<Bytes>),
}

#[derive(Debug)]
pub enum UidSetMember {
    UidRange(RangeInclusive<u32>),
    Uid(u32),
}

#[derive(Debug, PartialEq, Eq)]
pub enum Capability {
    Imap4rev1,
    Auth(Atom),
    Atom(Atom),
}

#[derive(Debug)]
pub enum MailboxData {
    Flags(Vec<Flag>),
    List(MailboxList),
    Lsub(MailboxList),
    Search(Vec<u32>),
    Status,
    Exists(u32),
    Recent(u32),
}

#[derive(Debug)]
pub enum Mailbox {
    Inbox,
    Name(Bytes),
}

#[derive(Debug)]
pub enum Flag {
    Answered,
    Flagged,
    Deleted,
    Seen,
    Draft,
    Recent,
    Keyword(Atom),
    Extension(Atom),
}

#[derive(Debug)]
pub struct MailboxList {
    pub flags: Vec<MailboxListFlag>,
    pub delimiter: Option<u8>,
    pub mailbox: Mailbox,
}

#[derive(Debug)]
pub enum MailboxListFlag {
    NoInferiors,
    NoSelect,
    Marked,
    Unmarked,
    Extension(Atom),
}