Пример #1
0
        private static void OnAuthorizationStateUpdated(TdApi.AuthorizationState authorizationState)
        {
            if (authorizationState != null)
            {
                _authorizationState = authorizationState;
            }
            if (_authorizationState is TdApi.AuthorizationStateWaitTdlibParameters)
            {
                TdApi.TdlibParameters parameters = new TdApi.TdlibParameters();
                parameters.DatabaseDirectory  = "tdlib";
                parameters.UseMessageDatabase = true;
                parameters.UseSecretChats     = true;
                parameters.ApiId                  = 94575;
                parameters.ApiHash                = "a3406de8d171bb422bb6ddf3bbd800e2";
                parameters.SystemLanguageCode     = "en";
                parameters.DeviceModel            = "Desktop";
                parameters.ApplicationVersion     = "1.0";
                parameters.EnableStorageOptimizer = true;

                _client.Send(new TdApi.SetTdlibParameters(parameters), new AuthorizationRequestHandler());
            }
            else if (_authorizationState is TdApi.AuthorizationStateWaitEncryptionKey)
            {
                _client.Send(new TdApi.CheckDatabaseEncryptionKey(), new AuthorizationRequestHandler());
            }
            else if (_authorizationState is TdApi.AuthorizationStateWaitPhoneNumber)
            {
                string phoneNumber = ReadLine("Please enter phone number: ");
                _client.Send(new TdApi.SetAuthenticationPhoneNumber(phoneNumber, null), new AuthorizationRequestHandler());
            }
            else if (_authorizationState is TdApi.AuthorizationStateWaitOtherDeviceConfirmation state)
            {
                Console.WriteLine("Please confirm this login link on another device: " + state.Link);
            }
            else if (_authorizationState is TdApi.AuthorizationStateWaitCode)
            {
                string code = ReadLine("Please enter authentication code: ");
                _client.Send(new TdApi.CheckAuthenticationCode(code), new AuthorizationRequestHandler());
            }
            else if (_authorizationState is TdApi.AuthorizationStateWaitRegistration)
            {
                string firstName = ReadLine("Please enter your first name: ");
                string lastName  = ReadLine("Please enter your last name: ");
                _client.Send(new TdApi.RegisterUser(firstName, lastName), new AuthorizationRequestHandler());
            }
            else if (_authorizationState is TdApi.AuthorizationStateWaitPassword)
            {
                string password = ReadLine("Please enter password: "******"Logging out");
            }
            else if (_authorizationState is TdApi.AuthorizationStateClosing)
            {
                _haveAuthorization = false;
                Print("Closing");
            }
            else if (_authorizationState is TdApi.AuthorizationStateClosed)
            {
                Print("Closed");
                _client.Dispose(); // _client is closed and native resources can be disposed now
                if (!_quiting)
                {
                    _client = CreateTdClient(); // recreate _client after previous has closed
                }
            }
            else
            {
                Print("Unsupported authorization state:" + _newLine + _authorizationState);
            }
        }
Пример #2
0
        public void OnAuthorizationStateUpdated(TdApi.AuthorizationState authorizationState)
        {
            if (authorizationState != null)
            {
                _authorizationState = authorizationState;
            }
            if (_authorizationState is TdApi.AuthorizationStateWaitTdlibParameters)
            {
                TdApi.TdlibParameters parameters = new TdApi.TdlibParameters();
                parameters.DatabaseDirectory  = _databaseDirectory;
                parameters.UseMessageDatabase = _useMessageDatabase;
                parameters.UseSecretChats     = _useSecretChats;
                parameters.ApiId                  = _apiId;
                parameters.ApiHash                = _apiHash;
                parameters.SystemLanguageCode     = _systemLanguageCode;
                parameters.DeviceModel            = _deviceModel;
                parameters.SystemVersion          = _systemVersion;
                parameters.ApplicationVersion     = _applicationVersion;
                parameters.EnableStorageOptimizer = _enableStorageOptimizer;

                _client.Send(new TdApi.SetTdlibParameters(parameters), new AuthorizationRequestHandler());

                OnAuthorizationStateUpdatedEvent(EnumAuthorization.TdlibParameters);
            }
            else if (_authorizationState is TdApi.AuthorizationStateWaitEncryptionKey)
            {
                _client.Send(new TdApi.CheckDatabaseEncryptionKey(), new AuthorizationRequestHandler());
                OnAuthorizationStateUpdatedEvent(EnumAuthorization.EncryptionKey);
            }
            else if (_authorizationState is TdApi.AuthorizationStateWaitPhoneNumber)
            {
                OnAuthorizationStateUpdatedEvent(EnumAuthorization.WaitPhoneNumber);
            }
            else if (_authorizationState is TdApi.AuthorizationStateWaitCode)
            {
                OnAuthorizationStateUpdatedEvent(EnumAuthorization.WaitCode);
            }
            else if (_authorizationState is TdApi.AuthorizationStateWaitPassword)
            {
                OnAuthorizationStateUpdatedEvent(EnumAuthorization.WaitPassword);
            }
            else if (_authorizationState is TdApi.AuthorizationStateReady)
            {
                _haveAuthorization = true;
                _gotAuthorization.Set();
                OnAuthorizationStateUpdatedEvent(EnumAuthorization.Ready);
            }
            else if (_authorizationState is TdApi.AuthorizationStateLoggingOut)
            {
                _haveAuthorization = false;
                Print("Logging out");
                OnAuthorizationStateUpdatedEvent(EnumAuthorization.LoggingOut);
            }
            else if (_authorizationState is TdApi.AuthorizationStateClosing)
            {
                _haveAuthorization = false;
                Print("Closing");
                OnAuthorizationStateUpdatedEvent(EnumAuthorization.Closing);
            }
            else if (_authorizationState is TdApi.AuthorizationStateClosed)
            {
                Print("Closed");
                if (!_quiting)
                {
                    _client = CreateTdClient(); // recreate _client after previous has closed
                }
                OnAuthorizationStateUpdatedEvent(EnumAuthorization.Closed);
            }
            else
            {
                Print("Unsupported authorization state:" + _newLine + _authorizationState);
                OnAuthorizationStateUpdatedEvent(EnumAuthorization.None);
            }
            OnAuthorizationStateUpdatedEvent(EnumAuthorization.None);
        }
