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 ++crp->tag_nesting_level;
42 for (i=0; i<crp->tag_nesting_level; ++i) {
45 for (i = 0; attr[i] != NULL; i += 2) {
46 syslog(LOG_DEBUG, "%sAttribute '%s' = '%s'", indent, attr[i], attr[i + 1]);
51 // RFC4791 7.8 "calendar-query" REPORT - Client will send a lot of search criteria.
52 if (!strcasecmp(el, "urn:ietf:params:xml:ns:caldav:calendar-query")) {
53 crp->report_type = cr_calendar_query;
56 // RFC4791 7.9 "calendar-multiget" REPORT - Client will supply a list of specific hrefs.
57 else if (!strcasecmp(el, "urn:ietf:params:xml:ns:caldav:calendar-multiget")) {
58 crp->report_type = cr_calendar_multiget;
61 // RFC4791 7.10 "free-busy-query" REPORT
62 else if (!strcasecmp(el, "urn:ietf:params:xml:ns:caldav:free-busy-query")) {
63 crp->report_type = cr_freebusy_query;
69 // XML parser callback
70 void caldav_xml_end(void *data, const char *el) {
71 struct cr_parms *crp = (struct cr_parms *) data;
73 --crp->tag_nesting_level;
75 #ifdef DEBUG_XML_PARSE
80 for (i=0; i<crp->tag_nesting_level; ++i) {
83 syslog(LOG_DEBUG, "%s</%s>", indent, el);
87 if ((!strcasecmp(el, "DAV::href")) || (!strcasecmp(el, "DAV:href"))) {
88 if (crp->Hrefs == NULL) { // append crp->Chardata to crp->Hrefs
89 crp->Hrefs = NewStrBuf();
92 StrBufAppendBufPlain(crp->Hrefs, HKEY("|"), 0);
94 StrBufAppendBuf(crp->Hrefs, crp->Chardata, 0);
97 if (crp->Chardata != NULL) { // Tag is closed; chardata is now out of scope.
98 FreeStrBuf(&crp->Chardata); // Free the buffer.
104 // XML parser callback
105 void caldav_xml_chardata(void *data, const XML_Char *s, int len) {
106 struct cr_parms *crp = (struct cr_parms *) data;
108 char *app = malloc(len+1);
115 if (crp->Chardata == NULL) {
116 crp->Chardata = NewStrBuf();
119 StrBufAppendBufPlain(crp->Chardata, app, len, 0);
121 #ifdef DEBUG_XML_PARSE
123 string_trim(app); // remove leading/trailing whitespace. ok to mangle it because we've already appended.
124 if (!IsEmptyStr(app)) {
128 for (i=0; i<crp->tag_nesting_level; ++i) {
129 strcat(indent, "··");
131 syslog(LOG_DEBUG, "%s%s", indent, app, len);
141 // Called by caldav_report_one_item() to fetch a message (by number) in the current room,
142 // and return only the icalendar data as a StrBuf. Returns NULL if not found.
144 // NOTE: this function expects that "MSGP text/calendar" was issued at the beginning
145 // of a REPORT operation to set our preferred MIME type to calendar data.
146 StrBuf *fetch_ical(struct ctdlsession *c, long msgnum) {
150 ctdl_printf(c, "MSG4 %ld", msgnum);
151 ctdl_readline(c, buf, sizeof(buf));
156 while (ctdl_readline(c, buf, sizeof(buf)), strcmp(buf, "000")) {
157 if (Buf != NULL) { // already in body
158 StrBufAppendPrintf(Buf, "%s\n", buf);
160 else if (IsEmptyStr(buf)) { // beginning of body
169 // Called by multiple REPORT types to actually perform the output in "multiget" format.
170 // We need to already know the source message number and the href, but also already have the output data.
171 void cal_multiget_out(long msgnum, StrBuf *ThisHref, StrBuf *Caldata, StrBuf *ReportOut) {
173 StrBufAppendPrintf(ReportOut, "<D:response>");
174 StrBufAppendPrintf(ReportOut, "<D:href>");
175 StrBufXMLEscAppend(ReportOut, ThisHref, NULL, 0, 0);
176 StrBufAppendPrintf(ReportOut, "</D:href>");
177 StrBufAppendPrintf(ReportOut, "<D:propstat>");
179 if (Caldata != NULL) {
180 // syslog(LOG_DEBUG, "caldav_report_one_item(%s) 200 OK", ChrPtr(ThisHref));
181 StrBufAppendPrintf(ReportOut, "<D:status>");
182 StrBufAppendPrintf(ReportOut, "HTTP/1.1 200 OK");
183 StrBufAppendPrintf(ReportOut, "</D:status>");
184 StrBufAppendPrintf(ReportOut, "<D:prop>");
185 StrBufAppendPrintf(ReportOut, "<D:getetag>");
186 StrBufAppendPrintf(ReportOut, "%ld", msgnum);
187 StrBufAppendPrintf(ReportOut, "</D:getetag>");
188 StrBufAppendPrintf(ReportOut, "<C:calendar-data>");
189 StrBufXMLEscAppend(ReportOut, Caldata, NULL, 0, 0);
190 StrBufAppendPrintf(ReportOut, "</C:calendar-data>");
191 StrBufAppendPrintf(ReportOut, "</D:prop>");
194 // syslog(LOG_DEBUG, "caldav_report_one_item(%s) 404 not found", ChrPtr(ThisHref));
195 StrBufAppendPrintf(ReportOut, "<D:status>");
196 StrBufAppendPrintf(ReportOut, "HTTP/1.1 404 not found");
197 StrBufAppendPrintf(ReportOut, "</D:status>");
200 StrBufAppendPrintf(ReportOut, "</D:propstat>");
201 StrBufAppendPrintf(ReportOut, "</D:response>");
205 // Called by caldav_report() to output a single item.
206 // Our policy is to throw away the list of properties the client asked for, and just send everything.
207 void caldav_report_one_item(struct http_transaction *h, struct ctdlsession *c, StrBuf *ReportOut, StrBuf *ThisHref) {
209 StrBuf *Caldata = NULL;
212 euid = strrchr(ChrPtr(ThisHref), '/');
217 euid = (char *) ChrPtr(ThisHref);
220 char *unescaped_euid = strdup(euid);
221 if (!unescaped_euid) {
224 unescape_input(unescaped_euid);
226 msgnum = locate_message_by_uid(c, unescaped_euid);
227 free(unescaped_euid);
229 Caldata = fetch_ical(c, msgnum);
235 cal_multiget_out(msgnum, ThisHref, Caldata, ReportOut);
237 if (Caldata != NULL) {
238 FreeStrBuf(&Caldata);
243 // Called by report_the_room_itself() in room_functions.c when a CalDAV REPORT method
244 // is requested on a calendar room. We fire up an XML Parser to decode the request and
245 // hopefully produce the correct output.
246 void caldav_report(struct http_transaction *h, struct ctdlsession *c) {
250 memset(&crp, 0, sizeof(struct cr_parms));
252 XML_Parser xp = XML_ParserCreateNS("UTF-8", ':');
254 syslog(LOG_INFO, "Cannot create XML parser!");
259 XML_SetElementHandler(xp, caldav_xml_start, caldav_xml_end);
260 XML_SetCharacterDataHandler(xp, caldav_xml_chardata);
261 XML_SetUserData(xp, &crp);
262 XML_SetDefaultHandler(xp, NULL); // Disable internal entity expansion to prevent "billion laughs attack"
263 XML_Parse(xp, h->request_body, h->request_body_length, 1);
266 if (crp.Chardata != NULL) { // Discard any trailing chardata ... normally nothing here
267 FreeStrBuf(&crp.Chardata);
271 // We're going to make a lot of MSG4 calls, and the preferred MIME type we want is "text/calendar".
272 // The iCalendar standard is mature now, and we are no longer interested in text/x-vcal or application/ics.
273 ctdl_printf(c, "MSGP text/calendar");
274 ctdl_readline(c, buf, sizeof buf);
276 // Now begin the REPORT.
277 syslog(LOG_DEBUG, "CalDAV REPORT type is: %d", crp.report_type);
278 StrBuf *ReportOut = NewStrBuf();
279 StrBufAppendPrintf(ReportOut,
280 "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
283 "xmlns:C=\"urn:ietf:params:xml:ns:caldav\""
287 // RFC4791 7.8 "calendar-query" REPORT - Client will send a lot of search criteria.
288 if (crp.report_type == cr_calendar_query) {
290 Array *msglist = get_msglist(c, "ALL");
291 if (msglist != NULL) {
292 for (i = 0; i < array_len(msglist); ++i) {
294 memcpy(&m, array_get_element_at(msglist, i), sizeof(long));
296 // load and parse one calendar item
297 StrBuf *one_item = fetch_ical(c, m);
298 icalcomponent *cal = icalcomponent_new_from_string(ChrPtr(one_item));
300 // qualify will be set to nonzero if this calendar item is a match for the QUERY.
303 // this is a horrible temporary hack to output every item (for now)
306 // Did this calendar item match the query? If so, output it.
308 // FIXME need to populate the Href instead of NULL
309 cal_multiget_out(m, NULL, one_item, ReportOut);
312 icalcomponent_free(cal);
313 FreeStrBuf(&one_item);
320 // RFC4791 7.9 "calendar-multiget" REPORT - go get the specific Hrefs the client asked for.
321 // Can we move this back into citserver too?
322 else if ( (crp.report_type == cr_calendar_multiget) && (crp.Hrefs != NULL) ) {
324 StrBuf *ThisHref = NewStrBuf();
325 const char *pvset = NULL;
326 while (StrBufExtract_NextToken(ThisHref, crp.Hrefs, &pvset, '|') >= 0) {
327 StrBufTrim(ThisHref); // remove leading/trailing whitespace from the href
328 caldav_report_one_item(h, c, ReportOut, ThisHref);
330 FreeStrBuf(&ThisHref);
333 // RFC4791 7.10 "free-busy-query" REPORT
334 else if (crp.report_type == cr_freebusy_query) {
335 // FIXME build this REPORT. At the moment we send an empty multistatus.
338 // Free any query parameters that might have been allocated during the xml parse
339 if (crp.Hrefs != NULL) {
340 FreeStrBuf(&crp.Hrefs);
344 StrBufAppendPrintf(ReportOut, "</D:multistatus>\n"); // End the REPORT.
346 add_response_header(h, strdup("Content-type"), strdup("text/xml"));
347 h->response_code = 207;
348 h->response_string = strdup("Multi-Status");
349 h->response_body_length = StrLength(ReportOut);
350 h->response_body = SmashStrBuf(&ReportOut);