/* * Create a message and put it onto the FIFO OutMsgQueue */ public override int CreateMessage(string contact, string body, string subject) { CommunicationMessage msg = new CommunicationMessage(); msg.Sender.Name = UserName; msg.Sender.NickName = UserNick; msg.Sender.IP4Address = LocalIP; msg.Sender.MachineID = MachineName; body = body.Trim(); if (isEncrypted) { // we need to encrypt the message body = "[CRYPTOR]" + cryptor.EncryptString(body); } msg.Message = body.Trim(); if (contact.Length > 0) { CommunicationRecipient s = new CommunicationRecipient(); s.Name = UserName; msg.Recipient.Add(s); } OutMsgQueue.Add(msg); if (DebugLevel > 8) { WriteDebug(string.Format("Saving Name '{0}' and body '{1}'", msg.Sender.Name, msg.Message)); } return(0); }
/* * Return a formatted list of current users * Info comes from the handshake files */ public override string GetContactList() { string[] files = Directory.GetFiles(Server, "*" + HandShakeExtension); string filetext; string contactList = string.Empty; Contacts = new List <CommunicationRecipient>(); if (Connected) { for (int i = 0; i < files.Length; i++) { filetext = File.ReadAllText(files[i]); string[] filelines = (filetext.Replace((char)10, ' ') + "\r\r\r\r").Split((char)13); // Add several return characters and split on them // Put this users info on top, otherwise add the user info to the bottom of the list // List format is UserName|MachineID|InstanceID (Address)|FileName[|"offline"] if (Server + HSFile + HandShakeExtension == files[i]) { // This is the current instance contactList = (filelines[1].Length == 0 ? string.Format("User[{0}]", i) : filelines[1].Trim()) + "|" + (filelines[2].Length == 0 ? string.Format("MachID[{0}]", i) : filelines[2].Trim()) + "|" + (filelines[3].Length == 0 ? string.Format("Instance[{0}]", i) : filelines[3].Trim()) + "|" + files[i].Replace(HandShakeExtension, "").Replace(Server, "") + "\r" + contactList; } else { // TODO - if a handshake file is over a certain age, it should be marked as off-line // This is someone else contactList += (filelines[1].Length == 0 ? string.Format("User[{0}]", i) : filelines[1].Trim()) + "|" + (filelines[2].Length == 0 ? string.Format("MachID[{0}]", i) : filelines[2].Trim()) + "|" + (filelines[3].Length == 0 ? string.Format("Instance[{0}]", i) : filelines[3].Trim()) + "|" + files[i].Replace(HandShakeExtension, "").Replace(Server, "") + "\r"; // Add everyone else to a List<> for internal use CommunicationRecipient con = new CommunicationRecipient(); con.Name = (filelines[1].Length == 0 ? string.Format("User[{0}]", i) : filelines[1].Trim()); con.Address = (filelines[3].Length == 0 ? string.Format("Instance[{0}]", i) : filelines[3].Trim()); // Unique Key con.MachineID = (filelines[2].Length == 0 ? string.Format("MachID[{0}]", i) : filelines[2].Trim()); con.IP4Address = files[i].Replace(HandShakeExtension, "").Replace(Server, ""); Contacts.Add(con); } } } return(contactList); }
/* * Create a message for outbound and add it to the out queue * * THOUGHTS * If address len>0 then it's private message * If subject len>0 then it's a message to another channel? Is that allowed? * */ public override int CreateMessage(string address, string body, string subject) { int Result = 0; try { CommunicationMessage msg = new CommunicationMessage(); CommunicationRecipient r = new CommunicationRecipient(); msg.Message = body; r.Name = address; msg.Recipient.Add(r); OutMsgQueue.Add(msg); } catch (Exception ex) { AddError(Result = 16, ex.Message, GetType().Name + ".CREATEMESSAGE", "Failed to create message -> " + body); } return(Result); }
/* * breaks up the text from the current message and puts it into the currentmsg structure */ private bool BreakMessage(string fName) { // read in the message file //string[] fileParts = msgFiles[0].Replace(SendSemaphore, "").Split('-'); string text = File.ReadAllText(fName).Replace("\n", ""); string[] lines = text.Split('\r'); string body = string.Empty; string subject = string.Empty; string address = string.Empty; string from = string.Empty; string wsid = string.Empty; string line = string.Empty; int e; DateTime d; CommunicationRecipient rec = new CommunicationRecipient(); // break up the message file CurrentMsg = new CommunicationMessage { ErrorCode = 1, ID = fName }; for (int i = 0; i < lines.Length; i++) { WriteDebug(string.Format("Line {0} -> {1}", i, lines[i])); if (lines[i].Substring(0, 4).Equals("SYNC")) { CurrentMsg.Async = false; if (lines[i].Contains("|")) { string[] s = lines[i].Split('|'); if (int.TryParse(s[1], out e)) { CurrentMsg.ErrorCode = e; } } } else if (lines[i].Substring(0, 4).Equals("TIME:")) { if (DateTime.TryParse(lines[i], out d)) { CurrentMsg.MessageTime = d; } } else if (lines[i].Substring(0, 4).Equals("IID:")) { rec.Address = lines[i].Substring(4).Trim(); } else if (lines[i].Substring(0, 5).Equals("WSID:")) { // break out other addresses rec.MachineID = lines[i].Substring(5).Trim(); } else if (lines[i].Substring(0, 5).Equals("FROM:")) { rec.Name = lines[i].Substring(5).Trim(); if (rec.Name.Contains("|")) { string[] s = rec.Name.Split('|'); rec.NickName = s[0]; rec.Name = s[1]; } } else if (lines[i].Substring(0, 8).Equals("SUBJECT:")) { CurrentMsg.Subject = lines[i].Substring(8).Trim(); } else if (lines[i].Substring(0, 5).Equals("BODY:")) { CurrentMsg.Message = lines[i].Substring(5).Trim(); CurrentMsg.ErrorCode = 0; } else { // add to the message CurrentMsg.Message += "\r" + lines[i]; } } if (rec.Address.Length > 18 && (rec.Name.Length == 0 || rec.MachineID.Length == 0)) { // Search by address and fill in info for (int i = 0; i < Contacts.Count; i++) { if (Contacts[i].Address == address) { rec = Contacts[i]; break; } } } CurrentMsg.Sender = rec; if (CurrentMsg.Async != async && async) { // Received an Synchronous msg and we're in sync mode // so return an error msg immediately and kill the // message so that it does not go anywhere CurrentMsg.Message = "Recipient is not set for synchronous communications"; CurrentMsg.ErrorCode = 1001; SendMessageOut(true); CurrentMsg = null; } // Remark on what we found if (DebugLevel > 4 && CurrentMsg != null) { WriteDebug("------------------------------------------------------------------------------"); WriteDebug("GETMESSAGE"); WriteDebug(" File =" + fName); WriteDebug(" Error =" + CurrentMsg.ErrorCode.ToString()); WriteDebug(" Msg ID =" + CurrentMsg.ID); WriteDebug(" From =" + CurrentMsg.Sender.Name); WriteDebug(" Inst ID=" + CurrentMsg.Sender.Address); WriteDebug(" MachID =" + CurrentMsg.Sender.MachineID); WriteDebug(" Subject=" + CurrentMsg.Subject); WriteDebug(" Body =" + CurrentMsg.Message); WriteDebug("------------------------------------------------------------------------------"); } // delete the semephore file try { System.IO.File.Delete(fName); } catch (Exception ex) { AddError(33, ex.Message, GetType().Name + ".GETMESSAGE", "Can't delete file " + fName); } // if async delete the message file if (async) { try { System.IO.File.Delete(fName.Replace(SendSemaphore, MessageExtension)); } catch (Exception ex) { AddError(34, ex.Message, GetType().Name + ".GETEMESSAGE", "Can't delete file " + fName.Replace(SendSemaphore, MessageExtension)); } } return(CurrentMsg != null); }