pidge 0.4.8

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
//! `pidge mail attachments {list,save}` — list and download a received
//! message's file attachments.
//!
//! The Graph client already exposes `list_attachments` (metadata) and
//! `get_attachment_bytes` (decoded content); this command resolves a message
//! fragment, picks which attachments to write, and saves them to disk. The
//! selection and destination logic is factored into pure helpers so it can be
//! unit-tested without a live mailbox.

use std::path::{Path, PathBuf};

use anyhow::{Context, Result, anyhow};
use colored::Colorize;
use comfy_table::{ContentArrangement, Table};
use humansize::{DECIMAL, format_size};

use pidge_client::{AuthClient, GraphClient};
use pidge_core::Attachment;

use crate::cli::MailAttachmentCommands;
use crate::commands::mail_fragment;

pub async fn run(command: MailAttachmentCommands, json: bool) -> Result<()> {
    match command {
        MailAttachmentCommands::List {
            fragment,
            include_inline,
        } => list(fragment, include_inline, json).await,
        MailAttachmentCommands::Save {
            fragment,
            name,
            out,
            include_inline,
            force,
        } => save(fragment, name, out, include_inline, force).await,
    }
}

async fn list(fragment: String, include_inline: bool, json: bool) -> Result<()> {
    let (_, message_ref) = mail_fragment::resolve(&fragment)?;
    let graph = GraphClient::new(AuthClient::from_env()?)?;
    let attachments = graph
        .list_attachments(&message_ref.account, &message_ref.graph_id)
        .await?;
    let shown: Vec<&Attachment> = attachments
        .iter()
        .filter(|a| include_inline || !a.is_inline)
        .collect();

    if json {
        println!("{}", serde_json::to_string_pretty(&shown)?);
        return Ok(());
    }

    if shown.is_empty() {
        println!("No attachments.");
        return Ok(());
    }

    let mut table = Table::new();
    table.load_preset(comfy_table::presets::UTF8_HORIZONTAL_ONLY);
    table.set_content_arrangement(ContentArrangement::Dynamic);
    table.set_header(vec!["NAME", "SIZE", "TYPE"]);
    for a in shown {
        let name = if a.is_inline {
            format!("{} {}", a.name, "(inline)".dimmed())
        } else {
            a.name.clone()
        };
        table.add_row(vec![
            name,
            format_size(a.size_bytes, DECIMAL),
            a.content_type.clone(),
        ]);
    }
    println!("{table}");
    Ok(())
}

async fn save(
    fragment: String,
    name: Option<String>,
    out: Option<PathBuf>,
    include_inline: bool,
    force: bool,
) -> Result<()> {
    let (_, message_ref) = mail_fragment::resolve(&fragment)?;
    let graph = GraphClient::new(AuthClient::from_env()?)?;
    let attachments = graph
        .list_attachments(&message_ref.account, &message_ref.graph_id)
        .await?;

    let selected = select_attachments(&attachments, name.as_deref(), include_inline)?;

    let out_is_existing_dir = out.as_deref().map(Path::is_dir).unwrap_or(false);
    let dest = resolve_destination(
        out,
        default_download_dir(),
        out_is_existing_dir,
        selected.len(),
    )?;

    for att in &selected {
        let target = final_path(&dest, &att.name);
        guard_overwrite(&target, target.exists(), force)?;
        if let Some(parent) = target.parent() {
            std::fs::create_dir_all(parent)
                .with_context(|| format!("creating {}", parent.display()))?;
        }
        let bytes = graph
            .get_attachment_bytes(&message_ref.account, &message_ref.graph_id, &att.id)
            .await
            .with_context(|| format!("downloading {}", att.name))?;
        std::fs::write(&target, &bytes).with_context(|| format!("writing {}", target.display()))?;
        println!(
            "  {} {}{} ({})",
            "+".green(),
            att.name,
            target.display(),
            format_size(bytes.len() as u64, DECIMAL),
        );
    }
    Ok(())
}

/// The directory `save` writes into when `--out` is omitted: the user's
/// Downloads folder, falling back to home, then the current directory.
fn default_download_dir() -> PathBuf {
    dirs::download_dir()
        .or_else(dirs::home_dir)
        .unwrap_or_else(|| PathBuf::from("."))
}

