Mailing list header changes (fuck you Google)
[citadel.git] / webcit / dav_main.c
1 /*
2  * Entry point for GroupDAV functions
3  *
4  * Copyright (c) 2005-2012 by the citadel.org team
5  *
6  * This program is open source software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License, version 3.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #include "webcit.h"
16 #include "webserver.h"
17 #include "dav.h"
18
19 CtxType CTX_DAVNS = CTX_NONE;
20 extern HashList *HandlerHash;
21
22 HashList *DavNamespaces = NULL;
23
24 /*
25  * Output HTTP headers which are common to all requests.
26  *
27  * Please observe that we don't use the usual output_headers()
28  * and wDumpContent() functions in the GroupDAV subsystem, so we
29  * do our own header stuff here.
30  *
31  */
32 void dav_common_headers(void) {
33         hprintf(
34                 "Server: %s / %s\r\n"
35                 "Connection: close\r\n",
36                 PACKAGE_STRING, ChrPtr(WC->serv_info->serv_software)
37         );
38 }
39
40
41
42 /*
43  * string conversion function
44  */
45 void euid_escapize(char *target, const char *source) {
46         int i, len;
47         int target_length = 0;
48
49         strcpy(target, "");
50         len = strlen(source);
51         for (i=0; i<len; ++i) {
52                 if ( (isalnum(source[i])) || (source[i]=='-') || (source[i]=='_') ) {
53                         target[target_length] = source[i];
54                         target[++target_length] = 0;
55                 }
56                 else {
57                         sprintf(&target[target_length], "=%02X", (0xFF & source[i]));
58                         target_length += 3;
59                 }
60         }
61 }
62
63 /*
64  * string conversion function
65  */
66 void euid_unescapize(char *target, const char *source) {
67         int a, b, len;
68         char hex[3];
69         int target_length = 0;
70
71         strcpy(target, "");
72
73         len = strlen(source);
74         for (a = 0; a < len; ++a) {
75                 if (source[a] == '=') {
76                         hex[0] = source[a + 1];
77                         hex[1] = source[a + 2];
78                         hex[2] = 0;
79                         b = 0;
80                         b = decode_hex(hex);
81                         target[target_length] = b;
82                         target[++target_length] = 0;
83                         a += 2;
84                 }
85                 else {
86                         target[target_length] = source[a];
87                         target[++target_length] = 0;
88                 }
89         }
90 }
91
92
93
94
95 /*
96  * Main entry point for GroupDAV requests
97  */
98 void dav_main(void)
99 {
100         wcsession *WCC = WC;
101         int i, len;
102
103         syslog(LOG_DEBUG, "dav_main() called, logged_in=%d", WCC->logged_in );
104
105         StrBufUnescape(WCC->Hdr->HR.ReqLine, 0);
106         StrBufStripSlashes(WCC->Hdr->HR.ReqLine, 0);
107
108         /*
109          * If there's an If-Match: header, strip out the quotes if present, and
110          * then if all that's left is an asterisk, make it go away entirely.
111          */
112         len = StrLength(WCC->Hdr->HR.dav_ifmatch);
113         if (len > 0) {
114                 StrBufTrim(WCC->Hdr->HR.dav_ifmatch);
115                 if (ChrPtr(WCC->Hdr->HR.dav_ifmatch)[0] == '\"') {
116                         StrBufCutLeft(WCC->Hdr->HR.dav_ifmatch, 1);
117                         len --;
118                         for (i=0; i<len; ++i) {
119                                 if (ChrPtr(WCC->Hdr->HR.dav_ifmatch)[i] == '\"') {
120                                         StrBufCutAt(WCC->Hdr->HR.dav_ifmatch, i, NULL);
121                                         len = StrLength(WCC->Hdr->HR.dav_ifmatch);
122                                 }
123                         }
124                 }
125                 if (!strcmp(ChrPtr(WCC->Hdr->HR.dav_ifmatch), "*")) {
126                         FlushStrBuf(WCC->Hdr->HR.dav_ifmatch);
127                 }
128         }
129
130         switch (WCC->Hdr->HR.eReqType)
131         {
132         /*
133          * The OPTIONS method is not required by GroupDAV but it will be
134          * needed for future implementations of other DAV-based protocols.
135          */
136         case eOPTIONS:
137                 dav_options();
138                 break;
139
140         /*
141          * The PROPFIND method is basically used to list all objects in a
142          * room, or to list all relevant rooms on the server.
143          */
144         case ePROPFIND:
145                 dav_propfind();
146                 break;
147
148         /*
149          * The GET method is used for fetching individual items.
150          */
151         case eGET:
152                 dav_get();
153                 break;
154         
155         /*
156          * The PUT method is used to add or modify items.
157          */
158         case ePUT:
159                 dav_put();
160                 break;
161         
162         /*
163          * The DELETE method kills, maims, and destroys.
164          */
165         case eDELETE:
166                 dav_delete();
167                 break;
168
169         /*
170          * The REPORT method tells us that Mike Shaver is a self-righteous asshole.
171          */
172         case eREPORT:
173                 dav_report();
174                 break;
175
176         default:
177         /*
178          * Couldn't find what we were looking for.  Die in a car fire.
179          */
180                 hprintf("HTTP/1.1 501 Method not implemented\r\n");
181                 dav_common_headers();
182                 hprintf("Content-Type: text/plain\r\n");
183                 wc_printf("GroupDAV method \"%s\" is not implemented.\r\n",
184                         ReqStrs[WCC->Hdr->HR.eReqType]);
185                 end_burst();
186         }
187 }
188
189
190 /*
191  * Output our host prefix for globally absolute URL's.
192  */  
193 void dav_identify_host(void) {
194         wc_printf("%s", ChrPtr(site_prefix));
195 }
196
197
198 void tmplput_dav_HOSTNAME(StrBuf *Target, WCTemplputParams *TP) 
199 {
200         StrBufAppendPrintf(Target, "%s", ChrPtr(site_prefix));
201 }
202
203 /*
204  * Output our host prefix for globally absolute URL's.
205  */  
206 void dav_identify_hosthdr(void) {
207         hprintf("%s", ChrPtr(site_prefix));
208 }
209
210
211 void Header_HandleIfMatch(StrBuf *Line, ParsedHttpHdrs *hdr)
212 {
213         hdr->HR.dav_ifmatch = Line;
214 }
215         
216
217 void Header_HandleDepth(StrBuf *Line, ParsedHttpHdrs *hdr)
218 {
219         if (!strcasecmp(ChrPtr(Line), "infinity")) {
220                 hdr->HR.dav_depth = 32767;
221         }
222         else if (strcmp(ChrPtr(Line), "0") == 0) {
223                 hdr->HR.dav_depth = 0;
224         }
225         else if (strcmp(ChrPtr(Line), "1") == 0) {
226                 hdr->HR.dav_depth = 1;
227         }
228 }
229
230
231 int Conditional_DAV_DEPTH(StrBuf *Target, WCTemplputParams *TP)
232 {
233         return WC->Hdr->HR.dav_depth == GetTemplateTokenNumber(Target, TP, 2, 0);
234 }
235
236
237 void RegisterDAVNamespace(const char * UrlString, 
238                           long UrlSLen, 
239                           const char *DisplayName, 
240                           long dslen, 
241                           WebcitHandlerFunc F, 
242                           WebcitRESTDispatchID RID,
243                           long Flags)
244 {
245         void *vHandler;
246
247         /* first put it in... */
248         WebcitAddUrlHandler(UrlString, UrlSLen, DisplayName, dslen, F, Flags|PARSE_REST_URL);
249         /* get it out again... */
250         GetHash(HandlerHash, UrlString, UrlSLen, &vHandler);
251         ((WebcitHandler*)vHandler)->RID = RID;
252         /* and keep a copy of it, so we can compare it later */
253         Put(DavNamespaces, UrlString, UrlSLen, vHandler, reference_free_handler);
254 }
255
256
257 int Conditional_DAV_NS(StrBuf *Target, WCTemplputParams *TP)
258 {
259         wcsession *WCC = WC;
260         void *vHandler;
261         const char *NS;
262         long NSLen;
263
264         GetTemplateTokenString(NULL, TP, 2, &NS, &NSLen);
265         GetHash(HandlerHash, NS, NSLen, &vHandler);
266         return WCC->Hdr->HR.Handler == vHandler;
267 }
268
269
270 int Conditional_DAV_NSCURRENT(StrBuf *Target, WCTemplputParams *TP)
271 {
272         wcsession *WCC = WC;
273         void *vHandler;
274
275         vHandler = CTX(CTX_NONE);
276         return WCC->Hdr->HR.Handler == vHandler;
277 }
278
279
280 void tmplput_DAV_NAMESPACE(StrBuf *Target, WCTemplputParams *TP)
281 {
282         wcsession *WCC = WC;
283
284         if (TP->Filter.ContextType == CTX_DAVNS) {
285                 WebcitHandler *H;
286                 H = (WebcitHandler*) CTX(CTX_DAVNS);
287                 if (H != NULL)
288                         StrBufAppendTemplate(Target, TP, H->Name, 0);
289         }
290         else if (WCC->Hdr->HR.Handler != NULL) {
291                 StrBufAppendTemplate(Target, TP, WCC->Hdr->HR.Handler->Name, 0);
292         }
293 }
294
295
296 int GroupdavDispatchREST(RESTDispatchID WhichAction, int IgnoreFloor)
297 {
298         wcsession *WCC = WC;
299         void *vDir;
300         
301         switch(WhichAction){
302         case ExistsID:
303                 GetHash(WCC->Directory, IKEY(WCC->ThisRoom->nRoomNameParts + 1), &vDir);
304                 return locate_message_by_uid(ChrPtr((StrBuf*)vDir)) != -1;
305                 /* TODO: remember euid */
306         case PutID:
307         case DeleteID:
308                 break;
309
310
311         }
312         return 0;
313 }
314
315
316 void
317 ServerStartModule_DAV
318 (void)
319 {
320
321         DavNamespaces = NewHash(1, NULL);
322 }
323
324
325 void 
326 ServerShutdownModule_DAV
327 (void)
328 {
329         DeleteHash(&DavNamespaces);
330 }
331
332
333 void 
334 InitModule_GROUPDAV
335 (void)
336 {
337         RegisterCTX(CTX_DAVNS);
338         RegisterDAVNamespace(HKEY("groupdav"), HKEY("GroupDAV"), 
339                              dav_main, GroupdavDispatchREST, 
340                              XHTTP_COMMANDS|COOKIEUNNEEDED|FORCE_SESSIONCLOSE
341         );
342
343         RegisterNamespace("DAV:HOSTNAME", 0, 0, tmplput_dav_HOSTNAME, NULL, CTX_NONE);
344
345         RegisterConditional("COND:DAV:NS", 0, Conditional_DAV_NS,  CTX_NONE);
346
347         RegisterIterator("DAV:NS", 0, DavNamespaces, NULL, 
348                          NULL, NULL, CTX_DAVNS, CTX_NONE, IT_NOFLAG
349         );
350
351         RegisterConditional("COND:DAV:NSCURRENT", 0, Conditional_DAV_NSCURRENT,  CTX_DAVNS);
352         RegisterNamespace("DAV:NAMESPACE", 0, 1, tmplput_DAV_NAMESPACE, NULL, CTX_NONE);
353
354         RegisterHeaderHandler(HKEY("IF-MATCH"), Header_HandleIfMatch);
355         RegisterHeaderHandler(HKEY("DEPTH"), Header_HandleDepth);
356         RegisterConditional("COND:DAV:DEPTH", 1, Conditional_DAV_DEPTH,  CTX_NONE);
357 }