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