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
27 // XML parser callback
28 void caldav_xml_start(void *data, const char *el, const char **attr) {
29 struct cr_parms *crp = (struct cr_parms *) data;
31 #ifdef DEBUG_XML_PARSE
36 for (i=0; i<crp->tag_nesting_level; ++i) {
39 syslog(LOG_DEBUG, "%s<%s>", indent, el);
40 for (i = 0; attr[i] != NULL; i += 2) {
41 syslog(LOG_DEBUG, "%sAttribute '%s' = '%s'", indent, attr[i], attr[i + 1]);
46 // RFC4791 7.8 "calendar-query" REPORT - Client will send a lot of search criteria.
47 if (!strcasecmp(el, "urn:ietf:params:xml:ns:caldav:calendar-query")) {
48 crp->report_type = cr_calendar_query;
51 // RFC4791 7.9 "calendar-multiget" REPORT - Client will supply a list of specific hrefs.
52 else if (!strcasecmp(el, "urn:ietf:params:xml:ns:caldav:calendar-multiget")) {
53 crp->report_type = cr_calendar_multiget;
56 // RFC4791 7.10 "free-busy-query" REPORT
57 else if (!strcasecmp(el, "urn:ietf:params:xml:ns:caldav:free-busy-query")) {
58 crp->report_type = cr_freebusy_query;
61 ++crp->tag_nesting_level;
65 // XML parser callback
66 void caldav_xml_end(void *data, const char *el) {
67 struct cr_parms *crp = (struct cr_parms *) data;
69 --crp->tag_nesting_level;
71 #ifdef DEBUG_XML_PARSE
76 for (i=0; i<crp->tag_nesting_level; ++i) {
79 syslog(LOG_DEBUG, "%s</%s>", indent, el);
83 if ((!strcasecmp(el, "DAV::href")) || (!strcasecmp(el, "DAV:href"))) {
84 if (crp->Hrefs == NULL) { // append crp->Chardata to crp->Hrefs
85 crp->Hrefs = NewStrBuf();
88 StrBufAppendBufPlain(crp->Hrefs, HKEY("|"), 0);
90 StrBufAppendBuf(crp->Hrefs, crp->Chardata, 0);
93 if (crp->Chardata != NULL) { // Tag is closed; chardata is now out of scope.
94 FreeStrBuf(&crp->Chardata); // Free the buffer.
100 // XML parser callback
101 void caldav_xml_chardata(void *data, const XML_Char *s, int len) {
102 struct cr_parms *crp = (struct cr_parms *) data;
104 char *app = malloc(len+1);
111 #ifdef DEBUG_XML_PARSE
116 for (i=0; i<crp->tag_nesting_level; ++i) {
117 strcat(indent, "··");
119 syslog(LOG_DEBUG, "%s%s", indent, app, len);
123 if (crp->Chardata == NULL) {
124 crp->Chardata = NewStrBuf();
127 StrBufAppendBufPlain(crp->Chardata, app, len, 0);
134 // Called by caldav_report_one_item() to fetch a message (by number) in the current room,
135 // and return only the icalendar data as a StrBuf. Returns NULL if not found.
137 // NOTE: this function expects that "MSGP text/calendar" was issued at the beginning
138 // of a REPORT operation to set our preferred MIME type to calendar data.
139 StrBuf *fetch_ical(struct ctdlsession *c, long msgnum) {
143 ctdl_printf(c, "MSG4 %ld", msgnum);
144 ctdl_readline(c, buf, sizeof(buf));
149 while (ctdl_readline(c, buf, sizeof(buf)), strcmp(buf, "000")) {
150 if (Buf != NULL) { // already in body
151 StrBufAppendPrintf(Buf, "%s\n", buf);
153 else if (IsEmptyStr(buf)) { // beginning of body
162 // Called by multiple REPORT types to actually perform the output in "multiget" format.
163 // We need to already know the source message number and the href, but also already have the output data.
164 void cal_multiget_out(long msgnum, StrBuf *ThisHref, StrBuf *Caldata, StrBuf *ReportOut) {
166 StrBufAppendPrintf(ReportOut, "<D:response>");
167 StrBufAppendPrintf(ReportOut, "<D:href>");
168 StrBufXMLEscAppend(ReportOut, ThisHref, NULL, 0, 0);
169 StrBufAppendPrintf(ReportOut, "</D:href>");
170 StrBufAppendPrintf(ReportOut, "<D:propstat>");
172 if (Caldata != NULL) {
173 // syslog(LOG_DEBUG, "caldav_report_one_item(%s) 200 OK", ChrPtr(ThisHref));
174 StrBufAppendPrintf(ReportOut, "<D:status>");
175 StrBufAppendPrintf(ReportOut, "HTTP/1.1 200 OK");
176 StrBufAppendPrintf(ReportOut, "</D:status>");
177 StrBufAppendPrintf(ReportOut, "<D:prop>");
178 StrBufAppendPrintf(ReportOut, "<D:getetag>");
179 StrBufAppendPrintf(ReportOut, "%ld", msgnum);
180 StrBufAppendPrintf(ReportOut, "</D:getetag>");
181 StrBufAppendPrintf(ReportOut, "<C:calendar-data>");
182 StrBufXMLEscAppend(ReportOut, Caldata, NULL, 0, 0);
183 StrBufAppendPrintf(ReportOut, "</C:calendar-data>");
184 StrBufAppendPrintf(ReportOut, "</D:prop>");
187 // syslog(LOG_DEBUG, "caldav_report_one_item(%s) 404 not found", ChrPtr(ThisHref));
188 StrBufAppendPrintf(ReportOut, "<D:status>");
189 StrBufAppendPrintf(ReportOut, "HTTP/1.1 404 not found");
190 StrBufAppendPrintf(ReportOut, "</D:status>");
193 StrBufAppendPrintf(ReportOut, "</D:propstat>");
194 StrBufAppendPrintf(ReportOut, "</D:response>");
198 // Called by caldav_report() to output a single item.
199 // Our policy is to throw away the list of properties the client asked for, and just send everything.
200 void caldav_report_one_item(struct http_transaction *h, struct ctdlsession *c, StrBuf *ReportOut, StrBuf *ThisHref) {
202 StrBuf *Caldata = NULL;
205 euid = strrchr(ChrPtr(ThisHref), '/');
210 euid = (char *) ChrPtr(ThisHref);
213 char *unescaped_euid = strdup(euid);
214 if (!unescaped_euid) {
217 unescape_input(unescaped_euid);
219 msgnum = locate_message_by_uid(c, unescaped_euid);
220 free(unescaped_euid);
222 Caldata = fetch_ical(c, msgnum);
228 cal_multiget_out(msgnum, ThisHref, Caldata, ReportOut);
230 if (Caldata != NULL) {
231 FreeStrBuf(&Caldata);
236 // Called by report_the_room_itself() in room_functions.c when a CalDAV REPORT method
237 // is requested on a calendar room. We fire up an XML Parser to decode the request and
238 // hopefully produce the correct output.
239 void caldav_report(struct http_transaction *h, struct ctdlsession *c) {
243 memset(&crp, 0, sizeof(struct cr_parms));
245 XML_Parser xp = XML_ParserCreateNS("UTF-8", ':');
247 syslog(LOG_INFO, "Cannot create XML parser!");
252 XML_SetElementHandler(xp, caldav_xml_start, caldav_xml_end);
253 XML_SetCharacterDataHandler(xp, caldav_xml_chardata);
254 XML_SetUserData(xp, &crp);
255 XML_SetDefaultHandler(xp, NULL); // Disable internal entity expansion to prevent "billion laughs attack"
256 XML_Parse(xp, h->request_body, h->request_body_length, 1);
259 if (crp.Chardata != NULL) { // Discard any trailing chardata ... normally nothing here
260 FreeStrBuf(&crp.Chardata);
264 // We're going to make a lot of MSG4 calls, and the preferred MIME type we want is "text/calendar".
265 // The iCalendar standard is mature now, and we are no longer interested in text/x-vcal or application/ics.
266 ctdl_printf(c, "MSGP text/calendar");
267 ctdl_readline(c, buf, sizeof buf);
269 // Now begin the REPORT.
270 syslog(LOG_DEBUG, "CalDAV REPORT type is: %d", crp.report_type);
271 StrBuf *ReportOut = NewStrBuf();
272 StrBufAppendPrintf(ReportOut,
273 "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
276 "xmlns:C=\"urn:ietf:params:xml:ns:caldav\""
280 // RFC4791 7.8 "calendar-query" REPORT - Client will send a lot of search criteria.
281 if (crp.report_type == cr_calendar_query) {
283 Array *msglist = get_msglist(c, "ALL");
284 if (msglist != NULL) {
285 for (i = 0; i < array_len(msglist); ++i) {
287 memcpy(&m, array_get_element_at(msglist, i), sizeof(long));
289 // load and parse one calendar item
290 StrBuf *one_item = fetch_ical(c, m);
291 icalcomponent *cal = icalcomponent_new_from_string(ChrPtr(one_item));
293 // qualify will be set to nonzero if this calendar item is a match for the QUERY.
296 // this is a horrible temporary hack to output every item (for now)
299 // Did this calendar item match the query? If so, output it.
301 // FIXME need to populate the Href instead of NULL
302 cal_multiget_out(m, NULL, one_item, ReportOut);
305 icalcomponent_free(cal);
306 FreeStrBuf(&one_item);
313 // RFC4791 7.9 "calendar-multiget" REPORT - go get the specific Hrefs the client asked for.
314 // Can we move this back into citserver too?
315 else if ( (crp.report_type == cr_calendar_multiget) && (crp.Hrefs != NULL) ) {
317 StrBuf *ThisHref = NewStrBuf();
318 const char *pvset = NULL;
319 while (StrBufExtract_NextToken(ThisHref, crp.Hrefs, &pvset, '|') >= 0) {
320 StrBufTrim(ThisHref); // remove leading/trailing whitespace from the href
321 caldav_report_one_item(h, c, ReportOut, ThisHref);
323 FreeStrBuf(&ThisHref);
326 // RFC4791 7.10 "free-busy-query" REPORT
327 else if (crp.report_type == cr_freebusy_query) {
328 // FIXME build this REPORT. At the moment we send an empty multistatus.
331 // Free any query parameters that might have been allocated during the xml parse
332 if (crp.Hrefs != NULL) {
333 FreeStrBuf(&crp.Hrefs);
337 StrBufAppendPrintf(ReportOut, "</D:multistatus>\n"); // End the REPORT.
339 add_response_header(h, strdup("Content-type"), strdup("text/xml"));
340 h->response_code = 207;
341 h->response_string = strdup("Multi-Status");
342 h->response_body_length = StrLength(ReportOut);
343 h->response_body = SmashStrBuf(&ReportOut);