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 if (crp->Chardata != NULL) {
80 syslog(LOG_DEBUG, "%sCHARDATA: '%s'", indent, ChrPtr(crp->Chardata));
82 syslog(LOG_DEBUG, "%s</%s>", indent, el);
86 if ((!strcasecmp(el, "DAV::href")) || (!strcasecmp(el, "DAV:href"))) {
87 if (crp->Hrefs == NULL) { // append crp->Chardata to crp->Hrefs
88 crp->Hrefs = NewStrBuf();
91 StrBufAppendBufPlain(crp->Hrefs, HKEY("|"), 0);
93 StrBufAppendBuf(crp->Hrefs, crp->Chardata, 0);
96 if (crp->Chardata != NULL) { // Tag is closed; chardata is now out of scope.
97 FreeStrBuf(&crp->Chardata); // Free the buffer.
103 // XML parser callback
104 void caldav_xml_chardata(void *data, const XML_Char * s, int len) {
105 struct cr_parms *crp = (struct cr_parms *) data;
107 if (crp->Chardata == NULL) {
108 crp->Chardata = NewStrBuf();
111 StrBufAppendBufPlain(crp->Chardata, s, len, 0);
117 // Called by caldav_report_one_item() to fetch a message (by number) in the current room,
118 // and return only the icalendar data as a StrBuf. Returns NULL if not found.
120 // NOTE: this function expects that "MSGP text/calendar" was issued at the beginning
121 // of a REPORT operation to set our preferred MIME type to calendar data.
122 StrBuf *fetch_ical(struct ctdlsession *c, long msgnum) {
126 ctdl_printf(c, "MSG4 %ld", msgnum);
127 ctdl_readline(c, buf, sizeof(buf));
132 while (ctdl_readline(c, buf, sizeof(buf)), strcmp(buf, "000")) {
133 if (Buf != NULL) { // already in body
134 StrBufAppendPrintf(Buf, "%s\n", buf);
136 else if (IsEmptyStr(buf)) { // beginning of body
145 // Called by multiple REPORT types to actually perform the output in "multiget" format.
146 // We need to already know the source message number and the href, but also already have the output data.
147 void cal_multiget_out(long msgnum, StrBuf *ThisHref, StrBuf *Caldata, StrBuf *ReportOut) {
149 StrBufAppendPrintf(ReportOut, "<D:response>");
150 StrBufAppendPrintf(ReportOut, "<D:href>");
151 StrBufXMLEscAppend(ReportOut, ThisHref, NULL, 0, 0);
152 StrBufAppendPrintf(ReportOut, "</D:href>");
153 StrBufAppendPrintf(ReportOut, "<D:propstat>");
155 if (Caldata != NULL) {
156 // syslog(LOG_DEBUG, "caldav_report_one_item(%s) 200 OK", ChrPtr(ThisHref));
157 StrBufAppendPrintf(ReportOut, "<D:status>");
158 StrBufAppendPrintf(ReportOut, "HTTP/1.1 200 OK");
159 StrBufAppendPrintf(ReportOut, "</D:status>");
160 StrBufAppendPrintf(ReportOut, "<D:prop>");
161 StrBufAppendPrintf(ReportOut, "<D:getetag>");
162 StrBufAppendPrintf(ReportOut, "%ld", msgnum);
163 StrBufAppendPrintf(ReportOut, "</D:getetag>");
164 StrBufAppendPrintf(ReportOut, "<C:calendar-data>");
165 StrBufXMLEscAppend(ReportOut, Caldata, NULL, 0, 0);
166 StrBufAppendPrintf(ReportOut, "</C:calendar-data>");
167 StrBufAppendPrintf(ReportOut, "</D:prop>");
170 // syslog(LOG_DEBUG, "caldav_report_one_item(%s) 404 not found", ChrPtr(ThisHref));
171 StrBufAppendPrintf(ReportOut, "<D:status>");
172 StrBufAppendPrintf(ReportOut, "HTTP/1.1 404 not found");
173 StrBufAppendPrintf(ReportOut, "</D:status>");
176 StrBufAppendPrintf(ReportOut, "</D:propstat>");
177 StrBufAppendPrintf(ReportOut, "</D:response>");
181 // Called by caldav_report() to output a single item.
182 // Our policy is to throw away the list of properties the client asked for, and just send everything.
183 void caldav_report_one_item(struct http_transaction *h, struct ctdlsession *c, StrBuf *ReportOut, StrBuf *ThisHref) {
185 StrBuf *Caldata = NULL;
188 euid = strrchr(ChrPtr(ThisHref), '/');
193 euid = (char *) ChrPtr(ThisHref);
196 char *unescaped_euid = strdup(euid);
197 if (!unescaped_euid) {
200 unescape_input(unescaped_euid);
202 msgnum = locate_message_by_uid(c, unescaped_euid);
203 free(unescaped_euid);
205 Caldata = fetch_ical(c, msgnum);
211 cal_multiget_out(msgnum, ThisHref, Caldata, ReportOut);
213 if (Caldata != NULL) {
214 FreeStrBuf(&Caldata);
219 // Called by report_the_room_itself() in room_functions.c when a CalDAV REPORT method
220 // is requested on a calendar room. We fire up an XML Parser to decode the request and
221 // hopefully produce the correct output.
222 void caldav_report(struct http_transaction *h, struct ctdlsession *c) {
226 memset(&crp, 0, sizeof(struct cr_parms));
228 XML_Parser xp = XML_ParserCreateNS("UTF-8", ':');
230 syslog(LOG_INFO, "Cannot create XML parser!");
235 XML_SetElementHandler(xp, caldav_xml_start, caldav_xml_end);
236 XML_SetCharacterDataHandler(xp, caldav_xml_chardata);
237 XML_SetUserData(xp, &crp);
238 XML_SetDefaultHandler(xp, NULL); // Disable internal entity expansion to prevent "billion laughs attack"
239 XML_Parse(xp, h->request_body, h->request_body_length, 1);
242 if (crp.Chardata != NULL) { // Discard any trailing chardata ... normally nothing here
243 FreeStrBuf(&crp.Chardata);
247 // We're going to make a lot of MSG4 calls, and the preferred MIME type we want is "text/calendar".
248 // The iCalendar standard is mature now, and we are no longer interested in text/x-vcal or application/ics.
249 ctdl_printf(c, "MSGP text/calendar");
250 ctdl_readline(c, buf, sizeof buf);
252 // Now begin the REPORT.
253 syslog(LOG_DEBUG, "CalDAV REPORT type is: %d", crp.report_type);
254 StrBuf *ReportOut = NewStrBuf();
255 StrBufAppendPrintf(ReportOut,
256 "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
259 "xmlns:C=\"urn:ietf:params:xml:ns:caldav\""
263 // RFC4791 7.8 "calendar-query" REPORT - Client will send a lot of search criteria.
264 if (crp.report_type == cr_calendar_query) {
266 Array *msglist = get_msglist(c, "ALL");
267 if (msglist != NULL) {
268 for (i = 0; i < array_len(msglist); ++i) {
270 memcpy(&m, array_get_element_at(msglist, i), sizeof(long));
272 // load and parse one calendar item
273 StrBuf *one_item = fetch_ical(c, m);
274 icalcomponent *cal = icalcomponent_new_from_string(ChrPtr(one_item));
276 // qualify will be set to nonzero if this calendar item is a match for the QUERY.
279 // this is a horrible temporary hack to output every item (for now)
282 // Did this calendar item match the query? If so, output it.
284 // FIXME need to populate the Href instead of NULL
285 cal_multiget_out(m, NULL, one_item, ReportOut);
288 icalcomponent_free(cal);
289 FreeStrBuf(&one_item);
296 // RFC4791 7.9 "calendar-multiget" REPORT - go get the specific Hrefs the client asked for.
297 // Can we move this back into citserver too?
298 else if ( (crp.report_type == cr_calendar_multiget) && (crp.Hrefs != NULL) ) {
300 StrBuf *ThisHref = NewStrBuf();
301 const char *pvset = NULL;
302 while (StrBufExtract_NextToken(ThisHref, crp.Hrefs, &pvset, '|') >= 0) {
303 StrBufTrim(ThisHref); // remove leading/trailing whitespace from the href
304 caldav_report_one_item(h, c, ReportOut, ThisHref);
306 FreeStrBuf(&ThisHref);
309 // RFC4791 7.10 "free-busy-query" REPORT
310 else if (crp.report_type == cr_freebusy_query) {
311 // FIXME build this REPORT. At the moment we send an empty multistatus.
314 // Free any query parameters that might have been allocated during the xml parse
315 if (crp.Hrefs != NULL) {
316 FreeStrBuf(&crp.Hrefs);
320 StrBufAppendPrintf(ReportOut, "</D:multistatus>\n"); // End the REPORT.
322 add_response_header(h, strdup("Content-type"), strdup("text/xml"));
323 h->response_code = 207;
324 h->response_string = strdup("Multi-Status");
325 h->response_body_length = StrLength(ReportOut);
326 h->response_body = SmashStrBuf(&ReportOut);