Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Split carddav/carddav.cpp down into 8 files to make it manageable.
[xestiaab/.git] / source / carddav / carddav-connect.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 bool CardDAV::Connect(){
15         PageData.Clear();
16         PageHeader.Clear();
18         SSLStatus = TRUE;
19         AuthPassed = TRUE;
20         AbortConnection = FALSE;
22         CURL *conn;
23         CURLcode conncode;
24         wxString ServerAddressURL;
25         wxString ServerAuth;
26         wxString ServerAddressSSL;
27         wxString ServerAddressNormal;   
29         conn = curl_easy_init();
31         struct CardDAVCURLPasser {
32         
33                 CardDAV *Data;
34                 bool HeaderMode = TRUE;
35         
36         } CardDAVHeader, CardDAVFooter;
38         CardDAVHeader.Data = this;
39         CardDAVHeader.HeaderMode = TRUE;
40         
41         CardDAVFooter.Data = this;
42         CardDAVFooter.HeaderMode = FALSE;
44         wxString Data1;
45         wxString Data2;
46         
47         ServerAddressURL = ServerAddress + wxT(":") + wxString::Format(wxT("%i"), ServerPort) + wxT("/");
48         ServerAddressSSL = wxT("https://") + ServerAddressURL;
49         ServerAddressNormal = wxT("http://") + ServerAddressURL;
50         
51         ServerAuth = ServerUser + wxT(":") + ServerPass;
52         
53         // Try SSL first.
56         /*
57         char *ServerAdrSSLChar = new char[(ServerAddressSSL.Length() - 1)];
58         //memset(ServerAdrSSLChar, 0, ServerAddressSSL.Length());
59         strncpy(ServerAdrSSLChar, (const char*)ServerAddressSSL.mb_str(wxConvUTF8), (ServerAddressSSL.Length() - 1));
60         
61         char *ServerAdrNorChar = new char[(ServerAddressNormal.Length() - 1)];
62         //memset(ServerAdrNorChar, 0, ServerAddressSSL.Length());       
63         strncpy(ServerAdrNorChar, (const char*)ServerAddressNormal.mb_str(wxConvUTF8), (ServerAddressNormal.Length() - 1));
65         char *ServerAuthChar = new char[(ServerAuth.Length() - 1)];
66         //memset(ServerAuthChar, 0, ServerAddressSSL.Length()); 
67         strncpy(ServerAuthChar, (const char*)ServerAuth.mb_str(wxConvUTF8), (ServerAuth.Length() - 1));
68         
69         */
70         
71         if (ServerSSL){
73                 union {
74                         struct curl_slist       *certdata;
75                         struct curl_certinfo    *certinfo;
76                 } ptr;
78                 ptr.certdata = NULL;
80                 // Setup two initial connections and attempt to get the certificate data.
82                 curl_easy_setopt(conn, CURLOPT_URL, (const char*)ServerAddressSSL.mb_str(wxConvUTF8));
83                 curl_easy_setopt(conn, CURLOPT_CERTINFO, 1);
85                 conncode = (curl_easy_perform(conn));
87                 // Check if the SSL certificate is valid or self-signed or some other
88                 // error occured.
90                 if (conncode == CURLE_OK){
92                         // Connection is OK. Do nothing.
94                 } else if (conncode == CURLE_SSL_CACERT){
96                         // Post message saying SSL certificate is invalid. 
98                         curl_easy_getinfo(conn, CURLINFO_CERTINFO, &ptr.certdata);
100                 } else {
102                         fprintf(stderr, "curl_easy_perform() failed: %s\n",
103                                 curl_easy_strerror(conncode));
105                         ErrorMessage = wxString::Format(wxT("%s"), curl_easy_strerror(conncode));
107                         *ServerResult = FALSE;
108                         return FALSE;
110                 }
112                 curl_easy_setopt(conn, CURLOPT_URL, (const char*)ServerAddressSSL.mb_str(wxConvUTF8));
113                 curl_easy_setopt(conn, CURLOPT_NOPROGRESS, 1L);
114                 curl_easy_setopt(conn, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
115                 curl_easy_setopt(conn, CURLOPT_TIMEOUT, 60);
116                 curl_easy_setopt(conn, CURLOPT_FAILONERROR, TRUE);
117                 curl_easy_setopt(conn, CURLOPT_USERAGENT, XSDAB_USERAGENT);             
118                 curl_easy_setopt(conn, CURLOPT_USERPWD, (const char*)ServerAuth.mb_str(wxConvUTF8));
119                 curl_easy_setopt(conn, CURLOPT_WRITEFUNCTION, WritebackFunc);
120                 curl_easy_setopt(conn, CURLOPT_WRITEDATA, &PageData);
121                 curl_easy_setopt(conn, CURLOPT_WRITEHEADER, &PageHeader);
122                 curl_easy_setopt(conn, CURLOPT_NOSIGNAL, 1);
123                 curl_easy_setopt(conn, CURLOPT_CERTINFO, 1);
124                 
125                 if (AllowSelfSign == TRUE){
126                         curl_easy_setopt(conn, CURLOPT_SSL_VERIFYPEER, 0L);
127                         curl_easy_setopt(conn, CURLOPT_SSL_VERIFYHOST, 0L);
128                 }
130                 conncode = (curl_easy_perform(conn));
131                 
132                 ptr.certdata = NULL;
134                 curl_easy_getinfo(conn, CURLINFO_CERTINFO, &ptr.certdata);
135                 
136                 if (conncode == CURLE_OK){
138                         // Process the server header response and look for
139                         // 'addressbook' within the DAV header.
141                         wxStringTokenizer wxSHeaderLines(PageHeader, wxT("\r\n"));
142                         wxString wxSHeaderLine;
143                         std::map<int, wxString> DAVHeaderLines;
145                         while (wxSHeaderLines.HasMoreTokens()){
147                                 wxSHeaderLine = wxSHeaderLines.GetNextToken();
149                                 if (wxSHeaderLine.Mid(0, 4) == wxT("DAV:")){
151                                         // Look for address book in the line.
153                                         if (wxSHeaderLine.Find(wxT("addressbook")) != wxNOT_FOUND){
155                                                 HasCalDAVSupport = TRUE;
157                                         }
159                                 }
161                         }
163                         *ServerResult = TRUE;
164                         ValidResponse = TRUE;
165                         AuthPassed = TRUE;
166                         SSLStatus = TRUE;
167                         return TRUE;
169                 } else if (conncode == CURLE_HTTP_RETURNED_ERROR){
170                 
171                         fprintf(stderr, "curl_easy_perform() failed: %s\n",
172                                         curl_easy_strerror(conncode));
173                                 
174                         ErrorMessage = wxString::Format(wxT("%s"), curl_easy_strerror(conncode));
176                         *ServerResult = TRUE;
177                         ValidResponse = FALSE;
178                         AuthPassed = FALSE;
179                         SSLStatus = TRUE;
180                         return TRUE;
181                 
182                 } else {
184                         fprintf(stderr, "curl_easy_perform() failed: %s\n",
185                                         curl_easy_strerror(conncode));
187                         ErrorMessage = wxString::Format(wxT("%s"), curl_easy_strerror(conncode));
189                         *ServerResult = FALSE;
190                         return FALSE;                                   
192                 }
194         } else {
195         
196         // No SSL.
197                 
198                 curl_easy_setopt(conn, CURLOPT_URL, (const char*)ServerAddressNormal.mb_str(wxConvUTF8));
199                 curl_easy_setopt(conn, CURLOPT_NOPROGRESS, 1L);
200                 curl_easy_setopt(conn, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
201                 curl_easy_setopt(conn, CURLOPT_TIMEOUT, 60);    
202                 curl_easy_setopt(conn, CURLOPT_FAILONERROR, TRUE);
203                 curl_easy_setopt(conn, CURLOPT_USERAGENT, XSDAB_USERAGENT);
204                 curl_easy_setopt(conn, CURLOPT_USERPWD, (const char*)ServerAuth.mb_str(wxConvUTF8));
205                 curl_easy_setopt(conn, CURLOPT_WRITEFUNCTION, WritebackFunc);
206                 curl_easy_setopt(conn, CURLOPT_WRITEDATA, &PageData);
207                 curl_easy_setopt(conn, CURLOPT_WRITEHEADER, &PageHeader);
208                 curl_easy_setopt(conn, CURLOPT_NOSIGNAL, 1);
210                 conncode = (curl_easy_perform(conn));
212                 if (conncode == CURLE_OK){
214                         // Process the server header response and look for
215                         // 'addressbook' within the DAV header.
216                         
217                         wxStringTokenizer wxSHeaderLines(PageHeader, wxT("\r\n"));
218                         wxString wxSHeaderLine;
219                         std::map<int, wxString> DAVHeaderLines;
220                         
221                         while (wxSHeaderLines.HasMoreTokens()){
222                         
223                                 wxSHeaderLine = wxSHeaderLines.GetNextToken();
224                                 
225                                 if (wxSHeaderLine.Mid(0, 4) == wxT("DAV:")){
226                                 
227                                         // Look for address book in the line.
228                                         
229                                         if (wxSHeaderLine.Find(wxT("addressbook")) != wxNOT_FOUND){
230                                         
231                                                 HasCalDAVSupport = TRUE;
232                                         
233                                         }
234                                 
235                                 }
236                         
237                         }
239                         *ServerResult = TRUE;
240                         ValidResponse = TRUE;                   
241                         AuthPassed = TRUE;
242                         SSLStatus = FALSE;
243                         return TRUE;
245                 } else if (conncode == CURLE_HTTP_RETURNED_ERROR){
246                 
247                         fprintf(stderr, "curl_easy_perform() failed: %s\n",
248                                         curl_easy_strerror(conncode));
249                                         
250                         *ServerResult = TRUE;
251                         ValidResponse = FALSE;
252                         AuthPassed = FALSE;
253                         SSLStatus = FALSE;
254                         return TRUE;
255                 
256                 } else {
258                         fprintf(stderr, "curl_easy_perform() failed: %s\n",
259                                         curl_easy_strerror(conncode));
260                                 
261                         *ServerResult = FALSE;
262                         return FALSE;
264                 }
265                 
266                 // TODO: Double check and make sure HTTP Authentication is possible.
267                 
268         }               
269                 
270         *ServerResult = TRUE;
271         return TRUE;    
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