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
28 int filter_nest; // tag nesting level where a FILTER stanza begins
32 // XML parser callback
33 void caldav_xml_start(void *data, const char *el, const char **attr) {
34 struct cr_params *crp = (struct cr_params *) data;
36 #ifdef DEBUG_XML_PARSE
41 for (i=0; i<crp->tag_nesting_level; ++i) {
44 syslog(LOG_DEBUG, "%s<%s>", indent, el);
46 ++crp->tag_nesting_level;
47 #ifdef DEBUG_XML_PARSE
49 for (i=0; i<crp->tag_nesting_level; ++i) {
52 for (i = 0; attr[i] != NULL; i += 2) {
53 syslog(LOG_DEBUG, "%sAttribute '%s' = '%s'", indent, attr[i], attr[i + 1]);
58 // RFC4791 7.8 "calendar-query" REPORT - Client will send a lot of search criteria.
59 if (!strcasecmp(el, CAL"calendar-query")) {
60 crp->report_type = cr_calendar_query;
63 // RFC4791 7.9 "calendar-multiget" REPORT - Client will supply a list of specific hrefs.
64 else if (!strcasecmp(el, CAL"calendar-multiget")) {
65 crp->report_type = cr_calendar_multiget;
68 // RFC4791 7.10 "free-busy-query" REPORT
69 else if (!strcasecmp(el, CAL"free-busy-query")) {
70 crp->report_type = cr_freebusy_query;
73 // RFC4791 9.7 create a filter array if this query contains a "filter" stanza
74 else if (!strcasecmp(el, CAL"filter")) {
75 crp->filters = array_new(SIZ);
76 crp->filter_nest = crp->tag_nesting_level;
79 // Handle the filters defined in RFC4791 9.7.1 through 9.7.5
80 else if ( ( (!strcasecmp(el, CAL"comp-filter"))
81 || (!strcasecmp(el, CAL"prop-filter"))
82 || (!strcasecmp(el, CAL"param-filter"))
83 || (!strcasecmp(el, CAL"is-not-defined"))
84 || (!strcasecmp(el, CAL"text-match"))
86 && (crp->filters) // Make sure we actually allocated an array
91 int len = snprintf(newfilter, SIZ, "%d|", crp->tag_nesting_level - crp->filter_nest - 1);
92 len += snprintf(&newfilter[len], SIZ-len, "%s", &el[CALLEN]); // filter name without the namespace
94 len += snprintf(&newfilter[len], SIZ-len, "|%s", attr[a++]); // now save the attributes
96 array_append(crp->filters, newfilter);
102 // XML parser callback
103 void caldav_xml_end(void *data, const char *el) {
104 struct cr_params *crp = (struct cr_params *) data;
106 --crp->tag_nesting_level;
108 #ifdef DEBUG_XML_PARSE
113 for (i=0; i<crp->tag_nesting_level; ++i) {
116 syslog(LOG_DEBUG, "%s</%s>", indent, el);
120 if ((!strcasecmp(el, "DAV::href")) || (!strcasecmp(el, "DAV:href"))) {
121 if (crp->Hrefs == NULL) { // append crp->Chardata to crp->Hrefs
122 crp->Hrefs = NewStrBuf();
125 StrBufAppendBufPlain(crp->Hrefs, HKEY("|"), 0);
127 StrBufAppendBuf(crp->Hrefs, crp->Chardata, 0);
130 if (crp->Chardata != NULL) { // Tag is closed; chardata is now out of scope.
131 FreeStrBuf(&crp->Chardata); // Free the buffer.
132 crp->Chardata = NULL;
137 // XML parser callback
138 void caldav_xml_chardata(void *data, const XML_Char *s, int len) {
139 struct cr_params *crp = (struct cr_params *) data;
141 char *app = malloc(len+1);
148 if (crp->Chardata == NULL) {
149 crp->Chardata = NewStrBuf();
152 StrBufAppendBufPlain(crp->Chardata, app, len, 0);
154 #ifdef DEBUG_XML_PARSE
156 string_trim(app); // remove leading/trailing whitespace. ok to mangle it because we've already appended.
157 if (!IsEmptyStr(app)) {
161 for (i=0; i<crp->tag_nesting_level; ++i) {
164 syslog(LOG_DEBUG, "%s%s", indent, app, len);
174 // Called by caldav_report_one_item() to fetch a message (by number) in the current room,
175 // and return only the icalendar data as a StrBuf. Returns NULL if not found.
177 // NOTE: this function expects that "MSGP text/calendar" was issued at the beginning
178 // of a REPORT operation to set our preferred MIME type to calendar data.
179 StrBuf *fetch_ical(struct ctdlsession *c, long msgnum) {
183 ctdl_printf(c, "MSG4 %ld", msgnum);
184 ctdl_readline(c, buf, sizeof(buf));
189 while (ctdl_readline(c, buf, sizeof(buf)), strcmp(buf, "000")) {
190 if (Buf != NULL) { // already in body
191 StrBufAppendPrintf(Buf, "%s\n", buf);
193 else if (IsEmptyStr(buf)) { // beginning of body
202 // Called by multiple REPORT types to actually perform the output in "multiget" format.
203 // We need to already know the source message number and the href, but also already have the output data.
204 void cal_multiget_out(long msgnum, StrBuf *ThisHref, StrBuf *Caldata, StrBuf *ReportOut) {
206 StrBufAppendPrintf(ReportOut, "<D:response>");
207 StrBufAppendPrintf(ReportOut, "<D:href>");
208 StrBufXMLEscAppend(ReportOut, ThisHref, NULL, 0, 0);
209 StrBufAppendPrintf(ReportOut, "</D:href>");
210 StrBufAppendPrintf(ReportOut, "<D:propstat>");
212 if (Caldata != NULL) {
213 // syslog(LOG_DEBUG, "caldav_report_one_item(%s) 200 OK", ChrPtr(ThisHref));
214 StrBufAppendPrintf(ReportOut, "<D:status>");
215 StrBufAppendPrintf(ReportOut, "HTTP/1.1 200 OK");
216 StrBufAppendPrintf(ReportOut, "</D:status>");
217 StrBufAppendPrintf(ReportOut, "<D:prop>");
218 StrBufAppendPrintf(ReportOut, "<D:getetag>");
219 StrBufAppendPrintf(ReportOut, "%ld", msgnum);
220 StrBufAppendPrintf(ReportOut, "</D:getetag>");
221 StrBufAppendPrintf(ReportOut, "<C:calendar-data>");
222 StrBufXMLEscAppend(ReportOut, Caldata, NULL, 0, 0);
223 StrBufAppendPrintf(ReportOut, "</C:calendar-data>");
224 StrBufAppendPrintf(ReportOut, "</D:prop>");
227 // syslog(LOG_DEBUG, "caldav_report_one_item(%s) 404 not found", ChrPtr(ThisHref));
228 StrBufAppendPrintf(ReportOut, "<D:status>");
229 StrBufAppendPrintf(ReportOut, "HTTP/1.1 404 not found");
230 StrBufAppendPrintf(ReportOut, "</D:status>");
233 StrBufAppendPrintf(ReportOut, "</D:propstat>");
234 StrBufAppendPrintf(ReportOut, "</D:response>");
238 // Called by caldav_report() to output a single item.
239 // Our policy is to throw away the list of properties the client asked for, and just send everything.
240 void caldav_report_one_item(struct http_transaction *h, struct ctdlsession *c, StrBuf *ReportOut, StrBuf *ThisHref) {
242 StrBuf *Caldata = NULL;
245 euid = strrchr(ChrPtr(ThisHref), '/');
250 euid = (char *) ChrPtr(ThisHref);
253 char *unescaped_euid = strdup(euid);
254 if (!unescaped_euid) {
257 unescape_input(unescaped_euid);
259 msgnum = locate_message_by_uid(c, unescaped_euid);
260 free(unescaped_euid);
262 Caldata = fetch_ical(c, msgnum);
268 cal_multiget_out(msgnum, ThisHref, Caldata, ReportOut);
270 if (Caldata != NULL) {
271 FreeStrBuf(&Caldata);
276 // Called by caldav_apply_filters() to apply ONE filter.
277 // Returns 0 if the calendar item does not match the filter.
278 // Returns 1 if the calendar item DOES match the filter.
279 int caldav_apply_one_filter(void *cal, char *filter) {
280 syslog(LOG_DEBUG, "applying filter: %s", filter);
282 char this_filter[SIZ]; // we have to copy the filter string because we will destructively tokenize it
283 safestrncpy(this_filter, filter, sizeof(this_filter));
285 char *t[10] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL } ;
286 char *f = this_filter;
288 while ( (t[num_tokens]=strtok_r(f, "|", &f)) && (num_tokens<10) ) {
292 // BEGIN experimental block -- see what happens when we cast to the wrong type
294 //icalcomponent *foo_comp = (icalcomponent *) cal;
295 //icalproperty *foo_prop = (icalproperty *) cal;
296 //icalparameter *foo_param = (icalparameter *) cal;
298 // END experimental block
300 // Handle the individual filters defined in RFC4791 9.7.1 through 9.7.5
302 if (!strcasecmp(t[1], "comp-filter")) { // RFC4791 9.7.1 - filter by component
303 syslog(LOG_DEBUG, "component filter FIXME not implemented yet");
304 if (icalcomponent_isa_component(cal)) {
305 syslog(LOG_DEBUG, "\033[32m yes this is a component \033[0m");
308 syslog(LOG_DEBUG, "\033[31m no this is not a component \033[0m");
312 else if (!strcasecmp(t[1], "prop-filter")) { // RFC4791 9.7.2 - filter by property
313 syslog(LOG_DEBUG, "property filter FIXME not implemented yet");
316 else if (!strcasecmp(t[1], "param-filter")) { // RFC4791 9.7.3 - filter by parameter
317 syslog(LOG_DEBUG, "parameter filter FIXME not implemented yet");
320 else if (!strcasecmp(t[1], "is-not-defined")) { // RFC4791 9.7.4
321 syslog(LOG_DEBUG, "is-not-defined filter FIXME not implemented yet");
324 else if (!strcasecmp(t[1], "text-match")) { // RFC4791 9.7.5
325 syslog(LOG_DEBUG, "text match filter FIXME not implemented yet");
332 // Recursive function to apply CalDAV FILTERS to a calendar item.
333 // Returns zero if the calendar item was disqualified by a filter, nonzero if the calendar item still qualifies.
334 int caldav_apply_filters(void *cal, Array *filters, int index) {
336 // Apply *this* filter.
337 if (caldav_apply_one_filter(cal, array_get_element_at(filters, index)) == 0) {
341 // If we get to this point, the current filter has passed, and we move on to the next one.
342 if (index < array_len(filters)-1) {
343 return(caldav_apply_filters(cal, filters, index+1));
346 // If we got this far, every filter passed, and the calendar item qualifies for output.
351 // Called by report_the_room_itself() in room_functions.c when a CalDAV REPORT method
352 // is requested on a calendar room. We fire up an XML Parser to decode the request and
353 // hopefully produce the correct output.
354 void caldav_report(struct http_transaction *h, struct ctdlsession *c) {
355 struct cr_params crp;
358 memset(&crp, 0, sizeof(struct cr_params));
360 XML_Parser xp = XML_ParserCreateNS("UTF-8", ':');
362 syslog(LOG_INFO, "Cannot create XML parser!");
367 XML_SetElementHandler(xp, caldav_xml_start, caldav_xml_end);
368 XML_SetCharacterDataHandler(xp, caldav_xml_chardata);
369 XML_SetUserData(xp, &crp);
370 XML_SetDefaultHandler(xp, NULL); // Disable internal entity expansion to prevent "billion laughs attack"
371 XML_Parse(xp, h->request_body, h->request_body_length, 1);
374 if (crp.Chardata != NULL) { // Discard any trailing chardata ... normally nothing here
375 FreeStrBuf(&crp.Chardata);
379 // We're going to make a lot of MSG4 calls, and the preferred MIME type we want is "text/calendar".
380 // The iCalendar standard is mature now, and we are no longer interested in text/x-vcal or application/ics.
381 ctdl_printf(c, "MSGP text/calendar");
382 ctdl_readline(c, buf, sizeof buf);
384 // Now begin the REPORT.
385 syslog(LOG_DEBUG, "CalDAV REPORT type is: %d", crp.report_type);
386 StrBuf *ReportOut = NewStrBuf();
387 StrBufAppendPrintf(ReportOut,
388 "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
391 "xmlns:C=\"urn:ietf:params:xml:ns:caldav\""
395 // RFC4791 7.8 "calendar-query" REPORT - Client will send a lot of search criteria.
396 if (crp.report_type == cr_calendar_query) {
398 Array *msglist = get_msglist(c, "ALL");
399 if (msglist != NULL) {
400 for (i = 0; i < array_len(msglist); ++i) {
402 memcpy(&m, array_get_element_at(msglist, i), sizeof(long));
404 // load and parse one calendar item
405 StrBuf *one_item = fetch_ical(c, m);
406 icalcomponent *cal = icalcomponent_new_from_string(ChrPtr(one_item));
408 // Does this calendar item qualify for output?
411 // If there was a filter stanza, run this calendar item through the filters.
412 qualify = caldav_apply_filters(cal, crp.filters, 0);
413 syslog(LOG_DEBUG, "Message %ld does%s qualify", m, (qualify ? "" : " NOT"));
415 // Did this calendar item match the query? If so, output it.
417 // FIXME need to populate the Href instead of NULL
418 cal_multiget_out(m, NULL, one_item, ReportOut);
421 icalcomponent_free(cal);
422 FreeStrBuf(&one_item);
429 // RFC4791 7.9 "calendar-multiget" REPORT - go get the specific Hrefs the client asked for.
430 // Can we move this back into citserver too?
431 else if ( (crp.report_type == cr_calendar_multiget) && (crp.Hrefs != NULL) ) {
433 StrBuf *ThisHref = NewStrBuf();
434 const char *pvset = NULL;
435 while (StrBufExtract_NextToken(ThisHref, crp.Hrefs, &pvset, '|') >= 0) {
436 StrBufTrim(ThisHref); // remove leading/trailing whitespace from the href
437 caldav_report_one_item(h, c, ReportOut, ThisHref);
439 FreeStrBuf(&ThisHref);
442 // RFC4791 7.10 "free-busy-query" REPORT
443 else if (crp.report_type == cr_freebusy_query) {
444 // FIXME build this REPORT. At the moment we send an empty multistatus.
447 // Free any query parameters that might have been allocated during the xml parse
448 if (crp.Hrefs != NULL) {
449 FreeStrBuf(&crp.Hrefs);
453 array_free(crp.filters);
457 StrBufAppendPrintf(ReportOut, "</D:multistatus>\n"); // End the REPORT.
459 add_response_header(h, strdup("Content-type"), strdup("text/xml"));
460 h->response_code = 207;
461 h->response_string = strdup("Multi-Status");
462 h->response_body_length = StrLength(ReportOut);
463 h->response_body = SmashStrBuf(&ReportOut);