5b16ba805c90e69569b0ca36a16bf2ca6d53acc7
[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 ;; If you have bug reports and/or suggestions for improvement, please
28 ;; send them via <URL:http://mixi.jp/view_community.pl?id=1596390>.
29
30 ;;; Code:
31
32 (require 'mixi)
33 (require 'shimbun)
34
35 (eval-and-compile
36   (luna-define-class shimbun-mixi (shimbun) (comment-cache))
37   (luna-define-internal-accessors 'shimbun-mixi))
38
39 (defcustom shimbun-mixi-group-alist '(("new-diaries" . mixi-get-new-diaries)
40                                       ("new-comments" . mixi-get-new-comments)
41                                       ("new-bbses" . mixi-get-new-bbses)
42                                       ("messages" . mixi-get-messages)
43                                       ("my-diaries" . "/home.pl"))
44   "*An alist of mixi shimbun group definition.
45 Each element looks like (NAME . URL) or (NAME . FUNCTION).
46 NAME is a shimbun group name.
47 URL is the URL for mixi access point of the group.  When URL is friend's, get
48 his/her diaries as article.  When community's, get its BBSes.  When diary's
49 or BBS's, get its comments.
50 FUNCTION is the function for getting articles."
51   :group 'shimbun
52   :type '(repeat (cons :fromat "%v"
53                        (string :tag "Group name")
54                        (radio (string :tag "URL")
55                               (const :tag "New diaries" mixi-get-new-diaries)
56                               (const :tag "New comments" mixi-get-new-comments)
57                               (const :tag "New BBSes" mixi-get-new-bbses)
58                               (const :tag "Messages" mixi-get-messages)
59                               (function :tag "Other function")))))
60
61 ;; FIXME: Don't use this user option.
62 (defcustom shimbun-mixi-page-articles 10
63   "*How many articles are there in one page."
64   :group 'shimbun
65   :type 'integer)
66
67 (defcustom shimbun-mixi-get-comment-p t
68   "*If non-nil, get diaries or BBSes together with its comments."
69   :group 'shimbun
70   :type 'boolean)
71
72 (defvar shimbun-mixi-x-face-alist
73   '(("default" . "X-Face: CY;j#FoBnpK^37`-IoJvN!J^u;GciiPmMQ@T)~RP1]t8iv?v)/bVI:I\"F!JfWJvhM5{zY!=
74  h.d+'g\\I{D>Ocy?Rc4uYUyOZj2%2Kl>,x-!MCSsyi3!L}psrrC1jlF,O?Ui>qf)X;sBz`/}\\066X%$
75  siG'|4K!2?==|oB&#E'5GGH\\#z[muyQ")))
76
77 (luna-define-method initialize-instance :after ((shimbun shimbun-mixi)
78                                                 &rest init-args)
79   (shimbun-mixi-set-comment-cache-internal shimbun
80                                            (make-hash-table :test 'equal))
81   shimbun)
82
83 (luna-define-method shimbun-groups ((shimbun shimbun-mixi))
84   (mapcar 'car shimbun-mixi-group-alist))
85
86 (defun shimbun-mixi-make-subject (object)
87   (let ((class (mixi-object-class object)))
88     (cond ((eq class 'mixi-comment)
89            (concat "Re: " (shimbun-mixi-make-subject
90                            (mixi-comment-parent object))))
91           (t (mixi-object-title object)))))
92
93 (defun shimbun-mixi-make-from (object)
94   (let ((owner (mixi-object-owner object)))
95     (mixi-friend-nick owner)))
96
97 (defun shimbun-mixi-make-date (object)
98   (let* ((time (mixi-object-time object))
99          (cts (current-time-string time))
100          (day-of-week (substring cts 0 3))
101          (month (substring cts 4 7)))
102     (concat day-of-week ", "
103             (format-time-string "%d" time) " "
104             month " "
105             (format-time-string "%Y %H:%M:%S %z" time))))
106
107 (defun shimbun-mixi-make-message-id (object)
108   (let ((class (mixi-object-class object)))
109     (concat "<"
110             (format-time-string "%Y%m%d%H%M" (mixi-object-time object)) "."
111             (if (eq class 'mixi-comment)
112                 (concat (mixi-friend-id (mixi-comment-owner object)) "@"
113                         (mixi-object-id (mixi-comment-parent object)) "."
114                         (mixi-friend-id (mixi-object-owner
115                                          (mixi-comment-parent object))) ".")
116               (concat (mixi-object-id object) "@"
117                       (mixi-object-id (mixi-object-owner object)) "."))
118             (mixi-object-name object) ".mixi.jp"
119             ">")))
120
121 (defun shimbun-mixi-make-xref (object)
122   (let ((class (mixi-object-class object)))
123     (cond ((eq class 'mixi-diary)
124            (mixi-expand-url (mixi-diary-page object)))
125           ((eq class 'mixi-topic)
126            (mixi-expand-url (mixi-topic-page object)))
127           ((eq class 'mixi-event)
128            (mixi-expand-url (mixi-event-page object)))
129           ((eq class 'mixi-comment)
130            (concat (shimbun-mixi-make-xref (mixi-comment-parent object))
131                    "#comment"))
132           ((eq class 'mixi-message)
133            (mixi-expand-url (mixi-message-page object))))))
134
135 (defun shimbun-mixi-make-body (object)
136   (mixi-object-content object))
137
138 (defun shimbun-mixi-get-headers (shimbun objects &optional range)
139   (when objects
140     (let (headers)
141       (catch 'stop
142         (mapc (lambda (object)
143                 (when (mixi-object-p object)
144                   (let ((class (mixi-object-class object))
145                         (id (shimbun-mixi-make-message-id object)))
146                     (push
147                      (shimbun-create-header
148                       0
149                       (shimbun-mixi-make-subject object)
150                       (shimbun-mixi-make-from object)
151                       (shimbun-mixi-make-date object)
152                       id
153                       (if (eq class 'mixi-comment)
154                           (shimbun-mixi-make-message-id
155                            (mixi-comment-parent object))
156                         "")
157                       0 0
158                       (shimbun-mixi-make-xref object))
159                      headers)
160                     (when (and shimbun-mixi-get-comment-p
161                                (or (eq class 'mixi-diary)
162                                    (eq class 'mixi-topic)
163                                    (eq class 'mixi-event)))
164                       (let ((comments (mixi-get-comments object range)))
165                         (mapc (lambda (header)
166                                 (push header headers))
167                               (shimbun-mixi-get-headers shimbun
168                                                         comments))))
169                     (when (eq class 'mixi-comment)
170                       (puthash id (mixi-comment-content object)
171                                (shimbun-mixi-comment-cache-internal
172                                 shimbun))))))
173               objects))
174       headers)))
175
176 (luna-define-method shimbun-get-headers ((shimbun shimbun-mixi)
177                                          &optional range)
178   (let ((url-or-function (cdr (assoc (shimbun-current-group-internal shimbun)
179                                      shimbun-mixi-group-alist)))
180         (range (when (integerp range) (* range shimbun-mixi-page-articles)))
181         objects)
182     (if (stringp url-or-function)
183         (let* ((object (mixi-make-object-from-url url-or-function))
184                (class (mixi-object-class object)))
185           (cond ((eq class 'mixi-friend)
186                  (setq objects (mixi-get-diaries object range)))
187                 ((eq class 'mixi-community)
188                  (setq objects (mixi-get-bbses object range)))
189                 ((or (eq class 'mixi-diary)
190                      (eq class 'mixi-topic)
191                      (eq class 'mixi-event))
192                  (setq objects (mixi-get-comments object range)))
193                 (t (error (concat (symbol-name class)
194                                   " is not supported yet.")))))
195       (setq objects (funcall url-or-function range)))
196     (shimbun-sort-headers (shimbun-mixi-get-headers shimbun objects range))))
197
198 (defun shimbun-comment-article (url shimbun header)
199   (let* ((parent (mixi-make-object-from-url url))
200          (date (shimbun-header-date header))
201          (message-id (shimbun-header-id header))
202          (cache (gethash message-id
203                          (shimbun-mixi-comment-cache-internal shimbun))))
204     (if (stringp cache)
205         cache
206       (catch 'found
207         (mapc (lambda (comment)
208                 (let ((id (mixi-friend-id (mixi-comment-owner comment)))
209                       (time (shimbun-mixi-make-date comment)))
210                   (when (and (string= time date)
211                              (string-match (concat "^<[0-9]+\\." id "@")
212                                            message-id))
213                     ;; FIXME: Concat parent's information?
214                     (let ((content (mixi-comment-content comment)))
215                       (puthash message-id content
216                                (shimbun-mixi-comment-cache-internal shimbun))
217                       (throw 'found content)))))
218               (mixi-get-comments parent))))))
219
220 (luna-define-method shimbun-article ((shimbun shimbun-mixi)
221                                      header &optional outbuf)
222   (when (shimbun-current-group-internal shimbun)
223     (with-current-buffer (or outbuf (current-buffer))
224       (w3m-insert-string
225        (or (with-temp-buffer
226              (let* ((url (shimbun-article-url shimbun header))
227                     (article (if (string-match "#comment$" url)
228                                  (shimbun-comment-article url shimbun header)
229                                ;; FIXME: Concat community information?
230                                (shimbun-mixi-make-body
231                                 (mixi-make-object-from-url url)))))
232                (when (stringp article)
233                  (insert article)))
234              (shimbun-message shimbun "shimbun: Make contents...")
235              (goto-char (point-min))
236              (prog1 (shimbun-make-contents shimbun header)
237                (shimbun-message shimbun "shimbun: Make contents...done")))
238            "")))))
239
240 (provide 'sb-mixi)
241
242 ;;; sb-mixi.el ends here