tuigram-cli 1.0.0

Telegram in your terminal: a fast, vim-style Telegram client built on TDLib
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
//! Thin wrapper around one TDLib client.
//!
//! A background thread pulls everything out of TDLib: updates are forwarded to
//! the app as [`TgEvent`]s, and responses complete the pending request futures.
//! Each request runs as its own tokio task so the UI never waits on the network.

use std::ffi::{CStr, CString, c_char};
use std::future::Future;
use std::path::Path;
use std::sync::Arc;
use std::sync::atomic::{AtomicI32, Ordering};
use std::time::Duration;

use anyhow::{Result, anyhow, bail};
use serde_json::json;
use tdlib_rs::{enums, functions, types};
use tokio::sync::mpsc::UnboundedSender;
use tokio::sync::mpsc::error::SendError;

use crate::chats::{Badge, Peer};
use crate::config::{ApiKeys, Config};
use crate::reactions::{self, Available, ReactionKind};
use crate::stickers::{Source, Sticker};

pub enum TgEvent {
    Update(Box<enums::Update>),
    /// A request failed. The text is TDLib's, e.g. `PHONE_CODE_INVALID`.
    Error(String),
    /// A `loadChats` call finished; `all` is true once every chat is loaded.
    /// `failed` if TDLib answered with an error (sent before as `Error`).
    ChatsLoaded {
        all: bool,
        failed: bool,
    },
    /// A page of history, newest first. `None` if the request failed.
    History {
        chat_id: i64,
        page: Page,
        messages: Option<Vec<types::Message>>,
    },
    /// A page of message search results. `None` if the request failed.
    Found {
        chat_id: i64,
        query: String,
        found: Option<Found>,
    },
    /// The message that `message_id` replies to; `None` if TDLib couldn't find it.
    Replied {
        chat_id: i64,
        message_id: i64,
        replied: Option<Box<types::Message>>,
    },
    /// Who a message can be deleted for; `None` if TDLib couldn't say.
    Deletable {
        chat_id: i64,
        message_id: i64,
        deletable: Option<Deletable>,
    },
    /// Whether a message can be edited; `None` if TDLib couldn't say. If it
    /// can, its words as Markdown, unless they couldn't be had.
    Editable {
        chat_id: i64,
        message_id: i64,
        editable: Option<bool>,
        text: Option<EditText>,
    },
    /// What reactions a message can get; `None` if TDLib couldn't say.
    Reactions {
        chat_id: i64,
        message_id: i64,
        available: Option<Available>,
    },
    /// Stickers for a tab of the sticker panel; `None` if TDLib couldn't
    /// send them.
    Stickers {
        source: Source,
        stickers: Option<Vec<types::Sticker>>,
    },
    /// Your sticker sets, by id and title; `None` if TDLib couldn't say.
    StickerSets(Option<Vec<(i64, String)>>),
    /// Stickers found for a search in the sticker panel.
    StickersFound {
        query: String,
        stickers: Option<Vec<types::Sticker>>,
    },
    /// A download finished; `path` is `None` if it failed.
    Downloaded {
        file_id: i32,
        path: Option<String>,
    },
    /// Whom Telegram found for a search in the `s` picker: contacts by
    /// user id, and public chats. Failures find nothing.
    ChatsFound {
        query: String,
        chat_ids: Vec<i64>,
        user_ids: Vec<i64>,
    },
    /// A chat looked up to open, by what was looked up (a username, a link
    /// or a contact's name): the chat, and the message a link pointed at.
    /// Or what to say if it can't be opened.
    ChatFound {
        request: String,
        found: Result<(i64, Option<i64>), Missed>,
    },
    /// Members of a group whose name has `query` in it, for `@` completion.
    /// Failures find nobody.
    Members {
        chat_id: i64,
        query: String,
        user_ids: Vec<i64>,
    },
    /// The commands of a chat's bots, for `/` completion. Failures find
    /// none.
    Commands {
        chat_id: i64,
        commands: Vec<crate::complete::Command>,
    },
    /// Telegram took messages to forward to this chat.
    Forwarded {
        chat_id: i64,
    },
    /// You joined this public group or channel.
    Joined {
        chat_id: i64,
    },
    /// You left this group or channel.
    Left {
        chat_id: i64,
    },
    /// An invite link to a chat you're not in, to ask before joining.
    Invite {
        request: String,
        link: String,
        invite: Invite,
    },
}

/// Why a chat looked up to open wasn't opened.
#[derive(Debug, PartialEq, Eq)]
pub enum Missed {
    /// What to tell the user.
    Said(String),
    /// Nothing more to say: an invite asks to join instead.
    Quiet,
    /// A link tuigram doesn't open itself (a sticker set, a bot's start
    /// link…), which a browser can.
    Elsewhere,
}

impl From<String> for Missed {
    fn from(why: String) -> Self {
        Missed::Said(why)
    }
}

/// A chat an invite link leads to, as the link describes it.
pub struct Invite {
    pub title: String,
    pub members: i32,
    pub channel: bool,
    pub badge: Option<Badge>,
    /// You'd ask to join, and an admin lets you in.
    pub by_request: bool,
}

/// Which part of a chat's history to fetch.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Page {
    /// The newest messages.
    Latest,
    /// Messages older than this one.
    Older(i64),
    /// This message and newer ones.
    Newer(i64),
    /// This message and the ones on both sides of it.
    Around(i64),
}

/// Messages matching a search, newest first.
pub struct Found {
    pub ids: Vec<i64>,
    /// TDLib's estimate of how many match in all; -1 if unknown.
    pub total: i32,
    /// Pass as `from` to get the next page; 0 when there are no more.
    pub next_from: i64,
}

/// Who a message can be deleted for. Depends on the chat type, your rights
/// in it, and Telegram's time limits, so TDLib works it out.
#[derive(Clone, Copy)]
pub struct Deletable {
    pub for_everyone: bool,
    pub for_me: bool,
}

/// TDLib download priority, 1 (lowest) to 32. Photos on screen matter.
const DOWNLOAD_PRIORITY: i32 = 16;
/// Chat photos in the list come after photos in messages.
const QUIET_DOWNLOAD_PRIORITY: i32 = 8;
/// Chats with notifications at once. Only new ones are announced, so a few
/// is plenty; TDLib allows up to 25.
const NOTIFICATION_GROUPS: i64 = 5;

/// Stickers a search in the sticker panel asks for.
const STICKER_SEARCH_LIMIT: i32 = 100;
/// Members an `@` completion asks for.
const MEMBER_SEARCH_LIMIT: i32 = 20;
/// Contacts a search in the `s` picker asks for.
const CONTACT_SEARCH_LIMIT: i32 = 20;

