private void lbMainMessages_ScrollBar_MouseUp(object sender, RoutedEventArgs e) { if (channel != "" && stopUpdating == false && lbMainMessages.Items.Count >= 50) { Border border = (Border)VisualTreeHelper.GetChild(lbMainMessages, 0); ScrollViewer scrollViewer = (ScrollViewer)VisualTreeHelper.GetChild(border, 0); scrollViewer.ReleaseMouseCapture(); if (scrollViewer.VerticalOffset == 0) { screenMain.timesUpdated += 1; Dictionary <string, object> message = new Dictionary <string, object>(); message.Add("username", ""); message.Add("channel", channel); message.Add("content", timesUpdated); message.Add("messagetype", "messageReq"); List <object> encryptedMessage = EMAES.Encrypt(JsonConvert.SerializeObject(message)); string jsonMessage = JsonConvert.SerializeObject(encryptedMessage); NM.SendMessage(jsonMessage); } } }
private void lbMainChannels_SelectionChanged(object sender, SelectionChangedEventArgs e) { object lbi = this.lbMainChannels.SelectedItem; if (lbi == null) { } else { stopUpdating = false; string channelName = lbi.ToString(); channel = channelName; Dictionary <string, object> message = new Dictionary <string, object>(); message.Add("username", username); message.Add("channel", ""); message.Add("content", channelName); message.Add("messagetype", "changedChannel"); List <object> encryptedMessage = EMAES.Encrypt(JsonConvert.SerializeObject(message)); string jsonMessage = JsonConvert.SerializeObject(encryptedMessage); NM.SendMessage(jsonMessage); } }
private void ChannelMembers_Context_Ban(object sender, RoutedEventArgs e) { if (lbMainChannelMembers.SelectedItem == null) { } else { string target = lbMainChannelMembers.SelectedItem.ToString(); if (target.Substring((target.Length - 2), 2) == " \u2606") { Dictionary <string, object> message = new Dictionary <string, object>(); message.Add("username", username); message.Add("channel", channel); message.Add("content", "/ban " + target.Substring(0, target.Length - 2)); message.Add("messagetype", "inboundMessage"); List <object> encryptedMessage = EMAES.Encrypt(JsonConvert.SerializeObject(message)); string jsonMessage = JsonConvert.SerializeObject(encryptedMessage); NM.SendMessage(jsonMessage); } else { Dictionary <string, object> message = new Dictionary <string, object>(); message.Add("username", username); message.Add("channel", channel); message.Add("content", "/ban " + target); message.Add("messagetype", "inboundMessage"); List <object> encryptedMessage = EMAES.Encrypt(JsonConvert.SerializeObject(message)); string jsonMessage = JsonConvert.SerializeObject(encryptedMessage); NM.SendMessage(jsonMessage); } } }
private void btnMainSendMsg_Click(object sender, RoutedEventArgs e) { if (channel != "" && this.tbMainMessageEntry.Text != "") { Dictionary <string, object> message = new Dictionary <string, object>(); message.Add("username", username); message.Add("channel", channel); message.Add("content", this.tbMainMessageEntry.Text); message.Add("messagetype", "inboundMessage"); List <object> encryptedMessage = EMAES.Encrypt(JsonConvert.SerializeObject(message)); string jsonMessage = JsonConvert.SerializeObject(encryptedMessage); NM.SendMessage(jsonMessage); } this.tbMainMessageEntry.Text = ""; }
private void viewAllUsers_Kick(object sender, RoutedEventArgs e) { if (lbMainChannelMembers.SelectedItem == null) { } else { string target = lbMainChannelMembers.SelectedItem.ToString(); Dictionary <string, object> message = new Dictionary <string, object>(); message.Add("username", username); message.Add("channel", channel); message.Add("content", "/kick " + target); message.Add("messagetype", "inboundMessage"); List <object> encryptedMessage = EMAES.Encrypt(JsonConvert.SerializeObject(message)); string jsonMessage = JsonConvert.SerializeObject(encryptedMessage); } }
private void viewAllUsers_Click(object sender, RoutedEventArgs e) { if (true) { Dictionary <string, object> message = new Dictionary <string, object> { { "username", "" }, { "channel", "" }, { "content", "" }, { "messagetype", "userReq" } }; List <object> encryptedMessage = EMAES.Encrypt(JsonConvert.SerializeObject(message)); string jsonMessage = JsonConvert.SerializeObject(encryptedMessage); NM.SendMessage(jsonMessage); } else { //MessageBox.Show("something else"); } }
private void lbMainMessages_SelectionChanged(object sender, SelectionChangedEventArgs e) { Message messageContent = (Message)this.lbMainMessages.SelectedItem; if (messageContent == null) { } else if (messageContent.Content == "[Load more messages]") { screenMain.timesUpdated += 1; Dictionary <string, object> message = new Dictionary <string, object>(); message.Add("username", ""); message.Add("channel", channel); message.Add("content", timesUpdated); message.Add("messagetype", "messageReq"); List <object> encryptedMessage = EMAES.Encrypt(JsonConvert.SerializeObject(message)); string jsonMessage = JsonConvert.SerializeObject(encryptedMessage); NM.SendMessage(jsonMessage); } }
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; }); } } }
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); } } }