Пример #3
0
        private static void OnAuthorizationStateUpdated(TdApi.AuthorizationState authorizationState)
        {
            if (authorizationState != null)
            {
                _authorizationState = authorizationState;
            }
            if (_authorizationState is TdApi.AuthorizationStateWaitTdlibParameters)
            {
                TdApi.TdlibParameters parameters = new TdApi.TdlibParameters();
                parameters.DatabaseDirectory  = "tdlib";
                parameters.UseMessageDatabase = true;
                parameters.UseSecretChats     = true;
                parameters.ApiId                  = 94575;
                parameters.ApiHash                = "a3406de8d171bb422bb6ddf3bbd800e2";
                parameters.SystemLanguageCode     = "en";
                parameters.DeviceModel            = "Desktop";
                parameters.SystemVersion          = "Unknown";
                parameters.ApplicationVersion     = "1.0";
                parameters.EnableStorageOptimizer = true;

                _client.Send(new TdApi.SetTdlibParameters(parameters), new AuthorizationRequestHandler());
            }
            else if (_authorizationState is TdApi.AuthorizationStateWaitEncryptionKey)
            {
                _client.Send(new TdApi.CheckDatabaseEncryptionKey(), new AuthorizationRequestHandler());
            }
            else if (_authorizationState is TdApi.AuthorizationStateWaitPhoneNumber)
            {
                string phoneNumber = ReadLine("Please enter phone number: ");
                _client.Send(new TdApi.SetAuthenticationPhoneNumber(phoneNumber, false, false), new AuthorizationRequestHandler());
            }
            else if (_authorizationState is TdApi.AuthorizationStateWaitCode)
            {
                string code = ReadLine("Please enter authentication code: ");
                _client.Send(new TdApi.CheckAuthenticationCode(code, "", ""), new AuthorizationRequestHandler());
            }
            else if (_authorizationState is TdApi.AuthorizationStateWaitPassword)
            {
                string password = ReadLine("Please enter password: "******"Logging out");
            }
            else if (_authorizationState is TdApi.AuthorizationStateClosing)
            {
                _haveAuthorization = false;
                Print("Closing");
            }
            else if (_authorizationState is TdApi.AuthorizationStateClosed)
            {
                Print("Closed");
                if (!_quiting)
                {
                    _client = CreateTdClient(); // recreate _client after previous has closed
                }
            }
            else
            {
                Print("Unsupported authorization state:" + _newLine + _authorizationState);
            }
        }
