Пример #1
0
        public screenMain()
        {
            InitializeComponent();

            pageMain = this;
            List <string> config = CFM.ReadSettings();

            Application.Current.MainWindow.Height = Convert.ToDouble(config[2]);
            Application.Current.MainWindow.Width  = Convert.ToDouble(config[3]);
            //MainWindow.main.frame.Height = 570;
            //MainWindow.main.frame.Width = 992;

            MainWindow.main.ResizeMode = ResizeMode.CanResize;

            List <string> channels = JsonConvert.DeserializeObject <List <string> >((NM.serverInfo["channels"]).ToString());

            foreach (string ch in channels)
            {
                this.lbMainChannels.Items.Add(ch);
            }

            if (config[4] == "Light Theme")
            {
            }
            else if (config[4] == "Dark Theme")
            {
                VM.DarkTheme("screenMain");
            }
        }
Пример #2
0
        private void cboSettingsClrSch_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            List <string> config = CFM.ReadSettings();

            if (cboSettingsClrSch.SelectedValue == null)
            {
            }
            else
            {
                if (cboSettingsClrSch.SelectedValue.ToString() == "Light Theme")
                {
                    if (config[4] == "Light Theme")
                    {
                    }
                    else
                    {
                        VM.LightTheme("screenSettings");
                        CFM.UpdateSetting("colourScheme", "Light Theme");
                    }
                }
                else if (cboSettingsClrSch.SelectedValue.ToString() == "Dark Theme")
                {
                    if (config[4] == "Dark Theme")
                    {
                    }
                    else
                    {
                        VM.DarkTheme("screenSettings");
                        CFM.UpdateSetting("colourScheme", "Dark Theme");
                    }
                }
            }
        }
Пример #3
0
 private void bStartupSettings_Click(object sender, RoutedEventArgs e)
 {
     if (config[0] == "true")
     {
         CFM.UpdateSetting("username", Regex.Replace(tbStartupUsername.Text, @" ", ""));
     }
     MainWindow.main.frame.Source = new Uri("screenSettings.xaml", UriKind.Relative);
 }
Пример #4
0
 private void MenuItem_Disconn_Click(object sender, RoutedEventArgs e)
 {
     NM.DC();
     CFM.UpdateSetting("mainHeight", Convert.ToString(MainWindow.main.ActualHeight));
     CFM.UpdateSetting("mainWidth", Convert.ToString(MainWindow.main.ActualWidth));
     MainWindow.main.frame.Source = new Uri("screenStartup.xaml", UriKind.Relative);
     NM.serverInfo.Clear();
     channel = "";
     Application.Current.MainWindow.Height = 350;
     Application.Current.MainWindow.Width  = 525;
 }
Пример #5
0
 private void cbRememberUser_Click(object sender, RoutedEventArgs e)
 {
     if (this.cbRememberUser.IsChecked == false)
     {
         CFM.UpdateSetting("saveUsername", "false");
     }
     else if (this.cbRememberUser.IsChecked == true)
     {
         CFM.UpdateSetting("saveUsername", "true");
     }
 }
Пример #6
0
        public screenSettings()
        {
            InitializeComponent();
            pageSettings = this;
            DBM.SQLInitialise();
            populateServers();

            List <string> config = CFM.ReadSettings();

            cboSettingsClrSch.SelectedValue = config[4];

            if (config[4] == "Light Theme")
            {
            }
            else if (config[4] == "Dark Theme")
            {
                VM.DarkTheme("screenSettings");
            }
        }
