summaryrefslogtreecommitdiff
path: root/content/calendarAutoconfig.js
blob: 8a683c059e1c290df332b97f4742d66599e28a79 (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * (C) 2013 Guido Günther <agx@sigxcpu.org>
 */

Components.utils.import("resource://calendar/modules/calUtils.jsm");

caCalendarAutoconfig = function (aDocument, aWindow)
{
    this._document = aDocument;
    this._window = aWindow;

    /* The user as uri */
    this._uri = null;

    /* The calendar server */
    this._proto = null;
    this._host = null;
    this._path = null;
}

caCalendarAutoconfig.prototype = {
    parse_uri: function (aUri)
    {
        /* Check if it's a valid URI */
        try {
            var uri = cal.makeURL(aUri);
        } catch (ex) {
            var msg = "Invalid uri " + aUri;
            caError(msg + " " + ex);
            throw msg;
        }

        if (uri.scheme != 'email') {
            throw aUri + "not a email uri";
        }
        this._domain = aUri.split('@')[1];
    },

    /* Query DNS SRV and TXT records */
    query_dns: function() {
        var proto = 'tcp';
        var service = this.type + 's';
        /* Currently not possible using a xulrunner interface:
         * #14328, #545866, #735967 */
        var resolver = new caDNSResolver();

        caDebug("Checking SRV records for " + [this._domain,
                                               proto,
                                               service,
                                              ].join(" "));
        var records = resolver.get_srv_records(service,
                                               proto,
                                               this._domain);
        caDebug("Got service records: " + records);
        if (records[0]) {
            this._host = records[0];
            this._proto = 'https://';
        }

        /*
         * FIXME: check TXT records for path
         * this._path = ...
         */
    },

    detect_calendar: function(aUri)
    {
        this.parse_uri(aUri);
        this._uri = aUri;
        this.query_dns();

        if (this._host == null) {
            this._host = this._domain;
        }
        if (this._path == null) {
            this._path = '/.well-known/' + this.type;
        }
        return this.check_calendar();
    },

    _register_calendar: function(aURL, calname) {
        caDebug("Registering " + calname + " calendar for " + aURL);
        var uri = cal.makeURL(aURL);
        var newCal = cal.getCalendarManager().createCalendar('ics', uri);
        newCal.name = calname;
        cal.getCalendarManager().registerCalendar(newCal);
    }
};

/* CalDAV */
function caCaldavAutoconfig(document, window) {
    /* FIXME: handle basic auth, parse dav properties after
     * DAV:current-user-principal
     * request.open("GET", url, true);
     * FIXME: use principal-URL to detect "home" collections
     * request.send(null);
     */
    this.type = 'caldav';
}
caCaldavAutoconfig.prototype = new caCalendarAutoconfig;
caCaldavAutoconfig.prototype.check_calendar = function() {
    return false;
}

function makeURI(aURL, aOriginCharset, aBaseURI) {
  var ioService = Components.classes["@mozilla.org/network/io-service;1"]
                  .getService(Components.interfaces.nsIIOService);
  return ioService.newURI(aURL, aOriginCharset, aBaseURI);
}

/* WebCal */
function caWebcalAutoconfig(document, window) {
    this.type = 'webcal';
}
caWebcalAutoconfig.prototype = new caCalendarAutoconfig;
caWebcalAutoconfig.prototype.check_calendar = function() {
    this._proto = this._proto || 'https://';

    var url = this._proto + this._host + this._path;
    var calName = this._uri;
    caDebug("Looking for calendar at " + url);

    var request = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"]
        .createInstance(Components.interfaces.nsIXMLHttpRequest);

    request.onload = function (e) {
        if (request.readyState === 4) {
            if (request.status === 200) {
                if (request.getResponseHeader('Content-Type') == 'text/calendar') {
                    this._register_calendar(url, calName);
                } else {
                    caDebug("Not a calendar: " + request.getResponseHeader('Content-Type'));
                    /* FIXME: Check collection for .ics files */
                }
            } else {
                caDebug(request.statusText);
            }
        }
    }.bind(this);

    request.onerror = function (e) {
        caDebug("Calendar detection failed " + e);
    };

    request.open("GET", url, true);
    request.send(null);
}


function do_calendarAutoconfig(aUri) {
    caDebug(aUri);

    caldav = new caCaldavAutoconfig(document, window);
    if (! caldav.detect_calendar(aUri)) {
        webcal = new caWebcalAutoconfig(document, window);
        webcal.detect_calendar(aUri);
    }
}