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