/// Pick which attachments to act on.
///
/// Inline attachments (embedded images, signatures) are excluded unless
/// `include_inline` is set. When `name` is given it is matched case-insensitively
/// as a substring of the filename: zero matches and (to force an explicit
/// choice) more than one match are both errors. With no `name`, every attachment
/// in the pool is selected.
fn select_attachments(
    attachments: &[Attachment],
    name: Option<&str>,
    include_inline: bool,
) -> Result<Vec<Attachment>> {
    let pool: Vec<Attachment> = attachments
        .iter()
        .filter(|a| include_inline || !a.is_inline)
        .cloned()
        .collect();

    let Some(name) = name else {
        if pool.is_empty() {
            return Err(anyhow!("Message has no downloadable attachments."));
        }
        return Ok(pool);
    };

    let needle = name.to_lowercase();
    let matches: Vec<Attachment> = pool
        .iter()
        .filter(|a| a.name.to_lowercase().contains(&needle))
        .cloned()
        .collect();

    match matches.len() {
        0 => Err(anyhow!(
            "No attachment matches '{name}'. Available: {}.",
            attachment_names(&pool)
        )),
        1 => Ok(matches),
        _ => Err(anyhow!(
            "'{name}' matches several attachments: {}. Narrow the name.",
            attachment_names(&matches)
        )),
    }
}

/// Comma-joined attachment filenames, for error messages.
fn attachment_names(atts: &[Attachment]) -> String {
    atts.iter()
        .map(|a| a.name.as_str())
        .collect::<Vec<_>>()
        .join(", ")
}

/// Where saved files should land.
#[derive(Debug, PartialEq, Eq)]
enum Destination {
    /// Write each attachment into this directory under its own filename.
    IntoDir(PathBuf),
    /// Write the single selected attachment to exactly this file path.
    AsFile(PathBuf),
}

/// Resolve the `--out` argument into a [`Destination`].
///
/// `None` → the default directory. A path that is an existing directory, or
/// that ends in the platform separator, is treated as a directory. Otherwise it
/// is a target file path — which is only valid when a single attachment is
/// selected; selecting several would collapse them onto one filename, so that
/// is rejected.
fn resolve_destination(
    out: Option<PathBuf>,
    default_dir: PathBuf,
    out_is_existing_dir: bool,
    selected_count: usize,
) -> Result<Destination> {
    let Some(path) = out else {
        return Ok(Destination::IntoDir(default_dir));
    };

    if out_is_existing_dir || has_trailing_separator(&path) {
        return Ok(Destination::IntoDir(path));
    }

    if selected_count > 1 {
        return Err(anyhow!(
            "--out is a file path but {selected_count} attachments are selected. \
             Pass a directory, or narrow to one attachment by name."
        ));
    }

    Ok(Destination::AsFile(path))
}

/// True if the path's text ends with the platform path separator (a user's way
/// of saying "this is a directory" even when it doesn't exist yet).
fn has_trailing_separator(path: &Path) -> bool {
    path.as_os_str()
        .to_string_lossy()
        .ends_with(std::path::MAIN_SEPARATOR)
}

/// Compute the on-disk path for one attachment given the resolved destination.
/// For a directory destination the attachment's filename is sanitized to its
/// final path component, guarding against a hostile name like `../../etc/x`.
fn final_path(dest: &Destination, original_name: &str) -> PathBuf {
    match dest {
        Destination::AsFile(path) => path.clone(),
        Destination::IntoDir(dir) => {
            let leaf = Path::new(original_name)
                .file_name()
                .map(PathBuf::from)
                .unwrap_or_else(|| PathBuf::from("attachment"));
            dir.join(leaf)
        }
    }
}

