]> code.citadel.org Git - citadel.git/blobdiff - webcit/html2html.c
* Finished gzip compression of dynamic pages (when browser supports it)
[citadel.git] / webcit / html2html.c
index e754587b2b96148f8b79f61b84443f79400c0bdd..a0a976ffc3d58cf776af23fbd93f2bcd787afe32 100644 (file)
@@ -37,31 +37,47 @@ void output_html(void) {
        char *ptr;
        char *msgstart;
        char *msgend;
-       int total_length = 1;
+       char *converted_msg;
+       int buffer_length = 1;
        int line_length = 0;
+       int content_length = 0;
+       int output_length = 0;
+       char new_window[SIZ];
+       int brak = 0;
+       int i;
+       int linklen;
 
        msg = strdup("");
-       msgstart = msg;
-       msgend = msg;
+       sprintf(new_window, "<A TARGET=\"%s\" HREF=", TARGET);
 
        while (serv_gets(buf), strcmp(buf, "000")) {
                line_length = strlen(buf);
-               total_length = total_length + line_length + 1;
-               msg = realloc(msg, total_length);
-               strcpy(msgend, buf);
-               strcat(msgend, "\n");
-               msgend = &msgend[line_length + 1];
+               buffer_length = content_length + line_length + 2;
+               msg = realloc(msg, buffer_length);
+               if (msg == NULL) {
+                       wprintf("<B>realloc() error!  "
+                               "couldn't get %d bytes: %s</B><BR><BR>\n",
+                               buffer_length + 1,
+                               strerror(errno));
+                       return;
+               }
+               strcpy(&msg[content_length], buf);
+               content_length += line_length;
+               strcpy(&msg[content_length], "\n");
+               content_length += 1;
        }
 
        ptr = msg;
        msgstart = msg;
-       msgend = &msg[total_length];
+       msgend = &msg[content_length];
 
        while (ptr < msgend) {
 
                /* Advance to next tag */
                ptr = strchr(ptr, '<');
+               if ((ptr == NULL) || (ptr >= msgend)) break;
                ++ptr;
+               if ((ptr == NULL) || (ptr >= msgend)) break;
 
                /* Any of these tags cause everything up to and including
                 * the tag to be removed.
@@ -71,7 +87,9 @@ void output_html(void) {
                   ||(!strncasecmp(ptr, "/HEAD", 5))
                   ||(!strncasecmp(ptr, "BODY", 4)) ) {
                        ptr = strchr(ptr, '>');
+                       if ((ptr == NULL) || (ptr >= msgend)) break;
                        ++ptr;
+                       if ((ptr == NULL) || (ptr >= msgend)) break;
                        msgstart = ptr;
                }
 
@@ -89,12 +107,72 @@ void output_html(void) {
                ++ptr;
        }
 
-       write(WC->http_sock, msgstart, strlen(msgstart));
+       converted_msg = malloc(content_length);
+       strcpy(converted_msg, "");
+       ptr = msgstart;
+       while (ptr < msgend) {
+               /* Make links open in a separate window */
+               if (!strncasecmp(ptr, "<A HREF=", 8)) {
+                       content_length += 64;
+                       converted_msg = realloc(converted_msg, content_length);
+                       sprintf(&converted_msg[output_length], new_window);
+                       output_length += strlen(new_window);
+                       ptr = &ptr[8];
+                       ++brak;
+               }
+               /* Turn loose URL's into hot links */
+               else if ( (brak == 0)
+                    && (!strncasecmp(ptr, "http://", 7))) {
+                               linklen = 0;
+                               /* Find the end of the link */
+                               for (i=0; i<=strlen(ptr); ++i) {
+                                       if ((ptr[i]==0)
+                                          ||(isspace(ptr[i]))
+                                          ||(ptr[i]==10)
+                                          ||(ptr[i]==13)
+                                          ||(ptr[i]==')')
+                                          ||(ptr[i]=='>')
+                                          ||(ptr[i]==']')
+                                       ) linklen = i;
+                                       if (linklen > 0) break;
+                               }
+                               if (linklen > 0) {
+                                       content_length += (32 + linklen);
+                                       converted_msg = realloc(converted_msg, content_length);
+                                       sprintf(&converted_msg[output_length], new_window);
+                                       output_length += strlen(new_window);
+                                       converted_msg[output_length] = '\"';
+                                       converted_msg[++output_length] = 0;
+                                       for (i=0; i<linklen; ++i) {
+                                               converted_msg[output_length] = ptr[i];
+                                               converted_msg[++output_length] = 0;
+                                       }
+                                       sprintf(&converted_msg[output_length], "\">");
+                                       output_length += 2;
+                                       for (i=0; i<linklen; ++i) {
+                                               converted_msg[output_length] = *ptr++;
+                                               converted_msg[++output_length] = 0;
+                                       }
+                                       sprintf(&converted_msg[output_length], "</A>");
+                                       output_length += 4;
+                               }
+               }
+               else {
+                       if (*ptr == '<') ++brak;
+                       if (*ptr == '>') --brak;
+                       converted_msg[output_length] = *ptr++;
+                       converted_msg[++output_length] = 0;
+               }
+       }
+
+       /* Output our big pile of markup */
+       http_write(WC->http_sock, converted_msg, output_length);
 
        /* A little trailing vertical whitespace... */
        wprintf("<BR><BR>\n");
 
        /* Now give back the memory */
+       free(converted_msg);
        free(msg);
 }