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)> {
|
|
|
|
match &ctx.req.body {
|
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-22 15:26:52 +00:00
|
|
|
_ => Status::no(
|
|
|
|
Some(ctx.req.tag.clone()),
|
|
|
|
None,
|
|
|
|
"This command is not available in the ANONYMOUS state.",
|
|
|
|
)
|
|
|
|
.map(|s| (vec![ImapRes::Status(s)], flow::Transition::No))
|
|
|
|
.map_err(Error::msg),
|
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];
|
|
|
|
let res = vec![
|
|
|
|
ImapRes::Data(Data::Capability(capabilities)),
|
|
|
|
ImapRes::Status(
|
|
|
|
Status::ok(Some(ctx.req.tag.clone()), None, "Server capabilities")
|
2022-06-22 15:26:52 +00:00
|
|
|
.map_err(Error::msg)?,
|
|
|
|
),
|
2022-06-17 16:39:36 +00:00
|
|
|
];
|
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-22 15:26:52 +00:00
|
|
|
return Ok((
|
|
|
|
vec![ImapRes::Status(
|
2022-06-17 16:39:36 +00:00
|
|
|
Status::no(Some(ctx.req.tag.clone()), None, "Authentication failed")
|
2022-06-22 15:26:52 +00:00
|
|
|
.map_err(Error::msg)?,
|
|
|
|
)],
|
|
|
|
flow::Transition::No,
|
|
|
|
));
|
2022-06-17 16:39:36 +00:00
|
|
|
}
|
|
|
|
Ok(c) => c,
|
|
|
|
};
|
|
|
|
|
|
|
|
let user = flow::User {
|
|
|
|
creds,
|
|
|
|
name: u.clone(),
|
|
|
|
};
|
2022-06-22 12:58:57 +00:00
|
|
|
let tr = flow::Transition::Authenticate(user);
|
2022-06-17 16:39:36 +00:00
|
|
|
|
|
|
|
tracing::info!(username=%u, "connected");
|
2022-06-22 15:26:52 +00:00
|
|
|
Ok((
|
|
|
|
vec![
|
|
|
|
//@FIXME we could send a capability status here too
|
|
|
|
ImapRes::Status(
|
|
|
|
Status::ok(Some(ctx.req.tag.clone()), None, "completed").map_err(Error::msg)?,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
tr,
|
|
|
|
))
|
2022-06-17 16:39:36 +00:00
|
|
|
}
|