Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
macOS: Implemented support for adding, editing and deleting a CalDAV account
[xestiacalendar/.git] / source / objects / CalDAV / CalDAV.cpp
index 25c322e..3da1e6d 100644 (file)
 
 using namespace std;
 
-size_t CalDAVReceive(char *receivedBuffer, size_t size, size_t newMemoryBytes, string *stringPointer)
+size_t CalDAV::CalDAVReceive(char *receivedBuffer, size_t size, size_t newMemoryBytes, void *stream)
 {
        
-       stringPointer->append(receivedBuffer, newMemoryBytes);
-       
+       // Writeback function for the CardDAV object.
+
+       CalDAVPassObject *data = static_cast<CalDAVPassObject*>(stream);
+       data->DataSetting->append(receivedBuffer);
+
+       // Get the SSL engine pointer and trust if required on certain operating systems.
+
+       if (data->ServerUsingSSL == true) {
+
+#if defined(__APPLE__)
+
+               const struct curl_tlssessioninfo *TLSInfo;
+               CURLcode TLSCode;
+               TLSCode = curl_easy_getinfo(data->ConnectionSessionObject, CURLINFO_TLS_SSL_PTR, &TLSInfo);
+
+               SecTrustRef CertificateData;
+
+               if (TLSInfo->internals != nullptr && TLSCode == CURLE_OK) {
+                       SSLCopyPeerTrust((SSLContext*)TLSInfo->internals, &CertificateData);
+                       data->SSLContext = CertificateData;
+               }
+
+#elif defined(__WIN32__)
+
+               const struct curl_tlssessioninfo *TLSInfo;
+               CURLcode TLSCode;
+               TLSCode = curl_easy_getinfo(data->ConnectionSessionObject, CURLINFO_TLS_SSL_PTR, &TLSInfo);
+
+               if (TLSInfo->internals != nullptr && TLSCode == CURLE_OK) {
+
+                       // Free the previous certificate data.
+
+                       //CertFreeCertificateContext(CertificateData);
+
+                       PCCERT_CONTEXT CertificateData;
+
+                       PCtxtHandle SSLHandle = (PCtxtHandle)TLSInfo->internals;
+                       SECURITY_STATUS GetData = QueryContextAttributes(SSLHandle, SECPKG_ATTR_REMOTE_CERT_CONTEXT, &CertificateData);
+
+                       data->SSLContext = CertificateData;
+
+               }
+
+#endif
+
+       }
+
        return size * newMemoryBytes;
        
 }
@@ -103,7 +148,10 @@ CalDAVStatus CalDAV::GetConnectionData(){
        
 }
 
