Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Moved ConnectionObject and subroutines that use it into defined(__APPLE__).
[xestiaab/.git] / source / carddav / carddav.cpp
1 // carddav.cpp - Main CardDAV Object file.\r
2 //\r
3 // (c) 2012-2015 Xestia Software Development.\r
4 //\r
5 // This file is part of Xestia Address Book.\r
6 //\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
10 //\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
15 //\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
18 \r
19 #include "carddav.h"\r
20 #include "../version.h"\r
21 #include <wx/wx.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
26 #include <map>\r
27 #include <thread>\r
28 #include "../vcard/vcard.h"\r
29 #include "../common/dirs.h"\r
30 \r
31 size_t CardDAV::WritebackFunc(char *ptr, size_t size, size_t nmemb, wxString *stream){\r
32         \r
33         // Writeback function for the CardDAV object.\r
34         \r
35         wxString Data;\r
36         Data = wxString::FromUTF8((char *)ptr);\r
37         \r
38         stream->Append(Data);\r
39         \r
40         // Get the SSL engine pointer and trust if required on certain operating systems.\r
41         \r
42 #if defined(__APPLE__)\r
43         \r
44         const struct curl_tlssessioninfo *TLSInfo;\r
45         CURLcode TLSCode;\r
46         CURL *Connection = GetConnectionObject();\r
47         TLSCode = curl_easy_getinfo(Connection, CURLINFO_TLS_SSL_PTR, &TLSInfo);\r
48         \r
49         if (TLSInfo->internals != nullptr && TLSCode == CURLE_OK){\r
50                 SSLCopyPeerTrust((SSLContext*)TLSInfo->internals, &SecTrustObject);\r
51         }\r
52         \r
53 #endif\r
54         \r
55         return size * nmemb;\r
56 \r
57 }\r
58 \r
59 int ProgressFunc(void *clientdata, double TTDown, double NDown, double TTUp, double NUp){\r
60 \r
61         // Progress function for the CardDAV object.\r
62         \r
63         int ProgressRet;\r
64         \r
65         CardDAV *IncCardDAV = static_cast<CardDAV*>(clientdata);\r
66         ProgressRet = IncCardDAV->ProgressFuncProc(clientdata, TTDown, NDown, TTUp, NUp);\r
67         if (ProgressRet != 0){\r
68                 return 1;\r
69         }\r
70         return 0;\r
71 \r
72 }\r
73 \r
74 wxString CardDAV::ServerAddress;\r
75 int CardDAV::ServerPort;\r
76 wxString CardDAV::ServerUser;\r
77 wxString CardDAV::ServerPass;\r
78 wxString CardDAV::ServerPrefix;\r
79 wxString CardDAV::ServerAccount;\r
80 bool CardDAV::ServerSSL;\r
81 bool *CardDAV::ServerResult;\r
82 bool *CardDAV::ServerMonitor;\r
83 bool CardDAV::SSLStatus;\r
84 bool CardDAV::SSLVerified;\r
85 bool CardDAV::ValidResponse;\r
86 bool CardDAV::AuthPassed;\r
87 bool CardDAV::HasCalDAVSupport;\r
88 bool CardDAV::AbortConnection;\r
89 wxString CardDAV::ServerResponse;\r
90 wxString CardDAV::ServerMethod;\r
91 wxString CardDAV::ServerFilenameLocation;\r
92 wxString CardDAV::ServerUploadData;\r
93 wxString CardDAV::ETagData;\r
94 wxString CardDAV::ETagResult;\r
95 bool CardDAV::UploadMode;\r
96 bool CardDAV::EditMode;\r
97 long CardDAV::ItemIndex;\r
98 std::map<int, int> *CardDAV::ActivityListPtr;\r
99 char CardDAV::curlerrbuffer[CURL_ERROR_SIZE];\r
100 SSLCertCollection CardDAV::SSLCertCol;\r
101 int CardDAV::SSLErrorCode;\r
102 int CardDAV::ConnectionErrorCode;\r
103 wxString CardDAV::PageHeader;\r
104 wxString CardDAV::PageData;\r
105 CURLcode CardDAV::claconncode;\r
106 int CardDAV::HTTPErrorCode;\r
107 wxString CardDAV::ErrorMessage;\r
108 SSLCertCollection CardDAV::VerifyCertCollection;\r
109 bool CardDAV::AllowSelfSign;\r
110 #if defined(__APPLE__)\r
111 SSLContext *CardDAV::SSLContextPointer;\r
112 SecTrustRef CardDAV::SecTrustObject;\r
113 CURL *CardDAV::ConnectionObject;\r
114 #endif\r
115 \r
116 CardDAV::CardDAV(){\r
117         \r
118         // Setup the CardDAV object.\r
119         \r
120         ServerPort = 8080;\r
121         SSLStatus = FALSE;\r
122         SSLVerified = FALSE;\r
123         AuthPassed = FALSE;\r
124         ValidResponse = FALSE;\r
125         HasCalDAVSupport = FALSE;\r
126         SSLCertCol.SuccessCode = 0;\r
127         AllowSelfSign = FALSE;\r
128 \r
129         AbortConnection = FALSE;\r
130         UploadMode = FALSE;\r
131         EditMode = FALSE;\r
132         HTTPErrorCode = 0;\r
133         \r
134 }\r
135 \r
136 CardDAV::~CardDAV(){\r
137 \r
138         // Destroy the CardDAV object.\r
139         \r
140 }\r
141 \r
142 size_t UploadReadFunc(void *ptr, size_t size, size_t nmemb, void *userdata){\r
143 \r
144         \r
145         // Upload function for the CardDAV object.\r
146         \r
147         struct UploadDataStruc *UploadPtr = (struct UploadDataStruc *)userdata;\r
148 \r
149         if (UploadPtr->sizeleft){\r
150 \r
151                 //MeepMoop->sizeleft--;\r
152                 //return 1;\r
153 \r
154                 UploadPtr->sizeleft--;\r
155                 wxString wxSChar;\r
156 \r
157                 wxSChar = UploadPtr->readptr->Mid(UploadPtr->seek,1);\r
158 \r
159                 //*(char *)ptr = (char)wxSChar.mb_str();\r
160 \r
161                 strncpy((char *)ptr, (const char*)wxSChar.mb_str(wxConvUTF8), 1);\r
162 \r
163                 UploadPtr->seek++;\r
164 \r
165                 return 1;\r
166 \r
167         }\r
168 \r
169         return 0;\r
170 \r
171 }\r
172 \r
173 bool CardDAV::SetupConnection(wxString SvrAddress, int SvrPort, wxString SvrUser, wxString SvrPass, bool SvrSSL){\r
174 \r
175         // Setup the CardDAV connection without the prefix/account.\r
176         \r
177         ServerAddress = SvrAddress;\r
178         ServerPort = SvrPort;\r
179         ServerUser = SvrUser;\r
180         ServerPass = SvrPass;\r
181         ServerSSL = SvrSSL;\r
182         \r
183         return TRUE;\r
184 \r
185 }\r
186 \r
187 bool CardDAV::SetupConnection(wxString SvrAddress, int SvrPort, wxString SvrUser, wxString SvrPass, bool SvrSSL, wxString SvrPrefix, wxString SvrAccount){\r
188 \r
189         // Setup the CardDAV connection with the prefix/account.\r
190         \r
191         ServerAddress = SvrAddress;\r
192         ServerPort = SvrPort;\r
193         ServerUser = SvrUser;\r
194         ServerPass = SvrPass;\r
195         ServerSSL = SvrSSL;\r
196         ServerPrefix = SvrPrefix;\r
197         ServerAccount = SvrAccount;\r
198 \r
199         return TRUE;\r
200 \r
201 }\r
202 \r
203 bool CardDAV::SetupResultBools(bool *SvrResult, bool *SvrMonitor){\r
204 \r
205         // Setup the result booleans.\r
206         \r
207         ServerResult = SvrResult;\r
208         ServerMonitor = SvrMonitor;\r
209 \r
210         return TRUE;\r
211 \r
212 }\r
213 \r
214 bool CardDAV::HasValidResponse(){\r
215 \r
216         // Check that CardDAV server gave a valid response.\r
217         \r
218         return ValidResponse;\r
219 \r
220 }\r
221 \r
222 bool CardDAV::CanDoCardDAV(){\r
223 \r
224         // Check that the server has CardDAV support.\r
225         \r
226         return HasCalDAVSupport;\r
227 \r
228 }\r
229 \r
230 bool CardDAV::CanDoSSL(){\r
231 \r
232         // Check that the server can do SSL.\r
233         \r
234         return SSLStatus;\r
235 \r
236 }\r
237 \r
238 bool CardDAV::SSLVerify(){\r
239 \r
240         // Check that the server can verify SSL.\r
241         \r
242         return SSLVerified;\r
243 \r
244 }\r
245 \r
246 bool CardDAV::AbleToLogin(){\r
247 \r
248         // Check that the user is able to login.\r
249         \r
250         return AuthPassed;\r
251 \r
252 }\r
253 \r
254 bool CardDAV::IsSelfSigned(){\r
255 \r
256         // Check that self-signed certificates are allowed.\r
257         \r
258         return AllowSelfSign;\r
259 \r
260 }\r
261 \r
262 int CardDAV::ProgressFuncProc(void *clientdata, double TTUp, double NUp, double TTDown, double NDown){\r
263 \r
264         // Progress function processing.\r
265         \r
266         if (AbortConnection == TRUE){\r
267         \r
268                 return -1;\r
269         \r
270         } else {\r
271         \r
272                 return 0;\r
273         \r
274         }\r
275 \r
276 }\r
277 \r
278 void CardDAV::Abort(){\r
279 \r
280         // Abort (close) the connection.\r
281         \r
282         AbortConnection = TRUE;\r
283 \r
284 }\r
285 \r
286 SSLCertCollection CardDAV::GetSSLVerifyResults(){\r
287         \r
288         // Get the SSL verification results.\r
289         \r
290         return VerifyCertCollection;\r
291         \r
292 }\r
293 \r
294 void CardDAV::AllowSelfSignTest(bool AllowSelfSignIn){\r
295         \r
296         // Set the value to enable/disable SSL self-signed certificates.\r
297         \r
298         AllowSelfSign = AllowSelfSignIn;\r
299 }\r
300 \r
301 void CardDAV::GetSSLResults(){\r
302 \r
303         // Get the SSL results.\r
304 \r
305 }\r
306 \r
307 void CardDAV::SetServerFilename(wxString Filename){\r
308 \r
309         // Set the server filename.\r
310         \r
311         ServerFilenameLocation = Filename;\r
312 \r
313 }\r
314 \r
315 wxString CardDAV::GetPageData()\r
316 {\r
317 \r
318         // Get the server page data.\r
319         \r
320         return PageData;\r
321 \r
322 }\r
323 \r
324 wxString CardDAV::ETagValueResult(){\r
325 \r
326         // Get the ETag Result value.\r
327         \r
328         return ETagResult;\r
329 \r
330 }\r
331 \r
332 void CardDAV::SetupData(wxString Method, wxString FilenameLocation, wxString UploadData){\r
333 \r
334         // Setup the data to use with the CardDAV connection.\r
335         \r
336         ServerMethod = Method;\r
337         ServerFilenameLocation = FilenameLocation;\r
338         ServerUploadData = UploadData;\r
339 \r
340         // Check if ServerFilenameLocation has a / at \r
341         // the start and if not then append it.\r
342         \r
343         /*if (ServerFilenameLocation.Left(1) != wxT("/")){\r
344         \r
345                 // Not there so insert.\r
346                 \r
347                 ServerFilenameLocation = wxT("/") + ServerFilenameLocation;\r
348         \r
349         }*/\r
350 \r
351 }\r
352 \r
353 void CardDAV::SetupVariables(std::map<int, int> *actlist, int actindex){\r
354 \r
355         // Setup the variable pointers.\r
356         \r
357         ActivityListPtr = actlist;\r
358         ItemIndex = actindex;\r
359 \r
360 }\r
361 \r
362 wxString CardDAV::GetETagData(){\r
363 \r
364         // Get the ETag data.\r
365         \r
366         return ETagData;\r
367 \r
368 }\r
369 \r
370 void CardDAV::SetUploadMode(bool IncMode){\r
371 \r
372         // Set the upload mode.\r
373         \r
374         UploadMode = IncMode;\r
375 \r
376 }\r
377 \r
378 void CardDAV::SetEditMode(bool EditModeInc){\r
379 \r
380         // Set the edit mode.\r
381         \r
382         EditMode = EditModeInc;\r
383 \r
384 }\r
385 \r
386 int CardDAV::GetResultCode(){\r
387 \r
388         // Get the result code.\r
389         \r
390         return (int)claconncode;\r
391 \r
392 }\r
393 \r
394 int CardDAV::GetHTTPCode(){\r
395 \r
396         // Get the HTTP error code.\r
397         \r
398         return HTTPErrorCode;\r
399 \r
400 }\r
401 \r
402 wxString CardDAV::GetErrorBuffer(){\r
403 \r
404         // Get the error buffer.\r
405         \r
406         wxString ErrorBuffer = wxString::FromUTF8(curlerrbuffer);\r
407 \r
408         return ErrorBuffer;\r
409 \r
410 }\r
411 \r
412 SSLCertCollection CardDAV::BuildSSLCollection(CURL *conn){\r
413 \r
414         // Build and return the SSL collection.\r
415         \r
416         SSLCertCollection SSLCertInfo;\r
417 \r
418         // Grab the certificate data.\r
419 \r
420         union {\r
421                 struct curl_slist *certdata;\r
422                 struct curl_certinfo *certinfo;\r
423         } certptr;\r
424 \r
425         certptr.certdata = NULL;\r
426         \r
427         curl_easy_getinfo(conn, CURLINFO_CERTINFO, &certptr.certinfo);\r
428 \r
429         wxString CertPropName;\r
430         wxString CertPropValue;\r
431 \r
432         for (int i = 0; i < certptr.certinfo->num_of_certs; i++){\r
433 \r
434                 struct curl_slist *slist;\r
435                 SSLCertData SSLCertDataInc;\r
436                 \r
437                 for (slist = certptr.certinfo->certinfo[i]; slist; slist = slist->next){\r
438                         \r
439                         wxStringTokenizer CertDataInc(wxString::FromUTF8(slist->data), ":");\r
440                         \r
441                         // Get first token as the property name.\r
442                         \r
443                         CertPropName = CertDataInc.GetNextToken();\r
444                         \r
445                         // Get remaining tokens as the property value.\r
446                         \r
447                         while(CertDataInc.HasMoreTokens()){\r
448                         \r
449                                 CertPropValue.Append(CertDataInc.GetNextToken());\r
450                         \r
451                         }\r
452                         \r
453                         SSLCertDataInc.CertData.insert(std::make_pair(CertPropName, CertPropValue));\r
454                         CertPropName.clear();\r
455                         CertPropValue.clear();\r
456                         \r
457                 }\r
458         \r
459                 SSLCertInfo.SSLCollection.insert(std::make_pair(i, SSLCertDataInc));\r
460         \r
461         }\r
462         \r
463         return SSLCertInfo;\r
464 \r
465 }\r
466 \r
467 SSLCertCollection CardDAV::GetCertificateData(){\r
468 \r
469         // Pass on the collected certificate data.\r
470 \r
471         return SSLCertCol;\r
472 \r
473 }\r
474 \r
475 wxString CardDAV::GetErrorMessage(){\r
476 \r
477         // Get the error message.\r
478         \r
479         return ErrorMessage;\r
480 \r
481 }\r
482 \r
483 #if defined(__APPLE__)\r
484 \r
485 CURL* CardDAV::GetConnectionObject(){\r
486         \r
487         // Get the CardDAV connection object.\r
488         \r
489         return ConnectionObject;\r
490         \r
491 }\r
492 \r
493 void CardDAV::SetConnectionObject(CURL *ConnectionObjectIn){\r
494 \r
495         // Set the connection object.\r
496         \r
497         ConnectionObject = ConnectionObjectIn;\r
498         \r
499 }\r
500 \r
501 #endif
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