diff --git a/av98.py b/av98.py index 645ba26..25b01db 100755 --- a/av98.py +++ b/av98.py @@ -43,6 +43,7 @@ except ModuleNotFoundError: from cache import Cache from tofu import TofuStore from clientcerts import ClientCertificateManager +import util _VERSION = "1.0.2dev" @@ -311,9 +312,7 @@ class GeminiClient(cmd.Cmd): webbrowser.open_new_tab(gi.url) return else: - print("Do you want to try to open this link with a http proxy?") - resp = input("(Y)/N ") - if resp.strip().lower() in ("n","no"): + if not util.ask_yes_no("Do you want to try to open this link with a http proxy?", True): webbrowser.open_new_tab(gi.url) return elif gi.scheme == "gopher" and not self.options.get("gopher_proxy", None): @@ -418,17 +417,17 @@ you'll be able to transparently follow links to Gopherspace!""") raise RuntimeError("Refusing to follow more than %d consecutive redirects!" % _MAX_REDIRECTS) # Never follow cross-domain redirects without asking elif new_gi.host != gi.host: - follow = input("Follow cross-domain redirect to %s? (y/n) " % new_gi.url) + follow = util.ask_yes_no("Follow cross-domain redirect to %s?" % new_gi.url) # Never follow cross-protocol redirects without asking elif new_gi.scheme != gi.scheme: - follow = input("Follow cross-protocol redirect to %s? (y/n) " % new_gi.url) + follow = util.ask_yes_no("Follow cross-protocol redirect to %s?" % new_gi.url) # Don't follow *any* redirect without asking if auto-follow is off elif not self.options["auto_follow_redirects"]: - follow = input("Follow redirect to %s? (y/n) " % new_gi.url) + follow = util.ask_yes_no("Follow redirect to %s?" % new_gi.url) # Otherwise, follow away else: - follow = "yes" - if follow.strip().lower() not in ("y", "yes"): + follow = True + if not follow: raise UserAbortException() ui_out.debug("Following redirect to %s." % new_gi.url) ui_out.debug("This is consecutive redirect number %d." % len(previous_redirectors)) diff --git a/clientcerts.py b/clientcerts.py index f3b7965..f7c7092 100644 --- a/clientcerts.py +++ b/clientcerts.py @@ -4,6 +4,8 @@ import os import os.path import uuid +import util + ui_out = logging.getLogger("av98_logger") class ClientCertificateManager: @@ -51,30 +53,24 @@ class ClientCertificateManager: # Are we crossing a domain boundary? if self.client_certs["active"] and gi.host not in self.active_cert_domains: if self.active_is_transient: - print("Permanently delete currently active transient certificate?") - resp = input("Y/N? ") - if resp.strip().lower() in ("y", "yes"): + if util.ask_yes_no("Permanently delete currently active transient certificate?"): print("Destroying certificate.") self._deactivate_client_cert() else: print("Staying here.") return False else: - print("PRIVACY ALERT: Deactivate client cert before connecting to a new domain?") - resp = input("Y/N? ") - if resp.strip().lower() in ("n", "no"): + if util.ask_yes_no("PRIVACY ALERT: Deactivate client cert before connecting to a new domain?"): + print("Deactivating certificate.") + self._deactivate_client_cert() + else: print("Keeping certificate active for {}".format(gi.host)) self.active_cert_domains.append(gi.host) self.client_certs[gi.host] = self.client_certs["active"] - else: - print("Deactivating certificate.") - self._deactivate_client_cert() # Suggest reactivating previous certs if not self.client_certs["active"] and gi.host in self.client_certs: - print("PRIVACY ALERT: Reactivate previously used client cert for {}?".format(gi.host)) - resp = input("Y/N? ") - if resp.strip().lower() in ("y", "yes"): + if util.ask_yes_no("PRIVACY ALERT: Reactivate previously used client cert for {}?".format(gi.host)): self._activate_client_cert(*self.client_certs[gi.host]) self.active_cert_domains.append(gi.host) else: diff --git a/util.py b/util.py new file mode 100644 index 0000000..a035c28 --- /dev/null +++ b/util.py @@ -0,0 +1,16 @@ +def ask_yes_no(prompt, default=None): + print(prompt) + if default == True: + prompt = "(Y)/N: " + elif default == False: + prompt = "Y/(N): " + else: + prompt = "Y/N: " + while True: + resp = input(prompt) + if not resp.strip() and default != None: + return efault + elif resp.strip().lower() in ("y", "yes"): + return True + elif resp.strip().lower() in ("n","no"): + return False