tvote.c: allow creating polls from query - vote - simple cgi voting system for web and gopher
 (HTM) git clone git://src.adamsgaard.dk/vote
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 3089fdd4c6326fa81bef1daa7b401513eaed785e
 (DIR) parent 4cb97e229cbb0d54d85e3960e3d7c4daa53a429e
 (HTM) Author: Anders Damsgaard <anders@adamsgaard.dk>
       Date:   Sun, 27 Sep 2020 13:37:30 +0200
       
       vote.c: allow creating polls from query
       
       Diffstat:
         M vote.c                              |      62 ++++++++++++++++++++++++++++---
       
       1 file changed, 57 insertions(+), 5 deletions(-)
       ---
 (DIR) diff --git a/vote.c b/vote.c
       t@@ -2,10 +2,12 @@
        #include <stdlib.h>
        #include <unistd.h>
        #include <string.h>
       +#include <sys/types.h>
        #include <sys/stat.h>
        #include <err.h>
        #include <fcntl.h>
        #include <limits.h>
       +#include <fts.h>
        
        #include "util.h"
        
       t@@ -13,6 +15,9 @@
        #define POLLS_DIR "polls"
        
        static char poll[1024];
       +static char create[2];
       +static char question[4096];
       +static char options[4096];
        
        void
        http_status(int statuscode)
       t@@ -96,7 +101,7 @@ print_poll_file(FILE *fp, const char *poll_name)
                puts("</table>");
        }
        
       -void
       +int
        create_poll_file(const char *name, const char *question, const char *options)
        {
                FILE *fp;
       t@@ -111,10 +116,23 @@ create_poll_file(const char *name, const char *question, const char *options)
                        err(1, "create_poll_file: snprintf fname %s/%s", POLLS_DIR, buf);
                }
        
       +        if (!*name || !*question || !*options) {
       +                puts("<p><b>Error: Could not create poll</b></p>");
       +                puts("<ul>");
       +                if (!*name)
       +                        puts("<li>Poll name is missing</li>");
       +                if (!*question)
       +                        puts("<li>Poll question is missing</li>");
       +                if (!*options)
       +                        puts("<li>Poll options are missing</li>");
       +                puts("</ul>");
       +                return -1;
       +        }
       +
                if (stat(fname, &sb) == 0) {
                        printf("<p>Poll '%s' already exists</p>", name);
       +                return -1;
                } else {
       -
                        if (!(fp = fopen(fname, "w"))) {
                                http_status(404);
                                exit(1);
       t@@ -152,6 +170,7 @@ create_poll_file(const char *name, const char *question, const char *options)
                                fclose(fp);
                        }
                }
       +        return 0;
        }
        
        void
       t@@ -178,6 +197,13 @@ show_poll(const char *poll_name)
        }
        
        void
       +list_polls()
       +{
       +        /* something with fts_open, fts_read */
       +        puts("listing polls");
       +}
       +
       +void
        parse_query()
        {
                char *query, *p;
       t@@ -185,12 +211,34 @@ parse_query()
                if (!(query = getenv("QUERY_STRING")))
                        query = "";
        
       +        if ((p = getparam(query, "create"))) {
       +                if (decodeparam(create, sizeof(create), p) == -1) {
       +                        http_status(401);
       +                        exit(1);
       +                }
       +        }
       +
                if ((p = getparam(query, "poll"))) {
                        if (decodeparam(poll, sizeof(poll), p) == -1) {
                                http_status(401);
                                exit(1);
                        }
                }
       +
       +        if ((p = getparam(query, "question"))) {
       +                if (decodeparam(question, sizeof(question), p) == -1) {
       +                        http_status(401);
       +                        exit(1);
       +                }
       +        }
       +
       +        if ((p = getparam(query, "options"))) {
       +                if (decodeparam(options, sizeof(options), p) == -1) {
       +                        http_status(401);
       +                        exit(1);
       +                }
       +        }
       +
        }
        
        int
       t@@ -218,10 +266,14 @@ main()
                print_html_head();
        
                parse_query();
       -        if (*poll)
       -                show_poll(poll);
        
       -        create_poll_file("test poll2", "What is your favorite color?", "Red\nGreen\nBlue");
       +        if (*create) {
       +                if (create_poll_file(poll, question, options) == 0)
       +                        show_poll(poll);
       +        } else if (*poll)
       +                show_poll(poll);
       +        else
       +                list_polls();
        
                print_html_foot();