From d269801e3d32dbbea14af5f261190ef097635fce Mon Sep 17 00:00:00 2001 From: Steve Brokenshire Date: Thu, 25 Aug 2016 22:47:03 +0100 Subject: [PATCH] Implemented EditContact and GetContact in CardDAV2 --- source/actmgr/frmActivityMgr-tasks.cpp | 4 + source/actmgr/frmActivityMgr.cpp | 264 +++++++++++++++++++++++-- source/carddav2/carddav2.cpp | 173 +++++++++++++++- source/carddav2/carddav2.h | 2 +- source/connobject/ConnectionObject.h | 2 +- 5 files changed, 426 insertions(+), 19 deletions(-) diff --git a/source/actmgr/frmActivityMgr-tasks.cpp b/source/actmgr/frmActivityMgr-tasks.cpp index f9046ea..513b8fe 100644 --- a/source/actmgr/frmActivityMgr-tasks.cpp +++ b/source/actmgr/frmActivityMgr-tasks.cpp @@ -146,11 +146,15 @@ int frmActivityMgr::AddTask(int TaskType, wxString TaskDetail, wxString TaskAcco lstActivity->SetItem(itemindex, 2, _("Queued")); ETagDB *ETagDBPtr = ETagTmrPtr->GetPointer(AccountDir); + + CardDAV2 *ConnObject = new CardDAV2(AccountAddress.ToStdString(), AccountPort, AccountUsername.ToStdString(), AccountPassword.ToStdString(), AccountSSL, AccountPrefix.ToStdString(), AccountDir.ToStdString()); + ConnObject->SetupConnectionObject(); ActivityListType.insert(std::make_pair(ActivityTaskID, 1)); ActivityListData.insert(std::make_pair(ActivityTaskID, TaskData)); ActivityListURL.insert(std::make_pair(ActivityTaskID, TaskURL)); ActivityListEditMode.insert(std::make_pair(ActivityTaskID, true)); + ActivityListConnObject.insert(std::make_pair(ActivityTaskID, ConnObject)); ActivityListIndex.insert(std::make_pair(ActivityTaskID, itemindex)); ActivityListTaskDetail.insert(std::make_pair(ActivityTaskID, TaskDetail)); ActivityListAccount.insert(std::make_pair(ActivityTaskID, TaskAccount)); diff --git a/source/actmgr/frmActivityMgr.cpp b/source/actmgr/frmActivityMgr.cpp index fef672f..606b9f1 100644 --- a/source/actmgr/frmActivityMgr.cpp +++ b/source/actmgr/frmActivityMgr.cpp @@ -646,7 +646,7 @@ void frmActivityMgr::ProcessTasksThread() COServerResponse ETagGetResponse = ConnObjectIter->second->GetServerEntityTagValue(StringURLIter->second.ToStdString()); - if (AddContactResponse.RequestResult != COREQUEST_OK){ + if (ETagGetResponse.RequestResult != COREQUEST_OK){ iter->second = 2; break; } @@ -696,13 +696,240 @@ void frmActivityMgr::ProcessTasksThread() FullFilename = StringFullFilenameIter->second; EditMode = ActivityListEditMode.find(iter->first)->second; - bool KeepUpdating = TRUE; + bool KeepUpdating = true; - while(KeepUpdating == TRUE){ + while(KeepUpdating == true){ - bool ExitLoop = FALSE; + COConnectResult ConnectResponse = ConnObjectIter->second->Connect(false); - while (ExitLoop == FALSE){ + bool ExitLoop = false; + + while(ExitLoop == false){ + + if (ConnObjectIter->second->SSLVerify() == COSSL_UNABLETOVERIFY){ + + frmMainPtrGet->PauseAllTimers(); + +#if defined(__APPLE__) +#elif defined(__WIN32__) +#else + + bool UsingSSLBypass = false; + int SSLResult = 0; + + // Connect again and fetch SSL certificate information. + + ConnObjectIter->second->BypassSSLVerification(true); + + COConnectResult ConnectionSSLResult = ConnObjectIter->second->Connect(false); + + ConnObjectIter->second->BypassSSLVerification(false); + + SSLInvalidCertNotifObjString SSLICNProcData; + + SSLCertCollectionString certcol = ConnObjectIter->second->BuildSSLCollection(); + + bool *PauseMode = new bool; + QRNotif qrn; + + *PauseMode = TRUE; + qrn.QResponse = &SSLResult; + qrn.PausePtr = PauseMode; + + SSLICNProcData.CertCollection = certcol; + SSLICNProcData.QRNotifData = &qrn; + SSLICNProcData.AccountName = AccountNameFriendly; + + wxCommandEvent event(INVALIDSSLCERTSTRING); + event.SetClientData(&SSLICNProcData); + wxPostEvent(frmMainPtrGet, event); + + while (*PauseMode == TRUE){ + //nanosleep(&n1, &n2); + SleepFor(250000000); + } + + // Process the response from the user. + + if (SSLResult == 1){ + + // Accept the Certificate. + + UsingSSLBypass = true; + ConnObjectIter->second->BypassSSLVerification(true); + + COConnectResult TestConnectionResult = ConnObjectIter->second->Connect(true); + WriteServerCertificate(AccountDir, certcol); + + ConnObjectIter->second->BypassSSLVerification(false); + + } else if (SSLResult == 2){ + + // Reject the certificate, abort the task and mark as failed. + + iter->second = 2; + break; + + } + +#endif + + frmMainPtrGet->ResumeAllTimers(); + ExitLoop = true; + + } else if (ConnectResponse == COCONNECT_AUTHFAIL){ + + ConnectResponse = ConnObjectIter->second->Connect(true); + + if (ConnectResponse == COCONNECT_OK){ + + ExitLoop = true; + break; + + } else { + + ExitLoop = true; + iter->second = 2; + break; + + } + + } else if (ConnectResponse == COCONNECT_OK){ + + ConnectResponse = ConnObjectIter->second->Connect(true); + + ExitLoop = true; + break; + + } else { + + ExitLoop = true; + iter->second = 2; + break; + + } + + } + + if (iter->second == 2 || iter->second == 3 || iter->second == 4){ + break; + } + + COServerResponse ETagGetResponse = ConnObjectIter->second->GetServerEntityTagValue(StringURLIter->second.ToStdString()); + + if (ETagGetResponse.RequestResult != COREQUEST_OK){ + iter->second = 2; + break; + } + + ETagServer = ETagGetResponse.EntityTag; + + // Compare the ETag with the Server ETag. + + std::cout << ETagOriginal << std::endl; + std::cout << ETagServer << std::endl; + + if (ETagOriginal != ETagServer){ + + // Server ETag is different from original ETag. + // This is a conflict which the user will now + // need to resolve. + + frmMain *frmMainPtrGet = static_cast(frmMainPtr); + + vCard34Conv vCard34Obj; + + std::string ServerContactData; + wxString wxStringServerContactData; + vCard ClientData; + vCard ConvertedV4Data; + + // Load the client data from the filename given. + + //ConnHandle.GetServerContactData(); + //ServerContactData = ConnHandle.GetPageData(); + + COServerResponse GetContactResponse = ConnObjectIter->second->GetContact(StringURLIter->second.ToStdString(), + &ServerContactData); + + if (GetContactResponse.RequestResult != COREQUEST_OK){ + iter->second = 2; + break; + } + + wxStringServerContactData = wxString::FromUTF8((const char*)ServerContactData.c_str()); + + // Process v3 version into v4 version. + + vCard34Obj.ConvertToV4(&wxStringServerContactData, &ConvertedV4Data); + + vCardConflictObj vCardProcData; + + vCardProcData.vCardLocalData = &ClientData; + vCardProcData.vCardServerData = &ConvertedV4Data; + ClientData.LoadFile(FullFilename); + + // Setup Conflict Resolution Dialog. + + // Fetch Data from Server and convert. + + bool *PauseMode = new bool; + int ConflictResult; + QRNotif qrn; + + *PauseMode = TRUE; + qrn.QResponse = &ConflictResult; + qrn.PausePtr = PauseMode; + + vCardProcData.QRNotifData = &qrn; + + wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED); + event.SetClientData(&vCardProcData); + wxPostEvent(frmMainPtrGet, event); + + while (*PauseMode == TRUE){ + SleepFor(250000000); + } + + delete PauseMode; + PauseMode = NULL; + + if (ConflictResult == 0){ + + // Decide Later. + + iter->second = 4; + break; + + } else if (ConflictResult == 1){ + + // Decided to use client version. + + } else if (ConflictResult == 2){ + + // Decided to use server version. + + // Download server version. + + ConvertedV4Data.WriteFile(FullFilename); + + } + + } else { + + // Upload the data to the server. + + COServerResponse EditContactResponse = ConnObjectIter->second->EditContact(StringURLIter->second.ToStdString(), + StringDataIter->second.ToStdString()); + + if (EditContactResponse.RequestResult != COREQUEST_OK){ + iter->second = 2; + break; + } + + } + + /*while (ExitLoop == FALSE){ int ErrorCode = ConnHandle.SSLVerifyTest(); @@ -941,7 +1168,7 @@ void frmActivityMgr::ProcessTasksThread() // Decided to use client version. - ConnHandle.ProcessData(); + //ConnHandle.ProcessData(); } else if (ConflictResult == 2){ @@ -959,13 +1186,24 @@ void frmActivityMgr::ProcessTasksThread() ConnHandle.ProcessData(); - } + }*/ // Update the ETag DB. - ConnHandle.GetServerETagValueThread(); - ETagServer = ConnHandle.ETagValueResult(); - ETagDBPtr->UpdateETag(ContactFilename, ETagServer, ETagServer); + //ConnHandle.GetServerETagValueThread(); + //ETagServer = ConnHandle.ETagValueResult(); + + ETagGetResponse = ConnObjectIter->second->GetServerEntityTagValue(StringURLIter->second.ToStdString()); + + if (ETagGetResponse.RequestResult != COREQUEST_OK){ + iter->second = 2; + break; + } + + std::cout << "Etag Update!" << std::endl; + std::cout << ETagGetResponse.EntityTag << std::endl; + + ETagDBPtr->UpdateETag(ContactFilename, ETagGetResponse.EntityTag, ETagGetResponse.EntityTag); iter->second = 4; break; @@ -2214,12 +2452,12 @@ void frmActivityMgr::ProcessTasksThread() while (iter->second == 1){ - SleepFor(250000000); + SleepFor(125000000); //nanosleep(&n1, &n2); } - if ((iter->second == 4 && TypeIter->second == 0) || + /*if ((iter->second == 4 && TypeIter->second == 0) || (iter->second == 8 && TypeIter->second == 0)){ AccountDir = StringAccountIter->second; @@ -2241,7 +2479,7 @@ void frmActivityMgr::ProcessTasksThread() ETagServer = ConnHandle.ETagValueResult(); ETagDBPtr->UpdateETag(ContactFilename, ETagServer, ETagServer); - } + }*/ break; diff --git a/source/carddav2/carddav2.cpp b/source/carddav2/carddav2.cpp index 66982d9..9d42c04 100644 --- a/source/carddav2/carddav2.cpp +++ b/source/carddav2/carddav2.cpp @@ -476,7 +476,7 @@ COServerResponse CardDAV2::GetDefaultPrefix(string *ServerPrefix){ ValidResponse = false; } - if (ValidResponse == false && AuthPassed == false){ + if (ValidResponse == false || AuthPassed == false){ ServerResponse.RequestResult = COREQUEST_ERROR_SERVER; ServerResponse.EntityTag = ""; ServerResponse.SessionCode = SessionResult; @@ -809,7 +809,7 @@ COServerResponse CardDAV2::AddContact(std::string Location, std::string Data){ ValidResponse = false; } - if (ValidResponse == false && AuthPassed == false){ + if (ValidResponse == false || AuthPassed == false){ ServerResponse.RequestResult = COREQUEST_ERROR_SERVER; ServerResponse.EntityTag = ""; ServerResponse.SessionCode = SessionResult; @@ -830,6 +830,88 @@ COServerResponse CardDAV2::AddContact(std::string Location, std::string Data){ } COServerResponse CardDAV2::EditContact(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; } @@ -908,7 +990,7 @@ COServerResponse CardDAV2::GetServerEntityTagValue(std::string Location){ ValidResponse = false; } - if (ValidResponse == false && AuthPassed == false){ + if (ValidResponse == false || AuthPassed == false){ ServerResponse.RequestResult = COREQUEST_ERROR_SERVER; ServerResponse.EntityTag = ""; ServerResponse.SessionCode = SessionResult; @@ -929,7 +1011,86 @@ COServerResponse CardDAV2::GetServerEntityTagValue(std::string Location){ } -COServerResponse CardDAV2::GetContact(std::string Location){ +COServerResponse CardDAV2::GetContact(std::string Location, std::string *ContactData){ + + // 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, "GET"); + + 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){ + 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; + + (*ContactData) = PageData; + + return ServerResponse; } @@ -980,6 +1141,8 @@ void CardDAV2::SetupDefaultParametersNonSSL(bool DoAuthentication){ curl_easy_setopt(ConnectionSession, CURLOPT_NOSIGNAL, 1); curl_easy_setopt(ConnectionSession, CURLOPT_CUSTOMREQUEST, "GET"); curl_easy_setopt(ConnectionSession, CURLOPT_HTTPHEADER, nullptr); + curl_easy_setopt(ConnectionSession, CURLOPT_POSTFIELDS, nullptr); + curl_easy_setopt(ConnectionSession, CURLOPT_POSTFIELDSIZE, 0L); if (DoAuthentication == true){ curl_easy_setopt(ConnectionSession, CURLOPT_USERPWD, UsernamePassword.c_str()); @@ -1011,6 +1174,8 @@ void CardDAV2::SetupDefaultParametersSSL(bool DoAuthentication){ curl_easy_setopt(ConnectionSession, CURLOPT_VERBOSE, 1); curl_easy_setopt(ConnectionSession, CURLOPT_CUSTOMREQUEST, "GET"); curl_easy_setopt(ConnectionSession, CURLOPT_HTTPHEADER, nullptr); + curl_easy_setopt(ConnectionSession, CURLOPT_POSTFIELDS, nullptr); + curl_easy_setopt(ConnectionSession, CURLOPT_POSTFIELDSIZE, 0L); if (DoAuthentication == true){ curl_easy_setopt(ConnectionSession, CURLOPT_USERPWD, UsernamePassword.c_str()); diff --git a/source/carddav2/carddav2.h b/source/carddav2/carddav2.h index 729b24b..212c5a2 100644 --- a/source/carddav2/carddav2.h +++ b/source/carddav2/carddav2.h @@ -56,7 +56,7 @@ class CardDAV2 : public ConnectionObject { COServerResponse EditContact(std::string Location, std::string Data); COServerResponse DeleteContact(std::string Location, std::string EntityTag); COServerResponse GetServerEntityTagValue(std::string Location); - COServerResponse GetContact(std::string Location); + COServerResponse GetContact(std::string Location, std::string *ContactData); COContactList GetContactList(std::string SyncToken); bool CanDoProcessing(); diff --git a/source/connobject/ConnectionObject.h b/source/connobject/ConnectionObject.h index 96aa615..6685b70 100644 --- a/source/connobject/ConnectionObject.h +++ b/source/connobject/ConnectionObject.h @@ -89,7 +89,7 @@ class ConnectionObject{ virtual COServerResponse EditContact(std::string Location, std::string Data) {}; virtual COServerResponse DeleteContact(std::string Location, std::string EntityTag) {}; virtual COServerResponse GetServerEntityTagValue(std::string Location) {}; - virtual COServerResponse GetContact(std::string Location) {}; + virtual COServerResponse GetContact(std::string Location, std::string *PageData) {}; virtual COContactList GetContactList(std::string SyncToken) {}; virtual bool CanDoProcessing() {}; -- 2.39.5