From fc590d9608dca5c9046c8f60177dc86ec09e68f6 Mon Sep 17 00:00:00 2001 From: Andrew Pamment Date: Sun, 21 Oct 2018 18:24:26 +1000 Subject: [PATCH] New tree thing for www. --- src/GNUmakefile.common | 4 +- src/www_blog.c | 98 +++++++++++++++++++++++++++++--------- src/www_tree.c | 105 +++++++++++++++++++++++++++++++++++++++++ src/www_tree.h | 21 +++++++++ 4 files changed, 203 insertions(+), 25 deletions(-) create mode 100644 src/www_tree.c create mode 100644 src/www_tree.h diff --git a/src/GNUmakefile.common b/src/GNUmakefile.common index f293532..c38f6e6 100644 --- a/src/GNUmakefile.common +++ b/src/GNUmakefile.common @@ -57,7 +57,7 @@ OBJS:= inih/ini.o bbs.o main.o users.o main_menu.o mail_menu.o \ nodelist.o blog.o util.o stralloc/stralloc.o ${EXTRAOBJS} WWWOBJS:= ../deps/aha/aha.o ../deps/hashids/hashids.o www.o www_email.o \ - www_msgs.o www_last10.o www_blog.o www_files.o ${OBJS} + www_msgs.o www_last10.o www_blog.o www_files.o www_tree.o ${OBJS} ifeq ($(MAKECMDGOALS), www) CFLAGS+= ${CFLAGS} -Istralloc -I${DEPSDIR}/libb64-1.2/include -DENABLE_WWW=1 @@ -67,7 +67,7 @@ endif ${CC} -c -o $@ $< ${CFLAGS} magickawww: ${OBJS} ${WWWOBJS} ${LUA} ${ZMODEM} ${B64} ${JAMLIB} ${JSMN} ${UUID} - ${CC} -o ../magicka $^ ${LIBS} -lmicrohttpd + ${CC} -o ../magicka $^ ${LIBS} -lmicrohttpd magicka: ${OBJS} ${LUA} ${ZMODEM} ${JAMLIB} ${JSMN} ${UUID} ${CC} -o ../magicka $^ ${LIBS} diff --git a/src/www_blog.c b/src/www_blog.c index 18edf8d..2b1ccba 100644 --- a/src/www_blog.c +++ b/src/www_blog.c @@ -3,20 +3,35 @@ #include #include #include +#include "www_tree.h" #include "bbs.h" extern struct bbs_config conf; char *www_blog() { - stralloc page = EMPTY_STRALLOC; + //stralloc page = EMPTY_STRALLOC; struct ptr_vector entries = blog_load(); - - stralloc_copys(&page, "

System Blog

\n"); + struct www_tag *page; + struct www_tag *cur_tag; + struct www_tag *child_tag; + struct www_tag *child_child_tag; + struct www_tag *child_child_child_tag; + + page = www_tag_new(NULL, ""); + cur_tag = www_tag_new("div", NULL); + www_tag_add_attrib(cur_tag, "class", "content-header"); + child_tag = www_tag_new("h2", NULL); + www_tag_add_child(cur_tag, child_tag); + www_tag_add_child(child_tag, www_tag_new(NULL, "System Blog")); + www_tag_add_child(page, cur_tag); if (ptr_vector_len(&entries) == 0) { - stralloc_cats(&page, "

No Entries

\n"); - stralloc_0(&page); - return page.s; + cur_tag = www_tag_new("p", NULL); + www_tag_add_child(cur_tag, www_tag_new(NULL, "No Entries")); + www_tag_add_child(page, cur_tag); + + + return www_tag_unwravel(page); } for (size_t i = 0; i < ptr_vector_len(&entries); i++) { struct blog_entry_t *entry = ptr_vector_get(&entries, i); @@ -30,36 +45,73 @@ char *www_blog() { strftime(timebuf, sizeof timebuf, "%l:%M", &entry_time); strftime(datebuf, sizeof datebuf, " %a, %e %b %Y", &entry_time); - stralloc_cats(&page, "

"); - stralloc_cats(&page, entry->subject); - stralloc_cats(&page, "

"); - stralloc_cats(&page, timebuf); - stralloc_cats(&page, hour >= 12 ? "pm" : "am"); - stralloc_cats(&page, datebuf); - stralloc_cats(&page, "
by "); - stralloc_cats(&page, entry->author); - stralloc_cats(&page, "
"); + cur_tag = www_tag_new("div", NULL); + www_tag_add_attrib(cur_tag, "class", "blog-header"); + www_tag_add_child(page, cur_tag); + + child_tag = www_tag_new("div", NULL); + www_tag_add_attrib(child_tag, "class", "blog-title"); + www_tag_add_child(cur_tag, child_tag); + + child_child_tag = www_tag_new("h3", NULL); + www_tag_add_child(child_tag, child_child_tag); + + child_child_child_tag = www_tag_new(NULL, entry->subject); + www_tag_add_child(child_child_tag, child_child_child_tag); + + child_tag = www_tag_new("div", NULL); + www_tag_add_attrib(child_tag, "class", "blog-date"); + www_tag_add_child(cur_tag, child_tag); + + child_child_tag = www_tag_new(NULL, timebuf); + www_tag_add_child(child_tag, child_child_tag); - stralloc_cats(&page, "

