forked from jachwe/node-caldav
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
201 lines (170 loc) · 5.52 KB
/
index.js
File metadata and controls
201 lines (170 loc) · 5.52 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
"use strict";
var https = require("https");
var xmljs = require("libxmljs");
module.exports = {
/**
* Get a list of Folders/Calendars from a given url
*
* @param {String} url
* @param {String} user
* @param {String} pass
* @param {function} cb
*/
getList: function (url, user, pass, cb) {
var urlparts = /(https?)\:\/\/(.*?):?(\d*)?(\/.*\/?)/gi.exec(url);
var protocol = urlparts[1];
var host = urlparts[2];
var port = urlparts[3] || (protocol === "https" ? 443 : 80);
var path = urlparts[4];
var xml = '<?xml version="1.0" encoding="utf-8" ?>\n' +
' <D:propfind xmlns:D="DAV:" xmlns:C="http://calendarserver.org/ns/">\n' +
' <D:prop>\n' +
' <D:displayname />\n' +
' </D:prop>\n' +
' </D:propfind>';
var options = {
rejectUnauthorized: false,
hostname : host,
port : port,
path : path,
method : 'PROPFIND',
headers : {
"Content-type" : "text/xml",
"Content-Length": xml.length,
"User-Agent" : "calDavClient",
"Connection" : "close",
"Depth" : "1"
}
};
if (user && pass) {
var userpass = new Buffer(user + ":" + pass).toString('base64');
options.headers["Authorization"] = "Basic " + userpass;
}
var req = https.request(options, function (res) {
var s = "";
res.on('data', function (chunk) {
s += chunk;
});
req.on('close', function () {
var reslist = [];
try {
var xmlDoc = xmljs.parseXml(s);
// console.log(xmlDoc.toString() );
var resp = xmlDoc.find("a:response", { a: 'DAV:'});
for (var i in resp) {
var el = resp[i];
var href = el.get("a:href", { a: 'DAV:'});
var dspn = el.get("a:propstat/a:prop/a:displayname", { a: 'DAV:'});
if (dspn) {
var resobj = {};
resobj.displayName = dspn.text();
resobj.href = href.text();
reslist.push(resobj);
}
}
}
catch (e) {
console.log("Error parsing response")
}
cb(reslist);
});
});
req.end(xml);
req.on('error', function (e) {
console.log('problem with request: ' + e.message);
});
},
/**
* Get a list of Events from a given Calendarurl
*
* @param {String} url
* @param {String} user
* @param {String} pass
* @param {String} date from which to start like 20140101T120000Z
* @param {String} date from which to stop like 20140102T120000Z, optional (can be undefined)
* @param {function} cb
*/
getEvents: function (url, user, pass, start, end, cb) {
var urlparts = /(https?)\:\/\/(.*?):?(\d*)?(\/.*\/?)/gi.exec(url);
var protocol = urlparts[1];
var host = urlparts[2];
var port = urlparts[3] || (protocol === "https" ? 443 : 80);
var path = urlparts[4];
var endTimeRange = (end) ? ' end="'+end+'"' : "";
var xml = '<?xml version="1.0" encoding="utf-8" ?>\n' +
'<C:calendar-query xmlns:D="DAV:" xmlns:C="urn:ietf:params:xml:ns:caldav">\n' +
' <D:prop>\n' +
' <C:calendar-data/>\n' +
' </D:prop>\n' +
' <C:filter>\n' +
' <C:comp-filter name="VCALENDAR">\n' +
' <C:comp-filter name="VEVENT">\n' +
' <C:time-range start="'+start+'"'+endTimeRange+'/>\n' +
' </C:comp-filter>\n' +
' </C:comp-filter>\n' +
' </C:filter>\n' +
'</C:calendar-query>';
var options = {
rejectUnauthorized: false,
hostname : host,
port : port,
path : path,
method : 'REPORT',
headers : {
"Content-type" : "text/xml",
"Content-Length": xml.length,
"User-Agent" : "calDavClient",
"Connection" : "close",
"Depth" : "1"
}
};
if (user && pass) {
var userpass = new Buffer(user + ":" + pass).toString('base64');
options.headers["Authorization"] = "Basic " + userpass;
}
var req = https.request(options, function (res) {
var s = "";
res.on('data', function (chunk) {
s += chunk;
});
req.on('close', function () {
var reslist = [];
try {
var xmlDoc = xmljs.parseXml(s);
// console.log(xmlDoc.toString() );
var data = xmlDoc.find("a:response/a:propstat/a:prop/c:calendar-data", { a: 'DAV:', c: "urn:ietf:params:xml:ns:caldav" });
for (var i in data) {
var ics = data[i].text();
var evs = ics.match(/BEGIN:VEVENT[\s\S]*END:VEVENT/gi);
for (var x in evs) {
var evobj = {};
var evstr = evs[x];
evstr = evstr.split("\n");
for (var y in evstr) {
var evpropstr = evstr[y];
if (evpropstr.match(/BEGIN:|END:/gi)) {
continue;
}
var sp = evpropstr.split(":");
var key = sp[0];
var val = sp[1];
if (key && val) {
evobj[key] = val;
}
}
reslist.push(evobj)
}
}
cb(reslist);
}
catch (e) {
console.log("Error parsing response")
}
});
});
req.end(xml);
req.on('error', function (e) {
console.log('problem with request: ' + e.message);
});
}
};