pidge 0.4.5

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
//! `pidge drafts ...` dispatcher.

use anyhow::{Context, Result, anyhow};
use colored::Colorize;
use futures::future::join_all;
use inquire::Confirm;

use pidge_client::{AuthClient, ClientError, GraphClient, Outgoing};
use pidge_core::{Config, Message, MessageCache, short_hash};

use crate::cli::{DraftAttachmentCommands, DraftsCommands};
use crate::commands::attachments::upload_files;
use crate::commands::mail_fragment::{purge_from_cache, resolve};

pub async fn run(command: DraftsCommands, json: bool) -> Result<()> {
    match command {
        DraftsCommands::List {
            account,
            limit,
            page,
            compact,
        } => list(account, limit, page, compact, json).await,
        DraftsCommands::Show { fragment } => {
            // Drafts are stored in the same /me/messages namespace as mail
            // messages, so the existing `mail show` rendering works as-is.
            crate::commands::mail_show::run(fragment, false, false, false, json).await
        }
        DraftsCommands::Edit { fragment } => edit(fragment).await,
        DraftsCommands::Send { fragment, yes } => send(fragment, yes).await,
        DraftsCommands::Delete { fragment, yes } => delete(fragment, yes).await,
        DraftsCommands::Attachments { command } => match command {
            DraftAttachmentCommands::List { fragment } => attachments_list(fragment, json).await,
            DraftAttachmentCommands::Add { fragment, path } => {
                attachments_add(fragment, path).await
            }
            DraftAttachmentCommands::Remove { fragment, name } => {
                attachments_remove(fragment, name).await
            }
        },
    }
}

async fn list(
    account_filter: Vec<String>,
    limit: usize,
    page: usize,
    compact: bool,
    json: bool,
) -> Result<()> {
    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 per_account = (limit as f64 * 1.2 / target_emails.len() as f64).ceil() as usize;
    let per_account = per_account.max(10);
    let skip = page.saturating_sub(1) * per_account;

    let graph = GraphClient::new(AuthClient::from_env()?)?;
    let futures = target_emails.iter().map(|email| {
        let graph = &graph;
        let e = email.clone();
        async move {
            let result = graph.list_drafts(&e, per_account, skip).await;
            (e, result)
        }
    });

    let mut messages: Vec<Message> = Vec::new();
    let mut had_success = false;
    for (email, result) in join_all(futures).await {
        match result {
            Ok(p) => {
                had_success = true;
                messages.extend(p.messages);
            }
            Err(ClientError::SessionExpired { email: e }) => {
                eprintln!(
                    "{} {e}: session expired, run `pidge account add`",
                    "WARNING:".yellow().bold()
                );
            }
            Err(e) => eprintln!("{} {email}: {e}", "WARNING:".yellow().bold()),
        }
    }
    if !had_success {
        return Err(anyhow!("All accounts failed."));
    }

    messages.sort_by_key(|m| std::cmp::Reverse(m.received_at));
    messages.truncate(limit);

    // Cache so the user can `pidge drafts show/edit/send/delete <fragment>`
    let mut cache = MessageCache::load()?;
    let pairs: Vec<(String, String)> = messages
        .iter()
        .map(|m| (m.id.clone(), m.account.clone()))
        .collect();
    cache.insert_many(&pairs);
    cache.save()?;

    if json {
        let rows: Vec<serde_json::Value> = messages
            .iter()
            .map(|m| {
                serde_json::json!({
                    "id": short_hash(&m.id),
                    "graph_id": m.id,
                    "account": m.account,
                    "subject": m.subject,
                    "last_modified": m.received_at,
                    "preview": m.preview,
                })
            })
            .collect();
        println!("{}", serde_json::to_string_pretty(&rows)?);
        return Ok(());
    }

    print_drafts_table(&messages, target_emails.len() == 1, compact)
}

