aerogramme/src/imap/command/anonymous.rs

93 lines
3.1 KiB
Rust
Raw Normal View History

2022-06-22 15:26:52 +00:00
use anyhow::{Error, Result};
2022-06-29 10:50:44 +00:00
use boitalettres::proto::{res::body::Data as Body, Request, Response};
2022-06-20 16:09:20 +00:00
use imap_codec::types::command::CommandBody;
2022-06-29 11:41:05 +00:00
use imap_codec::types::core::AString;
2022-06-29 10:52:58 +00:00
use imap_codec::types::response::{Capability, Data, Status};
2022-06-20 16:09:20 +00:00
use crate::imap::flow;
2022-06-29 10:50:44 +00:00
use crate::login::ArcLoginProvider;
2022-06-29 11:41:05 +00:00
use crate::mail::user::User;
2022-06-17 16:39:36 +00:00
//--- dispatching
2022-06-29 10:50:44 +00:00
pub struct AnonymousContext<'a> {
pub req: &'a Request,
pub login_provider: Option<&'a ArcLoginProvider>,
}
2023-05-15 16:23:23 +00:00
pub async fn dispatch(ctx: AnonymousContext<'_>) -> Result<(Response, flow::Transition)> {
2022-06-28 07:34:24 +00:00
match &ctx.req.command.body {
2022-06-29 10:50:44 +00:00
CommandBody::Noop => Ok((Response::ok("Noop completed.")?, flow::Transition::None)),
CommandBody::Capability => ctx.capability().await,
CommandBody::Logout => ctx.logout().await,
CommandBody::Login { username, password } => ctx.login(username, password).await,
2022-06-29 15:58:31 +00:00
_ => Ok((Response::no("Command unavailable")?, flow::Transition::None)),
2022-06-17 16:39:36 +00:00
}
}
2022-06-20 16:09:20 +00:00
//--- Command controllers, private
2022-06-17 16:39:36 +00:00
2022-06-29 10:50:44 +00:00
impl<'a> AnonymousContext<'a> {
async fn capability(self) -> Result<(Response, flow::Transition)> {
let capabilities = vec![Capability::Imap4Rev1, Capability::Idle];
let res = Response::ok("Server capabilities")?.with_body(Data::Capability(capabilities));
Ok((res, flow::Transition::None))
}
2022-06-17 16:39:36 +00:00
2022-06-29 10:50:44 +00:00
async fn login(
self,
username: &AString,
password: &AString,
) -> Result<(Response, flow::Transition)> {
let (u, p) = (
String::try_from(username.clone())?,
String::try_from(password.clone())?,
);
tracing::info!(user = %u, "command.login");
2022-06-17 16:39:36 +00:00
2022-06-29 10:50:44 +00:00
let login_provider = match &self.login_provider {
Some(lp) => lp,
None => {
return Ok((
Response::no("Login command not available (already logged in)")?,
flow::Transition::None,
))
}
};
2022-06-17 16:39:36 +00:00
2022-06-29 10:50:44 +00:00
let creds = match login_provider.login(&u, &p).await {
Err(e) => {
tracing::debug!(error=%e, "authentication failed");
return Ok((
Response::no("Authentication failed")?,
flow::Transition::None,
));
}
Ok(c) => c,
};
2022-06-17 16:39:36 +00:00
2022-06-30 12:02:57 +00:00
let user = User::new(u.clone(), creds).await?;
2022-06-29 10:50:44 +00:00
tracing::info!(username=%u, "connected");
Ok((
Response::ok("Completed")?,
flow::Transition::Authenticate(user),
))
}
// C: 10 logout
// S: * BYE Logging out
// S: 10 OK Logout completed.
async fn logout(self) -> Result<(Response, flow::Transition)> {
// @FIXME we should implement From<Vec<Status>> and From<Vec<ImapStatus>> in
// boitalettres/src/proto/res/body.rs
Ok((
Response::ok("Logout completed")?.with_body(vec![Body::Status(
Status::bye(None, "Logging out")
.map_err(|e| Error::msg(e).context("Unable to generate IMAP status"))?,
)]),
flow::Transition::Logout,
))
}
2022-06-28 13:38:06 +00:00
}