;;; auth-get.el ---
;;
;; Filename: auth-get.el
;; Description:
;; Author: Christian Giménez with help from Tiago Charters de Azevedo.
;; Maintainer:
;; Created: jue ene 12 14:53:35 2012 (-0300)
;; Version:
;; Last-Updated:
;; By:
;; Update #: 0
;; URL:
;; Keywords:
;; Compatibility:
;;
;; Features that might be required by this library:
;;
;; None
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Commentary:
;;
;;
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Change Log:
;;
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 3, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Code:
(require 'url)
(require 'json)
(defun d-get-auth-token (url)
"Find the first authenticity token from the given URL.
This looks for the tag in the HTML page that correspond to the name \"csrf-token\".
The auth-token will be saved in d-auth-token as well."
(let (
(url-request-extra-headers
'(("Content-Type" . "application/x-www-form-urlencoded")
("Accept-Language" . "en")
("Accept-Charset" . "UTF-8"))
)
)
(url-retrieve url 'd-find-auth-token)
)
)
(defun d-find-auth-token (status)
"Just look for the authenticity token in the buffer.
This is used as a \"callback\" function for `url-retrieve'."
;; DEBUGING PURPOSE ONLY:
;;(switch-to-buffer (current-buffer))
;; Look for the first Auth-token
(save-excursion
(goto-char (point-min))
(search-forward-regexp "")
(setq d-auth-token (match-string-no-properties 1))
)
;;(kill-buffer (current-buffer))
d-auth-token
)
(defvar d-pass nil
"Diaspora temporal Password")
(defvar d-user nil
"Diaspora temporal User"
)
(defvar d-auth-token nil
"Diaspora temporal authenticity token"
)
(defun d-send-login ()
"Send login information.
First call `d-get-login-page' for getting the authenticity token(`d-auth-token')."
(interactive)
(let (
(url-request-method "POST")
(url-request-extra-headers
'(("Content-Type" . "application/x-www-form-urlencoded")
("Accept-Language" . "en")
("Accept-Charset" . "UTF-8")
))
(url-request-data
(mapconcat (lambda (arg)
(concat (url-hexify-string (car arg)) "=" (url-hexify-string (cdr arg))))
(list (cons "user[username]" d-user)
(cons "user[password]" d-pass)
(cons "user[remember_me]" "1")
(cons "commit" "Entrar")
(cons "authenticity_token" d-auth-token)
)
"&"))
)
(url-retrieve d-login-page-url 'd-show-stream)
)
)
(defun d-show-stream (status &optional new-buffer-name)
"Show what was recieved in a new buffer.
If new-buffer-name is given then, the new buffer will have that name,
if not, the buffer called \"Diáspora Stream\" will be re-used or created if needed."
;; new-buffer-name has been given? if not, use "Diáspora Stream" as name.
(unless new-buffer-name
(setq new-buffer-name "Diáspora Stream")
)
(let ((buffer (get-buffer-create new-buffer-name))
(text (buffer-string))
(buf-kill (current-buffer))
)
;; copy text and switch
(switch-to-buffer buffer)
(insert text)
;; kill the http buffer
(kill-buffer buf-kill)
)
)
(defvar d-login-page-url "https://joindiaspora.com/users/sign_in"
"Diáspora Login page"
)
(defun d-get-login-page ()
"Get the Diáspora login page for getting the auth-token necessary."
(d-get-auth-token d-login-page-url)
(message "Getting authenticity token... done.")
(message d-auth-token)
)
(defun d-make-login (&optional user pass)
(interactive)
(if user
(setq d-user user) ;; Parameter user exists
(setq d-user (read-string "User?"))
)
(if pass
(setq d-pass pass) ;; Parameter pass exists
(setq d-pass (read-passwd "Password?"))
)
;; We need the auth-token and cookies...
(d-get-login-page)
;;(d-send-login)
)
(defun d-get-url (url buffer-name)
"Get the Diáspora URL and leave it in a new buffer."
(let (
(url-request-extra-headers
'(("Content-Type" . "application/x-www-form-urlencoded")
("Accept-Language" . "en")
("Accept-Charset" . "UTF-8")
))
)
(url-retrieve url 'd-show-stream (cons buffer-name nil))
)
)
(defvar d-entry-stream-url "http://joindiaspora.com/stream.json"
"JSON version of the entry stream(the main stream).")
(defun d-get-url-entry-stream (url)
"Get the Diáspora URL and leave it in a new buffer."
(let (
(url-request-extra-headers
'(("Content-Type" . "application/x-www-form-urlencoded")
("Accept-Language" . "en")
("Accept-Charset" . "UTF-8")
))
)
(url-retrieve-synchronously url)
)
)
(defun d-get-entry-stream ()
"Show the entry stream.
First look for the JSON file at `d-entry-stream-url' and then parse it.
I expect to be already logged in. For that you can use `d-make-login'."
(interactive)
(let (
(buff (d-get-url-entry-stream d-entry-stream-url))
)
(with-current-buffer buff
;; Delete the HTTP header...
(goto-char (point-min))
(search-forward "\n\n")
(delete-region (point-min) (match-beginning 0))
;; Parse JSON...
(d-parse-json)
)
;; Delete HTTP Buffer
(kill-buffer buff)
)
)
(defun d-show-message (parsed-message &optional buffer)
"Show a parsed message in a given buffer."
(with-current-buffer buffer
(let (
(name (cdr (assoc 'name (assoc 'author parsed-message))))
(diaspora_id (cdr (assoc 'diaspora_id (assoc 'author parsed-message))))
(text (cdr (assoc 'text parsed-message)))
(date (cdr (assoc 'created_at parsed-message)))
(amount-comments (cdr (assoc 'comments_count parsed-message)))
(amount-likes (cdr (assoc 'likes_count parsed-message)))
;; We can look for more data, including the last 3 comments!
)
(insert (format "---\n%s(%s):\n%s\n\n" name diaspora_id text))
)
)
)
(defun d-parse-json (&optional status)
"Parse de JSON entry stream."
(goto-char (point-min))
(let (
(lstparsed (cdr (assoc 'posts (json-read))))
(buff (get-buffer-create "*Diáspora*"))
)
(switch-to-buffer buff)
(let (
(le (length lstparsed))
)
;; Show all elements
(dotimes (i le)
(d-show-message (aref lstparsed i) buff)
)
)
)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; auth-get.el ends here