fn print_drafts_table(messages: &[Message], hide_account: bool, compact: bool) -> Result<()> {
    use comfy_table::{ContentArrangement, Table};

    if messages.is_empty() {
        println!("No drafts.");
        return Ok(());
    }

    let mut table = Table::new();
    table.load_preset(comfy_table::presets::UTF8_HORIZONTAL_ONLY);
    table.set_content_arrangement(ContentArrangement::Dynamic);

    let mut header = vec!["ID", "ACCOUNT", "SUBJECT", "MODIFIED"];
    if hide_account {
        header.remove(1);
    }
    table.set_header(header);

    for m in messages {
        let subject_cell = if compact || m.preview.is_empty() {
            style_draft_subject(&m.subject)
        } else {
            format!(
                "{}\n{}",
                style_draft_subject(&m.subject),
                crate::output::linkify_text(&m.preview).dimmed()
            )
        };
        let mut cells = vec![
            short_hash(&m.id).dimmed().to_string(),
            m.account.clone(),
            subject_cell,
            relative_time(m.received_at),
        ];
        if hide_account {
            cells.remove(1);
        }
        table.add_row(cells);
    }

    println!("{table}");
    Ok(())
}

fn style_draft_subject(subject: &str) -> String {
    let s = if subject.is_empty() {
        "(no subject)".italic().to_string()
    } else {
        subject.to_string()
    };
    s.bold().bright_yellow().to_string()
}

fn relative_time(then: chrono::DateTime<chrono::Utc>) -> String {
    let now = chrono::Utc::now();
    let delta = now - then;
    if delta.num_minutes() < 60 {
        format!("{}m ago", delta.num_minutes().max(0))
    } else if delta.num_hours() < 24 {
        format!("{}h ago", delta.num_hours())
    } else if delta.num_days() < 7 {
        format!("{}d ago", delta.num_days())
    } else {
        then.with_timezone(&chrono::Local)
            .format("%Y-%m-%d")
            .to_string()
    }
}

// ---- edit -----------------------------------------------------------------

async fn edit(fragment: String) -> Result<()> {
    let (_short, msg) = resolve(&fragment)?;
    let graph = GraphClient::new(AuthClient::from_env()?)?;

    // Fetch the current draft state to pre-fill the TUI compose form.
    let current = graph
        .get_message(&msg.account, &msg.graph_id)
        .await
        .context("failed to fetch draft from Microsoft Graph")?;

    let initial = crate::commands::compose_form::Compose {
        from: msg.account.clone(),
        to: current.to.iter().map(|r| r.address.clone()).collect(),
        cc: current.cc.iter().map(|r| r.address.clone()).collect(),
        bcc: current.bcc.iter().map(|r| r.address.clone()).collect(),
        subject: current.subject.clone(),
        body: current.body_content.clone(),
        attachments: Vec::new(),
    };
    let accounts: Vec<String> = Config::load()?
        .accounts
        .iter()
        .map(|a| a.email.clone())
        .collect();
    let outcome = crate::commands::compose_form::run(
        initial,
        accounts,
        crate::commands::compose_form::Context::EditDraft,
    )?;

    match outcome {
        crate::commands::compose_form::Outcome::Send(c) => {
            patch_draft(&graph, &msg.account, &msg.graph_id, &c).await?;
            if !c.attachments.is_empty() {
                crate::commands::attachments::upload_files(
                    &graph,
                    &msg.account,
                    &msg.graph_id,
                    &c.attachments,
                )
                .await?;
            }
            graph
                .send_draft(&msg.account, &msg.graph_id)
                .await
                .context("Microsoft Graph rejected /send for the draft")?;
            let _ = purge_from_cache(&pidge_core::short_hash(&msg.graph_id));
            println!("{} Sent.", "".green());
        }
        crate::commands::compose_form::Outcome::Draft(c) => {
            patch_draft(&graph, &msg.account, &msg.graph_id, &c).await?;
            if !c.attachments.is_empty() {
                crate::commands::attachments::upload_files(
                    &graph,
                    &msg.account,
                    &msg.graph_id,
                    &c.attachments,
                )
                .await?;
            }
            println!("{} Saved.", "".green());
        }
        crate::commands::compose_form::Outcome::Cancel => {
            println!("Aborted.");
        }
    }
    Ok(())
}

