use std::fmt;
use std::num::NonZeroU32;
use crate::error::{CoreError, ParseError};
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub struct MailboxPath(String);
impl MailboxPath {
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
impl TryFrom<&str> for MailboxPath {
type Error = ParseError;
fn try_from(value: &str) -> Result<Self, Self::Error> {
if value.is_empty()
|| value.len() > 1024
|| value.contains("://")
|| value.starts_with("//")
|| value.chars().any(char::is_control)
{
return Err(ParseError::MailboxPath);
}
Ok(Self(value.to_owned()))
}
}
impl fmt::Display for MailboxPath {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub struct Uid(NonZeroU32);
impl Uid {
#[must_use]
pub const fn get(self) -> u32 {
self.0.get()
}
}
impl TryFrom<u32> for Uid {
type Error = ParseError;
fn try_from(value: u32) -> Result<Self, Self::Error> {
NonZeroU32::new(value).map(Self).ok_or(ParseError::Uid)
}
}
impl fmt::Display for Uid {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.get())
}
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub struct UidValidity(NonZeroU32);
impl UidValidity {
#[must_use]
pub const fn get(self) -> u32 {
self.0.get()
}
}
impl TryFrom<u32> for UidValidity {
type Error = ParseError;
fn try_from(value: u32) -> Result<Self, Self::Error> {
NonZeroU32::new(value).map(Self).ok_or(ParseError::Uid)
}
}
impl fmt::Display for UidValidity {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.get())
}
}
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub struct MessageId {
pub mailbox: MailboxPath,
pub validity: UidValidity,
pub uid: Uid,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum MailboxRole {
Inbox,
Sent,
Drafts,
Trash,
Junk,
Archive,
Notes,
Other,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Mailbox {
pub path: MailboxPath,
pub role: MailboxRole,
pub total: u32,
pub unread: u32,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct MailSearch {
pub mailbox: Option<MailboxPath>,
pub role: Option<MailboxRole>,
pub from: Option<String>,
pub to: Option<String>,
pub subject: Option<String>,
pub text: Option<String>,
pub since: Option<String>,
pub before: Option<String>,
pub unread_only: bool,
pub flagged_only: bool,
pub limit: u32,
pub cursor: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct MessageSummary {
pub id: MessageId,
pub from: String,
pub subject: String,
pub date: String,
pub unread: bool,
pub flagged: bool,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct MailPage {
pub messages: Vec<MessageSummary>,
pub next_cursor: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct AttachmentInfo {
pub part: String,
pub content_type: String,
pub size: u64,
pub name: String,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Message {
pub id: MessageId,
pub from: String,
pub to: Vec<String>,
pub subject: String,
pub date: String,
pub body: String,
pub attachments: Vec<AttachmentInfo>,
pub unsubscribe: Option<Unsubscribe>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct UnsubscribeMail {
pub to: String,
pub subject: Option<String>,
pub body: Option<String>,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Unsubscribe {
pub mailto: Option<UnsubscribeMail>,
pub links: Vec<String>,
pub one_click: bool,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum OneClickFailure {
#[error("the link is not a plain https link on port 443")]
InvalidLink,
#[error("the link points to a private or local network address, so it was not contacted")]
Blocked,
#[error("the sender's server could not be reached")]
Unreachable,
#[error("the sender's server did not present a valid certificate")]
Tls,
#[error("the sender's server did not answer in time")]
Timeout,
#[error("the sender's server redirected the request, and redirects are not followed")]
Redirected,
#[error("the sender's server refused the request")]
Rejected,
#[error("the sender's server sent a reply that is not valid HTTP")]
BadResponse,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum UnsubscribeMethod {
Mailto,
OneClick,
Link,
}
impl Unsubscribe {
#[must_use]
pub fn one_click_link(&self) -> Option<&str> {
self.one_click
.then(|| {
self.links.iter().map(String::as_str).find(|link| {
link.get(..8)
.is_some_and(|head| head.eq_ignore_ascii_case("https://"))
})
})
.flatten()
}
#[must_use]
pub fn methods(&self) -> Vec<UnsubscribeMethod> {
let mut methods = Vec::new();
if self.one_click {
methods.push(UnsubscribeMethod::OneClick);
}
if self.mailto.is_some() {
methods.push(UnsubscribeMethod::Mailto);
}
if !self.links.is_empty() {
methods.push(UnsubscribeMethod::Link);
}
methods
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Attachment {
pub info: AttachmentInfo,
pub text: Option<String>,
pub bytes: Option<Vec<u8>>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Draft {
pub to: Vec<String>,
pub subject: String,
pub body: String,
pub from: Option<String>,
pub reply_to: Option<MessageId>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct FlagChange {
pub seen: Option<bool>,
pub flagged: Option<bool>,
}
#[trait_variant::make(Send)]
pub trait MailProvider {
async fn list_mailboxes(&self) -> Result<Vec<Mailbox>, CoreError>;
async fn search(&self, query: &MailSearch) -> Result<MailPage, CoreError>;
async fn get_message(&self, id: &MessageId) -> Result<Message, CoreError>;
async fn get_attachment(&self, id: &MessageId, part: &str) -> Result<Attachment, CoreError>;
async fn create_draft(&self, draft: &Draft) -> Result<MessageId, CoreError>;
async fn send(&self, draft: &Draft) -> Result<MessageId, CoreError>;
async fn move_to(&self, id: &MessageId, destination: &MailboxPath) -> Result<(), CoreError>;
async fn set_flags(&self, id: &MessageId, flags: &FlagChange) -> Result<(), CoreError>;
}