diff --git a/examples/simple.rs b/examples/simple.rs index 5bdd45c..f64ffaf 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -1,21 +1,41 @@ use miette::{IntoDiagnostic, Result}; +use imap_codec::types::command::CommandBody; + +use tracing::info; + use boitalettres::proto::{Request, Response}; use boitalettres::server::accept::addr::{AddrIncoming, AddrStream}; use boitalettres::server::Server; -async fn handle_req(_req: Request) -> Result { +async fn handle_req(req: Request) -> Result { use imap_codec::types::response::{Capability, Data as ImapData}; use boitalettres::proto::res::{body::Data, Status}; - let capabilities = vec![Capability::Imap4Rev1, Capability::Idle]; - let body: Vec = vec![ - Status::ok("Yeah")?.into(), - ImapData::Capability(capabilities).into(), - ]; + info!("Handling request: {:?}", req); - Ok(Response::ok("Done")?.with_body(body)) + match req.command.body { + CommandBody::Noop => Ok(Response::ok("NOOP done")?), + CommandBody::Capability => { + let capabilities = vec![Capability::Imap4Rev1, Capability::Idle]; + let body: Vec = vec![ + Status::ok("Yeah")?.into(), + ImapData::Capability(capabilities).into(), + ]; + + Ok(Response::ok("CAPABILITY done")?.with_body(body)) + } + CommandBody::Login { .. } => Ok(Response::ok("Faking successfull LOGIN")?), + CommandBody::Append { .. } => Ok(Response::ok("Faking successfull APPEND")?), + CommandBody::Logout { .. } => { + let body: Vec = vec![Status::bye("Logging out")?.into()]; + Ok(Response::ok("LOGOUT completed")? + .with_body(body) + .close(true)) + } + _ => Ok(Response::no("unavailable command")?), + } } #[tokio::main] diff --git a/scripts/test_imap.py b/scripts/test_imap.py old mode 100644 new mode 100755 index f8bdf17..ec51b96 --- a/scripts/test_imap.py +++ b/scripts/test_imap.py @@ -2,6 +2,24 @@ from imaplib import IMAP4 conn = IMAP4('127.0.0.1', port=4567) +print("Connected") + conn.noop() +print("NOOP done") + +conn.login("lx", "plop") +print("Logged in") + +print("Testing APPEND (this uses continuation style)") +conn.append("INBOX", [], None, +b"""From: alex@adnab.me +To: lx@deuxfleurs.fr +Subject: Hello, world! + +Test +. +""") +print("Append done") conn.logout() +print("Logged out") diff --git a/scripts/test_imap.sh b/scripts/test_imap.sh new file mode 100755 index 0000000..f729392 --- /dev/null +++ b/scripts/test_imap.sh @@ -0,0 +1,23 @@ +#!/bin/sh + +# APPEND example from RFC9051 + +nc localhost 4567 < +Subject: afternoon meeting +To: mooch@example.com +Message-Id: +MIME-Version: 1.0 +Content-Type: TEXT/PLAIN; CHARSET=US-ASCII + +Hello Joe, do you think we can meet at 3:30 tomorrow? + +D NOOP +E LOGOUT +EOF + +echo "^ expected output: responses to A B C D and E, last one is E OK LOGOUT completed" diff --git a/src/util/buf.rs b/src/util/buf.rs index e69de29..8b13789 100644 --- a/src/util/buf.rs +++ b/src/util/buf.rs @@ -0,0 +1 @@ +