Exemplo n.º 1
0
        private void GetInfoAboutFiles()
        {
            try
            {
                SocketHandler sh = new SocketHandler();
                sh.sendData("213");
                sh.getMsgCode();
                int amount = int.Parse(sh.recvDataWithSize());

                for (int i = 0; i < amount; i++)
                {
                    int    length   = sh.getDataLen(3);
                    string fileName = sh.recvDataWithGivenSize(length);
                    length = sh.getDataLen(2);
                    string fileType = sh.recvDataWithGivenSize(length);
                    length = sh.getDataLen(2);
                    string fileSize = sh.recvDataWithGivenSize(length);
                    length = sh.getDataLen(2);
                    string fileLoc     = sh.recvDataWithGivenSize(length);
                    int    isEncrypted = int.Parse(sh.recvDataWithGivenSize(1));

                    AddRowToGrid(fileName, fileType, fileLoc, fileSize, isEncrypted);
                }
            }
            catch (SocketException ex)
            {
                Notifier n = AsyncBlockingSocket.initNotifier();
                n.ShowError(ex.ToString());
            }
            catch (Exception ex)
            {
                Notifier n = AsyncBlockingSocket.initNotifier();
                n.ShowError(ex.ToString());
            }
        }
Exemplo n.º 2
0
        private void loginBtn_Click(object sender, RoutedEventArgs e)
        {
            if (usernameBox.Text == "" || passwordBox.Password == "")
            {
                Notifier n = new Notifier(cfg =>
                {
                    cfg.PositionProvider = new WindowPositionProvider(
                        parentWindow: this,
                        corner: Corner.BottomRight,
                        offsetX: 10,
                        offsetY: 10);

                    cfg.LifetimeSupervisor = new TimeAndCountBasedLifetimeSupervisor(
                        notificationLifetime: TimeSpan.FromSeconds(2),
                        maximumNotificationCount: MaximumNotificationCount.FromCount(1));

                    cfg.Dispatcher = Application.Current.Dispatcher;
                });

                n.ShowWarning("Please fill all required values.");
            }
            else
            {
                string hashPassword = ComputeHash(passwordBox.Password, new MD5CryptoServiceProvider());

                string data = "200";
                data += usernameBox.Text.Length.ToString().PadLeft(2, '0');
                data += SocketHandler.Encipher(usernameBox.Text, "cipher");
                data += hashPassword.Length.ToString().PadLeft(2, '0');
                data += SocketHandler.Encipher(hashPassword, "cipher");

                SocketHandler sh = new SocketHandler();
                try
                {
                    sh.sendData(data);
                    string result = sh.recvData();
                    if (result.Equals("1000"))
                    {
                        LoadingPage app = new LoadingPage();
                        this.Close();
                        app.Show();
                    }
                    else
                    {
                        Notifier n = AsyncBlockingSocket.initNotifier();
                        n.ShowWarning("Wrong username or password");
                    }
                }
                catch (SocketException ex)
                {
                    Notifier n = AsyncBlockingSocket.initNotifier();
                    n.ShowError(ex.Message);
                }
                catch (Exception ex)
                {
                    Notifier n = AsyncBlockingSocket.initNotifier();
                    n.ShowError(ex.Message);
                }
            }
        }
Exemplo n.º 3
0
        private void deleteBtn_Click(object sender, RoutedEventArgs e)
        {
            Button btn       = sender as Button;
            string username  = btn.DataContext as string;
            string groupName = groupNameLbl.Content.ToString();

            string data = "212";

            data += username.Length.ToString().PadLeft(2, '0');
            data += SocketHandler.Encipher(username, "cipher");;
            data += groupName.Length.ToString().PadLeft(2, '0');
            data += SocketHandler.Encipher(groupName, "cipher");;

            SocketHandler sh = new SocketHandler();

            try
            {
                sh.sendData(data);
            }
            catch (SocketException ex)
            {
                Notifier n = AsyncBlockingSocket.initNotifier();
                n.ShowError(ex.ToString());
            }
            catch (Exception ex)
            {
                Notifier n = AsyncBlockingSocket.initNotifier();
                n.ShowError(ex.ToString());
            }

            GetInfoAboutGroups();
            updateView(groupName);
        }
