kernoid-client 0.2.2

kernoid's protocols for clients: JMAP mail, files, calendars, contacts, push and end-to-end encryption
Documentation
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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
// SPDX-License-Identifier: MIT
//! Mail over JMAP (RFC 8621): mailboxes, the emails of one, one email to read, keywords, moving, deleting,
//! sending through the Drafts mailbox (RFC 8621, 7.5), and the vacation response (RFC 8621, 8). End-to-end encrypted
//! emails are opened and sent in `e2e`.

use std::collections::BTreeMap;

use chrono::{DateTime, Local, NaiveDateTime, TimeZone, Utc};
use serde::Deserialize;
use serde_json::{Map, Value, json};

use crate::{Client, Error, MAIL, Result, SUBMISSION, VACATION, e2e::OPENPGP, ids_of, set_error};

/// The vacation response of the user's account (RFC 8621, 8), its times here.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Vacation {
    pub on: bool,
    /// From when it answers; at once if not set.
    pub from: Option<NaiveDateTime>,
    /// Until when; until it is switched off if not set.
    pub until: Option<NaiveDateTime>,
    pub subject: String,
    pub text: String,
}

fn local(utc: Option<&str>) -> Option<NaiveDateTime> {
    let utc = DateTime::parse_from_rfc3339(utc?).ok()?;
    Some(utc.with_timezone(&Local).naive_local())
}

fn utc(local: Option<NaiveDateTime>) -> Option<String> {
    let at = Local.from_local_datetime(&local?).earliest()?;
    Some(at.with_timezone(&Utc).format("%Y-%m-%dT%H:%M:%SZ").to_string())
}

/// The vacation response; none if the server has none for the user.
pub async fn vacation(client: &Client) -> Result<Option<Vacation>> {
    let Ok(account) = client.primary(VACATION) else { return Ok(None) };
    let result =
        client.call(&[VACATION], "VacationResponse/get", json!({ "accountId": account, "ids": ["singleton"] })).await?;
    let Some(found) = result.get("list").and_then(Value::as_array).and_then(|list| list.first()) else {
        return Ok(Some(Vacation::default()));
    };
    let text = |name: &str| found.get(name).and_then(Value::as_str).unwrap_or_default().to_owned();
    Ok(Some(Vacation {
        on: found.get("isEnabled").and_then(Value::as_bool).unwrap_or(false),
        from: local(found.get("fromDate").and_then(Value::as_str)),
        until: local(found.get("toDate").and_then(Value::as_str)),
        subject: text("subject"),
        text: text("textBody"),
    }))
}

pub async fn set_vacation(client: &Client, vacation: &Vacation) -> Result<()> {
    let account = client.primary(VACATION)?;
    let update = json!({ "update": { "singleton": {
        "isEnabled": vacation.on,
        "fromDate": utc(vacation.from),
        "toDate": utc(vacation.until),
        "subject": (!vacation.subject.is_empty()).then_some(&vacation.subject),
        "textBody": (!vacation.text.is_empty()).then_some(&vacation.text),
    } } });
    client.set(&[VACATION], "VacationResponse", &account, update, json!({})).await?;
    Ok(())
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Mailbox {
    pub id: String,
    pub name: String,
    #[serde(default)]
    pub parent_id: Option<String>,
    #[serde(default)]
    pub role: Option<String>,
    #[serde(default)]
    pub sort_order: i64,
    #[serde(default)]
    pub total_emails: u64,
    #[serde(default)]
    pub unread_emails: u64,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, serde::Serialize)]
pub struct EmailAddress {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    pub email: String,
}

impl std::fmt::Display for EmailAddress {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match &self.name {
            Some(name) if !name.is_empty() => {
                if name.contains([',', ';', '<', '>', '"']) {
                    write!(f, "\"{}\" <{}>", name.replace('"', ""), self.email)
                } else {
                    write!(f, "{name} <{}>", self.email)
                }
            }
            _ => write!(f, "{}", self.email),
        }
    }
}

