diff --git a/SparkleShare/Mac/SparkleSetup.cs b/SparkleShare/Mac/SparkleSetup.cs index c48c26b0..863031c3 100755 --- a/SparkleShare/Mac/SparkleSetup.cs +++ b/SparkleShare/Mac/SparkleSetup.cs @@ -347,6 +347,13 @@ namespace SparkleShare { Buttons.Add (CancelButton); + Controller.CheckAddPage ( + AddressTextField.StringValue, + PathTextField.StringValue, + TableView.SelectedRow + ); + + break; } diff --git a/SparkleShare/SparkleControllerBase.cs b/SparkleShare/SparkleControllerBase.cs index b9d3e296..b23fa703 100755 --- a/SparkleShare/SparkleControllerBase.cs +++ b/SparkleShare/SparkleControllerBase.cs @@ -139,6 +139,10 @@ namespace SparkleShare { SparkleInviteListener invite_listener = new SparkleInviteListener (1986); invite_listener.InviteReceived += delegate (SparkleInvite invite) { + + Console.WriteLine (invite.Host + " " + invite.Path + " " + invite.Token); + return; + if (OnInvite != null && !FirstRun) OnInvite (invite); }; diff --git a/SparkleShare/SparkleInvite.cs b/SparkleShare/SparkleInvite.cs index 1b94f9b4..c07bd794 100644 --- a/SparkleShare/SparkleInvite.cs +++ b/SparkleShare/SparkleInvite.cs @@ -20,8 +20,11 @@ using System.IO; using System.Net; using System.Net.Sockets; using System.Text; +using System.Xml; using System.Threading; +using SparkleLib; + namespace SparkleShare { public class SparkleInvite { @@ -44,9 +47,21 @@ namespace SparkleShare { public SparkleInvite (string host, string path, string token) { - FullAddress = new Uri (host + "/" + path); + if (path.StartsWith ("/")) + path = path.Substring (1); + + if (!host.EndsWith ("/")) + host = host + "/"; + + FullAddress = new Uri ("ssh://" + host + path); Token = token; } + + + public SparkleInvite (string xml_file_path) + { + // TODO + } } @@ -93,7 +108,7 @@ namespace SparkleShare { TcpClient tcp_client = (TcpClient) client; NetworkStream client_stream = tcp_client.GetStream (); - byte [] message = new byte[4096]; + byte [] message = new byte [4096]; int bytes_read; while (true) @@ -114,15 +129,67 @@ namespace SparkleShare { ASCIIEncoding encoding = new ASCIIEncoding (); string received_message = encoding.GetString (message, 0, bytes_read); + string invite_xml = ""; - // SparkleShare's protocol format looks like this: - // sparkle://user@host.org/path/token - Uri uri = new Uri (received_message); - string token = uri.AbsolutePath.Substring (uri.AbsolutePath.LastIndexOf ("/") + 1); - string path = uri.AbsolutePath.Substring (0, uri.AbsolutePath.LastIndexOf ("/")); + if (received_message.StartsWith (Uri.UriSchemeHttp) || + received_message.StartsWith (Uri.UriSchemeHttps)) { + + WebClient web_client = new WebClient (); + + try { + // Fetch the invite file + byte [] buffer = web_client.DownloadData (received_message); + SparkleHelpers.DebugInfo ("Invite", "Received: " + received_message); + + invite_xml = ASCIIEncoding.ASCII.GetString (buffer); + + } catch (WebException e) { + SparkleHelpers.DebugInfo ("Invite", "Failed downloading: " + received_message); + continue; + } + + } else if (received_message.StartsWith (Uri.UriSchemeFile)) { + try { + received_message = received_message.Replace (Uri.UriSchemeFile + "://", ""); + invite_xml = File.ReadAllText (received_message); + + } catch { + SparkleHelpers.DebugInfo ("Invite", "Failed opening: " + received_message); + continue; + } + + } else { + SparkleHelpers.DebugInfo ("Invite", + "Path to invite must use either the file:// or http(s):// scheme"); + + continue; + } + + XmlDocument xml_document = new XmlDocument (); + XmlNode node; + string host = ""; + string path = ""; + string token = ""; + + try { + xml_document.LoadXml (invite_xml); + + node = xml_document.SelectSingleNode ("/sparkleshare/invite/host/text()"); + if (node != null) { host = node.Value; } + + node = xml_document.SelectSingleNode ("/sparkleshare/invite/path/text()"); + if (node != null) { path = node.Value; } + + node = xml_document.SelectSingleNode ("/sparkleshare/invite/token/text()"); + if (node != null) { token = node.Value; } + + } catch (XmlException e) { + SparkleHelpers.DebugInfo ("Invite", "Not valid XML: " + received_message + " " + e.Message); + return; + } if (InviteReceived != null) - InviteReceived (new SparkleInvite (uri.Host, path, token)); + InviteReceived (new SparkleInvite (host, path, token)); } tcp_client.Close ();