rmut-core 2.16.0

Core mail handling for rmut: maildir scanning, message parsing
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
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
//! Draft building and finalizing for outgoing mail.

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

use anyhow::{Context, Result, ensure};
use chrono::{Local, TimeZone};

use crate::pattern::Me;

pub struct DraftHeaders {
    /// From line when an identity override applies (reverse_name, a
    /// folder/recipient rule, the account); the user can edit it, and
    /// finalize falls back to the default identity when absent.
    pub from: Option<String>,
    pub to: String,
    pub cc: Option<String>,
    pub subject: String,
    pub in_reply_to: Option<String>,
    pub references: Option<String>,
}

/// The text the user edits in $EDITOR: header block, blank line, body.
pub fn draft_text(h: &DraftHeaders, body: &str) -> String {
    let mut out = String::new();
    if let Some(from) = &h.from {
        out += &format!("From: {from}\n");
    }
    out += &format!("To: {}\n", h.to);
    if let Some(cc) = &h.cc
        && !cc.trim().is_empty()
    {
        out += &format!("Cc: {cc}\n");
    }
    out += &format!("Subject: {}\n", h.subject);
    if let Some(x) = &h.in_reply_to {
        out += &format!("In-Reply-To: {x}\n");
    }
    if let Some(x) = &h.references {
        out += &format!("References: {x}\n");
    }
    out.push('\n');
    out.push_str(body);
    if !out.ends_with('\n') {
        out.push('\n');
    }
    out
}

/// mutt's $attribution: the line a quoted reply opens with.
pub const DEFAULT_ATTRIBUTION: &str = "On %d, %n wrote:";

/// mutt's $forward_format: the subject a forward carries.
pub const DEFAULT_FORWARD_FORMAT: &str = "[%a: %s]";

/// mutt's $indent_string: what a quoted line is prefixed with.
pub const DEFAULT_INDENT: &str = "> ";

/// The message an attribution or a forward subject is about, for the
/// format strings that describe it.
pub struct Quoted<'a> {
    /// The From header as written, name and address together.
    pub from: &'a str,
    pub subject: &'a str,
    pub message_id: Option<&'a str>,
    /// When it was sent, seconds since the epoch.
    pub date: i64,
}

impl Quoted<'_> {
    /// The author's display name, or their address when the header
    /// carries no name (as mutt's %n does).
    fn name(&self) -> String {
        let trimmed = self.from.trim();
        let name = match trimmed.split_once('<') {
            Some((name, _)) => name.trim().trim_matches('"').trim(),
            None => "",
        };
        match name.is_empty() {
            true => self.address(),
            false => name.to_string(),
        }
    }

    fn address(&self) -> String {
        bare_address(self.from).unwrap_or_else(|| self.from.trim().to_string())
    }
}

/// Expand one of mutt's message format strings: `%a` the author's
/// address, `%n` their name, `%s` the subject, `%i` the message-id,
/// `%d` the date, `%{...}` the date through strftime, `%%` a percent.
/// Padding and conditionals work as they do in the index format.
pub fn render_quoted(fmt: &str, m: &Quoted) -> String {
    // `%{...}` first: the strftime span would confuse the specifier
    // machinery, which reads one character.
    let fmt = expand_strftime(fmt, m.date);
    crate::format::render_with(&fmt, &|spec| match spec {
        'a' => m.address(),
        'n' => m.name(),
        'f' => m.from.trim().to_string(),
        's' => m.subject.trim().to_string(),
        'i' => m
            .message_id
            .unwrap_or_default()
            .trim_matches(['<', '>'])
            .to_string(),
        'd' => format_date(m.date),
        '%' => "%".to_string(),
        other => format!("%{other}"),
    })
}

/// mutt's `%{strftime}`: the message's date, in the caller's words.
fn expand_strftime(fmt: &str, date: i64) -> String {
    let mut out = String::new();
    let mut rest = fmt;
    while let Some(at) = rest.find("%{") {
        out.push_str(&rest[..at]);
        let Some(end) = rest[at + 2..].find('}') else {
            break;
        };
        let spec = &rest[at + 2..at + 2 + end];
        out.push_str(&strftime(spec, date));
        rest = &rest[at + 2 + end + 1..];
    }
    out.push_str(rest);
    out
}

fn strftime(spec: &str, epoch: i64) -> String {
    match Local.timestamp_opt(epoch, 0) {
        chrono::LocalResult::Single(dt) | chrono::LocalResult::Ambiguous(dt, _) => {
            dt.format(spec).to_string()
        }
        chrono::LocalResult::None => String::new(),
    }
}

/// mutt's $reply_regexp default: "Re:" with an optional bracketed
/// count, as Re[2]: has it.
pub const REPLY_REGEXP: &str = r"^(re)(\[[0-9]+\])*:[ \t]*";

/// Compile a $reply_regexp the way mutt does: case-insensitively
/// unless the pattern itself has an uppercase letter (mutt's
/// mutt_which_case).
pub fn reply_regexp(spec: &str) -> Result<regex_lite::Regex, regex_lite::Error> {
    let smart = if spec.chars().any(char::is_uppercase) {
        spec.to_string()
    } else {
        format!("(?i){spec}")
    };
    regex_lite::Regex::new(&smart)
}

pub fn default_reply_regexp() -> regex_lite::Regex {
    reply_regexp(REPLY_REGEXP).expect("default reply_regexp compiles")
}

/// The subject of a reply, as mutt makes it: "Re: " over the original
/// with whatever $reply_regexp matched at its start taken off, so an
/// "RE: x" or "Re[2]: x" answers as "Re: x" rather than piling up.
pub fn reply_subject(orig: &str, re: &regex_lite::Regex) -> String {
    let t = orig.trim();
    let rest = match re.find(t) {
        Some(m) if m.start() == 0 => t[m.end()..].trim_start(),
        _ => t,
    };
    format!("Re: {rest}")
}

/// mutt's $forward_format over the message being forwarded.
pub fn forward_subject(fmt: &str, m: &Quoted) -> String {
    render_quoted(fmt, m)
}

fn format_date(epoch: i64) -> String {
    match Local.timestamp_opt(epoch, 0) {
        chrono::LocalResult::Single(dt) | chrono::LocalResult::Ambiguous(dt, _) => {
            dt.format("%a, %d %b %Y %H:%M").to_string()
        }
        chrono::LocalResult::None => "an unknown date".into(),
    }
}

/// mutt's $attribution over the message being replied to.
pub fn attribution(fmt: &str, m: &Quoted) -> String {
    render_quoted(fmt, m)
}

/// Quote a body mutt-style under an attribution line, each line
/// prefixed with $indent_string.
pub fn quote(attribution: &str, indent: &str, body: &str) -> String {
    let mut out = format!("{attribution}\n");
    for line in body.lines() {
        out += &format!("{indent}{line}\n");
    }
    out
}

/// The forwarded original between mutt's markers. With mutt's
/// $forward_quote the message itself is prefixed with $indent_string,
/// the way a reply is quoted; the markers stay flush, since they are
/// rmut's words and not the original's.
pub fn forward_body(
    from: &str,
    date_epoch: i64,
    subject: &str,
    body: &str,
    quote_with: Option<&str>,
) -> String {
    let body = body.trim_end();
    let body = match quote_with {
        Some(indent) => body
            .lines()
            .map(|l| format!("{indent}{l}"))
            .collect::<Vec<_>>()
            .join("\n"),
        None => body.to_string(),
    };
    format!(
        "----- Forwarded message from {from} -----\nDate: {}\nSubject: {subject}\n\n{body}\n----- End forwarded message -----\n",
        format_date(date_epoch),
    )
}

/// mutt's $signature: the text a draft ends with. A name ending in
/// `|` is a command whose standard output is the signature (mutt's
/// rule); anything else is a file. A signature that cannot be read is
/// no signature: a draft is worth more than the ornament on it.
pub fn signature_text(setting: &str) -> Option<String> {
    let setting = setting.trim();
    if setting.is_empty() {
        return None;
    }
    let text = match setting.strip_suffix('|') {
        Some(command) => {
            let out = std::process::Command::new("sh")
                .arg("-c")
                .arg(command.trim())
                .output()
                .ok()?;
            String::from_utf8_lossy(&out.stdout).into_owned()
        }
        None => std::fs::read_to_string(expand_home(setting)).ok()?,
    };
    let text = text.trim_end_matches('\n');
    (!text.is_empty()).then(|| text.to_string())
}

