Beispiel #1
0
        public async Task SaveAuthorization(auth_Authorization authorization)
        {
            this.authorization = (Auth_authorizationConstructor)authorization;
            SaveUser(this.authorization.user);
            Task dialogsTask = dialogs.DialogsRequest();
            Task updateTask  = updates.GetStateRequest();
            await Task.WhenAll(dialogsTask, updateTask);

            save();
        }
Beispiel #2
0
        public void read(BinaryReader reader)
        {
            logger.info("read session...");
            id         = reader.ReadUInt64();
            sequence   = reader.ReadInt32();
            mainDcId   = reader.ReadInt32();
            timeOffset = reader.ReadInt32();
            cachedSalt = reader.ReadUInt64();
            int count = reader.ReadInt32();

            // contacts sync marker
            ContactsStateMarker = Serializers.String.read(reader);

            dcs = new Dictionary <int, TelegramDC>(count);
            for (int i = 0; i < count; i++)
            {
                int endpointId = reader.ReadInt32();
                dcs.Add(endpointId, new TelegramDC(reader));
            }

            int authorizationExists = reader.ReadInt32();

            if (authorizationExists != 0)
            {
                authorization = (Auth_authorizationConstructor)TL.Parse <auth_Authorization>(reader);
            }


            int usersCount = reader.ReadInt32();

            users = new Dictionary <int, UserModel>(usersCount + 10);
            for (int i = 0; i < usersCount; i++)
            {
                users.Add(reader.ReadInt32(), new UserModel(TL.Parse <User>(reader)));
            }

            int chatsCount = reader.ReadInt32();

            chats = new Dictionary <int, ChatModel>(chatsCount + 10);
            for (int i = 0; i < chatsCount; i++)
            {
                chats.Add(reader.ReadInt32(), new ChatModel(TL.Parse <Chat>(reader)));
            }

            logger.info("reading updates state....");
            updates = new UpdatesProcessor(this, reader);

            logger.info("reading dialogs...");
            dialogs = new Dialogs(this, reader);

            files          = new Files(this);
            encryptedChats = new EncryptedChats(this, reader);

            logger.info("session readed complete");
        }
Beispiel #3
0
        public override void OnResponse(BinaryReader reader)
        {
            uint code = reader.ReadUInt32();

            System.Diagnostics.Debug.WriteLine("code: " + code.ToString("x"));
            ;

            if (code == 0x4e32b894) // config constructor
            {
                ConfigConstructor config = new ConfigConstructor();
                config.Read(reader);
                ConfigConstructor = config;
            }
            else if (code == 0xff036af1) // auth.authorization :/
            {
                Auth_authorizationConstructor auth = new Auth_authorizationConstructor();
                auth.Read(reader);
                UserConstructor = (UserConstructor)auth.user;
            }
            else // TODO something didn't go right...
            {
                if (code == 0xb446ae3) // messages slice still?
                {
                    var result = TL.Parse(reader, code);

                    ;
                }
                else
                {
                    try
                    {
                        while (true)
                        {
                            reader.ReadUInt32();          // flush reader, it's the best we can do!
                        }
                    }
                    catch (EndOfStreamException) { }
                }
            }
        }
Beispiel #4
0
        public void read(BinaryReader reader)
        {
            int endpointCount = reader.ReadInt32();

            endpoints = new List <TelegramEndpoint>(endpointCount);
            for (int i = 0; i < endpointCount; i++)
            {
                endpoints.Add(new TelegramEndpoint(reader));
            }

            int keyExists = reader.ReadInt32();

            if (keyExists == 0)
            {
                authKey = null;
            }
            else
            {
                authKey = new AuthKey(Serializers.Bytes.read(reader));
            }

            //int fileSessionExists = reader.ReadInt32();
            //if(fileSessionExists == 0) {
            //    fileSession = null;
            //} else {
            //    fileSession = new TelegramFileSession(reader);
            //}

            int fileAuthExists = reader.ReadInt32();

            if (fileAuthExists == 0)
            {
                fileAuthorization = null;
            }
            else
            {
                fileAuthorization = (Auth_authorizationConstructor)TL.Parse <auth_Authorization>(reader);
            }
        }
