1 // Output an HTML message, modifying it slightly to make sure it plays nice
2 // with the rest of our web framework.
4 // Copyright (c) 2005-2022 by the citadel.org team
6 // This program is open source software. Use, duplication, or
7 // disclosure is subject to the GNU General Public License v3.
12 // Strip surrounding single or double quotes from a string.
13 void stripquotes(char *s) {
23 if (((s[0] == '\"') && (s[len - 1] == '\"')) || ((s[0] == '\'') && (s[len - 1] == '\''))) {
30 // Check to see if a META tag has overridden the declared MIME character set.
32 // charset Character set name (left unchanged if we don't do anything)
33 // meta_http_equiv Content of the "http-equiv" portion of the META tag
34 // meta_content Content of the "content" portion of the META tag
35 void extract_charset_from_meta(char *charset, char *meta_http_equiv, char *meta_content) {
46 if (strcasecmp(meta_http_equiv, "Content-type"))
49 ptr = strchr(meta_content, ';');
53 safestrncpy(buf, ++ptr, sizeof buf);
55 if (!strncasecmp(buf, "charset=", 8)) {
56 strcpy(charset, &buf[8]);
58 // The brain-damaged webmail program in Microsoft Exchange declares
59 // a charset of "unicode" when they really mean "UTF-8". GNU iconv
60 // treats "unicode" as an alias for "UTF-16" so we have to manually
61 // fix this here, otherwise messages generated in Exchange webmail
62 // show up as a big pile of weird characters.
63 if (!strcasecmp(charset, "unicode")) {
64 strcpy(charset, "UTF-8");
67 // Remove wandering punctuation
68 if ((ptr = strchr(charset, '\"')))
75 // Sanitize and enhance an HTML message for display.
76 // Also convert weird character sets to UTF-8 if necessary.
77 // Also fixup img src="cid:..." type inline images to fetch the image
78 StrBuf *html2html(const char *supplied_charset, int treat_as_wiki, char *roomname, long msgnum, StrBuf *Source) {
84 StrBuf *converted_msg;
85 int buffer_length = 1;
87 int content_length = 0;
92 int script_start_pos = (-1);
96 StrBuf *BodyArea = NULL;
98 iconv_t ic = (iconv_t) (-1);
99 char *ibuf; // Buffer of characters to be converted
100 char *obuf; // Buffer for converted characters
101 size_t ibuflen; // Length of input buffer
102 size_t obuflen; // Length of output buffer
103 char *osav; // Saved pointer to output buffer
105 StrBuf *Target = NewStrBuf();
106 if (Target == NULL) {
110 safestrncpy(charset, supplied_charset, sizeof charset);
111 sprintf(new_window, "<a target=\"%s\" href=", TARGET);
113 content_length = StrLength(Source);
114 msg = (char *) ChrPtr(Source);
115 buffer_length = content_length;
117 // Do a first pass to isolate the message body
120 msgend = &msg[content_length];
122 while (ptr < msgend) {
124 // Advance to next tag
125 ptr = strchr(ptr, '<');
126 if ((ptr == NULL) || (ptr >= msgend))
129 if ((ptr == NULL) || (ptr >= msgend))
132 // Look for META tags. Some messages (particularly in
133 // Asian locales) illegally declare a message's character
134 // set in the HTML instead of in the MIME headers. This
135 // is wrong but we have to work around it anyway.
136 if (!strncasecmp(ptr, "META", 4)) {
142 char *meta_http_equiv;
146 meta_start = &ptr[4];
147 meta_end = strchr(ptr, '>');
148 if ((meta_end != NULL) && (meta_end <= msgend)) {
149 meta_length = meta_end - meta_start + 1;
150 meta = malloc(meta_length + 1);
151 safestrncpy(meta, meta_start, meta_length);
152 meta[meta_length] = 0;
154 if (!strncasecmp(meta, "HTTP-EQUIV=", 11)) {
155 meta_http_equiv = strdup(&meta[11]);
156 spaceptr = strchr(meta_http_equiv, ' ');
157 if (spaceptr != NULL) {
159 meta_content = strdup(++spaceptr);
160 if (!strncasecmp(meta_content, "content=", 8)) {
161 strcpy(meta_content, &meta_content[8]);
162 stripquotes(meta_http_equiv);
163 stripquotes(meta_content);
164 extract_charset_from_meta(charset, meta_http_equiv, meta_content);
168 free(meta_http_equiv);
174 // Any of these tags cause everything up to and including
175 // the tag to be removed.
176 if ((!strncasecmp(ptr, "HTML", 4))
177 || (!strncasecmp(ptr, "HEAD", 4))
178 || (!strncasecmp(ptr, "/HEAD", 5))
179 || (!strncasecmp(ptr, "BODY", 4))) {
182 if (!strncasecmp(ptr, "BODY", 4)) {
185 ptr = strchr(ptr, '>');
186 if ((ptr == NULL) || (ptr >= msgend))
188 if ((pBody != NULL) && (ptr - pBody > 4)) {
190 char *cid_start, *cid_end;
194 while ((isspace(*pBody)) && (pBody < ptr))
196 BodyArea = NewStrBufPlain(NULL, ptr - pBody);
199 src = strstr(pBody, "cid:");
203 while ((*cid_end != '"') && !isspace(*cid_end) && (cid_end < ptr))
206 // copy tag and attributes up to src="cid:
207 StrBufAppendBufPlain(BodyArea, pBody, src - pBody, 0);
209 // add in /webcit/mimepart/<msgno>/CID/
210 // trailing / stops dumb URL filters getting excited
211 StrBufAppendPrintf(BodyArea, "/webcit/mimepart/%ld/", msgnum);
212 StrBufAppendBufPlain(BodyArea, cid_start, cid_end - cid_start, 0);
214 if (ptr - cid_end > 0)
215 StrBufAppendBufPlain(BodyArea, cid_end + 1, ptr - cid_end, 0);
218 StrBufAppendBufPlain(BodyArea, pBody, ptr - pBody, 0);
224 if ((ptr == NULL) || (ptr >= msgend))
229 // Any of these tags cause everything including and following
230 // the tag to be removed.
231 if ((!strncasecmp(ptr, "/HTML", 5)) || (!strncasecmp(ptr, "/BODY", 5))) {
239 if (msgstart > msg) {
240 strcpy(msg, msgstart);
243 // Now go through the message, parsing tags as necessary.
244 converted_msg = NewStrBufPlain(NULL, content_length + 8192);
246 // Convert foreign character sets to UTF-8 if necessary
247 if ((strcasecmp(charset, "us-ascii"))
248 && (strcasecmp(charset, "UTF-8"))
249 && (strcasecmp(charset, ""))
251 syslog(LOG_DEBUG, "Converting %s to UTF-8", charset);
252 ctdl_iconv_open("UTF-8", charset, &ic);
253 if (ic == (iconv_t) (-1)) {
254 syslog(LOG_WARNING, "%s:%d iconv_open() failed: %s", __FILE__, __LINE__, strerror(errno));
257 if (Source == NULL) {
258 if (ic != (iconv_t) (-1)) {
260 ibuflen = content_length;
261 obuflen = content_length + (content_length / 2);
262 obuf = (char *) malloc(obuflen);
264 iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
265 content_length = content_length + (content_length / 2) - obuflen;
266 osav[content_length] = 0;
273 if (ic != (iconv_t) (-1)) {
274 StrBuf *Buf = NewStrBufPlain(NULL, StrLength(Source) + 8096);;
275 StrBufConvert(Source, Buf, &ic);
278 msg = (char *) ChrPtr(Source); // TODO: get rid of this.
282 // At this point, the message has been stripped down to
283 // only the content inside the <BODY></BODY> tags, and has
284 // been converted to UTF-8 if it was originally in a foreign
285 // character set. The text is also guaranteed to be null
288 if (converted_msg == NULL) {
289 StrBufAppendPrintf(Target, "Error %d: %s<br>%s:%d", errno, strerror(errno), __FILE__, __LINE__);
293 if (BodyArea != NULL) { // Any attributes that were declared in the <body> tag
294 StrBufAppendBufPlain(converted_msg, HKEY("<div "), 0); // are instead declared in this <div> tag
295 StrBufAppendBuf(converted_msg, BodyArea, 0);
296 StrBufAppendBufPlain(converted_msg, HKEY(">"), 0);
299 msgend = strchr(msg, 0);
300 while (ptr < msgend) {
302 // Try to sanitize the html of any rogue scripts
303 if (!strncasecmp(ptr, "<script", 7)) {
304 if (scriptlevel == 0) {
305 script_start_pos = StrLength(converted_msg);
309 if (!strncasecmp(ptr, "</script", 8)) {
313 // Change mailto: links to WebCit mail, by replacing the
314 // link with one that points back to our mail room. Due to
315 // the way we parse URL's, it'll even handle mailto: links
316 // that have "?subject=" in them.
317 // FIXME change URL syntax for webcit-ng
318 if (!strncasecmp(ptr, "<a href=\"mailto:", 16)) {
319 content_length += 64;
320 StrBufAppendPrintf(converted_msg, "<a href=\"display_enter?force_room=_MAIL_?recp=");
326 // Make external links open in a separate window
327 else if (!strncasecmp(ptr, "<a href=\"", 9)) {
330 if (((strchr(ptr, ':') < strchr(ptr, '/'))) && ((strchr(ptr, '/') < strchr(ptr, '>')))) {
331 // open external links to new window
332 StrBufAppendPrintf(converted_msg, new_window);
335 else if ((treat_as_wiki)
336 && (strncasecmp(ptr, "<a href=\"wiki?", 14))
337 && (strncasecmp(ptr, "<a href=\"dotgoto?", 17))
338 && (strncasecmp(ptr, "<a href=\"knrooms?", 17))
340 content_length += 64;
341 StrBufAppendPrintf(converted_msg, "<a href=\"wiki?go=");
342 //StrBufUrlescAppend(converted_msg, "FIXME ROOM NAME", NULL); // FIXME make compatible with webcit-ng
343 StrBufAppendPrintf(converted_msg, "?page=");
347 StrBufAppendPrintf(converted_msg, "<a href=\"");
352 // Fixup <img src="cid:... ...> to fetch the mime part
353 else if (!strncasecmp(ptr, "<img ", 5)) {
354 char *cid_start, *cid_end;
355 char *tag_end = strchr(ptr, '>');
357 // FIXME - handle this situation (maybe someone opened an <img cid...
358 // and then ended the message)
360 syslog(LOG_DEBUG, "tag_end is null and ptr is:");
361 syslog(LOG_DEBUG, "%s", ptr);
362 syslog(LOG_DEBUG, "Theoretical bytes remaining: %d", (int) (msgend - ptr));
365 src = strstr(ptr, "src=\"cid:");
368 if (src && isspace(*(src - 1))
369 && tag_end && (cid_start = strchr(src, ':'))
370 && (cid_end = strchr(cid_start, '"'))
371 && (cid_end < tag_end)
373 // copy tag and attributes up to src="cid:
374 StrBufAppendBufPlain(converted_msg, ptr, src - ptr, 0);
377 // add in /webcit/mimepart/<msgnum>/CID/
378 // trailing / stops dumb URL filters getting excited
379 StrBufAppendPrintf(converted_msg, " src=\"/ctdl/r/");
380 StrBufXMLEscAppend(converted_msg, NULL, roomname, strlen(roomname), 0);
381 syslog(LOG_DEBUG, "room name is '%s'", roomname);
382 StrBufAppendPrintf(converted_msg, "/%ld/", msgnum);
383 StrBufAppendBufPlain(converted_msg, cid_start, cid_end - cid_start, 0);
384 StrBufAppendBufPlain(converted_msg, "\"", -1, 0);
387 StrBufAppendBufPlain(converted_msg, ptr, tag_end - ptr, 0);
391 // Turn anything that looks like a URL into a real link, as long
392 // as it's not inside a tag already
393 else if ((brak == 0) && (alevel == 0) && ((!strncasecmp(ptr, "http://", 7)) || (!strncasecmp(ptr, "https://", 8)))) {
394 // Find the end of the link
398 strlenptr = strlen(ptr);
399 for (i = 0; i <= strlenptr; ++i) {
416 if ((ptr[i + 2] == ';') ||
417 (ptr[i + 3] == ';') ||
418 (ptr[i + 5] == ';') || (ptr[i + 6] == ';') || (ptr[i + 7] == ';'))
431 linkedchar = ptr[len];
433 // spot for some subject strings tinymce tends to give us.
434 ltreviewptr = strchr(ptr, '<');
435 if (ltreviewptr != NULL) {
437 linklen = ltreviewptr - ptr;
440 nbspreviewptr = strstr(ptr, " ");
441 if (nbspreviewptr != NULL) {
442 // nbspreviewptr = '\0';
443 linklen = nbspreviewptr - ptr;
445 if (ltreviewptr != 0)
448 ptr[len] = linkedchar;
450 content_length += (32 + linklen);
451 StrBufAppendPrintf(converted_msg, "%s\"", new_window);
452 StrBufAppendBufPlain(converted_msg, ptr, linklen, 0);
453 StrBufAppendPrintf(converted_msg, "\">");
454 StrBufAppendBufPlain(converted_msg, ptr, linklen, 0);
456 StrBufAppendPrintf(converted_msg, "</a>");
460 StrBufAppendBufPlain(converted_msg, ptr, 1, 0);
464 if ((ptr >= msg) && (ptr <= msgend)) {
465 // We need to know when we're inside a tag,
466 // so we don't turn things that look like URL's into
467 // links, when they're already links - or image sources.
468 if ((ptr > msg) && (*(ptr - 1) == '<')) {
471 if ((ptr > msg) && (*(ptr - 1) == '>')) {
473 if ((scriptlevel == 0) && (script_start_pos >= 0)) {
474 StrBufCutRight(converted_msg, StrLength(converted_msg) - script_start_pos);
475 script_start_pos = (-1);
478 if (!strncasecmp(ptr, "</a>", 3))
483 if (BodyArea != NULL) {
484 StrBufAppendBufPlain(converted_msg, HKEY("</div>"), 0); // Close the div where we declared attributes copied
485 FreeStrBuf(&BodyArea); // from the original <body> tag
488 // uncomment these two lines to override conversion
489 // memcpy(converted_msg, msg, content_length);
490 // output_length = content_length;
492 // Output our big pile of markup
493 StrBufAppendBuf(Target, converted_msg, 0);
495 BAIL: // A little trailing vertical whitespace...
496 StrBufAppendPrintf(Target, "<br>\n");
498 // Now give back the memory
499 FreeStrBuf(&converted_msg);
500 if ((msg != NULL) && (Source == NULL))
506 // Look for URL's embedded in a buffer and make them linkable. We use a
507 // target window in order to keep the Citadel session in its own window.
508 void UrlizeText(StrBuf * Target, StrBuf * Source, StrBuf * WrkBuf) {
509 int len, UrlLen, Offset, TrailerLen;
510 const char *start, *end, *pos;
514 len = StrLength(Source);
515 end = ChrPtr(Source) + len;
516 for (pos = ChrPtr(Source); (pos < end) && (start == NULL); ++pos) {
517 if (!strncasecmp(pos, "http://", 7))
519 else if (!strncasecmp(pos, "ftp://", 6))
524 StrBufAppendBuf(Target, Source, 0);
529 for (pos = ChrPtr(Source) + len; pos > start; --pos) {
549 UrlLen = end - start;
550 StrBufAppendBufPlain(WrkBuf, start, UrlLen, 0);
552 Offset = start - ChrPtr(Source);
554 StrBufAppendBufPlain(Target, ChrPtr(Source), Offset, 0);
555 StrBufAppendPrintf(Target, "%ca href=%c%s%c TARGET=%c%s%c%c%s%c/A%c",
556 LB, QU, ChrPtr(WrkBuf), QU, QU, TARGET, QU, RB, ChrPtr(WrkBuf), LB, RB);
558 TrailerLen = StrLength(Source) - (end - ChrPtr(Source));
560 StrBufAppendBufPlain(Target, end, TrailerLen, 0);
564 void url(char *buf, size_t bufsize) {
565 int len, UrlLen, Offset, TrailerLen, outpos;
566 char *start, *end, *pos;
573 syslog(LOG_WARNING, "URL: content longer than buffer!");
577 for (pos = buf; (pos < end) && (start == NULL); ++pos) {
578 if (!strncasecmp(pos, "http://", 7))
580 if (!strncasecmp(pos, "ftp://", 6))
587 for (pos = buf + len; pos > start; --pos) {
607 UrlLen = end - start;
608 if (UrlLen > sizeof(urlbuf)) {
609 syslog(LOG_WARNING, "URL: content longer than buffer!");
612 memcpy(urlbuf, start, UrlLen);
613 urlbuf[UrlLen] = '\0';
615 Offset = start - buf;
616 if ((Offset != 0) && (Offset < sizeof(outbuf)))
617 memcpy(outbuf, buf, Offset);
618 outpos = snprintf(&outbuf[Offset], sizeof(outbuf) - Offset,
619 "%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);
620 if (outpos >= sizeof(outbuf) - Offset) {
621 syslog(LOG_WARNING, "URL: content longer than buffer!");
625 TrailerLen = len - (end - start);
627 memcpy(outbuf + Offset + outpos, end, TrailerLen);
628 if (Offset + outpos + TrailerLen > bufsize) {
629 syslog(LOG_WARNING, "URL: content longer than buffer!");
632 memcpy(buf, outbuf, Offset + outpos + TrailerLen);
633 *(buf + Offset + outpos + TrailerLen) = '\0';