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:");
418 && (cid_start=strchr(src,':'))
419 && (cid_end=strchr(cid_start,'"'))
420 && (cid_end < tag_end)
422 /* copy tag and attributes up to src="cid: */
423 StrBufAppendBufPlain(converted_msg, ptr, src - ptr, 0);
426 /* add in /webcit/mimepart/<msgno>/CID/
427 trailing / stops dumb URL filters getting excited */
428 StrBufAppendPrintf(converted_msg,
429 " src=\"/webcit/mimepart/%d/",msgnum);
430 StrBufAppendBufPlain(converted_msg, cid_start, cid_end - cid_start, 0);
431 StrBufAppendBufPlain(converted_msg, "/\"", -1, 0);
435 StrBufAppendBufPlain(converted_msg, ptr, tag_end - ptr, 0);
440 * Turn anything that looks like a URL into a real link, as long
441 * as it's not inside a tag already
443 else if ( (brak == 0) && (alevel == 0)
444 && (!strncasecmp(ptr, "http://", 7))) {
445 /** Find the end of the link */
449 strlenptr = strlen(ptr);
450 for (i=0; i<=strlenptr; ++i) {
464 /* did s.b. send us an entity? */
466 if ((ptr[i+2] ==';') ||
473 if (linklen > 0) break;
482 linkedchar = ptr[len];
484 /* spot for some subject strings tinymce tends to give us. */
485 ltreviewptr = strchr(ptr, '<');
486 if (ltreviewptr != NULL) {
488 linklen = ltreviewptr - ptr;
491 nbspreviewptr = strstr(ptr, " ");
492 if (nbspreviewptr != NULL) {
493 /* nbspreviewptr = '\0'; */
494 linklen = nbspreviewptr - ptr;
496 if (ltreviewptr != 0)
499 ptr[len] = linkedchar;
501 content_length += (32 + linklen);
502 StrBufAppendPrintf(converted_msg, "%s\"", new_window);
503 StrBufAppendBufPlain(converted_msg, ptr, linklen, 0);
504 StrBufAppendPrintf(converted_msg, "\">");
505 StrBufAppendBufPlain(converted_msg, ptr, linklen, 0);
507 StrBufAppendPrintf(converted_msg, "</A>");
511 StrBufAppendBufPlain(converted_msg, ptr, 1, 0);
516 if ((ptr >= msg) && (ptr <= msgend)) {
518 * We need to know when we're inside a tag,
519 * so we don't turn things that look like URL's into
520 * links, when they're already links - or image sources.
522 if ((ptr > msg) && (*(ptr-1) == '<')) {
525 if ((ptr > msg) && (*(ptr-1) == '>')) {
527 if ((scriptlevel == 0) && (script_start_pos >= 0)) {
528 StrBufCutRight(converted_msg, StrLength(converted_msg) - script_start_pos);
529 script_start_pos = (-1);
532 if (!strncasecmp(ptr, "</A>", 3)) --alevel;
536 if (BodyArea != NULL) {
537 StrBufAppendBufPlain(converted_msg, HKEY("</td></tr></table>"), 0);
538 FreeStrBuf(&BodyArea);
541 /** uncomment these two lines to override conversion */
542 /** memcpy(converted_msg, msg, content_length); */
543 /** output_length = content_length; */
545 /** Output our big pile of markup */
546 StrBufAppendBuf(Target, converted_msg, 0);
548 BAIL: /** A little trailing vertical whitespace... */
549 StrBufAppendPrintf(Target, "<br /><br />\n");
551 /** Now give back the memory */
552 FreeStrBuf(&converted_msg);
553 if ((msg != NULL) && (Source == NULL)) free(msg);
562 * Look for URL's embedded in a buffer and make them linkable. We use a
563 * target window in order to keep the Citadel session in its own window.
565 void UrlizeText(StrBuf* Target, StrBuf *Source, StrBuf *WrkBuf)
567 int len, UrlLen, Offset, TrailerLen;
568 const char *start, *end, *pos;
573 len = StrLength(Source);
574 end = ChrPtr(Source) + len;
575 for (pos = ChrPtr(Source); (pos < end) && (start == NULL); ++pos) {
576 if (!strncasecmp(pos, "http://", 7))
578 else if (!strncasecmp(pos, "ftp://", 6))
583 StrBufAppendBuf(Target, Source, 0);
588 for (pos = ChrPtr(Source) + len; pos > start; --pos) {
589 if ( (!isprint(*pos))
608 UrlLen = end - start;
609 StrBufAppendBufPlain(WrkBuf, start, UrlLen, 0);
611 Offset = start - ChrPtr(Source);
613 StrBufAppendBufPlain(Target, ChrPtr(Source), Offset, 0);
614 StrBufAppendPrintf(Target, "%ca href=%c%s%c TARGET=%c%s%c%c%s%c/A%c",
615 LB, QU, ChrPtr(WrkBuf), QU, QU, TARGET,
616 QU, RB, ChrPtr(WrkBuf), LB, RB);
618 TrailerLen = StrLength(Source) - (end - ChrPtr(Source));
620 StrBufAppendBufPlain(Target, end, TrailerLen, 0);
622 void url(char *buf, size_t bufsize)
624 int len, UrlLen, Offset, TrailerLen, outpos;
625 char *start, *end, *pos;
632 lprintf(1, "URL: content longer than buffer!");
636 for (pos = buf; (pos < end) && (start == NULL); ++pos) {
637 if (!strncasecmp(pos, "http://", 7))
639 if (!strncasecmp(pos, "ftp://", 6))
646 for (pos = buf+len; pos > start; --pos) {
647 if ( (!isprint(*pos))
666 UrlLen = end - start;
667 if (UrlLen > sizeof(urlbuf)){
668 lprintf(1, "URL: content longer than buffer!");
671 memcpy(urlbuf, start, UrlLen);
672 urlbuf[UrlLen] = '\0';
674 Offset = start - buf;
675 if ((Offset != 0) && (Offset < sizeof(outbuf)))
676 memcpy(outbuf, buf, Offset);
677 outpos = snprintf(&outbuf[Offset], sizeof(outbuf) - Offset,
678 "%ca href=%c%s%c TARGET=%c%s%c%c%s%c/A%c",
679 LB, QU, urlbuf, QU, QU, TARGET, QU, RB, urlbuf, LB, RB);
680 if (outpos >= sizeof(outbuf) - Offset) {
681 lprintf(1, "URL: content longer than buffer!");
685 TrailerLen = len - (end - start);
687 memcpy(outbuf + Offset + outpos, end, TrailerLen);
688 if (Offset + outpos + TrailerLen > bufsize) {
689 lprintf(1, "URL: content longer than buffer!");
692 memcpy (buf, outbuf, Offset + outpos + TrailerLen);
693 *(buf + Offset + outpos + TrailerLen) = '\0';