/// A list of addresses for people: `Name <a@b>, c@d`.
pub fn format_addresses(list: &[EmailAddress]) -> String {
    list.iter().map(ToString::to_string).collect::<Vec<_>>().join(", ")
}

/// Addresses as people type them, separated by commas or semicolons (`Name <a@b>`, `"Last, First" <a@b>`, `a@b`).
pub fn parse_addresses(text: &str) -> Vec<EmailAddress> {
    let mut parts = Vec::new();
    let mut current = String::new();
    let (mut quoted, mut angle) = (false, false);
    for c in text.chars() {
        match c {
            '"' if !angle => quoted = !quoted,
            '<' if !quoted => angle = true,
            '>' if !quoted => angle = false,
            ',' | ';' if !quoted && !angle => {
                parts.push(std::mem::take(&mut current));
                continue;
            }
            _ => {}
        }
        current.push(c);
    }
    parts.push(current);
    parts
        .into_iter()
        .map(|part| part.trim().to_owned())
        .filter(|part| !part.is_empty())
        .map(|part| match (part.rfind('<'), part.ends_with('>')) {
            (Some(start), true) => {
                let name = part[..start].trim().trim_matches('"').trim();
                EmailAddress {
                    name: (!name.is_empty()).then(|| name.to_owned()),
                    email: part[start + 1..part.len() - 1].trim().to_owned(),
                }
            }
            _ => EmailAddress { name: None, email: part },
        })
        .collect()
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct EmailSummary {
    pub id: String,
    #[serde(default)]
    pub thread_id: String,
    #[serde(default)]
    pub mailbox_ids: BTreeMap<String, bool>,
    #[serde(default)]
    pub keywords: BTreeMap<String, bool>,
    #[serde(default)]
    pub from: Option<Vec<EmailAddress>>,
    #[serde(default)]
    pub to: Option<Vec<EmailAddress>>,
    #[serde(default)]
    pub subject: Option<String>,
    #[serde(default)]
    pub received_at: String,
    #[serde(default)]
    pub preview: String,
    #[serde(default)]
    pub has_attachment: bool,
    #[serde(default)]
    pub size: u64,
    /// End-to-end encrypted (RFC 0006, 5): subject and preview are the summary's, once it is decrypted here.
    #[serde(default, rename = "kernoid:encrypted")]
    pub encrypted: bool,
    /// The summary, encrypted to the account's key (base64).
    #[serde(default, rename = "kernoid:encryptedSummary")]
    pub encrypted_summary: Option<String>,
    /// Subject, preview and attachments came from the decrypted summary.
    #[serde(skip)]
    pub opened: bool,
}

impl EmailSummary {
    pub fn is_seen(&self) -> bool {
        self.keywords.get("$seen").copied().unwrap_or(false)
    }

    pub fn is_flagged(&self) -> bool {
        self.keywords.get("$flagged").copied().unwrap_or(false)
    }
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BodyPart {
    #[serde(default)]
    pub part_id: Option<String>,
    #[serde(default)]
    pub blob_id: Option<String>,
    #[serde(rename = "type", default)]
    pub kind: String,
    #[serde(default)]
    pub name: Option<String>,
    #[serde(default)]
    pub size: u64,
    /// A part of an end-to-end encrypted message, decrypted here: its index in the inner message (`Email::inner`).
    #[serde(skip)]
    pub inner: Option<usize>,
}

#[derive(Debug, Clone, Deserialize)]
pub struct BodyValue {
    pub value: String,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Email {
    #[serde(flatten)]
    pub summary: EmailSummary,
    #[serde(default)]
    pub cc: Option<Vec<EmailAddress>>,
    #[serde(default)]
    pub reply_to: Option<Vec<EmailAddress>>,
    #[serde(default)]
    pub message_id: Option<Vec<String>>,
    #[serde(default)]
    pub references: Option<Vec<String>>,
    #[serde(default)]
    pub sent_at: Option<String>,
    #[serde(default)]
    pub text_body: Vec<BodyPart>,
    #[serde(default)]
    pub html_body: Vec<BodyPart>,
    #[serde(default)]
    pub attachments: Vec<BodyPart>,
    #[serde(default)]
    pub body_values: BTreeMap<String, BodyValue>,
    /// The raw message, for end-to-end encrypted emails (downloaded and decrypted here).
    #[serde(default)]
    pub blob_id: Option<String>,
    /// For an end-to-end encrypted email: locked or decrypted, and how it was signed.
    #[serde(skip)]
    pub sealed: Option<Sealed>,
    /// The decrypted message – its attachments are read from it.
    #[serde(skip)]
    pub inner: Option<Inner>,
}

/// What reading an end-to-end encrypted email gave.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Sealed {
    /// Not decrypted: the key is locked, or the email is encrypted to another key.
    pub locked: bool,
    pub signed: bool,
    /// Signed by a key of its sender.
    pub verified: bool,
}

/// A decrypted message, kept in memory only; its length is all `Debug` shows.
#[derive(Clone)]
pub struct Inner(pub Vec<u8>);

impl std::fmt::Debug for Inner {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Inner({} bytes)", self.0.len())
    }
}

impl Email {
    /// The text of the email: its plain-text parts, or its HTML parts as text `width` columns wide. Nothing of the
    /// HTML is run or loaded – html2text only reads it.
    pub fn text(&self, width: usize) -> String {
        let value = |part: &BodyPart| part.part_id.as_ref().and_then(|id| self.body_values.get(id)).map(|v| &v.value);
        let plain: Vec<&String> =
            self.text_body.iter().filter(|part| part.kind != "text/html").filter_map(value).collect();
        if !plain.is_empty() {
            return plain.iter().map(|s| s.as_str()).collect::<Vec<_>>().join("\n\n");
        }
        self.html_body
            .iter()
            .filter_map(|part| Some((part, value(part)?)))
            .map(|(part, text)| if part.kind == "text/html" { html_to_text(text, width) } else { text.clone() })
            .collect::<Vec<_>>()
            .join("\n\n")
    }

    /// The content of an attachment decrypted here.
    pub fn part(&self, part: &BodyPart) -> Option<Result<Vec<u8>>> {
        let (inner, index) = (self.inner.as_ref()?, part.inner?);
        Some(kernoid_e2e::compose::part(&inner.0, index).map_err(Error::from))
    }

    /// The attachments decrypted here, in memory – to forward them: they are on the server only inside the encrypted
    /// message, so a forward cannot name them by blob as it does with others.
    pub fn decrypted_attachments(&self) -> Result<Vec<InlineFile>> {
        self.attachments
            .iter()
            .filter_map(|part| {
                let data = self.part(part)?;
                Some(data.map(|data| InlineFile {
                    name: part.name.clone().unwrap_or_else(|| "attachment".into()),
                    media_type: part.kind.clone(),
                    data,
                }))
            })
            .collect()
    }
}

/// A file held in memory – an attachment of a message decrypted here, forwarded –, attached like a file of the device:
/// inside an encrypted message, or uploaded with [`attach_bytes`]. Its content is not in `Debug`.
#[derive(Clone, PartialEq, Eq)]
pub struct InlineFile {
    pub name: String,
    pub media_type: String,
    pub data: Vec<u8>,
}

impl std::fmt::Debug for InlineFile {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("InlineFile")
            .field("name", &self.name)
            .field("media_type", &self.media_type)
            .field("size", &self.data.len())
            .finish()
    }
}

