aerogramme/src/imap/command/anonymous.rs

62 lines
1.9 KiB
Rust
Raw Normal View History

2022-06-22 15:26:52 +00:00
use anyhow::{Error, Result};
2022-06-20 16:09:20 +00:00
use boitalettres::proto::Response;
use imap_codec::types::command::CommandBody;
use imap_codec::types::core::AString;
use imap_codec::types::response::{Capability, Data, Response as ImapRes, Status};
use crate::imap::flow;
use crate::imap::session::InnerContext;
2022-06-17 16:39:36 +00:00
//--- dispatching
2022-06-22 12:58:57 +00:00
pub async fn dispatch<'a>(ctx: InnerContext<'a>) -> Result<(Response, flow::Transition)> {
2022-06-28 07:34:24 +00:00
match &ctx.req.command.body {
2022-06-28 08:49:28 +00:00
CommandBody::Noop => Ok((Response::ok("Noop completed.")?, flow::Transition::No)),
2022-06-20 16:09:20 +00:00
CommandBody::Capability => capability(ctx).await,
2022-06-22 15:25:04 +00:00
CommandBody::Login { username, password } => login(ctx, username, password).await,
2022-06-28 07:34:24 +00:00
_ => Ok((
Response::no("This command is not available in the ANONYMOUS state.")?,
2022-06-28 08:49:28 +00:00
flow::Transition::No,
2022-06-28 07:34:24 +00:00
)),
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-22 12:58:57 +00:00
async fn capability<'a>(ctx: InnerContext<'a>) -> Result<(Response, flow::Transition)> {
2022-06-17 16:39:36 +00:00
let capabilities = vec![Capability::Imap4Rev1, Capability::Idle];
2022-06-28 07:34:24 +00:00
let res = Response::ok("Server capabilities")?.with_body(Data::Capability(capabilities));
2022-06-22 12:58:57 +00:00
Ok((res, flow::Transition::No))
2022-06-17 16:39:36 +00:00
}
2022-06-22 15:26:52 +00:00
async fn login<'a>(
ctx: InnerContext<'a>,
username: &AString,
password: &AString,
) -> Result<(Response, flow::Transition)> {
let (u, p) = (
String::try_from(username.clone())?,
String::try_from(password.clone())?,
);
2022-06-17 16:39:36 +00:00
tracing::info!(user = %u, "command.login");
2022-06-22 12:58:57 +00:00
let creds = match ctx.login.login(&u, &p).await {
2022-06-17 16:39:36 +00:00
Err(e) => {
tracing::debug!(error=%e, "authentication failed");
2022-06-28 07:34:24 +00:00
return Ok((Response::no("Authentication failed")?, flow::Transition::No));
2022-06-17 16:39:36 +00:00
}
Ok(c) => c,
};
let user = flow::User {
creds,
name: u.clone(),
};
tracing::info!(username=%u, "connected");
2022-06-22 15:26:52 +00:00
Ok((
2022-06-28 07:34:24 +00:00
Response::ok("Completed")?,
2022-06-28 08:49:28 +00:00
flow::Transition::Authenticate(user),
2022-06-22 15:26:52 +00:00
))
2022-06-17 16:39:36 +00:00
}