-CalDAVServerResult CalDAV::Connect(){
+CalDAVServerResult CalDAV::Connect(bool doAuthentication){
+
+       connectionData.useSSL ? SetupDefaultParametersSSL(doAuthentication) : SetupDefaultParametersNonSSL(doAuthentication);
+       ResetResults();
 
        CalDAVServerResult serverResult;
 
@@ -119,35 +167,102 @@ CalDAVServerResult CalDAV::Connect(){
        serverUserPass += connectionData.username;
        serverUserPass += ":";
        serverUserPass += connectionData.password;
-       
+
+       PageDataObject.CalDAVObject = this;
+       PageDataObject.ConnectionSessionObject = connectionHandle;
+       PageDataObject.DataSetting = &serverData;
+       PageDataObject.ServerUsingSSL = true;
+
+       PageHeaderObject.CalDAVObject = this;
+       PageHeaderObject.ConnectionSessionObject = connectionHandle;
+       PageHeaderObject.DataSetting = &serverHeader;
+       PageHeaderObject.ServerUsingSSL = true;
+
        curl_easy_setopt(connectionHandle, CURLOPT_URL, serverAddress.c_str());
        curl_easy_setopt(connectionHandle, CURLOPT_USERPWD, serverUserPass.c_str());
        curl_easy_setopt(connectionHandle, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
+       curl_easy_setopt(connectionHandle, CURLOPT_ERRORBUFFER, sessionErrorBuffer);
        curl_easy_setopt(connectionHandle, CURLOPT_FAILONERROR, 1L);
        curl_easy_setopt(connectionHandle, CURLOPT_TIMEOUT, connectionData.timeout);
-       curl_easy_setopt(connectionHandle, CURLOPT_WRITEFUNCTION, CalDAVReceive);
-       curl_easy_setopt(connectionHandle, CURLOPT_WRITEDATA, &serverData);
-       curl_easy_setopt(connectionHandle, CURLOPT_WRITEHEADER, &serverHeader);
+       curl_easy_setopt(connectionHandle, CURLOPT_WRITEFUNCTION, CalDAV::CalDAVReceive);
+       curl_easy_setopt(connectionHandle, CURLOPT_WRITEDATA, &PageDataObject);
+       curl_easy_setopt(connectionHandle, CURLOPT_WRITEHEADER, &PageHeaderObject);
        
        // Connect to the CalDAV server.
        
        serverResult.code = curl_easy_perform(connectionHandle);
 
        // Process the result received from the server.
-       
-       if (serverResult.code != CURLE_OK){
-               
-               serverResult.result = CALDAVQUERYRESULT_SERVERERROR;
-               
-       } else {
-               
-               serverResult.result = CALDAVQUERYRESULT_OK;
-               
-       }
-       
+
        // Get the HTTP code.
        
        curl_easy_getinfo(connectionHandle, CURLINFO_RESPONSE_CODE, &serverResult.httpCode);
+
+       switch(serverResult.code){
+               case CURLE_OK:
+                       authPassed = true;
+            if (connectionData.useSSL)
+            {
+                sslStatus = connectionData.useSSL;
+                sslVerified = COSSL_VERIFIED;
+            }
+            serverResult.result = CALDAVQUERYRESULT_OK;
+            break;
+               case CURLE_HTTP_RETURNED_ERROR:
+                       if (connectionData.useSSL)
+                       {
+                               sslStatus = connectionData.useSSL;
+                               sslVerified = COSSL_VERIFIED;
+                       }
+                       serverResult.result = CALDAVQUERYRESULT_OK;
+                       if (serverResult.httpCode == 401)
+                       {
+                               authPassed = false;
+                       }
+                       break;
+               case CURLE_SSL_CACERT:
+               case CURLE_SSL_CONNECT_ERROR:
+        case CURLE_PEER_FAILED_VERIFICATION:
+                       if (connectionData.useSSL)
+                       {
+                               sslStatus = connectionData.useSSL;
+                               sslVerified = COSSL_UNABLETOVERIFY;
+                       }
+                       serverResult.result = CALDAVQUERYRESULT_SSLFAILURE;
+                       authPassed = false;
+                       break;
+               default:
+                       serverResult.result = CALDAVQUERYRESULT_SERVERERROR;
+                       authPassed = false;
+                       break;
+       };
+
+       // Set the certificate data (if required).
+
+#if defined(__APPLE__)
+
+       if (connectionData.useSSL) {
+
+               certificateData = PageHeaderObject.SSLContext;
+
+       }
+
+#elif defined(__WIN32__)
+
+       if (connectionData.useSSL) {
+
+               certificateData = PageHeaderObject.SSLContext;
+
+       }
+
+#endif
+
+       // Check if a valid response was received before continuing.
+
+       if (serverResult.httpCode >= 200 && serverResult.httpCode <= 299)
+       {
+               validResponse = true;
+       }
        
        return serverResult;
        
@@ -328,6 +443,7 @@ string CalDAV::GetUserPrincipal(){
 
        curl_easy_setopt(connectionHandle, CURLOPT_CUSTOMREQUEST, "PROPFIND");
        curl_easy_setopt(connectionHandle, CURLOPT_UPLOAD, 1L);
+       curl_easy_setopt(connectionHandle, CURLOPT_ERRORBUFFER, sessionErrorBuffer);
        curl_easy_setopt(connectionHandle, CURLOPT_READDATA, &userPrincipalSendData);
        curl_easy_setopt(connectionHandle, CURLOPT_READFUNCTION, CalDAVSend);
        
@@ -361,6 +477,7 @@ string CalDAV::GetUserPrincipal(){
        // Reset the changed settings.
        
        curl_easy_setopt(connectionHandle, CURLOPT_UPLOAD, 0L);
+       curl_easy_setopt(connectionHandle, CURLOPT_ERRORBUFFER, sessionErrorBuffer);
        curl_easy_setopt(connectionHandle, CURLOPT_READDATA, NULL);
        curl_easy_setopt(connectionHandle, CURLOPT_READFUNCTION, NULL);
 
@@ -402,6 +519,7 @@ string CalDAV::GetCalendarHome(string userPrincipalURI){
        curl_easy_setopt(connectionHandle, CURLOPT_URL, calendarHomeURL.c_str());
        curl_easy_setopt(connectionHandle, CURLOPT_CUSTOMREQUEST, "PROPFIND");
        curl_easy_setopt(connectionHandle, CURLOPT_UPLOAD, 1L);
+       curl_easy_setopt(connectionHandle, CURLOPT_ERRORBUFFER, sessionErrorBuffer);
        curl_easy_setopt(connectionHandle, CURLOPT_READDATA, &calendarHomeSendData);
        curl_easy_setopt(connectionHandle, CURLOPT_READFUNCTION, CalDAVSend);
        
@@ -438,6 +556,7 @@ string CalDAV::GetCalendarHome(string userPrincipalURI){
        curl_easy_setopt(connectionHandle, CURLOPT_URL, originalServerAddress.c_str());
        
        curl_easy_setopt(connectionHandle, CURLOPT_UPLOAD, 0L);
+       curl_easy_setopt(connectionHandle, CURLOPT_ERRORBUFFER, sessionErrorBuffer);
        curl_easy_setopt(connectionHandle, CURLOPT_READDATA, NULL);
        curl_easy_setopt(connectionHandle, CURLOPT_READFUNCTION, NULL);
        curl_easy_setopt(connectionHandle, CURLOPT_HTTPHEADER, NULL);
@@ -497,6 +616,7 @@ CalDAVCalendarList CalDAV::GetCalendars(){
        curl_easy_setopt(connectionHandle, CURLOPT_URL, calendarListURLAddress.c_str());
        curl_easy_setopt(connectionHandle, CURLOPT_CUSTOMREQUEST, "PROPFIND");
        curl_easy_setopt(connectionHandle, CURLOPT_UPLOAD, 1L);
+       curl_easy_setopt(connectionHandle, CURLOPT_ERRORBUFFER, sessionErrorBuffer);
        curl_easy_setopt(connectionHandle, CURLOPT_READDATA, &calendarListSendData);
        curl_easy_setopt(connectionHandle, CURLOPT_READFUNCTION, CalDAVSend);
        
@@ -535,6 +655,7 @@ CalDAVCalendarList CalDAV::GetCalendars(){
        curl_easy_setopt(connectionHandle, CURLOPT_URL, originalServerAddress.c_str());
        curl_easy_setopt(connectionHandle, CURLOPT_CUSTOMREQUEST, NULL);        
        curl_easy_setopt(connectionHandle, CURLOPT_UPLOAD, 0L);
+       curl_easy_setopt(connectionHandle, CURLOPT_ERRORBUFFER, sessionErrorBuffer);
        curl_easy_setopt(connectionHandle, CURLOPT_READDATA, NULL);
        curl_easy_setopt(connectionHandle, CURLOPT_READFUNCTION, NULL);
        
@@ -604,6 +725,7 @@ CalDAVEntryList CalDAV::GetEntryList(string *calendarHREF){
        curl_easy_setopt(connectionHandle, CURLOPT_URL, entryListURLAddress.c_str());
        curl_easy_setopt(connectionHandle, CURLOPT_CUSTOMREQUEST, "REPORT");
        curl_easy_setopt(connectionHandle, CURLOPT_UPLOAD, 1L);
+       curl_easy_setopt(connectionHandle, CURLOPT_ERRORBUFFER, sessionErrorBuffer);
        curl_easy_setopt(connectionHandle, CURLOPT_READDATA, &entryListSendData);
        curl_easy_setopt(connectionHandle, CURLOPT_READFUNCTION, CalDAVSend);
        
@@ -642,6 +764,7 @@ CalDAVEntryList CalDAV::GetEntryList(string *calendarHREF){
        curl_easy_setopt(connectionHandle, CURLOPT_URL, originalServerAddress.c_str());
        curl_easy_setopt(connectionHandle, CURLOPT_CUSTOMREQUEST, NULL);        
        curl_easy_setopt(connectionHandle, CURLOPT_UPLOAD, 0L);
+       curl_easy_setopt(connectionHandle, CURLOPT_ERRORBUFFER, sessionErrorBuffer);
        curl_easy_setopt(connectionHandle, CURLOPT_READDATA, NULL);
        curl_easy_setopt(connectionHandle, CURLOPT_READFUNCTION, NULL);
        
@@ -698,6 +821,7 @@ CalDAVEntryList CalDAV::GetEntryList(string *calendarHREF, string *calendarTag){
        curl_easy_setopt(connectionHandle, CURLOPT_URL, entryListURLAddress.c_str());
        curl_easy_setopt(connectionHandle, CURLOPT_CUSTOMREQUEST, "REPORT");
        curl_easy_setopt(connectionHandle, CURLOPT_UPLOAD, 1L);
+       curl_easy_setopt(connectionHandle, CURLOPT_ERRORBUFFER, sessionErrorBuffer);
        curl_easy_setopt(connectionHandle, CURLOPT_READDATA, &entryListSendData);
        curl_easy_setopt(connectionHandle, CURLOPT_READFUNCTION, CalDAVSend);
        
@@ -775,6 +899,7 @@ CalDAVEntryList CalDAV::GetEntryList(string *calendarHREF, string *calendarTag){
        curl_easy_setopt(connectionHandle, CURLOPT_URL, entryListURLAddress.c_str());
        curl_easy_setopt(connectionHandle, CURLOPT_CUSTOMREQUEST, "REPORT");
        curl_easy_setopt(connectionHandle, CURLOPT_UPLOAD, 1L);
+       curl_easy_setopt(connectionHandle, CURLOPT_ERRORBUFFER, sessionErrorBuffer);
        curl_easy_setopt(connectionHandle, CURLOPT_READDATA, &updatedEntryListSendData);
        curl_easy_setopt(connectionHandle, CURLOPT_READFUNCTION, CalDAVSend);
        
@@ -817,6 +942,7 @@ CalDAVEntryList CalDAV::GetEntryList(string *calendarHREF, string *calendarTag){
        curl_easy_setopt(connectionHandle, CURLOPT_URL, originalServerAddress.c_str());
        curl_easy_setopt(connectionHandle, CURLOPT_CUSTOMREQUEST, NULL);        
        curl_easy_setopt(connectionHandle, CURLOPT_UPLOAD, 0L);
+       curl_easy_setopt(connectionHandle, CURLOPT_ERRORBUFFER, sessionErrorBuffer);
        curl_easy_setopt(connectionHandle, CURLOPT_READDATA, NULL);
        curl_easy_setopt(connectionHandle, CURLOPT_READFUNCTION, NULL);
        
@@ -898,10 +1024,11 @@ CalDAVServerResult CalDAV::AddCalendar(string *calendarName, string *calendarSho
        
        struct curl_slist *calendarRequestHeader = NULL;
        
-       //curl_easy_setopt(ConnectionHandle, CURLOPT_HTTPHEADER, CalendarRequestHeader);
+       //curl_easy_setopt(connectionHandle, CURLOPT_HTTPHEADER, CalendarRequestHeader);
        curl_easy_setopt(connectionHandle, CURLOPT_URL, calendarListURLAddress.c_str());
        curl_easy_setopt(connectionHandle, CURLOPT_CUSTOMREQUEST, "MKCALENDAR");
        curl_easy_setopt(connectionHandle, CURLOPT_UPLOAD, 1L);
+       curl_easy_setopt(connectionHandle, CURLOPT_ERRORBUFFER, sessionErrorBuffer);
        curl_easy_setopt(connectionHandle, CURLOPT_READDATA, &calendarAddSendData);
        curl_easy_setopt(connectionHandle, CURLOPT_READFUNCTION, CalDAVSend);
        
@@ -926,6 +1053,7 @@ CalDAVServerResult CalDAV::AddCalendar(string *calendarName, string *calendarSho
        curl_easy_setopt(connectionHandle, CURLOPT_URL, originalServerAddress.c_str());
        curl_easy_setopt(connectionHandle, CURLOPT_CUSTOMREQUEST, NULL);        
        curl_easy_setopt(connectionHandle, CURLOPT_UPLOAD, 0L);
+       curl_easy_setopt(connectionHandle, CURLOPT_ERRORBUFFER, sessionErrorBuffer);
        curl_easy_setopt(connectionHandle, CURLOPT_READDATA, NULL);
        curl_easy_setopt(connectionHandle, CURLOPT_READFUNCTION, NULL);
        
@@ -1026,10 +1154,11 @@ CalDAVServerResult CalDAV::EditCalendarProcess(string *calendarHREF,
        
        struct curl_slist *calendarRequestHeader = NULL;
        
-       //curl_easy_setopt(ConnectionHandle, CURLOPT_HTTPHEADER, CalendarRequestHeader);
+       //curl_easy_setopt(connectionHandle, CURLOPT_HTTPHEADER, CalendarRequestHeader);
        curl_easy_setopt(connectionHandle, CURLOPT_URL, calendarEditURLAddress.c_str());
        curl_easy_setopt(connectionHandle, CURLOPT_CUSTOMREQUEST, "PROPPATCH");
        curl_easy_setopt(connectionHandle, CURLOPT_UPLOAD, 1L);
+       curl_easy_setopt(connectionHandle, CURLOPT_ERRORBUFFER, sessionErrorBuffer);
        curl_easy_setopt(connectionHandle, CURLOPT_READDATA, &calendarEditSendData);
        curl_easy_setopt(connectionHandle, CURLOPT_READFUNCTION, CalDAVSend);
        
@@ -1054,6 +1183,7 @@ CalDAVServerResult CalDAV::EditCalendarProcess(string *calendarHREF,
        curl_easy_setopt(connectionHandle, CURLOPT_URL, originalServerAddress.c_str());
        curl_easy_setopt(connectionHandle, CURLOPT_CUSTOMREQUEST, NULL);        
        curl_easy_setopt(connectionHandle, CURLOPT_UPLOAD, 0L);
+       curl_easy_setopt(connectionHandle, CURLOPT_ERRORBUFFER, sessionErrorBuffer);
        curl_easy_setopt(connectionHandle, CURLOPT_READDATA, NULL);
        curl_easy_setopt(connectionHandle, CURLOPT_READFUNCTION, NULL);
        
@@ -1199,6 +1329,7 @@ CalDAVServerResult CalDAV::DeleteCalendar(string *calendarHREF){
        curl_easy_setopt(connectionHandle, CURLOPT_URL, originalServerAddress.c_str());
        curl_easy_setopt(connectionHandle, CURLOPT_CUSTOMREQUEST, NULL);        
        curl_easy_setopt(connectionHandle, CURLOPT_UPLOAD, 0L);
+       curl_easy_setopt(connectionHandle, CURLOPT_ERRORBUFFER, sessionErrorBuffer);
        curl_easy_setopt(connectionHandle, CURLOPT_READDATA, NULL);
        curl_easy_setopt(connectionHandle, CURLOPT_READFUNCTION, NULL);
        curl_easy_setopt(connectionHandle, CURLOPT_HTTPHEADER, NULL);
@@ -1262,6 +1393,7 @@ CalDAVServerResult CalDAV::GetEntryETag(string *calendarEntryHREF, string *eTagV
        curl_easy_setopt(connectionHandle, CURLOPT_URL, getETagURLAddress.c_str());
        curl_easy_setopt(connectionHandle, CURLOPT_CUSTOMREQUEST, "REPORT");
        curl_easy_setopt(connectionHandle, CURLOPT_UPLOAD, 1L);
+       curl_easy_setopt(connectionHandle, CURLOPT_ERRORBUFFER, sessionErrorBuffer);
        curl_easy_setopt(connectionHandle, CURLOPT_READDATA, &entryETagGetData);
        curl_easy_setopt(connectionHandle, CURLOPT_READFUNCTION, CalDAVSend);
        
@@ -1294,6 +1426,7 @@ CalDAVServerResult CalDAV::GetEntryETag(string *calendarEntryHREF, string *eTagV
        curl_easy_setopt(connectionHandle, CURLOPT_URL, originalServerAddress.c_str());
        curl_easy_setopt(connectionHandle, CURLOPT_CUSTOMREQUEST, NULL);        
        curl_easy_setopt(connectionHandle, CURLOPT_UPLOAD, 0L);
+       curl_easy_setopt(connectionHandle, CURLOPT_ERRORBUFFER, sessionErrorBuffer);
        curl_easy_setopt(connectionHandle, CURLOPT_READDATA, NULL);
        curl_easy_setopt(connectionHandle, CURLOPT_READFUNCTION, NULL);
        curl_easy_setopt(connectionHandle, CURLOPT_HTTPHEADER, NULL);
@@ -1324,6 +1457,7 @@ CalDAVServerResult CalDAV::AddEntry(string *calendarEntryHREF, string *entryData
        curl_easy_setopt(connectionHandle, CURLOPT_URL, entryAddURLAddress.c_str());
        curl_easy_setopt(connectionHandle, CURLOPT_CUSTOMREQUEST, "PUT");
        curl_easy_setopt(connectionHandle, CURLOPT_UPLOAD, 1L);
+       curl_easy_setopt(connectionHandle, CURLOPT_ERRORBUFFER, sessionErrorBuffer);
        curl_easy_setopt(connectionHandle, CURLOPT_READDATA, &entryAddSendData);
        curl_easy_setopt(connectionHandle, CURLOPT_READFUNCTION, CalDAVSend);
        
@@ -1348,6 +1482,7 @@ CalDAVServerResult CalDAV::AddEntry(string *calendarEntryHREF, string *entryData
        curl_easy_setopt(connectionHandle, CURLOPT_URL, originalServerAddress.c_str());
        curl_easy_setopt(connectionHandle, CURLOPT_CUSTOMREQUEST, NULL);        
        curl_easy_setopt(connectionHandle, CURLOPT_UPLOAD, 0L);
+       curl_easy_setopt(connectionHandle, CURLOPT_ERRORBUFFER, sessionErrorBuffer);
        curl_easy_setopt(connectionHandle, CURLOPT_READDATA, NULL);
        curl_easy_setopt(connectionHandle, CURLOPT_READFUNCTION, NULL);
        curl_easy_setopt(connectionHandle, CURLOPT_HTTPHEADER, NULL);
@@ -1385,6 +1520,7 @@ CalDAVServerResult CalDAV::EditEntry(string *calendarEntryHREF, string *entryDat
        curl_easy_setopt(connectionHandle, CURLOPT_URL, entryAddURLAddress.c_str());
        curl_easy_setopt(connectionHandle, CURLOPT_CUSTOMREQUEST, "PUT");
        curl_easy_setopt(connectionHandle, CURLOPT_UPLOAD, 1L);
+       curl_easy_setopt(connectionHandle, CURLOPT_ERRORBUFFER, sessionErrorBuffer);
        curl_easy_setopt(connectionHandle, CURLOPT_READDATA, &entryAddSendData);
        curl_easy_setopt(connectionHandle, CURLOPT_READFUNCTION, CalDAVSend);
        
@@ -1409,6 +1545,7 @@ CalDAVServerResult CalDAV::EditEntry(string *calendarEntryHREF, string *entryDat
        curl_easy_setopt(connectionHandle, CURLOPT_URL, originalServerAddress.c_str());
        curl_easy_setopt(connectionHandle, CURLOPT_CUSTOMREQUEST, NULL);        
        curl_easy_setopt(connectionHandle, CURLOPT_UPLOAD, 0L);
+       curl_easy_setopt(connectionHandle, CURLOPT_ERRORBUFFER, sessionErrorBuffer);
        curl_easy_setopt(connectionHandle, CURLOPT_READDATA, NULL);
        curl_easy_setopt(connectionHandle, CURLOPT_READFUNCTION, NULL);
        curl_easy_setopt(connectionHandle, CURLOPT_HTTPHEADER, NULL);
@@ -1452,6 +1589,7 @@ CalDAVServerResult CalDAV::DeleteEntry(string *calendarEntryHREF){
        curl_easy_setopt(connectionHandle, CURLOPT_URL, originalServerAddress.c_str());
        curl_easy_setopt(connectionHandle, CURLOPT_CUSTOMREQUEST, NULL);        
        curl_easy_setopt(connectionHandle, CURLOPT_UPLOAD, 0L);
+       curl_easy_setopt(connectionHandle, CURLOPT_ERRORBUFFER, sessionErrorBuffer);
        curl_easy_setopt(connectionHandle, CURLOPT_READDATA, NULL);
        curl_easy_setopt(connectionHandle, CURLOPT_READFUNCTION, NULL);
        curl_easy_setopt(connectionHandle, CURLOPT_HTTPHEADER, NULL);
@@ -1462,19 +1600,19 @@ CalDAVServerResult CalDAV::DeleteEntry(string *calendarEntryHREF){
 
 COSSLVerified CalDAV::SSLVerify()
 {
-       return SSLVerified;
+       return sslVerified;
 }
 
-void CalDAV::BypassSSLVerification(bool EnableBypass){
-       EnableSSLBypass = EnableBypass;
-       SSLSelfSigned = EnableBypass;
+void CalDAV::BypassSSLVerification(bool enableBypass){
+       enableSSLBypass = enableBypass;
+       sslSelfSigned = enableBypass;
 }
 
 #if defined(__APPLE__)
 
 SecTrustRef CalDAV::BuildSSLCollection(){
        
-       return CertificateData;
+       return certificateData;
        
 }
 
@@ -1482,7 +1620,7 @@ SecTrustRef CalDAV::BuildSSLCollection(){
 
 PCCERT_CONTEXT CalDAV::BuildSSLCollection(){
 
-       return CertificateData;
+       return certificateData;
 
 }
 
@@ -1492,7 +1630,7 @@ SSLCertCollectionString CalDAV::BuildSSLCollection(){
 
        // Build and return the SSL collection.
        
-       SSLCertCollectionString SSLCertInfo;
+       SSLCertCollectionString sslCertInfo;
 
        // Grab the certificate data.
 
@@ -1503,50 +1641,73 @@ SSLCertCollectionString CalDAV::BuildSSLCollection(){
 
        certptr.certdata = NULL;
        
-       CURLcode result = curl_easy_getinfo(ConnectionSession, CURLINFO_CERTINFO, &certptr.certinfo);
+       CURLcode result = curl_easy_getinfo(connectionHandle, CURLINFO_CERTINFO, &certptr.certinfo);
        
-       std::string CertPropName;
-       std::string CertPropValue;
+       std::string certPropName;
+       std::string certPropValue;
        
        for (int i = 0; i < certptr.certinfo->num_of_certs; i++){
                
                struct curl_slist *slist;
-               SSLCertDataString SSLCertDataInc;
+               SSLCertDataString sslCertDataInc;
                
                for (slist = certptr.certinfo->certinfo[i]; slist; slist = slist->next){
                        
                        // Using wxStringTokenizer from wxWidgets.
                        
-                       wxStringTokenizer CertDataInc(wxString::FromUTF8(slist->data), ":");
+                       wxStringTokenizer certDataInc(wxString::FromUTF8(slist->data), ":");
                        
                        // Get first token as the property name.
                        
-                       CertPropName = CertDataInc.GetNextToken().ToStdString();
+                       certPropName = certDataInc.GetNextToken().ToStdString();
                        
                        // Get remaining tokens as the property value.
                        
-                       while(CertDataInc.HasMoreTokens()){
+                       while(certDataInc.HasMoreTokens()){
                        
-                               CertPropValue.append(CertDataInc.GetNextToken());
+                               certPropValue.append(certDataInc.GetNextToken());
                        
                        }
                        
-                       SSLCertDataInc.CertData.insert(std::make_pair(CertPropName, CertPropValue));
-                       CertPropName.clear();
-                       CertPropValue.clear();
+                       sslCertDataInc.CertData.insert(std::make_pair(certPropName, certPropValue));
+                       certPropName.clear();
+                       certPropValue.clear();
                        
                }
        
-               SSLCertInfo.SSLCollection.insert(std::make_pair(i, SSLCertDataInc));
+               sslCertInfo.SSLCollection.insert(std::make_pair(i, sslCertDataInc));
        
        }
        
-       return SSLCertInfo;
+       return sslCertInfo;
 
 }
 
 #endif
 
+bool CalDAV::CanDoSSL(){
+       return sslStatus;
+}
+
+bool CalDAV::HasValidResponse(){
+       return validResponse;
+}
+
+bool CalDAV::AbleToLogin(){
+       return authPassed;
+}
+
+bool CalDAV::IsSelfSigned(){
+       return sslSelfSigned;
+}
+
+string CalDAV::GetErrorMessage(){
+       
+       errorMessage = sessionErrorBuffer;      
+       return errorMessage;
+       
+}
+
 static bool CalDAVObjectValidSettings(CalDAVConnectionData *connData){
 
        // Check if the passed CalDAV Connection Data is has
@@ -1608,7 +1769,7 @@ static bool CalDAVObjectValidSettings(CalDAVConnectionData *connData){
 
 }
 
-static string BuildServerAddress(CalDAVConnectionData *connData, string uriAddress){
+string CalDAV::BuildServerAddress(CalDAVConnectionData *connData, string uriAddress){
        
        string serverAddress;
        
@@ -1635,3 +1796,106 @@ static string BuildServerAddress(CalDAVConnectionData *connData, string uriAddre
        return serverAddress;
        
 }
+
+void CalDAV::SetupDefaultParametersNonSSL(bool doAuthentication){
+       
+       std::string serverAddress = "";
+
+       string serverAddressURL = "http://" + connectionData.hostname + ":" + to_string(connectionData.port) + "/";
+       string usernamePassword = connectionData.username + ":" + connectionData.password;
+       
+       curl_easy_setopt(connectionHandle, CURLOPT_URL, serverAddressURL.c_str());
+       curl_easy_setopt(connectionHandle, CURLOPT_NOPROGRESS, 1L);
+       curl_easy_setopt(connectionHandle, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST|CURLAUTH_BASIC);
+       curl_easy_setopt(connectionHandle, CURLOPT_TIMEOUT, 60);
+       curl_easy_setopt(connectionHandle, CURLOPT_FAILONERROR, true);
+       curl_easy_setopt(connectionHandle, CURLOPT_USERAGENT, XSDCAL_USERAGENT);
+       curl_easy_setopt(connectionHandle, CURLOPT_NOSIGNAL, 1L);
+       curl_easy_setopt(connectionHandle, CURLOPT_CUSTOMREQUEST, "GET");
+       curl_easy_setopt(connectionHandle, CURLOPT_HTTPHEADER, nullptr);
+       curl_easy_setopt(connectionHandle, CURLOPT_POSTFIELDS, nullptr);
+       curl_easy_setopt(connectionHandle, CURLOPT_POSTFIELDSIZE, 0L);
+
+       if (doAuthentication == true){
+               curl_easy_setopt(connectionHandle, CURLOPT_USERPWD, usernamePassword.c_str());
+       } else {
+               curl_easy_setopt(connectionHandle, CURLOPT_USERPWD, NULL);              
+       }
+       
+}
+
+void CalDAV::SetupDefaultParametersSSL(bool doAuthentication){
+       
+       // Setup the default parameters.
+       
+       string ServerAddressURL = "https://" + connectionData.hostname + ":" + to_string(connectionData.port) + "/";
+       string UsernamePassword = connectionData.username + ":" + connectionData.password;
+       
+       curl_easy_setopt(connectionHandle, CURLOPT_URL, ServerAddressURL.c_str());
+       curl_easy_setopt(connectionHandle, CURLOPT_NOPROGRESS, 1L);
+       curl_easy_setopt(connectionHandle, CURLOPT_CERTINFO, 1L);
+       curl_easy_setopt(connectionHandle, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST|CURLAUTH_BASIC);
+       curl_easy_setopt(connectionHandle, CURLOPT_TIMEOUT, 60);
+       curl_easy_setopt(connectionHandle, CURLOPT_FAILONERROR, 0L);
+       curl_easy_setopt(connectionHandle, CURLOPT_USERAGENT, XSDCAL_USERAGENT);
+       curl_easy_setopt(connectionHandle, CURLOPT_ERRORBUFFER, sessionErrorBuffer);
+       curl_easy_setopt(connectionHandle, CURLOPT_CUSTOMREQUEST, "GET");
+       curl_easy_setopt(connectionHandle, CURLOPT_HTTPHEADER, nullptr);
+       curl_easy_setopt(connectionHandle, CURLOPT_POSTFIELDS, nullptr);
+       curl_easy_setopt(connectionHandle, CURLOPT_POSTFIELDSIZE, 0L);
+
+       if (doAuthentication == true){
+               curl_easy_setopt(connectionHandle, CURLOPT_USERPWD, UsernamePassword.c_str());
+       } else {
+               curl_easy_setopt(connectionHandle, CURLOPT_USERPWD, NULL);              
+       }
+       
+       if (enableSSLBypass == true){
+               curl_easy_setopt(connectionHandle, CURLOPT_SSL_VERIFYHOST, 0L);
+               curl_easy_setopt(connectionHandle, CURLOPT_SSL_VERIFYPEER, 0L);
+               curl_easy_setopt(connectionHandle, CURLOPT_SSL_VERIFYSTATUS, 0L);
+       } else {
+               curl_easy_setopt(connectionHandle, CURLOPT_SSL_VERIFYHOST, 2L);
+               curl_easy_setopt(connectionHandle, CURLOPT_SSL_VERIFYPEER, 1L);
+               curl_easy_setopt(connectionHandle, CURLOPT_SSL_VERIFYSTATUS, 1L);
+       }
+       
+#if !defined(__APPLE__) || defined(__WIN32__)
+       
+       if (connectionData.account.size() > 0){
+               
+               // Check if the server certificate file exists.
+               
+               string certificateFilename = GetAccountDir(connectionData.account, true);
+               
+               if (wxFile::Exists(certificateFilename)){
+                       
+                       curl_easy_setopt(connectionHandle, CURLOPT_CAINFO, certificateFilename.c_str());
+                       
+               }
+               
+       }
+
+#endif
+       
+}
+
+void CalDAV::ResetResults(){
+       
+       sslStatus = false;
+       COSSLVerified SSLVerified = COSSL_NORESULT;
+       validResponse = false;
+       authPassed = false;
+       sslSelfSigned = false;
+       //taskCompleted = false;
+       errorMessage = "";
+       sessionErrorBuffer[0] = '\0';
+       //sessionResult = CURLE_OK;
+       serverData = "";
+       serverHeader = "";
+       /*if (headerList != nullptr){
+               curl_slist_free_all(headerList);
+               headerList = nullptr;
+       }*/
+       
+}
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