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
mod constants;
mod parse;
mod socket;
pub mod types;
mod utils;

use async_native_tls::{TlsConnector, TlsStream};
use parse::parse_capabilities;
use socket::Socket;

use tokio::{
    io::{AsyncRead, AsyncWrite},
    net::{TcpStream, ToSocketAddrs},
    time::{timeout, Duration},
};
use types::{
    Capabilities, Capability, Error, ErrorKind, Result, Stats, StatsResponse, UniqueID,
    UniqueIDResponse,
};

use utils::create_command;

#[derive(Eq, PartialEq, Debug)]
pub enum ClientState {
    Authentication,
    Transaction,
    Update,
    None,
}

pub struct Client<S: AsyncWrite + AsyncRead + Unpin> {
    socket: Option<Socket<S>>,
    capabilities: Capabilities,
    marked_as_del: Vec<u32>,
    greeting: Option<String>,
    read_greeting: bool,
    state: ClientState,
}

fn get_connection_timeout(timeout: Option<Duration>) -> Duration {
    match timeout {
        Some(timeout) => timeout,
        None => Duration::from_secs(60),
    }
}

/// Creates a client from a given socket connection.
async fn create_client_from_socket<S: AsyncRead + AsyncWrite + Unpin>(
    socket: Socket<S>,
) -> Result<Client<S>> {
    let mut client = Client {
        marked_as_del: Vec::new(),
        capabilities: Vec::new(),
        greeting: None,
        read_greeting: false,
        socket: Some(socket),
        state: ClientState::Authentication,
    };

    client.greeting = Some(client.read_greeting().await?);

    client.capabilities = client.capa().await?;

    Ok(client)
}

/// Creates a new pop3 client from an existing stream.
/// # Examples
/// ```rust,ignore
/// extern crate pop3;
/// use std::net::TcpStream;
///
/// fn main() {
///     // Not recommended to use plaintext, just an example.
///     let stream = TcpStream::connect(("outlook.office365.com", 110)).unwrap();
///
///     let mut client = pop3::new(stream).unwrap();
///
///     client.quit().unwrap();
/// }
/// ```
pub async fn new<S: AsyncRead + AsyncWrite + Unpin>(
    stream: S,
    timeout: Option<Duration>,
) -> Result<Client<S>> {
    let socket = Socket::new(stream, timeout);

    create_client_from_socket(socket).await
}

/// Create a new pop3 client with a tls connection.
pub async fn connect<A: ToSocketAddrs>(
    addr: A,
    domain: &str,
    tls_connector: &TlsConnector,
    connection_timeout: Option<Duration>,
) -> Result<Client<TlsStream<TcpStream>>> {
    let connection_timeout = get_connection_timeout(connection_timeout);

    let tcp_stream = timeout(connection_timeout, TcpStream::connect(addr)).await??;

    let tls_stream = tls_connector.connect(domain, tcp_stream).await?;

    let socket = Socket::new(tls_stream, None);

    create_client_from_socket(socket).await
}

/// Creates a new pop3 client using a plain connection.
///
/// DO NOT USE in a production environment. Your password will be sent over a plain tcp stream which hackers could intercept.
pub async fn connect_plain<A: ToSocketAddrs>(
    addr: A,
    connection_timeout: Option<Duration>,
) -> Result<Client<TcpStream>> {
    let connection_timeout = get_connection_timeout(connection_timeout);

    let tcp_stream = timeout(connection_timeout, TcpStream::connect(addr)).await??;

    let socket = Socket::new(tcp_stream, None);

    create_client_from_socket(socket).await
}

impl<S: AsyncRead + AsyncWrite + Unpin> Client<S> {
    /// Check if the client is in the correct state and return a mutable reference to the tcp connection.
    fn get_socket_mut(&mut self) -> Result<&mut Socket<S>> {
        match self.socket.as_mut() {
            Some(socket) => {
                if self.state == ClientState::Transaction
                    || self.state == ClientState::Authentication
                {
                    Ok(socket)
                } else {
                    Err(Error::new(
                        ErrorKind::ShouldNotBeConnected,
                        "There is a connection, but our state indicates that we should not be connected",
                    ))
                }
            }
            None => Err(Error::new(
                ErrorKind::NotConnected,
                "Not connected to any server",
            )),
        }
    }

