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 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;
51 const char *parser_name;
58 mcache_free_entry(const char *url)
60 struct mcache_entry *e;
63 slot = ohash_qlookup(&mcache.h, url);
64 if ((e = ohash_remove(&mcache.h, slot)) == NULL)
68 mcache.tot -= EVBUFFER_LENGTH(e->evb);
70 evbuffer_free(e->evb);
75 clean_old_entries(int fd, short ev, void *data)
77 struct mcache_entry *e;
81 treshold = time(NULL) - 15 * 60;
83 for (e = ohash_first(&mcache.h, &i); e != NULL; e = ohash_next(&mcache.h, &i))
85 mcache_free_entry(e->url);
87 evtimer_add(&timerev, &tv);
93 struct ohash_info info = {
94 .key_offset = offsetof(struct mcache_entry, url),
95 .calloc = hash_calloc,
100 ohash_init(&mcache.h, 5, &info);
102 evtimer_set(&timerev, clean_old_entries, NULL);
106 mcache_tab(struct tab *tab)
108 struct mcache_entry *e;
114 url = tab->hist_cur->h;
116 len = sizeof(*e) + l + 1;
118 if ((e = calloc(1, len)) == NULL)
121 e->parser_name = tab->buffer.page.name;
122 e->trust = tab->trust;
123 memcpy(e->url, url, l);
125 if ((e->evb = evbuffer_new()) == NULL)
128 TAILQ_FOREACH(line, &tab->buffer.page.head, lines) {
129 const char *text, *alt;
132 if ((text = line->line) == NULL)
135 if ((alt = line->alt) == NULL)
138 switch (line->type) {
146 case LINE_PRE_CONTENT:
148 r = evbuffer_add_printf(e->evb, "%s%s\n",
149 gemtext_prefixes[line->type], text);
153 r = evbuffer_add_printf(e->evb, "=> %s %s\n",
159 case LINE_PATCH_HUNK_HDR:
167 case LINE_COMPL_CURRENT:
170 case LINE_DOWNLOAD_DONE:
171 case LINE_DOWNLOAD_INFO:
181 /* free any previously cached copies of this page */
182 mcache_free_entry(url);
184 slot = ohash_qlookup(&mcache.h, url);
185 ohash_insert(&mcache.h, slot, e);
188 mcache.tot += EVBUFFER_LENGTH(e->evb);
190 if (!evtimer_pending(&timerev, NULL))
191 evtimer_add(&timerev, &tv);
197 evbuffer_free(e->evb);
203 mcache_lookup(const char *url, struct tab *tab)
205 struct mcache_entry *e;
208 slot = ohash_qlookup(&mcache.h, url);
209 if ((e = ohash_find(&mcache.h, slot)) == NULL)
212 parser_init(tab, gemtext_initparser);
213 if (!parser_parse(tab, EVBUFFER_DATA(e->evb), EVBUFFER_LENGTH(e->evb)))
215 if (!parser_free(tab))
218 tab->buffer.page.name = e->parser_name;
219 tab->trust = e->trust;
224 erase_buffer(&tab->buffer);
229 mcache_info(size_t *npages, size_t *tot)
231 *npages = mcache.npages;