/// The body with the signature under it, mutt's way: a blank line,
/// then $sig_dashes' "-- " line when it is on, then the signature.
pub fn with_signature(body: &str, signature: &str, dashes: bool) -> String {
    with_signature_at(body, signature, dashes, false)
}

/// The signature added to a draft. mutt's $sig_on_top puts it above
/// the quoted original instead of below (with a blank line between,
/// so the reply is written between the signature and the quote).
pub fn with_signature_at(body: &str, signature: &str, dashes: bool, on_top: bool) -> String {
    let mut sig = String::new();
    if dashes {
        sig += "-- \n";
    }
    sig += signature;
    if !sig.ends_with('\n') {
        sig.push('\n');
    }
    if on_top {
        return format!("{sig}\n{body}");
    }
    let mut out = body.to_string();
    if !out.is_empty() && !out.ends_with('\n') {
        out.push('\n');
    }
    out.push('\n');
    out += &sig;
    out
}

pub fn make_message_id(hostname: &str) -> String {
    format!(
        "<{}.{}.rmut@{hostname}>",
        Local::now().timestamp_millis(),
        std::process::id(),
    )
}

pub fn rfc2822_now() -> String {
    Local::now().to_rfc2822()
}

fn header_present(head: &str, name: &str) -> bool {
    head.lines().any(|l| {
        l.get(..name.len())
            .is_some_and(|k| k.eq_ignore_ascii_case(name))
            && l.as_bytes().get(name.len()) == Some(&b':')
    })
}

/// Finalize an edited draft for sending: require a recipient, add
/// mutt's $user_agent header, when the caller asks for it.
pub fn user_agent_header() -> String {
    format!("User-Agent: rmut/{}", env!("CARGO_PKG_VERSION"))
}

/// From/Date/Message-ID when the user didn't write them.
pub fn finalize(draft: &str, from: &str, msg_id: &str, date: &str) -> Result<String> {
    finalize_with(draft, from, msg_id, date, false)
}

/// Like `finalize`, adding a `User-Agent` header when `user_agent`
/// and the draft has none of its own.
pub fn finalize_with(
    draft: &str,
    from: &str,
    msg_id: &str,
    date: &str,
    user_agent: bool,
) -> Result<String> {
    let (head, body) = draft.split_once("\n\n").unwrap_or((draft.trim_end(), ""));
    let has_recipients = head.lines().any(|l| {
        l.split_once(':').is_some_and(|(k, v)| {
            ["to", "cc", "bcc"].contains(&k.trim().to_lowercase().as_str()) && !v.trim().is_empty()
        })
    });
    ensure!(has_recipients, "no recipients (To/Cc/Bcc)");
    let mut head = head.trim_end().to_string();
    if !header_present(&head, "From") {
        head += &format!("\nFrom: {from}");
    }
    if !header_present(&head, "Date") {
        head += &format!("\nDate: {date}");
    }
    if !header_present(&head, "Message-ID") {
        head += &format!("\nMessage-ID: {msg_id}");
    }
    if user_agent && !header_present(&head, "User-Agent") {
        head += &format!("\n{}", user_agent_header());
    }
    Ok(format!("{head}\n\n{body}"))
}

/// A file named in an `Attach:` pseudo-header of the draft.
pub struct Attachment {
    pub path: PathBuf,
    /// Content-type override (compose menu ctrl+t); guessed from the
    /// extension otherwise.
    pub mime: Option<String>,
    pub description: Option<String>,
    /// mutt's rename-attachment: the filename the part is sent under,
    /// when not the file's own. `@name="..."` on the line.
    pub name: Option<String>,
    /// mutt's toggle-disposition: Content-Disposition inline rather
    /// than attachment. `@inline` on the line.
    pub inline: bool,
    /// mutt's toggle-unlink: the file goes once the message has been
    /// sent. `@unlink` on the line.
    pub unlink: bool,
}

impl Attachment {
    /// A plain attachment of this file: nothing overridden.
    pub fn of(path: PathBuf) -> Attachment {
        Attachment {
            path,
            mime: None,
            description: None,
            name: None,
            inline: false,
            unlink: false,
        }
    }

    /// The filename the part goes out under.
    pub fn send_name(&self) -> &str {
        self.name
            .as_deref()
            .filter(|n| !n.is_empty())
            .unwrap_or_else(|| {
                self.path
                    .file_name()
                    .and_then(|n| n.to_str())
                    .unwrap_or("attachment")
            })
    }
}

/// A `type/subtype` token (letters, digits, `.+-`), so a content-type
/// between the path and the description is recognizable.
fn looks_like_mime(token: &str) -> bool {
    match token.split_once('/') {
        Some((t, s)) if !t.is_empty() && !s.is_empty() => token
            .chars()
            .all(|c| c.is_ascii_alphanumeric() || matches!(c, '/' | '.' | '+' | '-')),
        _ => false,
    }
}

/// One Attach: line back from its parts (quotes around a path with
/// spaces, then the optional type and description).
pub fn attach_line(a: &Attachment) -> String {
    let p = a.path.display().to_string();
    let mut line = if p.contains(' ') {
        format!("Attach: \"{p}\"")
    } else {
        format!("Attach: {p}")
    };
    if let Some(m) = &a.mime {
        line += &format!(" {m}");
    }
    // The @-options sit between the type and the description, where
    // a description is not expected to start with an @.
    if let Some(n) = a.name.as_deref().filter(|n| !n.is_empty()) {
        line += &format!(" @name=\"{}\"", n.replace('"', ""));
    }
    if a.inline {
        line += " @inline";
    }
    if a.unlink {
        line += " @unlink";
    }
    if let Some(d) = &a.description {
        line += &format!(" {d}");
    }
    line
}

/// Pull mutt-style `Attach: <path> [type/subtype] [description]`
/// pseudo-headers out of a draft's header block; quotes allow a path
/// with spaces, `~/` means $HOME. Returns the draft without those
/// lines.
pub fn extract_attachments(draft: &str) -> (String, Vec<Attachment>) {
    let (head, body) = match draft.split_once("\n\n") {
        Some((h, b)) => (h, Some(b)),
        None => (draft, None),
    };
    let mut attachments = Vec::new();
    let mut kept = Vec::new();
    for line in head.lines() {
        let value = match line.split_once(':') {
            Some((k, v)) if k.trim().eq_ignore_ascii_case("attach") => v.trim(),
            _ => {
                kept.push(line);
                continue;
            }
        };
        if value.is_empty() {
            continue;
        }
        let (path, desc) = match value.strip_prefix('"') {
            Some(rest) => rest.split_once('"').unwrap_or((rest, "")),
            None => value.split_once(char::is_whitespace).unwrap_or((value, "")),
        };
        let mut desc = desc.trim();
        let mut mime = None;
        match desc.split_once(char::is_whitespace) {
            Some((first, rest)) if looks_like_mime(first) => {
                mime = Some(first.to_string());
                desc = rest.trim();
            }
            None if looks_like_mime(desc) => {
                mime = Some(desc.to_string());
                desc = "";
            }
            _ => {}
        }
        // The @-options, in any order, ahead of the description.
        let mut a = Attachment::of(expand_home(path));
        a.mime = mime;
        while let Some(rest) = desc.strip_prefix('@') {
            if let Some(rest) = rest.strip_prefix("inline") {
                a.inline = true;
                desc = rest.trim_start();
            } else if let Some(rest) = rest.strip_prefix("unlink") {
                a.unlink = true;
                desc = rest.trim_start();
            } else if let Some(rest) = rest.strip_prefix("name=") {
                let (name, rest) = match rest.strip_prefix('"') {
                    Some(q) => q.split_once('"').unwrap_or((q, "")),
                    None => rest.split_once(char::is_whitespace).unwrap_or((rest, "")),
                };
                a.name = (!name.is_empty()).then(|| name.to_string());
                desc = rest.trim_start();
            } else {
                break; // a description that happens to start with @
            }
        }
        a.description = (!desc.is_empty()).then(|| desc.to_string());
        attachments.push(a);
    }
    let mut out = kept.join("\n");
    if let Some(body) = body {
        out += "\n\n";
        out += body;
    }
    (out, attachments)
}