/// How long [`offline_now`] gives TDLib to send it before the process ends.
const OFFLINE_GRACE: Duration = Duration::from_millis(300);

/// The client in use, for [`offline_now`]: 0 until there is one, and in
/// `--demo`.
static CLIENT: AtomicI32 = AtomicI32::new(0);

unsafe extern "C" {
    /// TDLib's synchronous entry point, for the few requests that need no
    /// client. It's linked in with tdlib-rs, which doesn't wrap it.
    fn td_execute(request: *const c_char) -> *const c_char;
    /// Sends a request without waiting for the answer. tdlib-rs wraps it in
    /// async functions, which [`offline_now`] can't count on.
    fn td_send(client_id: i32, request: *const c_char);
    /// OpenSSL's setup, from the libcrypto linked in with TDLib.
    fn OPENSSL_init_crypto(opts: u64, settings: *const std::ffi::c_void) -> std::ffi::c_int;
}

/// `OPENSSL_INIT_NO_LOAD_CONFIG`, the same in OpenSSL 1.1 and 3.
const OPENSSL_INIT_NO_LOAD_CONFIG: u64 = 0x80;

/// Keeps the OpenSSL inside TDLib from reading a configuration file. By
/// default it reads `openssl.cnf` from a folder fixed when TDLib was built,
/// which on some systems another account can create, and that file can load
/// code into tuigram, next to your session. OpenSSL sets itself up once, so
/// this must come before TDLib uses it.
fn no_openssl_config() -> Result<()> {
    // SAFETY: a plain call into the linked libcrypto with no settings; it
    // only records what to skip when OpenSSL sets itself up.
    let done = unsafe { OPENSSL_init_crypto(OPENSSL_INIT_NO_LOAD_CONFIG, std::ptr::null()) };
    anyhow::ensure!(done == 1, "OpenSSL couldn't be set up");
    Ok(())
}

/// Tells Telegram the user is offline, for the panic hook: without it,
/// others would see them online for minutes after a crash. It goes without
/// the async runtime, which may be what panicked, and waits a moment for
/// TDLib's own threads to send it. Best effort.
pub fn offline_now() {
    let client_id = CLIENT.load(Ordering::Relaxed);
    if client_id == 0 {
        return;
    }
    let request = json!({
        "@type": "setOption",
        "name": "online",
        "value": { "@type": "optionValueBoolean", "value": false },
    });
    let Ok(request) = CString::new(request.to_string()) else {
        return;
    };
    // SAFETY: `request` is a NUL-terminated string that outlives the call,
    // and TDLib takes requests from any thread.
    unsafe { td_send(client_id, request.as_ptr()) };
    std::thread::sleep(OFFLINE_GRACE);
}

/// Sends TDLib's logs (warnings and worse) to `path`. Must run before the
/// first client is created: TDLib logs to stderr until told otherwise, which
/// would print in the terminal around the TUI.
fn log_to_file(path: &Path) -> Result<()> {
    let requests = [
        json!({
            "@type": "setLogStream",
            "log_stream": {
                "@type": "logStreamFile",
                "path": path.to_string_lossy(),
                "max_file_size": 10 * 1024 * 1024,
                "redirect_stderr": false,
            },
        }),
        json!({ "@type": "setLogVerbosityLevel", "new_verbosity_level": 2 }),
    ];
    for request in requests {
        let response = execute(&request)?;
        if !response.contains(r#""@type":"ok""#) {
            bail!("TDLib log setup failed: {response}");
        }
    }
    Ok(())
}

/// Runs one of the TDLib requests that need no client, and returns its JSON
/// answer.
fn execute(request: &serde_json::Value) -> Result<String> {
    let request = CString::new(request.to_string())?;
    // SAFETY: `request` is a NUL-terminated string that outlives the call.
    // TDLib returns a NUL-terminated answer (or null) that stays valid until
    // the next `td_execute` call on this thread, and it's copied out before
    // then.
    let response = unsafe {
        let response = td_execute(request.as_ptr());
        if response.is_null() {
            String::new()
        } else {
            CStr::from_ptr(response).to_string_lossy().into_owned()
        }
    };
    Ok(response)
}

/// A formatted text from a TDLib request that needs no client, or `None` if
/// it failed.
fn execute_text(request: &serde_json::Value) -> Option<types::FormattedText> {
    let response = execute(request).ok()?;
    match serde_json::from_str(&response) {
        Ok(enums::FormattedText::FormattedText(text)) => Some(text),
        Err(_) => None,
    }
}

/// What's written in the composer, with Telegram's Markdown turned into
/// formatting, as Telegram Desktop does: `**bold**`, `__italic__`,
/// `~~strikethrough~~`, `||spoiler||`, `` `code` ``, ` ```code block``` `
/// and `[text](https://…)`. Markup that isn't closed stays as typed, so a
/// lone `*` or `_` is just a character. It runs at once rather than as a
/// request, so messages still go out in the order they were sent.
pub fn markdown(text: String) -> types::FormattedText {
    let request = json!({
        "@type": "parseMarkdown",
        "text": { "@type": "formattedText", "text": text, "entities": [] },
    });
    let mut parsed = execute_text(&request).unwrap_or_else(|| plain(text));
    // `[Bob](tg://user?id=123)`, which `@` completion writes for someone
    // without a username, mentions them, as in the Bot API's Markdown.
    for entity in &mut parsed.entities {
        if let enums::TextEntityType::TextUrl(link) = &entity.r#type
            && let Some(user_id) = link
                .url
                .strip_prefix("tg://user?id=")
                .and_then(|id| id.parse().ok())
        {
            entity.r#type =
                enums::TextEntityType::MentionName(types::TextEntityTypeMentionName { user_id });
        }
    }
    parsed
}

/// What `e` puts in the composer: your message as Markdown, so saving it
/// keeps its formatting.
pub struct EditText {
    pub markdown: String,
    /// Some formatting has no Markdown (underline, custom emoji, a mention
    /// of someone without a username…), so an edit would lose it.
    pub loses: bool,
}

