* Completed "folder list" PROPFIND, now listing all rooms set to a
[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         int i, j;
41         int ch;
42         long retval = (-1L);
43
44         /* Decode the uid */
45         j=0;
46         for (i=0; i<strlen(uid); i=i+2) {
47                 ch = 0;
48                 sscanf(&uid[i], "%02x", &ch);
49                 decoded_uid[j] = ch;
50                 decoded_uid[j+1] = 0;
51                 ++j;
52         }
53
54         serv_puts("MSGS ALL|0|1");
55         serv_gets(buf);
56         if (buf[0] == '8') {
57                 serv_printf("exti|%s", decoded_uid);
58                 serv_puts("000");
59                 while (serv_gets(buf), strcmp(buf, "000")) {
60                         retval = atol(buf);
61                 }
62         }
63         return(retval);
64 }
65
66
67 /*
68  * List folders containing interesting groupware objects
69  */
70 void groupdav_folder_list(void) {
71         char buf[SIZ];
72         char roomname[SIZ];
73         int view;
74
75         /*
76          * Be rude.  Completely ignore the XML request and simply send them
77          * everything we know about.  Let the client sort it out.
78          */
79         wprintf("HTTP/1.0 207 Multi-Status\n");
80         groupdav_common_headers();
81         wprintf("Content-type: text/xml\n"
82                 "\n"
83                 "<?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                 if ((view == VIEW_CALENDAR) || (view == VIEW_TASKS) || (view == VIEW_ADDRESSBOOK) ) {
94
95                         wprintf(" <D:response>\n");
96                         wprintf("   <D:href>http://splorph.xand.com/groupdav/");
97                         urlescputs(                                     roomname);
98                         wprintf(                                                "/</D:href>\n");
99                         wprintf("   <D:propstat>\n");
100                         wprintf("     <D:status>HTTP/1.1 200 OK</D:status>\n");
101                         wprintf("     <D:prop>\n");
102                         wprintf("      <D:displayname>");
103                         urlescputs(                     roomname);
104                         wprintf(                                "</D:displayname>\n");
105                         wprintf("      <resourcetype xmlns=\"DAV:\" xmlns:G=\"http://groupdav.org/\">\n");
106                         wprintf("        <collection />\n");
107                         switch(view) {
108                                 case VIEW_CALENDAR:
109                                         wprintf("        <G:vevent-collection />\n");
110                                         break;
111                                 case VIEW_TASKS:
112                                         wprintf("        <G:vtodo-collection />\n");
113                                         break;
114                                 case VIEW_ADDRESSBOOK:
115                                         wprintf("        <G:vcard-collection />\n");
116                                         break;
117                         }
118                         wprintf("      <resourcetype>\n");
119                         wprintf("     </D:prop>\n");
120                         wprintf("   </D:propstat>\n");
121                         wprintf(" </D:response>\n");
122                 }
123         }
124
125         wprintf("</D:multistatus>\n");
126
127 }
128
129
130
131 /*
132  * The pathname is always going to be /groupdav/room_name/msg_num
133  */
134 void groupdav_propfind(char *dav_pathname) {
135         char dav_roomname[SIZ];
136         char msgnum[SIZ];
137         char buf[SIZ];
138         char uid[SIZ];
139         long *msgs = NULL;
140         int num_msgs;
141         int i, j;
142
143         /* First, break off the "/groupdav/" prefix */
144         remove_token(dav_pathname, 0, '/');
145         remove_token(dav_pathname, 0, '/');
146
147         /* What's left is the room name.  Remove trailing slashes. */
148         if (dav_pathname[strlen(dav_pathname)-1] == '/') {
149                 dav_pathname[strlen(dav_pathname)-1] = 0;
150         }
151         strcpy(dav_roomname, dav_pathname);
152
153
154         /*
155          * If the room name is blank, the client is requesting a
156          * folder list.
157          */
158         if (strlen(dav_roomname) == 0) {
159                 groupdav_folder_list();
160                 return;
161         }
162
163         /* Go to the correct room. */
164         if (strcasecmp(WC->wc_roomname, dav_roomname)) {
165                 gotoroom(dav_roomname);
166         }
167         if (strcasecmp(WC->wc_roomname, dav_roomname)) {
168                 wprintf("HTTP/1.1 404 not found\n");
169                 groupdav_common_headers();
170                 wprintf(
171                         "Content-Type: text/plain\n"
172                         "\n"
173                         "There is no folder called \"%s\" on this server.\n",
174                         dav_roomname
175                 );
176                 return;
177         }
178
179         /*
180          * Be rude.  Completely ignore the XML request and simply send them
181          * everything we know about (which is going to simply be the ETag and
182          * nothing else).  Let the client-side parser sort it out.
183          */
184         wprintf("HTTP/1.0 207 Multi-Status\n");
185         groupdav_common_headers();
186         wprintf("Content-type: text/xml\n"
187                 "\n"
188                 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
189                 "<D:multistatus xmlns:D=\"DAV:\">\n"
190         );
191
192         serv_puts("MSGS ALL");
193         serv_gets(buf);
194         if (buf[0] == '1') while (serv_gets(msgnum), strcmp(msgnum, "000")) {
195                 msgs = realloc(msgs, ++num_msgs * sizeof(long));
196                 msgs[num_msgs-1] = atol(msgnum);
197         }
198
199         if (num_msgs > 0) for (i=0; i<num_msgs; ++i) {
200
201                 strcpy(uid, "");
202                 serv_printf("MSG0 %ld|3", msgs[i]);
203                 serv_gets(buf);
204                 if (buf[0] == '1') while (serv_gets(buf), strcmp(buf, "000")) {
205                         if (!strncasecmp(buf, "exti=", 5)) {
206                                 strcpy(uid, &buf[5]);
207                         }
208                 }
209
210                 if (strlen(uid) > 0) {
211                         wprintf(" <D:response>\n");
212                         wprintf("  <D:href>");
213                         if (strlen(WC->http_host) > 0) {
214                                 wprintf("%s://%s",
215                                         (is_https ? "https" : "http"),
216                                         WC->http_host);
217                         }
218                         wprintf("/groupdav/");
219                         urlescputs(WC->wc_roomname);
220                         wprintf("/");
221                         for (j=0; j<strlen(uid); ++j) {
222                                 wprintf("%02X", uid[j]);
223                         }
224                         wprintf("</D:href>\n");
225                         wprintf("   <D:propstat>\n");
226                         wprintf("    <D:status>HTTP/1.1 200 OK</D:status>\n");
227                         wprintf("    <D:prop><D:getetag>\"%ld\"</D:getetag></D:prop>\n", msgs[i]);
228                         wprintf("   </D:propstat>\n");
229                         wprintf(" </D:response>\n");
230                 }
231         }
232
233         wprintf("</D:multistatus>\n");
234         if (msgs != NULL) {
235                 free(msgs);
236         }
237 }