private void receiveNote(string data) { try { var temp = data.Split(PNStrings.END_OF_ADDRESS); var receivedFrom = PNStatic.Contacts.ContactNameByComputerName(temp[0]); var addresses = Dns.GetHostAddresses(temp[0]); // because we are on intranet, sender's ip which is equal to ourself ip is most probably ip of our computer var recIp = (addresses.Any(ip => ip.Equals(PNSingleton.Instance.IpAddress))) ? PNSingleton.Instance.IpAddress : addresses.FirstOrDefault(a => a.AddressFamily == AddressFamily.InterNetwork); var sb = new StringBuilder(); _ReceivedNotes = new List<string>(); var rawData = temp[1].Split(PNStrings.END_OF_NOTE); //rawData[rawData.Length - 1] = rawData[rawData.Length - 1].Substring(0, rawData[rawData.Length - 1].IndexOf(PNStrings.END_OF_FILE)); for (var i = 0; i < rawData.Length - 1; i++) { temp = rawData[i].Split(PNStrings.END_OF_TEXT); var nc = new NoteConverter(); var note = (PNote)nc.ConvertFromString(temp[1]); if (note == null) continue; note.ID = DateTime.Now.ToString("yyMMddHHmmssfff"); //note.NoteLocation = new Point(0, 0); note.GroupID = (int)SpecialGroups.Incoming; note.PrevGroupID = note.GroupID; note.SentReceived = SendReceiveStatus.Received; note.DateReceived = DateTime.Now; note.ReceivedFrom = receivedFrom; note.ReceivedIp = recIp != null ? recIp.ToString() : ""; note.NoteLocation = new Point( (Screen.GetWorkingArea(new System.Drawing.Point((int)Left, (int)Top)).Width - note.NoteSize.Width) / 2, (Screen.GetWorkingArea(new System.Drawing.Point((int)Left, (int)Top)).Height - note.NoteSize.Height) / 2); if (PNStatic.Settings.Network.ReceivedOnTop) { note.Topmost = true; } _ReceivedNotes.Add(note.ID); sb.Append(note.Name); sb.Append(";"); //sb.AppendLine(); if (!PNStatic.Settings.Network.ShowAfterArrive) { note.Visible = false; } var path = Path.Combine(PNPaths.Instance.DataDir, note.ID) + PNStrings.NOTE_EXTENSION; using (var sw = new StreamWriter(path, false)) { sw.Write(temp[0]); } if (PNStatic.Settings.Protection.PasswordString.Length > 0 && PNStatic.Settings.Protection.StoreAsEncrypted) { using (var pne = new PNEncryptor(PNStatic.Settings.Protection.PasswordString)) { pne.EncryptTextFile(path); } } if (note.Visible) { note.Dialog = new WndNote(note, note.ID, NewNoteMode.Identificator); note.Dialog.Show(); } PNStatic.Notes.Add(note); if (NewNoteCreated != null) { NewNoteCreated(this, new NewNoteCreatedEventArgs(note)); } subscribeToNoteEvents(note); // save received note PNNotesOperations.SaveNewNote(note); PNNotesOperations.SaveNoteTags(note); if (note.Schedule != null) { PNNotesOperations.SaveNoteSchedule(note); } note.Changed = false; } if (!PNStatic.Settings.Network.NoSoundOnArrive) { PNSound.PlayMailSound(); } if (!PNStatic.Settings.Network.NoNotificationOnArrive) { var sbb = new StringBuilder(PNLang.Instance.GetCaptionText("received", "New notes received")); sbb.Append(": "); sbb.Append(sb); if (sbb.Length > 1) sbb.Length -= 1; sbb.AppendLine(); sbb.Append(PNLang.Instance.GetMessageText("sender", "Sender:")); sbb.Append(" "); sbb.Append(receivedFrom); var baloon = new Baloon(BaloonMode.NoteReceived); if (PNStatic.Settings.Network.ShowReceivedOnClick || PNStatic.Settings.Network.ShowIncomingOnClick) { baloon.BaloonLink = sbb.ToString(); } else { baloon.BaloonText = sbb.ToString(); } baloon.BaloonLinkClicked += baloon_BaloonLinkClicked; ntfPN.ShowCustomBalloon(baloon, PopupAnimation.Slide, 10000); } if (NotesReceived != null) { NotesReceived(this, new EventArgs()); } } catch (Exception ex) { PNStatic.LogException(ex); } }
internal bool SendNotesViaNetwork(List<PNote> notes, PNContact cn) { try { var sb = new StringBuilder(); foreach (var note in notes) { if (PNStatic.Settings.Protection.PromptForPassword) if (!PNNotesOperations.LogIntoNoteOrGroup(note)) continue; string text, tempPath = ""; var newNote = false; var nc = new NoteConverter(); // decrypt note file to temp file if note is encrypted var path = Path.Combine(PNPaths.Instance.DataDir, note.ID); path += PNStrings.NOTE_EXTENSION; // save note first if (note.Dialog != null && note.Changed) { if (note.FromDB) { if (PNStatic.Settings.Network.SaveBeforeSending) { note.Dialog.ApplySaveNote(false); } } else { path = Path.Combine(Path.GetTempPath(), note.ID); path += PNStrings.NOTE_EXTENSION; note.Dialog.Edit.SaveFile(path, RichTextBoxStreamType.RichText); newNote = true; } } if (PNStatic.Settings.Protection.PasswordString.Length > 0 && PNStatic.Settings.Protection.StoreAsEncrypted && !newNote) { var fileName = Path.GetFileName(path); if (string.IsNullOrEmpty(fileName)) continue; tempPath = Path.Combine(Path.GetTempPath(), fileName); File.Copy(path, tempPath, true); using (var pne = new PNEncryptor(PNStatic.Settings.Protection.PasswordString)) { pne.DecryptTextFile(tempPath); } path = tempPath; } // read note file content using (var sr = new StreamReader(path)) { text = sr.ReadToEnd(); } // remove temp file if (tempPath != "") { File.Delete(tempPath); } //remove temporary file created for new note if (newNote) { File.Delete(path); } sb.Append(text); sb.Append(PNStrings.END_OF_TEXT); sb.Append(nc.ConvertToString(note)); sb.Append(PNStrings.END_OF_NOTE); } if (sb.Length <= 0) return false; string ipAddress; if (!cn.UseComputerName || !string.IsNullOrEmpty(cn.IpAddress)) { ipAddress = cn.IpAddress; } else { IPHostEntry ipHostInfo; try { ipHostInfo = Dns.GetHostEntry(cn.ComputerName); } catch (SocketException) { var msg = PNLang.Instance.GetMessageText("host_unknown", "Computer %PLACEHOLDER1% cannot be found on network"); msg = msg.Replace(PNStrings.PLACEHOLDER1, cn.ComputerName); PNMessageBox.Show(msg, PNStrings.PROG_NAME, MessageBoxButton.OK, MessageBoxImage.Error); return false; } var address = ipHostInfo.AddressList.FirstOrDefault(ip => ip.AddressFamily == AddressFamily.InterNetwork); if (address == null) return false; ipAddress = address.ToString(); } //check whether contact's computer is on network if (PNConnections.CheckContactConnection(ipAddress) == ContactConnection.Disconnected) { var msg = PNLang.Instance.GetMessageText("contact_disconnected", "Computer of contact %PLACEHOLDER1% is not connected to network"); PNMessageBox.Show(msg.Replace("%PLACEHOLDER1%", cn.Name), PNStrings.PROG_NAME, MessageBoxButton.OK); return false; } var clientRunner = new PNWCFClientRunner(); clientRunner.PNDataError += WCFClient_PNDataError; clientRunner.NotesSent += WCFClient_NotesSent; if (ipAddress == PNSingleton.Instance.IpAddress.ToString()) //we are on intranet, so most probably this is the same computer { Task.Factory.StartNew(() => clientRunner.SendNotes(cn.Name, PNSingleton.Instance.IpAddress.ToString(), sb.ToString(), PNStatic.Settings.Network.ExchangePort.ToString(CultureInfo.InvariantCulture), notes)); //var t = new Thread(() => clientRunner.SendNotes(cn.Name, ipAddress.ToString(), sb.ToString(), PNStatic.Settings.Network.ExchangePort.ToString(CultureInfo.InvariantCulture), notes)); //t.Start(); return true; } else { return clientRunner.SendNotes(cn.Name, ipAddress, sb.ToString(), PNStatic.Settings.Network.ExchangePort.ToString(CultureInfo.InvariantCulture), notes); } } catch (Exception ex) { PNStatic.LogException(ex); return false; } }