From de311c8ea26f336f8720f55bdaebf06e47b9be08 Mon Sep 17 00:00:00 2001 From: "me@ow.nekobit.net" Date: Thu, 27 Jan 2022 02:38:21 +0000 Subject: [PATCH] Alas, we display posts FossilOrigin-Name: 367cd50ddff8198249f7a5b037e729e8171bfe5c36784b0e8b5447719a5927d1 --- Makefile | 2 ++ dist/ratfe.css | 3 +- src/easprintf.c | 24 +++++++++++++++- src/easprintf.h | 2 ++ src/file-to-c/main.c | 1 + src/index.c | 22 +++++++++++++-- src/main.c | 2 +- src/status.c | 65 ++++++++++++++++++++++++++++++++++++++++++++ src/status.h | 25 +++++++++++++++++ static/index.html | 20 ++------------ static/status.html | 17 ++++++++++++ 11 files changed, 159 insertions(+), 24 deletions(-) create mode 100644 src/status.c create mode 100644 src/status.h create mode 100644 static/status.html diff --git a/Makefile b/Makefile index 9b23f01..4fc259c 100644 --- a/Makefile +++ b/Makefile @@ -27,6 +27,8 @@ filec: src/file-to-c/main.o $(PAGES_DIR)/index.chtml: $(PAGES_DIR)/index.html ./filec $< data_index_html > $@ +$(PAGES_DIR)/status.chtml: $(PAGES_DIR)/status.html + ./filec $< data_status_html > $@ $(MASTODONT_DIR): git clone $(MASTODONT_URL) || true diff --git a/dist/ratfe.css b/dist/ratfe.css index 9619c63..f182515 100644 --- a/dist/ratfe.css +++ b/dist/ratfe.css @@ -8,7 +8,6 @@ body { display: flex; justify-content: center; - } #display @@ -69,7 +68,7 @@ div#content main div#content aside { width: 180px; - max-width: 200px; + min-width: 180bpx; background-color: #fcfcfc; } diff --git a/src/easprintf.c b/src/easprintf.c index d7dcc94..1999d61 100644 --- a/src/easprintf.c +++ b/src/easprintf.c @@ -16,14 +16,36 @@ * along with this program. If not, see . */ +#include +#include #include "easprintf.h" int evasprintf(char** ret, const char* format, va_list ap) { + va_list ap_copy; + va_copy(ap_copy, ap); + int sz = vsnprintf(NULL, 0, format, ap); + va_end(ap); + + *ret = malloc(sz + 1); + if(*ret == NULL) + { + perror("malloc"); + *ret = NULL; + va_end(ap_copy); + return -1; + } + + vsnprintf(*ret, sz + 1, format, ap_copy); + + va_end(ap_copy); + return sz; } int easprintf(char** ret, const char* format, ...) { - + va_list ap; + va_start(ap, format); + return evasprintf(ret, format, ap); } diff --git a/src/easprintf.h b/src/easprintf.h index 5c6de6c..9ce6b64 100644 --- a/src/easprintf.h +++ b/src/easprintf.h @@ -18,7 +18,9 @@ #ifndef EASPRINTF_H #define EASPRINTF_H +#include +/* Alternatives to asprintf functions, which are glibc-only/bsd functions */ int evasprintf(char** ret, const char* format, va_list ap); int easprintf(char** ret, const char* format, ...); diff --git a/src/file-to-c/main.c b/src/file-to-c/main.c index faf2ecd..ef9d274 100644 --- a/src/file-to-c/main.c +++ b/src/file-to-c/main.c @@ -59,6 +59,7 @@ int main(int argc, char** argv) printf("#ifndef __%s\n", argv[ARG_VARIABLE]); printf("#define __%s\n", argv[ARG_VARIABLE]); + printf("static size_t %s_size = %ld;", argv[ARG_VARIABLE], size); printf("static const char %s[] = {", argv[ARG_VARIABLE]); for (size_t i = 0; i < size; ++i) { diff --git a/src/index.c b/src/index.c index c44743e..df6ad96 100644 --- a/src/index.c +++ b/src/index.c @@ -16,17 +16,35 @@ * along with this program. If not, see . */ +#include #include #include "../config.h" #include "index.h" +#include "easprintf.h" +#include "status.h" // Files #include "../static/index.chtml" void content_index(mastodont_t* api) { + size_t status_count, statuses_html_count; struct mstdnt_status* statuses; struct mstdnt_storage storage; - mastodont_timeline_public(api, NULL, &storage, &statuses); - printf(data_index_html, config_canonical_name); + char* status_format; + mastodont_timeline_public(api, NULL, &storage, &statuses, &status_count); + + /* Calculate statuses */ + status_format = construct_statuses(statuses, status_count, &statuses_html_count); + if (status_format == NULL) + status_format = "Error in malloc! Something has gone terribly wrong..."; + + /* Output */ + printf("Content-Length: %ld\r\n\r\n", + data_index_html_size + statuses_html_count + 4); + printf(data_index_html, config_canonical_name, status_format); + + /* Cleanup */ + mastodont_storage_cleanup(&storage); + free(status_format); } diff --git a/src/main.c b/src/main.c index e12fce8..7cedf79 100644 --- a/src/main.c +++ b/src/main.c @@ -27,7 +27,7 @@ int main() { char* path = getenv("PATH_INFO"); // Content type is always HTML - fputs("Content-type: text/html\r\n\r\n", stdout); + fputs("Content-type: text/html\r\n", stdout); // Global init mastodont_global_curl_init(); diff --git a/src/status.c b/src/status.c new file mode 100644 index 0000000..db179b3 --- /dev/null +++ b/src/status.c @@ -0,0 +1,65 @@ +/* + * RatFE - Lightweight frontend for Pleroma + * Copyright (C) 2022 Nekobit + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include +#include +#include "status.h" +#include "easprintf.h" +#include "../static/status.chtml" + +char* construct_statuses(struct mstdnt_status* statuses, size_t size, size_t* ret_size) +{ + char* stat_html, *result = NULL; + int curr_parse_size = 0, last_parse_size, parse_size; + + for (size_t i = 0; i < size; ++i) + { + parse_size = easprintf(&stat_html, data_status_html, + statuses[i].id, /* Username */ + "@who?", /* Account */ + "Public?", /* visibility */ + statuses[i].content); + if (parse_size == -1) /* Malloc error */ + { + if (result) free(result); + return NULL; + } + last_parse_size = curr_parse_size; + curr_parse_size += parse_size; + + result = realloc(result, curr_parse_size + 1); + if (result == NULL) + { + perror("malloc"); + free(stat_html); + return NULL; + } + + /* Copy stat_html to result in correct position */ + strncpy(result + last_parse_size, stat_html, parse_size); + /* Cleanup */ + free(stat_html); + } + + result[curr_parse_size] = '\0'; + + if (ret_size) *ret_size = curr_parse_size; + + return result; +} + diff --git a/src/status.h b/src/status.h new file mode 100644 index 0000000..2c595f1 --- /dev/null +++ b/src/status.h @@ -0,0 +1,25 @@ +/* + * RatFE - Lightweight frontend for Pleroma + * Copyright (C) 2022 Nekobit + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#ifndef STATUS_H +#define STATUS_H +#include + +char* construct_statuses(struct mstdnt_status* statuses, size_t size, size_t* ret_size); + +#endif // STATUS_H diff --git a/static/index.html b/static/index.html index 8172c03..1c5e908 100644 --- a/static/index.html +++ b/static/index.html @@ -27,24 +27,8 @@
-
-
- -
-
-
- Rat user - @ratlord@cum.desupost.soy - - public - -
- - This is my first post

- Just landed in L.A.! -
-
-
+ %s +
diff --git a/static/status.html b/static/status.html new file mode 100644 index 0000000..651e065 --- /dev/null +++ b/static/status.html @@ -0,0 +1,17 @@ +
+
+ +
+
+
+ %s + %s + + %s + +
+ + %s + +
+