/// <summary> /// Event handler for incoming FSD packets. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> static void OnIncomingFsdPacket(object sender, CaptureEventArgs e) { DateTime timestamp = DateTime.Now; // FsdPacket trims the newline, so we have to grab the byte[] ourselves string pktString = System.Text.Encoding.UTF8.GetString(e.Packet.Data); string[] inputs = pktString.Split(new String[] { "\n" }, StringSplitOptions.None); foreach (string line in inputs) { // Strip out the garbage that appears in between FSD packets string input = Regex.Replace(line, "^.*\\$", "$", RegexOptions.Multiline); input = Regex.Replace(input, "^.*#", "#", RegexOptions.Multiline); input = Regex.Replace(input, "^.*%", "%", RegexOptions.Multiline); input = Regex.Replace(input, "^.*@", "@", RegexOptions.Multiline); // First, create a FsdPacket object from the packet FsdPacket currPacket = new FsdPacket(timestamp, input); if (input.StartsWith("#TM")) { FsdMessage input_pm = new FsdMessage(timestamp, input); } } // First, create a FsdPacket object from the packet FsdPacket pkt = new FsdPacket(timestamp, e.Packet.Data); // Only do something if it's a PM if (pkt.PacketString.StartsWith("#TM")) { FsdMessage pm = new FsdMessage(timestamp, pkt.PacketString); if (IsForwardMessage(pm)) { string loggingString = String.Format("{0} > {1} ({2}):\"{3}\" ", pm.Sender, pm.Recipient, pm.Timestamp.ToUniversalTime(), pm.Message); Console.WriteLine(loggingString); logger.Log(String.Format("<{0}>", pkt.PacketString)); logger.Log(loggingString); try { am.ForwardMessage(pm); } catch (FcomApiException ex) { logger.Log(ex.Message); } // Do not forward messages sent over the frequency, that aren't addressed to the user //if (pm.Message.StartsWith(callsign)) // am.ForwardMessage(pm); } } }
/// <summary> /// Helper method for determining if a FsdMessage should be forwarded. /// </summary> /// <param name="f">The FsdMessage in question</param> /// <returns>True if it should be forwarded, False otherwise</returns> static private bool IsForwardMessage(FsdMessage msg) { // Under-the-hood ones to SERVER/FP/DATA... bool isServerMessage = string.Equals(msg.Sender, "server", StringComparison.OrdinalIgnoreCase) || string.Equals(msg.Recipient, "server", StringComparison.OrdinalIgnoreCase) || string.Equals(msg.Recipient, "fp", StringComparison.OrdinalIgnoreCase) || string.Equals(msg.Recipient, "data", StringComparison.OrdinalIgnoreCase) ; // on-frequency and private messages addressed to the user... // (NOTE: using string.StartsWith() results in partial matches (e.g. UAL1/UAL123), so use regex instead) // Regex: ^{callsign}( |,).* Regex frequencyMessagePattern = new Regex("^" + callsign + @"( |,).*", RegexOptions.IgnoreCase); bool isAddressedToUser = frequencyMessagePattern.IsMatch(msg.Message) || string.Equals(msg.Recipient, callsign, StringComparison.OrdinalIgnoreCase); // self-addressed messages: bool isSelfMessage = string.Equals(msg.Sender, callsign, StringComparison.OrdinalIgnoreCase); return(!isServerMessage && isAddressedToUser && !isSelfMessage); }
/// <summary> /// Uploads a message to the API for forwarding via Discord. /// </summary> /// <param name="pm"></param> public void UploadMessage(FsdMessage pm) { // convert timestamp to Unix time DateTimeOffset dateTimeOffset = new DateTimeOffset(pm.Timestamp); long unixTimestamp = dateTimeOffset.ToUnixTimeMilliseconds(); // Sorry for the ugly and messy-looking JSON :( string json = string.Format( "{{\"privateMessage\":{{\"token\":\"{0}\",\"timestamp\":\"{1}\",\"sender\":\"{2}\",\"receiver\":\"{3}\",\"message\":\"{4}\"}}}}", TOKEN, unixTimestamp, pm.Sender, pm.Recipient, pm.Message); Console.WriteLine("-- Forwarding to " + SERVER_ADDRESS); Console.WriteLine("-- " + json); var request = new RestRequest(MESSAGE_FORWARDING_ENDPOINT, Method.POST); request.AddParameter("application/json", json, ParameterType.RequestBody); IRestResponse response = client.Execute(request); var content = response.Content; // TODO: handle API response (success / error etc) }