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