pidge 1.4.0

A fast CLI for e-mail and calendar
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
//! Simple mail actions that operate on a single message by fragment:
//! `mark-read`, `mark-unread`, `flag`, `unflag`, `archive`. Archive also
//! has a bulk mode (`--from` / `--older-than`) — see `archive_bulk`.
//!
//! All five share the same shape — resolve the fragment, hit a Graph endpoint,
//! print a one-line confirmation. Stale 404s purge the cache entry so a future
//! list refresh picks up the new server state.

use anyhow::{Context, Result, anyhow};
use chrono::{DateTime, Utc};
use colored::Colorize;
use std::collections::HashSet;

use pidge_client::{AuthClient, ClientError, GraphClient};
use pidge_core::Config;

use crate::commands::mail_delete::parse_older_than;
use crate::commands::mail_fragment::{purge_from_cache, resolve};

pub async fn mark_read(fragment: String) -> Result<()> {
    let (short_hash, msg) = resolve(&fragment)?;
    with_graph(
        |graph| async move { graph.mark_read(&msg.account, &msg.graph_id).await },
        &short_hash,
    )
    .await?;
    println!("{} Marked {} as read.", "✔".green(), short_hash.dimmed());
    Ok(())
}

pub async fn mark_unread(fragment: String) -> Result<()> {
    let (short_hash, msg) = resolve(&fragment)?;
    with_graph(
        |graph| async move { graph.mark_unread(&msg.account, &msg.graph_id).await },
        &short_hash,
    )
    .await?;
    println!("{} Marked {} as unread.", "✔".green(), short_hash.dimmed());
    Ok(())
}

pub async fn flag(fragment: String) -> Result<()> {
    let (short_hash, msg) = resolve(&fragment)?;
    with_graph(
        |graph| async move { graph.set_flag(&msg.account, &msg.graph_id, true).await },
        &short_hash,
    )
    .await?;
    println!("{} Flagged {}.", "✔".green(), short_hash.dimmed());
    Ok(())
}

pub async fn unflag(fragment: String) -> Result<()> {
    let (short_hash, msg) = resolve(&fragment)?;
    with_graph(
        |graph| async move { graph.set_flag(&msg.account, &msg.graph_id, false).await },
        &short_hash,
    )
    .await?;
    println!("{} Unflagged {}.", "✔".green(), short_hash.dimmed());
    Ok(())
}

pub async fn archive(fragment: String) -> Result<()> {
    let (short_hash, msg) = resolve(&fragment)?;
    with_graph(
        |graph| async move {
            graph
                .move_message(&msg.account, &msg.graph_id, "archive")
                .await
        },
        &short_hash,
    )
    .await?;
    // Archived messages get a new ID in the Archive folder; the old cache
    // entry is stale.
    let _ = purge_from_cache(&short_hash);
    println!("{} Archived {}.", "✔".green(), short_hash.dimmed());
    Ok(())
}

/// Dispatch for `pidge mail archive`: single (fragment) or bulk (`--from` /
/// `--older-than`). Mirrors `mail delete` so the two destructive surfaces
/// share their safety model — bulk always needs `-y` and one filter.
pub async fn archive_dispatch(
    fragment: Option<String>,
    from: Vec<String>,
    older_than: Option<String>,
    accounts: Vec<String>,
    yes: bool,
) -> Result<()> {
    match (fragment, from.is_empty(), older_than.as_ref()) {
        (Some(f), true, None) => archive(f).await,
        (None, false, _) | (None, _, Some(_)) => {
            archive_bulk(from, older_than, accounts, yes).await
        }
        (None, true, None) => Err(anyhow!(
            "Specify a fragment, `--from <sender>`, or `--older-than <spec>`. \
             Run `pidge mail archive --help`."
        )),
        (Some(_), _, _) => unreachable!("clap enforces conflicts_with"),
    }
}