/// HTML as plain text, `width` columns wide.
pub fn html_to_text(html: &str, width: usize) -> String {
    html2text::config::plain().string_from_read(html.as_bytes(), width.max(20)).unwrap_or_else(|_| html.to_owned())
}

#[derive(Debug, Clone, Deserialize)]
pub struct Identity {
    pub id: String,
    #[serde(default)]
    pub name: String,
    pub email: String,
}

#[derive(Deserialize)]
struct List<T> {
    list: Vec<T>,
}

const ROLES: [&str; 6] = ["inbox", "drafts", "sent", "archive", "junk", "trash"];

/// The mailboxes of an account: by role (Inbox first), then by sort order and name.
pub async fn mailboxes(client: &Client, account: &str) -> Result<Vec<Mailbox>> {
    let result = client.call(&[MAIL], "Mailbox/get", json!({ "accountId": account })).await?;
    let mut list: Vec<Mailbox> = parse(result)?;
    let rank = |m: &Mailbox| m.role.as_deref().and_then(|r| ROLES.iter().position(|x| *x == r)).unwrap_or(99);
    list.sort_by(|a, b| rank(a).cmp(&rank(b)).then(a.sort_order.cmp(&b.sort_order)).then_with(|| a.name.cmp(&b.name)));
    Ok(list)
}

