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 = { 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 treshold = time(NULL) - 15 * 60;
81 for (e = ohash_first(&h, &i); e != NULL; e = ohash_next(&h, &i))
83 mcache_free_entry(e->url);
85 evtimer_add(&timerev, &tv);
91 struct ohash_info info = {
92 .key_offset = offsetof(struct mcache_entry, url),
93 .calloc = hash_calloc,
98 ohash_init(&h, 5, &info);
100 evtimer_set(&timerev, clean_old_entries, NULL);
104 mcache_tab(struct tab *tab)
106 struct mcache_entry *e;
112 url = tab->hist_cur->h;
114 len = sizeof(*e) + l + 1;
116 if ((e = calloc(1, len)) == NULL)
119 e->parser_name = tab->buffer.page.name;
120 e->trust = tab->trust;
121 memcpy(e->url, url, l);
123 if ((e->evb = evbuffer_new()) == NULL)
126 TAILQ_FOREACH(line, &tab->buffer.page.head, lines) {
127 const char *text, *alt;
130 if ((text = line->line) == NULL)
133 if ((alt = line->alt) == NULL)
136 switch (line->type) {
144 case LINE_PRE_CONTENT:
146 r = evbuffer_add_printf(e->evb, "%s%s\n",
147 gemtext_prefixes[line->type], text);
151 r = evbuffer_add_printf(e->evb, "=> %s %s\n",
157 case LINE_PATCH_HUNK_HDR:
165 case LINE_COMPL_CURRENT:
168 case LINE_DOWNLOAD_DONE:
169 case LINE_DOWNLOAD_INFO:
179 /* free any previously cached copies of this page */
180 mcache_free_entry(url);
182 slot = ohash_qlookup(&h, url);
183 ohash_insert(&h, slot, e);
186 tot += EVBUFFER_LENGTH(e->evb);
188 if (!evtimer_pending(&timerev, NULL))
189 evtimer_add(&timerev, &tv);
195 evbuffer_free(e->evb);
201 mcache_lookup(const char *url, struct tab *tab)
203 struct mcache_entry *e;
206 slot = ohash_qlookup(&h, url);
207 if ((e = ohash_find(&h, slot)) == NULL)
210 parser_init(tab, gemtext_initparser);
211 if (!parser_parse(tab, EVBUFFER_DATA(e->evb), EVBUFFER_LENGTH(e->evb)))
213 if (!parser_free(tab))
216 tab->buffer.page.name = e->parser_name;
217 tab->trust = e->trust;
222 erase_buffer(&tab->buffer);
227 mcache_info(size_t *r_npages, size_t *r_tot)