Exemplo n.º 4
0
        private bool isUserConntected(string username)
        {
            List <string> users = new List <string>();

            try
            {
                SocketHandler sh = new SocketHandler();
                sh.sendData("210");

                sh.getMsgCode();
                int amount = int.Parse(sh.recvDataWithSize());
                for (int i = 0; i < amount; i++)
                {
                    string connectedUser = sh.recvDataWithSize();
                    users.Add(connectedUser);
                }
            }
            catch (SocketException ex)
            {
                Notifier n = AsyncBlockingSocket.initNotifier();
                n.ShowError(ex.ToString());
            }
            catch (Exception ex)
            {
                Notifier n = AsyncBlockingSocket.initNotifier();
                n.ShowError(ex.ToString());
            }

            if (users.Contains(username))
            {
                return(true);
            }
            return(false);
        }
Exemplo n.º 5
0
        public LoginPage()
        {
            WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;

            AsyncBlockingSocket abs = new AsyncBlockingSocket();
            Task task = Task.Run((Action)abs.listenForConnection);

            InitializeComponent();
        }
Exemplo n.º 6
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            DataGrid    dataGridVar = dataGrid;
            DataGridRow Row         = (DataGridRow)dataGridVar.ItemContainerGenerator.ContainerFromIndex(dataGridVar.SelectedIndex);

            DataGridCell atUserCol   = (DataGridCell)dataGridVar.Columns[2].GetCellContent(Row).Parent;
            DataGridCell fileNameCol = (DataGridCell)dataGridVar.Columns[0].GetCellContent(Row).Parent;
            string       atUser      = ((TextBlock)atUserCol.Content).Text;
            string       fileName    = ((TextBlock)fileNameCol.Content).Text;

            if (isUserConntected(atUser))
            {
                string data = "214";
                data += fileName.Length.ToString().PadLeft(2, '0');
                data += SocketHandler.Encipher(fileName, "cipher");
                data += atUser.Length.ToString().PadLeft(2, '0');
                data += SocketHandler.Encipher(atUser, "cipher");

                SocketHandler sh = new SocketHandler();
                try
                {
                    sh.sendData(data);
                }
                catch (SocketException ex)
                {
                    Notifier n = AsyncBlockingSocket.initNotifier();
                    n.ShowError(ex.ToString());
                }
                catch (Exception ex)
                {
                    Notifier n = AsyncBlockingSocket.initNotifier();
                    n.ShowError(ex.ToString());
                }
            }
            else
            {
                Notifier n = new Notifier(cfg =>
                {
                    cfg.DisplayOptions.TopMost = true;

                    cfg.PositionProvider = new WindowPositionProvider(
                        parentWindow: Application.Current.MainWindow,
                        corner: Corner.BottomRight,
                        offsetX: 10,
                        offsetY: 10);

                    cfg.LifetimeSupervisor = new TimeAndCountBasedLifetimeSupervisor(
                        notificationLifetime: TimeSpan.FromSeconds(2),
                        maximumNotificationCount: MaximumNotificationCount.FromCount(1));

                    cfg.Dispatcher = Application.Current.Dispatcher;
                });

                n.ShowError("The user is not connected.");
            }
        }
Exemplo n.º 7
0
        private void PlaceholdersListBox_OnPreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            var item = ItemsControl.ContainerFromElement(sender as ListBox, e.OriginalSource as DependencyObject) as ListBoxItem;

            if (item != null)
            {
                usersCombo.Items.Clear();
                usersList.Items.Clear();

                groupNameLbl.Content = item.Content;

                string[] users = groups[item.Content.ToString()][0].Split('/');
                for (int i = 0; i < users.Length; i++)
                {
                    if (users[i] != "")
                    {
                        usersList.Items.Add(users[i]);
                    }
                }

                try
                {
                    SocketHandler sh = new SocketHandler();
                    sh.sendData("210");

                    sh.getMsgCode();
                    int amount = int.Parse(sh.recvDataWithSize());
                    for (int i = 0; i < amount; i++)
                    {
                        string username = sh.recvDataWithSize();
                        if (!usersList.Items.Contains(username))
                        {
                            usersCombo.Items.Add(username);
                        }
                    }
                }
                catch (SocketException ex)
                {
                    Notifier n = AsyncBlockingSocket.initNotifier();
                    n.ShowError(ex.ToString());
                }
                catch (Exception ex)
                {
                    Notifier n = AsyncBlockingSocket.initNotifier();
                    n.ShowError(ex.ToString());
                }
            }
        }