fn parse<T: serde::de::DeserializeOwned>(result: Value) -> Result<Vec<T>> {
    serde_json::from_value::<List<T>>(result).map(|list| list.list).map_err(|error| Error::Protocol(error.to_string()))
}

const SUMMARY: [&str; 10] =
    ["threadId", "mailboxIds", "keywords", "from", "to", "subject", "receivedAt", "preview", "hasAttachment", "size"];

/// The properties of a summary: with the end-to-end ones where the server has them.
fn summary(client: &Client) -> Vec<&'static str> {
    let mut properties = SUMMARY.to_vec();
    if client.session.capabilities.contains_key(OPENPGP) {
        properties.extend(["kernoid:encrypted", "kernoid:encryptedSummary"]);
    }
    properties
}

/// The capabilities a request for emails uses.
fn using(client: &Client) -> Vec<&'static str> {
    if client.session.capabilities.contains_key(OPENPGP) { vec![MAIL, OPENPGP] } else { vec![MAIL] }
}

/// A page of emails in a mailbox – or, without one, in all – newest first; `text` searches them (full text).
pub async fn emails(
    client: &Client,
    account: &str,
    mailbox: Option<&str>,
    text: &str,
    position: u64,
    limit: u64,
) -> Result<(Vec<EmailSummary>, u64)> {
    let mut filter = Map::new();
    if let Some(mailbox) = mailbox {
        filter.insert("inMailbox".into(), mailbox.into());
    }
    if !text.trim().is_empty() {
        filter.insert("text".into(), text.trim().into());
    }
    let properties = summary(client);
    let responses = client
        .request(
            &using(client),
            vec![
                (
                    "Email/query",
                    json!({
                        "accountId": account, "filter": filter, "position": position, "limit": limit,
                        "sort": [{ "property": "receivedAt", "isAscending": false }], "calculateTotal": true,
                    }),
                    "q",
                ),
                (
                    "Email/get",
                    json!({ "accountId": account, "#ids": ids_of("q", "Email/query"), "properties": properties }),
                    "g",
                ),
            ],
        )
        .await?;
    let total = responses.get("q").and_then(|q| q.get("total")).and_then(Value::as_u64).unwrap_or(0);
    let list = parse(responses.get("g").cloned().unwrap_or(Value::Null))?;
    Ok((list, total))
}

/// One email with its bodies and attachments.
pub async fn email(client: &Client, account: &str, id: &str) -> Result<Option<Email>> {
    Ok(full(client, account, &[id]).await?.into_iter().next())
}

/// Emails with their bodies and attachments, oldest first – the messages of a thread.
pub async fn full(client: &Client, account: &str, ids: &[&str]) -> Result<Vec<Email>> {
    let mut properties = summary(client);
    properties.extend([
        "blobId",
        "cc",
        "replyTo",
        "messageId",
        "references",
        "sentAt",
        "textBody",
        "htmlBody",
        "attachments",
        "bodyValues",
    ]);
    let result = client
        .call(
            &using(client),
            "Email/get",
            json!({
                "accountId": account, "ids": ids, "properties": properties,
                "fetchTextBodyValues": true, "fetchHTMLBodyValues": true, "maxBodyValueBytes": 4_000_000,
            }),
        )
        .await?;
    let mut list: Vec<Email> = parse(result)?;
    list.sort_by(|a, b| a.summary.received_at.cmp(&b.summary.received_at));
    Ok(list)
}

