Exemple #1
0
        public List <Contact> ReceiveContacts()
        {
            Message mess = new Message();

            mess.Text         = "";
            mess.Priorety     = "loadContacts";
            mess.LoginSend    = user.Login;
            mess.LoginReceive = user.Login;
            mess.Moment       = DateTime.Now.ToLongTimeString();
            mess.Answer       = "";
            Transfer.SendTCP(socket, new DataMessage()
            {
                Message = mess
            });
            Data data = Transfer.ReceiveTCP(socket);

            if (data is DataContacts)
            {
                return(((DataContacts)data).ListC);
            }
            else
            {
                return(null);
            }
        }
Exemple #2
0
 public async void ReceiveAsync()
 {
     try
     {
         await Task.Run(() =>
         {
             while (true)
             {
                 int count    = listViewMessages.Items.Count;
                 Message mess = new Message();
                 Data data    = Transfer.ReceiveTCP(socket);
                 if (data is DataFile)
                 {
                     fileByte = ((DataFile)data).FileByte;
                     LoadFileByte(fileByte);
                 }
                 else
                 {
                     mess = ((DataMessage)data).Message;
                     CheckReceives(mess);
                     string buff     = mess.Text;
                     Color color     = RandColor();
                     color           = ContactColor(mess.LoginSend, color);
                     string dateTime = DateTime.Now.ToLongTimeString();
                     string print    = "";
                     for (int i = 0; i < 3; i++)
                     {
                         if (i == 0)
                         {
                             print = "";
                         }
                         if (i == 1)
                         {
                             print = dateTime;
                         }
                         if (i == 2)
                         {
                             print = buff;
                         }
                         ListViewItem it = new ListViewItem(print);
                         if (i == 2)
                         {
                             it.ForeColor = color;
                         }
                         if (listViewMessages.InvokeRequired)
                         {
                             listViewMessages.Invoke(new Action(() => listViewMessages.Items.Add(it)));
                             listViewMessages.Invoke(new Action(() => listViewMessages.EnsureVisible(listViewMessages.Items.Count - 1)));
                         }
                     }
                 }
             }
         });
     }
     catch (Exception) { throw; }
 }
