From f15069fe640769a80245129a1016111c7a1bc9c5 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Tue, 30 Aug 2022 16:53:17 +0200 Subject: [PATCH] Complete example/simple.rs, tests that fail for APPEND --- examples/simple.rs | 34 +++++++++++++++++++++++++++------- scripts/test_imap.py | 18 ++++++++++++++++++ scripts/test_imap.sh | 23 +++++++++++++++++++++++ src/util/buf.rs | 1 + 4 files changed, 69 insertions(+), 7 deletions(-) mode change 100644 => 100755 scripts/test_imap.py create mode 100755 scripts/test_imap.sh 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 @@ +