Commit Diff


commit - 9073042649752b844bd9b22f67aa88db8317b44d
commit + 56e7efb45b77fdf3a17848ae698aae27f554af7c
blob - f5c1124f151d89f0b0239cec4e4410e95398b808
blob + 9deeb69cb5c2f4c0b36385c29dfccc669fee25ac
--- ChangeLog
+++ ChangeLog
@@ -1,4 +1,6 @@
 2021-07-21  Omar Polo  <op@omarpolo.com>
+
+	* fs.c (handle_get): allow about: pages to be overridden by ~/.telescope/pages/about_*.gmi
 
 	* cmd.c (cmd_tab_close): prefer the next tab instead of the previous as target in tab-close.
 
blob - a66906bb5e29db3235909bdcc0c709bb9b51dbe3
blob + 4f505881be945dff15d8145da221318c9cf16798
--- Makefile.am
+++ Makefile.am
@@ -77,3 +77,4 @@ pages.c: pagebundler $(srcdir)/pages.h ${PAGES}
 	./pagebundler -f $(srcdir)/pages/about_license.gmi -v about_license >> $@
 	./pagebundler -f $(srcdir)/pages/about_new.gmi     -v about_new     >> $@
 	./pagebundler -f $(srcdir)/pages/about_crash.gmi   -v about_crash   >> $@
+	./pagebundler -f $(srcdir)/pages/bookmarks.gmi     -v bookmarks     >> $@
blob - cdcdc01428ba60e2d4de1aa1fe3bb5cad5692585
blob + 83f351622c5781590eaea05b34fabfc7afc11438
--- fs.c
+++ fs.c
@@ -34,8 +34,6 @@
 #include "pages.h"
 
 static void		 die(void) __attribute__((__noreturn__));
-static void		 serve_bookmarks(uint32_t);
-static void		 send_page(struct imsg *, const uint8_t *, size_t);
 static void		 handle_get(struct imsg*, size_t);
 static void		 handle_quit(struct imsg*, size_t);
 static void		 handle_bookmark_page(struct imsg*, size_t);
@@ -52,6 +50,7 @@ static int		 fs_send_ui(int, uint32_t, int, const void
 static struct imsgev		*iev_ui;
 static FILE			*session;
 
+static char	base_path[PATH_MAX];
 static char	lockfile_path[PATH_MAX];
 static char	bookmark_file[PATH_MAX];
 static char	known_hosts_file[PATH_MAX], known_hosts_tmp[PATH_MAX];
@@ -79,77 +78,74 @@ die(void)
 }
 
 static void
-serve_bookmarks(uint32_t peerid)
-{
-	const char	*t;
-	char		 buf[BUFSIZ];
-	size_t		 r;
-	FILE		*f;
-
-	if ((f = fopen(bookmark_file, "r")) == NULL) {
-		t = "# Bookmarks\n\n"
-		    "No bookmarks yet!\n"
-		    "Create ~/.telescope/bookmarks.gmi or use `bookmark-page'.\n";
-		fs_send_ui(IMSG_BUF, peerid, -1, t, strlen(t));
-		fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
-		return;
-	}
-
-	for (;;) {
-		r = fread(buf, 1, sizeof(buf), f);
-		fs_send_ui(IMSG_BUF, peerid, -1, buf, r);
-		if (r != sizeof(buf))
-			break;
-	}
-
-	fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
-
-	fclose(f);
-}
-
-static void
-send_page(struct imsg *imsg, const uint8_t *page, size_t len)
-{
-	fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1, page, len);
-	fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1, NULL, 0);
-}
-
-static void
 handle_get(struct imsg *imsg, size_t datalen)
 {
+	const char	*bpath = "bookmarks.gmi";
+	char		 path[PATH_MAX], buf[BUFSIZ];
+	FILE		*f;
 	const char	*data, *p;
+	ssize_t		 r;
 	size_t		 i;
 	struct page {
 		const char	*name;
-		void		(*handle)(uint32_t);
+		const char	*path;
 		const uint8_t	*data;
 		size_t		 len;
 	} pages[] = {
-		{"about:about",		NULL,	about_about,	about_about_len},
-		{"about:blank",		NULL,	about_blank,	about_blank_len},
-		{"about:bookmarks",	serve_bookmarks, 0,	0},
-		{"about:crash",		NULL,	about_crash,	about_crash_len},
-		{"about:help",		NULL,	about_help,	about_help_len},
-		{"about:license",	NULL,	about_license,	about_license_len},
-		{"about:new",		NULL,	about_new,	about_new_len},
-	};
+		{"about",	NULL,	about_about,	about_about_len},
+		{"blank",	NULL,	about_blank,	about_blank_len},
+		{"bookmarks",	bpath,	bookmarks,	bookmarks_len},
+		{"crash",	NULL,	about_crash,	about_crash_len},
+		{"help",	NULL,	about_help,	about_help_len},
+		{"license",	NULL,	about_license,	about_license_len},
+		{"new",		NULL,	about_new,	about_new_len},
+	}, *page = NULL;
 
 	data = imsg->data;
-
-	if (data[datalen-1] != '\0')
+	if (data[datalen-1] != '\0') /* make sure it's NUL-terminated */
 		die();
+	if ((data = strchr(data, ':')) == NULL)
+		goto notfound;
+	data++;
 
