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 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
//!
//! # Pop3 client
//!
//! This is a simple Pop3 client that implements all of the features according to [RFC 1939](https://www.rfc-editor.org/rfc/rfc1939), written in Rust.
//!
//! ## Usage
//!
//! You can create a new session using the `connect` function or the `connect_plain` function.
//!
//! `connect` expects a tls connector from the `async-native-tls` crate. In the future more tls options will be supported.
//!
//! If you already have a connected socket, you can also create a new session using the `new` function.
//!
//! ## Example
//!
//! ```rust,ignore
//! extern crate async_pop;
//! extern crate async_native_tls;
//! extern crate mailparse;
//!
//! use async_native_tls::TlsConnector;
//! use mailparse::parse_mail;
//!
//! #[tokio::main]
//! async fn main() {
//! let tls = TlsConnector::new();
//!
//! let mut client = async_pop::connect(("pop.gmail.com", 995), "pop.gmail.com", &tls, None).await.unwrap();
//!
//! client.login("example@gmail.com", "password").await.unwrap();
//!
//! let bytes = client.retr(1).await.unwrap();
//!
//! let message = parse_mail(&bytes).unwrap();
//!
//! let subject = message.headers.get_first_value("Subject").unwrap();
//!
//! println!("{}", subject);
//!
//! }
//! ```
mod command;
mod constants;
pub mod error;
mod macros;
pub mod request;
pub mod response;
mod runtime;
mod stream;
#[cfg(feature = "sasl")]
mod base64;
#[cfg(feature = "sasl")]
pub mod sasl;
use std::collections::HashSet;
use async_native_tls::{TlsConnector, TlsStream};
use bytes::Bytes;
use command::Command::*;
use error::{ErrorKind, Result};
use request::Request;
use response::{
capability::{Capabilities, Capability},
list::ListResponse,
stat::Stat,
types::message::Text,
uidl::UidlResponse,
Response,
};
use stream::PopStream;
use crate::{
error::err,
runtime::{
io::{Read, Write},
net::{TcpStream, ToSocketAddrs},
Instant,
},
};
#[derive(Eq, PartialEq, Debug)]
pub enum ClientState {
Authentication,
Transaction,
Update,
None,
}
pub struct Client<S: Write + Read + Unpin + Send> {
inner: Option<PopStream<S>>,
capabilities: Capabilities,
marked_as_del: Vec<usize>,
greeting: Option<Text>,
read_greeting: bool,
state: ClientState,
}
/// Creates a client from a given socket connection.
async fn create_client_from_socket<S: Read + Write + Unpin + Send>(
socket: PopStream<S>,
) -> Result<Client<S>> {
let mut client = Client {
marked_as_del: Vec::new(),
capabilities: Vec::new(),
greeting: None,
read_greeting: false,
inner: Some(socket),
state: ClientState::Authentication,
};
client.greeting = Some(client.read_greeting().await?);
client.update_capabilities().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: Read + Write + Unpin + Send>(stream: S) -> Result<Client<S>> {
let socket = PopStream::new(stream);
create_client_from_socket(socket).await
}
/// Create a new pop3 client with a tls connection.
pub async fn connect<A: ToSocketAddrs, D: AsRef<str>>(
addr: A,
domain: D,
tls_connector: &TlsConnector,
) -> Result<Client<TlsStream<TcpStream>>> {
let tcp_stream = TcpStream::connect(addr).await?;
let tls_stream = tls_connector.connect(domain.as_ref(), tcp_stream).await?;
let socket = PopStream::new(tls_stream);
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) -> Result<Client<TcpStream>> {
let tcp_stream = TcpStream::connect(addr).await?;
let socket = PopStream::new(tcp_stream);
create_client_from_socket(socket).await
}
impl<S: Read + Write + Unpin + Send> Client<S> {
/// Check if the client is in the correct state and return a mutable reference to the tcp connection.
fn inner_mut(&mut self) -> Result<&mut PopStream<S>> {
match self.inner.as_mut() {
Some(socket) => {
if self.state == ClientState::Transaction
|| self.state == ClientState::Authentication
{
Ok(socket)
} else {
err!(
ErrorKind::ShouldNotBeConnected,
"There is a connection, but our state indicates that we should not be connected",
)
}
}
None => err!(ErrorKind::NotConnected, "Not connected to any server",),
}
}
pub fn inner(&self) -> &Option<PopStream<S>> {
&self.inner
}
pub fn into_inner(self) -> Option<PopStream<S>> {
self.inner
}
/// Check if the client is in the correct state.
fn check_client_state(&self, state: ClientState) -> Result<()> {
if self.state != state {
err!(
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<()> {
self.send_request(Noop).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<usize>) -> Result<UidlResponse> {
self.check_capability(vec![Capability::Uidl])?;
match msg_number.as_ref() {
Some(msg_number) => self.check_deleted(msg_number)?,
None => {}
};
let mut request: Request = Uidl.into();
if let Some(number) = msg_number {
request.add_arg(number)
}
let response = self.send_request(request).await?;
match response {
Response::Uidl(resp) => Ok(resp),
_ => {
err!(
ErrorKind::UnexpectedResponse,
"Did not received the expected uidl response"
)
}
}
}
/// When the last communication with the server happened.
///
/// Returns [None] if there is no connection or the connection is not in the right state.
pub fn last_activity(&self) -> Option<Instant> {
Some(self.inner.as_ref()?.last_activity())
}
pub async fn top(&mut self, msg_number: usize, lines: usize) -> Result<Bytes> {
self.check_deleted(&msg_number)?;
self.check_capability(vec![Capability::Top])?;
let mut request: Request = Top.into();
request.add_arg(msg_number);
request.add_arg(lines);
let response = self.send_request(request).await?;
match response {
Response::Bytes(resp) => Ok(resp),
_ => err!(
ErrorKind::UnexpectedResponse,
"Did not received the expected top 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: &usize) -> 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: &usize) -> Result<()> {
if self.is_deleted(msg_number) {
err!(
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: usize) -> Result<Text> {
self.check_deleted(&msg_number)?;
let mut request: Request = Dele.into();
request.add_arg(msg_number);
let response = self.send_request(request).await?;
match response {
Response::Message(resp) => Ok(resp),
_ => err!(
ErrorKind::UnexpectedResponse,
"Did not received the expected dele response"
),
}
}
/// ## 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
///
/// https://www.rfc-editor.org/rfc/rfc1939#page-9
pub async fn rset(&mut self) -> Result<Text> {
let response = self.send_request(Rset).await?;
self.marked_as_del = Vec::new();
match response {
Response::Message(resp) => Ok(resp),
_ => err!(
ErrorKind::UnexpectedResponse,
"Did not received the expected rset response"
),
}
}
/// ## 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: usize) -> Result<Bytes> {
self.check_deleted(&msg_number)?;
let mut request: Request = Retr.into();
request.add_arg(msg_number);
let response = self.send_request(request).await?;
match response {
Response::Bytes(resp) => Ok(resp),
_ => err!(
ErrorKind::UnexpectedResponse,
"Did not received the expected retr response"
),
}
}
/// ## LIST
///
/// 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 "scan 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 also called a "scan listing" for that message. If there are no messages in the maildrop, then the POP3 server responds with no scan listings--it issues a positive response followed by a line containing a termination octet and a CRLF pair.
///
/// ### 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 scan listing follows
/// - -ERR no such message
pub async fn list(&mut self, msg_number: Option<usize>) -> Result<ListResponse> {
let mut request: Request = List.into();
if let Some(msg_number) = msg_number {
self.check_deleted(&msg_number)?;
request.add_arg(msg_number)
}
let response = self.send_request(request).await?;
match response {
Response::List(list) => Ok(list.into()),
Response::Stat(stat) => Ok(stat.into()),
_ => err!(
ErrorKind::UnexpectedResponse,
"Did not received the expected list response"
),
}
}
/// ## STAT
/// The POP3 server issues a positive response with a line containing information for the maildrop. This line is called a "drop listing" for that maildrop.
/// ### Arguments: none
/// ### Restrictions:
/// - may only be given in the TRANSACTION state
/// ### Possible responses:
/// - +OK nn mm
pub async fn stat(&mut self) -> Result<Stat> {
let response = self.send_request(Stat).await?;
match response.into() {
Response::Stat(resp) => Ok(resp),
_ => err!(
ErrorKind::UnexpectedResponse,
"Did not received the expected stat response"
),
}
}
/// ## APOP
/// Normally, each POP3 session starts with a USER/PASS exchange. This results in a server/user-id specific password being sent in the clear on the network. For intermittent use of POP3, this may not introduce a sizable risk. However, many POP3 client implementations connect to the POP3 server on a regular basis -- to check for new mail. Further the interval of session initiation may be on the order of five minutes. Hence, the risk of password capture is greatly enhanced.
///
/// An alternate method of authentication is required which provides for both origin authentication and replay protection, but which does not involve sending a password in the clear over the network. The APOP command provides this functionality.
///
/// A POP3 server which implements the APOP command will include a timestamp in its banner greeting. The syntax of the timestamp corresponds to the `msg-id' in [RFC822], and MUST be different each time the POP3 server issues a banner greeting. For example, on a UNIX implementation in which a separate UNIX process is used for each instance of a POP3 server, the syntax of the timestamp might be:
///
/// `<process-ID.clock@hostname>`
///
/// where `process-ID' is the decimal value of the process's PID, clock is the decimal value of the system clock, and hostname is the fully-qualified domain-name corresponding to the host where the POP3 server is running.
///
/// The POP3 client makes note of this timestamp, and then issues the APOP command. The `name` parameter has identical semantics to the `name` parameter of the USER command. The `digest` parameter is calculated by applying the MD5 algorithm [RFC1321] to a string consisting of the timestamp (including angle-brackets) followed by a shared
///
/// ### Arguments:
/// a string identifying a mailbox and a MD5 digest string (both required)
///
/// ### Restrictions:
/// may only be given in the AUTHORIZATION state after the POP3 greeting or after an unsuccessful USER or PASS command
///
/// ### Possible responses:
/// - +OK maildrop locked and ready
/// - -ERR permission denied
pub async fn apop<N: AsRef<str>, D: AsRef<str>>(&mut self, name: N, digest: D) -> Result<Text> {
self.check_client_state(ClientState::Authentication)?;
self.has_read_greeting()?;
let mut request: Request = Apop.into();
request.add_arg(name.as_ref());
request.add_arg(digest.as_ref());
let response = self.send_request(request).await?;
self.update_capabilities().await;
self.state = ClientState::Transaction;
match response {
Response::Message(resp) => Ok(resp),
_ => err!(
ErrorKind::UnexpectedResponse,
"Did not received the expected apop response"
),
}
}
/// ### AUTH
///
/// Requires an [sasl::Authenticator] to work. One could implement this themeselves for any given mechanism, look at the documentation for this trait.
///
/// If a common mechanism is needed, it can probably be found in the [sasl] module.
///
/// The AUTH command indicates an authentication mechanism to the server. If the server supports the requested authentication mechanism, it performs an authentication protocol exchange to authenticate and identify the user. Optionally, it also negotiates a protection mechanism for subsequent protocol interactions. If the requested authentication mechanism is not supported, the server should reject the AUTH command by sending a negative response.
///
/// The authentication protocol exchange consists of a series of server challenges and client answers that are specific to the authentication mechanism. A server challenge, otherwise known as a ready response, is a line consisting of a "+" character followed by a single space and a BASE64 encoded string. The client answer consists of a line containing a BASE64 encoded string. If the client wishes to cancel an authentication exchange, it should issue a line with a single "*". If the server receives such an answer, it must reject the AUTH command by sending a negative response.
///
/// A protection mechanism provides integrity and privacy protection to the protocol session. If a protection mechanism is negotiated, it is applied to all subsequent data sent over the connection. The protection mechanism takes effect immediately following the CRLF that concludes the authentication exchange for the client, and the CRLF of the positive response for the server. Once the protection mechanism is in effect, the stream of command and response octets is processed into buffers of ciphertext. Each buffer is transferred over the connection as a stream of octets prepended with a four octet field in network byte order that represents the length of the following data. The maximum ciphertext buffer length is defined by the protection mechanism.
///
/// The server is not required to support any particular authentication mechanism, nor are authentication mechanisms required to support any protection mechanisms. If an AUTH command fails with a negative response, the session remains in the AUTHORIZATION state and client may try another authentication mechanism by issuing another AUTH command, or may attempt to authenticate by using the USER/PASS or APOP commands. In other words, the client may request authentication types in decreasing order of preference, with the USER/PASS or APOP command as a last resort.
#[cfg(feature = "sasl")]
pub async fn auth<A: sasl::Authenticator + Sync>(&mut self, authenticator: A) -> Result<Text> {
self.check_client_state(ClientState::Authentication)?;
self.has_read_greeting()?;
let mut request: Request = Auth.into();
let mechanism = authenticator.mechanism();
request.add_arg(mechanism);
if let Some(arg) = authenticator.auth() {
request.add_arg(crate::base64::encode(arg))
}
let stream = self.inner_mut()?;
stream.encode(&request).await?;
let communicator = sasl::Communicator::new(stream);
authenticator.handle(communicator).await?;
let message = match stream.read_response(request).await? {
Response::Message(message) => message,
_ => err!(
ErrorKind::UnexpectedResponse,
"Did not received the expected auith response"
),
};
self.update_capabilities().await;
self.state = ClientState::Transaction;
Ok(message)
}
/// ## USER & PASS
///
/// To authenticate using the USER and PASS command combination, the client must first issue the USER command. If the POP3 server responds with a positive status indicator ("+OK"), then the client may issue either the PASS command to complete the authentication, or the QUIT command to terminate the POP3 session. If the POP3 server responds with a negative status indicator ("-ERR") to the USER command, then the client may either issue a new authentication command or may issue the QUIT command.
///
/// The server may return a positive response even though no such mailbox exists. The server may return a negative response if mailbox exists, but does not permit plaintext password authentication.
///
/// When the client issues the PASS command, the POP3 server uses the argument pair from the USER and PASS commands to determine if the client should be given access to the appropriate maildrop.
///
/// Since the PASS command has exactly one argument, a POP3 server may treat spaces in the argument as part of the password, instead of as argument separators.
///
/// ### Arguments:
/// - a string identifying a mailbox (required), which is of significance ONLY to the server
/// - a server/mailbox-specific password (required)
///
/// ### Restrictions:
/// may only be given in the AUTHORIZATION state after the POP3 greeting or after an unsuccessful USER or PASS command
///
/// ### Possible responses:
/// - +OK maildrop locked and ready
/// - -ERR invalid password
/// - -ERR unable to lock maildrop
/// - -ERR never heard of mailbox name
pub async fn login<U: AsRef<str>, P: AsRef<str>>(
&mut self,
user: U,
password: P,
) -> Result<(Text, Text)> {
self.check_client_state(ClientState::Authentication)?;
// self.check_capability(vec![
// Capability::User,
// Capability::Sasl(vec![String::from("PLAIN")]),
// ])?;
self.has_read_greeting()?;
let mut request: Request = User.into();
request.add_arg(user.as_ref());
let user_response = self.send_request(request).await?;
let mut request: Request = Pass.into();
request.add_arg(password.as_ref());
let pass_response = self.send_request(request).await?;
self.update_capabilities().await;
self.state = ClientState::Transaction;
let user_response_str = match user_response {
Response::Message(resp) => resp,
_ => err!(
ErrorKind::UnexpectedResponse,
"Did not received the expected user response"
),
};
let pass_response_str = match pass_response {
Response::Message(resp) => resp,
_ => err!(
ErrorKind::UnexpectedResponse,
"Did not received the expected pass response"
),
};
Ok((user_response_str, pass_response_str))
}
/// ## 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<Text> {
let response = self.send_request(Quit).await?;
self.state = ClientState::Update;
self.inner = None;
self.state = ClientState::None;
self.read_greeting = false;
self.marked_as_del.clear();
self.capabilities.clear();
match response {
Response::Message(resp) => Ok(resp),
_ => err!(
ErrorKind::UnexpectedResponse,
"Did not received the expected quit response"
),
}
}
/// Check whether the server supports one of the given capabilities.
pub fn has_capability<C: AsRef<[Capability]>>(&mut self, capabilities: C) -> bool {
let to_find: HashSet<_> = capabilities.as_ref().iter().collect();
let server_has: HashSet<_> = self.capabilities.iter().collect();
let intersect: Vec<_> = server_has.intersection(&to_find).collect();
intersect.len() == capabilities.as_ref().len()
}
/// Make sure the given capabilities are present
fn check_capability<C: AsRef<[Capability]>>(&mut self, capability: C) -> Result<()> {
if !self.has_capability(capability) {
err!(
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 response = self.send_request(Capa).await?;
match response.into() {
Response::Capability(resp) => Ok(resp),
_ => err!(
ErrorKind::UnexpectedResponse,
"Did not received the expected capa response"
),
}
}
async fn update_capabilities(&mut self) {
if let Ok(capabilities) = self.capa().await {
self.capabilities = capabilities
}
}
/// Sends a valid Pop3 command and returns the response sent by the server.
pub async fn send_request<R: Into<Request>>(&mut self, request: R) -> Result<Response> {
let request = request.into();
let stream = self.inner_mut()?;
stream.encode(&request).await?;
let response = stream.read_response(request).await?;
Ok(response)
}
fn has_read_greeting(&self) -> Result<()> {
if !self.read_greeting {
err!(
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<Text> {
assert!(!self.read_greeting, "Cannot read greeting twice");
let socket = self.inner_mut()?;
let response = socket.read_response(Greet).await?;
match response {
Response::Message(resp) => {
self.greeting = Some(resp.clone());
self.read_greeting = true;
Ok(resp)
}
_ => err!(
ErrorKind::UnexpectedResponse,
"Did not received the expected greeting"
),
}
}
/// The greeting that the POP server sent when the connection opened.
pub fn greeting(&self) -> Option<&Text> {
self.greeting.as_ref()
}
}
#[cfg(test)]
mod test;