webcit_before_automake is now the trunk
[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 "vcard.h"
12 #include "webserver.h"
13
14
15 /**
16  * \brief       Strip surrounding single or double quotes from a string.
17  *
18  * \param s     String to be stripped.
19  */
20 void stripquotes(char *s)
21 {
22         int len;
23
24         if (!s) return;
25
26         len = strlen(s);
27         if (len < 2) return;
28
29         if ( ( (s[0] == '\"') && (s[len-1] == '\"') ) || ( (s[0] == '\'') && (s[len-1] == '\'') ) ) {
30                 s[len-1] = 0;
31                 strcpy(s, &s[1]);
32         }
33 }
34
35
36 /**
37  * \brief Check to see if a META tag has overridden the declared MIME character set.
38  *
39  * \param charset               Character set name (left unchanged if we don't do anything)
40  * \param meta_http_equiv       Content of the "http-equiv" portion of the META tag
41  * \param meta_content          Content of the "content" portion of the META tag
42  */
43 void extract_charset_from_meta(char *charset, char *meta_http_equiv, char *meta_content)
44 {
45         char *ptr;
46         char buf[64];
47
48         if (!charset) return;
49         if (!meta_http_equiv) return;
50         if (!meta_content) return;
51
52
53         if (strcasecmp(meta_http_equiv, "Content-type")) return;
54
55         ptr = strchr(meta_content, ';');
56         if (!ptr) return;
57
58         safestrncpy(buf, ++ptr, sizeof buf);
59         striplt(buf);
60         if (!strncasecmp(buf, "charset=", 8)) {
61                 strcpy(charset, &buf[8]);
62         }
63 }
64
65
66
67 /**
68  * \brief Sanitize and enhance an HTML message for display.
69  *        Also convert weird character sets to UTF-8 if necessary.
70  *
71  * \param supplied_charset the input charset as declared in the MIME headers
72  */
73 void output_html(char *supplied_charset, int treat_as_wiki) {
74         char buf[SIZ];
75         char *msg;
76         char *ptr;
77         char *msgstart;
78         char *msgend;
79         char *converted_msg;
80         int buffer_length = 1;
81         int line_length = 0;
82         int content_length = 0;
83         int output_length = 0;
84         char new_window[SIZ];
85         int brak = 0;
86         int alevel = 0;
87         int i;
88         int linklen;
89         char charset[128];
90 #ifdef HAVE_ICONV
91         iconv_t ic = (iconv_t)(-1) ;
92         char *ibuf;                   /**< Buffer of characters to be converted */
93         char *obuf;                   /**< Buffer for converted characters      */
94         size_t ibuflen;               /**< Length of input buffer               */
95         size_t obuflen;               /**< Length of output buffer              */
96         char *osav;                   /**< Saved pointer to output buffer       */
97 #endif
98
99         safestrncpy(charset, supplied_charset, sizeof charset);
100         msg = strdup("");
101         sprintf(new_window, "<a target=\"%s\" href=", TARGET);
102
103         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
104                 line_length = strlen(buf);
105                 buffer_length = content_length + line_length + 2;
106                 ptr = realloc(msg, buffer_length);
107                 if (ptr == NULL) {
108                         wprintf("<b>");
109                         wprintf(_("realloc() error! couldn't get %d bytes: %s"),
110                                 buffer_length + 1,
111                                 strerror(errno));
112                         wprintf("</b><br /><br />\n");
113                         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
114                                 /** flush */
115                         }
116                         free(msg);
117                         return;
118                 }
119                 msg = ptr;
120                 strcpy(&msg[content_length], buf);
121                 content_length += line_length;
122                 strcpy(&msg[content_length], "\n");
123                 content_length += 1;
124         }
125
126         /** Do a first pass to isolate the message body */
127         ptr = msg;
128         msgstart = msg;
129         msgend = &msg[content_length];
130
131         while (ptr < msgend) {
132
133                 /** Advance to next tag */
134                 ptr = strchr(ptr, '<');
135                 if ((ptr == NULL) || (ptr >= msgend)) break;
136                 ++ptr;
137                 if ((ptr == NULL) || (ptr >= msgend)) break;
138
139                 /**
140                  *  Look for META tags.  Some messages (particularly in
141                  *  Asian locales) illegally declare a message's character
142                  *  set in the HTML instead of in the MIME headers.  This
143                  *  is wrong but we have to work around it anyway.
144                  */
145                 if (!strncasecmp(ptr, "META", 4)) {
146
147                         char *meta_start;
148                         char *meta_end;
149                         int meta_length;
150                         char *meta;
151                         char *meta_http_equiv;
152                         char *meta_content;
153                         char *spaceptr;
154
155                         meta_start = &ptr[4];
156                         meta_end = strchr(ptr, '>');
157                         if ((meta_end != NULL) && (meta_end <= msgend)) {
158                                 meta_length = meta_end - meta_start + 1;
159                                 meta = malloc(meta_length + 1);
160                                 safestrncpy(meta, meta_start, meta_length);
161                                 meta[meta_length] = 0;
162                                 striplt(meta);
163                                 if (!strncasecmp(meta, "HTTP-EQUIV=", 11)) {
164                                         meta_http_equiv = strdup(&meta[11]);
165                                         spaceptr = strchr(meta_http_equiv, ' ');
166                                         if (spaceptr != NULL) {
167                                                 *spaceptr = 0;
168                                                 meta_content = strdup(++spaceptr);
169                                                 if (!strncasecmp(meta_content, "content=", 8)) {
170                                                         strcpy(meta_content, &meta_content[8]);
171                                                         stripquotes(meta_http_equiv);
172                                                         stripquotes(meta_content);
173                                                         extract_charset_from_meta(charset,
174                                                                 meta_http_equiv, meta_content);
175                                                 }
176                                                 free(meta_content);
177                                         }
178                                         free(meta_http_equiv);
179                                 }
180                                 free(meta);
181                         }
182                 }
183
184                 /**
185                  * Any of these tags cause everything up to and including
186                  * the tag to be removed.
187                  */     
188                 if ( (!strncasecmp(ptr, "HTML", 4))
189                    ||(!strncasecmp(ptr, "HEAD", 4))
190                    ||(!strncasecmp(ptr, "/HEAD", 5))
191                    ||(!strncasecmp(ptr, "BODY", 4)) ) {
192                         ptr = strchr(ptr, '>');
193                         if ((ptr == NULL) || (ptr >= msgend)) break;
194                         ++ptr;
195                         if ((ptr == NULL) || (ptr >= msgend)) break;
196                         msgstart = ptr;
197                 }
198
199                 /**
200                  * Any of these tags cause everything including and following
201                  * the tag to be removed.
202                  */
203                 if ( (!strncasecmp(ptr, "/HTML", 5))
204                    ||(!strncasecmp(ptr, "/BODY", 5)) ) {
205                         --ptr;
206                         msgend = ptr;
207                         strcpy(ptr, "");
208                         
209                 }
210
211                 ++ptr;
212         }
213         if (msgstart > msg) {
214                 strcpy(msg, msgstart);
215         }
216
217         /** Convert foreign character sets to UTF-8 if necessary. */
218 #ifdef HAVE_ICONV
219         if ( (strcasecmp(charset, "us-ascii"))
220            && (strcasecmp(charset, "UTF-8"))
221            && (strcasecmp(charset, ""))
222         ) {
223                 lprintf(9, "Converting %s to UTF-8\n", charset);
224                 ic = ctdl_iconv_open("UTF-8", charset);
225                 if (ic == (iconv_t)(-1) ) {
226                         lprintf(5, "%s:%d iconv_open() failed: %s\n",
227                                 __FILE__, __LINE__, strerror(errno));
228                 }
229         }
230         if (ic != (iconv_t)(-1) ) {
231                 ibuf = msg;
232                 ibuflen = content_length;
233                 obuflen = content_length + (content_length / 2) ;
234                 obuf = (char *) malloc(obuflen);
235                 osav = obuf;
236                 iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
237                 content_length = content_length + (content_length / 2) - obuflen;
238                 osav[content_length] = 0;
239                 free(msg);
240                 msg = osav;
241                 iconv_close(ic);
242         }
243 #endif
244
245         /**
246          *      At this point, the message has been stripped down to
247          *      only the content inside the <BODY></BODY> tags, and has
248          *      been converted to UTF-8 if it was originally in a foreign
249          *      character set.  The text is also guaranteed to be null
250          *      terminated now.
251          */
252
253         /** Now go through the message, parsing tags as necessary. */
254         converted_msg = malloc(content_length);
255         strcpy(converted_msg, "");
256         ptr = msg;
257         msgend = strchr(msg, 0);
258         while (ptr < msgend) {
259
260                 /**
261                  * Change mailto: links to WebCit mail, by replacing the
262                  * link with one that points back to our mail room.  Due to
263                  * the way we parse URL's, it'll even handle mailto: links
264                  * that have "?subject=" in them.
265                  */
266                 if (!strncasecmp(ptr, "<a href=\"mailto:", 16)) {
267                         content_length += 64;
268                         converted_msg = realloc(converted_msg, content_length);
269                         sprintf(&converted_msg[output_length],
270                                 "<a href=\"display_enter"
271                                 "?force_room=_MAIL_&recp=");
272                         output_length += 47;
273                         ptr = &ptr[16];
274                         ++alevel;
275                 }
276                 /** Make external links open in a separate window */
277                 else if (!strncasecmp(ptr, "<a href=\"", 9)) {
278                         ++alevel;
279                         if ( ((strchr(ptr, ':') < strchr(ptr, '/')))
280                              &&  ((strchr(ptr, '/') < strchr(ptr, '>'))) 
281                              ) {
282                                 /* open external links to new window */
283                                 content_length += 64;
284                                 converted_msg = realloc(converted_msg, content_length);
285                                 sprintf(&converted_msg[output_length], new_window);
286                                 output_length += strlen(new_window);
287                                 ptr = &ptr[8];
288                         }
289                         else if ( (treat_as_wiki) && (strncasecmp(ptr, "<a href=\"wiki?", 14)) ) {
290                                 content_length += 64;
291                                 converted_msg = realloc(converted_msg, content_length);
292                                 sprintf(&converted_msg[output_length], "<a href=\"wiki?page=");
293                                 output_length += 19;
294                                 ptr = &ptr[9];
295                         }
296                         else {
297                                 sprintf(&converted_msg[output_length], "<a href=\"");
298                                 output_length += 9;
299                                 ptr = &ptr[9];
300                         }
301                 }
302                 /**
303                  * Turn anything that looks like a URL into a real link, as long
304                  * as it's not inside a tag already
305                  */
306                 else if ( (brak == 0) && (alevel == 0)
307                      && (!strncasecmp(ptr, "http://", 7))) {
308                                 linklen = 0;
309                                 /** Find the end of the link */
310                                 for (i=0; i<=strlen(ptr); ++i) {
311                                         if ((ptr[i]==0)
312                                            ||(isspace(ptr[i]))
313                                            ||(ptr[i]==10)
314                                            ||(ptr[i]==13)
315                                            ||(ptr[i]=='(')
316                                            ||(ptr[i]==')')
317                                            ||(ptr[i]=='<')
318                                            ||(ptr[i]=='>')
319                                            ||(ptr[i]=='[')
320                                            ||(ptr[i]==']')
321                                         ) linklen = i;
322                                         if (linklen > 0) break;
323                                 }
324                                 if (linklen > 0) {
325                                         content_length += (32 + linklen);
326                                         converted_msg = realloc(converted_msg, content_length);
327                                         sprintf(&converted_msg[output_length], new_window);
328                                         output_length += strlen(new_window);
329                                         converted_msg[output_length] = '\"';
330                                         converted_msg[++output_length] = 0;
331                                         for (i=0; i<linklen; ++i) {
332                                                 converted_msg[output_length] = ptr[i];
333                                                 converted_msg[++output_length] = 0;
334                                         }
335                                         sprintf(&converted_msg[output_length], "\">");
336                                         output_length += 2;
337                                         for (i=0; i<linklen; ++i) {
338                                                 converted_msg[output_length] = *ptr++;
339                                                 converted_msg[++output_length] = 0;
340                                         }
341                                         sprintf(&converted_msg[output_length], "</A>");
342                                         output_length += 4;
343                                 }
344                 }
345                 else {
346                         /**
347                          * We need to know when we're inside a tag,
348                          * so we don't turn things that look like URL's into
349                          * links, when they're already links - or image sources.
350                          */
351                         if (*ptr == '<') ++brak;
352                         if (*ptr == '>') --brak;
353                         if (!strncasecmp(ptr, "</A>", 3)) --alevel;
354                         converted_msg[output_length] = *ptr++;
355                         converted_msg[++output_length] = 0;
356                 }
357         }
358
359         /**     uncomment these two lines to override conversion        */
360         /**     memcpy(converted_msg, msg, content_length);             */
361         /**     output_length = content_length;                         */
362
363         /** Output our big pile of markup */
364         client_write(converted_msg, output_length);
365
366         /** A little trailing vertical whitespace... */
367         wprintf("<br /><br />\n");
368
369         /** Now give back the memory */
370         free(converted_msg);
371         free(msg);
372 }
373
374 /*@}*/