better logging presentation of chardata
[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
8 // A CalDAV REPORT can only be one type.  This is stored in the report_type member.
9 enum cr_type {
10         cr_calendar_query,
11         cr_calendar_multiget,
12         cr_freebusy_query
13 };
14
15
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.
19 struct cr_parms {
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
24 };
25
26
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;
30
31 #ifdef DEBUG_XML_PARSE
32         // logging
33         int i;
34         char indent[256];
35         indent[0] = 0;
36         for (i=0; i<crp->tag_nesting_level; ++i) {
37                 strcat(indent, "··");
38         }
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]);
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, "urn:ietf:params:xml:ns: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, "urn:ietf:params:xml:ns: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, "urn:ietf:params:xml:ns:caldav:free-busy-query")) {
58                 crp->report_type = cr_freebusy_query;
59         }
60
61         ++crp->tag_nesting_level;
62 }
63
64
65 // XML parser callback
66 void caldav_xml_end(void *data, const char *el) {
67         struct cr_parms *crp = (struct cr_parms *) data;
68
69         --crp->tag_nesting_level;
70
71 #ifdef DEBUG_XML_PARSE
72         // logging
73         int i;
74         char indent[256];
75         indent[0] = 0;
76         for (i=0; i<crp->tag_nesting_level; ++i) {
77                 strcat(indent, "··");
78         }
79         syslog(LOG_DEBUG, "%s</%s>", indent, el);
80         // end logging
81 #endif
82
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();
86                 }
87                 else {
88                         StrBufAppendBufPlain(crp->Hrefs, HKEY("|"), 0);
89                 }
90                 StrBufAppendBuf(crp->Hrefs, crp->Chardata, 0);
91         }
92
93         if (crp->Chardata != NULL) {            // Tag is closed; chardata is now out of scope.
94                 FreeStrBuf(&crp->Chardata);     // Free the buffer.
95                 crp->Chardata = NULL;
96         }
97 }
98
99
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;
103
104         char *app = malloc(len+1);
105         if (!app) {
106                 return;
107         }
108         memcpy(app, s, len);
109         app[len] = 0;
110
111 #ifdef DEBUG_XML_PARSE
112         // logging
113         int i;
114         char indent[256];
115         indent[0] = 0;
116         for (i=0; i<crp->tag_nesting_level; ++i) {
117                 strcat(indent, "··");
118         }
119         syslog(LOG_DEBUG, "%s%s", indent, app);
120         // end logging
121 #endif
122
123
124         if (crp->Chardata == NULL) {
125                 crp->Chardata = NewStrBuf();
126         }
127
128         //StrBufAppendBufPlain(crp->Chardata, s, len, 0);
129         StrBufAppendBufPlain(crp->Chardata, app, len, 0);
130         free(app);
131
132         return;
133 }
134
135
136 // Called by caldav_report_one_item() to fetch a message (by number) in the current room,
137 // and return only the icalendar data as a StrBuf.  Returns NULL if not found.
138 //
139 // NOTE: this function expects that "MSGP text/calendar" was issued at the beginning
140 // of a REPORT operation to set our preferred MIME type to calendar data.
141 StrBuf *fetch_ical(struct ctdlsession *c, long msgnum) {
142         char buf[1024];
143         StrBuf *Buf = NULL;
144
145         ctdl_printf(c, "MSG4 %ld", msgnum);
146         ctdl_readline(c, buf, sizeof(buf));
147         if (buf[0] != '1') {
148                 return NULL;
149         }
150
151         while (ctdl_readline(c, buf, sizeof(buf)), strcmp(buf, "000")) {
152                 if (Buf != NULL) {              // already in body
153                         StrBufAppendPrintf(Buf, "%s\n", buf);
154                 }
155                 else if (IsEmptyStr(buf)) {     // beginning of body
156                         Buf = NewStrBuf();
157                 }
158         }
159
160         return Buf;
161 }
162
163
164 // Called by multiple REPORT types to actually perform the output in "multiget" format.
165 // We need to already know the source message number and the href, but also already have the output data.
166 void cal_multiget_out(long msgnum, StrBuf *ThisHref, StrBuf *Caldata, StrBuf *ReportOut) {
167
168         StrBufAppendPrintf(ReportOut, "<D:response>");
169         StrBufAppendPrintf(ReportOut, "<D:href>");
170         StrBufXMLEscAppend(ReportOut, ThisHref, NULL, 0, 0);
171         StrBufAppendPrintf(ReportOut, "</D:href>");
172         StrBufAppendPrintf(ReportOut, "<D:propstat>");
173
174         if (Caldata != NULL) {
175                 // syslog(LOG_DEBUG, "caldav_report_one_item(%s) 200 OK", ChrPtr(ThisHref));
176                 StrBufAppendPrintf(ReportOut, "<D:status>");
177                 StrBufAppendPrintf(ReportOut, "HTTP/1.1 200 OK");
178                 StrBufAppendPrintf(ReportOut, "</D:status>");
179                 StrBufAppendPrintf(ReportOut, "<D:prop>");
180                 StrBufAppendPrintf(ReportOut, "<D:getetag>");
181                 StrBufAppendPrintf(ReportOut, "%ld", msgnum);
182                 StrBufAppendPrintf(ReportOut, "</D:getetag>");
183                 StrBufAppendPrintf(ReportOut, "<C:calendar-data>");
184                 StrBufXMLEscAppend(ReportOut, Caldata, NULL, 0, 0);
185                 StrBufAppendPrintf(ReportOut, "</C:calendar-data>");
186                 StrBufAppendPrintf(ReportOut, "</D:prop>");
187         }
188         else {
189                 // syslog(LOG_DEBUG, "caldav_report_one_item(%s) 404 not found", ChrPtr(ThisHref));
190                 StrBufAppendPrintf(ReportOut, "<D:status>");
191                 StrBufAppendPrintf(ReportOut, "HTTP/1.1 404 not found");
192                 StrBufAppendPrintf(ReportOut, "</D:status>");
193         }
194
195         StrBufAppendPrintf(ReportOut, "</D:propstat>");
196         StrBufAppendPrintf(ReportOut, "</D:response>");
197 }
198
199
200 // Called by caldav_report() to output a single item.
201 // Our policy is to throw away the list of properties the client asked for, and just send everything.
202 void caldav_report_one_item(struct http_transaction *h, struct ctdlsession *c, StrBuf *ReportOut, StrBuf *ThisHref) {
203         long msgnum;
204         StrBuf *Caldata = NULL;
205         char *euid;
206
207         euid = strrchr(ChrPtr(ThisHref), '/');
208         if (euid != NULL) {
209                 ++euid;
210         }
211         else {
212                 euid = (char *) ChrPtr(ThisHref);
213         }
214
215         char *unescaped_euid = strdup(euid);
216         if (!unescaped_euid) {
217                 return;
218         }
219         unescape_input(unescaped_euid);
220
221         msgnum = locate_message_by_uid(c, unescaped_euid);
222         free(unescaped_euid);
223         if (msgnum > 0) {
224                 Caldata = fetch_ical(c, msgnum);
225         }
226         else {
227                 Caldata = NULL;
228         }
229
230         cal_multiget_out(msgnum, ThisHref, Caldata, ReportOut);
231
232         if (Caldata != NULL) {
233                 FreeStrBuf(&Caldata);
234         }
235 }
236
237
238 // Called by report_the_room_itself() in room_functions.c when a CalDAV REPORT method
239 // is requested on a calendar room.  We fire up an XML Parser to decode the request and
240 // hopefully produce the correct output.
241 void caldav_report(struct http_transaction *h, struct ctdlsession *c) {
242         struct cr_parms crp;
243         char buf[1024];
244
245         memset(&crp, 0, sizeof(struct cr_parms));
246
247         XML_Parser xp = XML_ParserCreateNS("UTF-8", ':');
248         if (xp == NULL) {
249                 syslog(LOG_INFO, "Cannot create XML parser!");
250                 do_404(h);
251                 return;
252         }
253
254         XML_SetElementHandler(xp, caldav_xml_start, caldav_xml_end);
255         XML_SetCharacterDataHandler(xp, caldav_xml_chardata);
256         XML_SetUserData(xp, &crp);
257         XML_SetDefaultHandler(xp, NULL);        // Disable internal entity expansion to prevent "billion laughs attack"
258         XML_Parse(xp, h->request_body, h->request_body_length, 1);
259         XML_ParserFree(xp);
260
261         if (crp.Chardata != NULL) {             // Discard any trailing chardata ... normally nothing here
262                 FreeStrBuf(&crp.Chardata);
263                 crp.Chardata = NULL;
264         }
265
266         // We're going to make a lot of MSG4 calls, and the preferred MIME type we want is "text/calendar".
267         // The iCalendar standard is mature now, and we are no longer interested in text/x-vcal or application/ics.
268         ctdl_printf(c, "MSGP text/calendar");
269         ctdl_readline(c, buf, sizeof buf);
270
271         // Now begin the REPORT.
272         syslog(LOG_DEBUG, "CalDAV REPORT type is: %d", crp.report_type);
273         StrBuf *ReportOut = NewStrBuf();
274         StrBufAppendPrintf(ReportOut,
275                 "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
276                 "<D:multistatus "
277                 "xmlns:D=\"DAV:\" "
278                 "xmlns:C=\"urn:ietf:params:xml:ns:caldav\""
279                 ">"
280         );
281
282         // RFC4791 7.8 "calendar-query" REPORT - Client will send a lot of search criteria.
283         if (crp.report_type == cr_calendar_query) {
284                 int i = 0;
285                 Array *msglist = get_msglist(c, "ALL");
286                 if (msglist != NULL) {
287                         for (i = 0; i < array_len(msglist); ++i) {
288                                 long m;
289                                 memcpy(&m, array_get_element_at(msglist, i), sizeof(long));
290
291                                 // load and parse one calendar item
292                                 StrBuf *one_item = fetch_ical(c, m);
293                                 icalcomponent *cal = icalcomponent_new_from_string(ChrPtr(one_item));
294
295                                 // qualify will be set to nonzero if this calendar item is a match for the QUERY.
296                                 int qualify = 0;
297
298                                 // this is a horrible temporary hack to output every item (for now)
299                                 qualify = 1;
300
301                                 // Did this calendar item match the query?  If so, output it.
302                                 if (qualify) {
303                                         // FIXME need to populate the Href instead of NULL
304                                         cal_multiget_out(m, NULL, one_item, ReportOut);
305                                 }
306
307                                 icalcomponent_free(cal);
308                                 FreeStrBuf(&one_item);
309
310                         }
311                         array_free(msglist);
312                 }
313         }
314
315         // RFC4791 7.9 "calendar-multiget" REPORT - go get the specific Hrefs the client asked for.
316         // Can we move this back into citserver too?
317         else if ( (crp.report_type == cr_calendar_multiget) && (crp.Hrefs != NULL) ) {
318
319                 StrBuf *ThisHref = NewStrBuf();
320                 const char *pvset = NULL;
321                 while (StrBufExtract_NextToken(ThisHref, crp.Hrefs, &pvset, '|') >= 0) {
322                         StrBufTrim(ThisHref);                           // remove leading/trailing whitespace from the href
323                         caldav_report_one_item(h, c, ReportOut, ThisHref);
324                 }
325                 FreeStrBuf(&ThisHref);
326         }
327
328         // RFC4791 7.10 "free-busy-query" REPORT
329         else if (crp.report_type == cr_freebusy_query) {
330                 // FIXME build this REPORT.  At the moment we send an empty multistatus.
331         }
332
333         // Free any query parameters that might have been allocated during the xml parse
334         if (crp.Hrefs != NULL) {
335                 FreeStrBuf(&crp.Hrefs);
336                 crp.Hrefs = NULL;
337         }
338
339         StrBufAppendPrintf(ReportOut, "</D:multistatus>\n");            // End the REPORT.
340
341         add_response_header(h, strdup("Content-type"), strdup("text/xml"));
342         h->response_code = 207;
343         h->response_string = strdup("Multi-Status");
344         h->response_body_length = StrLength(ReportOut);
345         h->response_body = SmashStrBuf(&ReportOut);
346 }