Skip to content

Commit

Permalink
Listen for urls to an invite xml file on port 1986
Browse files Browse the repository at this point in the history
  • Loading branch information
hbons committed Dec 2, 2011
1 parent be4cb49 commit a8f523e
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 8 deletions.
7 changes: 7 additions & 0 deletions SparkleShare/Mac/SparkleSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,13 @@ public SparkleSetup () : base ()

Buttons.Add (CancelButton);

Controller.CheckAddPage (
AddressTextField.StringValue,
PathTextField.StringValue,
TableView.SelectedRow
);


break;
}

Expand Down
4 changes: 4 additions & 0 deletions SparkleShare/SparkleControllerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ public virtual void Initialize ()
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);
};
Expand Down
83 changes: 75 additions & 8 deletions SparkleShare/SparkleInvite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Xml;
using System.Threading;

using SparkleLib;

namespace SparkleShare {

public class SparkleInvite {
Expand All @@ -44,9 +47,21 @@ public string Path {

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
}
}


Expand Down Expand Up @@ -93,7 +108,7 @@ private void HandleClient (object client)
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)
Expand All @@ -114,15 +129,67 @@ private void HandleClient (object client)

ASCIIEncoding encoding = new ASCIIEncoding ();
string received_message = encoding.GetString (message, 0, bytes_read);
string invite_xml = "";

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;
}

// 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 ("/"));
} 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 ();
Expand Down

0 comments on commit a8f523e

Please sign in to comment.