Reuse a consistent function for getting Y/N type user input.

This commit is contained in:
Solderpunk 2023-11-19 10:20:54 +01:00
parent 03b90fcd5e
commit 73ce79310d
3 changed files with 31 additions and 20 deletions

15
av98.py
View File

@ -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))

View File

@ -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:

16
util.py Normal file
View File

@ -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