private void processIncommingMessageLoop() { while (true) { MSNMessage message = null; try { #region wait for connected socket while (switchboardSocket.connected() != true) { try { Thread.Sleep(100); } catch (Exception) { } } #endregion message = switchboardSocket.recieve(); #region ignore null messages if (message == null) { continue; } #endregion String messageString = message.ToString(); #region handle JOI if (messageString.StartsWith("JOI")) { //JOI [email protected] Dave\r\n String username = message.getTokens()[1].ToString(); if (connectedUsers.Contains(username) != true) { connectedUsers.Add(username); } if (activeUsers.Contains(username) != true) { activeUsers.Add(username); } sendSwitchboardListenerUserConnectedDisconnected(username, true); } #endregion #region handle CAL else if (messageString.StartsWith("CAL")) { //CAL 8 RINGING 17342299\r\n } #endregion #region handle IRO else if (messageString.StartsWith("IRO")) { //IRO 1 1 2 [email protected] Mike\r\n String username = message.getTokens()[4].ToString(); if (connectedUsers.Contains(username) != true) { connectedUsers.Add(username); } if (activeUsers.Contains(username) != true) { activeUsers.Add(username); } sendSwitchboardListenerUserConnectedDisconnected(username, true); } #endregion #region handle MSG else if (messageString.StartsWith("MSG")) { //MSG [email protected] Mike 133\r\n //MIME-Version: 1.0\r\n //Content-Type: text/plain; charset=UTF-8\r\n //X-MMS-IM-Format: FN=Arial; EF=I; CO=0; CS=0; PF=22\r\n //\r\n //Hello! How are you? try { String wholeString = messageString + "\r\n" + switchboardSocket.recieve(int.Parse(message.getData()[1])); String[] wholeStringTokens = wholeString.Split(new String[] { "\r\n" }, StringSplitOptions.None); if (wholeStringTokens.Length >= 4 && wholeStringTokens[3].StartsWith("TypingUser:"******"Content-Type: text/x-mms-emoticon")) { String payload = ""; for (int i = 4; i < wholeStringTokens.Length; i++) { payload += wholeStringTokens[i] + "\r\n"; } MSNUserMessage num = new MSNUserIncommingEmoticon(wholeStringTokens[0].Split(new char[] { ' ' })[1], payload); incommingMSGQueue.Enqueue(num); } else { #region process payload String payload = ""; for (int i = 5; i < wholeStringTokens.Length; i++) { payload += wholeStringTokens[i] + "\r\n"; } if (payload.EndsWith("\r\n")) { payload = payload.Substring(0, payload.Length - 2); } #endregion if (wholeStringTokens[3].Equals("")) { wholeStringTokens[3] = "X-MMS-IM-Format: FN=Arial; EF=I; CO=0; CS=0; PF=22"; } MSNUserMessage num = new MSNUserIncommingMessage((wholeStringTokens[0].Split(new char[] { ' ' }))[1], (wholeStringTokens[3].Split(new char[] { ' ' }))[1].Replace("FN=", "").Replace(";", ""), payload); incommingMSGQueue.Enqueue(num); } } catch (Exception) { Console.WriteLine("Error processing message in MSNSwitchboard.processIncommingMessage(), message = " + messageString); } } #endregion #region handle BYE else if (messageString.StartsWith("BYE")) { //BYE [email protected]\r\n String username = messageString.Replace("\r\n", "").Split(new char[] { ' ' })[1]; activeUsers.Remove(username); //connectedUsers.Remove(username); sendSwitchboardListenerUserConnectedDisconnected(username, false); } #endregion } catch (Exception) { if (message != null) { Console.WriteLine("Error processing incomming message in MSNSwitchboard.processIncommingMessageLoop(), message = " + message.ToString()); } else { Console.WriteLine("Error processing incomming message in MSNSwitchboard.processIncommingMessageLoop(), message = NULL"); } if (switchboardSocket.connected() != true) { while (switchboardSocket != null && switchboardSocket.connected() != true) { try { Thread.Sleep(300); } catch (Exception) { Console.WriteLine("Error waiting for switchboard to be reconnected in MSNSwitchboard.processIncommingMessageLoop()"); } } } } } }
private void processMessageLoop() { MSNMessage message = new MSNMessage("NULL"); while (true) { if (processingQueue.Count > 0) { try { #region get message from queue message = processingQueue.Dequeue(); String command = message.getCommand(); String[] data = message.getTokens(); #endregion #region handle SYN if (command.Equals("SYN")) { //SYN 9 1 18 7 versionID = int.Parse(data[2]); } #endregion #region handle GTC else if (command.Equals("GTC")) { //GTC A if (data[1].Equals("A")) //manual { handleNewContact = MSNEnumerations.NewContact.manual_add; } else if (data[1].Equals("N")) //auto { handleNewContact = MSNEnumerations.NewContact.auto_add; } } #endregion #region handle LSG else if (command.Equals("LSG")) { //LSG 0 Individuals 0 if (!data[2].Equals("~")) { String groupName = HttpUtility.UrlDecode(data[2]); MSNGroup g = new MSNGroup(controller, groupName, int.Parse(data[1])); if (!groupsByName.ContainsKey(groupName)) { groupsByName.Add(groupName, g); groupsByNumber.Add(int.Parse(data[1]), g); } } } #endregion #region handle LST else if (command.Equals("LST")) { //LST [email protected] derek_bartram 11 3 //all contacts send 11!?! String username = data[1]; #region get / make contact MSNContact c = getContact(username); lastContact = c; #endregion #region process friendly name c.FriendlyName = HttpUtility.UrlDecode(data[2]); #endregion #region process groups if (data.Length == 5) { char[] splitChars = new char[1]; splitChars[0] = ','; String[] groupIDs = data[4].Split(splitChars); for (int i = 0; i < groupIDs.Length; i++) { int groupID = int.Parse(groupIDs[i]); if (groupsByNumber.ContainsKey(int.Parse(groupIDs[i]))) { MSNGroup g = groupsByNumber[int.Parse(groupIDs[i])]; c.Groups.Add(g); g.Contacts.Add(c); } } } #endregion } #endregion #region handle BPR else if (command.Equals("BPR")) { //BPR 12182 [email protected] PHH 555%20555%204321 //BPR 12182 [email protected] PHW I%20AM%20DUMB\r\n //BPR 12182 [email protected] PHM I%20Dont%20Have%20One\r\n //BPR 12182 [email protected] MOB Y\r\n //BPR MOB Y MSNContact c = lastContact; String phoneString = ""; if (data.Length == 3) { phoneString = HttpUtility.UrlDecode(data[2]); } c.setPhone(MSNStaticHelperFunctions.toPhoneTypes(data[1]), phoneString); } #endregion #region handle ILN else if (command.Equals("ILN")) { //ILN 10 NLN [email protected] 80%25%20Andy%20::%205.5%20ml%20Marky 1347272748 %3Cmsnobj%20Creator%3D%22webmaster%40norbosoft.zzn.com%22%20Size%3D%2216657%22%20Type%3D%223%22%20Location%3D%22TFR100.dat%22%20Friendly%3D%22UwBpAGQAAAA%3D%22%20SHA1D%3D%22LCJswyrzD1bDGR41HJ3xmfoBbqM%3D%22%20SHA1C%3D%22bYULHZIyQUxMB%2BFM1FNO%2BYAiP6M%3D%22%20contenttype%3D%22M%22%20contentid%3D%22U3097935%22%2F%3E MSNContact c = getContact(data[3]); c.Status = MSNStaticHelperFunctions.toUserStatus(data[2]); if (data.Length >= 5) { c.FriendlyName = HttpUtility.UrlDecode(data[4]); } } #endregion #region handle NLN else if (command.Equals("NLN")) { //NLN AWY [email protected] derek_bartram 1347272812 %3Cmsnobj%20Creator%3D%22derek_bartram%40hotmail.com%22%20Size%3D%2229106%22%20Type%3D%223%22%20Location%3D%22xpp539.dat%22%20Friendly%3D%22AAA%3D%22%20SHA1D%3D%22YeWGuRSbOGhJ1Wbk3JY5vCzoqT8%3D%22%20SHA1C%3D%22nj0278TZP%2FP4yIGc6qxIXrY2olQ%3D%22%2F%3E MSNContact c = getContact(data[2]); c.Status = MSNStaticHelperFunctions.toUserStatus(data[1]); if (data.Length >= 4) { c.FriendlyName = HttpUtility.UrlDecode(data[3]); } } #endregion else if (command.Equals("FLN")) { MSNContact c = getContact(data[1]); c.Status = MSNStaticHelperFunctions.toUserStatus("FLN"); } #region unknown else { throw new Exception("Unknown message type in MSNContactsList.processMessageLoop() <" + message.ToString() + ">"); } #endregion } catch (Exception) { Console.Error.WriteLine("Error processing command in MSNContactsList.processMessageLoop()\r\n" + message.ToString()); } } else { Thread.Sleep(20); } } }