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");
79 * \brief Sanitize and enhance an HTML message for display.
80 * Also convert weird character sets to UTF-8 if necessary.
81 * Also fixup img src="cid:..." type inline images to fetch the image
83 * \param supplied_charset the input charset as declared in the MIME headers
85 void output_html(const char *supplied_charset, int treat_as_wiki, int msgnum, StrBuf *Source, StrBuf *Target) {
91 StrBuf *converted_msg;
92 int buffer_length = 1;
94 int content_length = 0;
99 int script_start_pos = (-1);
104 iconv_t ic = (iconv_t)(-1) ;
105 char *ibuf; /**< Buffer of characters to be converted */
106 char *obuf; /**< Buffer for converted characters */
107 size_t ibuflen; /**< Length of input buffer */
108 size_t obuflen; /**< Length of output buffer */
109 char *osav; /**< Saved pointer to output buffer */
111 lprintf(9, "output_html() called with msgnum=%ld\n", msgnum);
115 safestrncpy(charset, supplied_charset, sizeof charset);
117 sprintf(new_window, "<a target=\"%s\" href=", TARGET);
119 if (Source == NULL) while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
120 line_length = strlen(buf);
121 buffer_length = content_length + line_length + 2;
122 ptr = realloc(msg, buffer_length);
124 StrBufAppendPrintf(Target, "<b>");
125 StrBufAppendPrintf(Target, _("realloc() error! couldn't get %d bytes: %s"),
128 StrBufAppendPrintf(Target, "</b><br /><br />\n");
129 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
136 strcpy(&msg[content_length], buf);
137 content_length += line_length;
138 strcpy(&msg[content_length], "\n");
142 content_length = StrLength(Source);
144 msg = (char*) ChrPtr(Source);/* TODO: remove cast */
145 buffer_length = content_length;
148 /** Do a first pass to isolate the message body */
151 msgend = &msg[content_length];
153 while (ptr < msgend) {
155 /** Advance to next tag */
156 ptr = strchr(ptr, '<');
157 if ((ptr == NULL) || (ptr >= msgend)) break;
159 if ((ptr == NULL) || (ptr >= msgend)) break;
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,
196 meta_http_equiv, meta_content);
200 free(meta_http_equiv);
207 * Any of these tags cause everything up to and including
208 * the tag to be removed.
210 if ( (!strncasecmp(ptr, "HTML", 4))
211 ||(!strncasecmp(ptr, "HEAD", 4))
212 ||(!strncasecmp(ptr, "/HEAD", 5))
213 ||(!strncasecmp(ptr, "BODY", 4)) ) {
214 ptr = strchr(ptr, '>');
215 if ((ptr == NULL) || (ptr >= msgend)) break;
217 if ((ptr == NULL) || (ptr >= msgend)) break;
222 * Any of these tags cause everything including and following
223 * the tag to be removed.
225 if ( (!strncasecmp(ptr, "/HTML", 5))
226 ||(!strncasecmp(ptr, "/BODY", 5)) ) {
235 if (msgstart > msg) {
236 strcpy(msg, msgstart);
239 /** Now go through the message, parsing tags as necessary. */
240 converted_msg = NewStrBufPlain(NULL, content_length + 8192);
243 /** Convert foreign character sets to UTF-8 if necessary. */
245 if ( (strcasecmp(charset, "us-ascii"))
246 && (strcasecmp(charset, "UTF-8"))
247 && (strcasecmp(charset, ""))
249 lprintf(9, "Converting %s to UTF-8\n", charset);
250 ctdl_iconv_open("UTF-8", charset, &ic);
251 if (ic == (iconv_t)(-1) ) {
252 lprintf(5, "%s:%d iconv_open() failed: %s\n",
253 __FILE__, __LINE__, strerror(errno));
256 if (Source == NULL) {
257 if (ic != (iconv_t)(-1) ) {
259 ibuflen = content_length;
260 obuflen = content_length + (content_length / 2) ;
261 obuf = (char *) malloc(obuflen);
263 iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
264 content_length = content_length + (content_length / 2) - obuflen;
265 osav[content_length] = 0;
272 if (ic != (iconv_t)(-1) ) {
273 StrBuf *Buf = NewStrBufPlain(NULL, StrLength(Source) + 8096);;
274 StrBufConvert(Source, Buf, &ic);
277 msg = (char*)ChrPtr(Source); /* TODO: get rid of this. */
284 * At this point, the message has been stripped down to
285 * only the content inside the <BODY></BODY> tags, and has
286 * been converted to UTF-8 if it was originally in a foreign
287 * character set. The text is also guaranteed to be null
291 if (converted_msg == NULL) {
292 StrBufAppendPrintf(Target, "Error %d: %s<br />%s:%d", errno, strerror(errno), __FILE__, __LINE__);
297 msgend = strchr(msg, 0);
298 while (ptr < msgend) {
300 /** Try to sanitize the html of any rogue scripts */
301 if (!strncasecmp(ptr, "<script", 7)) {
302 if (scriptlevel == 0) {
303 script_start_pos = StrLength(converted_msg);
307 if (!strncasecmp(ptr, "</script", 8)) {
312 * Change mailto: links to WebCit mail, by replacing the
313 * link with one that points back to our mail room. Due to
314 * the way we parse URL's, it'll even handle mailto: links
315 * that have "?subject=" in them.
317 if (!strncasecmp(ptr, "<a href=\"mailto:", 16)) {
318 content_length += 64;
319 StrBufAppendPrintf(converted_msg,
320 "<a href=\"display_enter?force_room=_MAIL_&recp=");
325 /** Make external links open in a separate window */
326 else if (!strncasecmp(ptr, "<a href=\"", 9)) {
329 if ( ((strchr(ptr, ':') < strchr(ptr, '/')))
330 && ((strchr(ptr, '/') < strchr(ptr, '>')))
332 /* open external links to new window */
333 StrBufAppendPrintf(converted_msg, new_window);
336 else if ( (treat_as_wiki) && (strncasecmp(ptr, "<a href=\"wiki?", 14)) ) {
337 content_length += 64;
338 StrBufAppendPrintf(converted_msg, "<a href=\"wiki?page=");
342 StrBufAppendPrintf(converted_msg, "<a href=\"");
346 /** Fixup <img src="cid:... ...> to fetch the mime part */
347 else if (!strncasecmp(ptr, "<img ", 5)) {
348 char* tag_end=strchr(ptr,'>');
349 char* src=strstr(ptr, " src=\"cid:");
350 char *cid_start, *cid_end;
354 (cid_start=strchr(src,':')) &&
355 (cid_end=strchr(cid_start,'"')) &&
356 (cid_end < tag_end)) {
358 /* copy tag and attributes up to src="cid: */
359 StrBufAppendBufPlain(converted_msg, ptr, src - ptr, 0);
362 /* add in /webcit/mimepart/<msgno>/CID/
363 trailing / stops dumb URL filters getting excited */
364 StrBufAppendPrintf(converted_msg,
365 "src=\"/webcit/mimepart/%d/",msgnum);
366 StrBufAppendBufPlain(converted_msg, cid_start, cid_end - cid_start, 0);
367 StrBufAppendBufPlain(converted_msg, "/\"", -1, 0);
371 StrBufAppendBufPlain(converted_msg, ptr, tag_end - ptr, 0);
376 * Turn anything that looks like a URL into a real link, as long
377 * as it's not inside a tag already
379 else if ( (brak == 0) && (alevel == 0)
380 && (!strncasecmp(ptr, "http://", 7))) {
381 /** Find the end of the link */
385 strlenptr = strlen(ptr);
386 for (i=0; i<=strlenptr; ++i) {
400 /* did s.b. send us an entity? */
402 if ((ptr[i+2] ==';') ||
409 if (linklen > 0) break;
418 linkedchar = ptr[len];
420 /* spot for some subject strings tinymce tends to give us. */
421 ltreviewptr = strchr(ptr, '<');
422 if (ltreviewptr != NULL) {
424 linklen = ltreviewptr - ptr;
427 nbspreviewptr = strstr(ptr, " ");
428 if (nbspreviewptr != NULL) {
429 /* nbspreviewptr = '\0'; */
430 linklen = nbspreviewptr - ptr;
432 if (ltreviewptr != 0)
435 ptr[len] = linkedchar;
437 content_length += (32 + linklen);
438 StrBufAppendPrintf(converted_msg, "%s\"", new_window);
439 StrBufAppendBufPlain(converted_msg, ptr, linklen, 0);
440 StrBufAppendPrintf(converted_msg, "\">");
441 StrBufAppendBufPlain(converted_msg, ptr, linklen, 0);
443 StrBufAppendPrintf(converted_msg, "</A>");
447 StrBufAppendBufPlain(converted_msg, ptr, 1, 0);
452 * We need to know when we're inside a tag,
453 * so we don't turn things that look like URL's into
454 * links, when they're already links - or image sources.
456 if (*(ptr-1) == '<') {
459 if (*(ptr-1) == '>') {
461 if ((scriptlevel == 0) && (script_start_pos >= 0)) {
462 StrBufCutRight(converted_msg, StrLength(converted_msg) - script_start_pos);
463 script_start_pos = (-1);
466 if (!strncasecmp(ptr, "</A>", 3)) --alevel;
469 /** uncomment these two lines to override conversion */
470 /** memcpy(converted_msg, msg, content_length); */
471 /** output_length = content_length; */
473 /** Output our big pile of markup */
474 StrBufAppendBuf(Target, converted_msg, 0);
476 BAIL: /** A little trailing vertical whitespace... */
477 StrBufAppendPrintf(Target, "<br /><br />\n");
479 /** Now give back the memory */
480 FreeStrBuf(&converted_msg);
481 if ((msg != NULL) && (Source == NULL)) free(msg);
490 * Look for URL's embedded in a buffer and make them linkable. We use a
491 * target window in order to keep the Citadel session in its own window.
493 void UrlizeText(StrBuf* Target, StrBuf *Source, StrBuf *WrkBuf)
495 int len, UrlLen, Offset, TrailerLen;
496 const char *start, *end, *pos;
501 len = StrLength(Source);
502 end = ChrPtr(Source) + len;
503 for (pos = ChrPtr(Source); (pos < end) && (start == NULL); ++pos) {
504 if (!strncasecmp(pos, "http://", 7))
506 else if (!strncasecmp(pos, "ftp://", 6))
511 StrBufAppendBuf(Target, Source, 0);
516 for (pos = ChrPtr(Source) + len; pos > start; --pos) {
517 if ( (!isprint(*pos))
536 UrlLen = end - start;
537 StrBufAppendBufPlain(WrkBuf, start, UrlLen, 0);
539 Offset = start - ChrPtr(Source);
541 StrBufAppendBufPlain(Target, ChrPtr(Source), Offset, 0);
542 StrBufAppendPrintf(Target, "%ca href=%c%s%c TARGET=%c%s%c%c%s%c/A%c",
543 LB, QU, ChrPtr(WrkBuf), QU, QU, TARGET,
544 QU, RB, ChrPtr(WrkBuf), LB, RB);
546 TrailerLen = StrLength(Source) - (end - ChrPtr(Source));
548 StrBufAppendBufPlain(Target, end, TrailerLen, 0);
550 void url(char *buf, size_t bufsize)
552 int len, UrlLen, Offset, TrailerLen, outpos;
553 char *start, *end, *pos;
560 lprintf(1, "URL: content longer than buffer!");
564 for (pos = buf; (pos < end) && (start == NULL); ++pos) {
565 if (!strncasecmp(pos, "http://", 7))
567 if (!strncasecmp(pos, "ftp://", 6))
574 for (pos = buf+len; pos > start; --pos) {
575 if ( (!isprint(*pos))
594 UrlLen = end - start;
595 if (UrlLen > sizeof(urlbuf)){
596 lprintf(1, "URL: content longer than buffer!");
599 memcpy(urlbuf, start, UrlLen);
600 urlbuf[UrlLen] = '\0';
602 Offset = start - buf;
603 if ((Offset != 0) && (Offset < sizeof(outbuf)))
604 memcpy(outbuf, buf, Offset);
605 outpos = snprintf(&outbuf[Offset], sizeof(outbuf) - Offset,
606 "%ca href=%c%s%c TARGET=%c%s%c%c%s%c/A%c",
607 LB, QU, urlbuf, QU, QU, TARGET, QU, RB, urlbuf, LB, RB);
608 if (outpos >= sizeof(outbuf) - Offset) {
609 lprintf(1, "URL: content longer than buffer!");
613 TrailerLen = len - (end - start);
615 memcpy(outbuf + Offset + outpos, end, TrailerLen);
616 if (Offset + outpos + TrailerLen > bufsize) {
617 lprintf(1, "URL: content longer than buffer!");
620 memcpy (buf, outbuf, Offset + outpos + TrailerLen);
621 *(buf + Offset + outpos + TrailerLen) = '\0';