cdc65becb459458d7741335b3255423f09b1641e
[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(StrBuf *dav_pathname, char *dav_ifmatch) {
17         char dav_uid[SIZ];
18         long dav_msgnum = (-1);
19         char buf[SIZ];
20         int n = 0;
21
22         /* First, break off the "/groupdav/" prefix */
23         StrBufCutLeft(dav_pathname, 9);
24
25         /* Now extract the message euid */
26         n = StrBufNum_tokens(dav_pathname, '/');
27         extract_token(dav_uid, ChrPtr(dav_pathname), n-1, '/', sizeof dav_uid);
28         StrBufRemove_token(dav_pathname, n-1, '/');
29
30         /* What's left is the room name.  Remove trailing slashes. */
31         //len = StrLength(dav_pathname);
32         //if ((len > 0) && (ChrPtr(dav_pathname)[len-1] == '/')) {
33         //      StrBufCutRight(dav_pathname, 1);
34         //}
35         StrBufCutLeft(dav_pathname, 1);
36
37         /* Go to the correct room. */
38         if (strcasecmp(ChrPtr(WC->wc_roomname), ChrPtr(dav_pathname))) {
39                 gotoroom(dav_pathname);
40         }
41         if (strcasecmp(ChrPtr(WC->wc_roomname), ChrPtr(dav_pathname))) {
42                 hprintf("HTTP/1.1 404 not found\r\n");
43                 groupdav_common_headers();
44                 hprintf("Content-Length: 0\r\n\r\n");
45                 return;
46         }
47
48         dav_msgnum = locate_message_by_uid(dav_uid);
49
50         /*
51          * If no item exists with the requested uid ... simple error.
52          */
53         if (dav_msgnum < 0L) {
54                 hprintf("HTTP/1.1 404 Not Found\r\n");
55                 groupdav_common_headers();
56                 hprintf("Content-Length: 0\r\n\r\n");
57                 return;
58         }
59
60         /*
61          * It's there ... check the ETag and make sure it matches
62          * the message number.
63          */
64         if (!IsEmptyStr(dav_ifmatch)) {
65                 if (atol(dav_ifmatch) != dav_msgnum) {
66                         hprintf("HTTP/1.1 412 Precondition Failed\r\n");
67                         groupdav_common_headers();
68                         hprintf("Content-Length: 0\r\n\r\n");
69                         return;
70                 }
71         }
72
73         /*
74          * Ok, attempt to delete the item.
75          */
76         serv_printf("DELE %ld", dav_msgnum);
77         serv_getln(buf, sizeof buf);
78         if (buf[0] == '2') {
79                 hprintf("HTTP/1.1 204 No Content\r\n"); /* success */
80                 groupdav_common_headers();
81                 hprintf("Content-Length: 0\r\n\r\n");
82         }
83         else {
84                 hprintf("HTTP/1.1 403 Forbidden\r\n");  /* access denied */
85                 groupdav_common_headers();
86                 hprintf("Content-Length: 0\r\n\r\n");
87         }
88         return;
89 }