removed a bunch of blank comment lines
[citadel.git] / webcit / dav_get.c
1 /*
2  * Handles GroupDAV GET requests.
3  *
4  * Copyright (c) 2005-2012 by the citadel.org team
5  *
6  * This program is open source software.  You can redistribute it and/or
7  * modify it under the terms of the GNU General Public License, version 3.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #include "webcit.h"
16 #include "webserver.h"
17 #include "dav.h"
18
19
20 /*
21  * Fetch the entire contents of the room as one big ics file.
22  * This is for "webcal://" type access.
23  */     
24 void dav_get_big_ics(void) {
25         char buf[1024];
26
27         serv_puts("ICAL getics");
28         serv_getln(buf, sizeof buf);
29         if (buf[0] != '1') {
30                 hprintf("HTTP/1.1 404 not found\r\n");
31                 dav_common_headers();
32                 hprintf("Content-Type: text/plain\r\n");
33                 begin_burst();
34                 wc_printf("%s\r\n",
35                         &buf[4]
36                         );
37                 end_burst();
38                 return;
39         }
40
41         hprintf("HTTP/1.1 200 OK\r\n");
42         dav_common_headers();
43         hprintf("Content-type: text/calendar; charset=UTF-8\r\n");
44         begin_burst();
45         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
46                 wc_printf("%s\r\n", buf);
47         }
48         end_burst();
49 }
50
51
52 /* 
53  * MIME parser callback function for dav_get()
54  * Helps identify the relevant section of a multipart message
55  */
56 void extract_preferred(char *name, char *filename, char *partnum, char *disp,
57                         void *content, char *cbtype, char *cbcharset,
58                         size_t length, char *encoding, char *cbid, void *userdata)
59 {
60         struct epdata *epdata = (struct epdata *)userdata;
61         int hit = 0;
62
63         /* We only want the first one that we found */
64         if (!IsEmptyStr(epdata->found_section)) return;
65
66         /* Check for a content type match */
67         if (strlen(epdata->desired_content_type_1) > 0) {
68                 if (!strcasecmp(epdata->desired_content_type_1, cbtype)) {
69                         hit = 1;
70                 }
71         }
72         if (!IsEmptyStr(epdata->desired_content_type_2)) {
73                 if (!strcasecmp(epdata->desired_content_type_2, cbtype)) {
74                         hit = 1;
75                 }
76         }
77
78         /* Is this the one?  If so, output it. */
79         if (hit) {
80                 safestrncpy(epdata->found_section, partnum, sizeof epdata->found_section);
81                 if (!IsEmptyStr(cbcharset)) {
82                         safestrncpy(epdata->charset, cbcharset, sizeof epdata->charset);
83                 }
84                 hprintf("Content-type: %s; charset=%s\r\n", cbtype, epdata->charset);
85                 begin_burst();
86                 StrBufAppendBufPlain(WC->WBuf, content, length, 0);
87                 end_burst();
88         }
89 }
90
91
92
93 /*
94  * The pathname is always going to take one of two formats:
95  * /groupdav/room_name/euid     (GroupDAV)
96  * /groupdav/room_name          (webcal)
97  */
98 void dav_get(void)
99 {
100         wcsession *WCC = WC;
101         StrBuf *dav_roomname;
102         StrBuf *dav_uid;
103         long dav_msgnum = (-1);
104         char buf[1024];
105         int in_body = 0;
106         char *ptr;
107         char *endptr;
108         char *msgtext = NULL;
109         size_t msglen = 0;
110         size_t msgalloc = 0;
111         int linelen;
112         char content_type[128];
113         char charset[128];
114         char date[128];
115         struct epdata epdata;
116
117         if (StrBufNum_tokens(WCC->Hdr->HR.ReqLine, '/') < 2) {
118                 hprintf("HTTP/1.1 404 not found\r\n");
119                 dav_common_headers();
120                 hprintf("Content-Type: text/plain\r\n");
121                 wc_printf("The object you requested was not found.\r\n");
122                 end_burst();
123                 return;
124         }
125
126         dav_roomname = NewStrBuf();;
127         dav_uid = NewStrBuf();;
128         StrBufExtract_token(dav_roomname, WCC->Hdr->HR.ReqLine, 0, '/');
129         StrBufExtract_token(dav_uid, WCC->Hdr->HR.ReqLine, 1, '/');
130         if ((!strcasecmp(ChrPtr(dav_uid), "ics")) || 
131             (!strcasecmp(ChrPtr(dav_uid), "calendar.ics"))) {
132                 FlushStrBuf(dav_uid);
133         }
134
135         /* Go to the correct room. */
136         if (strcasecmp(ChrPtr(WCC->CurRoom.name), ChrPtr(dav_roomname))) {
137                 gotoroom(dav_roomname);
138         }
139         if (strcasecmp(ChrPtr(WCC->CurRoom.name), ChrPtr(dav_roomname))) {
140                 hprintf("HTTP/1.1 404 not found\r\n");
141                 dav_common_headers();
142                 hprintf("Content-Type: text/plain\r\n");
143                 wc_printf("There is no folder called \"%s\" on this server.\r\n",
144                         ChrPtr(dav_roomname));
145                 end_burst();
146                 FreeStrBuf(&dav_roomname);
147                 FreeStrBuf(&dav_uid);
148                 return;
149         }
150
151         /** GET on the collection itself returns an ICS of the entire collection.
152          */
153         if (StrLength(dav_uid) == 0) {
154                 dav_get_big_ics();
155                 FreeStrBuf(&dav_roomname);
156                 FreeStrBuf(&dav_uid);
157                 return;
158         }
159
160         dav_msgnum = locate_message_by_uid(ChrPtr(dav_uid));
161         serv_printf("MSG2 %ld", dav_msgnum);
162         serv_getln(buf, sizeof buf);
163         if (buf[0] != '1') {
164                 hprintf("HTTP/1.1 404 not found\r\n");
165                 dav_common_headers();
166                 hprintf("Content-Type: text/plain\r\n");
167                 wc_printf("Object \"%s\" was not found in the \"%s\" folder.\r\n",
168                         ChrPtr(dav_uid),
169                         ChrPtr(dav_roomname));
170                 end_burst();
171                 FreeStrBuf(&dav_roomname);
172                 FreeStrBuf(&dav_uid);
173                 return;
174         }
175         FreeStrBuf(&dav_roomname);
176         FreeStrBuf(&dav_uid);
177
178         /* We got it; a message is now arriving from the server.  Read it in. */
179
180         in_body = 0;
181         strcpy(charset, "UTF-8");
182         strcpy(content_type, "text/plain");
183         strcpy(date, "");
184         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
185                 linelen = strlen(buf);
186
187                 /* Append it to the buffer */
188                 if ((msglen + linelen + 3) > msgalloc) {
189                         msgalloc = ( (msgalloc > 0) ? (msgalloc * 2) : 1024 );
190                         msgtext = realloc(msgtext, msgalloc);
191                 }
192                 strcpy(&msgtext[msglen], buf);
193                 msglen += linelen;
194                 strcpy(&msgtext[msglen], "\n");
195                 msglen += 1;
196
197                 /* Also learn some things about the message */
198                 if (linelen == 0) {
199                         in_body = 1;
200                 }
201                 if (!in_body) {
202                         if (!strncasecmp(buf, "Date:", 5)) {
203                                 safestrncpy(date, &buf[5], sizeof date);
204                                 striplt(date);
205                         }
206                         if (!strncasecmp(buf, "Content-type:", 13)) {
207                                 safestrncpy(content_type, &buf[13], sizeof content_type);
208                                 striplt(content_type);
209                                 ptr = bmstrcasestr(&buf[13], "charset=");
210                                 if (ptr) {
211                                         safestrncpy(charset, ptr+8, sizeof charset);
212                                         striplt(charset);
213                                         endptr = strchr(charset, ';');
214                                         if (endptr != NULL) strcpy(endptr, "");
215                                 }
216                                 endptr = strchr(content_type, ';');
217                                 if (endptr != NULL) strcpy(endptr, "");
218                         }
219                 }
220         }
221         msgtext[msglen] = 0;
222
223         /* Output headers common to single or multi part messages */
224
225         hprintf("HTTP/1.1 200 OK\r\n");
226         dav_common_headers();
227         hprintf("etag: \"%ld\"\r\n", dav_msgnum);
228         hprintf("Date: %s\r\n", date);
229
230         memset(&epdata, 0, sizeof(struct epdata));
231         safestrncpy(epdata.charset, charset, sizeof epdata.charset);
232
233         /* If we have a multipart message on our hands, and we are in a groupware room,
234          * strip it down to only the relevant part.
235          */
236         if (!strncasecmp(content_type, "multipart/", 10)) {
237
238                 if ( (WCC->CurRoom.defview == VIEW_CALENDAR) || (WCC->CurRoom.defview == VIEW_TASKS) ) {
239                         strcpy(epdata.desired_content_type_1, "text/calendar");
240                 }
241
242                 else if (WCC->CurRoom.defview == VIEW_ADDRESSBOOK) {
243                         strcpy(epdata.desired_content_type_1, "text/vcard");
244                         strcpy(epdata.desired_content_type_2, "text/x-vcard");
245                 }
246
247                 mime_parser(msgtext, &msgtext[msglen], extract_preferred, NULL, NULL, (void *)&epdata, 0);
248         }
249
250         /* If epdata.found_section is empty, we haven't output anything yet, so output the whole thing */
251
252         if (IsEmptyStr(epdata.found_section)) {
253                 ptr = msgtext;
254                 endptr = &msgtext[msglen];
255         
256                 hprintf("Content-type: %s; charset=%s\r\n", content_type, charset);
257         
258                 in_body = 0;
259                 do {
260                         ptr = memreadline(ptr, buf, sizeof buf);
261         
262                         if (in_body) {
263                                 wc_printf("%s\r\n", buf);
264                         }
265                         else if ((buf[0] == 0) && (in_body == 0)) {
266                                 in_body = 1;
267                                 begin_burst();
268                         }
269                 } while (ptr < endptr);
270         
271                 end_burst();
272         }
273
274         free(msgtext);
275 }