diary

Text-based journaling program
git clone https://git.in0rdr.ch/diary.git
Log | Files | Refs | README | LICENSE

commit 34d54b19a940b095afa5b2064c23cf0e7c1df8b2
parent 870050b4a291b5ad0e6991fbaf558dd440c8337f
Author: Andreas Gruhler <andreas.gruhler@adfinis.com>
Date:   Sat, 22 Jun 2024 16:05:02 +0200

feat(caldav): caldav testing with google

* extract caldav host without path as basis for further api paths
* fix start/end filter in event report request

Diffstat:
Msrc/caldav.c | 50+++++++++++++++++++++++++++++++++++++-------------
1 file changed, 37 insertions(+), 13 deletions(-)

diff --git a/src/caldav.c b/src/caldav.c @@ -623,9 +623,9 @@ void put_event(struct tm* date, const char* dir, size_t dir_size, char* calendar strftime(uid, sizeof uid, "%Y%m%d", date); char* ics = "BEGIN:VCALENDAR\n" - "PRODID:-//diary\n" // todo: add version - "VERSION:2.0\n" - "CALSCALE:GREGORIAN\n" + "PRODID:-//diary\n" // todo: add version + "VERSION:2.0\n" + "CALSCALE:GREGORIAN\n" "BEGIN:VEVENT\n" "UID:%s\n" "DTSTART;VALUE=DATE:%s\n" @@ -768,9 +768,22 @@ int caldav_sync(struct tm* date, char* current_user_principal = parse_caldav_current_user_principal(user_principal); tracepoint(diary, debug_string, "Current user principal", current_user_principal); + // extract host without path as basis for further api paths + char *caldav_host; char* caldav_host_scheme; + CURLU *h = curl_url(); + CURLUcode uc = curl_url_set(h, CURLUPART_URL, CONFIG.caldav_server, 0); + uc = curl_url_get(h, CURLUPART_HOST, &caldav_host, 0); + uc = curl_url_get(h, CURLUPART_SCHEME, &caldav_host_scheme, 0); + tracepoint(diary, debug_string, "Caldav server host name", caldav_host); + tracepoint(diary, debug_string, "Caldav server scheme/protocol", caldav_host_scheme); + tracepoint(diary, debug_string, "Caldav server host name", caldav_host); + if (!uc) { + tracepoint(diary, error, "cur_url_get() failed in caldav_sync()"); + } + // get the home-set of the user char uri[300]; - sprintf(uri, "%s%s", CONFIG.caldav_server, current_user_principal); + sprintf(uri, "%s://%s%s", caldav_host_scheme, caldav_host, current_user_principal); // free memory allocated by curl request free(user_principal); @@ -802,7 +815,7 @@ int caldav_sync(struct tm* date, // parse home set from xml response char* home_set_parsed = parse_home_set(home_set); - sprintf(uri, "%s%s", CONFIG.caldav_server, home_set_parsed); + sprintf(uri, "%s://%s%s", caldav_host_scheme, caldav_host, home_set_parsed); char* calendar_request = "<d:propfind xmlns:d='DAV:' xmlns:cs='http://calendarserver.org/ns/' xmlns:c='urn:ietf:params:xml:ns:caldav'>" " <d:prop>" @@ -827,17 +840,20 @@ int caldav_sync(struct tm* date, "<c:time-range start='%s' end='%s'/></c:comp-filter>" "</c:comp-filter></c:filter></c:calendar-query>"; - char dstr_cursor[30]; - char* format = "%Y%m%dT000000Z"; - strftime(dstr_cursor, sizeof dstr_cursor, format, date); + char dstr_start[30]; + char dstr_end[30]; + char* format_start = "%Y%m%dT000000Z"; // start of day + char* format_end = "%Y%m%dT235900Z"; // end of day + strftime(dstr_start, sizeof dstr_start, format_start, date); + strftime(dstr_end, sizeof dstr_end, format_end, date); - char caldata_postfields[strlen(xml_filter) + 2*strlen(dstr_cursor)]; + char caldata_postfields[strlen(xml_filter) + strlen(dstr_start) + strlen(dstr_end)]; sprintf(caldata_postfields, xml_filter, - dstr_cursor, - dstr_cursor); + dstr_start, + dstr_end); // fetch event for the cursor date - sprintf(uri, "%s%s", CONFIG.caldav_server, calendar_href); + sprintf(uri, "%s://%s%s", caldav_host_scheme, caldav_host, calendar_href); char* event = caldav_req(date, uri, "REPORT", caldata_postfields, 1, basicauth_enabled, "application/xml", 0); // todo: warn if multiple events, @@ -850,6 +866,7 @@ int caldav_sync(struct tm* date, // - Google Client ID // - Google Secret ID // - Google Calendar Name + tracepoint(diary, debug, "Event not found"); pthread_cancel(progress_tid); wclear(header); free(home_set); @@ -910,6 +927,8 @@ int caldav_sync(struct tm* date, pthread_cancel(progress_tid); wclear(header); // free memory allocated to store curl response + curl_free(caldav_host_scheme); + curl_free(caldav_host); free(event); free(remote_uid); free(home_set); @@ -919,7 +938,8 @@ int caldav_sync(struct tm* date, if (remote_uid) { // purge any existing daily calendar entries on the remote side char event_uri[300]; - sprintf(event_uri, "%s%s%s.ics", CONFIG.caldav_server, calendar_href, remote_uid); + sprintf(event_uri, "%s://%s%s%s.ics", caldav_host_scheme, caldav_host, calendar_href, remote_uid); + //sprintf(event_uri, "%s%s%s.ics", CONFIG.caldav_server, calendar_href, remote_uid); tracepoint(diary, debug_string, "Event URI for DELETE request", event_uri); char* response = caldav_req(date, event_uri, "DELETE", NULL, 0, basicauth_enabled, "", 0); free(response); @@ -937,6 +957,8 @@ int caldav_sync(struct tm* date, tracepoint(diary, error, "Could not fetch description of remote event. Aborting sync."); pthread_cancel(progress_tid); wclear(header); + curl_free(caldav_host_scheme); + curl_free(caldav_host); free(event); free(remote_uid); free(home_set); @@ -996,6 +1018,8 @@ int caldav_sync(struct tm* date, } // free memory allocated to store curl response + curl_free(caldav_host_scheme); + curl_free(caldav_host); free(event); free(remote_uid); free(home_set); // home_set was required for sprintfing calendar_href to event_uri