Skip to content
Newer
Older
100644 143 lines (105 sloc) 4.76 KB
7f826a0 @hbons add invites tcp listener (untested)
hbons authored Dec 1, 2011
1 // SparkleShare, a collaboration and sharing tool.
2 // Copyright (C) 2010 Hylke Bons (hylkebons@gmail.com)
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see (http://www.gnu.org/licenses/).
16
17
18 using System;
c1d5492 @hbons invites: upload the user's pubkey on invite accept
hbons authored Feb 17, 2012
19 using System.IO;
7f826a0 @hbons add invites tcp listener (untested)
hbons authored Dec 1, 2011
20 using System.Net;
c1d5492 @hbons invites: upload the user's pubkey on invite accept
hbons authored Feb 17, 2012
21 using System.Text;
a8f523e @hbons Listen for urls to an invite xml file on port 1986
hbons authored Dec 2, 2011
22 using System.Xml;
7f826a0 @hbons add invites tcp listener (untested)
hbons authored Dec 1, 2011
23
a8f523e @hbons Listen for urls to an invite xml file on port 1986
hbons authored Dec 2, 2011
24 using SparkleLib;
25
7f826a0 @hbons add invites tcp listener (untested)
hbons authored Dec 1, 2011
26 namespace SparkleShare {
27
28 public class SparkleInvite {
29
4429893 @hbons Finish support for invite.xml files dropped in ~/SparkleShare
hbons authored Feb 17, 2012
30 public string Address { get; private set; }
31 public string RemotePath { get; private set; }
390d1ba @hbons linux: Implement handling of sparkleshare:// links
hbons authored Feb 27, 2012
32 public string AcceptUrl { get; private set; }
33 public string AnnouncementsUrl { get; private set; }
4429893 @hbons Finish support for invite.xml files dropped in ~/SparkleShare
hbons authored Feb 16, 2012
34
91d62ac @hbons Allow invites and plugins to set a custom <announcements_url>. Closes #…
hbons authored Feb 24, 2012
35
36 public bool IsValid {
4429893 @hbons Finish support for invite.xml files dropped in ~/SparkleShare
hbons authored Feb 16, 2012
37 get {
38 return (!string.IsNullOrEmpty (Address) &&
6ee3a8d @hbons Allow invites without a accept_url element
hbons authored Feb 19, 2012
39 !string.IsNullOrEmpty (RemotePath));
4429893 @hbons Finish support for invite.xml files dropped in ~/SparkleShare
hbons authored Feb 16, 2012
40 }
41 }
7f826a0 @hbons add invites tcp listener (untested)
hbons authored Dec 1, 2011
42
43
91d62ac @hbons Allow invites and plugins to set a custom <announcements_url>. Closes #…
hbons authored Feb 24, 2012
44 public SparkleInvite (string address, string remote_path,
45 string accept_url)
7f826a0 @hbons add invites tcp listener (untested)
hbons authored Dec 1, 2011
46 {
91d62ac @hbons Allow invites and plugins to set a custom <announcements_url>. Closes #…
hbons authored Feb 24, 2012
47 Initialize (address, remote_path, accept_url, null);
48 }
49
50
51 public SparkleInvite (string address, string remote_path,
52 string accept_url, string announcements_url)
53 {
54 Initialize (address, remote_path, accept_url, announcements_url);
7f826a0 @hbons add invites tcp listener (untested)
hbons authored Dec 1, 2011
55 }
a8f523e @hbons Listen for urls to an invite xml file on port 1986
hbons authored Dec 2, 2011
56
57
58 public SparkleInvite (string xml_file_path)
59 {
cded663 @hbons invite: cleanup
hbons authored Feb 11, 2012
60 XmlDocument xml_document = new XmlDocument ();
61 XmlNode node;
62
91d62ac @hbons Allow invites and plugins to set a custom <announcements_url>. Closes #…
hbons authored Feb 24, 2012
63 string address = "";
64 string remote_path = "";
65 string accept_url = "";
66 string announcements_url = "";
cded663 @hbons invite: cleanup
hbons authored Feb 11, 2012
67
68 try {
69 xml_document.Load (xml_file_path);
70
390d1ba @hbons linux: Implement handling of sparkleshare:// links
hbons authored Feb 27, 2012
71
87111a8 @hbons Refactor the invite system, warn if cloning's taking place already
hbons authored Feb 12, 2012
72 node = xml_document.SelectSingleNode ("/sparkleshare/invite/address/text()");
73 if (node != null) { address = node.Value; }
cded663 @hbons invite: cleanup
hbons authored Feb 11, 2012
74
87111a8 @hbons Refactor the invite system, warn if cloning's taking place already
hbons authored Feb 12, 2012
75 node = xml_document.SelectSingleNode ("/sparkleshare/invite/remote_path/text()");
76 if (node != null) { remote_path = node.Value; }
cded663 @hbons invite: cleanup
hbons authored Feb 11, 2012
77
87111a8 @hbons Refactor the invite system, warn if cloning's taking place already
hbons authored Feb 12, 2012
78 node = xml_document.SelectSingleNode ("/sparkleshare/invite/accept_url/text()");
79 if (node != null) { accept_url = node.Value; }
cded663 @hbons invite: cleanup
hbons authored Feb 11, 2012
80
91d62ac @hbons Allow invites and plugins to set a custom <announcements_url>. Closes #…
hbons authored Feb 24, 2012
81 node = xml_document.SelectSingleNode ("/sparkleshare/invite/announcements_url/text()");
82 if (node != null) { announcements_url = node.Value; }
83
390d1ba @hbons linux: Implement handling of sparkleshare:// links
hbons authored Feb 27, 2012
84
91d62ac @hbons Allow invites and plugins to set a custom <announcements_url>. Closes #…
hbons authored Feb 24, 2012
85 Initialize (address, remote_path, accept_url, announcements_url);
4429893 @hbons Finish support for invite.xml files dropped in ~/SparkleShare
hbons authored Feb 16, 2012
86
cded663 @hbons invite: cleanup
hbons authored Feb 11, 2012
87 } catch (XmlException e) {
88 SparkleHelpers.DebugInfo ("Invite", "Invalid XML: " + e.Message);
89 return;
90 }
a8f523e @hbons Listen for urls to an invite xml file on port 1986
hbons authored Dec 2, 2011
91 }
7f826a0 @hbons add invites tcp listener (untested)
hbons authored Dec 1, 2011
92
93
87111a8 @hbons Refactor the invite system, warn if cloning's taking place already
hbons authored Feb 12, 2012
94 public bool Accept ()
7f826a0 @hbons add invites tcp listener (untested)
hbons authored Dec 1, 2011
95 {
390d1ba @hbons linux: Implement handling of sparkleshare:// links
hbons authored Feb 27, 2012
96 if (string.IsNullOrEmpty (AcceptUrl))
6ee3a8d @hbons Allow invites without a accept_url element
hbons authored Feb 19, 2012
97 return true;
98
87111a8 @hbons Refactor the invite system, warn if cloning's taking place already
hbons authored Feb 12, 2012
99 try {
c1d5492 @hbons invites: upload the user's pubkey on invite accept
hbons authored Feb 17, 2012
100 WebRequest request = WebRequest.Create (AcceptUrl);
101
102 request.Method = "POST";
103 request.ContentType = "application/x-www-form-urlencoded";
104 string post_data = "pubkey=" + SparkleConfig.DefaultConfig.User.PublicKey;
105 byte [] post_bytes = Encoding.UTF8.GetBytes (post_data);
106 request.ContentLength = post_bytes.Length;
107
108 Stream data_stream = request.GetRequestStream ();
109 data_stream.Write (post_bytes, 0, post_bytes.Length);
110 data_stream.Close ();
111
112 WebResponse response = request.GetResponse ();
113 response.Close ();
114
115
116 if ((response as HttpWebResponse).StatusCode == HttpStatusCode.OK) {
117 SparkleHelpers.DebugInfo ("Invite", "Uploaded public key to " + AcceptUrl);
118 return true;
7f826a0 @hbons add invites tcp listener (untested)
hbons authored Dec 1, 2011
119
c1d5492 @hbons invites: upload the user's pubkey on invite accept
hbons authored Feb 17, 2012
120 } else {
121 SparkleHelpers.DebugInfo ("Invite", "Failed uploading public key to " + AcceptUrl);
122 return false;
123 }
a8f523e @hbons Listen for urls to an invite xml file on port 1986
hbons authored Dec 2, 2011
124
87111a8 @hbons Refactor the invite system, warn if cloning's taking place already
hbons authored Feb 12, 2012
125 } catch (WebException e) {
c1d5492 @hbons invites: upload the user's pubkey on invite accept
hbons authored Feb 17, 2012
126 SparkleHelpers.DebugInfo ("Invite", "Failed uploading public key to " + AcceptUrl + ": " + e.Message);
7f826a0 @hbons add invites tcp listener (untested)
hbons authored Dec 1, 2011
127
87111a8 @hbons Refactor the invite system, warn if cloning's taking place already
hbons authored Feb 12, 2012
128 return false;
7f826a0 @hbons add invites tcp listener (untested)
hbons authored Dec 1, 2011
129 }
130 }
4429893 @hbons Finish support for invite.xml files dropped in ~/SparkleShare
hbons authored Feb 16, 2012
131
132
91d62ac @hbons Allow invites and plugins to set a custom <announcements_url>. Closes #…
hbons authored Feb 24, 2012
133 private void Initialize (string address, string remote_path,
134 string accept_url, string announcements_url)
6ee3a8d @hbons Allow invites without a accept_url element
hbons authored Feb 19, 2012
135 {
91d62ac @hbons Allow invites and plugins to set a custom <announcements_url>. Closes #…
hbons authored Feb 24, 2012
136 Address = address;
137 RemotePath = remote_path;
390d1ba @hbons linux: Implement handling of sparkleshare:// links
hbons authored Feb 27, 2012
138 AcceptUrl = accept_url;
139 AnnouncementsUrl = announcements_url;
4429893 @hbons Finish support for invite.xml files dropped in ~/SparkleShare
hbons authored Feb 16, 2012
140 }
7f826a0 @hbons add invites tcp listener (untested)
hbons authored Dec 1, 2011
141 }
142 }
Something went wrong with that request. Please try again.