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
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
|
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include "td/telegram/LinkManager.h"
#include "td/telegram/AccessRights.h"
#include "td/telegram/BackgroundType.h"
#include "td/telegram/ChannelId.h"
#include "td/telegram/ChannelType.h"
#include "td/telegram/ConfigManager.h"
#include "td/telegram/ContactsManager.h"
#include "td/telegram/DialogId.h"
#include "td/telegram/DialogParticipant.h"
#include "td/telegram/Global.h"
#include "td/telegram/MessageEntity.h"
#include "td/telegram/MessageId.h"
#include "td/telegram/MessagesManager.h"
#include "td/telegram/misc.h"
#include "td/telegram/ServerMessageId.h"
#include "td/telegram/Td.h"
#include "td/telegram/TdDb.h"
#include "td/telegram/telegram_api.h"
#include "td/telegram/UserId.h"
#include "td/mtproto/ProxySecret.h"
#include "td/utils/algorithm.h"
#include "td/utils/base64.h"
#include "td/utils/buffer.h"
#include "td/utils/HttpUrl.h"
#include "td/utils/logging.h"
#include "td/utils/misc.h"
#include "td/utils/SliceBuilder.h"
#include "td/utils/StringBuilder.h"
#include "td/utils/Time.h"
#include "td/utils/utf8.h"
#include <tuple>
namespace td {
static bool is_valid_start_parameter(Slice start_parameter) {
return start_parameter.size() <= 64 && is_base64url_characters(start_parameter);
}
static bool is_valid_phone_number(Slice phone_number) {
if (phone_number.empty() || phone_number.size() > 32) {
return false;
}
for (auto c : phone_number) {
if (!is_digit(c)) {
return false;
}
}
return true;
}
static string get_url_query_hash(bool is_tg, const HttpUrlQuery &url_query) {
const auto &path = url_query.path_;
if (is_tg) {
if (path.size() == 1 && path[0] == "join" && !url_query.get_arg("invite").empty()) {
// join?invite=abcdef
return url_query.get_arg("invite").str();
}
} else {
if (path.size() >= 2 && path[0] == "joinchat" && !path[1].empty()) {
// /joinchat/<link>
return path[1];
}
if (!path.empty() && path[0].size() >= 2 && (path[0][0] == ' ' || path[0][0] == '+')) {
if (is_valid_phone_number(Slice(path[0]).substr(1))) {
return string();
}
// /+<link>
return path[0].substr(1);
}
}
return string();
}
static AdministratorRights get_administrator_rights(Slice rights, bool for_channel) {
bool can_manage_dialog = false;
bool can_change_info = false;
bool can_post_messages = false;
bool can_edit_messages = false;
bool can_delete_messages = false;
bool can_invite_users = false;
bool can_restrict_members = false;
bool can_pin_messages = false;
bool can_manage_topics = false;
bool can_promote_members = false;
bool can_manage_calls = false;
bool is_anonymous = false;
for (auto right : full_split(rights, ' ')) {
if (right == "change_info") {
can_change_info = true;
} else if (right == "post_messages") {
can_post_messages = true;
} else if (right == "edit_messages") {
can_edit_messages = true;
} else if (right == "delete_messages") {
can_delete_messages = true;
} else if (right == "restrict_members") {
can_restrict_members = true;
} else if (right == "invite_users") {
can_invite_users = true;
} else if (right == "pin_messages") {
can_pin_messages = true;
} else if (right == "manage_topics") {
can_manage_topics = true;
} else if (right == "promote_members") {
can_promote_members = true;
} else if (right == "manage_video_chats") {
can_manage_calls = true;
} else if (right == "anonymous") {
is_anonymous = true;
} else if (right == "manage_chat") {
can_manage_dialog = true;
}
}
return AdministratorRights(is_anonymous, can_manage_dialog, can_change_info, can_post_messages, can_edit_messages,
can_delete_messages, can_invite_users, can_restrict_members, can_pin_messages,
can_manage_topics, can_promote_members, can_manage_calls,
for_channel ? ChannelType::Broadcast : ChannelType::Megagroup);
}
td_api::object_ptr<td_api::targetChatChosen> get_target_chat_chosen(Slice chat_types) {
bool allow_users = false;
bool allow_bots = false;
bool allow_groups = false;
bool allow_channels = false;
for (auto chat_type : full_split(chat_types, ' ')) {
if (chat_type == "users") {
allow_users = true;
} else if (chat_type == "bots") {
allow_bots = true;
} else if (chat_type == "groups") {
allow_groups = true;
} else if (chat_type == "channels") {
allow_channels = true;
}
}
if (!allow_users && !allow_bots && !allow_groups && !allow_channels) {
return nullptr;
}
return td_api::make_object<td_api::targetChatChosen>(allow_users, allow_bots, allow_groups, allow_channels);
}
class LinkManager::InternalLinkActiveSessions final : public InternalLink {
td_api::object_ptr<td_api::InternalLinkType> get_internal_link_type_object() const final {
return td_api::make_object<td_api::internalLinkTypeActiveSessions>();
}
};
class LinkManager::InternalLinkAttachMenuBot final : public InternalLink {
td_api::object_ptr<td_api::targetChatChosen> allowed_chat_types_;
unique_ptr<InternalLink> dialog_link_;
string bot_username_;
string url_;
td_api::object_ptr<td_api::InternalLinkType> get_internal_link_type_object() const final {
td_api::object_ptr<td_api::TargetChat> target_chat;
if (dialog_link_ != nullptr) {
target_chat = td_api::make_object<td_api::targetChatInternalLink>(dialog_link_->get_internal_link_type_object());
} else if (allowed_chat_types_ != nullptr) {
target_chat = td_api::make_object<td_api::targetChatChosen>(
allowed_chat_types_->allow_user_chats_, allowed_chat_types_->allow_bot_chats_,
allowed_chat_types_->allow_group_chats_, allowed_chat_types_->allow_channel_chats_);
} else {
target_chat = td_api::make_object<td_api::targetChatCurrent>();
}
return td_api::make_object<td_api::internalLinkTypeAttachmentMenuBot>(std::move(target_chat), bot_username_, url_);
}
public:
InternalLinkAttachMenuBot(td_api::object_ptr<td_api::targetChatChosen> allowed_chat_types,
unique_ptr<InternalLink> dialog_link, string bot_username, Slice start_parameter)
: allowed_chat_types_(std::move(allowed_chat_types))
, dialog_link_(std::move(dialog_link))
, bot_username_(std::move(bot_username)) {
if (!start_parameter.empty()) {
url_ = PSTRING() << "start://" << start_parameter;
}
}
};
class LinkManager::InternalLinkAuthenticationCode final : public InternalLink {
string code_;
td_api::object_ptr<td_api::InternalLinkType> get_internal_link_type_object() const final {
return td_api::make_object<td_api::internalLinkTypeAuthenticationCode>(code_);
}
public:
explicit InternalLinkAuthenticationCode(string code) : code_(std::move(code)) {
}
};
class LinkManager::InternalLinkBackground final : public InternalLink {
string background_name_;
td_api::object_ptr<td_api::InternalLinkType> get_internal_link_type_object() const final {
return td_api::make_object<td_api::internalLinkTypeBackground>(background_name_);
}
public:
explicit InternalLinkBackground(string background_name) : background_name_(std::move(background_name)) {
}
};
class LinkManager::InternalLinkBotAddToChannel final : public InternalLink {
string bot_username_;
AdministratorRights administrator_rights_;
td_api::object_ptr<td_api::InternalLinkType> get_internal_link_type_object() const final {
return td_api::make_object<td_api::internalLinkTypeBotAddToChannel>(
bot_username_, administrator_rights_.get_chat_administrator_rights_object());
}
public:
InternalLinkBotAddToChannel(string bot_username, AdministratorRights &&administrator_rights)
: bot_username_(std::move(bot_username)), administrator_rights_(std::move(administrator_rights)) {
}
};
class LinkManager::InternalLinkBotStart final : public InternalLink {
string bot_username_;
string start_parameter_;
bool autostart_;
td_api::object_ptr<td_api::InternalLinkType> get_internal_link_type_object() const final {
bool autostart = autostart_;
if (Scheduler::context() != nullptr && bot_username_ == G()->get_option_string("premium_bot_username")) {
autostart = true;
}
return td_api::make_object<td_api::internalLinkTypeBotStart>(bot_username_, start_parameter_, autostart);
}
public:
InternalLinkBotStart(string bot_username, string start_parameter, bool autostart)
: bot_username_(std::move(bot_username)), start_parameter_(std::move(start_parameter)), autostart_(autostart) {
}
};
class LinkManager::InternalLinkBotStartInGroup final : public InternalLink {
string bot_username_;
string start_parameter_;
AdministratorRights administrator_rights_;
td_api::object_ptr<td_api::InternalLinkType> get_internal_link_type_object() const final {
return td_api::make_object<td_api::internalLinkTypeBotStartInGroup>(
bot_username_, start_parameter_,
administrator_rights_ == AdministratorRights() ? nullptr
: administrator_rights_.get_chat_administrator_rights_object());
}
public:
InternalLinkBotStartInGroup(string bot_username, string start_parameter, AdministratorRights &&administrator_rights)
: bot_username_(std::move(bot_username))
, start_parameter_(std::move(start_parameter))
, administrator_rights_(std::move(administrator_rights)) {
}
};
class LinkManager::InternalLinkChangePhoneNumber final : public InternalLink {
td_api::object_ptr<td_api::InternalLinkType> get_internal_link_type_object() const final {
return td_api::make_object<td_api::internalLinkTypeChangePhoneNumber>();
}
};
class LinkManager::InternalLinkConfirmPhone final : public InternalLink {
string hash_;
string phone_number_;
td_api::object_ptr<td_api::InternalLinkType> get_internal_link_type_object() const final {
return td_api::make_object<td_api::internalLinkTypePhoneNumberConfirmation>(hash_, phone_number_);
}
public:
InternalLinkConfirmPhone(string hash, string phone_number)
: hash_(std::move(hash)), phone_number_(std::move(phone_number)) {
}
};
class LinkManager::InternalLinkDefaultMessageAutoDeleteTimerSettings final : public InternalLink {
td_api::object_ptr<td_api::InternalLinkType> get_internal_link_type_object() const final {
return td_api::make_object<td_api::internalLinkTypeDefaultMessageAutoDeleteTimerSettings>();
}
};
class LinkManager::InternalLinkDialogInvite final : public InternalLink {
string url_;
td_api::object_ptr<td_api::InternalLinkType> get_internal_link_type_object() const final {
return td_api::make_object<td_api::internalLinkTypeChatInvite>(url_);
}
public:
explicit InternalLinkDialogInvite(string url) : url_(std::move(url)) {
}
};
class LinkManager::InternalLinkEditProfileSettings final : public InternalLink {
td_api::object_ptr<td_api::InternalLinkType> get_internal_link_type_object() const final {
return td_api::make_object<td_api::internalLinkTypeEditProfileSettings>();
}
};
class LinkManager::InternalLinkFilterSettings final : public InternalLink {
td_api::object_ptr<td_api::InternalLinkType> get_internal_link_type_object() const final {
return td_api::make_object<td_api::internalLinkTypeFilterSettings>();
}
};
class LinkManager::InternalLinkGame final : public InternalLink {
string bot_username_;
string game_short_name_;
td_api::object_ptr<td_api::InternalLinkType> get_internal_link_type_object() const final {
return td_api::make_object<td_api::internalLinkTypeGame>(bot_username_, game_short_name_);
}
public:
InternalLinkGame(string bot_username, string game_short_name)
: bot_username_(std::move(bot_username)), game_short_name_(std::move(game_short_name)) {
}
};
class LinkManager::InternalLinkInstantView final : public InternalLink {
string url_;
string fallback_url_;
td_api::object_ptr<td_api::InternalLinkType> get_internal_link_type_object() const final {
return td_api::make_object<td_api::internalLinkTypeInstantView>(url_, fallback_url_);
}
public:
InternalLinkInstantView(string url, string fallback_url)
: url_(std::move(url)), fallback_url_(std::move(fallback_url)) {
}
};
class LinkManager::InternalLinkInvoice final : public InternalLink {
string invoice_name_;
td_api::object_ptr<td_api::InternalLinkType> get_internal_link_type_object() const final {
return td_api::make_object<td_api::internalLinkTypeInvoice>(invoice_name_);
}
public:
explicit InternalLinkInvoice(string invoice_name) : invoice_name_(std::move(invoice_name)) {
}
};
class LinkManager::InternalLinkLanguage final : public InternalLink {
string language_pack_id_;
td_api::object_ptr<td_api::InternalLinkType> get_internal_link_type_object() const final {
return td_api::make_object<td_api::internalLinkTypeLanguagePack>(language_pack_id_);
}
public:
explicit InternalLinkLanguage(string language_pack_id) : language_pack_id_(std::move(language_pack_id)) {
}
};
class LinkManager::InternalLinkLanguageSettings final : public InternalLink {
td_api::object_ptr<td_api::InternalLinkType> get_internal_link_type_object() const final {
return td_api::make_object<td_api::internalLinkTypeLanguageSettings>();
}
};
class LinkManager::InternalLinkMessage final : public InternalLink {
string url_;
td_api::object_ptr<td_api::InternalLinkType> get_internal_link_type_object() const final {
return td_api::make_object<td_api::internalLinkTypeMessage>(url_);
}
public:
explicit InternalLinkMessage(string url) : url_(std::move(url)) {
}
};
class LinkManager::InternalLinkMessageDraft final : public InternalLink {
FormattedText text_;
bool contains_link_ = false;
td_api::object_ptr<td_api::InternalLinkType> get_internal_link_type_object() const final {
return td_api::make_object<td_api::internalLinkTypeMessageDraft>(get_formatted_text_object(text_, true, -1),
contains_link_);
}
public:
InternalLinkMessageDraft(FormattedText &&text, bool contains_link)
: text_(std::move(text)), contains_link_(contains_link) {
}
};
class LinkManager::InternalLinkPassportDataRequest final : public InternalLink {
UserId bot_user_id_;
string scope_;
string public_key_;
string nonce_;
string callback_url_;
td_api::object_ptr<td_api::InternalLinkType> get_internal_link_type_object() const final {
return td_api::make_object<td_api::internalLinkTypePassportDataRequest>(bot_user_id_.get(), scope_, public_key_,
nonce_, callback_url_);
}
public:
InternalLinkPassportDataRequest(UserId bot_user_id, string scope, string public_key, string nonce,
string callback_url)
: bot_user_id_(bot_user_id)
, scope_(std::move(scope))
, public_key_(std::move(public_key))
, nonce_(std::move(nonce))
, callback_url_(std::move(callback_url)) {
}
};
class LinkManager::InternalLinkPremiumFeatures final : public InternalLink {
string referrer_;
td_api::object_ptr<td_api::InternalLinkType> get_internal_link_type_object() const final {
return td_api::make_object<td_api::internalLinkTypePremiumFeatures>(referrer_);
}
public:
explicit InternalLinkPremiumFeatures(string referrer) : referrer_(std::move(referrer)) {
}
};
class LinkManager::InternalLinkPrivacyAndSecuritySettings final : public InternalLink {
td_api::object_ptr<td_api::InternalLinkType> get_internal_link_type_object() const final {
return td_api::make_object<td_api::internalLinkTypePrivacyAndSecuritySettings>();
}
};
class LinkManager::InternalLinkProxy final : public InternalLink {
string server_;
int32 port_;
td_api::object_ptr<td_api::ProxyType> type_;
td_api::object_ptr<td_api::InternalLinkType> get_internal_link_type_object() const final {
CHECK(type_ != nullptr);
auto type = type_.get();
auto proxy_type = [type]() -> td_api::object_ptr<td_api::ProxyType> {
switch (type->get_id()) {
case td_api::proxyTypeSocks5::ID: {
auto type_socks = static_cast<const td_api::proxyTypeSocks5 *>(type);
return td_api::make_object<td_api::proxyTypeSocks5>(type_socks->username_, type_socks->password_);
}
case td_api::proxyTypeMtproto::ID: {
auto type_mtproto = static_cast<const td_api::proxyTypeMtproto *>(type);
return td_api::make_object<td_api::proxyTypeMtproto>(type_mtproto->secret_);
}
default:
UNREACHABLE();
return nullptr;
}
}();
return td_api::make_object<td_api::internalLinkTypeProxy>(server_, port_, std::move(proxy_type));
}
public:
InternalLinkProxy(string server, int32 port, td_api::object_ptr<td_api::ProxyType> type)
: server_(std::move(server)), port_(port), type_(std::move(type)) {
}
};
class LinkManager::InternalLinkPublicDialog final : public InternalLink {
string dialog_username_;
td_api::object_ptr<td_api::InternalLinkType> get_internal_link_type_object() const final {
return td_api::make_object<td_api::internalLinkTypePublicChat>(dialog_username_);
}
public:
explicit InternalLinkPublicDialog(string dialog_username) : dialog_username_(std::move(dialog_username)) {
}
};
class LinkManager::InternalLinkQrCodeAuthentication final : public InternalLink {
td_api::object_ptr<td_api::InternalLinkType> get_internal_link_type_object() const final {
return td_api::make_object<td_api::internalLinkTypeQrCodeAuthentication>();
}
};
class LinkManager::InternalLinkRestorePurchases final : public InternalLink {
td_api::object_ptr<td_api::InternalLinkType> get_internal_link_type_object() const final {
return td_api::make_object<td_api::internalLinkTypeRestorePurchases>();
}
};
class LinkManager::InternalLinkSettings final : public InternalLink {
td_api::object_ptr<td_api::InternalLinkType> get_internal_link_type_object() const final {
return td_api::make_object<td_api::internalLinkTypeSettings>();
}
};
class LinkManager::InternalLinkStickerSet final : public InternalLink {
string sticker_set_name_;
bool expect_custom_emoji_;
td_api::object_ptr<td_api::InternalLinkType> get_internal_link_type_object() const final {
return td_api::make_object<td_api::internalLinkTypeStickerSet>(sticker_set_name_, expect_custom_emoji_);
}
public:
InternalLinkStickerSet(string sticker_set_name, bool expect_custom_emoji)
: sticker_set_name_(std::move(sticker_set_name)), expect_custom_emoji_(expect_custom_emoji) {
}
};
class LinkManager::InternalLinkTheme final : public InternalLink {
string theme_name_;
td_api::object_ptr<td_api::InternalLinkType> get_internal_link_type_object() const final {
return td_api::make_object<td_api::internalLinkTypeTheme>(theme_name_);
}
public:
explicit InternalLinkTheme(string theme_name) : theme_name_(std::move(theme_name)) {
}
};
class LinkManager::InternalLinkThemeSettings final : public InternalLink {
td_api::object_ptr<td_api::InternalLinkType> get_internal_link_type_object() const final {
return td_api::make_object<td_api::internalLinkTypeThemeSettings>();
}
};
class LinkManager::InternalLinkUnknownDeepLink final : public InternalLink {
string link_;
td_api::object_ptr<td_api::InternalLinkType> get_internal_link_type_object() const final {
return td_api::make_object<td_api::internalLinkTypeUnknownDeepLink>(link_);
}
public:
explicit InternalLinkUnknownDeepLink(string link) : link_(std::move(link)) {
}
};
class LinkManager::InternalLinkUnsupportedProxy final : public InternalLink {
td_api::object_ptr<td_api::InternalLinkType> get_internal_link_type_object() const final {
return td_api::make_object<td_api::internalLinkTypeUnsupportedProxy>();
}
};
class LinkManager::InternalLinkUserPhoneNumber final : public InternalLink {
string phone_number_;
td_api::object_ptr<td_api::InternalLinkType> get_internal_link_type_object() const final {
return td_api::make_object<td_api::internalLinkTypeUserPhoneNumber>(phone_number_);
}
public:
explicit InternalLinkUserPhoneNumber(string phone_number) : phone_number_(std::move(phone_number)) {
}
};
class LinkManager::InternalLinkUserToken final : public InternalLink {
string token_;
td_api::object_ptr<td_api::InternalLinkType> get_internal_link_type_object() const final {
return td_api::make_object<td_api::internalLinkTypeUserToken>(token_);
}
public:
explicit InternalLinkUserToken(string token) : token_(std::move(token)) {
}
};
class LinkManager::InternalLinkVoiceChat final : public InternalLink {
string dialog_username_;
string invite_hash_;
bool is_live_stream_;
td_api::object_ptr<td_api::InternalLinkType> get_internal_link_type_object() const final {
return td_api::make_object<td_api::internalLinkTypeVideoChat>(dialog_username_, invite_hash_, is_live_stream_);
}
public:
InternalLinkVoiceChat(string dialog_username, string invite_hash, bool is_live_stream)
: dialog_username_(std::move(dialog_username))
, invite_hash_(std::move(invite_hash))
, is_live_stream_(is_live_stream) {
}
};
class GetDeepLinkInfoQuery final : public Td::ResultHandler {
Promise<td_api::object_ptr<td_api::deepLinkInfo>> promise_;
public:
explicit GetDeepLinkInfoQuery(Promise<td_api::object_ptr<td_api::deepLinkInfo>> &&promise)
: promise_(std::move(promise)) {
}
void send(Slice link) {
send_query(G()->net_query_creator().create_unauth(telegram_api::help_getDeepLinkInfo(link.str())));
}
void on_result(BufferSlice packet) final {
auto result_ptr = fetch_result<telegram_api::help_getDeepLinkInfo>(packet);
if (result_ptr.is_error()) {
return on_error(result_ptr.move_as_error());
}
auto result = result_ptr.move_as_ok();
switch (result->get_id()) {
case telegram_api::help_deepLinkInfoEmpty::ID:
return promise_.set_value(nullptr);
case telegram_api::help_deepLinkInfo::ID: {
auto info = telegram_api::move_object_as<telegram_api::help_deepLinkInfo>(result);
auto entities = get_message_entities(nullptr, std::move(info->entities_), "GetDeepLinkInfoQuery");
auto status = fix_formatted_text(info->message_, entities, true, true, true, true, true);
if (status.is_error()) {
LOG(ERROR) << "Receive error " << status << " while parsing deep link info " << info->message_;
if (!clean_input_string(info->message_)) {
info->message_.clear();
}
entities = find_entities(info->message_, true, true);
}
FormattedText text{std::move(info->message_), std::move(entities)};
return promise_.set_value(
td_api::make_object<td_api::deepLinkInfo>(get_formatted_text_object(text, true, -1), info->update_app_));
}
default:
UNREACHABLE();
}
}
void on_error(Status status) final {
promise_.set_error(std::move(status));
}
};
class RequestUrlAuthQuery final : public Td::ResultHandler {
Promise<td_api::object_ptr<td_api::LoginUrlInfo>> promise_;
string url_;
DialogId dialog_id_;
public:
explicit RequestUrlAuthQuery(Promise<td_api::object_ptr<td_api::LoginUrlInfo>> &&promise)
: promise_(std::move(promise)) {
}
void send(string url, FullMessageId full_message_id, int32 button_id) {
url_ = std::move(url);
int32 flags = 0;
tl_object_ptr<telegram_api::InputPeer> input_peer;
if (full_message_id.get_dialog_id().is_valid()) {
dialog_id_ = full_message_id.get_dialog_id();
input_peer = td_->messages_manager_->get_input_peer(dialog_id_, AccessRights::Read);
CHECK(input_peer != nullptr);
flags |= telegram_api::messages_requestUrlAuth::PEER_MASK;
} else {
flags |= telegram_api::messages_requestUrlAuth::URL_MASK;
}
send_query(G()->net_query_creator().create(telegram_api::messages_requestUrlAuth(
flags, std::move(input_peer), full_message_id.get_message_id().get_server_message_id().get(), button_id,
url_)));
}
void on_result(BufferSlice packet) final {
auto result_ptr = fetch_result<telegram_api::messages_requestUrlAuth>(packet);
if (result_ptr.is_error()) {
return on_error(result_ptr.move_as_error());
}
auto result = result_ptr.move_as_ok();
LOG(INFO) << "Receive result for RequestUrlAuthQuery: " << to_string(result);
switch (result->get_id()) {
case telegram_api::urlAuthResultRequest::ID: {
auto request = telegram_api::move_object_as<telegram_api::urlAuthResultRequest>(result);
UserId bot_user_id = ContactsManager::get_user_id(request->bot_);
if (!bot_user_id.is_valid()) {
return on_error(Status::Error(500, "Receive invalid bot_user_id"));
}
td_->contacts_manager_->on_get_user(std::move(request->bot_), "RequestUrlAuthQuery");
promise_.set_value(td_api::make_object<td_api::loginUrlInfoRequestConfirmation>(
url_, request->domain_, td_->contacts_manager_->get_user_id_object(bot_user_id, "RequestUrlAuthQuery"),
request->request_write_access_));
break;
}
case telegram_api::urlAuthResultAccepted::ID: {
auto accepted = telegram_api::move_object_as<telegram_api::urlAuthResultAccepted>(result);
promise_.set_value(td_api::make_object<td_api::loginUrlInfoOpen>(accepted->url_, true));
break;
}
case telegram_api::urlAuthResultDefault::ID:
promise_.set_value(td_api::make_object<td_api::loginUrlInfoOpen>(url_, false));
break;
}
}
void on_error(Status status) final {
if (!dialog_id_.is_valid() ||
!td_->messages_manager_->on_get_dialog_error(dialog_id_, status, "RequestUrlAuthQuery")) {
LOG(INFO) << "Receive error for RequestUrlAuthQuery: " << status;
}
promise_.set_value(td_api::make_object<td_api::loginUrlInfoOpen>(url_, false));
}
};
class AcceptUrlAuthQuery final : public Td::ResultHandler {
Promise<td_api::object_ptr<td_api::httpUrl>> promise_;
string url_;
DialogId dialog_id_;
public:
explicit AcceptUrlAuthQuery(Promise<td_api::object_ptr<td_api::httpUrl>> &&promise) : promise_(std::move(promise)) {
}
void send(string url, FullMessageId full_message_id, int32 button_id, bool allow_write_access) {
url_ = std::move(url);
int32 flags = 0;
tl_object_ptr<telegram_api::InputPeer> input_peer;
if (full_message_id.get_dialog_id().is_valid()) {
dialog_id_ = full_message_id.get_dialog_id();
input_peer = td_->messages_manager_->get_input_peer(dialog_id_, AccessRights::Read);
CHECK(input_peer != nullptr);
flags |= telegram_api::messages_acceptUrlAuth::PEER_MASK;
} else {
flags |= telegram_api::messages_acceptUrlAuth::URL_MASK;
}
if (allow_write_access) {
flags |= telegram_api::messages_acceptUrlAuth::WRITE_ALLOWED_MASK;
}
send_query(G()->net_query_creator().create(telegram_api::messages_acceptUrlAuth(
flags, false /*ignored*/, std::move(input_peer), full_message_id.get_message_id().get_server_message_id().get(),
button_id, url_)));
}
void on_result(BufferSlice packet) final {
auto result_ptr = fetch_result<telegram_api::messages_acceptUrlAuth>(packet);
if (result_ptr.is_error()) {
return on_error(result_ptr.move_as_error());
}
auto result = result_ptr.move_as_ok();
LOG(INFO) << "Receive " << to_string(result);
switch (result->get_id()) {
case telegram_api::urlAuthResultRequest::ID:
LOG(ERROR) << "Receive unexpected " << to_string(result);
return on_error(Status::Error(500, "Receive unexpected urlAuthResultRequest"));
case telegram_api::urlAuthResultAccepted::ID: {
auto accepted = telegram_api::move_object_as<telegram_api::urlAuthResultAccepted>(result);
promise_.set_value(td_api::make_object<td_api::httpUrl>(accepted->url_));
break;
}
case telegram_api::urlAuthResultDefault::ID:
promise_.set_value(td_api::make_object<td_api::httpUrl>(url_));
break;
}
}
void on_error(Status status) final {
if (!dialog_id_.is_valid() ||
!td_->messages_manager_->on_get_dialog_error(dialog_id_, status, "AcceptUrlAuthQuery")) {
LOG(INFO) << "Receive error for AcceptUrlAuthQuery: " << status;
}
promise_.set_error(std::move(status));
}
};
LinkManager::LinkManager(Td *td, ActorShared<> parent) : td_(td), parent_(std::move(parent)) {
}
LinkManager::~LinkManager() = default;
void LinkManager::start_up() {
autologin_update_time_ = Time::now() - 365 * 86400;
autologin_domains_ = full_split(G()->td_db()->get_binlog_pmc()->get("autologin_domains"), '\xFF');
url_auth_domains_ = full_split(G()->td_db()->get_binlog_pmc()->get("url_auth_domains"), '\xFF');
whitelisted_domains_ = full_split(G()->td_db()->get_binlog_pmc()->get("whitelisted_domains"), '\xFF');
}
void LinkManager::tear_down() {
parent_.reset();
}
static bool tolower_begins_with(Slice str, Slice prefix) {
if (prefix.size() > str.size()) {
return false;
}
for (size_t i = 0; i < prefix.size(); i++) {
if (to_lower(str[i]) != prefix[i]) {
return false;
}
}
return true;
}
Result<string> LinkManager::check_link(CSlice link, bool http_only, bool https_only) {
auto result = check_link_impl(link, http_only, https_only);
if (result.is_ok()) {
return result;
}
auto error = result.move_as_error();
if (check_utf8(link)) {
return Status::Error(400, PSLICE() << "URL '" << link << "' is invalid: " << error.message());
} else {
return Status::Error(400, PSLICE() << "URL is invalid: " << error.message());
}
}
string LinkManager::get_checked_link(Slice link, bool http_only, bool https_only) {
auto result = check_link_impl(link, http_only, https_only);
if (result.is_ok()) {
return result.move_as_ok();
}
return string();
}
Result<string> LinkManager::check_link_impl(Slice link, bool http_only, bool https_only) {
bool is_tg = false;
bool is_ton = false;
if (tolower_begins_with(link, "tg:")) {
link.remove_prefix(3);
is_tg = true;
} else if (tolower_begins_with(link, "ton:")) {
link.remove_prefix(4);
is_ton = true;
}
if ((is_tg || is_ton) && begins_with(link, "//")) {
link.remove_prefix(2);
}
TRY_RESULT(http_url, parse_url(link));
if (https_only && (http_url.protocol_ != HttpUrl::Protocol::Https || is_tg || is_ton)) {
return Status::Error("Only HTTPS links are allowed");
}
if (is_tg || is_ton) {
if (http_only) {
return Status::Error("Only HTTP links are allowed");
}
if (tolower_begins_with(link, "http://") || http_url.protocol_ == HttpUrl::Protocol::Https ||
!http_url.userinfo_.empty() || http_url.specified_port_ != 0 || http_url.is_ipv6_) {
return Status::Error(is_tg ? Slice("Wrong tg URL") : Slice("Wrong ton URL"));
}
Slice query(http_url.query_);
CHECK(query[0] == '/');
if (query.size() > 1 && query[1] == '?') {
query.remove_prefix(1);
}
for (auto c : http_url.host_) {
if (!is_alnum(c) && c != '-' && c != '_') {
return Status::Error("Unallowed characters in URL host");
}
}
return PSTRING() << (is_tg ? "tg" : "ton") << "://" << http_url.host_ << query;
}
if (http_url.host_.find('.') == string::npos && !http_url.is_ipv6_) {
return Status::Error("Wrong HTTP URL");
}
return http_url.get_url();
}
LinkManager::LinkInfo LinkManager::get_link_info(Slice link) {
LinkInfo result;
if (link.empty()) {
return result;
}
link.truncate(link.find('#'));
bool is_tg = false;
if (tolower_begins_with(link, "tg:")) {
link.remove_prefix(3);
if (begins_with(link, "//")) {
link.remove_prefix(2);
}
is_tg = true;
}
auto r_http_url = parse_url(link);
if (r_http_url.is_error()) {
return result;
}
auto http_url = r_http_url.move_as_ok();
if (!http_url.userinfo_.empty() || http_url.is_ipv6_) {
return result;
}
if (is_tg) {
if (tolower_begins_with(link, "http://") || http_url.protocol_ == HttpUrl::Protocol::Https ||
http_url.specified_port_ != 0) {
return result;
}
result.type_ = LinkType::Tg;
result.query_ = link.str();
return result;
} else {
if (http_url.port_ != 80 && http_url.port_ != 443) {
return result;
}
auto host = url_decode(http_url.host_, false);
to_lower_inplace(host);
if (ends_with(host, ".t.me") && host.size() >= 9 && host.find('.') == host.size() - 5) {
Slice subdomain(&host[0], host.size() - 5);
if (is_valid_username(subdomain) && subdomain != "addemoji" && subdomain != "addstickers" &&
subdomain != "addtheme" && subdomain != "auth" && subdomain != "confirmphone" && subdomain != "invoice" &&
subdomain != "joinchat" && subdomain != "login" && subdomain != "proxy" && subdomain != "setlanguage" &&
subdomain != "share" && subdomain != "socks") {
result.type_ = LinkType::TMe;
result.query_ = PSTRING() << '/' << subdomain << http_url.query_;
return result;
}
}
if (begins_with(host, "www.")) {
host = host.substr(4);
}
string cur_t_me_url;
vector<Slice> t_me_urls{Slice("t.me"), Slice("telegram.me"), Slice("telegram.dog")};
if (Scheduler::context() != nullptr) { // for tests only
cur_t_me_url = G()->get_option_string("t_me_url");
if (tolower_begins_with(cur_t_me_url, "http://") || tolower_begins_with(cur_t_me_url, "https://")) {
Slice t_me_url = cur_t_me_url;
t_me_url = t_me_url.substr(t_me_url[4] == 's' ? 8 : 7);
if (!td::contains(t_me_urls, t_me_url)) {
t_me_urls.push_back(t_me_url);
}
}
}
for (auto t_me_url : t_me_urls) {
if (host == t_me_url) {
result.type_ = LinkType::TMe;
Slice query = http_url.query_;
while (true) {
if (begins_with(query, "/s/")) {
query.remove_prefix(2);
continue;
}
if (begins_with(query, "/%73/")) {
query.remove_prefix(4);
continue;
}
break;
}
result.query_ = query.str();
return result;
}
}
if (http_url.query_.size() > 1) {
for (auto telegraph_url : {Slice("telegra.ph"), Slice("te.legra.ph"), Slice("graph.org")}) {
if (host == telegraph_url) {
result.type_ = LinkType::Telegraph;
result.query_ = std::move(http_url.query_);
return result;
}
}
}
}
return result;
}
bool LinkManager::is_internal_link(Slice link) {
auto info = get_link_info(link);
return info.type_ != LinkType::External;
}
unique_ptr<LinkManager::InternalLink> LinkManager::parse_internal_link(Slice link, bool is_trusted) {
auto info = get_link_info(link);
switch (info.type_) {
case LinkType::External:
return nullptr;
case LinkType::Tg:
return parse_tg_link_query(info.query_, is_trusted);
case LinkType::TMe:
return parse_t_me_link_query(info.query_, is_trusted);
case LinkType::Telegraph:
return td::make_unique<InternalLinkInstantView>(PSTRING() << "https://telegra.ph" << info.query_, link.str());
default:
UNREACHABLE();
return nullptr;
}
}
namespace {
struct CopyArg {
Slice name_;
const HttpUrlQuery *url_query_;
bool *is_first_;
CopyArg(Slice name, const HttpUrlQuery *url_query, bool *is_first)
: name_(name), url_query_(url_query), is_first_(is_first) {
}
};
StringBuilder &operator<<(StringBuilder &string_builder, const CopyArg ©_arg) {
auto arg = copy_arg.url_query_->get_arg(copy_arg.name_);
if (arg.empty()) {
for (const auto &query_arg : copy_arg.url_query_->args_) {
if (query_arg.first == copy_arg.name_) {
char c = *copy_arg.is_first_ ? '?' : '&';
*copy_arg.is_first_ = false;
return string_builder << c << copy_arg.name_;
}
}
return string_builder;
}
char c = *copy_arg.is_first_ ? '?' : '&';
*copy_arg.is_first_ = false;
return string_builder << c << copy_arg.name_ << '=' << url_encode(arg);
}
} // namespace
unique_ptr<LinkManager::InternalLink> LinkManager::parse_tg_link_query(Slice query, bool is_trusted) {
const auto url_query = parse_url_query(query);
const auto &path = url_query.path_;
bool is_first_arg = true;
auto copy_arg = [&](Slice name) {
return CopyArg(name, &url_query, &is_first_arg);
};
auto pass_arg = [&](Slice name) {
return url_encode(url_query.get_arg(name));
};
auto get_arg = [&](Slice name) {
return url_query.get_arg(name).str();
};
auto has_arg = [&](Slice name) {
return !url_query.get_arg(name).empty();
};
if (path.size() == 1 && path[0] == "resolve") {
if (is_valid_username(get_arg("domain"))) {
if (has_arg("post")) {
// resolve?domain=<username>&post=12345&single&thread=<thread_id>&comment=<message_id>&t=<media_timestamp>
return td::make_unique<InternalLinkMessage>(PSTRING() << "tg:resolve" << copy_arg("domain") << copy_arg("post")
<< copy_arg("single") << copy_arg("thread")
<< copy_arg("comment") << copy_arg("t"));
}
auto username = get_arg("domain");
for (auto &arg : url_query.args_) {
if (arg.first == "voicechat" || arg.first == "videochat" || arg.first == "livestream") {
// resolve?domain=<username>&videochat
// resolve?domain=<username>&videochat=<invite_hash>
if (Scheduler::context() != nullptr) {
send_closure(G()->messages_manager(), &MessagesManager::reload_voice_chat_on_search, username);
}
return td::make_unique<InternalLinkVoiceChat>(std::move(username), arg.second, arg.first == "livestream");
}
if (arg.first == "start" && is_valid_start_parameter(arg.second)) {
// resolve?domain=<bot_username>&start=<parameter>
return td::make_unique<InternalLinkBotStart>(std::move(username), arg.second, is_trusted);
}
if (arg.first == "startgroup" && is_valid_start_parameter(arg.second)) {
// resolve?domain=<bot_username>&startgroup=<parameter>
// resolve?domain=<bot_username>&startgroup=>parameter>&admin=change_info+delete_messages+restrict_members
// resolve?domain=<bot_username>&startgroup&admin=change_info+delete_messages+restrict_members
auto administrator_rights = get_administrator_rights(url_query.get_arg("admin"), false);
return td::make_unique<InternalLinkBotStartInGroup>(std::move(username), arg.second,
std::move(administrator_rights));
}
if (arg.first == "startchannel") {
// resolve?domain=<bot_username>&startchannel&admin=change_info+post_messages+promote_members
auto administrator_rights = get_administrator_rights(url_query.get_arg("admin"), true);
if (administrator_rights != AdministratorRights()) {
return td::make_unique<InternalLinkBotAddToChannel>(std::move(username), std::move(administrator_rights));
}
}
if (arg.first == "game" && !arg.second.empty()) {
// resolve?domain=<bot_username>&game=<short_name>
return td::make_unique<InternalLinkGame>(std::move(username), arg.second);
}
}
if (!url_query.get_arg("attach").empty()) {
// resolve?domain=<username>&attach=<bot_username>
// resolve?domain=<username>&attach=<bot_username>&startattach=<start_parameter>
return td::make_unique<InternalLinkAttachMenuBot>(
nullptr, td::make_unique<InternalLinkPublicDialog>(std::move(username)), url_query.get_arg("attach").str(),
url_query.get_arg("startattach"));
} else if (url_query.has_arg("startattach")) {
// resolve?domain=<bot_username>&startattach&choose=users+bots+groups+channels
// resolve?domain=<bot_username>&startattach=<start_parameter>&choose=users+bots+groups+channels
return td::make_unique<InternalLinkAttachMenuBot>(get_target_chat_chosen(url_query.get_arg("choose")), nullptr,
std::move(username), url_query.get_arg("startattach"));
}
if (username == "telegrampassport") {
// resolve?domain=telegrampassport&bot_id=<bot_user_id>&scope=<scope>&public_key=<public_key>&nonce=<nonce>
return get_internal_link_passport(query, url_query.args_);
}
// resolve?domain=<username>
return td::make_unique<InternalLinkPublicDialog>(std::move(username));
} else if (is_valid_phone_number(get_arg("phone"))) {
auto user_link = td::make_unique<InternalLinkUserPhoneNumber>(get_arg("phone"));
if (!url_query.get_arg("attach").empty()) {
// resolve?phone=<phone_number>&attach=<bot_username>
// resolve?phone=<phone_number>&attach=<bot_username>&startattach=<start_parameter>
return td::make_unique<InternalLinkAttachMenuBot>(
nullptr, std::move(user_link), url_query.get_arg("attach").str(), url_query.get_arg("startattach"));
}
// resolve?phone=12345
return std::move(user_link);
}
} else if (path.size() == 1 && path[0] == "contact") {
// contact?token=<token>
if (has_arg("token")) {
return td::make_unique<InternalLinkUserToken>(get_arg("token"));
}
} else if (path.size() == 1 && path[0] == "login") {
// login?code=123456
if (has_arg("code")) {
return td::make_unique<InternalLinkAuthenticationCode>(get_arg("code"));
}
// login?token=<token>
if (has_arg("token")) {
return td::make_unique<InternalLinkQrCodeAuthentication>();
}
} else if (path.size() == 1 && path[0] == "restore_purchases") {
// restore_purchases
return td::make_unique<InternalLinkRestorePurchases>();
} else if (path.size() == 1 && path[0] == "passport") {
// passport?bot_id=<bot_user_id>&scope=<scope>&public_key=<public_key>&nonce=<nonce>
return get_internal_link_passport(query, url_query.args_);
} else if (path.size() == 1 && path[0] == "premium_offer") {
// premium_offer?ref=<referrer>
return td::make_unique<InternalLinkPremiumFeatures>(get_arg("ref"));
} else if (!path.empty() && path[0] == "settings") {
if (path.size() == 2 && path[1] == "auto_delete") {
// settings/auto_delete
return td::make_unique<InternalLinkDefaultMessageAutoDeleteTimerSettings>();
}
if (path.size() == 2 && path[1] == "change_number") {
// settings/change_number
return td::make_unique<InternalLinkChangePhoneNumber>();
}
if (path.size() == 2 && path[1] == "devices") {
// settings/devices
return td::make_unique<InternalLinkActiveSessions>();
}
if (path.size() == 2 && path[1] == "edit_profile") {
// settings/edit_profile
return td::make_unique<InternalLinkEditProfileSettings>();
}
if (path.size() == 2 && path[1] == "folders") {
// settings/folders
return td::make_unique<InternalLinkFilterSettings>();
}
if (path.size() == 2 && path[1] == "language") {
// settings/language
return td::make_unique<InternalLinkLanguageSettings>();
}
if (path.size() == 2 && path[1] == "privacy") {
// settings/privacy
return td::make_unique<InternalLinkPrivacyAndSecuritySettings>();
}
if (path.size() == 2 && path[1] == "themes") {
// settings/themes
return td::make_unique<InternalLinkThemeSettings>();
}
// settings
return td::make_unique<InternalLinkSettings>();
} else if (path.size() == 1 && path[0] == "join") {
// join?invite=<hash>
if (has_arg("invite")) {
return td::make_unique<InternalLinkDialogInvite>(PSTRING() << "tg:join?invite="
<< url_encode(get_url_query_hash(true, url_query)));
}
} else if (path.size() == 1 && (path[0] == "addstickers" || path[0] == "addemoji")) {
// addstickers?set=<name>
// addemoji?set=<name>
if (has_arg("set")) {
return td::make_unique<InternalLinkStickerSet>(get_arg("set"), path[0] == "addemoji");
}
} else if (path.size() == 1 && path[0] == "setlanguage") {
// setlanguage?lang=<name>
if (has_arg("lang")) {
return td::make_unique<InternalLinkLanguage>(get_arg("lang"));
}
} else if (path.size() == 1 && path[0] == "addtheme") {
// addtheme?slug=<name>
if (has_arg("slug")) {
return td::make_unique<InternalLinkTheme>(get_arg("slug"));
}
} else if (path.size() == 1 && path[0] == "confirmphone") {
if (has_arg("hash") && has_arg("phone")) {
// confirmphone?phone=<phone>&hash=<hash>
return td::make_unique<InternalLinkConfirmPhone>(get_arg("hash"), get_arg("phone"));
}
} else if (path.size() == 1 && path[0] == "socks") {
if (has_arg("server") && has_arg("port")) {
// socks?server=<server>&port=<port>&user=<user>&pass=<pass>
auto port = to_integer<int32>(get_arg("port"));
if (0 < port && port < 65536) {
return td::make_unique<InternalLinkProxy>(
get_arg("server"), port, td_api::make_object<td_api::proxyTypeSocks5>(get_arg("user"), get_arg("pass")));
} else {
return td::make_unique<InternalLinkUnsupportedProxy>();
}
}
} else if (path.size() == 1 && path[0] == "proxy") {
if (has_arg("server") && has_arg("port")) {
// proxy?server=<server>&port=<port>&secret=<secret>
auto port = to_integer<int32>(get_arg("port"));
if (0 < port && port < 65536 && mtproto::ProxySecret::from_link(get_arg("secret")).is_ok()) {
return td::make_unique<InternalLinkProxy>(get_arg("server"), port,
td_api::make_object<td_api::proxyTypeMtproto>(get_arg("secret")));
} else {
return td::make_unique<InternalLinkUnsupportedProxy>();
}
}
} else if (path.size() == 1 && path[0] == "privatepost") {
// privatepost?channel=123456789&post=12345&single&thread=<thread_id>&comment=<message_id>&t=<media_timestamp>
if (has_arg("channel") && has_arg("post")) {
return td::make_unique<InternalLinkMessage>(
PSTRING() << "tg:privatepost" << copy_arg("channel") << copy_arg("post") << copy_arg("single")
<< copy_arg("thread") << copy_arg("comment") << copy_arg("t"));
}
} else if (path.size() == 1 && path[0] == "bg") {
// bg?color=<color>
// bg?gradient=<hex_color>-<hex_color>&rotation=...
// bg?gradient=<hex_color>~<hex_color>~<hex_color>~<hex_color>
// bg?slug=<background_name>&mode=blur+motion
// bg?slug=<pattern_name>&intensity=...&bg_color=...&mode=blur+motion
if (has_arg("color")) {
return td::make_unique<InternalLinkBackground>(pass_arg("color"));
}
if (has_arg("gradient")) {
return td::make_unique<InternalLinkBackground>(PSTRING() << pass_arg("gradient") << copy_arg("rotation"));
}
if (has_arg("slug")) {
return td::make_unique<InternalLinkBackground>(PSTRING()
<< pass_arg("slug") << copy_arg("mode") << copy_arg("intensity")
<< copy_arg("bg_color") << copy_arg("rotation"));
}
} else if (path.size() == 1 && path[0] == "invoice") {
// invoice?slug=<invoice_name>
if (has_arg("slug")) {
return td::make_unique<InternalLinkInvoice>(url_query.get_arg("slug").str());
}
} else if (path.size() == 1 && (path[0] == "share" || path[0] == "msg" || path[0] == "msg_url")) {
// msg_url?url=<url>
// msg_url?url=<url>&text=<text>
return get_internal_link_message_draft(get_arg("url"), get_arg("text"));
}
if (!path.empty() && !path[0].empty()) {
return td::make_unique<InternalLinkUnknownDeepLink>(PSTRING() << "tg://" << query);
}
return nullptr;
}
unique_ptr<LinkManager::InternalLink> LinkManager::parse_t_me_link_query(Slice query, bool is_trusted) {
CHECK(query[0] == '/');
const auto url_query = parse_url_query(query);
const auto &path = url_query.path_;
if (path.empty() || path[0].empty()) {
return nullptr;
}
bool is_first_arg = true;
auto copy_arg = [&](Slice name) {
return CopyArg(name, &url_query, &is_first_arg);
};
auto get_arg = [&](Slice name) {
return url_query.get_arg(name).str();
};
auto has_arg = [&](Slice name) {
return !url_query.get_arg(name).empty();
};
if (path[0] == "c") {
if (path.size() >= 3 && to_integer<int64>(path[1]) > 0 && to_integer<int64>(path[2]) > 0) {
// /c/123456789/12345?single&thread=<thread_id>&comment=<message_id>&t=<media_timestamp>
// /c/123456789/1234/12345?single&comment=<message_id>&t=<media_timestamp>
is_first_arg = false;
auto post = to_integer<int64>(path[2]);
auto thread = PSTRING() << copy_arg("thread");
if (path.size() >= 4 && to_integer<int64>(path[3]) > 0) {
thread = PSTRING() << "&thread=" << post;
post = to_integer<int64>(path[3]);
}
return td::make_unique<InternalLinkMessage>(PSTRING() << "tg:privatepost?channel=" << to_integer<int64>(path[1])
<< "&post=" << post << copy_arg("single") << thread
<< copy_arg("comment") << copy_arg("t"));
}
} else if (path[0] == "login") {
if (path.size() >= 2 && !path[1].empty()) {
// /login/<code>
return td::make_unique<InternalLinkAuthenticationCode>(path[1]);
}
} else if (path[0] == "joinchat") {
if (path.size() >= 2 && !path[1].empty()) {
// /joinchat/<link>
return td::make_unique<InternalLinkDialogInvite>(PSTRING() << "tg:join?invite="
<< url_encode(get_url_query_hash(false, url_query)));
}
} else if (path[0][0] == ' ' || path[0][0] == '+') {
if (path[0].size() >= 2) {
if (is_valid_phone_number(Slice(path[0]).substr(1))) {
auto user_link = td::make_unique<InternalLinkUserPhoneNumber>(path[0].substr(1));
if (!url_query.get_arg("attach").empty()) {
// /+<phone_number>?attach=<bot_username>
// /+<phone_number>?attach=<bot_username>&startattach=<start_parameter>
return td::make_unique<InternalLinkAttachMenuBot>(
nullptr, std::move(user_link), url_query.get_arg("attach").str(), url_query.get_arg("startattach"));
}
// /+<phone_number>
return std::move(user_link);
} else {
// /+<link>
return td::make_unique<InternalLinkDialogInvite>(PSTRING() << "tg:join?invite="
<< url_encode(get_url_query_hash(false, url_query)));
}
}
} else if (path[0] == "contact") {
if (path.size() >= 2 && !path[1].empty()) {
// /contact/<token>
return td::make_unique<InternalLinkUserToken>(path[1]);
}
} else if (path[0] == "addstickers" || path[0] == "addemoji") {
if (path.size() >= 2 && !path[1].empty()) {
// /addstickers/<name>
// /addemoji/<name>
return td::make_unique<InternalLinkStickerSet>(path[1], path[0] == "addemoji");
}
} else if (path[0] == "setlanguage") {
if (path.size() >= 2 && !path[1].empty()) {
// /setlanguage/<name>
return td::make_unique<InternalLinkLanguage>(path[1]);
}
} else if (path[0] == "addtheme") {
if (path.size() >= 2 && !path[1].empty()) {
// /addtheme/<name>
return td::make_unique<InternalLinkTheme>(path[1]);
}
} else if (path[0] == "confirmphone") {
if (has_arg("hash") && has_arg("phone")) {
// /confirmphone?phone=<phone>&hash=<hash>
return td::make_unique<InternalLinkConfirmPhone>(get_arg("hash"), get_arg("phone"));
}
} else if (path[0] == "socks") {
if (has_arg("server") && has_arg("port")) {
// /socks?server=<server>&port=<port>&user=<user>&pass=<pass>
auto port = to_integer<int32>(get_arg("port"));
if (0 < port && port < 65536) {
return td::make_unique<InternalLinkProxy>(
get_arg("server"), port, td_api::make_object<td_api::proxyTypeSocks5>(get_arg("user"), get_arg("pass")));
} else {
return td::make_unique<InternalLinkUnsupportedProxy>();
}
}
} else if (path[0] == "proxy") {
if (has_arg("server") && has_arg("port")) {
// /proxy?server=<server>&port=<port>&secret=<secret>
auto port = to_integer<int32>(get_arg("port"));
if (0 < port && port < 65536 && mtproto::ProxySecret::from_link(get_arg("secret")).is_ok()) {
return td::make_unique<InternalLinkProxy>(get_arg("server"), port,
td_api::make_object<td_api::proxyTypeMtproto>(get_arg("secret")));
} else {
return td::make_unique<InternalLinkUnsupportedProxy>();
}
}
} else if (path[0] == "bg") {
if (path.size() >= 2 && !path[1].empty()) {
// /bg/<hex_color>
// /bg/<hex_color>-<hex_color>?rotation=...
// /bg/<hex_color>~<hex_color>~<hex_color>~<hex_color>
// /bg/<background_name>?mode=blur+motion
// /bg/<pattern_name>?intensity=...&bg_color=...&mode=blur+motion
return td::make_unique<InternalLinkBackground>(PSTRING()
<< url_encode(path[1]) << copy_arg("mode") << copy_arg("intensity")
<< copy_arg("bg_color") << copy_arg("rotation"));
}
} else if (path[0] == "invoice") {
if (path.size() >= 2 && !path[1].empty()) {
// /invoice/<name>
return td::make_unique<InternalLinkInvoice>(path[1]);
}
} else if (path[0][0] == '$') {
if (path[0].size() >= 2) {
// /$<invoice_name>
return td::make_unique<InternalLinkInvoice>(path[0].substr(1));
}
} else if (path[0] == "share" || path[0] == "msg") {
if (!(path.size() > 1 && (path[1] == "bookmarklet" || path[1] == "embed"))) {
// /share?url=<url>
// /share/url?url=<url>&text=<text>
return get_internal_link_message_draft(get_arg("url"), get_arg("text"));
}
} else if (path[0] == "iv") {
if (path.size() == 1 && has_arg("url")) {
// /iv?url=<url>&rhash=<rhash>
return td::make_unique<InternalLinkInstantView>(
PSTRING() << "https://t.me/iv" << copy_arg("url") << copy_arg("rhash"), get_arg("url"));
}
} else if (is_valid_username(path[0])) {
if (path.size() >= 2 && to_integer<int64>(path[1]) > 0) {
// /<username>/12345?single&thread=<thread_id>&comment=<message_id>&t=<media_timestamp>
// /<username>/1234/12345?single&comment=<message_id>&t=<media_timestamp>
is_first_arg = false;
auto post = to_integer<int64>(path[1]);
auto thread = PSTRING() << copy_arg("thread");
if (path.size() >= 3 && to_integer<int64>(path[2]) > 0) {
thread = PSTRING() << "&thread=" << post;
post = to_integer<int64>(path[2]);
}
return td::make_unique<InternalLinkMessage>(PSTRING() << "tg:resolve?domain=" << url_encode(path[0])
<< "&post=" << post << copy_arg("single") << thread
<< copy_arg("comment") << copy_arg("t"));
}
auto username = path[0];
for (auto &arg : url_query.args_) {
if (arg.first == "voicechat" || arg.first == "videochat" || arg.first == "livestream") {
// /<username>?videochat
// /<username>?videochat=<invite_hash>
if (Scheduler::context() != nullptr) {
send_closure(G()->messages_manager(), &MessagesManager::reload_voice_chat_on_search, username);
}
return td::make_unique<InternalLinkVoiceChat>(std::move(username), arg.second, arg.first == "livestream");
}
if (arg.first == "start" && is_valid_start_parameter(arg.second)) {
// /<bot_username>?start=<parameter>
return td::make_unique<InternalLinkBotStart>(std::move(username), arg.second, is_trusted);
}
if (arg.first == "startgroup" && is_valid_start_parameter(arg.second)) {
// /<bot_username>?startgroup=<parameter>
// /<bot_username>?startgroup=<parameter>&admin=change_info+delete_messages+restrict_members
// /<bot_username>?startgroup&admin=change_info+delete_messages+restrict_members
auto administrator_rights = get_administrator_rights(url_query.get_arg("admin"), false);
return td::make_unique<InternalLinkBotStartInGroup>(std::move(username), arg.second,
std::move(administrator_rights));
}
if (arg.first == "startchannel") {
// /<bot_username>?startchannel&admin=change_info+post_messages+promote_members
auto administrator_rights = get_administrator_rights(url_query.get_arg("admin"), true);
if (administrator_rights != AdministratorRights()) {
return td::make_unique<InternalLinkBotAddToChannel>(std::move(username), std::move(administrator_rights));
}
}
if (arg.first == "game" && !arg.second.empty()) {
// /<bot_username>?game=<short_name>
return td::make_unique<InternalLinkGame>(std::move(username), arg.second);
}
}
if (!url_query.get_arg("attach").empty()) {
// /<username>?attach=<bot_username>
// /<username>?attach=<bot_username>&startattach=<start_parameter>
return td::make_unique<InternalLinkAttachMenuBot>(
nullptr, td::make_unique<InternalLinkPublicDialog>(std::move(username)), url_query.get_arg("attach").str(),
url_query.get_arg("startattach"));
} else if (url_query.has_arg("startattach")) {
// /<bot_username>?startattach&choose=users+bots+groups+channels
// /<bot_username>?startattach=<start_parameter>&choose=users+bots+groups+channels
return td::make_unique<InternalLinkAttachMenuBot>(get_target_chat_chosen(url_query.get_arg("choose")), nullptr,
std::move(username), url_query.get_arg("startattach"));
}
// /<username>
return td::make_unique<InternalLinkPublicDialog>(std::move(username));
}
return nullptr;
}
unique_ptr<LinkManager::InternalLink> LinkManager::get_internal_link_message_draft(Slice url, Slice text) {
if (url.empty() && text.empty()) {
return nullptr;
}
while (!text.empty() && text.back() == '\n') {
text.remove_suffix(1);
}
url = trim(url);
if (url.empty()) {
url = text;
text = Slice();
}
FormattedText full_text;
bool contains_url = false;
if (!text.empty()) {
contains_url = true;
full_text.text = PSTRING() << url << '\n' << text;
} else {
full_text.text = url.str();
}
if (fix_formatted_text(full_text.text, full_text.entities, false, false, false, true, true).is_error()) {
return nullptr;
}
if (full_text.text[0] == '@') {
full_text.text = ' ' + full_text.text;
for (auto &entity : full_text.entities) {
entity.offset++;
}
}
return td::make_unique<InternalLinkMessageDraft>(std::move(full_text), contains_url);
}
unique_ptr<LinkManager::InternalLink> LinkManager::get_internal_link_passport(
Slice query, const vector<std::pair<string, string>> &args) {
auto get_arg = [&args](Slice key) {
for (auto &arg : args) {
if (arg.first == key) {
return Slice(arg.second);
}
}
return Slice();
};
UserId bot_user_id(to_integer<int64>(get_arg("bot_id")));
auto scope = get_arg("scope");
auto public_key = get_arg("public_key");
auto nonce = get_arg("nonce");
if (nonce.empty()) {
nonce = get_arg("payload");
}
auto callback_url = get_arg("callback_url");
if (!bot_user_id.is_valid() || scope.empty() || public_key.empty() || nonce.empty()) {
return td::make_unique<InternalLinkUnknownDeepLink>(PSTRING() << "tg://" << query);
}
return td::make_unique<InternalLinkPassportDataRequest>(bot_user_id, scope.str(), public_key.str(), nonce.str(),
callback_url.str());
}
void LinkManager::update_autologin_domains(string autologin_token, vector<string> autologin_domains,
vector<string> url_auth_domains, vector<string> whitelisted_domains) {
autologin_update_time_ = Time::now();
autologin_token_ = std::move(autologin_token);
if (autologin_domains_ != autologin_domains) {
autologin_domains_ = std::move(autologin_domains);
G()->td_db()->get_binlog_pmc()->set("autologin_domains", implode(autologin_domains_, '\xFF'));
}
if (url_auth_domains_ != url_auth_domains) {
url_auth_domains_ = std::move(url_auth_domains);
G()->td_db()->get_binlog_pmc()->set("url_auth_domains", implode(url_auth_domains_, '\xFF'));
}
if (whitelisted_domains_ != whitelisted_domains) {
whitelisted_domains_ = std::move(whitelisted_domains);
G()->td_db()->get_binlog_pmc()->set("whitelisted_domains", implode(whitelisted_domains_, '\xFF'));
}
}
void LinkManager::get_deep_link_info(Slice link, Promise<td_api::object_ptr<td_api::deepLinkInfo>> &&promise) {
Slice link_scheme("tg:");
if (begins_with(link, link_scheme)) {
link.remove_prefix(link_scheme.size());
if (begins_with(link, "//")) {
link.remove_prefix(2);
}
}
size_t pos = 0;
while (pos < link.size() && link[pos] != '/' && link[pos] != '?' && link[pos] != '#') {
pos++;
}
link.truncate(pos);
td_->create_handler<GetDeepLinkInfoQuery>(std::move(promise))->send(link);
}
void LinkManager::get_external_link_info(string &&link, Promise<td_api::object_ptr<td_api::LoginUrlInfo>> &&promise) {
auto default_result = td_api::make_object<td_api::loginUrlInfoOpen>(link, false);
if (G()->close_flag()) {
return promise.set_value(std::move(default_result));
}
auto r_url = parse_url(link);
if (r_url.is_error()) {
return promise.set_value(std::move(default_result));
}
bool skip_confirm = td::contains(whitelisted_domains_, r_url.ok().host_);
default_result->skip_confirm_ = skip_confirm;
if (!td::contains(autologin_domains_, r_url.ok().host_)) {
if (td::contains(url_auth_domains_, r_url.ok().host_)) {
td_->create_handler<RequestUrlAuthQuery>(std::move(promise))->send(link, FullMessageId(), 0);
return;
}
return promise.set_value(std::move(default_result));
}
if (autologin_update_time_ < Time::now() - 10000) {
auto query_promise =
PromiseCreator::lambda([link = std::move(link), promise = std::move(promise)](Result<Unit> &&result) mutable {
if (result.is_error()) {
return promise.set_value(td_api::make_object<td_api::loginUrlInfoOpen>(link, false));
}
send_closure(G()->link_manager(), &LinkManager::get_external_link_info, std::move(link), std::move(promise));
});
return send_closure(G()->config_manager(), &ConfigManager::reget_app_config, std::move(query_promise));
}
if (autologin_token_.empty()) {
return promise.set_value(std::move(default_result));
}
auto url = r_url.move_as_ok();
url.protocol_ = HttpUrl::Protocol::Https;
Slice path = url.query_;
path.truncate(url.query_.find_first_of("?#"));
Slice parameters_hash = Slice(url.query_).substr(path.size());
Slice parameters = parameters_hash;
parameters.truncate(parameters.find('#'));
Slice hash = parameters_hash.substr(parameters.size());
string added_parameter;
if (parameters.empty()) {
added_parameter = '?';
} else if (parameters.size() == 1) {
CHECK(parameters == "?");
} else {
added_parameter = '&';
}
added_parameter += "autologin_token=";
added_parameter += autologin_token_;
url.query_ = PSTRING() << path << parameters << added_parameter << hash;
promise.set_value(td_api::make_object<td_api::loginUrlInfoOpen>(url.get_url(), skip_confirm));
}
void LinkManager::get_login_url_info(FullMessageId full_message_id, int64 button_id,
Promise<td_api::object_ptr<td_api::LoginUrlInfo>> &&promise) {
TRY_RESULT_PROMISE(promise, url, td_->messages_manager_->get_login_button_url(full_message_id, button_id));
td_->create_handler<RequestUrlAuthQuery>(std::move(promise))
->send(std::move(url), full_message_id, narrow_cast<int32>(button_id));
}
void LinkManager::get_login_url(FullMessageId full_message_id, int64 button_id, bool allow_write_access,
Promise<td_api::object_ptr<td_api::httpUrl>> &&promise) {
TRY_RESULT_PROMISE(promise, url, td_->messages_manager_->get_login_button_url(full_message_id, button_id));
td_->create_handler<AcceptUrlAuthQuery>(std::move(promise))
->send(std::move(url), full_message_id, narrow_cast<int32>(button_id), allow_write_access);
}
void LinkManager::get_link_login_url(const string &url, bool allow_write_access,
Promise<td_api::object_ptr<td_api::httpUrl>> &&promise) {
td_->create_handler<AcceptUrlAuthQuery>(std::move(promise))->send(url, FullMessageId(), 0, allow_write_access);
}
Result<string> LinkManager::get_background_url(const string &name,
td_api::object_ptr<td_api::BackgroundType> background_type) {
TRY_RESULT(type, BackgroundType::get_background_type(background_type.get()));
auto url = PSTRING() << G()->get_option_string("t_me_url", "https://t.me/") << "bg/";
auto link = type.get_link();
if (type.has_file()) {
url += name;
if (!link.empty()) {
url += '?';
url += link;
}
} else {
url += link;
}
return url;
}
string LinkManager::get_dialog_invite_link_hash(Slice invite_link) {
auto link_info = get_link_info(invite_link);
if (link_info.type_ != LinkType::Tg && link_info.type_ != LinkType::TMe) {
return string();
}
const auto url_query = parse_url_query(link_info.query_);
return get_url_query_hash(link_info.type_ == LinkType::Tg, url_query);
}
string LinkManager::get_dialog_invite_link(Slice hash, bool is_internal) {
if (!is_base64url_characters(hash)) {
return string();
}
if (is_internal) {
return PSTRING() << "tg:join?invite=" << hash;
} else {
return PSTRING() << G()->get_option_string("t_me_url", "https://t.me/") << '+' << hash;
}
}
string LinkManager::get_instant_view_link_url(Slice link) {
auto link_info = get_link_info(link);
if (link_info.type_ != LinkType::TMe) {
return string();
}
const auto url_query = parse_url_query(link_info.query_);
const auto &path = url_query.path_;
if (path.size() == 1 && path[0] == "iv") {
return url_query.get_arg("url").str();
}
return string();
}
string LinkManager::get_instant_view_link_rhash(Slice link) {
auto link_info = get_link_info(link);
if (link_info.type_ != LinkType::TMe) {
return string();
}
const auto url_query = parse_url_query(link_info.query_);
const auto &path = url_query.path_;
if (path.size() == 1 && path[0] == "iv" && !url_query.get_arg("url").empty()) {
return url_query.get_arg("rhash").str();
}
return string();
}
string LinkManager::get_instant_view_link(Slice url, Slice rhash) {
return PSTRING() << G()->get_option_string("t_me_url", "https://t.me/") << "iv?url=" << url_encode(url)
<< "&rhash=" << url_encode(rhash);
}
string LinkManager::get_public_chat_link(Slice username) {
return PSTRING() << G()->get_option_string("t_me_url", "https://t.me/") << url_encode(username);
}
UserId LinkManager::get_link_user_id(Slice url) {
string lower_cased_url = to_lower(url);
url = lower_cased_url;
Slice link_scheme("tg:");
if (!begins_with(url, link_scheme)) {
return UserId();
}
url.remove_prefix(link_scheme.size());
if (begins_with(url, "//")) {
url.remove_prefix(2);
}
Slice host("user");
if (!begins_with(url, host) || (url.size() > host.size() && Slice("/?#").find(url[host.size()]) == Slice::npos)) {
return UserId();
}
url.remove_prefix(host.size());
if (begins_with(url, "/")) {
url.remove_prefix(1);
}
if (!begins_with(url, "?")) {
return UserId();
}
url.remove_prefix(1);
url.truncate(url.find('#'));
for (auto parameter : full_split(url, '&')) {
Slice key;
Slice value;
std::tie(key, value) = split(parameter, '=');
if (key == Slice("id")) {
auto r_user_id = to_integer_safe<int64>(value);
if (r_user_id.is_error()) {
return UserId();
}
return UserId(r_user_id.ok());
}
}
return UserId();
}
Result<CustomEmojiId> LinkManager::get_link_custom_emoji_id(Slice url) {
string lower_cased_url = to_lower(url);
url = lower_cased_url;
Slice link_scheme("tg:");
if (!begins_with(url, link_scheme)) {
return Status::Error(400, "Custom emoji URL must have scheme tg");
}
url.remove_prefix(link_scheme.size());
if (begins_with(url, "//")) {
url.remove_prefix(2);
}
Slice host("emoji");
if (!begins_with(url, host) || (url.size() > host.size() && Slice("/?#").find(url[host.size()]) == Slice::npos)) {
return Status::Error(400, PSLICE() << "Custom emoji URL must have host \"" << host << '"');
}
url.remove_prefix(host.size());
if (begins_with(url, "/")) {
url.remove_prefix(1);
}
if (!begins_with(url, "?")) {
return Status::Error(400, "Custom emoji URL must have an emoji identifier");
}
url.remove_prefix(1);
url.truncate(url.find('#'));
for (auto parameter : full_split(url, '&')) {
Slice key;
Slice value;
std::tie(key, value) = split(parameter, '=');
if (key == Slice("id")) {
auto r_document_id = to_integer_safe<int64>(value);
if (r_document_id.is_error() || r_document_id.ok() == 0) {
return Status::Error(400, "Invalid custom emoji identifier specified");
}
return CustomEmojiId(r_document_id.ok());
}
}
return Status::Error(400, "Custom emoji URL must have an emoji identifier");
}
Result<MessageLinkInfo> LinkManager::get_message_link_info(Slice url) {
if (url.empty()) {
return Status::Error("URL must be non-empty");
}
auto link_info = get_link_info(url);
if (link_info.type_ != LinkType::Tg && link_info.type_ != LinkType::TMe) {
return Status::Error("Invalid message link URL");
}
url = link_info.query_;
Slice username;
Slice channel_id_slice;
Slice message_id_slice;
Slice comment_message_id_slice = "0";
Slice top_thread_message_id_slice;
Slice media_timestamp_slice;
bool is_single = false;
bool for_comment = false;
if (link_info.type_ == LinkType::Tg) {
// resolve?domain=username&post=12345&single&t=123&comment=12&thread=21
// privatepost?channel=123456789&post=12345&single&t=123&comment=12&thread=21
bool is_resolve = false;
if (begins_with(url, "resolve")) {
url = url.substr(7);
is_resolve = true;
} else if (begins_with(url, "privatepost")) {
url = url.substr(11);
} else {
return Status::Error("Wrong message link URL");
}
if (begins_with(url, "/")) {
url = url.substr(1);
}
if (!begins_with(url, "?")) {
return Status::Error("Wrong message link URL");
}
url = url.substr(1);
auto args = full_split(url, '&');
for (auto arg : args) {
auto key_value = split(arg, '=');
if (is_resolve) {
if (key_value.first == "domain") {
username = key_value.second;
}
} else {
if (key_value.first == "channel") {
channel_id_slice = key_value.second;
}
}
if (key_value.first == "post") {
message_id_slice = key_value.second;
}
if (key_value.first == "t") {
media_timestamp_slice = key_value.second;
}
if (key_value.first == "single") {
is_single = true;
}
if (key_value.first == "comment") {
comment_message_id_slice = key_value.second;
}
if (key_value.first == "thread") {
for_comment = true;
top_thread_message_id_slice = key_value.second;
}
}
} else {
// /c/123456789/12345
// /c/123456789/1234/12345
// /username/12345?single
CHECK(!url.empty() && url[0] == '/');
url.remove_prefix(1);
auto username_end_pos = url.find('/');
if (username_end_pos == Slice::npos) {
return Status::Error("Wrong message link URL");
}
username = url.substr(0, username_end_pos);
url = url.substr(username_end_pos + 1);
if (username == "c") {
username = Slice();
auto channel_id_end_pos = url.find('/');
if (channel_id_end_pos == Slice::npos) {
return Status::Error("Wrong message link URL");
}
channel_id_slice = url.substr(0, channel_id_end_pos);
url = url.substr(channel_id_end_pos + 1);
}
auto query_pos = url.find('?');
message_id_slice = url.substr(0, query_pos);
if (query_pos != Slice::npos) {
auto args = full_split(url.substr(query_pos + 1), '&');
for (auto arg : args) {
auto key_value = split(arg, '=');
if (key_value.first == "t") {
media_timestamp_slice = key_value.second;
}
if (key_value.first == "single") {
is_single = true;
}
if (key_value.first == "comment") {
comment_message_id_slice = key_value.second;
}
if (key_value.first == "thread") {
for_comment = true;
top_thread_message_id_slice = key_value.second;
}
}
}
auto slash_pos = message_id_slice.find('/');
if (slash_pos != Slice::npos) {
top_thread_message_id_slice = message_id_slice.substr(0, slash_pos);
message_id_slice.remove_prefix(slash_pos + 1);
}
}
ChannelId channel_id;
if (username.empty()) {
auto r_channel_id = to_integer_safe<int64>(channel_id_slice);
if (r_channel_id.is_error() || !ChannelId(r_channel_id.ok()).is_valid()) {
return Status::Error("Wrong channel ID");
}
channel_id = ChannelId(r_channel_id.ok());
}
auto r_message_id = to_integer_safe<int32>(message_id_slice);
if (r_message_id.is_error() || !ServerMessageId(r_message_id.ok()).is_valid()) {
return Status::Error("Wrong message ID");
}
int32 top_thread_message_id = 0;
if (!top_thread_message_id_slice.empty()) {
auto r_top_thread_message_id = to_integer_safe<int32>(top_thread_message_id_slice);
if (r_top_thread_message_id.is_error()) {
return Status::Error("Wrong message thread ID");
}
top_thread_message_id = r_top_thread_message_id.ok();
if (!ServerMessageId(top_thread_message_id).is_valid()) {
return Status::Error("Invalid message thread ID");
}
}
auto r_comment_message_id = to_integer_safe<int32>(comment_message_id_slice);
if (r_comment_message_id.is_error() ||
!(r_comment_message_id.ok() == 0 || ServerMessageId(r_comment_message_id.ok()).is_valid())) {
return Status::Error("Wrong comment message ID");
}
bool is_media_timestamp_invalid = false;
int32 media_timestamp = 0;
const int32 MAX_MEDIA_TIMESTAMP = 10000000;
if (!media_timestamp_slice.empty()) {
int32 current_value = 0;
for (size_t i = 0; i <= media_timestamp_slice.size(); i++) {
auto c = i < media_timestamp_slice.size() ? media_timestamp_slice[i] : 's';
if ('0' <= c && c <= '9') {
current_value = current_value * 10 + c - '0';
if (current_value > MAX_MEDIA_TIMESTAMP) {
is_media_timestamp_invalid = true;
break;
}
} else {
auto mul = 0;
switch (to_lower(c)) {
case 'h':
mul = 3600;
break;
case 'm':
mul = 60;
break;
case 's':
mul = 1;
break;
}
if (mul == 0 || current_value > MAX_MEDIA_TIMESTAMP / mul ||
media_timestamp + current_value * mul > MAX_MEDIA_TIMESTAMP) {
is_media_timestamp_invalid = true;
break;
}
media_timestamp += current_value * mul;
current_value = 0;
}
}
}
MessageLinkInfo info;
info.username = username.str();
info.channel_id = channel_id;
info.message_id = MessageId(ServerMessageId(r_message_id.ok()));
info.comment_message_id = MessageId(ServerMessageId(r_comment_message_id.ok()));
info.top_thread_message_id = MessageId(ServerMessageId(top_thread_message_id));
info.media_timestamp = is_media_timestamp_invalid ? 0 : media_timestamp;
info.is_single = is_single;
info.for_comment = for_comment;
LOG(INFO) << "Have link to " << info.message_id << " in chat @" << info.username << "/" << channel_id.get();
return std::move(info);
}
} // namespace td
|