* Minor cleanup of previous changes
[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);
51                 strcpy(msgend, buf);
52                 strcat(msgend, "\n");
53                 msgend = &msgend[line_length + 1];
54         }
55
56         ptr = msg;
57         msgstart = msg;
58         msgend = &msg[total_length];
59
60         while (ptr < msgend) {
61
62                 /* Advance to next tag */
63                 ptr = strchr(ptr, '<');
64                 ++ptr;
65
66                 /* Any of these tags cause everything up to and including
67                  * the tag to be removed.
68                  */     
69                 if ( (!strncasecmp(ptr, "HTML", 4))
70                    ||(!strncasecmp(ptr, "HEAD", 4))
71                    ||(!strncasecmp(ptr, "/HEAD", 5))
72                    ||(!strncasecmp(ptr, "BODY", 4)) ) {
73                         ptr = strchr(ptr, '>');
74                         ++ptr;
75                         msgstart = ptr;
76                 }
77
78                 /* Any of these tags cause everything including and following
79                  * the tag to be removed.
80                  */
81                 if ( (!strncasecmp(ptr, "/HTML", 5))
82                    ||(!strncasecmp(ptr, "/BODY", 5)) ) {
83                         --ptr;
84                         msgend = ptr;
85                         strcpy(ptr, "");
86                         
87                 }
88
89                 ++ptr;
90         }
91
92         write(WC->http_sock, msgstart, strlen(msgstart));
93
94         /* A little trailing vertical whitespace... */
95         wprintf("<BR><BR>\n");
96
97         /* Now give back the memory */
98         free(msg);
99 }
100