/// A message's text or caption as Markdown, for editing it.
pub fn to_markdown(text: &types::FormattedText) -> Option<EditText> {
    let request = json!({ "@type": "getMarkdownText", "text": text });
    let written = execute_text(&request)?;
    // What Markdown couldn't write is left as entities. A mention of
    // someone without a username is written as the link `markdown` makes a
    // mention of again: from the end, so the offsets before stay right.
    let mut markdown = written.text.clone();
    let mut loses = false;
    let mut left = written.entities.clone();
    left.sort_by_key(|e| std::cmp::Reverse(e.offset));
    for entity in left {
        match &entity.r#type {
            enums::TextEntityType::MentionName(mention) => {
                let start = crate::messages::byte_offset(&written.text, entity.offset);
                let end = crate::messages::byte_offset(
                    &written.text,
                    entity.offset.saturating_add(entity.length),
                );
                if start < end {
                    markdown.insert_str(end, &format!("](tg://user?id={})", mention.user_id));
                    markdown.insert(start, '[');
                }
            }
            kind if !crate::messages::found_by_telegram(kind) => loses = true,
            _ => {}
        }
    }
    Some(EditText {
        markdown: crate::text::clean(&markdown),
        loses,
    })
}

/// The words of a message that `e` can change: a text, or a caption.
fn editable_text(content: &enums::MessageContent) -> Option<&types::FormattedText> {
    use enums::MessageContent as C;
    match content {
        C::MessageText(m) => Some(&m.text),
        C::MessagePhoto(m) => Some(&m.caption),
        C::MessageVideo(m) => Some(&m.caption),
        C::MessageAnimation(m) => Some(&m.caption),
        C::MessageDocument(m) => Some(&m.caption),
        C::MessageAudio(m) => Some(&m.caption),
        C::MessageVoiceNote(m) => Some(&m.caption),
        _ => None,
    }
}

/// Events go to the app tagged with the client they came from, so it can
/// drop late ones from a client it replaced after logging out.
pub type Tagged = (i32, TgEvent);

/// Sends events from one client's requests, tagged with its id.
#[derive(Clone)]
struct Events {
    client_id: i32,
    tx: UnboundedSender<Tagged>,
}

impl Events {
    fn send(&self, event: TgEvent) -> Result<(), SendError<Tagged>> {
        self.tx.send((self.client_id, event))
    }
}

#[derive(Clone)]
pub struct Tg {
    client_id: i32,
    tx: Events,
    config: Arc<Config>,
}

impl Tg {
    pub async fn start(config: Config, tx: UnboundedSender<Tagged>) -> Result<Self> {
        no_openssl_config()?;
        log_to_file(&config.data_dir.join("tdlib.log"))?;
        let client_id = tdlib_rs::create_client();
        CLIENT.store(client_id, Ordering::Relaxed);

        // `receive` blocks for up to 2s at a time, so it gets a plain thread.
        // It stops once the app drops its end of the channel.
        std::thread::spawn({
            let tx = tx.clone();
            move || {
                loop {
                    if let Some((update, client_id)) = tdlib_rs::receive()
                        && tx
                            .send((client_id, TgEvent::Update(Box::new(update))))
                            .is_err()
                    {
                        break;
                    }
                }
            }
        });

        // A new client sends no updates until it gets its first request.
        functions::get_option("version".into(), client_id)
            .await
            .map_err(|e| anyhow!("TDLib didn't start: {}", e.message))?;

        Ok(Self {
            client_id,
            tx: Events { client_id, tx },
            config: Arc::new(config),
        })
    }

    /// A client that was never started, for `--demo`, which must make no
    /// requests: TDLib isn't set up, so one would start it with its logs on
    /// the terminal.
    pub fn detached(tx: UnboundedSender<Tagged>) -> Self {
        let client_id = 0;
        Self {
            client_id,
            tx: Events { client_id, tx },
            config: Arc::new(Config {
                api_keys: None,
                data_dir: std::env::temp_dir(),
            }),
        }
    }

    /// The client events must come from to count; see [`Tagged`].
    pub fn client_id(&self) -> i32 {
        self.client_id
    }

    pub fn set_tdlib_parameters(&self, keys: ApiKeys) {
        let config = Arc::clone(&self.config);
        let client_id = self.client_id;
        self.spawn(async move {
            let dir = |name: &str| config.data_dir.join(name).to_string_lossy().into_owned();
            functions::set_tdlib_parameters(
                false,
                dir("db"),
                dir("files"),
                String::new(), // database_encryption_key
                true,          // use_file_database
                true,          // use_chat_info_database
                true,          // use_message_database: keeps history cached locally
                false,         // use_secret_chats
                keys.id,
                keys.hash,
                "en".into(),
                "Terminal".into(), // device_model: what Settings → Devices shows
                std::env::consts::OS.into(),
                env!("CARGO_PKG_VERSION").into(),
                client_id,
            )
            .await
        });
    }

    pub fn send_phone_number(&self, phone: String) {
        self.spawn(functions::set_authentication_phone_number(
            phone,
            None,
            self.client_id,
        ));
    }

    pub fn send_code(&self, code: String) {
        self.spawn(functions::check_authentication_code(code, self.client_id));
    }

    pub fn send_password(&self, password: String) {
        self.spawn(functions::check_authentication_password(
            password,
            self.client_id,
        ));
    }

    pub fn send_email(&self, email: String) {
        self.spawn(functions::set_authentication_email_address(
            email,
            self.client_id,
        ));
    }

    /// Asks for a link to show as a QR code. TDLib answers with
    /// `authorizationStateWaitOtherDeviceConfirmation`, and again with a new
    /// link whenever the old one expires.
    pub fn request_qr_code(&self) {
        self.spawn(functions::request_qr_code_authentication(
            Vec::new(),
            self.client_id,
        ));
    }

    pub fn send_email_code(&self, code: String) {
        self.spawn(functions::check_authentication_email_code(
            enums::EmailAddressAuthentication::Code(types::EmailAddressAuthenticationCode { code }),
            self.client_id,
        ));
    }

    /// Asks TDLib for the next `limit` chats of the main list. They arrive as
    /// `updateNewChat`/`updateChatPosition` updates, not in the response.
    pub fn load_chats(&self, limit: i32) {
        let tx = self.tx.clone();
        let client_id = self.client_id;
        tokio::spawn(async move {
            let (all, failed) =
                match functions::load_chats(Some(enums::ChatList::Main), limit, client_id).await {
                    Ok(()) => (false, false),
                    // 404 is TDLib's way of saying there are no more chats.
                    Err(e) if e.code == 404 => (true, false),
                    Err(e) => {
                        let _ = tx.send(TgEvent::Error(e.message));
                        (false, true)
                    }
                };
            let _ = tx.send(TgEvent::ChatsLoaded { all, failed });
        });
    }

