Skip to content

Instantly share code, notes, and snippets.

@dignifiedquire
Last active November 8, 2019 20:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dignifiedquire/5661ffee0d8efc26c9119eb2ec9e7ffb to your computer and use it in GitHub Desktop.
Save dignifiedquire/5661ffee0d8efc26c9119eb2ec9e7ffb to your computer and use it in GitHub Desktop.
use async_imap::{connect, error::Result};
use async_std::prelude::*;
use async_std::task;
use async_tls::TlsConnector;
use std::time::Duration;
fn main() -> Result<()> {
task::block_on(async {
let server = "imap.server.com";
let tls = TlsConnector::default();
// ma client
let mut client = connect((server, 993), server, &tls).await?;
client.debug = true;
// login
let mut session = client
.login("user", "password")
.await
.ok()
.unwrap();
// get that inbox
let res = session.select("INBOX").await?;
println!("selected: {:#?}", res);
// fetchy fetch
let msg_stream = session.fetch("1:3", "(FLAGS BODY.PEEK[])").await?;
let msgs = msg_stream.collect::<Vec<_>>().await;
println!("msgs: {:?}", msgs.len());
// Idle session
println!("starting idle");
let mut idle = session.idle();
idle.init().await?;
let (send, receive) = async_std::sync::channel(1);
#[derive(Debug)]
enum IdleResult {
Interrupt,
Message,
}
task::spawn(async move {
println!("waiting for 2s");
task::sleep(Duration::from_secs(2)).await;
println!("interrupting idle");
send.send(()).await;
});
let idle_stream = idle.stream();
println!("idle wait");
let interrupt_chan =
futures::future::FutureExt::map(receive.recv(), |_| IdleResult::Interrupt);
let idle_chan =
futures::future::FutureExt::map(idle_stream.take(1).collect::<Vec<_>>(), |_| {
IdleResult::Message
});
let idle_result = interrupt_chan.race(idle_chan).await;
println!("idle msg: {:#?}", &idle_result);
// return the session after we are done with it
let mut session = idle.done().await?;
println!("logging out");
session.logout().await?;
Ok(())
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment