comparison page-new.c @ 78:9bfe5ce3cc45

pasterd: rework themes
author David Demelier <markand@malikania.fr>
date Thu, 16 Mar 2023 20:45:59 +0100
parents fe78b16c694d
children 52029a52a385
comparison
equal deleted inserted replaced
77:fe78b16c694d 78:9bfe5ce3cc45
21 21
22 #include "database.h" 22 #include "database.h"
23 #include "json-util.h" 23 #include "json-util.h"
24 #include "page-new.h" 24 #include "page-new.h"
25 #include "page.h" 25 #include "page.h"
26 #include "paste.h"
27 #include "util.h" 26 #include "util.h"
28 27
29 #include "html/new.h" 28 #include "html/new.h"
30 29
31 static const struct paste paste_default = { 30 #define TITLE "paster -- create a new paste"
32 .id = "",
33 .title = "unknown",
34 .author = "anonymous",
35 .language = "nohighlight",
36 .code = ""
37 };
38 31
39 static long long int 32 static long long int
40 duration(const char *val) 33 duration(const char *val)
41 { 34 {
42 for (size_t i = 0; i < durationsz; ++i) 35 for (size_t i = 0; i < durationsz; ++i)
43 if (strcmp(val, durations[i].title) == 0) 36 if (strcmp(val, durations[i].title) == 0)
44 return durations[i].secs; 37 return durations[i].secs;
45 38
46 /* Default to month. */ 39 /* Default to day. */
47 return PASTE_DURATION_MONTH; 40 return 60 * 60 * 24;
48 } 41 }
49 42
50 static void 43 static void
51 get(struct kreq *r) 44 get(struct kreq *r)
52 { 45 {
53 page_new_render(r, NULL); 46 page_new_render(r, NULL);
54 } 47 }
55 48
56 static void 49 static void
57 post(struct kreq *r) 50 post(struct kreq *req)
58 { 51 {
59 struct paste paste = { 52 const char *key, *val, *id, *scheme;
60 .author = estrdup("Anonymous"), 53 json_t *paste;
61 .title = estrdup("Untitled"),
62 .language = estrdup("nohighlight"),
63 .code = estrdup(""),
64 .visible = true,
65 .duration = PASTE_DURATION_DAY
66 };
67 int raw = 0; 54 int raw = 0;
68 55
69 for (size_t i = 0; i < r->fieldsz; ++i) { 56 paste = ju_paste_new();
70 const char *key = r->fields[i].key;
71 const char *val = r->fields[i].val;
72 57
73 if (strcmp(key, "title") == 0) 58 for (size_t i = 0; i < req->fieldsz; ++i) {
74 replace(&paste.title, val); 59 key = req->fields[i].key;
75 else if (strcmp(key, "author") == 0) 60 val = req->fields[i].val;
76 replace(&paste.author, val); 61
62 if (strcmp(key, "title") == 0 && strlen(val))
63 json_object_set_new(paste, "title", json_string(val));
64 else if (strcmp(key, "author") == 0 && strlen(val))
65 json_object_set_new(paste, "author", json_string(val));
77 else if (strcmp(key, "language") == 0) 66 else if (strcmp(key, "language") == 0)
78 replace(&paste.language, val); 67 json_object_set_new(paste, "language", json_string(val));
79 else if (strcmp(key, "duration") == 0) 68 else if (strcmp(key, "duration") == 0)
80 paste.duration = duration(val); 69 json_object_set_new(paste, "duration", json_integer(duration(val)));
81 else if (strcmp(key, "code") == 0) 70 else if (strcmp(key, "code") == 0)
82 paste.code = estrdup(val); 71 json_object_set_new(paste, "code", json_string(val));
83 else if (strcmp(key, "private") == 0) 72 else if (strcmp(key, "visible") == 0)
84 paste.visible = strcmp(val, "on") != 0; 73 json_object_set_new(paste, "visible", json_boolean(strcmp(val, "on") == 0));
85 else if (strcmp(key, "raw") == 0) { 74 else if (strcmp(key, "raw") == 0)
86 raw = strcmp(val, "on") == 0; 75 raw = strcmp(val, "on") == 0;
87 }
88 } 76 }
89 77
90 if (!database_insert(&paste)) 78 if (database_insert(paste) < 0)
91 page_status(r,KHTTP_500); 79 page_status(req, KHTTP_500);
92 else { 80 else {
81 id = ju_get_string(paste, "id");
82 scheme = req->scheme == KSCHEME_HTTP ? "http" : "https";
83
93 if (raw) { 84 if (raw) {
94 /* For CLI users (e.g. paster) just print the location. */ 85 /* For CLI users (e.g. paster) just print the location. */
95 khttp_head(r, kresps[KRESP_STATUS], "%s", khttps[KHTTP_201]); 86 khttp_head(req, kresps[KRESP_STATUS], "%s", khttps[KHTTP_201]);
96 khttp_body(r); 87 khttp_body(req);
97 khttp_printf(r, "%s://%s/paste/%s\n", 88 khttp_printf(req, "%s://%s/paste/%s\n", scheme, req->host, id);
98 r->scheme == KSCHEME_HTTP ? "http" : "https",
99 r->host, paste.id);
100 khttp_free(r);
101 } else { 89 } else {
102 /* Otherwise, redirect to paste details. */ 90 /* Otherwise, redirect to paste details. */
103 khttp_head(r, kresps[KRESP_STATUS], "%s", khttps[KHTTP_302]); 91 khttp_head(req, kresps[KRESP_STATUS], "%s", khttps[KHTTP_302]);
104 khttp_head(r, kresps[KRESP_LOCATION], "/paste/%s", paste.id); 92 khttp_head(req, kresps[KRESP_LOCATION], "/paste/%s", id);
105 khttp_body(r); 93 khttp_body(req);
106 khttp_free(r);
107 } 94 }
95
96 khttp_free(req);
108 } 97 }
109 98
110 paste_finish(&paste); 99 json_decref(paste);
111 } 100 }
112 101
102 #include "log.h"
103
113 void 104 void
114 page_new_render(struct kreq *req, const struct paste *paste) 105 page_new_render(struct kreq *req, json_t *paste)
115 { 106 {
116 assert(req); 107 assert(req);
117 108
118 if (!paste) 109 page(req, KHTTP_200, html_new, ju_extend(paste, "{ss so so}",
119 paste = &paste_default; 110 "pagetitle", TITLE,
120 111 "durations", ju_durations(),
121 page(req, KHTTP_200, html_new, json_pack("{ss ss so so ss}", 112 "languages", ju_languages(ju_get_string(paste, "language"))
122 "pagetitle", "paster -- create new paste",
123 "title", paste->title,
124 "languages", ju_languages(paste->language),
125 "durations", ju_durations(),
126 "code", paste->code
127 )); 113 ));
128 } 114 }
129 115
130 void 116 void
131 page_new(struct kreq *req) 117 page_new(struct kreq *req)