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