Began making changes to do better handling of character sets.
[citadel.git] / webcit / groupdav_get.c
index dafcc7653857d09f71fbd4c19c4354884239d2b9..d773fe29bf923701f236e0c99b9718e53f4eece5 100644 (file)
@@ -5,53 +5,72 @@
  *
  */
 
-#include <ctype.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <stdio.h>
-#include <fcntl.h>
-#include <signal.h>
-#include <sys/types.h>
-#include <sys/wait.h>
-#include <sys/socket.h>
-#include <limits.h>
-#include <string.h>
-#include <pwd.h>
-#include <errno.h>
-#include <stdarg.h>
-#include <time.h>
-#include <pthread.h>
 #include "webcit.h"
 #include "webserver.h"
 #include "groupdav.h"
 
 
 /*
- * The pathname is always going to be /groupdav/room_name/euid
+ * Fetch the entire contents of the room as one big ics file.
+ * This is for "webcal://" type access.
+ */    
+void groupdav_get_big_ics(void) {
+       char buf[1024];
+
+       serv_puts("ICAL getics");
+       serv_getln(buf, sizeof buf);
+       if (buf[0] != '1') {
+               wprintf("HTTP/1.1 404 not found\r\n");
+               groupdav_common_headers();
+               wprintf(
+                       "Content-Type: text/plain\r\n"
+                       "\r\n"
+                       "%s\r\n",
+                       &buf[4]
+               );
+               return;
+       }
+
+       wprintf("HTTP/1.1 200 OK\r\n");
+       groupdav_common_headers();
+       wprintf("Content-type: text/calendar; charset=UTF-8\r\n");
+       begin_burst();
+       while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
+               wprintf("%s\r\n", buf);
+       }
+       end_burst();
+}
+
+
+/*
+ * The pathname is always going to take one of two formats:
+ * /groupdav/room_name/euid    (GroupDAV)
+ * /groupdav/room_name         (webcal)
  */
 void groupdav_get(char *dav_pathname) {
-       char dav_roomname[SIZ];
-       char dav_uid[SIZ];
+       char dav_roomname[1024];
+       char dav_uid[1024];
        long dav_msgnum = (-1);
-       char buf[SIZ];
-       int n = 0;
+       char buf[1024];
        int in_body = 0;
        int found_content_type = 0;
 
-       /* First, break off the "/groupdav/" prefix */
-       remove_token(dav_pathname, 0, '/');
-       remove_token(dav_pathname, 0, '/');
-
-       /* Now extract the message euid */
-       n = num_tokens(dav_pathname, '/');
-       extract_token(dav_uid, dav_pathname, n-1, '/');
-       remove_token(dav_pathname, n-1, '/');
+       if (num_tokens(dav_pathname, '/') < 3) {
+               wprintf("HTTP/1.1 404 not found\r\n");
+               groupdav_common_headers();
+               wprintf(
+                       "Content-Type: text/plain\r\n"
+                       "\r\n"
+                       "The object you requested was not found.\r\n"
+               );
+               return;
+       }
 
-       /* What's left is the room name.  Remove trailing slashes. */
-       if (dav_pathname[strlen(dav_pathname)-1] == '/') {
-               dav_pathname[strlen(dav_pathname)-1] = 0;
+       extract_token(dav_roomname, dav_pathname, 2, '/', sizeof dav_roomname);
+       extract_token(dav_uid, dav_pathname, 3, '/', sizeof dav_uid);
+       if ((!strcasecmp(dav_uid, "ics")) || (!strcasecmp(dav_uid, "calendar.ics"))) {
+               strcpy(dav_uid, "");
        }
-       strcpy(dav_roomname, dav_pathname);
 
        /* Go to the correct room. */
        if (strcasecmp(WC->wc_roomname, dav_roomname)) {
@@ -69,9 +88,16 @@ void groupdav_get(char *dav_pathname) {
                return;
        }
 
+       /** GET on the collection itself returns an ICS of the entire collection.
+        */
+       if (!strcasecmp(dav_uid, "")) {
+               groupdav_get_big_ics();
+               return;
+       }
+
        dav_msgnum = locate_message_by_uid(dav_uid);
        serv_printf("MSG2 %ld", dav_msgnum);
-       serv_gets(buf);
+       serv_getln(buf, sizeof buf);
        if (buf[0] != '1') {
                wprintf("HTTP/1.1 404 not found\r\n");
                groupdav_common_headers();
@@ -87,23 +113,31 @@ void groupdav_get(char *dav_pathname) {
 
        wprintf("HTTP/1.1 200 OK\r\n");
        groupdav_common_headers();
-       wprintf("ETag: \"%ld\"\r\n", dav_msgnum);
-       while (serv_gets(buf), strcmp(buf, "000")) {
-               if (!strncasecmp(buf, "Date: ", 6)) {
+       wprintf("etag: \"%ld\"\r\n", dav_msgnum);
+       while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
+               if (in_body) {
                        wprintf("%s\r\n", buf);
                }
-               if (!strncasecmp(buf, "Content-type: ", 14)) {
+               else if (!strncasecmp(buf, "Date: ", 6)) {
                        wprintf("%s\r\n", buf);
+               }
+               else if (!strncasecmp(buf, "Content-type: ", 14)) {
+                       wprintf("%s", buf);
+                       if (bmstrcasestr(buf, "charset=")) {
+                               wprintf("%s\r\n", buf);
+                       }
+                       else {
+                               wprintf("%s;charset=UTF-8\r\n", buf);
+                       }
                        found_content_type = 1;
                }
-               if ((strlen(buf) == 0) && (in_body == 0)) {
+               else if ((strlen(buf) == 0) && (in_body == 0)) {
                        if (!found_content_type) {
                                wprintf("Content-type: text/plain\r\n");
                        }
                        in_body = 1;
-               }
-               if (in_body) {
-                       wprintf("%s\r\n", buf);
+                       begin_burst();
                }
        }
+       end_burst();
 }