async fn archive_bulk(
    from: Vec<String>,
    older_than: Option<String>,
    account_filter: Vec<String>,
    yes: bool,
) -> Result<()> {
    let gate = crate::guardrail::gate(
        crate::guardrail::GuardrailAction::Bulk,
        "bulk archive of matching messages",
    )?;
    if gate == crate::guardrail::Gate::DryRun {
        return Ok(());
    }

    if !yes {
        return Err(anyhow!(
            "Bulk archive requires explicit `-y` confirmation — there is no \
             interactive prompt. Re-run with `-y` if you really mean it."
        ));
    }

    let cutoff: Option<DateTime<Utc>> = older_than.as_deref().map(parse_older_than).transpose()?;
    let from_set: HashSet<String> = from.iter().map(|s| s.to_ascii_lowercase()).collect();

    let config = Config::load()?;
    if config.accounts.is_empty() {
        return Err(anyhow!(
            "No accounts signed in. Run `pidge account add` to add one."
        ));
    }
    let target_emails: Vec<String> = if account_filter.is_empty() {
        config.accounts.iter().map(|a| a.email.clone()).collect()
    } else {
        for f in &account_filter {
            if config.find(f).is_none() {
                return Err(anyhow!("not signed in to {f}"));
            }
        }
        account_filter
    };

    let filter_desc = describe_filter(&from_set, cutoff.as_ref(), older_than.as_deref());
    let scope = if from_set.is_empty() {
        "Inbox"
    } else {
        "mailbox"
    };
    println!(
        "{} Archiving {scope} messages where {}…",
        "Bulk".yellow().bold(),
        filter_desc
    );

    let graph = GraphClient::new(AuthClient::from_env()?)?;
    const PAGE_SIZE: usize = 50;
    const MAX_PAGES: usize = 400;

    let mut total = 0usize;
    for email in &target_emails {
        let count = if from_set.is_empty() {
            // Date-only mode: walk the Inbox sorted by date and stop once
            // we've passed the cutoff.
            move_bulk_inbox_for_account(
                &graph, email, "archive", "archive", cutoff, PAGE_SIZE, MAX_PAGES,
            )
            .await?
        } else {
            // Sender-filter mode: run one Graph search per sender so we
            // sweep ALL folders (marketing senders frequently land in
            // Junk, not Inbox), then archive everything that came back.
            move_bulk_by_sender_for_account(&graph, email, "archive", "archive", &from_set, cutoff)
                .await?
        };
        total += count;
        println!(
            "{} {}: archived {count} message{}",
            "✔".green(),
            email,
            if count == 1 { "" } else { "s" }
        );
    }
    println!("{} Total: {total}.", "✔".green().bold());
    Ok(())
}

pub(crate) fn describe_filter(
    from: &HashSet<String>,
    cutoff: Option<&DateTime<Utc>>,
    older_than_spec: Option<&str>,
) -> String {
    let mut parts: Vec<String> = Vec::new();
    if !from.is_empty() {
        let mut senders: Vec<&str> = from.iter().map(String::as_str).collect();
        senders.sort_unstable();
        parts.push(format!("from is one of [{}]", senders.join(", ")));
    }
    if let (Some(c), Some(spec)) = (cutoff, older_than_spec) {
        parts.push(format!(
            "received before {} (cutoff {})",
            spec,
            c.format("%Y-%m-%d %H:%M UTC")
        ));
    }
    parts.join(" AND ")
}

/// Date-only bulk: walk the Inbox newest-first and stop once we cross
/// the cutoff, moving matched messages to `destination`. Mirrors
/// `delete_bulk_for_account` — see that function's comments for the
/// rationale on PAGE_SIZE / MAX_PAGES / sort-and-stop. Shared by
/// `mail archive` (destination `"archive"`) and `mail move`.
#[allow(clippy::too_many_arguments)]
pub(crate) async fn move_bulk_inbox_for_account(
    graph: &GraphClient,
    account: &str,
    destination: &str,
    verb: &str,
    cutoff: Option<DateTime<Utc>>,
    page_size: usize,
    max_pages: usize,
) -> Result<usize> {
    let mut archived = 0usize;
    let mut skip = 0usize;
    for _ in 0..max_pages {
        let result = graph
            .list_inbox(account, page_size, skip, false)
            .await
            .context("listing inbox for bulk move")?;
        if result.messages.is_empty() {
            break;
        }

        let to_archive: Vec<&pidge_core::Message> = result
            .messages
            .iter()
            .filter(|m| cutoff.is_none_or(|c| m.received_at < c))
            .collect();
        let matched_now = to_archive.len();
        archived += move_many(graph, account, &to_archive, destination, verb).await;

        // Advance skip past kept items (the matched ones got moved out of
        // the Inbox, so the same skip now points at fresh messages).
        let page_len = result.messages.len();
        skip += page_len - matched_now;

        // Sorted desc by received_at — once the oldest on the page is
        // already newer than the cutoff, nothing further back can match.
        if let Some(c) = cutoff {
            let oldest = result
                .messages
                .iter()
                .map(|m| m.received_at)
                .min()
                .unwrap_or(c);
            if oldest >= c {
                break;
            }
        }

        if !result.has_more && matched_now == 0 {
            break;
        }
    }
    Ok(archived)
}

