public void CheckAndUpdateTransportInfoAsync(TLInt dcId, TLString host, TLInt port, Action callback, Action <TLRPCError> faultCallback = null)
        {
            LogPublicConfig(string.Format("CheckTransport dc_id={0} host={1} port={2}", dcId, host, port));

            if (dcId == null)
            {
                return;
            }
            if (TLString.IsNullOrEmpty(host))
            {
                return;
            }
            if (port == null)
            {
                return;
            }

            var dcOption = TLUtils.GetDCOption(_config, dcId);

            var transport = GetSpecialTransport(host.ToString(), port.Value, Type, new TransportSettings
            {
                DcId                  = dcId.Value,
                Secret                = TLUtils.ParseSecret(dcOption),
                AuthKey               = dcOption != null ? dcOption.AuthKey : null,
                Salt                  = dcOption != null ? dcOption.Salt : TLLong.Random(),
                SessionId             = TLLong.Random(),
                MessageIdDict         = new Dictionary <long, long>(),
                SequenceNumber        = 0,
                ClientTicksDelta      = dcOption != null ? dcOption.ClientTicksDelta : 0,
                PacketReceivedHandler = OnPacketReceivedByTransport
            });

            CheckAndUpdateTransportInfoInternalAsync(dcOption as TLDCOption78, transport, callback, faultCallback);
        }
        private static byte[] ComposeSignUpRequest(string phone, TLString phoneCodeHash, TLString phoneCode, TLString firstName, TLString lastName)
        {
            TLUtils.WriteLine("--Compose SignUp request--");
            //#1b067634x
            var signature = new byte[] { 0x34, 0x76, 0x06, 0x1b };

            var phoneNumberBytes = Encoding.UTF8.GetBytes(phone);
            var phoneNumberStr   = TLString.FromBigEndianData(phoneNumberBytes.ToArray());

            TLUtils.WriteLine("Phone: " + BitConverter.ToString(phoneNumberBytes));
            TLUtils.WriteLine("Phone serialized: " + BitConverter.ToString(phoneNumberStr.ToBytes()));

            TLUtils.WriteLine("PhoneCodeHash: " + phoneCodeHash.ToString());
            TLUtils.WriteLine("PhoneCodeHash serialized: " + BitConverter.ToString(phoneCodeHash.ToBytes()));

            TLUtils.WriteLine("PhoneCode: " + phoneCode.ToString());
            TLUtils.WriteLine("PhoneCode serialized: " + BitConverter.ToString(phoneCode.ToBytes()));

            TLUtils.WriteLine("FirstName: " + firstName.ToString());
            TLUtils.WriteLine("FirstName serialized: " + BitConverter.ToString(firstName.ToBytes()));

            TLUtils.WriteLine("LastName: " + lastName.ToString());
            TLUtils.WriteLine("LastName serialized: " + BitConverter.ToString(lastName.ToBytes()));

            return(signature
                   .Concat(phoneNumberStr.ToBytes())
                   .Concat(phoneCodeHash.ToBytes())
                   .Concat(phoneCode.ToBytes())
                   .Concat(firstName.ToBytes())
                   .Concat(lastName.ToBytes())
                   .ToArray());
        }
