* sb-mixi.el (shimbun-mixi-message-id-suffix): New constant.
[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 which has one `range' argument and returns the list
51 of mixi object."
52   :group 'shimbun
53   :type '(repeat (cons :fromat "%v"
54                        (string :tag "Group name")
55                        (radio (string :tag "URL")
56                               (const :tag "New diaries" mixi-get-new-diaries)
57                               (const :tag "New comments" mixi-get-new-comments)
58                               (const :tag "New BBSes" mixi-get-new-bbses)
59                               (const :tag "Messages" mixi-get-messages)
60                               (function :tag "Other function")))))
61
62 ;; FIXME: Don't use this user option.
63 (defcustom shimbun-mixi-page-articles 10
64   "*How many articles are there in one page."
65   :group 'shimbun
66   :type 'integer)
67
68 (defvar shimbun-mixi-x-face-alist
69   '(("default" . "X-Face: CY;j#FoBnpK^37`-IoJvN!J^u;GciiPmMQ@T)~RP1]t8iv?v)/bVI:I\"F!JfWJvhM5{zY!=
70  h.d+'g\\I{D>Ocy?Rc4uYUyOZj2%2Kl>,x-!MCSsyi3!L}psrrC1jlF,O?Ui>qf)X;sBz`/}\\066X%$
71  siG'|4K!2?==|oB&#E'5GGH\\#z[muyQ")))
72
73 (luna-define-method initialize-instance :after ((shimbun shimbun-mixi)
74                                                 &rest init-args)
75   (shimbun-mixi-set-comment-cache-internal shimbun
76                                            (make-hash-table :test 'equal))
77   shimbun)
78
79 (luna-define-method shimbun-close :after ((shimbun shimbun-mixi))
80   (shimbun-mixi-set-comment-cache-internal shimbun nil)
81   (mixi-logout))
82
83 (luna-define-method shimbun-groups ((shimbun shimbun-mixi))
84   (mapcar 'car shimbun-mixi-group-alist))
85
86 (defun shimbun-mixi-make-subject (shimbun object)
87   (let ((class (mixi-object-class object)))
88     (if (eq class 'mixi-comment)
89         (concat "Re: " (shimbun-mixi-make-subject
90                         shimbun (mixi-comment-parent object)))
91       (let ((prefix (when (eq class 'mixi-event) "[¥¤¥Ù¥ó¥È]"))
92             (subject (mixi-object-title object))
93             (suffix (when (string-match
94                            "^new-" (shimbun-current-group-internal shimbun))
95                       (concat " ("
96                               (if (eq class 'mixi-diary)
97                                   (mixi-friend-nick
98                                    (mixi-diary-owner object))
99                                 (mixi-community-name
100                                  (mixi-bbs-community object)))
101                               ")"))))
102         (concat prefix subject suffix)))))
103
104 (defun shimbun-mixi-make-from (object)
105   (let ((owner (mixi-object-owner object)))
106     (mixi-friend-nick owner)))
107
108 (defun shimbun-mixi-make-date (object)
109   (let* ((time (mixi-object-time object))
110          (cts (current-time-string time))
111          (day-of-week (substring cts 0 3))
112          (month (substring cts 4 7)))
113     (concat day-of-week ", "
114             (format-time-string "%d" time) " "
115             month " "
116             (format-time-string "%Y %H:%M:%S %z" time))))
117
118 (defconst shimbun-mixi-message-id-suffix "@mixi.jp")
119
120 (defun shimbun-mixi-make-message-id (object)
121   "Make message-id for OBJECT.
122 If OBJECT is comment, message-id is like follow:
123
124   <date$owner-id.owner-class$parent-id.parent-class$parent-owner-id.
125    parent-owner-class@mixi.jp>
126
127 If OBJECT is diary or BBS or message:
128
129   <date$id.class$owner-id.owner-class@mixi.jp>
130
131 If OBJECT is log:
132
133   <date$id.class@mixi.jp>
134
135 The others:
136
137   <id.class@mixi.jp>"
138   (let ((class (mixi-object-class object)))
139     (concat "<"
140             (unless (or (eq class 'mixi-friend) (eq class 'mixi-community))
141               (format-time-string "%Y%m%d%H%M." (mixi-object-time object)))
142             (mapconcat (lambda (object)
143                          (concat (mixi-object-id object)
144                                  "." (mixi-object-name object)))
145                        (cond ((eq class 'mixi-comment)
146                               (let ((parent (mixi-comment-parent object)))
147                                 (list (mixi-comment-owner object)
148                                       parent
149                                       (if (eq (mixi-object-class parent)
150                                               'mixi-diary)
151                                           (mixi-object-owner parent)
152                                         (mixi-bbs-community parent)))))
153                              ((or (eq class 'mixi-diary)
154                                   (eq class 'mixi-message))
155                               (list object (mixi-object-owner object)))
156                              ((mixi-bbs-p object)
157                               (list object (mixi-bbs-community object)))
158                              ((eq class 'mixi-log)
159                               (list (mixi-log-friend object)))
160                              (t
161                               (list object)))
162                        "$")
163             shimbun-mixi-message-id-suffix ">")))
164
165 (defun shimbun-mixi-make-xref (object)
166   (let ((class (mixi-object-class object)))
167     (cond ((eq class 'mixi-diary)
168            (mixi-expand-url (mixi-diary-page object)))
169           ((eq class 'mixi-topic)
170            (mixi-expand-url (mixi-topic-page object)))
171           ((eq class 'mixi-event)
172            (mixi-expand-url (mixi-event-page object)))
173           ((eq class 'mixi-comment)
174            (concat (shimbun-mixi-make-xref (mixi-comment-parent object))
175                    "#comment"))
176           ((eq class 'mixi-message)
177            (mixi-expand-url (mixi-message-page object))))))
178
179 (defun shimbun-mixi-make-body (object)
180   (let ((class (mixi-object-class object)))
181     (cond ((eq class 'mixi-event)
182            (let ((limit (mixi-event-limit object)))
183              (setq limit (if limit
184                              (format-time-string "%Yǯ%m·î%dÆü" limit)
185                            "»ØÄê¤Ê¤·"))
186              (concat "<dl><dt>³«ºÅÆü»þ¡§</dt>"
187                      "<dd>" (mixi-event-date object) "</dd>"
188                      "<dt>³«ºÅ¾ì½ê¡§</dt>"
189                      "<dd>" (mixi-event-place object) "</dd>"
190                      "<dt>¾ÜºÙ¡§</dt>"
191                      "<dd>" (mixi-event-detail object) "</dd>"
192                      "<dt>Ê罸´ü¸Â¡§</dt>"
193                      "<dd>" limit "</dd>"
194                      "<dt>»²²Ã¼Ô¡§</dt>"
195                      "<dd>" (mixi-event-members object) "</dd></dl>")))
196           (t (mixi-object-content object)))))
197
198 (defun shimbun-mixi-get-headers (shimbun objects &optional range)
199   (when objects
200     (let (headers)
201       (catch 'stop
202         (mapc (lambda (object)
203                 (when (mixi-object-p object)
204                   (let ((class (mixi-object-class object))
205                         (id (shimbun-mixi-make-message-id object)))
206                     (push
207                      (shimbun-create-header
208                       0
209                       (shimbun-mixi-make-subject shimbun object)
210                       (shimbun-mixi-make-from object)
211                       (shimbun-mixi-make-date object)
212                       id
213                       (if (eq class 'mixi-comment)
214                           (shimbun-mixi-make-message-id
215                            (mixi-comment-parent object))
216                         "")
217                       0 0
218                       (shimbun-mixi-make-xref object))
219                      headers)
220                     (when (or (eq class 'mixi-diary)
221                               (mixi-bbs-p object))
222                       (let ((comments (mixi-get-comments object range)))
223                         (mapc (lambda (header)
224                                 (push header headers))
225                               (shimbun-mixi-get-headers shimbun
226                                                         comments))))
227                     (when (eq class 'mixi-comment)
228                       (puthash id (mixi-comment-content object)
229                                (shimbun-mixi-comment-cache-internal
230                                 shimbun))))))
231               objects))
232       headers)))
233
234 (luna-define-method shimbun-get-headers ((shimbun shimbun-mixi)
235                                          &optional range)
236   (let ((url-or-function (cdr (assoc (shimbun-current-group-internal shimbun)
237                                      shimbun-mixi-group-alist)))
238         (range (when (integerp range) (* range shimbun-mixi-page-articles)))
239         objects)
240     (if (stringp url-or-function)
241         (let* ((object (mixi-make-object-from-url url-or-function))
242                (class (mixi-object-class object)))
243           (cond ((eq class 'mixi-friend)
244                  (setq objects (mixi-get-diaries object range)))
245                 ((eq class 'mixi-community)
246                  (setq objects (mixi-get-bbses object range)))
247                 ((or (eq class 'mixi-diary)
248                      (mixi-bbs-p object))
249                  (setq objects (mixi-get-comments object range)))
250                 (t (error (concat (symbol-name class)
251                                   " is not supported yet.")))))
252       (setq objects (funcall url-or-function range)))
253     (shimbun-sort-headers (shimbun-mixi-get-headers shimbun objects range))))
254
255 (defun shimbun-mixi-comment-article (url shimbun header)
256   (let* ((message-id (shimbun-header-id header))
257          (cache (shimbun-mixi-comment-cache-internal shimbun))
258          (article (gethash message-id cache)))
259     (unless (stringp article)
260       (let ((parent (mixi-make-object-from-url url)))
261         (mapc (lambda (comment)
262                 (let ((id (shimbun-mixi-make-message-id comment))
263                       (content (mixi-comment-content comment)))
264                   (puthash id content cache)
265                   (when (string= id message-id)
266                     (setq article content))))
267               (mixi-get-comments parent))))
268     article))
269
270 (luna-define-method shimbun-article ((shimbun shimbun-mixi)
271                                      header &optional outbuf)
272   (when (shimbun-current-group-internal shimbun)
273     (with-current-buffer (or outbuf (current-buffer))
274       (w3m-insert-string
275        (or (with-temp-buffer
276              (let* ((url (shimbun-article-url shimbun header))
277                     (article (if (string-match "#comment$" url)
278                                  (shimbun-mixi-comment-article
279                                   url shimbun header)
280                                (shimbun-mixi-make-body
281                                 (mixi-make-object-from-url url)))))
282                (when (stringp article)
283                  (insert article)))
284              (shimbun-message shimbun "shimbun: Make contents...")
285              (goto-char (point-min))
286              (prog1 (shimbun-make-contents shimbun header)
287                (shimbun-message shimbun "shimbun: Make contents...done")))
288            "")))))
289
290 (defun shimbun-mixi-make-object-from-message-id (message-id)
291   (when (string-match (concat "^<\\(.+\\)"
292                               (regexp-quote shimbun-mixi-message-id-suffix)
293                               ">$") message-id)
294     (let ((parts (reverse (split-string (match-string 1 message-id) "\\$")))
295           object parent comment)
296       (catch 'stop
297         (mapc (lambda (part)
298                 (if (string-match "\\([a-z0-9]+\\)\\.\\([a-z]+\\)$" part)
299                     (let ((id (match-string 1 part))
300                           (func (intern (concat
301                                          mixi-object-prefix
302                                          "make-" (match-string 2 part)))))
303                       (cond ((null object)
304                              (setq object (funcall func id)))
305                             ((null parent)
306                              (setq parent (funcall func object id)))
307                             ((null comment)
308                              (setq comment
309                                    (mixi-make-comment parent
310                                                       (mixi-make-friend id)
311                                                       nil "dummy")))))
312                   (throw 'stop nil)))
313               parts))
314       (or comment parent object))))
315
316 (provide 'sb-mixi)
317
318 ;;; sb-mixi.el ends here