Add comment.
[elisp/mixi.git] / mixi.el
diff --git a/mixi.el b/mixi.el
index 7e590d6..bcfcef8 100644 (file)
--- a/mixi.el
+++ b/mixi.el
@@ -36,6 +36,7 @@
 ;;  * mixi-get-new-topics
 ;;  * mixi-get-comments
 ;;  * mixi-get-new-comments
+;;  * mixi-get-messages
 
 ;; Example:
 ;;
@@ -61,7 +62,7 @@
 ;;   (setq buffer-read-only t)
 ;;   (goto-char (point-min)))
 ;;
-;; Display newest 3 diaries including all comments like a mail format.
+;; Display newest 3 diaries including newest 3 comments like a mail format.
 ;; Comments are displayed like a reply mail.
 ;;
 ;; (let ((max-numbers 3)
@@ -90,7 +91,7 @@
 ;;                           "Subject: " subject "\n"
 ;;                           "Date: " date "\n\n"
 ;;                           body "\n\n")))
-;;               (mixi-get-comments diary))))
+;;               (mixi-get-comments diary max-numbers))))
 ;;     (mixi-get-new-diaries max-numbers))
 ;;   (set-buffer-modified-p nil)
 ;;   (setq buffer-read-only t)
 ;;; Code:
 
 (condition-case nil
-    (require 'url "url")
+    (require 'url)
   (error))
 
 (condition-case nil
-    (require 'w3m "w3m")
+    (require 'w3m)
   (error))
 
 (eval-when-compile (require 'cl))
@@ -327,6 +328,7 @@ Increase this value when unexpected error frequently occurs."
         (setq buffer (funcall mixi-retrieve-function ,url))))
      ,@body))
 (put 'with-mixi-retrieve 'lisp-indent-function 'defun)
+(put 'with-mixi-retrieve 'edebug-form-spec '(form body))
 
 (defun mixi-get-matched-items (url max-numbers regexp)
   "Get matched items to REGEXP in URL."
@@ -337,12 +339,14 @@ Increase this value when unexpected error frequently occurs."
        (with-mixi-retrieve (format url page)
          (let ((pos 0))
            (while (and (string-match regexp buffer pos)
-                       (< (length ids) max-numbers))
+                       (or (null max-numbers) (< (length ids) max-numbers)))
              (let ((num 1)
                    list)
                (while (match-string num buffer)
                  (setq list (cons (match-string num buffer) list))
                  (incf num))
+               (when (member (reverse list) ids)
+                 (throw 'end ids))
                (setq ids (cons (reverse list) ids))
                (setq pos (match-end (1- num)))))
            (when (eq pos 0)
@@ -371,16 +375,32 @@ Increase this value when unexpected error frequently occurs."
     (buffer-string)))
 
 ;; Cache.
+;; stolen from time-date.el
+(defun mixi-time-less-p (t1 t2)
+  "Say whether time value T1 is less than time value T2."
+  (or (< (car t1) (car t2))
+      (and (= (car t1) (car t2))
+          (< (nth 1 t1) (nth 1 t2)))))
+
+(defun mixi-time-add (t1 t2)
+  "Add two time values.  One should represent a time difference."
+  (let ((low (+ (cdr t1) (cdr t2))))
+    (cons (+ (car t1) (car t2) (lsh low -16)) low)))
+
+;; stolen from time-date.el
+(defun mixi-seconds-to-time (seconds)
+  "Convert SECONDS (a floating point number) to a time value."
+  (cons (floor seconds 65536)
+       (floor (mod seconds 65536))))
 
 (defun mixi-cache-expired-p (object)
   "Whether a cache of OBJECT is expired."
-  ;; FIXME: Use method instead of `(aref (cdr object) 0)'.
-  (let ((timestamp (aref (cdr object) 0)))
+  (let ((timestamp (mixi-object-timestamp object)))
     (unless (or (null mixi-cache-expires)
                 (null timestamp))
-      (time-less-p (time-add timestamp
-                            (seconds-to-time mixi-cache-expires))
-                  (current-time)))))
+      (mixi-time-less-p
+       (mixi-time-add timestamp (mixi-seconds-to-time mixi-cache-expires))
+       (current-time)))))
 
 (defun mixi-make-cache (key value table)
   "Make a cache object and return it."
@@ -406,6 +426,13 @@ Increase this value when unexpected error frequently occurs."
   (let ((class (mixi-object-class object)))
     (substring (symbol-name class) (length mixi-object-prefix))))
 
+(defun mixi-object-timestamp (object)
+  "Return the timestamp of OJBECT."
+  (unless (mixi-object-p object)
+    (signal 'wrong-type-argument (list 'mixi-object-p object)))
+  (aref (cdr object) 0))
+(defalias 'mixi-object-realize-p 'mixi-object-timestamp)
+
 (defun mixi-object-id (object)
   "Return the id of OBJECT."
   (unless (mixi-object-p object)
@@ -414,6 +441,15 @@ Increase this value when unexpected error frequently occurs."
                              (mixi-object-name object) "-id"))))
     (funcall func object)))
 
+(defun mixi-object-set-timestamp (object timestamp)
+  "Set the timestamp of OBJECT."
+  (unless (mixi-object-p object)
+    (signal 'wrong-type-argument (list 'mixi-object-p object)))
+  (aset (cdr object) 0 timestamp))
+
+(defmacro mixi-object-touch (object)
+  `(mixi-object-set-timestamp ,object (current-time)))
+
 ;; Friend object.
 (defvar mixi-friend-cache (make-hash-table :test 'equal))
 (defun mixi-make-friend (id &optional nick)
@@ -462,7 +498,7 @@ Increase this value when unexpected error frequently occurs."
 (defun mixi-friend-realize (friend)
   "Realize a FRIEND."
   ;; FIXME: Check a expiration of cache?
-  (unless (mixi-friend-realize-p friend)
+  (unless (mixi-object-realize-p friend)
     (let (buf)
       (with-mixi-retrieve (mixi-friend-page friend)
        (setq buf buffer))
@@ -503,13 +539,7 @@ Increase this value when unexpected error frequently occurs."
       (when (string-match mixi-friend-profile-regexp buf)
        (mixi-friend-set-profile
         friend (mixi-remove-markup (match-string 1 buf)))))
-    (mixi-friend-touch friend)))
-
-(defun mixi-friend-realize-p (friend)
-  "Return the timestamp of FRIEND."
-  (unless (mixi-friend-p friend)
-    (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
-  (aref (cdr friend) 0))
+    (mixi-object-touch friend)))
 
 (defun mixi-friend-id (friend)
   "Return the id of FRIEND."
@@ -602,12 +632,6 @@ Increase this value when unexpected error frequently occurs."
   (mixi-friend-realize friend)
   (aref (cdr friend) 13))
 
-(defun mixi-friend-touch (friend)
-  "Set the timestamp of FRIEND."
-  (unless (mixi-friend-p friend)
-    (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
-  (aset (cdr friend) 0 (current-time)))
-
 (defun mixi-friend-set-nick (friend nick)
   "Set the nick of FRIEND."
   (unless (mixi-friend-p friend)
@@ -810,7 +834,7 @@ Increase this value when unexpected error frequently occurs."
 (defun mixi-diary-realize (diary)
   "Realize a DIARY."
   ;; FIXME: Check a expiration of cache?
-  (unless (mixi-diary-realize-p diary)
+  (unless (mixi-object-realize-p diary)
     (with-mixi-retrieve (mixi-diary-page diary)
       (if (string-match mixi-diary-owner-nick-regexp buffer)
          (mixi-friend-set-nick (mixi-diary-owner diary)
@@ -831,13 +855,7 @@ Increase this value when unexpected error frequently occurs."
          (mixi-diary-set-content diary (mixi-remove-markup
                                         (match-string 1 buffer)))
        (signal 'error (list 'cannot-find-content diary))))
-    (mixi-diary-touch diary)))
-
-(defun mixi-diary-realize-p (diary)
-  "Return the timestamp of DIARY."
-  (unless (mixi-diary-p diary)
-    (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
-  (aref (cdr diary) 0))
+    (mixi-object-touch diary)))
 
 (defun mixi-diary-owner (diary)
   "Return the owner of DIARY."
@@ -872,12 +890,6 @@ Increase this value when unexpected error frequently occurs."
   (mixi-diary-realize diary)
   (aref (cdr diary) 5))
 
-(defun mixi-diary-touch (diary)
-  "Set the timestamp of DIARY."
-  (unless (mixi-diary-p diary)
-    (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
-  (aset (cdr diary) 0 (current-time)))
-
 (defun mixi-diary-set-time (diary time)
   "Set the time of DIARY."
   (unless (mixi-diary-p diary)
@@ -974,7 +986,7 @@ Increase this value when unexpected error frequently occurs."
 (defun mixi-community-realize (community)
   "Realize a COMMUNITY."
   ;; FIXME: Check a expiration of cache?
-  (unless (mixi-community-realize-p community)
+  (unless (mixi-object-realize-p community)
     (with-mixi-retrieve (mixi-community-page community)
       (if (string-match mixi-community-nodata-regexp buffer)
          ;; FIXME: Set all members?
@@ -1013,13 +1025,7 @@ Increase this value when unexpected error frequently occurs."
            (mixi-community-set-description
             community (mixi-remove-markup (match-string 1 buffer)))
          (signal 'error (list 'cannot-find-description community)))))
-    (mixi-community-touch community)))
-
-(defun mixi-community-realize-p (community)
-  "Return the timestamp of COMMUNITY."
-  (unless (mixi-community-p community)
-    (signal 'wrong-type-argument (list 'mixi-community-p community)))
-  (aref (cdr community) 0))
+    (mixi-object-touch community)))
 
 (defun mixi-community-id (community)
   "Return the id of COMMUNITY."
@@ -1084,12 +1090,6 @@ Increase this value when unexpected error frequently occurs."
   (mixi-community-realize community)
   (aref (cdr community) 9))
 
-(defun mixi-community-touch (community)
-  "Set the timestamp of COMMUNITY."
-  (unless (mixi-community-p community)
-    (signal 'wrong-type-argument (list 'mixi-community-p community)))
-  (aset (cdr community) 0 (current-time)))
-
 (defun mixi-community-set-name (community name)
   "Set the name of COMMUNITY."
   (unless (mixi-community-p community)
@@ -1202,7 +1202,7 @@ Increase this value when unexpected error frequently occurs."
 (defun mixi-topic-realize (topic)
   "Realize a TOPIC."
   ;; FIXME: Check a expiration of cache?
-  (unless (mixi-topic-realize-p topic)
+  (unless (mixi-object-realize-p topic)
     (with-mixi-retrieve (mixi-topic-page topic)
       (if (string-match mixi-topic-time-regexp buffer)
          (mixi-topic-set-time
@@ -1224,13 +1224,7 @@ Increase this value when unexpected error frequently occurs."
          (mixi-topic-set-content topic (mixi-remove-markup
                                         (match-string 2 buffer)))
        (signal 'error (list 'cannot-find-content topic))))
-    (mixi-topic-touch topic)))
-
-(defun mixi-topic-realize-p (topic)
-  "Return the timestamp of TOPIC."
-  (unless (mixi-topic-p topic)
-    (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
-  (aref (cdr topic) 0))
+    (mixi-object-touch topic)))
 
 (defun mixi-topic-community (topic)
   "Return the community of TOPIC."
@@ -1272,12 +1266,6 @@ Increase this value when unexpected error frequently occurs."
   (mixi-topic-realize topic)
   (aref (cdr topic) 6))
 
-(defun mixi-topic-touch (topic)
-  "Set the timestamp of TOPIC."
-  (unless (mixi-topic-p topic)
-    (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
-  (aset (cdr topic) 0 (current-time)))
-
 (defun mixi-topic-set-time (topic time)
   "Set the time of TOPIC."
   (unless (mixi-topic-p topic)
@@ -1371,7 +1359,7 @@ Increase this value when unexpected error frequently occurs."
   (aref (cdr comment) 3))
 
 (defun mixi-diary-comment-list-page (diary)
-  (concat "/view_diary.pl?page=all"
+  (concat "/view_diary.pl?page=%d"
          "&id=" (mixi-diary-id diary)
          "&owner_id=" (mixi-friend-id (mixi-diary-owner diary))))
 
@@ -1403,7 +1391,7 @@ Increase this value when unexpected error frequently occurs."
 </td></tr></table>")
 
 (defun mixi-topic-comment-list-page (topic)
-  (concat "/view_bbs.pl?page=all"
+  (concat "/view_bbs.pl?page=%d"
          "&id=" (mixi-topic-id topic)
          "&comm_id=" (mixi-community-id (mixi-topic-community topic))))
 
@@ -1446,10 +1434,7 @@ Increase this value when unexpected error frequently occurs."
         (regexp (eval (intern (concat mixi-object-prefix name
                                       "-comment-list-regexp")))))
     (let ((items (mixi-get-matched-items
-                 (funcall list-page parent)
-                 ;; FIXME: Use `max-numbers' instead of 1.
-                 1
-                 regexp)))
+                 (funcall list-page parent) max-numbers regexp)))
       (mapcar (lambda (item)
                (mixi-make-comment parent (mixi-make-friend
                                           (nth 6 item) (nth 7 item))
@@ -1481,6 +1466,145 @@ Increase this value when unexpected error frequently occurs."
                (mixi-get-comments diary)))
            items)))
 
+;; Message object.
+(defvar mixi-message-cache (make-hash-table :test 'equal))
+(defun mixi-make-message (id box)
+  "Return a message object."
+  (mixi-make-cache (list id box)
+                  (cons 'mixi-message (vector nil id box nil nil nil nil))
+                  mixi-message-cache))
+
+(defmacro mixi-message-p (message)
+  `(eq (mixi-object-class ,message) 'mixi-message))
+
+(defmacro mixi-message-page (message)
+  `(concat "/view_message.pl?id=" (mixi-message-id ,message)
+          "&box=" (mixi-message-box ,message)))
+
+(defconst mixi-message-owner-regexp
+  "<font COLOR=#996600>º¹½Ð¿Í</font>&nbsp;:&nbsp;<a HREF=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.+\\)</a>")
+(defconst mixi-message-title-regexp
+  "<font COLOR=#996600>·ï¡¡Ì¾</font>&nbsp;:&nbsp;\\(.+\\)
+</td>")
+(defconst mixi-message-time-regexp
+  "<font COLOR=#996600>Æü¡¡ÉÕ</font>&nbsp;:&nbsp;\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü \\([0-9]+\\)»þ\\([0-9]+\\)ʬ&nbsp;&nbsp;")
+(defconst mixi-message-content-regexp
+  "<tr><td CLASS=h120>\\(.+\\)</td></tr>")
+
+(defun mixi-message-realize (message)
+  "Realize a MESSAGE."
+  (unless (mixi-object-realize-p message)
+    (with-mixi-retrieve (mixi-message-page message)
+      (if (string-match mixi-message-owner-regexp buffer)
+         (mixi-message-set-owner message
+                                 (mixi-make-friend (match-string 1 buffer)
+                                                   (match-string 2 buffer)))
+       (signal 'error (list 'cannot-find-owner message)))
+      (if (string-match mixi-message-title-regexp buffer)
+         (mixi-message-set-title message (match-string 1 buffer))
+       (signal 'error (list 'cannot-find-title message)))
+      (if (string-match mixi-message-time-regexp buffer)
+         (mixi-message-set-time
+          message (encode-time 0 (string-to-number (match-string 5 buffer))
+                               (string-to-number (match-string 4 buffer))
+                               (string-to-number (match-string 3 buffer))
+                               (string-to-number (match-string 2 buffer))
+                               (string-to-number (match-string 1 buffer))))
+       (signal 'error (list 'cannot-find-time message)))
+      (if (string-match mixi-message-content-regexp buffer)
+         (mixi-message-set-content message (mixi-remove-markup
+                                            (match-string 1 buffer)))
+       (signal 'error (list 'cannot-find-content message))))
+    (mixi-object-touch message)))
+
+(defun mixi-message-id (message)
+  "Return the id of MESSAGE."
+  (unless (mixi-message-p message)
+    (signal 'wrong-type-argument (list 'mixi-message-p message)))
+  (aref (cdr message) 1))
+
+(defun mixi-message-box (message)
+  "Return the box of MESSAGE."
+  (unless (mixi-message-p message)
+    (signal 'wrong-type-argument (list 'mixi-message-p message)))
+  (aref (cdr message) 2))
+
+(defun mixi-message-owner (message)
+  "Return the owner of MESSAGE."
+  (unless (mixi-message-p message)
+    (signal 'wrong-type-argument (list 'mixi-message-p message)))
+  (mixi-message-realize message)
+  (aref (cdr message) 3))
+
+(defun mixi-message-title (message)
+  "Return the title of MESSAGE."
+  (unless (mixi-message-p message)
+    (signal 'wrong-type-argument (list 'mixi-message-p message)))
+  (mixi-message-realize message)
+  (aref (cdr message) 4))
+
+(defun mixi-message-time (message)
+  "Return the date of MESSAGE."
+  (unless (mixi-message-p message)
+    (signal 'wrong-type-argument (list 'mixi-message-p message)))
+  (mixi-message-realize message)
+  (aref (cdr message) 5))
+
+(defun mixi-message-content (message)
+  "Return the content of MESSAGE."
+  (unless (mixi-message-p message)
+    (signal 'wrong-type-argument (list 'mixi-message-p message)))
+  (mixi-message-realize message)
+  (aref (cdr message) 6))
+
+(defun mixi-message-set-owner (message owner)
+  "Set the owner of MESSAGE."
+  (unless (mixi-message-p message)
+    (signal 'wrong-type-argument (list 'mixi-message-p message)))
+  (aset (cdr message) 3 owner))
+
+(defun mixi-message-set-title (message title)
+  "Set the title of MESSAGE."
+  (unless (mixi-message-p message)
+    (signal 'wrong-type-argument (list 'mixi-message-p message)))
+  (aset (cdr message) 4 title))
+
+(defun mixi-message-set-time (message time)
+  "Set the date of MESSAGE."
+  (unless (mixi-message-p message)
+    (signal 'wrong-type-argument (list 'mixi-message-p message)))
+  (aset (cdr message) 5 time))
+
+(defun mixi-message-set-content (message content)
+  "Set the content of MESSAGE."
+  (unless (mixi-message-p message)
+    (signal 'wrong-type-argument (list 'mixi-message-p message)))
+  (aset (cdr message) 6 content))
+
+(defmacro mixi-message-list-page (&optional box)
+  `(concat "/list_message.pl?page=%d"
+          (when ,box (concat "&box=" ,box))))
+
+(defconst mixi-message-list-regexp
+  "<td><a HREF=\"view_message\\.pl\\?id=\\(.+\\)&box=\\(.+\\)\">")
+
+(defun mixi-get-messages (&rest args)
+  "Get messages."
+  (when (> (length args) 2)
+    (signal 'wrong-number-of-arguments (list 'mixi-get-messages
+                                            (length args))))
+  (let ((box (nth 0 args))
+       (max-numbers (nth 1 args)))
+    (when (or (not (stringp box)) (stringp max-numbers))
+      (setq box (nth 1 args))
+      (setq max-numbers (nth 0 args)))
+    (let ((items (mixi-get-matched-items (mixi-message-list-page box)
+                                        max-numbers
+                                        mixi-message-list-regexp)))
+      (mapcar (lambda (item)
+               (mixi-make-message (nth 0 item) (nth 1 item)))
+             items))))
+
 (provide 'mixi)
 
 ;;; mixi.el ends here