/// A conversation in a mailbox: its newest email there, and all its emails (in any mailbox), oldest first.
#[derive(Debug, Clone)]
pub struct Conversation {
    pub latest: EmailSummary,
    pub emails: Vec<EmailSummary>,
}

impl Conversation {
    /// The ids of its emails in `mailbox` – those that moving or deleting the conversation there acts on.
    pub fn ids_in(&self, mailbox: &str) -> Vec<String> {
        let ids: Vec<String> = self
            .emails
            .iter()
            .filter(|email| email.mailbox_ids.get(mailbox).copied().unwrap_or(false))
            .map(|email| email.id.clone())
            .collect();
        if ids.is_empty() { vec![self.latest.id.clone()] } else { ids }
    }

    pub fn unread(&self) -> bool {
        self.emails.iter().any(|email| !email.is_seen())
    }

    pub fn flagged(&self) -> bool {
        self.emails.iter().any(EmailSummary::is_flagged)
    }
}

/// A page of conversations in a mailbox, newest first (RFC 8621, 4.4.3 `collapseThreads`, then `Thread/get`); the
/// total counts conversations.
pub async fn conversations(
    client: &Client,
    account: &str,
    mailbox: Option<&str>,
    text: &str,
    position: u64,
    limit: u64,
) -> Result<(Vec<Conversation>, u64)> {
    let mut filter = Map::new();
    if let Some(mailbox) = mailbox {
        filter.insert("inMailbox".into(), mailbox.into());
    }
    if !text.trim().is_empty() {
        filter.insert("text".into(), text.trim().into());
    }
    let properties = summary(client);
    let responses = client.request(
        &using(client),
        vec![
            (
                "Email/query",
                json!({
                    "accountId": account, "filter": filter, "position": position, "limit": limit,
                    "sort": [{ "property": "receivedAt", "isAscending": false }], "calculateTotal": true,
                    "collapseThreads": true,
                }),
                "q",
            ),
            (
                "Email/get",
                json!({ "accountId": account, "#ids": ids_of("q", "Email/query"), "properties": properties }),
                "g",
            ),
            (
                "Thread/get",
                json!({ "accountId": account, "#ids": { "resultOf": "g", "name": "Email/get", "path": "/list/*/threadId" } }),
                "t",
            ),
            (
                "Email/get",
                json!({
                    "accountId": account, "properties": properties,
                    "#ids": { "resultOf": "t", "name": "Thread/get", "path": "/list/*/emailIds" },
                }),
                "m",
            ),
        ],
    ).await?;
    let total = responses.get("q").and_then(|q| q.get("total")).and_then(Value::as_u64).unwrap_or(0);
    let latest: Vec<EmailSummary> = parse(responses.get("g").cloned().unwrap_or(Value::Null))?;
    #[derive(Deserialize)]
    #[serde(rename_all = "camelCase")]
    struct Thread {
        id: String,
        email_ids: Vec<String>,
    }
    let threads: Vec<Thread> = parse(responses.get("t").cloned().unwrap_or(Value::Null))?;
    let members: Vec<EmailSummary> = parse(responses.get("m").cloned().unwrap_or(Value::Null))?;
    let out = latest
        .into_iter()
        .map(|latest| {
            let ids =
                threads.iter().find(|t| t.id == latest.thread_id).map(|t| t.email_ids.clone()).unwrap_or_default();
            let mut emails: Vec<EmailSummary> =
                ids.iter().filter_map(|id| members.iter().find(|m| &m.id == id).cloned()).collect();
            if emails.is_empty() {
                emails.push(latest.clone());
            }
            emails.sort_by(|a, b| a.received_at.cmp(&b.received_at));
            Conversation { latest, emails }
        })
        .collect();
    Ok((out, total))
}

