aerogramme/src/imap/mod.rs

105 lines
3 KiB
Rust
Raw Normal View History

2022-06-17 16:39:36 +00:00
mod command;
2022-06-22 15:26:52 +00:00
mod flow;
mod session;
2022-06-17 16:39:36 +00:00
2022-06-03 15:26:25 +00:00
use std::task::{Context, Poll};
2022-06-07 10:38:59 +00:00
use anyhow::Result;
2022-06-03 15:26:25 +00:00
use boitalettres::errors::Error as BalError;
2022-06-03 15:37:39 +00:00
use boitalettres::proto::{Request, Response};
2022-06-20 16:09:20 +00:00
use boitalettres::server::accept::addr::AddrIncoming;
2022-06-14 08:19:24 +00:00
use boitalettres::server::accept::addr::AddrStream;
2022-06-17 16:39:36 +00:00
use boitalettres::server::Server as ImapServer;
2022-06-03 15:26:25 +00:00
use futures::future::BoxFuture;
2022-06-09 08:43:38 +00:00
use futures::future::FutureExt;
2022-06-20 16:09:20 +00:00
use tokio::sync::watch;
2022-06-03 15:26:25 +00:00
use tower::Service;
2022-06-20 16:09:20 +00:00
use crate::config::ImapConfig;
2022-06-22 15:26:52 +00:00
use crate::login::ArcLoginProvider;
2022-06-09 08:43:38 +00:00
2022-06-17 16:39:36 +00:00
/// Server is a thin wrapper to register our Services in BàL
2022-06-20 16:09:20 +00:00
pub struct Server(ImapServer<AddrIncoming, Instance>);
2022-06-29 10:50:44 +00:00
2022-06-22 15:26:52 +00:00
pub async fn new(config: ImapConfig, login: ArcLoginProvider) -> Result<Server> {
2022-06-17 16:39:36 +00:00
//@FIXME add a configuration parameter
let incoming = AddrIncoming::new(config.bind_addr).await?;
2022-06-22 12:58:57 +00:00
tracing::info!("IMAP activated, will listen on {:#}", incoming.local_addr);
2022-06-17 16:39:36 +00:00
2022-06-20 16:09:20 +00:00
let imap = ImapServer::new(incoming).serve(Instance::new(login.clone()));
Ok(Server(imap))
2022-06-17 16:39:36 +00:00
}
2022-06-29 10:50:44 +00:00
2022-06-17 16:39:36 +00:00
impl Server {
2022-06-20 16:09:20 +00:00
pub async fn run(self, mut must_exit: watch::Receiver<bool>) -> Result<()> {
2022-06-17 16:39:36 +00:00
tracing::info!("IMAP started!");
tokio::select! {
2022-06-20 16:09:20 +00:00
s = self.0 => s?,
2022-06-17 16:39:36 +00:00
_ = must_exit.changed() => tracing::info!("Stopped IMAP server"),
}
Ok(())
}
}
//---
/// Instance is the main Tokio Tower service that we register in BàL.
/// It receives new connection demands and spawn a dedicated service.
struct Instance {
2022-06-20 16:09:20 +00:00
login_provider: ArcLoginProvider,
2022-06-07 10:38:59 +00:00
}
2022-06-29 10:50:44 +00:00
2022-06-07 10:38:59 +00:00
impl Instance {
2022-06-20 16:09:20 +00:00
pub fn new(login_provider: ArcLoginProvider) -> Self {
2022-06-15 16:40:39 +00:00
Self { login_provider }
2022-06-07 10:38:59 +00:00
}
}
2022-06-29 10:50:44 +00:00
2022-06-07 10:38:59 +00:00
impl<'a> Service<&'a AddrStream> for Instance {
type Response = Connection;
type Error = anyhow::Error;
type Future = BoxFuture<'static, Result<Self::Response>>;
2022-06-29 10:52:58 +00:00
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
2022-06-07 10:38:59 +00:00
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");
2022-06-15 16:40:39 +00:00
let lp = self.login_provider.clone();
async { Ok(Connection::new(lp)) }.boxed()
2022-06-07 10:38:59 +00:00
}
}
2022-06-17 16:39:36 +00:00
//---
/// Connection is the per-connection Tokio Tower service we register in BàL.
/// It handles a single TCP connection, and thus has a business logic.
struct Connection {
2022-06-13 09:44:02 +00:00
session: session::Manager,
2022-06-03 15:26:25 +00:00
}
2022-06-29 10:50:44 +00:00
2022-06-03 15:26:25 +00:00
impl Connection {
2022-06-20 16:09:20 +00:00
pub fn new(login_provider: ArcLoginProvider) -> Self {
2022-06-14 08:19:24 +00:00
Self {
2022-06-15 16:40:39 +00:00
session: session::Manager::new(login_provider),
2022-06-14 08:19:24 +00:00
}
2022-06-03 15:37:39 +00:00
}
2022-06-03 15:26:25 +00:00
}
2022-06-29 10:50:44 +00:00
2022-06-03 15:26:25 +00:00
impl Service<Request> for Connection {
type Response = Response;
type Error = BalError;
type Future = BoxFuture<'static, Result<Self::Response, Self::Error>>;
2022-06-29 10:52:58 +00:00
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
2022-06-03 15:26:25 +00:00
Poll::Ready(Ok(()))
}
fn call(&mut self, req: Request) -> Self::Future {
2022-06-28 07:34:24 +00:00
tracing::debug!("Got request: {:#?}", req.command);
2022-06-13 09:44:02 +00:00
self.session.process(req)
2022-06-09 08:43:38 +00:00
}
}