1 // This file contains functions which handle all of the CalDAV "REPORT" queries
2 // specified in RFC4791 section 7.
4 // Copyright (c) 2023 by the citadel.org team
6 // This program is open source software. Use, duplication, or
7 // disclosure are subject to the GNU General Public License v3.
12 // A CalDAV REPORT can only be one type. This is stored in the report_type member.
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.
24 int tag_nesting_level; // not needed, just kept for pretty-printing
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
31 // XML parser callback
32 void caldav_xml_start(void *data, const char *el, const char **attr) {
33 struct cr_parms *crp = (struct cr_parms *) data;
36 // syslog(LOG_DEBUG, "CALDAV ELEMENT START: <%s> %d", el, crp->tag_nesting_level);
38 for (i = 0; attr[i] != NULL; i += 2) {
39 syslog(LOG_DEBUG, " Attribute '%s' = '%s'", attr[i], attr[i + 1]);
42 if (!strcasecmp(el, "urn:ietf:params:xml:ns:caldav:calendar-multiget")) {
43 crp->report_type = cr_calendar_multiget;
46 else if (!strcasecmp(el, "urn:ietf:params:xml:ns:caldav:calendar-query")) {
47 crp->report_type = cr_calendar_query;
50 else if (!strcasecmp(el, "urn:ietf:params:xml:ns:caldav:free-busy-query")) {
51 crp->report_type = cr_freebusy_query;
54 ++crp->tag_nesting_level;
58 // XML parser callback
59 void caldav_xml_end(void *data, const char *el) {
60 struct cr_parms *crp = (struct cr_parms *) data;
61 --crp->tag_nesting_level;
63 if (crp->Chardata != NULL) {
64 // syslog(LOG_DEBUG, "CALDAV CHARDATA : %s", ChrPtr(crp->Chardata));
66 // syslog(LOG_DEBUG, "CALDAV ELEMENT END : <%s> %d", el, crp->tag_nesting_level);
68 if ((!strcasecmp(el, "DAV::href")) || (!strcasecmp(el, "DAV:href"))) {
69 if (crp->Hrefs == NULL) { // append crp->Chardata to crp->Hrefs
70 crp->Hrefs = NewStrBuf();
73 StrBufAppendBufPlain(crp->Hrefs, HKEY("|"), 0);
75 StrBufAppendBuf(crp->Hrefs, crp->Chardata, 0);
78 if (crp->Chardata != NULL) { // Tag is closed; chardata is now out of scope.
79 FreeStrBuf(&crp->Chardata); // Free the buffer.
85 // XML parser callback
86 void caldav_xml_chardata(void *data, const XML_Char * s, int len) {
87 struct cr_parms *crp = (struct cr_parms *) data;
89 if (crp->Chardata == NULL) {
90 crp->Chardata = NewStrBuf();
93 StrBufAppendBufPlain(crp->Chardata, s, len, 0);
99 // Called by caldav_response() to fetch a message (by number) in the current room,
100 // and return only the icalendar data as a StrBuf. Returns NULL if not found.
102 // NOTE: this function expects that "MSGP text/calendar" was issued at the beginning
103 // of a REPORT operation to set our preferred MIME type to calendar data.
104 StrBuf *fetch_ical(struct ctdlsession * c, long msgnum) {
108 ctdl_printf(c, "MSG4 %ld", msgnum);
109 ctdl_readline(c, buf, sizeof(buf));
114 while (ctdl_readline(c, buf, sizeof(buf)), strcmp(buf, "000")) {
115 if (Buf != NULL) { // already in body
116 StrBufAppendPrintf(Buf, "%s\n", buf);
118 else if (IsEmptyStr(buf)) { // beginning of body
125 // webcit[13039]: msgn=53CE87AF-00392161@uncensored.citadel.org
126 // webcit[13039]: path=IGnatius T Foobar
127 // webcit[13039]: time=1208008800
128 // webcit[13039]: from=IGnatius T Foobar
129 // webcit[13039]: room=0000000001.Calendar
130 // webcit[13039]: node=uncnsrd
131 // webcit[13039]: hnod=Uncensored
132 // webcit[13039]: exti=040000E00074C5B7101A82E0080000000080C728F83E84C801000000000000000010000000E857E0DC57F53947ADF0BB91EE3A502F
133 // webcit[13039]: subj==?UTF-8?B?V2VzbGV5J3MgYmlydGhkYXkgcGFydHk=
135 // webcit[13039]: part=||1||text/calendar|1127||
136 // webcit[13039]: text
137 // webcit[13039]: Content-type: text/calendar
138 // webcit[13039]: Content-length: 1127
139 // webcit[13039]: Content-transfer-encoding: 7bit
140 // webcit[13039]: X-Citadel-MSG4-Partnum: 1
142 // webcit[13039]: BEGIN:VCALENDAR
146 // Called by caldav_report() to output a single item.
147 // Our policy is to throw away the list of properties the client asked for, and just send everything.
148 void caldav_response(struct http_transaction *h, struct ctdlsession *c, StrBuf *ReportOut, StrBuf *ThisHref) {
150 StrBuf *Caldata = NULL;
153 euid = strrchr(ChrPtr(ThisHref), '/');
158 euid = (char *) ChrPtr(ThisHref);
161 char *unescaped_euid = strdup(euid);
162 if (!unescaped_euid) {
165 unescape_input(unescaped_euid);
167 StrBufAppendPrintf(ReportOut, "<D:response>");
168 StrBufAppendPrintf(ReportOut, "<D:href>");
169 StrBufXMLEscAppend(ReportOut, ThisHref, NULL, 0, 0);
170 StrBufAppendPrintf(ReportOut, "</D:href>");
171 StrBufAppendPrintf(ReportOut, "<D:propstat>");
173 msgnum = locate_message_by_uid(c, unescaped_euid);
174 free(unescaped_euid);
176 Caldata = fetch_ical(c, msgnum);
179 if (Caldata != NULL) {
180 // syslog(LOG_DEBUG, "caldav_response(%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>");
192 FreeStrBuf(&Caldata);
196 // syslog(LOG_DEBUG, "caldav_response(%s) 404 not found", ChrPtr(ThisHref));
197 StrBufAppendPrintf(ReportOut, "<D:status>");
198 StrBufAppendPrintf(ReportOut, "HTTP/1.1 404 not found");
199 StrBufAppendPrintf(ReportOut, "</D:status>");
202 StrBufAppendPrintf(ReportOut, "</D:propstat>");
203 StrBufAppendPrintf(ReportOut, "</D:response>");
207 // Called by report_the_room_itself() in room_functions.c when a CalDAV REPORT method
208 // is requested on a calendar room. We fire up an XML Parser to decode the request and
209 // hopefully produce the correct output.
210 void caldav_report(struct http_transaction *h, struct ctdlsession *c) {
214 memset(&crp, 0, sizeof(struct cr_parms));
216 XML_Parser xp = XML_ParserCreateNS("UTF-8", ':');
218 syslog(LOG_INFO, "Cannot create XML parser!");
223 XML_SetElementHandler(xp, caldav_xml_start, caldav_xml_end);
224 XML_SetCharacterDataHandler(xp, caldav_xml_chardata);
225 XML_SetUserData(xp, &crp);
226 XML_SetDefaultHandler(xp, NULL); // Disable internal entity expansion to prevent "billion laughs attack"
227 XML_Parse(xp, h->request_body, h->request_body_length, 1);
230 if (crp.Chardata != NULL) { // Discard any trailing chardata ... normally nothing here
231 FreeStrBuf(&crp.Chardata);
235 // We're going to make a lot of MSG4 calls, and the preferred MIME type we want is "text/calendar".
236 // The iCalendar standard is mature now, and we are no longer interested in text/x-vcal or application/ics.
237 ctdl_printf(c, "MSGP text/calendar");
238 ctdl_readline(c, buf, sizeof buf);
240 // Now begin the REPORT.
241 syslog(LOG_DEBUG, "CalDAV REPORT type is: %d", crp.report_type);
242 StrBuf *ReportOut = NewStrBuf();
243 StrBufAppendPrintf(ReportOut,
244 "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
247 "xmlns:C=\"urn:ietf:params:xml:ns:caldav\""
251 if (crp.Hrefs != NULL) { // Output all qualifying calendar items!
252 StrBuf *ThisHref = NewStrBuf();
253 const char *pvset = NULL;
254 while (StrBufExtract_NextToken(ThisHref, crp.Hrefs, &pvset, '|') >= 0) {
255 caldav_response(h, c, ReportOut, ThisHref);
257 FreeStrBuf(&ThisHref);
258 FreeStrBuf(&crp.Hrefs);
262 StrBufAppendPrintf(ReportOut, "</D:multistatus>\n"); // End the REPORT.
264 add_response_header(h, strdup("Content-type"), strdup("text/xml"));
265 h->response_code = 207;
266 h->response_string = strdup("Multi-Status");
267 h->response_body_length = StrLength(ReportOut);
268 h->response_body = SmashStrBuf(&ReportOut);