1 // CalDAV.cpp - CalDAV Connection Object.
3 // (c) 2016 Xestia Software Development.
5 // This file is part of Xestia Calendar.
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.
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.
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/>
23 size_t CalDAVReceive(char *ReceivedBuffer, size_t Size, size_t NewMemoryBytes, string *StringPointer)
26 string ReceivedBufferString = "";
27 ReceivedBufferString.append(ReceivedBuffer, NewMemoryBytes);
29 StringPointer->append(ReceivedBufferString);
31 return Size * NewMemoryBytes;
35 size_t CalDAVSend(char *SendBuffer, size_t Size, size_t NewMemoryBytes, void *DataStruct){
37 struct CalDAVSendData *UploadPtr = (struct CalDAVSendData *)DataStruct;
39 if (UploadPtr->sizeleft){
41 UploadPtr->sizeleft--;
44 CharSend = (*UploadPtr->readptr)[UploadPtr->seek];
46 *SendBuffer = CharSend;
60 // Setup the objects within the CalDAV connection
63 ConnectionHandle = curl_easy_init();
69 // Destory the objects within the CalDAV connection
72 curl_easy_cleanup(ConnectionHandle);
73 ConnectionHandle = nullptr;
77 void CalDAV::SetupConnectionData(CalDAVConnectionData *ConnData){
79 // Check if ConnData is a nullptr, return if it is.
81 if (ConnData == nullptr){
85 // Set the connection settings to the values from ConnData.
87 ConnectionData = (*ConnData);
91 CalDAVStatus CalDAV::GetConnectionData(){
93 // Get the current connection settings for the CalDAV
94 // connection object and return a CalDAVStatus object.
96 CalDAVStatus ConnectionStatus;
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;
105 return ConnectionStatus;
109 CalDAVServerResult CalDAV::Connect(){
111 CalDAVServerResult ServerResult;
113 string ServerAddress = "";
114 string ServerUserPass = "";
116 // Setup the server address.
118 ServerAddress = BuildServerAddress(&ConnectionData, "/principals/");
120 // Setup the server password.
122 ServerUserPass += ConnectionData.Username;
123 ServerUserPass += ":";
124 ServerUserPass += ConnectionData.Password;
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, CalDAVOutput);
132 curl_easy_setopt(ConnectionHandle, CURLOPT_WRITEDATA, &ServerData);
133 curl_easy_setopt(ConnectionHandle, CURLOPT_WRITEHEADER, &ServerHeader);
135 // Connect to the CalDAV server.
137 ServerResult.Code = curl_easy_perform(ConnectionHandle);
139 // Process the result received from the server.
141 if (ServerResult.Code != CURLE_OK){
143 ServerResult.Result = CALDAVQUERYRESULT_SERVERERROR;
147 ServerResult.Result = CALDAVQUERYRESULT_OK;
151 // Get the HTTP code.
153 curl_easy_getinfo(ConnectionHandle, CURLINFO_RESPONSE_CODE, &ServerResult.HTTPCode);
159 CalDAVServerSupport CalDAV::GetServerSupport(){
161 CalDAVServerSupport ServerStatus;
163 // Setup the server connection.
165 curl_easy_setopt(ConnectionHandle, CURLOPT_CUSTOMREQUEST, "OPTIONS");
167 CURLcode ServerResult = curl_easy_perform(ConnectionHandle);
169 if (ServerResult != CURLE_OK){
173 // Check that the server header has data in,
174 // otherwise return an "empty" CalDAVServerSupport.
176 if (ServerHeader.size() == 0){
180 // Process each line looking for the first DAV header
183 bool NewlineMode = true;
187 for (int CharSeek = 0; CharSeek < ServerHeader.size(); CharSeek++){
189 if (NewlineMode == true){
191 // Check if we have reached the end of the string.
193 if (CharSeek >= ServerHeader.size()){
199 // Check the first four letters to make sure
202 string DAVHeaderCheck = "";
205 DAVHeaderCheck = ServerHeader.substr(CharSeek, 4);
208 catch (out_of_range &oor){
212 if (DAVHeaderCheck == "DAV:"){
216 for (; CharSeek < ServerHeader.size(); CharSeek++){
218 if (ServerHeader[CharSeek] == '\n'){
224 DAVLine.push_back(ServerHeader[CharSeek]);
236 if (ServerHeader[CharSeek] == '\n'){
244 // Process the DAV line.
246 vector<string> DAVLineData;
247 string DAVSegmentString;
249 for (int CharSeek = 0; CharSeek < DAVLine.size(); CharSeek++){
251 if (DAVLine[CharSeek] == ' '){
255 if (DAVLine[CharSeek] == ','){
257 DAVLineData.push_back(DAVSegmentString);
258 DAVSegmentString.clear();
263 DAVSegmentString += DAVLine[CharSeek];
267 // Process the DAV values and set each value
268 // to true as required.
270 for (int DAVItemSeek = 0;
271 DAVItemSeek < DAVLineData.size();
274 if (DAVLineData.at(DAVItemSeek) == "calendar-access"){
276 ServerStatus.BasicSupport = true;
286 bool CalDAVObjectValidSettings(CalDAVConnectionData *ConnData){
288 // Check if the passed CalDAV Connection Data is has
289 // an address set. Return false if nullptr is used.
291 if (ConnData == nullptr){
297 // Check the server hostname. Return false
298 // if no value has been set.
300 if (ConnData->Hostname.size() == 0){
306 // Check the server port. Return false if
307 // no value has been set or the port number
308 // is less than 1 or higher than 65535.
310 if (ConnData->Port < 1 || ConnData->Port > 65535){
316 // Check the server username. Return false
317 // if no value has been set.
319 if (ConnData->Username.size() == 0){
325 // Check the server password. Return false
326 // if no value has been set.
328 if (ConnData->Password.size() == 0){
334 // Cannot check UseSSL: It is either true
337 // Cannot check Prefix: The prefix may need
338 // to be worked out first.
340 // No errors were found whilst checking so
347 string BuildServerAddress(CalDAVConnectionData *ConnData, string URIAddress){
349 string ServerAddress;
351 // Setup the server address.
353 if (ConnData->UseSSL == true){
354 ServerAddress += "https://";
356 ServerAddress += "http://";
359 ServerAddress += ConnData->Hostname;
361 // Check if server port is 80, otherwise
362 // specifiy the port number in the address.
364 if (ConnData->Port != 80){
365 ServerAddress += ":";
366 ServerAddress += to_string(ConnData->Port);
369 ServerAddress += URIAddress;
371 return ServerAddress;