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
mod util;

use regex::Regex;
use std::{
    io::{prelude::*, BufReader},
    net::{TcpStream, ToSocketAddrs},
    ops::Deref,
};

type TlsStream = rustls::StreamOwned<rustls::ClientSession, TcpStream>;

pub struct PopStream {
    pub is_authenticated: bool,
    pub stream:           TlsStream,
}

impl PopStream {
    pub fn connect<A: ToSocketAddrs>(addr: A, sess: rustls::ClientSession) -> PopStream {
        let socket = TcpStream::connect(addr).expect(womp!());
        let owned = rustls::StreamOwned::new(sess, socket);
        let mut this = PopStream {
            is_authenticated: false,
            stream:           owned,
        };
        dbg!(this.read_response_line());
        this
    }

    pub fn write(&mut self, msg: &str) -> Result<(), std::io::Error> {
        let comm = dbg!(format!("{}\n", msg));
        self.stream.write_all(comm.as_bytes())?;
        Ok(())
    }

    pub fn stat(&mut self) -> Result<Stat, String> {
        self.write("STAT").expect(womp!());
        let resp = self.read_response_line();
        resp_code!(resp);
        let captures = capture!(r"(?P<msgs>[\d]+) (?P<oct>[\d]+)", &resp.first_line);
        // let captures = regex.captures(&resp.first_line).expect(womp!());
        assert_eq!(captures.len(), 2);
        Ok(Stat {
            num_messages: parse_match!(captures, "msgs"),
            size_octets:  parse_match!(captures, "oct"),
        })
    }

    pub fn read_scan(&mut self) -> Scan {
        let line = self.read_line();
        let captures = capture!(r"(?P<id>[\d]+) (?P<oct>[\d]+)", &line);
        Scan {
            id:     parse_match!(captures, "id"),
            octets: parse_match!(captures, "oct"),
        }
    }

    pub fn list_all(&mut self) -> Result<List, String> {
        self.write("LIST").expect(womp!());
        let resp = dbg!(self.read_response_line());
        resp_code!(resp);
        let captures = capture!(r"(?P<count>[\d]+) .*(?P<octs>[\d]+)", &resp.first_line);
        let count = parse_match!(captures, "count");
        let octets = parse_match!(captures, "octs");
        let mut scans = Vec::with_capacity(count as usize);
        for _ in 0..count {
            scans.push(self.read_scan());
        }
        Ok(List {
            count,
            octets,
            scans,
        })
    }

    pub fn list_one(&mut self, id: u64) -> Result<Scan, String> {
        self.write(&format!("LIST {}", id)).expect(womp!());
        let resp = self.read_response_line();
        resp_code!(resp);
        let captures = capture!(r"(?P<id>[\d]+) (?P<oct>[\d]+)", &resp.first_line);
        Ok(Scan {
            id:     parse_match!(captures, "id"),
            octets: parse_match!(captures, "oct"),
        })
    }

    pub fn retr(&mut self, id: u64) -> Result<Vec<String>, String> {
        self.write(&format!("RETR {}", id)).expect(womp!());
        let resp = self.read_response_to_end();
        resp_code!(resp);
        Ok(resp.lines)
    }

    pub fn dele(&mut self, id: u64) -> PopResponse {
        self.write(&format!("DELE {}", id)).expect(womp!());
        self.read_response_line()
    }

    pub fn noop(&mut self) -> PopResponse {
        self.write("NOOP").expect(womp!());
        self.read_response_line()
    }

    pub fn rset(&mut self) -> PopResponse {
        self.write("RSET").expect(womp!());
        self.read_response_line()
    }

    pub fn quit(&mut self) -> PopResponse {
        self.write("QUIT").expect(womp!());
        self.read_response_line()
    }

    pub fn user(&mut self, user: &str) -> PopResponse {
        let command = format!("USER {}", user);
        self.write(&command).expect(womp!());
        self.read_response_line()
    }

    pub fn pass(&mut self, user: &str) -> PopResponse {
        self.write(&format!("PASS {}", user)).expect(womp!());
        self.read_response_line()
    }

    pub fn login(&mut self, user: &str, pass: &str) -> (PopResponse, PopResponse) {
        let resp1 = self.user(user);
        let resp2 = self.pass(pass);
        if resp1.success && resp2.success {
            self.is_authenticated = true;
        }
        dbg!((resp1, resp2))
    }

