diff --git a/PolyChat/Controller.cs b/PolyChat/Controller.cs index 4034ffc..99ac8ac 100644 --- a/PolyChat/Controller.cs +++ b/PolyChat/Controller.cs @@ -36,8 +36,6 @@ namespace PolyChat { UIController = uiController; OwnIP = getIP(); - //loadChats(); - //SaveChats("10", "{das ist ein test}"); Serve(); // test @@ -59,7 +57,7 @@ namespace PolyChat { Connections.Add(ip, new Connection(ip, PORT, Data => OnMessage(ip, Data), CloseChat)); } - + } private void Serve() @@ -78,6 +76,7 @@ namespace PolyChat { Debug.WriteLine("--- initial packet received ---"); string ForeignIp = data[0].ToString(); + Debug.WriteLine($"--- this ip was in the inital packet: {ForeignIp} ---"); if (isInConnections(ForeignIp)) { @@ -178,6 +177,7 @@ namespace PolyChat /// public void loadChats() { + //TODO: also load chatlogs when user tries to connect //load dir and create if non existant if (Directory.Exists("U:\\PolyChat\\Saves")) { @@ -206,9 +206,43 @@ namespace PolyChat UIController.OnIncomingMessages(ip, jsonArr); } } - } + /* + public void loadChat(String ip) + { + //TODO: also load chatlogs when user tries to connect + //load dir and create if non existant + if (Directory.Exists("U:\\PolyChat\\Saves")) + { + Debug.WriteLine("--Path exists.--"); + } + else + { + Directory.CreateDirectory("U:\\PolyChat\\Saves"); + Debug.WriteLine("--Path Created--."); + } + + //go through all files and send ip and json array to ui + String[] filepaths = Directory.GetFiles("U:\\PolyChat\\Saves"); + if (filepaths.Length > 0) + { + Debug.WriteLine("---Loading Saves"); + foreach (String path in filepaths) + { + Debug.WriteLine($"--{path}"); + String jsonArr = File.ReadAllText(path); + String ip = Path.GetFileName(path); + ip = ip.Substring(0, ip.Length - 4); + Debug.WriteLine($"-{ip}"); + Debug.WriteLine(jsonArr); + Connect(ip); + UIController.OnIncomingConnection(ip); + UIController.OnIncomingMessages(ip, jsonArr); + } + } + } + */ /// /// Saves incoming chat message to /// @@ -221,43 +255,51 @@ namespace PolyChat //writing flag setzen oder auch in der datei selbst ne flag setzen new Thread(() => { - if (File.Exists($"U:\\PolyChat\\Saves\\{ip}.txt")) + //breaking if namechange + JObject obj = JObject.Parse(json); + if (!obj["type"].ToString().Equals("username")) { - Debug.WriteLine("--File allready exists--"); - //check for integraty of file - string output = File.ReadAllText($"U:\\PolyChat\\Saves\\{ip}.txt"); - Debug.WriteLine($"---{output}---"); - if (output.Substring(0, 1).Equals("[") && output.Substring(output.Length - 1, 1).Equals("]")) + //adding timestamp + obj = JObject.Parse(json); + obj.Add(new JProperty("timestamp", timeStamp)); + json = obj.ToString(); + if (File.Exists($"U:\\PolyChat\\Saves\\{ip}.txt")) { - Debug.WriteLine("--adding new chatmessage--"); - //structure intact - JObject obj = JObject.Parse(json); - //save new chat - String saved = output.Substring(0, output.Length - 1); - output = saved + ", " + json + " ]"; - File.Delete($"U:\\PolyChat\\Saves\\{ip}.txt"); - File.WriteAllText($"U:\\PolyChat\\Saves\\{ip}.txt", output); + Debug.WriteLine("--File allready exists--"); + //check for integraty of file + string output = File.ReadAllText($"U:\\PolyChat\\Saves\\{ip}.txt"); + Debug.WriteLine($"---{output}---"); + if (output.Substring(0, 1).Equals("[") && output.Substring(output.Length - 1, 1).Equals("]")) + { + Debug.WriteLine("--adding new chatmessage--"); + //structure intact + //save new chat + String saved = output.Substring(0, output.Length - 1); + output = saved + ", " + json + " ]"; + File.Delete($"U:\\PolyChat\\Saves\\{ip}.txt"); + File.WriteAllText($"U:\\PolyChat\\Saves\\{ip}.txt", output); + } + else + { + Debug.WriteLine("--Structure not intact--"); + Debug.WriteLine("--redoing file--"); + //structure not intact + //redo file + File.Delete($"U:\\PolyChat\\Saves\\{ip}.txt"); + File.WriteAllText($"U:\\PolyChat\\Saves\\{ip}.txt", $"[ {json} ]"); + } } else { - Debug.WriteLine("--Structure not intact--"); - Debug.WriteLine("--redoing file--"); - //structure not intact - //redo file - File.Delete($"U:\\PolyChat\\Saves\\{ip}.txt"); + Debug.WriteLine("--Creating new File--"); + //setup file File.WriteAllText($"U:\\PolyChat\\Saves\\{ip}.txt", $"[ {json} ]"); } } - else - { - Debug.WriteLine("--Creating new File--"); - //setup file - File.WriteAllText($"U:\\PolyChat\\Saves\\{ip}.txt", $"[ {json} ]"); - } }).Start(); } - private void encode(string json) + private void encode(string json) { byte[] plaintext = Encoding.UTF8.GetBytes(json); byte[] entropy = new byte[20]; @@ -266,8 +308,6 @@ namespace PolyChat rng.GetBytes(entropy); } - /*byte[] ciphertext = ProtectedData.Protect(plaintext, entropy, - DataProtectionScope.CurrentUser);*/ } } } diff --git a/PolyChat/MainPage.xaml.cs b/PolyChat/MainPage.xaml.cs index df1d05b..f918ff3 100644 --- a/PolyChat/MainPage.xaml.cs +++ b/PolyChat/MainPage.xaml.cs @@ -171,7 +171,11 @@ namespace PolyChat origin, item["type"].ToString(), item["content"].ToString()//, +<<<<<<< HEAD //DateTime.Parse(item["timestamp"].ToString()) +======= + //DateTime.Parse(item["timestamp"].ToString()) +>>>>>>> be4eada (stuff) ) ); } diff --git a/PolyChat/Models/ChatMessage.cs b/PolyChat/Models/ChatMessage.cs index 4056559..7428068 100644 --- a/PolyChat/Models/ChatMessage.cs +++ b/PolyChat/Models/ChatMessage.cs @@ -11,12 +11,7 @@ namespace PolyChat.Models public string Content; public DateTime TimeStamp; public readonly bool Foreign; - - public ChatMessage() - { - - } /// /// Create own Message (directly sent) ///