fn expand_home(path: &str) -> PathBuf {
    if let Some(rest) = path.strip_prefix("~/")
        && let Ok(home) = std::env::var("HOME")
    {
        return Path::new(&home).join(rest);
    }
    PathBuf::from(path)
}

/// Content type guessed from the filename extension.
pub fn content_type(path: &Path) -> &'static str {
    let ext = path
        .extension()
        .and_then(|e| e.to_str())
        .map(|e| e.to_ascii_lowercase());
    match ext.as_deref() {
        Some("txt" | "log" | "md" | "patch" | "diff") => "text/plain",
        Some("html" | "htm") => "text/html",
        Some("csv") => "text/csv",
        Some("pdf") => "application/pdf",
        Some("png") => "image/png",
        Some("jpg" | "jpeg") => "image/jpeg",
        Some("gif") => "image/gif",
        Some("zip") => "application/zip",
        Some("gz") => "application/gzip",
        Some("tar") => "application/x-tar",
        Some("json") => "application/json",
        Some("xml") => "application/xml",
        _ => "application/octet-stream",
    }
}

/// Base64 in MIME shape: 76-character lines, CRLF line endings.
fn b64_wrapped(bytes: &[u8]) -> String {
    let s = crate::smtp::b64(bytes);
    let mut out = String::with_capacity(s.len() + s.len() / 38 + 2);
    for chunk in s.as_bytes().chunks(76) {
        out.push_str(std::str::from_utf8(chunk).expect("base64 is ascii"));
        out.push_str("\r\n");
    }
    out
}

/// The text/plain entity of an outgoing message: its Content-Type
/// header and the body in canonical CRLF form. With mutt's
/// $text_flowed the part is declared `format=flowed` and the body is
/// space-stuffed, so a reader may rewrap it (RFC 3676). The
/// paragraphs are the editor's doing: rmut adds no trailing spaces.
pub fn text_entity(body: &str, flowed: bool) -> String {
    let mut out = String::from("Content-Type: text/plain; charset=utf-8");
    if flowed {
        out += "; format=flowed";
    }
    out += "\r\nContent-Transfer-Encoding: 8bit\r\n\r\n";
    let body = match flowed {
        true => crate::flowed::space_stuff(body),
        false => body.to_string(),
    };
    out += &String::from_utf8_lossy(&crate::pgp::crlf(body.as_bytes()));
    out
}

/// The draft's text as a MIME entity: text/plain, or with markdown
/// compose a multipart/alternative of that text/plain and the
/// text/html rendered from it. The plain half comes first, so a
/// reader that shows the last part it can (every graphical one) shows
/// the html, and a plain one loses nothing.
pub fn body_entity(body: &str, flowed: bool, markdown: bool) -> String {
    let plain = text_entity(body, flowed);
    if !markdown {
        return plain;
    }
    let html = format!(
        "Content-Type: text/html; charset=utf-8\r\n\
         Content-Transfer-Encoding: quoted-printable\r\n\r\n{}",
        quoted_printable(&crate::markdown::to_html(body))
    );
    let boundary = {
        let mut n = 0usize;
        loop {
            let b = format!("=-rmut-alt-{}-{n}", std::process::id());
            if !plain.contains(&b) && !html.contains(&b) {
                break b;
            }
            n += 1;
        }
    };
    let mut out = format!("Content-Type: multipart/alternative; boundary=\"{boundary}\"\r\n\r\n");
    for part in [plain, html] {
        out += &format!("--{boundary}\r\n{part}");
        if !out.ends_with("\r\n") {
            out += "\r\n";
        }
    }
    out += &format!("--{boundary}--\r\n");
    out
}

/// Quoted-printable (RFC 2045), CRLF line ends: the html half's
/// paragraphs are single lines, often past the 998 bytes SMTP allows.
pub fn quoted_printable(text: &str) -> String {
    let mut out = String::new();
    for (i, line) in text.split('\n').enumerate() {
        if i > 0 {
            out += "\r\n";
        }
        let line = line.strip_suffix('\r').unwrap_or(line);
        let bytes = line.as_bytes();
        let mut width = 0;
        for (j, &b) in bytes.iter().enumerate() {
            let last = j + 1 == bytes.len();
            let piece = match b {
                b'=' => "=3D".to_string(),
                b' ' | b'\t' if last => format!("={b:02X}"),
                b' ' | b'\t' | 33..=126 => (b as char).to_string(),
                _ => format!("={b:02X}"),
            };
            // A soft break keeps every line within 76, its "=" included.
            if width + piece.len() > 75 {
                out += "=\r\n";
                width = 0;
            }
            width += piece.len();
            out += &piece;
        }
    }
    out
}

/// The draft header that says whether this draft is markdown: rmut's
/// own, written by the compose menu's toggle, kept through postpone,
/// and taken off before the message goes anywhere.
pub const MARKDOWN_HEADER: &str = "X-Rmut-Markdown";

/// The draft without its markdown header, and what the header said
/// (None when it has none, and the config decides).
pub fn take_markdown(draft: &str) -> (String, Option<bool>) {
    let (head, body) = match draft.split_once("\n\n") {
        Some((h, b)) => (h, Some(b)),
        None => (draft, None),
    };
    let mut said = None;
    let kept: Vec<&str> = head
        .lines()
        .filter(|line| match line.split_once(':') {
            Some((k, v)) if k.trim().eq_ignore_ascii_case(MARKDOWN_HEADER) => {
                said = Some(matches!(
                    v.trim().to_lowercase().as_str(),
                    "yes" | "true" | "on"
                ));
                false
            }
            _ => true,
        })
        .collect();
    let head = kept.join("\n");
    let text = match body {
        Some(body) => format!("{head}\n\n{body}"),
        None => head,
    };
    (text, said)
}

/// mutt's $text_flowed for a message that goes out with no MIME
/// wrapper at all: declare the body and space-stuff it, in place, on
/// a finalized draft. One that already carries a Content-Type (the
/// user wrote their own) is left alone.
pub fn flow_plain(text: &str) -> String {
    let (head, body) = match text.split_once("\n\n") {
        Some(pair) => pair,
        None => return text.to_string(),
    };
    if header_present(head, "Content-Type") {
        return text.to_string();
    }
    format!(
        "{}\nMIME-Version: 1.0\nContent-Type: text/plain; charset=utf-8; format=flowed\n\
         Content-Transfer-Encoding: 8bit\n\n{}",
        head.trim_end(),
        crate::flowed::space_stuff(body),
    )
}

/// The Content-Disposition filename parameter: a plain quoted string
/// when the name is ASCII without `"` or `\`, else RFC 2231's
/// `filename*=utf-8''<percent-encoded>`, which carries any name and
/// which readers decode alike (quoted-pair escapes they often do not).
/// Control characters (a newline in a file name) become spaces.
fn filename_param(name: &str) -> String {
    let name = one_line_value(name);
    if name.is_ascii() && !name.contains(['"', '\\']) {
        return format!("filename=\"{name}\"");
    }
    let mut out = String::from("filename*=utf-8''");
    for b in name.bytes() {
        // RFC 2231's attribute-char: what may stand unencoded.
        if b.is_ascii_alphanumeric() || b"!#$&+-.^_`|~".contains(&b) {
            out.push(b as char);
        } else {
            out += &format!("%{b:02X}");
        }
    }
    out
}

/// A header value that stays one header: CR, LF and other controls
/// become spaces.
fn one_line_value(value: &str) -> String {
    value
        .chars()
        .map(|c| if c.is_control() { ' ' } else { c })
        .collect()
}

