* sb-mixi.el (shimbun-mixi-get-comment-p): New user option.
[elisp/mixi.git] / sb-mixi.el
1 ;;; sb-mixi.el --- shimbun backend for mixi
2
3 ;; Copyright (C) 2006 OHASHI Akira
4
5 ;; Author: OHASHI Akira <bg66@koka-in.org>
6 ;; Keywords: news
7
8 ;; This file is *NOT* a part of shimbun.
9
10 ;; This program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program; if not, you can either send email to this
22 ;; program's maintainer or write to: The Free Software Foundation,
23 ;; Inc.; 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (require 'mixi)
30 (require 'shimbun)
31
32 (luna-define-class shimbun-mixi (shimbun) ())
33
34 (defcustom shimbun-mixi-group-alist '(("new-diaries" . mixi-get-new-diaries)
35                                       ("new-comments" . mixi-get-new-comments)
36                                       ("new-topics" . mixi-get-new-topics)
37                                       ("messages" . mixi-get-messages)
38                                       ("my-diaries" . "/home.pl"))
39   "*An alist of mixi shimbun group definition.
40 Each element looks like (NAME . URL) or (NAME . FUNCTION).
41 NAME is a shimbun group name.
42 URL is the URL for mixi access point of the group.  When URL is friend's, get
43 his/her diaries as article.  When community's, get its topics.  When diary's
44 or topic's, get its comments.
45 FUNCTION is the function for getting articles."
46   :group 'shimbun
47   :type '(repeat (cons :fromat "%v"
48                        (string :tag "Group name")
49                        (radio (string :tag "URL")
50                               (const :tag "New diaries" mixi-get-new-diaries)
51                               (const :tag "New comments" mixi-get-new-comments)
52                               (const :tag "New topics" mixi-get-new-topics)
53                               (const :tag "Messages" mixi-get-messages)
54                               (function :tag "Other function")))))
55
56 (defcustom shimbun-mixi-page-articles 10
57   "*How many articles are there in one page."
58   :group 'shimbun
59   :type 'integer)
60
61 (defcustom shimbun-mixi-get-comment-p t
62   "*If non-nil, get diaries or topics together with its comments."
63   :group 'shimbun
64   :type 'boolean)
65
66 (luna-define-method shimbun-groups ((shimbun shimbun-mixi))
67   (mapcar 'car shimbun-mixi-group-alist))
68
69 (defun shimbun-mixi-make-subject (object)
70   (let ((class (mixi-object-class object)))
71     (if (eq class 'mixi-comment)
72         (concat "Re: " (mixi-object-title (mixi-comment-parent object)))
73       (mixi-object-title object))))
74
75 (defun shimbun-mixi-make-from (object)
76   (let ((owner (mixi-object-owner object)))
77     (mixi-friend-nick owner)))
78
79 (defun shimbun-mixi-make-date (object)
80   (let* ((time (mixi-object-time object))
81          (cts (current-time-string time))
82          (day-of-week (substring cts 0 3))
83          (month (substring cts 4 7)))
84     (concat day-of-week ", "
85             (format-time-string "%d" time) " "
86             month " "
87             (format-time-string "%Y %H:%M:%S %z" time))))
88
89 (defun shimbun-mixi-make-message-id (object)
90   (let ((class (mixi-object-class object)))
91     (concat "<"
92             (format-time-string "%Y%m%d%H%M" (mixi-object-time object)) "."
93             (if (eq class 'mixi-comment)
94                 (concat (mixi-friend-id (mixi-comment-owner object)) "@"
95                         (mixi-object-id (mixi-comment-parent object)) "."
96                         (mixi-friend-id (mixi-object-owner
97                                          (mixi-comment-parent object))) ".")
98               (concat (mixi-object-id object) "@"
99                       (mixi-object-id (mixi-object-owner object)) "."))
100             (mixi-object-name object) ".mixi.jp"
101             ">")))
102
103 (defun shimbun-mixi-make-xref (object)
104   (let ((class (mixi-object-class object)))
105     (cond ((eq class 'mixi-diary)
106            (mixi-expand-url (mixi-diary-page object)))
107           ((eq class 'mixi-topic)
108            (mixi-expand-url (mixi-topic-page object)))
109           ((eq class 'mixi-comment)
110            (concat (shimbun-mixi-make-xref (mixi-comment-parent object))
111                    "#comment"))
112           ((eq class 'mixi-message)
113            (mixi-expand-url (mixi-message-page object))))))
114
115 (defun shimbun-mixi-get-headers (shimbun objects &optional range)
116   (when objects
117     (let (headers)
118       (catch 'stop
119         (mapc (lambda (object)
120                 (when (mixi-object-p object)
121                   (let ((class (mixi-object-class object))
122                         (id (shimbun-mixi-make-message-id object)))
123                     (when (and (eq class 'mixi-comment)
124                                (shimbun-search-id shimbun id))
125                       (throw 'stop nil))
126                     (push
127                      (shimbun-create-header
128                       0
129                       (shimbun-mixi-make-subject object)
130                       (shimbun-mixi-make-from object)
131                       (shimbun-mixi-make-date object)
132                       id
133                       (if (eq class 'mixi-comment)
134                           (shimbun-mixi-make-message-id
135                            (mixi-comment-parent object))
136                         "")
137                       0 0
138                       (shimbun-mixi-make-xref object))
139                      headers)
140                     (when (and shimbun-mixi-get-comment-p
141                                (or (eq class 'mixi-diary)
142                                    (eq class 'mixi-topic)))
143                       (let ((comments (mixi-get-comments object range)))
144                         (mapc (lambda (header)
145                                 (push header headers))
146                               (shimbun-mixi-get-headers shimbun
147                                                         comments)))))))
148               objects))
149       headers)))
150
151 (luna-define-method shimbun-get-headers ((shimbun shimbun-mixi)
152                                          &optional range)
153   (let ((url-or-function (cdr (assoc (shimbun-current-group-internal shimbun)
154                                      shimbun-mixi-group-alist)))
155         (range (when (integerp range) (* range shimbun-mixi-page-articles)))
156         objects)
157     (if (stringp url-or-function)
158         (let* ((object (mixi-make-object-from-url url-or-function))
159                (class (mixi-object-class object)))
160           (cond ((eq class 'mixi-friend)
161                  (setq objects (mixi-get-diaries object range)))
162                 ((eq class 'mixi-community)
163                  (setq objects (mixi-get-topics object range)))
164                 ((or (eq class 'mixi-diary) (eq class 'mixi-topic))
165                  (setq objects (mixi-get-comments object range)))
166                 (t (error (concat (symbol-name class)
167                                   " is not supported yet.")))))
168       (when (fboundp url-or-function)
169         (setq objects (funcall url-or-function range))))
170     (shimbun-sort-headers (shimbun-mixi-get-headers shimbun objects range))))
171
172 (defun shimbun-comment-article (url header)
173   (let ((parent (mixi-make-object-from-url url))
174         (date (shimbun-header-date header))
175         (from (shimbun-header-from header)))
176     (catch 'found
177       (mapc (lambda (comment)
178               (let ((nick (mixi-friend-nick (mixi-comment-owner comment)))
179                     (time (shimbun-mixi-make-date comment))
180                     nick2)
181                 ;; FIXME: How tricky it is.
182                 (when (string-match "\\(.+\\)¤µ¤ó$" nick)
183                   (setq nick2 (match-string 1 nick)))
184                 (when (and
185                        (or (string= (shimbun-mime-encode-string nick) from)
186                            (string= (shimbun-mime-encode-string nick2) from))
187                        (string= time date))
188                   ;; FIXME: Concat parent's information?
189                   (throw 'found (mixi-comment-content comment)))))
190             ;; FIXME: Limit range?
191             (mixi-get-comments parent)))))
192
193 (luna-define-method shimbun-article ((shimbun shimbun-mixi)
194                                      header &optional outbuf)
195   (when (shimbun-current-group-internal shimbun)
196     (with-current-buffer (or outbuf (current-buffer))
197       (w3m-insert-string
198        (or (with-temp-buffer
199              (let* ((url (shimbun-article-url shimbun header))
200                     (article (if (string-match "#comment$" url)
201                                  (shimbun-comment-article url header)
202                                ;; FIXME: Concat community information?
203                                (mixi-object-content
204                                 (mixi-make-object-from-url url)))))
205                (when (stringp article)
206                  (insert article)))
207              (shimbun-message shimbun "shimbun: Make contents...")
208              (goto-char (point-min))
209              (prog1 (shimbun-make-contents shimbun header)
210                (shimbun-message shimbun "shimbun: Make contents...done")))
211            "")))))
212
213 (provide 'sb-mixi)
214
215 ;;; sb-mixi.el ends here