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-2018 by the citadel.org team
7 * This program is open source software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License, version 3.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
20 * Strip surrounding single or double quotes from a string.
22 void stripquotes(char *s)
33 if (((s[0] == '\"') && (s[len - 1] == '\"')) || ((s[0] == '\'') && (s[len - 1] == '\''))) {
41 * Check to see if a META tag has overridden the declared MIME character set.
43 * charset Character set name (left unchanged if we don't do anything)
44 * meta_http_equiv Content of the "http-equiv" portion of the META tag
45 * meta_content Content of the "content" portion of the META tag
47 void extract_charset_from_meta(char *charset, char *meta_http_equiv, char *meta_content)
59 if (strcasecmp(meta_http_equiv, "Content-type"))
62 ptr = strchr(meta_content, ';');
66 safestrncpy(buf, ++ptr, sizeof buf);
68 if (!strncasecmp(buf, "charset=", 8)) {
69 strcpy(charset, &buf[8]);
72 * The brain-damaged webmail program in Microsoft Exchange declares
73 * a charset of "unicode" when they really mean "UTF-8". GNU iconv
74 * treats "unicode" as an alias for "UTF-16" so we have to manually
75 * fix this here, otherwise messages generated in Exchange webmail
76 * show up as a big pile of weird characters.
78 if (!strcasecmp(charset, "unicode")) {
79 strcpy(charset, "UTF-8");
82 /* Remove wandering punctuation */
83 if ((ptr = strchr(charset, '\"')))
91 * Sanitize and enhance an HTML message for display.
92 * Also convert weird character sets to UTF-8 if necessary.
93 * Also fixup img src="cid:..." type inline images to fetch the image
96 StrBuf *html2html(const char *supplied_charset, int treat_as_wiki, char *roomname, long msgnum, StrBuf * Source)
103 StrBuf *converted_msg;
104 int buffer_length = 1;
106 int content_length = 0;
107 char new_window[SIZ];
111 int script_start_pos = (-1);
115 StrBuf *BodyArea = NULL;
117 iconv_t ic = (iconv_t) (-1);
118 char *ibuf; /* Buffer of characters to be converted */
119 char *obuf; /* Buffer for converted characters */
120 size_t ibuflen; /* Length of input buffer */
121 size_t obuflen; /* Length of output buffer */
122 char *osav; /* Saved pointer to output buffer */
128 StrBuf *Target = NewStrBuf();
129 if (Target == NULL) {
133 safestrncpy(charset, supplied_charset, sizeof charset);
134 sprintf(new_window, "<a target=\"%s\" href=", TARGET);
136 content_length = StrLength(Source);
137 msg = (char *) ChrPtr(Source);
138 buffer_length = content_length;
140 /* Do a first pass to isolate the message body */
143 msgend = &msg[content_length];
145 while (ptr < msgend) {
147 /* Advance to next tag */
148 ptr = strchr(ptr, '<');
149 if ((ptr == NULL) || (ptr >= msgend))
152 if ((ptr == NULL) || (ptr >= msgend))
156 * Look for META tags. Some messages (particularly in
157 * Asian locales) illegally declare a message's character
158 * set in the HTML instead of in the MIME headers. This
159 * is wrong but we have to work around it anyway.
161 if (!strncasecmp(ptr, "META", 4)) {
167 char *meta_http_equiv;
171 meta_start = &ptr[4];
172 meta_end = strchr(ptr, '>');
173 if ((meta_end != NULL) && (meta_end <= msgend)) {
174 meta_length = meta_end - meta_start + 1;
175 meta = malloc(meta_length + 1);
176 safestrncpy(meta, meta_start, meta_length);
177 meta[meta_length] = 0;
179 if (!strncasecmp(meta, "HTTP-EQUIV=", 11)) {
180 meta_http_equiv = strdup(&meta[11]);
181 spaceptr = strchr(meta_http_equiv, ' ');
182 if (spaceptr != NULL) {
184 meta_content = strdup(++spaceptr);
185 if (!strncasecmp(meta_content, "content=", 8)) {
186 strcpy(meta_content, &meta_content[8]);
187 stripquotes(meta_http_equiv);
188 stripquotes(meta_content);
189 extract_charset_from_meta(charset, meta_http_equiv, meta_content);
193 free(meta_http_equiv);
200 * Any of these tags cause everything up to and including
201 * the tag to be removed.
203 if ((!strncasecmp(ptr, "HTML", 4))
204 || (!strncasecmp(ptr, "HEAD", 4))
205 || (!strncasecmp(ptr, "/HEAD", 5))
206 || (!strncasecmp(ptr, "BODY", 4))) {
209 if (!strncasecmp(ptr, "BODY", 4)) {
212 ptr = strchr(ptr, '>');
213 if ((ptr == NULL) || (ptr >= msgend))
215 if ((pBody != NULL) && (ptr - pBody > 4)) {
217 char *cid_start, *cid_end;
221 while ((isspace(*pBody)) && (pBody < ptr))
223 BodyArea = NewStrBufPlain(NULL, ptr - pBody);
226 src = strstr(pBody, "cid:");
230 while ((*cid_end != '"') && !isspace(*cid_end) && (cid_end < ptr))
233 /* copy tag and attributes up to src="cid: */
234 StrBufAppendBufPlain(BodyArea, pBody, src - pBody, 0);
236 /* add in /webcit/mimepart/<msgno>/CID/
237 trailing / stops dumb URL filters getting excited */
238 StrBufAppendPrintf(BodyArea, "/webcit/mimepart/%ld/", msgnum);
239 StrBufAppendBufPlain(BodyArea, cid_start, cid_end - cid_start, 0);
241 if (ptr - cid_end > 0)
242 StrBufAppendBufPlain(BodyArea, cid_end + 1, ptr - cid_end, 0);
244 StrBufAppendBufPlain(BodyArea, pBody, ptr - pBody, 0);
249 if ((ptr == NULL) || (ptr >= msgend))
255 * Any of these tags cause everything including and following
256 * the tag to be removed.
258 if ((!strncasecmp(ptr, "/HTML", 5)) || (!strncasecmp(ptr, "/BODY", 5))) {
266 if (msgstart > msg) {
267 strcpy(msg, msgstart);
270 /* Now go through the message, parsing tags as necessary. */
271 converted_msg = NewStrBufPlain(NULL, content_length + 8192);
273 /* Convert foreign character sets to UTF-8 if necessary. */
274 if ((strcasecmp(charset, "us-ascii"))
275 && (strcasecmp(charset, "UTF-8"))
276 && (strcasecmp(charset, ""))
278 syslog(LOG_DEBUG, "Converting %s to UTF-8", charset);
279 ctdl_iconv_open("UTF-8", charset, &ic);
280 if (ic == (iconv_t) (-1)) {
281 syslog(LOG_WARNING, "%s:%d iconv_open() failed: %s", __FILE__, __LINE__, strerror(errno));
284 if (Source == NULL) {
285 if (ic != (iconv_t) (-1)) {
287 ibuflen = content_length;
288 obuflen = content_length + (content_length / 2);
289 obuf = (char *) malloc(obuflen);
291 iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
292 content_length = content_length + (content_length / 2) - obuflen;
293 osav[content_length] = 0;
299 if (ic != (iconv_t) (-1)) {
300 StrBuf *Buf = NewStrBufPlain(NULL, StrLength(Source) + 8096);;
301 StrBufConvert(Source, Buf, &ic);
304 msg = (char *) ChrPtr(Source); /* TODO: get rid of this. */
309 * At this point, the message has been stripped down to
310 * only the content inside the <BODY></BODY> tags, and has
311 * been converted to UTF-8 if it was originally in a foreign
312 * character set. The text is also guaranteed to be null
316 if (converted_msg == NULL) {
317 StrBufAppendPrintf(Target, "Error %d: %s<br>%s:%d", errno, strerror(errno), __FILE__, __LINE__);
321 if (BodyArea != NULL) { // Any attributes that were declared in the <body> tag
322 StrBufAppendBufPlain(converted_msg, HKEY("<div "), 0); // are instead declared in this <div> tag
323 StrBufAppendBuf(converted_msg, BodyArea, 0);
324 StrBufAppendBufPlain(converted_msg, HKEY(">"), 0);
327 msgend = strchr(msg, 0);
328 while (ptr < msgend) {
330 /* Try to sanitize the html of any rogue scripts */
331 if (!strncasecmp(ptr, "<script", 7)) {
332 if (scriptlevel == 0) {
333 script_start_pos = StrLength(converted_msg);
337 if (!strncasecmp(ptr, "</script", 8)) {
342 * Change mailto: links to WebCit mail, by replacing the
343 * link with one that points back to our mail room. Due to
344 * the way we parse URL's, it'll even handle mailto: links
345 * that have "?subject=" in them.
347 if (!strncasecmp(ptr, "<a href=\"mailto:", 16)) {
348 content_length += 64;
349 StrBufAppendPrintf(converted_msg, "<a href=\"display_enter?force_room=_MAIL_?recp="); // FIXME make compatible with webcit-ng
354 /* Make external links open in a separate window */
355 else if (!strncasecmp(ptr, "<a href=\"", 9)) {
358 if (((strchr(ptr, ':') < strchr(ptr, '/'))) && ((strchr(ptr, '/') < strchr(ptr, '>')))) {
359 /* open external links to new window */
360 StrBufAppendPrintf(converted_msg, new_window);
362 } else if ((treat_as_wiki)
363 && (strncasecmp(ptr, "<a href=\"wiki?", 14))
364 && (strncasecmp(ptr, "<a href=\"dotgoto?", 17))
365 && (strncasecmp(ptr, "<a href=\"knrooms?", 17))
367 content_length += 64;
368 StrBufAppendPrintf(converted_msg, "<a href=\"wiki?go=");
369 //StrBufUrlescAppend(converted_msg, "FIXME ROOM NAME", NULL); // FIXME make compatible with webcit-ng
370 StrBufAppendPrintf(converted_msg, "?page=");
373 StrBufAppendPrintf(converted_msg, "<a href=\"");
377 /* Fixup <img src="cid:... ...> to fetch the mime part */
378 else if (!strncasecmp(ptr, "<img ", 5)) {
379 char *cid_start, *cid_end;
380 char *tag_end = strchr(ptr, '>');
382 /* FIXME - handle this situation (maybe someone opened an <img cid...
383 * and then ended the message)
386 syslog(LOG_DEBUG, "tag_end is null and ptr is:");
387 syslog(LOG_DEBUG, "%s", ptr);
388 syslog(LOG_DEBUG, "Theoretical bytes remaining: %d", (int) (msgend - ptr));
391 src = strstr(ptr, "src=\"cid:");
394 if (src && isspace(*(src - 1))
395 && tag_end && (cid_start = strchr(src, ':'))
396 && (cid_end = strchr(cid_start, '"'))
397 && (cid_end < tag_end)
399 /* copy tag and attributes up to src="cid: */
400 StrBufAppendBufPlain(converted_msg, ptr, src - ptr, 0);
403 /* add in /webcit/mimepart/<msgno>/CID/
404 trailing / stops dumb URL filters getting excited */
405 StrBufAppendPrintf(converted_msg, " src=\"/ctdl/r/");
406 StrBufXMLEscAppend(converted_msg, NULL, roomname, strlen(roomname), 0);
407 syslog(LOG_DEBUG, "room name is '%s'", roomname);
408 StrBufAppendPrintf(converted_msg, "/%ld/", msgnum);
409 StrBufAppendBufPlain(converted_msg, cid_start, cid_end - cid_start, 0);
410 StrBufAppendBufPlain(converted_msg, "\"", -1, 0);
413 StrBufAppendBufPlain(converted_msg, ptr, tag_end - ptr, 0);
418 * Turn anything that looks like a URL into a real link, as long
419 * as it's not inside a tag already
421 else if ((brak == 0) && (alevel == 0) && ((!strncasecmp(ptr, "http://", 7)) || (!strncasecmp(ptr, "https://", 8)))) {
422 /* Find the end of the link */
426 strlenptr = strlen(ptr);
427 for (i = 0; i <= strlenptr; ++i) {
444 if ((ptr[i + 2] == ';') ||
445 (ptr[i + 3] == ';') ||
446 (ptr[i + 5] == ';') || (ptr[i + 6] == ';') || (ptr[i + 7] == ';'))
459 linkedchar = ptr[len];
461 /* spot for some subject strings tinymce tends to give us. */
462 ltreviewptr = strchr(ptr, '<');
463 if (ltreviewptr != NULL) {
465 linklen = ltreviewptr - ptr;
468 nbspreviewptr = strstr(ptr, " ");
469 if (nbspreviewptr != NULL) {
470 /* nbspreviewptr = '\0'; */
471 linklen = nbspreviewptr - ptr;
473 if (ltreviewptr != 0)
476 ptr[len] = linkedchar;
478 content_length += (32 + linklen);
479 StrBufAppendPrintf(converted_msg, "%s\"", new_window);
480 StrBufAppendBufPlain(converted_msg, ptr, linklen, 0);
481 StrBufAppendPrintf(converted_msg, "\">");
482 StrBufAppendBufPlain(converted_msg, ptr, linklen, 0);
484 StrBufAppendPrintf(converted_msg, "</a>");
487 StrBufAppendBufPlain(converted_msg, ptr, 1, 0);
491 if ((ptr >= msg) && (ptr <= msgend)) {
493 * We need to know when we're inside a tag,
494 * so we don't turn things that look like URL's into
495 * links, when they're already links - or image sources.
497 if ((ptr > msg) && (*(ptr - 1) == '<')) {
500 if ((ptr > msg) && (*(ptr - 1) == '>')) {
502 if ((scriptlevel == 0) && (script_start_pos >= 0)) {
503 StrBufCutRight(converted_msg, StrLength(converted_msg) - script_start_pos);
504 script_start_pos = (-1);
507 if (!strncasecmp(ptr, "</a>", 3))
512 if (BodyArea != NULL) {
513 StrBufAppendBufPlain(converted_msg, HKEY("</div>"), 0); // Close the div where we declared attributes copied
514 FreeStrBuf(&BodyArea); // from the original <body> tag
517 /* uncomment these two lines to override conversion */
518 /* memcpy(converted_msg, msg, content_length); */
519 /* output_length = content_length; */
521 /* Output our big pile of markup */
522 StrBufAppendBuf(Target, converted_msg, 0);
524 BAIL: /* A little trailing vertical whitespace... */
525 StrBufAppendPrintf(Target, "<br>\n");
527 /* Now give back the memory */
528 FreeStrBuf(&converted_msg);
529 if ((msg != NULL) && (Source == NULL))
536 * Look for URL's embedded in a buffer and make them linkable. We use a
537 * target window in order to keep the Citadel session in its own window.
539 void UrlizeText(StrBuf * Target, StrBuf * Source, StrBuf * WrkBuf)
541 int len, UrlLen, Offset, TrailerLen;
542 const char *start, *end, *pos;
546 len = StrLength(Source);
547 end = ChrPtr(Source) + len;
548 for (pos = ChrPtr(Source); (pos < end) && (start == NULL); ++pos) {
549 if (!strncasecmp(pos, "http://", 7))
551 else if (!strncasecmp(pos, "ftp://", 6))
556 StrBufAppendBuf(Target, Source, 0);
561 for (pos = ChrPtr(Source) + len; pos > start; --pos) {
581 UrlLen = end - start;
582 StrBufAppendBufPlain(WrkBuf, start, UrlLen, 0);
584 Offset = start - ChrPtr(Source);
586 StrBufAppendBufPlain(Target, ChrPtr(Source), Offset, 0);
587 StrBufAppendPrintf(Target, "%ca href=%c%s%c TARGET=%c%s%c%c%s%c/A%c",
588 LB, QU, ChrPtr(WrkBuf), QU, QU, TARGET, QU, RB, ChrPtr(WrkBuf), LB, RB);
590 TrailerLen = StrLength(Source) - (end - ChrPtr(Source));
592 StrBufAppendBufPlain(Target, end, TrailerLen, 0);
596 void url(char *buf, size_t bufsize)
598 int len, UrlLen, Offset, TrailerLen, outpos;
599 char *start, *end, *pos;
606 syslog(LOG_WARNING, "URL: content longer than buffer!");
610 for (pos = buf; (pos < end) && (start == NULL); ++pos) {
611 if (!strncasecmp(pos, "http://", 7))
613 if (!strncasecmp(pos, "ftp://", 6))
620 for (pos = buf + len; pos > start; --pos) {
640 UrlLen = end - start;
641 if (UrlLen > sizeof(urlbuf)) {
642 syslog(LOG_WARNING, "URL: content longer than buffer!");
645 memcpy(urlbuf, start, UrlLen);
646 urlbuf[UrlLen] = '\0';
648 Offset = start - buf;
649 if ((Offset != 0) && (Offset < sizeof(outbuf)))
650 memcpy(outbuf, buf, Offset);
651 outpos = snprintf(&outbuf[Offset], sizeof(outbuf) - Offset,
652 "%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);
653 if (outpos >= sizeof(outbuf) - Offset) {
654 syslog(LOG_WARNING, "URL: content longer than buffer!");
658 TrailerLen = len - (end - start);
660 memcpy(outbuf + Offset + outpos, end, TrailerLen);
661 if (Offset + outpos + TrailerLen > bufsize) {
662 syslog(LOG_WARNING, "URL: content longer than buffer!");
665 memcpy(buf, outbuf, Offset + outpos + TrailerLen);
666 *(buf + Offset + outpos + TrailerLen) = '\0';