/// Makes a mailbox – in `parent`, or at the top – and returns its id.
pub async fn create_mailbox(client: &Client, account: &str, name: &str, parent: Option<&str>) -> Result<String> {
    let create = json!({ "create": { "m": { "name": name, "parentId": parent } } });
    let result = client.set(&[MAIL], "Mailbox", account, create, json!({})).await?;
    result["created"]["m"]["id"]
        .as_str()
        .map(str::to_owned)
        .ok_or_else(|| Error::Protocol("the server named no new mailbox".into()))
}

/// Gives a mailbox another name.
pub async fn rename_mailbox(client: &Client, account: &str, id: &str, name: &str) -> Result<()> {
    client.set(&[MAIL], "Mailbox", account, json!({ "update": { id: { "name": name } } }), json!({})).await?;
    Ok(())
}

/// Moves a mailbox into `parent`, or to the top.
pub async fn move_mailbox(client: &Client, account: &str, id: &str, parent: Option<&str>) -> Result<()> {
    client.set(&[MAIL], "Mailbox", account, json!({ "update": { id: { "parentId": parent } } }), json!({})).await?;
    Ok(())
}

/// Removes a mailbox – with the emails only in it when `with_emails`; otherwise the server refuses a mailbox that
/// holds any (`mailboxHasEmail`).
pub async fn destroy_mailbox(client: &Client, account: &str, id: &str, with_emails: bool) -> Result<()> {
    let extra = json!({ "onDestroyRemoveEmails": with_emails });
    client.set(&[MAIL], "Mailbox", account, json!({ "destroy": [id] }), extra).await?;
    Ok(())
}

/// Removes every email of a mailbox for good – emptying the Trash or Junk – and returns how many.
pub async fn empty_mailbox(client: &Client, account: &str, id: &str) -> Result<u64> {
    let mut removed = 0;
    loop {
        let result = client
            .call(&[MAIL], "Email/query", json!({ "accountId": account, "filter": { "inMailbox": id }, "limit": 500 }))
            .await?;
        let ids: Vec<String> = serde_json::from_value(result["ids"].clone()).unwrap_or_default();
        if ids.is_empty() {
            return Ok(removed);
        }
        destroy_all(client, account, &ids).await?;
        removed += ids.len() as u64;
    }
}

/// Sets or clears a keyword on several emails at once.
pub async fn set_keywords(client: &Client, account: &str, ids: &[String], keyword: &str, on: bool) -> Result<()> {
    let value = if on { Value::Bool(true) } else { Value::Null };
    let path = format!("keywords/{keyword}");
    let update: Map<String, Value> = ids.iter().map(|id| (id.clone(), json!({ path.as_str(): value }))).collect();
    client.set(&[MAIL], "Email", account, json!({ "update": update }), json!({})).await?;
    Ok(())
}

/// Moves several emails into one mailbox.
pub async fn move_all(client: &Client, account: &str, ids: &[String], mailbox: &str) -> Result<()> {
    let update: Map<String, Value> =
        ids.iter().map(|id| (id.clone(), json!({ "mailboxIds": { mailbox: true } }))).collect();
    client.set(&[MAIL], "Email", account, json!({ "update": update }), json!({})).await?;
    Ok(())
}

/// Removes several emails for good.
pub async fn destroy_all(client: &Client, account: &str, ids: &[String]) -> Result<()> {
    client.set(&[MAIL], "Email", account, json!({ "destroy": ids }), json!({})).await?;
    Ok(())
}

/// Sets or clears a keyword (`$seen`, `$flagged`).
pub async fn set_keyword(client: &Client, account: &str, id: &str, keyword: &str, on: bool) -> Result<()> {
    let value = if on { Value::Bool(true) } else { Value::Null };
    let path = format!("keywords/{keyword}");
    client.set(&[MAIL], "Email", account, json!({ "update": { id: { path: value } } }), json!({})).await?;
    Ok(())
}

