2 // Output an HTML message, modifying it slightly to make sure it plays nice
3 // with the rest of our web framework.
5 // Copyright (c) 2005-2022 by the citadel.org team
7 // This program is open source software. Use, duplication, or
8 // disclosure are subject to the GNU General Public License v3.
13 // Strip surrounding single or double quotes from a string.
14 void stripquotes(char *s) {
24 if (((s[0] == '\"') && (s[len - 1] == '\"')) || ((s[0] == '\'') && (s[len - 1] == '\''))) {
31 // Check to see if a META tag has overridden the declared MIME character set.
33 // charset Character set name (left unchanged if we don't do anything)
34 // meta_http_equiv Content of the "http-equiv" portion of the META tag
35 // meta_content Content of the "content" portion of the META tag
36 void extract_charset_from_meta(char *charset, char *meta_http_equiv, char *meta_content) {
47 if (strcasecmp(meta_http_equiv, "Content-type"))
50 ptr = strchr(meta_content, ';');
54 safestrncpy(buf, ++ptr, sizeof buf);
56 if (!strncasecmp(buf, "charset=", 8)) {
57 strcpy(charset, &buf[8]);
59 // The brain-damaged webmail program in Microsoft Exchange declares
60 // a charset of "unicode" when they really mean "UTF-8". GNU iconv
61 // treats "unicode" as an alias for "UTF-16" so we have to manually
62 // fix this here, otherwise messages generated in Exchange webmail
63 // show up as a big pile of weird characters.
64 if (!strcasecmp(charset, "unicode")) {
65 strcpy(charset, "UTF-8");
68 // Remove wandering punctuation
69 if ((ptr = strchr(charset, '\"')))
76 // Sanitize and enhance an HTML message for display.
77 // Also convert weird character sets to UTF-8 if necessary.
78 // Also fixup img src="cid:..." type inline images to fetch the image
79 StrBuf *html2html(const char *supplied_charset, int treat_as_wiki, char *roomname, long msgnum, StrBuf *Source) {
85 StrBuf *converted_msg;
86 int buffer_length = 1;
88 int content_length = 0;
93 int script_start_pos = (-1);
97 StrBuf *BodyArea = NULL;
99 iconv_t ic = (iconv_t) (-1);
100 char *ibuf; // Buffer of characters to be converted
101 char *obuf; // Buffer for converted characters
102 size_t ibuflen; // Length of input buffer
103 size_t obuflen; // Length of output buffer
104 char *osav; // Saved pointer to output buffer
106 StrBuf *Target = NewStrBuf();
107 if (Target == NULL) {
111 safestrncpy(charset, supplied_charset, sizeof charset);
112 sprintf(new_window, "<a target=\"%s\" href=", TARGET);
114 content_length = StrLength(Source);
115 msg = (char *) ChrPtr(Source);
116 buffer_length = content_length;
118 // Do a first pass to isolate the message body
121 msgend = &msg[content_length];
123 while (ptr < msgend) {
125 // Advance to next tag
126 ptr = strchr(ptr, '<');
127 if ((ptr == NULL) || (ptr >= msgend))
130 if ((ptr == NULL) || (ptr >= msgend))
133 // Look for META tags. Some messages (particularly in
134 // Asian locales) illegally declare a message's character
135 // set in the HTML instead of in the MIME headers. This
136 // is wrong but we have to work around it anyway.
137 if (!strncasecmp(ptr, "META", 4)) {
143 char *meta_http_equiv;
147 meta_start = &ptr[4];
148 meta_end = strchr(ptr, '>');
149 if ((meta_end != NULL) && (meta_end <= msgend)) {
150 meta_length = meta_end - meta_start + 1;
151 meta = malloc(meta_length + 1);
152 safestrncpy(meta, meta_start, meta_length);
153 meta[meta_length] = 0;
155 if (!strncasecmp(meta, "HTTP-EQUIV=", 11)) {
156 meta_http_equiv = strdup(&meta[11]);
157 spaceptr = strchr(meta_http_equiv, ' ');
158 if (spaceptr != NULL) {
160 meta_content = strdup(++spaceptr);
161 if (!strncasecmp(meta_content, "content=", 8)) {
162 strcpy(meta_content, &meta_content[8]);
163 stripquotes(meta_http_equiv);
164 stripquotes(meta_content);
165 extract_charset_from_meta(charset, meta_http_equiv, meta_content);
169 free(meta_http_equiv);
175 // Any of these tags cause everything up to and including
176 // the tag to be removed.
177 if ((!strncasecmp(ptr, "HTML", 4))
178 || (!strncasecmp(ptr, "HEAD", 4))
179 || (!strncasecmp(ptr, "/HEAD", 5))
180 || (!strncasecmp(ptr, "BODY", 4))) {
183 if (!strncasecmp(ptr, "BODY", 4)) {
186 ptr = strchr(ptr, '>');
187 if ((ptr == NULL) || (ptr >= msgend))
189 if ((pBody != NULL) && (ptr - pBody > 4)) {
191 char *cid_start, *cid_end;
195 while ((isspace(*pBody)) && (pBody < ptr))
197 BodyArea = NewStrBufPlain(NULL, ptr - pBody);
200 src = strstr(pBody, "cid:");
204 while ((*cid_end != '"') && !isspace(*cid_end) && (cid_end < ptr))
207 // copy tag and attributes up to src="cid:
208 StrBufAppendBufPlain(BodyArea, pBody, src - pBody, 0);
210 // add in /webcit/mimepart/<msgno>/CID/
211 // trailing / stops dumb URL filters getting excited
212 StrBufAppendPrintf(BodyArea, "/webcit/mimepart/%ld/", msgnum);
213 StrBufAppendBufPlain(BodyArea, cid_start, cid_end - cid_start, 0);
215 if (ptr - cid_end > 0)
216 StrBufAppendBufPlain(BodyArea, cid_end + 1, ptr - cid_end, 0);
219 StrBufAppendBufPlain(BodyArea, pBody, ptr - pBody, 0);
225 if ((ptr == NULL) || (ptr >= msgend))
230 // Any of these tags cause everything including and following
231 // the tag to be removed.
232 if ((!strncasecmp(ptr, "/HTML", 5)) || (!strncasecmp(ptr, "/BODY", 5))) {
240 if (msgstart > msg) {
241 strcpy(msg, msgstart);
244 // Now go through the message, parsing tags as necessary.
245 converted_msg = NewStrBufPlain(NULL, content_length + 8192);
247 // Convert foreign character sets to UTF-8 if necessary
248 if ((strcasecmp(charset, "us-ascii"))
249 && (strcasecmp(charset, "UTF-8"))
250 && (strcasecmp(charset, ""))
252 syslog(LOG_DEBUG, "Converting %s to UTF-8", charset);
253 ctdl_iconv_open("UTF-8", charset, &ic);
254 if (ic == (iconv_t) (-1)) {
255 syslog(LOG_WARNING, "%s:%d iconv_open() failed: %s", __FILE__, __LINE__, strerror(errno));
258 if (Source == NULL) {
259 if (ic != (iconv_t) (-1)) {
261 ibuflen = content_length;
262 obuflen = content_length + (content_length / 2);
263 obuf = (char *) malloc(obuflen);
265 iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
266 content_length = content_length + (content_length / 2) - obuflen;
267 osav[content_length] = 0;
274 if (ic != (iconv_t) (-1)) {
275 StrBuf *Buf = NewStrBufPlain(NULL, StrLength(Source) + 8096);;
276 StrBufConvert(Source, Buf, &ic);
279 msg = (char *) ChrPtr(Source); // TODO: get rid of this.
283 // At this point, the message has been stripped down to
284 // only the content inside the <BODY></BODY> tags, and has
285 // been converted to UTF-8 if it was originally in a foreign
286 // character set. The text is also guaranteed to be null
289 if (converted_msg == NULL) {
290 StrBufAppendPrintf(Target, "Error %d: %s<br>%s:%d", errno, strerror(errno), __FILE__, __LINE__);
294 if (BodyArea != NULL) { // Any attributes that were declared in the <body> tag
295 StrBufAppendBufPlain(converted_msg, HKEY("<div "), 0); // are instead declared in this <div> tag
296 StrBufAppendBuf(converted_msg, BodyArea, 0);
297 StrBufAppendBufPlain(converted_msg, HKEY(">"), 0);
300 msgend = strchr(msg, 0);
301 while (ptr < msgend) {
303 // Try to sanitize the html of any rogue scripts
304 if (!strncasecmp(ptr, "<script", 7)) {
305 if (scriptlevel == 0) {
306 script_start_pos = StrLength(converted_msg);
310 if (!strncasecmp(ptr, "</script", 8)) {
314 // Change mailto: links to WebCit mail, by replacing the
315 // link with one that points back to our mail room. Due to
316 // the way we parse URL's, it'll even handle mailto: links
317 // that have "?subject=" in them.
318 // FIXME change URL syntax for webcit-ng
319 if (!strncasecmp(ptr, "<a href=\"mailto:", 16)) {
320 content_length += 64;
321 StrBufAppendPrintf(converted_msg, "<a href=\"display_enter?force_room=_MAIL_?recp=");
327 // Make external links open in a separate window
328 else if (!strncasecmp(ptr, "<a href=\"", 9)) {
331 if (((strchr(ptr, ':') < strchr(ptr, '/'))) && ((strchr(ptr, '/') < strchr(ptr, '>')))) {
332 // open external links to new window
333 StrBufAppendPrintf(converted_msg, new_window);
336 else if ((treat_as_wiki)
337 && (strncasecmp(ptr, "<a href=\"wiki?", 14))
338 && (strncasecmp(ptr, "<a href=\"dotgoto?", 17))
339 && (strncasecmp(ptr, "<a href=\"knrooms?", 17))
341 content_length += 64;
342 StrBufAppendPrintf(converted_msg, "<a href=\"wiki?go=");
343 //StrBufUrlescAppend(converted_msg, "FIXME ROOM NAME", NULL); // FIXME make compatible with webcit-ng
344 StrBufAppendPrintf(converted_msg, "?page=");
348 StrBufAppendPrintf(converted_msg, "<a href=\"");
353 // Fixup <img src="cid:... ...> to fetch the mime part
354 else if (!strncasecmp(ptr, "<img ", 5)) {
355 char *cid_start, *cid_end;
356 char *tag_end = strchr(ptr, '>');
358 // FIXME - handle this situation (maybe someone opened an <img cid...
359 // and then ended the message)
361 syslog(LOG_DEBUG, "tag_end is null and ptr is:");
362 syslog(LOG_DEBUG, "%s", ptr);
363 syslog(LOG_DEBUG, "Theoretical bytes remaining: %d", (int) (msgend - ptr));
366 src = strstr(ptr, "src=\"cid:");
369 if (src && isspace(*(src - 1))
370 && tag_end && (cid_start = strchr(src, ':'))
371 && (cid_end = strchr(cid_start, '"'))
372 && (cid_end < tag_end)
374 // copy tag and attributes up to src="cid:
375 StrBufAppendBufPlain(converted_msg, ptr, src - ptr, 0);
378 // add in /webcit/mimepart/<msgnum>/CID/
379 // trailing / stops dumb URL filters getting excited
380 StrBufAppendPrintf(converted_msg, " src=\"/ctdl/r/");
381 StrBufXMLEscAppend(converted_msg, NULL, roomname, strlen(roomname), 0);
382 syslog(LOG_DEBUG, "room name is '%s'", roomname);
383 StrBufAppendPrintf(converted_msg, "/%ld/", msgnum);
384 StrBufAppendBufPlain(converted_msg, cid_start, cid_end - cid_start, 0);
385 StrBufAppendBufPlain(converted_msg, "\"", -1, 0);
388 StrBufAppendBufPlain(converted_msg, ptr, tag_end - ptr, 0);
392 // Turn anything that looks like a URL into a real link, as long
393 // as it's not inside a tag already
394 else if ((brak == 0) && (alevel == 0) && ((!strncasecmp(ptr, "http://", 7)) || (!strncasecmp(ptr, "https://", 8)))) {
395 // Find the end of the link
399 strlenptr = strlen(ptr);
400 for (i = 0; i <= strlenptr; ++i) {
417 if ((ptr[i + 2] == ';') ||
418 (ptr[i + 3] == ';') ||
419 (ptr[i + 5] == ';') || (ptr[i + 6] == ';') || (ptr[i + 7] == ';'))
432 linkedchar = ptr[len];
434 // spot for some subject strings tinymce tends to give us.
435 ltreviewptr = strchr(ptr, '<');
436 if (ltreviewptr != NULL) {
438 linklen = ltreviewptr - ptr;
441 nbspreviewptr = strstr(ptr, " ");
442 if (nbspreviewptr != NULL) {
443 // nbspreviewptr = '\0';
444 linklen = nbspreviewptr - ptr;
446 if (ltreviewptr != 0)
449 ptr[len] = linkedchar;
451 content_length += (32 + linklen);
452 StrBufAppendPrintf(converted_msg, "%s\"", new_window);
453 StrBufAppendBufPlain(converted_msg, ptr, linklen, 0);
454 StrBufAppendPrintf(converted_msg, "\">");
455 StrBufAppendBufPlain(converted_msg, ptr, linklen, 0);
457 StrBufAppendPrintf(converted_msg, "</a>");
461 StrBufAppendBufPlain(converted_msg, ptr, 1, 0);
465 if ((ptr >= msg) && (ptr <= msgend)) {
466 // We need to know when we're inside a tag,
467 // so we don't turn things that look like URL's into
468 // links, when they're already links - or image sources.
469 if ((ptr > msg) && (*(ptr - 1) == '<')) {
472 if ((ptr > msg) && (*(ptr - 1) == '>')) {
474 if ((scriptlevel == 0) && (script_start_pos >= 0)) {
475 StrBufCutRight(converted_msg, StrLength(converted_msg) - script_start_pos);
476 script_start_pos = (-1);
479 if (!strncasecmp(ptr, "</a>", 3))
484 if (BodyArea != NULL) {
485 StrBufAppendBufPlain(converted_msg, HKEY("</div>"), 0); // Close the div where we declared attributes copied
486 FreeStrBuf(&BodyArea); // from the original <body> tag
489 // uncomment these two lines to override conversion
490 // memcpy(converted_msg, msg, content_length);
491 // output_length = content_length;
493 // Output our big pile of markup
494 StrBufAppendBuf(Target, converted_msg, 0);
496 BAIL: // A little trailing vertical whitespace...
497 StrBufAppendPrintf(Target, "<br>\n");
499 // Now give back the memory
500 FreeStrBuf(&converted_msg);
501 if ((msg != NULL) && (Source == NULL))
507 // Look for URL's embedded in a buffer and make them linkable. We use a
508 // target window in order to keep the Citadel session in its own window.
509 void UrlizeText(StrBuf * Target, StrBuf * Source, StrBuf * WrkBuf) {
510 int len, UrlLen, Offset, TrailerLen;
511 const char *start, *end, *pos;
515 len = StrLength(Source);
516 end = ChrPtr(Source) + len;
517 for (pos = ChrPtr(Source); (pos < end) && (start == NULL); ++pos) {
518 if (!strncasecmp(pos, "http://", 7))
520 else if (!strncasecmp(pos, "ftp://", 6))
525 StrBufAppendBuf(Target, Source, 0);
530 for (pos = ChrPtr(Source) + len; pos > start; --pos) {
550 UrlLen = end - start;
551 StrBufAppendBufPlain(WrkBuf, start, UrlLen, 0);
553 Offset = start - ChrPtr(Source);
555 StrBufAppendBufPlain(Target, ChrPtr(Source), Offset, 0);
556 StrBufAppendPrintf(Target, "%ca href=%c%s%c TARGET=%c%s%c%c%s%c/A%c",
557 LB, QU, ChrPtr(WrkBuf), QU, QU, TARGET, QU, RB, ChrPtr(WrkBuf), LB, RB);
559 TrailerLen = StrLength(Source) - (end - ChrPtr(Source));
561 StrBufAppendBufPlain(Target, end, TrailerLen, 0);
565 void url(char *buf, size_t bufsize) {
566 int len, UrlLen, Offset, TrailerLen, outpos;
567 char *start, *end, *pos;
574 syslog(LOG_WARNING, "URL: content longer than buffer!");
578 for (pos = buf; (pos < end) && (start == NULL); ++pos) {
579 if (!strncasecmp(pos, "http://", 7))
581 if (!strncasecmp(pos, "ftp://", 6))
588 for (pos = buf + len; pos > start; --pos) {
608 UrlLen = end - start;
609 if (UrlLen > sizeof(urlbuf)) {
610 syslog(LOG_WARNING, "URL: content longer than buffer!");
613 memcpy(urlbuf, start, UrlLen);
614 urlbuf[UrlLen] = '\0';
616 Offset = start - buf;
617 if ((Offset != 0) && (Offset < sizeof(outbuf)))
618 memcpy(outbuf, buf, Offset);
619 outpos = snprintf(&outbuf[Offset], sizeof(outbuf) - Offset,
620 "%ca href=%c%s%c TARGET=%c%s%c%c%s%c/A%c", LB, QU, urlbuf, QU, QU, TARGET, QU, RB, urlbuf, LB, RB);
621 if (outpos >= sizeof(outbuf) - Offset) {
622 syslog(LOG_WARNING, "URL: content longer than buffer!");
626 TrailerLen = len - (end - start);
628 memcpy(outbuf + Offset + outpos, end, TrailerLen);
629 if (Offset + outpos + TrailerLen > bufsize) {
630 syslog(LOG_WARNING, "URL: content longer than buffer!");
633 memcpy(buf, outbuf, Offset + outpos + TrailerLen);
634 *(buf + Offset + outpos + TrailerLen) = '\0';