4ac9a8fec9ecc9afc0ae9639dabd836c734e6b03
[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         int total_length = 1;
41         int line_length = 0;
42
43         msg = strdup("");
44         msgstart = msg;
45         msgend = msg;
46
47         while (serv_gets(buf), strcmp(buf, "000")) {
48                 line_length = strlen(buf);
49                 total_length = total_length + line_length + 1;
50                 msg = realloc(msg, total_length + 1);
51                 strcpy(msgend, buf);
52                 msgend[line_length++] = '\n' ;
53                 msgend[line_length] = 0;
54                 msgend = &msgend[line_length];
55         }
56
57         ptr = msg;
58         msgstart = msg;
59         /* msgend is already set correctly */
60
61         while (ptr < msgend) {
62
63                 /* Advance to next tag */
64                 ptr = strchr(ptr, '<');
65                 ++ptr;
66                 if ((ptr == NULL) || (ptr >= msgend)) break;
67
68                 /* Any of these tags cause everything up to and including
69                  * the tag to be removed.
70                  */     
71                 if ( (!strncasecmp(ptr, "HTML", 4))
72                    ||(!strncasecmp(ptr, "HEAD", 4))
73                    ||(!strncasecmp(ptr, "/HEAD", 5))
74                    ||(!strncasecmp(ptr, "BODY", 4)) ) {
75                         ptr = strchr(ptr, '>');
76                         if ((ptr == NULL) || (ptr >= msgend)) break;
77                         ++ptr;
78                         if ((ptr == NULL) || (ptr >= msgend)) break;
79                         msgstart = ptr;
80                 }
81
82                 /* Any of these tags cause everything including and following
83                  * the tag to be removed.
84                  */
85                 if ( (!strncasecmp(ptr, "/HTML", 5))
86                    ||(!strncasecmp(ptr, "/BODY", 5)) ) {
87                         --ptr;
88                         msgend = ptr;
89                         strcpy(ptr, "");
90                         
91                 }
92
93                 ++ptr;
94         }
95
96         write(WC->http_sock, msgstart, strlen(msgstart));
97
98         /* A little trailing vertical whitespace... */
99         wprintf("<BR><BR>\n");
100
101         /* Now give back the memory */
102         free(msg);
103 }
104