/// Sender-filter bulk: search the entire mailbox (all folders) for each
/// sender, moving matches to `destination`. Marketing senders frequently
/// get auto-classified into the Junk Email folder, so an Inbox-only sweep
/// would miss them. We use Graph's `$search` because that endpoint is
/// folder-agnostic. Shared by `mail archive` and `mail move`.
pub(crate) async fn move_bulk_by_sender_for_account(
    graph: &GraphClient,
    account: &str,
    destination: &str,
    verb: &str,
    from_set: &HashSet<String>,
    cutoff: Option<DateTime<Utc>>,
) -> Result<usize> {
    // Graph $search has a hard ~1000-result cap per query. One query per
    // sender keeps each search comfortably under that and lets us report
    // per-sender numbers if we ever want to.
    const SEARCH_LIMIT: usize = 1000;
    let mut archived = 0usize;
    for sender in from_set {
        let messages = match graph
            .search_messages(account, &format!("from:{sender}"), SEARCH_LIMIT)
            .await
        {
            Ok(page) => page.messages,
            Err(e) => {
                eprintln!("  {} search failed for {}: {e}", "!".red(), sender.dimmed());
                continue;
            }
        };
        let matches: Vec<&pidge_core::Message> = messages
            .iter()
            .filter(|m| {
                // Graph $search is fuzzy — re-check exact sender match
                // before moving anything. Also apply the date cutoff if set.
                m.from.address.to_ascii_lowercase() == *sender
                    && cutoff.is_none_or(|c| m.received_at < c)
            })
            .collect();
        archived += move_many(graph, account, &matches, destination, verb).await;
    }
    Ok(archived)
}

/// Concurrently move a batch of messages to `destination` (a Graph folder ID
/// or well-known name) and report how many succeeded. Cache entries are
/// purged for moved messages so the next list refresh sees the new state.
///
/// `verb` is only used to phrase the per-failure error line ("failed to
/// archive …" vs "failed to move …").
///
/// Concurrency is capped at `MAX_INFLIGHT` because Graph's per-mailbox
/// move endpoint trips an `ApplicationThrottled` (HTTP 429) on aggressive
/// parallelism — we saw it as low as ~8 inflight moves. On 429 we retry
/// with exponential backoff up to `MAX_RETRIES` times.
pub(crate) async fn move_many(
    graph: &GraphClient,
    account: &str,
    messages: &[&pidge_core::Message],
    destination: &str,
    verb: &str,
) -> usize {
    use pidge_client::graph::batch::BatchRequest;
    let requests: Vec<BatchRequest> = messages
        .iter()
        .map(|m| {
            BatchRequest::json(
                pidge_core::short_hash(&m.id),
                "POST",
                format!("/me/messages/{}/move", m.id),
                serde_json::json!({"destinationId": destination}),
            )
        })
        .collect();
    let responses = match graph.batch_all(account, requests).await {
        Ok(r) => r,
        Err(e) => {
            eprintln!("  {} bulk {verb} failed: {e}", "!".red());
            return 0;
        }
    };
    let mut ok = 0usize;
    for item in responses {
        if item.is_success() {
            ok += 1;
            let _ = purge_from_cache(&item.id);
        } else if item.status == 404 {
            // already gone
        } else {
            eprintln!(
                "  {} failed to {verb} {}: HTTP {}",
                "!".red(),
                item.id.dimmed(),
                item.status
            );
        }
    }
    ok
}

/// Move a single message to `destination`, retrying with exponential
/// backoff on `429 ApplicationThrottled` since Graph's per-mailbox move
/// limit is hit easily on bulk operations. Retry/backoff now lives in the
/// client's send_with_retry seam; this wrapper remains for call-site clarity.
pub(crate) async fn move_with_retry(
    graph: &GraphClient,
    account: &str,
    message_id: &str,
    destination: &str,
) -> Result<(), ClientError> {
    graph.move_message(account, message_id, destination).await
}

/// Run an async closure with a configured GraphClient. On a 404 from Graph,
/// purges the cache entry so the user's next list refresh sees the new state,
/// and converts the error into a user-friendly message.
async fn with_graph<F, Fut>(op: F, short_hash: &str) -> Result<()>
where
    F: FnOnce(GraphClient) -> Fut,
    Fut: std::future::Future<Output = Result<(), ClientError>>,
{
    let graph = GraphClient::new(AuthClient::from_env()?)?;
    match op(graph).await {
        Ok(()) => Ok(()),
        Err(ClientError::Graph { status: 404, .. }) => {
            let _ = purge_from_cache(short_hash);
            Err(anyhow!(
                "Message not found on server (it may have been deleted or moved). \
                 Run `pidge mail` to refresh the cache."
            ))
        }
        Err(e) => Err(e.into()),
    }
}