more doxygen doku.
[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  *
8  */
9 /*@{*/
10 #include "webcit.h"
11 #include "vcard.h"
12 #include "webserver.h"
13
14
15 /**
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
19  */
20 void output_html(char *charset) {
21         char buf[SIZ];
22         char *msg;
23         char *ptr;
24         char *msgstart;
25         char *msgend;
26         char *converted_msg;
27         int buffer_length = 1;
28         int line_length = 0;
29         int content_length = 0;
30         int output_length = 0;
31         char new_window[SIZ];
32         int brak = 0;
33         int alevel = 0;
34         int i;
35         int linklen;
36 #ifdef HAVE_ICONV
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       */
43 #endif
44
45         msg = strdup("");
46         sprintf(new_window, "<a target=\"%s\" href=", TARGET);
47
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);
52                 if (msg == NULL) {
53                         wprintf("<b>");
54                         wprintf(_("realloc() error! couldn't get %d bytes: %s"),
55                                 buffer_length + 1,
56                                 strerror(errno));
57                         wprintf("</b><br /><br />\n");
58                         return;
59                 }
60                 strcpy(&msg[content_length], buf);
61                 content_length += line_length;
62                 strcpy(&msg[content_length], "\n");
63                 content_length += 1;
64         }
65
66 #ifdef HAVE_ICONV
67         if ( (strcasecmp(charset, "us-ascii"))
68            && (strcasecmp(charset, "UTF-8"))
69            && (strcasecmp(charset, ""))
70         ) {
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));
75                 }
76         }
77         if (ic != (iconv_t)(-1) ) {
78                 ibuf = msg;
79                 ibuflen = content_length;
80                 obuflen = content_length + (content_length / 2) ;
81                 obuf = (char *) malloc(obuflen);
82                 osav = obuf;
83                 iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
84                 content_length = content_length + (content_length / 2) - obuflen;
85                 osav[content_length] = 0;
86                 free(msg);
87                 msg = osav;
88                 iconv_close(ic);
89         }
90 #endif
91
92         ptr = msg;
93         msgstart = msg;
94         msgend = &msg[content_length];
95
96         while (ptr < msgend) {
97
98                 /** Advance to next tag */
99                 ptr = strchr(ptr, '<');
100                 if ((ptr == NULL) || (ptr >= msgend)) break;
101                 ++ptr;
102                 if ((ptr == NULL) || (ptr >= msgend)) break;
103
104                 /**
105                  * Any of these tags cause everything up to and including
106                  * the tag to be removed.
107                  */     
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;
114                         ++ptr;
115                         if ((ptr == NULL) || (ptr >= msgend)) break;
116                         msgstart = ptr;
117                 }
118
119                 /**
120                  * Any of these tags cause everything including and following
121                  * the tag to be removed.
122                  */
123                 if ( (!strncasecmp(ptr, "/HTML", 5))
124                    ||(!strncasecmp(ptr, "/BODY", 5)) ) {
125                         --ptr;
126                         msgend = ptr;
127                         strcpy(ptr, "");
128                         
129                 }
130
131                 ++ptr;
132         }
133
134         converted_msg = malloc(content_length);
135         strcpy(converted_msg, "");
136         ptr = msgstart;
137         while (ptr < msgend) {
138                 /**
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.
143                  */
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=");
150                         output_length += 47;
151                         ptr = &ptr[16];
152                         ++alevel;
153                 }
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);
160                         ptr = &ptr[8];
161                         ++alevel;
162                 }
163                 /**
164                  * Turn anything that looks like a URL into a real link, as long
165                  * as it's not inside a tag already
166                  */
167                 else if ( (brak == 0) && (alevel == 0)
168                      && (!strncasecmp(ptr, "http://", 7))) {
169                                 linklen = 0;
170                                 /** Find the end of the link */
171                                 for (i=0; i<=strlen(ptr); ++i) {
172                                         if ((ptr[i]==0)
173                                            ||(isspace(ptr[i]))
174                                            ||(ptr[i]==10)
175                                            ||(ptr[i]==13)
176                                            ||(ptr[i]=='(')
177                                            ||(ptr[i]==')')
178                                            ||(ptr[i]=='<')
179                                            ||(ptr[i]=='>')
180                                            ||(ptr[i]=='[')
181                                            ||(ptr[i]==']')
182                                         ) linklen = i;
183                                         if (linklen > 0) break;
184                                 }
185                                 if (linklen > 0) {
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;
195                                         }
196                                         sprintf(&converted_msg[output_length], "\">");
197                                         output_length += 2;
198                                         for (i=0; i<linklen; ++i) {
199                                                 converted_msg[output_length] = *ptr++;
200                                                 converted_msg[++output_length] = 0;
201                                         }
202                                         sprintf(&converted_msg[output_length], "</A>");
203                                         output_length += 4;
204                                 }
205                 }
206                 else {
207                         /**
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.
211                          */
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;
217                 }
218         }
219
220         /** Output our big pile of markup */
221         client_write(converted_msg, output_length);
222
223         /** A little trailing vertical whitespace... */
224         wprintf("<br /><br />\n");
225
226         /** Now give back the memory */
227         free(converted_msg);
228         free(msg);
229 }
230
231 /*@}*/