async fn patch_draft(
    graph: &GraphClient,
    account: &str,
    message_id: &str,
    c: &crate::commands::compose_form::Compose,
) -> Result<()> {
    let outgoing = Outgoing {
        subject: c.subject.clone(),
        body_text: c.body.clone(),
        to: c.to.clone(),
        cc: c.cc.clone(),
        bcc: c.bcc.clone(),
    };
    graph
        .update_draft(account, message_id, &outgoing)
        .await
        .context("Microsoft Graph rejected the update")
}

// ---- send -----------------------------------------------------------------

async fn send(fragment: String, yes: bool) -> Result<()> {
    let (short, msg) = resolve(&fragment)?;
    if !yes {
        let confirmed = Confirm::new(&format!("Send draft {}?", 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
        .send_draft(&msg.account, &msg.graph_id)
        .await
        .context("Microsoft Graph rejected /send")?;
    // After send, the draft is gone — purge from cache.
    let _ = purge_from_cache(&short);
    println!("{} Sent.", "".green());
    Ok(())
}

// ---- delete ---------------------------------------------------------------

async fn delete(fragment: String, yes: bool) -> Result<()> {
    let (short, msg) = resolve(&fragment)?;
    if !yes {
        let confirmed = Confirm::new(&format!("Delete draft {}?", 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());
    Ok(())
}

// ---- attachments ----------------------------------------------------------

async fn attachments_list(fragment: String, json: bool) -> Result<()> {
    use comfy_table::{ContentArrangement, Table};
    use humansize::{DECIMAL, format_size};

    let (short, msg) = resolve(&fragment)?;
    let graph = GraphClient::new(AuthClient::from_env()?)?;
    let attachments = graph
        .list_attachments(&msg.account, &msg.graph_id)
        .await
        .context("failed to list attachments")?;

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

    if attachments.is_empty() {
        println!("Draft {} has no attachments.", short.dimmed());
        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", "TYPE", "SIZE"]);
    for a in &attachments {
        table.add_row(vec![
            a.name.clone(),
            a.content_type.clone(),
            format_size(a.size_bytes, DECIMAL),
        ]);
    }
    println!("{table}");
    Ok(())
}

async fn attachments_add(fragment: String, path: std::path::PathBuf) -> Result<()> {
    let (_, msg) = resolve(&fragment)?;
    let graph = GraphClient::new(AuthClient::from_env()?)?;
    upload_files(&graph, &msg.account, &msg.graph_id, &[path]).await?;
    Ok(())
}

async fn attachments_remove(fragment: String, name: String) -> Result<()> {
    let (_, msg) = resolve(&fragment)?;
    let graph = GraphClient::new(AuthClient::from_env()?)?;
    let attachments = graph
        .list_attachments(&msg.account, &msg.graph_id)
        .await
        .context("failed to list attachments before remove")?;

    let lower = name.to_lowercase();
    let matches: Vec<&pidge_core::Attachment> = attachments
        .iter()
        .filter(|a| a.name.to_lowercase() == lower)
        .collect();

    match matches.as_slice() {
        [] => Err(anyhow!(
            "No attachment named '{name}' on this draft. Run `pidge drafts attachments list <fragment>` to see what's there."
        )),
        [a] => {
            graph
                .delete_attachment(&msg.account, &msg.graph_id, &a.id)
                .await
                .context("Microsoft Graph rejected DELETE attachment")?;
            println!("{} Removed {}.", "".green(), a.name);
            Ok(())
        }
        many => Err(anyhow!(
            "Multiple attachments named '{}' on this draft ({}). Remove by Graph ID instead — \
             not yet supported in v0.3; remove via Outlook for now.",
            name,
            many.len()
        )),
    }
}