protonmail-client 0.1.0

IMAP client library for Proton Mail via Proton Bridge
Documentation
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
//! Proton Mail IMAP client with typestate access control
//!
//! `ProtonClient<M>` is parameterised by an access mode:
//!
//! - [`ReadOnly`]  -- only read operations (list, fetch, search)
//! - [`ReadWrite`] -- read **and** write operations (move, flag,
//!   archive)
//!
//! This prevents accidental use of destructive operations when only
//! read access is intended.
//!
//! ```rust,no_run
//! use protonmail_client::{ImapConfig, ProtonClient, ReadOnly, ReadWrite};
//!
//! let cfg = ImapConfig::from_env().unwrap();
//!
//! // Read-only client -- write methods are not available.
//! let reader: ProtonClient<ReadOnly> = ProtonClient::new(cfg.clone());
//!
//! // Read-write client -- all methods are available.
//! let writer: ProtonClient<ReadWrite> = ProtonClient::new(cfg);
//! ```
//!
//! A read-only client cannot call write operations — this fails to
//! compile:
//!
//! ```compile_fail
//! use protonmail_client::{Flag, Folder, ImapConfig, ProtonClient};
//!
//! let cfg = ImapConfig::from_env().unwrap();
//! let client: ProtonClient = ProtonClient::new(cfg);
//! // ERROR: no method named `add_flag` found for `ProtonClient<ReadOnly>`
//! let _ = client.add_flag(1, &Folder::Inbox, &Flag::Seen);
//! ```
//!
//! ```compile_fail
//! use protonmail_client::{Folder, ImapConfig, ProtonClient};
//!
//! let cfg = ImapConfig::from_env().unwrap();
//! let client: ProtonClient = ProtonClient::new(cfg);
//! // ERROR: no method named `move_to_folder` found for `ProtonClient<ReadOnly>`
//! let _ = client.move_to_folder(1, &Folder::Inbox, &Folder::Trash);
//! ```
//!
//! ```compile_fail
//! use protonmail_client::{Folder, ImapConfig, ProtonClient};
//!
//! let cfg = ImapConfig::from_env().unwrap();
//! let client: ProtonClient = ProtonClient::new(cfg);
//! // ERROR: no method named `archive` found for `ProtonClient<ReadOnly>`
//! let _ = client.archive(1, &Folder::Inbox);
//! ```

use std::marker::PhantomData;

use crate::config::ImapConfig;
use crate::connection::{self, ImapSession};
use crate::error::{Error, Result};
use crate::flag::Flag;
use crate::folder::Folder;
use chrono::NaiveDate;
use email_extract::{Email, parse_email};
use futures::{StreamExt, pin_mut};
use tracing::{info, warn};

// ── Access-mode markers ────────────────────────────────────────────

/// Marker: read-only access. Write methods are not available.
#[derive(Debug, Clone, Copy)]
pub struct ReadOnly;

/// Marker: read-write access. All methods are available.
#[derive(Debug, Clone, Copy)]
pub struct ReadWrite;

// ── Client ─────────────────────────────────────────────────────────

/// IMAP client for Proton Mail via Proton Bridge.
///
/// The type parameter `M` controls which operations are available:
///
/// | `M`         | Read ops | Write ops |
/// |-------------|----------|-----------|
/// | `ReadOnly`  | yes      | no        |
/// | `ReadWrite` | yes      | yes       |
pub struct ProtonClient<M = ReadOnly> {
    config: ImapConfig,
    _mode: PhantomData<M>,
}

impl<M> ProtonClient<M> {
    #[must_use]
    pub const fn new(config: ImapConfig) -> Self {
        Self {
            config,
            _mode: PhantomData,
        }
    }
}

// ── Read operations (available on any M) ───────────────────────────

impl<M: Send + Sync> ProtonClient<M> {
    /// List all available IMAP folders.
    ///
    /// # Errors
    ///
    /// Returns an error if the connection or LIST command fails.
    pub async fn list_folders(&self) -> Result<Vec<String>> {
        let mut session = connection::connect(&self.config).await?;

        let mut folder_stream = session
            .list(Some(""), Some("*"))
            .await
            .map_err(|e| Error::Imap(format!("List folders failed: {e}")))?;

        let mut names = Vec::new();
        while let Some(item) = folder_stream.next().await {
            if let Ok(name) = item {
                names.push(name.name().to_string());
            }
        }
        drop(folder_stream);

        session.logout().await.ok();
        Ok(names)
    }

