summaryrefslogtreecommitdiff
path: root/Sparkles/Invite.cs
blob: 364f56d366fd48d4e90491d16e63c91b5d7d067b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//   SparkleShare, a collaboration and sharing tool.
//   Copyright (C) 2010  Hylke Bons (hi@planetpeanut.uk)
//
//   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;
using System.IO;
using System.Net;
using System.Text;
using System.Xml;

namespace Sparkles {

    public class SparkleInvite : XmlDocument {

        public string Address { get; private set; }
        public string RemotePath { get; private set; }
        public string Fingerprint { get; private set; }
        public string AcceptUrl { get; private set; }
        public string AnnouncementsUrl { get; private set; }

        public bool IsValid {
            get {
                return (!string.IsNullOrEmpty (Address) && !string.IsNullOrEmpty (RemotePath));
            }
        }


        public SparkleInvite (string xml_file_path)
        {
            try {
                Load (xml_file_path);

            } catch (XmlException e) {
                Logger.LogInfo ("Invite", "Error parsing XML", e);
                return;
            }

            Address          = ReadField ("address");
            RemotePath       = ReadField ("remote_path");
            AcceptUrl        = ReadField ("accept_url");
            AnnouncementsUrl = ReadField ("announcements_url");
            Fingerprint      = ReadField ("fingerprint");
        }


        public bool Accept (string public_key)
        {
            #if __MonoCS__
            ServicePointManager.ServerCertificateValidationCallback = delegate {
                return true;
            };
            #endif

            if (string.IsNullOrEmpty (AcceptUrl))
                return true;

            string post_data   = "public_key=" + Uri.EscapeDataString (public_key);
            byte [] post_bytes = Encoding.UTF8.GetBytes (post_data);

            WebRequest request    = WebRequest.Create (AcceptUrl);
            request.Method        = "POST";
            request.ContentType   = "application/x-www-form-urlencoded";
            request.ContentLength = post_bytes.Length;

            Stream data_stream = request.GetRequestStream ();
            data_stream.Write (post_bytes, 0, post_bytes.Length);
            data_stream.Close ();

            HttpWebResponse response = null;

            try {
                response = (HttpWebResponse) request.GetResponse ();
                response.Close ();

            } catch (WebException e) {
                Logger.LogInfo ("Invite", "Failed uploading public key to " + AcceptUrl + "", e);
                return false;
            }

            if (response != null && response.StatusCode == HttpStatusCode.OK) {
                Logger.LogInfo ("Invite", "Uploaded public key to " + AcceptUrl);
                return true;
            }

            return false;
        }


        string ReadField (string name)
        {
            try {
                XmlNode node = SelectSingleNode ("/sparkleshare/invite/" + name + "/text()");

                if (node != null)
                    return node.Value;

                return "";

            } catch (XmlException e) {
                Logger.LogInfo ("Invite", "Error reading field '" + name + "'", e);
                return "";
            }
        }
    }
}