1 // This file contains functions which handle all of the CalDAV "REPORT" queries specified in RFC4791 section 7.
2 // Copyright (c) 2023-2024 by the citadel.org team
3 // This program is open source software. Use, duplication, or disclosure is subject to the GNU General Public License v3.
7 // Shorthand for the XML namespace of CalDAV
8 #define CAL "urn:ietf:params:xml:ns:caldav:"
9 #define CALLEN sizeof(CAL)-1
11 // A CalDAV REPORT can only be one type. This is stored in the report_type member.
19 // Data type for CalDAV Report Parameters.
20 // As we slog our way through the XML we learn what the client is asking for
21 // and build up the contents of this data type.
23 int tag_nesting_level; // not needed, just kept for pretty-printing
24 enum cr_type report_type; // which RFC4791 section 7 REPORT are we generating
25 StrBuf *Chardata; // XML chardata in between tags is built up here
26 StrBuf *Hrefs; // list of items requested by a `calendar-multiget` REPORT
27 Array *filters; // If the query contains a FILTER stanza, the filter criteria are populated here
31 // XML parser callback
32 void caldav_xml_start(void *data, const char *el, const char **attr) {
33 struct cr_params *crp = (struct cr_params *) data;
35 #ifdef DEBUG_XML_PARSE
40 lor (i=0; i<crp->tag_nesting_level; ++i) {
43 syslog(LOG_DEBUG, "%s<%s>", indent, el);
44 ++crp->tag_nesting_level;
46 for (i=0; i<crp->tag_nesting_level; ++i) {
49 for (i = 0; attr[i] != NULL; i += 2) {
50 syslog(LOG_DEBUG, "%sAttribute '%s' = '%s'", indent, attr[i], attr[i + 1]);
55 // RFC4791 7.8 "calendar-query" REPORT - Client will send a lot of search criteria.
56 if (!strcasecmp(el, CAL"calendar-query")) {
57 crp->report_type = cr_calendar_query;
60 // RFC4791 7.9 "calendar-multiget" REPORT - Client will supply a list of specific hrefs.
61 else if (!strcasecmp(el, CAL"calendar-multiget")) {
62 crp->report_type = cr_calendar_multiget;
65 // RFC4791 7.10 "free-busy-query" REPORT
66 else if (!strcasecmp(el, CAL"free-busy-query")) {
67 crp->report_type = cr_freebusy_query;
70 // RFC4791 9.7 create a filter array if this query contains a "filter" stanza
71 else if (!strcasecmp(el, CAL"filter")) {
72 crp->filters = array_new(SIZ);
75 // Handle the filters defined in RFC4791 9.7.1 through 9.7.5
76 else if ( ( (!strcasecmp(el, CAL"comp-filter"))
77 || (!strcasecmp(el, CAL"prop-filter"))
78 || (!strcasecmp(el, CAL"param-filter"))
79 || (!strcasecmp(el, CAL"is-not-defined"))
80 || (!strcasecmp(el, CAL"text-match"))
82 && (crp->filters) // Make sure we actually allocated an array
86 int len = snprintf(newfilter, SIZ, &el[CALLEN]); // strip off the namespace for our purposes
88 len += snprintf(&newfilter[len], SIZ-len, "|%s", attr[a++]); // now save the attributes
90 array_append(crp->filters, newfilter);
96 // XML parser callback
97 void caldav_xml_end(void *data, const char *el) {
98 struct cr_params *crp = (struct cr_params *) data;
100 --crp->tag_nesting_level;
102 #ifdef DEBUG_XML_PARSE
107 for (i=0; i<crp->tag_nesting_level; ++i) {
110 syslog(LOG_DEBUG, "%s</%s>", indent, el);
114 if ((!strcasecmp(el, "DAV::href")) || (!strcasecmp(el, "DAV:href"))) {
115 if (crp->Hrefs == NULL) { // append crp->Chardata to crp->Hrefs
116 crp->Hrefs = NewStrBuf();
119 StrBufAppendBufPlain(crp->Hrefs, HKEY("|"), 0);
121 StrBufAppendBuf(crp->Hrefs, crp->Chardata, 0);
124 if (crp->Chardata != NULL) { // Tag is closed; chardata is now out of scope.
125 FreeStrBuf(&crp->Chardata); // Free the buffer.
126 crp->Chardata = NULL;
131 // XML parser callback
132 void caldav_xml_chardata(void *data, const XML_Char *s, int len) {
133 struct cr_params *crp = (struct cr_params *) data;
135 char *app = malloc(len+1);
142 if (crp->Chardata == NULL) {
143 crp->Chardata = NewStrBuf();
146 StrBufAppendBufPlain(crp->Chardata, app, len, 0);
148 #ifdef DEBUG_XML_PARSE
150 string_trim(app); // remove leading/trailing whitespace. ok to mangle it because we've already appended.
151 if (!IsEmptyStr(app)) {
155 for (i=0; i<crp->tag_nesting_level; ++i) {
158 syslog(LOG_DEBUG, "%s%s", indent, app, len);
168 // Called by caldav_report_one_item() to fetch a message (by number) in the current room,
169 // and return only the icalendar data as a StrBuf. Returns NULL if not found.
171 // NOTE: this function expects that "MSGP text/calendar" was issued at the beginning
172 // of a REPORT operation to set our preferred MIME type to calendar data.
173 StrBuf *fetch_ical(struct ctdlsession *c, long msgnum) {
177 ctdl_printf(c, "MSG4 %ld", msgnum);
178 ctdl_readline(c, buf, sizeof(buf));
183 while (ctdl_readline(c, buf, sizeof(buf)), strcmp(buf, "000")) {
184 if (Buf != NULL) { // already in body
185 StrBufAppendPrintf(Buf, "%s\n", buf);
187 else if (IsEmptyStr(buf)) { // beginning of body
196 // Called by multiple REPORT types to actually perform the output in "multiget" format.
197 // We need to already know the source message number and the href, but also already have the output data.
198 void cal_multiget_out(long msgnum, StrBuf *ThisHref, StrBuf *Caldata, StrBuf *ReportOut) {
200 StrBufAppendPrintf(ReportOut, "<D:response>");
201 StrBufAppendPrintf(ReportOut, "<D:href>");
202 StrBufXMLEscAppend(ReportOut, ThisHref, NULL, 0, 0);
203 StrBufAppendPrintf(ReportOut, "</D:href>");
204 StrBufAppendPrintf(ReportOut, "<D:propstat>");
206 if (Caldata != NULL) {
207 // syslog(LOG_DEBUG, "caldav_report_one_item(%s) 200 OK", ChrPtr(ThisHref));
208 StrBufAppendPrintf(ReportOut, "<D:status>");
209 StrBufAppendPrintf(ReportOut, "HTTP/1.1 200 OK");
210 StrBufAppendPrintf(ReportOut, "</D:status>");
211 StrBufAppendPrintf(ReportOut, "<D:prop>");
212 StrBufAppendPrintf(ReportOut, "<D:getetag>");
213 StrBufAppendPrintf(ReportOut, "%ld", msgnum);
214 StrBufAppendPrintf(ReportOut, "</D:getetag>");
215 StrBufAppendPrintf(ReportOut, "<C:calendar-data>");
216 StrBufXMLEscAppend(ReportOut, Caldata, NULL, 0, 0);
217 StrBufAppendPrintf(ReportOut, "</C:calendar-data>");
218 StrBufAppendPrintf(ReportOut, "</D:prop>");
221 // syslog(LOG_DEBUG, "caldav_report_one_item(%s) 404 not found", ChrPtr(ThisHref));
222 StrBufAppendPrintf(ReportOut, "<D:status>");
223 StrBufAppendPrintf(ReportOut, "HTTP/1.1 404 not found");
224 StrBufAppendPrintf(ReportOut, "</D:status>");
227 StrBufAppendPrintf(ReportOut, "</D:propstat>");
228 StrBufAppendPrintf(ReportOut, "</D:response>");
232 // Called by caldav_report() to output a single item.
233 // Our policy is to throw away the list of properties the client asked for, and just send everything.
234 void caldav_report_one_item(struct http_transaction *h, struct ctdlsession *c, StrBuf *ReportOut, StrBuf *ThisHref) {
236 StrBuf *Caldata = NULL;
239 euid = strrchr(ChrPtr(ThisHref), '/');
244 euid = (char *) ChrPtr(ThisHref);
247 char *unescaped_euid = strdup(euid);
248 if (!unescaped_euid) {
251 unescape_input(unescaped_euid);
253 msgnum = locate_message_by_uid(c, unescaped_euid);
254 free(unescaped_euid);
256 Caldata = fetch_ical(c, msgnum);
262 cal_multiget_out(msgnum, ThisHref, Caldata, ReportOut);
264 if (Caldata != NULL) {
265 FreeStrBuf(&Caldata);
270 // Called by caldav_apply_filters() to apply ONE filter.
271 // Returns 0 if the calendar item does not match the filter.
272 // Returns 1 if the calendar item DOES match the filter.
273 int caldav_apply_one_filter(void *cal, char *filter) {
274 syslog(LOG_DEBUG, "applying filter: %s", filter);
276 char this_filter[SIZ]; // we have to copy the filter string because we will destructively tokenize it
277 safestrncpy(this_filter, filter, sizeof(this_filter));
279 char *t[10] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL } ;
280 char *f = this_filter;
282 while ( (t[num_tokens]=strtok_r(f, "|", &f)) && (num_tokens<10) ) {
286 // BEGIN experimental block -- see what happens when we cast to the wrong type
288 //icalcomponent *foo_comp = (icalcomponent *) cal;
289 //icalproperty *foo_prop = (icalproperty *) cal;
290 //icalparameter *foo_param = (icalparameter *) cal;
292 // END experimental block
294 // Handle the individual filters defined in RFC4791 9.7.1 through 9.7.5
296 if (!strcasecmp(t[0], "comp-filter")) { // RFC4791 9.7.1 - filter by component
297 syslog(LOG_DEBUG, "component filter FIXME not implemented yet");
298 if (icalcomponent_isa_component(cal)) {
299 syslog(LOG_DEBUG, "\033[32m yes this is a component \033[0m");
302 syslog(LOG_DEBUG, "\033[31m no this is not a component \033[0m");
306 else if (!strcasecmp(t[0], "prop-filter")) { // RFC4791 9.7.2 - filter by property
307 syslog(LOG_DEBUG, "property filter FIXME not implemented yet");
310 else if (!strcasecmp(t[0], "param-filter")) { // RFC4791 9.7.3 - filter by parameter
311 syslog(LOG_DEBUG, "parameter filter FIXME not implemented yet");
314 else if (!strcasecmp(t[0], "is-not-defined")) { // RFC4791 9.7.4
315 syslog(LOG_DEBUG, "is-not-defined filter FIXME not implemented yet");
318 else if (!strcasecmp(t[0], "text-match")) { // RFC4791 9.7.5
319 syslog(LOG_DEBUG, "text match filter FIXME not implemented yet");
326 // Recursive function to apply CalDAV FILTERS to a calendar item.
327 // Returns zero if the calendar item was disqualified by a filter, nonzero if the calendar item still qualifies.
328 int caldav_apply_filters(void *cal, Array *filters, int index) {
330 // Apply *this* filter.
331 if (caldav_apply_one_filter(cal, array_get_element_at(filters, index)) == 0) {
335 // If we get to this point, the current filter has passed, and we move on to the next one.
336 if (index < array_len(filters)-1) {
337 return(caldav_apply_filters(cal, filters, index+1));
340 // If we got this far, every filter passed, and the calendar item qualifies for output.
345 // Called by report_the_room_itself() in room_functions.c when a CalDAV REPORT method
346 // is requested on a calendar room. We fire up an XML Parser to decode the request and
347 // hopefully produce the correct output.
348 void caldav_report(struct http_transaction *h, struct ctdlsession *c) {
349 struct cr_params crp;
352 memset(&crp, 0, sizeof(struct cr_params));
354 XML_Parser xp = XML_ParserCreateNS("UTF-8", ':');
356 syslog(LOG_INFO, "Cannot create XML parser!");
361 XML_SetElementHandler(xp, caldav_xml_start, caldav_xml_end);
362 XML_SetCharacterDataHandler(xp, caldav_xml_chardata);
363 XML_SetUserData(xp, &crp);
364 XML_SetDefaultHandler(xp, NULL); // Disable internal entity expansion to prevent "billion laughs attack"
365 XML_Parse(xp, h->request_body, h->request_body_length, 1);
368 if (crp.Chardata != NULL) { // Discard any trailing chardata ... normally nothing here
369 FreeStrBuf(&crp.Chardata);
373 // We're going to make a lot of MSG4 calls, and the preferred MIME type we want is "text/calendar".
374 // The iCalendar standard is mature now, and we are no longer interested in text/x-vcal or application/ics.
375 ctdl_printf(c, "MSGP text/calendar");
376 ctdl_readline(c, buf, sizeof buf);
378 // Now begin the REPORT.
379 syslog(LOG_DEBUG, "CalDAV REPORT type is: %d", crp.report_type);
380 StrBuf *ReportOut = NewStrBuf();
381 StrBufAppendPrintf(ReportOut,
382 "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
385 "xmlns:C=\"urn:ietf:params:xml:ns:caldav\""
389 // RFC4791 7.8 "calendar-query" REPORT - Client will send a lot of search criteria.
390 if (crp.report_type == cr_calendar_query) {
392 Array *msglist = get_msglist(c, "ALL");
393 if (msglist != NULL) {
394 for (i = 0; i < array_len(msglist); ++i) {
396 memcpy(&m, array_get_element_at(msglist, i), sizeof(long));
398 // load and parse one calendar item
399 StrBuf *one_item = fetch_ical(c, m);
400 icalcomponent *cal = icalcomponent_new_from_string(ChrPtr(one_item));
402 // Does this calendar item qualify for output?
405 // If there was a filter stanza, run this calendar item through the filters.
406 qualify = caldav_apply_filters(cal, crp.filters, 0);
407 syslog(LOG_DEBUG, "Message %ld does%s qualify", m, (qualify ? "" : " NOT"));
409 // Did this calendar item match the query? If so, output it.
411 // FIXME need to populate the Href instead of NULL
412 cal_multiget_out(m, NULL, one_item, ReportOut);
415 icalcomponent_free(cal);
416 FreeStrBuf(&one_item);
423 // RFC4791 7.9 "calendar-multiget" REPORT - go get the specific Hrefs the client asked for.
424 // Can we move this back into citserver too?
425 else if ( (crp.report_type == cr_calendar_multiget) && (crp.Hrefs != NULL) ) {
427 StrBuf *ThisHref = NewStrBuf();
428 const char *pvset = NULL;
429 while (StrBufExtract_NextToken(ThisHref, crp.Hrefs, &pvset, '|') >= 0) {
430 StrBufTrim(ThisHref); // remove leading/trailing whitespace from the href
431 caldav_report_one_item(h, c, ReportOut, ThisHref);
433 FreeStrBuf(&ThisHref);
436 // RFC4791 7.10 "free-busy-query" REPORT
437 else if (crp.report_type == cr_freebusy_query) {
438 // FIXME build this REPORT. At the moment we send an empty multistatus.
441 // Free any query parameters that might have been allocated during the xml parse
442 if (crp.Hrefs != NULL) {
443 FreeStrBuf(&crp.Hrefs);
447 array_free(crp.filters);
451 StrBufAppendPrintf(ReportOut, "</D:multistatus>\n"); // End the REPORT.
453 add_response_header(h, strdup("Content-type"), strdup("text/xml"));
454 h->response_code = 207;
455 h->response_string = strdup("Multi-Status");
456 h->response_body_length = StrLength(ReportOut);
457 h->response_body = SmashStrBuf(&ReportOut);