1use std::fmt;
6use std::num::NonZeroU32;
7
8use crate::error::{CoreError, ParseError};
9
10#[derive(Clone, PartialEq, Eq, Hash, Debug)]
12pub struct MailboxPath(String);
13
14impl MailboxPath {
15 #[must_use]
17 pub fn as_str(&self) -> &str {
18 &self.0
19 }
20}
21
22impl TryFrom<&str> for MailboxPath {
23 type Error = ParseError;
24
25 fn try_from(value: &str) -> Result<Self, Self::Error> {
26 if value.is_empty()
27 || value.len() > 1024
28 || value.contains("://")
29 || value.starts_with("//")
30 || value.chars().any(char::is_control)
31 {
32 return Err(ParseError::MailboxPath);
33 }
34 Ok(Self(value.to_owned()))
35 }
36}
37
38impl fmt::Display for MailboxPath {
39 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40 f.write_str(self.as_str())
41 }
42}
43
44#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
46pub struct Uid(NonZeroU32);
47
48impl Uid {
49 #[must_use]
51 pub const fn get(self) -> u32 {
52 self.0.get()
53 }
54}
55
56impl TryFrom<u32> for Uid {
57 type Error = ParseError;
58
59 fn try_from(value: u32) -> Result<Self, Self::Error> {
60 NonZeroU32::new(value).map(Self).ok_or(ParseError::Uid)
61 }
62}
63
64impl fmt::Display for Uid {
65 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
66 write!(f, "{}", self.get())
67 }
68}
69
70#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
72pub struct UidValidity(NonZeroU32);
73
74impl UidValidity {
75 #[must_use]
77 pub const fn get(self) -> u32 {
78 self.0.get()
79 }
80}
81
82impl TryFrom<u32> for UidValidity {
83 type Error = ParseError;
84
85 fn try_from(value: u32) -> Result<Self, Self::Error> {
86 NonZeroU32::new(value).map(Self).ok_or(ParseError::Uid)
87 }
88}
89
90impl fmt::Display for UidValidity {
91 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
92 write!(f, "{}", self.get())
93 }
94}
95
96#[derive(Clone, PartialEq, Eq, Hash, Debug)]
100pub struct MessageId {
101 pub mailbox: MailboxPath,
103 pub validity: UidValidity,
105 pub uid: Uid,
107}
108
109#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
111#[non_exhaustive]
112pub enum MailboxRole {
113 Inbox,
115 Sent,
117 Drafts,
119 Trash,
121 Junk,
123 Archive,
125 Notes,
127 Other,
129}
130
131#[derive(Clone, Debug, PartialEq, Eq)]
133pub struct Mailbox {
134 pub path: MailboxPath,
136 pub role: MailboxRole,
138 pub total: u32,
140 pub unread: u32,
142}
143
144#[derive(Clone, Debug, PartialEq, Eq)]
146pub struct MailSearch {
147 pub mailbox: Option<MailboxPath>,
149 pub role: Option<MailboxRole>,
151 pub from: Option<String>,
153 pub to: Option<String>,
155 pub subject: Option<String>,
157 pub text: Option<String>,
159 pub since: Option<String>,
161 pub before: Option<String>,
163 pub unread_only: bool,
165 pub flagged_only: bool,
167 pub limit: u32,
169 pub cursor: Option<String>,
171}
172
173#[derive(Clone, Debug, PartialEq, Eq)]
175pub struct MessageSummary {
176 pub id: MessageId,
178 pub from: String,
180 pub subject: String,
182 pub date: String,
184 pub unread: bool,
186 pub flagged: bool,
188}
189
190#[derive(Clone, Debug, PartialEq, Eq)]
192pub struct MailPage {
193 pub messages: Vec<MessageSummary>,
195 pub next_cursor: Option<String>,
197}
198
199#[derive(Clone, Debug, PartialEq, Eq)]
201pub struct AttachmentInfo {
202 pub part: String,
204 pub content_type: String,
206 pub size: u64,
208 pub name: String,
210}
211
212#[derive(Clone, Debug, PartialEq, Eq)]
214pub struct Message {
215 pub id: MessageId,
217 pub from: String,
219 pub to: Vec<String>,
221 pub subject: String,
223 pub date: String,
225 pub body: String,
227 pub attachments: Vec<AttachmentInfo>,
229 pub unsubscribe: Option<Unsubscribe>,
231}
232
233#[derive(Clone, Debug, PartialEq, Eq)]
238pub struct UnsubscribeMail {
239 pub to: String,
241 pub subject: Option<String>,
243 pub body: Option<String>,
245}
246
247#[derive(Clone, Debug, Default, PartialEq, Eq)]
252pub struct Unsubscribe {
253 pub mailto: Option<UnsubscribeMail>,
255 pub links: Vec<String>,
257 pub one_click: bool,
259}
260
261#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)]
263#[non_exhaustive]
264pub enum OneClickFailure {
265 #[error("the link is not a plain https link on port 443")]
267 InvalidLink,
268 #[error("the link points to a private or local network address, so it was not contacted")]
270 Blocked,
271 #[error("the sender's server could not be reached")]
273 Unreachable,
274 #[error("the sender's server did not present a valid certificate")]
276 Tls,
277 #[error("the sender's server did not answer in time")]
279 Timeout,
280 #[error("the sender's server redirected the request, and redirects are not followed")]
282 Redirected,
283 #[error("the sender's server refused the request")]
285 Rejected,
286 #[error("the sender's server sent a reply that is not valid HTTP")]
288 BadResponse,
289}
290
291#[derive(Clone, Copy, Debug, PartialEq, Eq)]
293#[non_exhaustive]
294pub enum UnsubscribeMethod {
295 Mailto,
297 OneClick,
299 Link,
301}
302
303impl Unsubscribe {
304 #[must_use]
307 pub fn one_click_link(&self) -> Option<&str> {
308 self.one_click
309 .then(|| {
310 self.links.iter().map(String::as_str).find(|link| {
311 link.get(..8)
312 .is_some_and(|head| head.eq_ignore_ascii_case("https://"))
313 })
314 })
315 .flatten()
316 }
317
318 #[must_use]
320 pub fn methods(&self) -> Vec<UnsubscribeMethod> {
321 let mut methods = Vec::new();
322 if self.one_click {
323 methods.push(UnsubscribeMethod::OneClick);
324 }
325 if self.mailto.is_some() {
326 methods.push(UnsubscribeMethod::Mailto);
327 }
328 if !self.links.is_empty() {
329 methods.push(UnsubscribeMethod::Link);
330 }
331 methods
332 }
333}
334
335#[derive(Clone, Debug, PartialEq, Eq)]
337pub struct Attachment {
338 pub info: AttachmentInfo,
340 pub text: Option<String>,
342 pub bytes: Option<Vec<u8>>,
344}
345
346#[derive(Clone, Debug, PartialEq, Eq)]
348pub struct Draft {
349 pub to: Vec<String>,
351 pub subject: String,
353 pub body: String,
355 pub from: Option<String>,
357 pub reply_to: Option<MessageId>,
359}
360
361#[derive(Clone, Copy, Debug, PartialEq, Eq)]
363pub struct FlagChange {
364 pub seen: Option<bool>,
366 pub flagged: Option<bool>,
368}
369
370#[trait_variant::make(Send)]
372pub trait MailProvider {
373 async fn list_mailboxes(&self) -> Result<Vec<Mailbox>, CoreError>;
379
380 async fn search(&self, query: &MailSearch) -> Result<MailPage, CoreError>;
386
387 async fn get_message(&self, id: &MessageId) -> Result<Message, CoreError>;
393
394 async fn get_attachment(&self, id: &MessageId, part: &str) -> Result<Attachment, CoreError>;
400
401 async fn create_draft(&self, draft: &Draft) -> Result<MessageId, CoreError>;
407
408 async fn send(&self, draft: &Draft) -> Result<MessageId, CoreError>;
414
415 async fn move_to(&self, id: &MessageId, destination: &MailboxPath) -> Result<(), CoreError>;
421
422 async fn set_flags(&self, id: &MessageId, flags: &FlagChange) -> Result<(), CoreError>;
428}