indent -kr -i8 -l132 on everything in webcit-ng
[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) 2018 by the citadel.org team
6  *
7  * This program is open source software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 3.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15
16 #include "webcit.h"
17
18
19 /*
20  * A CalDAV REPORT can only be one type.  This is stored in the report_type member.
21  */
22 enum cr_type {
23         cr_calendar_query,
24         cr_calendar_multiget,
25         cr_freebusy_query
26 };
27
28
29 /*
30  * Data type for CalDAV Report Parameters.
31  * As we slog our way through the XML we learn what the client is asking for
32  * and build up the contents of this data type.
33  */
34 struct cr_parms {
35         int tag_nesting_level;          // not needed, just kept for pretty-printing
36         enum cr_type report_type;       // which RFC4791 section 7 REPORT are we generating
37         StrBuf *Chardata;               // XML chardata in between tags is built up here
38         StrBuf *Hrefs;                  // list of items requested by a calendar-multiget report
39 };
40
41
42 /*
43  * XML parser callback
44  */
45 void caldav_xml_start(void *data, const char *el, const char **attr)
46 {
47         struct cr_parms *crp = (struct cr_parms *) data;
48         int i;
49
50         // syslog(LOG_DEBUG, "CALDAV ELEMENT START: <%s> %d", el, crp->tag_nesting_level);
51
52         for (i = 0; attr[i] != NULL; i += 2) {
53                 syslog(LOG_DEBUG, "                    Attribute '%s' = '%s'", attr[i], attr[i + 1]);
54         }
55
56         if (!strcasecmp(el, "urn:ietf:params:xml:ns:caldav:calendar-multiget")) {
57                 crp->report_type = cr_calendar_multiget;
58         }
59
60         else if (!strcasecmp(el, "urn:ietf:params:xml:ns:caldav:calendar-query")) {
61                 crp->report_type = cr_calendar_query;
62         }
63
64         else if (!strcasecmp(el, "urn:ietf:params:xml:ns:caldav:free-busy-query")) {
65                 crp->report_type = cr_freebusy_query;
66         }
67
68         ++crp->tag_nesting_level;
69 }
70
71
72 /*
73  * XML parser callback
74  */
75 void caldav_xml_end(void *data, const char *el)
76 {
77         struct cr_parms *crp = (struct cr_parms *) data;
78         --crp->tag_nesting_level;
79
80         if (crp->Chardata != NULL) {
81                 // syslog(LOG_DEBUG, "CALDAV CHARDATA     : %s", ChrPtr(crp->Chardata));
82         }
83         // syslog(LOG_DEBUG, "CALDAV ELEMENT END  : <%s> %d", el, crp->tag_nesting_level);
84
85         if ((!strcasecmp(el, "DAV::href")) || (!strcasecmp(el, "DAV:href"))) {
86                 if (crp->Hrefs == NULL) {       // append crp->Chardata to crp->Hrefs
87                         crp->Hrefs = NewStrBuf();
88                 } else {
89                         StrBufAppendBufPlain(crp->Hrefs, HKEY("|"), 0);
90                 }
91                 StrBufAppendBuf(crp->Hrefs, crp->Chardata, 0);
92         }
93
94         if (crp->Chardata != NULL) {            // Tag is closed; chardata is now out of scope.
95                 FreeStrBuf(&crp->Chardata);     // Free the buffer.
96                 crp->Chardata = NULL;
97         }
98 }
99
100
101 /*
102  * XML parser callback
103  */
104 void caldav_xml_chardata(void *data, const XML_Char * s, int len)
105 {
106         struct cr_parms *crp = (struct cr_parms *) data;
107
108         if (crp->Chardata == NULL) {
109                 crp->Chardata = NewStrBuf();
110         }
111
112         StrBufAppendBufPlain(crp->Chardata, s, len, 0);
113
114         return;
115 }
116
117
118 /*
119  * Called by caldav_response() to fetch a message (by number) in the current room,
120  * and return only the icalendar data as a StrBuf.  Returns NULL if not found.
121  *
122  * NOTE: this function expects that "MSGP text/calendar" was issued at the beginning
123  * of a REPORT operation to set our preferred MIME type to calendar data.
124  */
125 StrBuf *fetch_ical(struct ctdlsession * c, long msgnum)
126 {
127         char buf[1024];
128         StrBuf *Buf = NULL;
129
130         ctdl_printf(c, "MSG4 %ld", msgnum);
131         ctdl_readline(c, buf, sizeof(buf));
132         if (buf[0] != '1') {
133                 return NULL;
134         }
135
136         while (ctdl_readline(c, buf, sizeof(buf)), strcmp(buf, "000")) {
137                 if (Buf != NULL) {      // already in body
138                         StrBufAppendPrintf(Buf, "%s\n", buf);
139                 } else if (IsEmptyStr(buf)) {   // beginning of body
140                         Buf = NewStrBuf();
141                 }
142         }
143
144         return Buf;
145
146 // webcit[13039]: msgn=53CE87AF-00392161@uncensored.citadel.org
147 // webcit[13039]: path=IGnatius T Foobar
148 // webcit[13039]: time=1208008800
149 // webcit[13039]: from=IGnatius T Foobar
150 // webcit[13039]: room=0000000001.Calendar
151 // webcit[13039]: node=uncnsrd
152 // webcit[13039]: hnod=Uncensored
153 // webcit[13039]: exti=040000008200E00074C5B7101A82E0080000000080C728F83E84C801000000000000000010000000E857E0DC57F53947ADF0BB91EE3A502F
154 // webcit[13039]: subj==?UTF-8?B?V2VzbGV5J3MgYmlydGhkYXkgcGFydHk=
155 // webcit[13039]: ?=
156 // webcit[13039]: part=||1||text/calendar|1127||
157 // webcit[13039]: text
158 // webcit[13039]: Content-type: text/calendar
159 // webcit[13039]: Content-length: 1127
160 // webcit[13039]: Content-transfer-encoding: 7bit
161 // webcit[13039]: X-Citadel-MSG4-Partnum: 1
162 // webcit[13039]:
163 // webcit[13039]: BEGIN:VCALENDAR
164 }
165
166
167 /*
168  * Called by caldav_report() to output a single item.
169  * Our policy is to throw away the list of properties the client asked for, and just send everything.
170  */
171 void caldav_response(struct http_transaction *h, struct ctdlsession *c, StrBuf * ReportOut, StrBuf * ThisHref)
172 {
173         long msgnum;
174         StrBuf *Caldata = NULL;
175         char *euid;
176
177         euid = strrchr(ChrPtr(ThisHref), '/');
178         if (euid != NULL) {
179                 ++euid;
180         } else {
181                 euid = (char *) ChrPtr(ThisHref);
182         }
183
184         char *unescaped_euid = strdup(euid);
185         if (!unescaped_euid)
186                 return;
187         unescape_input(unescaped_euid);
188
189         StrBufAppendPrintf(ReportOut, "<D:response>");
190         StrBufAppendPrintf(ReportOut, "<D:href>");
191         StrBufXMLEscAppend(ReportOut, ThisHref, NULL, 0, 0);
192         StrBufAppendPrintf(ReportOut, "</D:href>");
193         StrBufAppendPrintf(ReportOut, "<D:propstat>");
194
195         msgnum = locate_message_by_uid(c, unescaped_euid);
196         free(unescaped_euid);
197         if (msgnum > 0) {
198                 Caldata = fetch_ical(c, msgnum);
199         }
200
201         if (Caldata != NULL) {
202                 // syslog(LOG_DEBUG, "caldav_response(%s) 200 OK", ChrPtr(ThisHref));
203                 StrBufAppendPrintf(ReportOut, "<D:status>");
204                 StrBufAppendPrintf(ReportOut, "HTTP/1.1 200 OK");
205                 StrBufAppendPrintf(ReportOut, "</D:status>");
206                 StrBufAppendPrintf(ReportOut, "<D:prop>");
207                 StrBufAppendPrintf(ReportOut, "<D:getetag>");
208                 StrBufAppendPrintf(ReportOut, "%ld", msgnum);
209                 StrBufAppendPrintf(ReportOut, "</D:getetag>");
210                 StrBufAppendPrintf(ReportOut, "<C:calendar-data>");
211                 StrBufXMLEscAppend(ReportOut, Caldata, NULL, 0, 0);
212                 StrBufAppendPrintf(ReportOut, "</C:calendar-data>");
213                 StrBufAppendPrintf(ReportOut, "</D:prop>");
214                 FreeStrBuf(&Caldata);
215                 Caldata = NULL;
216         } else {
217                 // syslog(LOG_DEBUG, "caldav_response(%s) 404 not found", ChrPtr(ThisHref));
218                 StrBufAppendPrintf(ReportOut, "<D:status>");
219                 StrBufAppendPrintf(ReportOut, "HTTP/1.1 404 not found");
220                 StrBufAppendPrintf(ReportOut, "</D:status>");
221         }
222
223         StrBufAppendPrintf(ReportOut, "</D:propstat>");
224         StrBufAppendPrintf(ReportOut, "</D:response>");
225 }
226
227
228 /*
229  * Called by report_the_room_itself() in room_functions.c when a CalDAV REPORT method
230  * is requested on a calendar room.  We fire up an XML Parser to decode the request and
231  * hopefully produce the correct output.
232  */
233 void caldav_report(struct http_transaction *h, struct ctdlsession *c)
234 {
235         struct cr_parms crp;
236         char buf[1024];
237
238         memset(&crp, 0, sizeof(struct cr_parms));
239
240         XML_Parser xp = XML_ParserCreateNS("UTF-8", ':');
241         if (xp == NULL) {
242                 syslog(LOG_INFO, "Cannot create XML parser!");
243                 do_404(h);
244                 return;
245         }
246
247         XML_SetElementHandler(xp, caldav_xml_start, caldav_xml_end);
248         XML_SetCharacterDataHandler(xp, caldav_xml_chardata);
249         XML_SetUserData(xp, &crp);
250         XML_SetDefaultHandler(xp, NULL);        // Disable internal entity expansion to prevent "billion laughs attack"
251         XML_Parse(xp, h->request_body, h->request_body_length, 1);
252         XML_ParserFree(xp);
253
254         if (crp.Chardata != NULL) {     // Discard any trailing chardata ... normally nothing here
255                 FreeStrBuf(&crp.Chardata);
256                 crp.Chardata = NULL;
257         }
258
259         /*
260          * We're going to make a lot of MSG4 calls, and the preferred MIME type we want is "text/calendar".
261          * The iCalendar standard is mature now, and we are no longer interested in text/x-vcal or application/ics.
262          */
263         ctdl_printf(c, "MSGP text/calendar");
264         ctdl_readline(c, buf, sizeof buf);
265
266         /*
267          * Now begin the REPORT.
268          */
269         syslog(LOG_DEBUG, "CalDAV REPORT type is: %d", crp.report_type);
270         StrBuf *ReportOut = NewStrBuf();
271         StrBufAppendPrintf(ReportOut, "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
272                            "<D:multistatus " "xmlns:D=\"DAV:\" " "xmlns:C=\"urn:ietf:params:xml:ns:caldav\"" ">");
273
274         if (crp.Hrefs != NULL) {        // Output all qualifying calendar items!
275                 StrBuf *ThisHref = NewStrBuf();
276                 const char *pvset = NULL;
277                 while (StrBufExtract_NextToken(ThisHref, crp.Hrefs, &pvset, '|') >= 0) {
278                         caldav_response(h, c, ReportOut, ThisHref);
279                 }
280                 FreeStrBuf(&ThisHref);
281                 FreeStrBuf(&crp.Hrefs);
282                 crp.Hrefs = NULL;
283         }
284
285         StrBufAppendPrintf(ReportOut, "</D:multistatus>\n");    // End the REPORT.
286
287         add_response_header(h, strdup("Content-type"), strdup("text/xml"));
288         h->response_code = 207;
289         h->response_string = strdup("Multi-Status");
290         h->response_body_length = StrLength(ReportOut);
291         h->response_body = SmashStrBuf(&ReportOut);
292 }