* Doxygen groups. Sorted the files into groups. so now we have a nice structure
[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 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, int treat_as_wiki) {
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 external links open in a separate window */
155                 else if (!strncasecmp(ptr, "<a href=", 8)) {
156                         ++alevel;
157                         if ( ((strchr(ptr, ':') < strchr(ptr, '/')))
158                              &&  ((strchr(ptr, '/') < strchr(ptr, '>'))) 
159                              ) {
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);
165                                 ptr = &ptr[8];
166                         }
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=");
172                                 output_length += 19;
173                                 ptr = &ptr[9];
174                         }
175                         else {
176                                 sprintf(&converted_msg[output_length], "<a href=");
177                                 output_length += 8;
178                                 ptr = &ptr[8];
179                         }
180                 }
181                 /**
182                  * Turn anything that looks like a URL into a real link, as long
183                  * as it's not inside a tag already
184                  */
185                 else if ( (brak == 0) && (alevel == 0)
186                      && (!strncasecmp(ptr, "http://", 7))) {
187                                 linklen = 0;
188                                 /** Find the end of the link */
189                                 for (i=0; i<=strlen(ptr); ++i) {
190                                         if ((ptr[i]==0)
191                                            ||(isspace(ptr[i]))
192                                            ||(ptr[i]==10)
193                                            ||(ptr[i]==13)
194                                            ||(ptr[i]=='(')
195                                            ||(ptr[i]==')')
196                                            ||(ptr[i]=='<')
197                                            ||(ptr[i]=='>')
198                                            ||(ptr[i]=='[')
199                                            ||(ptr[i]==']')
200                                         ) linklen = i;
201                                         if (linklen > 0) break;
202                                 }
203                                 if (linklen > 0) {
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;
213                                         }
214                                         sprintf(&converted_msg[output_length], "\">");
215                                         output_length += 2;
216                                         for (i=0; i<linklen; ++i) {
217                                                 converted_msg[output_length] = *ptr++;
218                                                 converted_msg[++output_length] = 0;
219                                         }
220                                         sprintf(&converted_msg[output_length], "</A>");
221                                         output_length += 4;
222                                 }
223                 }
224                 else {
225                         /**
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.
229                          */
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;
235                 }
236         }
237
238         /** Output our big pile of markup */
239         client_write(converted_msg, output_length);
240
241         /** A little trailing vertical whitespace... */
242         wprintf("<br /><br />\n");
243
244         /** Now give back the memory */
245         free(converted_msg);
246         free(msg);
247 }
248
249 /*@}*/