/// Moves an email into one mailbox (out of all others).
pub async fn move_to(client: &Client, account: &str, id: &str, mailbox: &str) -> Result<()> {
    client
        .set(&[MAIL], "Email", account, json!({ "update": { id: { "mailboxIds": { mailbox: true } } } }), json!({}))
        .await?;
    Ok(())
}

/// Removes an email for good.
pub async fn destroy(client: &Client, account: &str, id: &str) -> Result<()> {
    client.set(&[MAIL], "Email", account, json!({ "destroy": [id] }), json!({})).await?;
    Ok(())
}

/// The addresses the account may send as.
pub async fn identities(client: &Client, account: &str) -> Result<Vec<Identity>> {
    parse(client.call(&[MAIL, SUBMISSION], "Identity/get", json!({ "accountId": account })).await?)
}

/// A file attached to a message being written: uploaded, or a part of another message (forwarding).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Attachment {
    pub blob_id: String,
    pub kind: String,
    pub name: String,
    pub size: u64,
}

/// A message to send or keep as a draft.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Draft {
    pub from: Option<EmailAddress>,
    pub to: Vec<EmailAddress>,
    pub cc: Vec<EmailAddress>,
    pub bcc: Vec<EmailAddress>,
    pub subject: String,
    pub body: String,
    pub in_reply_to: Vec<String>,
    pub references: Vec<String>,
    pub attachments: Vec<Attachment>,
}

impl Draft {
    /// The email object of `Email/set` (RFC 8621, 4.6), in `mailbox`.
    pub fn email(&self, mailbox: &str, draft: bool) -> Value {
        let mut keywords = json!({ "$seen": true });
        if draft {
            keywords["$draft"] = true.into();
        }
        let mut email = json!({
            "mailboxIds": { mailbox: true },
            "keywords": keywords,
            "to": self.to,
            "subject": self.subject,
            "bodyValues": { "body": { "value": self.body } },
            "textBody": [{ "partId": "body", "type": "text/plain" }],
        });
        if let Some(from) = &self.from {
            email["from"] = json!([from]);
        }
        for (key, list) in [("cc", &self.cc), ("bcc", &self.bcc)] {
            if !list.is_empty() {
                email[key] = json!(list);
            }
        }
        if !self.in_reply_to.is_empty() {
            email["inReplyTo"] = json!(self.in_reply_to);
        }
        if !self.references.is_empty() {
            email["references"] = json!(self.references);
        }
        if !self.attachments.is_empty() {
            email["attachments"] = self
                .attachments
                .iter()
                .map(|file| {
                    json!({ "blobId": file.blob_id, "type": file.kind, "name": file.name, "disposition": "attachment" })
                })
                .collect();
        }
        email
    }
}

pub(crate) fn role<'a>(boxes: &'a [Mailbox], role: &str) -> Option<&'a Mailbox> {
    boxes.iter().find(|mailbox| mailbox.role.as_deref() == Some(role))
}

/// Keeps a message in Drafts; its id.
pub async fn save_draft(client: &Client, account: &str, draft: &Draft, boxes: &[Mailbox]) -> Result<String> {
    let drafts = role(boxes, "drafts").ok_or_else(|| Error::Protocol("there is no Drafts mailbox".into()))?;
    let result = client
        .set(&[MAIL], "Email", account, json!({ "create": { "draft": draft.email(&drafts.id, true) } }), json!({}))
        .await?;
    result
        .pointer("/created/draft/id")
        .and_then(Value::as_str)
        .map(str::to_owned)
        .ok_or_else(|| Error::Protocol("the draft has no id".into()))
}