Пример #7
0
        public screenStartup()
        {
            InitializeComponent();

            pageStartup = this;
            DBM.SQLInitialise();
            populateServers();

            MainWindow.main.ResizeMode = ResizeMode.NoResize;

            config = CFM.ReadSettings();

            if (config[4] == "Light Theme")
            {
            }
            else if (config[4] == "Dark Theme")
            {
                VM.DarkTheme("screenStartup");
            }

            Panel mainContainer = (Panel)this.Content;

            /// GetAll UIElement
            UIElementCollection element = mainContainer.Children;

            /// casting the UIElementCollection into List
            List <FrameworkElement> lstElement = element.Cast <FrameworkElement>().ToList();

            /// Geting all Control from list
            var lstControl = lstElement.OfType <Control>();

            foreach (Control control in lstControl)
            {
                Debug.WriteLine(control.GetType().ToString());
            }

            if (Convert.ToBoolean(config[0]) == true)
            {
                this.cbRememberUser.IsChecked = true;
                this.tbStartupUsername.Text   = config[1];
            }
        }
Пример #8
0
        private static void RecvLoop()
        {
            ASCIIEncoding ascii = new ASCIIEncoding();

            byte[] bytes = new byte[20480];
            try
            {
                Queue netQueue = new Queue();

                while (recieving == true)
                {
                    int    bytesRec = sender.Receive(bytes);
                    string jsonData = Encoding.UTF8.GetString(bytes, 0, bytesRec);

                    netQueue.Clear();

                    if (jsonData[0].Equals('[') && jsonData[(jsonData.Length - 1)].Equals(']'))
                    {
                        netQueue.Enqueue(jsonData);
                    }
                    else
                    {
                        string incompleteMessage = jsonData;
                        bool   messageComplete   = false;
                        while (messageComplete == false)
                        {
                            bytesRec = sender.Receive(bytes);
                            jsonData = Encoding.UTF8.GetString(bytes, 0, bytesRec);
                            if (jsonData[(jsonData.Length - 1)].Equals(']'))
                            {
                                messageComplete    = true;
                                incompleteMessage += jsonData;

                                netQueue.Enqueue(incompleteMessage);

                                /*
                                 * bytesRec = sender.Receive(bytes);
                                 * jsonData = Encoding.UTF8.GetString(bytes, 0, bytesRec);
                                 *
                                 * if (jsonData[0].Equals('[') == false || jsonData == null)
                                 * {
                                 *  Debug.WriteLine("check 4");
                                 * }
                                 * else
                                 * {
                                 *  Debug.WriteLine("check 5");
                                 *  incompleteMessage = "";
                                 *  messageComplete = false;
                                 *  incompleteMessage = jsonData;
                                 * }
                                 */
                            }
                            else
                            {
                                incompleteMessage += jsonData;
                            }
                        }
                    }

                    foreach (string netMessage in netQueue)
                    {
                        List <string> encryptedData = JsonConvert.DeserializeObject <List <string> >(netMessage);

                        jsonData = EMAES.Decrypt(encryptedData[0], KeyGenerator.SecretKey, encryptedData[1]);

                        Dictionary <string, object> message = JsonConvert.DeserializeObject <Dictionary <string, object> >(jsonData);

                        switch (message["messagetype"])
                        {
                        case "outboundMessage":
                            screenMain.pageMain.lbMainMessages.Dispatcher.Invoke(() =>
                            {
                                List <string> messageContent = ((Newtonsoft.Json.Linq.JArray)message["content"]).ToObject <List <string> >();
                                screenMain.pageMain.lbMainMessages.Items.Add(new Message(messageContent[0].ToString(), messageContent[1].ToString(), messageContent[2].ToString(), messageContent[3].ToString()));
                                if (VisualTreeHelper.GetChildrenCount(screenMain.pageMain.lbMainMessages) > 0)
                                {
                                    Border border             = (Border)VisualTreeHelper.GetChild(screenMain.pageMain.lbMainMessages, 0);
                                    ScrollViewer scrollViewer = (ScrollViewer)VisualTreeHelper.GetChild(border, 0);
                                    scrollViewer.ScrollToBottom();
                                }
                            });
                            break;

                        case "connReqAccepted":
                            serverInfo.Add("channels", message["content"]);
                            MainWindow.main.frame.Dispatcher.Invoke(() => { MainWindow.main.frame.Source = new Uri("screenMain.xaml", UriKind.Relative); });
                            break;

                        case "connReqDenied":
                            NM.DC();
                            if (message["content"] as string == "banned")
                            {
                                MessageBox.Show("You are banned from this server");
                            }
                            else if (message["content"] as string == "password")
                            {
                                MessageBox.Show("Error - Incorrect password");
                            }
                            break;

                        case "channelMembers":
                            screenMain.pageMain.lbMainChannelMembers.Dispatcher.Invoke(() =>
                            {
                                screenMain.pageMain.lbMainChannelMembers.Items.Clear();
                                List <string> channelMembers = ((Newtonsoft.Json.Linq.JArray)message["content"]).ToObject <List <string> >();

                                foreach (string username in channelMembers)
                                {
                                    screenMain.pageMain.lbMainChannelMembers.Items.Add(username);
                                }
                            });
                            break;

                        case "channelHistory":
                            historyArchive.Clear();
                            screenMain.pageMain.lbMainMessages.Dispatcher.Invoke(() =>
                            {
                                screenMain.pageMain.lbMainMessages.Items.Clear();

                                List <List <string> > messageHistory = ((Newtonsoft.Json.Linq.JArray)message["content"]).ToObject <List <List <string> > >();

                                foreach (List <string> row in messageHistory)
                                {
                                    historyArchive.Add(new Message(row[2], row[0], row[3], row[4]));
                                }

                                //historyArchive = messageHistory;

                                //if (messageHistory.Count == 50)
                                //screenMain.pageMain.lbMainMessages.Items.Add(new Message("","system","[Load more messages]", ""));

                                foreach (Message msg in historyArchive)
                                {
                                    screenMain.pageMain.lbMainMessages.Items.Add(msg);
                                }
                                if (VisualTreeHelper.GetChildrenCount(screenMain.pageMain.lbMainMessages) > 0)
                                {
                                    Border border             = (Border)VisualTreeHelper.GetChild(screenMain.pageMain.lbMainMessages, 0);
                                    ScrollViewer scrollViewer = (ScrollViewer)VisualTreeHelper.GetChild(border, 0);
                                    scrollViewer.ScrollToBottom();
                                }
                                screenMain.timesUpdated = 1;
                            });
                            break;

                        case "userUpdate":
                            if ((string)message["content"] == "username")
                            {
                                screenMain.username = (string)message["username"];
                            }
                            break;

                        case "additionalHistory":
                        {
                            if (screenMain.channel == "")
                            {
                                moreMessages = false;
                            }
                            foreach (List <string> row in ((Newtonsoft.Json.Linq.JArray)message["content"]).ToObject <List <List <string> > >())
                            {
                                historyArchive.Insert(0, new Message(row[2], row[0], row[3], row[4]));
                            }

                            if (((Newtonsoft.Json.Linq.JArray)message["content"]).ToObject <List <List <string> > >().Count == 50)
                            {
                                moreMessages = true;
                            }
                            else
                            {
                                screenMain.stopUpdating = true;
                            }

                            screenMain.pageMain.lbMainMessages.Dispatcher.Invoke(() =>
                                {
                                    object posRetain = screenMain.pageMain.lbMainMessages.Items.GetItemAt(1);

                                    screenMain.pageMain.lbMainMessages.Items.Clear();
                                    //if (moreMessages == true)
                                    //{
                                    //screenMain.pageMain.lbMainMessages.Items.Add(new Message("", "system", "[Load more messages]", ""));
                                    //}

                                    foreach (Message msg in historyArchive)
                                    {
                                        screenMain.pageMain.lbMainMessages.Items.Add(msg);
                                    }
                                    //ScrollToVerticalOffset(row * _grid.RowHeight.Value);
                                    Border border             = (Border)VisualTreeHelper.GetChild(screenMain.pageMain.lbMainMessages, 0);
                                    ScrollViewer scrollViewer = (ScrollViewer)VisualTreeHelper.GetChild(border, 0);
                                    screenMain.pageMain.lbMainMessages.ScrollIntoView(posRetain);
                                    int visibleItems = Convert.ToInt32(scrollViewer.ViewportHeight);
                                    screenMain.pageMain.lbMainMessages.SelectedItem = posRetain;
                                    int currentIndex    = screenMain.pageMain.lbMainMessages.SelectedIndex;
                                    object scrollDownTo = screenMain.pageMain.lbMainMessages.Items.GetItemAt(currentIndex + visibleItems - 2);
                                    screenMain.pageMain.lbMainMessages.ScrollIntoView(scrollDownTo);
                                });
                        }
                        break;

                        case "userKicked":
                        {
                            MainWindow.main.Dispatcher.Invoke(() =>
                                {
                                    CFM.UpdateSetting("mainHeight", Convert.ToString(MainWindow.main.ActualHeight));
                                    CFM.UpdateSetting("mainWidth", Convert.ToString(MainWindow.main.ActualWidth));
                                    MainWindow.main.frame.Source          = new Uri("screenStartup.xaml", UriKind.Relative);
                                    Application.Current.MainWindow.Height = 350;
                                    Application.Current.MainWindow.Width  = 525;
                                    if ((string)message["content"] == "")
                                    {
                                        MessageBox.Show("You were kicked from the server. No reason given");
                                    }
                                    else
                                    {
                                        MessageBox.Show("You were kicked from the server. Reason: " + message["content"]);
                                    }
                                    NM.DC();
                                    NM.serverInfo.Clear();
                                });
                        }
                        break;

                        case "userBanned":
                        {
                            MainWindow.main.Dispatcher.Invoke(() =>
                                {
                                    CFM.UpdateSetting("mainHeight", Convert.ToString(MainWindow.main.ActualHeight));
                                    CFM.UpdateSetting("mainWidth", Convert.ToString(MainWindow.main.ActualWidth));
                                    MainWindow.main.frame.Source          = new Uri("screenStartup.xaml", UriKind.Relative);
                                    Application.Current.MainWindow.Height = 350;
                                    Application.Current.MainWindow.Width  = 525;
                                    if ((string)message["content"] == "")
                                    {
                                        MessageBox.Show("You were banned from the server. No reason given");
                                    }
                                    else
                                    {
                                        MessageBox.Show("You were banned from the server. Reason: " + message["content"]);
                                    }
                                    NM.DC();
                                    NM.serverInfo.Clear();
                                });
                        }
                        break;

                        case "userList":
                        {
                            screenMain.pageMain.Dispatcher.Invoke(() =>
                                {
                                    MenuItem subItem      = new MenuItem();
                                    MenuItem viewAllUsers = (MenuItem)screenMain.pageMain.menuMain.Items.GetItemAt(1);
                                    viewAllUsers.Items.Clear();
                                    foreach (string item in ((Newtonsoft.Json.Linq.JArray)message["content"]).ToObject <List <string> >())
                                    {
                                        subItem             = new MenuItem();
                                        subItem.Header      = item;
                                        subItem.Background  = new SolidColorBrush(Color.FromArgb(0xFF, (byte)23, (byte)28, (byte)24));
                                        subItem.BorderBrush = new SolidColorBrush(Color.FromArgb(0xFF, (byte)56, (byte)56, (byte)56));
                                        subItem.Foreground  = new SolidColorBrush(Color.FromArgb(0xFF, (byte)240, (byte)247, (byte)244));
                                        //subItem.Click =
                                        //Style style = Application.Current.FindResource("DarkTheme") as Style;
                                        //subItem.Style = style;
                                        viewAllUsers.Items.Add(subItem);
                                    }
                                });
                        }
                        break;
                        }
                    }
                }
            }
            catch (System.Net.Sockets.SocketException)
            {
                if (recieving == true)
                {
                    MessageBox.Show("Error - Connection Lost");
                    MainWindow.main.Dispatcher.Invoke(() =>
                    {
                        MainWindow.main.frame.Source = new Uri("screenStartup.xaml", UriKind.Relative);
                    });

                    NM.serverInfo.Clear();
                    screenMain.channel = "";
                    NM.DC();
                    MainWindow.main.Dispatcher.Invoke(() =>
                    {
                        Application.Current.MainWindow.Height = 350;
                        Application.Current.MainWindow.Width  = 525;
                    });
                }
            }
        }
