2022-06-07 10:57:24 +00:00
|
|
|
use std::sync::{Arc, Mutex};
|
2022-06-03 15:56:47 +00:00
|
|
|
|
|
|
|
use boitalettres::errors::Error as BalError;
|
|
|
|
use boitalettres::proto::{Request, Response};
|
2022-06-07 11:27:26 +00:00
|
|
|
use imap_codec::types::core::{Tag, AString};
|
2022-06-03 15:56:47 +00:00
|
|
|
use imap_codec::types::response::{Capability, Data};
|
2022-06-07 11:46:13 +00:00
|
|
|
use imap_codec::types::mailbox::{Mailbox as MailboxCodec, ListMailbox};
|
2022-06-07 11:27:26 +00:00
|
|
|
use imap_codec::types::sequence::SequenceSet;
|
|
|
|
use imap_codec::types::fetch_attributes::MacroOrFetchAttributes;
|
2022-06-03 15:56:47 +00:00
|
|
|
|
2022-06-07 11:27:26 +00:00
|
|
|
use crate::mailstore::Mailstore;
|
2022-06-07 11:46:13 +00:00
|
|
|
use crate::mailbox::Mailbox;
|
2022-06-07 11:27:26 +00:00
|
|
|
use crate::service::Session;
|
2022-06-03 15:56:47 +00:00
|
|
|
|
|
|
|
pub struct Command {
|
2022-06-07 11:27:26 +00:00
|
|
|
tag: Tag,
|
|
|
|
mailstore: Arc<Mailstore>,
|
|
|
|
session: Arc<Mutex<Session>>,
|
2022-06-03 15:56:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Command {
|
2022-06-07 11:27:26 +00:00
|
|
|
pub fn new(tag: Tag, mailstore: Arc<Mailstore>, session: Arc<Mutex<Session>>) -> Self {
|
|
|
|
Self { tag, mailstore, session }
|
2022-06-03 15:56:47 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 11:27:26 +00:00
|
|
|
pub async fn capability(&self) -> Result<Response, BalError> {
|
2022-06-03 15:56:47 +00:00
|
|
|
let capabilities = vec![Capability::Imap4Rev1, Capability::Idle];
|
|
|
|
let body = vec![Data::Capability(capabilities)];
|
|
|
|
let r = Response::ok("Pre-login capabilities listed, post-login capabilities have more.")?
|
|
|
|
.with_body(body);
|
|
|
|
Ok(r)
|
|
|
|
}
|
|
|
|
|
2022-06-07 11:27:26 +00:00
|
|
|
pub async fn login(&self, username: AString, password: AString) -> Result<Response, BalError> {
|
2022-06-03 15:56:47 +00:00
|
|
|
let (u, p) = match (String::try_from(username), String::try_from(password)) {
|
|
|
|
(Ok(u), Ok(p)) => (u, p),
|
|
|
|
_ => return Response::bad("Invalid characters"),
|
|
|
|
};
|
|
|
|
|
|
|
|
tracing::debug!(user = %u, "command.login");
|
|
|
|
let creds = match self.mailstore.login_provider.login(&u, &p).await {
|
|
|
|
Err(_) => return Response::no("[AUTHENTICATIONFAILED] Authentication failed."),
|
|
|
|
Ok(c) => c,
|
|
|
|
};
|
|
|
|
|
2022-06-07 10:57:24 +00:00
|
|
|
let mut session = match self.session.lock() {
|
2022-06-07 11:46:13 +00:00
|
|
|
Err(_) => return Response::bad("[AUTHENTICATIONFAILED] Unable to acquire lock on session."),
|
2022-06-07 10:57:24 +00:00
|
|
|
Ok(s) => s,
|
|
|
|
};
|
|
|
|
session.creds = Some(creds);
|
2022-06-07 11:46:13 +00:00
|
|
|
drop(session);
|
2022-06-07 10:57:24 +00:00
|
|
|
|
2022-06-03 15:56:47 +00:00
|
|
|
Response::ok("Logged in")
|
|
|
|
}
|
2022-06-07 11:27:26 +00:00
|
|
|
|
2022-06-07 11:46:13 +00:00
|
|
|
pub async fn lsub(&self, reference: MailboxCodec, mailbox_wildcard: ListMailbox) -> Result<Response, BalError> {
|
2022-06-07 11:27:26 +00:00
|
|
|
Response::bad("Not implemented")
|
|
|
|
}
|
|
|
|
|
2022-06-07 11:46:13 +00:00
|
|
|
pub async fn list(&self, reference: MailboxCodec, mailbox_wildcard: ListMailbox) -> Result<Response, BalError> {
|
2022-06-07 11:27:26 +00:00
|
|
|
Response::bad("Not implemented")
|
|
|
|
}
|
|
|
|
|
2022-06-07 11:46:13 +00:00
|
|
|
pub async fn select(&self, mailbox: MailboxCodec) -> Result<Response, BalError> {
|
|
|
|
|
|
|
|
let mut session = match self.session.lock() {
|
|
|
|
Err(_) => return Response::no("[SELECTFAILED] Unable to acquire lock on session."),
|
|
|
|
Ok(s) => s,
|
|
|
|
};
|
|
|
|
|
|
|
|
let mb = Mailbox::new(session.creds.as_ref().unwrap(), "TestMailbox".to_string()).unwrap();
|
|
|
|
session.selected = Some(mb);
|
|
|
|
drop(session);
|
|
|
|
|
2022-06-07 11:27:26 +00:00
|
|
|
Response::bad("Not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn fetch(&self, sequence_set: SequenceSet, attributes: MacroOrFetchAttributes, uid: bool) -> Result<Response, BalError> {
|
|
|
|
Response::bad("Not implemented")
|
|
|
|
}
|
2022-06-03 15:56:47 +00:00
|
|
|
}
|