    pub fn open_chat(&self, chat_id: i64) {
        self.spawn(functions::open_chat(chat_id, self.client_id));
    }

    pub fn close_chat(&self, chat_id: i64) {
        self.spawn(functions::close_chat(chat_id, self.client_id));
    }

    /// Fetches a page of history, up to `limit` (at most 100) messages.
    /// TDLib may return fewer.
    pub fn load_history(&self, chat_id: i64, page: Page, limit: i32) {
        // A negative offset adds that many messages newer than `from`.
        let (from, offset) = match page {
            Page::Latest => (0, 0),
            Page::Older(id) => (id, 0),
            Page::Newer(id) => (id, 1 - limit),
            Page::Around(id) => (id, -limit / 2),
        };
        let tx = self.tx.clone();
        let client_id = self.client_id;
        tokio::spawn(async move {
            let result =
                functions::get_chat_history(chat_id, from, offset, limit, false, client_id).await;
            let messages = match result {
                Ok(enums::Messages::Messages(page)) => {
                    Some(page.messages.into_iter().flatten().collect())
                }
                Err(e) => {
                    let _ = tx.send(TgEvent::Error(e.message));
                    None
                }
            };
            let _ = tx.send(TgEvent::History {
                chat_id,
                page,
                messages,
            });
        });
    }

    /// Searches the whole chat for messages with `query`, newest first,
    /// starting from `from` (0 = the newest message).
    pub fn search_messages(&self, chat_id: i64, query: String, from: i64, limit: i32) {
        let tx = self.tx.clone();
        let client_id = self.client_id;
        tokio::spawn(async move {
            let result = functions::search_chat_messages(
                chat_id,
                None,
                query.clone(),
                None,
                from,
                0,
                limit,
                None,
                client_id,
            )
            .await;
            let found = match result {
                Ok(enums::FoundChatMessages::FoundChatMessages(f)) => Some(Found {
                    ids: f.messages.iter().map(|m| m.id).collect(),
                    total: f.total_count,
                    next_from: f.next_from_message_id,
                }),
                Err(e) => {
                    let _ = tx.send(TgEvent::Error(e.message));
                    None
                }
            };
            let _ = tx.send(TgEvent::Found {
                chat_id,
                query,
                found,
            });
        });
    }

    /// Fetches the message that message `message_id` replies to, even from
    /// another chat. Failures aren't errors to show: the message was usually
    /// just deleted.
    pub fn get_replied_message(&self, chat_id: i64, message_id: i64) {
        let tx = self.tx.clone();
        let client_id = self.client_id;
        tokio::spawn(async move {
            let replied = functions::get_replied_message(chat_id, message_id, client_id)
                .await
                .ok()
                .map(|enums::Message::Message(m)| Box::new(m));
            let _ = tx.send(TgEvent::Replied {
                chat_id,
                message_id,
                replied,
            });
        });
    }

    /// Asks TDLib who a message can be deleted for. Answers from local data.
    pub fn check_deletable(&self, chat_id: i64, message_id: i64) {
        let tx = self.tx.clone();
        let client_id = self.client_id;
        tokio::spawn(async move {
            let result = functions::get_message_properties(chat_id, message_id, client_id).await;
            let deletable = match result {
                Ok(enums::MessageProperties::MessageProperties(p)) => Some(Deletable {
                    for_everyone: p.can_be_deleted_for_all_users,
                    for_me: p.can_be_deleted_only_for_self,
                }),
                Err(e) => {
                    let _ = tx.send(TgEvent::Error(e.message));
                    None
                }
            };
            let _ = tx.send(TgEvent::Deletable {
                chat_id,
                message_id,
                deletable,
            });
        });
    }

    /// Asks what reactions a message can get: the chat decides which, and
    /// some can't be added without Telegram Premium.
    pub fn available_reactions(&self, chat_id: i64, message_id: i64) {
        let tx = self.tx.clone();
        let client_id = self.client_id;
        tokio::spawn(async move {
            let row = reactions::COLUMNS as i32;
            let result =
                functions::get_message_available_reactions(chat_id, message_id, row, client_id)
                    .await;
            let available = match result {
                Ok(enums::AvailableReactions::AvailableReactions(r)) => Some(Available::from(r)),
                Err(e) => {
                    let _ = tx.send(TgEvent::Error(e.message));
                    None
                }
            };
            let _ = tx.send(TgEvent::Reactions {
                chat_id,
                message_id,
                available,
            });
        });
    }

    /// Adds your reaction to a message, or takes it back. TDLib shows the
    /// change at once with `updateMessageInteractionInfo`.
    pub fn react(&self, chat_id: i64, message_id: i64, kind: &ReactionKind, add: bool) {
        let Some(reaction) = kind.to_tdlib() else {
            return;
        };
        if add {
            self.spawn(functions::add_message_reaction(
                chat_id,
                message_id,
                reaction,
                false,
                true,
                self.client_id,
            ));
        } else {
            self.spawn(functions::remove_message_reaction(
                chat_id,
                message_id,
                reaction,
                self.client_id,
            ));
        }
    }

    /// Asks whether a message can be edited (TDLib knows whose it is, and
    /// how long the chat allows edits for), and for its words as Markdown.
    pub fn check_editable(&self, chat_id: i64, message_id: i64) {
        let tx = self.tx.clone();
        let client_id = self.client_id;
        tokio::spawn(async move {
            let result = functions::get_message_properties(chat_id, message_id, client_id).await;
            let editable = match result {
                Ok(enums::MessageProperties::MessageProperties(p)) => Some(p.can_be_edited),
                Err(e) => {
                    let _ = tx.send(TgEvent::Error(e.message));
                    None
                }
            };
            let mut text = None;
            if editable == Some(true)
                && let Ok(enums::Message::Message(message)) =
                    functions::get_message(chat_id, message_id, client_id).await
            {
                text = editable_text(&message.content).and_then(to_markdown);
            }
            let _ = tx.send(TgEvent::Editable {
                chat_id,
                message_id,
                editable,
                text,
            });
        });
    }

    /// Replaces a text message's text, with its Markdown made formatting.
    /// TDLib then sends `updateMessageContent` and `updateMessageEdited`.
    pub fn edit_text(&self, chat_id: i64, message_id: i64, text: String) {
        let content = enums::InputMessageContent::InputMessageText(types::InputMessageText {
            text: markdown(text),
            link_preview_options: None,
            clear_draft: false,
        });
        self.spawn(functions::edit_message_text(
            chat_id,
            message_id,
            content,
            self.client_id,
        ));
    }

