5 * \defgroup HTML2HTML Output an HTML message, modifying it slightly to make sure it plays nice
6 * with the rest of our web framework.
12 #include "webserver.h"
16 * \brief Sanitize and enhance an HTML message for display.
17 * Also convert weird character sets to UTF-8 if necessary.
18 * \param charset the input charset
20 void output_html(char *charset) {
27 int buffer_length = 1;
29 int content_length = 0;
30 int output_length = 0;
37 iconv_t ic = (iconv_t)(-1) ;
38 char *ibuf; /**< Buffer of characters to be converted */
39 char *obuf; /**< Buffer for converted characters */
40 size_t ibuflen; /**< Length of input buffer */
41 size_t obuflen; /**< Length of output buffer */
42 char *osav; /**< Saved pointer to output buffer */
46 sprintf(new_window, "<a target=\"%s\" href=", TARGET);
48 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
49 line_length = strlen(buf);
50 buffer_length = content_length + line_length + 2;
51 msg = realloc(msg, buffer_length);
54 wprintf(_("realloc() error! couldn't get %d bytes: %s"),
57 wprintf("</b><br /><br />\n");
60 strcpy(&msg[content_length], buf);
61 content_length += line_length;
62 strcpy(&msg[content_length], "\n");
67 if ( (strcasecmp(charset, "us-ascii"))
68 && (strcasecmp(charset, "UTF-8"))
69 && (strcasecmp(charset, ""))
71 ic = iconv_open("UTF-8", charset);
72 if (ic == (iconv_t)(-1) ) {
73 lprintf(5, "%s:%d iconv_open() failed: %s\n",
74 __FILE__, __LINE__, strerror(errno));
77 if (ic != (iconv_t)(-1) ) {
79 ibuflen = content_length;
80 obuflen = content_length + (content_length / 2) ;
81 obuf = (char *) malloc(obuflen);
83 iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
84 content_length = content_length + (content_length / 2) - obuflen;
85 osav[content_length] = 0;
94 msgend = &msg[content_length];
96 while (ptr < msgend) {
98 /** Advance to next tag */
99 ptr = strchr(ptr, '<');
100 if ((ptr == NULL) || (ptr >= msgend)) break;
102 if ((ptr == NULL) || (ptr >= msgend)) break;
105 * Any of these tags cause everything up to and including
106 * the tag to be removed.
108 if ( (!strncasecmp(ptr, "HTML", 4))
109 ||(!strncasecmp(ptr, "HEAD", 4))
110 ||(!strncasecmp(ptr, "/HEAD", 5))
111 ||(!strncasecmp(ptr, "BODY", 4)) ) {
112 ptr = strchr(ptr, '>');
113 if ((ptr == NULL) || (ptr >= msgend)) break;
115 if ((ptr == NULL) || (ptr >= msgend)) break;
120 * Any of these tags cause everything including and following
121 * the tag to be removed.
123 if ( (!strncasecmp(ptr, "/HTML", 5))
124 ||(!strncasecmp(ptr, "/BODY", 5)) ) {
134 converted_msg = malloc(content_length);
135 strcpy(converted_msg, "");
137 while (ptr < msgend) {
139 * Change mailto: links to WebCit mail, by replacing the
140 * link with one that points back to our mail room. Due to
141 * the way we parse URL's, it'll even handle mailto: links
142 * that have "?subject=" in them.
144 if (!strncasecmp(ptr, "<a href=\"mailto:", 16)) {
145 content_length += 64;
146 converted_msg = realloc(converted_msg, content_length);
147 sprintf(&converted_msg[output_length],
148 "<a href=\"display_enter"
149 "?force_room=_MAIL_&recp=");
154 /** Make links open in a separate window */
155 else if (!strncasecmp(ptr, "<a href=", 8)) {
156 content_length += 64;
157 converted_msg = realloc(converted_msg, content_length);
158 sprintf(&converted_msg[output_length], new_window);
159 output_length += strlen(new_window);
164 * Turn anything that looks like a URL into a real link, as long
165 * as it's not inside a tag already
167 else if ( (brak == 0) && (alevel == 0)
168 && (!strncasecmp(ptr, "http://", 7))) {
170 /** Find the end of the link */
171 for (i=0; i<=strlen(ptr); ++i) {
183 if (linklen > 0) break;
186 content_length += (32 + linklen);
187 converted_msg = realloc(converted_msg, content_length);
188 sprintf(&converted_msg[output_length], new_window);
189 output_length += strlen(new_window);
190 converted_msg[output_length] = '\"';
191 converted_msg[++output_length] = 0;
192 for (i=0; i<linklen; ++i) {
193 converted_msg[output_length] = ptr[i];
194 converted_msg[++output_length] = 0;
196 sprintf(&converted_msg[output_length], "\">");
198 for (i=0; i<linklen; ++i) {
199 converted_msg[output_length] = *ptr++;
200 converted_msg[++output_length] = 0;
202 sprintf(&converted_msg[output_length], "</A>");
208 * We need to know when we're inside a tag,
209 * so we don't turn things that look like URL's into
210 * links, when they're already links - or image sources.
212 if (*ptr == '<') ++brak;
213 if (*ptr == '>') --brak;
214 if (!strncasecmp(ptr, "</A>", 3)) --alevel;
215 converted_msg[output_length] = *ptr++;
216 converted_msg[++output_length] = 0;
220 /** Output our big pile of markup */
221 client_write(converted_msg, output_length);
223 /** A little trailing vertical whitespace... */
224 wprintf("<br /><br />\n");
226 /** Now give back the memory */