ebf145291cce8104f6c6b9b4018d3e44a2632dbc
[citadel.git] / webcit / dav_propfind.c
1 /*
2  * Handles GroupDAV PROPFIND requests.
3  *
4  * A few notes about our XML output:
5  *
6  * --> Yes, we are spewing tags directly instead of using an XML library.
7  *     Whining about it will be summarily ignored.
8  *
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.
12  *
13  * Copyright (c) 2005-2011 by the citadel.org team
14  *
15  * This program is open source software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License version 3.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  */
24
25 #include "webcit.h"
26 #include "webserver.h"
27 #include "dav.h"
28
29 extern int DisableGzip;
30
31 /*
32  * Given an encoded UID, translate that to an unencoded Citadel EUID and
33  * then search for it in the current room.  Return a message number or -1
34  * if not found.
35  *
36  */
37 long locate_message_by_uid(const char *uid) {
38         char buf[256];
39         char decoded_uid[1024];
40         long retval = (-1L);
41
42         /* decode the UID */
43         euid_unescapize(decoded_uid, uid);
44
45         /* ask Citadel if we have this one */
46         serv_printf("EUID %s", decoded_uid);
47         serv_getln(buf, sizeof buf);
48         if (buf[0] == '2') {
49                 retval = atol(&buf[4]);
50         }
51
52         return(retval);
53 }
54
55
56 /*
57  * IgnoreFloor: set to 0 or 1 _nothing else_
58  * Subfolders: direct child floors will be put here.
59  */
60 const folder *GetRESTFolder(int IgnoreFloor, HashList *Subfolders)
61 {
62         wcsession  *WCC = WC;
63         void *vFolder;
64         const folder *ThisFolder = NULL;
65         const folder *FoundFolder = NULL;
66         const folder *BestGuess = NULL;
67         int nBestGuess = 0;
68         HashPos    *itd, *itfl;
69         StrBuf     * Dir;
70         void       *vDir;
71         long        len;
72         const char *Key;
73         int iRoom, jURL, urlp;
74         int delta;
75
76 /*
77  * Guess room: if the full URL matches a room, list thats it. We also need to remember direct sub rooms.
78  * if the URL is longer, we need to find the "best guess" so we can find the room we're in, and the rest
79  * of the URL will be uids and so on.
80  */
81         itfl = GetNewHashPos(WCC->Floors, 0);
82         urlp = GetCount(WCC->Directory);
83
84         while (GetNextHashPos(WCC->Floors, itfl, &len, &Key, &vFolder) && 
85                (ThisFolder == NULL))
86         {
87                 ThisFolder = vFolder;
88                 if (!IgnoreFloor && /* so we can handle legacy URLS... */
89                     (ThisFolder->Floor != WCC->CurrentFloor))
90                         continue;
91
92                 if (ThisFolder->nRoomNameParts > 1) 
93                 {
94                         /*TODO: is that number all right? */
95 //                      if (urlp - ThisFolder->nRoomNameParts != 2) {
96 //                              if (BestGuess != NULL)
97 //                                      continue;
98 //ThisFolder->name
99 //                              itd  = GetNewHashPos(WCC->Directory, 0);
100 //                              GetNextHashPos(WCC->Directory, itd, &len, &Key, &vDir); //TODO: how many to fast forward?
101 //                      }
102                         itd  = GetNewHashPos(WCC->Directory, 0);
103                         GetNextHashPos(WCC->Directory, itd, &len, &Key, &vDir); //TODO: how many to fast forward?
104         
105                         for (iRoom = 0, /* Fast forward the floorname as we checked it above: */ jURL = IgnoreFloor; 
106
107                              (iRoom <= ThisFolder->nRoomNameParts) && (jURL <= urlp); 
108
109                              iRoom++, jURL++, GetNextHashPos(WCC->Directory, itd, &len, &Key, &vDir))
110                         {
111                                 Dir = (StrBuf*)vDir;
112                                 if (strcmp(ChrPtr(ThisFolder->RoomNameParts[iRoom]), 
113                                            ChrPtr(Dir)) != 0)
114                                 {
115                                         DeleteHashPos(&itd);
116                                         continue;
117                                 }
118                         }
119                         DeleteHashPos(&itd);
120                         /* Gotcha? */
121                         if ((iRoom == ThisFolder->nRoomNameParts) && (jURL == urlp))
122                         {
123                                 FoundFolder = ThisFolder;
124                         }
125                         /* URL got more parts then this room, so we remember it for the best guess*/
126                         else if ((jURL <= urlp) &&
127                                  (ThisFolder->nRoomNameParts <= nBestGuess))
128                         {
129                                 BestGuess = ThisFolder;
130                                 nBestGuess = jURL - 1;
131                         }
132                         /* Room has more parts than the URL, it might be a sub-room? */
133                         else if (iRoom <ThisFolder->nRoomNameParts) 
134                         {//// TODO: ThisFolder->nRoomNameParts == urlp - IgnoreFloor???
135                                 Put(Subfolders, SKEY(ThisFolder->name), 
136                                     /* Cast away const, its a reference. */
137                                     (void*)ThisFolder, reference_free_handler);
138                         }
139                 }
140                 else {
141                         delta = GetCount(WCC->Directory) - ThisFolder->nRoomNameParts;
142                         if ((delta != 2) && (nBestGuess > 1))
143                             continue;
144                         
145                         itd  = GetNewHashPos(WCC->Directory, 0);
146                                                 
147                         if (!GetNextHashPos(WCC->Directory, 
148                                             itd, &len, &Key, &vDir) ||
149                             (vDir == NULL))
150                         {
151                                 DeleteHashPos(&itd);
152                                 
153                                 syslog(0, "5\n");
154                                 continue;
155                         }
156                         DeleteHashPos(&itd);
157                         Dir = (StrBuf*) vDir;
158                         if (strcmp(ChrPtr(ThisFolder->name), 
159                                                ChrPtr(Dir))
160                             != 0)
161                         {
162                                 DeleteHashPos(&itd);
163                                 
164                                 syslog(0, "5\n");
165                                 continue;
166                         }
167                         DeleteHashPos(&itfl);
168                         DeleteHashPos(&itd);
169                         if (delta != 2) {
170                                 nBestGuess = 1;
171                                 BestGuess = ThisFolder;
172                         }
173                         else 
174                                 FoundFolder = ThisFolder;
175                 }
176         }
177
178 /* TODO: Subfolders: remove patterns not matching the best guess or thisfolder */
179         DeleteHashPos(&itfl);
180         if (FoundFolder != NULL)
181                 return FoundFolder;
182         else
183                 return BestGuess;
184 }
185
186
187
188
189 long GotoRestRoom(HashList *SubRooms)
190 {
191         int IgnoreFloor = 0; /* deprecated... */
192         wcsession *WCC = WC;
193         long Count;
194         long State;
195         const folder *ThisFolder;
196
197         State = REST_TOPLEVEL;
198
199         if (WCC->Hdr->HR.Handler != NULL) 
200                 State |= REST_IN_NAMESPACE;
201
202         Count = GetCount(WCC->Directory);
203         
204         if (Count == 0) return State;
205
206         if (Count >= 1) State |=REST_IN_FLOOR;
207         if (Count == 1) return State;
208         
209         /* 
210          * More than 3 params and no floor found? 
211          * -> fall back to old non-floored notation
212          */
213         if ((Count >= 3) && (WCC->CurrentFloor == NULL))
214                 IgnoreFloor = 1;
215         if (Count >= 3)
216         {
217                 IgnoreFloor = 0;
218                 State |= REST_IN_FLOOR;
219
220                 ThisFolder = GetRESTFolder(IgnoreFloor, SubRooms);
221                 if (ThisFolder != NULL)
222                 {
223                         if (WCC->ThisRoom != NULL)
224                                 if (CompareRooms(WCC->ThisRoom, ThisFolder) != 0)
225                                         gotoroom(ThisFolder->name);
226                         State |= REST_IN_ROOM;
227                         
228                 }
229                 if (GetCount(SubRooms) > 0)
230                         State |= REST_HAVE_SUB_ROOMS;
231         }
232         if ((WCC->ThisRoom != NULL) && 
233             (Count + IgnoreFloor > 3))
234         {
235                 if (WCC->Hdr->HR.Handler->RID(ExistsID, IgnoreFloor))
236                 {
237                         State |= REST_GOT_LOCAL_PART;
238                 }
239                 else {
240                         /// WHOOPS, not there???
241                         State |= REST_NONEXIST;
242                 }
243
244
245         }
246         return State;
247 }
248
249
250
251 /*
252  * List rooms (or "collections" in DAV terminology) which contain
253  * interesting groupware objects.
254  */
255 void dav_collection_list(void)
256 {
257         wcsession *WCC = WC;
258         char buf[256];
259         char roomname[256];
260         int view;
261         char datestring[256];
262         time_t now;
263         time_t mtime;
264         int is_groupware_collection = 0;
265         int starting_point = 1;         /**< 0 for /, 1 for /groupdav/ */
266
267         if (WCC->Hdr->HR.Handler == NULL) {
268                 starting_point = 0;
269         }
270         else if (StrLength(WCC->Hdr->HR.ReqLine) == 0) {
271                 starting_point = 1;
272         }
273         else {
274                 starting_point = 2;
275         }
276
277         now = time(NULL);
278         http_datestring(datestring, sizeof datestring, now);
279
280         /*
281          * Be rude.  Completely ignore the XML request and simply send them
282          * everything we know about.  Let the client sort it out.
283          */
284         hprintf("HTTP/1.0 207 Multi-Status\r\n");
285         dav_common_headers();
286         hprintf("Date: %s\r\n", datestring);
287         hprintf("Content-type: text/xml\r\n");
288         if (DisableGzip || (!WCC->Hdr->HR.gzip_ok))     
289                 hprintf("Content-encoding: identity\r\n");
290
291         begin_burst();
292
293         wc_printf("<?xml version=\"1.0\" encoding=\"utf-8\"?>"
294                 "<multistatus xmlns=\"DAV:\" xmlns:G=\"http://groupdav.org/\">"
295         );
296
297         /*
298          * If the client is requesting the root, show a root node.
299          */
300         if (starting_point == 0) {
301                 wc_printf("<response>");
302                         wc_printf("<href>");
303                                 dav_identify_host();
304                                 wc_printf("/");
305                         wc_printf("</href>");
306                         wc_printf("<propstat>");
307                                 wc_printf("<status>HTTP/1.1 200 OK</status>");
308                                 wc_printf("<prop>");
309                                         wc_printf("<displayname>/</displayname>");
310                                         wc_printf("<resourcetype><collection/></resourcetype>");
311                                         wc_printf("<getlastmodified>");
312                                                 escputs(datestring);
313                                         wc_printf("</getlastmodified>");
314                                 wc_printf("</prop>");
315                         wc_printf("</propstat>");
316                 wc_printf("</response>");
317         }
318
319         /*
320          * If the client is requesting "/groupdav", show a /groupdav subdirectory.
321          */
322         if ((starting_point + WCC->Hdr->HR.dav_depth) >= 1) {
323                 wc_printf("<response>");
324                         wc_printf("<href>");
325                                 dav_identify_host();
326                                 wc_printf("/groupdav");
327                         wc_printf("</href>");
328                         wc_printf("<propstat>");
329                                 wc_printf("<status>HTTP/1.1 200 OK</status>");
330                                 wc_printf("<prop>");
331                                         wc_printf("<displayname>GroupDAV</displayname>");
332                                         wc_printf("<resourcetype><collection/></resourcetype>");
333                                         wc_printf("<getlastmodified>");
334                                                 escputs(datestring);
335                                         wc_printf("</getlastmodified>");
336                                 wc_printf("</prop>");
337                         wc_printf("</propstat>");
338                 wc_printf("</response>");
339         }
340
341         /*
342          * Now go through the list and make it look like a DAV collection
343          */
344         serv_puts("LKRA");
345         serv_getln(buf, sizeof buf);
346         if (buf[0] == '1') while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
347
348                 extract_token(roomname, buf, 0, '|', sizeof roomname);
349                 view = extract_int(buf, 7);
350                 mtime = extract_long(buf, 8);
351                 http_datestring(datestring, sizeof datestring, mtime);
352
353                 /*
354                  * For now, only list rooms that we know a GroupDAV client
355                  * might be interested in.  In the future we may add
356                  * the rest.
357                  *
358                  * We determine the type of objects which are stored in each
359                  * room by looking at the *default* view for the room.  This
360                  * allows, for example, a Calendar room to appear as a
361                  * GroupDAV calendar even if the user has switched it to a
362                  * Calendar List view.
363                  */
364                 if (    (view == VIEW_CALENDAR) || 
365                         (view == VIEW_TASKS) || 
366                         (view == VIEW_ADDRESSBOOK) ||
367                         (view == VIEW_NOTES) ||
368                         (view == VIEW_JOURNAL) ||
369                         (view == VIEW_WIKI)
370                 ) {
371                         is_groupware_collection = 1;
372                 }
373                 else {
374                         is_groupware_collection = 0;
375                 }
376
377                 if ( (is_groupware_collection) && ((starting_point + WCC->Hdr->HR.dav_depth) >= 2) ) {
378                         wc_printf("<response>");
379
380                         wc_printf("<href>");
381                         dav_identify_host();
382                         wc_printf("/groupdav/");
383                         urlescputs(roomname);
384                         wc_printf("/</href>");
385
386                         wc_printf("<propstat>");
387                         wc_printf("<status>HTTP/1.1 200 OK</status>");
388                         wc_printf("<prop>");
389                         wc_printf("<displayname>");
390                         escputs(roomname);
391                         wc_printf("</displayname>");
392                         wc_printf("<resourcetype><collection/>");
393
394                         switch(view) {
395                         case VIEW_CALENDAR:
396                                 wc_printf("<G:vevent-collection />");
397                                 break;
398                         case VIEW_TASKS:
399                                 wc_printf("<G:vtodo-collection />");
400                                 break;
401                         case VIEW_ADDRESSBOOK:
402                                 wc_printf("<G:vcard-collection />");
403                                 break;
404                         case VIEW_NOTES:
405                                 wc_printf("<G:vnotes-collection />");
406                                 break;
407                         case VIEW_JOURNAL:
408                                 wc_printf("<G:vjournal-collection />");
409                                 break;
410                         case VIEW_WIKI:
411                                 wc_printf("<G:wiki-collection />");
412                                 break;
413                         }
414
415                         wc_printf("</resourcetype>");
416                         wc_printf("<getlastmodified>");
417                                 escputs(datestring);
418                         wc_printf("</getlastmodified>");
419                         wc_printf("</prop>");
420                         wc_printf("</propstat>");
421                         wc_printf("</response>");
422                 }
423         }
424         wc_printf("</multistatus>\n");
425
426         end_burst();
427 }
428
429
430 void propfind_xml_start(void *data, const char *supplied_el, const char **attr) {
431         syslog(LOG_DEBUG, "<%s>", supplied_el);
432 }
433
434 void propfind_xml_end(void *data, const char *supplied_el) {
435         syslog(LOG_DEBUG, "</%s>", supplied_el);
436 }
437
438
439
440 /*
441  * The pathname is always going to be /groupdav/room_name/msg_num
442  */
443 void dav_propfind(void) 
444 {
445         wcsession *WCC = WC;
446         StrBuf *dav_roomname;
447         StrBuf *dav_uid;
448         StrBuf *MsgNum;
449         long BufLen;
450         long dav_msgnum = (-1);
451         char uid[256];
452         char encoded_uid[256];
453         long *msgs = NULL;
454         int num_msgs = 0;
455         int i;
456         char datestring[256];
457         time_t now;
458
459         now = time(NULL);
460         http_datestring(datestring, sizeof datestring, now);
461
462         int parse_success = 0;
463         XML_Parser xp = XML_ParserCreateNS(NULL, '|');
464         if (xp) {
465                 // XML_SetUserData(xp, XXX);
466                 XML_SetElementHandler(xp, propfind_xml_start, propfind_xml_end);
467                 // XML_SetCharacterDataHandler(xp, xrds_xml_chardata);
468
469                 const char *req = ChrPtr(WCC->upload);
470                 if (req) {
471                         req = strchr(req, '<');                 /* hunt for the first tag */
472                 }
473                 if (!req) {
474                         req = "ERROR";                          /* force it to barf */
475                 }
476
477                 i = XML_Parse(xp, req, strlen(req), 1);
478                 if (!i) {
479                         syslog(LOG_DEBUG, "XML_Parse() failed: %s", XML_ErrorString(XML_GetErrorCode(xp)));
480                         XML_ParserFree(xp);
481                         parse_success = 0;
482                 }
483                 else {
484                         parse_success = 1;
485                 }
486         }
487
488         if (!parse_success) {
489                 hprintf("HTTP/1.1 500 Internal Server Error\r\n");
490                 dav_common_headers();
491                 hprintf("Date: %s\r\n", datestring);
492                 hprintf("Content-Type: text/plain\r\n");
493                 wc_printf("An internal error has occurred at %s:%d.\r\n", __FILE__ , __LINE__ );
494                 end_burst();
495                 return;
496         }
497
498         dav_roomname = NewStrBuf();
499         dav_uid = NewStrBuf();
500         StrBufExtract_token(dav_roomname, WCC->Hdr->HR.ReqLine, 0, '/');
501         StrBufExtract_token(dav_uid, WCC->Hdr->HR.ReqLine, 1, '/');
502
503         /*
504          * If the room name is blank, the client is requesting a folder list.
505          */
506         if (StrLength(dav_roomname) == 0) {
507                 dav_collection_list();
508                 FreeStrBuf(&dav_roomname);
509                 FreeStrBuf(&dav_uid);
510                 return;
511         }
512
513         /* Go to the correct room. */
514         if (strcasecmp(ChrPtr(WCC->CurRoom.name), ChrPtr(dav_roomname))) {
515                 gotoroom(dav_roomname);
516         }
517         if (strcasecmp(ChrPtr(WCC->CurRoom.name), ChrPtr(dav_roomname))) {
518                 hprintf("HTTP/1.1 404 not found\r\n");
519                 dav_common_headers();
520                 hprintf("Date: %s\r\n", datestring);
521                 hprintf("Content-Type: text/plain\r\n");
522                 wc_printf("There is no folder called \"%s\" on this server.\r\n", ChrPtr(dav_roomname));
523                 end_burst();
524                 FreeStrBuf(&dav_roomname);
525                 FreeStrBuf(&dav_uid);
526                 return;
527         }
528
529         /* If dav_uid is non-empty, client is requesting a PROPFIND on
530          * a specific item in the room.  This is not valid GroupDAV, but
531          * it is valid WebDAV (and probably CalDAV too).
532          */
533         if (StrLength(dav_uid) != 0) {
534
535                 dav_msgnum = locate_message_by_uid(ChrPtr(dav_uid));
536                 if (dav_msgnum < 0) {
537                         hprintf("HTTP/1.1 404 not found\r\n");
538                         dav_common_headers();
539                         hprintf("Content-Type: text/plain\r\n");
540                         wc_printf("Object \"%s\" was not found in the \"%s\" folder.\r\n",
541                                 ChrPtr(dav_uid),
542                                 ChrPtr(dav_roomname)
543                         );
544                         end_burst();
545                         FreeStrBuf(&dav_roomname);
546                         FreeStrBuf(&dav_uid);
547                         return;
548                 }
549
550                 /* Be rude.  Completely ignore the XML request and simply send them
551                  * everything we know about (which is going to simply be the ETag and
552                  * nothing else).  Let the client-side parser sort it out.
553                  */
554                 hprintf("HTTP/1.0 207 Multi-Status\r\n");
555                 dav_common_headers();
556                 hprintf("Date: %s\r\n", datestring);
557                 hprintf("Content-type: text/xml\r\n");
558                 if (DisableGzip || (!WCC->Hdr->HR.gzip_ok))     
559                         hprintf("Content-encoding: identity\r\n");
560         
561                 begin_burst();
562         
563                 wc_printf("<?xml version=\"1.0\" encoding=\"utf-8\"?>"
564                         "<multistatus xmlns=\"DAV:\">"
565                 );
566
567                 wc_printf("<response>");
568                 
569                 wc_printf("<href>");
570                 dav_identify_host();
571                 wc_printf("/groupdav/");
572                 urlescputs(ChrPtr(WCC->CurRoom.name));
573                 euid_escapize(encoded_uid, ChrPtr(dav_uid));
574                 wc_printf("/%s", encoded_uid);
575                 wc_printf("</href>");
576                 wc_printf("<propstat>");
577                 wc_printf("<status>HTTP/1.1 200 OK</status>");
578                 wc_printf("<prop>");
579                 wc_printf("<getetag>\"%ld\"</getetag>", dav_msgnum);
580                 wc_printf("<getlastmodified>");
581                 escputs(datestring);
582                 wc_printf("</getlastmodified>");
583                 wc_printf("</prop>");
584                 wc_printf("</propstat>");
585
586                 wc_printf("</response>\n");
587                 wc_printf("</multistatus>\n");
588                 end_burst();
589                 FreeStrBuf(&dav_roomname);
590                 FreeStrBuf(&dav_uid);
591                 return;
592         }
593         FreeStrBuf(&dav_roomname);
594         FreeStrBuf(&dav_uid);
595
596
597         /*
598          * We got to this point, which means that the client is requesting
599          * a 'collection' (i.e. a list of all items in the room).
600          *
601          * Be rude.  Completely ignore the XML request and simply send them
602          * everything we know about (which is going to simply be the ETag and
603          * nothing else).  Let the client-side parser sort it out.
604          */
605         hprintf("HTTP/1.0 207 Multi-Status\r\n");
606         dav_common_headers();
607         hprintf("Date: %s\r\n", datestring);
608         hprintf("Content-type: text/xml\r\n");
609         if (DisableGzip || (!WCC->Hdr->HR.gzip_ok)) {
610                 hprintf("Content-encoding: identity\r\n");
611         }
612         begin_burst();
613
614         wc_printf("<?xml version=\"1.0\" encoding=\"utf-8\"?>"
615                 "<multistatus "
616                         "xmlns=\"DAV:\" "
617                         "xmlns:G=\"http://groupdav.org/\" "
618                         "xmlns:CALDAV=\"urn:ietf:params:xml:ns:caldav\""
619                 ">"
620         );
621
622         /* Transmit the collection resource (FIXME check depth and starting point) */
623         wc_printf("<response>");
624
625         wc_printf("<href>");
626         dav_identify_host();
627         wc_printf("/groupdav/");
628         urlescputs(ChrPtr(WCC->CurRoom.name));
629         wc_printf("</href>");
630
631         wc_printf("<propstat>");
632         wc_printf("<status>HTTP/1.1 200 OK</status>");
633         wc_printf("<prop>");
634         wc_printf("<displayname>");
635         escputs(ChrPtr(WCC->CurRoom.name));
636         wc_printf("</displayname>");
637
638         wc_printf("<owner/>");          /* empty owner ought to be legal; see rfc3744 section 5.1 */
639
640         wc_printf("<resourcetype><collection/>");
641         switch(WCC->CurRoom.defview) {
642                 case VIEW_CALENDAR:
643                         wc_printf("<G:vevent-collection />");
644                         wc_printf("<CALDAV:calendar />");
645                         break;
646                 case VIEW_TASKS:
647                         wc_printf("<G:vtodo-collection />");
648                         break;
649                 case VIEW_ADDRESSBOOK:
650                         wc_printf("<G:vcard-collection />");
651                         break;
652         }
653         wc_printf("</resourcetype>");
654
655         /* FIXME get the mtime
656         wc_printf("<getlastmodified>");
657                 escputs(datestring);
658         wc_printf("</getlastmodified>");
659         */
660         wc_printf("</prop>");
661         wc_printf("</propstat>");
662         wc_printf("</response>");
663
664         /* Transmit the collection listing (FIXME check depth and starting point) */
665
666         MsgNum = NewStrBuf();
667         serv_puts("MSGS ALL");
668
669         StrBuf_ServGetln(MsgNum);
670         if (GetServerStatus(MsgNum, NULL) == 1)
671                 while (BufLen = StrBuf_ServGetln(MsgNum), 
672                        ((BufLen >= 0) && 
673                         ((BufLen != 3) || strcmp(ChrPtr(MsgNum), "000"))  ))
674                 {
675                         msgs = realloc(msgs, ++num_msgs * sizeof(long));
676                         msgs[num_msgs-1] = StrTol(MsgNum);
677                 }
678
679         if (num_msgs > 0) for (i=0; i<num_msgs; ++i) {
680
681                 syslog(LOG_DEBUG, "PROPFIND enumerating message # %ld", msgs[i]);
682                 strcpy(uid, "");
683                 now = (-1);
684                 serv_printf("MSG0 %ld|3", msgs[i]);
685                 StrBuf_ServGetln(MsgNum);
686                 if (GetServerStatus(MsgNum, NULL) == 1)
687                         while (BufLen = StrBuf_ServGetln(MsgNum), 
688                                ((BufLen >= 0) && 
689                                 ((BufLen != 3) || strcmp(ChrPtr(MsgNum), "000")) ))
690                         {
691                                 if (!strncasecmp(ChrPtr(MsgNum), "exti=", 5)) {
692                                         strcpy(uid, &ChrPtr(MsgNum)[5]);
693                                 }
694                                 else if (!strncasecmp(ChrPtr(MsgNum), "time=", 5)) {
695                                         now = atol(&ChrPtr(MsgNum)[5]);
696                         }
697                 }
698
699                 if (!IsEmptyStr(uid)) {
700                         wc_printf("<response>");
701                                 wc_printf("<href>");
702                                         dav_identify_host();
703                                         wc_printf("/groupdav/");
704                                         urlescputs(ChrPtr(WCC->CurRoom.name));
705                                         euid_escapize(encoded_uid, uid);
706                                         wc_printf("/%s", encoded_uid);
707                                 wc_printf("</href>");
708                                 switch(WCC->CurRoom.defview) {
709                                 case VIEW_CALENDAR:
710                                         wc_printf("<getcontenttype>text/x-ical</getcontenttype>");
711                                         break;
712                                 case VIEW_TASKS:
713                                         wc_printf("<getcontenttype>text/x-ical</getcontenttype>");
714                                         break;
715                                 case VIEW_ADDRESSBOOK:
716                                         wc_printf("<getcontenttype>text/x-vcard</getcontenttype>");
717                                         break;
718                                 }
719                                 wc_printf("<propstat>");
720                                         wc_printf("<status>HTTP/1.1 200 OK</status>");
721                                         wc_printf("<prop>");
722                                                 wc_printf("<getetag>\"%ld\"</getetag>", msgs[i]);
723                                         if (now > 0L) {
724                                                 http_datestring(datestring, sizeof datestring, now);
725                                                 wc_printf("<getlastmodified>");
726                                                 escputs(datestring);
727                                                 wc_printf("</getlastmodified>");
728                                         }
729                                         wc_printf("</prop>");
730                                 wc_printf("</propstat>");
731                         wc_printf("</response>");
732                 }
733         }
734         FreeStrBuf(&MsgNum);
735
736         wc_printf("</multistatus>\n");
737         end_burst();
738
739         if (msgs != NULL) {
740                 free(msgs);
741         }
742 }
743
744
745
746 int ParseMessageListHeaders_EUID(StrBuf *Line, 
747                                  const char **pos, 
748                                  message_summary *Msg, 
749                                  StrBuf *ConversionBuffer)
750 {
751         Msg->euid = NewStrBuf();
752         StrBufExtract_NextToken(Msg->euid,  Line, pos, '|');
753         Msg->date = StrBufExtractNext_long(Line, pos, '|');
754         
755         return StrLength(Msg->euid) > 0;
756 }
757
758 int DavUIDL_GetParamsGetServerCall(SharedMessageStatus *Stat, 
759                                     void **ViewSpecific, 
760                                     long oper, 
761                                     char *cmd, 
762                                     long len)
763 {
764         Stat->defaultsortorder = 0;
765         Stat->sortit = 0;
766         Stat->load_seen = 0;
767         Stat->maxmsgs  = 9999999;
768
769         snprintf(cmd, len, "MSGS ALL|||2");
770         return 200;
771 }
772
773 int DavUIDL_RenderView_or_Tail(SharedMessageStatus *Stat, 
774                                 void **ViewSpecific, 
775                                 long oper)
776 {
777         
778         DoTemplate(HKEY("msg_listview"),NULL,&NoCtx);
779         
780         return 0;
781 }
782
783 int DavUIDL_Cleanup(void **ViewSpecific)
784 {
785         /* Note: wDumpContent() will output one additional </div> tag. */
786         /* We ought to move this out into template */
787         wDumpContent(1);
788
789         return 0;
790 }
791
792
793
794
795 void 
796 InitModule_PROPFIND
797 (void)
798 {
799         RegisterReadLoopHandlerset(
800                 eReadEUIDS,
801                 DavUIDL_GetParamsGetServerCall,
802                 NULL, /// TODO: is this right?
803                 ParseMessageListHeaders_EUID,
804                 NULL, //// ""
805                 DavUIDL_RenderView_or_Tail,
806                 DavUIDL_Cleanup);
807
808 }