Listen for urls to an invite xml file on port 1986
This commit is contained in:
parent
d17a891dd5
commit
2184419290
|
@ -347,6 +347,13 @@ namespace SparkleShare {
|
||||||
|
|
||||||
Buttons.Add (CancelButton);
|
Buttons.Add (CancelButton);
|
||||||
|
|
||||||
|
Controller.CheckAddPage (
|
||||||
|
AddressTextField.StringValue,
|
||||||
|
PathTextField.StringValue,
|
||||||
|
TableView.SelectedRow
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -139,6 +139,10 @@ namespace SparkleShare {
|
||||||
SparkleInviteListener invite_listener = new SparkleInviteListener (1986);
|
SparkleInviteListener invite_listener = new SparkleInviteListener (1986);
|
||||||
|
|
||||||
invite_listener.InviteReceived += delegate (SparkleInvite invite) {
|
invite_listener.InviteReceived += delegate (SparkleInvite invite) {
|
||||||
|
|
||||||
|
Console.WriteLine (invite.Host + " " + invite.Path + " " + invite.Token);
|
||||||
|
return;
|
||||||
|
|
||||||
if (OnInvite != null && !FirstRun)
|
if (OnInvite != null && !FirstRun)
|
||||||
OnInvite (invite);
|
OnInvite (invite);
|
||||||
};
|
};
|
||||||
|
|
|
@ -20,8 +20,11 @@ using System.IO;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Sockets;
|
using System.Net.Sockets;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Xml;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
|
||||||
|
using SparkleLib;
|
||||||
|
|
||||||
namespace SparkleShare {
|
namespace SparkleShare {
|
||||||
|
|
||||||
public class SparkleInvite {
|
public class SparkleInvite {
|
||||||
|
@ -44,9 +47,21 @@ namespace SparkleShare {
|
||||||
|
|
||||||
public SparkleInvite (string host, string path, string token)
|
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;
|
Token = token;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public SparkleInvite (string xml_file_path)
|
||||||
|
{
|
||||||
|
// TODO
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -93,7 +108,7 @@ namespace SparkleShare {
|
||||||
TcpClient tcp_client = (TcpClient) client;
|
TcpClient tcp_client = (TcpClient) client;
|
||||||
NetworkStream client_stream = tcp_client.GetStream ();
|
NetworkStream client_stream = tcp_client.GetStream ();
|
||||||
|
|
||||||
byte [] message = new byte[4096];
|
byte [] message = new byte [4096];
|
||||||
int bytes_read;
|
int bytes_read;
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
|
@ -114,15 +129,67 @@ namespace SparkleShare {
|
||||||
|
|
||||||
ASCIIEncoding encoding = new ASCIIEncoding ();
|
ASCIIEncoding encoding = new ASCIIEncoding ();
|
||||||
string received_message = encoding.GetString (message, 0, bytes_read);
|
string received_message = encoding.GetString (message, 0, bytes_read);
|
||||||
|
string invite_xml = "";
|
||||||
|
|
||||||
// SparkleShare's protocol format looks like this:
|
if (received_message.StartsWith (Uri.UriSchemeHttp) ||
|
||||||
// sparkle://user@host.org/path/token
|
received_message.StartsWith (Uri.UriSchemeHttps)) {
|
||||||
Uri uri = new Uri (received_message);
|
|
||||||
string token = uri.AbsolutePath.Substring (uri.AbsolutePath.LastIndexOf ("/") + 1);
|
WebClient web_client = new WebClient ();
|
||||||
string path = uri.AbsolutePath.Substring (0, uri.AbsolutePath.LastIndexOf ("/"));
|
|
||||||
|
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)
|
if (InviteReceived != null)
|
||||||
InviteReceived (new SparkleInvite (uri.Host, path, token));
|
InviteReceived (new SparkleInvite (host, path, token));
|
||||||
}
|
}
|
||||||
|
|
||||||
tcp_client.Close ();
|
tcp_client.Close ();
|
||||||
|
|
Loading…
Reference in a new issue