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, int treat_as_wiki) {
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 external links open in a separate window */
155 else if (!strncasecmp(ptr, "<a href=", 8)) {
157 if ( ((strchr(ptr, ':') < strchr(ptr, '/')))
158 && ((strchr(ptr, '/') < strchr(ptr, '>')))
160 /* open external links to new window */
161 content_length += 64;
162 converted_msg = realloc(converted_msg, content_length);
163 sprintf(&converted_msg[output_length], new_window);
164 output_length += strlen(new_window);
167 else if ( (treat_as_wiki) && (strncasecmp(ptr, "<a href=\"wiki?", 14)) ) {
168 lprintf(9, "converting wiki link\n");
169 content_length += 64;
170 converted_msg = realloc(converted_msg, content_length);
171 sprintf(&converted_msg[output_length], "<a href=\"wiki?page=");
176 sprintf(&converted_msg[output_length], "<a href=");
182 * Turn anything that looks like a URL into a real link, as long
183 * as it's not inside a tag already
185 else if ( (brak == 0) && (alevel == 0)
186 && (!strncasecmp(ptr, "http://", 7))) {
188 /** Find the end of the link */
189 for (i=0; i<=strlen(ptr); ++i) {
201 if (linklen > 0) break;
204 content_length += (32 + linklen);
205 converted_msg = realloc(converted_msg, content_length);
206 sprintf(&converted_msg[output_length], new_window);
207 output_length += strlen(new_window);
208 converted_msg[output_length] = '\"';
209 converted_msg[++output_length] = 0;
210 for (i=0; i<linklen; ++i) {
211 converted_msg[output_length] = ptr[i];
212 converted_msg[++output_length] = 0;
214 sprintf(&converted_msg[output_length], "\">");
216 for (i=0; i<linklen; ++i) {
217 converted_msg[output_length] = *ptr++;
218 converted_msg[++output_length] = 0;
220 sprintf(&converted_msg[output_length], "</A>");
226 * We need to know when we're inside a tag,
227 * so we don't turn things that look like URL's into
228 * links, when they're already links - or image sources.
230 if (*ptr == '<') ++brak;
231 if (*ptr == '>') --brak;
232 if (!strncasecmp(ptr, "</A>", 3)) --alevel;
233 converted_msg[output_length] = *ptr++;
234 converted_msg[++output_length] = 0;
238 /** Output our big pile of markup */
239 client_write(converted_msg, output_length);
241 /** A little trailing vertical whitespace... */
242 wprintf("<br /><br />\n");
244 /** Now give back the memory */