* Reduced iconbar button size to 90% because it looked obtuse on a smaller screen
[citadel.git] / webcit / html2html.c
blobc1c2b64a55a8283b87d92b939a6cb7c5cd114ecc
1 /*
2 * $Id$
3 */
4 /**
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
8 */
9 /*@{*/
10 #include "webcit.h"
11 #include "webserver.h"
14 /**
15 * \brief Strip surrounding single or double quotes from a string.
17 * \param s String to be stripped.
19 void stripquotes(char *s)
21 int len;
23 if (!s) return;
25 len = strlen(s);
26 if (len < 2) return;
28 if ( ( (s[0] == '\"') && (s[len-1] == '\"') ) || ( (s[0] == '\'') && (s[len-1] == '\'') ) ) {
29 s[len-1] = 0;
30 strcpy(s, &s[1]);
35 /**
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)
44 char *ptr;
45 char buf[64];
47 if (!charset) return;
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, ';');
55 if (!ptr) return;
57 safestrncpy(buf, ++ptr, sizeof buf);
58 striplt(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");
78 /**
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) {
86 char buf[SIZ];
87 char *msg;
88 char *ptr;
89 char *msgstart;
90 char *msgend;
91 StrBuf *converted_msg;
92 int buffer_length = 1;
93 int line_length = 0;
94 int content_length = 0;
95 char new_window[SIZ];
96 int brak = 0;
97 int alevel = 0;
98 int scriptlevel = 0;
99 int script_start_pos = (-1);
100 int i;
101 int linklen;
102 char charset[128];
103 StrBuf *BodyArea = NULL;
104 #ifdef HAVE_ICONV
105 iconv_t ic = (iconv_t)(-1) ;
106 char *ibuf; /**< Buffer of characters to be converted */
107 char *obuf; /**< Buffer for converted characters */
108 size_t ibuflen; /**< Length of input buffer */
109 size_t obuflen; /**< Length of output buffer */
110 char *osav; /**< Saved pointer to output buffer */
111 #endif
112 if (Target == NULL)
113 Target = WC->WBuf;
115 safestrncpy(charset, supplied_charset, sizeof charset);
116 msg = strdup("");
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);
123 if (ptr == NULL) {
124 StrBufAppendPrintf(Target, "<b>");
125 StrBufAppendPrintf(Target, _("realloc() error! couldn't get %d bytes: %s"),
126 buffer_length + 1,
127 strerror(errno));
128 StrBufAppendPrintf(Target, "</b><br /><br />\n");
129 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
130 /** flush */
132 free(msg);
133 return;
135 msg = ptr;
136 strcpy(&msg[content_length], buf);
137 content_length += line_length;
138 strcpy(&msg[content_length], "\n");
139 content_length += 1;
141 else {
142 content_length = StrLength(Source);
143 free(msg);
144 msg = (char*) ChrPtr(Source);/* TODO: remove cast */
145 buffer_length = content_length;
148 /** Do a first pass to isolate the message body */
149 ptr = msg + 1;
150 msgstart = msg;
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;
158 ++ptr;
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)) {
169 char *meta_start;
170 char *meta_end;
171 int meta_length;
172 char *meta;
173 char *meta_http_equiv;
174 char *meta_content;
175 char *spaceptr;
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;
184 striplt(meta);
185 if (!strncasecmp(meta, "HTTP-EQUIV=", 11)) {
186 meta_http_equiv = strdup(&meta[11]);
187 spaceptr = strchr(meta_http_equiv, ' ');
188 if (spaceptr != NULL) {
189 *spaceptr = 0;
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);
198 free(meta_content);
200 free(meta_http_equiv);
202 free(meta);
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 char *pBody = NULL;
216 if (!strncasecmp(ptr, "BODY", 4)) {
217 pBody = ptr;
219 ptr = strchr(ptr, '>');
220 if ((ptr == NULL) || (ptr >= msgend)) break;
221 if ((pBody != NULL) && (ptr - pBody > 4)) {
222 char* src;
223 char *cid_start, *cid_end;
225 *ptr = '\0';
226 pBody += 4;
227 while ((isspace(*pBody)) && (pBody < ptr))
228 pBody ++;
229 BodyArea = NewStrBufPlain(NULL, ptr - pBody);
231 if (pBody < ptr) {
232 src = strstr(pBody, "cid:");
233 if (src) {
234 cid_start = src + 4;
235 cid_end = cid_start;
236 while ((*cid_end != '"') &&
237 !isspace(*cid_end) &&
238 (cid_end < ptr))
239 cid_end ++;
241 /* copy tag and attributes up to src="cid: */
242 StrBufAppendBufPlain(BodyArea, pBody, src - pBody, 0);
244 /* add in /webcit/mimepart/<msgno>/CID/
245 trailing / stops dumb URL filters getting excited */
246 StrBufAppendPrintf(BodyArea,
247 "/webcit/mimepart/%d/",msgnum);
248 StrBufAppendBufPlain(BodyArea, cid_start, cid_end - cid_start, 0);
250 if (ptr - cid_end > 0)
251 StrBufAppendBufPlain(BodyArea,
252 cid_end + 1,
253 ptr - cid_end, 0);
255 else
256 StrBufAppendBufPlain(BodyArea, pBody, ptr - pBody, 0);
258 *ptr = '>';
260 ++ptr;
261 if ((ptr == NULL) || (ptr >= msgend)) break;
262 msgstart = ptr;
266 * Any of these tags cause everything including and following
267 * the tag to be removed.
269 if ( (!strncasecmp(ptr, "/HTML", 5))
270 ||(!strncasecmp(ptr, "/BODY", 5)) ) {
271 --ptr;
272 msgend = ptr;
273 strcpy(ptr, "");
277 ++ptr;
279 if (msgstart > msg) {
280 strcpy(msg, msgstart);
283 /** Now go through the message, parsing tags as necessary. */
284 converted_msg = NewStrBufPlain(NULL, content_length + 8192);
287 /** Convert foreign character sets to UTF-8 if necessary. */
288 #ifdef HAVE_ICONV
289 if ( (strcasecmp(charset, "us-ascii"))
290 && (strcasecmp(charset, "UTF-8"))
291 && (strcasecmp(charset, ""))
293 lprintf(9, "Converting %s to UTF-8\n", charset);
294 ctdl_iconv_open("UTF-8", charset, &ic);
295 if (ic == (iconv_t)(-1) ) {
296 lprintf(5, "%s:%d iconv_open() failed: %s\n",
297 __FILE__, __LINE__, strerror(errno));
300 if (Source == NULL) {
301 if (ic != (iconv_t)(-1) ) {
302 ibuf = msg;
303 ibuflen = content_length;
304 obuflen = content_length + (content_length / 2) ;
305 obuf = (char *) malloc(obuflen);
306 osav = obuf;
307 iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
308 content_length = content_length + (content_length / 2) - obuflen;
309 osav[content_length] = 0;
310 free(msg);
311 msg = osav;
312 iconv_close(ic);
315 else {
316 if (ic != (iconv_t)(-1) ) {
317 StrBuf *Buf = NewStrBufPlain(NULL, StrLength(Source) + 8096);;
318 StrBufConvert(Source, Buf, &ic);
319 FreeStrBuf(&Buf);
320 iconv_close(ic);
321 msg = (char*)ChrPtr(Source); /* TODO: get rid of this. */
325 #endif
328 * At this point, the message has been stripped down to
329 * only the content inside the <BODY></BODY> tags, and has
330 * been converted to UTF-8 if it was originally in a foreign
331 * character set. The text is also guaranteed to be null
332 * terminated now.
335 if (converted_msg == NULL) {
336 StrBufAppendPrintf(Target, "Error %d: %s<br />%s:%d", errno, strerror(errno), __FILE__, __LINE__);
337 goto BAIL;
340 if (BodyArea != NULL) {
341 StrBufAppendBufPlain(converted_msg, HKEY("<table "), 0);
342 StrBufAppendBuf(converted_msg, BodyArea, 0);
343 StrBufAppendBufPlain(converted_msg, HKEY(" width=\"100%\"><tr><td>"), 0);
345 ptr = msg;
346 msgend = strchr(msg, 0);
347 while (ptr < msgend) {
349 /** Try to sanitize the html of any rogue scripts */
350 if (!strncasecmp(ptr, "<script", 7)) {
351 if (scriptlevel == 0) {
352 script_start_pos = StrLength(converted_msg);
354 ++scriptlevel;
356 if (!strncasecmp(ptr, "</script", 8)) {
357 --scriptlevel;
361 * Change mailto: links to WebCit mail, by replacing the
362 * link with one that points back to our mail room. Due to
363 * the way we parse URL's, it'll even handle mailto: links
364 * that have "?subject=" in them.
366 if (!strncasecmp(ptr, "<a href=\"mailto:", 16)) {
367 content_length += 64;
368 StrBufAppendPrintf(converted_msg,
369 "<a href=\"display_enter?force_room=_MAIL_&recp=");
370 ptr = &ptr[16];
371 ++alevel;
372 ++brak;
374 /** Make external links open in a separate window */
375 else if (!strncasecmp(ptr, "<a href=\"", 9)) {
376 ++alevel;
377 ++brak;
378 if ( ((strchr(ptr, ':') < strchr(ptr, '/')))
379 && ((strchr(ptr, '/') < strchr(ptr, '>')))
381 /* open external links to new window */
382 StrBufAppendPrintf(converted_msg, new_window);
383 ptr = &ptr[8];
385 else if ( (treat_as_wiki) && (strncasecmp(ptr, "<a href=\"wiki?", 14)) ) {
386 content_length += 64;
387 StrBufAppendPrintf(converted_msg, "<a href=\"wiki?page=");
388 ptr = &ptr[9];
390 else {
391 StrBufAppendPrintf(converted_msg, "<a href=\"");
392 ptr = &ptr[9];
395 /** Fixup <img src="cid:... ...> to fetch the mime part */
396 else if (!strncasecmp(ptr, "<img ", 5)) {
397 char* tag_end=strchr(ptr,'>');
398 char* src=strstr(ptr, " src=\"cid:");
399 char *cid_start, *cid_end;
400 ++brak;
402 if (src &&
403 (cid_start=strchr(src,':')) &&
404 (cid_end=strchr(cid_start,'"')) &&
405 (cid_end < tag_end)) {
407 /* copy tag and attributes up to src="cid: */
408 StrBufAppendBufPlain(converted_msg, ptr, src - ptr, 0);
409 cid_start++;
411 /* add in /webcit/mimepart/<msgno>/CID/
412 trailing / stops dumb URL filters getting excited */
413 StrBufAppendPrintf(converted_msg,
414 " src=\"/webcit/mimepart/%d/",msgnum);
415 StrBufAppendBufPlain(converted_msg, cid_start, cid_end - cid_start, 0);
416 StrBufAppendBufPlain(converted_msg, "/\"", -1, 0);
418 ptr = cid_end+1;
420 StrBufAppendBufPlain(converted_msg, ptr, tag_end - ptr, 0);
421 ptr = tag_end;
425 * Turn anything that looks like a URL into a real link, as long
426 * as it's not inside a tag already
428 else if ( (brak == 0) && (alevel == 0)
429 && (!strncasecmp(ptr, "http://", 7))) {
430 /** Find the end of the link */
431 int strlenptr;
432 linklen = 0;
434 strlenptr = strlen(ptr);
435 for (i=0; i<=strlenptr; ++i) {
436 if ((ptr[i]==0)
437 ||(isspace(ptr[i]))
438 ||(ptr[i]==10)
439 ||(ptr[i]==13)
440 ||(ptr[i]=='(')
441 ||(ptr[i]==')')
442 ||(ptr[i]=='<')
443 ||(ptr[i]=='>')
444 ||(ptr[i]=='[')
445 ||(ptr[i]==']')
446 ||(ptr[i]=='"')
447 ||(ptr[i]=='\'')
448 ) linklen = i;
449 /* did s.b. send us an entity? */
450 if (ptr[i] == '&') {
451 if ((ptr[i+2] ==';') ||
452 (ptr[i+3] ==';') ||
453 (ptr[i+5] ==';') ||
454 (ptr[i+6] ==';') ||
455 (ptr[i+7] ==';'))
456 linklen = i;
458 if (linklen > 0) break;
460 if (linklen > 0) {
461 char *ltreviewptr;
462 char *nbspreviewptr;
463 char linkedchar;
464 int len;
466 len = linklen;
467 linkedchar = ptr[len];
468 ptr[len] = '\0';
469 /* spot for some subject strings tinymce tends to give us. */
470 ltreviewptr = strchr(ptr, '<');
471 if (ltreviewptr != NULL) {
472 *ltreviewptr = '\0';
473 linklen = ltreviewptr - ptr;
476 nbspreviewptr = strstr(ptr, "&nbsp;");
477 if (nbspreviewptr != NULL) {
478 /* nbspreviewptr = '\0'; */
479 linklen = nbspreviewptr - ptr;
481 if (ltreviewptr != 0)
482 *ltreviewptr = '<';
484 ptr[len] = linkedchar;
486 content_length += (32 + linklen);
487 StrBufAppendPrintf(converted_msg, "%s\"", new_window);
488 StrBufAppendBufPlain(converted_msg, ptr, linklen, 0);
489 StrBufAppendPrintf(converted_msg, "\">");
490 StrBufAppendBufPlain(converted_msg, ptr, linklen, 0);
491 ptr += linklen;
492 StrBufAppendPrintf(converted_msg, "</A>");
495 else {
496 StrBufAppendBufPlain(converted_msg, ptr, 1, 0);
497 ptr++;
501 * We need to know when we're inside a tag,
502 * so we don't turn things that look like URL's into
503 * links, when they're already links - or image sources.
505 if (*(ptr-1) == '<') {
506 ++brak;
508 if (*(ptr-1) == '>') {
509 --brak;
510 if ((scriptlevel == 0) && (script_start_pos >= 0)) {
511 StrBufCutRight(converted_msg, StrLength(converted_msg) - script_start_pos);
512 script_start_pos = (-1);
515 if (!strncasecmp(ptr, "</A>", 3)) --alevel;
517 if (BodyArea != NULL) {
518 StrBufAppendBufPlain(converted_msg, HKEY("</td></tr></table>"), 0);
519 FreeStrBuf(&BodyArea);
522 /** uncomment these two lines to override conversion */
523 /** memcpy(converted_msg, msg, content_length); */
524 /** output_length = content_length; */
526 /** Output our big pile of markup */
527 StrBufAppendBuf(Target, converted_msg, 0);
529 BAIL: /** A little trailing vertical whitespace... */
530 StrBufAppendPrintf(Target, "<br /><br />\n");
532 /** Now give back the memory */
533 FreeStrBuf(&converted_msg);
534 if ((msg != NULL) && (Source == NULL)) free(msg);
543 * Look for URL's embedded in a buffer and make them linkable. We use a
544 * target window in order to keep the Citadel session in its own window.
546 void UrlizeText(StrBuf* Target, StrBuf *Source, StrBuf *WrkBuf)
548 int len, UrlLen, Offset, TrailerLen;
549 const char *start, *end, *pos;
551 FlushStrBuf(Target);
553 start = NULL;
554 len = StrLength(Source);
555 end = ChrPtr(Source) + len;
556 for (pos = ChrPtr(Source); (pos < end) && (start == NULL); ++pos) {
557 if (!strncasecmp(pos, "http://", 7))
558 start = pos;
559 else if (!strncasecmp(pos, "ftp://", 6))
560 start = pos;
563 if (start == NULL) {
564 StrBufAppendBuf(Target, Source, 0);
565 return;
567 FlushStrBuf(WrkBuf);
569 for (pos = ChrPtr(Source) + len; pos > start; --pos) {
570 if ( (!isprint(*pos))
571 || (isspace(*pos))
572 || (*pos == '{')
573 || (*pos == '}')
574 || (*pos == '|')
575 || (*pos == '\\')
576 || (*pos == '^')
577 || (*pos == '[')
578 || (*pos == ']')
579 || (*pos == '`')
580 || (*pos == '<')
581 || (*pos == '>')
582 || (*pos == '(')
583 || (*pos == ')')
585 end = pos;
589 UrlLen = end - start;
590 StrBufAppendBufPlain(WrkBuf, start, UrlLen, 0);
592 Offset = start - ChrPtr(Source);
593 if (Offset != 0)
594 StrBufAppendBufPlain(Target, ChrPtr(Source), Offset, 0);
595 StrBufAppendPrintf(Target, "%ca href=%c%s%c TARGET=%c%s%c%c%s%c/A%c",
596 LB, QU, ChrPtr(WrkBuf), QU, QU, TARGET,
597 QU, RB, ChrPtr(WrkBuf), LB, RB);
599 TrailerLen = StrLength(Source) - (end - ChrPtr(Source));
600 if (TrailerLen > 0)
601 StrBufAppendBufPlain(Target, end, TrailerLen, 0);
603 void url(char *buf, size_t bufsize)
605 int len, UrlLen, Offset, TrailerLen, outpos;
606 char *start, *end, *pos;
607 char urlbuf[SIZ];
608 char outbuf[SIZ];
610 start = NULL;
611 len = strlen(buf);
612 if (len > bufsize) {
613 lprintf(1, "URL: content longer than buffer!");
614 return;
616 end = buf + len;
617 for (pos = buf; (pos < end) && (start == NULL); ++pos) {
618 if (!strncasecmp(pos, "http://", 7))
619 start = pos;
620 if (!strncasecmp(pos, "ftp://", 6))
621 start = pos;
624 if (start == NULL)
625 return;
627 for (pos = buf+len; pos > start; --pos) {
628 if ( (!isprint(*pos))
629 || (isspace(*pos))
630 || (*pos == '{')
631 || (*pos == '}')
632 || (*pos == '|')
633 || (*pos == '\\')
634 || (*pos == '^')
635 || (*pos == '[')
636 || (*pos == ']')
637 || (*pos == '`')
638 || (*pos == '<')
639 || (*pos == '>')
640 || (*pos == '(')
641 || (*pos == ')')
643 end = pos;
647 UrlLen = end - start;
648 if (UrlLen > sizeof(urlbuf)){
649 lprintf(1, "URL: content longer than buffer!");
650 return;
652 memcpy(urlbuf, start, UrlLen);
653 urlbuf[UrlLen] = '\0';
655 Offset = start - buf;
656 if ((Offset != 0) && (Offset < sizeof(outbuf)))
657 memcpy(outbuf, buf, Offset);
658 outpos = snprintf(&outbuf[Offset], sizeof(outbuf) - Offset,
659 "%ca href=%c%s%c TARGET=%c%s%c%c%s%c/A%c",
660 LB, QU, urlbuf, QU, QU, TARGET, QU, RB, urlbuf, LB, RB);
661 if (outpos >= sizeof(outbuf) - Offset) {
662 lprintf(1, "URL: content longer than buffer!");
663 return;
666 TrailerLen = len - (end - start);
667 if (TrailerLen > 0)
668 memcpy(outbuf + Offset + outpos, end, TrailerLen);
669 if (Offset + outpos + TrailerLen > bufsize) {
670 lprintf(1, "URL: content longer than buffer!");
671 return;
673 memcpy (buf, outbuf, Offset + outpos + TrailerLen);
674 *(buf + Offset + outpos + TrailerLen) = '\0';
680 /*@}*/