70a4e8095722d8312f763319bdf07a72f6ad97f5
[citadel.git] / webcit-ng / server / caldav_reports.c
1 // This file contains functions which handle all of the CalDAV "REPORT" queries
2 // specified in RFC4791 section 7.
3 //
4 // Copyright (c) 2023 by the citadel.org team
5 //
6 // This program is open source software.  Use, duplication, or
7 // disclosure is subject to the GNU General Public License v3.
8
9 #include "webcit.h"
10
11
12 // A CalDAV REPORT can only be one type.  This is stored in the report_type member.
13 enum cr_type {
14         cr_calendar_query,
15         cr_calendar_multiget,
16         cr_freebusy_query
17 };
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_parms {
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
28 };
29
30
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;
34         int i;
35
36         // syslog(LOG_DEBUG, "CALDAV ELEMENT START: <%s> %d", el, crp->tag_nesting_level);
37
38         for (i = 0; attr[i] != NULL; i += 2) {
39                 syslog(LOG_DEBUG, "                    Attribute '%s' = '%s'", attr[i], attr[i + 1]);
40         }
41
42         if (!strcasecmp(el, "urn:ietf:params:xml:ns:caldav:calendar-multiget")) {
43                 crp->report_type = cr_calendar_multiget;
44         }
45
46         else if (!strcasecmp(el, "urn:ietf:params:xml:ns:caldav:calendar-query")) {
47                 crp->report_type = cr_calendar_query;
48         }
49
50         else if (!strcasecmp(el, "urn:ietf:params:xml:ns:caldav:free-busy-query")) {
51                 crp->report_type = cr_freebusy_query;
52         }
53
54         ++crp->tag_nesting_level;
55 }
56
57
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;
62
63         if (crp->Chardata != NULL) {
64                 // syslog(LOG_DEBUG, "CALDAV CHARDATA     : %s", ChrPtr(crp->Chardata));
65         }
66         // syslog(LOG_DEBUG, "CALDAV ELEMENT END  : <%s> %d", el, crp->tag_nesting_level);
67
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();
71                 }
72                 else {
73                         StrBufAppendBufPlain(crp->Hrefs, HKEY("|"), 0);
74                 }
75                 StrBufAppendBuf(crp->Hrefs, crp->Chardata, 0);
76         }
77
78         if (crp->Chardata != NULL) {            // Tag is closed; chardata is now out of scope.
79                 FreeStrBuf(&crp->Chardata);     // Free the buffer.
80                 crp->Chardata = NULL;
81         }
82 }
83
84
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;
88
89         if (crp->Chardata == NULL) {
90                 crp->Chardata = NewStrBuf();
91         }
92
93         StrBufAppendBufPlain(crp->Chardata, s, len, 0);
94
95         return;
96 }
97
98
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.
101 //
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) {
105         char buf[1024];
106         StrBuf *Buf = NULL;
107
108         ctdl_printf(c, "MSG4 %ld", msgnum);
109         ctdl_readline(c, buf, sizeof(buf));
110         if (buf[0] != '1') {
111                 return NULL;
112         }
113
114         while (ctdl_readline(c, buf, sizeof(buf)), strcmp(buf, "000")) {
115                 if (Buf != NULL) {      // already in body
116                         StrBufAppendPrintf(Buf, "%s\n", buf);
117                 }
118                 else if (IsEmptyStr(buf)) {     // beginning of body
119                         Buf = NewStrBuf();
120                 }
121         }
122
123         return Buf;
124
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=
134 // webcit[13039]: ?=
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
141 // webcit[13039]:
142 // webcit[13039]: BEGIN:VCALENDAR
143 }
144
145
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) {
149         long msgnum;
150         StrBuf *Caldata = NULL;
151         char *euid;
152
153         euid = strrchr(ChrPtr(ThisHref), '/');
154         if (euid != NULL) {
155                 ++euid;
156         }
157         else {
158                 euid = (char *) ChrPtr(ThisHref);
159         }
160
161         char *unescaped_euid = strdup(euid);
162         if (!unescaped_euid) {
163                 return;
164         }
165         unescape_input(unescaped_euid);
166
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>");
172
173         msgnum = locate_message_by_uid(c, unescaped_euid);
174         free(unescaped_euid);
175         if (msgnum > 0) {
176                 Caldata = fetch_ical(c, msgnum);
177         }
178
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);
193                 Caldata = NULL;
194         }
195         else {
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>");
200         }
201
202         StrBufAppendPrintf(ReportOut, "</D:propstat>");
203         StrBufAppendPrintf(ReportOut, "</D:response>");
204 }
205
206
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) {
211         struct cr_parms crp;
212         char buf[1024];
213
214         memset(&crp, 0, sizeof(struct cr_parms));
215
216         XML_Parser xp = XML_ParserCreateNS("UTF-8", ':');
217         if (xp == NULL) {
218                 syslog(LOG_INFO, "Cannot create XML parser!");
219                 do_404(h);
220                 return;
221         }
222
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);
228         XML_ParserFree(xp);
229
230         if (crp.Chardata != NULL) {     // Discard any trailing chardata ... normally nothing here
231                 FreeStrBuf(&crp.Chardata);
232                 crp.Chardata = NULL;
233         }
234
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);
239
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\"?>"
245                 "<D:multistatus "
246                 "xmlns:D=\"DAV:\" "
247                 "xmlns:C=\"urn:ietf:params:xml:ns:caldav\""
248                 ">"
249         );
250
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);
256                 }
257                 FreeStrBuf(&ThisHref);
258                 FreeStrBuf(&crp.Hrefs);
259                 crp.Hrefs = NULL;
260         }
261
262         StrBufAppendPrintf(ReportOut, "</D:multistatus>\n");    // End the REPORT.
263
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);
269 }