* Removed the gzip compression stuff due to bugs in Internet Explorer.
[citadel.git] / webcit / html2html.c
1 /*
2  * $Id$
3  *
4  * Output an HTML message, modifying it slightly to make sure it plays nice
5  * with the rest of our web framework.
6  *
7  */
8
9 #include <ctype.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <stdio.h>
13 #include <fcntl.h>
14 #include <signal.h>
15 #include <sys/types.h>
16 #include <sys/wait.h>
17 #include <sys/socket.h>
18 #include <limits.h>
19 #include <netinet/in.h>
20 #include <netdb.h>
21 #include <string.h>
22 #include <pwd.h>
23 #include <errno.h>
24 #include <stdarg.h>
25 #include <pthread.h>
26 #include <signal.h>
27 #include "webcit.h"
28 #include "vcard.h"
29 #include "webserver.h"
30
31
32 /*
33  */
34 void output_html(void) {
35         char buf[SIZ];
36         char *msg;
37         char *ptr;
38         char *msgstart;
39         char *msgend;
40         char *converted_msg;
41         int buffer_length = 1;
42         int line_length = 0;
43         int content_length = 0;
44         int output_length = 0;
45         char new_window[SIZ];
46         int brak = 0;
47         int i;
48         int linklen;
49
50         msg = strdup("");
51         sprintf(new_window, "<A TARGET=\"%s\" HREF=", TARGET);
52
53         while (serv_gets(buf), strcmp(buf, "000")) {
54                 line_length = strlen(buf);
55                 buffer_length = content_length + line_length + 2;
56                 msg = realloc(msg, buffer_length);
57                 if (msg == NULL) {
58                         wprintf("<B>realloc() error!  "
59                                 "couldn't get %d bytes: %s</B><BR><BR>\n",
60                                 buffer_length + 1,
61                                 strerror(errno));
62                         return;
63                 }
64                 strcpy(&msg[content_length], buf);
65                 content_length += line_length;
66                 strcpy(&msg[content_length], "\n");
67                 content_length += 1;
68         }
69
70         ptr = msg;
71         msgstart = msg;
72         msgend = &msg[content_length];
73
74         while (ptr < msgend) {
75
76                 /* Advance to next tag */
77                 ptr = strchr(ptr, '<');
78                 if ((ptr == NULL) || (ptr >= msgend)) break;
79                 ++ptr;
80                 if ((ptr == NULL) || (ptr >= msgend)) break;
81
82                 /* Any of these tags cause everything up to and including
83                  * the tag to be removed.
84                  */     
85                 if ( (!strncasecmp(ptr, "HTML", 4))
86                    ||(!strncasecmp(ptr, "HEAD", 4))
87                    ||(!strncasecmp(ptr, "/HEAD", 5))
88                    ||(!strncasecmp(ptr, "BODY", 4)) ) {
89                         ptr = strchr(ptr, '>');
90                         if ((ptr == NULL) || (ptr >= msgend)) break;
91                         ++ptr;
92                         if ((ptr == NULL) || (ptr >= msgend)) break;
93                         msgstart = ptr;
94                 }
95
96                 /* Any of these tags cause everything including and following
97                  * the tag to be removed.
98                  */
99                 if ( (!strncasecmp(ptr, "/HTML", 5))
100                    ||(!strncasecmp(ptr, "/BODY", 5)) ) {
101                         --ptr;
102                         msgend = ptr;
103                         strcpy(ptr, "");
104                         
105                 }
106
107                 ++ptr;
108         }
109
110         converted_msg = malloc(content_length);
111         strcpy(converted_msg, "");
112         ptr = msgstart;
113         while (ptr < msgend) {
114                 /* Make links open in a separate window */
115                 if (!strncasecmp(ptr, "<A HREF=", 8)) {
116                         content_length += 64;
117                         converted_msg = realloc(converted_msg, content_length);
118                         sprintf(&converted_msg[output_length], new_window);
119                         output_length += strlen(new_window);
120                         ptr = &ptr[8];
121                         ++brak;
122                 }
123                 /* Turn loose URL's into hot links */
124                 else if ( (brak == 0)
125                      && (!strncasecmp(ptr, "http://", 7))) {
126                                 linklen = 0;
127                                 /* Find the end of the link */
128                                 for (i=0; i<=strlen(ptr); ++i) {
129                                         if ((ptr[i]==0)
130                                            ||(isspace(ptr[i]))
131                                            ||(ptr[i]==10)
132                                            ||(ptr[i]==13)
133                                            ||(ptr[i]==')')
134                                            ||(ptr[i]=='>')
135                                            ||(ptr[i]==']')
136                                         ) linklen = i;
137                                         if (linklen > 0) break;
138                                 }
139                                 if (linklen > 0) {
140                                         content_length += (32 + linklen);
141                                         converted_msg = realloc(converted_msg, content_length);
142                                         sprintf(&converted_msg[output_length], new_window);
143                                         output_length += strlen(new_window);
144                                         converted_msg[output_length] = '\"';
145                                         converted_msg[++output_length] = 0;
146                                         for (i=0; i<linklen; ++i) {
147                                                 converted_msg[output_length] = ptr[i];
148                                                 converted_msg[++output_length] = 0;
149                                         }
150                                         sprintf(&converted_msg[output_length], "\">");
151                                         output_length += 2;
152                                         for (i=0; i<linklen; ++i) {
153                                                 converted_msg[output_length] = *ptr++;
154                                                 converted_msg[++output_length] = 0;
155                                         }
156                                         sprintf(&converted_msg[output_length], "</A>");
157                                         output_length += 4;
158                                 }
159                 }
160                 else {
161                         if (*ptr == '<') ++brak;
162                         if (*ptr == '>') --brak;
163                         converted_msg[output_length] = *ptr++;
164                         converted_msg[++output_length] = 0;
165                 }
166         }
167
168         /* Output our big pile of markup */
169         write(WC->http_sock, converted_msg, output_length);
170
171         /* A little trailing vertical whitespace... */
172         wprintf("<BR><BR>\n");
173
174         /* Now give back the memory */
175         free(converted_msg);
176         free(msg);
177 }
178