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