Пример #4
0
        public TDLibCore(Helper hp)
        {
            this.hpcore             = hp;
            lockerobject            = new object();
            commands                = new Dictionary <string, Action <tdapi.BaseObject, Reshandler> >();
            mainresponsehandlers    = new Dictionary <Type, Action <TDLibCoreEventArgs> >();
            this.authorizationstate = enums.AuhtorizationState.BackgroundActions;
            this.connectionstate    = enums.ConnectionState.Updating;

            #region handle UpdateAuthorizationState,UpdateOption,UpdateConnectionState

            mainresponsehandlers.Add(new tdapi.UpdateAuthorizationState().GetType(), (b) =>
            {
                tdapi.UpdateAuthorizationState ustate = b.additionalobject as tdapi.UpdateAuthorizationState;
                tdapi.AuthorizationState state        = ustate.AuthorizationState;
                if (state is tdapi.AuthorizationStateClosed)
                {
                    authorizationstate = enums.AuhtorizationState.Closed;
                }
                else if (state is tdapi.AuthorizationStateClosing)
                {
                    authorizationstate = enums.AuhtorizationState.Closing;
                }
                else if (state is tdapi.AuthorizationStateLoggingOut)
                {
                    authorizationstate = enums.AuhtorizationState.LoggingOut;
                }
                else if (state is tdapi.AuthorizationStateReady)
                {
                    b.core.OnReadyAction(new TDLibCoreEventArgs()
                    {
                        core = b.core
                    });
                    authorizationstate = enums.AuhtorizationState.Ready;
                }
                else if (state is tdapi.AuthorizationStateWaitEncryptionKey)
                {
                    client.Send(new tdapi.CheckDatabaseEncryptionKey(), null);
                    if (b.core.hpcore.useproxy is true)
                    {
                        string[] split = b.core.hpcore.debugproxy.Split(':');
                        string proxyip = split[0];
                        int proxyport  = int.Parse(split[1]);
                        client.Send(new tdapi.AddProxy(proxyip, proxyport, true, new tdapi.ProxyTypeHttp()), null);
                        hpcore.addlog("[TDLibCore][proxystate] - enabled");
                    }
                    else
                    {
                        client.Send(new tdapi.DisableProxy(), null);
                        hpcore.addlog("[TDLibCore][proxystate] - disabled");
                    }
                    authorizationstate = enums.AuhtorizationState.BackgroundActions;
                }
                else if (state is tdapi.AuthorizationStateWaitOtherDeviceConfirmation)
                {
                    authorizationstate = enums.AuhtorizationState.BackgroundActions;
                }
                else if (state is tdapi.AuthorizationStateWaitCode)
                {
                    authorizationstate = enums.AuhtorizationState.WaitingForVerificationCode;
                    b.core.OnVerificationCodeNeededAction(new TDLibCoreEventArgs()
                    {
                        core = b.core
                    });
                }
                else if (state is tdapi.AuthorizationStateWaitPassword)
                {
                    authorizationstate = enums.AuhtorizationState.WaitingForVerificationPassword;
                    b.core.OnVerificationPasswordNeededAction(new TDLibCoreEventArgs()
                    {
                        core = b.core
                    });
                }
                else if (state is tdapi.AuthorizationStateWaitPhoneNumber)
                {
                    client.Send(new tdapi.SetAuthenticationPhoneNumber(b.core.phonenumber, new tdapi.PhoneNumberAuthenticationSettings()
                    {
                        AllowFlashCall       = true,
                        AllowSmsRetrieverApi = true
                    }), null);
                    authorizationstate = enums.AuhtorizationState.BackgroundActions;
                }
                else if (state is tdapi.AuthorizationStateWaitTdlibParameters)
                {
                    client.Send(new tdapi.SetTdlibParameters(new tdapi.TdlibParameters()
                    {
                        ApiHash                = b.core.hpcore.APIHASH,
                        ApiId                  = b.core.hpcore.APIID,
                        ApplicationVersion     = "1.0.0",
                        DeviceModel            = "Desktop",
                        EnableStorageOptimizer = true,
                        SystemLanguageCode     = "en",
                        UseSecretChats         = true,
                        UseMessageDatabase     = true,
                        UseChatInfoDatabase    = true,
                        DatabaseDirectory      = $"TDLibCoreData-{   b.core.phonenumber.Replace("+", "0")}"
                    }), null);
                    authorizationstate = enums.AuhtorizationState.BackgroundActions;
                }
                else
                {
                    b.core.authorizationstate = enums.AuhtorizationState.InvalidData;
                }
            });
            mainresponsehandlers.Add(new tdapi.UpdateConnectionState().GetType(), (b) =>
            {
                tdapi.UpdateConnectionState uct = b.additionalobject as tdapi.UpdateConnectionState;
                if (uct.State is tdapi.ConnectionStateConnecting)
                {
                    b.core.connectionstate = enums.ConnectionState.Connecting;
                }
                else if (uct.State is tdapi.ConnectionStateConnectingToProxy)
                {
                    b.core.connectionstate = enums.ConnectionState.ConnectingToProxy;
                }
                else if (uct.State is tdapi.ConnectionStateReady)
                {
                    b.core.connectionstate = enums.ConnectionState.Connected;
                }
                else if (uct.State is tdapi.ConnectionStateUpdating)
                {
                    b.core.connectionstate = enums.ConnectionState.Updating;
                }
                else if (uct.State is tdapi.ConnectionStateWaitingForNetwork)
                {
                    b.core.connectionstate = enums.ConnectionState.WaitingForNetwork;
                }
                else
                {
                    b.core.connectionstate = enums.ConnectionState.InvalidData;
                }
            });
            mainresponsehandlers.Add(new tdapi.UpdateOption().GetType(), (b) =>
            {
                tdapi.UpdateOption upn = b.additionalobject as tdapi.UpdateOption;
                client.Send(new tdapi.SetOption(upn.Name, upn.Value), null);
            });

            #endregion handle UpdateAuthorizationState,UpdateOption,UpdateConnectionState
        }