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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
//! `pidge mail delete` — single or bulk delete with safety gates.
//!
//! Graph's DELETE /me/messages/{id} moves the message to Deleted Items (it's
//! not a hard delete). Recovery is still possible from Outlook's Deleted
//! Items folder.
//!
//! Safety:
//! - Single delete asks for an interactive Confirm by default; `-y` skips.
//! - Bulk delete (`--older-than`) ALWAYS requires `-y`. There is no
//!   interactive prompt — bulk mode is intended for scripts where typing
//!   `y` would be inconvenient, and forcing a flag means the user can't
//!   trip into it accidentally.

use anyhow::{Context, Result, anyhow};
use chrono::{DateTime, Datelike, Duration, NaiveDate, Timelike, Utc};
use colored::Colorize;
use futures::future::join_all;
use inquire::Confirm;
use std::collections::{BTreeMap, HashSet};

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

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

pub async fn run(
    fragments: Vec<String>,
    from: Vec<String>,
    older_than: Option<String>,
    accounts: Vec<String>,
    yes: bool,
) -> Result<()> {
    match (fragments.len(), from.is_empty(), older_than.as_ref()) {
        (1, true, None) => delete_single(fragments.into_iter().next().unwrap(), yes).await,
        (n, true, None) if n > 1 => delete_multi(fragments, yes).await,
        (0, false, _) | (0, _, Some(_)) => delete_bulk(from, older_than, accounts, yes).await,
        (0, true, None) => Err(anyhow!(
            "Specify one or more fragments, `--from <sender>`, or `--older-than <spec>`. \
             Run `pidge mail delete --help`."
        )),
        _ => unreachable!("clap enforces conflicts_with"),
    }
}

/// Delete an exact, hand-picked set of messages named by hash fragments.
/// Each fragment is resolved against the local cache; resolution failures are
/// reported but don't abort the run (the resolvable ones still get deleted).
/// Requires `-y` — there's no per-message prompt for a batch this size.
async fn delete_multi(fragments: Vec<String>, yes: bool) -> Result<()> {
    let gate = crate::guardrail::gate(
        crate::guardrail::GuardrailAction::Bulk,
        &format!("delete {} hand-picked messages", fragments.len()),
    )?;
    if gate == crate::guardrail::Gate::DryRun {
        return Ok(());
    }
    if !yes {
        return Err(anyhow!(
            "Deleting multiple messages requires explicit `-y` confirmation. \
             Re-run with `-y` if you really mean it."
        ));
    }

    // Resolve every fragment first, grouping by account for batched deletes.
    // (short_hash, graph_id) per account. De-dupe on short_hash so a repeated
    // fragment isn't issued twice.
    let mut by_account: BTreeMap<String, Vec<(String, String)>> = BTreeMap::new();
    let mut seen: HashSet<String> = HashSet::new();
    let mut unresolved: Vec<String> = Vec::new();
    for frag in &fragments {
        match resolve(frag) {
            Ok((short, r)) => {
                if seen.insert(short.clone()) {
                    by_account
                        .entry(r.account)
                        .or_default()
                        .push((short, r.graph_id));
                }
            }
            Err(_) => unresolved.push(frag.clone()),
        }
    }

    if by_account.is_empty() {
        return Err(anyhow!(
            "None of the {} fragment(s) resolved to a cached message. \
             Run `pidge mail` (or `mail search`) to refresh the cache.",
            fragments.len()
        ));
    }

    let graph = GraphClient::new(AuthClient::from_env()?)?;
    let mut total = 0usize;
    for (account, items) in &by_account {
        let requests: Vec<BatchRequest> = items
            .iter()
            .map(|(short, gid)| {
                BatchRequest::bare(short.clone(), "DELETE", format!("/me/messages/{gid}"))
            })
            .collect();
        let responses = match graph.batch_all(account, requests).await {
            Ok(r) => r,
            Err(e) => {
                eprintln!("  {} delete failed for {account}: {e}", "!".red());
                continue;
            }
        };
        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 delete {}: HTTP {}",
                    "!".red(),
                    item.id.dimmed(),
                    item.status
                );
            }
        }
        total += ok;
        println!(
            "{} {account}: deleted {ok} message{}",
            "✔".green(),
            if ok == 1 { "" } else { "s" }
        );
    }
    if !unresolved.is_empty() {
        eprintln!(
            "{} {} fragment(s) didn't resolve and were skipped: {}",
            "!".yellow(),
            unresolved.len(),
            unresolved.join(", ").dimmed()
        );
    }
    println!("{} Total deleted: {total}.", "✔".green().bold());
    Ok(())
}