Beispiel #5
0
        public async Task Start()
        {
            try {
//                phoneSource = new TaskCompletionSource<string>();
//                codeSource = new TaskCompletionSource<string>();
//                signupSource = new TaskCompletionSource<SignUpData>();

                await Task.Run(() => session.ConnectAsync());

//                await session.Established;

                string phone = await phoneSource.Task;

                Auth_sentCodeConstructor sendCodeResponse = null;

                for (int i = 0; i < 5; i++)
                {
                    // 5 migration tries
                    bool sendCodeSuccess;
                    int  migrateDc = -1;
                    try {
                        sendCodeResponse =
                            (Auth_sentCodeConstructor)
                            await
                            session.Api.auth_sendCode(phone, 0, 1097, "712986b054dc1311bec3c2dd92e843e7",
                                                      langCode);

                        sendCodeSuccess = true;
                    }
                    catch (MTProtoErrorException e) {
                        logger.warning("connect exception: {0}", e);
                        sendCodeSuccess = false;
                        if (e.ErrorMessage.StartsWith("PHONE_MIGRATE_") || e.ErrorMessage.StartsWith("NETWORK_MIGRATE_"))
                        {
                            migrateDc =
                                Convert.ToInt32(
                                    e.ErrorMessage.Replace("PHONE_MIGRATE_", "").Replace("NETWORK_MIGRATE_", ""), 10);
                        }
                    }

                    if (sendCodeSuccess)
                    {
                        break;
                    }
                    else if (migrateDc != -1)
                    {
                        await session.Migrate(migrateDc);
                    }
                }

                if (sendCodeResponse == null)
                {
                    logger.error("login failed");
                    return;
                }

                if (sendCodeResponse.phone_registered)
                {
                    // sign in
                    string code;

                    NeedCodeEvent(this);
                    while (true)
                    {
                        // wait 30 seconds and send phone call
                        if (await Task.WhenAny(codeSource.Task, Task.Delay(TimeSpan.FromSeconds(60))) == codeSource.Task)
                        {
                            code = codeSource.Task.Result;
                        }
                        else
                        {
                            session.Api.auth_sendCall(phone, sendCodeResponse.phone_code_hash);
                            code = await codeSource.Task;
                        }

                        try {
                            Auth_authorizationConstructor authorization =
                                (Auth_authorizationConstructor)
                                await session.Api.auth_signIn(phone, sendCodeResponse.phone_code_hash, code);

                            await session.SaveAuthorization(authorization);

                            LoginSuccessEvent(this);
                            break;
                        }
                        catch (MTProtoErrorException e) {
                            codeSource = new TaskCompletionSource <string>();
                            WrongCodeEvent(this);
                        }
                    }
                }
                else
                {
                    // sign up
                    string code;
                    NeedCodeEvent(this);

                    // wait 30 seconds and send phone call
                    if (await Task.WhenAny(codeSource.Task, Task.Delay(TimeSpan.FromSeconds(60))) == codeSource.Task)
                    {
                        code = codeSource.Task.Result;
                    }
                    else
                    {
                        session.Api.auth_sendCall(phone, sendCodeResponse.phone_code_hash);
                        code = await codeSource.Task;
                    }

                    while (true)
                    {
                        try {
                            await session.Api.auth_signIn(phone, sendCodeResponse.phone_code_hash, code);

                            codeSource = new TaskCompletionSource <string>();
                            WrongCodeEvent(this);
                        }
                        catch (MTProtoErrorException e) {
                            if (e.ErrorMessage.StartsWith("PHONE_NUMBER_UNOCCUPIED"))
                            {
                                // Код верен, но пользователя с таким номером телефона не существует
                                break;
                            }
                            else if (e.ErrorMessage.StartsWith("PHONE_CODE_INVALID"))
                            {
                                codeSource = new TaskCompletionSource <string>();
                                WrongCodeEvent(this);
                            }
                            else if (e.ErrorMessage.StartsWith("PHONE_CODE_EXPIRED"))
                            {
                                codeSource = new TaskCompletionSource <string>();
                                WrongCodeEvent(this);
                            }
                            else if (e.ErrorMessage.StartsWith("PHONE_CODE_EMPTY"))
                            {
                                codeSource = new TaskCompletionSource <string>();
                                WrongCodeEvent(this);
                            }
                        }
                    }

                    NeedSignupEvent(this);
                    SignUpData signUpData = await signupSource.Task;

                    while (true)
                    {
                        try {
                            Auth_authorizationConstructor authorization =
                                (Auth_authorizationConstructor)
                                await
                                session.Api.auth_signUp(phone, sendCodeResponse.phone_code_hash, code,
                                                        signUpData.firstname, signUpData.lastname);

                            await session.SaveAuthorization(authorization);

                            LoginSuccessEvent(this);
                            break;
                        }
                        catch (MTProtoErrorException e) {
                            codeSource = new TaskCompletionSource <string>();
                            WrongCodeEvent(this);
                        }
                    }
                }
            }
            catch (Exception ex) {
                logger.error("login flow exception {0}", ex);
            }
        }