"); + child_child_tag = www_tag_new(NULL, hour >= 12 ? "pm" : "am"); + www_tag_add_child(child_tag, child_child_tag); + child_child_tag = www_tag_new(NULL, datebuf); + www_tag_add_child(child_tag, child_child_tag); + + child_tag = www_tag_new("div", NULL); + www_tag_add_attrib(child_tag, "class", "blog-author"); + www_tag_add_child(cur_tag, child_tag); + + child_child_tag = www_tag_new(NULL, "by "); + www_tag_add_child(child_tag, child_child_tag); + + child_child_tag = www_tag_new(NULL, entry->author); + www_tag_add_child(child_tag, child_child_tag); + + cur_tag = www_tag_new("div", NULL); + www_tag_add_attrib(cur_tag, "class", "blog-entry"); + www_tag_add_child(page, cur_tag); + + child_tag = www_tag_new("p", NULL); + www_tag_add_child(cur_tag, child_tag); + + stralloc blog_body = EMPTY_STRALLOC; + for (char *p = entry->body; *p != '\0'; ++p) { if (*p != '\r') { - stralloc_append1(&page, *p); + stralloc_append1(&blog_body, *p); continue; } if (p[1] != '\0' && p[1] != '\r') { - stralloc_append1(&page, ' '); + stralloc_append1(&blog_body, ' '); continue; } - stralloc_cats(&page, "

"); } - stralloc_cats(&page, "

"); + + child_child_tag = www_tag_new(NULL, blog_body.s); + + free(blog_body.s); + + www_tag_add_child(child_tag, child_child_tag); } ptr_vector_apply(&entries, free); destroy_ptr_vector(&entries); - stralloc_0(&page); - - return page.s; + + return www_tag_unwravel(page); } #endif diff --git a/src/www_tree.c b/src/www_tree.c new file mode 100644 index 0000000..01f7d5a --- /dev/null +++ b/src/www_tree.c @@ -0,0 +1,105 @@ +#if defined(ENABLE_WWW) + +#include +#include "www_tree.h" +#include "bbs.h" + +struct www_tag *www_tag_new(char *tag, char *data) { + struct www_tag *new_tag = malloz(sizeof(struct www_tag)); + + + new_tag->attribs = EMPTY_PTR_VECTOR; + new_tag->values = EMPTY_PTR_VECTOR; + new_tag->children = EMPTY_PTR_VECTOR; + + if (tag == NULL) { + new_tag->tag = NULL; + + /* SANATIZE DATA HERE */ + stralloc str = EMPTY_STRALLOC; + for (char *p = data; *p != '\0'; ++p) { + switch (*p) { + case '&': + stralloc_cats(&str, "&"); + break; + case '<': + stralloc_cats(&str, "<"); + break; + case '>': + stralloc_cats(&str, ">"); + break; + default: + stralloc_append1(&str, *p); + break; + } + } + + new_tag->data = str.s; + } else { + new_tag->tag = strdup(tag); + new_tag->data = NULL; + + init_ptr_vector(&new_tag->attribs); + init_ptr_vector(&new_tag->values); + } + + init_ptr_vector(&new_tag->children); + + return new_tag; +} + +void www_tag_add_attrib(struct www_tag *tag, char *attrib, char *value) { + ptr_vector_append(&tag->attribs, strdup(attrib)); + ptr_vector_append(&tag->values, strdup(value)); +} + +void www_tag_add_child(struct www_tag *tag, struct www_tag *child) { + ptr_vector_append(&tag->children, child); +} + +char *www_tag_unwravel(struct www_tag *tag) { + stralloc thedata = EMPTY_STRALLOC; + + while (tag->children.len > 0) { + struct www_tag *child = ptr_vector_del(&tag->children, 0); + if (child->tag != NULL) { + stralloc_append1(&thedata, '<'); + stralloc_cats(&thedata, child->tag); + for (int i = 0; i < child->attribs.len; i++) { + stralloc_append1(&thedata, ' '); + stralloc_cats(&thedata, (char *)ptr_vector_get(&child->attribs, i)); + stralloc_append1(&thedata, '='); + stralloc_append1(&thedata, '\"'); + stralloc_cats(&thedata, (char *)ptr_vector_get(&child->values, i)); + stralloc_append1(&thedata, '\"'); + } + + + stralloc_append1(&thedata, '>'); + } + char *data = www_tag_unwravel(child); + stralloc_cats(&thedata, data); + free(data); + + if (child->tag != NULL) { + stralloc_cats(&thedata, "tag); + stralloc_append1(&thedata, '>'); + ptr_vector_apply(&child->attribs, free); + destroy_ptr_vector(&child->attribs); + ptr_vector_apply(&child->values, free); + destroy_ptr_vector(&child->values); + } + destroy_ptr_vector(&child->children); + + } + + if (tag->data != NULL) { + stralloc_cats(&thedata, tag->data); + } + stralloc_0(&thedata); + + return thedata.s; +} + +#endif diff --git a/src/www_tree.h b/src/www_tree.h new file mode 100644 index 0000000..d3cb7ba --- /dev/null +++ b/src/www_tree.h @@ -0,0 +1,21 @@ +#if defined(ENABLE_WWW) +#ifndef __WWW_TREE_H__ +#define __WWW_TREE_H__ + +#include "bbs.h" + +struct www_tag { + char *tag; + char *data; + struct ptr_vector attribs; + struct ptr_vector values; + + struct ptr_vector children; +}; + +extern struct www_tag *www_tag_new(char *tag, char *data); +extern void www_tag_add_attrib(struct www_tag *tag, char *attrib, char *value); +extern void www_tag_add_child(struct www_tag *tag, struct www_tag *child); +extern char *www_tag_unwravel(struct www_tag *tag); +#endif +#endif