Skip to main content

ibridge_server/
mail_api.rs

1// SPDX-License-Identifier: Apache-2.0
2
3//! Mail operations the tools call. The binary adapts `MailSession` to this.
4
5use ibridge_core::{
6    Attachment, CoreError, Draft, FlagChange, MailSearch, Mailbox, MailboxPath, MailboxRole,
7    Message, MessageId, MessageSummary, OneClickFailure, UnsubscribeMethod,
8};
9
10/// One page of search hits, plus how many messages were skipped.
11#[derive(Debug, Clone, PartialEq, Eq)]
12pub struct MailHitPage {
13    /// Hits, newest first.
14    pub messages: Vec<MessageSummary>,
15    /// Present when another page exists.
16    pub next_cursor: Option<String>,
17    /// Messages skipped because they did not parse.
18    pub warnings: u32,
19}
20
21/// A message body window.
22#[derive(Debug, Clone, PartialEq, Eq)]
23pub struct MailBody {
24    /// Headers, the body window, and the attachment list.
25    pub message: Message,
26    /// Set when another window starts at this scalar offset.
27    pub next_offset: Option<usize>,
28}
29
30/// One attachment window.
31#[derive(Debug, Clone, PartialEq, Eq)]
32pub struct MailPart {
33    /// The part that was fetched.
34    pub attachment: Attachment,
35    /// Set when a text part has another window at this scalar offset.
36    pub next_offset: Option<usize>,
37}
38
39/// A message SMTP accepted.
40#[derive(Debug, Clone, PartialEq, Eq)]
41pub struct SentMail {
42    /// Id of the copy in Sent Messages.
43    ///
44    /// `None` when the message was delivered and the Sent append failed.
45    pub id: Option<MessageId>,
46}
47
48/// What unsubscribing from a message did.
49#[derive(Debug, Clone, PartialEq, Eq)]
50#[non_exhaustive]
51pub enum Unsubscribed {
52    /// An unsubscribe email to the sender was accepted by SMTP.
53    Emailed(SentMail),
54    /// The sender's server accepted a one-click POST (RFC 8058).
55    OneClicked,
56    /// The one-click POST failed, and nothing else was sent.
57    OneClickFailed {
58        /// Why it failed, as a class that carries no server text.
59        failure: OneClickFailure,
60        /// The message also offers an unsubscribe email, which was not sent.
61        mailto_offered: bool,
62    },
63    /// The sender offers only these web links, and nothing was sent.
64    Links(Vec<String>),
65    /// The caller asked for a method the message does not offer. Nothing was sent.
66    MethodNotOffered(UnsubscribeMethod),
67    /// The message has no usable `List-Unsubscribe` header.
68    NotOffered,
69}
70
71/// Where a move puts a message.
72#[derive(Debug, Clone, PartialEq, Eq)]
73#[non_exhaustive]
74pub enum MailDest {
75    /// A mailbox path.
76    Path(MailboxPath),
77    /// A well-known role.
78    Role(MailboxRole),
79}
80
81/// Mail backend for one account.
82#[trait_variant::make(Send)]
83pub trait MailApi {
84    /// Lists mailboxes.
85    ///
86    /// # Errors
87    ///
88    /// Returns [`CoreError`] when the mailbox list cannot be read.
89    async fn list_mailboxes(&self) -> Result<Vec<Mailbox>, CoreError>;
90
91    /// Searches messages.
92    ///
93    /// # Errors
94    ///
95    /// Returns [`CoreError`] when the search cannot be run.
96    async fn search(&self, query: &MailSearch) -> Result<MailHitPage, CoreError>;
97
98    /// Fetches one message. `offset` is a Unicode scalar index into the body.
99    ///
100    /// # Errors
101    ///
102    /// Returns [`CoreError`] when the message cannot be read.
103    async fn get_message(&self, id: &MessageId, offset: usize) -> Result<MailBody, CoreError>;
104
105    /// Fetches one attachment. `offset` is a Unicode scalar index into a text part.
106    ///
107    /// # Errors
108    ///
109    /// Returns [`CoreError`] when the part cannot be read.
110    async fn get_attachment(
111        &self,
112        id: &MessageId,
113        part: &str,
114        offset: usize,
115    ) -> Result<MailPart, CoreError>;
116
117    /// Fetches every decoded byte of one attachment, for saving to disk.
118    ///
119    /// A backend that cannot do this keeps the default, which refuses.
120    ///
121    /// # Errors
122    ///
123    /// Returns [`CoreError`] when the part cannot be read.
124    async fn get_attachment_bytes(
125        &self,
126        _id: &MessageId,
127        _part: &str,
128    ) -> Result<Attachment, CoreError> {
129        async { Err(CoreError::Unsupported("saving attachments")) }
130    }
131
132    /// Appends a draft.
133    ///
134    /// # Errors
135    ///
136    /// Returns [`CoreError`] when the draft cannot be saved.
137    async fn create_draft(&self, draft: &Draft) -> Result<MessageId, CoreError>;
138
139    /// Sends a message. `id: None` means SMTP accepted it and the Sent copy failed.
140    ///
141    /// # Errors
142    ///
143    /// Returns [`CoreError`] when the message cannot be sent.
144    async fn send(&self, draft: &Draft) -> Result<SentMail, CoreError>;
145
146    /// Unsubscribes from the sender of a message, using only what its own headers offer.
147    ///
148    /// A backend that cannot do this keeps the default, which refuses.
149    ///
150    /// # Errors
151    ///
152    /// Returns [`CoreError`] when the message cannot be read or the email cannot be sent.
153    async fn unsubscribe(
154        &self,
155        _id: &MessageId,
156        _method: Option<UnsubscribeMethod>,
157    ) -> Result<Unsubscribed, CoreError> {
158        async { Err(CoreError::Unsupported("unsubscribing")) }
159    }
160
161    /// Moves a message.
162    ///
163    /// # Errors
164    ///
165    /// Returns [`CoreError`] when the move cannot be done.
166    async fn move_message(&self, id: &MessageId, destination: &MailDest) -> Result<(), CoreError>;
167
168    /// Sets or clears flags.
169    ///
170    /// # Errors
171    ///
172    /// Returns [`CoreError`] when the flags cannot be stored.
173    async fn set_flags(&self, id: &MessageId, flags: &FlagChange) -> Result<(), CoreError>;
174}