-	for (i = 0; i < sizeof(pages)/sizeof(pages[0]); ++i) {
-		if (strcmp(data, pages[i].name) != 0)
-			continue;
-
-		if (pages[i].handle != NULL)
-			pages[i].handle(imsg->hdr.peerid);
-		else
-			send_page(imsg, pages[i].data, pages[i].len);
+	for (i = 0; i < sizeof(pages)/sizeof(pages[0]); ++i)
+		if (!strcmp(data, pages[i].name)) {
+			page = &pages[i];
+			break;
+		}
+
+	if (page == NULL)
+		goto notfound;
+
+	strlcpy(path, base_path, sizeof(path));
+	strlcat(path, "/", sizeof(path));
+	if (page->path != NULL)
+		strlcat(path, page->path, sizeof(path));
+	else {
+		strlcat(path, "pages/about_", sizeof(path));
+		strlcat(path, page->name, sizeof(path));
+		strlcat(path, ".gmi", sizeof(path));
+	}
+
+	if ((f = fopen(path, "r")) == NULL) {
+		fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1,
+		    page->data, page->len);
+		fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1,
+		    NULL, 0);
 		return;
 	}
 
+	for (;;) {
+		r = fread(buf, 1, sizeof(buf), f);
+		fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1, buf, r);
+		if (r != sizeof(buf))
+			break;
+	}
+	fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1, NULL, 0);
+	fclose(f);
+	return;
+
+notfound:
 	p = "# not found!\n";
 	fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1, p, strlen(p));
 	fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1, NULL, 0);
@@ -381,30 +377,28 @@ fs_send_ui(int type, uint32_t peerid, int fd, const vo
 int
 fs_init(void)
 {
-	char	dir[PATH_MAX];
-
-	strlcpy(dir, getenv("HOME"), sizeof(dir));
-	strlcat(dir, "/.telescope", sizeof(dir));
-	mkdir(dir, 0700);
+	strlcpy(base_path, getenv("HOME"), sizeof(base_path));
+	strlcat(base_path, "/.telescope", sizeof(base_path));
+	mkdir(base_path, 0700);
 
-	strlcpy(lockfile_path, getenv("HOME"), sizeof(lockfile_path));
-	strlcat(lockfile_path, "/.telescope/lock", sizeof(lockfile_path));
+	strlcpy(lockfile_path, base_path, sizeof(lockfile_path));
+	strlcat(lockfile_path, "/lock", sizeof(lockfile_path));
 
-	strlcpy(bookmark_file, getenv("HOME"), sizeof(bookmark_file));
-	strlcat(bookmark_file, "/.telescope/bookmarks.gmi", sizeof(bookmark_file));
+	strlcpy(bookmark_file, base_path, sizeof(bookmark_file));
+	strlcat(bookmark_file, "/bookmarks.gmi", sizeof(bookmark_file));
 
-	strlcpy(known_hosts_file, getenv("HOME"), sizeof(known_hosts_file));
-	strlcat(known_hosts_file, "/.telescope/known_hosts", sizeof(known_hosts_file));
+	strlcpy(known_hosts_file, base_path, sizeof(known_hosts_file));
+	strlcat(known_hosts_file, "/known_hosts", sizeof(known_hosts_file));
 
-	strlcpy(known_hosts_tmp, getenv("HOME"), sizeof(known_hosts_tmp));
-	strlcat(known_hosts_tmp, "/.telescope/known_hosts.tmp.XXXXXXXXXX",
+	strlcpy(known_hosts_tmp, base_path, sizeof(known_hosts_tmp));
+	strlcat(known_hosts_tmp, "/known_hosts.tmp.XXXXXXXXXX",
 	    sizeof(known_hosts_file));
 
-	strlcpy(session_file, getenv("HOME"), sizeof(session_file));
-	strlcat(session_file, "/.telescope/session", sizeof(session_file));
+	strlcpy(session_file, base_path, sizeof(session_file));
+	strlcat(session_file, "/session", sizeof(session_file));
 
-	strlcpy(crashed_file, getenv("HOME"), sizeof(crashed_file));
-	strlcat(crashed_file, "/.telescope/crashed", sizeof(crashed_file));
+	strlcpy(crashed_file, base_path, sizeof(crashed_file));
+	strlcat(crashed_file, "/crashed", sizeof(crashed_file));
 
 	return 1;
 }
blob - /dev/null
blob + b7d11fff56505b844c2b07147aa9b9b5982456eb (mode 644)
--- /dev/null
+++ pages/bookmarks.gmi
@@ -0,0 +1,5 @@
+# Bookmarks
+
+No bookmarks yet!
+
+Create ~/.telescope/bookmarks.gmi or use ‘bookmark-page’.
blob - 4bb343be226b5c1c85b2876cb6dbed8ce5dde200
blob + 93fd09804c8dc8c83fd775626dbc9a2de14f997d
--- pages.h
+++ pages.h
@@ -38,4 +38,7 @@ extern size_t		 about_license_len;
 extern const uint8_t	 about_new[];
 extern size_t		 about_new_len;
 
+extern const uint8_t	 bookmarks[];
+extern size_t		 bookmarks_len;
+
 #endif
blob - 4d0c2c8b823f2cf5a15beca3e205aa3f53494dc1
blob + 1b0274bc80eac0b4e284f058cfc851f16440b495
--- telescope.1
+++ telescope.1
@@ -761,6 +761,8 @@ See the TOFU section for more info.
 Lock file used to prevent multiple instance of
 .Nm
 from running at the same time.
+.It Pa ~/.telescope/pages/about_*.gmi
+Overrides for built-in about: pages.
 .It Pa ~/.telescope/session
 Contains the list of tabs from the last session.
 Every line identifies a tab.