Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
188e7721ea2bc34924f99483712cf1ef4d4ca1e2
[xestiaab/.git] / source / carddav / carddav-contactlist.cpp
1 #include "carddav.h"
2 #include "../version.h"
3 #include <wx/wx.h>
4 #include <wx/tokenzr.h>
5 #include <wx/ffile.h>
6 #include <libxml/parser.h>
7 #include <libxml/tree.h>
8 #include <map>
9 #include <thread>
10 #include "../vcard/vcard.h"
11 #include "../common/dirs.h"
13 ContactListData CardDAV::GetContactList(wxString SyncTokenInc){
15         ContactListData ContactListFinal;
16         std::map<wxString,FileSyncData> ContactList;
17         
18         PageData.Clear();
19         PageHeader.Clear();
21         SSLStatus = TRUE;
22         AuthPassed = TRUE;
23         AbortConnection = FALSE;
25         CURL *conn;
26         wxString ServerAddressURL;
27         wxString ServerAuth;
28         wxString ServerAddressSSL;
29         wxString ServerAddressNormal;
31         conn = curl_easy_init();
32         
33         struct CardDAVCURLPasser {
34         
35                 CardDAV *Data;
36                 bool HeaderMode = TRUE;
37         
38         } CardDAVHeader, CardDAVFooter;
40         CardDAVHeader.Data = this;
41         CardDAVHeader.HeaderMode = TRUE;
42         
43         CardDAVFooter.Data = this;
44         CardDAVFooter.HeaderMode = FALSE;
46         wxString Data1;
47         wxString Data2;
48         
49         ServerAddressURL = ServerAddress + wxT(":") + wxString::Format(wxT("%i"), ServerPort) + wxT("/") + ServerPrefix + wxT("/");
50         ServerAddressSSL = wxT("https://") + ServerAddressURL;
51         ServerAddressNormal = wxT("http://") + ServerAddressURL;
52         
53         ServerAuth = ServerUser + wxT(":") + ServerPass;
54         
55         // Load the sync token file (if it exists).
56         
57         wxCharBuffer SyncDataBuffer;
58         wxString SyncData;
59         
60         SyncData.Clear();
61         
62         SyncTokenInc.Trim();
63         
64         if (!SyncTokenInc.IsEmpty()){
65                 
66                 SyncData = wxT("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n");
67                 SyncData.Append(wxT("<D:sync-collection xmlns:D=\"DAV:\"\n"));
68                 SyncData.Append(wxT(" xmlns:C=\"urn:ietf:params:xml:ns:carddav\">\n"));
69                 SyncData.Append(wxT("<D:sync-token>"));
70                 //SyncData.Trim();
71                 //SyncData.Append(wxT("data:,00378c55-1f44-44a2-a255-84f6560b5cac_580"));
72                 SyncData.Append(SyncTokenInc);
73                 //SyncData.Trim();
74                 SyncData.Append(wxT("</D:sync-token>\n"));
75                 SyncData.Append(wxT("<D:sync-level>1</D:sync-level>\n"));
76                 SyncData.Append(wxT("<D:prop>\n"));
77                 SyncData.Append(wxT("   <D:getetag/>\n"));
78                 SyncData.Append(wxT("</D:prop>\n"));
79                 SyncData.Append(wxT("</D:sync-collection>"));
80                 
81                 SyncDataBuffer = SyncData.ToUTF8();
82         
83         } else {
84                 
85                 SyncData = wxT("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
86                 SyncData.Append(wxT("<D:sync-collection xmlns:D=\"DAV:\""));
87                 SyncData.Append(wxT(" xmlns:C=\"urn:ietf:params:xml:ns:carddav\">\n"));
88                 SyncData.Append(wxT("<D:sync-token/>\n"));
89                 SyncData.Append(wxT("<D:sync-level>1</D:sync-level>\n"));
90                 SyncData.Append(wxT("<D:prop>\n"));
91                 SyncData.Append(wxT("   <D:getetag/>\n"));
92                 SyncData.Append(wxT("</D:prop>\n"));
93                 SyncData.Append(wxT("</D:sync-collection>\n"));
94                 
95                 SyncDataBuffer = SyncData.ToUTF8();
97         }
98         
99         //static const char* query = SyncData.mb_str();
100         
101         /*char *query = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n\
102                 <D:sync-collection xmlns:D=\"DAV:\"\n\
103                  xmlns:C=\"urn:ietf:params:xml:ns:carddav\">\n\
104                 <D:sync-token>data:,00378c55-1f44-44a2-a255-84f6560b5cac_580</D:sync-token>\n\
105                 <D:sync-level>1</D:sync-level>\n\
106                 <D:prop>\n\
107                         <D:getetag/>\n\
108                 </D:prop>\n\
109         </D:sync-collection>\n";*/
110         const char* query = SyncDataBuffer.data();
111         
112         // Try SSL first.
114         std::map<int,int>::iterator ActIter;
115         struct UploadDataStruc UploadData;
116         
117         ActIter = ActivityListPtr->find((int)ItemIndex);
119         curl_slist *slist = NULL;       
121         slist = curl_slist_append(slist, "Depth: 1");
123         if (ServerSSL){
125                 wxString ServerCertFilename;
126                 bool MatchingCert = FALSE;
128                 curl_easy_setopt(conn, CURLOPT_URL, (const char*)ServerAddressSSL.mb_str(wxConvUTF8));
129                 curl_easy_setopt(conn, CURLOPT_NOPROGRESS, 0);
130                 curl_easy_setopt(conn, CURLOPT_HTTPAUTH, CURLAUTH_ANYSAFE);
131                 curl_easy_setopt(conn, CURLOPT_TIMEOUT, 60);
132                 curl_easy_setopt(conn, CURLOPT_FAILONERROR, TRUE);
133                 curl_easy_setopt(conn, CURLOPT_USERAGENT, XSDAB_USERAGENT);
134                 curl_easy_setopt(conn, CURLOPT_USERPWD, (const char*)ServerAuth.mb_str(wxConvUTF8));
135                 curl_easy_setopt(conn, CURLOPT_WRITEFUNCTION, WritebackFunc);
136                 curl_easy_setopt(conn, CURLOPT_WRITEDATA, &PageData);
137                 curl_easy_setopt(conn, CURLOPT_WRITEHEADER, &PageHeader);
138                 curl_easy_setopt(conn, CURLOPT_PROGRESSDATA, this);
139                 curl_easy_setopt(conn, CURLOPT_PROGRESSFUNCTION, ProgressFunc);
140                 curl_easy_setopt(conn, CURLOPT_CUSTOMREQUEST, "REPORT");
141                 curl_easy_setopt(conn, CURLOPT_NOSIGNAL, 1);
142                 curl_easy_setopt(conn, CURLOPT_HTTPHEADER, slist);
143                 curl_easy_setopt(conn, CURLOPT_CERTINFO, 1);
145                 ServerCertFilename = GetAccountDir(ServerAccount, TRUE);
147                 if (wxFile::Exists(ServerCertFilename) == TRUE){
148                 
149                         curl_easy_setopt(conn, CURLOPT_SSL_VERIFYPEER, 1);
150                         curl_easy_setopt(conn, CURLOPT_SSL_VERIFYHOST, 2);
151                         curl_easy_setopt(conn, CURLOPT_CAINFO, (const char*)ServerCertFilename.mb_str(wxConvUTF8));
152                 
153                 }
155                 //UploadData.readptr = &CardDAVDataQuery;
156                 //UploadData.sizeleft = CardDAVDataQuery.Len();
157                 //curl_easy_setopt(conn, CURLOPT_UPLOAD, 1);
158                 //curl_easy_setopt(conn, CURLOPT_READDATA, &UploadData);
159                 //curl_easy_setopt(conn, CURLOPT_READFUNCTION, UploadReadFunc);
160                 
161                 //curl_easy_setopt(conn, CURLOPT_WRITEFUNCTION, writefunc);
162                 curl_easy_setopt(conn, CURLOPT_POSTFIELDS, query);
163                 curl_easy_setopt(conn, CURLOPT_POSTFIELDSIZE, strlen(query));
165                 claconncode = (curl_easy_perform(conn));
167                 // If CURLE_PEER_FAILED_VERIFICATION is returned, retry without
168                 // the local certificate in use.
170                 if (claconncode == CURLE_PEER_FAILED_VERIFICATION){
171                         
172                         curl_easy_cleanup(conn);
173                         conn = curl_easy_init();
174                         
175                         curl_easy_setopt(conn, CURLOPT_URL, (const char*)ServerAddressSSL.mb_str(wxConvUTF8));
176                         curl_easy_setopt(conn, CURLOPT_NOPROGRESS, 0);
177                         curl_easy_setopt(conn, CURLOPT_HTTPAUTH, CURLAUTH_ANYSAFE);
178                         curl_easy_setopt(conn, CURLOPT_TIMEOUT, 60);
179                         curl_easy_setopt(conn, CURLOPT_FAILONERROR, TRUE);
180                         curl_easy_setopt(conn, CURLOPT_USERAGENT, XSDAB_USERAGENT);
181                         curl_easy_setopt(conn, CURLOPT_USERPWD, (const char*)ServerAuth.mb_str(wxConvUTF8));
182                         curl_easy_setopt(conn, CURLOPT_WRITEFUNCTION, WritebackFunc);
183                         curl_easy_setopt(conn, CURLOPT_WRITEDATA, &PageData);
184                         curl_easy_setopt(conn, CURLOPT_WRITEHEADER, &PageHeader);
185                         curl_easy_setopt(conn, CURLOPT_PROGRESSDATA, this);
186                         curl_easy_setopt(conn, CURLOPT_PROGRESSFUNCTION, ProgressFunc);
187                         curl_easy_setopt(conn, CURLOPT_CUSTOMREQUEST, "REPORT");
188                         curl_easy_setopt(conn, CURLOPT_NOSIGNAL, 1);
189                         curl_easy_setopt(conn, CURLOPT_HTTPHEADER, slist);
190                         curl_easy_setopt(conn, CURLOPT_CERTINFO, 1);
191                         curl_easy_setopt(conn, CURLOPT_POSTFIELDS, query);
192                         curl_easy_setopt(conn, CURLOPT_POSTFIELDSIZE, strlen(query));
193                         
194                         claconncode = (curl_easy_perform(conn));
195                         
196                         // If claconncode is CURLE_OK then delete the certificate file as that
197                         // is no longer needed.
198                         
199                         if (claconncode == CURLE_OK){
200                         
201                                 // Delete the certificate file.
202                                 
203                                 wxRemoveFile(ServerCertFilename);
204                         
205                         }
206                 
207                 }
209                 // Check if it fails with a CURLE_SSL_CACERT then compare
210                 // the certificates as PEM files.
211                 
212                 if (claconncode == CURLE_SSL_CACERT && wxFile::Exists(ServerCertFilename) == TRUE){
213                 
214                         //curl_easy_cleanup(conn);
215                         //conn = curl_easy_init();
217                         CURL *sslerrconn;
218                         sslerrconn = curl_easy_init();
219                         CURLcode sslerrconncode;
221                         //claconncode = (curl_easy_perform(conn));
223                         wxString ServerAddressOnly = wxT("https://") + ServerAddress + wxT(":") + wxString::Format(wxT("%i"), ServerPort) + wxT("/");
225                         curl_easy_setopt(sslerrconn, CURLOPT_URL, (const char*)ServerAddressOnly.mb_str(wxConvUTF8));
226                         curl_easy_setopt(sslerrconn, CURLOPT_NOPROGRESS, 0);
227                         curl_easy_setopt(sslerrconn, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
228                         curl_easy_setopt(sslerrconn, CURLOPT_TIMEOUT, 60);
229                         curl_easy_setopt(sslerrconn, CURLOPT_FAILONERROR, TRUE);
230                         curl_easy_setopt(sslerrconn, CURLOPT_USERAGENT, XSDAB_USERAGENT);
231                         curl_easy_setopt(sslerrconn, CURLOPT_WRITEFUNCTION, WritebackFunc);
232                         curl_easy_setopt(sslerrconn, CURLOPT_WRITEDATA, &PageData);
233                         curl_easy_setopt(sslerrconn, CURLOPT_WRITEHEADER, &PageHeader);
234                         curl_easy_setopt(sslerrconn, CURLOPT_PROGRESSDATA, this);
235                         curl_easy_setopt(sslerrconn, CURLOPT_PROGRESSFUNCTION, ProgressFunc);
236                         curl_easy_setopt(sslerrconn, CURLOPT_NOSIGNAL, 1);
237                         curl_easy_setopt(sslerrconn, CURLOPT_SSL_VERIFYPEER, 0);
238                         curl_easy_setopt(sslerrconn, CURLOPT_CERTINFO, 1);
239                 
240                         wxString SSLLocalData;
241                         wxString SSLServerData;
242                 
243                         sslerrconncode = (curl_easy_perform(sslerrconn));
244                 
245                         SSLCertCol = BuildSSLCollection(sslerrconn);
246                         std::map<int, SSLCertData>::iterator SSLCDIter = SSLCertCol.SSLCollection.find(0);
247                         std::multimap<wxString,wxString>::iterator SSLDataIter = SSLCDIter->second.CertData.find(wxT("Cert"));
248                         
249                         wxFFile SSLLocalFile;
250                         
251 #if wxABI_VERSION < 20900
252                         SSLLocalFile.Open(ServerCertFilename.c_str(), wxT("r"));
253 #else
254                         SSLLocalFile.Open(ServerCertFilename, wxT("r"));
255 #endif  
256         
257                         // Load the recovery database for tasks not done.
258         
259                         if (SSLLocalFile.IsOpened() == TRUE){
261                         // Check if we are using wxWidgets version 2.8 or less and
262                         // execute the required command accordingly.
263         
264                                 SSLLocalFile.ReadAll(&SSLLocalData, wxConvAuto());
265                 
266         
267                         }
268                         
269                         SSLServerData = SSLDataIter->second;
270                         
271                         if (SSLLocalData == SSLServerData){
272                         
273                                 // Server key matches with local key so retry with CURLOPT_SSL_VERIFYPEER
274                                 // and CURLOPT_SSL_VERIFYHOST off.
275                         
276                                 curl_easy_cleanup(conn);
277                                 conn = curl_easy_init();
278                                 
279                                 PageHeader.clear();
280                                 PageData.clear();
281                                 
282                                 curl_easy_setopt(conn, CURLOPT_URL, (const char*)ServerAddressSSL.mb_str(wxConvUTF8));
283                                 curl_easy_setopt(conn, CURLOPT_NOPROGRESS, 0);
284                                 curl_easy_setopt(conn, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
285                                 curl_easy_setopt(conn, CURLOPT_TIMEOUT, 60);
286                                 curl_easy_setopt(conn, CURLOPT_FAILONERROR, TRUE);
287                                 curl_easy_setopt(conn, CURLOPT_USERAGENT, XSDAB_USERAGENT);
288                                 curl_easy_setopt(conn, CURLOPT_USERPWD, (const char*)ServerAuth.mb_str(wxConvUTF8));
289                                 curl_easy_setopt(conn, CURLOPT_WRITEFUNCTION, WritebackFunc);
290                                 curl_easy_setopt(conn, CURLOPT_WRITEDATA, &PageData);
291                                 curl_easy_setopt(conn, CURLOPT_WRITEHEADER, &PageHeader);
292                                 curl_easy_setopt(conn, CURLOPT_PROGRESSDATA, this);
293                                 curl_easy_setopt(conn, CURLOPT_PROGRESSFUNCTION, ProgressFunc);
294                                 curl_easy_setopt(conn, CURLOPT_CUSTOMREQUEST, "REPORT");
295                                 curl_easy_setopt(conn, CURLOPT_NOSIGNAL, 1);
296                                 curl_easy_setopt(conn, CURLOPT_HTTPHEADER, slist);
297                                 curl_easy_setopt(conn, CURLOPT_CERTINFO, 1);
298                                 curl_easy_setopt(conn, CURLOPT_POSTFIELDS, query);
299                                 curl_easy_setopt(conn, CURLOPT_POSTFIELDSIZE, strlen(query));
300                                 curl_easy_setopt(conn, CURLOPT_SSL_VERIFYHOST, 0);
301                                 curl_easy_setopt(conn, CURLOPT_SSL_VERIFYPEER, 0);
302                         
303                                 claconncode = (curl_easy_perform(conn));
304                                 
305                                 MatchingCert = TRUE;
306                         
307                         }
308                         
309                         if (MatchingCert == FALSE){
310                 
311                                 claconncode = CURLE_SSL_CACERT;
312                                 return ContactListFinal;
313                 
314                         }
315                         
316                         curl_easy_cleanup(sslerrconn);
317                 
318                 }
320                 // Sort out SSL error.
321                 
322                 // When SSL cert error occurs, connect again and fetch certificates.
323                 // Display a message to the user explaining that an invalid
324                 // certificate has been given and let the user decide what
325                 // to do next.
327                 if (claconncode == CURLE_OK){
329                 } else if (claconncode == CURLE_SSL_CACERT || claconncode == CURLE_PEER_FAILED_VERIFICATION){
330                 
331                         CURL *sslerrconn;
332                         sslerrconn = curl_easy_init();
333                         CURLcode sslerrconncode;
334                 
335                         wxString ServerAddressOnly = wxT("https://") + ServerAddress + wxT(":") + wxString::Format(wxT("%i"), ServerPort) + wxT("/");
336                 
337                         // Replace conn with sslerrconn!
338                 
339                         curl_easy_setopt(sslerrconn, CURLOPT_URL, (const char*)ServerAddressOnly.mb_str(wxConvUTF8));
340                         curl_easy_setopt(sslerrconn, CURLOPT_NOPROGRESS, 0);
341                         curl_easy_setopt(sslerrconn, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
342                         curl_easy_setopt(sslerrconn, CURLOPT_TIMEOUT, 60);
343                         curl_easy_setopt(sslerrconn, CURLOPT_FAILONERROR, TRUE);
344                         curl_easy_setopt(sslerrconn, CURLOPT_USERAGENT, XSDAB_USERAGENT);
345                         curl_easy_setopt(sslerrconn, CURLOPT_WRITEFUNCTION, WritebackFunc);
346                         curl_easy_setopt(sslerrconn, CURLOPT_WRITEDATA, &PageData);
347                         curl_easy_setopt(sslerrconn, CURLOPT_WRITEHEADER, &PageHeader);
348                         curl_easy_setopt(sslerrconn, CURLOPT_PROGRESSDATA, this);
349                         curl_easy_setopt(sslerrconn, CURLOPT_PROGRESSFUNCTION, ProgressFunc);
350                         curl_easy_setopt(sslerrconn, CURLOPT_NOSIGNAL, 1);
351                         curl_easy_setopt(sslerrconn, CURLOPT_SSL_VERIFYPEER, 0);
352                         curl_easy_setopt(sslerrconn, CURLOPT_CERTINFO, 1);
353                 
354                         sslerrconncode = (curl_easy_perform(sslerrconn));
356                         SSLCertCol = BuildSSLCollection(sslerrconn);
357                         SSLCertCol.SuccessCode = 1;
359                         return ContactListFinal;
360                 
361                 } else if (claconncode == CURLE_HTTP_RETURNED_ERROR){
362                 
363                         fprintf(stderr, "curl_easy_perform() failed: %s\n",
364                                         curl_easy_strerror(claconncode));
365                         int http_code = 0;
366                         curl_easy_getinfo(conn, CURLINFO_RESPONSE_CODE, &http_code);
367                         fprintf(stderr, "Error code was: %d\n", http_code);
368                                         
369                         return ContactListFinal;
370                 
371                 } else {
373                         fprintf(stderr, "curl_easy_perform() failed: %s\n",
374                                         curl_easy_strerror(claconncode));
375                         int http_code = 0;
376                         curl_easy_getinfo(conn, CURLINFO_RESPONSE_CODE, &http_code);
377                         fprintf(stderr, "Error code was: %d\n", http_code);
379                         return ContactListFinal;
381                 }
383                 SSLCertCol = BuildSSLCollection(conn);
385         } else {
386         
387         // No SSL.
388                 
389                 wxString EmptyString;
390                 
391                 curl_easy_setopt(conn, CURLOPT_URL, (const char*)ServerAddressNormal.mb_str(wxConvUTF8));
392                 curl_easy_setopt(conn, CURLOPT_NOPROGRESS, 0);
393                 curl_easy_setopt(conn, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
394                 curl_easy_setopt(conn, CURLOPT_TIMEOUT, 60);
395                 curl_easy_setopt(conn, CURLOPT_FAILONERROR, TRUE);
396                 curl_easy_setopt(conn, CURLOPT_USERAGENT, XSDAB_USERAGENT);
397                 curl_easy_setopt(conn, CURLOPT_USERPWD, (const char*)ServerAuth.mb_str(wxConvUTF8));
398                 curl_easy_setopt(conn, CURLOPT_WRITEFUNCTION, WritebackFunc);
399                 curl_easy_setopt(conn, CURLOPT_WRITEDATA, &PageData);
400                 curl_easy_setopt(conn, CURLOPT_WRITEHEADER, &PageHeader);
401                 curl_easy_setopt(conn, CURLOPT_PROGRESSDATA, this);
402                 curl_easy_setopt(conn, CURLOPT_PROGRESSFUNCTION, ProgressFunc);
403                 curl_easy_setopt(conn, CURLOPT_CUSTOMREQUEST, "REPORT");
404                 curl_easy_setopt(conn, CURLOPT_NOSIGNAL, 1);
405                 curl_easy_setopt(conn, CURLOPT_HTTPHEADER, slist);
407                 //UploadData.readptr = &CardDAVDataQuery;
408                 //UploadData.sizeleft = CardDAVDataQuery.Len();
409                 //curl_easy_setopt(conn, CURLOPT_UPLOAD, 1);
410                 //curl_easy_setopt(conn, CURLOPT_READDATA, &UploadData);
411                 //curl_easy_setopt(conn, CURLOPT_READFUNCTION, UploadReadFunc);
412                 
413                 //curl_easy_setopt(conn, CURLOPT_WRITEFUNCTION, writefunc);
414                 curl_easy_setopt(conn, CURLOPT_POSTFIELDS, query);
415                 curl_easy_setopt(conn, CURLOPT_POSTFIELDSIZE, strlen(query));           
416                 
417                 PageData.Clear();
418                 PageHeader.Clear();
419                 
420                 claconncode = (curl_easy_perform(conn));
422                 if (claconncode == CURLE_OK){
426                 } else if (claconncode == CURLE_HTTP_RETURNED_ERROR){
427                 
428                         fprintf(stderr, "curl_easy_perform() failed: %s\n",
429                                         curl_easy_strerror(claconncode));
430                         int http_code = 0;
431                         curl_easy_getinfo(conn, CURLINFO_RESPONSE_CODE, &http_code);
432                         fprintf(stderr, "Error code was: %i\n", http_code);
433                                         
434                         return ContactListFinal;
435                         
436                 } else {
438                         fprintf(stderr, "curl_easy_perform() failed: %s\n",
439                                         curl_easy_strerror(claconncode));
440                         int http_code = 0;
441                         curl_easy_getinfo(conn, CURLINFO_RESPONSE_CODE, &http_code);
442                         fprintf(stderr, "Error code was: %i\n", http_code);
443                                 
444                         return ContactListFinal;
446                 }
447                 
448         }
450         xmlDocPtr xmlCardDAVDoc;
451         xmlCardDAVDoc = xmlReadMemory(PageData.mb_str(wxConvUTF8), (int)PageData.Len(), "noname.xml", NULL, 0);
453         xmlNodePtr nodeLevel1;
454         xmlNodePtr nodeLevel2;
455         xmlNodePtr nodeLevel3;
456         xmlNodePtr nodeLevel4;
457         xmlNodePtr nodeLevel5;
458         xmlNodePtr nodeLevel6;
459         
460         xmlNodePtr nodeStatusLv1;
461         xmlNodePtr nodeStatusLv2;
463         std::map<wxString,wxString> xmlDataMap;
464         std::map<wxString,wxString> ServerETagData;
466         wxString DataFilename;
467         wxString DataSyncToken;
468         int DataFileStatus;
469         wxString ETagData;
470         bool SyncTokenFound = FALSE;
472         std::string xmlStringSafe;
474         // Tranverse through the catacombs of the response to get our ETag for the file and
475         // the server syncronisation token.
477         // Start by getting all the server ETag data.
479         for (nodeLevel1 = xmlCardDAVDoc->children;
480                 nodeLevel1 != NULL;
481                 nodeLevel1 = nodeLevel1->next)
482         {
484                 for (nodeLevel2 = nodeLevel1->children;
485                         nodeLevel2 != NULL;
486                         nodeLevel2 = nodeLevel2->next)
487                 {
489                         for (nodeLevel3 = nodeLevel2->children;
490                         nodeLevel3 != NULL;
491                         nodeLevel3 = nodeLevel3->next)
492                         {
494                                 DataFileStatus = 0;
495                                 bool HREFFound = FALSE;
496                                 bool ETagFound = FALSE;
497                                 bool HTTPStatus = FALSE;
499                                 if (!xmlStrcmp(nodeLevel3->name, (const xmlChar *)"href") ||
500                                 !xmlStrcmp(nodeLevel3->name, (const xmlChar *)"d:href") ||
501                                 !xmlStrcmp(nodeLevel3->name, (const xmlChar *)"D:href")
502                                 ){
504                                         // Get the filename.
505                                         
506                                         for (nodeLevel4 = nodeLevel3->children;
507                                         nodeLevel4 != NULL;
508                                         nodeLevel4 = nodeLevel4->next)
509                                         {
510                                         
511                                                 if (!xmlStrcmp(nodeLevel4->name, (const xmlChar *)"text") ||
512                                                 !xmlStrcmp(nodeLevel4->name, (const xmlChar *)"d:text") ||
513                                                 !xmlStrcmp(nodeLevel4->name, (const xmlChar *)"D:text")
514                                                 ){
515                                                 
516                                                         DataFilename = wxString::FromUTF8((const char*)nodeLevel4->content);
517                                                         wxStringTokenizer wSTDFilename(DataFilename, wxT("/"));
518                                                 
519                                                         while (wSTDFilename.HasMoreTokens()){
520                                                         
521                                                                 DataFilename = wSTDFilename.GetNextToken();
522                                                         
523                                                         }
524                                                         
525                                                         HREFFound = TRUE;
526                                                 
527                                                 }
528                                                 
529         
530                                         
531                                         }
532                                         
534                                 } else {
536                                         for (nodeLevel4 = nodeLevel3->children;
537                                         nodeLevel4 != NULL;
538                                         nodeLevel4 = nodeLevel4->next)
539                                         {
541                                                 for (nodeStatusLv1 = nodeLevel3->children;
542                                                         nodeStatusLv1 != NULL;
543                                                         nodeStatusLv1 = nodeStatusLv1->next)
544                                                 {
546                                                         if (wxString::FromUTF8((const char*)nodeStatusLv1->content) == wxT("HTTP/1.1 404 Not Found")){
547                 
548                                                                 DataFileStatus = 2;
549                                                                                 
550                                                                 HTTPStatus = TRUE;
551                                                                                 
552                                                         }
553                                         
554                                                         if ((!xmlStrcmp(nodeStatusLv1->name, (const xmlChar *)"status") ||
555                                                         !xmlStrcmp(nodeStatusLv1->name, (const xmlChar *)"d:status") ||
556                                                         !xmlStrcmp(nodeStatusLv1->name, (const xmlChar *)"D:status")) && HTTPStatus == FALSE)
557                                                         {
559                                                                 // Get the filename.
560                                         
561                                                                 for (nodeStatusLv2 = nodeStatusLv1->children;
562                                                                 nodeStatusLv2 != NULL;
563                                                                 nodeStatusLv2 = nodeStatusLv2->next)
564                                                                 {
565                                         
566                                                                         if (!xmlStrcmp(nodeStatusLv2->name, (const xmlChar *)"text") ||
567                                                                         !xmlStrcmp(nodeStatusLv2->name, (const xmlChar *)"d:text") ||
568                                                                         !xmlStrcmp(nodeStatusLv2->name, (const xmlChar *)"D:text")
569                                                                         ){
571                                                                                 if (wxString::FromUTF8((const char*)nodeStatusLv2->content) == wxT("HTTP/1.1 200 OK")){
572                                                                         
573                                                                                         DataFileStatus = 1;
574                                                                                         
575                                                                                         HTTPStatus = TRUE;
576                                                                         
577                                                                                 // This is currently in a WebDAV draft and may hopefully be enabled when this changes.
578                                                                         
579                                                                                 //} else if (wxString::FromUTF8((const char*)nodeStatusLv2->content) == wxT("HTTP/1.1 201 Created")){
580                                                                                 
581                                                                                 //      DataFileStatus = 0;
582                                                                                 
583                                                                                 }
584                                                 
585                                                                         }
586                                                 
587         
588                                         
589                                                                 }
590                                                         
591                                                         }
593                                         
594                                                 }
595                                                 
596                                                 for (nodeLevel5 = nodeLevel4->children;
597                                                 nodeLevel5 != NULL;
598                                                 nodeLevel5 = nodeLevel5->next)
599                                                 {
601                                                         if (!xmlStrcmp(nodeLevel5->name, (const xmlChar *)"getetag") ||
602                                                         !xmlStrcmp(nodeLevel5->name, (const xmlChar *)"d:getetag") ||
603                                                         !xmlStrcmp(nodeLevel5->name, (const xmlChar *)"D:getetag")
604                                                         ){
606                                                                 for (nodeLevel6 = nodeLevel5->children;
607                                                                 nodeLevel6 != NULL;
608                                                                 nodeLevel6 = nodeLevel6->next)
609                                                                 {
611                                                                         // Strip the quotes from the ETag.
612                                                 
613                                                                         ETagData = wxString::FromUTF8((const char*)nodeLevel6->content);
614                                                                         if (ETagData.Mid(0, 1) == wxT("\"") && ETagData.Mid((ETagData.Len() - 1), 1) == wxT("\"")){
615                                                 
616                                                                                 ETagData.Remove(0, 1);
617                                                                                 ETagData.RemoveLast();
618                                                 
619                                                                         }
620                                                                 
621                                                                         ETagFound = TRUE;
623                                                                 }
624                                                                 
625                                                         }
627                                                 }       
629                                         }
631                                 }
633                                 if (HREFFound == TRUE && ETagFound == TRUE && HTTPStatus == TRUE){
634                                 
635                                         // Add to the map data.
636                                         
637                                         FileSyncData SData;
638                                         
639                                         SData.ETagData = ETagData;
640                                         SData.DataFlag = DataFileStatus;
641                                         
642                                         ContactListFinal.ListData.insert(std::make_pair(DataFilename, SData));
643                                 
644                                 }
645                                 
646                                 // Reset the values.
647                                 
648                                 HREFFound = FALSE;
649                                 ETagFound = FALSE;
650                                 HTTPStatus = FALSE;
652                         }
654                         if ((!xmlStrcmp(nodeLevel2->name, (const xmlChar *)"sync-token") ||
655                         !xmlStrcmp(nodeLevel2->name, (const xmlChar *)"d:sync-token") ||
656                         !xmlStrcmp(nodeLevel2->name, (const xmlChar *)"D:sync-token")) &&
657                         SyncTokenFound == FALSE
658                         ){
660                                 for (nodeLevel3 = nodeLevel2->children;
661                                 nodeLevel3 != NULL;
662                                 nodeLevel3 = nodeLevel3->next)
663                                 {
665                                         if (!xmlStrcmp(nodeLevel3->name, (const xmlChar *)"text") ||
666                                         !xmlStrcmp(nodeLevel3->name, (const xmlChar *)"d:text") ||
667                                         !xmlStrcmp(nodeLevel3->name, (const xmlChar *)"D:text")
668                                         ){
669                         
670                                                 DataSyncToken = wxString::FromUTF8((const char*)nodeLevel3->content);
672                                                 SyncTokenFound = TRUE;
673                         
674                                         }
675                         
676                                 }
677         
678                         }
680                 }
682         }
683         
684         for (nodeLevel1 = xmlCardDAVDoc->children;
685                 nodeLevel1 != NULL;
686                 nodeLevel1 = nodeLevel1->next)
687         {
689                 for (nodeLevel2 = nodeLevel1->children;
690                         nodeLevel2 != NULL;
691                         nodeLevel2 = nodeLevel2->next)
692                 {
694                         DataFileStatus = 0;
695                         bool HREFFound = FALSE;
696                         bool ETagFound = FALSE;
697                         bool HTTPStatus = FALSE;
699                         for (nodeLevel3 = nodeLevel2->children;
700                         nodeLevel3 != NULL;
701                         nodeLevel3 = nodeLevel3->next)
702                         {
704                                 if (!xmlStrcmp(nodeLevel3->name, (const xmlChar *)"href") ||
705                                 !xmlStrcmp(nodeLevel3->name, (const xmlChar *)"d:href") ||
706                                 !xmlStrcmp(nodeLevel3->name, (const xmlChar *)"D:href")
707                                 ){
709                                         // Get the filename.
710                                         
711                                         for (nodeLevel4 = nodeLevel3->children;
712                                         nodeLevel4 != NULL;
713                                         nodeLevel4 = nodeLevel4->next)
714                                         {
715                                         
716                                                 if (!xmlStrcmp(nodeLevel4->name, (const xmlChar *)"text") ||
717                                                 !xmlStrcmp(nodeLevel4->name, (const xmlChar *)"d:text") ||
718                                                 !xmlStrcmp(nodeLevel4->name, (const xmlChar *)"D:text")
719                                                 ){
720                                                 
721                                                         DataFilename = wxString::FromUTF8((const char*)nodeLevel4->content);
722                                                         wxStringTokenizer wSTDFilename(DataFilename, wxT("/"));
723                                                 
724                                                         while (wSTDFilename.HasMoreTokens()){
725                                                         
726                                                                 DataFilename = wSTDFilename.GetNextToken();
727                                                         
728                                                         }
729                                                         
730                                                         HREFFound = TRUE;
731                                                 
732                                                 }
733                                                 
734         
735                                         
736                                         }
737                                         
739                                 } else {
741                                         for (nodeLevel4 = nodeLevel3->children;
742                                         nodeLevel4 != NULL;
743                                         nodeLevel4 = nodeLevel4->next)
744                                         {
746                                                 for (nodeStatusLv1 = nodeLevel3->children;
747                                                         nodeStatusLv1 != NULL;
748                                                         nodeStatusLv1 = nodeStatusLv1->next)
749                                                 {
751                                                         if (wxString::FromUTF8((const char*)nodeStatusLv1->content) == wxT("HTTP/1.1 404 Not Found")){
752                 
753                                                                 DataFileStatus = 2;
755                                                                 HTTPStatus = TRUE;
756                                                                                 
757                                                         }
758                                         
759                                                         if ((!xmlStrcmp(nodeStatusLv1->name, (const xmlChar *)"status") ||
760                                                         !xmlStrcmp(nodeStatusLv1->name, (const xmlChar *)"d:status") ||
761                                                         !xmlStrcmp(nodeStatusLv1->name, (const xmlChar *)"D:status")) && HTTPStatus == FALSE)
762                                                         {
764                                                                 // Get the filename.
765                                         
766                                                                 for (nodeStatusLv2 = nodeStatusLv1->children;
767                                                                 nodeStatusLv2 != NULL;
768                                                                 nodeStatusLv2 = nodeStatusLv2->next)
769                                                                 {
770                                         
771                                                                         if (!xmlStrcmp(nodeStatusLv2->name, (const xmlChar *)"text") ||
772                                                                         !xmlStrcmp(nodeStatusLv2->name, (const xmlChar *)"d:text") ||
773                                                                         !xmlStrcmp(nodeStatusLv2->name, (const xmlChar *)"D:text")
774                                                                         ){
776                                                                                 if (wxString::FromUTF8((const char*)nodeStatusLv2->content) == wxT("HTTP/1.1 200 OK")){
778                                                                                         DataFileStatus = 1;
779                                                                                         
780                                                                                         HTTPStatus = TRUE;
781                                                                         
782                                                                                 // This is currently in a WebDAV draft and may hopefully be enabled when this changes.
783                                                                         
784                                                                                 //} else if (wxString::FromUTF8((const char*)nodeStatusLv2->content) == wxT("HTTP/1.1 201 Created")){
785                                                                                 
786                                                                                 //      DataFileStatus = 0;
787                                                                                 
788                                                                                 }
789                                                 
790                                                                         }
791                                                 
792         
793                                         
794                                                                 }
795                                                         
796                                                         }
798                                         
799                                                 }
800                                                 
801                                                 for (nodeLevel5 = nodeLevel4->children;
802                                                 nodeLevel5 != NULL;
803                                                 nodeLevel5 = nodeLevel5->next)
804                                                 {
806                                                         if (!xmlStrcmp(nodeLevel5->name, (const xmlChar *)"getetag") ||
807                                                         !xmlStrcmp(nodeLevel5->name, (const xmlChar *)"d:getetag") ||
808                                                         !xmlStrcmp(nodeLevel5->name, (const xmlChar *)"D:getetag")
809                                                         ){
811                                                                 for (nodeLevel6 = nodeLevel5->children;
812                                                                 nodeLevel6 != NULL;
813                                                                 nodeLevel6 = nodeLevel6->next)
814                                                                 {
816                                                                         // Strip the quotes from the ETag.
817                                                 
818                                                                         ETagData = wxString::FromUTF8((const char*)nodeLevel6->content);
819                                                                         if (ETagData.Mid(0, 1) == wxT("\"") && ETagData.Mid((ETagData.Len() - 1), 1) == wxT("\"")){
820                                                 
821                                                                                 ETagData.Remove(0, 1);
822                                                                                 ETagData.RemoveLast();
823                                                 
824                                                                         }
825                                                                 
826                                                                         ETagFound = TRUE;
828                                                                 }
829                                                                 
830                                                         }
832                                                 }       
834                                         }
836                                 }
838                         }
840                         if (HREFFound == TRUE && HTTPStatus == TRUE && DataFileStatus == 2){
841                         
842                                 FileSyncData SData;
843                                         
844                                 SData.ETagData = wxT("");
845                                 SData.DataFlag = DataFileStatus;
846                                         
847                                 ContactListFinal.ListData.insert(std::make_pair(DataFilename, SData));                          
848                         
849                         }
851                         if (HREFFound == TRUE && ETagFound == TRUE && HTTPStatus == TRUE){
852                                 
853                                 // Add to the map data.
854                                         
855                                 FileSyncData SData;
856                                         
857                                 SData.ETagData = ETagData;
858                                 SData.DataFlag = DataFileStatus;
859                                         
860                                 ContactListFinal.ListData.insert(std::make_pair(DataFilename, SData));
861                                 
862                         }
863                                 
864                         // Reset the values.
865                                 
866                         HREFFound = FALSE;
867                         ETagFound = FALSE;
868                         HTTPStatus = FALSE;
869                         DataFilename.Clear();
871                         if ((!xmlStrcmp(nodeLevel2->name, (const xmlChar *)"sync-token") ||
872                         !xmlStrcmp(nodeLevel2->name, (const xmlChar *)"d:sync-token") ||
873                         !xmlStrcmp(nodeLevel2->name, (const xmlChar *)"D:sync-token")) &&
874                         SyncTokenFound == FALSE
875                         ){
877                                 for (nodeLevel3 = nodeLevel2->children;
878                                 nodeLevel3 != NULL;
879                                 nodeLevel3 = nodeLevel3->next)
880                                 {
882                                         if (!xmlStrcmp(nodeLevel3->name, (const xmlChar *)"text") ||
883                                         !xmlStrcmp(nodeLevel3->name, (const xmlChar *)"d:text") ||
884                                         !xmlStrcmp(nodeLevel3->name, (const xmlChar *)"D:text")
885                                         ){
886                         
887                                                 DataSyncToken = wxString::FromUTF8((const char*)nodeLevel3->content);
889                                                 SyncTokenFound = TRUE;
890                         
891                                         }
892                         
893                                 }
894         
895                         }
897                 }
899         }
900         
901         // Get the sync token.
902         
903         if (SyncTokenFound == TRUE){
904         
905                 ContactListFinal.SyncToken = DataSyncToken;
906         
907         } else {
908         
909         }
911         SleepFor(2000000000);
913         /*timespec n1, n2;
914                                                 
915         n1.tv_sec = 0;
916         n1.tv_nsec = 2000000000L;
917                 
918         nanosleep(&n1, &n2);*/
920         xmlFreeDoc(xmlCardDAVDoc);
921         curl_easy_cleanup(conn);
923         SyncDataBuffer.reset();
925         // Get the first result.
927         return ContactListFinal;
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