    /// Replaces the caption of a photo, video or file; empty removes it.
    /// `above` keeps it over the media.
    pub fn edit_caption(&self, chat_id: i64, message_id: i64, text: String, above: bool) {
        self.spawn(functions::edit_message_caption(
            chat_id,
            message_id,
            Some(markdown(text)),
            above,
            self.client_id,
        ));
    }

    /// Deletes a message for everyone in the chat (`revoke`), or only for
    /// you. TDLib confirms with `updateDeleteMessages`.
    pub fn delete_message(&self, chat_id: i64, message_id: i64, revoke: bool) {
        self.spawn(functions::delete_messages(
            chat_id,
            vec![message_id],
            revoke,
            self.client_id,
        ));
    }

    /// Sends a message, its Markdown made formatting (see [`markdown`]), as
    /// a reply to message `reply_to` if given. TDLib first reports it with a
    /// temporary id (`updateNewMessage`), then `updateMessageSendSucceeded`
    /// or `…Failed`.
    pub fn send_text(&self, chat_id: i64, text: String, reply_to: Option<i64>) {
        let content = enums::InputMessageContent::InputMessageText(types::InputMessageText {
            text: markdown(text),
            link_preview_options: None,
            clear_draft: true,
        });
        let reply_to = reply_to.map(reply_to_message);
        self.spawn(functions::send_message(
            chat_id,
            None,
            reply_to,
            None,
            content,
            self.client_id,
        ));
    }

    /// Sends a sticker, as a reply to message `reply_to` if given.
    pub fn send_sticker(&self, chat_id: i64, sticker: &Sticker, reply_to: Option<i64>) {
        let content = enums::InputMessageContent::InputMessageSticker(types::InputMessageSticker {
            sticker: enums::InputFile::Id(types::InputFileId {
                id: sticker.file_id,
            }),
            thumbnail: None,
            width: sticker.width,
            height: sticker.height,
            emoji: sticker.emoji.clone(),
        });
        let reply_to = reply_to.map(reply_to_message);
        self.spawn(functions::send_message(
            chat_id,
            None,
            reply_to,
            None,
            content,
            self.client_id,
        ));
    }

    /// Asks for the stickers of a tab of the sticker panel.
    pub fn stickers(&self, source: Source) {
        let tx = self.tx.clone();
        let client_id = self.client_id;
        tokio::spawn(async move {
            let result = match source {
                Source::Recent => functions::get_recent_stickers(false, client_id)
                    .await
                    .map(|enums::Stickers::Stickers(s)| s.stickers),
                Source::Favorites => functions::get_favorite_stickers(client_id)
                    .await
                    .map(|enums::Stickers::Stickers(s)| s.stickers),
                Source::Set(id) => functions::get_sticker_set(id, client_id)
                    .await
                    .map(|enums::StickerSet::StickerSet(s)| s.stickers),
            };
            let stickers = match result {
                Ok(stickers) => Some(stickers),
                Err(e) => {
                    let _ = tx.send(TgEvent::Error(e.message));
                    None
                }
            };
            let _ = tx.send(TgEvent::Stickers { source, stickers });
        });
    }

    /// Asks for the sticker sets you added, for the sticker panel's tabs.
    pub fn sticker_sets(&self) {
        let tx = self.tx.clone();
        let client_id = self.client_id;
        tokio::spawn(async move {
            let result =
                functions::get_installed_sticker_sets(enums::StickerType::Regular, client_id).await;
            let sets = match result {
                Ok(enums::StickerSets::StickerSets(s)) => {
                    Some(s.sets.into_iter().map(|set| (set.id, set.title)).collect())
                }
                Err(e) => {
                    let _ = tx.send(TgEvent::Error(e.message));
                    None
                }
            };
            let _ = tx.send(TgEvent::StickerSets(sets));
        });
    }

    /// Finds stickers by emoji, or by a word their emoji or set are known
    /// by: your own first, then others Telegram suggests.
    pub fn find_stickers(&self, chat_id: i64, query: String) {
        let tx = self.tx.clone();
        let client_id = self.client_id;
        tokio::spawn(async move {
            let result = functions::get_stickers(
                enums::StickerType::Regular,
                query.clone(),
                STICKER_SEARCH_LIMIT,
                chat_id,
                client_id,
            )
            .await;
            let stickers = match result {
                Ok(enums::Stickers::Stickers(s)) => Some(s.stickers),
                Err(e) => {
                    let _ = tx.send(TgEvent::Error(e.message));
                    None
                }
            };
            let _ = tx.send(TgEvent::StickersFound { query, stickers });
        });
    }

    /// Sends files in order, one album per group (a group of one is a plain
    /// message), each request waiting for the one before so they arrive in
    /// that order. The caption goes under the first file, and the first
    /// group answers message `reply_to` if given. Uploads continue after
    /// TDLib reports the messages, like a text message's sending.
    pub fn send_files(
        &self,
        chat_id: i64,
        groups: Vec<Vec<Upload>>,
        caption: String,
        reply_to: Option<i64>,
    ) {
        let tx = self.tx.clone();
        let client_id = self.client_id;
        let mut caption = (!caption.is_empty()).then(|| markdown(caption));
        tokio::spawn(async move {
            let mut reply_to = reply_to.map(reply_to_message);
            for group in groups {
                let mut contents: Vec<_> = group
                    .into_iter()
                    .map(|upload| upload.content(caption.take()))
                    .collect();
                let reply_to = reply_to.take();
                let result = if contents.len() == 1 {
                    let content = contents.remove(0);
                    functions::send_message(chat_id, None, reply_to, None, content, client_id)
                        .await
                        .map(drop)
                } else {
                    functions::send_message_album(
                        chat_id, None, reply_to, None, contents, client_id,
                    )
                    .await
                    .map(drop)
                };
                // What failed once (no right to send media, say) fails again.
                if let Err(e) = result {
                    let _ = tx.send(TgEvent::Error(e.message));
                    break;
                }
            }
        });
    }

    /// Forwards messages of chat `from_chat_id`, in order, to chat
    /// `chat_id`. They arrive there as new messages.
    pub fn forward(&self, chat_id: i64, from_chat_id: i64, message_ids: Vec<i64>) {
        let tx = self.tx.clone();
        let client_id = self.client_id;
        tokio::spawn(async move {
            let result = functions::forward_messages(
                chat_id,
                None,
                from_chat_id,
                message_ids,
                None,
                false, // send_copy: keep "Forwarded from"
                false, // remove_caption
                client_id,
            )
            .await;
            let _ = match result {
                Ok(_) => tx.send(TgEvent::Forwarded { chat_id }),
                Err(e) => tx.send(TgEvent::Error(e.message)),
            };
        });
    }

