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