* Moved to the new string tokenizer API
[citadel.git] / webcit / groupdav_propfind.c
index 76003ffa92b80f40b5af745241c019560bfa906d..2780a5909cb9f4b6c5c1c00736ca671aa7953290 100644 (file)
@@ -3,6 +3,16 @@
  *
  * Handles GroupDAV PROPFIND requests.
  *
+ * A few notes about our XML output:
+ *
+ * --> Yes, we are spewing tags directly instead of using an XML library.
+ *     If you would like to rewrite this using libxml2, code it up and submit
+ *     a patch.  Whining will be summarily ignored.
+ *
+ * --> XML is deliberately output with no whitespace/newlines between tags.
+ *     This makes it difficult to read, but we have discovered clients which
+ *     crash when you try to pretty it up.
+ *
  */
 
 #include <ctype.h>
 long locate_message_by_uid(char *uid) {
        char buf[SIZ];
        char decoded_uid[SIZ];
-       int i, j;
-       int ch;
        long retval = (-1L);
 
        /* Decode the uid */
-       j=0;
-       for (i=0; i<strlen(uid); i=i+2) {
-               ch = 0;
-               sscanf(&uid[i], "%02x", &ch);
-               decoded_uid[j] = ch;
-               decoded_uid[j+1] = 0;
-               ++j;
-       }
+       euid_unescapize(decoded_uid, uid);
 
        serv_puts("MSGS ALL|0|1");
        serv_gets(buf);
@@ -71,59 +72,85 @@ void groupdav_folder_list(void) {
        char buf[SIZ];
        char roomname[SIZ];
        int view;
+       char datestring[SIZ];
+       time_t now;
+
+       now = time(NULL);
+       http_datestring(datestring, sizeof datestring, now);
 
        /*
         * Be rude.  Completely ignore the XML request and simply send them
         * everything we know about.  Let the client sort it out.
         */
-       wprintf("HTTP/1.0 207 Multi-Status\n");
+       wprintf("HTTP/1.0 207 Multi-Status\r\n");
        groupdav_common_headers();
-       wprintf("Content-type: text/xml\n"
-               "\n"
-               "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
-               "<D:multistatus xmlns:D=\"DAV:\">\n"
+       wprintf("Date: %s\r\n", datestring);
+       wprintf("Content-type: text/xml\r\n");
+       wprintf("Content-encoding: identity\r\n");
+
+       begin_burst();
+
+       wprintf("<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+               "<D:multistatus xmlns:D=\"DAV:\">"
        );
 
        serv_puts("LKRA");
        serv_gets(buf);
        if (buf[0] == '1') while (serv_gets(buf), strcmp(buf, "000")) {
 
-               extract(roomname, buf, 0);
+               extract_token(roomname, buf, 0, '|', sizeof roomname);
                view = extract_int(buf, 6);
-               if ((view == VIEW_CALENDAR) || (view == VIEW_TASKS) || (view == VIEW_ADDRESSBOOK) ) {
-
-                       wprintf(" <D:response>\n");
-                       wprintf("   <D:href>http://splorph.xand.com/groupdav/");
-                       urlescputs(                                     roomname);
-                       wprintf(                                                "/</D:href>\n");
-                       wprintf("   <D:propstat>\n");
-                       wprintf("     <D:status>HTTP/1.1 200 OK</D:status>\n");
-                       wprintf("     <D:prop>\n");
-                       wprintf("      <D:displayname>");
-                       urlescputs(                     roomname);
-                       wprintf(                                "</D:displayname>\n");
-                       wprintf("      <resourcetype xmlns=\"DAV:\" xmlns:G=\"http://groupdav.org/\">\n");
-                       wprintf("        <collection />\n");
+
+               /*
+                * For now, only list rooms that we know a GroupDAV client
+                * might be interested in.  In the future we may add
+                * the rest.
+                */
+               if ((view == VIEW_CALENDAR)
+                  || (view == VIEW_TASKS)
+                  || (view == VIEW_ADDRESSBOOK) ) {
+
+                       wprintf("<D:response>");
+
+                       wprintf("<D:href>");
+                       if (strlen(WC->http_host) > 0) {
+                               wprintf("%s://%s",
+                                       (is_https ? "https" : "http"),
+                                       WC->http_host);
+                       }
+                       wprintf("/groupdav/");
+                       urlescputs(roomname);
+                       wprintf("/</D:href>");
+
+                       wprintf("<D:propstat>");
+                       wprintf("<D:status>HTTP/1.1 200 OK</D:status>");
+                       wprintf("<D:prop>");
+                       wprintf("<D:displayname>");
+                       escputs(roomname);
+                       wprintf("</D:displayname>");
+                       wprintf("<D:resourcetype><D:collection/>");
+
                        switch(view) {
                                case VIEW_CALENDAR:
-                                       wprintf("        <G:vevent-collection />\n");
+                                       wprintf("<G:vevent-collection />");
                                        break;
                                case VIEW_TASKS:
-                                       wprintf("        <G:vtodo-collection />\n");
+                                       wprintf("<G:vtodo-collection />");
                                        break;
                                case VIEW_ADDRESSBOOK:
-                                       wprintf("        <G:vcard-collection />\n");
+                                       wprintf("<G:vcard-collection />");
                                        break;
                        }
-                       wprintf("      <resourcetype>\n");
-                       wprintf("     </D:prop>\n");
-                       wprintf("   </D:propstat>\n");
-                       wprintf(" </D:response>\n");
+
+                       wprintf("</D:resourcetype>");
+                       wprintf("</D:prop>");
+                       wprintf("</D:propstat>");
+                       wprintf("</D:response>");
                }
        }
-
        wprintf("</D:multistatus>\n");
 
+       end_burst();
 }
 
 
@@ -136,9 +163,16 @@ void groupdav_propfind(char *dav_pathname) {
        char msgnum[SIZ];
        char buf[SIZ];
        char uid[SIZ];
+       char encoded_uid[SIZ];
        long *msgs = NULL;
-       int num_msgs;
-       int i, j;
+       int num_msgs = 0;
+       int i;
+       char datestring[SIZ];
+       time_t now;
+
+       now = time(NULL);
+       http_datestring(datestring, sizeof datestring, now);
+
 
        /* First, break off the "/groupdav/" prefix */
        remove_token(dav_pathname, 0, '/');
@@ -165,12 +199,13 @@ void groupdav_propfind(char *dav_pathname) {
                gotoroom(dav_roomname);
        }
        if (strcasecmp(WC->wc_roomname, dav_roomname)) {
-               wprintf("HTTP/1.1 404 not found\n");
+               wprintf("HTTP/1.1 404 not found\r\n");
                groupdav_common_headers();
+               wprintf("Date: %s\r\n", datestring);
                wprintf(
-                       "Content-Type: text/plain\n"
-                       "\n"
-                       "There is no folder called \"%s\" on this server.\n",
+                       "Content-Type: text/plain\r\n"
+                       "\r\n"
+                       "There is no folder called \"%s\" on this server.\r\n",
                        dav_roomname
                );
                return;
@@ -181,12 +216,16 @@ void groupdav_propfind(char *dav_pathname) {
         * everything we know about (which is going to simply be the ETag and
         * nothing else).  Let the client-side parser sort it out.
         */
-       wprintf("HTTP/1.0 207 Multi-Status\n");
+       wprintf("HTTP/1.0 207 Multi-Status\r\n");
        groupdav_common_headers();
-       wprintf("Content-type: text/xml\n"
-               "\n"
-               "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
-               "<D:multistatus xmlns:D=\"DAV:\">\n"
+       wprintf("Date: %s\r\n", datestring);
+       wprintf("Content-type: text/xml\r\n");
+       wprintf("Content-encoding: identity\r\n");
+
+       begin_burst();
+
+       wprintf("<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+               "<D:multistatus xmlns:D=\"DAV:\">"
        );
 
        serv_puts("MSGS ALL");
@@ -208,8 +247,8 @@ void groupdav_propfind(char *dav_pathname) {
                }
 
                if (strlen(uid) > 0) {
-                       wprintf(" <D:response>\n");
-                       wprintf("  <D:href>");
+                       wprintf("<D:response>");
+                       wprintf("<D:href>");
                        if (strlen(WC->http_host) > 0) {
                                wprintf("%s://%s",
                                        (is_https ? "https" : "http"),
@@ -217,20 +256,20 @@ void groupdav_propfind(char *dav_pathname) {
                        }
                        wprintf("/groupdav/");
                        urlescputs(WC->wc_roomname);
-                       wprintf("/");
-                       for (j=0; j<strlen(uid); ++j) {
-                               wprintf("%02X", uid[j]);
-                       }
-                       wprintf("</D:href>\n");
-                       wprintf("   <D:propstat>\n");
-                       wprintf("    <D:status>HTTP/1.1 200 OK</D:status>\n");
-                       wprintf("    <D:prop><D:getetag>\"%ld\"</D:getetag></D:prop>\n", msgs[i]);
-                       wprintf("   </D:propstat>\n");
-                       wprintf(" </D:response>\n");
+                       euid_escapize(encoded_uid, uid);
+                       wprintf("/%s", encoded_uid);
+                       wprintf("</D:href>");
+                       wprintf("<D:propstat>");
+                       wprintf("<D:status>HTTP/1.1 200 OK</D:status>");
+                       wprintf("<D:prop><D:getetag>\"%ld\"</D:getetag></D:prop>", msgs[i]);
+                       wprintf("</D:propstat>");
+                       wprintf("</D:response>");
                }
        }
 
        wprintf("</D:multistatus>\n");
+       end_burst();
+
        if (msgs != NULL) {
                free(msgs);
        }