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-2020 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
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 */
125 StrBuf *Target = NewStrBuf();
126 if (Target == NULL) {
130 safestrncpy(charset, supplied_charset, sizeof charset);
131 sprintf(new_window, "<a target=\"%s\" href=", TARGET);
133 content_length = StrLength(Source);
134 msg = (char *) ChrPtr(Source);
135 buffer_length = content_length;
137 /* Do a first pass to isolate the message body */
140 msgend = &msg[content_length];
142 while (ptr < msgend) {
144 /* Advance to next tag */
145 ptr = strchr(ptr, '<');
146 if ((ptr == NULL) || (ptr >= msgend))
149 if ((ptr == NULL) || (ptr >= msgend))
153 * Look for META tags. Some messages (particularly in
154 * Asian locales) illegally declare a message's character
155 * set in the HTML instead of in the MIME headers. This
156 * is wrong but we have to work around it anyway.
158 if (!strncasecmp(ptr, "META", 4)) {
164 char *meta_http_equiv;
168 meta_start = &ptr[4];
169 meta_end = strchr(ptr, '>');
170 if ((meta_end != NULL) && (meta_end <= msgend)) {
171 meta_length = meta_end - meta_start + 1;
172 meta = malloc(meta_length + 1);
173 safestrncpy(meta, meta_start, meta_length);
174 meta[meta_length] = 0;
176 if (!strncasecmp(meta, "HTTP-EQUIV=", 11)) {
177 meta_http_equiv = strdup(&meta[11]);
178 spaceptr = strchr(meta_http_equiv, ' ');
179 if (spaceptr != NULL) {
181 meta_content = strdup(++spaceptr);
182 if (!strncasecmp(meta_content, "content=", 8)) {
183 strcpy(meta_content, &meta_content[8]);
184 stripquotes(meta_http_equiv);
185 stripquotes(meta_content);
186 extract_charset_from_meta(charset, meta_http_equiv, meta_content);
190 free(meta_http_equiv);
197 * Any of these tags cause everything up to and including
198 * the tag to be removed.
200 if ((!strncasecmp(ptr, "HTML", 4))
201 || (!strncasecmp(ptr, "HEAD", 4))
202 || (!strncasecmp(ptr, "/HEAD", 5))
203 || (!strncasecmp(ptr, "BODY", 4))) {
206 if (!strncasecmp(ptr, "BODY", 4)) {
209 ptr = strchr(ptr, '>');
210 if ((ptr == NULL) || (ptr >= msgend))
212 if ((pBody != NULL) && (ptr - pBody > 4)) {
214 char *cid_start, *cid_end;
218 while ((isspace(*pBody)) && (pBody < ptr))
220 BodyArea = NewStrBufPlain(NULL, ptr - pBody);
223 src = strstr(pBody, "cid:");
227 while ((*cid_end != '"') && !isspace(*cid_end) && (cid_end < ptr))
230 /* copy tag and attributes up to src="cid: */
231 StrBufAppendBufPlain(BodyArea, pBody, src - pBody, 0);
233 /* add in /webcit/mimepart/<msgno>/CID/
234 trailing / stops dumb URL filters getting excited */
235 StrBufAppendPrintf(BodyArea, "/webcit/mimepart/%ld/", msgnum);
236 StrBufAppendBufPlain(BodyArea, cid_start, cid_end - cid_start, 0);
238 if (ptr - cid_end > 0)
239 StrBufAppendBufPlain(BodyArea, cid_end + 1, ptr - cid_end, 0);
241 StrBufAppendBufPlain(BodyArea, pBody, ptr - pBody, 0);
246 if ((ptr == NULL) || (ptr >= msgend))
252 * Any of these tags cause everything including and following
253 * the tag to be removed.
255 if ((!strncasecmp(ptr, "/HTML", 5)) || (!strncasecmp(ptr, "/BODY", 5))) {
263 if (msgstart > msg) {
264 strcpy(msg, msgstart);
267 /* Now go through the message, parsing tags as necessary. */
268 converted_msg = NewStrBufPlain(NULL, content_length + 8192);
270 /* Convert foreign character sets to UTF-8 if necessary. */
271 if ((strcasecmp(charset, "us-ascii"))
272 && (strcasecmp(charset, "UTF-8"))
273 && (strcasecmp(charset, ""))
275 syslog(LOG_DEBUG, "Converting %s to UTF-8", charset);
276 ctdl_iconv_open("UTF-8", charset, &ic);
277 if (ic == (iconv_t) (-1)) {
278 syslog(LOG_WARNING, "%s:%d iconv_open() failed: %s", __FILE__, __LINE__, strerror(errno));
281 if (Source == NULL) {
282 if (ic != (iconv_t) (-1)) {
284 ibuflen = content_length;
285 obuflen = content_length + (content_length / 2);
286 obuf = (char *) malloc(obuflen);
288 iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
289 content_length = content_length + (content_length / 2) - obuflen;
290 osav[content_length] = 0;
296 if (ic != (iconv_t) (-1)) {
297 StrBuf *Buf = NewStrBufPlain(NULL, StrLength(Source) + 8096);;
298 StrBufConvert(Source, Buf, &ic);
301 msg = (char *) ChrPtr(Source); /* TODO: get rid of this. */
306 * At this point, the message has been stripped down to
307 * only the content inside the <BODY></BODY> tags, and has
308 * been converted to UTF-8 if it was originally in a foreign
309 * character set. The text is also guaranteed to be null
313 if (converted_msg == NULL) {
314 StrBufAppendPrintf(Target, "Error %d: %s<br>%s:%d", errno, strerror(errno), __FILE__, __LINE__);
318 if (BodyArea != NULL) { // Any attributes that were declared in the <body> tag
319 StrBufAppendBufPlain(converted_msg, HKEY("<div "), 0); // are instead declared in this <div> tag
320 StrBufAppendBuf(converted_msg, BodyArea, 0);
321 StrBufAppendBufPlain(converted_msg, HKEY(">"), 0);
324 msgend = strchr(msg, 0);
325 while (ptr < msgend) {
327 /* Try to sanitize the html of any rogue scripts */
328 if (!strncasecmp(ptr, "<script", 7)) {
329 if (scriptlevel == 0) {
330 script_start_pos = StrLength(converted_msg);
334 if (!strncasecmp(ptr, "</script", 8)) {
339 * Change mailto: links to WebCit mail, by replacing the
340 * link with one that points back to our mail room. Due to
341 * the way we parse URL's, it'll even handle mailto: links
342 * that have "?subject=" in them.
344 if (!strncasecmp(ptr, "<a href=\"mailto:", 16)) {
345 content_length += 64;
346 StrBufAppendPrintf(converted_msg, "<a href=\"display_enter?force_room=_MAIL_?recp="); // FIXME make compatible with webcit-ng
351 /* Make external links open in a separate window */
352 else if (!strncasecmp(ptr, "<a href=\"", 9)) {
355 if (((strchr(ptr, ':') < strchr(ptr, '/'))) && ((strchr(ptr, '/') < strchr(ptr, '>')))) {
356 /* open external links to new window */
357 StrBufAppendPrintf(converted_msg, new_window);
359 } else if ((treat_as_wiki)
360 && (strncasecmp(ptr, "<a href=\"wiki?", 14))
361 && (strncasecmp(ptr, "<a href=\"dotgoto?", 17))
362 && (strncasecmp(ptr, "<a href=\"knrooms?", 17))
364 content_length += 64;
365 StrBufAppendPrintf(converted_msg, "<a href=\"wiki?go=");
366 //StrBufUrlescAppend(converted_msg, "FIXME ROOM NAME", NULL); // FIXME make compatible with webcit-ng
367 StrBufAppendPrintf(converted_msg, "?page=");
370 StrBufAppendPrintf(converted_msg, "<a href=\"");
374 /* Fixup <img src="cid:... ...> to fetch the mime part */
375 else if (!strncasecmp(ptr, "<img ", 5)) {
376 char *cid_start, *cid_end;
377 char *tag_end = strchr(ptr, '>');
379 /* FIXME - handle this situation (maybe someone opened an <img cid...
380 * and then ended the message)
383 syslog(LOG_DEBUG, "tag_end is null and ptr is:");
384 syslog(LOG_DEBUG, "%s", ptr);
385 syslog(LOG_DEBUG, "Theoretical bytes remaining: %d", (int) (msgend - ptr));
388 src = strstr(ptr, "src=\"cid:");
391 if (src && isspace(*(src - 1))
392 && tag_end && (cid_start = strchr(src, ':'))
393 && (cid_end = strchr(cid_start, '"'))
394 && (cid_end < tag_end)
396 /* copy tag and attributes up to src="cid: */
397 StrBufAppendBufPlain(converted_msg, ptr, src - ptr, 0);
400 /* add in /webcit/mimepart/<msgnum>/CID/
401 trailing / stops dumb URL filters getting excited */
402 StrBufAppendPrintf(converted_msg, " src=\"/ctdl/r/");
403 StrBufXMLEscAppend(converted_msg, NULL, roomname, strlen(roomname), 0);
404 syslog(LOG_DEBUG, "room name is '%s'", roomname);
405 StrBufAppendPrintf(converted_msg, "/%ld/", msgnum);
406 StrBufAppendBufPlain(converted_msg, cid_start, cid_end - cid_start, 0);
407 StrBufAppendBufPlain(converted_msg, "\"", -1, 0);
410 StrBufAppendBufPlain(converted_msg, ptr, tag_end - ptr, 0);
415 * Turn anything that looks like a URL into a real link, as long
416 * as it's not inside a tag already
418 else if ((brak == 0) && (alevel == 0) && ((!strncasecmp(ptr, "http://", 7)) || (!strncasecmp(ptr, "https://", 8)))) {
419 /* Find the end of the link */
423 strlenptr = strlen(ptr);
424 for (i = 0; i <= strlenptr; ++i) {
441 if ((ptr[i + 2] == ';') ||
442 (ptr[i + 3] == ';') ||
443 (ptr[i + 5] == ';') || (ptr[i + 6] == ';') || (ptr[i + 7] == ';'))
456 linkedchar = ptr[len];
458 /* spot for some subject strings tinymce tends to give us. */
459 ltreviewptr = strchr(ptr, '<');
460 if (ltreviewptr != NULL) {
462 linklen = ltreviewptr - ptr;
465 nbspreviewptr = strstr(ptr, " ");
466 if (nbspreviewptr != NULL) {
467 /* nbspreviewptr = '\0'; */
468 linklen = nbspreviewptr - ptr;
470 if (ltreviewptr != 0)
473 ptr[len] = linkedchar;
475 content_length += (32 + linklen);
476 StrBufAppendPrintf(converted_msg, "%s\"", new_window);
477 StrBufAppendBufPlain(converted_msg, ptr, linklen, 0);
478 StrBufAppendPrintf(converted_msg, "\">");
479 StrBufAppendBufPlain(converted_msg, ptr, linklen, 0);
481 StrBufAppendPrintf(converted_msg, "</a>");
484 StrBufAppendBufPlain(converted_msg, ptr, 1, 0);
488 if ((ptr >= msg) && (ptr <= msgend)) {
490 * We need to know when we're inside a tag,
491 * so we don't turn things that look like URL's into
492 * links, when they're already links - or image sources.
494 if ((ptr > msg) && (*(ptr - 1) == '<')) {
497 if ((ptr > msg) && (*(ptr - 1) == '>')) {
499 if ((scriptlevel == 0) && (script_start_pos >= 0)) {
500 StrBufCutRight(converted_msg, StrLength(converted_msg) - script_start_pos);
501 script_start_pos = (-1);
504 if (!strncasecmp(ptr, "</a>", 3))
509 if (BodyArea != NULL) {
510 StrBufAppendBufPlain(converted_msg, HKEY("</div>"), 0); // Close the div where we declared attributes copied
511 FreeStrBuf(&BodyArea); // from the original <body> tag
514 /* uncomment these two lines to override conversion */
515 /* memcpy(converted_msg, msg, content_length); */
516 /* output_length = content_length; */
518 /* Output our big pile of markup */
519 StrBufAppendBuf(Target, converted_msg, 0);
521 BAIL: /* A little trailing vertical whitespace... */
522 StrBufAppendPrintf(Target, "<br>\n");
524 /* Now give back the memory */
525 FreeStrBuf(&converted_msg);
526 if ((msg != NULL) && (Source == NULL))
533 * Look for URL's embedded in a buffer and make them linkable. We use a
534 * target window in order to keep the Citadel session in its own window.
536 void UrlizeText(StrBuf * Target, StrBuf * Source, StrBuf * WrkBuf)
538 int len, UrlLen, Offset, TrailerLen;
539 const char *start, *end, *pos;
543 len = StrLength(Source);
544 end = ChrPtr(Source) + len;
545 for (pos = ChrPtr(Source); (pos < end) && (start == NULL); ++pos) {
546 if (!strncasecmp(pos, "http://", 7))
548 else if (!strncasecmp(pos, "ftp://", 6))
553 StrBufAppendBuf(Target, Source, 0);
558 for (pos = ChrPtr(Source) + len; pos > start; --pos) {
578 UrlLen = end - start;
579 StrBufAppendBufPlain(WrkBuf, start, UrlLen, 0);
581 Offset = start - ChrPtr(Source);
583 StrBufAppendBufPlain(Target, ChrPtr(Source), Offset, 0);
584 StrBufAppendPrintf(Target, "%ca href=%c%s%c TARGET=%c%s%c%c%s%c/A%c",
585 LB, QU, ChrPtr(WrkBuf), QU, QU, TARGET, QU, RB, ChrPtr(WrkBuf), LB, RB);
587 TrailerLen = StrLength(Source) - (end - ChrPtr(Source));
589 StrBufAppendBufPlain(Target, end, TrailerLen, 0);
593 void url(char *buf, size_t bufsize)
595 int len, UrlLen, Offset, TrailerLen, outpos;
596 char *start, *end, *pos;
603 syslog(LOG_WARNING, "URL: content longer than buffer!");
607 for (pos = buf; (pos < end) && (start == NULL); ++pos) {
608 if (!strncasecmp(pos, "http://", 7))
610 if (!strncasecmp(pos, "ftp://", 6))
617 for (pos = buf + len; pos > start; --pos) {
637 UrlLen = end - start;
638 if (UrlLen > sizeof(urlbuf)) {
639 syslog(LOG_WARNING, "URL: content longer than buffer!");
642 memcpy(urlbuf, start, UrlLen);
643 urlbuf[UrlLen] = '\0';
645 Offset = start - buf;
646 if ((Offset != 0) && (Offset < sizeof(outbuf)))
647 memcpy(outbuf, buf, Offset);
648 outpos = snprintf(&outbuf[Offset], sizeof(outbuf) - Offset,
649 "%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);
650 if (outpos >= sizeof(outbuf) - Offset) {
651 syslog(LOG_WARNING, "URL: content longer than buffer!");
655 TrailerLen = len - (end - start);
657 memcpy(outbuf + Offset + outpos, end, TrailerLen);
658 if (Offset + outpos + TrailerLen > bufsize) {
659 syslog(LOG_WARNING, "URL: content longer than buffer!");
662 memcpy(buf, outbuf, Offset + outpos + TrailerLen);
663 *(buf + Offset + outpos + TrailerLen) = '\0';