* render_MAIL_html() now passes a message number instead of a MIME part number as...
[citadel.git] / webcit / html2html.c
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"
12
13
14 /**
15  * \brief       Strip surrounding single or double quotes from a string.
16  *
17  * \param s     String to be stripped.
18  */
19 void stripquotes(char *s)
20 {
21         int len;
22
23         if (!s) return;
24
25         len = strlen(s);
26         if (len < 2) return;
27
28         if ( ( (s[0] == '\"') && (s[len-1] == '\"') ) || ( (s[0] == '\'') && (s[len-1] == '\'') ) ) {
29                 s[len-1] = 0;
30                 strcpy(s, &s[1]);
31         }
32 }
33
34
35 /**
36  * \brief Check to see if a META tag has overridden the declared MIME character set.
37  *
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
41  */
42 void extract_charset_from_meta(char *charset, char *meta_http_equiv, char *meta_content)
43 {
44         char *ptr;
45         char buf[64];
46
47         if (!charset) return;
48         if (!meta_http_equiv) return;
49         if (!meta_content) return;
50
51
52         if (strcasecmp(meta_http_equiv, "Content-type")) return;
53
54         ptr = strchr(meta_content, ';');
55         if (!ptr) return;
56
57         safestrncpy(buf, ++ptr, sizeof buf);
58         striplt(buf);
59         if (!strncasecmp(buf, "charset=", 8)) {
60                 strcpy(charset, &buf[8]);
61
62                 /*
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.
68                  */
69                 if (!strcasecmp(charset, "unicode")) {
70                         strcpy(charset, "UTF-8");
71                 }
72
73         }
74 }
75
76
77
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
82  *
83  * \param supplied_charset the input charset as declared in the MIME headers
84  */
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 #ifdef HAVE_ICONV
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       */
110 #endif
111         lprintf(9, "output_html() called with msgnum=%ld\n", msgnum);
112         if (Target == NULL)
113                 Target = WC->WBuf;
114
115         safestrncpy(charset, supplied_charset, sizeof charset);
116         msg = strdup("");
117         sprintf(new_window, "<a target=\"%s\" href=", TARGET);
118
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 */
131                         }
132                         free(msg);
133                         return;
134                 }
135                 msg = ptr;
136                 strcpy(&msg[content_length], buf);
137                 content_length += line_length;
138                 strcpy(&msg[content_length], "\n");
139                 content_length += 1;
140         }
141         else {
142                 content_length = StrLength(Source);
143                 free(msg);
144                 msg = (char*) ChrPtr(Source);/* TODO: remove cast */
145                 buffer_length = content_length;
146         }
147
148         /** Do a first pass to isolate the message body */
149         ptr = msg + 1;
150         msgstart = msg;
151         msgend = &msg[content_length];
152
153         while (ptr < msgend) {
154
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;
160
161                 /**
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.
166                  */
167                 if (!strncasecmp(ptr, "META", 4)) {
168
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;
176
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);
197                                                 }
198                                                 free(meta_content);
199                                         }
200                                         free(meta_http_equiv);
201                                 }
202                                 free(meta);
203                         }
204                 }
205
206                 /**
207                  * Any of these tags cause everything up to and including
208                  * the tag to be removed.
209                  */     
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;
216                         ++ptr;
217                         if ((ptr == NULL) || (ptr >= msgend)) break;
218                         msgstart = ptr;
219                 }
220
221                 /**
222                  * Any of these tags cause everything including and following
223                  * the tag to be removed.
224                  */
225                 if ( (!strncasecmp(ptr, "/HTML", 5))
226                    ||(!strncasecmp(ptr, "/BODY", 5)) ) {
227                         --ptr;
228                         msgend = ptr;
229                         strcpy(ptr, "");
230                         
231                 }
232
233                 ++ptr;
234         }
235         if (msgstart > msg) {
236                 strcpy(msg, msgstart);
237         }
238
239         /** Now go through the message, parsing tags as necessary. */
240         converted_msg = NewStrBufPlain(NULL, content_length + 8192);
241
242
243         /** Convert foreign character sets to UTF-8 if necessary. */
244 #ifdef HAVE_ICONV
245         if ( (strcasecmp(charset, "us-ascii"))
246            && (strcasecmp(charset, "UTF-8"))
247            && (strcasecmp(charset, ""))
248         ) {
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));
254                 }
255         }
256         if  (Source == NULL) {
257                 if (ic != (iconv_t)(-1) ) {
258                         ibuf = msg;
259                         ibuflen = content_length;
260                         obuflen = content_length + (content_length / 2) ;
261                         obuf = (char *) malloc(obuflen);
262                         osav = obuf;
263                         iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
264                         content_length = content_length + (content_length / 2) - obuflen;
265                         osav[content_length] = 0;
266                         free(msg);
267                         msg = osav;
268                         iconv_close(ic);
269                 }
270         }
271         else {
272                 if (ic != (iconv_t)(-1) ) {
273                         StrBuf *Buf = NewStrBufPlain(NULL, StrLength(Source) + 8096);;
274                         StrBufConvert(Source, Buf, &ic);
275                         FreeStrBuf(&Buf);
276                         iconv_close(ic);
277                         msg = (char*)ChrPtr(Source); /* TODO: get rid of this. */
278                 }
279         }
280                 
281 #endif
282
283         /**
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
288          *      terminated now.
289          */
290
291         if (converted_msg == NULL) {
292                 StrBufAppendPrintf(Target, "Error %d: %s<br />%s:%d", errno, strerror(errno), __FILE__, __LINE__);
293                 goto BAIL;
294         }
295
296         ptr = msg;
297         msgend = strchr(msg, 0);
298         while (ptr < msgend) {
299
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);
304                         }
305                         ++scriptlevel;
306                 }
307                 if (!strncasecmp(ptr, "</script", 8)) {
308                         --scriptlevel;
309                 }
310
311                 /**
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.
316                  */
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=");
321                         ptr = &ptr[16];
322                         ++alevel;
323                         ++brak;
324                 }
325                 /** Make external links open in a separate window */
326                 else if (!strncasecmp(ptr, "<a href=\"", 9)) {
327                         ++alevel;
328                         ++brak;
329                         if ( ((strchr(ptr, ':') < strchr(ptr, '/')))
330                              &&  ((strchr(ptr, '/') < strchr(ptr, '>'))) 
331                              ) {
332                                 /* open external links to new window */
333                                 StrBufAppendPrintf(converted_msg, new_window);
334                                 ptr = &ptr[8];
335                         }
336                         else if ( (treat_as_wiki) && (strncasecmp(ptr, "<a href=\"wiki?", 14)) ) {
337                                 content_length += 64;
338                                 StrBufAppendPrintf(converted_msg, "<a href=\"wiki?page=");
339                                 ptr = &ptr[9];
340                         }
341                         else {
342                                 StrBufAppendPrintf(converted_msg, "<a href=\"");
343                                 ptr = &ptr[9];
344                         }
345                 }
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;
351                         ++brak;
352
353                         if (src && 
354                                 (cid_start=strchr(src,':')) && 
355                                 (cid_end=strchr(cid_start,'"')) &&
356                                 (cid_end < tag_end)) {
357
358                                 /* copy tag and attributes up to src="cid: */
359                                 StrBufAppendBufPlain(converted_msg, ptr, src - ptr, 0);
360                                 cid_start++;
361
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);
368                                 
369                                 ptr = cid_end+1;
370                         }
371                         StrBufAppendBufPlain(converted_msg, ptr, tag_end - ptr, 0);
372                         ptr = tag_end;
373                 }
374
375                 /**
376                  * Turn anything that looks like a URL into a real link, as long
377                  * as it's not inside a tag already
378                  */
379                 else if ( (brak == 0) && (alevel == 0)
380                      && (!strncasecmp(ptr, "http://", 7))) {
381                                 /** Find the end of the link */
382                                 int strlenptr;
383                                 linklen = 0;
384                                 
385                                 strlenptr = strlen(ptr);
386                                 for (i=0; i<=strlenptr; ++i) {
387                                         if ((ptr[i]==0)
388                                            ||(isspace(ptr[i]))
389                                            ||(ptr[i]==10)
390                                            ||(ptr[i]==13)
391                                            ||(ptr[i]=='(')
392                                            ||(ptr[i]==')')
393                                            ||(ptr[i]=='<')
394                                            ||(ptr[i]=='>')
395                                            ||(ptr[i]=='[')
396                                            ||(ptr[i]==']')
397                                            ||(ptr[i]=='"')
398                                            ||(ptr[i]=='\'')
399                                         ) linklen = i;
400                                         /* did s.b. send us an entity? */
401                                         if (ptr[i] == '&') {
402                                                 if ((ptr[i+2] ==';') ||
403                                                     (ptr[i+3] ==';') ||
404                                                     (ptr[i+5] ==';') ||
405                                                     (ptr[i+6] ==';') ||
406                                                     (ptr[i+7] ==';'))
407                                                         linklen = i;
408                                         }
409                                         if (linklen > 0) break;
410                                 }
411                                 if (linklen > 0) {
412                                         char *ltreviewptr;
413                                         char *nbspreviewptr;
414                                         char linkedchar;
415                                         int len = linklen;
416                                         
417                                         len = linklen;
418                                         linkedchar = ptr[len];
419                                         ptr[len] = '\0';
420                                         /* spot for some subject strings tinymce tends to give us. */
421                                         ltreviewptr = strchr(ptr, '<');
422                                         if (ltreviewptr != NULL) {
423                                                 *ltreviewptr = '\0';
424                                                 linklen = ltreviewptr - ptr;
425                                         }
426
427                                         nbspreviewptr = strstr(ptr, "&nbsp;");
428                                         if (nbspreviewptr != NULL) {
429                                                 /* nbspreviewptr = '\0'; */
430                                                 linklen = nbspreviewptr - ptr;
431                                         }
432                                         if (ltreviewptr != 0)
433                                                 *ltreviewptr = '<';
434
435                                         ptr[len] = linkedchar;
436
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);
442                                         ptr += linklen;
443                                         StrBufAppendPrintf(converted_msg, "</A>");
444                                 }
445                 }
446                 else {
447                         StrBufAppendBufPlain(converted_msg, ptr, 1, 0);
448                         ptr++;
449                 }
450
451                 /**
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.
455                  */
456                 if (*(ptr-1) == '<') {
457                         ++brak;
458                 }
459                 if (*(ptr-1) == '>') {
460                         --brak;
461                         if ((scriptlevel == 0) && (script_start_pos >= 0)) {
462                                 StrBufCutRight(converted_msg, StrLength(converted_msg) - script_start_pos);
463                                 script_start_pos = (-1);
464                         }
465                 }
466                 if (!strncasecmp(ptr, "</A>", 3)) --alevel;
467         }
468
469         /**     uncomment these two lines to override conversion        */
470         /**     memcpy(converted_msg, msg, content_length);             */
471         /**     output_length = content_length;                         */
472
473         /** Output our big pile of markup */
474         StrBufAppendBuf(Target, converted_msg, 0);
475
476 BAIL:   /** A little trailing vertical whitespace... */
477         StrBufAppendPrintf(Target, "<br /><br />\n");
478
479         /** Now give back the memory */
480         FreeStrBuf(&converted_msg);
481         if ((msg != NULL) && (Source == NULL)) free(msg);
482 }
483
484
485
486
487
488
489 /*
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.
492  */
493 void UrlizeText(StrBuf* Target, StrBuf *Source, StrBuf *WrkBuf)
494 {
495         int len, UrlLen, Offset, TrailerLen;
496         const char *start, *end, *pos;
497         
498         FlushStrBuf(Target);
499
500         start = NULL;
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))
505                         start = pos;
506                 else if (!strncasecmp(pos, "ftp://", 6))
507                         start = pos;
508         }
509
510         if (start == NULL) {
511                 StrBufAppendBuf(Target, Source, 0);
512                 return;
513         }
514         FlushStrBuf(WrkBuf);
515
516         for (pos = ChrPtr(Source) + len; pos > start; --pos) {
517                 if (  (!isprint(*pos))
518                    || (isspace(*pos))
519                    || (*pos == '{')
520                    || (*pos == '}')
521                    || (*pos == '|')
522                    || (*pos == '\\')
523                    || (*pos == '^')
524                    || (*pos == '[')
525                    || (*pos == ']')
526                    || (*pos == '`')
527                    || (*pos == '<')
528                    || (*pos == '>')
529                    || (*pos == '(')
530                    || (*pos == ')')
531                 ) {
532                         end = pos;
533                 }
534         }
535         
536         UrlLen = end - start;
537         StrBufAppendBufPlain(WrkBuf, start, UrlLen, 0);
538
539         Offset = start - ChrPtr(Source);
540         if (Offset != 0)
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);
545
546         TrailerLen = StrLength(Source) - (end - ChrPtr(Source));
547         if (TrailerLen > 0)
548                 StrBufAppendBufPlain(Target, end, TrailerLen, 0);
549 }
550 void url(char *buf, size_t bufsize)
551 {
552         int len, UrlLen, Offset, TrailerLen, outpos;
553         char *start, *end, *pos;
554         char urlbuf[SIZ];
555         char outbuf[SIZ];
556
557         start = NULL;
558         len = strlen(buf);
559         if (len > bufsize) {
560                 lprintf(1, "URL: content longer than buffer!");
561                 return;
562         }
563         end = buf + len;
564         for (pos = buf; (pos < end) && (start == NULL); ++pos) {
565                 if (!strncasecmp(pos, "http://", 7))
566                         start = pos;
567                 if (!strncasecmp(pos, "ftp://", 6))
568                         start = pos;
569         }
570
571         if (start == NULL)
572                 return;
573
574         for (pos = buf+len; pos > start; --pos) {
575                 if (  (!isprint(*pos))
576                    || (isspace(*pos))
577                    || (*pos == '{')
578                    || (*pos == '}')
579                    || (*pos == '|')
580                    || (*pos == '\\')
581                    || (*pos == '^')
582                    || (*pos == '[')
583                    || (*pos == ']')
584                    || (*pos == '`')
585                    || (*pos == '<')
586                    || (*pos == '>')
587                    || (*pos == '(')
588                    || (*pos == ')')
589                 ) {
590                         end = pos;
591                 }
592         }
593         
594         UrlLen = end - start;
595         if (UrlLen > sizeof(urlbuf)){
596                 lprintf(1, "URL: content longer than buffer!");
597                 return;
598         }
599         memcpy(urlbuf, start, UrlLen);
600         urlbuf[UrlLen] = '\0';
601
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!");
610                 return;
611         }
612
613         TrailerLen = len - (end - start);
614         if (TrailerLen > 0)
615                 memcpy(outbuf + Offset + outpos, end, TrailerLen);
616         if (Offset + outpos + TrailerLen > bufsize) {
617                 lprintf(1, "URL: content longer than buffer!");
618                 return;
619         }
620         memcpy (buf, outbuf, Offset + outpos + TrailerLen);
621         *(buf + Offset + outpos + TrailerLen) = '\0';
622 }
623
624
625
626
627 /*@}*/