Struct imap::Client

source ·
pub struct Client<T: Read + Write> { /* private fields */ }
Expand description

An (unauthenticated) handle to talk to an IMAP server. This is what you get when first connecting. A succesfull call to Client::login or Client::authenticate will return a Session instance that provides the usual IMAP methods.

Implementations§

source§

impl<T: Read + Write> Client<T>

source

pub fn new(stream: T) -> Client<T>

Creates a new client over the given stream.

This method primarily exists for writing tests that mock the underlying transport, but can also be used to support IMAP over custom tunnels. If you do not need to do that, then it is simpler to use the ClientBuilder to get a new client.

For an example, see examples/timeout.rs which uses a custom timeout on the tcp stream.

Note: In case you do need to use Client::new instead of the ClientBuilder you will need to listen for the IMAP protocol server greeting before authenticating:

let mut client = Client::new(tls);
client.read_greeting().unwrap();
let session = client.login(username, password).unwrap();
source

pub fn into_inner(self) -> Result<T>

Yield the underlying connection for this Client.

This consumes self since the Client is not much use without an underlying transport.

source

pub fn capabilities(&mut self) -> Result<Capabilities>

The CAPABILITY command requests a listing of capabilities that the server supports. The server will include “IMAP4rev1” as one of the listed capabilities. See Capabilities for further details.

This allows reading capabilities before authentication.

source

pub fn login( self, username: impl AsRef<str>, password: impl AsRef<str> ) -> Result<Session<T>, (Error, Client<T>)>

Log in to the IMAP server. Upon success a Session instance is returned; on error the original Client instance is returned in addition to the error. This is because login takes ownership of self, so in order to try again (e.g. after prompting the user for credetials), ownership of the original Client needs to be transferred back to the caller.

let client = imap::ClientBuilder::new("imap.example.org", 993)
    .connect().unwrap();

match client.login("user", "pass") {
    Ok(s) => {
        // you are successfully authenticated!
    },
    Err((e, orig_client)) => {
        eprintln!("error logging in: {}", e);
        // prompt user and try again with orig_client here
        return;
    }
}
source

pub fn authenticate<A: Authenticator>( self, auth_type: impl AsRef<str>, authenticator: &A ) -> Result<Session<T>, (Error, Client<T>)>

Authenticate with the server using the given custom authenticator to handle the server’s challenge.

struct OAuth2 {
    user: String,
    access_token: String,
}

impl imap::Authenticator for OAuth2 {
    type Response = String;
    fn process(&self, _: &[u8]) -> Self::Response {
        format!(
            "user={}\x01auth=Bearer {}\x01\x01",
            self.user, self.access_token
        )
    }
}

fn main() {
    let auth = OAuth2 {
        user: String::from("me@example.com"),
        access_token: String::from("<access_token>"),
    };
    let client = imap::ClientBuilder::new("imap.example.com", 993).connect()
        .expect("Could not connect to server");

    match client.authenticate("XOAUTH2", &auth) {
        Ok(session) => {
            // you are successfully authenticated!
        },
        Err((e, orig_client)) => {
            eprintln!("error authenticating: {}", e);
            // prompt user and try again with orig_client here
            return;
        }
    };
}

Trait Implementations§

source§

impl<T: Debug + Read + Write> Debug for Client<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T: Read + Write> DerefMut for Client<T>

source§

fn deref_mut(&mut self) -> &mut Connection<T>

Mutably dereferences the value.
source§

impl<T: Read + Write> Deref for Client<T>

§

type Target = Connection<T>

The resulting type after dereferencing.
source§

fn deref(&self) -> &Connection<T>

Dereferences the value.

Auto Trait Implementations§

§

impl<T> Freeze for Client<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Client<T>
where T: RefUnwindSafe,

§

impl<T> Send for Client<T>
where T: Send,

§

impl<T> Sync for Client<T>
where T: Sync,

§

impl<T> Unpin for Client<T>
where T: Unpin,

§

impl<T> UnwindSafe for Client<T>
where T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.