Begin gathering references and message IDs for replies
[citadel.git] / webcit-ng / forum_view.c
1 //
2 // Forum view (threaded/flat)
3 //
4 // Copyright (c) 1996-2021 by the citadel.org team
5 //
6 // This program is open source software.  It runs great on the
7 // Linux operating system (and probably elsewhere).  You can use,
8 // copy, and run it under the terms of the GNU General Public
9 // License version 3.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15
16 #include "webcit.h"
17
18 struct mthread {
19         long msgnum;
20         time_t datetime;
21         int threadhash;
22         int refhashes[10];
23         char from[64];
24         int parent;
25 };
26
27
28 // Commands we need to send to Citadel Server before we begin rendering forum view.
29 // These are common to flat and threaded views.
30 void setup_for_forum_view(struct ctdlsession *c) {
31         char buf[1024];
32         ctdl_printf(c, "MSGP text/html|text/plain");    // Declare the MIME types we know how to render
33         ctdl_readline(c, buf, sizeof(buf));             // Ignore the response
34         ctdl_printf(c, "MSGP dont_decode");             // Tell the server we will decode base64/etc client-side
35         ctdl_readline(c, buf, sizeof(buf));             // Ignore the response
36 }
37
38
39 // Fetch a single message and return it in JSON format for client-side rendering
40 void json_render_one_message(struct http_transaction *h, struct ctdlsession *c, long msgnum) {
41         StrBuf *raw_msg = NULL;
42         StrBuf *sanitized_msg = NULL;
43         char buf[1024];
44         char content_transfer_encoding[1024] = { 0 };
45         char content_type[1024] = { 0 };
46         char datetime[128] = { 0 };
47         char author[1024] = { 0 };
48         char emailaddr[1024] = { 0 };
49         int message_originated_locally = 0;
50
51         setup_for_forum_view(c);
52
53         ctdl_printf(c, "MSG4 %ld", msgnum);
54         ctdl_readline(c, buf, sizeof(buf));
55         if (buf[0] != '1') {
56                 do_404(h);
57                 return;
58         }
59
60         JsonValue *j = NewJsonObject(HKEY("message"));
61
62         while ((ctdl_readline(c, buf, sizeof(buf)) >= 0) && (strcmp(buf, "text")) && (strcmp(buf, "000"))) {
63
64                 // citadel header parsing here
65                 if (!strncasecmp(buf, "from=", 5)) {
66                         safestrncpy(author, &buf[5], sizeof author);
67                 }
68                 else if (!strncasecmp(buf, "rfca=", 5)) {
69                         safestrncpy(emailaddr, &buf[5], sizeof emailaddr);
70                 }
71                 else if (!strncasecmp(buf, "time=", 5)) {
72                         time_t tt;
73                         struct tm tm;
74                         tt = atol(&buf[5]);
75                         localtime_r(&tt, &tm);
76                         strftime(datetime, sizeof datetime, "%c", &tm);
77                         JsonObjectAppend(j, NewJsonPlainString(HKEY("time"), datetime, -1));
78                 }
79                 else if (!strncasecmp(buf, "locl=", 5)) {
80                         message_originated_locally = 1;
81                 }
82                 else if (!strncasecmp(buf, "subj=", 5)) {
83                         JsonObjectAppend(j, NewJsonPlainString(HKEY("subj"), &buf[5], -1));
84                 }
85                 else if (!strncasecmp(buf, "msgn=", 5)) {
86                         JsonObjectAppend(j, NewJsonPlainString(HKEY("msgn"), &buf[5], -1));
87                 }
88                 else if (!strncasecmp(buf, "wefw=", 5)) {
89                         JsonObjectAppend(j, NewJsonPlainString(HKEY("wefw"), &buf[5], -1));
90                 }
91         }
92
93         if (message_originated_locally) {
94                 JsonObjectAppend(j, NewJsonPlainString(HKEY("from"), author, -1));
95         }
96         else {
97                 JsonObjectAppend(j, NewJsonPlainString(HKEY("from"), emailaddr, -1));           // FIXME do the compound address string
98         }
99
100         if (!strcmp(buf, "text")) {
101                 while ((ctdl_readline(c, buf, sizeof(buf)) >= 0) && (strcmp(buf, "")) && (strcmp(buf, "000"))) {
102                         // rfc822 header parsing here
103                         if (!strncasecmp(buf, "Content-transfer-encoding:", 26)) {
104                                 strcpy(content_transfer_encoding, &buf[26]);
105                                 striplt(content_transfer_encoding);
106                         }
107                         if (!strncasecmp(buf, "Content-type:", 13)) {
108                                 strcpy(content_type, &buf[13]);
109                                 striplt(content_type);
110                         }
111                 }
112                 raw_msg = ctdl_readtextmsg(c);
113         }
114         else {
115                 raw_msg = NULL;
116         }
117
118         if (raw_msg) {
119                 // These are the encodings we know how to handle.  Decode in-place.
120
121                 if (!strcasecmp(content_transfer_encoding, "base64")) {
122                         StrBufDecodeBase64(raw_msg);
123                 }
124                 if (!strcasecmp(content_transfer_encoding, "quoted-printable")) {
125                         StrBufDecodeQP(raw_msg);
126                 }
127
128                 // At this point, raw_msg contains the decoded message.
129                 // Now run through the renderers we have available.
130
131                 if (!strncasecmp(content_type, "text/html", 9)) {
132                         sanitized_msg = html2html("UTF-8", 0, c->room, msgnum, raw_msg);
133                 }
134                 else if (!strncasecmp(content_type, "text/plain", 10)) {
135                         sanitized_msg = text2html("UTF-8", 0, c->room, msgnum, raw_msg);
136                 }
137                 else if (!strncasecmp(content_type, "text/x-citadel-variformat", 25)) {
138                         sanitized_msg = variformat2html(raw_msg);
139                 }
140                 else {
141                         sanitized_msg = NewStrBufPlain(HKEY("<i>No renderer for this content type</i><br>"));
142                         syslog(LOG_WARNING, "forum_view: no renderer for content type %s", content_type);
143                 }
144                 FreeStrBuf(&raw_msg);
145
146                 // If sanitized_msg is not NULL, we have rendered the message and can output it.
147                 if (sanitized_msg) {
148                         JsonObjectAppend(j, NewJsonString(HKEY("text"), sanitized_msg, NEWJSONSTRING_SMASHBUF));
149                 }
150                 else {
151                         syslog(LOG_WARNING, "forum_view: message %ld of content type %s converted to NULL", msgnum, content_type);
152                 }
153         }
154
155         StrBuf *sj = NewStrBuf();
156         SerializeJson(sj, j, 1);        // '1' == free the source object
157
158         add_response_header(h, strdup("Content-type"), strdup("application/json"));
159         h->response_code = 200;
160         h->response_string = strdup("OK");
161         h->response_body_length = StrLength(sj);
162         h->response_body = SmashStrBuf(&sj);
163 }