From c7dabee1300ca84a011d9066eafeb2608cad7c01 Mon Sep 17 00:00:00 2001 From: cel Date: Fri, 7 Aug 2020 14:48:07 -0400 Subject: [PATCH] Add "Add New ID" button --- zet.dpi.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/zet.dpi.c b/zet.dpi.c index f59bc27..a555361 100644 --- a/zet.dpi.c +++ b/zet.dpi.c @@ -36,6 +36,7 @@ static const int max_cols = 54; static char server_hostname[256]; static char server_url[256]; static char zet_dir[_POSIX_PATH_MAX-64]; +static bool seeded = false; enum http_method { HTTP_METHOD_GET, @@ -58,6 +59,7 @@ struct http_request { int fd; char *data, *full_data; size_t len; + bool add_new_id; }; struct zet_search { @@ -822,6 +824,7 @@ static int dpi_serve_zet(int fd, char *path) { if (rc < 0) { warnx("passthrough_note_html"); close(fd); return 0; } rc = dprintf(fd, "
" "" + " " "" "" "" @@ -954,8 +957,21 @@ static int http_serve_index(struct http_request *req) { return 0; } +static void generate_id(char id[64]) { + if (!seeded) { + srandom(time(NULL) ^ getpid()); + seeded = true; + } + (void)snprintf(id, 64, "%08lx%08lx", random(), random()); +} + static int save_part(struct http_request *req, int note_fd) { int rc = write_all(note_fd, (unsigned char *)req->data, req->len); + if (req->add_new_id) { + char id[72] = "\n§"; + generate_id(id + 1 + sigil_size); + rc |= write_all(note_fd, id, strlen(id)); + } rc |= close(note_fd); free(req->full_data); return rc; @@ -1018,6 +1034,8 @@ static int parse_multipart(struct http_request *req) { if (!strcmp(name, "text")) { req->data = body; req->len = end == NULL ? strlen(body) : (size_t)end - (size_t)body; + } else if (!strcmp(name, "add_new_id")) { + req->add_new_id = true; } } return 0; @@ -1040,8 +1058,7 @@ static int http_serve_new(struct http_request *req) { (void)mkdir(zet_dir, 0700); - srandom(time(NULL) ^ getpid()); - (void)snprintf(id, sizeof(id), "%08lx%08lx", random(), random()); + generate_id(id); (void)snprintf(note_path, sizeof(note_path), "%s/%s", zet_dir, id); int note_fd = open(note_path, O_CREAT|O_WRONLY|O_TRUNC, 0640); if (note_fd < 0) { @@ -1175,6 +1192,7 @@ static int handle_http_client(int fd, int firstchar) { req.content_length = -1; req.content_type = NULL; req.multipart_boundary = NULL; + req.add_new_id = false; char *line = nextline; while (line != NULL) {