* Moved to the new string tokenizer API
[citadel.git] / webcit / groupdav_propfind.c
1 /*
2  * $Id$
3  *
4  * Handles GroupDAV PROPFIND requests.
5  *
6  * A few notes about our XML output:
7  *
8  * --> Yes, we are spewing tags directly instead of using an XML library.
9  *     If you would like to rewrite this using libxml2, code it up and submit
10  *     a patch.  Whining will be summarily ignored.
11  *
12  * --> XML is deliberately output with no whitespace/newlines between tags.
13  *     This makes it difficult to read, but we have discovered clients which
14  *     crash when you try to pretty it up.
15  *
16  */
17
18 #include <ctype.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <stdio.h>
22 #include <fcntl.h>
23 #include <signal.h>
24 #include <sys/types.h>
25 #include <sys/wait.h>
26 #include <sys/socket.h>
27 #include <limits.h>
28 #include <string.h>
29 #include <pwd.h>
30 #include <errno.h>
31 #include <stdarg.h>
32 #include <time.h>
33 #include <pthread.h>
34 #include "webcit.h"
35 #include "webserver.h"
36 #include "groupdav.h"
37
38
39 /*
40  * Given an encoded UID, translate that to an unencoded Citadel EUID and
41  * then search for it in the current room.  Return a message number or -1
42  * if not found.
43  *
44  * NOTE: this function relies on the Citadel server's brute-force search.
45  * There's got to be a way to optimize this better.
46  */
47 long locate_message_by_uid(char *uid) {
48         char buf[SIZ];
49         char decoded_uid[SIZ];
50         long retval = (-1L);
51
52         /* Decode the uid */
53         euid_unescapize(decoded_uid, uid);
54
55         serv_puts("MSGS ALL|0|1");
56         serv_gets(buf);
57         if (buf[0] == '8') {
58                 serv_printf("exti|%s", decoded_uid);
59                 serv_puts("000");
60                 while (serv_gets(buf), strcmp(buf, "000")) {
61                         retval = atol(buf);
62                 }
63         }
64         return(retval);
65 }
66
67
68 /*
69  * List folders containing interesting groupware objects
70  */
71 void groupdav_folder_list(void) {
72         char buf[SIZ];
73         char roomname[SIZ];
74         int view;
75         char datestring[SIZ];
76         time_t now;
77
78         now = time(NULL);
79         http_datestring(datestring, sizeof datestring, now);
80
81         /*
82          * Be rude.  Completely ignore the XML request and simply send them
83          * everything we know about.  Let the client sort it out.
84          */
85         wprintf("HTTP/1.0 207 Multi-Status\r\n");
86         groupdav_common_headers();
87         wprintf("Date: %s\r\n", datestring);
88         wprintf("Content-type: text/xml\r\n");
89         wprintf("Content-encoding: identity\r\n");
90
91         begin_burst();
92
93         wprintf("<?xml version=\"1.0\" encoding=\"utf-8\"?>"
94                 "<D:multistatus xmlns:D=\"DAV:\">"
95         );
96
97         serv_puts("LKRA");
98         serv_gets(buf);
99         if (buf[0] == '1') while (serv_gets(buf), strcmp(buf, "000")) {
100
101                 extract_token(roomname, buf, 0, '|', sizeof roomname);
102                 view = extract_int(buf, 6);
103
104                 /*
105                  * For now, only list rooms that we know a GroupDAV client
106                  * might be interested in.  In the future we may add
107                  * the rest.
108                  */
109                 if ((view == VIEW_CALENDAR)
110                    || (view == VIEW_TASKS)
111                    || (view == VIEW_ADDRESSBOOK) ) {
112
113                         wprintf("<D:response>");
114
115                         wprintf("<D:href>");
116                         if (strlen(WC->http_host) > 0) {
117                                 wprintf("%s://%s",
118                                         (is_https ? "https" : "http"),
119                                         WC->http_host);
120                         }
121                         wprintf("/groupdav/");
122                         urlescputs(roomname);
123                         wprintf("/</D:href>");
124
125                         wprintf("<D:propstat>");
126                         wprintf("<D:status>HTTP/1.1 200 OK</D:status>");
127                         wprintf("<D:prop>");
128                         wprintf("<D:displayname>");
129                         escputs(roomname);
130                         wprintf("</D:displayname>");
131                         wprintf("<D:resourcetype><D:collection/>");
132
133                         switch(view) {
134                                 case VIEW_CALENDAR:
135                                         wprintf("<G:vevent-collection />");
136                                         break;
137                                 case VIEW_TASKS:
138                                         wprintf("<G:vtodo-collection />");
139                                         break;
140                                 case VIEW_ADDRESSBOOK:
141                                         wprintf("<G:vcard-collection />");
142                                         break;
143                         }
144
145                         wprintf("</D:resourcetype>");
146                         wprintf("</D:prop>");
147                         wprintf("</D:propstat>");
148                         wprintf("</D:response>");
149                 }
150         }
151         wprintf("</D:multistatus>\n");
152
153         end_burst();
154 }
155
156
157
158 /*
159  * The pathname is always going to be /groupdav/room_name/msg_num
160  */
161 void groupdav_propfind(char *dav_pathname) {
162         char dav_roomname[SIZ];
163         char msgnum[SIZ];
164         char buf[SIZ];
165         char uid[SIZ];
166         char encoded_uid[SIZ];
167         long *msgs = NULL;
168         int num_msgs = 0;
169         int i;
170         char datestring[SIZ];
171         time_t now;
172
173         now = time(NULL);
174         http_datestring(datestring, sizeof datestring, now);
175
176
177         /* First, break off the "/groupdav/" prefix */
178         remove_token(dav_pathname, 0, '/');
179         remove_token(dav_pathname, 0, '/');
180
181         /* What's left is the room name.  Remove trailing slashes. */
182         if (dav_pathname[strlen(dav_pathname)-1] == '/') {
183                 dav_pathname[strlen(dav_pathname)-1] = 0;
184         }
185         strcpy(dav_roomname, dav_pathname);
186
187
188         /*
189          * If the room name is blank, the client is requesting a
190          * folder list.
191          */
192         if (strlen(dav_roomname) == 0) {
193                 groupdav_folder_list();
194                 return;
195         }
196
197         /* Go to the correct room. */
198         if (strcasecmp(WC->wc_roomname, dav_roomname)) {
199                 gotoroom(dav_roomname);
200         }
201         if (strcasecmp(WC->wc_roomname, dav_roomname)) {
202                 wprintf("HTTP/1.1 404 not found\r\n");
203                 groupdav_common_headers();
204                 wprintf("Date: %s\r\n", datestring);
205                 wprintf(
206                         "Content-Type: text/plain\r\n"
207                         "\r\n"
208                         "There is no folder called \"%s\" on this server.\r\n",
209                         dav_roomname
210                 );
211                 return;
212         }
213
214         /*
215          * Be rude.  Completely ignore the XML request and simply send them
216          * everything we know about (which is going to simply be the ETag and
217          * nothing else).  Let the client-side parser sort it out.
218          */
219         wprintf("HTTP/1.0 207 Multi-Status\r\n");
220         groupdav_common_headers();
221         wprintf("Date: %s\r\n", datestring);
222         wprintf("Content-type: text/xml\r\n");
223         wprintf("Content-encoding: identity\r\n");
224
225         begin_burst();
226
227         wprintf("<?xml version=\"1.0\" encoding=\"utf-8\"?>"
228                 "<D:multistatus xmlns:D=\"DAV:\">"
229         );
230
231         serv_puts("MSGS ALL");
232         serv_gets(buf);
233         if (buf[0] == '1') while (serv_gets(msgnum), strcmp(msgnum, "000")) {
234                 msgs = realloc(msgs, ++num_msgs * sizeof(long));
235                 msgs[num_msgs-1] = atol(msgnum);
236         }
237
238         if (num_msgs > 0) for (i=0; i<num_msgs; ++i) {
239
240                 strcpy(uid, "");
241                 serv_printf("MSG0 %ld|3", msgs[i]);
242                 serv_gets(buf);
243                 if (buf[0] == '1') while (serv_gets(buf), strcmp(buf, "000")) {
244                         if (!strncasecmp(buf, "exti=", 5)) {
245                                 strcpy(uid, &buf[5]);
246                         }
247                 }
248
249                 if (strlen(uid) > 0) {
250                         wprintf("<D:response>");
251                         wprintf("<D:href>");
252                         if (strlen(WC->http_host) > 0) {
253                                 wprintf("%s://%s",
254                                         (is_https ? "https" : "http"),
255                                         WC->http_host);
256                         }
257                         wprintf("/groupdav/");
258                         urlescputs(WC->wc_roomname);
259                         euid_escapize(encoded_uid, uid);
260                         wprintf("/%s", encoded_uid);
261                         wprintf("</D:href>");
262                         wprintf("<D:propstat>");
263                         wprintf("<D:status>HTTP/1.1 200 OK</D:status>");
264                         wprintf("<D:prop><D:getetag>\"%ld\"</D:getetag></D:prop>", msgs[i]);
265                         wprintf("</D:propstat>");
266                         wprintf("</D:response>");
267                 }
268         }
269
270         wprintf("</D:multistatus>\n");
271         end_burst();
272
273         if (msgs != NULL) {
274                 free(msgs);
275         }
276 }