2 // This file contains functions which handle all of the CalDAV "REPORT" queries
3 // specified in RFC4791 section 7.
5 // Copyright (c) 2022 by the citadel.org team
7 // This program is open source software. Use, duplication, or
8 // disclosure are subject to the GNU General Public License v3.
13 // A CalDAV REPORT can only be one type. This is stored in the report_type member.
21 // Data type for CalDAV Report Parameters.
22 // As we slog our way through the XML we learn what the client is asking for
23 // and build up the contents of this data type.
25 int tag_nesting_level; // not needed, just kept for pretty-printing
26 enum cr_type report_type; // which RFC4791 section 7 REPORT are we generating
27 StrBuf *Chardata; // XML chardata in between tags is built up here
28 StrBuf *Hrefs; // list of items requested by a calendar-multiget report
32 // XML parser callback
33 void caldav_xml_start(void *data, const char *el, const char **attr) {
34 struct cr_parms *crp = (struct cr_parms *) data;
37 // syslog(LOG_DEBUG, "CALDAV ELEMENT START: <%s> %d", el, crp->tag_nesting_level);
39 for (i = 0; attr[i] != NULL; i += 2) {
40 syslog(LOG_DEBUG, " Attribute '%s' = '%s'", attr[i], attr[i + 1]);
43 if (!strcasecmp(el, "urn:ietf:params:xml:ns:caldav:calendar-multiget")) {
44 crp->report_type = cr_calendar_multiget;
47 else if (!strcasecmp(el, "urn:ietf:params:xml:ns:caldav:calendar-query")) {
48 crp->report_type = cr_calendar_query;
51 else if (!strcasecmp(el, "urn:ietf:params:xml:ns:caldav:free-busy-query")) {
52 crp->report_type = cr_freebusy_query;
55 ++crp->tag_nesting_level;
59 // XML parser callback
60 void caldav_xml_end(void *data, const char *el) {
61 struct cr_parms *crp = (struct cr_parms *) data;
62 --crp->tag_nesting_level;
64 if (crp->Chardata != NULL) {
65 // syslog(LOG_DEBUG, "CALDAV CHARDATA : %s", ChrPtr(crp->Chardata));
67 // syslog(LOG_DEBUG, "CALDAV ELEMENT END : <%s> %d", el, crp->tag_nesting_level);
69 if ((!strcasecmp(el, "DAV::href")) || (!strcasecmp(el, "DAV:href"))) {
70 if (crp->Hrefs == NULL) { // append crp->Chardata to crp->Hrefs
71 crp->Hrefs = NewStrBuf();
74 StrBufAppendBufPlain(crp->Hrefs, HKEY("|"), 0);
76 StrBufAppendBuf(crp->Hrefs, crp->Chardata, 0);
79 if (crp->Chardata != NULL) { // Tag is closed; chardata is now out of scope.
80 FreeStrBuf(&crp->Chardata); // Free the buffer.
86 // XML parser callback
87 void caldav_xml_chardata(void *data, const XML_Char * s, int len) {
88 struct cr_parms *crp = (struct cr_parms *) data;
90 if (crp->Chardata == NULL) {
91 crp->Chardata = NewStrBuf();
94 StrBufAppendBufPlain(crp->Chardata, s, len, 0);
100 // Called by caldav_response() to fetch a message (by number) in the current room,
101 // and return only the icalendar data as a StrBuf. Returns NULL if not found.
103 // NOTE: this function expects that "MSGP text/calendar" was issued at the beginning
104 // of a REPORT operation to set our preferred MIME type to calendar data.
105 StrBuf *fetch_ical(struct ctdlsession * c, long msgnum) {
109 ctdl_printf(c, "MSG4 %ld", msgnum);
110 ctdl_readline(c, buf, sizeof(buf));
115 while (ctdl_readline(c, buf, sizeof(buf)), strcmp(buf, "000")) {
116 if (Buf != NULL) { // already in body
117 StrBufAppendPrintf(Buf, "%s\n", buf);
119 else if (IsEmptyStr(buf)) { // beginning of body
126 // webcit[13039]: msgn=53CE87AF-00392161@uncensored.citadel.org
127 // webcit[13039]: path=IGnatius T Foobar
128 // webcit[13039]: time=1208008800
129 // webcit[13039]: from=IGnatius T Foobar
130 // webcit[13039]: room=0000000001.Calendar
131 // webcit[13039]: node=uncnsrd
132 // webcit[13039]: hnod=Uncensored
133 // webcit[13039]: exti=040000008200E00074C5B7101A82E0080000000080C728F83E84C801000000000000000010000000E857E0DC57F53947ADF0BB91EE3A502F
134 // webcit[13039]: subj==?UTF-8?B?V2VzbGV5J3MgYmlydGhkYXkgcGFydHk=
136 // webcit[13039]: part=||1||text/calendar|1127||
137 // webcit[13039]: text
138 // webcit[13039]: Content-type: text/calendar
139 // webcit[13039]: Content-length: 1127
140 // webcit[13039]: Content-transfer-encoding: 7bit
141 // webcit[13039]: X-Citadel-MSG4-Partnum: 1
143 // webcit[13039]: BEGIN:VCALENDAR
147 // Called by caldav_report() to output a single item.
148 // Our policy is to throw away the list of properties the client asked for, and just send everything.
149 void caldav_response(struct http_transaction *h, struct ctdlsession *c, StrBuf * ReportOut, StrBuf * ThisHref) {
151 StrBuf *Caldata = NULL;
154 euid = strrchr(ChrPtr(ThisHref), '/');
159 euid = (char *) ChrPtr(ThisHref);
162 char *unescaped_euid = strdup(euid);
163 if (!unescaped_euid) {
166 unescape_input(unescaped_euid);
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>");
174 msgnum = locate_message_by_uid(c, unescaped_euid);
175 free(unescaped_euid);
177 Caldata = fetch_ical(c, msgnum);
180 if (Caldata != NULL) {
181 // syslog(LOG_DEBUG, "caldav_response(%s) 200 OK", ChrPtr(ThisHref));
182 StrBufAppendPrintf(ReportOut, "<D:status>");
183 StrBufAppendPrintf(ReportOut, "HTTP/1.1 200 OK");
184 StrBufAppendPrintf(ReportOut, "</D:status>");
185 StrBufAppendPrintf(ReportOut, "<D:prop>");
186 StrBufAppendPrintf(ReportOut, "<D:getetag>");
187 StrBufAppendPrintf(ReportOut, "%ld", msgnum);
188 StrBufAppendPrintf(ReportOut, "</D:getetag>");
189 StrBufAppendPrintf(ReportOut, "<C:calendar-data>");
190 StrBufXMLEscAppend(ReportOut, Caldata, NULL, 0, 0);
191 StrBufAppendPrintf(ReportOut, "</C:calendar-data>");
192 StrBufAppendPrintf(ReportOut, "</D:prop>");
193 FreeStrBuf(&Caldata);
197 // syslog(LOG_DEBUG, "caldav_response(%s) 404 not found", ChrPtr(ThisHref));
198 StrBufAppendPrintf(ReportOut, "<D:status>");
199 StrBufAppendPrintf(ReportOut, "HTTP/1.1 404 not found");
200 StrBufAppendPrintf(ReportOut, "</D:status>");
203 StrBufAppendPrintf(ReportOut, "</D:propstat>");
204 StrBufAppendPrintf(ReportOut, "</D:response>");
208 // Called by report_the_room_itself() in room_functions.c when a CalDAV REPORT method
209 // is requested on a calendar room. We fire up an XML Parser to decode the request and
210 // hopefully produce the correct output.
211 void caldav_report(struct http_transaction *h, struct ctdlsession *c) {
215 memset(&crp, 0, sizeof(struct cr_parms));
217 XML_Parser xp = XML_ParserCreateNS("UTF-8", ':');
219 syslog(LOG_INFO, "Cannot create XML parser!");
224 XML_SetElementHandler(xp, caldav_xml_start, caldav_xml_end);
225 XML_SetCharacterDataHandler(xp, caldav_xml_chardata);
226 XML_SetUserData(xp, &crp);
227 XML_SetDefaultHandler(xp, NULL); // Disable internal entity expansion to prevent "billion laughs attack"
228 XML_Parse(xp, h->request_body, h->request_body_length, 1);
231 if (crp.Chardata != NULL) { // Discard any trailing chardata ... normally nothing here
232 FreeStrBuf(&crp.Chardata);
236 // We're going to make a lot of MSG4 calls, and the preferred MIME type we want is "text/calendar".
237 // The iCalendar standard is mature now, and we are no longer interested in text/x-vcal or application/ics.
238 ctdl_printf(c, "MSGP text/calendar");
239 ctdl_readline(c, buf, sizeof buf);
241 // Now begin the REPORT.
242 syslog(LOG_DEBUG, "CalDAV REPORT type is: %d", crp.report_type);
243 StrBuf *ReportOut = NewStrBuf();
244 StrBufAppendPrintf(ReportOut,
245 "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
248 "xmlns:C=\"urn:ietf:params:xml:ns:caldav\""
252 if (crp.Hrefs != NULL) { // Output all qualifying calendar items!
253 StrBuf *ThisHref = NewStrBuf();
254 const char *pvset = NULL;
255 while (StrBufExtract_NextToken(ThisHref, crp.Hrefs, &pvset, '|') >= 0) {
256 caldav_response(h, c, ReportOut, ThisHref);
258 FreeStrBuf(&ThisHref);
259 FreeStrBuf(&crp.Hrefs);
263 StrBufAppendPrintf(ReportOut, "</D:multistatus>\n"); // End the REPORT.
265 add_response_header(h, strdup("Content-type"), strdup("text/xml"));
266 h->response_code = 207;
267 h->response_string = strdup("Multi-Status");
268 h->response_body_length = StrLength(ReportOut);
269 h->response_body = SmashStrBuf(&ReportOut);