From ac974184def76fefba6c36c5c9e0ff0298f860ff Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Wed, 22 Jun 2022 17:25:04 +0200 Subject: [PATCH] Refactor selected --- src/imap/command/anonymous.rs | 6 +++--- src/imap/command/authenticated.rs | 18 +++++++++--------- src/imap/command/selected.rs | 30 ++++++++++++++---------------- src/imap/session.rs | 2 +- 4 files changed, 27 insertions(+), 29 deletions(-) diff --git a/src/imap/command/anonymous.rs b/src/imap/command/anonymous.rs index c6af354..7a5e514 100644 --- a/src/imap/command/anonymous.rs +++ b/src/imap/command/anonymous.rs @@ -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 { diff --git a/src/imap/command/authenticated.rs b/src/imap/command/authenticated.rs index 5ac3092..b4c4432 100644 --- a/src/imap/command/authenticated.rs +++ b/src/imap/command/authenticated.rs @@ -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"); diff --git a/src/imap/command/selected.rs b/src/imap/command/selected.rs index 93592c1..a068fab 100644 --- a/src/imap/command/selected.rs +++ b/src/imap/command/selected.rs @@ -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 { - 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 { - 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)) } } -*/ diff --git a/src/imap/session.rs b/src/imap/session.rs index e270ec6..a500e2b 100644 --- a/src/imap/session.rs +++ b/src/imap/session.rs @@ -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),