2 * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 #include "telescope.h"
28 static const char *gemtext_prefixes[] = {
30 [LINE_TITLE_1] = "# ",
31 [LINE_TITLE_2] = "## ",
32 [LINE_TITLE_3] = "### ",
35 [LINE_PRE_START] = "``` ",
36 [LINE_PRE_CONTENT] = "",
37 [LINE_PRE_END] = "```",
40 static struct timeval tv = { 5 * 60, 0 };
41 static struct event timerev;
43 static struct ohash h;
49 const char *parser_name;
56 mcache_free_entry(const char *url)
58 struct mcache_entry *e;
61 slot = ohash_qlookup(&h, url);
62 if ((e = ohash_remove(&h, slot)) == NULL)
66 tot -= EVBUFFER_LENGTH(e->evb);
68 evbuffer_free(e->evb);
73 clean_old_entries(int fd, short ev, void *data)
75 struct mcache_entry *e;
79 /* delete pages older than an hour */
80 treshold = time(NULL) - 60 * 60;
82 for (e = ohash_first(&h, &i); e != NULL; e = ohash_next(&h, &i))
84 mcache_free_entry(e->url);
86 evtimer_add(&timerev, &tv);
92 struct ohash_info info = {
93 .key_offset = offsetof(struct mcache_entry, url),
94 .calloc = hash_calloc,
99 ohash_init(&h, 5, &info);
101 evtimer_set(&timerev, clean_old_entries, NULL);
105 mcache_tab(struct tab *tab)
107 struct mcache_entry *e;
113 url = tab->hist_cur->h;
115 len = sizeof(*e) + l + 1;
117 if ((e = calloc(1, len)) == NULL)
120 e->parser_name = tab->buffer.page.name;
121 e->trust = tab->trust;
122 memcpy(e->url, url, l);
124 if ((e->evb = evbuffer_new()) == NULL)
127 TAILQ_FOREACH(line, &tab->buffer.page.head, lines) {
128 const char *text, *alt;
131 if ((text = line->line) == NULL)
134 if ((alt = line->alt) == NULL)
137 switch (line->type) {
145 case LINE_PRE_CONTENT:
147 r = evbuffer_add_printf(e->evb, "%s%s\n",
148 gemtext_prefixes[line->type], text);
152 r = evbuffer_add_printf(e->evb, "=> %s %s\n",
158 case LINE_PATCH_HUNK_HDR:
166 case LINE_COMPL_CURRENT:
169 case LINE_DOWNLOAD_DONE:
170 case LINE_DOWNLOAD_INFO:
180 /* free any previously cached copies of this page */
181 mcache_free_entry(url);
183 slot = ohash_qlookup(&h, url);
184 ohash_insert(&h, slot, e);
187 tot += EVBUFFER_LENGTH(e->evb);
189 if (!evtimer_pending(&timerev, NULL))
190 evtimer_add(&timerev, &tv);
196 evbuffer_free(e->evb);
202 mcache_lookup(const char *url, struct tab *tab)
204 struct mcache_entry *e;
207 slot = ohash_qlookup(&h, url);
208 if ((e = ohash_find(&h, slot)) == NULL)
211 parser_init(tab, gemtext_initparser);
212 if (!parser_parse(tab, EVBUFFER_DATA(e->evb), EVBUFFER_LENGTH(e->evb)))
214 if (!parser_free(tab))
217 tab->buffer.page.name = e->parser_name;
218 tab->trust = e->trust;
223 erase_buffer(&tab->buffer);
228 mcache_info(size_t *r_npages, size_t *r_tot)