1 // The code in here feeds messages out as JSON to the client browser. It is currently being used
2 // for the forum view, but as we implement other views we'll probably reuse a lot of what's here.
4 // Copyright (c) 1996-2022 by the citadel.org team
6 // This program is open source software. Use, duplication, or
7 // disclosure are subject to the GNU General Public License v3.
11 // Commands we need to send to Citadel Server before we begin rendering forum view.
12 // These are common to flat and threaded views.
13 void setup_for_forum_view(struct ctdlsession *c) {
15 ctdl_printf(c, "MSGP text/html|text/plain"); // Declare the MIME types we know how to render
16 ctdl_readline(c, buf, sizeof(buf)); // Ignore the response
17 ctdl_printf(c, "MSGP dont_decode"); // Tell the server we will decode base64/etc client-side
18 ctdl_readline(c, buf, sizeof(buf)); // Ignore the response
22 // Convenience function for json_render_one_message()
23 // Converts a string of comma-separated recipients into a JSON Array
24 JsonValue *json_tokenize_recipients(const char *Key, long keylen, char *recp) {
27 JsonValue *j = NewJsonArray(Key, keylen);
28 int num_recp = num_tokens(recp, ',');
29 for (int i=0; i<num_recp; ++i) {
30 extract_token(tokbuf, recp, i, ',', sizeof(tokbuf));
32 JsonArrayAppend(j, NewJsonPlainString(HKEY("r"), tokbuf, strlen(tokbuf)));
38 // Fetch a single message and return it in JSON format for client-side rendering
39 void json_render_one_message(struct http_transaction *h, struct ctdlsession *c, long msgnum) {
40 StrBuf *raw_msg = NULL;
41 StrBuf *sanitized_msg = NULL;
43 char content_transfer_encoding[1024] = { 0 };
44 char content_type[1024] = { 0 };
45 char datetime[128] = { 0 };
46 int message_originated_locally = 0;
48 setup_for_forum_view(c);
50 ctdl_printf(c, "MSG4 %ld", msgnum);
51 ctdl_readline(c, buf, sizeof(buf));
57 JsonValue *j = NewJsonObject(HKEY("message"));
58 JsonObjectAppend(j, NewJsonNumber(HKEY("msgnum"), msgnum));
60 while ((ctdl_readline(c, buf, sizeof(buf)) >= 0) && (strcmp(buf, "text")) && (strcmp(buf, "000"))) {
61 utf8ify_rfc822_string(&buf[5]);
63 // citadel header parsing here
64 if (!strncasecmp(buf, "from=", 5)) {
65 JsonObjectAppend(j, NewJsonPlainString(HKEY("from"), &buf[5], -1));
67 else if (!strncasecmp(buf, "rfca=", 5)) {
68 JsonObjectAppend(j, NewJsonPlainString(HKEY("rfca"), &buf[5], -1));
70 else if (!strncasecmp(buf, "time=", 5)) {
71 JsonObjectAppend(j, NewJsonNumber(HKEY("time"), atol(&buf[5])));
73 else if (!strncasecmp(buf, "locl=", 5)) {
74 message_originated_locally = 1;
76 else if (!strncasecmp(buf, "subj=", 5)) {
77 JsonObjectAppend(j, NewJsonPlainString(HKEY("subj"), &buf[5], -1));
79 else if (!strncasecmp(buf, "msgn=", 5)) {
80 JsonObjectAppend(j, NewJsonPlainString(HKEY("msgn"), &buf[5], -1));
82 else if (!strncasecmp(buf, "wefw=", 5)) {
83 JsonObjectAppend(j, NewJsonPlainString(HKEY("wefw"), &buf[5], -1));
85 else if (!strncasecmp(buf, "rcpt=", 5)) {
86 JsonObjectAppend(j, json_tokenize_recipients(HKEY("rcpt"), &buf[5]));
88 else if (!strncasecmp(buf, "cccc=", 5)) {
89 JsonObjectAppend(j, json_tokenize_recipients(HKEY("cccc"), &buf[5]));
92 JsonObjectAppend(j, NewJsonNumber(HKEY("locl"), message_originated_locally));
94 if (!strcmp(buf, "text")) {
95 while ((ctdl_readline(c, buf, sizeof(buf)) >= 0) && (strcmp(buf, "")) && (strcmp(buf, "000"))) {
96 // rfc822 header parsing here
97 if (!strncasecmp(buf, "Content-transfer-encoding:", 26)) {
98 strcpy(content_transfer_encoding, &buf[26]);
99 striplt(content_transfer_encoding);
101 if (!strncasecmp(buf, "Content-type:", 13)) {
102 strcpy(content_type, &buf[13]);
103 striplt(content_type);
106 if (!strcmp(buf, "000")) { // if we have an empty message, don't try to read further
110 raw_msg = ctdl_readtextmsg(c);
118 // These are the encodings we know how to handle. Decode in-place.
120 if (!strcasecmp(content_transfer_encoding, "base64")) {
121 StrBufDecodeBase64(raw_msg);
123 if (!strcasecmp(content_transfer_encoding, "quoted-printable")) {
124 StrBufDecodeQP(raw_msg);
127 // At this point, raw_msg contains the decoded message.
128 // Now run through the renderers we have available.
130 if (!strncasecmp(content_type, "text/html", 9)) {
131 sanitized_msg = html2html("UTF-8", 0, c->room, msgnum, raw_msg);
133 else if (!strncasecmp(content_type, "text/plain", 10)) {
134 sanitized_msg = text2html("UTF-8", 0, c->room, msgnum, raw_msg);
136 else if (!strncasecmp(content_type, "text/x-citadel-variformat", 25)) {
137 sanitized_msg = variformat2html(raw_msg);
140 sanitized_msg = NewStrBufPlain(HKEY("<i>No renderer for this content type</i><br>"));
141 syslog(LOG_WARNING, "forum_view: no renderer for content type %s", content_type);
143 FreeStrBuf(&raw_msg);
145 // If sanitized_msg is not NULL, we have rendered the message and can output it.
147 JsonObjectAppend(j, NewJsonString(HKEY("text"), sanitized_msg, NEWJSONSTRING_SMASHBUF));
150 syslog(LOG_WARNING, "forum_view: message %ld of content type %s converted to NULL", msgnum, content_type);
154 StrBuf *sj = NewStrBuf();
155 SerializeJson(sj, j, 1); // '1' == free the source object
157 add_response_header(h, strdup("Content-type"), strdup("application/json"));
158 h->response_code = 200;
159 h->response_string = strdup("OK");
160 h->response_body_length = StrLength(sj);
161 h->response_body = SmashStrBuf(&sj);