]> code.citadel.org Git - citadel.git/blob - webcit-ng/server/forum_view.c
6d3feaffaa47bf858f0384b5d2139cf09685336f
[citadel.git] / webcit-ng / server / forum_view.c
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.
3 //
4 // Copyright (c) 1996-2022 by the citadel.org team
5 //
6 // This program is open source software.  Use, duplication, or
7 // disclosure are subject to the GNU General Public License v3.
8
9 #include "webcit.h"
10
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) {
14         char buf[1024];
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
19 }
20
21
22 // Fetch a single message and return it in JSON format for client-side rendering
23 void json_render_one_message(struct http_transaction *h, struct ctdlsession *c, long msgnum) {
24         StrBuf *raw_msg = NULL;
25         StrBuf *sanitized_msg = NULL;
26         char buf[1024];
27         char content_transfer_encoding[1024] = { 0 };
28         char content_type[1024] = { 0 };
29         char datetime[128] = { 0 };
30         int message_originated_locally = 0;
31
32         setup_for_forum_view(c);
33
34         ctdl_printf(c, "MSG4 %ld", msgnum);
35         ctdl_readline(c, buf, sizeof(buf));
36         if (buf[0] != '1') {
37                 do_404(h);
38                 return;
39         }
40
41         JsonValue *j = NewJsonObject(HKEY("message"));
42         JsonObjectAppend(j, NewJsonNumber(HKEY("msgnum"), msgnum));
43
44         while ((ctdl_readline(c, buf, sizeof(buf)) >= 0) && (strcmp(buf, "text")) && (strcmp(buf, "000"))) {
45                 utf8ify_rfc822_string(&buf[5]);
46
47                 // citadel header parsing here
48                 if (!strncasecmp(buf, "from=", 5)) {
49                         JsonObjectAppend(j, NewJsonPlainString(HKEY("from"), &buf[5], -1));
50                 }
51                 else if (!strncasecmp(buf, "rfca=", 5)) {
52                         JsonObjectAppend(j, NewJsonPlainString(HKEY("rfca"), &buf[5], -1));
53                 }
54                 else if (!strncasecmp(buf, "time=", 5)) {
55                         JsonObjectAppend(j, NewJsonNumber(HKEY("time"), atol(&buf[5])));
56                 }
57                 else if (!strncasecmp(buf, "locl=", 5)) {
58                         message_originated_locally = 1;
59                 }
60                 else if (!strncasecmp(buf, "subj=", 5)) {
61                         JsonObjectAppend(j, NewJsonPlainString(HKEY("subj"), &buf[5], -1));
62                 }
63                 else if (!strncasecmp(buf, "msgn=", 5)) {
64                         JsonObjectAppend(j, NewJsonPlainString(HKEY("msgn"), &buf[5], -1));
65                 }
66                 else if (!strncasecmp(buf, "wefw=", 5)) {
67                         JsonObjectAppend(j, NewJsonPlainString(HKEY("wefw"), &buf[5], -1));
68                 }
69         }
70
71         JsonObjectAppend(j, NewJsonNumber(HKEY("locl"), message_originated_locally));
72
73         if (!strcmp(buf, "text")) {
74                 while ((ctdl_readline(c, buf, sizeof(buf)) >= 0) && (strcmp(buf, "")) && (strcmp(buf, "000"))) {
75                         // rfc822 header parsing here
76                         if (!strncasecmp(buf, "Content-transfer-encoding:", 26)) {
77                                 strcpy(content_transfer_encoding, &buf[26]);
78                                 striplt(content_transfer_encoding);
79                         }
80                         if (!strncasecmp(buf, "Content-type:", 13)) {
81                                 strcpy(content_type, &buf[13]);
82                                 striplt(content_type);
83                         }
84                 }
85                 if (!strcmp(buf, "000")) {              // if we have an empty message, don't try to read further
86                         raw_msg = NULL;
87                 }
88                 else {
89                         raw_msg = ctdl_readtextmsg(c);
90                 }
91         }
92         else {
93                 raw_msg = NULL;
94         }
95
96         if (raw_msg) {
97                 // These are the encodings we know how to handle.  Decode in-place.
98
99                 if (!strcasecmp(content_transfer_encoding, "base64")) {
100                         StrBufDecodeBase64(raw_msg);
101                 }
102                 if (!strcasecmp(content_transfer_encoding, "quoted-printable")) {
103                         StrBufDecodeQP(raw_msg);
104                 }
105
106                 // At this point, raw_msg contains the decoded message.
107                 // Now run through the renderers we have available.
108
109                 if (!strncasecmp(content_type, "text/html", 9)) {
110                         sanitized_msg = html2html("UTF-8", 0, c->room, msgnum, raw_msg);
111                 }
112                 else if (!strncasecmp(content_type, "text/plain", 10)) {
113                         sanitized_msg = text2html("UTF-8", 0, c->room, msgnum, raw_msg);
114                 }
115                 else if (!strncasecmp(content_type, "text/x-citadel-variformat", 25)) {
116                         sanitized_msg = variformat2html(raw_msg);
117                 }
118                 else {
119                         sanitized_msg = NewStrBufPlain(HKEY("<i>No renderer for this content type</i><br>"));
120                         syslog(LOG_WARNING, "forum_view: no renderer for content type %s", content_type);
121                 }
122                 FreeStrBuf(&raw_msg);
123
124                 // If sanitized_msg is not NULL, we have rendered the message and can output it.
125                 if (sanitized_msg) {
126                         JsonObjectAppend(j, NewJsonString(HKEY("text"), sanitized_msg, NEWJSONSTRING_SMASHBUF));
127                 }
128                 else {
129                         syslog(LOG_WARNING, "forum_view: message %ld of content type %s converted to NULL", msgnum, content_type);
130                 }
131         }
132
133         StrBuf *sj = NewStrBuf();
134         SerializeJson(sj, j, 1);        // '1' == free the source object
135
136         add_response_header(h, strdup("Content-type"), strdup("application/json"));
137         h->response_code = 200;
138         h->response_string = strdup("OK");
139         h->response_body_length = StrLength(sj);
140         h->response_body = SmashStrBuf(&sj);
141 }