* mixi.el (mixi-object-title): Follow the event 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 ;;; Code:
28
29 (require 'mixi)
30 (require 'shimbun)
31
32 (luna-define-class shimbun-mixi (shimbun) ())
33
34 (defcustom shimbun-mixi-group-alist '(("new-diaries" . mixi-get-new-diaries)
35                                       ("new-comments" . mixi-get-new-comments)
36                                       ("new-bbses" . mixi-get-new-bbses)
37                                       ("messages" . mixi-get-messages)
38                                       ("my-diaries" . "/home.pl"))
39   "*An alist of mixi shimbun group definition.
40 Each element looks like (NAME . URL) or (NAME . FUNCTION).
41 NAME is a shimbun group name.
42 URL is the URL for mixi access point of the group.  When URL is friend's, get
43 his/her diaries as article.  When community's, get its BBSes.  When diary's
44 or BBS's, get its comments.
45 FUNCTION is the function for getting articles."
46   :group 'shimbun
47   :type '(repeat (cons :fromat "%v"
48                        (string :tag "Group name")
49                        (radio (string :tag "URL")
50                               (const :tag "New diaries" mixi-get-new-diaries)
51                               (const :tag "New comments" mixi-get-new-comments)
52                               (const :tag "New BBSes" mixi-get-new-bbses)
53                               (const :tag "Messages" mixi-get-messages)
54                               (function :tag "Other function")))))
55
56 ;; FIXME: Don't use this user option.
57 (defcustom shimbun-mixi-page-articles 10
58   "*How many articles are there in one page."
59   :group 'shimbun
60   :type 'integer)
61
62 (defcustom shimbun-mixi-get-comment-p t
63   "*If non-nil, get diaries or BBSes together with its comments."
64   :group 'shimbun
65   :type 'boolean)
66
67 (luna-define-method shimbun-groups ((shimbun shimbun-mixi))
68   (mapcar 'car shimbun-mixi-group-alist))
69
70 (defun shimbun-mixi-make-subject (object)
71   (let ((class (mixi-object-class object)))
72     (cond ((eq class 'mixi-comment)
73            (concat "Re: " (shimbun-mixi-make-subject
74                            (mixi-comment-parent object))))
75           (t (mixi-object-title object)))))
76
77 (defun shimbun-mixi-make-from (object)
78   (let ((owner (mixi-object-owner object)))
79     (mixi-friend-nick owner)))
80
81 (defun shimbun-mixi-make-date (object)
82   (let* ((time (mixi-object-time object))
83          (cts (current-time-string time))
84          (day-of-week (substring cts 0 3))
85          (month (substring cts 4 7)))
86     (concat day-of-week ", "
87             (format-time-string "%d" time) " "
88             month " "
89             (format-time-string "%Y %H:%M:%S %z" time))))
90
91 (defun shimbun-mixi-make-message-id (object)
92   (let ((class (mixi-object-class object)))
93     (concat "<"
94             (format-time-string "%Y%m%d%H%M" (mixi-object-time object)) "."
95             (if (eq class 'mixi-comment)
96                 (concat (mixi-friend-id (mixi-comment-owner object)) "@"
97                         (mixi-object-id (mixi-comment-parent object)) "."
98                         (mixi-friend-id (mixi-object-owner
99                                          (mixi-comment-parent object))) ".")
100               (concat (mixi-object-id object) "@"
101                       (mixi-object-id (mixi-object-owner object)) "."))
102             (mixi-object-name object) ".mixi.jp"
103             ">")))
104
105 (defun shimbun-mixi-make-xref (object)
106   (let ((class (mixi-object-class object)))
107     (cond ((eq class 'mixi-diary)
108            (mixi-expand-url (mixi-diary-page object)))
109           ((eq class 'mixi-topic)
110            (mixi-expand-url (mixi-topic-page object)))
111           ((eq class 'mixi-event)
112            (mixi-expand-url (mixi-event-page object)))
113           ((eq class 'mixi-comment)
114            (concat (shimbun-mixi-make-xref (mixi-comment-parent object))
115                    "#comment"))
116           ((eq class 'mixi-message)
117            (mixi-expand-url (mixi-message-page object))))))
118
119 (defun shimbun-mixi-make-body (object)
120   (mixi-object-content object))
121
122 (defun shimbun-mixi-get-headers (shimbun objects &optional range)
123   (when objects
124     (let (headers)
125       (catch 'stop
126         (mapc (lambda (object)
127                 (when (mixi-object-p object)
128                   (let ((class (mixi-object-class object))
129                         (id (shimbun-mixi-make-message-id object)))
130                     (push
131                      (shimbun-create-header
132                       0
133                       (shimbun-mixi-make-subject object)
134                       (shimbun-mixi-make-from object)
135                       (shimbun-mixi-make-date object)
136                       id
137                       (if (eq class 'mixi-comment)
138                           (shimbun-mixi-make-message-id
139                            (mixi-comment-parent object))
140                         "")
141                       0 0
142                       (shimbun-mixi-make-xref object))
143                      headers)
144                     (when (and shimbun-mixi-get-comment-p
145                                (or (eq class 'mixi-diary)
146                                    (eq class 'mixi-topic)
147                                    (eq class 'mixi-event)))
148                       (let ((comments (mixi-get-comments object range)))
149                         (mapc (lambda (header)
150                                 (push header headers))
151                               (shimbun-mixi-get-headers shimbun
152                                                         comments)))))))
153               objects))
154       headers)))
155
156 (luna-define-method shimbun-get-headers ((shimbun shimbun-mixi)
157                                          &optional range)
158   (let ((url-or-function (cdr (assoc (shimbun-current-group-internal shimbun)
159                                      shimbun-mixi-group-alist)))
160         (range (when (integerp range) (* range shimbun-mixi-page-articles)))
161         objects)
162     (if (stringp url-or-function)
163         (let* ((object (mixi-make-object-from-url url-or-function))
164                (class (mixi-object-class object)))
165           (cond ((eq class 'mixi-friend)
166                  (setq objects (mixi-get-diaries object range)))
167                 ((eq class 'mixi-community)
168                  (setq objects (mixi-get-bbses object range)))
169                 ((or (eq class 'mixi-diary)
170                      (eq class 'mixi-topic)
171                      (eq class 'mixi-event))
172                  (setq objects (mixi-get-comments object range)))
173                 (t (error (concat (symbol-name class)
174                                   " is not supported yet.")))))
175       (setq objects (funcall url-or-function range)))
176     (shimbun-sort-headers (shimbun-mixi-get-headers shimbun objects range))))
177
178 (defun shimbun-comment-article (url header)
179   (let ((parent (mixi-make-object-from-url url))
180         (date (shimbun-header-date header))
181         (message-id (shimbun-header-id header)))
182     (catch 'found
183       (mapc (lambda (comment)
184               (let ((id (mixi-friend-id (mixi-comment-owner comment)))
185                     (time (shimbun-mixi-make-date comment)))
186                 (when (and (string= time date)
187                            (string-match (concat "^<[0-9]+\\." id "@")
188                                          message-id))
189                   ;; FIXME: Concat parent's information?
190                   (throw 'found (mixi-comment-content comment)))))
191             (mixi-get-comments parent)))))
192
193 (luna-define-method shimbun-article ((shimbun shimbun-mixi)
194                                      header &optional outbuf)
195   (when (shimbun-current-group-internal shimbun)
196     (with-current-buffer (or outbuf (current-buffer))
197       (w3m-insert-string
198        (or (with-temp-buffer
199              (let* ((url (shimbun-article-url shimbun header))
200                     (article (if (string-match "#comment$" url)
201                                  (shimbun-comment-article url header)
202                                ;; FIXME: Concat community information?
203                                (shimbun-mixi-make-body
204                                 (mixi-make-object-from-url url)))))
205                (when (stringp article)
206                  (insert article)))
207              (shimbun-message shimbun "shimbun: Make contents...")
208              (goto-char (point-min))
209              (prog1 (shimbun-make-contents shimbun header)
210                (shimbun-message shimbun "shimbun: Make contents...done")))
211            "")))))
212
213 (provide 'sb-mixi)
214
215 ;;; sb-mixi.el ends here