summaryrefslogtreecommitdiff
path: root/lisp/erc
diff options
context:
space:
mode:
authorF. Jason Park <jp@neverwas.me>2022-11-23 21:31:19 -0800
committerAmin Bandali <bandali@gnu.org>2022-11-29 00:01:13 -0500
commit00de296d1b4f629fd828cdeff588bb4f742d9ffe (patch)
tree7b791016be2034d15c749ab0206957a164cbc389 /lisp/erc
parent83b9496a193c41ed7e41b36838727e234c81a2d1 (diff)
downloademacs-00de296d1b4f629fd828cdeff588bb4f742d9ffe.tar.gz
emacs-00de296d1b4f629fd828cdeff588bb4f742d9ffe.tar.bz2
emacs-00de296d1b4f629fd828cdeff588bb4f742d9ffe.zip
Simplify erc-sasl's auth-source API
* doc/misc/erc.texi: Revise descriptions in SASL chapter to reflect simplified auth-source options. * lisp/erc/erc-sasl.el (erc-sasl-password, erc-sasl-auth-source-function): Revise doc strings. (erc-sasl-auth-source-password-as-host): New function to serve as more useful choice for option `erc-sasl-auth-source-function'. (erc-sasl--read-password): Promote auth-source to pole position, above an explicit string and `:password'. * test/lisp/erc/erc-sasl-tests.el (erc-sasl--read-password--basic): Massage tests to conform to simplified `erc-sasl-password' API. (Bug#29108.)
Diffstat (limited to 'lisp/erc')
-rw-r--r--lisp/erc/erc-sasl.el77
1 files changed, 47 insertions, 30 deletions
diff --git a/lisp/erc/erc-sasl.el b/lisp/erc/erc-sasl.el
index 5ee7169de5f..5b2c93988af 100644
--- a/lisp/erc/erc-sasl.el
+++ b/lisp/erc/erc-sasl.el
@@ -77,15 +77,14 @@ version is used."
(defcustom erc-sasl-password :password
"Optional account password to send when authenticating.
-When the value is a string, ERC will use it unconditionally for
-most mechanisms. Likewise with `:password', except ERC will
-instead use the \"session password\" on file, which often
-originates from the entry-point commands `erc' or `erc-tls'.
-Otherwise, when `erc-sasl-auth-source-function' is a function,
-ERC will attempt an auth-source query, possibly using a non-nil
-symbol for the suggested `:host' parameter if set as this
-option's value or passed as an `:id' to `erc-tls'. Failing that,
-ERC will prompt for input.
+When `erc-sasl-auth-source-function' is a function, ERC will
+attempt an auth-source query and prompt for input if it fails.
+Otherwise, when the value is a nonempty string, ERC will use it
+unconditionally for most mechanisms. Likewise with `:password',
+except ERC will instead use the \"session password\" on file, if
+any, which often originates from the entry-point commands `erc'
+or `erc-tls'. As with auth-source, ERC will prompt for input as
+a fallback.
Note that, with `:password', ERC will forgo sending a traditional
server password via the IRC \"PASS\" command. Also, when
@@ -95,15 +94,18 @@ option should hold the file name of the key."
(defcustom erc-sasl-auth-source-function nil
"Function to query auth-source for an SASL password.
-Called with keyword params known to `auth-source-search', which
-includes `erc-sasl-user' for the `:user' field and
-`erc-sasl-password' for the `:host' field, when the latter option
-is a non-nil, non-keyword symbol. In return, ERC expects a
-string to send as the SASL password, or nil, to move on to the
-next approach, as described in the doc string for the option
-`erc-sasl-password'. See info node `(erc) Connecting' for
-details on ERC's auth-source integration."
- :type '(choice (function-item erc-auth-source-search)
+If provided, this function should expect to be called with any
+number of keyword params known to `auth-source-search', even
+though ERC itself only specifies `:user' paired with a
+\"resolved\" `erc-sasl-user' value. When calling this function,
+ERC binds all options defined in this library, such as
+`erc-sasl-password', to their values from entry-point invocation.
+In return, ERC expects a string to send as the SASL password, or
+nil, in which case, ERC will prompt the for input. See info
+node `(erc) Connecting' for details on ERC's auth-source
+integration."
+ :type '(choice (function-item erc-sasl-auth-source-password-as-host)
+ (function-item erc-auth-source-search)
(const nil)
function))
@@ -130,20 +132,35 @@ details on ERC's auth-source integration."
(:nick (erc-downcase (erc-current-nick)))
(v v)))
+(defun erc-sasl-auth-source-password-as-host (&rest plist)
+ "Call `erc-auth-source-search' with `erc-sasl-password' as `:host'.
+But only do so when it's a string or a non-nil symbol, unless
+that symbol is `:password', in which case, use a non-nil
+`erc-session-password' instead. Otherwise, just defer to
+`erc-auth-source-search' to pick a suitable `:host'. Expect
+PLIST to contain keyword params known to `auth-source-search'."
+ (when erc-sasl-password
+ (when-let ((host (if (eq :password erc-sasl-password)
+ (and (not (functionp erc-session-password))
+ erc-session-password)
+ erc-sasl-password)))
+ (setq plist `(,@plist :host ,(format "%s" host)))))
+ (apply #'erc-auth-source-search plist))
+
(defun erc-sasl--read-password (prompt)
"Return configured option or server password.
-PROMPT is passed to `read-passwd' if necessary."
- (if-let
- ((found (pcase (alist-get 'password erc-sasl--options)
- (:password erc-session-password)
- ((and (pred stringp) v) (unless (string-empty-p v) v))
- ((and (let fn (alist-get 'authfn erc-sasl--options))
- (guard fn) v
- (let host
- (or v (erc-networks--id-given erc-networks--id))))
- (apply fn
- :user (erc-sasl--get-user)
- (and host (list :host (symbol-name host))))))))
+If necessary, pass PROMPT to `read-passwd'."
+ (if-let ((found (pcase (alist-get 'password erc-sasl--options)
+ ((guard (alist-get 'authfn erc-sasl--options))
+ (let-alist erc-sasl--options
+ (let ((erc-sasl-user .user)
+ (erc-sasl-password .password)
+ (erc-sasl-mechanism .mechanism)
+ (erc-sasl-authzid .authzid)
+ (erc-sasl-auth-source-function .authfn))
+ (funcall .authfn :user (erc-sasl--get-user)))))
+ (:password erc-session-password)
+ ((and (pred stringp) v) (unless (string-empty-p v) v)))))
(copy-sequence (erc--unfun found))
(read-passwd prompt)))