From 9ff5af65f954b81cc5fec7c3fe48c1c0b34468e0 Mon Sep 17 00:00:00 2001 From: icedream Date: Wed, 7 May 2014 22:10:15 +0200 Subject: [PATCH] np.Connect --- src/npmotd/Program.cs | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/src/npmotd/Program.cs b/src/npmotd/Program.cs index ac3de4f..8a2e0c2 100644 --- a/src/npmotd/Program.cs +++ b/src/npmotd/Program.cs @@ -8,10 +8,26 @@ namespace NPSharp.CommandLine.MOTD { static void Main(string[] args) { - var hostname = args[0]; - var username = args[1]; - var password = args[2]; + if (args.Length < 4) + { + Console.Error.WriteLine("Needs 4 arguments: hostname port username password"); + return; + } + var hostname = args[0]; + var port = ushort.Parse(args[1]); + var username = args[2]; + var password = args[3]; + + // NP connection setup + var np = new NPClient(hostname, port); + if (!np.Connect()) + { + Console.Error.WriteLine("Connection to NP server failed."); + return; + } + + // Get session token var ah = new AuthenticationHelper(hostname); try { @@ -27,14 +43,18 @@ namespace NPSharp.CommandLine.MOTD return; } - var np = new NPClient(hostname); + // Validate authentication using session token try { np.AuthenticateWithToken(ah.SessionToken).Wait(); } - catch + catch (Exception err) { - Console.Error.WriteLine("Authenticated but session token was invalid."); +#if DEBUG + Console.Error.WriteLine("Authenticated but session token was invalid. {0}", err); +#else + Console.Error.WriteLine("Authenticated but session token was invalid ({0}).", err.Message); +#endif return; }