From e975fa032d5b9f3b5a221907bffe598105aa64ae Mon Sep 17 00:00:00 2001 From: Ashley Duckworth Date: Thu, 14 Jan 2021 16:55:52 -0600 Subject: [PATCH] Enable gemini-write to use the auth-source library --- README.md | 8 ++++++++ gemini-write.el | 27 +++++++++++++++++++++++++-- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0751cb8..d9e141d 100644 --- a/README.md +++ b/README.md @@ -33,3 +33,11 @@ of how to set it all up: ;; make sure "e" can be used to edit raw pages (eval-after-load "elpher" '(load-library "gemini-write")) ``` + +## Tokens + +`gemini-write` can pull tokens from the `elpher-gemini-tokens` alist, or from the `auth-source` library. A gemini-write token entry in ~/.authsource should contain the host and the port, which by default is 1965, along with the token in the `password` field, like this: + +``` +machine example.com port 1965 password example-password +``` diff --git a/gemini-write.el b/gemini-write.el index 05f00e7..562979c 100644 --- a/gemini-write.el +++ b/gemini-write.el @@ -39,6 +39,7 @@ (require 'elpher) (require 'gemini-mode) +(require 'auth-source) ;;; gemini-write support @@ -109,6 +110,11 @@ used when writing Gemini pages." :type '(alist :key-type (string :tag "Host") :value-type (string :tag "Token")) :group 'gemini-mode) +(defcustom gemini-write-use-auth-source t + "Enable password fetching from `auth-source', as well as from `elpher-gemini-tokens'." + :type 'boolean + :group 'gemini-mode) + (defun get-elpher-buffer-showing (page) "Return the first Elpher buffer showing PAGE." (catch 'buf @@ -122,6 +128,23 @@ used when writing Gemini pages." address))) (throw 'buf buf))))))) +(defun gemini-write-get-token (host &optional port) + "Get a token from `elpher-gemini-tokens', or `auth-sources' if `gemini-write-use-auth-source' is enabled." + (if-let (token (cdr (assoc host elpher-gemini-tokens))) + token + (if gemini-write-use-auth-source + (let ((info (nth 0 (auth-source-search + :host host + :port (or port 1965) + :require '(:secret))))) + (if info + (let ((secret (plist-get info :secret))) + (if (functionp secret) + (funcall secret) + secret)) + nil)) + nil))) + (defun gemini-write () "Save the current Gemini buffer. This will be saved to `elpher-current-page'. If there's an Elpher @@ -137,7 +160,7 @@ going to use. Otherwise, a new buffer is used." (generate-new-buffer (default-value 'elpher-buffer-name)) (elpher-mode) (current-buffer)))) - (token (cdr (assoc (url-host address) elpher-gemini-tokens))) + (token (gemini-write-get-token (url-host address))) (data (encode-coding-string (buffer-string) 'utf-8 t))) (switch-to-buffer buf) (setq-local elpher-current-page page) @@ -164,7 +187,7 @@ going to use. Otherwise, a new buffer is used." (elpher-mode) (current-buffer))) (address (elpher-address-from-url url)) - (token (cdr (assoc (url-host address) elpher-gemini-tokens))) + (token (gemini-write-get-token (url-host address))) (mime-type (completing-read "MIME type: " (mailcap-mime-types) nil t (mailcap-extension-to-mime (file-name-extension file t))))