/// Stores the message in Drafts and submits it as `identity`; once sent it moves to Sent and is no draft any more
/// (RFC 8621, 7.5).
pub async fn send(client: &Client, account: &str, identity: &Identity, draft: &Draft, boxes: &[Mailbox]) -> Result<()> {
    let drafts = role(boxes, "drafts").ok_or_else(|| Error::Protocol("there is no Drafts mailbox".into()))?;
    let mut update = json!({ "keywords/$draft": null });
    if let Some(sent) = role(boxes, "sent") {
        update["mailboxIds"] = json!({ sent.id.as_str(): true });
    }
    let mut draft = draft.clone();
    draft.from = Some(EmailAddress {
        name: (!identity.name.is_empty()).then(|| identity.name.clone()),
        email: identity.email.clone(),
    });
    let responses = client
        .request(
            &[MAIL, SUBMISSION],
            vec![
                (
                    "Email/set",
                    json!({ "accountId": account, "create": { "draft": draft.email(&drafts.id, true) } }),
                    "e",
                ),
                (
                    "EmailSubmission/set",
                    json!({
                        "accountId": account,
                        "create": { "send": { "identityId": identity.id, "emailId": "#draft" } },
                        "onSuccessUpdateEmail": { "#send": update },
                    }),
                    "s",
                ),
            ],
        )
        .await?;
    for (_, result, _) in &responses.0 {
        set_error(result)?;
    }
    Ok(())
}

/// Uploads a file to attach.
pub async fn attach(client: &Client, account: &str, path: &std::path::Path) -> Result<Attachment> {
    let file = tokio::fs::File::open(path).await?;
    let size = file.metadata().await?.len();
    let name = path.file_name().and_then(|n| n.to_str()).unwrap_or("attachment").to_owned();
    let kind = crate::files::media_type(&name);
    let upload = client.upload(account, kind, file, size, |_, _| {}).await?;
    Ok(Attachment { blob_id: upload.blob_id, kind: kind.to_owned(), name, size: upload.size })
}

/// Uploads a file held in memory, to attach it to a message that does not go end-to-end encrypted.
pub async fn attach_bytes(client: &Client, account: &str, file: &InlineFile) -> Result<Attachment> {
    let size = file.data.len() as u64;
    let upload =
        client.upload(account, &file.media_type, std::io::Cursor::new(file.data.clone()), size, |_, _| {}).await?;
    Ok(Attachment {
        blob_id: upload.blob_id,
        kind: file.media_type.clone(),
        name: file.name.clone(),
        size: upload.size,
    })
}

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

    #[test]
    fn addresses() {
        let list = parse_addresses("Alice <a@x.example>, b@x.example; \"Doe, Jane\" <j@x.example>");
        assert_eq!(
            list,
            vec![
                EmailAddress { name: Some("Alice".into()), email: "a@x.example".into() },
                EmailAddress { name: None, email: "b@x.example".into() },
                EmailAddress { name: Some("Doe, Jane".into()), email: "j@x.example".into() },
            ]
        );
        assert_eq!(format_addresses(&list), "Alice <a@x.example>, b@x.example, \"Doe, Jane\" <j@x.example>");
        assert!(parse_addresses(" , ").is_empty());
    }

    #[test]
    fn html_becomes_text() {
        let text = html_to_text("<p>Hello <b>you</b></p><script>alert(1)</script><p>Second</p>", 80);
        assert!(text.contains("Hello") && text.contains("you"), "{text}");
        assert!(text.contains("Second"));
        assert!(!text.contains("alert"));
    }

    #[test]
    fn draft_email() {
        let draft = Draft {
            to: parse_addresses("b@x.example"),
            subject: "Hi".into(),
            body: "Text".into(),
            in_reply_to: vec!["m1@x".into()],
            attachments: vec![Attachment {
                blob_id: "B".into(),
                kind: "text/plain".into(),
                name: "a.txt".into(),
                size: 1,
            }],
            ..Draft::default()
        };
        let email = draft.email("D", true);
        assert_eq!(email["mailboxIds"], json!({ "D": true }));
        assert_eq!(email["keywords"], json!({ "$seen": true, "$draft": true }));
        assert_eq!(email["inReplyTo"], json!(["m1@x"]));
        assert_eq!(email["attachments"][0]["blobId"], "B");
        assert!(email.get("cc").is_none());
    }
}