X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?p=elpher.git;a=blobdiff_plain;f=elpher.el;h=c6ee0b491db985189e1398ac2aa4f041d0f601af;hp=de21b640057f641d98b85ca4bc45c42ff463be5e;hb=914f190a3d33214d43d0d1014e65ae022460299b;hpb=329bded18811e816d1f4cddfa5ecd8cb82acc41a diff --git a/elpher.el b/elpher.el index de21b64..c6ee0b4 100644 --- a/elpher.el +++ b/elpher.el @@ -4,7 +4,7 @@ ;; Author: Tim Vaughan ;; Created: 11 April 2019 -;; Version: 2.9.1 +;; Version: 2.10.2 ;; Keywords: comm gopher ;; Homepage: http://thelambdalab.xyz/elpher ;; Package-Requires: ((emacs "26.2")) @@ -71,7 +71,7 @@ ;;; Global constants ;; -(defconst elpher-version "2.9.1" +(defconst elpher-version "2.10.2" "Current version of elpher.") (defconst elpher-margin-width 6 @@ -209,7 +209,7 @@ some servers which do not support IPv6 can take a long time to time-out." "Face used for html type directory records.") (defface elpher-gemini - '((t :inherit font-lock-regexp-grouping-backslash)) + '((t :inherit font-lock-constant-face)) "Face used for Gemini type directory records.") (defface elpher-other-url @@ -478,7 +478,7 @@ unless NO-HISTORY is non-nil." (if previous-page (elpher-visit-page previous-page nil t) (error "No previous page")))) - + (defun elpher-reload-current-page () "Reload the current page, discarding any existing cached content." (elpher-cache-content (elpher-page-address elpher-current-page) nil) @@ -763,8 +763,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. @@ -785,15 +785,34 @@ 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'." + (unless (file-directory-p elpher-certificate-directory) + (make-directory 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) @@ -1034,7 +1053,7 @@ The response is rendered using the rendering function RENDERER." (elpher-get-gopher-response search-address renderer)) (if aborted (elpher-visit-previous-page)))))) - + ;; Raw server response rendering (defun elpher-render-raw (data &optional mime-type-string) @@ -1157,40 +1176,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)) @@ -1207,7 +1248,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) @@ -1319,23 +1359,26 @@ treatment that a separate function is warranted." 'help-echo #'elpher--page-button-help)) (insert (propertize display-string 'face 'elpher-unknown))) (insert "\n")))) - + (defun elpher-gemini-insert-header (header-line) "Insert header described by HEADER-LINE into a text/gemini document. The gemini map file line describing the header is given by HEADER-LINE." (when (string-match "^\\(#+\\)[ \t]*" header-line) - (let ((level (length (match-string 1 header-line))) - (header (substring header-line (match-end 0)))) + (let* ((level (length (match-string 1 header-line))) + (header (substring header-line (match-end 0))) + (face (pcase level + (1 'elpher-gemini-heading1) + (2 'elpher-gemini-heading2) + (3 'elpher-gemini-heading3) + (_ 'default))) + (fill-column (/ (* fill-column + (font-get (font-spec :name (face-font 'default)) :size)) + (font-get (font-spec :name (face-font face)) :size)))) (unless (display-graphic-p) (insert (make-string level ?#) " ")) - (insert (propertize header 'face - (pcase level - (1 'elpher-gemini-heading1) - (2 'elpher-gemini-heading2) - (3 'elpher-gemini-heading3) - (_ 'default))) - "\n")))) + (insert (propertize header 'face face)) + (newline)))) (defun elpher-gemini-insert-text (text-line) "Insert a plain non-preformatted TEXT-LINE into a text/gemini document. @@ -1349,10 +1392,7 @@ width defined by elpher-gemini-max-fill-width." (match-string 0 text-line)) (substring text-line (match-end 0))) text-line)) - (adaptive-fill-mode nil) - (fill-prefix (if (match-string 2 text-line) - (replace-regexp-in-string "[>\*]" " " (match-string 0 text-line)) - nil))) + (adaptive-fill-mode nil)) (insert (elpher-process-text-for-display processed-text-line)) (newline))) @@ -1549,7 +1589,7 @@ The result is rendered using RENDERER." 'help-echo help-string)) (insert "\n") (elpher-restore-pos))) - + ;;; Bookmarks ;; @@ -1559,7 +1599,7 @@ The result is rendered using RENDERER." DISPLAY-STRING determines how the bookmark will appear in the bookmark list, while URL is the url of the entry." (list display-string url)) - + (defun elpher-bookmark-display-string (bookmark) "Get the display string of BOOKMARK." (elt bookmark 0)) @@ -1845,7 +1885,7 @@ When run interactively HOST-OR-URL is read from the minibuffer." (if button (elpher-info-page (button-get button 'elpher-page)) (error "No item selected")))) - + (defun elpher-info-current () "Display information on current page." (interactive)