2 * Copyright (c) 2021 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.
18 * Handles config and runtime files
24 #include <sys/types.h>
39 #include "telescope.h"
45 #define nitems(x) (sizeof(x) / sizeof(x[0]))
48 static void die(void) __attribute__((__noreturn__));
49 static int select_non_dot(const struct dirent *);
50 static int select_non_dotdot(const struct dirent *);
51 static size_t join_path(char*, const char*, const char*, size_t);
52 static void getenv_default(char*, const char*, const char*, size_t);
53 static void mkdirs(const char*, mode_t);
54 static void init_paths(void);
55 static void load_last_session(void);
56 static void load_hist(void);
57 static int last_time_crashed(void);
58 static void load_certs(struct ohash *);
61 * Where to store user data. These are all equal to ~/.telescope if
64 char config_path_base[PATH_MAX];
65 char data_path_base[PATH_MAX];
66 char cache_path_base[PATH_MAX];
68 char ctlsock_path[PATH_MAX];
69 char config_path[PATH_MAX];
70 char lockfile_path[PATH_MAX];
71 char bookmark_file[PATH_MAX];
72 char known_hosts_file[PATH_MAX], known_hosts_tmp[PATH_MAX];
73 char crashed_file[PATH_MAX];
74 char session_file[PATH_MAX], session_file_tmp[PATH_MAX];
75 char history_file[PATH_MAX], history_file_tmp[PATH_MAX];
77 static void __attribute__((__noreturn__))
84 select_non_dot(const struct dirent *d)
86 return strcmp(d->d_name, ".");
90 select_non_dotdot(const struct dirent *d)
92 return strcmp(d->d_name, ".") && strcmp(d->d_name, "..");
96 send_dir(struct tab *tab, const char *path)
98 struct dirent **names;
99 int (*selector)(const struct dirent *) = select_non_dot;
104 * need something to fake a redirect
107 if (!has_suffix(path, "/")) {
108 if (asprintf(&s, "%s/", path) == -1)
110 send_hdr(peerid, 30, s);
116 if (!strcmp(path, "/"))
117 selector = select_non_dotdot;
119 if ((len = scandir(path, &names, selector, alphasort)) == -1) {
120 load_page_from_str(tab, "# failure reading the directory\n");
124 parser_init(tab, gemtext_initparser);
125 parser_parsef(tab, "# Index of %s\n\n", path);
127 for (i = 0; i < len; ++i) {
128 const char *sufx = "";
130 if (names[i]->d_type == DT_DIR)
133 parser_parsef(tab, "=> %s%s\n", names[i]->d_name, sufx);
145 if (fstat(fileno(fp), &sb) == -1)
148 return S_ISDIR(sb.st_mode);
152 file_type(const char *path)
158 {"diff", textpatch_initparser},
159 {"gemini", gemtext_initparser},
160 {"gmi", gemtext_initparser},
161 {"markdown", textplain_initparser},
162 {"md", textplain_initparser},
163 {"patch", gemtext_initparser},
168 if ((dot = strrchr(path, '.')) == NULL)
169 return textplain_initparser;
173 for (m = ms; m->ext != NULL; ++m)
174 if (!strcmp(m->ext, dot))
177 return textplain_initparser;
181 fs_load_url(struct tab *tab, const char *url)
183 const char *bpath = "bookmarks.gmi", *fallback = "# Not found\n";
184 parserinit initfn = gemtext_initparser;
195 {"about", NULL, about_about, about_about_len},
196 {"blank", NULL, about_blank, about_blank_len},
197 {"bookmarks", bpath, bookmarks, bookmarks_len},
198 {"crash", NULL, about_crash, about_crash_len},
199 {"help", NULL, about_help, about_help_len},
200 {"license", NULL, about_license, about_license_len},
201 {"new", NULL, about_new, about_new_len},
204 if (!strncmp(url, "about:", 6)) {
207 for (i = 0; page == NULL && i < nitems(pages); ++i) {
208 if (!strcmp(url, pages[i].name))
215 strlcpy(path, data_path_base, sizeof(path));
216 strlcat(path, "/", sizeof(path));
217 if (page->path != NULL)
218 strlcat(path, page->path, sizeof(path));
220 strlcat(path, "page/about_", sizeof(path));
221 strlcat(path, page->name, sizeof(path));
222 strlcat(path, ".gmi", sizeof(path));
225 fallback = page->data;
226 } else if (!strncmp(url, "file://", 7)) {
228 strlcpy(path, url, sizeof(path));
229 initfn = file_type(url);
233 if ((fp = fopen(path, "r")) == NULL)
242 parser_init(tab, initfn);
246 r = fread(buf, 1, sizeof(buf), fp);
247 if (!parser_parse(tab, buf, r))
249 if (r != sizeof(buf))
258 load_page_from_str(tab, fallback);
262 bookmark_page(const char *url)
266 if ((f = fopen(bookmark_file, "a")) == NULL)
268 fprintf(f, "=> %s\n", url);
274 save_cert(const struct tofu_entry *e)
278 if ((f = fopen(known_hosts_file, "a")) == NULL)
280 fprintf(f, "%s %s %d\n", e->domain, e->hash, e->verified);
286 update_cert(const struct tofu_entry *e)
289 char sfn[PATH_MAX], *line = NULL, *t;
290 size_t l, linesize = 0;
294 strlcpy(sfn, known_hosts_tmp, sizeof(sfn));
295 if ((fd = mkstemp(sfn)) == -1 ||
296 (tmp = fdopen(fd, "w")) == NULL) {
304 if ((f = fopen(known_hosts_file, "r")) == NULL) {
310 l = strlen(e->domain);
311 while ((linelen = getline(&line, &linesize, f)) != -1) {
312 if ((t = strstr(line, e->domain)) != NULL &&
313 (line[l] == ' ' || line[l] == '\t'))
315 /* line has a trailing \n */
316 fprintf(tmp, "%s", line);
318 fprintf(tmp, "%s %s %d\n", e->domain, e->hash, e->verified);
331 if (rename(sfn, known_hosts_file))
337 join_path(char *buf, const char *lhs, const char *rhs, size_t buflen)
339 strlcpy(buf, lhs, buflen);
340 return strlcat(buf, rhs, buflen);
344 getenv_default(char *buf, const char *name, const char *def, size_t buflen)
349 if ((home = getenv("HOME")) == NULL)
350 errx(1, "HOME is not defined");
352 if ((env = getenv(name)) != NULL)
353 ret = strlcpy(buf, env, buflen);
355 ret = join_path(buf, home, def, buflen);
358 errx(1, "buffer too small for %s", name);
362 mkdirs(const char *path, mode_t mode)
364 char copy[PATH_MAX+1], orig[PATH_MAX+1], *parent;
366 strlcpy(copy, path, sizeof(copy));
367 strlcpy(orig, path, sizeof(orig));
368 parent = dirname(copy);
369 if (!strcmp(parent, "/"))
371 mkdirs(parent, mode);
373 if (mkdir(orig, mode) != 0) {
376 err(1, "can't mkdir %s", orig);
383 char xdg_config_base[PATH_MAX];
384 char xdg_data_base[PATH_MAX];
385 char xdg_cache_base[PATH_MAX];
386 char old_path[PATH_MAX];
391 if ((home = getenv("HOME")) == NULL)
392 errx(1, "HOME is not defined");
393 join_path(old_path, home, "/.telescope", sizeof(old_path));
395 /* if ~/.telescope exists, use that instead of xdg dirs */
396 if (stat(old_path, &info) == 0 && S_ISDIR(info.st_mode)) {
397 join_path(config_path_base, home, "/.telescope",
398 sizeof(config_path_base));
399 join_path(data_path_base, home, "/.telescope",
400 sizeof(data_path_base));
401 join_path(cache_path_base, home, "/.telescope",
402 sizeof(cache_path_base));
407 getenv_default(xdg_config_base, "XDG_CONFIG_HOME", "/.config",
408 sizeof(xdg_config_base));
409 getenv_default(xdg_data_base, "XDG_DATA_HOME", "/.local/share",
410 sizeof(xdg_data_base));
411 getenv_default(xdg_cache_base, "XDG_CACHE_HOME", "/.cache",
412 sizeof(xdg_cache_base));
414 join_path(config_path_base, xdg_config_base, "/telescope",
415 sizeof(config_path_base));
416 join_path(data_path_base, xdg_data_base, "/telescope",
417 sizeof(data_path_base));
418 join_path(cache_path_base, xdg_cache_base, "/telescope",
419 sizeof(cache_path_base));
421 mkdirs(xdg_config_base, S_IRWXU);
422 mkdirs(xdg_data_base, S_IRWXU);
423 mkdirs(xdg_cache_base, S_IRWXU);
425 mkdirs(config_path_base, S_IRWXU);
426 mkdirs(data_path_base, S_IRWXU);
427 mkdirs(cache_path_base, S_IRWXU);
435 join_path(ctlsock_path, cache_path_base, "/ctl",
436 sizeof(ctlsock_path));
437 join_path(config_path, config_path_base, "/config",
438 sizeof(config_path));
439 join_path(lockfile_path, cache_path_base, "/lock",
440 sizeof(lockfile_path));
441 join_path(bookmark_file, data_path_base, "/bookmarks.gmi",
442 sizeof(bookmark_file));
443 join_path(known_hosts_file, data_path_base, "/known_hosts",
444 sizeof(known_hosts_file));
445 join_path(known_hosts_tmp, cache_path_base,
446 "/known_hosts.tmp.XXXXXXXXXX", sizeof(known_hosts_tmp));
447 join_path(session_file, cache_path_base, "/session",
448 sizeof(session_file));
449 join_path(session_file_tmp, cache_path_base, "/session.XXXXXXXXXX",
450 sizeof(session_file));
451 join_path(history_file, cache_path_base, "/history",
452 sizeof(history_file));
453 join_path(history_file_tmp, cache_path_base, "/history.XXXXXXXXXX",
454 sizeof(history_file));
455 join_path(crashed_file, cache_path_base, "/crashed",
456 sizeof(crashed_file));
462 * Parse a line of the session file and restores it. The format is:
464 * URL [flags,...] [title]\n
466 static inline struct tab *
467 parse_session_line(char *line, struct tab **ct)
471 const char *uri, *title = "";
472 int current = 0, killed = 0;
473 size_t top_line = 0, current_line = 0;
476 if ((s = strchr(line, ' ')) == NULL)
481 if ((t = strchr(s, ' ')) != NULL) {
486 while ((ap = strsep(&s, ",")) != NULL) {
487 if (!strcmp(ap, "current"))
489 else if (!strcmp(ap, "killed"))
491 else if (has_prefix(ap, "top="))
492 top_line = strtonum(ap+4, 0, UINT32_MAX, NULL);
493 else if (has_prefix(ap, "cur="))
494 current_line = strtonum(ap+4, 0, UINT32_MAX, NULL);
497 if (top_line > current_line) {
502 if ((tab = new_tab(uri, NULL, NULL)) == NULL)
504 tab->hist_cur->line_off = top_line;
505 tab->hist_cur->current_off = current_line;
506 strlcpy(tab->buffer.page.title, title, sizeof(tab->buffer.page.title));
517 sendhist(struct tab *tab, const char *uri, int future)
521 if ((h = calloc(1, sizeof(*h))) == NULL)
523 strlcpy(h->h, uri, sizeof(h->h));
526 hist_push(&tab->hist, h);
528 hist_add_before(&tab->hist, tab->hist_cur, h);
532 load_last_session(void)
534 struct tab *tab = NULL, *ct = NULL;
539 char *nl, *s, *line = NULL;
541 if ((session = fopen(session_file, "r")) == NULL) {
542 new_tab("about:new", NULL, NULL);
543 switch_to_tab(new_tab("about:help", NULL, NULL));
547 while ((linelen = getline(&line, &linesize, session)) != -1) {
548 if ((nl = strchr(line, '\n')) != NULL)
551 if (*line == '<' || *line == '>') {
552 future = *line == '>';
554 if (*s != ' ' || tab == NULL)
556 sendhist(tab, ++s, future);
558 tab = parse_session_line(line, &ct);
568 if (last_time_crashed())
569 switch_to_tab(new_tab("about:crash", NULL, NULL));
578 char *nl, *spc, *line = NULL;
582 if ((hist = fopen(history_file, "r")) == NULL)
585 while ((linelen = getline(&line, &linesize, hist)) != -1) {
586 if ((nl = strchr(line, '\n')) != NULL)
588 if ((spc = strchr(line, ' ')) == NULL)
593 memset(&hi, 0, sizeof(hi));
594 hi.ts = strtonum(line, INT64_MIN, INT64_MAX, &errstr);
597 if (strlcpy(hi.uri, spc, sizeof(hi.uri)) >= sizeof(hi.uri))
610 fs_load_state(struct ohash *certs)
619 * Check if the last time telescope crashed. The check is done by
620 * looking at `crashed_file': if it exists then last time we crashed.
621 * Then, while here, touch the file too. During IMSG_QUIT we'll
625 last_time_crashed(void)
632 if (unlink(crashed_file) == -1 && errno == ENOENT)
635 if ((fd = open(crashed_file, O_CREAT|O_WRONLY, 0600)) == -1)
648 if ((fd = open(lockfile_path, O_WRONLY|O_CREAT, 0600)) == -1)
653 lock.l_type = F_WRLCK;
654 lock.l_whence = SEEK_SET;
656 if (fcntl(fd, F_SETLK, &lock) == -1) {
665 parse_khost_line(char *line, char *tmp[3])
669 for (ap = tmp; ap < &tmp[3] &&
670 (*ap = strsep(&line, " \t\n")) != NULL;) {
675 return ap == &tmp[3] && *line == '\0';
679 load_certs(struct ohash *certs)
681 char *tmp[3], *line = NULL;
683 size_t lineno = 0, linesize = 0;
686 struct tofu_entry *e;
688 if ((f = fopen(known_hosts_file, "r")) == NULL)
691 if ((e = calloc(1, sizeof(*e))) == NULL) {
696 while ((linelen = getline(&line, &linesize, f)) != -1) {
699 if (parse_khost_line(line, tmp)) {
700 strlcpy(e->domain, tmp[0], sizeof(e->domain));
701 strlcpy(e->hash, tmp[1], sizeof(e->hash));
703 e->verified = strtonum(tmp[2], 0, 1, &errstr);
705 errx(1, "%s:%zu verification for %s is %s: %s",
706 known_hosts_file, lineno,
707 e->domain, errstr, tmp[2]);
711 warnx("%s:%zu invalid entry",
712 known_hosts_file, lineno);