/// Reject writing over an existing file unless the user passed `--force`.
/// Existence is checked by the caller so this stays pure and testable.
fn guard_overwrite(path: &Path, exists: bool, force: bool) -> Result<()> {
    if exists && !force {
        return Err(anyhow!(
            "{} already exists. Pass --force to overwrite.",
            path.display()
        ));
    }
    Ok(())
}

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

    fn att(name: &str, inline: bool) -> Attachment {
        Attachment {
            id: format!("id-{name}"),
            name: name.to_string(),
            content_type: "application/octet-stream".to_string(),
            size_bytes: 10,
            is_inline: inline,
            content_id: None,
        }
    }

    #[test]
    fn no_name_excludes_inline_by_default() {
        let atts = vec![att("report.pdf", false), att("logo.png", true)];
        let picked = select_attachments(&atts, None, false).unwrap();
        let names: Vec<_> = picked.iter().map(|a| a.name.as_str()).collect();
        assert_eq!(names, vec!["report.pdf"]);
    }

    #[test]
    fn no_name_with_include_inline_keeps_inline() {
        let atts = vec![att("report.pdf", false), att("logo.png", true)];
        let picked = select_attachments(&atts, None, true).unwrap();
        assert_eq!(picked.len(), 2);
    }

    #[test]
    fn name_substring_matches_case_insensitively() {
        let atts = vec![att("Quarterly-Report.pdf", false), att("photo.jpg", false)];
        let picked = select_attachments(&atts, Some("report"), false).unwrap();
        let names: Vec<_> = picked.iter().map(|a| a.name.as_str()).collect();
        assert_eq!(names, vec!["Quarterly-Report.pdf"]);
    }

    #[test]
    fn name_matching_nothing_is_an_error() {
        let atts = vec![att("report.pdf", false)];
        let err = select_attachments(&atts, Some("invoice"), false).unwrap_err();
        assert!(err.to_string().contains("invoice"));
        assert!(err.to_string().contains("report.pdf"));
    }

    #[test]
    fn name_matching_several_is_an_error() {
        let atts = vec![att("report-q1.pdf", false), att("report-q2.pdf", false)];
        let err = select_attachments(&atts, Some("report"), false).unwrap_err();
        assert!(err.to_string().contains("report-q1.pdf"));
        assert!(err.to_string().contains("report-q2.pdf"));
    }

    #[test]
    fn empty_pool_is_an_error() {
        let atts = vec![att("logo.png", true)];
        let err = select_attachments(&atts, None, false).unwrap_err();
        assert!(err.to_string().to_lowercase().contains("no"));
    }

    #[test]
    fn no_out_uses_default_dir() {
        let dest =
            resolve_destination(None, PathBuf::from("/home/jane/Downloads"), false, 3).unwrap();
        assert_eq!(
            dest,
            Destination::IntoDir(PathBuf::from("/home/jane/Downloads"))
        );
    }

    #[test]
    fn out_file_path_with_single_selection_is_a_file() {
        let dest = resolve_destination(
            Some(PathBuf::from("/tmp/renamed.pdf")),
            PathBuf::from("/dl"),
            false,
            1,
        )
        .unwrap();
        assert_eq!(dest, Destination::AsFile(PathBuf::from("/tmp/renamed.pdf")));
    }

    #[test]
    fn out_file_path_with_multiple_selection_is_an_error() {
        let err = resolve_destination(
            Some(PathBuf::from("/tmp/renamed.pdf")),
            PathBuf::from("/dl"),
            false,
            2,
        )
        .unwrap_err();
        assert!(err.to_string().contains("2"));
    }

    #[test]
    fn out_existing_dir_is_a_directory() {
        let dest = resolve_destination(
            Some(PathBuf::from("/tmp/some-dir")),
            PathBuf::from("/dl"),
            true,
            2,
        )
        .unwrap();
        assert_eq!(dest, Destination::IntoDir(PathBuf::from("/tmp/some-dir")));
    }

    #[test]
    fn out_trailing_separator_is_a_directory_even_if_missing() {
        let with_sep = format!("/tmp/new-dir{}", std::path::MAIN_SEPARATOR);
        let dest = resolve_destination(
            Some(PathBuf::from(&with_sep)),
            PathBuf::from("/dl"),
            false,
            2,
        )
        .unwrap();
        assert_eq!(dest, Destination::IntoDir(PathBuf::from(&with_sep)));
    }

    #[test]
    fn final_path_into_dir_joins_sanitized_filename() {
        let dest = Destination::IntoDir(PathBuf::from("/dl"));
        assert_eq!(
            final_path(&dest, "report.pdf"),
            PathBuf::from("/dl/report.pdf")
        );
        // A hostile name collapses to its final component.
        assert_eq!(
            final_path(&dest, "../../etc/passwd"),
            PathBuf::from("/dl/passwd")
        );
    }

    #[test]
    fn existing_target_without_force_is_rejected() {
        let err = guard_overwrite(Path::new("/dl/report.pdf"), true, false).unwrap_err();
        assert!(err.to_string().contains("report.pdf"));
        assert!(err.to_string().contains("force"));
    }

    #[test]
    fn existing_target_with_force_is_allowed() {
        assert!(guard_overwrite(Path::new("/dl/report.pdf"), true, true).is_ok());
    }

    #[test]
    fn missing_target_is_allowed() {
        assert!(guard_overwrite(Path::new("/dl/report.pdf"), false, false).is_ok());
    }

    #[test]
    fn final_path_as_file_returns_the_file_path() {
        let dest = Destination::AsFile(PathBuf::from("/tmp/renamed.pdf"));
        assert_eq!(
            final_path(&dest, "original.pdf"),
            PathBuf::from("/tmp/renamed.pdf")
        );
    }
}