2022-06-13 16:01:07 +00:00
|
|
|
use anyhow::Result;
|
2022-06-03 15:56:47 +00:00
|
|
|
use boitalettres::proto::{Request, Response};
|
2022-06-13 16:01:07 +00:00
|
|
|
use boitalettres::errors::Error as BalError;
|
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-13 09:44:02 +00:00
|
|
|
use crate::session;
|
2022-06-03 15:56:47 +00:00
|
|
|
|
2022-06-09 08:43:38 +00:00
|
|
|
pub struct Command<'a> {
|
2022-06-07 11:27:26 +00:00
|
|
|
tag: Tag,
|
2022-06-13 09:44:02 +00:00
|
|
|
session: &'a mut session::Instance,
|
2022-06-03 15:56:47 +00:00
|
|
|
}
|
|
|
|
|
2022-06-09 08:43:38 +00:00
|
|
|
impl<'a> Command<'a> {
|
2022-06-13 09:44:02 +00:00
|
|
|
pub fn new(tag: Tag, session: &'a mut session::Instance) -> Self {
|
2022-06-09 08:43:38 +00:00
|
|
|
Self { tag, session }
|
2022-06-03 15:56:47 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 16:01:07 +00:00
|
|
|
pub async fn capability(&self) -> Result<Response> {
|
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-13 16:01:07 +00:00
|
|
|
pub async fn login(&mut self, username: AString, password: AString) -> Result<Response> {
|
|
|
|
let (u, p) = (String::try_from(username)?, String::try_from(password)?);
|
|
|
|
tracing::info!(user = %u, "command.login");
|
2022-06-03 15:56:47 +00:00
|
|
|
|
2022-06-09 08:43:38 +00:00
|
|
|
let creds = match self.session.mailstore.login_provider.login(&u, &p).await {
|
2022-06-13 16:01:07 +00:00
|
|
|
Err(_) => return Ok(Response::no("[AUTHENTICATIONFAILED] Authentication failed.")?),
|
2022-06-03 15:56:47 +00:00
|
|
|
Ok(c) => c,
|
|
|
|
};
|
|
|
|
|
2022-06-13 16:01:07 +00:00
|
|
|
self.session.user = Some(session::User { creds, name: u.clone(), });
|
2022-06-07 10:57:24 +00:00
|
|
|
|
2022-06-13 10:25:19 +00:00
|
|
|
tracing::info!(username=%u, "connected");
|
2022-06-13 16:01:07 +00:00
|
|
|
Ok(Response::ok("Logged in")?)
|
2022-06-03 15:56:47 +00:00
|
|
|
}
|
2022-06-07 11:27:26 +00:00
|
|
|
|
2022-06-13 16:01:07 +00:00
|
|
|
pub async fn lsub(&self, reference: MailboxCodec, mailbox_wildcard: ListMailbox) -> Result<Response> {
|
|
|
|
Ok(Response::bad("Not implemented")?)
|
2022-06-07 11:27:26 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 16:01:07 +00:00
|
|
|
pub async fn list(&self, reference: MailboxCodec, mailbox_wildcard: ListMailbox) -> Result<Response> {
|
|
|
|
Ok(Response::bad("Not implemented")?)
|
2022-06-07 11:27:26 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 16:01:07 +00:00
|
|
|
/*
|
|
|
|
* TRACE BEGIN ---
|
|
|
|
|
|
|
|
|
|
|
|
Example: C: A142 SELECT INBOX
|
|
|
|
S: * 172 EXISTS
|
|
|
|
S: * 1 RECENT
|
|
|
|
S: * OK [UNSEEN 12] Message 12 is first unseen
|
|
|
|
S: * OK [UIDVALIDITY 3857529045] UIDs valid
|
|
|
|
S: * OK [UIDNEXT 4392] Predicted next UID
|
|
|
|
S: * FLAGS (\Answered \Flagged \Deleted \Seen \Draft)
|
|
|
|
S: * OK [PERMANENTFLAGS (\Deleted \Seen \*)] Limited
|
|
|
|
S: A142 OK [READ-WRITE] SELECT completed
|
|
|
|
|
|
|
|
* TRACE END ---
|
|
|
|
*/
|
|
|
|
pub async fn select(&mut self, mailbox: MailboxCodec) -> Result<Response> {
|
|
|
|
let name = String::try_from(mailbox)?;
|
|
|
|
let user = match self.session.user.as_ref() {
|
|
|
|
Some(u) => u,
|
|
|
|
_ => return Ok(Response::no("You must be connected to use SELECT")?),
|
2022-06-13 10:25:19 +00:00
|
|
|
};
|
2022-06-07 11:46:13 +00:00
|
|
|
|
2022-06-13 16:01:07 +00:00
|
|
|
let mut mb = Mailbox::new(&user.creds, name.clone())?;
|
|
|
|
tracing::info!(username=%user.name, mailbox=%name, "mailbox.selected");
|
|
|
|
|
|
|
|
let sum = mb.summary().await?;
|
|
|
|
tracing::trace!(summary=%sum, "mailbox.summary");
|
2022-06-07 11:46:13 +00:00
|
|
|
|
2022-06-13 16:01:07 +00:00
|
|
|
let body = vec![
|
|
|
|
Data::Exists(sum.exists.try_into()?),
|
|
|
|
Data::Recent(0),
|
|
|
|
];
|
|
|
|
|
|
|
|
self.session.selected = Some(mb);
|
|
|
|
Ok(Response::ok("[READ-WRITE] Select completed")?.with_body(body))
|
2022-06-07 11:27:26 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 16:01:07 +00:00
|
|
|
pub async fn fetch(&self, sequence_set: SequenceSet, attributes: MacroOrFetchAttributes, uid: bool) -> Result<Response> {
|
|
|
|
Ok(Response::bad("Not implemented")?)
|
2022-06-07 11:27:26 +00:00
|
|
|
}
|
2022-06-03 15:56:47 +00:00
|
|
|
}
|