From fc3d99fa8211f4b69ebc0eef83528cdce0aa4fe8 Mon Sep 17 00:00:00 2001 From: thematdev Date: Mon, 3 Jul 2023 20:25:23 +0300 Subject: [PATCH] First fully featured prototype --- src/main.c | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++-- src/utils.c | 8 +++++ src/utils.h | 2 ++ 3 files changed, 104 insertions(+), 3 deletions(-) diff --git a/src/main.c b/src/main.c index 9727661..731513b 100644 --- a/src/main.c +++ b/src/main.c @@ -7,14 +7,22 @@ #include "drivers/unix_fs/unix_fs_driver.h" #include +#include #define COMMENTS_PER_PAGE 20 #define MAX_NAME 1024 +#define MAX_COMMENT_SIZE 4096 #define QMENTS_PATH "qments-storage" #define DRIVER_DATA { QMENTS_PATH } #define DRIVER unix_fs_driver +int +page_by_id(int id) +{ + return id / COMMENTS_PER_PAGE + 1; +} + void fct_callback(char character, void *arg) { @@ -26,15 +34,22 @@ render_comment(const Comment *comment) { StringBuffer _buffer, *buffer; char *retval; + int rid; buffer = &_buffer; sb_init_empty(buffer); - fctprintf(fct_callback, buffer, "
\n", comment->id); + fctprintf(fct_callback, buffer, "
\n", comment->id); /* begin header */ fctprintf(fct_callback, buffer, "
\n"); fctprintf(fct_callback, buffer, "Posted by: %s\n", comment->header->user_displayname); + + rid = comment->header->reply_id; + if (rid > 0) { + fctprintf(fct_callback, buffer, " in reply to ", page_by_id(rid), rid); + } + fctprintf(fct_callback, buffer, "
\n"); /* end header */ @@ -98,9 +113,22 @@ defer: return retval; } +void +print_submit_form() +{ + fputs("
\n" + "\n" + "
\n" + "
\n" + "
\n" + "\n" + "
\n", cgiOut); +} + /* each page stores comments with ids in [P * (page - 1), P * page) * if page is not specified, then we'll just print last P comments (for now) * TODO: print errors in div + * FIXME: NOW PAGING IS FAULTY, FIX IT */ void print_page() @@ -147,6 +175,65 @@ print_page() } } +/* TODO: restore fields for user no to lose data */ +int +handle_submitted_comment() +{ + char displayname[MAX_NAME], text[MAX_COMMENT_SIZE], *sanitized_text; + int rid, retval; + cgiFormResultType err; + Driver driver = DRIVER; + UnixFsDriverData driver_data = DRIVER_DATA; + + err = cgiFormString("text", text, MAX_COMMENT_SIZE); + if (err == cgiFormTruncated) { + fprintf(cgiOut, "Comment too long (max %d bytes)\n", MAX_COMMENT_SIZE); + goto defer; + } + if (err == cgiFormNotFound) { + fprintf(cgiOut, "Comment text not provided\n"); + goto defer; + } + + err = cgiFormString("displayname", displayname, MAX_NAME); + if (err == cgiFormTruncated) { + fprintf(cgiOut, "Name too long (max %d bytes)\n", MAX_NAME); + goto defer; + } + if (err == cgiFormNotFound) { + fprintf(cgiOut, "Name not provided\n"); + goto defer; + } + + if (contain_special(displayname)) { + err = cgiFormTruncated; + fprintf(cgiOut, "Name must not contain HTML special characters\n"); + goto defer; + } + + cgiFormInteger("reply-to", &rid, 0); + + sanitized_text = mk_specialchars(text); + + CommentHeader header; + time(&header.creation_time); + header.reply_id = rid; + header.text_length = strlen(sanitized_text); + header.user_sid = "anonymous"; + header.user_displayname = displayname; + + if (driver.leave_comment(&driver_data, &header, sanitized_text) < 0) { + fprintf(cgiOut, "Failed to leave your comment\n"); + } else { + fprintf(cgiOut, "Comment successfully left\n"); + } + + free(sanitized_text); + +defer: + return (err == cgiFormSuccess ? 0 : -1); +} + int cgiMain() { @@ -155,12 +242,16 @@ cgiMain() fprintf(cgiOut, "\n"); fprintf(cgiOut, "\n"); - fprintf(cgiOut, " Simple discuss powered by qments\n"); + fprintf(cgiOut, "<title> Simple discuss powered by qments \n"); fprintf(cgiOut, "\n"); fprintf(cgiOut, "\n"); + + if (cgiFormSubmitClicked("submit") == cgiFormSuccess) { + handle_submitted_comment(); + } - /* print_submit_form(); */ + print_submit_form(); fprintf(cgiOut, "
\n"); print_page(); diff --git a/src/utils.c b/src/utils.c index 24b22bf..93285f6 100644 --- a/src/utils.c +++ b/src/utils.c @@ -1,7 +1,15 @@ #include +#include + #include "string_buffer.h" #include "utils.h" +int +contain_special(const char *s) +{ + return strchr(s, '&') || strchr(s, '"') || strchr(s, '\'') || strchr(s, '<') || strchr(s, '>'); +} + char * mk_specialchars(const char *input) { diff --git a/src/utils.h b/src/utils.h index aa4d6d3..9fa5502 100644 --- a/src/utils.h +++ b/src/utils.h @@ -1,6 +1,8 @@ #ifndef UTILS_H #define UTILS_H +int contain_special(const char *s); + char *mk_specialchars(const char *input); #endif /* UTILS_H */