Listen for urls to an invite xml file on port 1986

This commit is contained in:
Hylke Bons 2011-12-02 12:22:35 +01:00
parent d17a891dd5
commit 2184419290
3 changed files with 86 additions and 8 deletions

View file

@ -347,6 +347,13 @@ namespace SparkleShare {
Buttons.Add (CancelButton);
Controller.CheckAddPage (
AddressTextField.StringValue,
PathTextField.StringValue,
TableView.SelectedRow
);
break;
}

View file

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

View file

@ -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 ();