]> code.citadel.org Git - citadel.git/blob - webcit/groupdav_propfind.c
API change: pass the 'Depth:' header and content along
[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 "webcit.h"
19 #include "webserver.h"
20 #include "groupdav.h"
21
22
23 /*
24  * Given an encoded UID, translate that to an unencoded Citadel EUID and
25  * then search for it in the current room.  Return a message number or -1
26  * if not found.
27  *
28  * NOTE: this function relies on the Citadel server's brute-force search.
29  * There's got to be a way to optimize this better.
30  */
31 long locate_message_by_uid(char *uid) {
32         char buf[SIZ];
33         char decoded_uid[SIZ];
34         long retval = (-1L);
35
36         /* Decode the uid */
37         euid_unescapize(decoded_uid, uid);
38
39         serv_puts("MSGS ALL|0|1");
40         serv_getln(buf, sizeof buf);
41         if (buf[0] == '8') {
42                 serv_printf("exti|%s", decoded_uid);
43                 serv_puts("000");
44                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
45                         retval = atol(buf);
46                 }
47         }
48         return(retval);
49 }
50
51
52 /*
53  * List folders containing interesting groupware objects
54  */
55 void groupdav_folder_list(void) {
56         char buf[SIZ];
57         char roomname[SIZ];
58         int view;
59         char datestring[SIZ];
60         time_t now;
61
62         now = time(NULL);
63         http_datestring(datestring, sizeof datestring, now);
64
65         /*
66          * Be rude.  Completely ignore the XML request and simply send them
67          * everything we know about.  Let the client sort it out.
68          */
69         wprintf("HTTP/1.0 207 Multi-Status\r\n");
70         groupdav_common_headers();
71         wprintf("Date: %s\r\n", datestring);
72         wprintf("Content-type: text/xml\r\n");
73         wprintf("Content-encoding: identity\r\n");
74
75         begin_burst();
76
77         wprintf("<?xml version=\"1.0\" encoding=\"utf-8\"?>"
78                 "<D:multistatus xmlns:D=\"DAV:\" xmlns:G=\"http://groupdav.org/\">"
79         );
80
81         serv_puts("LKRA");
82         serv_getln(buf, sizeof buf);
83         if (buf[0] == '1') while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
84
85                 extract_token(roomname, buf, 0, '|', sizeof roomname);
86                 view = extract_int(buf, 6);
87
88                 /*
89                  * For now, only list rooms that we know a GroupDAV client
90                  * might be interested in.  In the future we may add
91                  * the rest.
92                  */
93                 if ((view == VIEW_CALENDAR)
94                    || (view == VIEW_TASKS)
95                    || (view == VIEW_ADDRESSBOOK) ) {
96
97                         wprintf("<D:response>");
98
99                         wprintf("<D:href>");
100                         groupdav_identify_host();
101                         wprintf("/groupdav/");
102                         urlescputs(roomname);
103                         wprintf("/</D:href>");
104
105                         wprintf("<D:propstat>");
106                         wprintf("<D:status>HTTP/1.1 200 OK</D:status>");
107                         wprintf("<D:prop>");
108                         wprintf("<D:displayname>");
109                         escputs(roomname);
110                         wprintf("</D:displayname>");
111                         wprintf("<D:resourcetype><D:collection/>");
112
113                         switch(view) {
114                                 case VIEW_CALENDAR:
115                                         wprintf("<G:vevent-collection />");
116                                         break;
117                                 case VIEW_TASKS:
118                                         wprintf("<G:vtodo-collection />");
119                                         break;
120                                 case VIEW_ADDRESSBOOK:
121                                         wprintf("<G:vcard-collection />");
122                                         break;
123                         }
124
125                         wprintf("</D:resourcetype>");
126                         wprintf("</D:prop>");
127                         wprintf("</D:propstat>");
128                         wprintf("</D:response>");
129                 }
130         }
131         wprintf("</D:multistatus>\n");
132
133         end_burst();
134 }
135
136
137
138 /*
139  * The pathname is always going to be /groupdav/room_name/msg_num
140  */
141 void groupdav_propfind(char *dav_pathname, char *dav_depth, char *dav_content_type, char *dav_content) {
142         char dav_roomname[256];
143         char dav_uid[256];
144         char msgnum[256];
145         long dav_msgnum = (-1);
146         char buf[256];
147         char uid[256];
148         char encoded_uid[256];
149         long *msgs = NULL;
150         int num_msgs = 0;
151         int i;
152         char datestring[256];
153         time_t now;
154
155         now = time(NULL);
156         http_datestring(datestring, sizeof datestring, now);
157
158         extract_token(dav_roomname, dav_pathname, 2, '/', sizeof dav_roomname);
159         extract_token(dav_uid, dav_pathname, 3, '/', sizeof dav_uid);
160
161         lprintf(9, "dav_pathname: %s\n", dav_pathname);
162         lprintf(9, "dav_roomname: %s\n", dav_roomname);
163         lprintf(9, "     dav_uid: %s\n", dav_uid);
164         lprintf(9, "   dav_depth: %s\n", dav_depth);
165
166         /*
167          * If the room name is blank, the client is requesting a
168          * folder list.
169          */
170         if (strlen(dav_roomname) == 0) {
171                 groupdav_folder_list();
172                 return;
173         }
174
175         /* Go to the correct room. */
176         if (strcasecmp(WC->wc_roomname, dav_roomname)) {
177                 gotoroom(dav_roomname);
178         }
179         if (strcasecmp(WC->wc_roomname, dav_roomname)) {
180                 wprintf("HTTP/1.1 404 not found\r\n");
181                 groupdav_common_headers();
182                 wprintf("Date: %s\r\n", datestring);
183                 wprintf(
184                         "Content-Type: text/plain\r\n"
185                         "\r\n"
186                         "There is no folder called \"%s\" on this server.\r\n",
187                         dav_roomname
188                 );
189                 return;
190         }
191
192         /* If dav_uid is non-empty, client is requesting a PROPFIND on
193          * a specific item in the room.  This is not valid GroupDAV, but
194          * we try to honor it anyway because some clients are expecting
195          * it to work...
196          */
197         if (strlen(dav_uid) > 0) {
198
199                 dav_msgnum = locate_message_by_uid(dav_uid);
200                 if (dav_msgnum < 0) {
201                         wprintf("HTTP/1.1 404 not found\r\n");
202                         groupdav_common_headers();
203                         wprintf(
204                                 "Content-Type: text/plain\r\n"
205                                 "\r\n"
206                                 "Object \"%s\" was not found in the \"%s\" folder.\r\n",
207                                 dav_uid,
208                                 dav_roomname
209                         );
210                         return;
211                 }
212
213                 /* Be rude.  Completely ignore the XML request and simply send them
214                  * everything we know about (which is going to simply be the ETag and
215                  * nothing else).  Let the client-side parser sort it out.
216                  */
217                 wprintf("HTTP/1.0 207 Multi-Status\r\n");
218                 groupdav_common_headers();
219                 wprintf("Date: %s\r\n", datestring);
220                 wprintf("Content-type: text/xml\r\n");
221                 wprintf("Content-encoding: identity\r\n");
222         
223                 begin_burst();
224         
225                 wprintf("<?xml version=\"1.0\" encoding=\"utf-8\"?>"
226                         "<D:multistatus xmlns:D=\"DAV:\" xmlns:G=\"http://groupdav.org/\">"
227                 );
228
229                 wprintf("<D:response>");
230
231                 wprintf("<D:href>");
232                 groupdav_identify_host();
233                 wprintf("/groupdav/");
234                 urlescputs(WC->wc_roomname);
235                 euid_escapize(encoded_uid, dav_uid);
236                 wprintf("/%s", encoded_uid);
237                 wprintf("</D:href>");
238                 wprintf("<D:propstat>");
239                 wprintf("<D:status>HTTP/1.1 200 OK</D:status>");
240                 wprintf("<D:prop><D:getetag>\"%ld\"</D:getetag></D:prop>", dav_msgnum);
241                 wprintf("</D:propstat>");
242
243                 wprintf("</D:response>\n");
244                 wprintf("</D:multistatus>\n");
245                 end_burst();
246                 return;
247         }
248
249
250         /*
251          * We got to this point, which means that the client is requesting
252          * a 'collection' (i.e. a list of all items in the room).
253          *
254          * Be rude.  Completely ignore the XML request and simply send them
255          * everything we know about (which is going to simply be the ETag and
256          * nothing else).  Let the client-side parser sort it out.
257          */
258         wprintf("HTTP/1.0 207 Multi-Status\r\n");
259         groupdav_common_headers();
260         wprintf("Date: %s\r\n", datestring);
261         wprintf("Content-type: text/xml\r\n");
262         wprintf("Content-encoding: identity\r\n");
263
264         begin_burst();
265
266         wprintf("<?xml version=\"1.0\" encoding=\"utf-8\"?>"
267                 "<D:multistatus xmlns:D=\"DAV:\" xmlns:G=\"http://groupdav.org/\">"
268         );
269
270         serv_puts("MSGS ALL");
271         serv_getln(buf, sizeof buf);
272         if (buf[0] == '1') while (serv_getln(msgnum, sizeof msgnum), strcmp(msgnum, "000")) {
273                 msgs = realloc(msgs, ++num_msgs * sizeof(long));
274                 msgs[num_msgs-1] = atol(msgnum);
275         }
276
277         if (num_msgs > 0) for (i=0; i<num_msgs; ++i) {
278
279                 strcpy(uid, "");
280                 serv_printf("MSG0 %ld|3", msgs[i]);
281                 serv_getln(buf, sizeof buf);
282                 if (buf[0] == '1') while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
283                         if (!strncasecmp(buf, "exti=", 5)) {
284                                 strcpy(uid, &buf[5]);
285                         }
286                 }
287
288                 if (strlen(uid) > 0) {
289                         wprintf("<D:response>");
290                         wprintf("<D:href>");
291                         groupdav_identify_host();
292                         wprintf("/groupdav/");
293                         urlescputs(WC->wc_roomname);
294                         euid_escapize(encoded_uid, uid);
295                         wprintf("/%s", encoded_uid);
296                         wprintf("</D:href>");
297                         wprintf("<D:propstat>");
298                         wprintf("<D:status>HTTP/1.1 200 OK</D:status>");
299                         wprintf("<D:prop><D:getetag>\"%ld\"</D:getetag></D:prop>", msgs[i]);
300                         wprintf("</D:propstat>");
301                         wprintf("</D:response>");
302                 }
303         }
304
305         wprintf("</D:multistatus>\n");
306         end_burst();
307
308         if (msgs != NULL) {
309                 free(msgs);
310         }
311 }