2011-12-01 20:16:57 +00:00
|
|
|
// SparkleShare, a collaboration and sharing tool.
|
|
|
|
// Copyright (C) 2010 Hylke Bons (hylkebons@gmail.com)
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see (http://www.gnu.org/licenses/).
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
2012-02-17 20:26:13 +00:00
|
|
|
using System.IO;
|
2011-12-01 20:16:57 +00:00
|
|
|
using System.Net;
|
2012-02-17 20:26:13 +00:00
|
|
|
using System.Text;
|
2011-12-02 11:22:35 +00:00
|
|
|
using System.Xml;
|
2011-12-01 20:16:57 +00:00
|
|
|
|
2011-12-02 11:22:35 +00:00
|
|
|
using SparkleLib;
|
|
|
|
|
2011-12-01 20:16:57 +00:00
|
|
|
namespace SparkleShare {
|
|
|
|
|
|
|
|
public class SparkleInvite {
|
|
|
|
|
2012-02-16 23:51:44 +00:00
|
|
|
public string Address { get; private set; }
|
|
|
|
public string RemotePath { get; private set; }
|
2012-02-27 02:01:41 +00:00
|
|
|
public string AcceptUrl { get; private set; }
|
|
|
|
public string AnnouncementsUrl { get; private set; }
|
2012-02-16 23:51:44 +00:00
|
|
|
|
2012-02-24 17:50:05 +00:00
|
|
|
|
|
|
|
public bool IsValid {
|
2012-02-16 23:51:44 +00:00
|
|
|
get {
|
|
|
|
return (!string.IsNullOrEmpty (Address) &&
|
2012-02-19 00:16:44 +00:00
|
|
|
!string.IsNullOrEmpty (RemotePath));
|
2012-02-16 23:51:44 +00:00
|
|
|
}
|
|
|
|
}
|
2011-12-01 20:16:57 +00:00
|
|
|
|
|
|
|
|
2012-02-24 17:50:05 +00:00
|
|
|
public SparkleInvite (string address, string remote_path,
|
|
|
|
string accept_url)
|
2011-12-01 20:16:57 +00:00
|
|
|
{
|
2012-02-24 17:50:05 +00:00
|
|
|
Initialize (address, remote_path, accept_url, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public SparkleInvite (string address, string remote_path,
|
|
|
|
string accept_url, string announcements_url)
|
|
|
|
{
|
|
|
|
Initialize (address, remote_path, accept_url, announcements_url);
|
2011-12-01 20:16:57 +00:00
|
|
|
}
|
2011-12-02 11:22:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
public SparkleInvite (string xml_file_path)
|
|
|
|
{
|
2012-02-11 18:52:49 +00:00
|
|
|
XmlDocument xml_document = new XmlDocument ();
|
|
|
|
XmlNode node;
|
|
|
|
|
2012-02-24 17:50:05 +00:00
|
|
|
string address = "";
|
|
|
|
string remote_path = "";
|
|
|
|
string accept_url = "";
|
|
|
|
string announcements_url = "";
|
2012-02-11 18:52:49 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
xml_document.Load (xml_file_path);
|
|
|
|
|
2012-02-27 02:01:41 +00:00
|
|
|
|
2012-02-12 21:25:20 +00:00
|
|
|
node = xml_document.SelectSingleNode ("/sparkleshare/invite/address/text()");
|
|
|
|
if (node != null) { address = node.Value; }
|
2012-02-11 18:52:49 +00:00
|
|
|
|
2012-02-12 21:25:20 +00:00
|
|
|
node = xml_document.SelectSingleNode ("/sparkleshare/invite/remote_path/text()");
|
|
|
|
if (node != null) { remote_path = node.Value; }
|
2012-02-11 18:52:49 +00:00
|
|
|
|
2012-02-12 21:25:20 +00:00
|
|
|
node = xml_document.SelectSingleNode ("/sparkleshare/invite/accept_url/text()");
|
|
|
|
if (node != null) { accept_url = node.Value; }
|
2012-02-11 18:52:49 +00:00
|
|
|
|
2012-02-24 17:50:05 +00:00
|
|
|
node = xml_document.SelectSingleNode ("/sparkleshare/invite/announcements_url/text()");
|
|
|
|
if (node != null) { announcements_url = node.Value; }
|
|
|
|
|
2012-02-27 02:01:41 +00:00
|
|
|
|
2012-02-24 17:50:05 +00:00
|
|
|
Initialize (address, remote_path, accept_url, announcements_url);
|
2012-02-16 23:51:44 +00:00
|
|
|
|
2012-02-11 18:52:49 +00:00
|
|
|
} catch (XmlException e) {
|
|
|
|
SparkleHelpers.DebugInfo ("Invite", "Invalid XML: " + e.Message);
|
|
|
|
return;
|
|
|
|
}
|
2011-12-02 11:22:35 +00:00
|
|
|
}
|
2011-12-01 20:16:57 +00:00
|
|
|
|
|
|
|
|
2012-02-12 21:25:20 +00:00
|
|
|
public bool Accept ()
|
2011-12-01 20:16:57 +00:00
|
|
|
{
|
2012-02-27 02:01:41 +00:00
|
|
|
if (string.IsNullOrEmpty (AcceptUrl))
|
2012-02-19 00:16:44 +00:00
|
|
|
return true;
|
|
|
|
|
2012-02-12 21:25:20 +00:00
|
|
|
try {
|
2012-02-17 20:26:13 +00:00
|
|
|
WebRequest request = WebRequest.Create (AcceptUrl);
|
|
|
|
|
|
|
|
request.Method = "POST";
|
|
|
|
request.ContentType = "application/x-www-form-urlencoded";
|
|
|
|
string post_data = "pubkey=" + SparkleConfig.DefaultConfig.User.PublicKey;
|
|
|
|
byte [] post_bytes = Encoding.UTF8.GetBytes (post_data);
|
|
|
|
request.ContentLength = post_bytes.Length;
|
|
|
|
|
|
|
|
Stream data_stream = request.GetRequestStream ();
|
|
|
|
data_stream.Write (post_bytes, 0, post_bytes.Length);
|
|
|
|
data_stream.Close ();
|
|
|
|
|
|
|
|
WebResponse response = request.GetResponse ();
|
|
|
|
response.Close ();
|
|
|
|
|
|
|
|
|
|
|
|
if ((response as HttpWebResponse).StatusCode == HttpStatusCode.OK) {
|
|
|
|
SparkleHelpers.DebugInfo ("Invite", "Uploaded public key to " + AcceptUrl);
|
|
|
|
return true;
|
2011-12-01 20:16:57 +00:00
|
|
|
|
2012-02-17 20:26:13 +00:00
|
|
|
} else {
|
|
|
|
SparkleHelpers.DebugInfo ("Invite", "Failed uploading public key to " + AcceptUrl);
|
|
|
|
return false;
|
|
|
|
}
|
2011-12-02 11:22:35 +00:00
|
|
|
|
2012-02-12 21:25:20 +00:00
|
|
|
} catch (WebException e) {
|
2012-02-17 20:26:13 +00:00
|
|
|
SparkleHelpers.DebugInfo ("Invite", "Failed uploading public key to " + AcceptUrl + ": " + e.Message);
|
2011-12-01 20:16:57 +00:00
|
|
|
|
2012-02-12 21:25:20 +00:00
|
|
|
return false;
|
2011-12-01 20:16:57 +00:00
|
|
|
}
|
|
|
|
}
|
2012-02-16 23:51:44 +00:00
|
|
|
|
|
|
|
|
2012-02-24 17:50:05 +00:00
|
|
|
private void Initialize (string address, string remote_path,
|
|
|
|
string accept_url, string announcements_url)
|
2012-02-19 00:16:44 +00:00
|
|
|
{
|
2012-02-24 17:50:05 +00:00
|
|
|
Address = address;
|
|
|
|
RemotePath = remote_path;
|
2012-02-27 02:01:41 +00:00
|
|
|
AcceptUrl = accept_url;
|
|
|
|
AnnouncementsUrl = announcements_url;
|
2012-02-16 23:51:44 +00:00
|
|
|
}
|
2011-12-01 20:16:57 +00:00
|
|
|
}
|
|
|
|
}
|