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