* .cvsignore: Add mkinstalldirs entry.
[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
40   '(("new-diaries" . mixi-get-new-diaries)
41     ("new-comments" . mixi-get-new-comments)
42     ("new-bbses" . mixi-get-new-bbses)
43     ("messages" . mixi-get-messages)
44     ("messages.sent" .
45      (lambda (range)
46        (mixi-get-messages 'outbox range)))
47     ("logs" . mixi-get-logs)
48     ("my-diaries" . "/home.pl")
49     ("mixi-el" . "/view_community.pl?id=1596390")
50     ("news.newest.domestic" .
51      (lambda (range)
52        (mixi-get-news 'domestic 'newest range)))
53     ("news.newest.politics" .
54      (lambda (range)
55        (mixi-get-news 'politics 'newest range)))
56     ("news.newest.economy" .
57      (lambda (range)
58        (mixi-get-news 'economy 'newest range)))
59     ("news.newest.area" .
60      (lambda (range)
61        (mixi-get-news 'area 'newest range)))
62     ("news.newest.abroad" .
63      (lambda (range)
64        (mixi-get-news 'abroad 'newest range)))
65     ("news.newest.sports" .
66      (lambda (range)
67        (mixi-get-news 'sports 'newest range)))
68     ("news.newest.entertainment" .
69      (lambda (range)
70        (mixi-get-news 'entertainment 'newest range)))
71     ("news.newest.it" .
72      (lambda (range)
73        (mixi-get-news 'IT 'newest range)))
74     ("news.pickup.domestic" .
75      (lambda (range)
76        (mixi-get-news 'domestic 'pickup range)))
77     ("news.pickup.politics" .
78      (lambda (range)
79        (mixi-get-news 'politics 'pickup range)))
80     ("news.pickup.economy" .
81      (lambda (range)
82        (mixi-get-news 'economy 'pickup range)))
83     ("news.pickup.area" .
84      (lambda (range)
85        (mixi-get-news 'area 'pickup range)))
86     ("news.pickup.abroad" .
87      (lambda (range)
88        (mixi-get-news 'abroad 'pickup range)))
89     ("news.pickup.sports" .
90      (lambda (range)
91        (mixi-get-news 'sports 'pickup range)))
92     ("news.pickup.entertainment" .
93      (lambda (range)
94        (mixi-get-news 'entertainment 'pickup range)))
95     ("news.pickup.it" .
96      (lambda (range)
97        (mixi-get-news 'IT 'pickup range))))
98   "*An alist of mixi shimbun group definition.
99 Each element looks like (NAME . URL) or (NAME . FUNCTION).
100 NAME is a shimbun group name.
101 URL is the URL for mixi access point of the group.  If URL is friend's, get
102 his/her diaries as article.  If community's, get its BBSes.  If diary's or
103 BBS's, get its comments.
104 FUNCTION is the function which has one `range' argument and returns the list
105 of mixi object."
106   :group 'shimbun
107   :type '(repeat (cons :fromat "%v"
108                        (string :tag "Group name")
109                        (radio (string :tag "URL")
110                               (const :tag "New diaries" mixi-get-new-diaries)
111                               (const :tag "New comments" mixi-get-new-comments)
112                               (const :tag "New BBSes" mixi-get-new-bbses)
113                               (const :tag "Messages" mixi-get-messages)
114                               (const :tag "Logs" mixi-get-logs)
115                               (function :tag "Other function")))))
116
117 ;; FIXME: Don't use this user option.
118 (defcustom shimbun-mixi-page-articles 10
119   "*How many articles are there in one page."
120   :group 'shimbun
121   :type 'integer)
122
123 (defvar shimbun-mixi-x-face-alist
124   '(("default" . "X-Face: CY;j#FoBnpK^37`-IoJvN!J^u;GciiPmMQ@T)~RP1]t8iv?v)/bVI:I\"F!JfWJvhM5{zY!=
125  h.d+'g\\I{D>Ocy?Rc4uYUyOZj2%2Kl>,x-!MCSsyi3!L}psrrC1jlF,O?Ui>qf)X;sBz`/}\\066X%$
126  siG'|4K!2?==|oB&#E'5GGH\\#z[muyQ")))
127
128 (defvar shimbun-mixi-reply-to nil)
129
130 (luna-define-method initialize-instance :after ((shimbun shimbun-mixi)
131                                                 &rest init-args)
132   (shimbun-mixi-set-comment-cache-internal shimbun
133                                            (make-hash-table :test 'equal))
134   shimbun)
135
136 (luna-define-method shimbun-close :after ((shimbun shimbun-mixi))
137   (shimbun-mixi-set-comment-cache-internal shimbun nil)
138   (mixi-logout))
139
140 (luna-define-method shimbun-groups ((shimbun shimbun-mixi))
141   (mapcar 'car shimbun-mixi-group-alist))
142
143 (luna-define-method shimbun-reply-to ((shimbun shimbun-mixi))
144   shimbun-mixi-reply-to)
145
146 (defun shimbun-mixi-make-subject (shimbun object)
147   (let ((class (mixi-object-class object)))
148     (cond ((eq class 'mixi-comment)
149            (concat "Re: " (shimbun-mixi-make-subject
150                            shimbun (mixi-comment-parent object))))
151           ((eq class 'mixi-log)
152            (mixi-friend-nick (mixi-log-friend object)))
153           (t
154            (let ((prefix (when (eq class 'mixi-event) "[¥¤¥Ù¥ó¥È]"))
155                  (subject (mixi-object-title object))
156                  (suffix (when (string-match
157                                 "^new-"
158                                 (shimbun-current-group-internal shimbun))
159                            (concat " ("
160                                    (if (eq class 'mixi-diary)
161                                        (mixi-friend-nick
162                                         (mixi-diary-owner object))
163                                      (mixi-community-name
164                                       (mixi-bbs-community object)))
165                                    ")"))))
166              (concat prefix subject suffix))))))
167
168 (defun shimbun-mixi-make-from (object)
169   (let ((class (mixi-object-class object)))
170     (if (eq class 'mixi-news)
171         (mixi-news-media object)
172       (let ((owner (if (eq class 'mixi-log)
173                        (mixi-log-friend object)
174                      (mixi-object-owner object))))
175         (mixi-friend-nick owner)))))
176
177 (defun shimbun-mixi-make-date (object)
178   (let* ((time (mixi-object-time object))
179          (cts (current-time-string time))
180          (day-of-week (substring cts 0 3))
181          (month (substring cts 4 7)))
182     (concat day-of-week ", "
183             (format-time-string "%d" time) " "
184             month " "
185             (format-time-string "%Y %H:%M:%S %z" time))))
186
187 (defun shimbun-mixi-make-message-id (object)
188   (let ((class (mixi-object-class object)))
189     (concat
190      (format-time-string "<%Y%m%d%H%M" (mixi-object-time object)) "."
191      (cond ((eq class 'mixi-comment)
192             (concat (mixi-friend-id (mixi-comment-owner object)) "@"
193                     (mixi-object-id (mixi-comment-parent object)) "."
194                     (mixi-friend-id (mixi-object-owner
195                                      (mixi-comment-parent object))) "."))
196            ((eq class 'mixi-log)
197             (concat (mixi-friend-id (mixi-log-friend object)) "@"))
198            (t
199             (concat (mixi-object-id object) "@"
200                     (if (eq class 'mixi-news)
201                         (mixi-news-media-id object)
202                       (mixi-object-id (mixi-object-owner object))) ".")))
203      (mixi-object-name object) ".mixi.jp>")))
204
205 (defun shimbun-mixi-make-xref (object)
206   (let ((class (mixi-object-class object)))
207     (cond ((eq class 'mixi-diary)
208            (mixi-expand-url (mixi-diary-page object)))
209           ((eq class 'mixi-topic)
210            (mixi-expand-url (mixi-topic-page object)))
211           ((eq class 'mixi-event)
212            (mixi-expand-url (mixi-event-page object)))
213           ((eq class 'mixi-comment)
214            (concat (shimbun-mixi-make-xref (mixi-comment-parent object))
215                    "#comment"))
216           ((eq class 'mixi-message)
217            (mixi-expand-url (mixi-message-page object)))
218           ((eq class 'mixi-news)
219            (mixi-news-page object))
220           ((eq class 'mixi-log)
221            (mixi-expand-url (mixi-friend-page (mixi-log-friend object))))
222           ((eq class 'mixi-friend)
223            (mixi-expand-url (mixi-friend-page object))))))
224
225 (defun shimbun-mixi-make-body (object)
226   (let ((class (mixi-object-class object)))
227     (cond ((eq class 'mixi-event)
228            (let ((limit (mixi-event-limit object)))
229              (setq limit (if limit
230                              (format-time-string "%Yǯ%m·î%dÆü" limit)
231                            "»ØÄê¤Ê¤·"))
232              (concat "<dl>"
233                      "<dt>³«ºÅÆü»þ¡§</dt>"
234                      "<dd>" (mixi-event-date object) "</dd>\n"
235                      "<dt>³«ºÅ¾ì½ê¡§</dt>"
236                      "<dd>" (mixi-event-place object) "</dd>\n"
237                      "<dt>¾ÜºÙ¡§</dt>"
238                      "<dd>" (mixi-event-detail object) "</dd>\n"
239                      "<dt>Ê罸´ü¸Â¡§</dt>"
240                      "<dd>" limit "</dd>\n"
241                      "<dt>»²²Ã¼Ô¡§</dt>"
242                      "<dd>" (mixi-event-members object) "</dd>\n"
243                      "</dl>")))
244           ((eq class 'mixi-friend)
245            (if (mixi-object-realized-p object)
246                (let ((sex (if (eq (mixi-friend-sex object) 'male) "ÃË" "½÷"))
247                      (age (number-to-string (mixi-friend-age object)))
248                      (birthday
249                       (concat (mapconcat (lambda (number)
250                                            (number-to-string number))
251                                          (mixi-friend-birthday object) "·î")
252                               "Æü"))
253                      (blood-type (symbol-name
254                                   (mixi-friend-blood-type object)))
255                      (hobby (mapconcat 'identity
256                                        (mixi-friend-hobby object) ", ")))
257                  (concat "<dl>"
258                          "<dt>̾Á°¡§</dt>"
259                          "<dd>" (mixi-friend-name object) "</dd>\n"
260                          "<dt>À­ÊÌ¡§</dt>"
261                          "<dd>" sex "À­</dd>\n"
262                          "<dt>¸½½»½ê¡§</dt>"
263                          "<dd>" (mixi-friend-address object) "</dd>\n"
264                          "<dt>ǯÎð¡§</dt>"
265                          "<dd>" age "ºÐ</dd>\n"
266                          "<dt>ÃÂÀ¸Æü¡§</dt>"
267                          "<dd>" birthday "</dd>\n"
268                          "<dt>·ì±Õ·¿¡§</dt>"
269                          "<dd>" blood-type "·¿</dd>\n"
270                          "<dt>½Ð¿ÈÃÏ¡§</dt>"
271                          "<dd>" (mixi-friend-birthplace object) "</dd>\n"
272                          "<dt>¼ñÌ£¡§</dt>"
273                          "<dd>" hobby "</dd>\n"
274                          "<dt>¿¦¶È¡§</dt>"
275                          "<dd>" (mixi-friend-job object) "</dd>\n"
276                          "<dt>½ê°¡§</dt>"
277                          "<dd>" (mixi-friend-organization object) "</dd>\n"
278                          "<dt>¼«¸Ê¾Ò²ð¡§</dt>"
279                          "<dd>" (mixi-friend-profile object) "</dd>\n"
280                          "</dl>"))
281              (concat "<a href=\"" (shimbun-mixi-make-xref object)
282                      "\">¥×¥í¥Õ¥£¡¼¥ë¤òɽ¼¨¤¹¤ë</a>")))
283           (t (mixi-object-content object)))))
284
285 (defun shimbun-mixi-make-reply-to (object)
286   (setq shimbun-mixi-reply-to "mixi;")
287   (let ((class (mixi-object-class object)))
288     (setq shimbun-mixi-reply-to
289           (concat
290            (cond ((eq class 'mixi-diary)
291                   (concat shimbun-mixi-reply-to "comment;diary;"
292                           (mixi-friend-id (mixi-diary-owner object)) ";"
293                           (mixi-diary-id object)))
294                  ((mixi-bbs-p object)
295                   (concat shimbun-mixi-reply-to "comment;"
296                           (mixi-object-name object) ";"
297                           (mixi-community-id (mixi-bbs-community object)) ";"
298                           (mixi-bbs-id object)))
299                  ((eq class 'mixi-community)
300                   (concat shimbun-mixi-reply-to "topic;"
301                           (mixi-community-id object)))
302                  ((or (eq class 'mixi-news) (eq object (mixi-make-me)))
303                   (concat shimbun-mixi-reply-to "diary"))
304                  ((eq class 'mixi-message)
305                   (concat shimbun-mixi-reply-to "message;"
306                           (mixi-friend-id (mixi-message-owner object))))
307                  ((or (eq class 'mixi-friend) (eq class 'mixi-log))
308                   (concat shimbun-mixi-reply-to "message;"
309                           (mixi-friend-id object))))))))
310
311 (defun shimbun-mixi-get-headers (shimbun objects &optional range)
312   (when objects
313     (let (headers)
314       (catch 'stop
315         (mapc (lambda (object)
316                 (when (mixi-object-p object)
317                   (let ((class (mixi-object-class object)))
318                     (when (mixi-parent-p object)
319                       (let ((comments (mixi-get-comments object range)))
320                         (mapc (lambda (header)
321                                 (push header headers))
322                               (shimbun-mixi-get-headers shimbun
323                                                         comments))))
324                     (let ((id (shimbun-mixi-make-message-id object)))
325                       (when (and (eq class 'mixi-comment)
326                                  (shimbun-search-id shimbun id))
327                         (throw 'stop nil))
328                       (push
329                        (shimbun-create-header
330                         0
331                         (shimbun-mixi-make-subject shimbun object)
332                         (shimbun-mixi-make-from object)
333                         (shimbun-mixi-make-date object)
334                         id
335                         (if (eq class 'mixi-comment)
336                             (shimbun-mixi-make-message-id
337                              (mixi-comment-parent object))
338                           "")
339                         0 0
340                         (shimbun-mixi-make-xref object))
341                        headers)
342                       (when (eq class 'mixi-comment)
343                         (puthash id (mixi-comment-content object)
344                                  (shimbun-mixi-comment-cache-internal
345                                   shimbun)))))))
346               objects))
347       headers)))
348
349 (luna-define-method shimbun-get-headers ((shimbun shimbun-mixi)
350                                          &optional range)
351   (let ((url-or-function (cdr (assoc (shimbun-current-group-internal shimbun)
352                                      shimbun-mixi-group-alist)))
353         (range (when (integerp range) (* range shimbun-mixi-page-articles)))
354         objects)
355     (if (stringp url-or-function)
356         (let* ((object (mixi-make-object-from-url url-or-function))
357                (class (mixi-object-class object)))
358           (cond ((eq class 'mixi-friend)
359                  (setq objects (mixi-get-diaries object range)))
360                 ((eq class 'mixi-community)
361                  (setq objects (mixi-get-bbses object range)))
362                 ((mixi-parent-p object)
363                  (setq objects (mixi-get-comments object range)))
364                 (t (error (concat (symbol-name class)
365                                   " is not supported yet.")))))
366       (setq objects (funcall url-or-function range)))
367     (shimbun-sort-headers (shimbun-mixi-get-headers shimbun objects range))))
368
369 (defun shimbun-mixi-comment-article (url shimbun header)
370   (let* ((message-id (shimbun-header-id header))
371          (cache (shimbun-mixi-comment-cache-internal shimbun))
372          (article (gethash message-id cache)))
373     (unless (stringp article)
374       (let ((parent (mixi-make-object-from-url url)))
375         (mapc (lambda (comment)
376                 (let ((id (shimbun-mixi-make-message-id comment))
377                       (content (mixi-comment-content comment)))
378                   (puthash id content cache)
379                   (when (string= id message-id)
380                     (setq article content))))
381               (mixi-get-comments parent))))
382     article))
383
384 (luna-define-method shimbun-article ((shimbun shimbun-mixi)
385                                      header &optional outbuf)
386   (when (shimbun-current-group-internal shimbun)
387     (with-current-buffer (or outbuf (current-buffer))
388       (w3m-insert-string
389        (or (with-temp-buffer
390              (let* ((url (shimbun-article-url shimbun header))
391                     (object (mixi-make-object-from-url url))
392                     (article (if (string-match "#comment$" url)
393                                  (shimbun-mixi-comment-article
394                                   url shimbun header)
395                                (shimbun-mixi-make-body object))))
396                (shimbun-mixi-make-reply-to object)
397                (when (stringp article)
398                  (insert article)))
399              (shimbun-message shimbun "shimbun: Make contents...")
400              (goto-char (point-min))
401              (prog1 (shimbun-make-contents shimbun header)
402                (shimbun-message shimbun "shimbun: Make contents...done")))
403            "")))))
404
405 (defconst shimbun-mixi-to-regexp
406   "^mixi;\\([a-z]+\\);?\\([a-z0-9]+\\)?;?\\([0-9]+\\)?;?\\([0-9]+\\)?")
407
408 (defun shimbun-mixi-send-mail (to title content)
409   (when (string-match shimbun-mixi-to-regexp to)
410     (let ((method (match-string 1 to)))
411       (cond ((string= method "comment")
412              (let ((parent (match-string 2 to))
413                    (owner-id (match-string 3 to))
414                    (id (match-string 4 to)))
415                (if (string= parent "diary")
416                    (mixi-post-comment
417                     (mixi-make-diary (mixi-make-friend owner-id) id) content)
418                  (let ((func (intern
419                               (concat mixi-object-prefix "make-" parent))))
420                    (mixi-post-comment
421                     (funcall func (mixi-make-community owner-id) id)
422                     content)))))
423             ((string= method "topic")
424              (mixi-post-topic (mixi-make-community (match-string 2 to))
425                               title content))
426             ((string= method "diary")
427              (mixi-post-diary title content))
428             ((string= method "message")
429              (mixi-post-message (mixi-make-friend (match-string 2 to))
430                                 title content))))))
431
432 (provide 'sb-mixi)
433
434 ;;; sb-mixi.el ends here