diff --git a/config/config.go b/config/config.go index 71ebc97..784ac0c 100644 --- a/config/config.go +++ b/config/config.go @@ -145,6 +145,7 @@ func Init() error { } viper.SetDefault("a-general.home", "gemini.circumlunar.space") + viper.SetDefault("a-general.auto_redirect", false) viper.SetDefault("a-general.http", "default") viper.SetDefault("a-general.search", "gus.guru/search") viper.SetDefault("a-general.color", true) diff --git a/config/default.go b/config/default.go index 89f85e4..572716f 100644 --- a/config/default.go +++ b/config/default.go @@ -16,6 +16,11 @@ var defaultConf = []byte(`# This is the default config file. # Press Ctrl-H to access it home = "gemini://gemini.circumlunar.space" +# Follow up to 5 Gemini redirects without prompting. +# A prompt is always shown after the 5th redirect and for redirects to protocols other than Gemini. +# If set to false, a prompt will be shown before following redirects. +auto_redirect = false + # What command to run to open a HTTP URL. Set to "default" to try to guess the browser, # or set to "off" to not open HTTP URLs. # If a command is set, than the URL will be added (in quotes) to the end of the command. diff --git a/default-config.toml b/default-config.toml index ff204ac..c304bf7 100644 --- a/default-config.toml +++ b/default-config.toml @@ -13,6 +13,11 @@ # Press Ctrl-H to access it home = "gemini://gemini.circumlunar.space" +# Follow up to 5 Gemini redirects without prompting. +# A prompt is always shown after the 5th redirect and for redirects to protocols other than Gemini. +# If set to false, a prompt will be shown before following redirects. +auto_redirect = false + # What command to run to open a HTTP URL. Set to "default" to try to guess the browser, # or set to "off" to not open HTTP URLs. # If a command is set, than the URL will be added (in quotes) to the end of the command. diff --git a/display/display.go b/display/display.go index 74cfe35..353f68c 100644 --- a/display/display.go +++ b/display/display.go @@ -521,7 +521,7 @@ func Reload() { go func(t *tab) { cache.RemovePage(tabs[curTab].page.URL) cache.RemoveFavicon(parsed.Host) - handleURL(t, t.page.URL) // goURL is not used bc history shouldn't be added to + handleURL(t, t.page.URL, 0) // goURL is not used bc history shouldn't be added to if t == tabs[curTab] { // Display the bottomBar state that handleURL set t.applyBottomBar() diff --git a/display/history.go b/display/history.go index df60641..333adcc 100644 --- a/display/history.go +++ b/display/history.go @@ -2,7 +2,7 @@ package display // applyHist is a history.go internal function, to load a URL in the history. func applyHist(t *tab) { - handleURL(t, t.history.urls[t.history.pos]) // Load that position in history + handleURL(t, t.history.urls[t.history.pos], 0) // Load that position in history t.applyAll() } diff --git a/display/private.go b/display/private.go index 828390e..edc5214 100644 --- a/display/private.go +++ b/display/private.go @@ -267,7 +267,7 @@ func handleFavicon(t *tab, host, old string) { // // It should be called in a goroutine. func goURL(t *tab, u string) { - final, displayed := handleURL(t, u) + final, displayed := handleURL(t, u, 0) if displayed { t.addToHistory(final) } @@ -289,7 +289,10 @@ func goURL(t *tab, u string) { // // The bottomBar is not actually changed in this func, except during loading. // The func that calls this one should apply the bottomBar values if necessary. -func handleURL(t *tab, u string) (string, bool) { +// +// numRedirects is the number of redirects that resulted in the provided URL. +// It should typically be 0. +func handleURL(t *tab, u string, numRedirects int) (string, bool) { defer App.Draw() // Just in case // Save for resetting on error @@ -421,7 +424,7 @@ func handleURL(t *tab, u string) (string, bool) { Error("Input Error", "URL for that input would be too long.") return ret("", false) } - return ret(handleURL(t, parsed.String())) + return ret(handleURL(t, parsed.String(), 0)) } return ret("", false) case 30: @@ -431,11 +434,22 @@ func handleURL(t *tab, u string) (string, bool) { return ret("", false) } redir := parsed.ResolveReference(parsedMeta).String() - if YesNo("Follow redirect?\n" + redir) { + // Prompt before redirecting to non-Gemini protocol + redirect := false + if !strings.HasPrefix(redir, "gemini") { + if YesNo("Follow redirect to non-Gemini URL?\n" + redir) { + redirect = true + } else { + return ret("", false) + } + } + // Prompt before redirecting + autoRedirect := viper.GetBool("a-general.auto_redirect") + if redirect || (autoRedirect && numRedirects < 5) || YesNo("Follow redirect?\n"+redir) { if res.Status == gemini.StatusRedirectPermanent { go cache.AddRedir(u, redir) } - return ret(handleURL(t, redir)) + return ret(handleURL(t, redir, numRedirects+1)) } return ret("", false) case 40: