Skip to content

Commit

Permalink
Merge branch 'release-0.8.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
norrisng committed Oct 19, 2018
2 parents d635e9a + 8fc9902 commit 7c5094f
Show file tree
Hide file tree
Showing 14 changed files with 171 additions and 67 deletions.
43 changes: 43 additions & 0 deletions FcomClient/FcomClient.Diagnostics/Logger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;

namespace FcomClient.Diagnostics
{
class Logger
{
/// <summary>
/// Name of the log file.
/// </summary>
private string Filename = "log.txt";

/// <summary>
/// Default constructor. Saves all log messages to "log.txt".
/// </summary>
public Logger()
{

}

/// <summary>
/// Constructor for saving all log messages to a specific file.
/// </summary>
/// <param name="Filename">Name of file to save to</param>
public Logger(string Filename)
{
this.Filename = Filename;
}

/// <summary>
/// Saves a log message to the specified file.
/// The timestamp is automatically appended to the front of the message.
/// </summary>
/// <param name="msg"></param>
public void Log(string msg)
{
// YYYY-MM-DD hh:mm:ssZ
DateTime timestamp = DateTime.UtcNow;
string logMessage = String.Format("{0}: {1}", timestamp.ToString("u"), msg);
System.IO.File.AppendAllLines(Filename, new string[] { logMessage });
}

}
}
6 changes: 1 addition & 5 deletions FcomClient/FcomClient.FsdDetection/ConnectionManager.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections.Generic;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using SharpPcap;