async fn delete_single(fragment: String, yes: bool) -> Result<()> {
    let (short, msg) = resolve(&fragment)?;
    let gate = crate::guardrail::gate(
        crate::guardrail::GuardrailAction::Delete,
        &format!("delete message {short} (moves to Deleted Items)"),
    )?;
    if gate == crate::guardrail::Gate::DryRun {
        return Ok(());
    }
    if !yes {
        let confirmed = Confirm::new(&format!(
            "Delete message {} (moves to Deleted Items)?",
            short.dimmed()
        ))
        .with_default(false)
        .prompt()
        .map_err(|e| anyhow!("prompt cancelled: {e}"))?;
        if !confirmed {
            println!("Aborted.");
            return Ok(());
        }
    }
    let graph = GraphClient::new(AuthClient::from_env()?)?;
    graph
        .delete_message(&msg.account, &msg.graph_id)
        .await
        .context("Microsoft Graph rejected DELETE")?;
    let _ = purge_from_cache(&short);
    println!("{} Deleted {}.", "✔".green(), short.dimmed());
    Ok(())
}

async fn delete_bulk(
    from: Vec<String>,
    older_than: Option<String>,
    account_filter: Vec<String>,
    yes: bool,
) -> Result<()> {
    let gate = crate::guardrail::gate(
        crate::guardrail::GuardrailAction::Bulk,
        &format!("bulk delete: from={from:?} older_than={older_than:?}"),
    )?;
    if gate == crate::guardrail::Gate::DryRun {
        return Ok(());
    }

    if !yes {
        return Err(anyhow!(
            "Bulk delete 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 scope = if from_set.is_empty() {
        "Inbox"
    } else {
        "mailbox"
    };
    let filter_desc = describe_filter(&from_set, cutoff.as_ref(), older_than.as_deref());
    println!(
        "{} Deleting {scope} messages where {}…",
        "Bulk".yellow().bold(),
        filter_desc
    );

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

    let mut total_deleted = 0usize;
    for email in &target_emails {
        let count = if from_set.is_empty() {
            // Date-only mode: same shape as before — walk Inbox newest-first
            // and stop once we cross the cutoff.
            delete_bulk_for_account(
                &graph,
                email,
                cutoff.expect("date-only mode implies cutoff is Some"),
                PAGE_SIZE,
                MAX_PAGES,
            )
            .await?
        } else {
            // Sender-filter mode: search the whole mailbox per sender so
            // we sweep across all folders (Inbox + Junk + Archive + …).
            delete_bulk_by_sender_for_account(&graph, email, &from_set, cutoff).await?
        };
        total_deleted += count;
        println!(
            "{} {}: removed {count} message{}",
            "✔".green(),
            email,
            if count == 1 { "" } else { "s" }
        );
    }
    println!("{} Total: {total_deleted}.", "✔".green().bold());
    Ok(())
}

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 ")
}

/// Sender-filter bulk: one Graph search per sender across the whole
/// mailbox, then delete each match. Mirrors the corresponding archive
/// helper — see `mail_actions::archive_bulk_by_sender_for_account` for
/// the rationale (in short: marketing often auto-routes to Junk, so an
/// Inbox-only scan would miss it).
async fn delete_bulk_by_sender_for_account(
    graph: &GraphClient,
    account: &str,
    from_set: &HashSet<String>,
    cutoff: Option<DateTime<Utc>>,
) -> Result<usize> {
    const SEARCH_LIMIT: usize = 1000;
    let mut deleted = 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| {
                m.from.address.to_ascii_lowercase() == *sender
                    && cutoff.is_none_or(|c| m.received_at < c)
            })
            .collect();
        deleted += delete_messages(graph, account, &matches).await;
    }
    Ok(deleted)
}

