X-Git-Url: http://git.chise.org/gitweb/?a=blobdiff_plain;f=mixi.el;h=8d52aa33ac43a575cffe7197d386185f112528d7;hb=1b6c1fb55da90b146674304d55e01f591c2a6bbf;hp=af0e62c9934ee7487a8326e4b97fd2b3ca98aa2d;hpb=dfeabc1a37f520942fff4a9cb5e0a0f3ec725c43;p=elisp%2Fmixi.git
diff --git a/mixi.el b/mixi.el
index af0e62c..8d52aa3 100644
--- a/mixi.el
+++ b/mixi.el
@@ -33,12 +33,22 @@
;; * mixi-get-new-diaries
;; * mixi-search-diaries
;; * mixi-get-communities
+;; * mixi-search-communities
;; * mixi-get-bbses
;; * mixi-get-new-bbses
+;; * mixi-search-bbses
;; * mixi-get-comments
;; * mixi-get-new-comments
;; * mixi-get-messages
;; * mixi-get-introductions
+;; * mixi-get-news
+;;
+;; API for posting:
+;;
+;; * mixi-post-diary
+;; * mixi-post-topic
+;; * mixi-post-comment
+;; * mixi-post-message
;;
;; Utilities:
;;
@@ -109,6 +119,17 @@
(eval-when-compile (require 'cl))
+;; Functions and variables which should be defined in the other module
+;; at run-time.
+(eval-when-compile
+ (defvar w3m-use-cookies)
+ (defvar url-request-method)
+ (defvar url-request-data)
+ (defvar url-request-extra-headers)
+ (autoload 'w3m-decode-buffer "w3m")
+ (autoload 'w3m-retrieve "w3m")
+ (autoload 'url-retrieve-synchronously "url"))
+
(defgroup mixi nil
"API library for accessing to mixi."
:group 'hypermedia)
@@ -118,6 +139,11 @@
:type 'string
:group 'mixi)
+(defcustom mixi-directory (expand-file-name "~/.mixi")
+ "*Where to look for mixi files."
+ :type 'directory
+ :group 'mixi)
+
(defcustom mixi-coding-system 'euc-jp
"*Coding system for mixi."
:type 'coding-system
@@ -128,32 +154,38 @@
:type 'file
:group 'mixi)
-(defcustom mixi-curl-cookie-file (expand-file-name "~/.mixi-cookies.txt")
+(defcustom mixi-curl-cookie-file (expand-file-name "cookies.txt"
+ mixi-directory)
"*The location of cookie file created by `curl'."
:type 'file
:group 'mixi)
-(defcustom mixi-retrieve-function
+(defcustom mixi-backend
(or (condition-case nil
(progn
- (require 'url)
- (if (fboundp 'url-retrieve-synchronously)
- 'mixi-url-retrieve))
+ (require 'w3m)
+ 'w3m)
(error))
(condition-case nil
(progn
- (require 'w3m)
- 'mixi-w3m-retrieve)
+ (require 'url)
+ (if (fboundp 'url-retrieve-synchronously)
+ 'url))
(error))
(if (and (fboundp 'executable-find)
(executable-find mixi-curl-program))
- 'mixi-curl-retrieve)
- (error "Can't set `mixi-retrieve-function'"))
- "*The function for retrieving."
- :type '(radio (const :tag "Using url" mixi-url-retrieve)
- (const :tag "Using w3m" mixi-w3m-retrieve)
- (const :tag "Using curl" mixi-curl-retrieve)
- (function :format "Other function: %v\n" :size 0))
+ 'curl)
+ (error "Cannot set `mixi-backend'."))
+ "*The backend for accessing to mixi."
+ :type '(radio (const :tag "Use emacs-w3m" w3m)
+ (const :tag "Use url.el" url)
+ (const :tag "Use curl" curl)
+ (symbol :tag "The other backend"))
+ :group 'mixi)
+
+(defcustom mixi-login-use-ssl nil
+ "*If non-ni, login using SSL."
+ :type 'boolean
:group 'mixi)
(defcustom mixi-default-email nil
@@ -179,7 +211,7 @@ Increase this value when unexpected error frequently occurs."
:type 'number
:group 'mixi)
-(defcustom mixi-cache-expires 3600
+(defcustom mixi-cache-expires nil
"*Seconds for expiration of a cached object."
:type '(radio (integer :tag "Expired seconds")
(const :tag "Don't expire" nil)
@@ -192,16 +224,34 @@ Increase this value when unexpected error frequently occurs."
:type 'boolean
:group 'mixi)
-(defcustom mixi-cache-directory (expand-file-name "~/.mixi")
- "*Where to look for cache files."
- :type 'directory
- :group 'mixi)
-
(defvar mixi-me nil)
;; Utilities.
-(defmacro mixi-message (string)
- `(concat "[mixi] " ,string))
+(defmacro mixi-message (&rest strings)
+ `(concat "[mixi] " ,@strings))
+
+(put 'mixi-realization-error
+ 'error-message (mixi-message "Cannot realize object"))
+(put 'mixi-realization-error
+ 'error-conditions '(mixi-realization-error error))
+
+(put 'mixi-post-error
+ 'error-message (mixi-message "Cannot post"))
+(put 'mixi-post-error
+ 'error-conditions '(mixi-post-error error))
+
+(defmacro mixi-realization-error (type object)
+ `(let ((data (if debug-on-error
+ (list ,type ,object (buffer-string))
+ (list ,type ,object))))
+ (signal 'mixi-realization-error data)))
+
+(defmacro mixi-post-error (type &optional object)
+ `(let ((data (when debug-on-error (list (buffer-string)))))
+ (if ,object
+ (setq data (cons ,type (cons ,object data)))
+ (setq data (cons ,type data)))
+ (signal 'mixi-post-error data)))
(defconst mixi-message-adult-contents
"¤³¤Î¥Ú¡¼¥¸¤«¤éÀè¤Ï¥¢¥À¥ë¥È¡ÊÀ®¿Í¸þ¤±¡Ë¥³¥ó¥Æ¥ó¥Ä¤¬´Þ¤Þ¤ì¤Æ¤¤¤Þ¤¹¡£
@@ -216,7 +266,12 @@ Increase this value when unexpected error frequently occurs."
¤»¤ó¤¬¡¢¤·¤Ð¤é¤¯¤Î´Ö¤ªÂÔ¤Á¤¯¤À¤µ¤¤¡£")
(defmacro mixi-retrieve (url &optional post-data)
- `(funcall mixi-retrieve-function ,url ,post-data))
+ `(funcall (intern (concat "mixi-" (symbol-name mixi-backend) "-retrieve"))
+ ,url ,post-data))
+
+(defmacro mixi-post-form (url fields)
+ `(funcall (intern (concat "mixi-" (symbol-name mixi-backend) "-post-form"))
+ ,url ,fields))
(defun mixi-parse-buffer (url buffer &optional post-data)
(when (string-match mixi-message-adult-contents buffer)
@@ -224,47 +279,60 @@ Increase this value when unexpected error frequently occurs."
(setq buffer (mixi-retrieve url "submit=agree"))
(setq buffer (mixi-retrieve (concat url "?")))))
(when (string-match mixi-warning-continuously-accessing buffer)
- (error (mixi-message "Access denied. Please wait a while.")))
+ (error (mixi-message "Access denied. Please wait a while and increase "
+ "the value of `mixi-continuously-access-interval'.")))
(if (not (string-match mixi-message-continuously-accessing buffer))
buffer
(message (mixi-message "Waiting for continuously accessing..."))
- (sit-for mixi-continuously-access-interval)
+ (sleep-for mixi-continuously-access-interval)
(mixi-retrieve url post-data)))
(defmacro mixi-expand-url (url)
- `(if (string-match (concat "^" mixi-url) ,url)
+ `(if (string-match "^http" ,url)
,url
(concat mixi-url ,url)))
-(defun mixi-url-retrieve (url &optional post-data)
+;; FIXME: Support file, checkbox and so on.
+(defun mixi-make-form-data (fields)
+ "Make form data and return (CONTENT-TYPE . FORM-DATA)."
+ (let* ((boundary (apply 'format "--_%d_%d_%d" (current-time)))
+ (content-type (concat "multipart/form-data; boundary=" boundary))
+ (form-data
+ (mapconcat
+ (lambda (field)
+ (concat "--" boundary "\r\n"
+ "Content-Disposition: form-data; name=\""
+ (car field) "\"\r\n"
+ "\r\n"
+ (encode-coding-string (cdr field) mixi-coding-system)))
+ fields "\r\n")))
+ (cons content-type (concat form-data "\r\n--" boundary "--"))))
+
+(defun mixi-url-retrieve (url &optional post-data extra-headers)
"Retrieve the URL and return gotten strings."
- (if post-data
- (progn
- (setq url-request-method "POST")
- (setq url-request-data post-data))
- (setq url-request-method "GET")
- (setq url-request-data nil))
- (let* ((url (mixi-expand-url url))
+ (let* ((url-request-method (if post-data "POST" "GET"))
+ (url-request-data post-data)
+ (url-request-extra-headers extra-headers)
+ (url (mixi-expand-url url))
(buffer (url-retrieve-synchronously url))
ret)
(unless (bufferp buffer)
(error (mixi-message "Cannot retrieve")))
(with-current-buffer buffer
(goto-char (point-min))
- (if (re-search-forward "HTTP/[0-9.]+ 302 Moved" nil t)
- (if (re-search-forward
- (concat "Location: " mixi-url "\\(.+\\)") nil t)
- (setq ret (mixi-url-retrieve (match-string 1) post-data))
- (setq ret (mixi-url-retrieve "/home.pl" post-data)))
- (unless (re-search-forward "HTTP/[0-9.]+ 200 OK" nil t)
- (error (mixi-message "Cannot retrieve")))
- (search-forward "\n\n")
- (setq ret (decode-coding-string
- (buffer-substring-no-properties (point) (point-max))
- mixi-coding-system))
- (kill-buffer buffer)
- (setq ret (mixi-parse-buffer url ret post-data))))
- ret))
+ (while (looking-at "HTTP/[0-9]+\\.[0-9]+ [13][0-9][0-9]")
+ (delete-region (point) (re-search-forward "\r?\n\r?\n")))
+ (unless (looking-at "HTTP/[0-9]+\\.[0-9]+ 200")
+ (error (mixi-message "Cannot retrieve")))
+ (delete-region (point) (re-search-forward "\r?\n\r?\n"))
+ (setq ret (decode-coding-string (buffer-string) mixi-coding-system))
+ (kill-buffer buffer)
+ (mixi-parse-buffer url ret post-data))))
+
+(defun mixi-url-post-form (url fields)
+ (let* ((form-data (mixi-make-form-data fields))
+ (extra-headers `(("Content-Type" . ,(car form-data)))))
+ (mixi-url-retrieve url (cdr form-data) extra-headers)))
(defun mixi-w3m-retrieve (url &optional post-data)
"Retrieve the URL and return gotten strings."
@@ -276,6 +344,10 @@ Increase this value when unexpected error frequently occurs."
(let ((ret (buffer-substring-no-properties (point-min) (point-max))))
(mixi-parse-buffer url ret post-data))))))
+(defun mixi-w3m-post-form (url fields)
+ (let ((form-data (mixi-make-form-data fields)))
+ (mixi-w3m-retrieve url form-data)))
+
(defun mixi-curl-retrieve (url &optional post-data)
"Retrieve the URL and return gotten strings."
(with-temp-buffer
@@ -317,61 +389,83 @@ Increase this value when unexpected error frequently occurs."
(defun mixi-login (&optional email password)
"Login to mixi."
- (when (and (eq mixi-retrieve-function 'mixi-w3m-retrieve)
- (not w3m-use-cookies))
- (error
- (mixi-message
- "Require to accept cookies. Please set `w3m-use-cookies' to t.")))
+ (when (and (eq mixi-backend 'w3m) (not w3m-use-cookies))
+ (error (mixi-message "Require to accept cookies. Please set "
+ "`w3m-use-cookies' to t.")))
(let ((email (or email mixi-default-email
(read-from-minibuffer (mixi-message "Login Email: "))))
(password (or password mixi-default-password
(read-passwd (mixi-message "Login Password: ")))))
- (let ((buffer (mixi-retrieve "/login.pl"
- (concat "email=" email
- "&password=" password
- "&next_url=/home.pl"
- "&sticky=on"))))
- (unless (string-match "url=/check\\.pl\\?n=" buffer)
- (error (mixi-message "Cannot login")))
- (setq buffer (mixi-retrieve "/check.pl?n=home.pl"))
- (if (string-match mixi-my-id-regexp buffer)
- (setq mixi-me (mixi-make-friend (match-string 1 buffer)))
- (error (mixi-message "Cannot login"))))))
+ (let ((url "/login.pl"))
+ (when mixi-login-use-ssl
+ (setq url (concat "https://mixi.jp" url)))
+ (let ((buffer (mixi-retrieve url
+ (concat "email=" email
+ "&password=" password
+ "&next_url=/home.pl"
+ "&sticky=on"))))
+ (unless (string-match "url=/check\\.pl\\?n=" buffer)
+ (error (mixi-message "Cannot login")))
+ (setq buffer (mixi-retrieve "/check.pl?n=home.pl"))
+ (if (string-match mixi-my-id-regexp buffer)
+ (setq mixi-me (mixi-make-friend (match-string 1 buffer)))
+ (error (mixi-message "Cannot login")))))))
(defun mixi-logout ()
(mixi-retrieve "/logout.pl"))
(defmacro with-mixi-retrieve (url &rest body)
- `(let (buffer)
- (when ,url
- (setq buffer (mixi-retrieve ,url))
- (when (string-match "login.pl" buffer)
+ `(when ,url
+ (with-temp-buffer
+ (insert (mixi-retrieve ,url))
+ (goto-char (point-min))
+ (when (search-forward
+ "