unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Leon Vack <dev@lgcl.de>
To: Lars Ingebrigtsen <larsi@gnus.org>
Cc: 45340@debbugs.gnu.org, Amin Bandali <bandali@gnu.org>
Subject: bug#45340: erc-services.el: Auth-source support for passwords
Date: Mon, 11 Jan 2021 14:38:02 GMT	[thread overview]
Message-ID: <87wnwjk08m.fsf@sol.hw.lgcl.de> (raw)
In-Reply-To: <87eeitvwnu.fsf@gnus.org>

[-- Attachment #1: Type: text/plain, Size: 630 bytes --]


Lars Ingebrigtsen <larsi@gnus.org> writes:

> Leon Vack <dev@lgcl.de> writes:
>
>> Thanks for the pointers. I attached a new patch with all of them addressed.
>
> This leads to the following warning:
>
> In erc-nickserv-call-identify-function:
> erc/erc-services.el:454:11: Warning: reference to free variable `password'
>
> And the code in that function doesn't look correct to me...

Yes, that is a mistake on my part, it must have slipped through from an
earlier implementation I wrote and used. Sorry about that.

I have fixed that in the attached patch and done some testing (without
changing anything after it this time).


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Support-using-auth-source-for-NickServ-passwords-in-.patch --]
[-- Type: text/x-patch, Size: 6315 bytes --]

From 02d6ddfe3b07a793855dd87ea0d9ba4daee8af3f Mon Sep 17 00:00:00 2001
From: Leon Vack <dev@lgcl.de>
Date: Sun, 20 Dec 2020 10:53:33 +0100
Subject: [PATCH] Support using auth-source for NickServ passwords in ERC

* lisp/etc/erc-services.el (erc-nickserv-passwords): Document that
the passwords are only used when erc-prompt-for-nickserv-password
is nil.
* (erc-use-auth-source-for-nickserv-password): New customizable
variable to enable checking auth-source for NickServ passwords.
* (etc-nickserv-get-password): New function to handle the lookup
of the NickServ password from both auth-source and the
erc-nickserv-passwords variable.
* (erc-nickserv-call-identify-function): Use new
erc-nickserv-get-password function to lookup NickServ passwords.
* (erc-nickserv-identify-autodetect, erc-nickserv-identify-on-connect,
erc-nickserv-identify-on-nick-change): Call
erc-nickserv-call-identify-function when
erc-use-auth-source-for-nickserv-password is set.
* etc/NEWS: Document change.
---
 etc/NEWS                 |  9 ++++++
 lisp/erc/erc-services.el | 59 ++++++++++++++++++++++++++++++++--------
 2 files changed, 56 insertions(+), 12 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index 4a8e70e6a6..d722592fd3 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1283,6 +1283,15 @@ https://www.w3.org/TR/xml/#charsets).  Now it rejects such strings.
 
 ** erc
 
+*** erc-services.el now supports NickServ passwords from auth-source.
+The 'erc-use-auth-source-for-nickserv-password' enables querying
+auth-source for NickServ passwords. To enable this, add the following
+to your init file:
+
+    (setq erc-prompt-for-nickserv-password nil
+          erc-use-auth-source-for-nickserv-password t)
+
+
 ---
 *** The '/ignore' command will now ask for a timeout to stop ignoring the user.
 Allowed inputs are seconds or ISO8601-like periods like "1h" or "4h30m".
diff --git a/lisp/erc/erc-services.el b/lisp/erc/erc-services.el
index c0011f9808..60c7852edf 100644
--- a/lisp/erc/erc-services.el
+++ b/lisp/erc/erc-services.el
@@ -168,9 +168,23 @@ erc-prompt-for-nickserv-password
   :group 'erc-services
   :type 'boolean)
 
+(defcustom erc-use-auth-source-for-nickserv-password nil
+  "Query auth-source for a password when identifiying to NickServ.
+
+
+This option has an no effect if `erc-prompt-for-nickserv-password'
+is not nil, and passwords from `erc-nickserv-passwords' take
+precedence."
+  :version "28.1"
+  :group 'erc-services
+  :type 'boolean)
+
 (defcustom erc-nickserv-passwords nil
   "Passwords used when identifying to NickServ automatically.
 
+`erc-prompt-for-nickserv-password' must be nil for these
+passwords to be used.
+
 Example of use:
   (setq erc-nickserv-passwords
         \\='((freenode ((\"nick-one\" . \"password\")
@@ -375,7 +389,8 @@ erc-nickserv-identify-autodetect
 If `erc-prompt-for-nickserv-password' is non-nil, prompt the user for the
 password for this nickname, otherwise try to send it automatically."
   (unless (and (null erc-nickserv-passwords)
-	       (null erc-prompt-for-nickserv-password))
+               (null erc-prompt-for-nickserv-password)
+               (null erc-use-auth-source-for-nickserv-password))
     (let* ((network (erc-network))
 	   (sender (erc-nickserv-alist-sender network))
 	   (identify-regex (erc-nickserv-alist-regexp network))
@@ -394,30 +409,49 @@ erc-nickserv-identify-autodetect
 (defun erc-nickserv-identify-on-connect (_server nick)
   "Identify to Nickserv after the connection to the server is established."
   (unless (or (and (null erc-nickserv-passwords)
-		   (null erc-prompt-for-nickserv-password))
-	      (and (eq erc-nickserv-identify-mode 'both)
-		   (erc-nickserv-alist-regexp (erc-network))))
+                   (null erc-prompt-for-nickserv-password)
+                   (null erc-use-auth-source-for-nickserv-password))
+              (and (eq erc-nickserv-identify-mode 'both)
+                   (erc-nickserv-alist-regexp (erc-network))))
     (erc-nickserv-call-identify-function nick)))
 
 (defun erc-nickserv-identify-on-nick-change (nick _old-nick)
   "Identify to Nickserv whenever your nick changes."
   (unless (or (and (null erc-nickserv-passwords)
-		   (null erc-prompt-for-nickserv-password))
-	      (and (eq erc-nickserv-identify-mode 'both)
-		   (erc-nickserv-alist-regexp (erc-network))))
+                   (null erc-prompt-for-nickserv-password)
+                   (null erc-use-auth-source-for-nickserv-password))
+              (and (eq erc-nickserv-identify-mode 'both)
+                   (erc-nickserv-alist-regexp (erc-network))))
     (erc-nickserv-call-identify-function nick)))
 
+(defun erc-nickserv-get-password (nickname)
+  "Return the password for NICKNAME from configured sources.
+
+It uses `erc-nickserv-passwords' and additionally auth-source
+when `erc-use-auth-source-for-nickserv-password' is not nil."
+  (or
+   (when erc-nickserv-passwords
+     (cdr (assoc nickname
+                 (nth 1 (assoc (erc-network)
+                               erc-nickserv-passwords)))))
+   (when erc-use-auth-source-for-nickserv-password
+     (let* ((secret (nth 0 (auth-source-search
+                            :max 1 :require '(:secret)
+                            :host (erc-with-server-buffer erc-session-server)
+                            :port (format ; ensure we have a string
+                                   "%s" (erc-with-server-buffer erc-session-port))
+                            :user nickname))))
+       (when secret
+         (let ((passwd (plist-get secret :secret)))
+           (if (functionp passwd) (funcall passwd) passwd)))))))
+
 (defun erc-nickserv-call-identify-function (nickname)
   "Call `erc-nickserv-identify'.
 Either call it interactively or run it with NICKNAME's password,
 depending on the value of `erc-prompt-for-nickserv-password'."
   (if erc-prompt-for-nickserv-password
       (call-interactively 'erc-nickserv-identify)
-    (when erc-nickserv-passwords
-      (erc-nickserv-identify
-       (cdr (assoc nickname
-		   (nth 1 (assoc (erc-network)
-				 erc-nickserv-passwords))))))))
+    (erc-nickserv-identify (erc-nickserv-get-password nickname))))
 
 (defvar erc-auto-discard-away)
 
@@ -451,6 +485,7 @@ erc-nickserv-identify
 
 (provide 'erc-services)
 
+
 ;;; erc-services.el ends here
 ;;
 ;; Local Variables:
-- 
2.28.0


  reply	other threads:[~2021-01-11 14:38 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-20 10:54 bug#45340: erc-services.el: Auth-source support for passwords Leon Vack
2020-12-21  4:46 ` Lars Ingebrigtsen
2020-12-21  5:44   ` Leon Vack
2020-12-29  0:12 ` Amin Bandali
2020-12-29  8:24   ` Leon Vack
2021-01-09  7:22     ` Leon Vack
2021-01-10 11:49     ` Lars Ingebrigtsen
2021-01-11 14:38       ` Leon Vack [this message]
2021-01-11 14:51         ` Lars Ingebrigtsen
2021-01-11 16:04           ` Leon Vack
2021-01-11 16:06             ` Lars Ingebrigtsen
2021-01-15  9:46               ` Olivier Certner
2021-01-15 11:10                 ` Leon Vack
2021-01-15 12:41                   ` Olivier Certner
2021-01-20 10:42                     ` Leon Vack
2021-01-13  8:27 ` bug#45340: foo Richard Copley
2021-01-15 21:48   ` Basil L. Contovounesios

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87wnwjk08m.fsf@sol.hw.lgcl.de \
    --to=dev@lgcl.de \
    --cc=45340@debbugs.gnu.org \
    --cc=bandali@gnu.org \
    --cc=larsi@gnus.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).