Beispiel #6
0
 public async Task SaveAuthorization(auth_Authorization authorization) {
     this.authorization = (Auth_authorizationConstructor) authorization;
     SaveUser(this.authorization.user);
     Task dialogsTask = dialogs.DialogsRequest();
     Task updateTask = updates.GetStateRequest();
     await Task.WhenAll(dialogsTask, updateTask);
     save();
 }
Beispiel #7
0
        public void read(BinaryReader reader) {
            logger.info("read session...");
            id = reader.ReadUInt64();
            sequence = reader.ReadInt32();
            mainDcId = reader.ReadInt32();
            timeOffset = reader.ReadInt32();
            cachedSalt = reader.ReadUInt64();
            int count = reader.ReadInt32();

            // contacts sync marker
            ContactsStateMarker = Serializers.String.read(reader);

            dcs = new Dictionary<int, TelegramDC>(count);
            for(int i = 0; i < count; i++) {
                int endpointId = reader.ReadInt32();
                dcs.Add(endpointId, new TelegramDC(reader));
            }

            int authorizationExists = reader.ReadInt32();
            if(authorizationExists != 0) {
                authorization = (Auth_authorizationConstructor) TL.Parse<auth_Authorization>(reader);
            }


            int usersCount = reader.ReadInt32();
            users = new Dictionary<int, UserModel>(usersCount + 10);
            for (int i = 0; i < usersCount; i++) {
                users.Add(reader.ReadInt32(), new UserModel(TL.Parse<User>(reader)));
            }

            int chatsCount = reader.ReadInt32();
            chats = new Dictionary<int, ChatModel>(chatsCount + 10);
            for (int i = 0; i < chatsCount; i++) {
                chats.Add(reader.ReadInt32(), new ChatModel(TL.Parse<Chat>(reader)));
            }

            logger.info("reading updates state....");
            updates = new UpdatesProcessor(this, reader);

            logger.info("reading dialogs...");
            dialogs = new Dialogs(this, reader);
            
            files = new Files(this);
            encryptedChats = new EncryptedChats(this, reader);

            logger.info("session readed complete");
        }
Beispiel #8
0
        public void read(BinaryReader reader) {
            int endpointCount = reader.ReadInt32();
            endpoints = new List<TelegramEndpoint>(endpointCount);
            for(int i = 0; i < endpointCount; i++) {
                endpoints.Add(new TelegramEndpoint(reader));
            }

            int keyExists = reader.ReadInt32();
            if(keyExists == 0) {
                authKey = null;
            } else {
                authKey = new AuthKey(Serializers.Bytes.read(reader));    
            }

            //int fileSessionExists = reader.ReadInt32();
            //if(fileSessionExists == 0) {
            //    fileSession = null;
            //} else {
            //    fileSession = new TelegramFileSession(reader);
            //}

            int fileAuthExists = reader.ReadInt32();
            if(fileAuthExists == 0) {
                fileAuthorization = null;
            } else {
                fileAuthorization = (Auth_authorizationConstructor) TL.Parse<auth_Authorization>(reader);
            }
        }
Beispiel #9
0
 public void SaveFileAuthorization(auth_Authorization authorization) {
     this.fileAuthorization = (Auth_authorizationConstructor) authorization;
 }
Beispiel #10
0
 public void SaveFileAuthorization(auth_Authorization authorization)
 {
     this.fileAuthorization = (Auth_authorizationConstructor)authorization;
 }