From c03be0a8c335c462bcc2f171145aef779a652736 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Tue, 7 Jun 2022 12:57:24 +0200 Subject: [PATCH] Implement a Mutex --- src/command.rs | 14 +++++++++++--- src/service.rs | 12 +++++++++--- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/command.rs b/src/command.rs index 56291ad..24c452b 100644 --- a/src/command.rs +++ b/src/command.rs @@ -1,4 +1,4 @@ -use std::sync::Arc; +use std::sync::{Arc, Mutex}; use boitalettres::errors::Error as BalError; use boitalettres::proto::{Request, Response}; @@ -6,14 +6,16 @@ use imap_codec::types::core::AString; use imap_codec::types::response::{Capability, Data}; use crate::mailstore; +use crate::service; pub struct Command { mailstore: Arc, + session: Arc>, } impl Command { - pub fn new(mailstore: Arc) -> Self { - Self { mailstore } + pub fn new(mailstore: Arc, session: Arc>) -> Self { + Self { mailstore, session } } pub async fn capability(self) -> Result { @@ -36,6 +38,12 @@ impl Command { Ok(c) => c, }; + let mut session = match self.session.lock() { + Err(_) => return Response::bad("[AUTHENTICATIONFAILED] Unable to acquire mutex."), + Ok(s) => s, + }; + session.creds = Some(creds); + Response::ok("Logged in") } } diff --git a/src/service.rs b/src/service.rs index 888a217..f032971 100644 --- a/src/service.rs +++ b/src/service.rs @@ -1,4 +1,4 @@ -use std::sync::Arc; +use std::sync::{Arc, Mutex}; use std::task::{Context, Poll}; use anyhow::Result; @@ -10,6 +10,7 @@ use imap_codec::types::command::CommandBody; use tower::Service; use crate::command; +use crate::login::Credentials; use crate::mailstore::Mailstore; pub struct Instance { @@ -36,12 +37,17 @@ impl<'a> Service<&'a AddrStream> for Instance { } } +pub struct Session { + pub creds: Option, +} + pub struct Connection { pub mailstore: Arc, + pub session: Arc>, } impl Connection { pub fn new(mailstore: Arc) -> Self { - Self { mailstore } + Self { mailstore, session: Arc::new(Mutex::new(Session { creds: None })) } } } impl Service for Connection { @@ -55,7 +61,7 @@ impl Service for Connection { fn call(&mut self, req: Request) -> Self::Future { tracing::debug!("Got request: {:#?}", req); - let cmd = command::Command::new(self.mailstore.clone()); + let cmd = command::Command::new(self.mailstore.clone(), self.session.clone()); Box::pin(async move { match req.body { CommandBody::Capability => cmd.capability().await,