Fix lots of warnings all over the place, make inbound buffers of FMOUT const.
[citadel.git] / webcit / groupdav_get.c
index 8ebee42ab84d1cb3f0f1f85aedd657c87453c5e1..b99e7caaf80c008ec1cfefcb7052169ccbabaefd 100644 (file)
@@ -1,14 +1,26 @@
 /*
- * $Id$
- *
  * Handles GroupDAV GET requests.
  *
+ * Copyright (c) 2005-2010 by the citadel.org team
+ *
+ * This program is open source software.  You can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  */
 
 #include "webcit.h"
 #include "webserver.h"
 #include "groupdav.h"
-#include "mime_parser.h"
 
 
 /*
@@ -21,23 +33,23 @@ void groupdav_get_big_ics(void) {
        serv_puts("ICAL getics");
        serv_getln(buf, sizeof buf);
        if (buf[0] != '1') {
-               wprintf("HTTP/1.1 404 not found\r\n");
+               hprintf("HTTP/1.1 404 not found\r\n");
                groupdav_common_headers();
-               wprintf(
-                       "Content-Type: text/plain\r\n"
-                       "\r\n"
-                       "%s\r\n",
+               hprintf("Content-Type: text/plain\r\n");
+               begin_burst();
+               wc_printf("%s\r\n",
                        &buf[4]
-               );
+                       );
+               end_burst();
                return;
        }
 
-       wprintf("HTTP/1.1 200 OK\r\n");
+       hprintf("HTTP/1.1 200 OK\r\n");
        groupdav_common_headers();
-       wprintf("Content-type: text/calendar; charset=UTF-8\r\n");
+       hprintf("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);
+               wc_printf("%s\r\n", buf);
        }
        end_burst();
 }
@@ -49,13 +61,13 @@ void groupdav_get_big_ics(void) {
  */
 void extract_preferred(char *name, char *filename, char *partnum, char *disp,
                        void *content, char *cbtype, char *cbcharset,
-                       size_t length, char *encoding, void *userdata)
+                       size_t length, char *encoding, char *cbid, void *userdata)
 {
        struct epdata *epdata = (struct epdata *)userdata;
        int hit = 0;
 
        /* We only want the first one that we found */
-       if (strlen(epdata->found_section) > 0) return;
+       if (!IsEmptyStr(epdata->found_section)) return;
 
        /* Check for a content type match */
        if (strlen(epdata->desired_content_type_1) > 0) {
@@ -63,7 +75,7 @@ void extract_preferred(char *name, char *filename, char *partnum, char *disp,
                        hit = 1;
                }
        }
-       if (strlen(epdata->desired_content_type_2) > 0) {
+       if (!IsEmptyStr(epdata->desired_content_type_2)) {
                if (!strcasecmp(epdata->desired_content_type_2, cbtype)) {
                        hit = 1;
                }
@@ -72,12 +84,12 @@ void extract_preferred(char *name, char *filename, char *partnum, char *disp,
        /* Is this the one?  If so, output it. */
        if (hit) {
                safestrncpy(epdata->found_section, partnum, sizeof epdata->found_section);
-               if (strlen(cbcharset) > 0) {
+               if (!IsEmptyStr(cbcharset)) {
                        safestrncpy(epdata->charset, cbcharset, sizeof epdata->charset);
                }
-               wprintf("Content-type: %s; charset=%s\r\n", cbtype, epdata->charset);
+               hprintf("Content-type: %s; charset=%s\r\n", cbtype, epdata->charset);
                begin_burst();
-               client_write(content, length);
+               StrBufAppendBufPlain(WC->WBuf, content, length, 0);
                end_burst();
        }
 }
@@ -89,13 +101,14 @@ void extract_preferred(char *name, char *filename, char *partnum, char *disp,
  * /groupdav/room_name/euid    (GroupDAV)
  * /groupdav/room_name         (webcal)
  */
-void groupdav_get(char *dav_pathname) {
-       char dav_roomname[1024];
-       char dav_uid[1024];
+void groupdav_get(void)
+{
+       wcsession *WCC = WC;
+       StrBuf *dav_roomname;
+       StrBuf *dav_uid;
        long dav_msgnum = (-1);
        char buf[1024];
        int in_body = 0;
-       int found_content_type = 0;
        char *ptr;
        char *endptr;
        char *msgtext = NULL;
@@ -107,66 +120,70 @@ void groupdav_get(char *dav_pathname) {
        char date[128];
        struct epdata epdata;
 
-       if (num_tokens(dav_pathname, '/') < 3) {
-               wprintf("HTTP/1.1 404 not found\r\n");
+       if (StrBufNum_tokens(WCC->Hdr->HR.ReqLine, '/') < 2) {
+               hprintf("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"
-               );
+               hprintf("Content-Type: text/plain\r\n");
+               wc_printf("The object you requested was not found.\r\n");
+               end_burst();
                return;
        }
 
-       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, "");
+       dav_roomname = NewStrBuf();;
+       dav_uid = NewStrBuf();;
+       StrBufExtract_token(dav_roomname, WCC->Hdr->HR.ReqLine, 0, '/');
+       StrBufExtract_token(dav_uid, WCC->Hdr->HR.ReqLine, 1, '/');
+       if ((!strcasecmp(ChrPtr(dav_uid), "ics")) || 
+           (!strcasecmp(ChrPtr(dav_uid), "calendar.ics"))) {
+               FlushStrBuf(dav_uid);
        }
 
        /* Go to the correct room. */
-       if (strcasecmp(WC->wc_roomname, dav_roomname)) {
+       if (strcasecmp(ChrPtr(WCC->CurRoom.name), ChrPtr(dav_roomname))) {
                gotoroom(dav_roomname);
        }
-       if (strcasecmp(WC->wc_roomname, dav_roomname)) {
-               wprintf("HTTP/1.1 404 not found\r\n");
+       if (strcasecmp(ChrPtr(WCC->CurRoom.name), ChrPtr(dav_roomname))) {
+               hprintf("HTTP/1.1 404 not found\r\n");
                groupdav_common_headers();
-               wprintf(
-                       "Content-Type: text/plain\r\n"
-                       "\r\n"
-                       "There is no folder called \"%s\" on this server.\r\n",
-                       dav_roomname
-               );
+               hprintf("Content-Type: text/plain\r\n");
+               wc_printf("There is no folder called \"%s\" on this server.\r\n",
+                       ChrPtr(dav_roomname));
+               end_burst();
+               FreeStrBuf(&dav_roomname);
+               FreeStrBuf(&dav_uid);
                return;
        }
 
        /** GET on the collection itself returns an ICS of the entire collection.
         */
-       if (!strcasecmp(dav_uid, "")) {
+       if (StrLength(dav_uid) == 0) {
                groupdav_get_big_ics();
+               FreeStrBuf(&dav_roomname);
+               FreeStrBuf(&dav_uid);
                return;
        }
 
-       dav_msgnum = locate_message_by_uid(dav_uid);
+       dav_msgnum = locate_message_by_uid(ChrPtr(dav_uid));
        serv_printf("MSG2 %ld", dav_msgnum);
        serv_getln(buf, sizeof buf);
        if (buf[0] != '1') {
-               wprintf("HTTP/1.1 404 not found\r\n");
+               hprintf("HTTP/1.1 404 not found\r\n");
                groupdav_common_headers();
-               wprintf(
-                       "Content-Type: text/plain\r\n"
-                       "\r\n"
-                       "Object \"%s\" was not found in the \"%s\" folder.\r\n",
-                       dav_uid,
-                       dav_roomname
-               );
+               hprintf("Content-Type: text/plain\r\n");
+               wc_printf("Object \"%s\" was not found in the \"%s\" folder.\r\n",
+                       ChrPtr(dav_uid),
+                       ChrPtr(dav_roomname));
+               end_burst();
+               FreeStrBuf(&dav_roomname);
+               FreeStrBuf(&dav_uid);
                return;
        }
+       FreeStrBuf(&dav_roomname);
+       FreeStrBuf(&dav_uid);
 
        /* We got it; a message is now arriving from the server.  Read it in. */
 
        in_body = 0;
-       found_content_type = 0;
        strcpy(charset, "UTF-8");
        strcpy(content_type, "text/plain");
        strcpy(date, "");
@@ -211,10 +228,10 @@ void groupdav_get(char *dav_pathname) {
 
        /* Output headers common to single or multi part messages */
 
-       wprintf("HTTP/1.1 200 OK\r\n");
+       hprintf("HTTP/1.1 200 OK\r\n");
        groupdav_common_headers();
-       wprintf("etag: \"%ld\"\r\n", dav_msgnum);
-       wprintf("Date: %s\r\n", date);
+       hprintf("etag: \"%ld\"\r\n", dav_msgnum);
+       hprintf("Date: %s\r\n", date);
 
        memset(&epdata, 0, sizeof(struct epdata));
        safestrncpy(epdata.charset, charset, sizeof epdata.charset);
@@ -224,11 +241,11 @@ void groupdav_get(char *dav_pathname) {
         */
        if (!strncasecmp(content_type, "multipart/", 10)) {
 
-               if ( (WC->wc_default_view == VIEW_CALENDAR) || (WC->wc_default_view == VIEW_TASKS) ) {
+               if ( (WCC->CurRoom.defview == VIEW_CALENDAR) || (WCC->CurRoom.defview == VIEW_TASKS) ) {
                        strcpy(epdata.desired_content_type_1, "text/calendar");
                }
 
-               else if (WC->wc_default_view == VIEW_ADDRESSBOOK) {
+               else if (WCC->CurRoom.defview == VIEW_ADDRESSBOOK) {
                        strcpy(epdata.desired_content_type_1, "text/vcard");
                        strcpy(epdata.desired_content_type_2, "text/x-vcard");
                }
@@ -238,18 +255,18 @@ void groupdav_get(char *dav_pathname) {
 
        /* If epdata.found_section is empty, we haven't output anything yet, so output the whole thing */
 
-       if (strlen(epdata.found_section) == 0) {
+       if (IsEmptyStr(epdata.found_section)) {
                ptr = msgtext;
                endptr = &msgtext[msglen];
        
-               wprintf("Content-type: %s; charset=%s\r\n", content_type, charset);
+               hprintf("Content-type: %s; charset=%s\r\n", content_type, charset);
        
                in_body = 0;
                do {
                        ptr = memreadline(ptr, buf, sizeof buf);
        
                        if (in_body) {
-                               wprintf("%s\r\n", buf);
+                               wc_printf("%s\r\n", buf);
                        }
                        else if ((buf[0] == 0) && (in_body == 0)) {
                                in_body = 1;