    /// Check if the client is in the correct state.
    fn check_client_state(&self, state: ClientState) -> Result<()> {
        if self.state != state {
            Err(Error::new(
                ErrorKind::IncorrectStateForCommand,
                "The connection is not the right state to use this command",
            ))
        } else {
            Ok(())
        }
    }

    /// ## Current client state
    ///
    /// Indicates what state the client is currently in, can be either
    /// Authentication, Transaction, Update or None.
    ///
    /// Some methods are only available in some specified states and will error if run in an incorrect state.
    ///
    /// https://www.rfc-editor.org/rfc/rfc1939#section-3
    pub fn get_state(&self) -> &ClientState {
        &self.state
    }

    /// ## NOOP
    /// The POP3 server does nothing, it merely replies with a positive response.
    /// ### Arguments: none
    /// ### Restrictions:
    /// - May only be given in the TRANSACTION state
    /// ### Possible Responses:
    /// - OK
    /// # Examples:
    /// ```rust,ignore
    /// client.noop()?;
    /// ```
    /// https://www.rfc-editor.org/rfc/rfc1939#page-9
    pub async fn noop(&mut self) -> Result<()> {
        let socket = self.get_socket_mut()?;

        let command = "NOOP";

        socket.send_command(command, false).await?;

        Ok(())
    }

    /// ## UIDL
    /// If an argument was given and the POP3 server issues a positive response with a line containing information for that message.
    /// This line is called a "unique-id listing" for that message.
    ///
    /// If no argument was given and the POP3 server issues a positive response, then the response given is multi-line.
    /// After the initial +OK, for each message in the maildrop, the POP3 server responds with a line containing information for that message.          This line is called a "unique-id listing" for that message.
    ///
    /// ### Arguments:
    /// - a message-number (optional), which, if present, may NOT refer to a message marked as deleted.
    ///
    /// ### Restrictions:
    /// - May only be given in the TRANSACTION state.
    ///
    /// ### Possible responses:
    /// - +OK unique-id listing follows
    /// - -ERR no such message
    ///
    /// https://www.rfc-editor.org/rfc/rfc1939#page-12
    pub async fn uidl(&mut self, msg_number: Option<u32>) -> Result<UniqueIDResponse> {
        self.check_capability(vec![Capability::Uidl])?;

        match msg_number.as_ref() {
            Some(msg_number) => self.check_deleted(msg_number)?,
            None => {}
        };

        let response_is_multi_line = msg_number.is_none();

        let socket = self.get_socket_mut()?;

        let arguments = if msg_number.is_some() {
            vec![msg_number.unwrap().to_string()]
        } else {
            Vec::new()
        };

        let command = create_command("UIDL", &arguments)?;

        let response = socket.send_command(command, response_is_multi_line).await?;

        UniqueID::from_response(response)
    }

    pub async fn top(&mut self, msg_number: u32, lines: u32) -> Result<Vec<u8>> {
        self.check_deleted(&msg_number)?;

        self.check_capability(vec![Capability::Top])?;

        let socket = self.get_socket_mut()?;

        let command = format!("TOP {} {}", msg_number, lines);

        socket.send_command(command, false).await?;

        let mut response: Vec<u8> = Vec::new();

        socket.read_multi_line(&mut response).await?;

        Ok(response)
    }

    /// Check whether a given message is marked as deleted by the server.
    ///
    /// If this function returns true then the message may still not exist.
    /// # Examples:
    /// ```rust,ignore
    /// let msg_number: u32 = 8;
    /// let is_deleted = client.is_deleted(msg_number);
    /// assert_eq!(is_deleted, false);
    /// ```
    pub fn is_deleted(&mut self, msg_number: &u32) -> bool {
        self.marked_as_del.sort();

        match self.marked_as_del.binary_search(msg_number) {
            Ok(_) => true,
            Err(_) => false,
        }
    }

    fn check_deleted(&mut self, msg_number: &u32) -> Result<()> {
        if self.is_deleted(msg_number) {
            Err(types::Error::new(
                types::ErrorKind::MessageIsDeleted,
                "This message has been marked as deleted and cannot be refenced anymore",
            ))
        } else {
            Ok(())
        }
    }

