Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Added AddContact for CardDAV2 and frmActivityMgr
[xestiaab/.git] / source / carddav2 / carddav2.cpp
index f5bc2a3..494468c 100644 (file)
@@ -77,8 +77,15 @@ size_t CardDAV2::WritebackFuncImplementation(char *ptr, size_t size, size_t nmem
 }
 
 CardDAV2::~CardDAV2(){
+       
        curl_easy_cleanup(ConnectionSession);
        ConnectionSession = nullptr;
+       
+       if (HeaderList != nullptr){
+               curl_slist_free_all(HeaderList);
+               HeaderList = nullptr;
+       }
+       
 }
 
 #if defined(__APPLE__)
@@ -172,11 +179,12 @@ COConnectResult CardDAV2::Connect(bool DoAuthentication){
        if (TestMode == true){
                SessionResult = curl_easy_perform(ConnectionSession);
        } else {
-               
+               SessionResult = curl_easy_perform(ConnectionSession);           
        }
        
        switch(SessionResult){
                case CURLE_OK:
+               case CURLE_HTTP_RETURNED_ERROR:
                        SSLStatus = true;
                        SSLVerified = COSSL_VERIFIED;
                        ConnectResult = COCONNECT_OK;
@@ -209,7 +217,7 @@ COConnectResult CardDAV2::Connect(bool DoAuthentication){
                        ConnectResult = COCONNECT_OK;
                        AuthPassed = true;
                        ValidResponse = true;
-               } else if (SessionResponseCode == 403){
+               } else if (SessionResponseCode == 401){
                        ConnectResult = COCONNECT_AUTHFAIL;
                        AuthPassed = false;
                        ValidResponse = true;
@@ -336,7 +344,7 @@ COServerResponse CardDAV2::GetDefaultPrefix(string *ServerPrefix){
        } else if (SessionResponseCode == 403){
                AuthPassed = false;
                ValidResponse = true;
-       } else if (SessionResponseCode >= 200) {
+       } else if (SessionResponseCode >= 400) {
                AuthPassed = false;
                ValidResponse = true;
        } else {
@@ -393,19 +401,15 @@ COServerResponse CardDAV2::GetDefaultPrefix(string *ServerPrefix){
        curl_easy_getinfo(ConnectionSession, CURLINFO_RESPONSE_CODE, &SessionResponseCode);
        
        if (SessionResponseCode == 200 || SessionResponseCode == 207){
-               //ConnectResult = COCONNECT_OK;
                AuthPassed = true;
                ValidResponse = true;
        } else if (SessionResponseCode == 403){
-               //ConnectResult = COCONNECT_AUTHFAIL;
                AuthPassed = false;
                ValidResponse = true;
-       } else if (SessionResponseCode >= 200) {
-               //ConnectResult = COCONNECT_INVALID;
+       } else if (SessionResponseCode >= 400) {
                AuthPassed = false;
                ValidResponse = true;
        } else {
-               //ConnectResult = COCONNECT_INVALID;
                AuthPassed = false;
                ValidResponse = false;                  
        }
@@ -741,6 +745,88 @@ std::string CardDAV2::GetDefaultAddressBookURI(){
 
 COServerResponse CardDAV2::AddContact(std::string Location, std::string Data){
        
+       // Check if authentication was successful, otherwise don't do anything.
+
+       COServerResponse ServerResponse;
+       
+       if (AuthPassed == false){
+               ServerResponse.RequestResult = COREQUEST_ERROR_NOTCONNECTED;
+               ServerResponse.EntityTag = "";
+               ServerResponse.SessionCode = 0;
+               ServerResponse.ResultCode = 0;
+               ServerResponse.ResultMessage = "";
+               return ServerResponse;
+       }
+
+       ServerSSL ? SetupDefaultParametersSSL(true) : SetupDefaultParametersNonSSL(true);
+       ResetResults();
+       
+       string ServerAddressURL = BuildURL(ServerPrefix + Location);
+       curl_easy_setopt(ConnectionSession, CURLOPT_URL, ServerAddressURL.c_str());
+       curl_easy_setopt(ConnectionSession, CURLOPT_CUSTOMREQUEST, "PUT");
+       curl_easy_setopt(ConnectionSession, CURLOPT_POSTFIELDS, Data.c_str());
+       curl_easy_setopt(ConnectionSession, CURLOPT_POSTFIELDSIZE, strlen(Data.c_str()));
+       
+       HeaderList = curl_slist_append(HeaderList, "Content-Type: text/vcard; charset=utf-8");
+
+       curl_easy_setopt(ConnectionSession, CURLOPT_HTTPHEADER, HeaderList);
+       
+       if (TestMode == true){
+               SessionResult = curl_easy_perform(ConnectionSession);
+       } else {
+               SessionResult = curl_easy_perform(ConnectionSession);
+       }
+       
+       switch(SessionResult){
+               case CURLE_OK:
+                       SSLStatus = true;
+                       SSLVerified = COSSL_VERIFIED;
+                       break;
+               case CURLE_SSL_CACERT:
+               case CURLE_SSL_CONNECT_ERROR:
+                       SSLStatus = true;
+                       SSLVerified = COSSL_UNABLETOVERIFY;
+                       break;
+               default:
+                       break;
+       };
+       
+       long SessionResponseCode = 0;
+       
+       curl_easy_getinfo(ConnectionSession, CURLINFO_RESPONSE_CODE, &SessionResponseCode);
+       
+       if (SessionResponseCode == 200 || SessionResponseCode == 201 || SessionResponseCode == 204){
+               AuthPassed = true;
+               ValidResponse = true;
+       } else if (SessionResponseCode == 403){
+               AuthPassed = false;
+               ValidResponse = true;
+       } else if (SessionResponseCode >= 400){
+               AuthPassed = false;
+               ValidResponse = true;
+       } else {
+               AuthPassed = false;
+               ValidResponse = false;                  
+       }
+       
+       if (ValidResponse == false && AuthPassed == false){
+               ServerResponse.RequestResult = COREQUEST_ERROR_SERVER;
+               ServerResponse.EntityTag = "";
+               ServerResponse.SessionCode = SessionResult;
+               ServerResponse.ResultCode = SessionResponseCode;
+               ServerResponse.ResultMessage = "";
+               return ServerResponse;
+       }
+       
+       CanProcess = true;
+       
+       ServerResponse.RequestResult = COREQUEST_OK;
+       ServerResponse.EntityTag = "";
+       ServerResponse.SessionCode = SessionResult;
+       ServerResponse.ResultCode = SessionResponseCode;
+       ServerResponse.ResultMessage = SessionErrorBuffer;
+       return ServerResponse;
+       
 }
 
 COServerResponse CardDAV2::EditContact(std::string Location, std::string Data){
@@ -805,6 +891,7 @@ void CardDAV2::SetupDefaultParametersNonSSL(bool DoAuthentication){
        curl_easy_setopt(ConnectionSession, CURLOPT_WRITEHEADER, &PageHeader);
        curl_easy_setopt(ConnectionSession, CURLOPT_NOSIGNAL, 1);
        curl_easy_setopt(ConnectionSession, CURLOPT_CUSTOMREQUEST, "GET");
+       curl_easy_setopt(ConnectionSession, CURLOPT_HTTPHEADER, nullptr);
        
        if (DoAuthentication == true){
                curl_easy_setopt(ConnectionSession, CURLOPT_USERPWD, UsernamePassword.c_str());
@@ -835,6 +922,7 @@ void CardDAV2::SetupDefaultParametersSSL(bool DoAuthentication){
        curl_easy_setopt(ConnectionSession, CURLOPT_CERTINFO, 1);
        curl_easy_setopt(ConnectionSession, CURLOPT_VERBOSE, 1);
        curl_easy_setopt(ConnectionSession, CURLOPT_CUSTOMREQUEST, "GET");
+       curl_easy_setopt(ConnectionSession, CURLOPT_HTTPHEADER, nullptr);
        
        if (DoAuthentication == true){
                curl_easy_setopt(ConnectionSession, CURLOPT_USERPWD, UsernamePassword.c_str());
@@ -850,6 +938,20 @@ void CardDAV2::SetupDefaultParametersSSL(bool DoAuthentication){
                curl_easy_setopt(ConnectionSession, CURLOPT_SSL_VERIFYPEER, 1);         
        }
        
+       if (TestMode == false && ServerAccount.size() > 0){
+               
+               // Check if the server certificate file exists.
+               
+               string CertificateFilename = GetAccountDir(ServerAccount, true);
+               
+               if (wxFile::Exists(CertificateFilename)){
+                       
+                       curl_easy_setopt(ConnectionSession, CURLOPT_CAINFO, CertificateFilename.c_str());
+                       
+               }
+               
+       }
+       
 }
 
 string CardDAV2::BuildURL(string URI){
@@ -886,18 +988,96 @@ void CardDAV2::ResetResults(){
        SessionErrorBuffer[0] = '\0';
        PageData = "";
        PageHeader = "";
+       if (HeaderList != nullptr){
+               curl_slist_free_all(HeaderList);
+               HeaderList = nullptr;
+       }
        
 }
 
-vector<string> CardDAV2::GetDAVHeader(){
+string CardDAV2::GetETagHeader(){
        
        // Go through each of the lines looking for the
        // 'DAV:' section.
        
+       string HeaderName;
+       string HeaderValue;
+       bool FastForward = false;
+       
+       for (int HeaderSeek = 0; HeaderSeek < PageHeader.size(); HeaderSeek++){
+               
+               if (FastForward == true){
+                       
+                       if (PageHeader[HeaderSeek] == '\n'){
+                               FastForward = false;
+                       }
+                       
+                       continue;
+                       
+               }
+               
+               try {
+                       PageHeader.substr(HeaderSeek, 5) == "ETag:";
+               }
+               
+               catch (const out_of_range &oor){
+                       break;
+               }
+               
+               if (PageHeader.substr(HeaderSeek, 5) == "ETag:"){
+                       
+                       int CharacterSeek = 5;
+                       
+                       while ((HeaderSeek + CharacterSeek) < PageHeader.size()){
+                               
+                               if (PageHeader.substr((HeaderSeek + CharacterSeek), 2) == "\r\n"){
+                                       break;
+                               }
+                               
+                               HeaderValue += PageHeader.substr((HeaderSeek + CharacterSeek), 1);
+                               CharacterSeek++;
+                       }
+                       
+                       break;
+                       
+               } else {
+                       
+                       FastForward = true;
+                       continue;
+                       
+               }
+               
+               if (PageHeader[HeaderSeek] == '\n'){
+                       HeaderName = "";
+               }
+               
+               //HeaderName += PageHeader.substr(HeaderSeek, 1);
+               
+       }
+       
+       // Check for quotation marks at the start and end and strip
+       // them out.
+       
+       if (HeaderValue.size() >= 2){
+               
+               if (HeaderValue[0] == '"'){
+                       HeaderValue.erase((HeaderValue.size() - 1));
+                       HeaderValue.erase(0);
+               }
+               
+       }
+       
+       return HeaderValue;
+       
+}
+
+vector<string> CardDAV2::GetDAVHeader(){
+       
+       // Go through each of the lines looking for the
+       // 'DAV:' section.
        
        string HeaderName;
        string HeaderValue;
-       bool DAVFound = false;
        bool FastForward = false;
        vector<string> DAVHeaderList;
        
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