5 * \defgroup HTML2HTML Output an HTML message, modifying it slightly to make sure it plays nice
6 * with the rest of our web framework.
7 * \ingroup WebcitHttpServer
11 #include "webserver.h"
15 * \brief Strip surrounding single or double quotes from a string.
17 * \param s String to be stripped.
19 void stripquotes(char *s)
28 if ( ( (s[0] == '\"') && (s[len-1] == '\"') ) || ( (s[0] == '\'') && (s[len-1] == '\'') ) ) {
36 * \brief Check to see if a META tag has overridden the declared MIME character set.
38 * \param charset Character set name (left unchanged if we don't do anything)
39 * \param meta_http_equiv Content of the "http-equiv" portion of the META tag
40 * \param meta_content Content of the "content" portion of the META tag
42 void extract_charset_from_meta(char *charset, char *meta_http_equiv, char *meta_content)
48 if (!meta_http_equiv) return;
49 if (!meta_content) return;
52 if (strcasecmp(meta_http_equiv, "Content-type")) return;
54 ptr = strchr(meta_content, ';');
57 safestrncpy(buf, ++ptr, sizeof buf);
59 if (!strncasecmp(buf, "charset=", 8)) {
60 strcpy(charset, &buf[8]);
63 * The brain-damaged webmail program in Microsoft Exchange declares
64 * a charset of "unicode" when they really mean "UTF-8". GNU iconv
65 * treats "unicode" as an alias for "UTF-16" so we have to manually
66 * fix this here, otherwise messages generated in Exchange webmail
67 * show up as a big pile of weird characters.
69 if (!strcasecmp(charset, "unicode")) {
70 strcpy(charset, "UTF-8");
73 /* Remove wandering punctuation */
74 if ((ptr=strchr(charset, '\"'))) *ptr = 0;
82 * \brief Sanitize and enhance an HTML message for display.
83 * Also convert weird character sets to UTF-8 if necessary.
84 * Also fixup img src="cid:..." type inline images to fetch the image
86 * \param supplied_charset the input charset as declared in the MIME headers
88 void output_html(const char *supplied_charset, int treat_as_wiki, int msgnum, StrBuf *Source, StrBuf *Target) {
94 StrBuf *converted_msg;
95 int buffer_length = 1;
97 int content_length = 0;
102 int script_start_pos = (-1);
106 StrBuf *BodyArea = NULL;
108 iconv_t ic = (iconv_t)(-1) ;
109 char *ibuf; /**< Buffer of characters to be converted */
110 char *obuf; /**< Buffer for converted characters */
111 size_t ibuflen; /**< Length of input buffer */
112 size_t obuflen; /**< Length of output buffer */
113 char *osav; /**< Saved pointer to output buffer */
118 safestrncpy(charset, supplied_charset, sizeof charset);
120 sprintf(new_window, "<a target=\"%s\" href=", TARGET);
122 if (Source == NULL) while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
123 line_length = strlen(buf);
124 buffer_length = content_length + line_length + 2;
125 ptr = realloc(msg, buffer_length);
127 StrBufAppendPrintf(Target, "<b>");
128 StrBufAppendPrintf(Target, _("realloc() error! couldn't get %d bytes: %s"),
131 StrBufAppendPrintf(Target, "</b><br /><br />\n");
132 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
139 strcpy(&msg[content_length], buf);
140 content_length += line_length;
141 strcpy(&msg[content_length], "\n");
145 content_length = StrLength(Source);
147 msg = (char*) ChrPtr(Source);/* TODO: remove cast */
148 buffer_length = content_length;
151 /** Do a first pass to isolate the message body */
154 msgend = &msg[content_length];
156 while (ptr < msgend) {
158 /** Advance to next tag */
159 ptr = strchr(ptr, '<');
160 if ((ptr == NULL) || (ptr >= msgend)) break;
162 if ((ptr == NULL) || (ptr >= msgend)) break;
165 * Look for META tags. Some messages (particularly in
166 * Asian locales) illegally declare a message's character
167 * set in the HTML instead of in the MIME headers. This
168 * is wrong but we have to work around it anyway.
170 if (!strncasecmp(ptr, "META", 4)) {
176 char *meta_http_equiv;
180 meta_start = &ptr[4];
181 meta_end = strchr(ptr, '>');
182 if ((meta_end != NULL) && (meta_end <= msgend)) {
183 meta_length = meta_end - meta_start + 1;
184 meta = malloc(meta_length + 1);
185 safestrncpy(meta, meta_start, meta_length);
186 meta[meta_length] = 0;
188 if (!strncasecmp(meta, "HTTP-EQUIV=", 11)) {
189 meta_http_equiv = strdup(&meta[11]);
190 spaceptr = strchr(meta_http_equiv, ' ');
191 if (spaceptr != NULL) {
193 meta_content = strdup(++spaceptr);
194 if (!strncasecmp(meta_content, "content=", 8)) {
195 strcpy(meta_content, &meta_content[8]);
196 stripquotes(meta_http_equiv);
197 stripquotes(meta_content);
198 extract_charset_from_meta(charset,
199 meta_http_equiv, meta_content);
203 free(meta_http_equiv);
210 * Any of these tags cause everything up to and including
211 * the tag to be removed.
213 if ( (!strncasecmp(ptr, "HTML", 4))
214 ||(!strncasecmp(ptr, "HEAD", 4))
215 ||(!strncasecmp(ptr, "/HEAD", 5))
216 ||(!strncasecmp(ptr, "BODY", 4)) ) {
219 if (!strncasecmp(ptr, "BODY", 4)) {
222 ptr = strchr(ptr, '>');
223 if ((ptr == NULL) || (ptr >= msgend)) break;
224 if ((pBody != NULL) && (ptr - pBody > 4)) {
226 char *cid_start, *cid_end;
230 while ((isspace(*pBody)) && (pBody < ptr))
232 BodyArea = NewStrBufPlain(NULL, ptr - pBody);
235 src = strstr(pBody, "cid:");
239 while ((*cid_end != '"') &&
240 !isspace(*cid_end) &&
244 /* copy tag and attributes up to src="cid: */
245 StrBufAppendBufPlain(BodyArea, pBody, src - pBody, 0);
247 /* add in /webcit/mimepart/<msgno>/CID/
248 trailing / stops dumb URL filters getting excited */
249 StrBufAppendPrintf(BodyArea,
250 "/webcit/mimepart/%d/",msgnum);
251 StrBufAppendBufPlain(BodyArea, cid_start, cid_end - cid_start, 0);
253 if (ptr - cid_end > 0)
254 StrBufAppendBufPlain(BodyArea,
259 StrBufAppendBufPlain(BodyArea, pBody, ptr - pBody, 0);
264 if ((ptr == NULL) || (ptr >= msgend)) break;
269 * Any of these tags cause everything including and following
270 * the tag to be removed.
272 if ( (!strncasecmp(ptr, "/HTML", 5))
273 ||(!strncasecmp(ptr, "/BODY", 5)) ) {
282 if (msgstart > msg) {
283 strcpy(msg, msgstart);
286 /** Now go through the message, parsing tags as necessary. */
287 converted_msg = NewStrBufPlain(NULL, content_length + 8192);
290 /** Convert foreign character sets to UTF-8 if necessary. */
292 if ( (strcasecmp(charset, "us-ascii"))
293 && (strcasecmp(charset, "UTF-8"))
294 && (strcasecmp(charset, ""))
296 lprintf(9, "Converting %s to UTF-8\n", charset);
297 ctdl_iconv_open("UTF-8", charset, &ic);
298 if (ic == (iconv_t)(-1) ) {
299 lprintf(5, "%s:%d iconv_open() failed: %s\n",
300 __FILE__, __LINE__, strerror(errno));
303 if (Source == NULL) {
304 if (ic != (iconv_t)(-1) ) {
306 ibuflen = content_length;
307 obuflen = content_length + (content_length / 2) ;
308 obuf = (char *) malloc(obuflen);
310 iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
311 content_length = content_length + (content_length / 2) - obuflen;
312 osav[content_length] = 0;
319 if (ic != (iconv_t)(-1) ) {
320 StrBuf *Buf = NewStrBufPlain(NULL, StrLength(Source) + 8096);;
321 StrBufConvert(Source, Buf, &ic);
324 msg = (char*)ChrPtr(Source); /* TODO: get rid of this. */
331 * At this point, the message has been stripped down to
332 * only the content inside the <BODY></BODY> tags, and has
333 * been converted to UTF-8 if it was originally in a foreign
334 * character set. The text is also guaranteed to be null
338 if (converted_msg == NULL) {
339 StrBufAppendPrintf(Target, "Error %d: %s<br />%s:%d", errno, strerror(errno), __FILE__, __LINE__);
343 if (BodyArea != NULL) {
344 StrBufAppendBufPlain(converted_msg, HKEY("<table "), 0);
345 StrBufAppendBuf(converted_msg, BodyArea, 0);
346 StrBufAppendBufPlain(converted_msg, HKEY(" width=\"100%\"><tr><td>"), 0);
349 msgend = strchr(msg, 0);
350 while (ptr < msgend) {
352 /** Try to sanitize the html of any rogue scripts */
353 if (!strncasecmp(ptr, "<script", 7)) {
354 if (scriptlevel == 0) {
355 script_start_pos = StrLength(converted_msg);
359 if (!strncasecmp(ptr, "</script", 8)) {
364 * Change mailto: links to WebCit mail, by replacing the
365 * link with one that points back to our mail room. Due to
366 * the way we parse URL's, it'll even handle mailto: links
367 * that have "?subject=" in them.
369 if (!strncasecmp(ptr, "<a href=\"mailto:", 16)) {
370 content_length += 64;
371 StrBufAppendPrintf(converted_msg,
372 "<a href=\"display_enter?force_room=_MAIL_&recp=");
377 /** Make external links open in a separate window */
378 else if (!strncasecmp(ptr, "<a href=\"", 9)) {
381 if ( ((strchr(ptr, ':') < strchr(ptr, '/')))
382 && ((strchr(ptr, '/') < strchr(ptr, '>')))
384 /* open external links to new window */
385 StrBufAppendPrintf(converted_msg, new_window);
388 else if ( (treat_as_wiki) && (strncasecmp(ptr, "<a href=\"wiki?", 14)) ) {
389 content_length += 64;
390 StrBufAppendPrintf(converted_msg, "<a href=\"wiki?page=");
394 StrBufAppendPrintf(converted_msg, "<a href=\"");
398 /** Fixup <img src="cid:... ...> to fetch the mime part */
399 else if (!strncasecmp(ptr, "<img ", 5)) {
400 char *cid_start, *cid_end;
401 char* tag_end=strchr(ptr,'>');
403 /* FIXME - handle this situation (maybe someone opened an <img cid...
404 * and then ended the message)
407 lprintf(9, "tag_end is null and ptr is:\n");
408 lprintf(9, "%s\n", ptr);
409 lprintf(9, "Theoretical bytes remaining: %d\n", msgend - ptr);
412 src=strstr(ptr, " src=\"cid:");
417 && (cid_start=strchr(src,':'))
418 && (cid_end=strchr(cid_start,'"'))
419 && (cid_end < tag_end)
421 /* copy tag and attributes up to src="cid: */
422 StrBufAppendBufPlain(converted_msg, ptr, src - ptr, 0);
425 /* add in /webcit/mimepart/<msgno>/CID/
426 trailing / stops dumb URL filters getting excited */
427 StrBufAppendPrintf(converted_msg,
428 " src=\"/webcit/mimepart/%d/",msgnum);
429 StrBufAppendBufPlain(converted_msg, cid_start, cid_end - cid_start, 0);
430 StrBufAppendBufPlain(converted_msg, "/\"", -1, 0);
434 StrBufAppendBufPlain(converted_msg, ptr, tag_end - ptr, 0);
439 * Turn anything that looks like a URL into a real link, as long
440 * as it's not inside a tag already
442 else if ( (brak == 0) && (alevel == 0)
443 && (!strncasecmp(ptr, "http://", 7))) {
444 /** Find the end of the link */
448 strlenptr = strlen(ptr);
449 for (i=0; i<=strlenptr; ++i) {
463 /* did s.b. send us an entity? */
465 if ((ptr[i+2] ==';') ||
472 if (linklen > 0) break;
481 linkedchar = ptr[len];
483 /* spot for some subject strings tinymce tends to give us. */
484 ltreviewptr = strchr(ptr, '<');
485 if (ltreviewptr != NULL) {
487 linklen = ltreviewptr - ptr;
490 nbspreviewptr = strstr(ptr, " ");
491 if (nbspreviewptr != NULL) {
492 /* nbspreviewptr = '\0'; */
493 linklen = nbspreviewptr - ptr;
495 if (ltreviewptr != 0)
498 ptr[len] = linkedchar;
500 content_length += (32 + linklen);
501 StrBufAppendPrintf(converted_msg, "%s\"", new_window);
502 StrBufAppendBufPlain(converted_msg, ptr, linklen, 0);
503 StrBufAppendPrintf(converted_msg, "\">");
504 StrBufAppendBufPlain(converted_msg, ptr, linklen, 0);
506 StrBufAppendPrintf(converted_msg, "</A>");
510 StrBufAppendBufPlain(converted_msg, ptr, 1, 0);
515 if ((ptr >= msg) && (ptr <= msgend)) {
517 * We need to know when we're inside a tag,
518 * so we don't turn things that look like URL's into
519 * links, when they're already links - or image sources.
521 if ((ptr > msg) && (*(ptr-1) == '<')) {
524 if ((ptr > msg) && (*(ptr-1) == '>')) {
526 if ((scriptlevel == 0) && (script_start_pos >= 0)) {
527 StrBufCutRight(converted_msg, StrLength(converted_msg) - script_start_pos);
528 script_start_pos = (-1);
531 if (!strncasecmp(ptr, "</A>", 3)) --alevel;
535 if (BodyArea != NULL) {
536 StrBufAppendBufPlain(converted_msg, HKEY("</td></tr></table>"), 0);
537 FreeStrBuf(&BodyArea);
540 /** uncomment these two lines to override conversion */
541 /** memcpy(converted_msg, msg, content_length); */
542 /** output_length = content_length; */
544 /** Output our big pile of markup */
545 StrBufAppendBuf(Target, converted_msg, 0);
547 BAIL: /** A little trailing vertical whitespace... */
548 StrBufAppendPrintf(Target, "<br /><br />\n");
550 /** Now give back the memory */
551 FreeStrBuf(&converted_msg);
552 if ((msg != NULL) && (Source == NULL)) free(msg);
561 * Look for URL's embedded in a buffer and make them linkable. We use a
562 * target window in order to keep the Citadel session in its own window.
564 void UrlizeText(StrBuf* Target, StrBuf *Source, StrBuf *WrkBuf)
566 int len, UrlLen, Offset, TrailerLen;
567 const char *start, *end, *pos;
572 len = StrLength(Source);
573 end = ChrPtr(Source) + len;
574 for (pos = ChrPtr(Source); (pos < end) && (start == NULL); ++pos) {
575 if (!strncasecmp(pos, "http://", 7))
577 else if (!strncasecmp(pos, "ftp://", 6))
582 StrBufAppendBuf(Target, Source, 0);
587 for (pos = ChrPtr(Source) + len; pos > start; --pos) {
588 if ( (!isprint(*pos))
607 UrlLen = end - start;
608 StrBufAppendBufPlain(WrkBuf, start, UrlLen, 0);
610 Offset = start - ChrPtr(Source);
612 StrBufAppendBufPlain(Target, ChrPtr(Source), Offset, 0);
613 StrBufAppendPrintf(Target, "%ca href=%c%s%c TARGET=%c%s%c%c%s%c/A%c",
614 LB, QU, ChrPtr(WrkBuf), QU, QU, TARGET,
615 QU, RB, ChrPtr(WrkBuf), LB, RB);
617 TrailerLen = StrLength(Source) - (end - ChrPtr(Source));
619 StrBufAppendBufPlain(Target, end, TrailerLen, 0);
621 void url(char *buf, size_t bufsize)
623 int len, UrlLen, Offset, TrailerLen, outpos;
624 char *start, *end, *pos;
631 lprintf(1, "URL: content longer than buffer!");
635 for (pos = buf; (pos < end) && (start == NULL); ++pos) {
636 if (!strncasecmp(pos, "http://", 7))
638 if (!strncasecmp(pos, "ftp://", 6))
645 for (pos = buf+len; pos > start; --pos) {
646 if ( (!isprint(*pos))
665 UrlLen = end - start;
666 if (UrlLen > sizeof(urlbuf)){
667 lprintf(1, "URL: content longer than buffer!");
670 memcpy(urlbuf, start, UrlLen);
671 urlbuf[UrlLen] = '\0';
673 Offset = start - buf;
674 if ((Offset != 0) && (Offset < sizeof(outbuf)))
675 memcpy(outbuf, buf, Offset);
676 outpos = snprintf(&outbuf[Offset], sizeof(outbuf) - Offset,
677 "%ca href=%c%s%c TARGET=%c%s%c%c%s%c/A%c",
678 LB, QU, urlbuf, QU, QU, TARGET, QU, RB, urlbuf, LB, RB);
679 if (outpos >= sizeof(outbuf) - Offset) {
680 lprintf(1, "URL: content longer than buffer!");
684 TrailerLen = len - (end - start);
686 memcpy(outbuf + Offset + outpos, end, TrailerLen);
687 if (Offset + outpos + TrailerLen > bufsize) {
688 lprintf(1, "URL: content longer than buffer!");
691 memcpy (buf, outbuf, Offset + outpos + TrailerLen);
692 *(buf + Offset + outpos + TrailerLen) = '\0';