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