]> code.citadel.org Git - citadel.git/blobdiff - webcit-ng/messages.c
Don't use \r\n with ctdl_printf() because it adds a \n on its own. Only add \r when...
[citadel.git] / webcit-ng / messages.c
index 342ba4f7a1bac06edbbbae376c8ce72de800cfe1..ed88c9970ed2d1764669059347ab5a7e4e153f5d 100644 (file)
@@ -1,7 +1,7 @@
 //
 // Message base functions
 //
-// Copyright (c) 1996-2018 by the citadel.org team
+// Copyright (c) 1996-2021 by the citadel.org team
 //
 // This program is open source software.  It runs great on the
 // Linux operating system (and probably elsewhere).  You can use,
 #include "webcit.h"
 
 
-/*
- * Given an encoded UID, translate that to an unencoded Citadel EUID and
- * then search for it in the current room.  Return a message number or -1
- * if not found.
- *
- */
-long locate_message_by_uid(struct ctdlsession *c, char *uid)
-{
+// Given an encoded UID, translate that to an unencoded Citadel EUID and
+// then search for it in the current room.  Return a message number or -1
+// if not found.
+long locate_message_by_uid(struct ctdlsession *c, char *uid) {
        char buf[1024];
 
        ctdl_printf(c, "EUID %s", uid);
@@ -33,7 +29,7 @@ long locate_message_by_uid(struct ctdlsession *c, char *uid)
 
        }
 
-       /* Ugly hack to handle Mozilla Thunderbird, try stripping ".ics" if present */
+       // Ugly hack to handle Mozilla Thunderbird, try stripping ".ics" if present
        if (!strcasecmp(&uid[strlen(uid) - 4], ".ics")) {
                safestrncpy(buf, uid, sizeof buf);
                buf[strlen(buf) - 4] = 0;
@@ -49,22 +45,16 @@ long locate_message_by_uid(struct ctdlsession *c, char *uid)
 }
 
 
-/*
- * DAV delete an object in a room.
- */
-void dav_delete_message(struct http_transaction *h, struct ctdlsession *c, long msgnum)
-{
+// DAV delete an object in a room.
+void dav_delete_message(struct http_transaction *h, struct ctdlsession *c, long msgnum) {
        ctdl_delete_msgs(c, &msgnum, 1);
        h->response_code = 204;
        h->response_string = strdup("no content");
 }
 
 
-/*
- * GET method directly on a message in a room
- */
-void dav_get_message(struct http_transaction *h, struct ctdlsession *c, long msgnum)
-{
+// GET method directly on a message in a room
+void dav_get_message(struct http_transaction *h, struct ctdlsession *c, long msgnum) {
        char buf[1024];
        int in_body = 0;
        int encoding = 0;
@@ -87,7 +77,8 @@ void dav_get_message(struct http_transaction *h, struct ctdlsession *c, long msg
                if (IsEmptyStr(buf) && (in_body == 0)) {
                        in_body = 1;
                        Body = NewStrBuf();
-               } else if (in_body == 0) {
+               }
+               else if (in_body == 0) {
                        char *k = buf;
                        char *v = strchr(buf, ':');
                        if (v) {
@@ -96,17 +87,20 @@ void dav_get_message(struct http_transaction *h, struct ctdlsession *c, long msg
                                striplt(v);     // we now have a key (k) and a value (v)
                                if ((!strcasecmp(k, "content-type"))    // fields which can be passed from RFC822 to HTTP as-is
                                    || (!strcasecmp(k, "date"))
-                                   ) {
+                               ) {
                                        add_response_header(h, strdup(k), strdup(v));
-                               } else if (!strcasecmp(k, "content-transfer-encoding")) {
+                               }
+                               else if (!strcasecmp(k, "content-transfer-encoding")) {
                                        if (!strcasecmp(v, "base64")) {
                                                encoding = 'b';
-                                       } else if (!strcasecmp(v, "quoted-printable")) {
+                                       }
+                                       else if (!strcasecmp(v, "quoted-printable")) {
                                                encoding = 'q';
                                        }
                                }
                        }
-               } else if ((in_body == 1) && (Body != NULL)) {
+               }
+               else if ((in_body == 1) && (Body != NULL)) {
                        StrBufAppendPrintf(Body, "%s\n", buf);
                }
        }
@@ -122,13 +116,15 @@ void dav_get_message(struct http_transaction *h, struct ctdlsession *c, long msg
                                    CtdlDecodeQuotedPrintable(h->response_body, (char *) ChrPtr(Body), StrLength(Body));
                        }
                        FreeStrBuf(&Body);
-               } else if (encoding == 'b') {
+               }
+               else if (encoding == 'b') {
                        h->response_body = malloc(StrLength(Body));
                        if (h->response_body != NULL) {
                                h->response_body_length = CtdlDecodeBase64(h->response_body, ChrPtr(Body), StrLength(Body));
                        }
                        FreeStrBuf(&Body);
-               } else {
+               }
+               else {
                        h->response_body_length = StrLength(Body);
                        h->response_body = SmashStrBuf(&Body);
                }
@@ -136,11 +132,8 @@ void dav_get_message(struct http_transaction *h, struct ctdlsession *c, long msg
 }
 
 
-/*
- * PUT a message into a room
- */
-void dav_put_message(struct http_transaction *h, struct ctdlsession *c, char *euid, long old_msgnum)
-{
+// PUT a message into a room
+void dav_put_message(struct http_transaction *h, struct ctdlsession *c, char *euid, long old_msgnum) {
        char buf[1024];
        char *content_type = NULL;
        int n;
@@ -149,7 +142,7 @@ void dav_put_message(struct http_transaction *h, struct ctdlsession *c, char *eu
        char response_string[1024];
 
        if ((h->request_body == NULL) || (h->request_body_length < 1)) {
-               do_404(h);      // Refuse to post a null message
+               do_404(h);                      // Refuse to post a null message
                return;
        }
 
@@ -164,18 +157,17 @@ void dav_put_message(struct http_transaction *h, struct ctdlsession *c, char *eu
                return;
        }
 
+       // Remember, ctdl_printf() appends \n on its own, so we only need \r here.
        content_type = header_val(h, "Content-type");
-       ctdl_printf(c, "Content-type: %s\r\n", (content_type ? content_type : "application/octet-stream"));
-       ctdl_printf(c, "\r\n");
+       ctdl_printf(c, "Content-type: %s\r", (content_type ? content_type : "application/octet-stream"));
+       ctdl_printf(c, "\r");
        ctdl_write(c, h->request_body, h->request_body_length);
        if (h->request_body[h->request_body_length] != '\n') {
-               ctdl_printf(c, "\r\n");
+               ctdl_printf(c, "\r");
        }
        ctdl_printf(c, "000");
 
-       /*
-        * Now handle the response from the Citadel server.
-        */
+       // Now handle the response from the Citadel server.
 
        n = 0;
        new_msgnum = 0;
@@ -198,9 +190,9 @@ void dav_put_message(struct http_transaction *h, struct ctdlsession *c, char *eu
                        break;
                }
 
-       /* Tell the client what happened. */
+       // Tell the client what happened.
 
-       /* Citadel failed in some way? */
+       // Citadel failed in some way?
        char *new_location = malloc(1024);
        if ((new_msgnum < 0L) || (new_location == NULL)) {
                h->response_code = 502;
@@ -227,24 +219,21 @@ void dav_put_message(struct http_transaction *h, struct ctdlsession *c, char *eu
        if (old_msgnum <= 0) {
                h->response_code = 201; // We created this item for the first time.
                h->response_string = strdup("created");
-       } else {
+       }
+       else {
                h->response_code = 204; // We modified an existing item.
                h->response_string = strdup("no content");
 
-               /* The item we replaced has probably already been deleted by
-                * the Citadel server, but we'll do this anyway, just in case.
-                */
+               // The item we replaced has probably already been deleted by
+               // the Citadel server, but we'll do this anyway, just in case.
                ctdl_delete_msgs(c, &old_msgnum, 1);
        }
 
 }
 
 
-/*
- * Download a single component of a MIME-encoded message
- */
-void download_mime_component(struct http_transaction *h, struct ctdlsession *c, long msgnum, char *partnum)
-{
+// Download a single component of a MIME-encoded message
+void download_mime_component(struct http_transaction *h, struct ctdlsession *c, long msgnum, char *partnum) {
        char buf[1024];
        char content_type[1024];