/// Concurrently delete a batch of messages with the same throttling
/// safeguards as `move_to_archive`: 4 inflight, retry on HTTP 429.
async fn delete_messages(
    graph: &GraphClient,
    account: &str,
    messages: &[&pidge_core::Message],
) -> usize {
    use pidge_client::graph::batch::BatchRequest;
    let requests: Vec<BatchRequest> = messages
        .iter()
        .map(|m| {
            BatchRequest::bare(
                pidge_core::short_hash(&m.id),
                "DELETE",
                format!("/me/messages/{}", m.id),
            )
        })
        .collect();
    let responses = match graph.batch_all(account, requests).await {
        Ok(r) => r,
        Err(e) => {
            eprintln!("  {} bulk delete 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 delete {}: HTTP {}",
                "!".red(),
                item.id.dimmed(),
                item.status
            );
        }
    }
    ok
}

async fn delete_bulk_for_account(
    graph: &GraphClient,
    account: &str,
    cutoff: DateTime<Utc>,
    page_size: usize,
    max_pages: usize,
) -> Result<usize> {
    let mut deleted = 0usize;
    for page in 0..max_pages {
        let skip = page * page_size;
        let result = graph
            .list_inbox(account, page_size, skip, false)
            .await
            .context("listing inbox for bulk delete")?;
        if result.messages.is_empty() {
            break;
        }
        // Filter to messages strictly older than cutoff.
        let to_delete: Vec<&pidge_core::Message> = result
            .messages
            .iter()
            .filter(|m| m.received_at < cutoff)
            .collect();

        // Delete in parallel within a small concurrency window — keeps Graph
        // happy and the user feedback timely.
        let futures = to_delete.iter().map(|m| {
            let id = m.graph_id_alias();
            async move { graph.delete_message(account, &id).await }
        });
        for (i, result) in join_all(futures).await.into_iter().enumerate() {
            match result {
                Ok(()) => {
                    deleted += 1;
                    let _ = purge_from_cache(&pidge_core::short_hash(&to_delete[i].id));
                }
                Err(ClientError::Graph { status: 404, .. }) => {
                    // already gone — ignore
                }
                Err(e) => {
                    eprintln!(
                        "  {} failed to delete {}: {e}",
                        "!".red(),
                        pidge_core::short_hash(&to_delete[i].id).dimmed()
                    );
                }
            }
        }

        // Stop paging if the OLDEST item on this page is still newer than
        // the cutoff — everything beyond is even newer.
        let oldest = result
            .messages
            .iter()
            .map(|m| m.received_at)
            .min()
            .unwrap_or(cutoff);
        if oldest >= cutoff {
            break;
        }
        if !result.has_more {
            break;
        }
    }
    Ok(deleted)
}

