//Check if the body of the email/sms contains a form notification private void CheckMessage(String message, String from, DateTime received) { if (message.StartsWith("X-Greylist")) { int encodingStart = message.IndexOf("\r\n\r\n"); if (encodingStart >= 0) { String base64Message = message.Substring(encodingStart + 4); Byte[] bytes = Convert.FromBase64String(base64Message); String decodedMessage = Encoding.UTF8.GetString(bytes, 0, bytes.Length); CheckMessage(decodedMessage, from, received); } } else { int start = message.LastIndexOf(MobileListener.Properties.Resources.NotificationPattern); if (start >= 0) { int end = message.IndexOf("\n", start); if (end > start) { String changed = message.Substring(start, end - start - 2); FormRequestInfo info = CommunicationUtil.ParseUrlParameters(changed); //Da cambiare: invece di mandare l'evento comunica info con WCF all'applicazione if (info != null) { info.From = from; info.Time = received; //If the main application is not running start it StringBuilder builder = new StringBuilder(); StringWriter writer = new StringWriter(builder); serializer.Serialize(writer, info); String data = XmlConvert.EncodeName(builder.ToString()); String mobilePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase); String mobileAppPath = mobilePath + @"\Mobile.exe"; Process proc = Process.Start(new ProcessStartInfo(mobileAppPath, data)); SendNotification(info); } } } } }
//Sends the notification to the main application private void SendNotification(FormRequestInfo info) { TcpClient client = null; NetworkStream stream = null; try { client = new TcpClient("localhost", 11111); StringBuilder builder = new StringBuilder(); StringWriter writer = new StringWriter(builder); serializer.Serialize(writer, info); Byte[] data = Encoding.UTF8.GetBytes(builder.ToString()); stream = client.GetStream(); stream.Write(data, 0, data.Length); stream.Flush(); stream.Close(); client.Close(); } catch (ArgumentNullException e) { if (stream != null) { stream.Close(); } if (client != null) { client.Close(); } } catch (SocketException e) { if (stream != null) { stream.Close(); } if (client != null) { client.Close(); } } }