New tree thing for www.

This commit is contained in:
Andrew Pamment 2018-10-21 18:24:26 +10:00
parent 931c5465d5
commit fc590d9608
4 changed files with 203 additions and 25 deletions

View File

@ -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}

View File

@ -3,20 +3,35 @@
#include <string.h>
#include <time.h>
#include <stdlib.h>
#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, "<div class=\"content-header\"><h2>System Blog</h2></div>\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, "<p>No Entries</p>\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, "<div class=\"blog-header\"><div class=\"blog-title\"><h3>");
stralloc_cats(&page, entry->subject);
stralloc_cats(&page, "</h3></div><div class=\"blog-date\">");
stralloc_cats(&page, timebuf);
stralloc_cats(&page, hour >= 12 ? "pm" : "am");
stralloc_cats(&page, datebuf);
stralloc_cats(&page, "</div><div class=\"blog-author\">by ");
stralloc_cats(&page, entry->author);
stralloc_cats(&page, "</div></div>");
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, "<div class=\"blog-entry\"><p>");
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, "</p><p>");
}
stralloc_cats(&page, "</p></div>");
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

105
src/www_tree.c Normal file
View File

@ -0,0 +1,105 @@
#if defined(ENABLE_WWW)
#include <stdlib.h>
#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, "&amp;");
break;
case '<':
stralloc_cats(&str, "&lt;");
break;
case '>':
stralloc_cats(&str, "&gt;");
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, "</");
stralloc_cats(&thedata, child->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

21
src/www_tree.h Normal file
View File

@ -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