public override bool GetMessage() { bool Result = false; if (MessagesWaiting() > 0) { try { //WriteDebug(string.Format("getmessage - Connected={0}", clientSocket.Connected)); CurrentMsg = new CommunicationMessage(); CurrentMsg = InMsgQueue[0]; InMsgQueue.RemoveAt(0); // Is it an action message? Result = true; } catch (Exception ex) { AddError(904, ex.Message, ".GETMESSAGE", "Failed to retrieve message from inbound queue"); } } return(Result); }
/* * Create a new message structure and fill it in */ public override int CreateMessage(string address, string body, string subject) { int Result = 0; GetContactList(); try { Result = 1; CurrentMsg = new CommunicationMessage { Message = body, Subject = subject, Sender = MyInfo }; Result = AddRecipient(address); } catch (Exception ex) { AddError(Result = 35, ex.Message, GetType().Name + ".CREATEMESSAGE", "Failed to create message -> " + body); } // any failure during creation kills the message if (Result > 0) { CurrentMsg = null; } return(Result); }
/* * 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); }
public ComTCP() { Type = 4; MsgFormat = 2; Name = "Chat Client for Server"; CurrentMsg = new CommunicationMessage(); Contacts = new List <CommunicationRecipient>(); }
public ComChat() { Type = 3; MsgFormat = 2; Name = "Chat Client"; CurrentMsg = new CommunicationMessage(); Contacts = new List <CommunicationRecipient>(); }
/* * Pop the message at the top of the FIFO stack */ public override bool GetMessage() { CurrentMsg = new CommunicationMessage(); if (InMsgQueue.Count > 0) { CurrentMsg = InMsgQueue[0]; InMsgQueue.RemoveAt(0); CurrentMsg.ErrorCode = 0; CurrentMsg.ID = string.Empty; } return(CurrentMsg.Message.Length > 0); }
// Process the incoming messsage private void msg() { try { WriteDebug(string.Format("Msg - Connected={0}", clientSocket.Connected)); CommunicationMessage Msg = new CommunicationMessage(); Msg.Message = readData; InMsgQueue.Add(Msg); readData = null; } catch (Exception ex) { AddError(702, ex.Message, "GETMESSAGE"); } }
public override int CreateMessage(string contact, string body, string subject) { try { //WriteDebug(string.Format("Createmsg - Connected={0}", clientSocket.Connected)); CommunicationMessage Msg = new CommunicationMessage(); Msg.Message = body; OutMsgQueue.Add(Msg); } catch (Exception ex) { AddError(703, ex.Message, "GETMESSAGE"); } return(0); }
/* * 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); }
/* * When a message comes in, put it into the inbound message queue * which is why it's continuously looping in a thread * * TODO: Add automation to handle various things and to allow ease of * making a simple bot to bounce flooders and other such common * issues for an IRC channel * */ void readChat(IrcClient irc) { string message = null; inThread = true; if (DebugLevel > 8) { WriteDebug("readChat has started"); } while (chatThreadStop == false) { // Are you connected? try { Connected = irc.Connected(); chatThreadStop = (!Connected); } catch (Exception ex) { AddError(50, ex.Message, GetType().Name + ".READCHAT", "Failure checking connection"); } // Get the next message try { if (DebugLevel > 8) { WriteDebug("ComIRC.readChat - Waiting for message"); } message = irc.readMessage(); } catch (Exception ex) { AddError(50, ex.Message, GetType().Name + ".READCHAT", "readMessage() failure"); } // Is there a message? if (message != null) { try { // Is it a ping? if (message.Contains("PING ")) { if (DebugLevel > 5) { WriteDebug(string.Format("{0}", message)); } irc.sendIrcMessage(message.Replace("PING", "PONG")); pings++; } else { // parse the message CommunicationMessage msg = new CommunicationMessage(); if (message.IndexOf(':') == 0) { // This is a message from someone or something int prv = message.IndexOf(" PRIVMSG "); int xpt = message.IndexOf('!'); int xpt2 = message.IndexOf('!', xpt + 1); int spc = message.IndexOf(' '); int cln2 = message.IndexOf(':', 1); int nk = message.IndexOf(" " + UserNick + " "); int nkmsg = message.IndexOf(" " + UserNick + " :"); if (xpt > 0 && xpt < spc) { // there's a nick/name combo such as // :JWVFPDev1!JWVFPDev1@localhost // or // :JWVFPDev1!~JWVFPDev1@localhost msg.Sender.NickName = message.Substring(1, xpt - 1); msg.Sender.Name = message.Substring(xpt + 1, spc - xpt).Replace("~", "").Trim(); } else { // must be an address like // :nonstop.ix.me.dal.net 372 JWVFPDev4 msg.Sender.Address = message.Substring(1, spc).Trim(); } if (prv > 0) { // Actual PRIVMSG from someone // Example-> :JWVFPDev1!JWVFPDev1@localhost PRIVMSG #Test1234 :hello // ^Nick^^^^ ^Name@host^^^^^^^^^ ^Channel^ ^Msg^ msg.Sender.Address = (message.IndexOf(':', prv) > 0 ? message.Substring(prv + 7, message.IndexOf(':', prv) - prv + 7) : "").Trim(); // Channel message = (message.IndexOf(':', prv) > 0 ? message.Substring(message.IndexOf(':', prv) + 1) : ""); // message if (DebugLevel > 8) { WriteDebug(string.Format("Code 1 msg - '{0}", message)); } try { // Encryption tie-in if (message.Length > 8 && message.Substring(0, 8).Equals("[CRYPTOR")) { if (DebugLevel > 8) { WriteDebug(string.Format("Decryption of '{0}'", message)); } message = cryptText(message); if (DebugLevel > 8) { WriteDebug(string.Format(" returns '{0}'", message)); } } } catch (Exception ex) { AddError(61, ex.Message, GetType().Name + ".READCHAT", string.Format("Failed with message {0}", message)); } msg.Message = message; msg.Code = 1; // TODO: Keep track of users on the channel - is there a way to see when people leave? } else { if (message.Substring(0, UserNick.Length + 1).Equals(":" + UserNick)) { // Expecting a user control message like // :JWVFPDev1 MODE JWVFPDev1 :+iw // :[email protected] JOIN :#Test1234 msg.Subject = message.Substring(message.IndexOf(' ')).Trim(); // expect a : but have a backup plan if (msg.Subject.Contains(":")) { msg.Message = msg.Subject.Substring(msg.Subject.IndexOf(':') + 1).Trim(); } else { msg.Message = msg.Subject.Substring(msg.Subject.IndexOf(' ') + 1).Trim(); } msg.Subject = msg.Subject.Substring(0, msg.Subject.IndexOf(' ')).Trim(); msg.Code = 2; } else if (nkmsg > spc) { // "Nick :" is just like PRIVMSG // :nonstop.ix.me.dal.net 372 JWVFPDev4 :- * We reserve the right to deny access to this server without * // :^Address^^^^^^^^^^^^^ ^Nick^^^^ ^Message^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ msg.Message = (message.IndexOf(':', nkmsg) > 0 ? message.Substring(message.IndexOf(':', nkmsg) + 1) : ""); // message msg.Code = 3; } else { if (nk > spc) { // _Nick_ after first space in raw message // Example-> :lion.tx.us.dal.net 005 JWVFPDev2 CASEMAPPING=ascii WATCH=128 SILENCE=10 ELIST=cmntu EXCEPTS INVEX CHANMODES=beI,k,jl,ciPAmMnOprRsSt msg.Subject = message.Substring(nk).Trim(); // command msg.Code = 4; } else { // TODO Break command and misc out from message // Message from system // Examples-> :[email protected] JOIN :#Test1234 // ^Nick^^^^ ^Name@host^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^CMD ^Channel^ if (cln2 > 0 && cln2 > spc) { msg.Subject = message.Substring(spc + 1, cln2 - spc - 1).Trim(); msg.Message = message.Substring(cln2 + 1).Trim(); msg.Code = 5; } else { // it's a command msg.Message = message.Substring(spc + 1).Trim(); msg.Code = 6; } } } } } else { // Weird message msg.Code = 99; msg.Message = message; } if (DebugLevel > 5) { WriteDebug(string.Format("<-{0}\r\n Nick: {1}\r\n Name: {2}\r\n Address: {3}\r\n Subject: {4}\r\n Code: {5}\r\n Message: {6}\r\n", message, msg.Sender.NickName, msg.Sender.Name, msg.Sender.Address, msg.Subject, msg.Code, msg.Message)); } // Code 4 - User commands from Server if (msg.Code == 2) { // Possible command if (msg.Subject.Equals("JOIN") && UserHost.Length == 0) { if (msg.Sender.Name.Contains("@")) { UserHost = msg.Sender.Name.Substring(msg.Sender.Name.IndexOf("@") + 1); irc.SetHost(UserHost); //if (DebugLevel > 3) WriteDebug("Setting UserHost to " + UserHost); } } } InMsgQueue.Add(msg); } } catch (Exception ex) { AddError(51, ex.Message, GetType().Name + ".READCHAT"); } } } inThread = false; }
/* * 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); }
/* * * Receiving a message * */ private void MessageCallBack(IAsyncResult aResult) { if (DebugLevel > 8) { WriteDebug("In MessageCallBack"); } try { int size = sck.EndReceiveFrom(aResult, ref epRemote); // check if theres actually information if (size > 0) { // used to help us on getting the data byte[] receivedData = new byte[1464]; // getting the message data receivedData = (byte[])aResult.AsyncState; // converts message data byte array to string ASCIIEncoding eEncoding = new ASCIIEncoding(); string receivedMessage = eEncoding.GetString(receivedData); receivedMessage = receivedMessage.Replace("\0", string.Empty).Trim(); if (DebugLevel > 5) { WriteDebug("<-" + receivedMessage); } // Save the message to the InMsgQueue list CommunicationMessage msg = new CommunicationMessage(); if (receivedMessage.IndexOf("#") > 0) { if (DebugLevel > 8) { WriteDebug("Breaking out name"); } string[] nameInfo = receivedMessage.Substring(0, receivedMessage.IndexOf("#")).Split('|'); msg.Sender.NickName = nameInfo[0]; if (nameInfo.Length > 1) { msg.Sender.Name = nameInfo[1]; } if (nameInfo.Length > 2) { msg.Sender.IP4Address = nameInfo[2]; } if (nameInfo.Length > 3) { msg.Sender.MachineID = nameInfo[3]; // add to contacts list if not already } if (nameInfo.Length > 5) { // private message string recipient = nameInfo[4].Replace("[", "").Replace("]", ""); // datetime it was sent DateTime dtm; if (DateTime.TryParse(nameInfo[5].Replace("{", "").Replace("}", ""), out dtm) == false) { dtm = DateTime.Now; } msg.MessageTime = dtm; if (recipient.Length > 0) { if (recipient.ToUpper().Equals(UserName.ToUpper()) == false) { // Received a server message, so trash it AddError(805, "Received private message", GetType().Name + ".MESSAGECALLBACK", "Received message meant for " + (recipient.Equals("?") ? "Chat Server" : recipient)); msg = null; } } } else { // This goes to a debug file no matter what int dbl = DebugLevel; DebugLevel = 1; WriteDebug(string.Format(">>>> BAD MESSAGE FORMAT <<<<\r\n {0}\r\n\r\n", receivedMessage)); DebugLevel = dbl; } if (msg != null) { string m = receivedMessage.Substring(receivedMessage.IndexOf("#") + 1).Trim(); // handle any received cryptor messages if (m.Substring(0, 8).Equals("[CRYPTOR")) { if (DebugLevel > 5) { WriteDebug(string.Format("Calling Cryptor with {0}", m)); } m = cryptText(m); } msg.Message = m; } } else { msg.Message = receivedMessage; } // was the message tossed (is null) or do we save it to the queue? if (msg != null) { if (DebugLevel > 5) { WriteDebug(string.Format("<-{0}\r\n Nick: {1}\r\n Name: {2}\r\n Address: {3}\r\n Machine: {4}\r\n Message: {5}\r\n", receivedMessage, msg.Sender.NickName, msg.Sender.Name, msg.Sender.Address, msg.Sender.MachineID, msg.Message)); } InMsgQueue.Add(msg); } if (DebugLevel > 5) { WriteDebug("Waiting..."); } // starts listening to the socket again buffer = new byte[1500]; sck.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(MessageCallBack), buffer); } catch (Exception ex) { AddError(803, ex.Message, GetType().Name + ".MESSAGECALLBACK"); } }