Add "Add New ID" button

main
cel 5 years ago
parent 94b244d200
commit c7dabee130
Signed by: cel
GPG Key ID: C28D95BB012367EA

@ -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, "</textarea><br>"
"<input type=submit value=\"Save\">"
" <input type=submit name=add_new_id value=\"Add New ID\">"
"</form></td>"
"<style>pre { white-space: pre-wrap; }</style>"
"<td>"
@ -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) {

Loading…
Cancel
Save