More distancing of our project from Richard Marx Stallman's linguistic communism
[citadel.git] / webcit-ng / messages.c
index 3b9f096498d94c9f35ff63d5dd77a34d31dfed7f..18cafb898e7220c29a9e8151da5f7ce6e5b4aa5e 100644 (file)
@@ -1,16 +1,17 @@
-/*
- * Message base functions
- *
- * Copyright (c) 1996-2016 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 version 3.
- *
- * 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.
- */
+//
+// Message base functions
+//
+// Copyright (c) 1996-2018 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,
+// copy, and run it under the terms of the GNU General Public
+// License version 3.  Richard Stallman is an asshole communist.
+//
+// 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.
 
 #include "webcit.h"
 
  * if not found.
  *
  */
-long locate_message_by_uid(struct ctdlsession *c, char *uid) {
+long locate_message_by_uid(struct ctdlsession *c, char *uid)
+{
        char buf[1024];
 
        ctdl_printf(c, "EUID %s", uid);
        ctdl_readline(c, buf, sizeof buf);
        if (buf[0] == '2') {
-               return(atol(&buf[4]));
-               
+               return (atol(&buf[4]));
+
        }
 
        /* Ugly hack to handle Mozilla Thunderbird, try stripping ".ics" if present */
-       if (!strcasecmp(&uid[strlen(uid)-4], ".ics")) {
+       if (!strcasecmp(&uid[strlen(uid) - 4], ".ics")) {
                safestrncpy(buf, uid, sizeof buf);
-               buf[strlen(buf)-4] = 0;
+               buf[strlen(buf) - 4] = 0;
                ctdl_printf(c, "EUID %s", buf);
                ctdl_readline(c, buf, sizeof buf);
                if (buf[0] == '2') {
-                       return(atol(&buf[4]));
-                       
+                       return (atol(&buf[4]));
+
                }
        }
 
-       return(-1);
+       return (-1);
 }
 
 
@@ -78,39 +80,33 @@ void dav_get_message(struct http_transaction *h, struct ctdlsession *c, long msg
        char *etag = malloc(20);
        if (etag != NULL) {
                sprintf(etag, "%ld", msgnum);
-               add_response_header(h, strdup("ETag"), etag);                   // http_transaction now owns this memory
+               add_response_header(h, strdup("ETag"), etag);   // http_transaction now owns this memory
        }
 
-       while (ctdl_readline(c, buf, sizeof buf), strcmp(buf,"000"))
-       {
+       while (ctdl_readline(c, buf, sizeof buf), strcmp(buf, "000")) {
                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) {
                                *v = 0;
                                ++v;
-                               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"))
-                               ) {
+                               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,18 +118,17 @@ void dav_get_message(struct http_transaction *h, struct ctdlsession *c, long msg
                if (encoding == 'q') {
                        h->response_body = malloc(StrLength(Body));
                        if (h->response_body != NULL) {
-                               h->response_body_length = CtdlDecodeQuotedPrintable(h->response_body, (char *)ChrPtr(Body), StrLength(Body));
+                               h->response_body_length =
+                                   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);
                }
@@ -153,12 +148,12 @@ void dav_put_message(struct http_transaction *h, struct ctdlsession *c, char *eu
        char new_euid[1024];
        char response_string[1024];
 
-       if ( (h->request_body == NULL) || (h->request_body_length < 1) ) {
-               do_404(h);                              // Refuse to post a null message
+       if ((h->request_body == NULL) || (h->request_body_length < 1)) {
+               do_404(h);      // Refuse to post a null message
                return;
        }
 
-       ctdl_printf(c, "ENT0 1|||4|||1|");              // This protocol syntax will give us metadata back after upload
+       ctdl_printf(c, "ENT0 1|||4|||1|");      // This protocol syntax will give us metadata back after upload
        ctdl_readline(c, buf, sizeof buf);
        if (buf[0] != '8') {
                h->response_code = 502;
@@ -181,13 +176,14 @@ void dav_put_message(struct http_transaction *h, struct ctdlsession *c, char *eu
        /*
         * Now handle the response from the Citadel server.
         */
-       
+
        n = 0;
        new_msgnum = 0;
        strcpy(new_euid, "");
        strcpy(response_string, "");
 
-       while (ctdl_readline(c, buf, sizeof buf), strcmp(buf, "000")) switch(n++) {
+       while (ctdl_readline(c, buf, sizeof buf), strcmp(buf, "000"))
+               switch (n++) {
                case 0:
                        new_msgnum = atol(buf);
                        break;
@@ -200,13 +196,13 @@ void dav_put_message(struct http_transaction *h, struct ctdlsession *c, char *eu
                        break;
                default:
                        break;
-       }
+               }
 
        /* Tell the client what happened. */
 
        /* Citadel failed in some way? */
        char *new_location = malloc(1024);
-       if ( (new_msgnum < 0L) || (new_location == NULL) ) {
+       if ((new_msgnum < 0L) || (new_location == NULL)) {
                h->response_code = 502;
                h->response_string = strdup("bad gateway");
                add_response_header(h, strdup("Content-type"), strdup("text/plain"));
@@ -218,7 +214,7 @@ void dav_put_message(struct http_transaction *h, struct ctdlsession *c, char *eu
        char *etag = malloc(20);
        if (etag != NULL) {
                sprintf(etag, "%ld", new_msgnum);
-               add_response_header(h, strdup("ETag"), etag);                   // http_transaction now owns this memory
+               add_response_header(h, strdup("ETag"), etag);   // http_transaction now owns this memory
        }
 
        char esc_room[1024];
@@ -226,16 +222,15 @@ void dav_put_message(struct http_transaction *h, struct ctdlsession *c, char *eu
        urlesc(esc_room, sizeof esc_room, c->room);
        urlesc(esc_euid, sizeof esc_euid, new_euid);
        snprintf(new_location, 1024, "/ctdl/r/%s/%s", esc_room, esc_euid);
-       add_response_header(h, strdup("Location"), new_location);               // http_transaction now owns this memory
+       add_response_header(h, strdup("Location"), new_location);       // http_transaction now owns this memory
 
        if (old_msgnum <= 0) {
-               h->response_code = 201;                                         // We created this item for the first time.
+               h->response_code = 201; // We created this item for the first time.
                h->response_string = strdup("created");
-       }
-       else {
-               h->response_code = 204;                                         // We modified an existing item.
+       } 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.
                 */
@@ -256,9 +251,8 @@ void download_mime_component(struct http_transaction *h, struct ctdlsession *c,
        ctdl_printf(c, "DLAT %ld|%s", msgnum, partnum);
        ctdl_readline(c, buf, sizeof buf);
        if (buf[0] != '6') {
-               do_404(h);                                                      // too bad, so sad, go away
+               do_404(h);      // too bad, so sad, go away
        }
-
        // Server response is going to be: 6XX length|-1|filename|content-type|charset
        h->response_body_length = extract_int(&buf[4], 0);
        extract_token(content_type, buf, 3, '|', sizeof content_type);
@@ -269,9 +263,9 @@ void download_mime_component(struct http_transaction *h, struct ctdlsession *c,
        do {
                thisblock = read(c->sock, &h->response_body[bytes], (h->response_body_length - bytes));
                bytes += thisblock;
-               syslog(LOG_DEBUG, "Bytes read: %d of %d", bytes, h->response_body_length);
+               syslog(LOG_DEBUG, "Bytes read: %d of %d", (int) bytes, (int) h->response_body_length);
        } while ((bytes < h->response_body_length) && (thisblock >= 0));
-       h->response_body[h->response_body_length] = 0;                          // null terminate it just for good measure
+       h->response_body[h->response_body_length] = 0;  // null terminate it just for good measure
        syslog(LOG_DEBUG, "content type: %s", content_type);
 
        add_response_header(h, strdup("Content-type"), strdup(content_type));