pidge 0.4.1

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
//! `pidge mail new` / `mail reply` / `mail reply-all` / `mail forward`.
//!
//! Two paths through each command:
//!
//! 1. **Wizard** (default) — `inquire` prompts walk the user through To, Cc,
//!    Bcc, Subject, and body, then show a summary and a final yes/no Confirm.
//! 2. **Power-user / scripting** — pass every value as a flag; with `-y` the
//!    confirmation is skipped too.
//!
//! Bodies are plain text (HTML composition is out of scope until drafts in
//! Phase 4). Reply and forward send a "comment" that Graph prepends to its
//! auto-generated quote of the original — i.e. we never compose the quoted
//! text ourselves; Graph owns that.
//!
//! Safety:
//! - We always show a summary before sending; `-y` is the explicit opt-out.
//! - `--to` values are validated to look like e-mail addresses (contain `@`).
//! - Body and `--body-file` are mutually exclusive (clap enforces).

use std::io::Read;

use anyhow::{Context, Result, anyhow};
use colored::Colorize;
use inquire::{Confirm, Editor, Text};

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

use crate::cli::{ComposeArgs, ForwardArgs, ReplyArgs};
use crate::commands::attachments::upload_files;
use crate::commands::compose_form;
use crate::commands::mail_fragment::resolve;

// ---- mail new -------------------------------------------------------------

pub async fn send(args: ComposeArgs) -> 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 from_default = resolve_from_account(&config, args.from.as_deref())?;

    // Non-interactive scripting path: if every required field is on the
    // command line and `--confirm` wasn't requested, send straight through
    // without launching the TUI. This is the common path for scripts and
    // one-liners — the user already spelled out the whole message, no
    // need for a review step they didn't ask for.
    let fully_specified = !args.to.is_empty()
        && args.subject.is_some()
        && (args.body.is_some() || args.body_file.is_some());
    if fully_specified && !args.confirm {
        let body = resolve_body(args.body.clone(), args.body_file.clone(), "Body", false)?;
        let to = parse_addrs(&args.to)?;
        let cc = parse_addrs(&args.cc)?;
        let bcc = parse_addrs(&args.bcc)?;
        return send_or_draft(
            &from_default,
            compose_form::Compose {
                from: from_default.clone(),
                to,
                cc,
                bcc,
                subject: args.subject.unwrap_or_default(),
                body,
                attachments: args.attach,
            },
            args.draft,
        )
        .await;
    }

    // Interactive: launch the TUI form pre-filled with any flags the user
    // did pass. The form handles validation, attachments, and the final
    // send/draft/cancel decision; we just route the outcome.
    let initial = compose_form::Compose {
        from: from_default.clone(),
        to: args.to,
        cc: args.cc,
        bcc: args.bcc,
        subject: args.subject.unwrap_or_default(),
        body: resolve_body_or_empty(args.body, args.body_file)?,
        attachments: args.attach,
    };
    let accounts: Vec<String> = config.accounts.iter().map(|a| a.email.clone()).collect();
    let outcome = compose_form::run(initial, accounts, compose_form::Context::NewSend)?;
    match outcome {
        compose_form::Outcome::Send(c) => send_or_draft(&c.from.clone(), c, false).await,
        compose_form::Outcome::Draft(c) => send_or_draft(&c.from.clone(), c, true).await,
        compose_form::Outcome::Cancel => {
            println!("Aborted.");
            Ok(())
        }
    }
}

/// Push a fully-specified compose through Graph — either send-and-go or
/// create-draft (then either send or stop). Shared by the non-interactive
/// path and the TUI's `Send` / `Draft` outcomes.
async fn send_or_draft(from: &str, c: compose_form::Compose, save_as_draft: bool) -> Result<()> {
    let graph = GraphClient::new(AuthClient::from_env()?)?;
    let outgoing = Outgoing {
        subject: c.subject,
        body_text: c.body,
        to: c.to,
        cc: c.cc,
        bcc: c.bcc,
    };

    // Same rule as before: attachments force the draft-then-send pathway
    // because Graph's /sendMail can't carry uploads in one call.
    let need_draft = save_as_draft || !c.attachments.is_empty();
    if !need_draft {
        graph
            .send_mail(from, &outgoing)
            .await
            .context("Microsoft Graph rejected the message")?;
        println!("{} Sent.", "".green());
        return Ok(());
    }

    let id = graph
        .create_draft(from, &outgoing)
        .await
        .context("Microsoft Graph rejected the draft")?;
    if !c.attachments.is_empty() {
        println!();
        println!("{}", "Uploading attachments:".bold());
        upload_files(&graph, from, &id, &c.attachments).await?;
    }
    if save_as_draft {
        cache_and_report_draft(from, &id, "Saved draft")?;
    } else {
        graph
            .send_draft(from, &id)
            .await
            .context("Microsoft Graph rejected /send for the draft")?;
        println!("{} Sent.", "".green());
    }
    Ok(())
}

