Hi,

I have attached a draft of way getting authenticating info using
authinfo rather than storing the password in plain text.  I dislike
saving passwords in my init file, as I may share it with other people.

This is the first time I work with auth-source.el so let me know about
any bug.  It seemed to work on my computer.  The main missing feature
is that it does not prompt the user for username and password, if
missing.  I guess this could be relatively easy using :create
and :saving-function.  I.e. it would do a handshake and save it the
provided credentials are good.

Let me know what you think.
—Rasmus

-- 
And I faced endless streams of vendor-approved Ikea furniture. . .
>From a5f70832e7654d0ae52b6c0dce6bf506989ee045 Mon Sep 17 00:00:00 2001
From: Rasmus <[email protected]>
Date: Tue, 29 Jul 2014 12:59:38 +0200
Subject: [PATCH] emms-librefm-scrobbler.el: use authinfo

* emms-librefm-scrobbler.el (emms-librefm-scrobbler-password): default to nil.
(emms-librefm-scrobbler--get-auth-detail, emms-librefm-scrobbler--password, emms-librefm-scrobbler--username): new defun.
(emms-librefm-scrobbler-handshake): updated to use
`emms-librefm-scrobbler--username' and
'emms-librefm-scrobbler--password'.

* emms.texinfo (GNU FM): Update documentation.
---
 doc/emms.texinfo               | 14 +++++++--
 lisp/emms-librefm-scrobbler.el | 64 ++++++++++++++++++++++++++++++++++++------
 2 files changed, 67 insertions(+), 11 deletions(-)

diff --git a/doc/emms.texinfo b/doc/emms.texinfo
index a0a8fe6..592662a 100644
--- a/doc/emms.texinfo
+++ b/doc/emms.texinfo
@@ -1239,15 +1239,23 @@ but can work with any GNU FM server by configuring the variable
 @var{emms-librefm-scrobbler-handshake-url} to the URL of the GNU FM
 server.
 
-Provide your credentials on the GNU FM server by setting the
-appropriate variables:
+The recommended way of providing your credentials to the GNU FM server
+is by using an authinfo file.  Add authentication to your auth-info
+file, typically @file{~/.authinfo.gpg} as:
+@example
+machine SERVER login USERNAME password PASSWORD
+@end example
+where SERVER should match @var{emms-librefm-scrobbler-handshake-url} or
+@code{libre.fm}.
+
+Alternatively, you can save the password in plaintext in your init-file
+by setting these variables:
 
 @lisp
 (setq emms-librefm-scrobbler-username "USERNAME"
       emms-librefm-scrobbler-password "PASSWORD")
 @end lisp
 
-
 @menu
 * Uploading Track Information:: How to submit listened track information.
 * GNU FM Streaming:: Streaming music from a GNU FM server.
diff --git a/lisp/emms-librefm-scrobbler.el b/lisp/emms-librefm-scrobbler.el
index 4f83cdd..182c26c 100644
--- a/lisp/emms-librefm-scrobbler.el
+++ b/lisp/emms-librefm-scrobbler.el
@@ -35,13 +35,25 @@
   "http"
   "Transfer method.")
 
-(defvar emms-librefm-scrobbler-username
-  ""
-  "Libre.fm username.")
+(defvar emms-librefm-scrobbler-username nil
+  "Libre.fm username.
 
-(defvar emms-librefm-scrobbler-password
-  ""
-  "Libre.fm user password.")
+Note that the preferred way of authenticating is using authinfo
+and only setting `emms-librefm-scrobbler-handshake-url'.  Too to
+do so add the following line to ~/.authinfo.gpg (or your
+preferred netrc file)
+
+    machine SERVER login USER password PASSWORD
+
+where SERVER matches  `emms-librefm-scrobbler-handshake-url'.
+See also `auth-sources'.
+")
+
+(defvar emms-librefm-scrobbler-password nil
+  "Libre.fm user password.
+
+Note that the preferred way of authenticating is using authinfo.
+See also `emms-librefm-scrobbler-username'.")
 
 (defvar emms-librefm-scrobbler-debug
   ""
@@ -69,6 +81,42 @@
 
 
 ;;; ------------------------------------------------------------------
+;;; authenticate
+;;; ------------------------------------------------------------------
+(defun emms-librefm-scrobbler--get-auth-detail (token)
+  (require 'auth-source)
+  (plist-get
+   (car-safe
+    ;; TODO: It would be convenient to use :create t here it would
+    ;; require a save-function that checks that the user-password
+    ;; combination is OK
+    (remq nil (mapcar
+               (lambda (URL)
+                 (car-safe
+                  (auth-source-search :host URL
+                                      :user (when (and
+                                                   (characterp emms-librefm-scrobbler-username)
+                                                   (not (equal emms-librefm-scrobbler-username "")))
+                                              emms-librefm-scrobbler-username)
+                                      :type 'netrc :max 1 :require '(:user :secret))))
+               `(,emms-librefm-scrobbler-handshake-url "libre.fm"))))
+   token))
+
+
+(defun emms-librefm-scrobbler--username ()
+  (if (and (characterp emms-librefm-scrobbler-username)
+           (not (equal emms-librefm-scrobbler-username "")))
+      emms-librefm-scrobbler-username
+    (emms-librefm-scrobbler--get-auth-detail :user)))
+
+(defun emms-librefm-scrobbler--password ()
+  (if (and (characterp emms-librefm-scrobbler-password)
+           (not (equal emms-librefm-scrobbler-password "")))
+      emms-librefm-scrobbler-password
+    (let  ((token (emms-librefm-scrobbler--get-auth-detail :secret)))
+      (cond ((functionp token) (funcall token)) ((characterp token) token)))))
+
+;;; ------------------------------------------------------------------
 ;;; handshake
 ;;; ------------------------------------------------------------------
 
@@ -146,8 +194,8 @@
   (emms-librefm-scrobbler-handle-handshake-response
    (emms-librefm-scrobbler-handshake-call
     emms-librefm-scrobbler-handshake-url
-    emms-librefm-scrobbler-username
-    emms-librefm-scrobbler-password)))
+    (emms-librefm-scrobbler--username)
+    (emms-librefm-scrobbler--password))))
 
 
 ;;; ------------------------------------------------------------------
-- 
2.0.3

_______________________________________________
Emms-help mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/emms-help

Reply via email to