    /// Searches your contacts, and Telegram's public chats, for the `s`
    /// picker. TDLib sends the users and chats it finds as updates first.
    pub fn find_chats(&self, query: String) {
        let tx = self.tx.clone();
        let client_id = self.client_id;
        tokio::spawn(async move {
            let user_ids =
                match functions::search_contacts(query.clone(), CONTACT_SEARCH_LIMIT, client_id)
                    .await
                {
                    Ok(enums::Users::Users(u)) => u.user_ids,
                    Err(_) => Vec::new(),
                };
            // Too short a query, or one searched too often, is an error;
            // either way nothing was found.
            let chat_ids = match functions::search_public_chats(query.clone(), client_id).await {
                Ok(enums::Chats::Chats(c)) => c.chat_ids,
                Err(_) => Vec::new(),
            };
            let _ = tx.send(TgEvent::ChatsFound {
                query,
                chat_ids,
                user_ids,
            });
        });
    }

    /// Opens a chat with one of your contacts, creating it if there's none.
    pub fn find_private_chat(&self, user_id: i64, request: String) {
        let client_id = self.client_id;
        self.find(request, async move {
            let enums::Chat::Chat(chat) = functions::create_private_chat(user_id, false, client_id)
                .await
                .map_err(|e| Missed::Said(e.message))?;
            Ok((chat.id, None))
        });
    }

    /// Finds the chat with a username: a person, bot, group or channel.
    pub fn find_username(&self, username: String, request: String) {
        let client_id = self.client_id;
        self.find(request, async move {
            let id = public_chat(&username, client_id).await?;
            Ok((id, None))
        });
    }

