1 //
2 // Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 #pragma once
8 
9 #include "td/telegram/FullMessageId.h"
10 #include "td/telegram/MessageLinkInfo.h"
11 #include "td/telegram/td_api.h"
12 #include "td/telegram/UserId.h"
13 
14 #include "td/actor/actor.h"
15 #include "td/actor/PromiseFuture.h"
16 
17 #include "td/utils/common.h"
18 #include "td/utils/Slice.h"
19 #include "td/utils/Status.h"
20 
21 #include <utility>
22 
23 namespace td {
24 
25 class Td;
26 
27 class LinkManager final : public Actor {
28  public:
29   LinkManager(Td *td, ActorShared<> parent);
30 
31   LinkManager(const LinkManager &) = delete;
32   LinkManager &operator=(const LinkManager &) = delete;
33   LinkManager(LinkManager &&) = delete;
34   LinkManager &operator=(LinkManager &&) = delete;
35   ~LinkManager() final;
36 
37   class InternalLink {
38    public:
39     InternalLink() = default;
40     InternalLink(const InternalLink &) = delete;
41     InternalLink &operator=(const InternalLink &) = delete;
42     InternalLink(InternalLink &&) = delete;
43     InternalLink &operator=(InternalLink &&) = delete;
44     virtual ~InternalLink() = default;
45 
46     virtual td_api::object_ptr<td_api::InternalLinkType> get_internal_link_type_object() const = 0;
47   };
48 
49   // checks whether the link is a valid tg, ton or HTTP(S) URL and returns it in a canonical form
50   static Result<string> check_link(Slice link);
51 
52   // checks whether the link is a supported tg or t.me link and parses it
53   static unique_ptr<InternalLink> parse_internal_link(Slice link);
54 
55   void update_autologin_domains(string autologin_token, vector<string> autologin_domains,
56                                 vector<string> url_auth_domains);
57 
58   void get_deep_link_info(Slice link, Promise<td_api::object_ptr<td_api::deepLinkInfo>> &&promise);
59 
60   void get_external_link_info(string &&link, Promise<td_api::object_ptr<td_api::LoginUrlInfo>> &&promise);
61 
62   void get_login_url_info(FullMessageId full_message_id, int64 button_id,
63                           Promise<td_api::object_ptr<td_api::LoginUrlInfo>> &&promise);
64 
65   void get_login_url(FullMessageId full_message_id, int64 button_id, bool allow_write_access,
66                      Promise<td_api::object_ptr<td_api::httpUrl>> &&promise);
67 
68   void get_link_login_url(const string &url, bool allow_write_access,
69                           Promise<td_api::object_ptr<td_api::httpUrl>> &&promise);
70 
71   static string get_dialog_invite_link_hash(Slice invite_link);
72 
73   static UserId get_link_user_id(Slice url);
74 
75   static Result<MessageLinkInfo> get_message_link_info(Slice url);
76 
77  private:
78   void start_up() final;
79 
80   void tear_down() final;
81 
82   class InternalLinkActiveSessions;
83   class InternalLinkAuthenticationCode;
84   class InternalLinkBackground;
85   class InternalLinkBotStart;
86   class InternalLinkBotStartInGroup;
87   class InternalLinkChangePhoneNumber;
88   class InternalLinkConfirmPhone;
89   class InternalLinkDialogInvite;
90   class InternalLinkFilterSettings;
91   class InternalLinkGame;
92   class InternalLinkLanguage;
93   class InternalLinkMessage;
94   class InternalLinkMessageDraft;
95   class InternalLinkPassportDataRequest;
96   class InternalLinkProxy;
97   class InternalLinkPublicDialog;
98   class InternalLinkQrCodeAuthentication;
99   class InternalLinkSettings;
100   class InternalLinkStickerSet;
101   class InternalLinkTheme;
102   class InternalLinkThemeSettings;
103   class InternalLinkUnknownDeepLink;
104   class InternalLinkUnsupportedProxy;
105   class InternalLinkVoiceChat;
106 
107   struct LinkInfo {
108     bool is_internal_ = false;
109     bool is_tg_ = false;
110     string query_;
111   };
112   // returns information about the link
113   static LinkInfo get_link_info(Slice link);
114 
115   static unique_ptr<InternalLink> parse_tg_link_query(Slice query);
116 
117   static unique_ptr<InternalLink> parse_t_me_link_query(Slice query);
118 
119   static unique_ptr<InternalLink> get_internal_link_passport(Slice query,
120                                                              const vector<std::pair<string, string>> &args);
121 
122   static unique_ptr<InternalLink> get_internal_link_message_draft(Slice url, Slice text);
123 
124   Td *td_;
125   ActorShared<> parent_;
126 
127   string autologin_token_;
128   vector<string> autologin_domains_;
129   double autologin_update_time_ = 0.0;
130   vector<string> url_auth_domains_;
131 };
132 
133 }  // namespace td
134