* mixi-utils.el: New file.
[elisp/mixi.git] / sb-mixi.el
index 027dee6..3b6af77 100644 (file)
 ;;; Code:
 
 (require 'mixi)
+(require 'mixi-utils)
 (require 'shimbun)
 
 (eval-and-compile
   (luna-define-class shimbun-mixi (shimbun) (comment-cache))
   (luna-define-internal-accessors 'shimbun-mixi))
 
-(defcustom shimbun-mixi-group-alist '(("new-diaries" . mixi-get-new-diaries)
-                                     ("new-comments" . mixi-get-new-comments)
-                                     ("new-bbses" . mixi-get-new-bbses)
-                                     ("messages" . mixi-get-messages)
-                                     ("my-diaries" . "/home.pl"))
+(defcustom shimbun-mixi-group-alist
+  '(("new-diaries" . mixi-get-new-diaries)
+    ("new-comments" . mixi-get-new-comments)
+    ("new-bbses" . mixi-get-new-bbses)
+    ("messages" . mixi-get-messages)
+    ("messages.sent" .
+     (lambda (range)
+       (mixi-get-messages 'outbox range)))
+    ("logs" . mixi-get-logs)
+    ("my-diaries" . "/home.pl")
+    ("mixi-el" . "/view_community.pl?id=1596390")
+    ("news.newest.domestic" .
+     (lambda (range)
+       (mixi-get-news 'domestic 'newest range)))
+    ("news.newest.politics" .
+     (lambda (range)
+       (mixi-get-news 'politics 'newest range)))
+    ("news.newest.economy" .
+     (lambda (range)
+       (mixi-get-news 'economy 'newest range)))
+    ("news.newest.area" .
+     (lambda (range)
+       (mixi-get-news 'area 'newest range)))
+    ("news.newest.abroad" .
+     (lambda (range)
+       (mixi-get-news 'abroad 'newest range)))
+    ("news.newest.sports" .
+     (lambda (range)
+       (mixi-get-news 'sports 'newest range)))
+    ("news.newest.entertainment" .
+     (lambda (range)
+       (mixi-get-news 'entertainment 'newest range)))
+    ("news.newest.it" .
+     (lambda (range)
+       (mixi-get-news 'IT 'newest range)))
+    ("news.pickup.domestic" .
+     (lambda (range)
+       (mixi-get-news 'domestic 'pickup range)))
+    ("news.pickup.politics" .
+     (lambda (range)
+       (mixi-get-news 'politics 'pickup range)))
+    ("news.pickup.economy" .
+     (lambda (range)
+       (mixi-get-news 'economy 'pickup range)))
+    ("news.pickup.area" .
+     (lambda (range)
+       (mixi-get-news 'area 'pickup range)))
+    ("news.pickup.abroad" .
+     (lambda (range)
+       (mixi-get-news 'abroad 'pickup range)))
+    ("news.pickup.sports" .
+     (lambda (range)
+       (mixi-get-news 'sports 'pickup range)))
+    ("news.pickup.entertainment" .
+     (lambda (range)
+       (mixi-get-news 'entertainment 'pickup range)))
+    ("news.pickup.it" .
+     (lambda (range)
+       (mixi-get-news 'IT 'pickup range))))
   "*An alist of mixi shimbun group definition.
 Each element looks like (NAME . URL) or (NAME . FUNCTION).
 NAME is a shimbun group name.
-URL is the URL for mixi access point of the group.  When URL is friend's, get
-his/her diaries as article.  When community's, get its BBSes.  When diary's
-or BBS's, get its comments.
+URL is the URL for mixi access point of the group.  If URL is friend's, get
+his/her diaries as article.  If community's, get its BBSes.  If diary's or
+BBS's, get its comments.
 FUNCTION is the function which has one `range' argument and returns the list
 of mixi object."
   :group 'shimbun
@@ -57,6 +112,7 @@ of mixi object."
                              (const :tag "New comments" mixi-get-new-comments)
                              (const :tag "New BBSes" mixi-get-new-bbses)
                              (const :tag "Messages" mixi-get-messages)
+                             (const :tag "Logs" mixi-get-logs)
                              (function :tag "Other function")))))
 
 ;; FIXME: Don't use this user option.
@@ -83,84 +139,8 @@ of mixi object."
 (luna-define-method shimbun-groups ((shimbun shimbun-mixi))
   (mapcar 'car shimbun-mixi-group-alist))
 
-(defun shimbun-mixi-make-subject (shimbun object)
-  (let ((class (mixi-object-class object)))
-    (if (eq class 'mixi-comment)
-        (concat "Re: " (shimbun-mixi-make-subject
-                       shimbun (mixi-comment-parent object)))
-      (let ((prefix (when (eq class 'mixi-event) "[¥¤¥Ù¥ó¥È]"))
-           (subject (mixi-object-title object))
-           (suffix (when (string-match
-                          "^new-" (shimbun-current-group-internal shimbun))
-                     (concat " ("
-                             (if (eq class 'mixi-diary)
-                                 (mixi-friend-nick
-                                  (mixi-diary-owner object))
-                               (mixi-community-name
-                                (mixi-bbs-community object)))
-                             ")"))))
-       (concat prefix subject suffix)))))
-
-(defun shimbun-mixi-make-from (object)
-  (let ((owner (mixi-object-owner object)))
-    (mixi-friend-nick owner)))
-
-(defun shimbun-mixi-make-date (object)
-  (let* ((time (mixi-object-time object))
-        (cts (current-time-string time))
-        (day-of-week (substring cts 0 3))
-        (month (substring cts 4 7)))
-    (concat day-of-week ", "
-           (format-time-string "%d" time) " "
-           month " "
-           (format-time-string "%Y %H:%M:%S %z" time))))
-
-(defun shimbun-mixi-make-message-id (object)
-  (let ((class (mixi-object-class object)))
-    (concat "<"
-           (format-time-string "%Y%m%d%H%M" (mixi-object-time object)) "."
-           (if (eq class 'mixi-comment)
-               (concat (mixi-friend-id (mixi-comment-owner object)) "@"
-                       (mixi-object-id (mixi-comment-parent object)) "."
-                       (mixi-friend-id (mixi-object-owner
-                                        (mixi-comment-parent object))) ".")
-             (concat (mixi-object-id object) "@"
-                     (mixi-object-id (mixi-object-owner object)) "."))
-           (mixi-object-name object) ".mixi.jp"
-           ">")))
-
-(defun shimbun-mixi-make-xref (object)
-  (let ((class (mixi-object-class object)))
-    (cond ((eq class 'mixi-diary)
-          (mixi-expand-url (mixi-diary-page object)))
-         ((eq class 'mixi-topic)
-          (mixi-expand-url (mixi-topic-page object)))
-         ((eq class 'mixi-event)
-          (mixi-expand-url (mixi-event-page object)))
-         ((eq class 'mixi-comment)
-          (concat (shimbun-mixi-make-xref (mixi-comment-parent object))
-                  "#comment"))
-         ((eq class 'mixi-message)
-          (mixi-expand-url (mixi-message-page object))))))
-
-(defun shimbun-mixi-make-body (object)
-  (let ((class (mixi-object-class object)))
-    (cond ((eq class 'mixi-event)
-          (let ((limit (mixi-event-limit object)))
-            (setq limit (if limit
-                            (format-time-string "%Yǯ%m·î%dÆü" limit)
-                          "»ØÄê¤Ê¤·"))
-            (concat "<dl><dt>³«ºÅÆü»þ¡§</dt>"
-                    "<dd>" (mixi-event-date object) "</dd>"
-                    "<dt>³«ºÅ¾ì½ê¡§</dt>"
-                    "<dd>" (mixi-event-place object) "</dd>"
-                    "<dt>¾ÜºÙ¡§</dt>"
-                    "<dd>" (mixi-event-detail object) "</dd>"
-                    "<dt>Ê罸´ü¸Â¡§</dt>"
-                    "<dd>" limit "</dd>"
-                    "<dt>»²²Ã¼Ô¡§</dt>"
-                    "<dd>" (mixi-event-members object) "</dd></dl>")))
-         (t (mixi-object-content object)))))
+(luna-define-method shimbun-reply-to ((shimbun shimbun-mixi))
+  mixi-reply-to)
 
 (defun shimbun-mixi-get-headers (shimbun objects &optional range)
   (when objects
@@ -168,33 +148,39 @@ of mixi object."
       (catch 'stop
        (mapc (lambda (object)
                (when (mixi-object-p object)
-                 (let ((class (mixi-object-class object))
-                       (id (shimbun-mixi-make-message-id object)))
-                   (push
-                    (shimbun-create-header
-                     0
-                     (shimbun-mixi-make-subject shimbun object)
-                     (shimbun-mixi-make-from object)
-                     (shimbun-mixi-make-date object)
-                     id
-                     (if (eq class 'mixi-comment)
-                         (shimbun-mixi-make-message-id
-                          (mixi-comment-parent object))
-                       "")
-                     0 0
-                     (shimbun-mixi-make-xref object))
-                    headers)
-                   (when (or (eq class 'mixi-diary)
-                             (mixi-bbs-p object))
+                 (let ((class (mixi-object-class object)))
+                   (when (mixi-parent-p object)
                      (let ((comments (mixi-get-comments object range)))
                        (mapc (lambda (header)
                                (push header headers))
                              (shimbun-mixi-get-headers shimbun
                                                        comments))))
-                   (when (eq class 'mixi-comment)
-                     (puthash id (mixi-comment-content object)
-                              (shimbun-mixi-comment-cache-internal
-                               shimbun))))))
+                   (let ((id (mixi-make-message-id object)))
+                     (when (and (eq class 'mixi-comment)
+                                (shimbun-search-id shimbun id))
+                       (throw 'stop nil))
+                     (push
+                      (shimbun-create-header
+                       0
+                       (mixi-make-title object
+                                        (string-match
+                                         "^new-"
+                                         (shimbun-current-group-internal
+                                          shimbun)))
+                       (mixi-make-author object)
+                       (mixi-make-date object)
+                       id
+                       (if (eq class 'mixi-comment)
+                           (mixi-make-message-id
+                            (mixi-comment-parent object))
+                         "")
+                       0 0
+                       (mixi-make-url object))
+                      headers)
+                     (when (eq class 'mixi-comment)
+                       (puthash id (mixi-comment-content object)
+                                (shimbun-mixi-comment-cache-internal
+                                 shimbun)))))))
              objects))
       headers)))
 
@@ -211,8 +197,7 @@ of mixi object."
                 (setq objects (mixi-get-diaries object range)))
                ((eq class 'mixi-community)
                 (setq objects (mixi-get-bbses object range)))
-               ((or (eq class 'mixi-diary)
-                    (mixi-bbs-p object))
+               ((mixi-parent-p object)
                 (setq objects (mixi-get-comments object range)))
                (t (error (concat (symbol-name class)
                                  " is not supported yet.")))))
@@ -226,7 +211,7 @@ of mixi object."
     (unless (stringp article)
       (let ((parent (mixi-make-object-from-url url)))
        (mapc (lambda (comment)
-               (let ((id (shimbun-mixi-make-message-id comment))
+               (let ((id (mixi-make-message-id comment))
                      (content (mixi-comment-content comment)))
                  (puthash id content cache)
                  (when (string= id message-id)
@@ -241,11 +226,12 @@ of mixi object."
       (w3m-insert-string
        (or (with-temp-buffer
             (let* ((url (shimbun-article-url shimbun header))
+                   (object (mixi-make-object-from-url url))
                    (article (if (string-match "#comment$" url)
                                 (shimbun-mixi-comment-article
                                  url shimbun header)
-                              (shimbun-mixi-make-body
-                               (mixi-make-object-from-url url)))))
+                              (mixi-make-content object))))
+              (mixi-make-reply-to object)
               (when (stringp article)
                 (insert article)))
             (shimbun-message shimbun "shimbun: Make contents...")
@@ -254,47 +240,32 @@ of mixi object."
               (shimbun-message shimbun "shimbun: Make contents...done")))
           "")))))
 
-(defun shimbun-mixi-make-object-from-message-id (message-id)
-  (when (string-match (concat "^<\\(.+\\)"
-                             (regexp-quote shimbun-mixi-message-id-suffix)
-                             ">$") message-id)
-    (let ((parts (reverse (split-string (match-string 1 message-id) "\\$")))
-         object parent comment)
-      (catch 'stop
-       (mapc (lambda (part)
-               (if (string-match "\\([a-z0-9]+\\)\\.\\([a-z]+\\)$" part)
-                   (let ((id (match-string 1 part))
-                         (func (intern (concat
-                                        mixi-object-prefix
-                                        "make-" (match-string 2 part)))))
-                     (cond ((null object)
-                            (setq object (funcall func id)))
-                           ((null parent)
-                            (setq parent (funcall func object id)))
-                           ((null comment)
-                            (setq comment
-                                  (mixi-make-comment parent
-                                                     (mixi-make-friend id)
-                                                     nil "dummy")))))
-                 (throw 'stop nil)))
-             parts))
-      (or comment parent object))))
+(defconst shimbun-mixi-to-regexp
+  "^mixi;\\([a-z]+\\);?\\([a-z0-9]+\\)?;?\\([0-9]+\\)?;?\\([0-9]+\\)?")
 
-(defun shimbun-mixi-send-mail-wrapper (in-reply-to title content)
-  (let ((object (shimbun-mixi-make-object-from-message-id in-reply-to)))
-    (when (mixi-object-p object)
-      (let ((class (mixi-object-class object)))
-       (cond ((eq class 'mixi-comment)
-              (let ((parent (mixi-comment-parent object)))
-                (mixi-post-comment parent content)))
-             ((or (eq class 'mixi-diary) (mixi-bbs-p object))
-              (mixi-post-comment object content))
-             ((eq class 'mixi-community)
-              (mixi-post-topic object title content))
-             ((eq object (mixi-make-me))
-              (mixi-post-diary title content))
-             ((or (eq class 'mixi-friend) (eq class 'mixi-log))
-              (mixi-post-message title content)))))))
+(defun shimbun-mixi-send-mail (to title content)
+  (when (string-match shimbun-mixi-to-regexp to)
+    (let ((method (match-string 1 to)))
+      (cond ((string= method "comment")
+            (let ((parent (match-string 2 to))
+                  (owner-id (match-string 3 to))
+                  (id (match-string 4 to)))
+              (if (string= parent "diary")
+                  (mixi-post-comment
+                   (mixi-make-diary (mixi-make-friend owner-id) id) content)
+                (let ((func (intern
+                             (concat mixi-object-prefix "make-" parent))))
+                  (mixi-post-comment
+                   (funcall func (mixi-make-community owner-id) id)
+                   content)))))
+           ((string= method "topic")
+            (mixi-post-topic (mixi-make-community (match-string 2 to))
+                             title content))
+           ((string= method "diary")
+            (mixi-post-diary title content))
+           ((string= method "message")
+            (mixi-post-message (mixi-make-friend (match-string 2 to))
+                               title content))))))
 
 (provide 'sb-mixi)