    /// ## DELE
    /// The POP3 server marks the message as deleted.  Any future reference to the message-number associated with the message in a POP3 command generates an error.  The POP3 server does not actually delete the message until the POP3 session enters the UPDATE state.
    /// ### Arguments:
    /// - a message-number (required) which may NOT refer to a message marked as deleted.
    /// ### Restrictions:
    /// - may only be given in the TRANSACTION state
    /// ### Possible Responses:
    /// - OK: message deleted
    /// - ERR: no such message
    /// # Examples
    /// ```rust,ignore
    /// let msg_number: u32 = 8;
    /// let is_deleted = client.is_deleted(msg_number);
    ///
    /// println!("{}", is_deleted);
    /// ```
    pub async fn dele(&mut self, msg_number: u32) -> Result<()> {
        self.check_deleted(&msg_number)?;

        let socket = self.get_socket_mut()?;

        let command = format!("DELE {}", msg_number);

        socket.send_command(command.as_bytes(), false).await?;

        Ok(())
    }

    /// ## RSET
    /// If any messages have been marked as deleted by the POP3
    /// server, they are unmarked.
    /// ### Arguments: none
    /// ### Restrictions:
    /// - May only be given in the TRANSACTION state
    /// ### Possible Responses:
    /// - +OK
    /// # Examples:
    /// ```rust,ignore
    /// client.rset().unwrap();
    /// ```
    /// https://www.rfc-editor.org/rfc/rfc1939#page-9
    pub async fn rset(&mut self) -> Result<()> {
        let socket = self.get_socket_mut()?;

        let command = b"RSET";

        socket.send_command(command, false).await?;

        Ok(())
    }

    /// ## RETR
    /// Retrieves the full RFC822 compliant message from the server and returns it as a byte vector
    /// ### Arguments:
    /// - A message-number (required) which may NOT refer to a message marked as deleted
    /// ### Restrictions:
    /// - May only be given in the TRANSACTION state
    /// ### Possible Responses:
    /// - OK: message follows
    /// - ERR: no such message
    /// # Examples
    /// ```rust,ignore
    /// extern crate mailparse;
    /// use mailparse::parse_mail;
    ///
    /// let response = client.retr(1).unwrap();
    ///
    /// let parsed = parse_mail(&response);
    ///
    /// let subject = parsed.headers.get_first_value("Subject").unwrap();
    ///
    /// println!("{}", subject);
    /// ```
    /// https://www.rfc-editor.org/rfc/rfc1939#page-8
    pub async fn retr(&mut self, msg_number: u32) -> Result<Vec<u8>> {
        self.check_deleted(&msg_number)?;

        let socket = self.get_socket_mut()?;

        let arguments = vec![msg_number.to_string()];

        let command = create_command("RETR", &arguments)?;

        socket.send_bytes(command.as_bytes()).await?;

        let mut response: Vec<u8> = Vec::new();

        socket.read_multi_line(&mut response).await?;

        Ok(response)
    }

    pub async fn list(&mut self, msg_number: Option<u32>) -> Result<StatsResponse> {
        match msg_number.as_ref() {
            Some(msg_number) => {
                self.check_deleted(msg_number)?;
            }
            None => {}
        };

        let socket = self.get_socket_mut()?;

        let response_is_multi_line = msg_number.is_none();

        let arguments = if !response_is_multi_line {
            vec![msg_number.unwrap().to_string()]
        } else {
            Vec::new()
        };

        let command = create_command("LIST", &arguments)?;

        let response = socket.send_command(command, response_is_multi_line).await?;

        Stats::from_response(response)
    }

    pub async fn stat(&mut self) -> Result<Stats> {
        let socket = self.get_socket_mut()?;

        let command = b"STAT";

        let response = socket.send_command(command, false).await?;

        match Stats::from_response(response)? {
            StatsResponse::Stats(stats) => Ok(stats),
            StatsResponse::StatsList(_) => unreachable!(),
        }
    }