    /// Fetch a single email by UID from a folder.
    ///
    /// # Errors
    ///
    /// Returns an error if the connection, SELECT, or FETCH fails,
    /// or if the message body cannot be parsed.
    pub async fn fetch_uid(&self, folder: &Folder, uid: u32) -> Result<Email> {
        let mut session = connection::connect(&self.config).await?;
        connection::select(&mut session, folder.as_str()).await?;

        let email = Self::fetch_single(&mut session, uid).await?;

        session.logout().await.ok();
        Ok(email)
    }

    /// Fetch all unseen emails from a folder.
    ///
    /// # Errors
    ///
    /// Returns an error if the connection, SELECT, or SEARCH fails.
    pub async fn fetch_unseen(&self, folder: &Folder) -> Result<Vec<Email>> {
        self.search(folder, "UNSEEN").await
    }

    /// Fetch all emails from a folder.
    ///
    /// # Errors
    ///
    /// Returns an error if the connection, SELECT, or SEARCH fails.
    pub async fn fetch_all(&self, folder: &Folder) -> Result<Vec<Email>> {
        self.search(folder, "ALL").await
    }

    /// Fetch the N most recent emails from a folder.
    ///
    /// # Errors
    ///
    /// Returns an error if the connection, SELECT, SEARCH, or
    /// FETCH fails.
    pub async fn fetch_last_n(&self, folder: &Folder, n: usize) -> Result<Vec<Email>> {
        let mut session = connection::connect(&self.config).await?;
        connection::select(&mut session, folder.as_str()).await?;

        let uids = session
            .uid_search("ALL")
            .await
            .map_err(|e| Error::Imap(format!("Search failed: {e}")))?;

        let mut uid_list: Vec<u32> = uids.into_iter().collect();
        uid_list.sort_unstable();

        let start = uid_list.len().saturating_sub(n);
        let recent_uids = &uid_list[start..];

        if recent_uids.is_empty() {
            session.logout().await.ok();
            return Ok(vec![]);
        }

        info!("Fetching {} most recent messages", recent_uids.len());

        let mut emails = Self::fetch_by_uids(&mut session, recent_uids).await?;
        emails.sort_by(|a, b| b.date.cmp(&a.date));

        session.logout().await.ok();
        Ok(emails)
    }

    /// Fetch emails within a date range from a folder.
    ///
    /// IMAP semantics: SINCE >= date, BEFORE < date.
    ///
    /// # Errors
    ///
    /// Returns an error if the connection, SELECT, or SEARCH fails.
    pub async fn fetch_date_range(
        &self,
        folder: &Folder,
        since: NaiveDate,
        before: NaiveDate,
    ) -> Result<Vec<Email>> {
        let since_str = since.format("%-d-%b-%Y").to_string();
        let before_str = before.format("%-d-%b-%Y").to_string();
        let query = format!("SINCE {since_str} BEFORE {before_str}");

        let mut emails = self.search(folder, &query).await?;
        emails.sort_by(|a, b| b.date.cmp(&a.date));
        Ok(emails)
    }

    /// Search emails using an arbitrary IMAP search query.
    ///
    /// # Errors
    ///
    /// Returns an error if the connection, SELECT, or SEARCH fails.
    pub async fn search(&self, folder: &Folder, query: &str) -> Result<Vec<Email>> {
        let mut session = connection::connect(&self.config).await?;
        connection::select(&mut session, folder.as_str()).await?;

        let uids = session
            .uid_search(query)
            .await
            .map_err(|e| Error::Imap(format!("Search failed: {e}")))?;

        let uid_list: Vec<u32> = uids.into_iter().collect();
        if uid_list.is_empty() {
            session.logout().await.ok();
            return Ok(vec![]);
        }

        info!("Found {} messages matching '{}'", uid_list.len(), query);

        let emails = Self::fetch_by_uids(&mut session, &uid_list).await?;

        session.logout().await.ok();
        Ok(emails)
    }

    // -- private helpers (read) --

    async fn fetch_by_uids(session: &mut ImapSession, uids: &[u32]) -> Result<Vec<Email>> {
        let mut emails = Vec::new();

        for uid in uids {
            match Self::fetch_single(session, *uid).await {
                Ok(email) => emails.push(email),
                Err(e) => {
                    warn!("Failed to fetch UID {}: {}", uid, e);
                }
            }
        }

        Ok(emails)
    }

    async fn fetch_single(session: &mut ImapSession, uid: u32) -> Result<Email> {
        let uid_set = format!("{uid}");
        let mut messages = session
            .uid_fetch(&uid_set, "(BODY.PEEK[])")
            .await
            .map_err(|e| Error::Imap(format!("Fetch failed: {e}")))?;

        if let Some(msg_result) = messages.next().await {
            let msg = msg_result.map_err(|e| Error::Imap(format!("Fetch error: {e}")))?;
            if let Some(body) = msg.body() {
                return parse_email(uid, body).map_err(|e| Error::Parse(e.to_string()));
            }
        }

        Err(Error::Imap(format!("No body found for UID {uid}")))
    }
}