fn parse_addrs(raw: &[String]) -> Result<Vec<String>> {
    let joined = raw.join(",");
    compose_form::parse_addresses(&joined)
}

/// `--body`/`--body-file` flag resolution that returns "" if neither was set,
/// instead of dropping to the interactive editor (the TUI form will handle
/// the body field itself).
fn resolve_body_or_empty(inline: Option<String>, body_file: Option<String>) -> Result<String> {
    if let Some(b) = inline {
        return Ok(b);
    }
    if let Some(path) = body_file {
        if path == "-" {
            let mut buf = String::new();
            std::io::stdin()
                .read_to_string(&mut buf)
                .context("failed to read body from stdin")?;
            return Ok(buf);
        }
        return std::fs::read_to_string(&path)
            .with_context(|| format!("failed to read body from {path}"));
    }
    Ok(String::new())
}

/// Insert a freshly-created draft into the message cache (so future fragment
/// lookups find it) and tell the user its short hash.
fn cache_and_report_draft(account: &str, message_id: &str, action: &str) -> Result<()> {
    let mut cache = MessageCache::load()?;
    cache.insert_many(&[(message_id.to_string(), account.to_string())]);
    cache.save()?;
    let hash = short_hash(message_id);
    println!(
        "{} {}. Use `pidge drafts edit {}` or `pidge drafts send {}`.",
        "".green(),
        action,
        hash,
        hash,
    );
    Ok(())
}

// ---- mail reply / reply-all ----------------------------------------------

pub async fn reply(fragment: String, args: ReplyArgs, reply_all: bool) -> Result<()> {
    let (short, msg) = resolve(&fragment)?;
    let from = args.from.clone().unwrap_or(msg.account.clone());
    let body = resolve_body(args.body, args.body_file, "Comment", true)?;

    println!();
    println!(
        "{} {} (id {})",
        if reply_all {
            "Reply-all to"
        } else {
            "Reply to"
        }
        .bold(),
        short.dimmed(),
        msg.account.dimmed(),
    );
    println!("{} {}", "From:".bold(), from);
    if body.trim().is_empty() {
        println!("{} (none — only Graph's auto-quote)", "Comment:".bold());
    } else {
        let preview: String = body.lines().take(3).collect::<Vec<_>>().join("\n");
        println!("{}\n{}", "Comment:".bold(), preview);
    }
    println!();

    let prompt = if args.draft {
        "Save as draft?"
    } else {
        "Send this reply?"
    };
    if !confirm_send(args.yes, prompt)? {
        return Ok(());
    }

    let graph = GraphClient::new(AuthClient::from_env()?)?;

    // Attachments → must use createReply/createReplyAll then upload then send,
    // because Graph's /reply takes a comment string only, not attachments.
    let need_draft = args.draft || !args.attach.is_empty();

    if !need_draft {
        if reply_all {
            graph
                .reply_all_message(&from, &msg.graph_id, &body)
                .await
                .context("Microsoft Graph rejected the reply")?;
        } else {
            graph
                .reply_message(&from, &msg.graph_id, &body)
                .await
                .context("Microsoft Graph rejected the reply")?;
        }
        println!("{} Sent.", "".green());
        return Ok(());
    }

    let id = if reply_all {
        graph
            .create_reply_all_draft(&from, &msg.graph_id, &body)
            .await
    } else {
        graph.create_reply_draft(&from, &msg.graph_id, &body).await
    }
    .context("Microsoft Graph rejected the draft")?;
    if !args.attach.is_empty() {
        println!();
        println!("{}", "Uploading attachments:".bold());
        upload_files(&graph, &from, &id, &args.attach).await?;
    }
    if args.draft {
        let label = if reply_all {
            "Saved reply-all draft"
        } else {
            "Saved reply draft"
        };
        cache_and_report_draft(&from, &id, label)?;
    } else {
        graph
            .send_draft(&from, &id)
            .await
            .context("Microsoft Graph rejected /send for the draft")?;
        println!("{} Sent.", "".green());
    }
    Ok(())
}

// ---- mail forward ---------------------------------------------------------