/// The MIME entity (Content-Type header + body, CRLF endings) for a
/// draft body with attachments: multipart/mixed with the text first,
/// files base64-encoded, and optionally the forwarded original as
/// message/rfc822 (mutt's mime_forward). The caller puts it under the
/// draft's top-level headers, or inside a PGP layer.
pub fn mixed_entity(
    body: &str,
    files: &[Attachment],
    original: Option<&[u8]>,
    flowed: bool,
    markdown: bool,
) -> Result<String> {
    let mut parts: Vec<String> = Vec::new();
    parts.push(body_entity(body, flowed, markdown));
    for a in files {
        let bytes =
            std::fs::read(&a.path).with_context(|| format!("reading {}", a.path.display()))?;
        let mime = a.mime.as_deref().unwrap_or_else(|| content_type(&a.path));
        let disposition = if a.inline { "inline" } else { "attachment" };
        // An attached message (mutt's attach-message) goes in as it
        // is, a message/rfc822 part with no encoding and no filename.
        if mime.eq_ignore_ascii_case("message/rfc822") {
            let mut p =
                format!("Content-Type: message/rfc822\r\nContent-Disposition: {disposition}\r\n");
            if let Some(d) = &a.description {
                p += &format!("Content-Description: {}\r\n", one_line_value(d));
            }
            p += "\r\n";
            p += &String::from_utf8_lossy(&crate::pgp::crlf(&bytes));
            parts.push(p);
            continue;
        }
        let mut p = format!(
            "Content-Type: {mime}\r\nContent-Disposition: {disposition}; {}\r\n",
            filename_param(a.send_name()),
        );
        if let Some(d) = &a.description {
            p += &format!("Content-Description: {}\r\n", one_line_value(d));
        }
        p += "Content-Transfer-Encoding: base64\r\n\r\n";
        p += &b64_wrapped(&bytes);
        parts.push(p);
    }
    if let Some(orig) = original {
        let mut p =
            String::from("Content-Type: message/rfc822\r\nContent-Disposition: attachment\r\n\r\n");
        p += &String::from_utf8_lossy(&crate::pgp::crlf(orig));
        parts.push(p);
    }
    let boundary = {
        let mut n = 0usize;
        loop {
            let b = format!("=-rmut-mixed-{}-{n}", std::process::id());
            if !parts.iter().any(|p| p.contains(&b)) {
                break b;
            }
            n += 1;
        }
    };
    let mut out = format!("Content-Type: multipart/mixed; boundary=\"{boundary}\"\r\n\r\n");
    for p in &parts {
        out += &format!("--{boundary}\r\n");
        out += p;
        if !out.ends_with("\r\n") {
            out += "\r\n";
        }
    }
    out += &format!("--{boundary}--\r\n");
    Ok(out)
}

/// Message text for a bounce: the original with a fresh Resent-* block
/// prepended, per RFC 5322 (the newest resend goes first).
pub fn bounce_text(original: &[u8], from: &str, to: &str, date: &str, msg_id: &str) -> String {
    format!(
        "Resent-From: {from}\r\nResent-Date: {date}\r\nResent-Message-ID: {msg_id}\r\nResent-To: {to}\r\n{}",
        String::from_utf8_lossy(original),
    )
}

/// First address in an RFC 5322 address field, without display name,
/// e.g. the SMTP envelope sender from a From line.
/// The posting address from a List-Post header value:
/// `<mailto:dev@example.com>`, with the RFC 2369 `NO` meaning the list
/// takes no posts. Extra mailto parameters are dropped.
pub fn list_post_address(value: &str) -> Option<String> {
    let value = value.trim();
    if value.eq_ignore_ascii_case("NO") {
        return None;
    }
    let start = value.to_ascii_lowercase().find("mailto:")? + "mailto:".len();
    let rest = &value[start..];
    let addr = rest
        .split(['>', '?', ',', ' '])
        .next()
        .unwrap_or(rest)
        .trim();
    (!addr.is_empty()).then(|| addr.to_string())
}

/// mutt's $followup_to: the Mail-Followup-To for a message going to a
/// mailing list. Every recipient goes in; your own address is left out
/// when you are subscribed (the list copy is the one you will get) and
/// kept when you are not.
pub fn followup_to(to: &str, cc: &str, me: Me, subscribed: bool, my_from: &str) -> String {
    let mut out: Vec<String> = Vec::new();
    let mut push = |single: &mailparse::SingleInfo| {
        if subscribed && me.is_me(&single.addr) {
            return;
        }
        let written = match &single.display_name {
            Some(name) if !name.trim().is_empty() => format!("{name} <{}>", single.addr),
            _ => single.addr.clone(),
        };
        if !out.iter().any(|a| a == &written) {
            out.push(written);
        }
    };
    for field in [to, cc] {
        let Ok(list) = mailparse::addrparse(field) else {
            continue;
        };
        for addr in list.iter() {
            match addr {
                mailparse::MailAddr::Single(single) => push(single),
                mailparse::MailAddr::Group(group) => group.addrs.iter().for_each(&mut push),
            }
        }
    }
    if !subscribed
        && let Some(from) = bare_address(my_from)
        && !out
            .iter()
            .any(|a| bare_address(a).is_some_and(|b| b == from))
    {
        out.push(my_from.trim().to_string());
    }
    out.join(", ")
}

/// The Cc a group reply gets: everyone the original named in To and
/// Cc, written the way they were written, minus anyone already in
/// `to` (the sender, normally) and, unless mutt's $metoo is set, minus
/// me. Duplicates collapse on the bare address, so a person listed in
/// both To and Cc appears once.
pub fn group_recipients(orig_to: &str, orig_cc: &str, to: &str, me: Me, metoo: bool) -> String {
    let mut seen: Vec<String> = addresses(to).iter().map(|a| a.to_lowercase()).collect();
    let mut out: Vec<String> = Vec::new();
    let mut push = |single: &mailparse::SingleInfo| {
        let bare = single.addr.to_lowercase();
        if seen.contains(&bare) || (!metoo && me.is_me(&bare)) {
            return;
        }
        seen.push(bare);
        out.push(match &single.display_name {
            Some(name) if !name.trim().is_empty() => format!("{name} <{}>", single.addr),
            _ => single.addr.clone(),
        });
    };
    for field in [orig_to, orig_cc] {
        let Ok(list) = mailparse::addrparse(field) else {
            continue;
        };
        for addr in list.iter() {
            match addr {
                mailparse::MailAddr::Single(single) => push(single),
                mailparse::MailAddr::Group(group) => group.addrs.iter().for_each(&mut push),
            }
        }
    }
    out.join(", ")
}

/// A draft seen as a message, so hook patterns (`~t`, `~c`, `~s`,
/// `~f`, ...) can be matched against outgoing mail the way mutt
/// matches fcc-hook. `path` is where the body lives, so `~b` and `~h`
/// still have something to read; the date is now, since the draft has
/// no Date header yet. Bcc joins the Cc
/// addresses, so a hook on `~c` sees a blind recipient too.
pub fn draft_envelope(text: &str, path: &Path) -> crate::message::Envelope {
    let (head, body) = text.split_once("\n\n").unwrap_or((text.trim_end(), ""));
    let header = |name: &str| -> String {
        head.lines()
            .filter_map(|l| {
                let (k, v) = l.split_once(':')?;
                k.trim().eq_ignore_ascii_case(name).then(|| v.trim())
            })
            .collect::<Vec<_>>()
            .join(", ")
    };
    let bare = |field: &str| -> Vec<String> {
        addresses(field).iter().map(|a| a.to_lowercase()).collect()
    };
    let from_full = header("From");
    crate::message::Envelope {
        file: crate::maildir::MailFile {
            path: path.to_path_buf(),
            is_new: false,
            flags: crate::maildir::Flags {
                seen: true,
                ..Default::default()
            },
            size: text.len() as u64,
        },
        from: crate::message::short_from(&from_full),
        from_full,
        subject: header("Subject"),
        date: Local::now().timestamp(),
        msg_id: None,
        references: Vec::new(),
        tagged: false,
        to: bare(&header("To")),
        cc: [header("Cc"), header("Bcc")]
            .iter()
            .flat_map(|f| bare(f))
            .collect(),
        lines: Some(body.lines().count()),
        list: None,
        label: None,
        broken: false,
    }
}

