* Moved to the new string tokenizer API
[citadel.git] / webcit / groupdav_delete.c
1 /*
2  * $Id$
3  *
4  * Handles GroupDAV DELETE 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  * The pathname is always going to be /groupdav/room_name/euid
31  */
32 void groupdav_delete(char *dav_pathname, char *dav_ifmatch) {
33         char dav_roomname[SIZ];
34         char dav_uid[SIZ];
35         long dav_msgnum = (-1);
36         char buf[SIZ];
37         int n = 0;
38
39         /* First, break off the "/groupdav/" prefix */
40         remove_token(dav_pathname, 0, '/');
41         remove_token(dav_pathname, 0, '/');
42
43         /* Now extract the message euid */
44         n = num_tokens(dav_pathname, '/');
45         extract_token(dav_uid, dav_pathname, n-1, '/', sizeof dav_uid);
46         remove_token(dav_pathname, n-1, '/');
47
48         /* What's left is the room name.  Remove trailing slashes. */
49         if (dav_pathname[strlen(dav_pathname)-1] == '/') {
50                 dav_pathname[strlen(dav_pathname)-1] = 0;
51         }
52         strcpy(dav_roomname, dav_pathname);
53
54         /* Go to the correct room. */
55         if (strcasecmp(WC->wc_roomname, dav_roomname)) {
56                 gotoroom(dav_roomname);
57         }
58         if (strcasecmp(WC->wc_roomname, dav_roomname)) {
59                 wprintf("HTTP/1.1 404 not found\r\n");
60                 groupdav_common_headers();
61                 wprintf("Content-Length: 0\r\n\r\n");
62                 return;
63         }
64
65         dav_msgnum = locate_message_by_uid(dav_uid);
66
67         /*
68          * If no item exists with the requested uid ... simple error.
69          */
70         if (dav_msgnum < 0L) {
71                 wprintf("HTTP/1.1 404 Not Found\r\n");
72                 groupdav_common_headers();
73                 wprintf("Content-Length: 0\r\n\r\n");
74                 return;
75         }
76
77         /*
78          * It's there ... check the ETag and make sure it matches
79          * the message number.
80          */
81         if (strlen(dav_ifmatch) > 0) {
82                 if (atol(dav_ifmatch) != dav_msgnum) {
83                         wprintf("HTTP/1.1 412 Precondition Failed\r\n");
84                         groupdav_common_headers();
85                         wprintf("Content-Length: 0\r\n\r\n");
86                         return;
87                 }
88         }
89
90         /*
91          * Ok, attempt to delete the item.
92          */
93         serv_printf("DELE %ld", dav_msgnum);
94         serv_gets(buf);
95         if (buf[0] == '2') {
96                 wprintf("HTTP/1.1 204 No Content\r\n"); /* success */
97                 groupdav_common_headers();
98                 wprintf("Content-Length: 0\r\n\r\n");
99         }
100         else {
101                 wprintf("HTTP/1.1 403 Forbidden\r\n");  /* access denied */
102                 groupdav_common_headers();
103                 wprintf("Content-Length: 0\r\n\r\n");
104         }
105         return;
106 }