aerogramme/src/command.rs

80 lines
3.0 KiB
Rust
Raw Normal View History

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-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-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-09 08:43:38 +00:00
pub async fn login(&mut 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");
2022-06-09 08:43:38 +00:00
let creds = match self.session.mailstore.login_provider.login(&u, &p).await {
2022-06-03 15:56:47 +00:00
Err(_) => return Response::no("[AUTHENTICATIONFAILED] Authentication failed."),
Ok(c) => c,
};
2022-06-09 08:43:38 +00:00
self.session.creds = Some(creds);
2022-06-13 10:25:19 +00:00
self.session.username = Some(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-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-09 08:43:38 +00:00
pub async fn select(&mut self, mailbox: MailboxCodec) -> Result<Response, BalError> {
2022-06-13 10:25:19 +00:00
let (name, creds) = match (String::try_from(mailbox), self.session.creds.as_ref()) {
(Ok(n), Some(c)) => (n, c),
(_, None) => return Response::no("You must be connected to use SELECT"),
(Err(e), _) => {
tracing::warn!("Unable to decode mailbox name: {:#?}", e);
return Response::bad("Unable to decode mailbox name")
},
};
2022-06-07 11:46:13 +00:00
2022-06-13 10:25:19 +00:00
let mb = Mailbox::new(creds, name.clone()).unwrap();
2022-06-09 08:43:38 +00:00
self.session.selected = Some(mb);
2022-06-13 10:25:19 +00:00
let user = self.session.username.as_ref().unwrap();
2022-06-07 11:46:13 +00:00
2022-06-13 10:25:19 +00:00
tracing::info!(username=%user, mailbox=%name, "mailbox-selected");
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
}