Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
macOS: Implemented support for adding, editing and deleting a CalDAV account
[xestiacalendar/.git] / source / objects / CalDAV / CalDAV.h
1 // CalDAV.h - CalDAV Connection Object header.
2 //
3 // (c) 2016-2017 Xestia Software Development.
4 //
5 // This file is part of Xestia Calendar.
6 //
7 // Xestia Calendar is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by the
9 // Free Software Foundation, version 3 of the license.
10 //
11 // Xestia Calendar is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with Xestia Calendar. If not, see <http://www.gnu.org/licenses/>
19 #ifndef __OBJECTS_CALDAV_CALDAV_H__
20 #define __OBJECTS_CALDAV_CALDAV_H__
22 #include <curl/curl.h>
23 #include <libxml/parser.h>
24 #include <libxml/tree.h>
25 #include <wx/tokenzr.h>
26 #include <wx/file.h>
27 #include <string>
28 #include <iostream>
29 #include <vector>
30 #include <stdexcept>
31 #include <map>
32 #include "../../common/colour.h"
33 #include "../../common/text.h"
34 #include "../../common/uuid.h"
35 #include "../../common/dirs.h"
36 #include "../../common/sslcertstructs.h"
37 #include "../../version.h"
39 #if defined (__APPLE__)
40 #import <Foundation/Foundation.h>
41 #import <SecurityInterface/SFCertificateTrustPanel.h>
42 #endif
44 #if defined(__WIN32__)
45 #include "../common/win32ssl.h"
46 #endif
48 using namespace std;
50 // CalDAVConnectionData: used for
51 // connecting to the server.
53 enum CalDAVQueryResult {
54         CALDAVQUERYRESULT_UNITTESTFAIL = -1,
55         CALDAVQUERYRESULT_OK,
56         CALDAVQUERYRESULT_NOTRUN,
57         CALDAVQUERYRESULT_SERVERERROR,
58         CALDAVQUERYRESULT_SSLFAILURE,
59 };
61 struct CalDAVCalendarList {
63         map<int,string> name;
64         map<int,string> href;
65         map<int,int> order;
66         map<int,string> description;
67         map<int,Colour> calColour;
68         map<int,string> tag;
69         map<int,string> tagURL;
70         
71 };
73 struct CalDAVEntryList {
74         
75         map<int,string> href;
76         map<int,string> data;
77         map<int,string> tag;
78         
79 };
81 struct CalDAVConnectionData{
82         
83         string hostname = "";
84         int port = 8008;
85         string username = "";
86         string password = "";
87         string prefix = "";
88         bool useSSL = true;
89         int timeout = 60;
90         string account = "";
91         
92 };
94 // CalDAVStatusData: used for
95 // getting the current server
96 // settings for the CalDAV
97 // object.
99 struct CalDAVStatus{
101         string hostname;
102         int port;
103         string username;
104         string prefix;
105         bool useSSL;    
106         int timeout;
108 };
110 // CalDAVServerSupport: used for
111 // getting what the server supports
112 // from the CalDAV specification.
114 struct CalDAVServerSupport{
115         
116         // Variable name.                          Name in CalDAV header.
117         
118         bool basicSupport = false;              // calendar-access
119         
120 };
122 // CalDAVServerResult: used for
123 // getting the result of the
124 // request made via the CalDAV
125 // object.
127 struct CalDAVServerResult{
129         CalDAVQueryResult result = CALDAVQUERYRESULT_NOTRUN;
130         CURLcode code = CURLE_OK;
131         long httpCode = 0;
132         
133 };
135 // CalDAVSendData: used for
136 // sending data to the CaLDAV
137 // server.
139 struct CalDAVSendData{
140         string *readptr;
141         long sizeleft;
142         int seek = 0;
143 };
145 // Objects to move to a ConnectionObject-like interface in the future.
147 enum COSSLVerified {
148         COSSL_UNITTESTFAIL = -1,
149         COSSL_VERIFIED,
150         COSSL_VERIFIED_USER,
151         COSSL_UNABLETOVERIFY,
152         COSSL_NOTAPPLICABLE,
153         COSSL_NORESULT
154 };
156 class CalDAV{
158         private:
159                 string ProcessXMLUserPrincipal();
160                 string ProcessXMLCalendarHome();
161                 CalDAVCalendarList ProcessXMLCalendarList();
162                 CalDAVEntryList ProcessXMLEntryList();
163                 CalDAVEntryList ProcessXMLSyncTokenList();
164                 string ProcessXMLEntryETag();
165                 bool MatchXMLNameTransverse(xmlNodePtr *nodePtr, string nodeName);
166                 bool MatchXMLName(xmlNodePtr *nodePtrOriginal, string nodeName);
167                 string FetchXMLData(xmlNodePtr *nodePtr);
168                 CalDAVServerResult EditCalendarProcess(string *calendarHREF,
169                         string *calendarName,
170                         Colour *calendarColour,
171                         string *calendarDescription,
172                         int *calendarOrder);
173                 string BuildServerAddress(CalDAVConnectionData *connData, string uriAddress);
174                 void SetupDefaultParametersNonSSL(bool doAuthentication);
175                 void SetupDefaultParametersSSL(bool doAuthentication);
176                 void ResetResults();
178                 static size_t CalDAVReceive(char *receivedBuffer, size_t size, size_t newMemoryBytes, void *stream);
179         
180                 CalDAVConnectionData connectionData;
181                 CalDAVServerResult connectionServerResult;
182                 CURL *connectionHandle = nullptr;
183                 char sessionErrorBuffer[CURL_ERROR_SIZE];
184                 string serverData = "";
185                 string serverHeader = "";
186         
187                 COSSLVerified sslVerified;
188                 bool enableSSLBypass = false;
189                 bool sslSelfSigned = false;
190                 bool sslStatus = false;
191                 
192                 bool validResponse = false;
193                 bool authPassed = false;
194                 string errorMessage = "";
196 #if defined(__APPLE__)
197                 SecTrustRef certificateData = nullptr;
198 #elif defined(__WIN32__)
199                 PCCERT_CONTEXT certificateData = nullptr;
200 #endif
201         public:
202                 CalDAV();
203                 ~CalDAV();
204                 void SetupConnectionData(CalDAVConnectionData *connData);
205                 CalDAVStatus GetConnectionData();
206                 CalDAVServerResult Connect(bool doAuthentication);
207                 CalDAVServerResult GetServerResult();
208                 CalDAVServerSupport GetServerSupport();
209                 CalDAVCalendarList GetCalendars();
210                 CalDAVEntryList GetEntryList(string *calendarHREF);
211                 CalDAVEntryList GetEntryList(string *calendarHREF, string *calendarTag);
212         
213                 CalDAVServerResult AddCalendar(string calendarName);
214                 CalDAVServerResult AddCalendar(string *calendarName, string *calendarShortName);
215         
216                 CalDAVServerResult EditCalendar(string *calendarHREF,
217                         string *calendarName,
218                         Colour *calendarColour,
219                         string *calendarDescription,
220                         int *calendarOrder);
221                 CalDAVServerResult EditCalendar(string *calendarHREF,
222                         Colour *calendarColour);
223                 CalDAVServerResult EditCalendar(string *calendarHREF,
224                         string *calendarName);
225                 CalDAVServerResult EditCalendar(string *calendarHREF,
226                         int *calendarOrder);
227                 CalDAVServerResult EditCalendarDescription(string *calendarHREF,
228                         string *calendarDescription);
229                 
230                 CalDAVServerResult DeleteCalendar(string *calendarHREF);
231                 
232                 CalDAVServerResult AddEntry(string *calendarEntryHREF, string *entryData);
233                 CalDAVServerResult EditEntry(string *calendarEntryHREF, string *entryData, string *entryETag);
234                 CalDAVServerResult DeleteEntry(string *calendarEntryHREF);
235         
236                 string GetUserPrincipal();
237                 string GetCalendarHome(string userPrincipalURI);
238                 
239                 CalDAVServerResult GetEntryETag(string *calendarEntryHREF, string *eTagValue);
241                 bool CanDoSSL();
242                 bool HasValidResponse();
243                 bool AbleToLogin();
244                 bool IsSelfSigned();
245                 std::string GetErrorMessage();
247                 COSSLVerified SSLVerify();
248                 void BypassSSLVerification(bool EnableBypass);
249                 
250 #if defined(__APPLE__)
251     
252                 SecTrustRef BuildSSLCollection();
253     
254 #elif defined(__WIN32__)
256                 PCCERT_CONTEXT BuildSSLCollection();
258 #else
259                 SSLCertCollectionString BuildSSLCollection();
261 #endif
263                 struct CalDAVPassObject {
264                         CalDAV *CalDAVObject = nullptr;
265                         std::string *DataSetting = nullptr;
266                         bool ServerUsingSSL = false;
267                         CURL *ConnectionSessionObject = nullptr;
268 #if defined(__APPLE__)
269                         SecTrustRef SSLContext = nullptr;
270 #elif defined (__WIN32__)
271                         PCCERT_CONTEXT SSLContext = nullptr;
272 #endif
273                 };
275                 private:
276                         CalDAVPassObject PageDataObject;
277                         CalDAVPassObject PageHeaderObject;
278 };
280 // Subroutines that are used with the 
281 // CalDAVConnectionData struct.
283 //bool CalDAVObjectValidSettings(CalDAVConnectionData *connData);
285 #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