X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?a=blobdiff_plain;f=elpher.el;h=8940f087d8f37e0fe8a953b9ee8e3029adf89673;hb=79d8cf963148534c8ec23a9dbaa6db1325232d85;hp=04f38a33c936bf320c3ba70068880b553470a248;hpb=ab6a018cc080dfedca166a8c053538f3248f1eb6;p=elpher.git diff --git a/elpher.el b/elpher.el index 04f38a3..8940f08 100644 --- a/elpher.el +++ b/elpher.el @@ -4,7 +4,7 @@ ;; Author: Tim Vaughan ;; Created: 11 April 2019 -;; Version: 2.9.0 +;; Version: 2.10.0 ;; Keywords: comm gopher ;; Homepage: http://thelambdalab.xyz/elpher ;; Package-Requires: ((emacs "26.2")) @@ -71,7 +71,7 @@ ;;; Global constants ;; -(defconst elpher-version "2.9.0" +(defconst elpher-version "2.10.0" "Current version of elpher.") (defconst elpher-margin-width 6 @@ -100,6 +100,13 @@ "Association list from types to getters, renderers, margin codes and index faces.") +;;; Internal variables +;; + +(defvar elpher--gemini-page-links '() + "Internal variable containing list of links on page.") + + ;;; Customization group ;; @@ -178,6 +185,11 @@ This can be useful when browsing from a computer that supports IPv6, because some servers which do not support IPv6 can take a long time to time-out." :type '(boolean)) +(defcustom elpher-gemini-number-links nil + "If non-nil, number links in gemini pages when rendering. +Links can be accessed by pressing `v' ('visit') followed by the link number." + :type '(boolean)) + ;; Face customizations (defgroup elpher-faces nil @@ -286,9 +298,23 @@ some servers which do not support IPv6 can take a long time to time-out." ;; Gemini defaults (if (equal (url-filename url) "") (setf (url-filename url) "/")))) - url) + (elpher-remove-redundant-ports url)) (set-match-data data)))) +(defun elpher-remove-redundant-ports (address) + "Remove redundant port specifiers from ADDRESS. +Here 'redundant' means that the specified port matches the default +for that protocol, eg 70 for gopher." + (if (and (not (elpher-address-special-p address)) + (eq (url-portspec address) ; (url-port) is too slow! + (pcase (url-type address) + ("gemini" 1965) + ((or "gopher" "gophers") 70) + ("finger" 79) + (_ -1)))) + (setf (url-portspec address) nil)) + address) + (defun elpher-make-gopher-address (type selector host port &optional tls) "Create an ADDRESS object using gopher directory record attributes. The basic attributes include: TYPE, SELECTOR, HOST and PORT. @@ -650,7 +676,8 @@ the host operating system and the local network capabilities." (t (elpher-network-error address "Connection time-out."))))))) (setq elpher-network-timer timer) - (elpher-buffer-message (concat "Connecting to " host "...")) + (elpher-buffer-message (concat "Connecting to " host "..." + " (press 'u' to abort)")) (set-process-filter proc (lambda (_proc string) (when timer @@ -675,7 +702,8 @@ the host operating system and the local network capabilities." (cond ((string-prefix-p "open" event) ; request URL (elpher-buffer-message - (concat "Connected to " host ". Receiving data...")) + (concat "Connected to " host ". Receiving data..." + " (press 'u' to abort)")) (let ((inhibit-eol-conversion t)) (process-send-string proc query-string))) ((string-prefix-p "deleted" event)) ; do nothing @@ -747,8 +775,8 @@ longer needed for this session." (cert-file (concat temporary-file-directory file-base ".crt"))) (elpher-generate-certificate file-base key-file cert-file t))) -(defun elpher-generate-permanent-certificate (file-base common-name) - "Generate and return details of a persistant certificate. +(defun elpher-generate-persistent-certificate (file-base common-name) + "Generate and return details of a persistent certificate. The argument FILE-BASE is used as the base for the key and certificate files, while COMMON-NAME specifies the common name field of the certificate. @@ -769,22 +797,39 @@ the directory `elpher-certificate-directory'." (expand-file-name key-file) (expand-file-name cert-file)))) +(defun elpher-install-and-use-existing-certificate (key-file-src cert-file-src file-base) + "Install a key+certificate file pair in `elpher-certificate-directory'. +The strings KEY-FILE-SRC and CERT-FILE-SRC are the existing key and +certificate files to install. The argument FILE-BASE is used as the +base for the installed key and certificate files." + (let* ((key-file (concat elpher-certificate-directory file-base ".key")) + (cert-file (concat elpher-certificate-directory file-base ".crt"))) + (if (or (file-exists-p key-file) + (file-exists-p cert-file)) + (error "A certificate with base name %s is already installed" file-base)) + (copy-file key-file-src key-file) + (copy-file cert-file-src cert-file) + (list (elpher-address-host (elpher-page-address elpher-current-page)) + nil + (expand-file-name key-file) + (expand-file-name cert-file)))) + (defun elpher-list-existing-certificates () - "Return a list of the persistant certificates in `elpher-certificate-directory'." + "Return a list of the persistent certificates in `elpher-certificate-directory'." (mapcar (lambda (file) (file-name-sans-extension file)) (directory-files elpher-certificate-directory nil "\.key$"))) (defun elpher-forget-current-certificate () - "Causes any current certificate to be forgotten. + "Causes any current certificate to be forgotten.) In the case of throwaway certificates, the key and certificate files are also deleted." (interactive) (when elpher-client-certificate (unless (and (called-interactively-p 'any) (not (y-or-n-p (concat "Really forget client certificate? " - "(Throwaway certertificates will be deleted.)")))) + "(Throwaway certificates will be deleted.)")))) (when (cadr elpher-client-certificate) (delete-file (elt elpher-client-certificate 2)) (delete-file (elt elpher-client-certificate 3))) @@ -1141,40 +1186,62 @@ that the response was malformed." (insert "Gemini server is requesting a valid TLS certificate:\n\n")) (auto-fill-mode 1) (elpher-gemini-insert-text response-meta)) - (let* ((read-answer-short t)) - (pcase (read-answer "What do you want to do? " - '(("throwaway" ?t - "generate and use throw-away certificate") - ("permanent" ?p - "generate new or use existing permanent certificate") - ("abort" ?a - "stop immediately"))) - ("throwaway" - (setq elpher-client-certificate (elpher-generate-throwaway-certificate))) - ("permanent" - (let* ((existing-certificates (elpher-list-existing-certificates)) - (file-base (completing-read - "Name of new or existing certificate (autocompletes, empty response aborts): " - existing-certificates))) - (if (string-empty-p (string-trim file-base)) - (error "Gemini server requires certificate and none was provided") - (if (member file-base existing-certificates) - (setq elpher-client-certificate - (elpher-get-existing-certificate file-base)) - (let ((common-name (read-string "Common Name field for new certificate: " - file-base))) - (setq elpher-client-certificate - (elpher-generate-permanent-certificate file-base common-name)) - (message "New key and self-signed certificate written to %s" - elpher-certificate-directory)))))) - ("abort" - (error "Gemini server requires a client certificate and none was provided"))) - (elpher-with-clean-buffer) - (elpher-get-gemini-response (elpher-page-address elpher-current-page) renderer))) + (let ((chosen-certificate (elpher-choose-client-certificate))) + (unless chosen-certificate + (error "Gemini server requires a client certificate and none was provided")) + (setq elpher-client-certificate chosen-certificate)) + (elpher-with-clean-buffer) + (elpher-get-gemini-response (elpher-page-address elpher-current-page) renderer)) (_other (error "Gemini server response unknown: %s %s" response-code response-meta)))))) +(defun elpher-choose-client-certificate () + "Prompt for a client certificate to use to establish a TLS connection." + (let* ((read-answer-short t)) + (pcase (read-answer "What do you want to do? " + '(("throwaway" ?t + "generate and use throw-away certificate") + ("persistent" ?p + "generate new or use existing persistent certificate") + ("abort" ?a + "stop immediately"))) + ("throwaway" + (setq elpher-client-certificate (elpher-generate-throwaway-certificate))) + ("persistent" + (let* ((existing-certificates (elpher-list-existing-certificates)) + (file-base (completing-read + "Nickname for new or existing certificate (autocompletes, empty response aborts): " + existing-certificates))) + (if (string-empty-p (string-trim file-base)) + nil + (if (member file-base existing-certificates) + (setq elpher-client-certificate + (elpher-get-existing-certificate file-base)) + (pcase (read-answer "Generate new certificate or install externally-generated one? " + '(("new" ?n + "generate new certificate") + ("install" ?i + "install existing certificate") + ("abort" ?a + "stop immediately"))) + ("new" + (let ((common-name (read-string "Common Name field for new certificate: " + file-base))) + (message "New key and self-signed certificate written to %s" + elpher-certificate-directory) + (elpher-generate-persistent-certificate file-base common-name))) + ("install" + (let* ((cert-file (read-file-name "Certificate file: " nil nil t)) + (key-file (read-file-name "Key file: " nil nil t))) + (message "Key and certificate installed in %s for future use" + elpher-certificate-directory) + (elpher-install-and-use-existing-certificate key-file + cert-file + file-base))) + ("abort" nil)))))) + ("abort" nil)))) + (defun elpher-get-gemini-page (renderer) "Getter which retrieves and renders a Gemini page and renders it using RENDERER." (let* ((address (elpher-page-address elpher-current-page)) @@ -1191,7 +1258,6 @@ that the response was malformed." (error (elpher-network-error address the-error))))) - (defun elpher-render-gemini (body &optional mime-type-string) "Render gemini response BODY with rendering MIME-TYPE-STRING." (if (not body) @@ -1258,7 +1324,10 @@ For instance, the filename /a/b/../c/./d will reduce to /a/c/d" (string-join (reverse path-reversed-normalized) "/"))) (defun elpher-address-from-gemini-url (url) - "Extract address from URL with defaults as per gemini map files." + "Extract address from URL with defaults as per gemini map files. +While there's obviously some redundancy here between this function and +`elpher-address-from-url', gemini map file URLs require enough special +treatment that a separate function is warranted." (let ((address (url-generic-parse-url url)) (current-address (elpher-page-address elpher-current-page))) (unless (and (url-type address) (not (url-fullness address))) ;avoid mangling mailto: urls @@ -1274,10 +1343,10 @@ For instance, the filename /a/b/../c/./d will reduce to /a/c/d" (url-filename address))))) (unless (url-type address) (setf (url-type address) "gemini")) - (if (equal (url-type address) "gemini") - (setf (url-filename address) - (elpher-collapse-dot-sequences (url-filename address))))) - address)) + (when (equal (url-type address) "gemini") + (setf (url-filename address) + (elpher-collapse-dot-sequences (url-filename address))))) + (elpher-remove-redundant-ports address))) (defun elpher-gemini-insert-link (link-line) "Insert link described by LINK-LINE into a text/gemini document." @@ -1286,6 +1355,7 @@ For instance, the filename /a/b/../c/./d will reduce to /a/c/d" (address (elpher-address-from-gemini-url url)) (type (if address (elpher-address-type address) nil)) (type-map-entry (cdr (assoc type elpher-type-map)))) + (setq elpher--gemini-page-links (append elpher--gemini-page-links `(,address))) (when display-string (insert elpher-gemini-link-string) (if type-map-entry @@ -1340,16 +1410,27 @@ width defined by elpher-gemini-max-fill-width." (defun elpher-render-gemini-map (data _parameters) "Render DATA as a gemini map file, PARAMETERS is currently unused." (elpher-with-clean-buffer - (let ((preformatted nil)) + (let ((preformatted nil) + (link-counter 1)) (auto-fill-mode 1) (setq-local fill-column (min (window-width) elpher-gemini-max-fill-width)) + (setq elpher--gemini-page-links '()) (dolist (line (split-string data "\n")) (cond ((string-prefix-p "```" line) (setq preformatted (not preformatted))) (preformatted (insert (elpher-process-text-for-display (propertize line 'face 'elpher-gemini-preformatted)) "\n")) - ((string-prefix-p "=>" line) (elpher-gemini-insert-link line)) + ((string-prefix-p "=>" line) + (progn + (if elpher-gemini-number-links + (insert + (concat + "[" + (number-to-string link-counter) + "] "))) + (setq link-counter (1+ link-counter)) + (elpher-gemini-insert-link line))) ((string-prefix-p "#" line) (elpher-gemini-insert-header line)) (t (elpher-gemini-insert-text line))))) (elpher-cache-content @@ -1445,6 +1526,7 @@ The result is rendered using RENDERER." " - u/mouse-3/U: return to previous page or to the start page\n" " - o/O: visit different selector or the root menu of the current server\n" " - g: go to a particular address (gopher, gemini, finger)\n" + " - v: visit a numbered link on a gemini page\n" " - d/D: download item under cursor or current page\n" " - i/I: info on item under cursor or current page\n" " - c/C: copy URL representation of item under cursor or current page\n" @@ -1455,6 +1537,7 @@ The result is rendered using RENDERER." " - R: reload current page (regenerates cache)\n" " - S: set character coding system for gopher (default is to autodetect)\n" " - T: toggle TLS gopher mode\n" + " - F: forget/discard current TLS client certificate\n" " - .: display the raw server response for the current page\n" "\n" "Start your exploration of gopher space and gemini:\n") @@ -1638,6 +1721,15 @@ When run interactively HOST-OR-URL is read from the minibuffer." (let ((url (read-string "Gopher or Gemini URL: " (elpher-address-to-url address)))) (elpher-visit-page (elpher-make-page url (elpher-address-from-url url))))))) +(defun elpher-visit-gemini-numbered-link (n) + "Visit link designated by a number." + (interactive "nLink number: ") + (if (or (> n (length elpher--gemini-page-links)) + (< n 1)) + (user-error "Invalid link number")) + (let ((address (nth (1- n) elpher--gemini-page-links))) + (elpher-go (url-recreate-url address)))) + (defun elpher-redraw () "Redraw current page." (interactive) @@ -1895,6 +1987,7 @@ When run interactively HOST-OR-URL is read from the minibuffer." (define-key map (kbd "B") 'elpher-bookmarks) (define-key map (kbd "S") 'elpher-set-gopher-coding-system) (define-key map (kbd "F") 'elpher-forget-current-certificate) + (define-key map (kbd "v") 'elpher-visit-gemini-numbered-link) (when (fboundp 'evil-define-key*) (evil-define-key* 'motion map (kbd "TAB") 'elpher-next-link @@ -1922,7 +2015,8 @@ When run interactively HOST-OR-URL is read from the minibuffer." (kbd "X") 'elpher-unbookmark-current (kbd "B") 'elpher-bookmarks (kbd "S") 'elpher-set-gopher-coding-system - (kbd "F") 'elpher-forget-current-certificate)) + (kbd "F") 'elpher-forget-current-certificate + (kbd "v") 'elpher-visit-gemini-numbered-link)) map) "Keymap for gopher client.")