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