new license declaration text
[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         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, "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
244                            "<D:multistatus " "xmlns:D=\"DAV:\" " "xmlns:C=\"urn:ietf:params:xml:ns:caldav\"" ">");
245
246         if (crp.Hrefs != NULL) {        // Output all qualifying calendar items!
247                 StrBuf *ThisHref = NewStrBuf();
248                 const char *pvset = NULL;
249                 while (StrBufExtract_NextToken(ThisHref, crp.Hrefs, &pvset, '|') >= 0) {
250                         caldav_response(h, c, ReportOut, ThisHref);
251                 }
252                 FreeStrBuf(&ThisHref);
253                 FreeStrBuf(&crp.Hrefs);
254                 crp.Hrefs = NULL;
255         }
256
257         StrBufAppendPrintf(ReportOut, "</D:multistatus>\n");    // End the REPORT.
258
259         add_response_header(h, strdup("Content-type"), strdup("text/xml"));
260         h->response_code = 207;
261         h->response_string = strdup("Multi-Status");
262         h->response_body_length = StrLength(ReportOut);
263         h->response_body = SmashStrBuf(&ReportOut);
264 }