Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Set the connection results after attempting to fetch data in GetServerSupport.
[xestiacalendar/.git] / source / objects / CalDAV / CalDAV.cpp
1 // CalDAV.cpp - CalDAV Connection Object.
2 //
3 // (c) 2016 Xestia Software Development.
4 //
5 // This file is part of Xestia Calendar.
6 //
7 // Xestia Address Book is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by the
9 // Free Software Foundation, version 3 of the license.
10 //
11 // Xestia Address Book is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with Xestia Calendar. If not, see <http://www.gnu.org/licenses/>
19 #include "CalDAV.h"
21 using namespace std;
23 size_t CalDAVReceive(char *ReceivedBuffer, size_t Size, size_t NewMemoryBytes, string *StringPointer)
24 {
25         
26         string ReceivedBufferString = "";
27         ReceivedBufferString.append(ReceivedBuffer, NewMemoryBytes);
28         
29         StringPointer->append(ReceivedBufferString);
30         
31         return Size * NewMemoryBytes;
32         
33 }
35 size_t CalDAVSend(char *SendBuffer, size_t Size, size_t NewMemoryBytes, void *DataStruct){
37         struct CalDAVSendData *UploadPtr = (struct CalDAVSendData *)DataStruct;
38         
39         if (UploadPtr->sizeleft){
41                 UploadPtr->sizeleft--;
42                 char CharSend;
44                 CharSend = (*UploadPtr->readptr)[UploadPtr->seek];
45                 
46                 *SendBuffer = CharSend;
47                 
48                 UploadPtr->seek++;
50                 return 1;
52         }
54         return 0;
56 }
58 CalDAV::CalDAV(){
60         // Setup the objects within the CalDAV connection 
61         // object.
62         
63         ConnectionHandle = curl_easy_init();
64         
65 }
67 CalDAV::~CalDAV(){
68         
69         // Destory the objects within the CalDAV connection
70         // object.
71         
72         curl_easy_cleanup(ConnectionHandle);
73         ConnectionHandle = nullptr;
74         
75 }
77 void CalDAV::SetupConnectionData(CalDAVConnectionData *ConnData){
78         
79         // Check if ConnData is a nullptr, return if it is.
80         
81         if (ConnData == nullptr){
82                 return;
83         }
84         
85         // Set the connection settings to the values from ConnData.
87         ConnectionData = (*ConnData);
88         
89 }
91 CalDAVStatus CalDAV::GetConnectionData(){
92         
93         // Get the current connection settings for the CalDAV
94         // connection object and return a CalDAVStatus object.
95         
96         CalDAVStatus ConnectionStatus;
97         
98         ConnectionStatus.Hostname = ConnectionData.Hostname;
99         ConnectionStatus.Port = ConnectionData.Port;
100         ConnectionStatus.Username = ConnectionData.Username;
101         ConnectionStatus.Prefix = ConnectionData.Prefix;
102         ConnectionStatus.UseSSL = ConnectionData.UseSSL;
103         ConnectionStatus.Timeout = ConnectionData.Timeout;
104         
105         return ConnectionStatus;
106         
109 CalDAVServerResult CalDAV::Connect(){
111         CalDAVServerResult ServerResult;
113         string ServerAddress = "";
114         string ServerUserPass = "";
116         // Setup the server address.
117         
118         ServerAddress = BuildServerAddress(&ConnectionData, "/principals/");
120         // Setup the server password.
121         
122         ServerUserPass += ConnectionData.Username;
123         ServerUserPass += ":";
124         ServerUserPass += ConnectionData.Password;
125         
126         curl_easy_setopt(ConnectionHandle, CURLOPT_URL, ServerAddress.c_str());
127         curl_easy_setopt(ConnectionHandle, CURLOPT_USERPWD, ServerUserPass.c_str());
128         curl_easy_setopt(ConnectionHandle, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
129         curl_easy_setopt(ConnectionHandle, CURLOPT_FAILONERROR, 1L);
130         curl_easy_setopt(ConnectionHandle, CURLOPT_TIMEOUT, ConnectionData.Timeout);
131         curl_easy_setopt(ConnectionHandle, CURLOPT_WRITEFUNCTION, CalDAVReceive);
132         curl_easy_setopt(ConnectionHandle, CURLOPT_WRITEDATA, &ServerData);
133         curl_easy_setopt(ConnectionHandle, CURLOPT_WRITEHEADER, &ServerHeader);
134         
135         // Connect to the CalDAV server.
136         
137         ServerResult.Code = curl_easy_perform(ConnectionHandle);
139         // Process the result received from the server.
140         
141         if (ServerResult.Code != CURLE_OK){
142                 
143                 ServerResult.Result = CALDAVQUERYRESULT_SERVERERROR;
144                 
145         } else {
146                 
147                 ServerResult.Result = CALDAVQUERYRESULT_OK;
148                 
149         }
150         
151         // Get the HTTP code.
152         
153         curl_easy_getinfo(ConnectionHandle, CURLINFO_RESPONSE_CODE, &ServerResult.HTTPCode);
154         
155         return ServerResult;
156         
159 CalDAVServerResult CalDAV::GetServerResult(){
160         
161         return ConnectionServerResult;
162         
165 CalDAVServerSupport CalDAV::GetServerSupport(){
167         CalDAVServerSupport ServerStatus;
168         
169         // Setup the server connection.
170         
171         curl_easy_setopt(ConnectionHandle, CURLOPT_CUSTOMREQUEST, "OPTIONS");
172         
173         CURLcode ServerResult = curl_easy_perform(ConnectionHandle);
174         
175         // Set the results.
176         
177         if (ServerResult == CURLE_OK){
178                 ConnectionServerResult.Result = CALDAVQUERYRESULT_OK;
179         } else {
180                 ConnectionServerResult.Result = CALDAVQUERYRESULT_SERVERERROR;          
181         }
182         ConnectionServerResult.Code = ServerResult;
183         curl_easy_getinfo(ConnectionHandle, CURLINFO_RESPONSE_CODE, &ConnectionServerResult.HTTPCode);
184         
185         if (ServerResult != CURLE_OK){
186                 return ServerStatus;
187         }
188         
189         // Check that the server header has data in,
190         // otherwise return an "empty" CalDAVServerSupport.
191         
192         if (ServerHeader.size() == 0){
193                 return ServerStatus;
194         }
195         
196         // Process each line looking for the first DAV header 
197         // line.
198         
199         bool NewlineMode = true;
200         
201         string DAVLine;
202         
203         for (int CharSeek = 0; CharSeek < ServerHeader.size(); CharSeek++){
204                 
205                 if (NewlineMode == true){
206                         
207                         // Check if we have reached the end of the string.
208                         
209                         if (CharSeek >= ServerHeader.size()){
210                                 
211                                 break;
212                                 
213                         }
214                         
215                         // Check the first four letters to make sure
216                         // they are 'DAV:'.
217                         
218                         string DAVHeaderCheck = "";
219                         
220                         try {
221                                 DAVHeaderCheck = ServerHeader.substr(CharSeek, 4);
222                         }
223                         
224                         catch (out_of_range &oor){
225                                 break;
226                         }
227                         
228                         if (DAVHeaderCheck == "DAV:"){
229                                 
230                                 CharSeek += 5;
231                                 
232                                 for (; CharSeek < ServerHeader.size(); CharSeek++){
233                                         
234                                         if (ServerHeader[CharSeek] == '\n'){
235                                         
236                                                 break;
237                                                 
238                                         }
239                                         
240                                         DAVLine.push_back(ServerHeader[CharSeek]);
241                                         
242                                 }
243                                 
244                                 break;
245                                 
246                         }
247                         
248                         NewlineMode = false;
249                         
250                 }
251                 
252                 if (ServerHeader[CharSeek] == '\n'){
253                         
254                         NewlineMode = true;
255                         
256                 }
257                 
258         }
259         
260         // Process the DAV line.
261         
262         vector<string> DAVLineData;
263         string DAVSegmentString;
264         
265         for (int CharSeek = 0; CharSeek < DAVLine.size(); CharSeek++){
266                 
267                 if (DAVLine[CharSeek] == ' '){
268                         continue;
269                 }
270                 
271                 if (DAVLine[CharSeek] == ','){
272                         
273                         DAVLineData.push_back(DAVSegmentString);
274                         DAVSegmentString.clear();
275                         continue;
276                         
277                 }
278                 
279                 DAVSegmentString += DAVLine[CharSeek];
280                 
281         }
282         
283         // Process the DAV values and set each value
284         // to true as required.
285         
286         for (int DAVItemSeek = 0; 
287                 DAVItemSeek < DAVLineData.size();
288                 DAVItemSeek++){
289                         
290                 if (DAVLineData.at(DAVItemSeek) == "calendar-access"){
291                         
292                         ServerStatus.BasicSupport = true;
293                 
294                 }
295                         
296         }
297         
298         return ServerStatus;
299         
302 bool CalDAVObjectValidSettings(CalDAVConnectionData *ConnData){
304         // Check if the passed CalDAV Connection Data is has
305         // an address set. Return false if nullptr is used.
307         if (ConnData == nullptr){
308         
309                 return false;
310         
311         }
312         
313         // Check the server hostname. Return false
314         // if no value has been set.
315         
316         if (ConnData->Hostname.size() == 0){
317         
318                 return false;
319         
320         }
321         
322         // Check the server port. Return false if
323         // no value has been set or the port number
324         // is less than 1 or higher than 65535.
325         
326         if (ConnData->Port < 1 || ConnData->Port > 65535){
327         
328                 return false;
329         
330         }
331         
332         // Check the server username. Return false
333         // if no value has been set.
334         
335         if (ConnData->Username.size() == 0){
336                 
337                 return false;
338                 
339         }       
340         
341         // Check the server password. Return false
342         // if no value has been set.
343         
344         if (ConnData->Password.size() == 0){
345                 
346                 return false;
347                 
348         }
350         // Cannot check UseSSL: It is either true
351         // or false.
352         
353         // Cannot check Prefix: The prefix may need
354         // to be worked out first.
356         // No errors were found whilst checking so
357         // return true.
358         
359         return true;
363 string BuildServerAddress(CalDAVConnectionData *ConnData, string URIAddress){
364         
365         string ServerAddress;
366         
367         // Setup the server address.
368         
369         if (ConnData->UseSSL == true){
370                 ServerAddress += "https://";
371         } else {
372                 ServerAddress += "http://";
373         }
374         
375         ServerAddress += ConnData->Hostname;
376         
377         // Check if server port is 80, otherwise
378         // specifiy the port number in the address.
379         
380         if (ConnData->Port != 80){
381                 ServerAddress += ":";
382                 ServerAddress += to_string(ConnData->Port);
383         }
384         
385         ServerAddress += URIAddress;
386         
387         return ServerAddress;
388         
Xestia Software Development
Yn Maystri
© 2006 - 2019 Xestia Software Development
Software

Xestia Address Book
Xestia Calendar
Development

Xestia Gelforn
Everything else

About
News
Privacy Policy