"rcpt" and "cccc" fields are now delivered as json arrays
authorArt Cancro <ajc@citadel.org>
Thu, 3 Nov 2022 00:08:54 +0000 (20:08 -0400)
committerArt Cancro <ajc@citadel.org>
Thu, 3 Nov 2022 00:08:54 +0000 (20:08 -0400)
libcitadel/lib/json.c
webcit-ng/server/forum_view.c
webcit-ng/static/js/view_mail.js

index 0b3a7d16ff64e69b9c3bf78cd926ce7da9f093d2..ff649f75a07d6754c5a7c75830e7c8b6bebbff46 100644 (file)
@@ -1,11 +1,10 @@
-/*
- * JSON data type and serializer for Citadel
- *
- * Copyright (c) 1987-2018 by the citadel.org team
- *
+//
+// JSON data type and serializer for Citadel
+//
+// Copyright (c) 1987-2018 by the citadel.org team
+//
 // This program is open source software.  Use, duplication, or disclosure
 // is subject to the terms of the GNU General Public License, version 3.
- */
 
 #include "sysdep.h"
 #include <sys/types.h>
@@ -25,8 +24,7 @@
 #define JSON_ARRAY 4
 #define JSON_OBJECT 7
 
-struct JsonValue
-{
+struct JsonValue {
        int Type;
        StrBuf *Name;
        StrBuf *Value;
@@ -34,8 +32,7 @@ struct JsonValue
 };
 
 
-void DeleteJSONValue(void *vJsonValue)
-{
+void DeleteJSONValue(void *vJsonValue) {
        JsonValue *Val = (JsonValue*) vJsonValue;
        FreeStrBuf(&Val->Name);
        FreeStrBuf(&Val->Value);
@@ -44,15 +41,13 @@ void DeleteJSONValue(void *vJsonValue)
 }
 
 
-JsonValue *NewJsonObject(const char *Key, long keylen)
-{
+JsonValue *NewJsonObject(const char *Key, long keylen) {
        JsonValue *Ret;
 
        Ret = (JsonValue*) malloc(sizeof(JsonValue));
        memset(Ret, 0, sizeof(JsonValue));
        Ret->Type = JSON_OBJECT;
-       if (Key != NULL)
-       {
+       if (Key != NULL) {
                Ret->Name = NewStrBufPlain(Key, keylen);
        }
        Ret->SubValues = NewHash(1, NULL);
@@ -60,15 +55,13 @@ JsonValue *NewJsonObject(const char *Key, long keylen)
 }
 
 
-JsonValue *NewJsonArray(const char *Key, long keylen)
-{
+JsonValue *NewJsonArray(const char *Key, long keylen) {
        JsonValue *Ret;
 
        Ret = (JsonValue*) malloc(sizeof(JsonValue));
        memset(Ret, 0, sizeof(JsonValue));
        Ret->Type = JSON_ARRAY;
-       if (Key != NULL)
-       {
+       if (Key != NULL) {
                Ret->Name = NewStrBufPlain(Key, keylen);
        }
        Ret->SubValues = NewHash(1, lFlathash);
@@ -76,15 +69,13 @@ JsonValue *NewJsonArray(const char *Key, long keylen)
 }
 
 
-JsonValue *NewJsonNumber(const char *Key, long keylen, long Number)
-{
+JsonValue *NewJsonNumber(const char *Key, long keylen, long Number) {
        JsonValue *Ret;
 
        Ret = (JsonValue*) malloc(sizeof(JsonValue));
        memset(Ret, 0, sizeof(JsonValue));
        Ret->Type = JSON_NUM;
-       if (Key != NULL)
-       {
+       if (Key != NULL) {
                Ret->Name = NewStrBufPlain(Key, keylen);
        }
        Ret->Value = NewStrBufPlain(NULL, 64);
@@ -93,15 +84,13 @@ JsonValue *NewJsonNumber(const char *Key, long keylen, long Number)
 }
 
 
-JsonValue *NewJsonBigNumber(const char *Key, long keylen, double Number)
-{
+JsonValue *NewJsonBigNumber(const char *Key, long keylen, double Number) {
        JsonValue *Ret;
 
        Ret = (JsonValue*) malloc(sizeof(JsonValue));
        memset(Ret, 0, sizeof(JsonValue));
        Ret->Type = JSON_NUM;
-       if (Key != NULL)
-       {
+       if (Key != NULL) {
                Ret->Name = NewStrBufPlain(Key, keylen);
        }
        Ret->Value = NewStrBufPlain(NULL, 128);
@@ -110,42 +99,35 @@ JsonValue *NewJsonBigNumber(const char *Key, long keylen, double Number)
 }
 
 
-JsonValue *NewJsonString(const char *Key, long keylen, StrBuf *CopyMe, int copy_or_smash)
-{
+JsonValue *NewJsonString(const char *Key, long keylen, StrBuf *CopyMe, int copy_or_smash) {
        JsonValue *Ret;
 
        Ret = (JsonValue*) malloc(sizeof(JsonValue));
        memset(Ret, 0, sizeof(JsonValue));
        Ret->Type = JSON_STRING;
-       if (Key != NULL)
-       {
+       if (Key != NULL) {
                Ret->Name = NewStrBufPlain(Key, keylen);
        }
-       if (copy_or_smash == NEWJSONSTRING_COPYBUF)
-       {
+       if (copy_or_smash == NEWJSONSTRING_COPYBUF) {
                Ret->Value = NewStrBufDup(CopyMe);
        }
-       else if (copy_or_smash == NEWJSONSTRING_SMASHBUF)
-       {
+       else if (copy_or_smash == NEWJSONSTRING_SMASHBUF) {
                Ret->Value = CopyMe;
        }
-       else
-       {
+       else {
                Ret->Value = NULL;              // error condition
        }
        return Ret;
 }
 
 
-JsonValue *NewJsonPlainString(const char *Key, long keylen, const char *CopyMe, long len)
-{
+JsonValue *NewJsonPlainString(const char *Key, long keylen, const char *CopyMe, long len) {
        JsonValue *Ret;
 
        Ret = (JsonValue*) malloc(sizeof(JsonValue));
        memset(Ret, 0, sizeof(JsonValue));
        Ret->Type = JSON_STRING;
-       if (Key != NULL)
-       {
+       if (Key != NULL) {
                Ret->Name = NewStrBufPlain(Key, keylen);
        }
        Ret->Value = NewStrBufPlain(CopyMe, len);
@@ -153,8 +135,7 @@ JsonValue *NewJsonPlainString(const char *Key, long keylen, const char *CopyMe,
 }
 
 
-JsonValue *NewJsonNull(const char *Key, long keylen)
-{
+JsonValue *NewJsonNull(const char *Key, long keylen) {
        JsonValue *Ret;
 
        Ret = (JsonValue*) malloc(sizeof(JsonValue));
@@ -169,33 +150,28 @@ JsonValue *NewJsonNull(const char *Key, long keylen)
 }
 
 
-JsonValue *NewJsonBool(const char *Key, long keylen, int value)
-{
+JsonValue *NewJsonBool(const char *Key, long keylen, int value) {
        JsonValue *Ret;
 
        Ret = (JsonValue*) malloc(sizeof(JsonValue));
        memset(Ret, 0, sizeof(JsonValue));
        Ret->Type = JSON_BOOL;
-       if (Key != NULL)
-       {
+       if (Key != NULL) {
                Ret->Name = NewStrBufPlain(Key, keylen);
        }
        if (value) {
                Ret->Value = NewStrBufPlain(HKEY("true"));
        }
-       else
-       {
+       else {
                Ret->Value = NewStrBufPlain(HKEY("false"));
        }
        return Ret;
 }
 
 
-void JsonArrayAppend(JsonValue *Array, JsonValue *Val)
-{
+void JsonArrayAppend(JsonValue *Array, JsonValue *Val) {
        long n;
-       if (Array->Type != JSON_ARRAY)
-       {
+       if (Array->Type != JSON_ARRAY) {
                return;
        }
 
@@ -204,18 +180,15 @@ void JsonArrayAppend(JsonValue *Array, JsonValue *Val)
 }
 
 
-void JsonObjectAppend(JsonValue *Array, JsonValue *Val)
-{
-       if ((Array->Type != JSON_OBJECT) || (Val->Name == NULL))
-       {
+void JsonObjectAppend(JsonValue *Array, JsonValue *Val) {
+       if ((Array->Type != JSON_OBJECT) || (Val->Name == NULL)) {
                return;
        }
        Put(Array->SubValues, SKEY(Val->Name), Val, DeleteJSONValue);
 }
 
 
-void SerializeJson(StrBuf *Target, JsonValue *Val, int FreeVal)
-{
+void SerializeJson(StrBuf *Target, JsonValue *Val, int FreeVal) {
        void *vValue, *vPrevious;
        JsonValue *SubVal;
        HashPos *It;
@@ -243,10 +216,8 @@ void SerializeJson(StrBuf *Target, JsonValue *Val, int FreeVal)
                vPrevious = NULL;
                StrBufAppendBufPlain(Target, HKEY("["), 0);
                It = GetNewHashPos(Val->SubValues, 0);
-               while (GetNextHashPos(Val->SubValues, It, &keylen, &Key, &vValue))
-               {
-                       if (vPrevious != NULL) 
-                       {
+               while (GetNextHashPos(Val->SubValues, It, &keylen, &Key, &vValue)) {
+                       if (vPrevious != NULL) {
                                StrBufAppendBufPlain(Target, HKEY(","), 0);
                        }
                        SubVal = (JsonValue*) vValue;
@@ -260,12 +231,10 @@ void SerializeJson(StrBuf *Target, JsonValue *Val, int FreeVal)
                vPrevious = NULL;
                StrBufAppendBufPlain(Target, HKEY("{"), 0);
                It = GetNewHashPos(Val->SubValues, 0);
-               while (GetNextHashPos(Val->SubValues, It, &keylen, &Key, &vValue))
-               {
+               while (GetNextHashPos(Val->SubValues, It, &keylen, &Key, &vValue)) {
                        SubVal = (JsonValue*) vValue;
 
-                       if (vPrevious != NULL)
-                       {
+                       if (vPrevious != NULL) {
                                StrBufAppendBufPlain(Target, HKEY(","), 0);
                        }
                        StrBufAppendBufPlain(Target, HKEY("\""), 0);
@@ -279,8 +248,7 @@ void SerializeJson(StrBuf *Target, JsonValue *Val, int FreeVal)
                DeleteHashPos(&It);
                break;
        }
-       if (FreeVal)
-       {
+       if (FreeVal) {
                DeleteJSONValue(Val);
        }
 }
index 6d3feaffaa47bf858f0384b5d2139cf09685336f..9fccab60f104b8435bf8c46f8971edea486c63bd 100644 (file)
@@ -19,6 +19,22 @@ void setup_for_forum_view(struct ctdlsession *c) {
 }
 
 
+// Convenience function for json_render_one_message()
+// Converts a string of comma-separated recipients into a JSON Array
+JsonValue *json_tokenize_recipients(const char *Key, long keylen, char *recp) {
+       char tokbuf[1024];
+
+       JsonValue *j = NewJsonArray(Key, keylen);
+       int num_recp = num_tokens(recp, ',');
+       for (int i=0; i<num_recp; ++i) {
+               extract_token(tokbuf, recp, i, ',', sizeof(tokbuf));
+               striplt(tokbuf);
+               JsonArrayAppend(j, NewJsonPlainString(HKEY("r"), tokbuf, strlen(tokbuf)));
+       }
+       return(j);
+}
+
+
 // Fetch a single message and return it in JSON format for client-side rendering
 void json_render_one_message(struct http_transaction *h, struct ctdlsession *c, long msgnum) {
        StrBuf *raw_msg = NULL;
@@ -66,8 +82,13 @@ void json_render_one_message(struct http_transaction *h, struct ctdlsession *c,
                else if (!strncasecmp(buf, "wefw=", 5)) {
                        JsonObjectAppend(j, NewJsonPlainString(HKEY("wefw"), &buf[5], -1));
                }
+               else if (!strncasecmp(buf, "rcpt=", 5)) {
+                       JsonObjectAppend(j, json_tokenize_recipients(HKEY("rcpt"), &buf[5]));
+               }
+               else if (!strncasecmp(buf, "cccc=", 5)) {
+                       JsonObjectAppend(j, json_tokenize_recipients(HKEY("cccc"), &buf[5]));
+               }
        }
-
        JsonObjectAppend(j, NewJsonNumber(HKEY("locl"), message_originated_locally));
 
        if (!strcmp(buf, "text")) {
index e1acae54f8dc4ec97afe89ba10c97730da1ff9a7..595408c1e1d5b0648e951ff428cd321b2f1b0ca3 100644 (file)
@@ -66,6 +66,7 @@ function mail_render_one(msgnum, msg, target_div, include_controls) {
                
                        outmsg +=
                          "</span>";                                            // end buttons on right side
+                       outmsg += "<br><span>To: lorem ipsum &lt;dolor@sit.amet&gt;</span>"
                        if (msg.subj) {
                                outmsg +=
                                "<br><span class=\"ctdl-msgsubject\">" + msg.subj + "</span>";