25ce53954d6f647fca88dd40cb1a7fbea5b2a4d3
[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 '(("new-diaries" . mixi-get-new-diaries)
40                                       ("new-comments" . mixi-get-new-comments)
41                                       ("new-bbses" . mixi-get-new-bbses)
42                                       ("messages" . mixi-get-messages)
43                                       ("my-diaries" . "/home.pl"))
44   "*An alist of mixi shimbun group definition.
45 Each element looks like (NAME . URL) or (NAME . FUNCTION).
46 NAME is a shimbun group name.
47 URL is the URL for mixi access point of the group.  When URL is friend's, get
48 his/her diaries as article.  When community's, get its BBSes.  When diary's
49 or BBS's, get its comments.
50 FUNCTION is the function which has one `range' argument and returns the list
51 of mixi object."
52   :group 'shimbun
53   :type '(repeat (cons :fromat "%v"
54                        (string :tag "Group name")
55                        (radio (string :tag "URL")
56                               (const :tag "New diaries" mixi-get-new-diaries)
57                               (const :tag "New comments" mixi-get-new-comments)
58                               (const :tag "New BBSes" mixi-get-new-bbses)
59                               (const :tag "Messages" mixi-get-messages)
60                               (function :tag "Other function")))))
61
62 ;; FIXME: Don't use this user option.
63 (defcustom shimbun-mixi-page-articles 10
64   "*How many articles are there in one page."
65   :group 'shimbun
66   :type 'integer)
67
68 (defvar shimbun-mixi-x-face-alist
69   '(("default" . "X-Face: CY;j#FoBnpK^37`-IoJvN!J^u;GciiPmMQ@T)~RP1]t8iv?v)/bVI:I\"F!JfWJvhM5{zY!=
70  h.d+'g\\I{D>Ocy?Rc4uYUyOZj2%2Kl>,x-!MCSsyi3!L}psrrC1jlF,O?Ui>qf)X;sBz`/}\\066X%$
71  siG'|4K!2?==|oB&#E'5GGH\\#z[muyQ")))
72
73 (defvar shimbun-mixi-reply-to nil)
74
75 (luna-define-method initialize-instance :after ((shimbun shimbun-mixi)
76                                                 &rest init-args)
77   (shimbun-mixi-set-comment-cache-internal shimbun
78                                            (make-hash-table :test 'equal))
79   shimbun)
80
81 (luna-define-method shimbun-close :after ((shimbun shimbun-mixi))
82   (shimbun-mixi-set-comment-cache-internal shimbun nil)
83   (mixi-logout))
84
85 (luna-define-method shimbun-groups ((shimbun shimbun-mixi))
86   (mapcar 'car shimbun-mixi-group-alist))
87
88 (luna-define-method shimbun-reply-to ((shimbun shimbun-mixi))
89   shimbun-mixi-reply-to)
90
91 (defun shimbun-mixi-make-subject (shimbun object)
92   (let ((class (mixi-object-class object)))
93     (if (eq class 'mixi-comment)
94         (concat "Re: " (shimbun-mixi-make-subject
95                         shimbun (mixi-comment-parent object)))
96       (let ((prefix (when (eq class 'mixi-event) "[¥¤¥Ù¥ó¥È]"))
97             (subject (mixi-object-title object))
98             (suffix (when (string-match
99                            "^new-" (shimbun-current-group-internal shimbun))
100                       (concat " ("
101                               (if (eq class 'mixi-diary)
102                                   (mixi-friend-nick
103                                    (mixi-diary-owner object))
104                                 (mixi-community-name
105                                  (mixi-bbs-community object)))
106                               ")"))))
107         (concat prefix subject suffix)))))
108
109 (defun shimbun-mixi-make-from (object)
110   (let ((owner (mixi-object-owner object)))
111     (mixi-friend-nick owner)))
112
113 (defun shimbun-mixi-make-date (object)
114   (let* ((time (mixi-object-time object))
115          (cts (current-time-string time))
116          (day-of-week (substring cts 0 3))
117          (month (substring cts 4 7)))
118     (concat day-of-week ", "
119             (format-time-string "%d" time) " "
120             month " "
121             (format-time-string "%Y %H:%M:%S %z" time))))
122
123 (defun shimbun-mixi-make-message-id (object)
124   (let ((class (mixi-object-class object)))
125     (concat "<"
126             (format-time-string "%Y%m%d%H%M" (mixi-object-time object)) "."
127             (if (eq class 'mixi-comment)
128                 (concat (mixi-friend-id (mixi-comment-owner object)) "@"
129                         (mixi-object-id (mixi-comment-parent object)) "."
130                         (mixi-friend-id (mixi-object-owner
131                                          (mixi-comment-parent object))) ".")
132               (concat (mixi-object-id object) "@"
133                       (mixi-object-id (mixi-object-owner object)) "."))
134             (mixi-object-name object) ".mixi.jp"
135             ">")))
136
137 (defun shimbun-mixi-make-xref (object)
138   (let ((class (mixi-object-class object)))
139     (cond ((eq class 'mixi-diary)
140            (mixi-expand-url (mixi-diary-page object)))
141           ((eq class 'mixi-topic)
142            (mixi-expand-url (mixi-topic-page object)))
143           ((eq class 'mixi-event)
144            (mixi-expand-url (mixi-event-page object)))
145           ((eq class 'mixi-comment)
146            (concat (shimbun-mixi-make-xref (mixi-comment-parent object))
147                    "#comment"))
148           ((eq class 'mixi-message)
149            (mixi-expand-url (mixi-message-page object))))))
150
151 (defun shimbun-mixi-make-body (object)
152   (let ((class (mixi-object-class object)))
153     (cond ((eq class 'mixi-event)
154            (let ((limit (mixi-event-limit object)))
155              (setq limit (if limit
156                              (format-time-string "%Yǯ%m·î%dÆü" limit)
157                            "»ØÄê¤Ê¤·"))
158              (concat "<dl><dt>³«ºÅÆü»þ¡§</dt>"
159                      "<dd>" (mixi-event-date object) "</dd>"
160                      "<dt>³«ºÅ¾ì½ê¡§</dt>"
161                      "<dd>" (mixi-event-place object) "</dd>"
162                      "<dt>¾ÜºÙ¡§</dt>"
163                      "<dd>" (mixi-event-detail object) "</dd>"
164                      "<dt>Ê罸´ü¸Â¡§</dt>"
165                      "<dd>" limit "</dd>"
166                      "<dt>»²²Ã¼Ô¡§</dt>"
167                      "<dd>" (mixi-event-members object) "</dd></dl>")))
168           (t (mixi-object-content object)))))
169
170 (defun shimbun-mixi-make-reply-to (object)
171   (setq shimbun-mixi-reply-to "mixi;")
172   (let ((class (mixi-object-class object)))
173     (setq shimbun-mixi-reply-to
174           (concat
175            (cond ((eq class 'mixi-diary)
176                   (concat shimbun-mixi-reply-to "comment;diary;"
177                           (mixi-friend-id (mixi-diary-owner object)) ";"
178                           (mixi-diary-id object)))
179                  ((mixi-bbs-p object)
180                   (concat shimbun-mixi-reply-to "comment;"
181                           (mixi-object-name object) ";"
182                           (mixi-community-id (mixi-bbs-community object)) ";"
183                           (mixi-bbs-id object)))
184                  ((eq class 'mixi-community)
185                   (concat shimbun-mixi-reply-to "topic;"
186                           (mixi-community-id object)))
187                  ((eq object (mixi-make-me))
188                   (concat shimbun-mixi-reply-to "diary"))
189                  ((eq class 'mixi-message)
190                   (concat shimbun-mixi-reply-to "message;"
191                           (mixi-friend-id (mixi-message-owner object))))
192                  ((or (eq class 'mixi-friend) (eq class 'mixi-log))
193                   (concat shimbun-mixi-reply-to "message;"
194                           (mixi-friend-id object))))))))
195
196 (defun shimbun-mixi-get-headers (shimbun objects &optional range)
197   (when objects
198     (let (headers)
199       (catch 'stop
200         (mapc (lambda (object)
201                 (when (mixi-object-p object)
202                   (let ((class (mixi-object-class object))
203                         (id (shimbun-mixi-make-message-id object)))
204                     (push
205                      (shimbun-create-header
206                       0
207                       (shimbun-mixi-make-subject shimbun object)
208                       (shimbun-mixi-make-from object)
209                       (shimbun-mixi-make-date object)
210                       id
211                       (if (eq class 'mixi-comment)
212                           (shimbun-mixi-make-message-id
213                            (mixi-comment-parent object))
214                         "")
215                       0 0
216                       (shimbun-mixi-make-xref object))
217                      headers)
218                     (when (or (eq class 'mixi-diary)
219                               (mixi-bbs-p object))
220                       (let ((comments (mixi-get-comments object range)))
221                         (mapc (lambda (header)
222                                 (push header headers))
223                               (shimbun-mixi-get-headers shimbun
224                                                         comments))))
225                     (when (eq class 'mixi-comment)
226                       (puthash id (mixi-comment-content object)
227                                (shimbun-mixi-comment-cache-internal
228                                 shimbun))))))
229               objects))
230       headers)))
231
232 (luna-define-method shimbun-get-headers ((shimbun shimbun-mixi)
233                                          &optional range)
234   (let ((url-or-function (cdr (assoc (shimbun-current-group-internal shimbun)
235                                      shimbun-mixi-group-alist)))
236         (range (when (integerp range) (* range shimbun-mixi-page-articles)))
237         objects)
238     (if (stringp url-or-function)
239         (let* ((object (mixi-make-object-from-url url-or-function))
240                (class (mixi-object-class object)))
241           (cond ((eq class 'mixi-friend)
242                  (setq objects (mixi-get-diaries object range)))
243                 ((eq class 'mixi-community)
244                  (setq objects (mixi-get-bbses object range)))
245                 ((or (eq class 'mixi-diary)
246                      (mixi-bbs-p object))
247                  (setq objects (mixi-get-comments object range)))
248                 (t (error (concat (symbol-name class)
249                                   " is not supported yet.")))))
250       (setq objects (funcall url-or-function range)))
251     (shimbun-sort-headers (shimbun-mixi-get-headers shimbun objects range))))
252
253 (defun shimbun-mixi-comment-article (url shimbun header)
254   (let* ((message-id (shimbun-header-id header))
255          (cache (shimbun-mixi-comment-cache-internal shimbun))
256          (article (gethash message-id cache)))
257     (unless (stringp article)
258       (let ((parent (mixi-make-object-from-url url)))
259         (mapc (lambda (comment)
260                 (let ((id (shimbun-mixi-make-message-id comment))
261                       (content (mixi-comment-content comment)))
262                   (puthash id content cache)
263                   (when (string= id message-id)
264                     (setq article content))))
265               (mixi-get-comments parent))))
266     article))
267
268 (luna-define-method shimbun-article ((shimbun shimbun-mixi)
269                                      header &optional outbuf)
270   (when (shimbun-current-group-internal shimbun)
271     (with-current-buffer (or outbuf (current-buffer))
272       (w3m-insert-string
273        (or (with-temp-buffer
274              (let* ((url (shimbun-article-url shimbun header))
275                     (object (mixi-make-object-from-url url))
276                     (article (if (string-match "#comment$" url)
277                                  (shimbun-mixi-comment-article
278                                   url shimbun header)
279                                (shimbun-mixi-make-body object))))
280                (shimbun-mixi-make-reply-to object)
281                (when (stringp article)
282                  (insert article)))
283              (shimbun-message shimbun "shimbun: Make contents...")
284              (goto-char (point-min))
285              (prog1 (shimbun-make-contents shimbun header)
286                (shimbun-message shimbun "shimbun: Make contents...done")))
287            "")))))
288
289 (defconst shimbun-mixi-to-regexp
290   "^mixi;\\([a-z]+\\);?\\([a-z0-9]+\\)?;?\\([0-9]+\\)?;?\\([0-9]+\\)?")
291
292 (defun shimbun-mixi-send-mail-wrapper (to title content)
293   (when (string-match shimbun-mixi-to-regexp to)
294     (let ((method (match-string 1 to)))
295       (cond ((string= method "comment")
296              (let ((parent (match-string 2 to))
297                    (owner-id (match-string 3 to))
298                    (id (match-string 4 to)))
299                (if (string= parent "diary")
300                    (mixi-post-comment
301                     (mixi-make-diary (mixi-make-friend owner-id) id) content)
302                  (let ((func (intern
303                               (concat mixi-object-prefix "make-" parent))))
304                    (mixi-post-comment
305                     (funcall func (mixi-make-community owner-id) id)
306                     content)))))
307             ((string= method "topic")
308              (mixi-post-topic (mixi-make-community (match-string 2 to))
309                               title content))
310             ((string= method "diary")
311              (mixi-post-diary title content))
312             ((string= method "message")
313              (mixi-post-message (mixi-make-friend (match-string 2 to))
314                                 title content))))))
315
316 (provide 'sb-mixi)
317
318 ;;; sb-mixi.el ends here