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