8930bfb328b325d626323b586498c67aef3eeab1
[citadel.git] / webcit-ng / server / caldav_reports.c
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.
4
5 #include "webcit.h"
6
7 #define CALDAV "urn:ietf:params:xml:ns:caldav:"         // Shorthand for the XML namespace of CalDAV
8 #define CALDAVLEN sizeof(CALDAV)-1                      // And the length of that string
9
10 const char *the_beginning_of_time       = "19010101T010101Z";
11 const char *the_end_of_time             = "99991231T235959Z";
12
13 // A CalDAV REPORT can only be one type.  This is stored in the report_type member.
14 enum cr_type {
15         cr_calendar_query,
16         cr_calendar_multiget,
17         cr_freebusy_query
18 };
19
20 // Data type for CalDAV Report Parameters.
21 // As we slog our way through the XML we learn what the client is asking for
22 // and build up the contents of this data type.
23 struct cr_params {
24         int comp_filter_nesting_level;
25         enum cr_type report_type;       // which RFC4791 section 7 REPORT are we generating
26         StrBuf *Chardata;               // XML chardata in between tags is built up here
27         StrBuf *Hrefs;                  // list of items requested by a `calendar-multiget` REPORT
28         Array *filters;                 // If the query contains a FILTER stanza, the filter criteria are populated here
29         int filter_nest;                // tag nesting level where a FILTER stanza begins
30 };
31
32
33 // XML parser callback
34 void caldav_xml_start(void *data, const char *el, const char **attr) {
35         struct cr_params *crp = (struct cr_params *) data;
36
37 #ifdef DEBUG_XML_PARSE
38         // logging
39         syslog(LOG_DEBUG, "<%s>", el);
40         for (int i = 0; attr[i] != NULL; i += 2) {
41                 syslog(LOG_DEBUG, "Attribute '%s' = '%s'", attr[i], attr[i + 1]);
42         }
43         // end logging
44 #endif
45
46         // RFC4791 7.8 "calendar-query" REPORT - Client will send a lot of search criteria.
47         if (!strcasecmp(el, CALDAV"calendar-query")) {
48                 crp->report_type = cr_calendar_query;
49         }
50
51         // RFC4791 7.9 "calendar-multiget" REPORT - Client will supply a list of specific hrefs.
52         else if (!strcasecmp(el, CALDAV"calendar-multiget")) {
53                 crp->report_type = cr_calendar_multiget;
54         }
55
56         // RFC4791 7.10 "free-busy-query" REPORT
57         else if (!strcasecmp(el, CALDAV"free-busy-query")) {
58                 crp->report_type = cr_freebusy_query;
59         }
60
61         // RFC4791 9.7 create a filter array if this query contains a "filter" stanza
62         else if (!strcasecmp(el, CALDAV"filter")) {
63                 crp->filters = array_new(SIZ);
64                 crp->filter_nest = crp->comp_filter_nesting_level;
65         }
66
67         // Handle the filters defined in RFC4791 9.7.1 through 9.7.5
68         else if (       (       (!strcasecmp(el, CALDAV"comp-filter"))
69                                 || (!strcasecmp(el, CALDAV"prop-filter"))
70                                 || (!strcasecmp(el, CALDAV"param-filter"))
71                                 || (!strcasecmp(el, CALDAV"is-not-defined"))
72                                 || (!strcasecmp(el, CALDAV"text-match"))
73                                 || (!strcasecmp(el, CALDAV"time-range"))
74                         )
75                         && (crp->filters)                       // Make sure we actually allocated an array
76         ) {
77
78                 if (!strcasecmp(el, CALDAV"comp-filter")) {
79                         ++crp->comp_filter_nesting_level;
80                 }
81
82                 char newfilter[SIZ];
83                 int a = 0;
84                 int len = snprintf(newfilter, SIZ, "%d|", crp->comp_filter_nesting_level - crp->filter_nest - 1);
85                 len += snprintf(&newfilter[len], SIZ-len, "%s", &el[CALDAVLEN]);        // filter name without the namespace
86                 while (attr[a]) {
87                         len += snprintf(&newfilter[len], SIZ-len, "|%s", attr[a++]);    // now save the attributes
88                 }
89                 array_append(crp->filters, newfilter);
90         }
91 }
92
93
94 // XML parser callback
95 void caldav_xml_end(void *data, const char *el) {
96         struct cr_params *crp = (struct cr_params *) data;
97
98 #ifdef DEBUG_XML_PARSE
99         // logging
100         int i;
101         syslog(LOG_DEBUG, "</%s>", el);
102         // end logging
103 #endif
104
105         if (!strcasecmp(el, CALDAV"comp-filter")) {
106                 --crp->comp_filter_nesting_level;
107         }
108
109
110         if ((!strcasecmp(el, "DAV::href")) || (!strcasecmp(el, "DAV:href"))) {
111                 if (crp->Hrefs == NULL) {       // append crp->Chardata to crp->Hrefs
112                         crp->Hrefs = NewStrBuf();
113                 }
114                 else {
115                         StrBufAppendBufPlain(crp->Hrefs, HKEY("|"), 0);
116                 }
117                 StrBufAppendBuf(crp->Hrefs, crp->Chardata, 0);
118         }
119
120         if (crp->Chardata != NULL) {            // Tag is closed; chardata is now out of scope.
121                 FreeStrBuf(&crp->Chardata);     // Free the buffer.
122                 crp->Chardata = NULL;
123         }
124 }
125
126
127 // XML parser callback
128 void caldav_xml_chardata(void *data, const XML_Char *s, int len) {
129         struct cr_params *crp = (struct cr_params *) data;
130
131         char *app = malloc(len+1);
132         if (!app) {
133                 return;
134         }
135         memcpy(app, s, len);
136         app[len] = 0;
137
138         if (crp->Chardata == NULL) {
139                 crp->Chardata = NewStrBuf();
140         }
141
142         StrBufAppendBufPlain(crp->Chardata, app, len, 0);
143
144 #ifdef DEBUG_XML_PARSE
145         // logging
146         string_trim(app);               // remove leading/trailing whitespace.  ok to mangle it because we've already appended.
147         if (!IsEmptyStr(app)) {
148                 int i;
149                 syslog(LOG_DEBUG, "%s", app, len);
150         }
151         // end logging
152 #endif
153
154         free(app);
155         return;
156 }
157
158
159 // Called by caldav_report_one_item() to fetch a message (by number) in the current room,
160 // and return only the icalendar data as a StrBuf.  Returns NULL if not found.
161 //
162 // NOTE: this function expects that "MSGP text/calendar" was issued at the beginning
163 // of a REPORT operation to set our preferred MIME type to calendar data.
164 StrBuf *fetch_ical(struct ctdlsession *c, long msgnum) {
165         char buf[1024];
166         StrBuf *Buf = NULL;
167
168         ctdl_printf(c, "MSG4 %ld", msgnum);
169         ctdl_readline(c, buf, sizeof(buf));
170         if (buf[0] != '1') {
171                 return NULL;
172         }
173
174         while (ctdl_readline(c, buf, sizeof(buf)), strcmp(buf, "000")) {
175                 if (Buf != NULL) {              // already in body
176                         StrBufAppendPrintf(Buf, "%s\n", buf);
177                 }
178                 else if (IsEmptyStr(buf)) {     // beginning of body
179                         Buf = NewStrBuf();
180                 }
181         }
182
183         return Buf;
184 }
185
186
187 // Called by multiple REPORT types to actually perform the output in "multiget" format.
188 // We need to already know the source message number and the href, but also already have the output data.
189 void cal_multiget_out(long msgnum, StrBuf *ThisHref, StrBuf *Caldata, StrBuf *ReportOut) {
190
191         StrBufAppendPrintf(ReportOut, "<D:response>");
192         StrBufAppendPrintf(ReportOut, "<D:href>");
193         StrBufXMLEscAppend(ReportOut, ThisHref, NULL, 0, 0);
194         StrBufAppendPrintf(ReportOut, "</D:href>");
195         StrBufAppendPrintf(ReportOut, "<D:propstat>");
196
197         if (Caldata != NULL) {
198                 // syslog(LOG_DEBUG, "caldav_report_one_item(%s) 200 OK", ChrPtr(ThisHref));
199                 StrBufAppendPrintf(ReportOut, "<D:status>");
200                 StrBufAppendPrintf(ReportOut, "HTTP/1.1 200 OK");
201                 StrBufAppendPrintf(ReportOut, "</D:status>");
202                 StrBufAppendPrintf(ReportOut, "<D:prop>");
203                 StrBufAppendPrintf(ReportOut, "<D:getetag>");
204                 StrBufAppendPrintf(ReportOut, "%ld", msgnum);
205                 StrBufAppendPrintf(ReportOut, "</D:getetag>");
206                 StrBufAppendPrintf(ReportOut, "<C:calendar-data>");
207                 StrBufXMLEscAppend(ReportOut, Caldata, NULL, 0, 0);
208                 StrBufAppendPrintf(ReportOut, "</C:calendar-data>");
209                 StrBufAppendPrintf(ReportOut, "</D:prop>");
210         }
211         else {
212                 // syslog(LOG_DEBUG, "caldav_report_one_item(%s) 404 not found", ChrPtr(ThisHref));
213                 StrBufAppendPrintf(ReportOut, "<D:status>");
214                 StrBufAppendPrintf(ReportOut, "HTTP/1.1 404 not found");
215                 StrBufAppendPrintf(ReportOut, "</D:status>");
216         }
217
218         StrBufAppendPrintf(ReportOut, "</D:propstat>");
219         StrBufAppendPrintf(ReportOut, "</D:response>");
220 }
221
222
223 // Called by caldav_report() to output a single item.
224 // Our policy is to throw away the list of properties the client asked for, and just send everything.
225 void caldav_report_one_item(struct http_transaction *h, struct ctdlsession *c, StrBuf *ReportOut, StrBuf *ThisHref) {
226         long msgnum;
227         StrBuf *Caldata = NULL;
228         char *euid;
229
230         euid = strrchr(ChrPtr(ThisHref), '/');
231         if (euid != NULL) {
232                 ++euid;
233         }
234         else {
235                 euid = (char *) ChrPtr(ThisHref);
236         }
237
238         char *unescaped_euid = strdup(euid);
239         if (!unescaped_euid) {
240                 return;
241         }
242         unescape_input(unescaped_euid);
243
244         msgnum = locate_message_by_uid(c, unescaped_euid);
245         free(unescaped_euid);
246         if (msgnum > 0) {
247                 Caldata = fetch_ical(c, msgnum);
248         }
249         else {
250                 Caldata = NULL;
251         }
252
253         cal_multiget_out(msgnum, ThisHref, Caldata, ReportOut);
254
255         if (Caldata != NULL) {
256                 FreeStrBuf(&Caldata);
257         }
258 }
259
260
261 // Compare function for "time-range" tests (RFC4791 section 9.9)
262 // Returns nonzero if the supplied icalcomponent occurs within the specified time range
263 //
264 // IMPLEMENTATION NOTE:
265 // ical_ctdl_is_overlap() works because icaltime_compare() is really smart.
266 // It looks at the time zone of the dtstart/dtend and can apparently go back up the icalcomponent
267 // hierarchy to find its time zone data.  I tested this by creating an event with a fictional
268 // time zone and it did the right thing.  It even showed the fictional name to me.  This saves us
269 // from having to convert everything to UTC before comparing.  Nice!
270 //
271 int caldav_time_range_filter_matches(icalcomponent *cal, char *start_str, char *end_str) {
272         struct icaltimetype dtstart = icalcomponent_get_dtstart(cal);
273         struct icaltimetype dtend = icalcomponent_get_dtend(cal);
274
275         struct icaltimetype search_start = icaltime_from_string(start_str);
276         syslog(LOG_DEBUG, "   search start: \033[36m%-16s\033[0m (%s)", icaltime_as_ical_string_r(search_start), icaltime_get_tzid(search_start));
277
278         struct icaltimetype search_end = icaltime_from_string(end_str);
279         syslog(LOG_DEBUG, "   search   end: \033[36m%-16s\033[0m (%s)", icaltime_as_ical_string_r(search_end), icaltime_get_tzid(search_end));
280
281         // If it is a recurring event, RRULE is available at this level.  We can handle it here.
282
283         icalproperty *rrule;                                                                                                          
284         rrule = icalcomponent_get_first_property(cal, ICAL_RRULE_PROPERTY);
285         if (rrule) {
286                 if (icaltime_is_null_time(dtend)) {
287                         dtend = dtstart;
288                 }
289                 struct icaldurationtype dur = icaltime_subtract(dtend, dtstart);        // recurrences need duration to find dtend
290                 struct icalrecurrencetype recur = icalproperty_get_rrule(rrule);
291
292                 icalrecur_iterator *ritr = icalrecur_iterator_new(recur, dtstart);      // iterate through recurrences
293                 int rcount = 0;
294                 syslog(LOG_DEBUG, "\033[7m RECURRENCE: \033[0m");
295                 while (dtstart = icalrecur_iterator_next(ritr), !icaltime_is_null_time(dtstart)) {
296                         dtend = icaltime_add(dtstart, dur);
297                         syslog(LOG_DEBUG, "recurrence %3d start: \033[36m%-16s\033[0m (%s)", rcount, icaltime_as_ical_string_r(dtstart), icaltime_get_tzid(dtstart));
298                         syslog(LOG_DEBUG, "recurrence %3d   end: \033[36m%-16s\033[0m (%s)", rcount, icaltime_as_ical_string_r(dtend), icaltime_get_tzid(dtend));
299
300                         // Does THIS recurrence match the query?
301                         if (ical_ctdl_is_overlap(dtstart, dtend, search_start, search_end)) {
302                                 icalrecur_iterator_free(ritr);
303                                 return(1);
304                         }
305
306                         ++rcount;
307                 }
308
309                 icalrecur_iterator_free(ritr);
310                 return(0);                              // compared all recurrences, no match was found for any of them
311         }
312
313         // For non recurring events, do a simple time range compare.
314         syslog(LOG_DEBUG, "event start: \033[36m%-16s\033[0m (%s)", icaltime_as_ical_string_r(dtstart), icaltime_get_tzid(dtstart));
315         syslog(LOG_DEBUG, "event   end: \033[36m%-16s\033[0m (%s)", icaltime_as_ical_string_r(dtend), icaltime_get_tzid(dtend));
316         return(ical_ctdl_is_overlap(dtstart, dtend, search_start, search_end)); // We have a convenience function for this.
317 }
318
319
320 // Recursive function to apply CalDAV FILTERS to a calendar item.
321 // Returns zero if the calendar item was disqualified by a filter, nonzero if the calendar item still qualifies.
322 int caldav_apply_filters(void *cal, Array *filters, int apply_at_level) {
323
324         int f = 0;                                              // filter number iterator
325         int qual = 1;                                           // 0 for disqualify, 1 for qualify
326         int previous_level = -1;
327         int disregard_further_comp_filters = 0;
328
329         while ( (f<array_len(filters)) && (qual) ) {
330
331                 // Tokenize the filter (a future performance hack would be to pre-tokenize instead of storing delimited strings)
332                 char this_filter[SIZ];
333                 safestrncpy(this_filter, array_get_element_at(filters, f), sizeof(this_filter));
334                 char *t[10] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL } ;
335                 char *ft = this_filter;
336                 int num_tokens = 0;
337                 while ( (t[num_tokens]=strtok_r(ft, "|", &ft)) && (num_tokens<10) ) {
338                         ++num_tokens;
339                 }
340                 int this_rule_level = atoi(t[0]);
341                 syslog(LOG_DEBUG, "caldav_apply_filters() filter=%d, level=%d, <%s>", f, this_rule_level, array_get_element_at(filters, f) );
342
343                 // Handle the individual filters defined in RFC4791 9.7.1 through 9.7.5
344
345                 if (apply_at_level < previous_level) {
346                         syslog(LOG_DEBUG, "caldav: walking back down");
347                         return(qual);
348                 }
349
350                 else if (this_rule_level != apply_at_level) {
351                         syslog(LOG_DEBUG, "caldav: apply_at_level=%d, this_rule_level=%d, skipping this rule", apply_at_level, this_rule_level);
352                 }
353
354                 else if (       (!strcasecmp(t[1], "comp-filter"))              // RFC4791 9.7.1 - filter by component
355                                 && (!disregard_further_comp_filters)            // one is enough to succeed
356                         ) {
357                         syslog(LOG_DEBUG, "component filter at level %d", this_rule_level);
358
359                         // comp-filter requires exactly one parameter (name="VXXXX")
360                         if (num_tokens < 4) {
361                                 syslog(LOG_DEBUG, "caldav: comp-filter has no parameters - rejecting");
362                                 return(0);
363                         }
364
365                         // Root element is NOT a component, but the root filter is "comp-filter" -- reject!
366                         if ( (!icalcomponent_isa_component(cal)) && (this_rule_level == 0) ) {
367                                 syslog(LOG_DEBUG, "caldav: root element is not a component, rejecting");
368                                 return(0);
369                         }
370
371                         // Current element is a component and the filter is "comp-filter" -- see if it matches the requested type
372                         if (    (icalcomponent_isa_component(cal))
373                                 && (!strcasecmp(t[2], "name"))
374                         ) {
375                                 if (icalcomponent_isa(cal) == icalcomponent_string_to_kind(t[3]) ) {
376                                         syslog(LOG_DEBUG, "caldav: component at level %d is <%s>, looking for <%s>, recursing...",
377                                                 apply_at_level,
378                                                 icalcomponent_kind_to_string(icalcomponent_isa(cal)), t[3]
379                                         );
380
381                                         // We have a match.  Drill down into the subcomponents.
382
383                                         icalcomponent *c = NULL;
384                                         int number_of_subcomponents = 0;
385                                         int number_of_matches = 0;
386                                         for (   c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
387                                                 (c != 0);                                                
388                                                 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)
389                                         ) {
390                                                 ++number_of_subcomponents;
391                                                 if (caldav_apply_filters(c, filters, apply_at_level+1)) {
392                                                         syslog(LOG_DEBUG, "Subcomponent %d might match", number_of_subcomponents);
393                                                         ++number_of_matches;
394                                                 }
395                                         }
396                                         if (number_of_matches > 0) {                    // something matched
397                                                 qual = 1;
398                                                 disregard_further_comp_filters = 1;
399
400                                         }
401                                         else if (number_of_subcomponents > 0) {         // nothing matched
402                                                 return(0);                              // but only fail if there *were* subcomponents.
403                                         }
404
405                                 }
406                                 else {
407                                         syslog(LOG_DEBUG, "caldav: component at level %d is <%s>, looking for <%s>, rejecting",
408                                                 apply_at_level,
409                                                 icalcomponent_kind_to_string(icalcomponent_isa(cal)),
410                                                 t[3]
411                                         );
412                                         return(0);
413                                 }
414                         }
415
416                 }
417
418                 else if (!strcasecmp(t[1], "prop-filter")) {                    // RFC4791 9.7.2 - filter by property
419                         syslog(LOG_DEBUG, "property filter at level %d FIXME not implemented yet", this_rule_level);
420                 }
421
422                 else if (!strcasecmp(t[1], "param-filter")) {                   // RFC4791 9.7.3 - filter by parameter
423                         syslog(LOG_DEBUG, "parameter filter at level %d FIXME not implemented yet", this_rule_level);
424                 }
425
426                 else if (!strcasecmp(t[1], "is-not-defined")) {                 // RFC4791 9.7.4
427                         syslog(LOG_DEBUG, "is-not-defined filter at level %d FIXME not implemented yet", this_rule_level);
428                 }
429
430                 else if (!strcasecmp(t[1], "text-match")) {                     // RFC4791 9.7.5
431                         syslog(LOG_DEBUG, "text match filter at level %d FIXME not implemented yet", this_rule_level);
432                 }
433
434                 else if (!strcasecmp(t[1], "time-range")) {                     // RFC4791 9.9
435                         syslog(LOG_DEBUG, "time range filter at level %d FIXME add recurrence", this_rule_level);
436                         for (int i=2; (i+1)<num_tokens; i+=2) {
437                                 char *tr_start  = (char *)the_beginning_of_time;        // default if not specified
438                                 char *tr_end    = (char *)the_end_of_time;              // default if not specified
439                                 if (!strcasecmp(t[i], "start")) {
440                                         tr_start = t[i+1];
441                                 }
442                                 else if (!strcasecmp(t[i], "end")) {
443                                         tr_end = t[i+1];
444                                 }
445                                 if (caldav_time_range_filter_matches(cal, tr_start, tr_end)) {
446                                         syslog(LOG_DEBUG, "time range matches");
447                                 }
448                                 else {
449                                         syslog(LOG_DEBUG, "time range does not match -- rejecting");
450                                         qual = 0;
451                                 }
452                         }
453                 }
454
455                 ++f;
456         }
457
458         syslog(LOG_DEBUG, "caldav: we reached the end of level %d , returning %d", apply_at_level, qual);
459         return(qual);
460 }
461
462
463 // Called by report_the_room_itself() in room_functions.c when a CalDAV REPORT method
464 // is requested on a calendar room.  We fire up an XML Parser to decode the request and
465 // hopefully produce the correct output.
466 void caldav_report(struct http_transaction *h, struct ctdlsession *c) {
467         struct cr_params crp;
468         char buf[1024];
469
470         memset(&crp, 0, sizeof(struct cr_params));
471
472         XML_Parser xp = XML_ParserCreateNS("UTF-8", ':');
473         if (xp == NULL) {
474                 syslog(LOG_INFO, "Cannot create XML parser!");
475                 do_404(h);
476                 return;
477         }
478
479         XML_SetElementHandler(xp, caldav_xml_start, caldav_xml_end);
480         XML_SetCharacterDataHandler(xp, caldav_xml_chardata);
481         XML_SetUserData(xp, &crp);
482         XML_SetDefaultHandler(xp, NULL);        // Disable internal entity expansion to prevent "billion laughs attack"
483         XML_Parse(xp, h->request_body, h->request_body_length, 1);
484         XML_ParserFree(xp);
485
486         if (crp.Chardata != NULL) {             // Discard any trailing chardata ... normally nothing here
487                 FreeStrBuf(&crp.Chardata);
488                 crp.Chardata = NULL;
489         }
490
491         // We're going to make a lot of MSG4 calls, and the preferred MIME type we want is "text/calendar".
492         // The iCalendar standard is mature now, and we are no longer interested in text/x-vcal or application/ics.
493         ctdl_printf(c, "MSGP text/calendar");
494         ctdl_readline(c, buf, sizeof buf);
495
496         // Now begin the REPORT.
497         syslog(LOG_DEBUG, "CalDAV REPORT type is: %d", crp.report_type);
498         StrBuf *ReportOut = NewStrBuf();
499         StrBufAppendPrintf(ReportOut,
500                 "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
501                 "<D:multistatus "
502                 "xmlns:D=\"DAV:\" "
503                 "xmlns:C=\"urn:ietf:params:xml:ns:caldav\""
504                 ">"
505         );
506
507         // RFC4791 7.8 "calendar-query" REPORT - Client will send a lot of search criteria.
508         if (crp.report_type == cr_calendar_query) {
509                 int i = 0;
510                 Array *msglist = get_msglist(c, "ALL");
511                 if (msglist != NULL) {
512                         for (i = 0; i < array_len(msglist); ++i) {
513                                 long m;
514                                 memcpy(&m, array_get_element_at(msglist, i), sizeof(long));
515
516                                 // load and parse one calendar item
517                                 StrBuf *one_item = fetch_ical(c, m);
518                                 icalcomponent *cal = icalcomponent_new_from_string(ChrPtr(one_item));
519
520                                 // Does this calendar item qualify for output?  Run this calendar item through the filters.
521                                 syslog(LOG_DEBUG, "Evaluating message \033[33m%ld\033[0m...", m);
522                                 if (caldav_apply_filters(cal, crp.filters, 0)) {
523                                         syslog(LOG_DEBUG, "Message %ld \033[32mQUALIFIES\033[0m");
524                                         // FIXME need to populate the Href instead of NULL
525                                         cal_multiget_out(m, NULL, one_item, ReportOut);
526                                 }
527                                 else {
528                                         syslog(LOG_DEBUG, "Message %ld \033[31mDOES NOT QUALIFY\033[0m");
529                                 }
530                                 syslog(LOG_DEBUG, "");
531
532                                 icalcomponent_free(cal);
533                                 FreeStrBuf(&one_item);
534
535                         }
536                         array_free(msglist);
537                 }
538         }
539
540         // RFC4791 7.9 "calendar-multiget" REPORT - go get the specific Hrefs the client asked for.
541         // Can we move this back into citserver too?
542         else if ( (crp.report_type == cr_calendar_multiget) && (crp.Hrefs != NULL) ) {
543
544                 StrBuf *ThisHref = NewStrBuf();
545                 const char *pvset = NULL;
546                 while (StrBufExtract_NextToken(ThisHref, crp.Hrefs, &pvset, '|') >= 0) {
547                         StrBufTrim(ThisHref);                           // remove leading/trailing whitespace from the href
548                         caldav_report_one_item(h, c, ReportOut, ThisHref);
549                 }
550                 FreeStrBuf(&ThisHref);
551         }
552
553         // RFC4791 7.10 "free-busy-query" REPORT
554         else if (crp.report_type == cr_freebusy_query) {
555                 // FIXME build this REPORT.  At the moment we send an empty multistatus.
556         }
557
558         // Free any query parameters that might have been allocated during the xml parse
559         if (crp.Hrefs != NULL) {
560                 FreeStrBuf(&crp.Hrefs);
561                 crp.Hrefs = NULL;
562         }
563         if (crp.filters) {
564                 array_free(crp.filters);
565                 crp.filters = NULL;
566         }
567
568         StrBufAppendPrintf(ReportOut, "</D:multistatus>\n");            // End the REPORT.
569
570         add_response_header(h, strdup("Content-type"), strdup("text/xml"));
571         h->response_code = 207;
572         h->response_string = strdup("Multi-Status");
573         h->response_body_length = StrLength(ReportOut);
574         h->response_body = SmashStrBuf(&ReportOut);
575 }