* mixi.el (mixi-get-friends): Rename ARGS argument to FRIEND-OR-RANGE.
[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 (defcustom shimbun-mixi-get-comment-p t
69   "*If non-nil, get diaries or BBSes together with its comments."
70   :group 'shimbun
71   :type 'boolean)
72
73 (defcustom shimbun-mixi-logout-p nil
74   "*If non-ni, logout from mixi when shimbun server was closed."
75   :group 'shimbun
76   :type 'boolean)
77
78 (defvar shimbun-mixi-x-face-alist
79   '(("default" . "X-Face: CY;j#FoBnpK^37`-IoJvN!J^u;GciiPmMQ@T)~RP1]t8iv?v)/bVI:I\"F!JfWJvhM5{zY!=
80  h.d+'g\\I{D>Ocy?Rc4uYUyOZj2%2Kl>,x-!MCSsyi3!L}psrrC1jlF,O?Ui>qf)X;sBz`/}\\066X%$
81  siG'|4K!2?==|oB&#E'5GGH\\#z[muyQ")))
82
83 (luna-define-method initialize-instance :after ((shimbun shimbun-mixi)
84                                                 &rest init-args)
85   (shimbun-mixi-set-comment-cache-internal shimbun
86                                            (make-hash-table :test 'equal))
87   shimbun)
88
89 (luna-define-method shimbun-close :after ((shimbun shimbun-mixi))
90   (shimbun-mixi-set-comment-cache-internal shimbun nil)
91   (when shimbun-mixi-logout-p
92     (mixi-logout)))
93
94 (luna-define-method shimbun-groups ((shimbun shimbun-mixi))
95   (mapcar 'car shimbun-mixi-group-alist))
96
97 (defun shimbun-mixi-make-subject (shimbun object)
98   (let ((class (mixi-object-class object)))
99     (if (eq class 'mixi-comment)
100         (concat "Re: " (shimbun-mixi-make-subject
101                         shimbun (mixi-comment-parent object)))
102       (let ((prefix (when (eq class 'mixi-event) "[¥¤¥Ù¥ó¥È]"))
103             (subject (mixi-object-title object))
104             (suffix (when (string-match
105                            "^new-" (shimbun-current-group-internal shimbun))
106                       (concat " ("
107                               (if (eq class 'mixi-diary)
108                                   (mixi-friend-nick
109                                    (mixi-diary-owner object))
110                                 (mixi-community-name
111                                  (mixi-bbs-community object)))
112                               ")"))))
113         (concat prefix subject suffix)))))
114
115 (defun shimbun-mixi-make-from (object)
116   (let ((owner (mixi-object-owner object)))
117     (mixi-friend-nick owner)))
118
119 (defun shimbun-mixi-make-date (object)
120   (let* ((time (mixi-object-time object))
121          (cts (current-time-string time))
122          (day-of-week (substring cts 0 3))
123          (month (substring cts 4 7)))
124     (concat day-of-week ", "
125             (format-time-string "%d" time) " "
126             month " "
127             (format-time-string "%Y %H:%M:%S %z" time))))
128
129 (defun shimbun-mixi-make-message-id (object)
130   (let ((class (mixi-object-class object)))
131     (concat "<"
132             (format-time-string "%Y%m%d%H%M" (mixi-object-time object)) "."
133             (if (eq class 'mixi-comment)
134                 (concat (mixi-friend-id (mixi-comment-owner object)) "@"
135                         (mixi-object-id (mixi-comment-parent object)) "."
136                         (mixi-friend-id (mixi-object-owner
137                                          (mixi-comment-parent object))) ".")
138               (concat (mixi-object-id object) "@"
139                       (mixi-object-id (mixi-object-owner object)) "."))
140             (mixi-object-name object) ".mixi.jp"
141             ">")))
142
143 (defun shimbun-mixi-make-xref (object)
144   (let ((class (mixi-object-class object)))
145     (cond ((eq class 'mixi-diary)
146            (mixi-expand-url (mixi-diary-page object)))
147           ((eq class 'mixi-topic)
148            (mixi-expand-url (mixi-topic-page object)))
149           ((eq class 'mixi-event)
150            (mixi-expand-url (mixi-event-page object)))
151           ((eq class 'mixi-comment)
152            (concat (shimbun-mixi-make-xref (mixi-comment-parent object))
153                    "#comment"))
154           ((eq class 'mixi-message)
155            (mixi-expand-url (mixi-message-page object))))))
156
157 (defun shimbun-mixi-make-body (object)
158   (let ((class (mixi-object-class object)))
159     (cond ((eq class 'mixi-event)
160            (let ((limit (mixi-event-limit object)))
161              (setq limit (if limit
162                              (format-time-string "%Yǯ%m·î%dÆü" limit)
163                            "»ØÄê¤Ê¤·"))
164              (concat "<dl><dt>³«ºÅÆü»þ¡§</dt>"
165                      "<dd>" (mixi-event-date object) "</dd>"
166                      "<dt>³«ºÅ¾ì½ê¡§</dt>"
167                      "<dd>" (mixi-event-place object) "</dd>"
168                      "<dt>¾ÜºÙ¡§</dt>"
169                      "<dd>" (mixi-event-detail object) "</dd>"
170                      "<dt>Ê罸´ü¸Â¡§</dt>"
171                      "<dd>" limit "</dd>"
172                      "<dt>»²²Ã¼Ô¡§</dt>"
173                      "<dd>" (mixi-event-members object) "</dd></dl>")))
174           (t (mixi-object-content object)))))
175
176 (defun shimbun-mixi-get-headers (shimbun objects &optional range)
177   (when objects
178     (let (headers)
179       (catch 'stop
180         (mapc (lambda (object)
181                 (when (mixi-object-p object)
182                   (let ((class (mixi-object-class object))
183                         (id (shimbun-mixi-make-message-id object)))
184                     (push
185                      (shimbun-create-header
186                       0
187                       (shimbun-mixi-make-subject shimbun object)
188                       (shimbun-mixi-make-from object)
189                       (shimbun-mixi-make-date object)
190                       id
191                       (if (eq class 'mixi-comment)
192                           (shimbun-mixi-make-message-id
193                            (mixi-comment-parent object))
194                         "")
195                       0 0
196                       (shimbun-mixi-make-xref object))
197                      headers)
198                     (when (and shimbun-mixi-get-comment-p
199                                (or (eq class 'mixi-diary)
200                                    (eq class 'mixi-topic)
201                                    (eq class 'mixi-event)))
202                       (let ((comments (mixi-get-comments object range)))
203                         (mapc (lambda (header)
204                                 (push header headers))
205                               (shimbun-mixi-get-headers shimbun
206                                                         comments))))
207                     (when (eq class 'mixi-comment)
208                       (puthash id (mixi-comment-content object)
209                                (shimbun-mixi-comment-cache-internal
210                                 shimbun))))))
211               objects))
212       headers)))
213
214 (luna-define-method shimbun-get-headers ((shimbun shimbun-mixi)
215                                          &optional range)
216   (let ((url-or-function (cdr (assoc (shimbun-current-group-internal shimbun)
217                                      shimbun-mixi-group-alist)))
218         (range (when (integerp range) (* range shimbun-mixi-page-articles)))
219         objects)
220     (if (stringp url-or-function)
221         (let* ((object (mixi-make-object-from-url url-or-function))
222                (class (mixi-object-class object)))
223           (cond ((eq class 'mixi-friend)
224                  (setq objects (mixi-get-diaries object range)))
225                 ((eq class 'mixi-community)
226                  (setq objects (mixi-get-bbses object range)))
227                 ((or (eq class 'mixi-diary)
228                      (eq class 'mixi-topic)
229                      (eq class 'mixi-event))
230                  (setq objects (mixi-get-comments object range)))
231                 (t (error (concat (symbol-name class)
232                                   " is not supported yet.")))))
233       (setq objects (funcall url-or-function range)))
234     (shimbun-sort-headers (shimbun-mixi-get-headers shimbun objects range))))
235
236 (defun shimbun-mixi-comment-article (url shimbun header)
237   (let* ((message-id (shimbun-header-id header))
238          (cache (shimbun-mixi-comment-cache-internal shimbun))
239          (article (gethash message-id cache)))
240     (unless (stringp article)
241       (let ((parent (mixi-make-object-from-url url)))
242         (mapc (lambda (comment)
243                 (let ((id (shimbun-mixi-make-message-id comment))
244                       (content (mixi-comment-content comment)))
245                   (puthash id content cache)
246                   (when (string= id message-id)
247                     (setq article content))))
248               (mixi-get-comments parent))))
249     article))
250
251 (luna-define-method shimbun-article ((shimbun shimbun-mixi)
252                                      header &optional outbuf)
253   (when (shimbun-current-group-internal shimbun)
254     (with-current-buffer (or outbuf (current-buffer))
255       (w3m-insert-string
256        (or (with-temp-buffer
257              (let* ((url (shimbun-article-url shimbun header))
258                     (article (if (string-match "#comment$" url)
259                                  (shimbun-mixi-comment-article
260                                   url shimbun header)
261                                (shimbun-mixi-make-body
262                                 (mixi-make-object-from-url url)))))
263                (when (stringp article)
264                  (insert article)))
265              (shimbun-message shimbun "shimbun: Make contents...")
266              (goto-char (point-min))
267              (prog1 (shimbun-make-contents shimbun header)
268                (shimbun-message shimbun "shimbun: Make contents...done")))
269            "")))))
270
271 (provide 'sb-mixi)
272
273 ;;; sb-mixi.el ends here