Exemplo n.º 8
0
        private void sendBtn_Click(object sender, RoutedEventArgs e)
        {
            string fileName = " ";

            if (ipLbl.Content != null)
            {
                Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
                // Display OpenFileDialog by calling ShowDialog method
                Nullable <bool> result = dlg.ShowDialog();

                // Get the selected file name and display in a TextBox
                if (result == true)
                {
                    // Open document
                    fileName = dlg.FileName;

                    string data = "203" + fileName.Length.ToString().PadLeft(3, '0');
                    data += SocketHandler.Encipher(fileName, "cipher");
                    data += Convert.ToInt32(encryptBtn.IsChecked); // No encryption
                    data += "01" + "1";                            // One user

                    if (ipLbl.Content != null)
                    {
                        data += ipLbl.Content.ToString().Length.ToString().PadLeft(2, '0');
                        data += SocketHandler.Encipher(ipLbl.Content.ToString(), "cipher");
                    }

                    SocketHandler sh = new SocketHandler();
                    try
                    {
                        sh.sendData(data);
                    }
                    catch (SocketException ex)
                    {
                        Notifier n = AsyncBlockingSocket.initNotifier();
                        n.ShowError(ex.ToString());
                    }
                    catch (Exception ex)
                    {
                        Notifier n = AsyncBlockingSocket.initNotifier();
                        n.ShowError(ex.ToString());
                    }
                }
            }
        }
Exemplo n.º 9
0
        private void deleteBtn_Click(object sender, RoutedEventArgs e)
        {
            SocketHandler sh = new SocketHandler();

            try
            {
                string data = "216";
                sh.sendData(data);
            }
            catch (SocketException ex)
            {
                Notifier n = AsyncBlockingSocket.initNotifier();
                n.ShowError(ex.ToString());
            }
            catch (Exception ex)
            {
                Notifier n = AsyncBlockingSocket.initNotifier();
                n.ShowError(ex.ToString());
            }
        }
Exemplo n.º 10
0
        private void uploadBtn_Click(object sender, RoutedEventArgs e)
        {
            string fileName = " ";

            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            // Display OpenFileDialog by calling ShowDialog method
            Nullable <bool> result = dlg.ShowDialog();

            // Get the selected file name and display in a TextBox
            if (result == true)
            {
                // Open document
                fileName = dlg.FileName;

                string data = "209";
                if (groupNameLbl.Content == null)
                {
                    return;
                }
                data += groupNameLbl.Content.ToString().Length.ToString().PadLeft(2, '0');
                data += SocketHandler.Encipher(groupNameLbl.Content.ToString(), "cipher");
                data += fileName.Length.ToString().PadLeft(3, '0');
                data += SocketHandler.Encipher(fileName, "cipher");

                SocketHandler sh = new SocketHandler();
                try
                {
                    sh.sendData(data);
                }
                catch (SocketException ex)
                {
                    Notifier n = AsyncBlockingSocket.initNotifier();
                    n.ShowError(ex.ToString());
                }
                catch (Exception ex)
                {
                    Notifier n = AsyncBlockingSocket.initNotifier();
                    n.ShowError(ex.ToString());
                }
            }
        }
Exemplo n.º 11
0
        private void updateView(string groupName)
        {
            usersCombo.Items.Clear();
            usersList.Items.Clear();

            groupNameLbl.Content = groupName;

            string[] users = groups[groupName][0].Split('/');
            for (int i = 0; i < users.Length; i++)
            {
                if (users[i] != "")
                {
                    usersList.Items.Add(users[i]);
                }
            }

            try
            {
                SocketHandler sh = new SocketHandler();
                sh.sendData("210");

                sh.getMsgCode();
                int amount = int.Parse(sh.recvDataWithSize());
                for (int i = 0; i < amount; i++)
                {
                    string username = sh.recvDataWithSize();
                    usersCombo.Items.Add(username);
                }
            }
            catch (SocketException ex)
            {
                Notifier n = AsyncBlockingSocket.initNotifier();
                n.ShowError(ex.ToString());
            }
            catch (Exception ex)
            {
                Notifier n = AsyncBlockingSocket.initNotifier();
                n.ShowError(ex.ToString());
            }
        }
Exemplo n.º 12
0
        private void leaveBtn_Click(object sender, RoutedEventArgs e)
        {
            if (groupNameLbl.Content != null)
            {
                string groupName = groupNameLbl.Content.ToString();

                string data = "215";
                data += groupName.Length.ToString().PadLeft(2, '0');
                data += SocketHandler.Encipher(groupName, "cipher");

                SocketHandler sh = new SocketHandler();
                try
                {
                    sh.sendData(data);
                }
                catch (SocketException ex)
                {
                    Notifier n = AsyncBlockingSocket.initNotifier();
                    n.ShowError(ex.ToString());
                }
                catch (Exception ex)
                {
                    Notifier n = AsyncBlockingSocket.initNotifier();
                    n.ShowError(ex.ToString());
                }

                GetInfoAboutGroups();
                updateView(groupName);

                groupsList.Items.RemoveAt(groupsList.SelectedIndex);
                groupsList.SelectedItem = null;

                usersCombo.Items.Clear();
                usersList.Items.Clear();

                groupNameLbl.Content = null;
            }
        }
Exemplo n.º 13
0
        private void addBtn_Click(object sender, RoutedEventArgs e)
        {
            if (usersCombo.SelectedValue != null)
            {
                string username  = usersCombo.SelectedValue.ToString();
                string groupName = groupNameLbl.Content.ToString();

                string data = "207";
                data += username.Length.ToString().PadLeft(2, '0');
                data += SocketHandler.Encipher(username, "cipher");
                data += groupName.Length.ToString().PadLeft(2, '0');
                data += SocketHandler.Encipher(groupName, "cipher");

                SocketHandler sh = new SocketHandler();
                try
                {
                    sh.sendData(data);
                }
                catch (SocketException ex)
                {
                    Notifier n = AsyncBlockingSocket.initNotifier();
                    n.ShowError(ex.ToString());
                }
                catch (Exception ex)
                {
                    Notifier n = AsyncBlockingSocket.initNotifier();
                    n.ShowError(ex.ToString());
                }

                usersList.Items.Add(username);
                usersCombo.Items.Remove(username);
            }
            else
            {
                Notifier n = AsyncBlockingSocket.initNotifier();
                n.ShowError("Please pick a valid user.");
            }
        }
Exemplo n.º 14
0
        private void GetInfoAboutGroups()
        {
            try
            {
                SocketHandler sh = new SocketHandler();
                sh.sendData("208");
                sh.getMsgCode();
                int amount = int.Parse(sh.recvDataWithSize());

                for (int i = 0; i < amount; i++)
                {
                    string groupName = sh.recvDataWithSize();
                    if (!groups.ContainsKey(groupName))
                    {
                        ListBoxItem newItem = new ListBoxItem();
                        newItem.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Left;
                        newItem.FontSize = 26;
                        newItem.Content  = groupName;
                        groupsList.Items.Add(newItem);

                        List <string> values = new List <string>();
                        int           length = sh.getDataLen(2);
                        if (length != 0)
                        {
                            string data = sh.recvDataWithGivenSize(length);
                            values.Add(data);
                        }
                        else
                        {
                            values.Add("");
                        }

                        groups[newItem.Content.ToString()] = values;
                    }
                    else
                    {
                        List <string> values = new List <string>();
                        int           length = sh.getDataLen(2);
                        if (length != 0)
                        {
                            string data = sh.recvDataWithGivenSize(length);
                            values.Add(data);
                        }
                        else
                        {
                            values.Add("");
                        }

                        groups[groupName] = values;
                    }
                }
            }
            catch (SocketException ex)
            {
                Notifier n = AsyncBlockingSocket.initNotifier();
                n.ShowError(ex.ToString());
            }
            catch (Exception ex)
            {
                Notifier n = AsyncBlockingSocket.initNotifier();
                n.ShowError(ex.ToString());
            }
        }