Пример #9
0
        private void bStartupConn_Click(object sender, RoutedEventArgs e)
        {
            if (config[0] == "true")
            {
                CFM.UpdateSetting("username", Regex.Replace(tbStartupUsername.Text, @" ", ""));
            }
            if (this.lbStartupServers.SelectedItem == null)
            {
                MessageBox.Show("Error - Please select a server");
            }
            else if (this.tbStartupUsername.Text == "")
            {
                MessageBox.Show("Please enter a username");
            }
            else
            {
                ListBoxItem lbi      = this.lbStartupServers.SelectedItem as ListBoxItem;
                string      servName = lbi.Content.ToString();

                List <List <string> > data = DBM.SQLRaw("SELECT * FROM servers WHERE name='" + servName + "'", "servers");
                string servIP    = data[0][2];
                int    servPort  = Convert.ToInt16(data[0][3]);
                bool   connected = NM.Connect(servIP, servPort);

                if (connected == true)
                {
                    KeyGenerator.SecretKey = KeyGenerator.GetUniqueKey(16);

                    Debug.WriteLine(KeyGenerator.SecretKey);


                    Dictionary <string, object> message = new Dictionary <string, object>
                    {
                        { "username", "" },
                        { "channel", "" },
                        { "content", "" },
                        { "messagetype", "keyRequest" }
                    };

                    string jsonMessage = JsonConvert.SerializeObject(message);
                    NM.SendMessage(jsonMessage);

                    byte[] bytes = new byte[20480];

                    int    bytesRec = NM.sender.Receive(bytes); // Receieve "publicKey"
                    string jsonData = Encoding.UTF8.GetString(bytes, 0, bytesRec);

                    message = JsonConvert.DeserializeObject <Dictionary <string, object> >(jsonData);

                    string publicKey  = (string)message["content"];
                    string ciphertext = EMRSA.Encrypt(KeyGenerator.SecretKey, publicKey);

                    message = new Dictionary <string, object>
                    {
                        { "username", "" },
                        { "channel", "" },
                        { "content", ciphertext },
                        { "messagetype", "secretKey" }
                    };

                    jsonMessage = JsonConvert.SerializeObject(message);
                    NM.SendMessage(jsonMessage);

                    bytes = new byte[20480];

                    bytesRec = NM.sender.Receive(bytes); // Recieve "confirmed"
                    jsonData = Encoding.UTF8.GetString(bytes, 0, bytesRec);

                    message = JsonConvert.DeserializeObject <Dictionary <string, object> >(jsonData);



                    NM.RecieveMessage();

                    string username = tbStartupUsername.Text;
                    username = Regex.Replace(username, @" ", "");

                    List <string> connRequest = new List <string> {
                        username, this.tbStartupPassword.Password
                    };

                    message = new Dictionary <string, object>
                    {
                        { "username", "" },
                        { "channel", "" },
                        { "content", connRequest },
                        { "messagetype", "connRequest" }
                    };

                    List <object> encryptedMessage = EMAES.Encrypt(JsonConvert.SerializeObject(message));

                    jsonMessage = JsonConvert.SerializeObject(encryptedMessage);

                    NM.SendMessage(jsonMessage);
                }
            }
        }