* sb-mixi.el: Fix comments.
[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-topics" . mixi-get-new-topics)
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 topics.  When diary's
44 or topic'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 topics" mixi-get-new-topics)
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 topics 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     (if (eq class 'mixi-comment)
73         (concat "Re: " (mixi-object-title (mixi-comment-parent object)))
74       (mixi-object-title object))))
75
76 (defun shimbun-mixi-make-from (object)
77   (let ((owner (mixi-object-owner object)))
78     (mixi-friend-nick owner)))
79
80 (defun shimbun-mixi-make-date (object)
81   (let* ((time (mixi-object-time object))
82          (cts (current-time-string time))
83          (day-of-week (substring cts 0 3))
84          (month (substring cts 4 7)))
85     (concat day-of-week ", "
86             (format-time-string "%d" time) " "
87             month " "
88             (format-time-string "%Y %H:%M:%S %z" time))))
89
90 (defun shimbun-mixi-make-message-id (object)
91   (let ((class (mixi-object-class object)))
92     (concat "<"
93             (format-time-string "%Y%m%d%H%M" (mixi-object-time object)) "."
94             (if (eq class 'mixi-comment)
95                 (concat (mixi-friend-id (mixi-comment-owner object)) "@"
96                         (mixi-object-id (mixi-comment-parent object)) "."
97                         (mixi-friend-id (mixi-object-owner
98                                          (mixi-comment-parent object))) ".")
99               (concat (mixi-object-id object) "@"
100                       (mixi-object-id (mixi-object-owner object)) "."))
101             (mixi-object-name object) ".mixi.jp"
102             ">")))
103
104 (defun shimbun-mixi-make-xref (object)
105   (let ((class (mixi-object-class object)))
106     (cond ((eq class 'mixi-diary)
107            (mixi-expand-url (mixi-diary-page object)))
108           ((eq class 'mixi-topic)
109            (mixi-expand-url (mixi-topic-page object)))
110           ((eq class 'mixi-comment)
111            (concat (shimbun-mixi-make-xref (mixi-comment-parent object))
112                    "#comment"))
113           ((eq class 'mixi-message)
114            (mixi-expand-url (mixi-message-page object))))))
115
116 (defun shimbun-mixi-get-headers (shimbun objects &optional range)
117   (when objects
118     (let (headers)
119       (catch 'stop
120         (mapc (lambda (object)
121                 (when (mixi-object-p object)
122                   (let ((class (mixi-object-class object))
123                         (id (shimbun-mixi-make-message-id object)))
124                     (push
125                      (shimbun-create-header
126                       0
127                       (shimbun-mixi-make-subject object)
128                       (shimbun-mixi-make-from object)
129                       (shimbun-mixi-make-date object)
130                       id
131                       (if (eq class 'mixi-comment)
132                           (shimbun-mixi-make-message-id
133                            (mixi-comment-parent object))
134                         "")
135                       0 0
136                       (shimbun-mixi-make-xref object))
137                      headers)
138                     (when (and shimbun-mixi-get-comment-p
139                                (or (eq class 'mixi-diary)
140                                    (eq class 'mixi-topic)))
141                       (let ((comments (mixi-get-comments object range)))
142                         (mapc (lambda (header)
143                                 (push header headers))
144                               (shimbun-mixi-get-headers shimbun
145                                                         comments)))))))
146               objects))
147       headers)))
148
149 (luna-define-method shimbun-get-headers ((shimbun shimbun-mixi)
150                                          &optional range)
151   (let ((url-or-function (cdr (assoc (shimbun-current-group-internal shimbun)
152                                      shimbun-mixi-group-alist)))
153         (range (when (integerp range) (* range shimbun-mixi-page-articles)))
154         objects)
155     (if (stringp url-or-function)
156         (let* ((object (mixi-make-object-from-url url-or-function))
157                (class (mixi-object-class object)))
158           (cond ((eq class 'mixi-friend)
159                  (setq objects (mixi-get-diaries object range)))
160                 ((eq class 'mixi-community)
161                  (setq objects (mixi-get-topics object range)))
162                 ((or (eq class 'mixi-diary) (eq class 'mixi-topic))
163                  (setq objects (mixi-get-comments object range)))
164                 (t (error (concat (symbol-name class)
165                                   " is not supported yet.")))))
166       (setq objects (funcall url-or-function range)))
167     (shimbun-sort-headers (shimbun-mixi-get-headers shimbun objects range))))
168
169 (defun shimbun-comment-article (url header)
170   (let ((parent (mixi-make-object-from-url url))
171         (date (shimbun-header-date header))
172         (message-id (shimbun-header-id header)))
173     (catch 'found
174       (mapc (lambda (comment)
175               (let ((id (mixi-friend-id (mixi-comment-owner comment)))
176                     (time (shimbun-mixi-make-date comment)))
177                 (when (and (string= time date)
178                            (string-match (concat "^<[0-9]+\\." id "@")
179                                          message-id))
180                   ;; FIXME: Concat parent's information?
181                   (throw 'found (mixi-comment-content comment)))))
182             (mixi-get-comments parent)))))
183
184 (luna-define-method shimbun-article ((shimbun shimbun-mixi)
185                                      header &optional outbuf)
186   (when (shimbun-current-group-internal shimbun)
187     (with-current-buffer (or outbuf (current-buffer))
188       (w3m-insert-string
189        (or (with-temp-buffer
190              (let* ((url (shimbun-article-url shimbun header))
191                     (article (if (string-match "#comment$" url)
192                                  (shimbun-comment-article url header)
193                                ;; FIXME: Concat community information?
194                                (mixi-object-content
195                                 (mixi-make-object-from-url url)))))
196                (when (stringp article)
197                  (insert article)))
198              (shimbun-message shimbun "shimbun: Make contents...")
199              (goto-char (point-min))
200              (prog1 (shimbun-make-contents shimbun header)
201                (shimbun-message shimbun "shimbun: Make contents...done")))
202            "")))))
203
204 (provide 'sb-mixi)
205
206 ;;; sb-mixi.el ends here