    /// Follows a t.me link: to a chat, to a message in one, or an invite.
    /// An invite to a chat you're not in comes back as [`TgEvent::Invite`],
    /// to ask first.
    pub fn find_link(&self, link: String, request: String) {
        let tx = self.tx.clone();
        let client_id = self.client_id;
        self.find(request.clone(), async move {
            let kind = functions::get_internal_link_type(link.clone(), client_id)
                .await
                .map_err(|_| Missed::Elsewhere)?;
            match kind {
                enums::InternalLinkType::PublicChat(p) => {
                    Ok((public_chat(&p.chat_username, client_id).await?, None))
                }
                enums::InternalLinkType::Message(m) => {
                    let enums::MessageLinkInfo::MessageLinkInfo(info) =
                        functions::get_message_link_info(m.url, client_id)
                            .await
                            .map_err(|e| Missed::Said(e.message))?;
                    if info.chat_id == 0 {
                        return Err(Missed::Said("That message can't be found".into()));
                    }
                    Ok((info.chat_id, info.message.map(|m| m.id)))
                }
                enums::InternalLinkType::ChatInvite(i) => {
                    let enums::ChatInviteLinkInfo::ChatInviteLinkInfo(info) =
                        functions::check_chat_invite_link(i.invite_link.clone(), client_id)
                            .await
                            .map_err(|e| Missed::Said(e.message))?;
                    // A chat you're in has an id and no time limit on reading it.
                    if info.chat_id != 0 && info.accessible_for == 0 {
                        return Ok((info.chat_id, None));
                    }
                    let invite = Invite {
                        title: crate::text::clean(&info.title),
                        members: info.member_count,
                        channel: matches!(info.r#type, enums::InviteLinkChatType::Channel),
                        badge: Badge::of(info.verification_status.as_ref(), false),
                        by_request: info.creates_join_request,
                    };
                    let _ = tx.send(TgEvent::Invite {
                        request,
                        link: i.invite_link,
                        invite,
                    });
                    Err(Missed::Quiet)
                }
                _ => Err(Missed::Elsewhere),
            }
        });
    }

    /// Joins a chat with an invite link, then opens it.
    pub fn join_by_link(&self, link: String, request: String) {
        let client_id = self.client_id;
        self.find(request, async move {
            let enums::Chat::Chat(chat) = functions::join_chat_by_invite_link(link, client_id)
                .await
                .map_err(|e| Missed::Said(e.message))?;
            Ok((chat.id, None))
        });
    }

    /// Joins a public group or channel. TDLib then sends `updateSupergroup`.
    pub fn join_chat(&self, chat_id: i64) {
        let tx = self.tx.clone();
        let client_id = self.client_id;
        tokio::spawn(async move {
            let _ = match functions::join_chat(chat_id, client_id).await {
                Ok(()) => tx.send(TgEvent::Joined { chat_id }),
                Err(e) => tx.send(TgEvent::Error(e.message)),
            };
        });
    }

    /// Searches a group's members by name, for `@` completion. TDLib sends
    /// the users first.
    pub fn find_members(&self, chat_id: i64, query: String) {
        let tx = self.tx.clone();
        let client_id = self.client_id;
        tokio::spawn(async move {
            let result = functions::search_chat_members(
                chat_id,
                query.clone(),
                MEMBER_SEARCH_LIMIT,
                None,
                client_id,
            )
            .await;
            let user_ids = match result {
                Ok(enums::ChatMembers::ChatMembers(m)) => m
                    .members
                    .into_iter()
                    .filter_map(|m| match m.member_id {
                        enums::MessageSender::User(u) => Some(u.user_id),
                        enums::MessageSender::Chat(_) => None,
                    })
                    .collect(),
                Err(_) => Vec::new(),
            };
            let _ = tx.send(TgEvent::Members {
                chat_id,
                query,
                user_ids,
            });
        });
    }

    /// Asks what commands the bots of a chat take: the bot a private chat is
    /// with, or the bots in a group.
    pub fn bot_commands(&self, chat_id: i64, peer: Peer) {
        let tx = self.tx.clone();
        let client_id = self.client_id;
        tokio::spawn(async move {
            let by_bot: Vec<(i64, Vec<types::BotCommand>)> = match peer {
                Peer::User(id) => match functions::get_user_full_info(id, client_id).await {
                    Ok(enums::UserFullInfo::UserFullInfo(info)) => info
                        .bot_info
                        .map(|b| vec![(id, b.commands)])
                        .unwrap_or_default(),
                    Err(_) => Vec::new(),
                },
                Peer::BasicGroup(id) => {
                    match functions::get_basic_group_full_info(id, client_id).await {
                        Ok(enums::BasicGroupFullInfo::BasicGroupFullInfo(info)) => info
                            .bot_commands
                            .into_iter()
                            .map(|b| (b.bot_user_id, b.commands))
                            .collect(),
                        Err(_) => Vec::new(),
                    }
                }
                Peer::Supergroup(id) => {
                    match functions::get_supergroup_full_info(id, client_id).await {
                        Ok(enums::SupergroupFullInfo::SupergroupFullInfo(info)) => info
                            .bot_commands
                            .into_iter()
                            .map(|b| (b.bot_user_id, b.commands))
                            .collect(),
                        Err(_) => Vec::new(),
                    }
                }
            };
            // A bot writes these, so they're cleaned like any message.
            let line = |text: &str| {
                crate::text::clean(text)
                    .split_whitespace()
                    .collect::<Vec<_>>()
                    .join(" ")
            };
            let commands = by_bot
                .into_iter()
                .flat_map(|(bot, commands)| {
                    commands.into_iter().map(move |c| crate::complete::Command {
                        bot,
                        name: line(&c.command),
                        description: line(&c.description),
                    })
                })
                .collect();
            let _ = tx.send(TgEvent::Commands { chat_id, commands });
        });
    }

    /// Pins a chat to the top of the main list, or unpins it. TDLib then
    /// sends `updateChatPosition`; past Telegram's limit it's an error.
    pub fn pin_chat(&self, chat_id: i64, pinned: bool) {
        self.spawn(functions::toggle_chat_is_pinned(
            enums::ChatList::Main,
            chat_id,
            pinned,
            self.client_id,
        ));
    }

    /// Changes how a chat notifies (`m` mutes it). TDLib then sends
    /// `updateChatNotificationSettings`.
    pub fn set_notifications(&self, chat_id: i64, settings: types::ChatNotificationSettings) {
        self.spawn(functions::set_chat_notification_settings(
            chat_id,
            settings,
            self.client_id,
        ));
    }

    /// Votes in a poll for these answers, by index; none takes your vote
    /// back. TDLib then sends the poll's new counts.
    pub fn vote(&self, chat_id: i64, message_id: i64, answers: Vec<i32>) {
        self.spawn(functions::set_poll_answer(
            chat_id,
            message_id,
            answers,
            self.client_id,
        ));
    }

    /// Leaves a group or channel. TDLib then takes it out of the list.
    pub fn leave_chat(&self, chat_id: i64) {
        let tx = self.tx.clone();
        let client_id = self.client_id;
        tokio::spawn(async move {
            let _ = match functions::leave_chat(chat_id, client_id).await {
                Ok(()) => tx.send(TgEvent::Left { chat_id }),
                Err(e) => tx.send(TgEvent::Error(e.message)),
            };
        });
    }

    /// Runs a lookup of a chat to open, and reports it as
    /// [`TgEvent::ChatFound`].
    fn find(
        &self,
        request: String,
        lookup: impl Future<Output = Result<(i64, Option<i64>), Missed>> + Send + 'static,
    ) {
        let tx = self.tx.clone();
        tokio::spawn(async move {
            let found = lookup.await;
            let _ = tx.send(TgEvent::ChatFound { request, found });
        });
    }

    /// Tells the chat you're typing, or that you stopped. Others see it for
    /// about 5 seconds unless it's sent again. Failing is harmless (TDLib
    /// already skips chats you can't write in), so errors aren't shown.
    pub fn send_typing(&self, chat_id: i64, typing: bool) {
        let action = typing.then_some(enums::ChatAction::Typing);
        let client_id = self.client_id;
        tokio::spawn(async move {
            let _ = functions::send_chat_action(chat_id, None, action, client_id).await;
        });
    }

    /// Downloads a file into TDLib's files directory, or returns at once if it's
    /// already there.
    pub fn download(&self, file_id: i32) {
        self.fetch_file(file_id, DOWNLOAD_PRIORITY, true);
    }

    /// Like [`download`](Self::download), for files nobody asked for (chat
    /// photos): they wait behind other downloads, and a failure isn't reported.
    pub fn download_quiet(&self, file_id: i32) {
        self.fetch_file(file_id, QUIET_DOWNLOAD_PRIORITY, false);
    }

    fn fetch_file(&self, file_id: i32, priority: i32, report_errors: bool) {
        let tx = self.tx.clone();
        let client_id = self.client_id;
        tokio::spawn(async move {
            // synchronous = true: answer only once the whole file is on disk.
            let result = functions::download_file(file_id, priority, 0, 0, true, client_id).await;
            let path = match result {
                Ok(enums::File::File(f)) if f.local.is_downloading_completed => Some(f.local.path),
                Ok(_) => None,
                Err(e) => {
                    if report_errors {
                        let _ = tx.send(TgEvent::Error(e.message));
                    }
                    None
                }
            };
            let _ = tx.send(TgEvent::Downloaded { file_id, path });
        });
    }

    /// Shows the user as online to others, or not. While online, TDLib also
    /// sends notifications without waiting to see if the phone reads the
    /// message first.
    pub fn set_online(&self, online: bool) {
        let value = enums::OptionValue::Boolean(types::OptionValueBoolean { value: online });
        self.set_option("online", value);
    }

    /// Turns on TDLib's notifications (`updateNotificationGroup`), which are
    /// off until a client says how many chats it shows at once.
    pub fn enable_notifications(&self) {
        let value = enums::OptionValue::Integer(types::OptionValueInteger {
            value: NOTIFICATION_GROUPS,
        });
        self.set_option("notification_group_count_max", value);
    }

    fn set_option(&self, name: &str, value: enums::OptionValue) {
        self.spawn(functions::set_option(
            name.into(),
            Some(value),
            self.client_id,
        ));
    }

    /// Marks messages as read, which also sends read receipts.
    pub fn view_messages(&self, chat_id: i64, message_ids: Vec<i64>) {
        self.spawn(functions::view_messages(
            chat_id,
            message_ids,
            None,
            true,
            self.client_id,
        ));
    }

    /// Flushes TDLib's database and ends with `authorizationStateClosed`.
    pub fn close(&self) {
        let client_id = self.client_id;
        self.spawn(async move {
            // Others see you go offline now, not minutes later. Before login
            // this fails, which is fine.
            let offline = enums::OptionValue::Boolean(types::OptionValueBoolean { value: false });
            let _ = functions::set_option("online".into(), Some(offline), client_id).await;
            functions::close(client_id).await
        });
    }

    /// Ends the session and deletes TDLib's database, ending with
    /// `authorizationStateClosed`. Before login finishes, it's the only way out
    /// of a QR login: TDLib takes no phone number in that state, and keeps the
    /// state across restarts.
    pub fn log_out(&self) {
        self.spawn(functions::log_out(self.client_id));
    }

    /// Replaces a closed client with a new one, which starts over at
    /// `authorizationStateWaitTdlibParameters`.
    pub fn reopen(&mut self) {
        self.client_id = tdlib_rs::create_client();
        CLIENT.store(self.client_id, Ordering::Relaxed);
        self.tx.client_id = self.client_id;
        // A new client sends no updates until it gets its first request.
        self.spawn(functions::get_option("version".into(), self.client_id));
    }

    fn spawn<T: Send + 'static>(
        &self,
        request: impl Future<Output = Result<T, types::Error>> + Send + 'static,
    ) {
        let tx = self.tx.clone();
        tokio::spawn(async move {
            if let Err(e) = request.await {
                let _ = tx.send(TgEvent::Error(e.message));
            }
        });
    }
}

