Blob


1 /*
2 * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
3 *
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.
7 *
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.
15 */
17 #include "compat.h"
19 #include <stdlib.h>
20 #include <stdint.h>
21 #include <string.h>
23 #include "telescope.h"
24 #include "mcache.h"
25 #include "parser.h"
26 #include "utils.h"
28 static const char *gemtext_prefixes[] = {
29 [LINE_TEXT] = "",
30 [LINE_TITLE_1] = "# ",
31 [LINE_TITLE_2] = "## ",
32 [LINE_TITLE_3] = "### ",
33 [LINE_ITEM] = "* ",
34 [LINE_QUOTE] = "> ",
35 [LINE_PRE_START] = "``` ",
36 [LINE_PRE_CONTENT] = "",
37 [LINE_PRE_END] = "```",
38 };
40 static struct timeval tv = { 60, 0 };
41 static struct event timerev;
43 static struct ohash h;
44 static size_t npages;
45 static size_t tot;
47 struct mcache_entry {
48 time_t ts;
49 const char *parser_name;
50 int trust;
51 struct evbuffer *evb;
52 char url[];
53 };
55 static void
56 mcache_free_entry(const char *url)
57 {
58 struct mcache_entry *e;
59 unsigned int slot;
61 slot = ohash_qlookup(&h, url);
62 if ((e = ohash_remove(&h, slot)) == NULL)
63 return;
65 npages--;
66 tot -= EVBUFFER_LENGTH(e->evb);
68 evbuffer_free(e->evb);
69 free(e);
70 }
72 static void
73 clean_old_entries(int fd, short ev, void *data)
74 {
75 struct mcache_entry *e;
76 unsigned int i;
77 time_t treshold;
79 treshold = time(NULL) - 15 * 60;
81 for (e = ohash_first(&h, &i); e != NULL; e = ohash_next(&h, &i))
82 if (e->ts < treshold)
83 mcache_free_entry(e->url);
85 evtimer_add(&timerev, &tv);
86 }
88 void
89 mcache_init(void)
90 {
91 struct ohash_info info = {
92 .key_offset = offsetof(struct mcache_entry, url),
93 .calloc = hash_calloc,
94 .free = hash_free,
95 .alloc = hash_alloc,
96 };
98 ohash_init(&h, 5, &info);
100 evtimer_set(&timerev, clean_old_entries, NULL);
103 int
104 mcache_tab(struct tab *tab)
106 struct mcache_entry *e;
107 struct line *line;
108 unsigned int slot;
109 size_t l, len;
110 const char *url;
112 url = tab->hist_cur->h;
113 l = strlen(url);
114 len = sizeof(*e) + l + 1;
116 if ((e = calloc(1, len)) == NULL)
117 return -1;
118 e->ts = time(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)
124 goto err;
126 TAILQ_FOREACH(line, &tab->buffer.page.head, lines) {
127 const char *text, *alt;
128 int r;
130 if ((text = line->line) == NULL)
131 text = "";
133 if ((alt = line->alt) == NULL)
134 alt = "";
136 switch (line->type) {
137 case LINE_TEXT:
138 case LINE_TITLE_1:
139 case LINE_TITLE_2:
140 case LINE_TITLE_3:
141 case LINE_ITEM:
142 case LINE_QUOTE:
143 case LINE_PRE_START:
144 case LINE_PRE_CONTENT:
145 case LINE_PRE_END:
146 r = evbuffer_add_printf(e->evb, "%s%s\n",
147 gemtext_prefixes[line->type], text);
148 break;
150 case LINE_LINK:
151 r = evbuffer_add_printf(e->evb, "=> %s %s\n",
152 alt, text);
153 break;
155 case LINE_PATCH:
156 case LINE_PATCH_HDR:
157 case LINE_PATCH_HUNK_HDR:
158 case LINE_PATCH_ADD:
159 case LINE_PATCH_DEL:
160 /* TODO */
161 r = -1;
162 break;
164 case LINE_COMPL:
165 case LINE_COMPL_CURRENT:
166 case LINE_HELP:
167 case LINE_DOWNLOAD:
168 case LINE_DOWNLOAD_DONE:
169 case LINE_DOWNLOAD_INFO:
170 case LINE_FRINGE:
171 /* not reached */
172 abort();
175 if (r == -1)
176 goto err;
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);
185 npages++;
186 tot += EVBUFFER_LENGTH(e->evb);
188 if (!evtimer_pending(&timerev, NULL))
189 evtimer_add(&timerev, &tv);
191 return 0;
193 err:
194 if (e->evb != NULL)
195 evbuffer_free(e->evb);
196 free(e);
197 return -1;
200 int
201 mcache_lookup(const char *url, struct tab *tab)
203 struct mcache_entry *e;
204 unsigned int slot;
206 slot = ohash_qlookup(&h, url);
207 if ((e = ohash_find(&h, slot)) == NULL)
208 return 0;
210 parser_init(tab, gemtext_initparser);
211 if (!parser_parse(tab, EVBUFFER_DATA(e->evb), EVBUFFER_LENGTH(e->evb)))
212 goto err;
213 if (!parser_free(tab))
214 goto err;
216 tab->buffer.page.name = e->parser_name;
217 tab->trust = e->trust;
218 return 1;
220 err:
221 parser_free(tab);
222 erase_buffer(&tab->buffer);
223 return 0;
226 void
227 mcache_info(size_t *r_npages, size_t *r_tot)
229 *r_npages = npages;
230 *r_tot = tot;