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