public static Telegram.Api.WindowsPhone.Tuple <TLDecryptedMessageService, TLObject> GetDecryptedServiceMessageAndObject(TLDecryptedMessageActionBase action, TLEncryptedChat chat, TLInt currentUserId, ICacheService cacheService)
        {
            var mtProtoService = IoC.Get <IMTProtoService>();

            TLDecryptedMessageService decryptedMessage;
            TLObject decryptedObject;

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

            var randomBytes = new byte[16];

            random.NextBytes(randomBytes);

            var encryptedChat17 = chat as TLEncryptedChat17;

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

                var inSeqNo  = TLUtils.GetInSeqNo(currentUserId, cachedEncryptedChat17);
                var outSeqNo = TLUtils.GetOutSeqNo(currentUserId, cachedEncryptedChat17);

                cachedEncryptedChat17.RawOutSeqNo = new TLInt(cachedEncryptedChat17.RawOutSeqNo.Value + 1);
                var decryptedMessage17 = new TLDecryptedMessageService17
                {
                    Action      = action,
                    RandomId    = randomId,
                    RandomBytes = TLString.FromBigEndianData(randomBytes),

                    ChatId = chat.Id,
                    FromId = 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 TLDecryptedMessageService
                {
                    Action      = action,
                    RandomId    = randomId,
                    RandomBytes = TLString.FromBigEndianData(randomBytes),

                    ChatId = chat.Id,
                    FromId = 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 <TLDecryptedMessageService, TLObject>(decryptedMessage, decryptedObject));
        }
        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));
        }
Ejemplo n.º 3
0
        public void Resend(TLDecryptedMessage message)
#endif
        {
            if (message == null)
            {
                return;
            }

            var chat = Chat as TLEncryptedChat;

            if (chat == null)
            {
                return;
            }

            TLObject obj = message;
            var      decryptedMessage17 = message as TLDecryptedMessage17;

            if (decryptedMessage17 != null)
            {
                var encryptedChat17 = chat as TLEncryptedChat17;
                if (encryptedChat17 == null)
                {
                    return;
                }

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

                obj = messageLayer17;
            }

            message.Status = MessageStatus.Sending;

            if (message.Media is TLDecryptedMessageMediaEmpty)
            {
                SendEncrypted(chat, obj, MTProtoService, CacheService);
            }
            else
            {
                message.Media.UploadingProgress = 0.001;
                if (message.Media is TLDecryptedMessageMediaPhoto)
                {
                    SendPhotoInternal(null, obj);
                }
                else if (message.Media is TLDecryptedMessageMediaVideo)
                {
                    SendVideoInternal(null, obj);
                }
                else if (message.Media is TLDecryptedMessageMediaDocument)
                {
#if WP81
                    var file = await GetStorageFile(message.Media);

                    if (file != null)
                    {
                        SendDocumentInternal(file, message);
                    }
                    else
                    {
                        MessageBox.Show(AppResources.UnableToAccessDocument, AppResources.Error, MessageBoxButton.OK);
                        message.Status = MessageStatus.Failed;
                        DeleteMessage(message);
                        return;
                    }
#else
                    SendDocumentInternal(null, message);
#endif
                }
                else if (message.Media is TLDecryptedMessageMediaAudio)
                {
#if WP8
                    SendAudioInternal(obj);
#endif
                }
                else if (message.Media is TLDecryptedMessageMediaContact)
                {
                }
            }

            message.NotifyOfPropertyChange(() => message.Status);
        }