Add `noticebox'.
[elisp/mixi.git] / sb-mixi.el
1 ;;; sb-mixi.el --- shimbun backend for mixi
2
3 ;; Copyright (C) 2006, 2007, 2008 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 GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; 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 'mixi-utils)
34 (require 'shimbun)
35
36 (defconst shimbun-mixi-revision "$Revision: 1.65 $")
37
38 (eval-and-compile
39   (luna-define-class shimbun-mixi (shimbun) (comment-cache release-cache))
40   (luna-define-internal-accessors 'shimbun-mixi))
41
42 (defconst shimbun-mixi-default-group-alist
43   '(("new-diaries" . mixi-get-new-diaries)
44     ("new-echoes" . mixi-get-new-echoes)
45     ("new-comments" . mixi-get-new-comments)
46     ("new-bbses" . mixi-get-new-bbses)
47     ("new-bbs-comments" . mixi-get-new-bbs-comments)
48     ("messages" . mixi-get-messages)
49     ("messages.sent" .
50      (lambda (range)
51        (mixi-get-messages 'outbox range)))
52     ("messages.notice" .
53      (lambda (range)
54        (mixi-get-messages 'noticebox range)))
55     ("logs" . mixi-get-logs)
56     ("my-diaries" . "/home.pl")
57     ("mixi-el" . "/view_community.pl?id=1596390")
58     ("news.newest.domestic" .
59      (lambda (range)
60        (mixi-get-news 'domestic 'newest range)))
61     ("news.newest.politics" .
62      (lambda (range)
63        (mixi-get-news 'politics 'newest range)))
64     ("news.newest.economy" .
65      (lambda (range)
66        (mixi-get-news 'economy 'newest range)))
67     ("news.newest.area" .
68      (lambda (range)
69        (mixi-get-news 'area 'newest range)))
70     ("news.newest.abroad" .
71      (lambda (range)
72        (mixi-get-news 'abroad 'newest range)))
73     ("news.newest.sports" .
74      (lambda (range)
75        (mixi-get-news 'sports 'newest range)))
76     ("news.newest.entertainment" .
77      (lambda (range)
78        (mixi-get-news 'entertainment 'newest range)))
79     ("news.newest.it" .
80      (lambda (range)
81        (mixi-get-news 'IT 'newest range)))
82     ("news.newest.game-anime" .
83      (lambda (range)
84        (mixi-get-news 'game-anime 'newest range)))
85     ("news.newest.column" .
86      (lambda (range)
87        (mixi-get-news 'column 'newest range)))
88     ("news.pickup.domestic" .
89      (lambda (range)
90        (mixi-get-news 'domestic 'pickup range)))
91     ("news.pickup.politics" .
92      (lambda (range)
93        (mixi-get-news 'politics 'pickup range)))
94     ("news.pickup.economy" .
95      (lambda (range)
96        (mixi-get-news 'economy 'pickup range)))
97     ("news.pickup.area" .
98      (lambda (range)
99        (mixi-get-news 'area 'pickup range)))
100     ("news.pickup.abroad" .
101      (lambda (range)
102        (mixi-get-news 'abroad 'pickup range)))
103     ("news.pickup.sports" .
104      (lambda (range)
105        (mixi-get-news 'sports 'pickup range)))
106     ("news.pickup.entertainment" .
107      (lambda (range)
108        (mixi-get-news 'entertainment 'pickup range)))
109     ("news.pickup.it" .
110      (lambda (range)
111        (mixi-get-news 'IT 'pickup range)))
112     ("news.pickup.game-anime" .
113      (lambda (range)
114        (mixi-get-news 'game-anime 'pickup range)))
115     ("news.pickup.column" .
116      (lambda (range)
117        (mixi-get-news 'column 'pickup range)))
118     ("releases" . mixi-get-releases))
119   "An alist of mixi shimbun group default definition.")
120
121 (defcustom shimbun-mixi-group-alist nil
122   "*An alist of mixi shimbun group definition.
123 Each element looks like (NAME . URL) or (NAME . FUNCTION).
124 NAME is a shimbun group name that you can name it as you like.
125 URL is the URL for mixi access point of the group.  If URL is friend's, get
126 his/her diaries as article.  If community's, get its BBSes.  If diary's or
127 BBS's, get its comments.
128 FUNCTION is the function which has one `range' argument and returns the list
129 of mixi object."
130   :group 'shimbun
131   :type '(repeat (cons :fromat "%v"
132                        (string :tag "Group name")
133                        (radio (string :tag "URL")
134                               (function :tag "Function")))))
135
136 (defcustom shimbun-mixi-get-profile-with-diary t
137   "*If non-nil, get his/her profile together with diaries."
138   :group 'shimbun
139   :type 'boolean)
140
141 (defcustom shimbun-mixi-add-comment-count-to-author t
142   "*If non-nil, add comment count to author."
143   :group 'shimbun
144   :type 'boolean)
145
146 ;; FIXME: Don't use this user option.
147 (defcustom shimbun-mixi-page-articles 10
148   "*How many articles are there in one page."
149   :group 'shimbun
150   :type 'integer)
151
152 (defvar shimbun-mixi-x-face-alist
153   '(("default" . "X-Face: CY;j#FoBnpK^37`-IoJvN!J^u;GciiPmMQ@T)~RP1]t8iv?v)/bVI:I\"F!JfWJvhM5{zY!=
154  h.d+'g\\I{D>Ocy?Rc4uYUyOZj2%2Kl>,x-!MCSsyi3!L}psrrC1jlF,O?Ui>qf)X;sBz`/}\\066X%$
155  siG'|4K!2?==|oB&#E'5GGH\\#z[muyQ")))
156
157 (defun shimbun-mixi-group-alist ()
158   (append shimbun-mixi-default-group-alist
159           shimbun-mixi-group-alist))
160
161 (luna-define-method initialize-instance :after ((shimbun shimbun-mixi)
162                                                 &rest init-args)
163   (shimbun-mixi-set-comment-cache-internal shimbun
164                                            (make-hash-table :test 'equal))
165   (shimbun-mixi-set-release-cache-internal shimbun
166                                            (make-hash-table :test 'equal))
167   shimbun)
168
169 (luna-define-method shimbun-close :after ((shimbun shimbun-mixi))
170   (shimbun-mixi-set-comment-cache-internal shimbun nil)
171   (shimbun-mixi-set-release-cache-internal shimbun nil)
172   (mixi-logout))
173
174 (luna-define-method shimbun-groups ((shimbun shimbun-mixi))
175   (mapcar 'car (shimbun-mixi-group-alist)))
176
177 (luna-define-method shimbun-reply-to ((shimbun shimbun-mixi))
178   mixi-reply-to)
179
180 (defun shimbun-mixi-get-headers (shimbun objects &optional range)
181   (let (headers)
182     (catch 'stop
183       (while objects
184         (let ((object (car objects)))
185           (when (and shimbun-mixi-get-profile-with-diary
186                      (mixi-diary-p object))
187             (let ((owner (mixi-diary-owner object)))
188               (unless (mixi-object-realized-p owner)
189                 (mixi-realize-friend owner))))
190           (when (mixi-parent-p object)
191             (let* ((comments (mixi-get-comments object range))
192                    (comment-headers (shimbun-mixi-get-headers shimbun
193                                                               comments)))
194               (while comment-headers
195                 (push (car comment-headers) headers)
196                 (setq comment-headers (cdr comment-headers)))))
197           (let ((class (mixi-object-class object))
198                 (id (mixi-make-message-id object)))
199             (when (and (eq class 'mixi-comment)
200                        (shimbun-search-id shimbun id))
201               (throw 'stop nil))
202             (push
203              (shimbun-create-header
204               0
205               (mixi-make-title object (string-match
206                                        "^new-"
207                                        (shimbun-current-group-internal
208                                         shimbun)))
209               (mixi-make-author object shimbun-mixi-add-comment-count-to-author)
210               (mixi-make-date object)
211               id
212               (if (eq class 'mixi-comment)
213                   (mixi-make-message-id (mixi-comment-parent object))
214                 "")
215               0 0
216               (mixi-make-url object))
217              headers)
218             (when (eq class 'mixi-comment)
219               (puthash id (mixi-comment-content object)
220                        (shimbun-mixi-comment-cache-internal shimbun)))))
221         (setq objects (cdr objects))))
222     headers))
223
224 (luna-define-method shimbun-get-headers ((shimbun shimbun-mixi)
225                                          &optional range)
226   (let ((url-or-function (cdr (assoc (shimbun-current-group-internal shimbun)
227                                      (shimbun-mixi-group-alist))))
228         (range (shimbun-header-index-pages range)))
229     (when (integerp range)
230       (setq range (* range shimbun-mixi-page-articles)))
231     (shimbun-sort-headers
232      (shimbun-mixi-get-headers shimbun
233                                (mixi-make-objects url-or-function range)
234                                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              (comments (mixi-get-comments parent)))
243         (while comments
244           (let ((id (mixi-make-message-id (car comments)))
245                 (content (mixi-comment-content (car comments))))
246             (puthash id content cache)
247             (when (string= id message-id)
248               (setq article content)))
249           (setq comments (cdr comments)))))
250     article))
251
252 (defun shimbun-mixi-release-article (shimbun header)
253   (let* ((message-id (shimbun-header-id header))
254          (cache (shimbun-mixi-release-cache-internal shimbun))
255          (article (gethash message-id cache)))
256     (unless (stringp article)
257       (let ((releases (mixi-get-releases)))
258         (while releases
259           (let ((id (mixi-make-message-id (car releases)))
260                 (content (mixi-release-content (car releases))))
261             (puthash id content cache)
262             (when (string= id message-id)
263               (setq article content)))
264           (setq releases (cdr releases)))))
265     article))
266
267 (luna-define-method shimbun-article ((shimbun shimbun-mixi)
268                                      header &optional outbuf)
269   (when (shimbun-current-group-internal shimbun)
270     (with-current-buffer (or outbuf (current-buffer))
271       (w3m-insert-string
272        (or (with-temp-buffer
273              (let* ((url (shimbun-article-url shimbun header))
274                     (object (mixi-make-object-from-url url))
275                     (article (cond ((string-match "#comment$" url)
276                                     (shimbun-mixi-comment-article
277                                      url shimbun header))
278                                    ((string-match "release_info.pl$" url)
279                                     (shimbun-mixi-release-article
280                                      shimbun header))
281                                    (t
282                                     (mixi-make-content object)))))
283                (mixi-make-reply-to object)
284                (when (stringp article)
285                  (insert article)))
286              (shimbun-message shimbun "shimbun: Make contents...")
287              (goto-char (point-min))
288              (prog1 (shimbun-make-contents shimbun header)
289                (shimbun-message shimbun "shimbun: Make contents...done")))
290            "")))))
291
292 (provide 'sb-mixi)
293
294 ;;; sb-mixi.el ends here