|
|
|
@ -68,8 +68,6 @@ enum page {
|
|
|
|
|
PAGE_NEW,
|
|
|
|
|
PAGE_ALL,
|
|
|
|
|
PAGE_RECENT,
|
|
|
|
|
PAGE_HEADS,
|
|
|
|
|
PAGE_TAILS,
|
|
|
|
|
PAGE_ZET,
|
|
|
|
|
PAGE_SEARCH,
|
|
|
|
|
};
|
|
|
|
@ -141,15 +139,6 @@ static int write_html(int fd, const char *buf, size_t len) {
|
|
|
|
|
static int write_topbar(int fd, enum page page, const char *id, const char *title) {
|
|
|
|
|
int rc = write_buf(fd, "<form action=\"search\">");
|
|
|
|
|
|
|
|
|
|
if (page == PAGE_HEADS) rc |= write_buf(fd, "<strong>");
|
|
|
|
|
rc |= write_buf(fd, "<a href=\".\">Heads</a>");
|
|
|
|
|
if (page == PAGE_HEADS) rc |= write_buf(fd, "</strong>");
|
|
|
|
|
|
|
|
|
|
rc |= write_buf(fd, " ");
|
|
|
|
|
if (page == PAGE_TAILS) rc |= write_buf(fd, "<strong>");
|
|
|
|
|
rc |= write_buf(fd, "<a href=\"tails\">Tails</a>");
|
|
|
|
|
if (page == PAGE_TAILS) rc |= write_buf(fd, "</strong>");
|
|
|
|
|
|
|
|
|
|
rc |= write_buf(fd, " ");
|
|
|
|
|
if (page == PAGE_ALL) rc |= write_buf(fd, "<strong>");
|
|
|
|
|
rc |= write_buf(fd, "<a href=\"all\">All</a>");
|
|
|
|
@ -247,50 +236,6 @@ static int zet_stat(const char *id, struct stat *st) {
|
|
|
|
|
return stat(path_buf, st);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int zet_has_links(const char *id) {
|
|
|
|
|
char path_buf[_POSIX_PATH_MAX];
|
|
|
|
|
ssize_t sz = snprintf(path_buf, sizeof(path_buf), "%s/%s", zet_dir, id);
|
|
|
|
|
if (sz < 0 || (size_t)sz >= sizeof(path_buf)) return -1;
|
|
|
|
|
int fd = open(path_buf, O_RDONLY);
|
|
|
|
|
if (fd < 0) return -1;
|
|
|
|
|
struct stat st;
|
|
|
|
|
int rc = fstat(fd, &st);
|
|
|
|
|
if (rc < 0) goto error;
|
|
|
|
|
char *buf = malloc(st.st_size+1);
|
|
|
|
|
rc = read_all(fd, (unsigned char *)buf, st.st_size);
|
|
|
|
|
if (rc < 0) goto error;
|
|
|
|
|
buf[st.st_size] = '\0';
|
|
|
|
|
char *str = buf;
|
|
|
|
|
bool found_link = false;
|
|
|
|
|
while ((str = strstr(str, "§"))) {
|
|
|
|
|
int i;
|
|
|
|
|
bool matched_id_format = true;
|
|
|
|
|
str += sigil_size;
|
|
|
|
|
for (i = 0; i < id_length; i++) {
|
|
|
|
|
if (!isxdigit(str[i])) {
|
|
|
|
|
matched_id_format = false;
|
|
|
|
|
str += i;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (matched_id_format) {
|
|
|
|
|
found_link = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
free(buf);
|
|
|
|
|
close(fd);
|
|
|
|
|
return found_link ? 0 : 1;
|
|
|
|
|
|
|
|
|
|
int err;
|
|
|
|
|
error:
|
|
|
|
|
err = errno;
|
|
|
|
|
free(buf);
|
|
|
|
|
close(fd);
|
|
|
|
|
errno = err;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static char *zet_get_buf_title(const struct buf *link, char *buf, size_t len) {
|
|
|
|
|
char id_buf[128];
|
|
|
|
|
strncpy(id_buf, (char *)link->data, link->size);
|
|
|
|
@ -533,126 +478,6 @@ cleanup:
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int dpi_serve_zet_heads(int fd) {
|
|
|
|
|
int rc;
|
|
|
|
|
struct zet_search zs;
|
|
|
|
|
rc = zet_search_start(&zs, NULL, NULL);
|
|
|
|
|
if (rc < 0) return dpi_respond_err(fd, "Unable to list");
|
|
|
|
|
rc = dpi_send_header(fd, "text/html");
|
|
|
|
|
if (rc < 0) { warn("dpi_send_header"); close(fd); return 0; }
|
|
|
|
|
rc = dprintf(fd,
|
|
|
|
|
"<!doctype html><html><head>"
|
|
|
|
|
"<title>Notes - Heads</title>"
|
|
|
|
|
"<meta charset=utf-8>"
|
|
|
|
|
"</head>"
|
|
|
|
|
"<body>");
|
|
|
|
|
rc |= write_topbar(fd, PAGE_HEADS, NULL, NULL);
|
|
|
|
|
rc |= write_buf(fd, "<ul>");
|
|
|
|
|
if (rc < 0) { warn("write"); close(fd); return 0; }
|
|
|
|
|
const char *id;
|
|
|
|
|
while (1) {
|
|
|
|
|
rc = zet_search_next(&zs, &id);
|
|
|
|
|
if (rc < 0) {
|
|
|
|
|
rc = dprintf(fd,
|
|
|
|
|
"\n<strong>Error</strong>: %s\n",
|
|
|
|
|
strerror(errno));
|
|
|
|
|
if (rc < 0) warnx("dprintf");
|
|
|
|
|
close(fd);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
if (id == NULL) break;
|
|
|
|
|
|
|
|
|
|
bool has_backlink = false;
|
|
|
|
|
struct zet_search backlink_zs;
|
|
|
|
|
rc = zet_search_start(&backlink_zs, id, NULL);
|
|
|
|
|
if (rc < 0) warn("zet_search_start");
|
|
|
|
|
while (1) {
|
|
|
|
|
const char *backlink_id;
|
|
|
|
|
rc = zet_search_next(&backlink_zs, &backlink_id);
|
|
|
|
|
if (rc < 0) warn("zet_search_next");
|
|
|
|
|
if (backlink_id == NULL) break;
|
|
|
|
|
has_backlink = true;
|
|
|
|
|
}
|
|
|
|
|
(void)zet_search_close(&backlink_zs);
|
|
|
|
|
if (has_backlink) continue;
|
|
|
|
|
|
|
|
|
|
char title_buf[128];
|
|
|
|
|
char *title = zet_get_title(id, title_buf, sizeof(title_buf));
|
|
|
|
|
if (title == NULL) {
|
|
|
|
|
warn("zet_get_title");
|
|
|
|
|
title = title_buf;
|
|
|
|
|
strncpy(title, id, sizeof(title)-1);
|
|
|
|
|
title[sizeof(title)-1] = '\0';
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
rc = dprintf(fd, "<li><a href=\"%s\">", id);
|
|
|
|
|
rc |= write_html(fd, title, strlen(title));
|
|
|
|
|
rc |= write_buf(fd, "</a></li>");
|
|
|
|
|
if (rc < 0) { warnx("write"); close(fd); return 0; }
|
|
|
|
|
}
|
|
|
|
|
rc = dprintf(fd, "</ul></body></html>");
|
|
|
|
|
if (rc < 0) { warn("dprintf"); close(fd); return 0; }
|
|
|
|
|
rc = zet_search_close(&zs);
|
|
|
|
|
if (rc < 0) warn("closedir");
|
|
|
|
|
close(fd);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int dpi_serve_zet_tails(int fd) {
|
|
|
|
|
int rc;
|
|
|
|
|
struct zet_search zs;
|
|
|
|
|
rc = zet_search_start(&zs, NULL, NULL);
|
|
|
|
|
if (rc < 0) return dpi_respond_err(fd, "Unable to list");
|
|
|
|
|
rc = dpi_send_header(fd, "text/html");
|
|
|
|
|
if (rc < 0) { warn("dpi_send_header"); close(fd); return 0; }
|
|
|
|
|
rc = dprintf(fd,
|
|
|
|
|
"<!doctype html><html><head>"
|
|
|
|
|
"<title>Notes - Tails</title>"
|
|
|
|
|
"<meta charset=utf-8>"
|
|
|
|
|
"</head>"
|
|
|
|
|
"<body>");
|
|
|
|
|
rc |= write_topbar(fd, PAGE_HEADS, NULL, NULL);
|
|
|
|
|
rc |= write_buf(fd, "<ul>");
|
|
|
|
|
if (rc < 0) { warn("write"); close(fd); return 0; }
|
|
|
|
|
const char *id;
|
|
|
|
|
while (1) {
|
|
|
|
|
rc = zet_search_next(&zs, &id);
|
|
|
|
|
if (rc < 0) {
|
|
|
|
|
rc = dprintf(fd,
|
|
|
|
|
"\n<strong>Error</strong>: %s\n",
|
|
|
|
|
strerror(errno));
|
|
|
|
|
if (rc < 0) warnx("dprintf");
|
|
|
|
|
close(fd);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
if (id == NULL) break;
|
|
|
|
|
|
|
|
|
|
int has_link = zet_has_links(id);
|
|
|
|
|
if (has_link < 0) warn("zet_has_link");
|
|
|
|
|
if (has_link == 0) continue;
|
|
|
|
|
|
|
|
|
|
char title_buf[128];
|
|
|
|
|
char *title = zet_get_title(id, title_buf, sizeof(title_buf));
|
|
|
|
|
if (title == NULL) {
|
|
|
|
|
warn("zet_get_title");
|
|
|
|
|
title = title_buf;
|
|
|
|
|
strncpy(title, id, sizeof(title)-1);
|
|
|
|
|
title[sizeof(title)-1] = '\0';
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
rc = dprintf(fd, "<li><a href=\"%s\">", id);
|
|
|
|
|
rc |= write_html(fd, title, strlen(title));
|
|
|
|
|
rc |= write_buf(fd, "</a></li>");
|
|
|
|
|
if (rc < 0) { warnx("write"); close(fd); return 0; }
|
|
|
|
|
}
|
|
|
|
|
rc = dprintf(fd, "</ul></body></html>");
|
|
|
|
|
if (rc < 0) { warn("dprintf"); close(fd); return 0; }
|
|
|
|
|
rc = zet_search_close(&zs);
|
|
|
|
|
if (rc < 0) warn("closedir");
|
|
|
|
|
close(fd);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void unescape(char *buf, size_t len, const char *str) {
|
|
|
|
|
struct buf *ob = bufnew(1024);
|
|
|
|
|
houdini_unescape_url(ob, (unsigned char *)str, strlen(str));
|
|
|
|
@ -907,8 +732,7 @@ static int dpi_serve_zet(int fd, char *path) {
|
|
|
|
|
char *hash, *query;
|
|
|
|
|
parse_req_uri(path, &hash, &query);
|
|
|
|
|
|
|
|
|
|
if (!strcmp(path, "/")) return dpi_serve_zet_heads(fd);
|
|
|
|
|
if (!strcmp(path, "/tails")) return dpi_serve_zet_tails(fd);
|
|
|
|
|
if (!strcmp(path, "/")) return dpi_serve_zet_all(fd);
|
|
|
|
|
if (!strcmp(path, "/all")) return dpi_serve_zet_all(fd);
|
|
|
|
|
if (!strcmp(path, "/recent")) return dpi_serve_zet_recent(fd);
|
|
|
|
|
if (!strcmp(path, "/search")) return dpi_serve_zet_search(fd, query);
|
|
|
|
|