/// A file to send, and how Telegram shows it.
pub enum Upload {
    /// Compressed and shown in the chat. The size is the image's, for TDLib
    /// to lay it out before the upload is done.
    Photo {
        path: String,
        width: i32,
        height: i32,
    },
    /// As it is, with its name.
    File { path: String },
}

impl Upload {
    fn content(self, caption: Option<types::FormattedText>) -> enums::InputMessageContent {
        let local = |path| enums::InputFile::Local(types::InputFileLocal { path });
        match self {
            Upload::Photo {
                path,
                width,
                height,
            } => enums::InputMessageContent::InputMessagePhoto(types::InputMessagePhoto {
                photo: local(path),
                thumbnail: None,
                added_sticker_file_ids: Vec::new(),
                width,
                height,
                caption,
                show_caption_above_media: false,
                self_destruct_type: None,
                has_spoiler: false,
            }),
            Upload::File { path } => {
                enums::InputMessageContent::InputMessageDocument(types::InputMessageDocument {
                    document: local(path),
                    thumbnail: None,
                    // Telegram may then show a song or video as one.
                    disable_content_type_detection: false,
                    caption,
                })
            }
        }
    }
}

/// The chat with a username, or why there's none.
async fn public_chat(username: &str, client_id: i32) -> Result<i64, String> {
    match functions::search_public_chat(username.to_string(), client_id).await {
        Ok(enums::Chat::Chat(chat)) => Ok(chat.id),
        Err(e) if e.code == 400 => Err(format!(
            "Nobody on Telegram is called @{}",
            crate::text::clean(username)
        )),
        Err(e) => Err(e.message),
    }
}

fn reply_to_message(message_id: i64) -> enums::InputMessageReplyTo {
    enums::InputMessageReplyTo::Message(types::InputMessageReplyToMessage {
        message_id,
        quote: None,
        checklist_task_id: 0,
    })
}

/// Text without formatting. Telegram still finds links, mentions and the
/// like in it by itself.
pub fn plain(text: String) -> types::FormattedText {
    types::FormattedText {
        text,
        entities: Vec::new(),
    }
}

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

    /// Without `log_to_file`, TDLib prints every request it runs here.
    fn quiet() {
        let request = json!({ "@type": "setLogVerbosityLevel", "new_verbosity_level": 1 });
        execute(&request).unwrap();
    }

    fn entities(text: &types::FormattedText) -> Vec<(i32, i32, String)> {
        text.entities
            .iter()
            .map(|e| {
                let kind = format!("{:?}", e.r#type);
                (e.offset, e.length, kind)
            })
            .collect()
    }

    #[test]
    fn markdown_in_the_composer_is_sent_as_formatting() {
        quiet();
        let sent =
            markdown("**bold** __it__ ~~gone~~ ||secret|| `code` [site](https://x.dev)".into());
        assert_eq!(sent.text, "bold it gone secret code site");
        assert_eq!(
            entities(&sent),
            [
                (0, 4, "Bold".into()),
                (5, 2, "Italic".into()),
                (8, 4, "Strikethrough".into()),
                (13, 6, "Spoiler".into()),
                (20, 4, "Code".into()),
                (
                    25,
                    4,
                    "TextUrl(TextEntityTypeTextUrl { url: \"https://x.dev/\" })".into()
                ),
            ]
        );
    }

    #[test]
    fn a_markdown_link_to_a_user_mentions_them() {
        quiet();
        let sent = markdown("hi [Bob Smith](tg://user?id=123)".into());
        assert_eq!(sent.text, "hi Bob Smith");
        assert_eq!(
            entities(&sent),
            [(
                3,
                9,
                "MentionName(TextEntityTypeMentionName { user_id: 123 })".into()
            )]
        );
    }

    #[test]
    fn single_stars_and_underscores_stay_as_typed() {
        quiet();
        let sent = markdown("2*3*4 = 24, snake_case_name, a ** b".into());
        assert_eq!(sent.text, "2*3*4 = 24, snake_case_name, a ** b");
        assert!(sent.entities.is_empty(), "{:?}", sent.entities);
    }

    #[test]
    fn edits_start_from_the_message_as_markdown() {
        quiet();
        let entity = |offset, length, r#type| types::TextEntity {
            offset,
            length,
            r#type,
        };
        let bold = types::FormattedText {
            text: "bold and plain".into(),
            entities: vec![entity(0, 4, enums::TextEntityType::Bold)],
        };
        let edit = to_markdown(&bold).unwrap();
        assert_eq!(edit.markdown, "**bold** and plain");
        assert!(!edit.loses);
        // Saving it gives the same message back.
        assert_eq!(markdown(edit.markdown), bold);

        let underlined = types::FormattedText {
            text: "under".into(),
            entities: vec![entity(0, 5, enums::TextEntityType::Underline)],
        };
        assert!(
            to_markdown(&underlined).unwrap().loses,
            "Markdown has no underline"
        );

        let mention = types::FormattedText {
            text: "hi Bob".into(),
            entities: vec![entity(
                3,
                3,
                enums::TextEntityType::MentionName(types::TextEntityTypeMentionName {
                    user_id: 123,
                }),
            )],
        };
        let edit = to_markdown(&mention).unwrap();
        assert_eq!(edit.markdown, "hi [Bob](tg://user?id=123)");
        assert!(!edit.loses);
        assert_eq!(markdown(edit.markdown), mention);
    }

    #[test]
    fn openssl_is_told_not_to_read_a_config_file() {
        super::no_openssl_config().unwrap();
        // Again, as after a logout's new client: still fine.
        super::no_openssl_config().unwrap();
    }
}