webcit_before_automake is now the trunk
[citadel.git] / webcit / groupdav_delete.c
1 /*
2  * $Id$
3  *
4  * Handles GroupDAV DELETE requests.
5  *
6  */
7
8 #include "webcit.h"
9 #include "webserver.h"
10 #include "groupdav.h"
11
12
13 /*
14  * The pathname is always going to be /groupdav/room_name/euid
15  */
16 void groupdav_delete(char *dav_pathname, char *dav_ifmatch) {
17         char dav_roomname[SIZ];
18         char dav_uid[SIZ];
19         long dav_msgnum = (-1);
20         char buf[SIZ];
21         int n = 0;
22
23         /* First, break off the "/groupdav/" prefix */
24         remove_token(dav_pathname, 0, '/');
25         remove_token(dav_pathname, 0, '/');
26
27         /* Now extract the message euid */
28         n = num_tokens(dav_pathname, '/');
29         extract_token(dav_uid, dav_pathname, n-1, '/', sizeof dav_uid);
30         remove_token(dav_pathname, n-1, '/');
31
32         /* What's left is the room name.  Remove trailing slashes. */
33         if (dav_pathname[strlen(dav_pathname)-1] == '/') {
34                 dav_pathname[strlen(dav_pathname)-1] = 0;
35         }
36         strcpy(dav_roomname, dav_pathname);
37
38         /* Go to the correct room. */
39         if (strcasecmp(WC->wc_roomname, dav_roomname)) {
40                 gotoroom(dav_roomname);
41         }
42         if (strcasecmp(WC->wc_roomname, dav_roomname)) {
43                 wprintf("HTTP/1.1 404 not found\r\n");
44                 groupdav_common_headers();
45                 wprintf("Content-Length: 0\r\n\r\n");
46                 return;
47         }
48
49         dav_msgnum = locate_message_by_uid(dav_uid);
50
51         /*
52          * If no item exists with the requested uid ... simple error.
53          */
54         if (dav_msgnum < 0L) {
55                 wprintf("HTTP/1.1 404 Not Found\r\n");
56                 groupdav_common_headers();
57                 wprintf("Content-Length: 0\r\n\r\n");
58                 return;
59         }
60
61         /*
62          * It's there ... check the ETag and make sure it matches
63          * the message number.
64          */
65         if (strlen(dav_ifmatch) > 0) {
66                 if (atol(dav_ifmatch) != dav_msgnum) {
67                         wprintf("HTTP/1.1 412 Precondition Failed\r\n");
68                         groupdav_common_headers();
69                         wprintf("Content-Length: 0\r\n\r\n");
70                         return;
71                 }
72         }
73
74         /*
75          * Ok, attempt to delete the item.
76          */
77         serv_printf("DELE %ld", dav_msgnum);
78         serv_getln(buf, sizeof buf);
79         if (buf[0] == '2') {
80                 wprintf("HTTP/1.1 204 No Content\r\n"); /* success */
81                 groupdav_common_headers();
82                 wprintf("Content-Length: 0\r\n\r\n");
83         }
84         else {
85                 wprintf("HTTP/1.1 403 Forbidden\r\n");  /* access denied */
86                 groupdav_common_headers();
87                 wprintf("Content-Length: 0\r\n\r\n");
88         }
89         return;
90 }