namespace FcomClient.FsdDetection
Expand Down
4 changes: 0 additions & 4 deletions FcomClient/FcomClient.FsdObject/AbstractFsdPacket.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FcomClient.FsdObject
{
Expand Down
4 changes: 0 additions & 4 deletions FcomClient/FcomClient.FsdObject/FsdMessage.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FcomClient.FsdObject
{
Expand Down
8 changes: 4 additions & 4 deletions FcomClient/FcomClient.FsdObject/FsdPacket.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FcomClient.FsdObject
{
Expand Down Expand Up @@ -66,6 +62,10 @@ public FsdPacket(DateTime timestamp, byte[] packetString)
// then, convert to string and chop off the trailing newline
this.PacketString = System.Text.Encoding.UTF8.GetString(noHeader).Trim();

// it seems that the server will sometimes smush multiple FSD packets into a single TCP packet.
// if this happens, break them up.
// TODO

// raise event if PM
if (this.IsPrivateMessage() && OnMessageArrival != null)
OnMessageArrival();
Expand Down
4 changes: 0 additions & 4 deletions FcomClient/FcomClient.FsdObject/PrivateMessage.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FcomClient.FsdObject
{
Expand Down
10 changes: 7 additions & 3 deletions FcomClient/FcomClient.Serialization/ApiManager.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FcomClient.Serialization.ApiObjects;
using RestSharp;
using RestSharp.Deserializers;
Expand All @@ -14,6 +11,11 @@ namespace FcomClient.Serialization
/// </summary>
class ApiManager
{
/// <summary>
/// User agent string to use.
/// </summary>
private readonly string CLIENT_VERSION = "FcomClient/0.8.0";

/// <summary>
/// Server address, read from the file server_location.txt
/// </summary>
Expand Down Expand Up @@ -64,6 +66,8 @@ public ApiManager(string token, string callsign)
RestRequest registerRequest
= new RestRequest(REGISTRATION_ENDPOINT, Method.GET);

client.UserAgent = CLIENT_VERSION;

registerRequest.AddParameter("token", token);
registerRequest.AddParameter("callsign", callsign);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Generic;

namespace FcomClient.Serialization.ApiObjects
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using RestSharp.Deserializers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FcomClient.Serialization.ApiObjects
{
Expand Down
4 changes: 0 additions & 4 deletions FcomClient/FcomClient.Serialization/MessageForwarder.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RestSharp;
using FcomClient.FsdObject;

Expand Down
87 changes: 59 additions & 28 deletions FcomClient/FcomClient.UI/FcomClient.cs
Original file line number Diff line number Diff line change
@@ -1,43 +1,58 @@
using FcomClient.FsdDetection;
using FcomClient.Diagnostics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SharpPcap;
using FcomClient.Serialization;
using FcomClient.FsdObject;
using System.Text.RegularExpressions;

namespace FcomClient.UI
{
class FcomClient
{
static string callsign = "";
static ApiManager am;
static Logger logger = new Logger(); // log.txt

static void Main(string[] args)
{
logger.Log("Starting FcomClient...");

bool isRegistered = false;

while (!isRegistered)
{
Console.Write("\nPlease enter your exact callsign, then press Enter: ");
callsign = Console.ReadLine();
bool isInputValid = false;

while (!isInputValid)
{
Console.Write("\nPlease enter your exact callsign, then press Enter: ");
callsign = Console.ReadLine();

Regex callsignFormat = new Regex(@"^(\d|\w|_|-)+$");
if (callsignFormat.IsMatch(callsign))
isInputValid = true;
else
Console.WriteLine("Invalid callsign!");
}

Console.Write("\nPlease enter the verification code from Discord, then press Enter:\n");
string token = Console.ReadLine();

logger.Log(String.Format("Callsign: \"{0}\", Token: \"{1}\"", callsign, token));

Console.WriteLine("\nRegistering token with Discord bot...");
am = new ApiManager(token, callsign);

if (am.DiscordId != null)
if (am.DiscordId != 0)
{
Console.WriteLine("Registered {0} to Discord user {1} ({2})", callsign, am.DiscordName, am.DiscordId);
isRegistered = true;
}
else
{
Console.WriteLine("Could not register!");
Console.WriteLine("Could not register! ");
}
}

Expand Down Expand Up @@ -72,16 +87,17 @@ static void Main(string[] args)
}

i++;
}
}

bool parseSuccess = false;
int deviceNumber = -1;
Console.WriteLine("\nWhich of the above is your internet connection?");

// Ignore invalid inputs
while (deviceNumber < 0 || deviceNumber >= connections.Count)
while (deviceNumber < 0 || deviceNumber >= connections.Count || !parseSuccess)
{
Console.Write("Enter the corresponding number: ");
Int32.TryParse(Console.ReadLine(), out deviceNumber);
parseSuccess = Int32.TryParse(Console.ReadLine(), out deviceNumber);
}

device = connections[deviceNumber].Device;
Expand All @@ -105,6 +121,8 @@ static void Main(string[] args)
Console.WriteLine("\nYou may now minimize this window. To quit, simply close it.");
Console.WriteLine("When you're done, close this window and send \"remove\" to the Discord bot!\n\n");

logger.Log("Starting FSD capture...");

// Start capturing packets indefinitely
device.Capture();

Expand All @@ -130,28 +148,41 @@ static void OnIncomingFsdPacket(object sender, CaptureEventArgs e)
// Only do something if it's a PM
if (/*pkt.PacketString.EndsWith("\n") && */pkt.PacketString.StartsWith("#TM"))
{
FsdMessage pm = new FsdMessage(timestamp, pkt.PacketString);

// ignore non-PM messages (i.e. from/to SERVER, to FP, to DATA), (as well as outgoing PMs)
if (!string.Equals(pm.Sender, "server", StringComparison.OrdinalIgnoreCase) &&
!string.Equals(pm.Recipient, "server", StringComparison.OrdinalIgnoreCase) &&
!string.Equals(pm.Recipient, "fp", StringComparison.OrdinalIgnoreCase) &&
!string.Equals(pm.Recipient, "data", StringComparison.OrdinalIgnoreCase) &&
!string.Equals(pm.Sender, callsign, StringComparison.OrdinalIgnoreCase))
{
Console.WriteLine("{0} ({1}): \n" +
"{2}",
pm.Sender, pm.Timestamp.ToLongTimeString(), pm.Message);
FsdMessage pm = new FsdMessage(timestamp, pkt.PacketString);

// Send it to the API
MessageForwarder api = new MessageForwarder();
// ignore certain messages:

// Do not forward messages sent over the frequency, that aren't addressed to the user
if (pm.Message.StartsWith(callsign))
am.ForwardMessage(pm);
// this includes under-the-hood ones to SERVER/FP/DATA...
bool isServerMessage =
string.Equals(pm.Sender, "server", StringComparison.OrdinalIgnoreCase) ||
string.Equals(pm.Recipient, "server", StringComparison.OrdinalIgnoreCase) ||
string.Equals(pm.Recipient, "fp", StringComparison.OrdinalIgnoreCase) ||
string.Equals(pm.Recipient, "data", StringComparison.OrdinalIgnoreCase)
;

// private/frequency messages not addressed to the user...
bool isAddressedToUser = pm.Message.StartsWith(callsign, StringComparison.OrdinalIgnoreCase) ||
string.Equals(pm.Recipient, callsign, StringComparison.OrdinalIgnoreCase);

// and self-addressed messages:
bool isSelfMessage = string.Equals(pm.Sender, callsign, StringComparison.OrdinalIgnoreCase);

// putting all of the above conditions together:
if (!isServerMessage && isAddressedToUser && !isSelfMessage)
{
string loggingString = String.Format("{0} > {1} ({2}):\"{3}\" ",
pm.Sender,
pm.Recipient,
pm.Timestamp.ToUniversalTime(),
pm.Message);
Console.WriteLine(loggingString);
logger.Log(loggingString);

am.ForwardMessage(pm);
//api.UploadMessage(pm);

// Do not forward messages sent over the frequency, that aren't addressed to the user
//if (pm.Message.StartsWith(callsign))
// am.ForwardMessage(pm);
}

}
Expand Down
1 change: 1 addition & 0 deletions FcomClient/FcomClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="FcomClient.Diagnostics\Logger.cs" />
<Compile Include="FcomClient.FsdDetection\ConnectionManager.cs" />
<Compile Include="FcomClient.FsdDetection\HardwareDevice.cs" />
<Compile Include="FcomClient.FsdObject\FsdPacket.cs" />
Expand Down
36 changes: 36 additions & 0 deletions FcomClient/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("FcomClient")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("FcomClient")]
[assembly: AssemblyCopyright("Noris Ng")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("045d79bf-2195-4f88-9802-a9fa28fb0f9f")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.8")]
[assembly: AssemblyFileVersion("0.8.0")]
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
# FCOM client #

The client that capture messages received over VATSIM / IVAO
The client that capture messages received over VATSIM / IVAO



## Requirements ##

* Windows 7 or newer
* .NET Framework 4.7.1 or newer
* [WinPcap](https://www.winpcap.org/)



## Download and installation ##

See the [Releases page on GitHub](https://github.com/norrisng/FcomClient/releases) for downloads.

To install, simply extract the FcomClient folder to anywhere as desired.

To run FcomClient, simply open `FcomClient.exe`.

0 comments on commit 7c5094f

Please sign in to comment.