pub async fn forward(fragment: String, args: ForwardArgs) -> Result<()> {
    let (short, msg) = resolve(&fragment)?;
    let from = args.from.clone().unwrap_or(msg.account.clone());
    let to = collect_addresses("Forward to", args.to, true)?;
    let body = resolve_body(args.body, args.body_file, "Comment", true)?;

    println!();
    println!(
        "{} {} (id {})",
        "Forward".bold(),
        short.dimmed(),
        msg.account.dimmed()
    );
    println!("{} {}", "From:".bold(), from);
    println!("{} {}", "To:".bold(), to.join(", "));
    if body.trim().is_empty() {
        println!("{} (none — only Graph's auto-quote)", "Comment:".bold());
    } else {
        let preview: String = body.lines().take(3).collect::<Vec<_>>().join("\n");
        println!("{}\n{}", "Comment:".bold(), preview);
    }
    println!();

    let prompt = if args.draft {
        "Save as draft?"
    } else {
        "Send this forward?"
    };
    if !confirm_send(args.yes, prompt)? {
        return Ok(());
    }

    let graph = GraphClient::new(AuthClient::from_env()?)?;
    let need_draft = args.draft || !args.attach.is_empty();

    if !need_draft {
        graph
            .forward_message(&from, &msg.graph_id, &to, &body)
            .await
            .context("Microsoft Graph rejected the forward")?;
        println!("{} Forwarded.", "".green());
        return Ok(());
    }

    let id = graph
        .create_forward_draft(&from, &msg.graph_id, &to, &body)
        .await
        .context("Microsoft Graph rejected the draft")?;
    if !args.attach.is_empty() {
        println!();
        println!("{}", "Uploading attachments:".bold());
        upload_files(&graph, &from, &id, &args.attach).await?;
    }
    if args.draft {
        cache_and_report_draft(&from, &id, "Saved forward draft")?;
    } else {
        graph
            .send_draft(&from, &id)
            .await
            .context("Microsoft Graph rejected /send for the draft")?;
        println!("{} Forwarded.", "".green());
    }
    Ok(())
}

// ---- shared helpers -------------------------------------------------------

fn resolve_from_account(config: &Config, requested: Option<&str>) -> Result<String> {
    if let Some(req) = requested {
        if config.find(req).is_none() {
            return Err(anyhow!("not signed in to {req}"));
        }
        return Ok(req.to_string());
    }
    if let Some(d) = &config.defaults.send {
        return Ok(d.clone());
    }
    if config.accounts.len() == 1 {
        return Ok(config.accounts[0].email.clone());
    }
    Err(anyhow!(
        "No default e-mail account set. Pass `--from <email>` or run \
         `pidge account default e-mail <email>` first."
    ))
}

/// Collect recipients. If `provided` is non-empty, validate and return it.
/// Otherwise prompt interactively; `required` controls whether an empty
/// answer is accepted.
fn collect_addresses(label: &str, provided: Vec<String>, required: bool) -> Result<Vec<String>> {
    let raw = if provided.is_empty() {
        let help = if required {
            "Comma-separated e-mail addresses"
        } else {
            "Comma-separated e-mail addresses. Optional — leave empty to skip"
        };
        Text::new(&format!("{label}:"))
            .with_help_message(help)
            .prompt()
            .map_err(|e| anyhow!("input cancelled: {e}"))?
    } else {
        provided.join(",")
    };

    let addrs: Vec<String> = raw
        .split([',', ';'])
        .map(|s| s.trim().to_string())
        .filter(|s| !s.is_empty())
        .collect();

    if addrs.is_empty() {
        if required {
            return Err(anyhow!("at least one recipient is required for {label}"));
        }
        return Ok(Vec::new());
    }

    for a in &addrs {
        if !a.contains('@') {
            return Err(anyhow!(
                "'{a}' doesn't look like an e-mail address (no '@'). Aborting."
            ));
        }
    }
    Ok(addrs)
}

/// Resolve the body text. Precedence (highest first):
/// 1. `--body "text"`
/// 2. `--body-file path` (or `-` for stdin)
/// 3. Interactive — open the in-pidge multi-line editor; user can press
///    Ctrl-X from inside it to hand off to `$EDITOR` (nano, vim, code …).
fn resolve_body(
    inline: Option<String>,
    body_file: Option<String>,
    label: &str,
    _optional: bool,
) -> Result<String> {
    if let Some(b) = inline {
        return Ok(b);
    }
    if let Some(path) = body_file {
        if path == "-" {
            let mut buf = String::new();
            std::io::stdin()
                .read_to_string(&mut buf)
                .context("failed to read body from stdin")?;
            return Ok(buf);
        }
        return std::fs::read_to_string(&path)
            .with_context(|| format!("failed to read body from {path}"));
    }
    interactive_body(label, "")
}

/// Open the user's `$EDITOR` (or system default) for multi-line text input.
/// Used by the reply / forward flows for the comment field; the new-send
/// and draft-edit flows use the full TUI compose form instead.
pub(crate) fn interactive_body(label: &str, initial: &str) -> Result<String> {
    let label = format!("{label}:");
    let mut e = Editor::new(&label);
    if !initial.is_empty() {
        e = e.with_predefined_text(initial);
    }
    e.with_help_message("Opens $EDITOR (set the $EDITOR env var to change the editor)")
        .prompt()
        .map_err(|e| anyhow!("editor cancelled: {e}"))
}

fn confirm_send(skip: bool, prompt: &str) -> Result<bool> {
    if skip {
        return Ok(true);
    }
    let yes = Confirm::new(prompt)
        .with_default(false)
        .prompt()
        .map_err(|e| anyhow!("prompt cancelled: {e}"))?;
    if !yes {
        println!("Aborted.");
    }
    Ok(yes)
}