Example #3
0
        public static void TestBytesToHexString()
        {
            var bytesString = "7E-43-4F-4E-54-45-53-54-5F-46-49-4E-49-53-48-45-44-3A-20-53-6F-72-72-79-2C-20-63-6F-6E-74-65-73-74-20-68-61-73-20-61-6C-72-65-61-64-79-20-66-69-6E-69-73-68-65-64-2E-20-59-6F-75-20-63-61-6E-20-63-68-65-63-6B-20-72-65-73-75-6C-74-73-20-68-65-72-65-3A-20-68-74-74-70-3A-2F-2F-64-65-76-2E-73-74-65-6C-2E-63-6F-6D-2F-63-6F-6E-74-65-73-74-2F-72-6F-75-6E-64-31-2D-72-65-73-75-6C-74-73-2E-00";
            //bytesString = new string(bytesString.ToArray());
            var bytes    = Utils.StringToByteArray(bytesString.Replace("-", string.Empty));
            int position = 0;
            var tlString = new TLString().FromBytes(bytes, ref position);

            var str = tlString.ToString();
        }
        private static byte[] ComposeGetContactsRequest(TLString hash)
        {
            //#22c6aa08
            var signature = new byte[] { 0x08, 0xaa, 0xc6, 0x22 };

            TLUtils.WriteLine("Hash: " + hash.ToString());
            TLUtils.WriteLine("Hash: " + BitConverter.ToString(hash.ToBytes()));

            return(signature
                   .Concat(hash.ToBytes())
                   .ToArray());
        }
        private static byte[] ComposeSendCallRequest(TLString phone, TLString phoneCodeHash)
        {
            TLUtils.WriteLine("--Compose SendCallAsync request--");

            var signature = new byte[] { 0x64, 0x15, 0xc5, 0x03 };

            TLUtils.WriteLine("Phone: " + phone);
            TLUtils.WriteLine("Phone serialized: " + BitConverter.ToString(phone.ToBytes()));

            TLUtils.WriteLine("PhoneCodeHash: " + phoneCodeHash.ToString());
            TLUtils.WriteLine("PhoneCodeHash serialized: " + BitConverter.ToString(phoneCodeHash.ToBytes()));

            return(signature
                   .Concat(phone.ToBytes())
                   .Concat(phoneCodeHash.ToBytes())
                   .ToArray());
        }
        public void StartEditMessage(TLString text, TLMessage message)
        {
            if (text == null)
            {
                return;
            }
            if (message == null)
            {
                return;
            }

            _editedMessage = message;
            var config    = CacheService.GetConfig() as TLConfig48;
            var editUntil = config != null ? message.DateIndex + config.EditTimeLimit.Value + 5 * 60 : 0;

            if (message.FromId != null &&
                message.ToId is TLPeerUser &&
                message.FromId.Value == message.ToId.Id.Value)
            {
                editUntil = 0;
            }

            Reply = new TLMessagesContainter {
                EditMessage = _editedMessage as TLMessage25, EditUntil = editUntil
            };

            if (_editMessageTimer == null)
            {
                _editMessageTimer          = new DispatcherTimer();
                _editMessageTimer.Tick    += OnEditMessageTimerTick;
                _editMessageTimer.Interval = TimeSpan.FromSeconds(1.0);
            }

            _editMessageTimer.Start();

            IsEditingEnabled = true;
            Text             = text.ToString();

            CurrentInlineBot = null;
            ClearStickerHints();
            ClearInlineBotResults();
            ClearUsernameHints();
            ClearHashtagHints();
            ClearCommandHints();
        }
        private static byte[] ComposeSendCodeRequest(TLString phone, TLSmsType type, int apiId, TLString apiHash)
        {
            TLUtils.WriteLine("--Compose TLSendCode request--");

            var signature = new byte[] { 0x72, 0xf3, 0x6f, 0xd1 };

            TLUtils.WriteLine("Phone: " + phone.ToString());
            TLUtils.WriteLine("Phone serialized: " + BitConverter.ToString(phone.ToBytes()));

            TLUtils.WriteLine("ApiHash: " + apiHash.ToString());
            TLUtils.WriteLine("ApiHash serialized: " + BitConverter.ToString(apiHash.ToBytes()));

            return(signature
                   .Concat(phone.ToBytes())
                   .Concat(BitConverter.GetBytes((int)type))
                   .Concat(BitConverter.GetBytes(apiId))
                   .Concat(apiHash.ToBytes())
                   .ToArray());
        }
        private static byte[] ComposeSignInRequest(TLString phone, TLString phoneCodeHash, TLString phoneCode)
        {
            TLUtils.WriteLine("--Compose SignInAsync request--");
            //#bcd51581
            var signature = new byte[] { 0x81, 0x15, 0xd5, 0xbc };

            TLUtils.WriteLine("Phone: " + phone.ToString());
            TLUtils.WriteLine("Phone serialized: " + BitConverter.ToString(phone.ToBytes()));

            TLUtils.WriteLine("PhoneCodeHash: " + phoneCodeHash.ToString());
            TLUtils.WriteLine("PhoneCodeHash serialized: " + BitConverter.ToString(phoneCodeHash.ToBytes()));

            TLUtils.WriteLine("PhoneCode: " + phoneCode.ToString());
            TLUtils.WriteLine("PhoneCode serialized: " + BitConverter.ToString(phoneCode.ToBytes()));

            return(signature
                   .Concat(phone.ToBytes())
                   .Concat(phoneCodeHash.ToBytes())
                   .Concat(phoneCode.ToBytes())
                   .ToArray());
        }
        private void SearchAsync(TLString text, DateTime?date, TLUserBase from, TLInt maxId, TLInt limit)
        {
            var maxDate = date != null
                ? TLUtils.DateToUniversalTimeTLInt(MTProtoService.ClientTicksDelta, date.Value.AddDays(1).AddSeconds(-1))
                : null;

            if (TLString.IsNullOrEmpty(text) && date.HasValue)
            {
                IsWorking = true;
                MTProtoService.GetHistoryAsync(
                    Stopwatch.StartNew(),
                    Peer,
                    TLUtils.InputPeerToPeer(Peer, StateService.CurrentUserId),
                    false, maxDate ?? new TLInt(0),
                    new TLInt(0),
                    maxId,
                    new TLInt(1),
                    result => BeginOnUIThread(() =>
                {
                    var messagesSlice = result as TLMessagesSlice;
                    if (messagesSlice != null)
                    {
                        messagesSlice.Count = new TLInt(messagesSlice.Messages.Count);
                    }

                    IsWorking = false;

                    ProcessMessages(result.Messages);

                    var key = GetKey(text.ToString(), date, from);

                    TLMessagesBase cachedResult;
                    if (_searchResults.TryGetValue(key, out cachedResult))
                    {
                        var lastId = cachedResult.Messages.Last().Id;
                        if (lastId.Value == maxId.Value)
                        {
                            var cachedUsersIndex = new Dictionary <int, int>();
                            foreach (var cachedUser in cachedResult.Users)
                            {
                                cachedUsersIndex[cachedUser.Index] = cachedUser.Index;
                            }
                            foreach (var user in result.Users)
                            {
                                if (!cachedUsersIndex.ContainsKey(user.Index))
                                {
                                    cachedResult.Users.Add(user);
                                }
                            }
                            var cachedChatsIndex = new Dictionary <int, int>();
                            foreach (var cachedChat in cachedResult.Chats)
                            {
                                cachedChatsIndex[cachedChat.Index] = cachedChat.Index;
                            }
                            foreach (var chat in result.Chats)
                            {
                                if (!cachedChatsIndex.ContainsKey(chat.Index))
                                {
                                    cachedResult.Chats.Add(chat);
                                }
                            }
                            foreach (var message in result.Messages)
                            {
                                cachedResult.Messages.Add(message);
                            }

                            SearchMessages.ResultLoaded(_currentResultIndex, cachedResult.Messages.Count);
                        }
                    }
                    else
                    {
                        _searchResults[key] = result;
                        var currentKey      = GetKey(SearchMessages.Text, SearchMessages.Date, SearchMessages.From);
                        if (string.Equals(key, currentKey, StringComparison.Ordinal))
                        {
                            ContinueSearch(result);
                        }
                    }
                }),
                    error => BeginOnUIThread(() =>
                {
                    IsWorking = false;
                    Execute.ShowDebugMessage("messages.search error " + error);
                }));
            }
            else
            {
                IsWorking = true;
                MTProtoService.SearchAsync(
                    Peer,
                    text,
                    from == null ? null : from.ToInputUser(),
                    new TLInputMessagesFilterEmpty(),
                    new TLInt(0),
                    maxDate ?? new TLInt(0),
                    new TLInt(0),
                    maxId,
                    limit,
                    new TLInt(0),
                    result => BeginOnUIThread(() =>
                {
                    IsWorking = false;

                    ProcessMessages(result.Messages);

                    var key = GetKey(text.ToString(), date, from);

                    TLMessagesBase cachedResult;
                    if (_searchResults.TryGetValue(key, out cachedResult))
                    {
                        var lastId = cachedResult.Messages.Last().Id;
                        if (lastId.Value == maxId.Value)
                        {
                            var cachedUsersIndex = new Dictionary <int, int>();
                            foreach (var cachedUser in cachedResult.Users)
                            {
                                cachedUsersIndex[cachedUser.Index] = cachedUser.Index;
                            }
                            foreach (var user in result.Users)
                            {
                                if (!cachedUsersIndex.ContainsKey(user.Index))
                                {
                                    cachedResult.Users.Add(user);
                                }
                            }
                            var cachedChatsIndex = new Dictionary <int, int>();
                            foreach (var cachedChat in cachedResult.Chats)
                            {
                                cachedChatsIndex[cachedChat.Index] = cachedChat.Index;
                            }
                            foreach (var chat in result.Chats)
                            {
                                if (!cachedChatsIndex.ContainsKey(chat.Index))
                                {
                                    cachedResult.Chats.Add(chat);
                                }
                            }
                            foreach (var message in result.Messages)
                            {
                                cachedResult.Messages.Add(message);
                            }

                            SearchMessages.ResultLoaded(_currentResultIndex, cachedResult.Messages.Count);
                        }
                    }
                    else
                    {
                        _searchResults[key] = result;
                        var currentKey      = GetKey(SearchMessages.Text, SearchMessages.Date, SearchMessages.From);
                        if (string.Equals(key, currentKey, StringComparison.Ordinal))
                        {
                            ContinueSearch(result);
                        }
                    }
                }),
                    error => BeginOnUIThread(() =>
                {
                    IsWorking = false;
                    Execute.ShowDebugMessage("messages.search error " + error);
                }));
            }
        }
        public Telegram.Api.WindowsPhone.Tuple <TLDecryptedMessageBase, TLObject> GetDecryptedMessageAndObject(TLString text, TLDecryptedMessageMediaBase media, TLEncryptedChat chat, bool delaySeq = false)
        {
            TLDecryptedMessageBase decryptedMessage;
            TLObject decryptedObject;

            var random   = new Random();
            var randomId = TLLong.Random();

            var textString  = text.ToString();
            var randomBytes = new byte[] { };

            if (string.IsNullOrEmpty(textString) ||
                textString.Length < 16)
            {
                randomBytes = new byte[16];
                random.NextBytes(randomBytes);
            }

            var encryptedChat17 = chat as TLEncryptedChat17;

            if (encryptedChat17 != null)
            {
                var cachedEncryptedChat17 = (TLEncryptedChat17)CacheService.GetEncryptedChat(encryptedChat17.Id);

                TLInt inSeqNo;
                TLInt outSeqNo;
                if (!delaySeq)
                {
                    inSeqNo  = TLUtils.GetInSeqNo(MTProtoService.CurrentUserId, cachedEncryptedChat17);
                    outSeqNo = TLUtils.GetOutSeqNo(MTProtoService.CurrentUserId, cachedEncryptedChat17);

                    cachedEncryptedChat17.RawOutSeqNo = new TLInt(cachedEncryptedChat17.RawOutSeqNo.Value + 1);
                }
                else
                {
                    inSeqNo  = new TLInt(-1);
                    outSeqNo = new TLInt(-1);
                }

                var decryptedMessage17 = new TLDecryptedMessage17
                {
                    Media       = media,
                    Message     = text,
                    RandomId    = randomId,
                    RandomBytes = TLString.FromBigEndianData(randomBytes),

                    ChatId = chat.Id,
                    FromId = new TLInt(StateService.CurrentUserId),
                    Out    = new TLBool(true),
                    Unread = new TLBool(true),
                    Date   = TLUtils.DateToUniversalTimeTLInt(MTProtoService.ClientTicksDelta, DateTime.Now),
                    Status = MessageStatus.Sending,

                    TTL      = chat.MessageTTL ?? new TLInt(0),
                    InSeqNo  = inSeqNo,
                    OutSeqNo = outSeqNo
                };

                var decryptedMessageLayer17 = new TLDecryptedMessageLayer17();
                decryptedMessageLayer17.Layer       = encryptedChat17.Layer;
                decryptedMessageLayer17.InSeqNo     = inSeqNo;
                decryptedMessageLayer17.OutSeqNo    = outSeqNo;
                decryptedMessageLayer17.RandomBytes = TLString.Empty;
                decryptedMessageLayer17.Message     = decryptedMessage17;

                decryptedMessage = decryptedMessage17;
                decryptedObject  = decryptedMessageLayer17;
            }
            else
            {
                var decryptedMessage8 = new TLDecryptedMessage
                {
                    Media       = media,
                    Message     = text,
                    RandomId    = randomId,
                    RandomBytes = TLString.FromBigEndianData(randomBytes),

                    ChatId = chat.Id,
                    FromId = new TLInt(StateService.CurrentUserId),
                    Out    = new TLBool(true),
                    Unread = new TLBool(true),
                    Date   = TLUtils.DateToUniversalTimeTLInt(MTProtoService.ClientTicksDelta, DateTime.Now),
                    Status = MessageStatus.Sending,

                    TTL = chat.MessageTTL
                };

                decryptedMessage = decryptedMessage8;
                decryptedObject  = decryptedMessage;
            }

            return(new Telegram.Api.WindowsPhone.Tuple <TLDecryptedMessageBase, TLObject>(decryptedMessage, decryptedObject));
        }
        private void SearchAsync(TLString text, TLInt maxId, TLInt limit)
        {
            IsWorking = true;
            MTProtoService.SearchAsync(Peer, text, new TLInputMessagesFilterEmpty(), new TLInt(0), new TLInt(Int32.MaxValue), new TLInt(0), maxId, limit,
                                       result => BeginOnUIThread(() =>
            {
                IsWorking = false;

                ProcessRepliesAndAudio(result.Messages);

                var key = text.ToString();
                TLMessagesBase cachedResult;
                if (_searchResults.TryGetValue(key, out cachedResult))
                {
                    var lastId = cachedResult.Messages.Last().Id;
                    if (lastId.Value == maxId.Value)
                    {
                        var cachedUsersIndex = new Dictionary <int, int>();
                        foreach (var cachedUser in cachedResult.Users)
                        {
                            cachedUsersIndex[cachedUser.Index] = cachedUser.Index;
                        }
                        foreach (var user in result.Users)
                        {
                            if (!cachedUsersIndex.ContainsKey(user.Index))
                            {
                                cachedResult.Users.Add(user);
                            }
                        }
                        var cachedChatsIndex = new Dictionary <int, int>();
                        foreach (var cachedChat in cachedResult.Chats)
                        {
                            cachedChatsIndex[cachedChat.Index] = cachedChat.Index;
                        }
                        foreach (var chat in result.Chats)
                        {
                            if (!cachedChatsIndex.ContainsKey(chat.Index))
                            {
                                cachedResult.Chats.Add(chat);
                            }
                        }
                        foreach (var message in result.Messages)
                        {
                            cachedResult.Messages.Add(message);
                        }

                        SearchMessages.ResultLoaded(_currentResultIndex, cachedResult.Messages.Count);
                    }
                }
                else
                {
                    _searchResults[key] = result;

                    if (string.Equals(key, SearchMessages.Text, StringComparison.Ordinal))
                    {
                        ContinueSearch(result);
                    }
                }
            }),
                                       error => BeginOnUIThread(() =>
            {
                IsWorking = false;
                Execute.ShowDebugMessage("messages.search error " + error);
            }));
        }