2 * Handles GroupDAV PROPFIND requests.
4 * A few notes about our XML output:
6 * --> Yes, we are spewing tags directly instead of using an XML library.
7 * Whining about it will be summarily ignored.
9 * --> XML is deliberately output with no whitespace/newlines between tags.
10 * This makes it difficult to read, but we have discovered clients which
11 * crash when you try to pretty it up.
13 * Copyright (c) 2005-2011 by the citadel.org team
15 * This program is open source software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 3 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include "webserver.h"
34 extern int DisableGzip;
37 * Given an encoded UID, translate that to an unencoded Citadel EUID and
38 * then search for it in the current room. Return a message number or -1
42 long locate_message_by_uid(const char *uid) {
44 char decoded_uid[1024];
48 euid_unescapize(decoded_uid, uid);
50 /* ask Citadel if we have this one */
51 serv_printf("EUID %s", decoded_uid);
52 serv_getln(buf, sizeof buf);
54 retval = atol(&buf[4]);
62 * IgnoreFloor: set to 0 or 1 _nothing else_
63 * Subfolders: direct child floors will be put here.
65 const folder *GetRESTFolder(int IgnoreFloor, HashList *Subfolders)
69 const folder *ThisFolder = NULL;
70 const folder *FoundFolder = NULL;
71 const folder *BestGuess = NULL;
78 int iRoom, jURL, urlp;
82 * Guess room: if the full URL matches a room, list thats it. We also need to remember direct sub rooms.
83 * if the URL is longer, we need to find the "best guess" so we can find the room we're in, and the rest of the URL will be uids and so on.
85 itfl = GetNewHashPos(WCC->Floors, 0);
86 urlp = GetCount(WCC->Directory);
88 while (GetNextHashPos(WCC->Floors, itfl, &len, &Key, &vFolder) &&
92 if (!IgnoreFloor && /* so we can handle legacy URLS... */
93 (ThisFolder->Floor != WCC->CurrentFloor))
97 if (ThisFolder->nRoomNameParts > 1)
99 /*TODO: is that number all right? */
100 // if (urlp - ThisFolder->nRoomNameParts != 2) {
101 // if (BestGuess != NULL)
104 // itd = GetNewHashPos(WCC->Directory, 0);
105 // GetNextHashPos(WCC->Directory, itd, &len, &Key, &vDir); //TODO: how many to fast forward?
107 itd = GetNewHashPos(WCC->Directory, 0);
108 GetNextHashPos(WCC->Directory, itd, &len, &Key, &vDir); //TODO: how many to fast forward?
110 for (iRoom = 0, /* Fast forward the floorname as we checked it above: */ jURL = IgnoreFloor;
112 (iRoom <= ThisFolder->nRoomNameParts) && (jURL <= urlp);
114 iRoom++, jURL++, GetNextHashPos(WCC->Directory, itd, &len, &Key, &vDir))
117 if (strcmp(ChrPtr(ThisFolder->RoomNameParts[iRoom]),
126 if ((iRoom == ThisFolder->nRoomNameParts) && (jURL == urlp))
128 FoundFolder = ThisFolder;
130 /* URL got more parts then this room, so we remember it for the best guess*/
131 else if ((jURL <= urlp) &&
132 (ThisFolder->nRoomNameParts <= nBestGuess))
134 BestGuess = ThisFolder;
135 nBestGuess = jURL - 1;
137 /* Room has more parts than the URL, it might be a sub-room? */
138 else if (iRoom <ThisFolder->nRoomNameParts)
139 {//// TODO: ThisFolder->nRoomNameParts == urlp - IgnoreFloor???
140 Put(Subfolders, SKEY(ThisFolder->name),
141 /* Cast away const, its a reference. */
142 (void*)ThisFolder, reference_free_handler);
146 delta = GetCount(WCC->Directory) - ThisFolder->nRoomNameParts;
147 if ((delta != 2) && (nBestGuess > 1))
150 itd = GetNewHashPos(WCC->Directory, 0);
152 if (!GetNextHashPos(WCC->Directory,
153 itd, &len, &Key, &vDir) ||
162 Dir = (StrBuf*) vDir;
163 if (strcmp(ChrPtr(ThisFolder->name),
172 DeleteHashPos(&itfl);
176 BestGuess = ThisFolder;
179 FoundFolder = ThisFolder;
183 /* TODO: Subfolders: remove patterns not matching the best guess or thisfolder */
184 DeleteHashPos(&itfl);
185 if (FoundFolder != NULL)
194 long GotoRestRoom(HashList *SubRooms)
196 int IgnoreFloor = 0; /* deprecated... */
200 const folder *ThisFolder;
202 State = REST_TOPLEVEL;
204 if (WCC->Hdr->HR.Handler != NULL)
205 State |= REST_IN_NAMESPACE;
207 Count = GetCount(WCC->Directory);
209 if (Count == 0) return State;
211 if (Count >= 1) State |=REST_IN_FLOOR;
212 if (Count == 1) return State;
215 * More than 3 params and no floor found?
216 * -> fall back to old non-floored notation
218 if ((Count >= 3) && (WCC->CurrentFloor == NULL))
223 State |= REST_IN_FLOOR;
225 ThisFolder = GetRESTFolder(IgnoreFloor, SubRooms);
226 if (ThisFolder != NULL)
228 if (WCC->ThisRoom != NULL)
229 if (CompareRooms(WCC->ThisRoom, ThisFolder) != 0)
230 gotoroom(ThisFolder->name);
231 State |= REST_IN_ROOM;
234 if (GetCount(SubRooms) > 0)
235 State |= REST_HAVE_SUB_ROOMS;
237 if ((WCC->ThisRoom != NULL) &&
238 (Count + IgnoreFloor > 3))
240 if (WCC->Hdr->HR.Handler->RID(ExistsID, IgnoreFloor))
242 State |= REST_GOT_LOCAL_PART;
245 /// WHOOPS, not there???
246 State |= REST_NONEXIST;
257 * List rooms (or "collections" in DAV terminology) which contain
258 * interesting groupware objects.
260 void dav_collection_list(void)
266 char datestring[256];
269 int is_groupware_collection = 0;
270 int starting_point = 1; /**< 0 for /, 1 for /groupdav/ */
272 if (WCC->Hdr->HR.Handler == NULL) {
275 else if (StrLength(WCC->Hdr->HR.ReqLine) == 0) {
283 http_datestring(datestring, sizeof datestring, now);
286 * Be rude. Completely ignore the XML request and simply send them
287 * everything we know about. Let the client sort it out.
289 hprintf("HTTP/1.0 207 Multi-Status\r\n");
290 dav_common_headers();
291 hprintf("Date: %s\r\n", datestring);
292 hprintf("Content-type: text/xml\r\n");
293 if (DisableGzip || (!WCC->Hdr->HR.gzip_ok))
294 hprintf("Content-encoding: identity\r\n");
298 wc_printf("<?xml version=\"1.0\" encoding=\"utf-8\"?>"
299 "<multistatus xmlns=\"DAV:\" xmlns:G=\"http://groupdav.org/\">"
303 * If the client is requesting the root, show a root node.
305 if (starting_point == 0) {
306 wc_printf("<response>");
310 wc_printf("</href>");
311 wc_printf("<propstat>");
312 wc_printf("<status>HTTP/1.1 200 OK</status>");
314 wc_printf("<displayname>/</displayname>");
315 wc_printf("<resourcetype><collection/></resourcetype>");
316 wc_printf("<getlastmodified>");
318 wc_printf("</getlastmodified>");
319 wc_printf("</prop>");
320 wc_printf("</propstat>");
321 wc_printf("</response>");
325 * If the client is requesting "/groupdav", show a /groupdav subdirectory.
327 if ((starting_point + WCC->Hdr->HR.dav_depth) >= 1) {
328 wc_printf("<response>");
331 wc_printf("/groupdav");
332 wc_printf("</href>");
333 wc_printf("<propstat>");
334 wc_printf("<status>HTTP/1.1 200 OK</status>");
336 wc_printf("<displayname>GroupDAV</displayname>");
337 wc_printf("<resourcetype><collection/></resourcetype>");
338 wc_printf("<getlastmodified>");
340 wc_printf("</getlastmodified>");
341 wc_printf("</prop>");
342 wc_printf("</propstat>");
343 wc_printf("</response>");
347 * Now go through the list and make it look like a DAV collection
350 serv_getln(buf, sizeof buf);
351 if (buf[0] == '1') while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
353 extract_token(roomname, buf, 0, '|', sizeof roomname);
354 view = extract_int(buf, 7);
355 mtime = extract_long(buf, 8);
356 http_datestring(datestring, sizeof datestring, mtime);
359 * For now, only list rooms that we know a GroupDAV client
360 * might be interested in. In the future we may add
363 * We determine the type of objects which are stored in each
364 * room by looking at the *default* view for the room. This
365 * allows, for example, a Calendar room to appear as a
366 * GroupDAV calendar even if the user has switched it to a
367 * Calendar List view.
369 if ( (view == VIEW_CALENDAR) ||
370 (view == VIEW_TASKS) ||
371 (view == VIEW_ADDRESSBOOK) ||
372 (view == VIEW_NOTES) ||
373 (view == VIEW_JOURNAL) ||
376 is_groupware_collection = 1;
379 is_groupware_collection = 0;
382 if ( (is_groupware_collection) && ((starting_point + WCC->Hdr->HR.dav_depth) >= 2) ) {
383 wc_printf("<response>");
387 wc_printf("/groupdav/");
388 urlescputs(roomname);
389 wc_printf("/</href>");
391 wc_printf("<propstat>");
392 wc_printf("<status>HTTP/1.1 200 OK</status>");
394 wc_printf("<displayname>");
396 wc_printf("</displayname>");
397 wc_printf("<resourcetype><collection/>");
401 wc_printf("<G:vevent-collection />");
404 wc_printf("<G:vtodo-collection />");
406 case VIEW_ADDRESSBOOK:
407 wc_printf("<G:vcard-collection />");
410 wc_printf("<G:vnotes-collection />");
413 wc_printf("<G:vjournal-collection />");
416 wc_printf("<G:wiki-collection />");
420 wc_printf("</resourcetype>");
421 wc_printf("<getlastmodified>");
423 wc_printf("</getlastmodified>");
424 wc_printf("</prop>");
425 wc_printf("</propstat>");
426 wc_printf("</response>");
429 wc_printf("</multistatus>\n");
437 * The pathname is always going to be /groupdav/room_name/msg_num
439 void dav_propfind(void)
442 StrBuf *dav_roomname;
446 long dav_msgnum = (-1);
448 char encoded_uid[256];
452 char datestring[256];
455 syslog(LOG_DEBUG, "PROPFIND\n\033[31m%s\033[0m", ChrPtr(WCC->upload));
458 http_datestring(datestring, sizeof datestring, now);
460 dav_roomname = NewStrBuf();
461 dav_uid = NewStrBuf();
462 StrBufExtract_token(dav_roomname, WCC->Hdr->HR.ReqLine, 0, '/');
463 StrBufExtract_token(dav_uid, WCC->Hdr->HR.ReqLine, 1, '/');
466 * If the room name is blank, the client is requesting a
469 if (StrLength(dav_roomname) == 0) {
470 dav_collection_list();
471 FreeStrBuf(&dav_roomname);
472 FreeStrBuf(&dav_uid);
476 /* Go to the correct room. */
477 if (strcasecmp(ChrPtr(WCC->CurRoom.name), ChrPtr(dav_roomname))) {
478 gotoroom(dav_roomname);
480 if (strcasecmp(ChrPtr(WCC->CurRoom.name), ChrPtr(dav_roomname))) {
481 hprintf("HTTP/1.1 404 not found\r\n");
482 dav_common_headers();
483 hprintf("Date: %s\r\n", datestring);
484 hprintf("Content-Type: text/plain\r\n");
485 wc_printf("There is no folder called \"%s\" on this server.\r\n",
489 FreeStrBuf(&dav_roomname);
490 FreeStrBuf(&dav_uid);
494 /* If dav_uid is non-empty, client is requesting a PROPFIND on
495 * a specific item in the room. This is not valid GroupDAV, but
496 * it is valid WebDAV (and probably CalDAV too).
498 if (StrLength(dav_uid) != 0) {
500 dav_msgnum = locate_message_by_uid(ChrPtr(dav_uid));
501 if (dav_msgnum < 0) {
502 hprintf("HTTP/1.1 404 not found\r\n");
503 dav_common_headers();
504 hprintf("Content-Type: text/plain\r\n");
505 wc_printf("Object \"%s\" was not found in the \"%s\" folder.\r\n",
510 FreeStrBuf(&dav_roomname);
511 FreeStrBuf(&dav_uid);
515 /* Be rude. Completely ignore the XML request and simply send them
516 * everything we know about (which is going to simply be the ETag and
517 * nothing else). Let the client-side parser sort it out.
519 hprintf("HTTP/1.0 207 Multi-Status\r\n");
520 dav_common_headers();
521 hprintf("Date: %s\r\n", datestring);
522 hprintf("Content-type: text/xml\r\n");
523 if (DisableGzip || (!WCC->Hdr->HR.gzip_ok))
524 hprintf("Content-encoding: identity\r\n");
528 wc_printf("<?xml version=\"1.0\" encoding=\"utf-8\"?>"
529 "<multistatus xmlns=\"DAV:\">"
532 wc_printf("<response>");
536 wc_printf("/groupdav/");
537 urlescputs(ChrPtr(WCC->CurRoom.name));
538 euid_escapize(encoded_uid, ChrPtr(dav_uid));
539 wc_printf("/%s", encoded_uid);
540 wc_printf("</href>");
541 wc_printf("<propstat>");
542 wc_printf("<status>HTTP/1.1 200 OK</status>");
544 wc_printf("<getetag>\"%ld\"</getetag>", dav_msgnum);
545 wc_printf("<getlastmodified>");
547 wc_printf("</getlastmodified>");
548 wc_printf("</prop>");
549 wc_printf("</propstat>");
551 wc_printf("</response>\n");
552 wc_printf("</multistatus>\n");
554 FreeStrBuf(&dav_roomname);
555 FreeStrBuf(&dav_uid);
558 FreeStrBuf(&dav_roomname);
559 FreeStrBuf(&dav_uid);
563 * We got to this point, which means that the client is requesting
564 * a 'collection' (i.e. a list of all items in the room).
566 * Be rude. Completely ignore the XML request and simply send them
567 * everything we know about (which is going to simply be the ETag and
568 * nothing else). Let the client-side parser sort it out.
570 hprintf("HTTP/1.0 207 Multi-Status\r\n");
571 dav_common_headers();
572 hprintf("Date: %s\r\n", datestring);
573 hprintf("Content-type: text/xml\r\n");
574 if (DisableGzip || (!WCC->Hdr->HR.gzip_ok))
575 hprintf("Content-encoding: identity\r\n");
579 wc_printf("<?xml version=\"1.0\" encoding=\"utf-8\"?>"
580 "<multistatus xmlns=\"DAV:\" xmlns:G=\"http://groupdav.org/\">"
584 /* Transmit the collection resource (FIXME check depth and starting point) */
585 wc_printf("<response>");
589 wc_printf("/groupdav/");
590 urlescputs(ChrPtr(WCC->CurRoom.name));
591 wc_printf("</href>");
593 wc_printf("<propstat>");
594 wc_printf("<status>HTTP/1.1 200 OK</status>");
596 wc_printf("<displayname>");
597 escputs(ChrPtr(WCC->CurRoom.name));
598 wc_printf("</displayname>");
599 wc_printf("<resourcetype><collection/>");
601 switch(WCC->CurRoom.defview) {
603 wc_printf("<G:vevent-collection />");
606 wc_printf("<G:vtodo-collection />");
608 case VIEW_ADDRESSBOOK:
609 wc_printf("<G:vcard-collection />");
613 wc_printf("</resourcetype>");
614 /* FIXME get the mtime
615 wc_printf("<getlastmodified>");
617 wc_printf("</getlastmodified>");
619 wc_printf("</prop>");
620 wc_printf("</propstat>");
621 wc_printf("</response>");
623 /* Transmit the collection listing (FIXME check depth and starting point) */
625 MsgNum = NewStrBuf();
626 serv_puts("MSGS ALL");
628 StrBuf_ServGetln(MsgNum);
629 if (GetServerStatus(MsgNum, NULL) == 1)
630 while (BufLen = StrBuf_ServGetln(MsgNum),
632 ((BufLen != 3) || strcmp(ChrPtr(MsgNum), "000")) ))
634 msgs = realloc(msgs, ++num_msgs * sizeof(long));
635 msgs[num_msgs-1] = StrTol(MsgNum);
638 if (num_msgs > 0) for (i=0; i<num_msgs; ++i) {
642 serv_printf("MSG0 %ld|3", msgs[i]);
643 StrBuf_ServGetln(MsgNum);
644 if (GetServerStatus(MsgNum, NULL) == 1)
645 while (BufLen = StrBuf_ServGetln(MsgNum),
647 ((BufLen != 3) || strcmp(ChrPtr(MsgNum), "000")) ))
649 if (!strncasecmp(ChrPtr(MsgNum), "exti=", 5)) {
650 strcpy(uid, &ChrPtr(MsgNum)[5]);
652 else if (!strncasecmp(ChrPtr(MsgNum), "time=", 5)) {
653 now = atol(&ChrPtr(MsgNum)[5]);
657 if (!IsEmptyStr(uid)) {
658 wc_printf("<response>");
661 wc_printf("/groupdav/");
662 urlescputs(ChrPtr(WCC->CurRoom.name));
663 euid_escapize(encoded_uid, uid);
664 wc_printf("/%s", encoded_uid);
665 wc_printf("</href>");
666 switch(WCC->CurRoom.defview) {
668 wc_printf("<getcontenttype>text/x-ical</getcontenttype>");
671 wc_printf("<getcontenttype>text/x-ical</getcontenttype>");
673 case VIEW_ADDRESSBOOK:
674 wc_printf("<getcontenttype>text/x-vcard</getcontenttype>");
677 wc_printf("<propstat>");
678 wc_printf("<status>HTTP/1.1 200 OK</status>");
680 wc_printf("<getetag>\"%ld\"</getetag>", msgs[i]);
682 http_datestring(datestring, sizeof datestring, now);
683 wc_printf("<getlastmodified>");
685 wc_printf("</getlastmodified>");
687 wc_printf("</prop>");
688 wc_printf("</propstat>");
689 wc_printf("</response>");
694 wc_printf("</multistatus>\n");
704 int ParseMessageListHeaders_EUID(StrBuf *Line,
706 message_summary *Msg,
707 StrBuf *ConversionBuffer)
709 Msg->euid = NewStrBuf();
710 StrBufExtract_NextToken(Msg->euid, Line, pos, '|');
711 Msg->date = StrBufExtractNext_long(Line, pos, '|');
713 return StrLength(Msg->euid) > 0;
716 int DavUIDL_GetParamsGetServerCall(SharedMessageStatus *Stat,
722 Stat->defaultsortorder = 0;
725 Stat->maxmsgs = 9999999;
727 snprintf(cmd, len, "MSGS ALL|||2");
731 int DavUIDL_RenderView_or_Tail(SharedMessageStatus *Stat,
736 DoTemplate(HKEY("msg_listview"),NULL,&NoCtx);
741 int DavUIDL_Cleanup(void **ViewSpecific)
743 /* Note: wDumpContent() will output one additional </div> tag. */
744 /* We ought to move this out into template */
757 RegisterReadLoopHandlerset(
759 DavUIDL_GetParamsGetServerCall,
760 NULL, /// TODO: is this right?
761 ParseMessageListHeaders_EUID,
763 DavUIDL_RenderView_or_Tail,