// ── Write operations (only on ReadWrite) ───────────────────────────

impl ProtonClient<ReadWrite> {
    /// Move an email from one folder to another.
    ///
    /// Selects `from`, copies the message to `to`, marks it
    /// `\Deleted` in the source folder, and expunges.
    ///
    /// # Errors
    ///
    /// Returns an error if any IMAP command fails.
    pub async fn move_to_folder(&self, uid: u32, from: &Folder, to: &Folder) -> Result<()> {
        let mut session = connection::connect(&self.config).await?;
        connection::select(&mut session, from.as_str()).await?;

        let uid_set = format!("{uid}");

        // COPY to destination
        session
            .uid_copy(&uid_set, to.as_str())
            .await
            .map_err(|e| Error::Imap(format!("Copy failed: {e}")))?;

        // Mark \Deleted in source
        let mut store_stream = session
            .uid_store(&uid_set, "+FLAGS (\\Deleted)")
            .await
            .map_err(|e| Error::Imap(format!("Store +Deleted failed: {e}")))?;
        while store_stream.next().await.is_some() {}
        drop(store_stream);

        // Expunge to permanently remove
        {
            let expunge_stream = session
                .expunge()
                .await
                .map_err(|e| Error::Imap(format!("Expunge failed: {e}")))?;
            pin_mut!(expunge_stream);
            while expunge_stream.next().await.is_some() {}
        }

        session.logout().await.ok();
        Ok(())
    }

    /// Add a flag to an email.
    ///
    /// # Errors
    ///
    /// Returns an error if the connection, SELECT, or STORE fails.
    pub async fn add_flag(&self, uid: u32, folder: &Folder, flag: &Flag) -> Result<()> {
        let mut session = connection::connect(&self.config).await?;
        connection::select(&mut session, folder.as_str()).await?;

        let uid_set = format!("{uid}");
        let store_arg = format!("+FLAGS ({})", flag.as_imap_str());

        let mut stream = session
            .uid_store(&uid_set, &store_arg)
            .await
            .map_err(|e| Error::Imap(format!("Store failed: {e}")))?;
        while stream.next().await.is_some() {}
        drop(stream);

        session.logout().await.ok();
        Ok(())
    }

    /// Remove a flag from an email.
    ///
    /// # Errors
    ///
    /// Returns an error if the connection, SELECT, or STORE fails.
    pub async fn remove_flag(&self, uid: u32, folder: &Folder, flag: &Flag) -> Result<()> {
        let mut session = connection::connect(&self.config).await?;
        connection::select(&mut session, folder.as_str()).await?;

        let uid_set = format!("{uid}");
        let store_arg = format!("-FLAGS ({})", flag.as_imap_str());

        let mut stream = session
            .uid_store(&uid_set, &store_arg)
            .await
            .map_err(|e| Error::Imap(format!("Store failed: {e}")))?;
        while stream.next().await.is_some() {}
        drop(stream);

        session.logout().await.ok();
        Ok(())
    }

    /// Archive an email by moving it to the Archive folder.
    ///
    /// # Errors
    ///
    /// Returns an error if the move operation fails.
    pub async fn archive(&self, uid: u32, from: &Folder) -> Result<()> {
        self.move_to_folder(uid, from, &Folder::Archive).await
    }

    /// Remove the `\Seen` flag from all messages in a folder.
    ///
    /// # Errors
    ///
    /// Returns an error if the connection, SELECT, SEARCH, or STORE
    /// fails.
    pub async fn unmark_all_read(&self, folder: &Folder) -> Result<()> {
        let mut session = connection::connect(&self.config).await?;
        connection::select(&mut session, folder.as_str()).await?;

        let uids = session
            .uid_search("SEEN")
            .await
            .map_err(|e| Error::Imap(format!("Search failed: {e}")))?;

        let uid_list: Vec<u32> = uids.into_iter().collect();
        if uid_list.is_empty() {
            session.logout().await.ok();
            return Ok(());
        }

        let uid_set = uid_list
            .iter()
            .map(ToString::to_string)
            .collect::<Vec<_>>()
            .join(",");

        let mut stream = session
            .uid_store(&uid_set, "-FLAGS (\\Seen)")
            .await
            .map_err(|e| Error::Imap(format!("Store failed: {e}")))?;
        while stream.next().await.is_some() {}
        drop(stream);

        session.logout().await.ok();
        Ok(())
    }
}