Refactor selected

This commit is contained in:
Quentin 2022-06-22 17:25:04 +02:00
parent 50b1972987
commit ac974184de
Signed by: quentin
GPG Key ID: E9602264D639FF68
4 changed files with 27 additions and 29 deletions

View File

@ -13,7 +13,7 @@ use crate::imap::session::InnerContext;
pub async fn dispatch<'a>(ctx: InnerContext<'a>) -> Result<(Response, flow::Transition)> {
match &ctx.req.body {
CommandBody::Capability => capability(ctx).await,
CommandBody::Login { username, password } => login(ctx, username.clone(), password.clone()).await,
CommandBody::Login { username, password } => login(ctx, username, password).await,
_ => 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),
@ -34,8 +34,8 @@ async fn capability<'a>(ctx: InnerContext<'a>) -> Result<(Response, flow::Transi
Ok((res, flow::Transition::No))
}
async fn login<'a>(ctx: InnerContext<'a>, username: AString, password: AString) -> Result<(Response, flow::Transition)> {
let (u, p) = (String::try_from(username)?, String::try_from(password)?);
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())?);
tracing::info!(user = %u, "command.login");
let creds = match ctx.login.login(&u, &p).await {

View File

@ -15,9 +15,9 @@ pub async fn dispatch<'a>(inner: InnerContext<'a>, user: &'a flow::User) -> Resu
let ctx = StateContext { user, tag: &inner.req.tag, inner };
match &ctx.inner.req.body {
CommandBody::Lsub { reference, mailbox_wildcard, } => ctx.lsub(reference.clone(), mailbox_wildcard.clone()).await,
CommandBody::List { reference, mailbox_wildcard, } => ctx.list(reference.clone(), mailbox_wildcard.clone()).await,
CommandBody::Select { mailbox } => ctx.select(mailbox.clone()).await,
CommandBody::Lsub { reference, mailbox_wildcard, } => ctx.lsub(reference, mailbox_wildcard).await,
CommandBody::List { reference, mailbox_wildcard, } => ctx.list(reference, mailbox_wildcard).await,
CommandBody::Select { mailbox } => ctx.select(mailbox).await,
_ => anonymous::dispatch(ctx.inner).await,
}
}
@ -34,8 +34,8 @@ struct StateContext<'a> {
impl<'a> StateContext<'a> {
async fn lsub(
&self,
reference: MailboxCodec,
mailbox_wildcard: ListMailbox,
reference: &MailboxCodec,
mailbox_wildcard: &ListMailbox,
) -> Result<(Response, flow::Transition)> {
Ok((vec![ImapRes::Status(
Status::bad(Some(self.tag.clone()), None, "Not implemented").map_err(Error::msg)?,
@ -44,8 +44,8 @@ impl<'a> StateContext<'a> {
async fn list(
&self,
reference: MailboxCodec,
mailbox_wildcard: ListMailbox,
reference: &MailboxCodec,
mailbox_wildcard: &ListMailbox,
) -> Result<(Response, flow::Transition)> {
Ok((vec![
ImapRes::Status(Status::bad(Some(self.tag.clone()), None, "Not implemented").map_err(Error::msg)?),
@ -68,8 +68,8 @@ impl<'a> StateContext<'a> {
* TRACE END ---
*/
async fn select(&self, mailbox: MailboxCodec) -> Result<(Response, flow::Transition)> {
let name = String::try_from(mailbox)?;
async fn select(&self, mailbox: &MailboxCodec) -> Result<(Response, flow::Transition)> {
let name = String::try_from(mailbox.clone())?;
let mut mb = Mailbox::new(&self.user.creds, name.clone())?;
tracing::info!(username=%self.user.name, mailbox=%name, "mailbox.selected");

View File

@ -9,26 +9,25 @@ use imap_codec::types::sequence::SequenceSet;
use crate::imap::command::authenticated;
use crate::imap::session::InnerContext;
use crate::imap::flow::User;
use crate::imap::flow;
use crate::mailbox::Mailbox;
/*
pub async fn dispatch<'a>(inner: InnerContext<'a>, user: &'a User, mailbox: &'a Mailbox) -> Result<Response> {
let ctx = StateContext { inner, user, mailbox, tag: &inner.req.tag };
match ctx.inner.req.body {
pub async fn dispatch<'a>(inner: InnerContext<'a>, user: &'a flow::User, mailbox: &'a Mailbox) -> Result<(Response, flow::Transition)> {
let ctx = StateContext { tag: &inner.req.tag, inner, user, mailbox };
match &ctx.inner.req.body {
CommandBody::Fetch { sequence_set, attributes, uid, } => ctx.fetch(sequence_set, attributes, uid).await,
_ => authenticated::dispatch(inner, user).await,
_ => authenticated::dispatch(ctx.inner, user).await,
}
}
*/
// --- PRIVATE ---
/*
struct StateContext<'a> {
inner: InnerContext<'a>,
user: &'a User,
user: &'a flow::User,
mailbox: &'a Mailbox,
tag: &'a Tag,
}
@ -37,13 +36,12 @@ struct StateContext<'a> {
impl<'a> StateContext<'a> {
pub async fn fetch(
&self,
sequence_set: SequenceSet,
attributes: MacroOrFetchAttributes,
uid: bool,
) -> Result<Response> {
Ok(vec![
sequence_set: &SequenceSet,
attributes: &MacroOrFetchAttributes,
uid: &bool,
) -> Result<(Response, flow::Transition)> {
Ok((vec![
ImapRes::Status(Status::bad(Some(self.tag.clone()), None, "Not implemented").map_err(Error::msg)?),
])
], flow::Transition::No))
}
}
*/

View File

@ -126,7 +126,7 @@ impl Instance {
let ctrl = match &self.state {
flow::State::NotAuthenticated => anonymous::dispatch(ctx).await,
flow::State::Authenticated(user) => authenticated::dispatch(ctx, user).await,
/*flow::State::Selected(user, mailbox) => selected::dispatch(ctx, user, mailbox).await,*/
flow::State::Selected(user, mailbox) => selected::dispatch(ctx, user, mailbox).await,
_ => Status::bad(Some(ctx.req.tag.clone()), None, "No commands are allowed in the LOGOUT state.")
.map(|s| (vec![ImapRes::Status(s)], flow::Transition::No))
.map_err(Error::msg),