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