    pub async fn apop(&mut self, name: &str, digest: &str) -> Result<()> {
        self.check_client_state(ClientState::Authentication)?;

        self.has_read_greeting()?;

        let socket = self.get_socket_mut()?;

        let command = format!("APOP {} {}", name, digest);

        socket.send_command(command, false).await?;

        self.state = ClientState::Transaction;

        Ok(())
    }

    pub async fn auth<U: AsRef<str>>(&mut self, token: U) -> Result<()> {
        self.check_client_state(ClientState::Authentication)?;

        self.check_capability(vec![Capability::Sasl(vec![String::from("XOAUTH2")])])?;

        self.has_read_greeting()?;

        let socket = self.get_socket_mut()?;

        let command = format!("AUTH {}", token.as_ref());

        socket.send_command(command, false).await?;

        self.state = ClientState::Transaction;

        Ok(())
    }

    pub async fn login<U: AsRef<str>, P: AsRef<str>>(
        &mut self,
        user: U,
        password: P,
    ) -> Result<()> {
        self.check_client_state(ClientState::Authentication)?;

        self.check_capability(vec![
            Capability::User,
            Capability::Sasl(vec![String::from("PLAIN")]),
        ])?;

        self.has_read_greeting()?;

        let socket = self.get_socket_mut()?;

        let command = format!("USER {}", user.as_ref());

        socket.send_command(command, false).await?;

        let command = format!("PASS {}", password.as_ref());

        socket.send_command(command, false).await?;

        self.capabilities = self.capa().await?;

        self.state = ClientState::Transaction;

        Ok(())
    }

    /// ## QUIT
    /// Quits the session
    ///
    /// ### Arguments: none
    ///
    /// ### Restrictions: none
    ///
    /// ### Possible Responses:
    /// - +OK
    ///
    /// https://www.rfc-editor.org/rfc/rfc1939#page-5
    pub async fn quit(&mut self) -> Result<()> {
        let socket = self.get_socket_mut()?;

        let command = "QUIT";

        socket.send_command(command, false).await?;

        self.state = ClientState::Update;
        self.socket = None;
        self.state = ClientState::None;

        self.marked_as_del.clear();
        self.capabilities.clear();

        Ok(())
    }

    /// Check whether the server supports one of the given capabilities.
    pub fn has_capability(&mut self, capabilities: Vec<Capability>) -> bool {
        self.capabilities.sort();

        match capabilities.iter().find(|capability| {
            match self.capabilities.binary_search(&capability) {
                Ok(_) => true,
                Err(_) => false,
            }
        }) {
            Some(_) => true,
            None => false,
        }
    }

    /// Make sure the given capabilities are present
    fn check_capability(&mut self, capability: Vec<Capability>) -> Result<()> {
        if !self.has_capability(capability) {
            Err(types::Error::new(
                types::ErrorKind::FeatureUnsupported,
                "The remote pop server does not support this command/function",
            ))
        } else {
            Ok(())
        }
    }

    /// Returns the current list of capabilities given by the server.
    pub fn capabilities(&self) -> &Capabilities {
        &self.capabilities
    }

    /// Fetches a list of capabilities for the currently connected server and returns it.
    pub async fn capa(&mut self) -> Result<Capabilities> {
        let socket = self.get_socket_mut()?;

        let command = b"CAPA";

        let response = socket.send_command(command, true).await?;

        Ok(parse_capabilities(&response))
    }

    fn has_read_greeting(&self) -> Result<()> {
        if !self.read_greeting {
            Err(Error::new(
                ErrorKind::ServerFailedToGreet,
                "Did not connect to the server correctly, as we did not get a greeting yet",
            ))
        } else {
            Ok(())
        }
    }

    async fn read_greeting(&mut self) -> Result<String> {
        assert!(!self.read_greeting, "Cannot read greeting twice");

        let socket = self.get_socket_mut()?;

        let response = socket.read_response(false).await?;

        self.read_greeting = true;
        Ok(response)
    }

    /// The greeting that the POP server sent when the connection opened.
    pub fn greeting(&self) -> Option<&str> {
        match self.greeting.as_ref() {
            Some(greeting) => Some(greeting),
            None => None,
        }
    }
}

#[cfg(test)]
mod test;