* All GroupDAV HTTP output is now \r\n instead of \n terminated.
[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
66         /*
67          * Be rude.  Completely ignore the XML request and simply send them
68          * everything we know about.  Let the client sort it out.
69          */
70         wprintf("HTTP/1.0 207 Multi-Status\r\n");
71         groupdav_common_headers();
72         wprintf("Content-type: text/xml\r\n");
73
74         begin_burst();
75
76         wprintf("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n"
77                 "<D:multistatus xmlns:D=\"DAV:\">\r\n"
78         );
79
80         serv_puts("LKRA");
81         serv_gets(buf);
82         if (buf[0] == '1') while (serv_gets(buf), strcmp(buf, "000")) {
83
84                 extract(roomname, buf, 0);
85                 view = extract_int(buf, 6);
86
87                 /*
88                  * For now, only list rooms that we know a GroupDAV client
89                  * might be interested in.  In the future we may add
90                  * the rest.
91                  */
92                 if ((view == VIEW_CALENDAR) || (view == VIEW_TASKS) || (view == VIEW_ADDRESSBOOK) ) {
93
94                         wprintf(" <D:response>\r\n");
95
96                         wprintf("  <D:href>");
97                         if (strlen(WC->http_host) > 0) {
98                                 wprintf("%s://%s",
99                                         (is_https ? "https" : "http"),
100                                         WC->http_host);
101                         }
102                         wprintf("/groupdav/");
103                         urlescputs(roomname);
104                         wprintf("/</D:href>\r\n");
105
106                         wprintf("  <D:propstat>\r\n");
107                         wprintf("   <D:status>HTTP/1.1 200 OK</D:status>\r\n");
108                         wprintf("   <D:prop>\r\n");
109                         wprintf("    <D:displayname>");
110                         escputs(                roomname);
111                         wprintf(                        "</D:displayname>\r\n");
112                         wprintf("    <D:resourcetype><D:collection/>");
113
114                         switch(view) {
115                                 case VIEW_CALENDAR:
116                                         wprintf("        <G:vevent-collection />\r\n");
117                                         break;
118                                 case VIEW_TASKS:
119                                         wprintf("        <G:vtodo-collection />\r\n");
120                                         break;
121                                 case VIEW_ADDRESSBOOK:
122                                         wprintf("        <G:vcard-collection />\r\n");
123                                         break;
124                         }
125
126                         wprintf(                                "</D:resourcetype>\r\n");
127                         wprintf("   </D:prop>\r\n");
128                         wprintf("  </D:propstat>\r\n");
129                         wprintf(" </D:response>\r\n");
130                 }
131         }
132         wprintf("</D:multistatus>\r\n\r\n\r\n");
133
134         end_burst();
135 }
136
137
138
139 /*
140  * The pathname is always going to be /groupdav/room_name/msg_num
141  */
142 void groupdav_propfind(char *dav_pathname) {
143         char dav_roomname[SIZ];
144         char msgnum[SIZ];
145         char buf[SIZ];
146         char uid[SIZ];
147         char encoded_uid[SIZ];
148         long *msgs = NULL;
149         int num_msgs = 0;
150         int i;
151
152         /* First, break off the "/groupdav/" prefix */
153         remove_token(dav_pathname, 0, '/');
154         remove_token(dav_pathname, 0, '/');
155
156         /* What's left is the room name.  Remove trailing slashes. */
157         if (dav_pathname[strlen(dav_pathname)-1] == '/') {
158                 dav_pathname[strlen(dav_pathname)-1] = 0;
159         }
160         strcpy(dav_roomname, dav_pathname);
161
162
163         /*
164          * If the room name is blank, the client is requesting a
165          * folder list.
166          */
167         if (strlen(dav_roomname) == 0) {
168                 groupdav_folder_list();
169                 return;
170         }
171
172         /* Go to the correct room. */
173         if (strcasecmp(WC->wc_roomname, dav_roomname)) {
174                 gotoroom(dav_roomname);
175         }
176         if (strcasecmp(WC->wc_roomname, dav_roomname)) {
177                 wprintf("HTTP/1.1 404 not found\r\n");
178                 groupdav_common_headers();
179                 wprintf(
180                         "Content-Type: text/plain\r\n"
181                         "\r\n"
182                         "There is no folder called \"%s\" on this server.\r\n",
183                         dav_roomname
184                 );
185                 return;
186         }
187
188         /*
189          * Be rude.  Completely ignore the XML request and simply send them
190          * everything we know about (which is going to simply be the ETag and
191          * nothing else).  Let the client-side parser sort it out.
192          */
193         wprintf("HTTP/1.0 207 Multi-Status\r\n");
194         groupdav_common_headers();
195         wprintf("Content-type: text/xml\r\n");
196
197         begin_burst();
198
199         wprintf("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n"
200                 "<D:multistatus xmlns:D=\"DAV:\">\r\n"
201         );
202
203         serv_puts("MSGS ALL");
204         serv_gets(buf);
205         if (buf[0] == '1') while (serv_gets(msgnum), strcmp(msgnum, "000")) {
206                 msgs = realloc(msgs, ++num_msgs * sizeof(long));
207                 msgs[num_msgs-1] = atol(msgnum);
208         }
209
210         if (num_msgs > 0) for (i=0; i<num_msgs; ++i) {
211
212                 strcpy(uid, "");
213                 serv_printf("MSG0 %ld|3", msgs[i]);
214                 serv_gets(buf);
215                 if (buf[0] == '1') while (serv_gets(buf), strcmp(buf, "000")) {
216                         if (!strncasecmp(buf, "exti=", 5)) {
217                                 strcpy(uid, &buf[5]);
218                         }
219                 }
220
221                 if (strlen(uid) > 0) {
222                         wprintf(" <D:response>\r\n");
223                         wprintf("  <D:href>");
224                         if (strlen(WC->http_host) > 0) {
225                                 wprintf("%s://%s",
226                                         (is_https ? "https" : "http"),
227                                         WC->http_host);
228                         }
229                         wprintf("/groupdav/");
230                         urlescputs(WC->wc_roomname);
231                         euid_escapize(encoded_uid, uid);
232                         wprintf("/%s", encoded_uid);
233                         wprintf("</D:href>\r\n");
234                         wprintf("   <D:propstat>\r\n");
235                         wprintf("    <D:status>HTTP/1.1 200 OK</D:status>\r\n");
236                         wprintf("    <D:prop><D:getetag>\"%ld\"</D:getetag></D:prop>\r\n", msgs[i]);
237                         wprintf("   </D:propstat>\r\n");
238                         wprintf(" </D:response>\r\n");
239                 }
240         }
241
242         wprintf("</D:multistatus>\r\n\r\n\r\n");
243         end_burst();
244
245         if (msgs != NULL) {
246                 free(msgs);
247         }
248 }