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