aerogramme/src/command.rs

70 lines
2.4 KiB
Rust
Raw Normal View History

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:27:26 +00:00
use imap_codec::types::mailbox::{Mailbox, ListMailbox};
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;
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() {
Err(_) => return Response::bad("[AUTHENTICATIONFAILED] Unable to acquire mutex."),
Ok(s) => s,
};
session.creds = Some(creds);
2022-06-03 15:56:47 +00:00
Response::ok("Logged in")
}
2022-06-07 11:27:26 +00:00
pub async fn lsub(&self, reference: Mailbox, mailbox_wildcard: ListMailbox) -> Result<Response, BalError> {
Response::bad("Not implemented")
}
pub async fn list(&self, reference: Mailbox, mailbox_wildcard: ListMailbox) -> Result<Response, BalError> {
Response::bad("Not implemented")
}
pub async fn select(&self, mailbox: Mailbox) -> Result<Response, BalError> {
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
}