/// Resolve a `--older-than` argument to an absolute UTC cutoff.
///
/// Accepts:
/// - `YYYY-MM-DD` — interpreted as midnight UTC on that date
/// - `Nd` / `Nw` / `Nm` / `Ny` — N days/weeks/months/years before now
pub fn parse_older_than(spec: &str) -> Result<DateTime<Utc>> {
    let trimmed = spec.trim();
    // Absolute date?
    if let Ok(date) = NaiveDate::parse_from_str(trimmed, "%Y-%m-%d") {
        let dt = date
            .and_hms_opt(0, 0, 0)
            .ok_or_else(|| anyhow!("invalid date"))?;
        return Ok(DateTime::from_naive_utc_and_offset(dt, Utc));
    }
    // Relative duration?
    if let Some(unit_pos) = trimmed.find(|c: char| !c.is_ascii_digit())
        && unit_pos > 0
    {
        let n: i64 = trimmed[..unit_pos]
            .parse()
            .map_err(|_| anyhow!("'{spec}' is not a valid duration"))?;
        let unit = &trimmed[unit_pos..];
        let now = Utc::now();
        let cutoff = match unit {
            "d" => now - Duration::days(n),
            "w" => now - Duration::weeks(n),
            "m" => subtract_months(now, n)?,
            "y" => subtract_months(now, n * 12)?,
            other => {
                return Err(anyhow!(
                    "unknown duration unit '{other}' in '{spec}'. Use d/w/m/y."
                ));
            }
        };
        return Ok(cutoff);
    }
    Err(anyhow!(
        "'{spec}' is not a date (YYYY-MM-DD) or duration (e.g. 30d, 6m, 1y)."
    ))
}

/// Subtract N months from a UTC datetime, clamping the day to the new month's
/// length (so 2026-03-31 minus 1 month is 2026-02-28, not invalid).
fn subtract_months(dt: DateTime<Utc>, months: i64) -> Result<DateTime<Utc>> {
    let total = dt.year() as i64 * 12 + (dt.month() as i64 - 1) - months;
    let new_year = (total / 12) as i32;
    let new_month = (total % 12 + 1) as u32;
    let last_day_of_month = match new_month {
        1 | 3 | 5 | 7 | 8 | 10 | 12 => 31,
        4 | 6 | 9 | 11 => 30,
        2 => {
            // crude leap-year check; close enough for "30 days ago" bucket
            if new_year % 4 == 0 && (new_year % 100 != 0 || new_year % 400 == 0) {
                29
            } else {
                28
            }
        }
        _ => return Err(anyhow!("month overflow")),
    };
    let day = dt.day().min(last_day_of_month);
    let nd = NaiveDate::from_ymd_opt(new_year, new_month, day)
        .ok_or_else(|| anyhow!("date overflow"))?;
    let naive = nd
        .and_hms_opt(dt.hour(), dt.minute(), dt.second())
        .ok_or_else(|| anyhow!("time overflow"))?;
    Ok(DateTime::from_naive_utc_and_offset(naive, Utc))
}

// Thin alias so the call site reads better.
trait MessageExt {
    fn graph_id_alias(&self) -> String;
}
impl MessageExt for pidge_core::Message {
    fn graph_id_alias(&self) -> String {
        self.id.clone()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parse_absolute_date() {
        let d = parse_older_than("2026-01-01").unwrap();
        assert_eq!(d.format("%Y-%m-%d").to_string(), "2026-01-01");
    }

    #[test]
    fn parse_days() {
        let now = Utc::now();
        let d = parse_older_than("30d").unwrap();
        let delta = now - d;
        assert!(delta.num_hours() >= 29 * 24);
        assert!(delta.num_hours() <= 30 * 24 + 1);
    }

    #[test]
    fn parse_months() {
        let now = Utc::now();
        let d = parse_older_than("6m").unwrap();
        let delta = now - d;
        // ~6 months = ~180 days, allow a fairly wide band for varying month lengths
        assert!(delta.num_days() > 150);
        assert!(delta.num_days() < 200);
    }

    #[test]
    fn parse_unknown_unit_rejects() {
        assert!(parse_older_than("30x").is_err());
    }

    #[test]
    fn parse_garbage_rejects() {
        assert!(parse_older_than("yesterday").is_err());
        assert!(parse_older_than("").is_err());
    }

    #[test]
    fn subtract_months_clamps_day() {
        let dt = DateTime::from_naive_utc_and_offset(
            NaiveDate::from_ymd_opt(2026, 3, 31)
                .unwrap()
                .and_hms_opt(12, 0, 0)
                .unwrap(),
            Utc,
        );
        let result = subtract_months(dt, 1).unwrap();
        assert_eq!(result.month(), 2);
        assert_eq!(result.day(), 28); // 2026 is not a leap year
    }
}