/// mutt's `my_hdr`: extra header lines that go on every draft. An
/// entry naming a header the draft already carries replaces it, so
/// `my_hdr From:` and `my_hdr Reply-To:` win over what rmut chose;
/// To, Cc and Bcc instead gain the address, like mutt, so a standing
/// `my_hdr Bcc: me@example.com` cannot erase a reply's recipients.
/// Malformed entries (no colon, no name) are ignored.
pub fn apply_my_hdr(text: &str, my_hdr: &[String]) -> String {
    if my_hdr.is_empty() {
        return text.to_string();
    }
    let (head, body) = match text.split_once("\n\n") {
        Some((head, body)) => (head, body),
        None => (text.trim_end(), ""),
    };
    let mut lines: Vec<String> = head.lines().map(String::from).collect();
    for entry in my_hdr {
        let Some((name, value)) = entry.split_once(':') else {
            continue;
        };
        let (name, value) = (name.trim(), value.trim());
        if name.is_empty() {
            continue;
        }
        let at = lines.iter().position(|l| {
            l.get(..name.len())
                .is_some_and(|k| k.eq_ignore_ascii_case(name))
                && l.as_bytes().get(name.len()) == Some(&b':')
        });
        let addressy = ["to", "cc", "bcc"].contains(&name.to_lowercase().as_str());
        match at {
            Some(i) if addressy => {
                let old = lines[i]
                    .split_once(':')
                    .map(|(_, v)| v.trim().to_string())
                    .unwrap_or_default();
                lines[i] = if old.is_empty() {
                    format!("{name}: {value}")
                } else {
                    format!("{name}: {old}, {value}")
                };
            }
            Some(i) => lines[i] = format!("{name}: {value}"),
            None => lines.push(format!("{name}: {value}")),
        }
    }
    format!("{}\n\n{body}", lines.join("\n"))
}

pub fn bare_address(field: &str) -> Option<String> {
    match mailparse::addrparse(field)
        .ok()?
        .into_inner()
        .into_iter()
        .next()?
    {
        mailparse::MailAddr::Single(single) => Some(single.addr),
        mailparse::MailAddr::Group(group) => group.addrs.first().map(|a| a.addr.clone()),
    }
}

/// The SMTP envelope sender: the bare address of the From header.
pub fn from_address(text: &str) -> Option<String> {
    let mail = mailparse::parse_mail(text.as_bytes()).ok()?;
    bare_address(&crate::rfc2047::first(&mail.get_headers(), "From")?)
}

fn field_addresses(value: &str, out: &mut Vec<String>) {
    let Ok(list) = mailparse::addrparse(value) else {
        return;
    };
    for addr in list.iter() {
        match addr {
            mailparse::MailAddr::Single(single) => out.push(single.addr.clone()),
            mailparse::MailAddr::Group(group) => {
                out.extend(group.addrs.iter().map(|a| a.addr.clone()));
            }
        }
    }
}

/// Every bare address in an RFC 5322 address field.
pub fn addresses(field: &str) -> Vec<String> {
    let mut out = Vec::new();
    field_addresses(field, &mut out);
    out
}

/// mutt's reverse_name: the first of my addresses (`alternates`
/// included) the original was addressed to, in the form it appeared:
/// the display name from the To/Cc header is kept.
pub fn reverse_from(orig_to: &str, orig_cc: &str, me: Me, realname: bool) -> Option<String> {
    let mine = |single: &mailparse::SingleInfo| -> Option<String> {
        if !me.is_me(&single.addr) {
            return None;
        }
        Some(match &single.display_name {
            // mutt's $reverse_realname off: the address is theirs,
            // the name stays whatever the identity says.
            Some(name) if realname && !name.trim().is_empty() => {
                format!("{name} <{}>", single.addr)
            }
            _ => single.addr.clone(),
        })
    };
    for field in [orig_to, orig_cc] {
        let Ok(list) = mailparse::addrparse(field) else {
            continue;
        };
        for addr in list.iter() {
            match addr {
                mailparse::MailAddr::Single(single) => {
                    if let Some(from) = mine(single) {
                        return Some(from);
                    }
                }
                mailparse::MailAddr::Group(group) => {
                    if let Some(from) = group.addrs.iter().find_map(&mine) {
                        return Some(from);
                    }
                }
            }
        }
    }
    None
}

