* mixi.el (mixi-make-object-from-url): Support friend object.
[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                               (function :tag "Other function")))))
115
116 ;; FIXME: Don't use this user option.
117 (defcustom shimbun-mixi-page-articles 10
118   "*How many articles are there in one page."
119   :group 'shimbun
120   :type 'integer)
121
122 (defvar shimbun-mixi-x-face-alist
123   '(("default" . "X-Face: CY;j#FoBnpK^37`-IoJvN!J^u;GciiPmMQ@T)~RP1]t8iv?v)/bVI:I\"F!JfWJvhM5{zY!=
124  h.d+'g\\I{D>Ocy?Rc4uYUyOZj2%2Kl>,x-!MCSsyi3!L}psrrC1jlF,O?Ui>qf)X;sBz`/}\\066X%$
125  siG'|4K!2?==|oB&#E'5GGH\\#z[muyQ")))
126
127 (defvar shimbun-mixi-reply-to nil)
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   shimbun-mixi-reply-to)
144
145 (defun shimbun-mixi-make-subject (shimbun object)
146   (let ((class (mixi-object-class object)))
147     (cond ((eq class 'mixi-comment)
148            (concat "Re: " (shimbun-mixi-make-subject
149                            shimbun (mixi-comment-parent object))))
150           ((eq class 'mixi-log)
151            (mixi-friend-nick (mixi-log-friend object)))
152           (t
153            (let ((prefix (when (eq class 'mixi-event) "[¥¤¥Ù¥ó¥È]"))
154                  (subject (mixi-object-title object))
155                  (suffix (when (string-match
156                                 "^new-"
157                                 (shimbun-current-group-internal shimbun))
158                            (concat " ("
159                                    (if (eq class 'mixi-diary)
160                                        (mixi-friend-nick
161                                         (mixi-diary-owner object))
162                                      (mixi-community-name
163                                       (mixi-bbs-community object)))
164                                    ")"))))
165              (concat prefix subject suffix))))))
166
167 (defun shimbun-mixi-make-from (object)
168   (let ((class (mixi-object-class object)))
169     (if (eq class 'mixi-news)
170         (mixi-news-media object)
171       (let ((owner (if (eq class 'mixi-log)
172                        (mixi-log-friend object)
173                      (mixi-object-owner object))))
174         (mixi-friend-nick owner)))))
175
176 (defun shimbun-mixi-make-date (object)
177   (let* ((time (mixi-object-time object))
178          (cts (current-time-string time))
179          (day-of-week (substring cts 0 3))
180          (month (substring cts 4 7)))
181     (concat day-of-week ", "
182             (format-time-string "%d" time) " "
183             month " "
184             (format-time-string "%Y %H:%M:%S %z" time))))
185
186 (defun shimbun-mixi-make-message-id (object)
187   (let ((class (mixi-object-class object)))
188     (concat
189      (format-time-string "<%Y%m%d%H%M" (mixi-object-time object)) "."
190      (cond ((eq class 'mixi-comment)
191             (concat (mixi-friend-id (mixi-comment-owner object)) "@"
192                     (mixi-object-id (mixi-comment-parent object)) "."
193                     (mixi-friend-id (mixi-object-owner
194                                      (mixi-comment-parent object))) "."))
195            ((eq class 'mixi-log)
196             (concat (mixi-friend-id (mixi-log-friend object)) "@"))
197            (t
198             (concat (mixi-object-id object) "@"
199                     (if (eq class 'mixi-news)
200                         (mixi-news-media-id object)
201                       (mixi-object-id (mixi-object-owner object))) ".")))
202      (mixi-object-name object) ".mixi.jp>")))
203
204 (defun shimbun-mixi-make-xref (object)
205   (let ((class (mixi-object-class object)))
206     (cond ((eq class 'mixi-diary)
207            (mixi-expand-url (mixi-diary-page object)))
208           ((eq class 'mixi-topic)
209            (mixi-expand-url (mixi-topic-page object)))
210           ((eq class 'mixi-event)
211            (mixi-expand-url (mixi-event-page object)))
212           ((eq class 'mixi-comment)
213            (concat (shimbun-mixi-make-xref (mixi-comment-parent object))
214                    "#comment"))
215           ((eq class 'mixi-message)
216            (mixi-expand-url (mixi-message-page object)))
217           ((eq class 'mixi-news)
218            (mixi-news-page object))
219           ((eq class 'mixi-log)
220            (mixi-expand-url (mixi-friend-page (mixi-log-friend object))))
221           ((eq class 'mixi-friend)
222            (mixi-expand-url (mixi-friend-page object))))))
223
224 (defun shimbun-mixi-make-body (object)
225   (let ((class (mixi-object-class object)))
226     (cond ((eq class 'mixi-event)
227            (let ((limit (mixi-event-limit object)))
228              (setq limit (if limit
229                              (format-time-string "%Yǯ%m·î%dÆü" limit)
230                            "»ØÄê¤Ê¤·"))
231              (concat "<dl>"
232                      "<dt>³«ºÅÆü»þ¡§</dt>"
233                      "<dd>" (mixi-event-date object) "</dd>\n"
234                      "<dt>³«ºÅ¾ì½ê¡§</dt>"
235                      "<dd>" (mixi-event-place object) "</dd>\n"
236                      "<dt>¾ÜºÙ¡§</dt>"
237                      "<dd>" (mixi-event-detail object) "</dd>\n"
238                      "<dt>Ê罸´ü¸Â¡§</dt>"
239                      "<dd>" limit "</dd>\n"
240                      "<dt>»²²Ã¼Ô¡§</dt>"
241                      "<dd>" (mixi-event-members object) "</dd>\n"
242                      "</dl>")))
243           ((eq class 'mixi-friend)
244            (if (mixi-object-realized-p object)
245                (let ((sex (if (eq (mixi-friend-sex object) 'male) "ÃË" "½÷"))
246                      (age (number-to-string (mixi-friend-age object)))
247                      (birthday
248                       (concat (mapconcat (lambda (number)
249                                            (number-to-string number))
250                                          (mixi-friend-birthday object) "·î")
251                               "Æü"))
252                      (blood-type (symbol-name
253                                   (mixi-friend-blood-type object)))
254                      (hobby (mapconcat 'identity
255                                        (mixi-friend-hobby object) ", ")))
256                  (concat "<dl>"
257                          "<dt>̾Á°¡§</dt>"
258                          "<dd>" (mixi-friend-name object) "</dd>\n"
259                          "<dt>À­ÊÌ¡§</dt>"
260                          "<dd>" sex "À­</dd>\n"
261                          "<dt>¸½½»½ê¡§</dt>"
262                          "<dd>" (mixi-friend-address object) "</dd>\n"
263                          "<dt>ǯÎð¡§</dt>"
264                          "<dd>" age "ºÐ</dd>\n"
265                          "<dt>ÃÂÀ¸Æü¡§</dt>"
266                          "<dd>" birthday "</dd>\n"
267                          "<dt>·ì±Õ·¿¡§</dt>"
268                          "<dd>" blood-type "·¿</dd>\n"
269                          "<dt>½Ð¿ÈÃÏ¡§</dt>"
270                          "<dd>" (mixi-friend-birthplace object) "</dd>\n"
271                          "<dt>¼ñÌ£¡§</dt>"
272                          "<dd>" hobby "</dd>\n"
273                          "<dt>¿¦¶È¡§</dt>"
274                          "<dd>" (mixi-friend-job object) "</dd>\n"
275                          "<dt>½ê°¡§</dt>"
276                          "<dd>" (mixi-friend-organization object) "</dd>\n"
277                          "<dt>¼«¸Ê¾Ò²ð¡§</dt>"
278                          "<dd>" (mixi-friend-profile object) "</dd>\n"
279                          "</dl>"))
280              (concat "<a href=\"" (shimbun-mixi-make-xref object)
281                      "\">¥×¥í¥Õ¥£¡¼¥ë¤òɽ¼¨¤¹¤ë</a>")))
282           (t (mixi-object-content object)))))
283
284 (defun shimbun-mixi-make-reply-to (object)
285   (setq shimbun-mixi-reply-to "mixi;")
286   (let ((class (mixi-object-class object)))
287     (setq shimbun-mixi-reply-to
288           (concat
289            (cond ((eq class 'mixi-diary)
290                   (concat shimbun-mixi-reply-to "comment;diary;"
291                           (mixi-friend-id (mixi-diary-owner object)) ";"
292                           (mixi-diary-id object)))
293                  ((mixi-bbs-p object)
294                   (concat shimbun-mixi-reply-to "comment;"
295                           (mixi-object-name object) ";"
296                           (mixi-community-id (mixi-bbs-community object)) ";"
297                           (mixi-bbs-id object)))
298                  ((eq class 'mixi-community)
299                   (concat shimbun-mixi-reply-to "topic;"
300                           (mixi-community-id object)))
301                  ((or (eq class 'mixi-news) (eq object (mixi-make-me)))
302                   (concat shimbun-mixi-reply-to "diary"))
303                  ((eq class 'mixi-message)
304                   (concat shimbun-mixi-reply-to "message;"
305                           (mixi-friend-id (mixi-message-owner object))))
306                  ((or (eq class 'mixi-friend) (eq class 'mixi-log))
307                   (concat shimbun-mixi-reply-to "message;"
308                           (mixi-friend-id object))))))))
309
310 (defun shimbun-mixi-get-headers (shimbun objects &optional range)
311   (when objects
312     (let (headers)
313       (catch 'stop
314         (mapc (lambda (object)
315                 (when (mixi-object-p object)
316                   (let ((class (mixi-object-class object)))
317                     (when (mixi-parent-p object)
318                       (let ((comments (mixi-get-comments object range)))
319                         (mapc (lambda (header)
320                                 (push header headers))
321                               (shimbun-mixi-get-headers shimbun
322                                                         comments))))
323                     (let ((id (shimbun-mixi-make-message-id object)))
324                       (when (and (eq class 'mixi-comment)
325                                  (shimbun-search-id shimbun id))
326                         (throw 'stop nil))
327                       (push
328                        (shimbun-create-header
329                         0
330                         (shimbun-mixi-make-subject shimbun object)
331                         (shimbun-mixi-make-from object)
332                         (shimbun-mixi-make-date object)
333                         id
334                         (if (eq class 'mixi-comment)
335                             (shimbun-mixi-make-message-id
336                              (mixi-comment-parent object))
337                           "")
338                         0 0
339                         (shimbun-mixi-make-xref object))
340                        headers)
341                       (when (eq class 'mixi-comment)
342                         (puthash id (mixi-comment-content object)
343                                  (shimbun-mixi-comment-cache-internal
344                                   shimbun)))))))
345               objects))
346       headers)))
347
348 (luna-define-method shimbun-get-headers ((shimbun shimbun-mixi)
349                                          &optional range)
350   (let ((url-or-function (cdr (assoc (shimbun-current-group-internal shimbun)
351                                      shimbun-mixi-group-alist)))
352         (range (when (integerp range) (* range shimbun-mixi-page-articles)))
353         objects)
354     (if (stringp url-or-function)
355         (let* ((object (mixi-make-object-from-url url-or-function))
356                (class (mixi-object-class object)))
357           (cond ((eq class 'mixi-friend)
358                  (setq objects (mixi-get-diaries object range)))
359                 ((eq class 'mixi-community)
360                  (setq objects (mixi-get-bbses object range)))
361                 ((mixi-parent-p object)
362                  (setq objects (mixi-get-comments object range)))
363                 (t (error (concat (symbol-name class)
364                                   " is not supported yet.")))))
365       (setq objects (funcall url-or-function range)))
366     (shimbun-sort-headers (shimbun-mixi-get-headers shimbun objects range))))
367
368 (defun shimbun-mixi-comment-article (url shimbun header)
369   (let* ((message-id (shimbun-header-id header))
370          (cache (shimbun-mixi-comment-cache-internal shimbun))
371          (article (gethash message-id cache)))
372     (unless (stringp article)
373       (let ((parent (mixi-make-object-from-url url)))
374         (mapc (lambda (comment)
375                 (let ((id (shimbun-mixi-make-message-id comment))
376                       (content (mixi-comment-content comment)))
377                   (puthash id content cache)
378                   (when (string= id message-id)
379                     (setq article content))))
380               (mixi-get-comments parent))))
381     article))
382
383 (luna-define-method shimbun-article ((shimbun shimbun-mixi)
384                                      header &optional outbuf)
385   (when (shimbun-current-group-internal shimbun)
386     (with-current-buffer (or outbuf (current-buffer))
387       (w3m-insert-string
388        (or (with-temp-buffer
389              (let* ((url (shimbun-article-url shimbun header))
390                     (object (mixi-make-object-from-url url))
391                     (article (if (string-match "#comment$" url)
392                                  (shimbun-mixi-comment-article
393                                   url shimbun header)
394                                (shimbun-mixi-make-body object))))
395                (shimbun-mixi-make-reply-to object)
396                (when (stringp article)
397                  (insert article)))
398              (shimbun-message shimbun "shimbun: Make contents...")
399              (goto-char (point-min))
400              (prog1 (shimbun-make-contents shimbun header)
401                (shimbun-message shimbun "shimbun: Make contents...done")))
402            "")))))
403
404 (defconst shimbun-mixi-to-regexp
405   "^mixi;\\([a-z]+\\);?\\([a-z0-9]+\\)?;?\\([0-9]+\\)?;?\\([0-9]+\\)?")
406
407 (defun shimbun-mixi-send-mail (to title content)
408   (when (string-match shimbun-mixi-to-regexp to)
409     (let ((method (match-string 1 to)))
410       (cond ((string= method "comment")
411              (let ((parent (match-string 2 to))
412                    (owner-id (match-string 3 to))
413                    (id (match-string 4 to)))
414                (if (string= parent "diary")
415                    (mixi-post-comment
416                     (mixi-make-diary (mixi-make-friend owner-id) id) content)
417                  (let ((func (intern
418                               (concat mixi-object-prefix "make-" parent))))
419                    (mixi-post-comment
420                     (funcall func (mixi-make-community owner-id) id)
421                     content)))))
422             ((string= method "topic")
423              (mixi-post-topic (mixi-make-community (match-string 2 to))
424                               title content))
425             ((string= method "diary")
426              (mixi-post-diary title content))
427             ((string= method "message")
428              (mixi-post-message (mixi-make-friend (match-string 2 to))
429                                 title content))))))
430
431 (provide 'sb-mixi)
432
433 ;;; sb-mixi.el ends here