aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYoni Rabkin <yonirabkin@member.fsf.org>2014-03-10 10:48:15 -0400
committerYoni Rabkin <yonirabkin@member.fsf.org>2014-03-10 10:48:15 -0400
commitc578f5783972b7090014895af4e08a36d20d7c2b (patch)
tree5be82d6bee695299eda284a8696995654bbcdb7c
parentfc4275f6c6d31a73875f649f884bf1bcf04e3448 (diff)
* lisp/emms-librefm-client.el: Implement handshake
-rw-r--r--lisp/emms-librefm-client.el89
1 files changed, 77 insertions, 12 deletions
diff --git a/lisp/emms-librefm-client.el b/lisp/emms-librefm-client.el
index 19a92bc..56ee543 100644
--- a/lisp/emms-librefm-client.el
+++ b/lisp/emms-librefm-client.el
@@ -21,31 +21,50 @@
;; Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
;; MA 02110-1301, USA.
-;;; Commentary:
-;;
-;; Definitive information on how to setup and use this package is
-;; provided in the wonderful Emms manual, in the /doc directory of the
-;; Emms distribution.
;;; Code:
(defvar emms-librefm-client-handshake-url
- "turtle.libre.fm")
+ "turtle.libre.fm"
+ "Endpoint for client handshake.")
(defvar emms-librefm-client-method
- "http")
+ "http"
+ "Transfer method.")
(defvar emms-librefm-client-username
- "")
+ ""
+ "Libre.fm username.")
(defvar emms-librefm-client-password
- "")
+ ""
+ "Libre.fm user password.")
(defvar emms-librefm-client-debug
- "")
+ ""
+ "Debugging variable to store communication.")
+
+(defvar emms-librefm-client-session-id
+ ""
+ "Session ID for Libre.fm.")
+
+(defvar emms-librefm-client-now-playing-url
+ ""
+ "URL for getting the track playing.")
+
+(defvar emms-librefm-client-submission-url
+ ""
+ "URL for submissions.")
(defun emms-librefm-client-handshake-string (url username password)
+ "Return the client handshake string."
+ (when (= 0 (length url))
+ (error "bad url"))
+ (when (= 0 (length username))
+ (error "bad username"))
+ (when (= 0 (length password))
+ (error "bad password"))
(let ((timestamp (format-time-string "%s")))
(concat emms-librefm-client-method
"://"
@@ -58,7 +77,8 @@
"t=" timestamp "&"
"a=" (md5 (concat (md5 password) timestamp)))))
-(defun emms-librefm-client-handshake (url username password)
+(defun emms-librefm-client-handshake-call (url username password)
+ "Perform client handshake and return a response in a buffer."
(let ((url-request-method "POST"))
(let ((response
(url-retrieve-synchronously
@@ -67,7 +87,52 @@
(setq emms-librefm-client-debug
(with-current-buffer response
(buffer-substring-no-properties (point-min)
- (point-max)))))))
+ (point-max))))
+ response)))
+
+(defun emms-librefm-client-handle-handshake-response (resbuf)
+ "Handle the client handshake server response."
+ (when (not (bufferp resbuf))
+ (error "response not a buffer"))
+ (with-current-buffer resbuf
+ (goto-char (point-min))
+ (when (not (re-search-forward "^.*200 OK$" (point-at-eol) t))
+ (error "bad HTTP server response"))
+ ;; go to the start of the FM response
+ (when (not (re-search-forward "\n\n" (point-max) t))
+ (error "bad FM server response"))
+ (let ((status (buffer-substring (point-at-bol)
+ (point-at-eol))))
+ (when (not (string= status "OK"))
+ (error "FM server returned: %s" status))
+ (let (session-id
+ now-playing-url
+ submission-url)
+ (forward-line 1)
+ (setq session-id (buffer-substring (point-at-bol)
+ (point-at-eol)))
+ (forward-line 1)
+ (setq now-playing-url (buffer-substring (point-at-bol)
+ (point-at-eol)))
+ (forward-line 1)
+ (setq submission-url (buffer-substring (point-at-bol)
+ (point-at-eol)))
+ (when (or (= 0 (length session-id))
+ (= 0 (length now-playing-url))
+ (= 0 (length submission-url)))
+ (error "couldn't parse FM server response"))
+ (setq emms-librefm-client-session-id session-id
+ emms-librefm-client-now-playing-url now-playing-url
+ emms-librefm-client-submission-url submission-url)
+ (message "handshake successful")))))
+
+(defun emms-librefm-client-handshake ()
+ "Perform handshake call and handle response."
+ (emms-librefm-client-handle-handshake-response
+ (emms-librefm-client-handshake-call
+ emms-librefm-client-handshake-url
+ emms-librefm-client-username
+ emms-librefm-client-password)))
(provide 'emms-librefm-client)