gopher: handle search item and fix lot of stuff

This commit is contained in:
dece 2021-06-06 03:02:32 +02:00
parent 161b5c79e3
commit 403f5da5b4

View file

@ -5,6 +5,7 @@ from enum import Enum
from typing import Optional from typing import Optional
from bebop.browser.browser import Browser from bebop.browser.browser import Browser
from bebop.command_line import CommandLine
from bebop.links import Links from bebop.links import Links
from bebop.metalines import LineType from bebop.metalines import LineType
from bebop.navigation import parse_url, parse_host_and_port from bebop.navigation import parse_url, parse_host_and_port
@ -15,7 +16,7 @@ from bebop.plugins import SchemePlugin
class ItemType(Enum): class ItemType(Enum):
FILE = "0" FILE = "0"
DIR = "1" DIR = "1"
CSO = "2" CCSO = "2"
ERROR = "3" ERROR = "3"
BINHEXED = "4" BINHEXED = "4"
DOS = "5" DOS = "5"
@ -35,10 +36,15 @@ class ItemType(Enum):
_missing_ = lambda s: ItemType.FILE _missing_ = lambda s: ItemType.FILE
UNHANDLED_TYPES = (
ItemType.CCSO, ItemType.ERROR, ItemType.TELNET, ItemType.REDUNDANT,
ItemType.TN3270
)
ICONS = { ICONS = {
ItemType.FILE: "📄", ItemType.FILE: "📄",
ItemType.DIR: "📂", ItemType.DIR: "📂",
ItemType.ERROR: "", ItemType.ERROR: "",
ItemType.SEARCH: "🤔",
ItemType.HTML: "🌐", ItemType.HTML: "🌐",
} }
@ -74,19 +80,33 @@ class GopherPlugin(SchemePlugin):
if type_path_match: if type_path_match:
item_type = ItemType(type_path_match.group(1)) item_type = ItemType(type_path_match.group(1))
path = type_path_match.group(2) path = type_path_match.group(2)
logging.debug(f"Gopher type for URL: type {item_type} path {path}") # Don't try to open a Telnet connection or other silly retro things.
else: if item_type in UNHANDLED_TYPES:
# Else you're on your own! If we're opening a link of a map, find browser.set_status_error(f"Unhandled item {item_type.name}.")
# that item type. Default to DIR item type. return None
# Let user input some text for search items.
if item_type == ItemType.SEARCH:
user_input = browser.get_user_text_input(
"Input:",
CommandLine.CHAR_TEXT,
strip=True
)
if not user_input:
return None
item_type = ItemType.DIR
previous_search_index = path.find("%09")
if previous_search_index > -1:
path = path[:previous_search_index]
path = f"{path}\t{user_input}"
# Note that we don't try to handle "h" items here because if the URL
# actually uses http scheme, it should not end up in this plugin.
else:
item_type = ItemType.DIR item_type = ItemType.DIR
if browser.current_page:
for meta, line in browser.current_page.metalines:
if meta.get("url") == url:
item_type = meta.get("gophertype") or ItemType.DIR
break
logging.debug(f"Gopher type from current map: type {item_type}")
browser.set_status(f"Loading {url}") # If we have text search in our path, encode it for UI & logging.
encoded_path = path.replace("\t", "%09")
browser.set_status(f"Loading {host} {port} '{encoded_path}'")
timeout = browser.config["connect_timeout"] timeout = browser.config["connect_timeout"]
try: try:
response = self.request(host, port, path, timeout) response = self.request(host, port, path, timeout)
@ -94,7 +114,9 @@ class GopherPlugin(SchemePlugin):
except GopherPluginException as exc: except GopherPluginException as exc:
browser.set_status_error("Error: " + exc.message) browser.set_status_error("Error: " + exc.message)
return None return None
browser.load_page(page) browser.load_page(page)
url = f"gopher://{host}:{port}/{item_type.value}{encoded_path}"
browser.current_url = url browser.current_url = url
return url return url
@ -167,7 +189,6 @@ def parse_source(source: str, item_type: ItemType):
meta = { meta = {
"type": LineType.LINK, "type": LineType.LINK,
"gophertype": item_type,
"url": link_url, "url": link_url,
"link": current_link_id "link": current_link_id
} }