Exemple #3
0
        private void buttonOK_Click(object sender, EventArgs e)
        {
            Regex regLog  = new Regex("^[A-ZА-Я]{1}\\S{1,8}$");
            Regex regPass = new Regex("^\\S{1,8}$");

            user = new User();

            if (!regLog.IsMatch(textBoxLogin.Text))
            {
                MessageBox.Show("Login entered incorrectly", "Warning",
                                MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            else
            {
                user.Login = textBoxLogin.Text;
            }
            user.Tag = "avtorization";
            if (!regPass.IsMatch(textBoxPass.Text))
            {
                MessageBox.Show("Password entered incorrectly", "Warning",
                                MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            else
            {
                user.Password = Hash(textBoxPass.Text);
            }
            Message answer = new Message();

            Transfer.SendTCP(tcp, new DataUser()
            {
                User = user
            });
            Data data = Transfer.ReceiveTCP(tcp);

            if (data is DataMessage)
            {
                answer = ((DataMessage)data).Message;
            }
            if (answer.Answer == "No")
            {
                MessageBox.Show("Login or password is not correct, or such a login is already in the chat", "Warning",
                                MessageBoxButtons.OK, MessageBoxIcon.Warning);
                textBoxLogin.Clear();
                textBoxPass.Clear();
                return;
            }
            else
            {
                DialogResult = DialogResult.OK;
            }
        }
Exemple #4
0
 public void buttonExit_Click(object sender, EventArgs e)
 {
     if (socket.Connected)
     {
         if (message == null)
         {
             message = new Message();
         }
         message.Text     = "";
         message.Priorety = "exit";
         message.Answer   = "";
         message.Moment   = DateTime.Now.ToLongTimeString();
         buttonSend_Click(this, new EventArgs());
         Close();
     }
     else
     {
         Close();
     }
 }
Exemple #5
0
        private void buttonOK_Click(object sender, EventArgs e)
        {
            Regex regLog  = new Regex("^[A-ZА-Я]{1}\\S{1,8}$");
            Regex regPass = new Regex("^\\S{1,8}$");

            if (client.Login == null)
            {
                tcp  = (Owner as Form1).socket;
                user = new User();
                bool res = true;
                while (res)
                {
                    if (!regLog.IsMatch(textBoxLogin.Text))
                    {
                        MessageBox.Show("Login entered incorrectly", "Warning",
                                        MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    else
                    {
                        user.Login = textBoxLogin.Text;
                    }
                    user.Tag = "create";
                    if (!regPass.IsMatch(textBoxPass.Text))
                    {
                        MessageBox.Show("Password entered incorrectly", "Warning",
                                        MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    else
                    {
                        while (textBoxPass.Text != textBoxRepeat.Text)
                        {
                            MessageBox.Show("Enter your password again", "Warning",
                                            MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            textBoxPass.Clear();
                            textBoxRepeat.Clear();
                        }
                        user.Password = Hash(textBoxPass.Text);
                    }
                    user.Birthday = dateTimePickerBirthday.Value.ToShortDateString();

                    Message answer = new Message();
                    Transfer.SendTCP(tcp, new DataUser()
                    {
                        User = user
                    });
                    Data data = Transfer.ReceiveTCP(tcp);
                    if (data is DataMessage)
                    {
                        answer = ((DataMessage)data).Message;
                    }
                    if (answer.Answer == "No")
                    {
                        MessageBox.Show("There is such a Login. Please enter a different Login", "Warning",
                                        MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        res = true;
                        textBoxLogin.Clear();
                        textBoxPass.Clear();
                        textBoxRepeat.Clear();
                        dateTimePickerBirthday.Format = DateTimePickerFormat.Custom;
                    }
                    else
                    {
                        res = false;
                    }
                }
            }
            else
            {
                if (!regLog.IsMatch(textBoxLogin.Text))
                {
                    MessageBox.Show("Login entered incorrectly", "Warning",
                                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                else
                {
                    client.Login = textBoxLogin.Text;
                }
                if (client.Login == "Admin")
                {
                    if (!regPass.IsMatch(textBoxPass.Text))
                    {
                        MessageBox.Show("Password entered incorrectly", "Warning",
                                        MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    else
                    {
                        while (textBoxPass.Text != textBoxRepeat.Text)
                        {
                            MessageBox.Show("Enter your password again", "Warning",
                                            MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            textBoxPass.Clear();
                            textBoxRepeat.Clear();
                        }
                        client.Password = Hash(textBoxPass.Text);
                    }
                }
                client.Birthday = dateTimePickerBirthday.Value.ToShortDateString();
            }

            DialogResult = DialogResult.OK;
        }
Exemple #6
0
        private void Form1_Load(object sender, EventArgs e)
        {
            user = new User();
            ConnectServer();
            Input inp = new Input();

            try
            {
                if (inp.ShowDialog(this) == DialogResult.OK)
                {
                    if (socket.Connected)
                    {
                        user = inp.user;
                        if (user.Login == "Admin")
                        {
                            listByte = new List <byte[]>();
                            usersB   = new List <User>();
                            usersB   = ReceiveUsers();
                            listMess = new List <Message>();
                            listMess = ReceiveMessages();
                            listViewContacts.Visible  = false;
                            listViewClients.Visible   = true;
                            listViewStatistic.Visible = false;
                            buttonCloseGroup.Visible  = false;
                            buttonGroupChat.Visible   = false;
                            buttonClearSt.Enabled     = false;
                            buttonBackChat.Enabled    = false;
                            groupBox2.Text            = "CONTACTS CLIENTS";
                        }
                        else
                        {
                            listViewContacts.Visible           = true;
                            listViewClients.Visible            = false;
                            listViewStatistic.Visible          = false;
                            buttonBackChat.Visible             = false;
                            buttonClearSt.Visible              = false;
                            statisticToolStripMenuItem.Visible = false;
                            groupBox2.Text = "MY CONTACTS";
                        }
                        message              = new Message();
                        contacts             = new List <Contact>();
                        contacts             = ReceiveContacts();
                        tempContacts         = new List <Contact>();
                        contByte             = new List <byte[]>();
                        fileByte             = new List <byte[]>();
                        message.Priorety     = "other";
                        message.LoginSend    = user.Login;
                        message.LoginReceive = "";
                        message.Answer       = "";
                        listViewMessages.Items.Clear();
                        listViewMessages.Columns[0].Width = listViewMessages.Width - 5;
                        listViewMessages.Columns[0].Text  = "SERVER CONNEСTOR: " + DateTime.Now.ToString();
                        buttonSend.Enabled         = false;
                        buttonSaveFile.Enabled     = false;
                        textBoxMessage.Enabled     = true;
                        buttonSaveContacts.Enabled = false;
                        textBoxFileName.Enabled    = false;
                        buttonCloseGroup.Enabled   = false;
                        this.Text = "MYOWNCHAT: " + $"Login - {user.Login}";
                        StartChat();
                    }
                }
                else
                {
                    inp.Close();
                    Close();
                }
            }
            catch { }
        }
Exemple #7
0
        public void CheckReceives(Message mess)
        {
            string messCheck = mess.Priorety;

            switch (messCheck)
            {
            case "group":
                string log = mess.LoginSend;
                if (log == user.Login)
                {
                    logs = mess.LoginReceive;
                    Message messAns = new Message();
                    string  regex   = "\\b" + $"{user.Login}" + "\\b";
                    logs                 = Regex.Replace(logs, regex, log);
                    messAns.Priorety     = "group";
                    messAns.LoginSend    = user.Login;
                    messAns.LoginReceive = logs;
                    messAns.Moment       = DateTime.Now.ToLongTimeString();
                    message              = messAns;
                }
                else
                {
                    if (countMess == 0)
                    {
                        if (MessageBox.Show($"{log} sent a request for a group chat", "Inquiry", MessageBoxButtons.OKCancel,
                                            MessageBoxIcon.Question) == DialogResult.OK)
                        {
                            countMess++;
                            Message messAns = new Message();
                            logs = mess.LoginReceive;
                            string regex = "\\b" + $"{user.Login}" + "\\b";
                            logs                 = Regex.Replace(logs, regex, log);
                            messAns.Priorety     = "group";
                            messAns.LoginSend    = user.Login;
                            messAns.LoginReceive = logs;
                            messAns.Moment       = DateTime.Now.ToLongTimeString();
                            message              = messAns;
                        }
                        else
                        {
                            Message messAns = new Message();
                            messAns.Text         = "Sorry, i can't, busy!";
                            messAns.Priorety     = "private";
                            messAns.LoginSend    = user.Login;
                            messAns.LoginReceive = log;
                            messAns.Moment       = DateTime.Now.ToLongTimeString();
                            Transfer.SendTCP(socket, new DataMessage()
                            {
                                Message = messAns
                            });
                        }
                    }
                }
                break;

            case "close":
            {
                Message messAns = new Message();
                messAns.Text         = $"Sorry, group \"{nameGroup}\" CLOSE!";
                messAns.Priorety     = "other";
                messAns.LoginSend    = user.Login;
                messAns.LoginReceive = "";
                messAns.Answer       = "";
                messAns.Moment       = DateTime.Now.ToLongTimeString();
                message = messAns;
                Transfer.SendTCP(socket, new DataMessage()
                    {
                        Message = message
                    });
                break;
            }

            default:
                break;
            }
        }