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.
8 // A CalDAV REPORT can only be one type. This is stored in the report_type member.
16 // Data type for CalDAV Report Parameters.
17 // As we slog our way through the XML we learn what the client is asking for
18 // and build up the contents of this data type.
20 int tag_nesting_level; // not needed, just kept for pretty-printing
21 enum cr_type report_type; // which RFC4791 section 7 REPORT are we generating
22 StrBuf *Chardata; // XML chardata in between tags is built up here
23 StrBuf *Hrefs; // list of items requested by a `calendar-multiget` REPORT
24 Array *filters; // If the query contains a FILTER stanza, the filter criteria are populated here
28 // XML parser callback
29 void caldav_xml_start(void *data, const char *el, const char **attr) {
30 struct cr_params *crp = (struct cr_params *) data;
32 #ifdef DEBUG_XML_PARSE
37 for (i=0; i<crp->tag_nesting_level; ++i) {
40 syslog(LOG_DEBUG, "%s<%s>", indent, el);
41 ++crp->tag_nesting_level;
43 for (i=0; i<crp->tag_nesting_level; ++i) {
46 for (i = 0; attr[i] != NULL; i += 2) {
47 syslog(LOG_DEBUG, "%sAttribute '%s' = '%s'", indent, attr[i], attr[i + 1]);
52 // RFC4791 7.8 "calendar-query" REPORT - Client will send a lot of search criteria.
53 if (!strcasecmp(el, "urn:ietf:params:xml:ns:caldav:calendar-query")) {
54 crp->report_type = cr_calendar_query;
57 // RFC4791 7.9 "calendar-multiget" REPORT - Client will supply a list of specific hrefs.
58 else if (!strcasecmp(el, "urn:ietf:params:xml:ns:caldav:calendar-multiget")) {
59 crp->report_type = cr_calendar_multiget;
62 // RFC4791 7.10 "free-busy-query" REPORT
63 else if (!strcasecmp(el, "urn:ietf:params:xml:ns:caldav:free-busy-query")) {
64 crp->report_type = cr_freebusy_query;
67 // RFC4791 9.7 create a filter array if this query contains a "CALDAV:filter" stanza
68 else if (!strcasecmp(el, "urn:ietf:params:xml:ns:caldav:filter")) {
69 crp->filters = array_new(SIZ);
72 // Handle the filters defined in RFC4791 9.7.1 through 9.7.5
73 else if ( ( (!strcasecmp(el, "urn:ietf:params:xml:ns:caldav:comp-filter"))
74 || (!strcasecmp(el, "urn:ietf:params:xml:ns:caldav:prop-filter"))
75 || (!strcasecmp(el, "urn:ietf:params:xml:ns:caldav:param-filter"))
76 || (!strcasecmp(el, "urn:ietf:params:xml:ns:caldav:is-not-defined"))
77 || (!strcasecmp(el, "urn:ietf:params:xml:ns:caldav:text-match"))
79 && (crp->filters) // Make sure we actually allocated an array
83 int len = snprintf(newfilter, SIZ, &el[30]); // strip off "urn:ietf:params:xml:ns:caldav:" for our purposes
85 len += snprintf(&newfilter[len], SIZ-len, "|%s", attr[a++]); // now save the attributes
87 array_append(crp->filters, newfilter);
93 // XML parser callback
94 void caldav_xml_end(void *data, const char *el) {
95 struct cr_params *crp = (struct cr_params *) data;
97 --crp->tag_nesting_level;
99 #ifdef DEBUG_XML_PARSE
104 for (i=0; i<crp->tag_nesting_level; ++i) {
107 syslog(LOG_DEBUG, "%s</%s>", indent, el);
111 if ((!strcasecmp(el, "DAV::href")) || (!strcasecmp(el, "DAV:href"))) {
112 if (crp->Hrefs == NULL) { // append crp->Chardata to crp->Hrefs
113 crp->Hrefs = NewStrBuf();
116 StrBufAppendBufPlain(crp->Hrefs, HKEY("|"), 0);
118 StrBufAppendBuf(crp->Hrefs, crp->Chardata, 0);
121 if (crp->Chardata != NULL) { // Tag is closed; chardata is now out of scope.
122 FreeStrBuf(&crp->Chardata); // Free the buffer.
123 crp->Chardata = NULL;
128 // XML parser callback
129 void caldav_xml_chardata(void *data, const XML_Char *s, int len) {
130 struct cr_params *crp = (struct cr_params *) data;
132 char *app = malloc(len+1);
139 if (crp->Chardata == NULL) {
140 crp->Chardata = NewStrBuf();
143 StrBufAppendBufPlain(crp->Chardata, app, len, 0);
145 #ifdef DEBUG_XML_PARSE
147 string_trim(app); // remove leading/trailing whitespace. ok to mangle it because we've already appended.
148 if (!IsEmptyStr(app)) {
152 for (i=0; i<crp->tag_nesting_level; ++i) {
155 syslog(LOG_DEBUG, "%s%s", indent, app, len);
165 // Called by caldav_report_one_item() to fetch a message (by number) in the current room,
166 // and return only the icalendar data as a StrBuf. Returns NULL if not found.
168 // NOTE: this function expects that "MSGP text/calendar" was issued at the beginning
169 // of a REPORT operation to set our preferred MIME type to calendar data.
170 StrBuf *fetch_ical(struct ctdlsession *c, long msgnum) {
174 ctdl_printf(c, "MSG4 %ld", msgnum);
175 ctdl_readline(c, buf, sizeof(buf));
180 while (ctdl_readline(c, buf, sizeof(buf)), strcmp(buf, "000")) {
181 if (Buf != NULL) { // already in body
182 StrBufAppendPrintf(Buf, "%s\n", buf);
184 else if (IsEmptyStr(buf)) { // beginning of body
193 // Called by multiple REPORT types to actually perform the output in "multiget" format.
194 // We need to already know the source message number and the href, but also already have the output data.
195 void cal_multiget_out(long msgnum, StrBuf *ThisHref, StrBuf *Caldata, StrBuf *ReportOut) {
197 StrBufAppendPrintf(ReportOut, "<D:response>");
198 StrBufAppendPrintf(ReportOut, "<D:href>");
199 StrBufXMLEscAppend(ReportOut, ThisHref, NULL, 0, 0);
200 StrBufAppendPrintf(ReportOut, "</D:href>");
201 StrBufAppendPrintf(ReportOut, "<D:propstat>");
203 if (Caldata != NULL) {
204 // syslog(LOG_DEBUG, "caldav_report_one_item(%s) 200 OK", ChrPtr(ThisHref));
205 StrBufAppendPrintf(ReportOut, "<D:status>");
206 StrBufAppendPrintf(ReportOut, "HTTP/1.1 200 OK");
207 StrBufAppendPrintf(ReportOut, "</D:status>");
208 StrBufAppendPrintf(ReportOut, "<D:prop>");
209 StrBufAppendPrintf(ReportOut, "<D:getetag>");
210 StrBufAppendPrintf(ReportOut, "%ld", msgnum);
211 StrBufAppendPrintf(ReportOut, "</D:getetag>");
212 StrBufAppendPrintf(ReportOut, "<C:calendar-data>");
213 StrBufXMLEscAppend(ReportOut, Caldata, NULL, 0, 0);
214 StrBufAppendPrintf(ReportOut, "</C:calendar-data>");
215 StrBufAppendPrintf(ReportOut, "</D:prop>");
218 // syslog(LOG_DEBUG, "caldav_report_one_item(%s) 404 not found", ChrPtr(ThisHref));
219 StrBufAppendPrintf(ReportOut, "<D:status>");
220 StrBufAppendPrintf(ReportOut, "HTTP/1.1 404 not found");
221 StrBufAppendPrintf(ReportOut, "</D:status>");
224 StrBufAppendPrintf(ReportOut, "</D:propstat>");
225 StrBufAppendPrintf(ReportOut, "</D:response>");
229 // Called by caldav_report() to output a single item.
230 // Our policy is to throw away the list of properties the client asked for, and just send everything.
231 void caldav_report_one_item(struct http_transaction *h, struct ctdlsession *c, StrBuf *ReportOut, StrBuf *ThisHref) {
233 StrBuf *Caldata = NULL;
236 euid = strrchr(ChrPtr(ThisHref), '/');
241 euid = (char *) ChrPtr(ThisHref);
244 char *unescaped_euid = strdup(euid);
245 if (!unescaped_euid) {
248 unescape_input(unescaped_euid);
250 msgnum = locate_message_by_uid(c, unescaped_euid);
251 free(unescaped_euid);
253 Caldata = fetch_ical(c, msgnum);
259 cal_multiget_out(msgnum, ThisHref, Caldata, ReportOut);
261 if (Caldata != NULL) {
262 FreeStrBuf(&Caldata);
267 // Called by caldav_apply_filters() to apply ONE filter.
268 // Returns 0 if the calendar item does not match the filter.
269 // Returns 1 if the calendar item DOES match the filter.
270 int caldav_apply_one_filter(void *cal, char *filter) {
271 syslog(LOG_DEBUG, "applying filter: %s", filter);
273 char this_filter[SIZ]; // we have to copy the filter string because we will destructively tokenize it
274 safestrncpy(this_filter, filter, sizeof(this_filter));
276 char *t[10] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL } ;
277 char *f = this_filter;
279 while ( (t[num_tokens]=strtok_r(f, "|", &f)) && (num_tokens<10) ) {
283 // Handle the individual filters defined in RFC4791 9.7.1 through 9.7.5
285 if (!strcasecmp(t[0], "comp-filter")) { // RFC4791 9.7.1 - filter by component
286 syslog(LOG_DEBUG, "component filter FIXME not implemented yet");
289 else if (!strcasecmp(t[0], "prop-filter")) { // RFC4791 9.7.2 - filter by property
290 syslog(LOG_DEBUG, "property filter FIXME not implemented yet");
293 else if (!strcasecmp(t[0], "param-filter")) { // RFC4791 9.7.3 - filter by parameter
294 syslog(LOG_DEBUG, "parameter filter FIXME not implemented yet");
297 else if (!strcasecmp(t[0], "is-not-defined")) { // RFC4791 9.7.4
298 syslog(LOG_DEBUG, "is-not-defined filter FIXME not implemented yet");
301 else if (!strcasecmp(t[0], "text-match")) { // RFC4791 9.7.5
302 syslog(LOG_DEBUG, "text match filter FIXME not implemented yet");
309 // Recursive function to apply CalDAV FILTERS to a calendar item.
310 // Returns zero if the calendar item was disqualified by a filter, nonzero if the calendar item still qualifies.
311 int caldav_apply_filters(void *cal, Array *filters, int index) {
313 // Apply *this* filter.
314 if (caldav_apply_one_filter(cal, array_get_element_at(filters, index)) == 0) {
318 // If we get to this point, the current filter has passed, and we move on to the next one.
319 if (index < array_len(filters)-1) {
320 return(caldav_apply_filters(cal, filters, index+1));
323 // If we got this far, every filter passed, and the calendar item qualifies for output.
328 // Called by report_the_room_itself() in room_functions.c when a CalDAV REPORT method
329 // is requested on a calendar room. We fire up an XML Parser to decode the request and
330 // hopefully produce the correct output.
331 void caldav_report(struct http_transaction *h, struct ctdlsession *c) {
332 struct cr_params crp;
335 memset(&crp, 0, sizeof(struct cr_params));
337 XML_Parser xp = XML_ParserCreateNS("UTF-8", ':');
339 syslog(LOG_INFO, "Cannot create XML parser!");
344 XML_SetElementHandler(xp, caldav_xml_start, caldav_xml_end);
345 XML_SetCharacterDataHandler(xp, caldav_xml_chardata);
346 XML_SetUserData(xp, &crp);
347 XML_SetDefaultHandler(xp, NULL); // Disable internal entity expansion to prevent "billion laughs attack"
348 XML_Parse(xp, h->request_body, h->request_body_length, 1);
351 if (crp.Chardata != NULL) { // Discard any trailing chardata ... normally nothing here
352 FreeStrBuf(&crp.Chardata);
356 // We're going to make a lot of MSG4 calls, and the preferred MIME type we want is "text/calendar".
357 // The iCalendar standard is mature now, and we are no longer interested in text/x-vcal or application/ics.
358 ctdl_printf(c, "MSGP text/calendar");
359 ctdl_readline(c, buf, sizeof buf);
361 // Now begin the REPORT.
362 syslog(LOG_DEBUG, "CalDAV REPORT type is: %d", crp.report_type);
363 StrBuf *ReportOut = NewStrBuf();
364 StrBufAppendPrintf(ReportOut,
365 "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
368 "xmlns:C=\"urn:ietf:params:xml:ns:caldav\""
372 // RFC4791 7.8 "calendar-query" REPORT - Client will send a lot of search criteria.
373 if (crp.report_type == cr_calendar_query) {
375 Array *msglist = get_msglist(c, "ALL");
376 if (msglist != NULL) {
377 for (i = 0; i < array_len(msglist); ++i) {
379 memcpy(&m, array_get_element_at(msglist, i), sizeof(long));
381 // load and parse one calendar item
382 StrBuf *one_item = fetch_ical(c, m);
383 icalcomponent *cal = icalcomponent_new_from_string(ChrPtr(one_item));
385 // Does this calendar item qualify for output?
388 // If there was a filter stanza, run this calendar item through the filters.
389 qualify = caldav_apply_filters(cal, crp.filters, 0);
390 syslog(LOG_DEBUG, "Message %ld does%s qualify", m, (qualify ? "" : " NOT"));
392 // Did this calendar item match the query? If so, output it.
394 // FIXME need to populate the Href instead of NULL
395 cal_multiget_out(m, NULL, one_item, ReportOut);
398 icalcomponent_free(cal);
399 FreeStrBuf(&one_item);
406 // RFC4791 7.9 "calendar-multiget" REPORT - go get the specific Hrefs the client asked for.
407 // Can we move this back into citserver too?
408 else if ( (crp.report_type == cr_calendar_multiget) && (crp.Hrefs != NULL) ) {
410 StrBuf *ThisHref = NewStrBuf();
411 const char *pvset = NULL;
412 while (StrBufExtract_NextToken(ThisHref, crp.Hrefs, &pvset, '|') >= 0) {
413 StrBufTrim(ThisHref); // remove leading/trailing whitespace from the href
414 caldav_report_one_item(h, c, ReportOut, ThisHref);
416 FreeStrBuf(&ThisHref);
419 // RFC4791 7.10 "free-busy-query" REPORT
420 else if (crp.report_type == cr_freebusy_query) {
421 // FIXME build this REPORT. At the moment we send an empty multistatus.
424 // Free any query parameters that might have been allocated during the xml parse
425 if (crp.Hrefs != NULL) {
426 FreeStrBuf(&crp.Hrefs);
430 array_free(crp.filters);
434 StrBufAppendPrintf(ReportOut, "</D:multistatus>\n"); // End the REPORT.
436 add_response_header(h, strdup("Content-type"), strdup("text/xml"));
437 h->response_code = 207;
438 h->response_string = strdup("Multi-Status");
439 h->response_body_length = StrLength(ReportOut);
440 h->response_body = SmashStrBuf(&ReportOut);