    fn read_ok_err(&mut self) -> bool {
        let mut buf = vec![0; 1];
        self.stream.read_exact(&mut buf).expect(womp!());
        match buf.deref() {
            b"+" => {
                buf = vec![0; 2];
                self.stream.read_exact(&mut buf).expect(womp!());
                assert_eq!(&buf, b"OK");
                true
            }
            b"-" => {
                buf = vec![0; 3];
                self.stream.read_exact(&mut buf).expect(womp!());
                assert_eq!(&buf, b"ERR");
                false
            }
            e => panic!("BAD RESPONSE: expected [+|-], found {:x?}", e),
        }
    }

    fn read_line(&mut self) -> String {
        let stream = rustls::Stream::new(&mut self.stream.sess, &mut self.stream.sock);
        let mut bufread = BufReader::new(stream);
        let mut buf = String::new();
        bufread.read_line(&mut buf).expect(womp!());
        assert_eq!(buf.pop(), Some('\n'));
        assert_eq!(buf.pop(), Some('\r'));
        buf
    }

    fn read_response_line(&mut self) -> PopResponse {
        let success = self.read_ok_err();
        let first_line = self.read_line();
        PopResponse {
            success,
            first_line,
            lines: vec![],
        }
    }

    fn read_response_to_end(&mut self) -> PopResponse {
        let mut resp = self.read_response_line();
        if resp.success {
            let mut nextline = self.read_line();
            while &nextline != "." {
                resp.lines.push(nextline);
                nextline = self.read_line();
            }
        }
        resp
    }
}

#[derive(Clone, Debug)]
pub struct PopResponse {
    pub success:    bool,
    pub first_line: String,
    pub lines:      Vec<String>,
}

#[derive(Clone, Debug)]
pub struct Stat {
    num_messages: u64,
    size_octets:  u64,
}

#[derive(Clone, Debug)]
pub struct List {
    count:  u64,
    octets: u64,
    scans:  Vec<Scan>,
}

#[derive(Clone, Debug)]
pub struct Scan {
    id:     u64,
    octets: u64,
}

#[derive(Clone, Debug)]
pub struct Retr {
    bytes: Vec<u8>,
}

/// List of POP3 Commands
#[derive(Clone, Debug)]
pub enum PopCommand {
    Greet,
    User(String),
    Pass(String),
    Stat,
    UidlAll,
    UidlOne,
    ListAll,
    ListOne,
    Retr(i64),
    Dele(i64),
    Noop,
    Rset,
    Quit,
}

impl std::fmt::Display for PopCommand {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        use PopCommand::*;
        match self {
            Greet => write!(f, "GREET"),
            User(s) => write!(f, "USER {}", s),
            Pass(s) => write!(f, "PASS {}", s),
            Stat => write!(f, "STAT"),
            UidlAll => write!(f, "UIDLALL"),
            UidlOne => write!(f, "UIDLONE"),
            ListAll => write!(f, "LISTALL"),
            ListOne => write!(f, "LISTONE"),
            Retr(i) => write!(f, "RETR {}", i),
            Dele(i) => write!(f, "DELE {}", i),
            Noop => write!(f, "NOOP"),
            Rset => write!(f, "RSET"),
            Quit => write!(f, "QUIT"),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use mail_test_account::{test_account_info, AccountAndServiceInfo};
    use regex::Regex;
    use rustls::{ClientConfig, ClientSession};
    use std::sync::Arc;
    use untrusted::Input as UIN;
    use webpki::DNSNameRef;

    #[test]
    fn it_works() {
        let AccountAndServiceInfo {
            account,
            smtp,
            imap,
            pop3,
            web,
        } = test_account_info().expect(womp!());
        drop((smtp, imap));
        let popinfo = pop3.expect(womp!());
        let webinfo = web.expect(womp!());
        let mut config = ClientConfig::new();
        config
            .root_store
            .add_server_trust_anchors(&webpki_roots::TLS_SERVER_ROOTS);
        let dn = capture!(r"http[s]{0,1}://(?P<name>[a-z]+\.[a-z]+)", &webinfo.uri)
            .name("name")
            .expect(womp!())
            .as_str();
        let session = ClientSession::new(
            &Arc::new(config),
            DNSNameRef::try_from_ascii(UIN::from(dn.as_bytes())).expect(womp!()),
        );
        let mut pop = PopStream::connect((popinfo.host.deref(), popinfo.port), session);
        assert!(dbg!(pop.user(&account.username)).success);
        assert!(dbg!(pop.pass(&account.password)).success);
        assert!(dbg!(pop.list_all()).is_ok());
        assert!(dbg!(pop.quit()).success);
    }
}