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. It runs great on the
8 // Linux operating system (and probably elsewhere). You can use,
9 // copy, and run it under the terms of the GNU General Public
10 // License version 3. Richard Stallman is an asshole communist.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
21 * Strip surrounding single or double quotes from a string.
23 void stripquotes(char *s)
34 if (((s[0] == '\"') && (s[len - 1] == '\"')) || ((s[0] == '\'') && (s[len - 1] == '\''))) {
42 * Check to see if a META tag has overridden the declared MIME character set.
44 * charset Character set name (left unchanged if we don't do anything)
45 * meta_http_equiv Content of the "http-equiv" portion of the META tag
46 * meta_content Content of the "content" portion of the META tag
48 void extract_charset_from_meta(char *charset, char *meta_http_equiv, char *meta_content)
60 if (strcasecmp(meta_http_equiv, "Content-type"))
63 ptr = strchr(meta_content, ';');
67 safestrncpy(buf, ++ptr, sizeof buf);
69 if (!strncasecmp(buf, "charset=", 8)) {
70 strcpy(charset, &buf[8]);
73 * The brain-damaged webmail program in Microsoft Exchange declares
74 * a charset of "unicode" when they really mean "UTF-8". GNU iconv
75 * treats "unicode" as an alias for "UTF-16" so we have to manually
76 * fix this here, otherwise messages generated in Exchange webmail
77 * show up as a big pile of weird characters.
79 if (!strcasecmp(charset, "unicode")) {
80 strcpy(charset, "UTF-8");
83 /* Remove wandering punctuation */
84 if ((ptr = strchr(charset, '\"')))
92 * Sanitize and enhance an HTML message for display.
93 * Also convert weird character sets to UTF-8 if necessary.
94 * Also fixup img src="cid:..." type inline images to fetch the image
97 StrBuf *html2html(const char *supplied_charset, int treat_as_wiki, char *roomname, long msgnum, StrBuf *Source)
104 StrBuf *converted_msg;
105 int buffer_length = 1;
107 int content_length = 0;
108 char new_window[SIZ];
112 int script_start_pos = (-1);
116 StrBuf *BodyArea = NULL;
118 iconv_t ic = (iconv_t) (-1);
119 char *ibuf; /* Buffer of characters to be converted */
120 char *obuf; /* Buffer for converted characters */
121 size_t ibuflen; /* Length of input buffer */
122 size_t obuflen; /* Length of output buffer */
123 char *osav; /* Saved pointer to output buffer */
129 StrBuf *Target = NewStrBuf();
130 if (Target == NULL) {
135 syslog(LOG_DEBUG, "CONVERT: <%s>", ChrPtr(Source));
139 safestrncpy(charset, supplied_charset, sizeof charset);
140 sprintf(new_window, "<a target=\"%s\" href=", TARGET);
142 content_length = StrLength(Source);
143 msg = (char *) ChrPtr(Source);
144 buffer_length = content_length;
146 /* Do a first pass to isolate the message body */
149 msgend = &msg[content_length];
151 while (ptr < msgend) {
153 /* Advance to next tag */
154 ptr = strchr(ptr, '<');
155 if ((ptr == NULL) || (ptr >= msgend))
158 if ((ptr == NULL) || (ptr >= msgend))
162 * Look for META tags. Some messages (particularly in
163 * Asian locales) illegally declare a message's character
164 * set in the HTML instead of in the MIME headers. This
165 * is wrong but we have to work around it anyway.
167 if (!strncasecmp(ptr, "META", 4)) {
173 char *meta_http_equiv;
177 meta_start = &ptr[4];
178 meta_end = strchr(ptr, '>');
179 if ((meta_end != NULL) && (meta_end <= msgend)) {
180 meta_length = meta_end - meta_start + 1;
181 meta = malloc(meta_length + 1);
182 safestrncpy(meta, meta_start, meta_length);
183 meta[meta_length] = 0;
185 if (!strncasecmp(meta, "HTTP-EQUIV=", 11)) {
186 meta_http_equiv = strdup(&meta[11]);
187 spaceptr = strchr(meta_http_equiv, ' ');
188 if (spaceptr != NULL) {
190 meta_content = strdup(++spaceptr);
191 if (!strncasecmp(meta_content, "content=", 8)) {
192 strcpy(meta_content, &meta_content[8]);
193 stripquotes(meta_http_equiv);
194 stripquotes(meta_content);
195 extract_charset_from_meta(charset, meta_http_equiv, meta_content);
199 free(meta_http_equiv);
206 * Any of these tags cause everything up to and including
207 * the tag to be removed.
209 if ((!strncasecmp(ptr, "HTML", 4))
210 || (!strncasecmp(ptr, "HEAD", 4))
211 || (!strncasecmp(ptr, "/HEAD", 5))
212 || (!strncasecmp(ptr, "BODY", 4))) {
215 if (!strncasecmp(ptr, "BODY", 4)) {
218 ptr = strchr(ptr, '>');
219 if ((ptr == NULL) || (ptr >= msgend))
221 if ((pBody != NULL) && (ptr - pBody > 4)) {
223 char *cid_start, *cid_end;
227 while ((isspace(*pBody)) && (pBody < ptr))
229 BodyArea = NewStrBufPlain(NULL, ptr - pBody);
232 src = strstr(pBody, "cid:");
236 while ((*cid_end != '"') && !isspace(*cid_end) && (cid_end < ptr))
239 /* copy tag and attributes up to src="cid: */
240 StrBufAppendBufPlain(BodyArea, pBody, src - pBody, 0);
242 /* add in /webcit/mimepart/<msgno>/CID/
243 trailing / stops dumb URL filters getting excited */
244 StrBufAppendPrintf(BodyArea, "/webcit/mimepart/%ld/", msgnum);
245 StrBufAppendBufPlain(BodyArea, cid_start, cid_end - cid_start, 0);
247 if (ptr - cid_end > 0)
248 StrBufAppendBufPlain(BodyArea, cid_end + 1, ptr - cid_end, 0);
250 StrBufAppendBufPlain(BodyArea, pBody, ptr - pBody, 0);
255 if ((ptr == NULL) || (ptr >= msgend))
261 * Any of these tags cause everything including and following
262 * the tag to be removed.
264 if ((!strncasecmp(ptr, "/HTML", 5)) || (!strncasecmp(ptr, "/BODY", 5))) {
272 if (msgstart > msg) {
273 strcpy(msg, msgstart);
276 /* Now go through the message, parsing tags as necessary. */
277 converted_msg = NewStrBufPlain(NULL, content_length + 8192);
279 /* Convert foreign character sets to UTF-8 if necessary. */
280 if ((strcasecmp(charset, "us-ascii"))
281 && (strcasecmp(charset, "UTF-8"))
282 && (strcasecmp(charset, ""))
284 syslog(LOG_DEBUG, "Converting %s to UTF-8", charset);
285 ctdl_iconv_open("UTF-8", charset, &ic);
286 if (ic == (iconv_t) (-1)) {
287 syslog(LOG_WARNING, "%s:%d iconv_open() failed: %s", __FILE__, __LINE__, strerror(errno));
290 if (Source == NULL) {
291 if (ic != (iconv_t) (-1)) {
293 ibuflen = content_length;
294 obuflen = content_length + (content_length / 2);
295 obuf = (char *) malloc(obuflen);
297 iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
298 content_length = content_length + (content_length / 2) - obuflen;
299 osav[content_length] = 0;
305 if (ic != (iconv_t) (-1)) {
306 StrBuf *Buf = NewStrBufPlain(NULL, StrLength(Source) + 8096);;
307 StrBufConvert(Source, Buf, &ic);
310 msg = (char *) ChrPtr(Source); /* TODO: get rid of this. */
315 * At this point, the message has been stripped down to
316 * only the content inside the <BODY></BODY> tags, and has
317 * been converted to UTF-8 if it was originally in a foreign
318 * character set. The text is also guaranteed to be null
322 if (converted_msg == NULL) {
323 StrBufAppendPrintf(Target, "Error %d: %s<br>%s:%d", errno, strerror(errno), __FILE__, __LINE__);
327 if (BodyArea != NULL) { // Any attributes that were declared in the <body> tag
328 StrBufAppendBufPlain(converted_msg, HKEY("<div "), 0); // are instead declared in this <div> tag
329 StrBufAppendBuf(converted_msg, BodyArea, 0);
330 StrBufAppendBufPlain(converted_msg, HKEY(">"), 0);
333 msgend = strchr(msg, 0);
334 while (ptr < msgend) {
336 /* Try to sanitize the html of any rogue scripts */
337 if (!strncasecmp(ptr, "<script", 7)) {
338 if (scriptlevel == 0) {
339 script_start_pos = StrLength(converted_msg);
343 if (!strncasecmp(ptr, "</script", 8)) {
348 * Change mailto: links to WebCit mail, by replacing the
349 * link with one that points back to our mail room. Due to
350 * the way we parse URL's, it'll even handle mailto: links
351 * that have "?subject=" in them.
353 if (!strncasecmp(ptr, "<a href=\"mailto:", 16)) {
354 content_length += 64;
355 StrBufAppendPrintf(converted_msg, "<a href=\"display_enter?force_room=_MAIL_?recp="); // FIXME make compatible with webcit-ng
360 /* Make external links open in a separate window */
361 else if (!strncasecmp(ptr, "<a href=\"", 9)) {
364 if (((strchr(ptr, ':') < strchr(ptr, '/'))) && ((strchr(ptr, '/') < strchr(ptr, '>')))) {
365 /* open external links to new window */
366 StrBufAppendPrintf(converted_msg, new_window);
368 } else if ((treat_as_wiki)
369 && (strncasecmp(ptr, "<a href=\"wiki?", 14))
370 && (strncasecmp(ptr, "<a href=\"dotgoto?", 17))
371 && (strncasecmp(ptr, "<a href=\"knrooms?", 17))
373 content_length += 64;
374 StrBufAppendPrintf(converted_msg, "<a href=\"wiki?go=");
375 //StrBufUrlescAppend(converted_msg, "FIXME ROOM NAME", NULL); // FIXME make compatible with webcit-ng
376 StrBufAppendPrintf(converted_msg, "?page=");
379 StrBufAppendPrintf(converted_msg, "<a href=\"");
383 /* Fixup <img src="cid:... ...> to fetch the mime part */
384 else if (!strncasecmp(ptr, "<img ", 5)) {
385 char *cid_start, *cid_end;
386 char *tag_end = strchr(ptr, '>');
388 /* FIXME - handle this situation (maybe someone opened an <img cid...
389 * and then ended the message)
392 syslog(LOG_DEBUG, "tag_end is null and ptr is:");
393 syslog(LOG_DEBUG, "%s", ptr);
394 syslog(LOG_DEBUG, "Theoretical bytes remaining: %d", (int) (msgend - ptr));
397 src = strstr(ptr, "src=\"cid:");
400 if (src && isspace(*(src - 1))
401 && tag_end && (cid_start = strchr(src, ':'))
402 && (cid_end = strchr(cid_start, '"'))
403 && (cid_end < tag_end)
405 /* copy tag and attributes up to src="cid: */
406 StrBufAppendBufPlain(converted_msg, ptr, src - ptr, 0);
409 /* add in /webcit/mimepart/<msgnum>/CID/
410 trailing / stops dumb URL filters getting excited */
411 StrBufAppendPrintf(converted_msg, " src=\"/ctdl/r/");
412 StrBufXMLEscAppend(converted_msg, NULL, roomname, strlen(roomname), 0);
413 syslog(LOG_DEBUG, "room name is '%s'", roomname);
414 StrBufAppendPrintf(converted_msg, "/%ld/", msgnum);
415 StrBufAppendBufPlain(converted_msg, cid_start, cid_end - cid_start, 0);
416 StrBufAppendBufPlain(converted_msg, "\"", -1, 0);
419 StrBufAppendBufPlain(converted_msg, ptr, tag_end - ptr, 0);
424 * Turn anything that looks like a URL into a real link, as long
425 * as it's not inside a tag already
427 else if ((brak == 0) && (alevel == 0) && ((!strncasecmp(ptr, "http://", 7)) || (!strncasecmp(ptr, "https://", 8)))) {
428 /* Find the end of the link */
432 strlenptr = strlen(ptr);
433 for (i = 0; i <= strlenptr; ++i) {
450 if ((ptr[i + 2] == ';') ||
451 (ptr[i + 3] == ';') ||
452 (ptr[i + 5] == ';') || (ptr[i + 6] == ';') || (ptr[i + 7] == ';'))
465 linkedchar = ptr[len];
467 /* spot for some subject strings tinymce tends to give us. */
468 ltreviewptr = strchr(ptr, '<');
469 if (ltreviewptr != NULL) {
471 linklen = ltreviewptr - ptr;
474 nbspreviewptr = strstr(ptr, " ");
475 if (nbspreviewptr != NULL) {
476 /* nbspreviewptr = '\0'; */
477 linklen = nbspreviewptr - ptr;
479 if (ltreviewptr != 0)
482 ptr[len] = linkedchar;
484 content_length += (32 + linklen);
485 StrBufAppendPrintf(converted_msg, "%s\"", new_window);
486 StrBufAppendBufPlain(converted_msg, ptr, linklen, 0);
487 StrBufAppendPrintf(converted_msg, "\">");
488 StrBufAppendBufPlain(converted_msg, ptr, linklen, 0);
490 StrBufAppendPrintf(converted_msg, "</a>");
493 StrBufAppendBufPlain(converted_msg, ptr, 1, 0);
497 if ((ptr >= msg) && (ptr <= msgend)) {
499 * We need to know when we're inside a tag,
500 * so we don't turn things that look like URL's into
501 * links, when they're already links - or image sources.
503 if ((ptr > msg) && (*(ptr - 1) == '<')) {
506 if ((ptr > msg) && (*(ptr - 1) == '>')) {
508 if ((scriptlevel == 0) && (script_start_pos >= 0)) {
509 StrBufCutRight(converted_msg, StrLength(converted_msg) - script_start_pos);
510 script_start_pos = (-1);
513 if (!strncasecmp(ptr, "</a>", 3))
518 if (BodyArea != NULL) {
519 StrBufAppendBufPlain(converted_msg, HKEY("</div>"), 0); // Close the div where we declared attributes copied
520 FreeStrBuf(&BodyArea); // from the original <body> tag
523 /* uncomment these two lines to override conversion */
524 /* memcpy(converted_msg, msg, content_length); */
525 /* output_length = content_length; */
527 /* Output our big pile of markup */
528 StrBufAppendBuf(Target, converted_msg, 0);
530 BAIL: /* A little trailing vertical whitespace... */
531 StrBufAppendPrintf(Target, "<br>\n");
533 /* Now give back the memory */
534 FreeStrBuf(&converted_msg);
535 if ((msg != NULL) && (Source == NULL))
542 * Look for URL's embedded in a buffer and make them linkable. We use a
543 * target window in order to keep the Citadel session in its own window.
545 void UrlizeText(StrBuf * Target, StrBuf * Source, StrBuf * WrkBuf)
547 int len, UrlLen, Offset, TrailerLen;
548 const char *start, *end, *pos;
552 len = StrLength(Source);
553 end = ChrPtr(Source) + len;
554 for (pos = ChrPtr(Source); (pos < end) && (start == NULL); ++pos) {
555 if (!strncasecmp(pos, "http://", 7))
557 else if (!strncasecmp(pos, "ftp://", 6))
562 StrBufAppendBuf(Target, Source, 0);
567 for (pos = ChrPtr(Source) + len; pos > start; --pos) {
587 UrlLen = end - start;
588 StrBufAppendBufPlain(WrkBuf, start, UrlLen, 0);
590 Offset = start - ChrPtr(Source);
592 StrBufAppendBufPlain(Target, ChrPtr(Source), Offset, 0);
593 StrBufAppendPrintf(Target, "%ca href=%c%s%c TARGET=%c%s%c%c%s%c/A%c",
594 LB, QU, ChrPtr(WrkBuf), QU, QU, TARGET, QU, RB, ChrPtr(WrkBuf), LB, RB);
596 TrailerLen = StrLength(Source) - (end - ChrPtr(Source));
598 StrBufAppendBufPlain(Target, end, TrailerLen, 0);
602 void url(char *buf, size_t bufsize)
604 int len, UrlLen, Offset, TrailerLen, outpos;
605 char *start, *end, *pos;
612 syslog(LOG_WARNING, "URL: content longer than buffer!");
616 for (pos = buf; (pos < end) && (start == NULL); ++pos) {
617 if (!strncasecmp(pos, "http://", 7))
619 if (!strncasecmp(pos, "ftp://", 6))
626 for (pos = buf + len; pos > start; --pos) {
646 UrlLen = end - start;
647 if (UrlLen > sizeof(urlbuf)) {
648 syslog(LOG_WARNING, "URL: content longer than buffer!");
651 memcpy(urlbuf, start, UrlLen);
652 urlbuf[UrlLen] = '\0';
654 Offset = start - buf;
655 if ((Offset != 0) && (Offset < sizeof(outbuf)))
656 memcpy(outbuf, buf, Offset);
657 outpos = snprintf(&outbuf[Offset], sizeof(outbuf) - Offset,
658 "%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);
659 if (outpos >= sizeof(outbuf) - Offset) {
660 syslog(LOG_WARNING, "URL: content longer than buffer!");
664 TrailerLen = len - (end - start);
666 memcpy(outbuf + Offset + outpos, end, TrailerLen);
667 if (Offset + outpos + TrailerLen > bufsize) {
668 syslog(LOG_WARNING, "URL: content longer than buffer!");
671 memcpy(buf, outbuf, Offset + outpos + TrailerLen);
672 *(buf + Offset + outpos + TrailerLen) = '\0';