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