]> code.citadel.org Git - citadel.git/blob - webcit/groupdav_propfind.c
* GroupDAV object URL's are now based on the EUID of a message (which is
[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
69 /*
70  * The pathname is always going to be /groupdav/room_name/msg_num
71  */
72 void groupdav_propfind(char *dav_pathname) {
73         char dav_roomname[SIZ];
74         char msgnum[SIZ];
75         char buf[SIZ];
76         char uid[SIZ];
77         long *msgs = NULL;
78         int num_msgs;
79         int i, j;
80
81         /* First, break off the "/groupdav/" prefix */
82         remove_token(dav_pathname, 0, '/');
83         remove_token(dav_pathname, 0, '/');
84
85         /* What's left is the room name.  Remove trailing slashes. */
86         if (dav_pathname[strlen(dav_pathname)-1] == '/') {
87                 dav_pathname[strlen(dav_pathname)-1] = 0;
88         }
89         strcpy(dav_roomname, dav_pathname);
90
91         /* Go to the correct room. */
92         if (strcasecmp(WC->wc_roomname, dav_roomname)) {
93                 gotoroom(dav_roomname);
94         }
95         if (strcasecmp(WC->wc_roomname, dav_roomname)) {
96                 wprintf("HTTP/1.1 404 not found\n");
97                 groupdav_common_headers();
98                 wprintf(
99                         "Content-Type: text/plain\n"
100                         "\n"
101                         "There is no folder called \"%s\" on this server.\n",
102                         dav_roomname
103                 );
104                 return;
105         }
106
107         /*
108          * Be rude.  Completely ignore the XML request and simply send them
109          * everything we know about (which is going to simply be the ETag and
110          * nothing else).  Let the client-side parser sort it out.
111          */
112         wprintf("HTTP/1.0 207 Multi-Status\n");
113         groupdav_common_headers();
114         wprintf("Content-type: text/xml\n"
115                 "\n"
116                 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
117                 "<D:multistatus xmlns:D=\"DAV:\">\n"
118         );
119
120         serv_puts("MSGS ALL");
121         serv_gets(buf);
122         if (buf[0] == '1') while (serv_gets(msgnum), strcmp(msgnum, "000")) {
123                 msgs = realloc(msgs, ++num_msgs * sizeof(long));
124                 msgs[num_msgs-1] = atol(msgnum);
125         }
126
127         if (num_msgs > 0) for (i=0; i<num_msgs; ++i) {
128
129                 strcpy(uid, "");
130                 serv_printf("MSG0 %ld|3", msgs[i]);
131                 serv_gets(buf);
132                 if (buf[0] == '1') while (serv_gets(buf), strcmp(buf, "000")) {
133                         if (!strncasecmp(buf, "exti=", 5)) {
134                                 strcpy(uid, &buf[5]);
135                         }
136                 }
137
138                 if (strlen(uid) > 0) {
139                         wprintf(" <D:response>\n");
140                         wprintf("  <D:href>");
141                         if (strlen(WC->http_host) > 0) {
142                                 wprintf("%s://%s",
143                                         (is_https ? "https" : "http"),
144                                         WC->http_host);
145                         }
146                         wprintf("/groupdav/Calendar/");
147                         for (j=0; j<strlen(uid); ++j) {
148                                 wprintf("%02X", uid[j]);
149                         }
150                         wprintf("</D:href>\n");
151                         wprintf("   <D:propstat>\n");
152                         wprintf("    <D:status>HTTP/1.1 200 OK</D:status>\n");
153                         wprintf("    <D:prop><D:getetag>\"%ld\"</D:getetag></D:prop>\n", msgs[i]);
154                         wprintf("   </D:propstat>\n");
155                         wprintf(" </D:response>\n");
156                 }
157         }
158
159         wprintf("</D:multistatus>\n");
160         if (msgs != NULL) {
161                 free(msgs);
162         }
163 }