1 // carddav.cpp - Main CardDAV Object file.
\r
3 // (c) 2012-2015 Xestia Software Development.
\r
5 // This file is part of Xestia Address Book.
\r
7 // Xestia Address Book is free software: you can redistribute it and/or modify
\r
8 // it under the terms of the GNU General Public License as published by the
\r
9 // Free Software Foundation, version 3 of the license.
\r
11 // Xestia Address Book is distributed in the hope that it will be useful,
\r
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 // GNU General Public License for more details.
\r
16 // You should have received a copy of the GNU General Public License along
\r
17 // with Xestia Address Book. If not, see <http://www.gnu.org/licenses/>
\r
19 #include "carddav.h"
\r
20 #include "../version.h"
\r
22 #include <wx/tokenzr.h>
\r
23 #include <wx/ffile.h>
\r
24 #include <libxml/parser.h>
\r
25 #include <libxml/tree.h>
\r
28 #include "../vcard/vcard.h"
\r
29 #include "../common/dirs.h"
\r
31 size_t WritebackFunc(char *ptr, size_t size, size_t nmemb, wxString *stream){
\r
33 // Writeback function for the CardDAV object.
\r
36 Data = wxString::FromUTF8((char *)ptr);
\r
38 stream->Append(Data);
\r
40 return size * nmemb;
\r
44 int ProgressFunc(void *clientdata, double TTDown, double NDown, double TTUp, double NUp){
\r
46 // Progress function for the CardDAV object.
\r
50 CardDAV *IncCardDAV = static_cast<CardDAV*>(clientdata);
\r
51 ProgressRet = IncCardDAV->ProgressFuncProc(clientdata, TTDown, NDown, TTUp, NUp);
\r
52 if (ProgressRet != 0){
\r
59 wxString CardDAV::ServerAddress;
\r
60 int CardDAV::ServerPort;
\r
61 wxString CardDAV::ServerUser;
\r
62 wxString CardDAV::ServerPass;
\r
63 wxString CardDAV::ServerPrefix;
\r
64 wxString CardDAV::ServerAccount;
\r
65 bool CardDAV::ServerSSL;
\r
66 bool *CardDAV::ServerResult;
\r
67 bool *CardDAV::ServerMonitor;
\r
68 bool CardDAV::SSLStatus;
\r
69 bool CardDAV::SSLVerified;
\r
70 bool CardDAV::ValidResponse;
\r
71 bool CardDAV::AuthPassed;
\r
72 bool CardDAV::HasCalDAVSupport;
\r
73 bool CardDAV::AbortConnection;
\r
74 wxString CardDAV::ServerResponse;
\r
75 wxString CardDAV::ServerMethod;
\r
76 wxString CardDAV::ServerFilenameLocation;
\r
77 wxString CardDAV::ServerUploadData;
\r
78 wxString CardDAV::ETagData;
\r
79 wxString CardDAV::ETagResult;
\r
80 bool CardDAV::UploadMode;
\r
81 bool CardDAV::EditMode;
\r
82 long CardDAV::ItemIndex;
\r
83 std::map<int, int> *CardDAV::ActivityListPtr;
\r
84 char CardDAV::curlerrbuffer[CURL_ERROR_SIZE];
\r
85 SSLCertCollection CardDAV::SSLCertCol;
\r
86 int CardDAV::SSLErrorCode;
\r
87 int CardDAV::ConnectionErrorCode;
\r
88 wxString CardDAV::PageHeader;
\r
89 wxString CardDAV::PageData;
\r
90 CURLcode CardDAV::claconncode;
\r
91 int CardDAV::HTTPErrorCode;
\r
92 wxString CardDAV::ErrorMessage;
\r
93 SSLCertCollection CardDAV::VerifyCertCollection;
\r
94 bool CardDAV::AllowSelfSign;
\r
98 // Setup the CardDAV object.
\r
102 SSLVerified = FALSE;
\r
103 AuthPassed = FALSE;
\r
104 ValidResponse = FALSE;
\r
105 HasCalDAVSupport = FALSE;
\r
106 SSLCertCol.SuccessCode = 0;
\r
107 AllowSelfSign = FALSE;
\r
109 AbortConnection = FALSE;
\r
110 UploadMode = FALSE;
\r
116 CardDAV::~CardDAV(){
\r
118 // Destroy the CardDAV object.
\r
122 size_t UploadReadFunc(void *ptr, size_t size, size_t nmemb, void *userdata){
\r
125 // Upload function for the CardDAV object.
\r
127 struct UploadDataStruc *UploadPtr = (struct UploadDataStruc *)userdata;
\r
129 if (UploadPtr->sizeleft){
\r
131 //MeepMoop->sizeleft--;
\r
134 UploadPtr->sizeleft--;
\r
137 wxSChar = UploadPtr->readptr->Mid(UploadPtr->seek,1);
\r
139 //*(char *)ptr = (char)wxSChar.mb_str();
\r
141 strncpy((char *)ptr, (const char*)wxSChar.mb_str(wxConvUTF8), 1);
\r
153 bool CardDAV::SetupConnection(wxString SvrAddress, int SvrPort, wxString SvrUser, wxString SvrPass, bool SvrSSL){
\r
155 // Setup the CardDAV connection without the prefix/account.
\r
157 ServerAddress = SvrAddress;
\r
158 ServerPort = SvrPort;
\r
159 ServerUser = SvrUser;
\r
160 ServerPass = SvrPass;
\r
161 ServerSSL = SvrSSL;
\r
167 bool CardDAV::SetupConnection(wxString SvrAddress, int SvrPort, wxString SvrUser, wxString SvrPass, bool SvrSSL, wxString SvrPrefix, wxString SvrAccount){
\r
169 // Setup the CardDAV connection with the prefix/account.
\r
171 ServerAddress = SvrAddress;
\r
172 ServerPort = SvrPort;
\r
173 ServerUser = SvrUser;
\r
174 ServerPass = SvrPass;
\r
175 ServerSSL = SvrSSL;
\r
176 ServerPrefix = SvrPrefix;
\r
177 ServerAccount = SvrAccount;
\r
183 bool CardDAV::SetupResultBools(bool *SvrResult, bool *SvrMonitor){
\r
185 // Setup the result booleans.
\r
187 ServerResult = SvrResult;
\r
188 ServerMonitor = SvrMonitor;
\r
194 bool CardDAV::HasValidResponse(){
\r
196 // Check that CardDAV server gave a valid response.
\r
198 return ValidResponse;
\r
202 bool CardDAV::CanDoCardDAV(){
\r
204 // Check that the server has CardDAV support.
\r
206 return HasCalDAVSupport;
\r
210 bool CardDAV::CanDoSSL(){
\r
212 // Check that the server can do SSL.
\r
218 bool CardDAV::SSLVerify(){
\r
220 // Check that the server can verify SSL.
\r
222 return SSLVerified;
\r
226 bool CardDAV::AbleToLogin(){
\r
228 // Check that the user is able to login.
\r
234 bool CardDAV::IsSelfSigned(){
\r
236 // Check that self-signed certificates are allowed.
\r
238 return AllowSelfSign;
\r
242 int CardDAV::ProgressFuncProc(void *clientdata, double TTUp, double NUp, double TTDown, double NDown){
\r
244 // Progress function processing.
\r
246 if (AbortConnection == TRUE){
\r
258 void CardDAV::Abort(){
\r
260 // Abort (close) the connection.
\r
262 AbortConnection = TRUE;
\r
266 SSLCertCollection CardDAV::GetSSLVerifyResults(){
\r
268 // Get the SSL verification results.
\r
270 return VerifyCertCollection;
\r
274 void CardDAV::AllowSelfSignTest(bool AllowSelfSignIn){
\r
276 // Set the value to enable/disable SSL self-signed certificates.
\r
278 AllowSelfSign = AllowSelfSignIn;
\r
281 void CardDAV::GetSSLResults(){
\r
283 // Get the SSL results.
\r
287 void CardDAV::SetServerFilename(wxString Filename){
\r
289 // Set the server filename.
\r
291 ServerFilenameLocation = Filename;
\r
295 wxString CardDAV::GetPageData()
\r
298 // Get the server page data.
\r
304 wxString CardDAV::ETagValueResult(){
\r
306 // Get the ETag Result value.
\r
312 void CardDAV::SetupData(wxString Method, wxString FilenameLocation, wxString UploadData){
\r
314 // Setup the data to use with the CardDAV connection.
\r
316 ServerMethod = Method;
\r
317 ServerFilenameLocation = FilenameLocation;
\r
318 ServerUploadData = UploadData;
\r
320 // Check if ServerFilenameLocation has a / at
\r
321 // the start and if not then append it.
\r
323 if (ServerFilenameLocation.Left(1) != wxT("/")){
\r
325 // Not there so insert.
\r
327 ServerFilenameLocation = wxT("/") + ServerFilenameLocation;
\r
333 void CardDAV::SetupVariables(std::map<int, int> *actlist, int actindex){
\r
335 // Setup the variable pointers.
\r
337 ActivityListPtr = actlist;
\r
338 ItemIndex = actindex;
\r
342 wxString CardDAV::GetETagData(){
\r
344 // Get the ETag data.
\r
350 void CardDAV::SetUploadMode(bool IncMode){
\r
352 // Set the upload mode.
\r
354 UploadMode = IncMode;
\r
358 void CardDAV::SetEditMode(bool EditModeInc){
\r
360 // Set the edit mode.
\r
362 EditMode = EditModeInc;
\r
366 int CardDAV::GetResultCode(){
\r
368 // Get the result code.
\r
370 return (int)claconncode;
\r
374 int CardDAV::GetHTTPCode(){
\r
376 // Get the HTTP error code.
\r
378 return HTTPErrorCode;
\r
382 wxString CardDAV::GetErrorBuffer(){
\r
384 // Get the error buffer.
\r
386 wxString ErrorBuffer = wxString::FromUTF8(curlerrbuffer);
\r
388 return ErrorBuffer;
\r
392 SSLCertCollection CardDAV::BuildSSLCollection(CURL *conn){
\r
394 // Build and return the SSL collection.
\r
396 SSLCertCollection SSLCertInfo;
\r
398 // Grab the certificate data.
\r
401 struct curl_slist *certdata;
\r
402 struct curl_certinfo *certinfo;
\r
405 certptr.certdata = NULL;
\r
407 curl_easy_getinfo(conn, CURLINFO_CERTINFO, &certptr.certinfo);
\r
409 wxString CertPropName;
\r
410 wxString CertPropValue;
\r
412 for (int i = 0; i < certptr.certinfo->num_of_certs; i++){
\r
414 struct curl_slist *slist;
\r
415 SSLCertData SSLCertDataInc;
\r
417 for (slist = certptr.certinfo->certinfo[i]; slist; slist = slist->next){
\r
419 wxStringTokenizer CertDataInc(wxString::FromUTF8(slist->data), ":");
\r
421 // Get first token as the property name.
\r
423 CertPropName = CertDataInc.GetNextToken();
\r
425 // Get remaining tokens as the property value.
\r
427 while(CertDataInc.HasMoreTokens()){
\r
429 CertPropValue.Append(CertDataInc.GetNextToken());
\r
433 SSLCertDataInc.CertData.insert(std::make_pair(CertPropName, CertPropValue));
\r
434 CertPropName.clear();
\r
435 CertPropValue.clear();
\r
439 SSLCertInfo.SSLCollection.insert(std::make_pair(i, SSLCertDataInc));
\r
443 return SSLCertInfo;
\r
447 SSLCertCollection CardDAV::GetCertificateData(){
\r
449 // Pass on the collected certificate data.
\r
455 wxString CardDAV::GetErrorMessage(){
\r
457 // Get the error message.
\r
459 return ErrorMessage;
\r