/// SMTP envelope for a finalized draft: every To/Cc/Bcc address, and
/// the text with Bcc headers removed (they must not go on the wire).
pub fn smtp_envelope(text: &str) -> Result<(Vec<String>, String)> {
    let mail = mailparse::parse_mail(text.as_bytes())?;
    let mut rcpts = Vec::new();
    for header in &mail.headers {
        let key = header.get_key();
        if ["to", "cc", "bcc"].contains(&key.to_lowercase().as_str()) {
            field_addresses(&crate::rfc2047::value(header), &mut rcpts);
        }
    }
    rcpts.dedup();
    let (head, body) = text.split_once("\n\n").unwrap_or((text.trim_end(), ""));
    let mut out = String::new();
    let mut skipping = false;
    for line in head.lines() {
        if line.starts_with(' ') || line.starts_with('\t') {
            // Folded continuation belongs to the previous header.
            if skipping {
                continue;
            }
        } else {
            skipping = line
                .split_once(':')
                .is_some_and(|(k, _)| k.trim().eq_ignore_ascii_case("bcc"));
        }
        if !skipping {
            out.push_str(line);
            out.push('\n');
        }
    }
    out.push('\n');
    out.push_str(body);
    Ok((rcpts, out))
}

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

    #[test]
    fn a_markdown_body_is_text_and_html() {
        let entity = body_entity("Hi *Jane*\n", false, true);
        assert!(
            entity.starts_with("Content-Type: multipart/alternative; boundary="),
            "{entity}"
        );
        let plain = entity.find("Content-Type: text/plain").expect(&entity);
        let html = entity.find("Content-Type: text/html").expect(&entity);
        assert!(plain < html, "the plain half first: {entity}");
        assert!(
            entity.contains("\r\nHi *Jane*\r\n"),
            "the text as typed: {entity}"
        );
        assert!(entity.contains("<em>Jane</em>"), "{entity}");
        assert!(entity.contains("Content-Transfer-Encoding: quoted-printable"));
        // Off, it is the plain part alone.
        assert!(body_entity("Hi\n", false, false).starts_with("Content-Type: text/plain"));
    }

    #[test]
    fn quoted_printable_keeps_lines_short_and_bytes_safe() {
        let long = "word ".repeat(40);
        let qp = quoted_printable(&format!("{long}\na=b \u{17e}luť"));
        assert!(
            qp.lines().all(|l| l.trim_end_matches('\r').len() <= 76),
            "{qp}"
        );
        assert!(qp.contains("=\r\n"), "a soft break: {qp}");
        // The trailing space of the long line is encoded, = is, and
        // the UTF-8 bytes are.
        assert!(qp.contains("=20\r\na=3Db =C5=BElu=C5=A5"), "{qp}");
        let back: String = qp.replace("=\r\n", "");
        assert!(back.starts_with("word word"), "{back}");
    }

    #[test]
    fn the_markdown_header_is_read_and_taken_off() {
        let (text, said) = take_markdown("To: a@x\nX-Rmut-Markdown: yes\nSubject: s\n\nbody\n");
        assert_eq!(said, Some(true));
        assert_eq!(text, "To: a@x\nSubject: s\n\nbody\n");
        let (_, said) = take_markdown("To: a@x\nx-rmut-markdown: no\n\nbody\n");
        assert_eq!(said, Some(false));
        let (text, said) = take_markdown("To: a@x\n\nX-Rmut-Markdown: yes in the body\n");
        assert_eq!(said, None, "only the header block counts");
        assert!(text.contains("in the body"));
    }

    #[test]
    fn a_forward_can_come_in_quoted() {
        let plain = forward_body("Ann <ann@x>", 0, "hi", "one\ntwo\n", None);
        assert!(plain.contains("\none\ntwo\n"), "{plain}");
        let quoted = forward_body("Ann <ann@x>", 0, "hi", "one\ntwo\n", Some("> "));
        assert!(quoted.contains("\n> one\n> two\n"), "{quoted}");
        // The markers are rmut's words, so they stay flush.
        assert!(quoted.starts_with("----- Forwarded message from Ann <ann@x> -----"));
        assert!(quoted.ends_with("----- End forwarded message -----\n"));
    }

    #[test]
    fn the_signature_sits_under_a_dashes_line() {
        let body = with_signature("hello\n", "Ann\nx.example", true);
        assert_eq!(body, "hello\n\n-- \nAnn\nx.example\n");
        // nosig_dashes: the text alone, still a blank line down.
        assert_eq!(with_signature("hello\n", "Ann", false), "hello\n\nAnn\n");
        // An empty draft starts with the blank line all the same.
        assert_eq!(with_signature("", "Ann", true), "\n-- \nAnn\n");
    }

    #[test]
    fn a_signature_comes_from_a_file_or_a_command() {
        let dir = std::env::temp_dir().join(format!("rmut-sig-{}", std::process::id()));
        std::fs::create_dir_all(&dir).unwrap();
        let file = dir.join("signature");
        std::fs::write(&file, "Ann\n\n\n").unwrap();
        // Trailing blank lines are the file's, not the signature's.
        assert_eq!(
            signature_text(file.to_str().unwrap()).as_deref(),
            Some("Ann")
        );
        assert_eq!(signature_text("echo hello|").as_deref(), Some("hello"));
        // Nothing readable is no signature, not an empty one.
        assert_eq!(signature_text(dir.join("gone").to_str().unwrap()), None);
        assert_eq!(signature_text("  "), None);
        assert_eq!(signature_text("true|"), None);
        std::fs::remove_dir_all(&dir).unwrap();
    }

    #[test]
    fn list_post_addresses() {
        assert_eq!(
            list_post_address("<mailto:dev@example.com>").as_deref(),
            Some("dev@example.com")
        );
        assert_eq!(
            list_post_address("<mailto:dev@example.com?subject=help>").as_deref(),
            Some("dev@example.com")
        );
        assert_eq!(
            list_post_address("NOTE: <mailto:dev@example.com>, <http://x/post>").as_deref(),
            Some("dev@example.com")
        );
        // RFC 2369: a list that takes no posts, and a non-mail method.
        assert_eq!(list_post_address("NO"), None);
        assert_eq!(list_post_address("<http://example.com/post>"), None);
    }

    #[test]
    fn followup_to_drops_me_only_when_subscribed() {
        let addrs = vec!["alex@example.com".to_string()];
        let me = Me::addresses(&addrs);
        let subscribed = followup_to(
            "dev@example.com, Alex <alex@example.com>",
            "",
            me,
            true,
            "Alex <alex@example.com>",
        );
        assert_eq!(subscribed, "dev@example.com");
        let unsubscribed = followup_to(
            "dev@example.com",
            "petr@example.com",
            me,
            false,
            "Alex <alex@example.com>",
        );
        assert_eq!(
            unsubscribed,
            "dev@example.com, petr@example.com, Alex <alex@example.com>"
        );
        // Already listed: no second copy of my address.
        let once = followup_to(
            "dev@example.com, alex@example.com",
            "",
            me,
            false,
            "Alex <alex@example.com>",
        );
        assert_eq!(once, "dev@example.com, alex@example.com");
    }

    #[test]
    fn group_reply_drops_me_and_the_sender() {
        let addrs = vec!["alex@example.com".to_string()];
        let alternates = vec![crate::pattern::Matcher::new("^jp@old\\.example\\.com$")];
        let me = Me::new(&addrs, &alternates);
        let cc = group_recipients(
            "Team <team@example.com>, Alex <alex@example.com>, jp@old.example.com",
            "boss@example.com, team@example.com",
            "Petr <petr@example.com>",
            me,
            false,
        );
        // My exact address and my alternate are gone, the sender in To
        // is gone, and team@ appears once despite being listed twice.
        assert_eq!(cc, "Team <team@example.com>, boss@example.com");
        // $metoo keeps me on the copy.
        let cc = group_recipients(
            "team@example.com, alex@example.com",
            "",
            "petr@example.com",
            me,
            true,
        );
        assert_eq!(cc, "team@example.com, alex@example.com");
    }

    #[test]
    fn text_flowed_declares_and_stuffs_the_body() {
        let entity = text_entity("plain\n>looks quoted\n", true);
        assert!(
            entity.starts_with("Content-Type: text/plain; charset=utf-8; format=flowed\r\n"),
            "{entity}"
        );
        assert!(
            entity.ends_with("plain\r\n >looks quoted\r\n"),
            "{entity:?}"
        );
        // Off, the part is what it always was.
        let plain = text_entity("plain\n>looks quoted\n", false);
        assert!(plain.starts_with("Content-Type: text/plain; charset=utf-8\r\n"));
        assert!(plain.ends_with("plain\r\n>looks quoted\r\n"), "{plain:?}");
    }

    #[test]
    fn flow_plain_declares_an_unwrapped_draft() {
        let draft = "From: a@x\nTo: b@x\nSubject: s\n\n>quoted line\n";
        let out = flow_plain(draft);
        assert!(out.contains("Content-Type: text/plain; charset=utf-8; format=flowed\n"));
        assert!(out.ends_with("\n\n >quoted line\n"), "{out:?}");
        // A draft that declares its own type is left alone.
        let typed = "From: a@x\nContent-Type: text/x-diff\n\nbody\n";
        assert_eq!(flow_plain(typed), typed);
    }

    #[test]
    fn draft_envelope_reads_the_header_block() {
        let draft = "From: Jane Doe <jane@example.com>\n\
                     To: Bob <BOB@work.example.com>, team@x\n\
                     Cc: boss@x\n\
                     Bcc: archive@x\n\
                     Subject: quarterly\n\n\
                     two\nlines\n";
        let env = draft_envelope(draft, std::path::Path::new("/tmp/draft"));
        assert_eq!(env.subject, "quarterly");
        assert_eq!(env.from_full, "Jane Doe <jane@example.com>");
        assert_eq!(env.to, ["bob@work.example.com", "team@x"]);
        // Bcc joins Cc, so a hook on ~c sees a blind recipient too.
        assert_eq!(env.cc, ["boss@x", "archive@x"]);
        assert_eq!(env.lines, Some(2));
        let hit = |p: &str| {
            crate::pattern::matches_in(
                &crate::pattern::parse(p).unwrap(),
                &env,
                crate::pattern::Scope::default(),
                None,
            )
        };
        assert!(hit("~t @work\\.example\\.com"));
        assert!(hit("~c archive@"));
        assert!(hit("~A"));
        assert!(!hit("~t nobody@"));
    }

    #[test]
    fn my_hdr_merges_into_the_draft_head() {
        let draft = "From: jane@example.com\nTo: bob@x\nSubject: s\n\nbody\n";
        let merged = apply_my_hdr(
            draft,
            &[
                "Organization: Acme".to_string(),
                "From: Jane <jane@work.example.com>".into(),
                "Bcc: jane@example.com".into(),
                "To: archive@x".into(),
                "bogus".into(),
            ],
        );
        assert_eq!(
            merged,
            "From: Jane <jane@work.example.com>\n\
             To: bob@x, archive@x\n\
             Subject: s\n\
             Organization: Acme\n\
             Bcc: jane@example.com\n\
             \nbody\n"
        );
        // Nothing configured: the draft is untouched.
        assert_eq!(apply_my_hdr(draft, &[]), draft);
    }

    /// A message sent at a known moment, for the format strings.
    fn quoted() -> Quoted<'static> {
        Quoted {
            from: "Jane Doe <jane@example.com>",
            subject: "Lunch",
            message_id: Some("<m1@example.com>"),
            // 2024-03-11 10:00:00 UTC.
            date: 1_710_151_200,
        }
    }

    #[test]
    fn subjects_do_not_stack_prefixes() {
        let re = default_reply_regexp();
        assert_eq!(reply_subject("Lunch", &re), "Re: Lunch");
        assert_eq!(reply_subject("RE: Lunch", &re), "Re: Lunch");
        assert_eq!(reply_subject("Re[2]: Lunch", &re), "Re: Lunch");
        // A locale's own prefixes, and mutt's smart case: an
        // uppercase letter in the regex makes it exact.
        let aw = reply_regexp("^(re|aw|sv):[ \t]*").unwrap();
        assert_eq!(reply_subject("AW: Lunch", &aw), "Re: Lunch");
        let exact = reply_regexp("^(Re):[ \t]*").unwrap();
        assert_eq!(reply_subject("RE: Lunch", &exact), "Re: RE: Lunch");
        assert_eq!(
            forward_subject(DEFAULT_FORWARD_FORMAT, &quoted()),
            "[jane@example.com: Lunch]"
        );
    }

    #[test]
    fn quote_prefixes_every_line_with_the_indent_string() {
        assert_eq!(
            quote("On X, Y wrote:", DEFAULT_INDENT, "a\nb"),
            "On X, Y wrote:\n> a\n> b\n"
        );
        assert_eq!(quote("head", "| ", "a"), "head\n| a\n");
    }

    #[test]
    fn an_attribution_says_who_and_when() {
        let m = quoted();
        // The default names the author and the date it was sent.
        let line = attribution(DEFAULT_ATTRIBUTION, &m);
        assert!(line.starts_with("On "), "{line}");
        assert!(line.ends_with(", Jane Doe wrote:"), "{line}");
        // Every specifier mutt's does, and an unknown one stays put.
        assert_eq!(render_quoted("%a", &m), "jane@example.com");
        assert_eq!(render_quoted("%n", &m), "Jane Doe");
        assert_eq!(render_quoted("%f", &m), "Jane Doe <jane@example.com>");
        assert_eq!(render_quoted("%s", &m), "Lunch");
        assert_eq!(render_quoted("%i", &m), "m1@example.com");
        assert_eq!(render_quoted("100%%", &m), "100%");
        assert_eq!(render_quoted("%q", &m), "%q");
        // %{...} is the date in the caller's own words.
        assert_eq!(render_quoted("%{%Y}", &m), "2024");
        assert_eq!(render_quoted("[%{%Y}] %s", &m), "[2024] Lunch");
    }

    #[test]
    fn a_name_falls_back_to_the_address() {
        let m = Quoted {
            from: "bare@example.com",
            subject: "x",
            message_id: None,
            date: 0,
        };
        assert_eq!(render_quoted("%n", &m), "bare@example.com");
        assert_eq!(render_quoted("%i", &m), "");
    }

    #[test]
    fn draft_text_skips_empty_optional_headers() {
        let text = draft_text(
            &DraftHeaders {
                from: None,
                to: "a@x".into(),
                cc: Some("".into()),
                subject: "s".into(),
                in_reply_to: None,
                references: None,
            },
            "hi",
        );
        assert_eq!(text, "To: a@x\nSubject: s\n\nhi\n");
        let text = draft_text(
            &DraftHeaders {
                from: Some("Jane Work <jane@work.example.com>".into()),
                to: "a@x".into(),
                cc: None,
                subject: "s".into(),
                in_reply_to: None,
                references: None,
            },
            "hi",
        );
        assert!(text.starts_with("From: Jane Work <jane@work.example.com>\nTo: a@x\n"));
    }

    #[test]
    fn reverse_from_finds_my_address_as_it_appeared() {
        let addrs = vec!["jane@example.com".to_string(), "old@example.com".into()];
        let me = Me::addresses(&addrs);
        // Display name kept, match case-insensitive.
        assert_eq!(
            reverse_from("Boss Me <Jane@example.com>, bob@y", "", me, true).as_deref(),
            Some("Boss Me <Jane@example.com>")
        );
        // mutt's $reverse_realname off: the address alone comes over.
        assert_eq!(
            reverse_from("Boss Me <Jane@example.com>, bob@y", "", me, false).as_deref(),
            Some("Jane@example.com")
        );
        // Bare address stays bare; Cc is searched after To.
        assert_eq!(
            reverse_from("bob@y", "old@example.com", me, true).as_deref(),
            Some("old@example.com")
        );
        assert_eq!(reverse_from("bob@y, eve@z", "", me, true), None);
        assert_eq!(reverse_from("", "", me, true), None);
    }

    #[test]
    fn finalize_adds_missing_headers_once() {
        let draft = "To: a@x\nSubject: s\n\nbody\n";
        let out = finalize(draft, "me@host", "<id@host>", "DATE").unwrap();
        assert!(out.contains("From: me@host"));
        assert!(out.contains("Message-ID: <id@host>"));
        assert!(out.contains("Date: DATE"));
        assert!(out.ends_with("\n\nbody\n"));
        // User-provided From wins.
        let draft2 = "To: a@x\nFrom: custom@x\n\nbody\n";
        let out2 = finalize(draft2, "me@host", "<i>", "D").unwrap();
        assert!(out2.contains("From: custom@x"));
        assert!(!out2.contains("me@host"));
    }

    #[test]
    fn user_agent_and_signature_placement() {
        let draft = "To: a@b\n\nhi";
        let plain = finalize_with(draft, "me@x", "<id@h>", "Mon", false).unwrap();
        assert!(!plain.contains("User-Agent"));
        let ua = finalize_with(draft, "me@x", "<id@h>", "Mon", true).unwrap();
        assert!(ua.contains(&user_agent_header()), "{ua}");
        // A draft that already carries one is left alone.
        let own = finalize_with(
            "To: a@b\nUser-Agent: mine\n\nhi",
            "me@x",
            "<id@h>",
            "Mon",
            true,
        )
        .unwrap();
        assert_eq!(own.matches("User-Agent").count(), 1, "{own}");
        // sig_on_top puts the signature above the body.
        let below = with_signature_at("the reply", "Jane", true, false);
        assert!(below.trim_end().ends_with("Jane"), "{below}");
        let above = with_signature_at("the reply", "Jane", true, true);
        assert!(above.starts_with("-- \nJane\n"), "{above}");
        assert!(above.trim_end().ends_with("the reply"), "{above}");
    }

    #[test]
    fn finalize_rejects_missing_recipients() {
        assert!(finalize("Subject: s\n\nbody", "f", "<i>", "d").is_err());
        assert!(finalize("To:   \nSubject: s\n\nbody", "f", "<i>", "d").is_err());
        assert!(finalize("Bcc: a@x\n\nbody", "f", "<i>", "d").is_ok());
    }

    #[test]
    fn bare_address_drops_display_name() {
        assert_eq!(
            bare_address("Jane <jane@x.org>").as_deref(),
            Some("jane@x.org")
        );
        assert_eq!(bare_address("jane@x.org").as_deref(), Some("jane@x.org"));
        assert_eq!(bare_address(""), None);
    }

    #[test]
    fn smtp_envelope_collects_rcpts_and_strips_bcc() {
        let text =
            "To: Alice <a@x>, b@y\nCc: c@z\nBcc: hidden@q,\n also-hidden@q\nSubject: s\n\nbody\n";
        let (rcpts, out) = smtp_envelope(text).unwrap();
        assert_eq!(
            rcpts,
            vec!["a@x", "b@y", "c@z", "hidden@q", "also-hidden@q"]
        );
        assert!(!out.to_lowercase().contains("bcc"));
        assert!(!out.contains("hidden@q"));
        assert!(out.contains("To: Alice <a@x>, b@y\n"));
        assert!(out.ends_with("\n\nbody\n"));
    }

    #[test]
    fn extract_attachments_takes_the_pseudo_headers_out() {
        let draft = "To: a@x\nAttach: /tmp/report.pdf the Q2 numbers\n\
                     attach: \"/tmp/two words.png\"\nAttach:\nSubject: s\n\nbody\n";
        let (out, files) = extract_attachments(draft);
        assert_eq!(out, "To: a@x\nSubject: s\n\nbody\n");
        assert_eq!(files.len(), 2);
        assert_eq!(files[0].path, PathBuf::from("/tmp/report.pdf"));
        assert_eq!(files[0].description.as_deref(), Some("the Q2 numbers"));
        assert_eq!(files[1].path, PathBuf::from("/tmp/two words.png"));
        assert_eq!(files[1].description, None);
    }

    #[test]
    fn attach_lines_carry_a_type_override() {
        let draft = "Attach: /tmp/x.bin application/x-custom raw dump\n\
                     Attach: /tmp/y.txt see notes\n\n";
        let (_, files) = extract_attachments(draft);
        assert_eq!(files[0].mime.as_deref(), Some("application/x-custom"));
        assert_eq!(files[0].description.as_deref(), Some("raw dump"));
        // "see notes" is not a type/subtype token.
        assert_eq!(files[1].mime, None);
        assert_eq!(files[1].description.as_deref(), Some("see notes"));
        // attach_line writes back what extract_attachments reads.
        let line = attach_line(&files[0]);
        assert_eq!(line, "Attach: /tmp/x.bin application/x-custom raw dump");
        let (_, roundtrip) = extract_attachments(&format!("{line}\n\n"));
        assert_eq!(roundtrip[0].mime.as_deref(), Some("application/x-custom"));
        // A quoted path with spaces survives too.
        let spaced = Attachment {
            path: PathBuf::from("/tmp/two words.png"),
            mime: Some("image/png".into()),
            description: None,
            name: None,
            inline: false,
            unlink: false,
        };
        let (_, files) = extract_attachments(&format!("{}\n\n", attach_line(&spaced)));
        assert_eq!(files[0].path, PathBuf::from("/tmp/two words.png"));
        assert_eq!(files[0].mime.as_deref(), Some("image/png"));
    }

    #[test]
    fn extract_attachments_leaves_plain_drafts_alone() {
        let draft = "To: a@x\nSubject: s\n\nAttach: not a header, body text\n";
        let (out, files) = extract_attachments(draft);
        assert_eq!(out, draft);
        assert!(files.is_empty());
    }

    #[test]
    fn mixed_entity_encodes_files_and_original() {
        use mailparse::MailHeaderMap;
        let dir = std::env::temp_dir().join(format!("rmut-attach-test-{}", std::process::id()));
        std::fs::create_dir_all(&dir).unwrap();
        let blob: Vec<u8> = (0..=255u8).collect();
        std::fs::write(dir.join("blob.bin"), &blob).unwrap();
        let files = [Attachment {
            path: dir.join("blob.bin"),
            mime: None,
            description: Some("raw bytes".into()),
            name: None,
            inline: false,
            unlink: false,
        }];
        let orig = b"From: jane@x\r\nSubject: hi\r\n\r\noriginal body\r\n";
        let entity = mixed_entity("see attached", &files, Some(orig), false, false).unwrap();
        let mail = mailparse::parse_mail(entity.as_bytes()).unwrap();
        assert_eq!(mail.ctype.mimetype, "multipart/mixed");
        assert_eq!(mail.subparts.len(), 3);
        assert_eq!(mail.subparts[0].get_body().unwrap().trim(), "see attached");
        let file = &mail.subparts[1];
        assert_eq!(file.ctype.mimetype, "application/octet-stream");
        assert_eq!(file.get_body_raw().unwrap(), blob);
        let disp = file.get_headers().get_first_value("Content-Disposition");
        assert!(disp.unwrap().contains("filename=\"blob.bin\""));
        assert_eq!(
            file.get_headers()
                .get_first_value("Content-Description")
                .as_deref(),
            Some("raw bytes")
        );
        assert_eq!(mail.subparts[2].ctype.mimetype, "message/rfc822");
        assert!(
            mail.subparts[2]
                .get_body()
                .unwrap()
                .contains("original body")
        );
        std::fs::remove_dir_all(&dir).unwrap();
    }

    #[test]
    fn mixed_entity_reports_a_missing_file() {
        let files = [Attachment {
            path: PathBuf::from("/nonexistent/nope.pdf"),
            mime: None,
            description: None,
            name: None,
            inline: false,
            unlink: false,
        }];
        let err = mixed_entity("hi", &files, None, false, false).unwrap_err();
        assert!(err.to_string().contains("/nonexistent/nope.pdf"));
    }

    #[test]
    fn bounce_text_prepends_resent_headers() {
        let orig = b"From: jane@x\nSubject: hi\n\nbody\n";
        let out = bounce_text(orig, "Me <me@x>", "bob@y", "DATE", "<id@x>");
        assert!(out.starts_with("Resent-From: Me <me@x>\r\n"));
        assert!(out.contains("Resent-Date: DATE\r\n"));
        assert!(out.contains("Resent-To: bob@y\r\n"));
        assert!(out.ends_with("From: jane@x\nSubject: hi\n\nbody\n"));
    }

    #[test]
    fn smtp_envelope_without_bcc_is_unchanged() {
        let text = "To: a@x\nSubject: s\n\nbody\n";
        let (rcpts, out) = smtp_envelope(text).unwrap();
        assert_eq!(rcpts, vec!["a@x"]);
        assert_eq!(out, text);
    }

    #[test]
    fn attach_line_options_round_trip() {
        let mut a = Attachment::of(PathBuf::from("/tmp/q2 report.pdf"));
        a.mime = Some("application/pdf".into());
        a.name = Some("report.pdf".into());
        a.inline = true;
        a.unlink = true;
        a.description = Some("the Q2 numbers".into());
        let line = attach_line(&a);
        assert_eq!(
            line,
            "Attach: \"/tmp/q2 report.pdf\" application/pdf @name=\"report.pdf\" @inline @unlink the Q2 numbers"
        );
        let (_, back) = extract_attachments(&format!("To: x\n{line}\n\nbody"));
        assert_eq!(back.len(), 1);
        let b = &back[0];
        assert_eq!(b.path, a.path);
        assert_eq!(b.mime.as_deref(), Some("application/pdf"));
        assert_eq!(b.name.as_deref(), Some("report.pdf"));
        assert!(b.inline && b.unlink);
        assert_eq!(b.description.as_deref(), Some("the Q2 numbers"));
        // A line without options reads as before, and a description
        // starting with @ is a description.
        let (_, plain) = extract_attachments("Attach: /tmp/a.txt text/plain @home notes\n\n");
        assert!(!plain[0].inline && plain[0].name.is_none());
        assert_eq!(plain[0].description.as_deref(), Some("@home notes"));
    }

    #[test]
    fn mixed_entity_honours_name_disposition_and_rfc822() {
        let dir = std::env::temp_dir().join(format!("rmut-attach-opts-{}", std::process::id()));
        std::fs::create_dir_all(&dir).unwrap();
        let file = dir.join("data.bin");
        std::fs::write(&file, b"xyz").unwrap();
        let msg = dir.join("1.host:2,S");
        std::fs::write(&msg, "From: a@x\nSubject: inner\n\nhello\n").unwrap();
        let mut a = Attachment::of(file.clone());
        a.name = Some("renamed.bin".into());
        a.inline = true;
        let mut m = Attachment::of(msg.clone());
        m.mime = Some("message/rfc822".into());
        let entity = mixed_entity("see attached", &[a, m], None, false, false).unwrap();
        assert!(
            entity.contains("Content-Disposition: inline; filename=\"renamed.bin\""),
            "{entity}"
        );
        assert!(
            entity.contains(
                "Content-Type: message/rfc822\r\nContent-Disposition: attachment\r\n\r\nFrom: a@x"
            ),
            "{entity}"
        );
        assert!(
            !entity.contains("filename=\"1.host"),
            "a message has no filename"
        );
        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn attachment_names_and_descriptions_stay_one_header() {
        let dir = tempfile::tempdir().unwrap();
        let file = dir.path().join("x.bin");
        std::fs::write(&file, b"xyz").unwrap();
        let named = |name: &str, desc: &str| {
            let mut a = Attachment::of(file.clone());
            a.name = Some(name.into());
            a.description = Some(desc.into());
            let entity = mixed_entity("hi", &[a], None, false, false).unwrap();
            let raw = format!("MIME-Version: 1.0\r\n{entity}");
            let mail = mailparse::parse_mail(raw.as_bytes()).unwrap();
            let part = &mail.subparts[1];
            let filename = part.get_content_disposition().params["filename"].clone();
            let headers: Vec<String> = part.headers.iter().map(|h| h.get_key()).collect();
            use mailparse::MailHeaderMap as _;
            (
                filename,
                part.headers.get_first_value("Content-Description"),
                headers,
            )
        };
        let (name, desc, headers) = named("say \"hi\".txt", "ok\r\nContent-Type: text/html");
        assert_eq!(name, "say \"hi\".txt");
        assert_eq!(desc.as_deref(), Some("ok  Content-Type: text/html"));
        assert_eq!(headers.iter().filter(|k| *k == "Content-Type").count(), 1);
        let (name, _, _) = named("Příloha č. 1.pdf", "d");
        assert_eq!(name, "Příloha č. 1.pdf");
    }
}