diff options
author | Quentin Dufour <quentin@deuxfleurs.fr> | 2022-06-02 17:59:29 +0200 |
---|---|---|
committer | Quentin Dufour <quentin@deuxfleurs.fr> | 2022-06-02 17:59:29 +0200 |
commit | 3cb7c65b70250a6a7837291386d0f7f9ebd43ec6 (patch) | |
tree | 8745daa1b1ff2e677e56e649fab8f85a46b12828 /src/server.rs | |
parent | deced08513e9971eeb4165a5c27139e4d9a8daf3 (diff) | |
download | aerogramme-3cb7c65b70250a6a7837291386d0f7f9ebd43ec6.tar.gz aerogramme-3cb7c65b70250a6a7837291386d0f7f9ebd43ec6.zip |
Working server
Diffstat (limited to 'src/server.rs')
-rw-r--r-- | src/server.rs | 81 |
1 files changed, 48 insertions, 33 deletions
diff --git a/src/server.rs b/src/server.rs index b06299c..cd15bef 100644 --- a/src/server.rs +++ b/src/server.rs @@ -10,14 +10,10 @@ use crate::mailbox::Mailbox; use boitalettres::proto::{Request, Response}; use boitalettres::server::accept::addr::{AddrIncoming, AddrStream}; use boitalettres::server::Server as ImapServer; -use tracing_subscriber; +use std::pin::Pin; use std::task::{Context, Poll}; use tower::Service; -use std::future::Future; -use std::pin::Pin; - -use std::error::Error; pub struct Server { pub login_provider: Box<dyn LoginProvider>, @@ -25,38 +21,57 @@ pub struct Server { struct Connection; impl Service<Request> for Connection { - type Response = Response; - type Error = anyhow::Error; - type Future = Pin<Box<dyn futures::Future<Output = Result<Self::Response>> + Send>>; - - fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { - Poll::Ready(Ok(())) - } - - fn call(&mut self, req: Request) -> Self::Future { - Box::pin(async move { - println!("Got request: {:#?}", req); - Ok(Response::ok("Done")?) - }) - } + type Response = Response; + type Error = anyhow::Error; + type Future = Pin<Box<dyn futures::Future<Output = Result<Self::Response>> + Send>>; + + fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { + Poll::Ready(Ok(())) + } + + fn call(&mut self, req: Request) -> Self::Future { + tracing::debug!("Got request: {:#?}", req); + Box::pin(async move { + use imap_codec::types::{ + command::CommandBody, + response::{Capability, Data}, + }; + + let r = match req.body { + CommandBody::Capability => { + let capabilities = vec![Capability::Imap4Rev1, Capability::Idle]; + let body = vec![Data::Capability(capabilities)]; + Response::ok( + "Pre-login capabilities listed, post-login capabilities have more.", + )? + .with_body(body) + } + CommandBody::Login { + username: _, + password: _, + } => Response::ok("Logged in")?, + _ => Response::bad("Error in IMAP command received by server.")?, + }; + + Ok(r) + }) + } } struct Instance; impl<'a> Service<&'a AddrStream> for Instance { - type Response = Connection; - type Error = anyhow::Error; - type Future = Pin<Box<dyn futures::Future<Output = Result<Self::Response>> + Send>>; - - fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { - Poll::Ready(Ok(())) - } - - fn call(&mut self, addr: &'a AddrStream) -> Self::Future { - println!("{}, {}", addr.remote_addr, addr.local_addr); - Box::pin(async { - Ok(Connection) - }) - } + type Response = Connection; + type Error = anyhow::Error; + type Future = Pin<Box<dyn futures::Future<Output = Result<Self::Response>> + Send>>; + + fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { + Poll::Ready(Ok(())) + } + + fn call(&mut self, addr: &'a AddrStream) -> Self::Future { + tracing::info!(remote_addr = %addr.remote_addr, local_addr = %addr.local_addr, "accept"); + Box::pin(async { Ok(Connection) }) + } } impl Server { |