Пример #1
0
        public void SendMessage(String text, bool encrypt = false)
        {
            if (this._chat == null)
            {
                return;
            }

            new Thread(async() =>
            {
                while (this._sending)
                {
                    Thread.Sleep(10);
                }

                String chat = "user_id=" + this._chat.Message.UserID;
                if (this._chat.Message.ChatID > 0)
                {
                    chat = "chat_id=" + this._chat.Message.ChatID;
                }

                if (!String.IsNullOrEmpty(text))
                {
                    this._sending = true;

                    this.InvokeEx(t => t.Invalidate());

                    if (encrypt)
                    {
                        if (String.IsNullOrEmpty(this._chat.CryptKey))
                        {
                            this.InvokeEx(t =>
                            {
                                DecryptMessagesForm frm = new DecryptMessagesForm();
                                if (frm.ShowDialog(t.FindForm()) == DialogResult.OK)
                                {
                                    t._chat.CryptKey = frm.Passphrase;
                                }
                            });
                        }

                        if (!String.IsNullOrEmpty(this._chat.CryptKey))
                        {
                            text = Message.Encrypt(text, this._chat.CryptKey);
                        }
                    }

                    await Message.Send(chat, text);

                    this._sending = false;

                    this.InvokeEx(t => t.Invalidate());
                }
            }).Start();
        }
Пример #2
0
        private void OpenChat(AppEvent e)
        {
            this._chat = (Dialog)e.Data[0];

            (new Thread(async() =>
            {
                List <Message> messages = await this._chat.GetMessages();

                List <int> ids = new List <int>();

                lock (_messages_lock)
                {
                    this._messages = messages;

                    if (this._chat.Crypt)
                    {
                        this.InvokeEx(t =>
                        {
                            DecryptMessagesForm frm = new DecryptMessagesForm();
                            if (frm.ShowDialog(t.FindForm()) == DialogResult.OK)
                            {
                                t._chat.CryptKey = frm.Passphrase;
                            }
                        });
                    }

                    this._so = 0;
                    this._oldH = 0;
                    if (this._messages != null)
                    {
                        this._messages = this._messages.Reverse <Message>().ToList();

                        foreach (Message msg in this._messages)
                        {
                            if (!ids.Contains(msg.UserID))
                            {
                                ids.Add(msg.UserID);
                            }

                            if (msg.CryptedNow && this._chat.CryptKey != null)
                            {
                                msg.Decrypt(this._chat.CryptKey);
                            }

                            if (msg.Attachments != null && msg.Attachments.Length > 0)
                            {
                                foreach (Attachment a in msg.Attachments)
                                {
                                    if (a.Type == "photo" && a.Photo != null)
                                    {
                                        a.Photo.Load();
                                    }
                                }
                            }
                        }
                    }
                }

                User[] users = await User.GetAll(ids);

                if (users != null)
                {
                    foreach (User u in users)
                    {
                        u.GetPhoto();
                    }
                }

                this._loaded = true;

                this._heightCalculated = false;

                AppEvents.Dispatch(AppEventType.ChatOpened, this._chat);

                this.InvokeEx(t =>
                {
                    t.Invalidate();
                });
            })).Start();
        }