1 /* Blerg is (C) 2011 The Dominion of Awesome, and is distributed under a
2 * BSD-style license. Please see the COPYING file for details.
8 #include <yajl/yajl_gen.h>
12 #include "subscription.h"
14 #include "canned_responses.h"
18 yajl_gen_config yajl_c = { 0, 0 };
20 int check_auth(const char *username) {
21 if (username == NULL || username[0] == 0) {
26 const char *given_token = cgi_getcookie("auth");
27 if (!auth_check_token(username, given_token)) {
34 void respond_yajl(yajl_gen g) {
35 const unsigned char *ybuf;
36 unsigned int content_len;
38 yajl_gen_get_buf(g, &ybuf, &content_len);
40 printf("Content-type: application/json\r\n");
41 printf("Content-length: %d\r\n\r\n", content_len);
42 fwrite(ybuf, content_len, 1, stdout);
45 void respond_for_range(struct blerg *b, uint64_t from, uint64_t to) {
46 const unsigned char *ybuf;
50 uint64_t record_count = blerg_get_record_count(b);
52 printf("Content-type: application/json\r\n\r\n");
54 if (from > to || from >= record_count || to >= record_count || to - from > 99) {
55 respond_JSON_Failure();
59 g = yajl_gen_alloc(&yajl_c, NULL);
60 yajl_gen_array_open(g);
62 for (i = to; i != from - 1; i--) {
63 json_generate_one_record(g, NULL, b, i, 0);
64 yajl_gen_get_buf(g, &ybuf, &len);
65 fwrite(ybuf, len, 1, stdout);
69 yajl_gen_array_close(g);
70 yajl_gen_get_buf(g, &ybuf, &len);
71 fwrite(ybuf, len, 1, stdout);
75 void respond_blergref_list(struct blergref * results, int i) {
76 const unsigned char *ybuf;
83 printf("Content-type: application/json\r\n\r\n");
84 g = yajl_gen_alloc(&yajl_c, NULL);
86 yajl_gen_array_open(g);
89 b = blerg_open(results[i].author);
91 json_generate_one_record(g, results[i].author, b, results[i].record, 0);
94 yajl_gen_get_buf(g, &ybuf, &len);
95 fwrite(ybuf, len, 1, stdout);
101 yajl_gen_array_close(g);
102 yajl_gen_get_buf(g, &ybuf, &len);
103 fwrite(ybuf, len, 1, stdout);
107 int main(int argc, char *argv[]) {
109 char *request_method;
111 struct url_info info;
117 if (cgi_init() != CGIERR_NONE)
120 path = getenv("PATH_INFO");
125 request_method = getenv("REQUEST_METHOD");
126 if (request_method == NULL) {
127 fprintf(stderr, "Request method is null!?\n");
131 if (strncmp(path, "/get", 4) == 0 && strlen(path) > 4) {
132 if (strncmp(request_method, "GET", 4) != 0) {
137 if (path[4] != '/') {
142 ret = parse_url_info(path + 5, &info);
143 if ((ret & URL_INFO_NAME) == 0) {
148 if (!blerg_exists(info.name)) {
153 struct blerg *b = blerg_open(info.name);
155 if ((ret & URL_INFO_RECORD) && (ret & URL_INFO_RECORD_TO)) {
156 respond_for_range(b, info.record, info.record_to);
157 } else if (ret & URL_INFO_RECORD) {
158 ret = blerg_fetch(b, info.record, &data, &len);
164 respond_simple_data(data, len);
166 uint64_t record_count, from, to;
167 record_count = blerg_get_record_count(b);
168 if (record_count == 0) {
169 respond_simple_data("[]", 2);
171 to = record_count - 1;
172 from = (record_count > 50 ? to - 49 : 0);
173 respond_for_range(b, from, to);
178 } else if (strncmp(path, "/tag", 4) == 0 && strlen(path) > 4) {
179 if (strcmp(request_method, "GET") != 0) {
184 if (path[4] != '/') {
189 ret = parse_url_info(path + 5, &info);
190 if ((ret & URL_INFO_NAME) == 0) {
195 if (info.name[0] == 'H')
197 if (!tag_exists(info.name)) {
203 struct blergref *taglist = tag_list(info.name, 0, &recs, -1);
206 respond_simple_data("[]", 2);
208 respond_blergref_list(taglist, recs);
210 } else if (strncmp(path, "/put", 4) == 0) {
211 if (strcmp(request_method, "POST") != 0) {
216 const char *username = cgi_getentrystr("username");
217 if (!check_auth(username))
220 if (path[4] == '/') {
225 const char *data = cgi_getentrystr("data");
226 if (data == NULL || data[0] == 0) {
227 respond_JSON_Failure();
231 struct blerg *b = blerg_open(username);
233 respond_JSON_Failure();
236 ret = blerg_store(b, data, strlen(data));
239 respond_JSON_Failure();
243 respond_JSON_Success();
244 } else if (strncmp(path, "/info", 5) == 0) {
245 if (strcmp(request_method, "GET") != 0) {
250 if (path[5] != '/') {
255 ret = parse_url_info(path + 6, &info);
256 if ((ret & URL_INFO_NAME) == 0) {
261 if (!blerg_exists(info.name)) {
266 struct blerg *b = blerg_open(info.name);
267 uint64_t record_count = blerg_get_record_count(b);
271 yajl_gen g = yajl_gen_alloc(&yajl_c, NULL);
272 yajl_gen_map_open(g);
273 yajl_gen_string(g, (unsigned char *)"record_count", 12);
274 snprintf(number, 21, "%llu", record_count);
275 yajl_gen_string(g, (unsigned char *)number, strlen(number));
276 yajl_gen_map_close(g);
281 } else if (strncmp(path, "/create", 8) == 0) {
282 if (strcmp(request_method, "POST") != 0) {
287 const char *username = cgi_getentrystr("username");
288 const char *password = cgi_getentrystr("password");
289 if (username == NULL || username[0] == 0 ||
290 password == NULL || password[0] == 0) {
291 respond_JSON_Failure();
295 if (blerg_exists(username)) {
296 respond_JSON_Failure();
300 struct blerg *b = blerg_open(username);
303 auth_set_password(username, password);
305 respond_JSON_Success();
307 respond_JSON_Failure();
309 } else if (strncmp(path, "/login", 7) == 0) {
310 if (strcmp(request_method, "POST") != 0) {
315 const char *username = cgi_getentrystr("username");
316 const char *password = cgi_getentrystr("password");
317 if (username == NULL || username[0] == 0 ||
318 password == NULL || password[0] == 0) {
319 respond_JSON_Failure();
323 char *token = auth_login(username, password);
325 respond_JSON_Failure();
329 printf("Set-Cookie: auth=%s\r\n", token);
332 respond_JSON_Success();
333 } else if (strncmp(path, "/logout", 8) == 0) {
334 if (strcmp(request_method, "POST") != 0) {
339 const char *username = cgi_getentrystr("username");
340 if (!check_auth(username))
343 const char *given_token = cgi_getcookie("auth");
344 auth_logout(username, given_token);
345 respond_JSON_Success();
346 } else if (strncmp(path, "/subscribe", 10) == 0 || strncmp(path, "/unsubscribe", 12) == 0) {
347 const char *username = cgi_getentrystr("username");
348 if (!check_auth(username))
351 if (path[1] == 'u') {
352 if (path[12] != '/') {
357 ret = parse_url_info(path + 13, &info);
358 if ((ret & URL_INFO_NAME) == 0) {
363 subscription_remove(username, info.name);
365 if (path[10] != '/') {
370 ret = parse_url_info(path + 11, &info);
371 if ((ret & URL_INFO_NAME) == 0) {
376 subscription_add(username, info.name);
378 respond_JSON_Success();
379 } else if (strncmp(path, "/feed", 6) == 0) {
380 const char *username = cgi_getentrystr("username");
381 if (!check_auth(username))
385 struct blergref *feedlist = subscription_list(username, 0, &recs, -1);
388 respond_simple_data("[]", 2);
390 respond_blergref_list(feedlist, recs);
392 } else if (strncmp(path, "/status", 7) == 0) {
393 const char *username = cgi_getentrystr("username");
394 if (!check_auth(username))
397 if (strncmp(request_method, "POST", 4) != 0) {
404 if (path[7] == 0) { /* No username */
405 const char *clear = cgi_getentrystr("clear");
408 struct blerg *b = blerg_open(username);
409 if (strncmp(clear, "feed", 4) == 0) {
410 blerg_set_subscription_mark(b);
411 } else if (strncmp(clear, "mentioned", 9) == 0) {
412 blerg_set_status(b, BLERGSTATUS_MENTIONED, 0);
415 respond_JSON_Success();
417 g = yajl_gen_alloc(&yajl_c, NULL);
418 yajl_gen_map_open(g);
420 struct blerg *b = blerg_open(username);
421 uint64_t subscription_mark = blerg_get_subscription_mark(b);
422 int mentioned = blerg_get_status(b, BLERGSTATUS_MENTIONED);
425 yajl_gen_string(g, (unsigned char *)"feed_new", 8);
426 yajl_gen_integer(g, subscription_count_items(username) - subscription_mark);
428 yajl_gen_string(g, (unsigned char *)"mentioned", 9);
429 yajl_gen_bool(g, mentioned);
431 yajl_gen_map_close(g);
435 } else { /* with username */
436 g = yajl_gen_alloc(&yajl_c, NULL);
437 yajl_gen_map_open(g);
439 yajl_gen_string(g, (unsigned char *)"subscribed", 10);
440 ret = parse_url_info(path + 8, &info);
441 if ((ret & URL_INFO_NAME) == 1) {
442 yajl_gen_bool(g, is_subscribed(username, info.name));
447 yajl_gen_map_close(g);
451 } else if (strncmp(path, "/passwd", 7) == 0) {
452 const char *username = cgi_getentrystr("username");
453 if (!check_auth(username))
456 const char *password = cgi_getentrystr("password");
457 const char *new_password = cgi_getentrystr("new_password");
458 if (password == NULL || new_password == NULL) {
459 respond_JSON_Failure();
461 if (auth_check_password(username, password)) {
462 auth_set_password(username, new_password);
463 respond_JSON_Success();
465 respond_JSON_Failure();