Remove FIXME comment.
[elisp/mixi.git] / mixi.el
1 ;; mixi.el --- API library for accessing to mixi
2
3 ;; Copyright (C) 2005,2006 OHASHI Akira
4
5 ;; Author: OHASHI Akira <bg66@koka-in.org>
6 ;; Keywords: hypermedia
7
8 ;; This file is *NOT* a part of Emacs.
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 ;; API for getting contents:
28 ;;
29 ;;  * mixi-get-friends
30 ;;  * mixi-get-favorites
31 ;;  * mixi-get-logs
32 ;;  * mixi-get-diaries
33 ;;  * mixi-get-new-diaries
34 ;;  * mixi-search-diaries
35 ;;  * mixi-get-communities
36 ;;  * mixi-search-communities
37 ;;  * mixi-get-bbses
38 ;;  * mixi-get-new-bbses
39 ;;  * mixi-search-bbses
40 ;;  * mixi-get-comments
41 ;;  * mixi-get-new-comments
42 ;;  * mixi-get-messages
43 ;;  * mixi-get-introductions
44 ;; 
45 ;; Utilities:
46 ;;
47 ;;  * mixi-remove-markup
48
49 ;; Example:
50 ;;
51 ;; Display newest 3 diaries like a mail format.
52 ;;
53 ;; (let ((range 3)
54 ;;       (buffer (get-buffer-create "*temp*"))
55 ;;       (format "%Y/%m/%d %H:%M"))
56 ;;   (pop-to-buffer buffer)
57 ;;   (mapc (lambda (diary)
58 ;;        (let ((subject (mixi-diary-title diary))
59 ;;              (from (mixi-friend-nick (mixi-diary-owner diary)))
60 ;;              (date (format-time-string format (mixi-diary-time diary)))
61 ;;              (body (mixi-remove-markup (mixi-diary-content diary))))
62 ;;          (insert "From: " from "\n"
63 ;;                  "Subject: " subject "\n"
64 ;;                  "Date: " date "\n\n"
65 ;;                  body "\n\n")))
66 ;;      (mixi-get-new-diaries range))
67 ;;   (set-buffer-modified-p nil)
68 ;;   (setq buffer-read-only t)
69 ;;   (goto-char (point-min)))
70 ;;
71 ;; Display newest 3 diaries including newest 3 comments like a mail format.
72 ;; Comments are displayed like a reply mail.
73 ;;
74 ;; (let ((range 3)
75 ;;       (buffer (get-buffer-create "*temp*"))
76 ;;       (format "%Y/%m/%d %H:%M"))
77 ;;   (pop-to-buffer buffer)
78 ;;   (mapc (lambda (diary)
79 ;;        (let ((subject (mixi-diary-title diary))
80 ;;              (from (mixi-friend-nick (mixi-diary-owner diary)))
81 ;;              (date (format-time-string format (mixi-diary-time diary)))
82 ;;              (body (mixi-remove-markup (mixi-diary-content diary))))
83 ;;          (insert "From: " from "\n"
84 ;;                  "Subject: " subject "\n"
85 ;;                  "Date: " date "\n\n"
86 ;;                  body "\n\n")
87 ;;          (mapc (lambda (comment)
88 ;;                  (let ((from (mixi-friend-nick
89 ;;                               (mixi-comment-owner comment)))
90 ;;                        (subject (concat "Re: " subject))
91 ;;                        (date (format-time-string
92 ;;                               format (mixi-comment-time comment)))
93 ;;                        (body (mixi-remove-markup
94 ;;                               (mixi-comment-content comment))))
95 ;;                    (insert "From: " from "\n"
96 ;;                            "Subject: " subject "\n"
97 ;;                            "Date: " date "\n\n"
98 ;;                            body "\n\n")))
99 ;;                (mixi-get-comments diary range))))
100 ;;      (mixi-get-new-diaries range))
101 ;;   (set-buffer-modified-p nil)
102 ;;   (setq buffer-read-only t)
103 ;;   (goto-char (point-min)))
104
105 ;; Bug reports:
106 ;;
107 ;; If you have bug reports and/or suggestions for improvement, please
108 ;; send them via <URL:http://mixi.jp/view_community.pl?id=1596390>.
109
110 ;;; Code:
111
112 (eval-when-compile (require 'cl))
113
114 (defgroup mixi nil
115   "API library for accessing to mixi."
116   :group 'hypermedia)
117
118 (defcustom mixi-url "http://mixi.jp"
119   "*The URL of mixi."
120   :type 'string
121   :group 'mixi)
122
123 (defcustom mixi-directory (expand-file-name "~/.mixi")
124   "*Where to look for mixi files."
125   :type 'directory
126   :group 'mixi)
127
128 (defcustom mixi-coding-system 'euc-jp
129   "*Coding system for mixi."
130   :type 'coding-system
131   :group 'mixi)
132
133 (defcustom mixi-curl-program "curl"
134   "*The program name of `curl'."
135   :type 'file
136   :group 'mixi)
137
138 (defcustom mixi-curl-cookie-file (expand-file-name "cookies.txt"
139                                                    mixi-directory)
140   "*The location of cookie file created by `curl'."
141   :type 'file
142   :group 'mixi)
143
144 (defcustom mixi-retrieve-function
145   (or (condition-case nil
146           (progn
147             (require 'url)
148             (if (fboundp 'url-retrieve-synchronously)
149                 'mixi-url-retrieve))
150         (error))
151       (condition-case nil
152           (progn
153             (require 'w3m)
154             'mixi-w3m-retrieve)
155         (error))
156       (if (and (fboundp 'executable-find)
157                (executable-find mixi-curl-program))
158           'mixi-curl-retrieve)
159       (error "Can't set `mixi-retrieve-function'"))
160   "*The function for retrieving."
161   :type '(radio (const :tag "Using url" mixi-url-retrieve)
162                 (const :tag "Using w3m" mixi-w3m-retrieve)
163                 (const :tag "Using curl" mixi-curl-retrieve)
164                 (function :format "Other function: %v\n" :size 0))
165   :group 'mixi)
166
167 (defcustom mixi-default-email nil
168   "*Default E-mail address that is used to login automatically."
169   :type '(radio (string :tag "E-mail address")
170                 (const :tag "Asked when it is necessary" nil))
171   :group 'mixi)
172
173 (defcustom mixi-default-password nil
174   "*Default password that is used to login automatically."
175   :type '(radio (string :tag "Password")
176                 (const :tag "Asked when it is necessary" nil))
177   :group 'mixi)
178
179 (defcustom mixi-accept-adult-contents t
180   "*If non-nil, accept to access to the adult contents."
181   :type 'boolean
182   :group 'mixi)
183
184 (defcustom mixi-continuously-access-interval 4.0
185   "*Time interval between each mixi access.
186 Increase this value when unexpected error frequently occurs."
187   :type 'number
188   :group 'mixi)
189
190 (defcustom mixi-cache-expires nil
191   "*Seconds for expiration of a cached object."
192   :type '(radio (integer :tag "Expired seconds")
193                 (const :tag "Don't expire" nil)
194                 (const :tag "Don't cache" t))
195   :group 'mixi)
196
197 ;; FIXME: Not implemented.
198 (defcustom mixi-cache-use-file t
199   "*If non-nil, caches are saved to files."
200   :type 'boolean
201   :group 'mixi)
202
203 (defvar mixi-me nil)
204
205 ;; Utilities.
206 (defmacro mixi-message (&rest strings)
207   `(concat "[mixi] " ,@strings))
208
209 (defconst mixi-message-adult-contents
210   "¤³¤Î¥Ú¡¼¥¸¤«¤éÀè¤Ï¥¢¥À¥ë¥È¡ÊÀ®¿Í¸þ¤±¡Ë¥³¥ó¥Æ¥ó¥Ä¤¬´Þ¤Þ¤ì¤Æ¤¤¤Þ¤¹¡£<br>
211 ±ÜÍ÷¤ËƱ°Õ¤µ¤ì¤¿Êý¤Î¤ß¡¢Àè¤Ø¤ª¿Ê¤ß¤¯¤À¤µ¤¤¡£")
212 (defconst mixi-message-continuously-accessing
213   "°ÂÄꤷ¤Æ¥µ¥¤¥È¤Î±¿±Ä¤ò¤ª¤³¤Ê¤¦°Ù¡¢´Ö³Ö¤ò¶õ¤±¤Ê¤¤Ï¢Â³Åª¤Ê¥Ú¡¼¥¸¤ÎÁ«°Ü¡¦¹¹<br>
214 ¿·¤ÏÀ©¸Â¤µ¤»¤Æ¤¤¤¿¤À¤¤¤Æ¤ª¤ê¤Þ¤¹¡£¤´ÌÂÏǤò¤ª¤«¤±¤¤¤¿¤·¤Þ¤¹¤¬¡¢¤·¤Ð¤é¤¯¤ª<br>
215 ÂÔ¤Á¤¤¤¿¤À¤¤¤Æ¤«¤éÁàºî¤ò¤ª¤³¤Ê¤Ã¤Æ¤¯¤À¤µ¤¤¡£")
216 (defconst mixi-warning-continuously-accessing
217   "´Ö³Ö¤ò¶õ¤±¤Ê¤¤Ï¢Â³Åª¤Ê¥Ú¡¼¥¸¤ÎÁ«°Ü¡¦¹¹¿·¤òÉÑÈˤˤª¤³¤Ê¤ï¤ì¤Æ¤¤¤ë¤³¤È¤¬¸«<br>
218 ¼õ¤±¤é¤ì¤Þ¤·¤¿¤Î¤Ç¡¢°ì»þŪ¤ËÁàºî¤òÄä»ß¤µ¤»¤Æ¤¤¤¿¤À¤­¤Þ¤¹¡£¿½¤·Ìõ¤´¤¶¤¤¤Þ<br>
219 ¤»¤ó¤¬¡¢¤·¤Ð¤é¤¯¤Î´Ö¤ªÂÔ¤Á¤¯¤À¤µ¤¤¡£")
220
221 (defmacro mixi-retrieve (url &optional post-data)
222   `(funcall mixi-retrieve-function ,url ,post-data))
223
224 (defun mixi-parse-buffer (url buffer &optional post-data)
225   (when (string-match mixi-message-adult-contents buffer)
226     (if mixi-accept-adult-contents
227         (setq buffer (mixi-retrieve url "submit=agree"))
228       (setq buffer (mixi-retrieve (concat url "?")))))
229   (when (string-match mixi-warning-continuously-accessing buffer)
230     (error (mixi-message "Access denied.  Please wait a while and increase "
231                          "the value of `mixi-continuously-access-interval'.")))
232   (if (not (string-match mixi-message-continuously-accessing buffer))
233       buffer
234     (message (mixi-message "Waiting for continuously accessing..."))
235     (sit-for mixi-continuously-access-interval)
236     (mixi-retrieve url post-data)))
237
238 (defmacro mixi-expand-url (url)
239   `(if (string-match (concat "^" mixi-url) ,url)
240        ,url
241      (concat mixi-url ,url)))
242
243 (defun mixi-url-retrieve (url &optional post-data)
244   "Retrieve the URL and return gotten strings."
245   (if post-data
246       (progn
247         (setq url-request-method "POST")
248         (setq url-request-data post-data))
249     (setq url-request-method "GET")
250     (setq url-request-data nil))
251   (let* ((url (mixi-expand-url url))
252          (buffer (url-retrieve-synchronously url))
253          ret)
254     (unless (bufferp buffer)
255       (error (mixi-message "Cannot retrieve")))
256     (with-current-buffer buffer
257       (goto-char (point-min))
258       (while (looking-at "HTTP/[0-9]+\\.[0-9]+ [13][0-9][0-9]")
259         (delete-region (point) (re-search-forward "\r?\n\r?\n")))
260       (unless (looking-at "HTTP/[0-9]+\\.[0-9]+ 200")
261         (error (mixi-message "Cannot retrieve")))
262       (delete-region (point) (re-search-forward "\r?\n\r?\n"))
263       (setq ret (decode-coding-string (buffer-string) mixi-coding-system))
264       (kill-buffer buffer)
265       (mixi-parse-buffer url ret post-data))))
266
267 (defun mixi-w3m-retrieve (url &optional post-data)
268   "Retrieve the URL and return gotten strings."
269   (let ((url (mixi-expand-url url)))
270     (with-temp-buffer
271       (if (not (string= (w3m-retrieve url nil nil post-data) "text/html"))
272           (error (mixi-message "Cannot retrieve"))
273         (w3m-decode-buffer url)
274         (let ((ret (buffer-substring-no-properties (point-min) (point-max))))
275           (mixi-parse-buffer url ret post-data))))))
276
277 (defun mixi-curl-retrieve (url &optional post-data)
278   "Retrieve the URL and return gotten strings."
279   (with-temp-buffer
280     (if (fboundp 'set-buffer-multibyte)
281         (set-buffer-multibyte nil))
282     (let ((orig-mode (default-file-modes))
283           (coding-system-for-read 'binary)
284           (coding-system-for-write 'binary)
285           process ret)
286       (unwind-protect
287           (progn
288             (set-default-file-modes 448)
289             (setq process
290                   (apply #'start-process "curl" (current-buffer)
291                          mixi-curl-program
292                          (append (if post-data '("-d" "@-"))
293                                  (list "-i" "-L" "-s"
294                                        "-b" mixi-curl-cookie-file
295                                        "-c" mixi-curl-cookie-file
296                                        (mixi-expand-url url)))))
297             (set-process-sentinel process #'ignore))
298         (set-default-file-modes orig-mode))
299       (when post-data
300         (process-send-string process (concat post-data "\n"))
301         (process-send-eof process))
302       (while (eq (process-status process) 'run)
303         (accept-process-output process 1))
304       (goto-char (point-min))
305       (while (looking-at "HTTP/[0-9]+\\.[0-9]+ [13][0-9][0-9]")
306         (delete-region (point) (re-search-forward "\r?\n\r?\n")))
307       (unless (looking-at "HTTP/[0-9]+\\.[0-9]+ 200")
308         (error (mixi-message "Cannot retrieve")))
309       (delete-region (point) (re-search-forward "\r?\n\r?\n"))
310       (setq ret (decode-coding-string (buffer-string) mixi-coding-system))
311       (mixi-parse-buffer url ret post-data))))
312
313 (defconst mixi-my-id-regexp
314   "<a href=\"add_diary\\.pl\\?id=\\([0-9]+\\)")
315
316 (defun mixi-login (&optional email password)
317   "Login to mixi."
318   (when (and (eq mixi-retrieve-function 'mixi-w3m-retrieve)
319              (not w3m-use-cookies))
320     (error (mixi-message "Require to accept cookies.  Please set "
321                          "`w3m-use-cookies' to t.")))
322   (let ((email (or email mixi-default-email
323                    (read-from-minibuffer (mixi-message "Login Email: "))))
324         (password (or password mixi-default-password
325                       (read-passwd (mixi-message "Login Password: ")))))
326     (let ((buffer (mixi-retrieve "/login.pl"
327                                  (concat "email=" email
328                                          "&password=" password
329                                          "&next_url=/home.pl"
330                                          "&sticky=on"))))
331       (unless (string-match "url=/check\\.pl\\?n=" buffer)
332         (error (mixi-message "Cannot login")))
333       (setq buffer (mixi-retrieve "/check.pl?n=home.pl"))
334       (if (string-match mixi-my-id-regexp buffer)
335           (setq mixi-me (mixi-make-friend (match-string 1 buffer)))
336         (error (mixi-message "Cannot login"))))))
337
338 (defun mixi-logout ()
339   (mixi-retrieve "/logout.pl"))
340
341 (defmacro with-mixi-retrieve (url &rest body)
342   `(let (buffer)
343      (when ,url
344        (setq buffer (mixi-retrieve ,url))
345        (when (string-match "login.pl" buffer)
346          (mixi-login)
347          (setq buffer (mixi-retrieve ,url))))
348      ,@body))
349 (put 'with-mixi-retrieve 'lisp-indent-function 'defun)
350 (put 'with-mixi-retrieve 'edebug-form-spec '(form body))
351
352 (defun mixi-get-matched-items (url regexp &optional range)
353   "Get matched items to REGEXP in URL."
354   (let ((page 1)
355         ids)
356     (catch 'end
357       (while (or (null range) (< (length ids) range))
358         (with-mixi-retrieve (format url page)
359           (let ((pos 0)
360                 found)
361             (while (and (string-match regexp buffer pos)
362                         (or (null range) (< (length ids) range)))
363               (let ((num 1)
364                     list)
365                 (while (match-string num buffer)
366                   (setq list (cons (match-string num buffer) list))
367                   (incf num))
368                 (when (not (member (reverse list) ids))
369                   (setq found t)
370                   (setq ids (cons (reverse list) ids)))
371                 (setq pos (match-end (1- num)))))
372             (when (not found)
373               (throw 'end ids))))
374         (incf page)))
375     (reverse ids)))
376
377 ;; stolen (and modified) from shimbun.el
378 (defun mixi-remove-markup (string)
379   "Remove markups from STRING."
380   (with-temp-buffer
381     (insert (or string ""))
382     (save-excursion
383       (goto-char (point-min))
384       (while (search-forward "<!--" nil t)
385         (delete-region (match-beginning 0)
386                        (or (search-forward "-->" nil t)
387                            (point-max))))
388       (goto-char (point-min))
389       (while (re-search-forward "<[^>]+>" nil t)
390         (replace-match "" t t))
391       (goto-char (point-min))
392       (while (re-search-forward "\r" nil t)
393         (replace-match "\n" t t)))
394     ;; FIXME: Decode entities.
395     (buffer-string)))
396
397 ;; stolen (and modified) from w3m.el
398 ;; FIXME: Hmm.
399 (defun mixi-url-encode-and-quote-percent-string (string)
400   (apply (function concat)
401          (mapcar
402           (lambda (char)
403             (cond
404              ((eq char ?\n)             ; newline
405               "%%0D%%0A")
406              ((string-match "[-a-zA-Z0-9_:/.]" (char-to-string char)) ; xxx?
407               (char-to-string char))    ; printable
408              ((char-equal char ?\x20)   ; space
409               "+")
410              (t
411               (format "%%%%%02x" char))))       ; escape
412           ;; Coerce a string into a list of chars.
413           (append (encode-coding-string (or string "") mixi-coding-system)
414                   nil))))
415
416 ;; Object.
417 (defconst mixi-object-prefix "mixi-")
418
419 (defmacro mixi-object-class (object)
420   `(car-safe ,object))
421
422 (defmacro mixi-object-p (object)
423   `(let ((class (mixi-object-class ,object)))
424      (when (symbolp class)
425        (eq (string-match (concat "^" mixi-object-prefix)
426                          (symbol-name class)) 0))))
427
428 (defun mixi-object-name (object)
429   "Return the name of OBJECT."
430   (unless (mixi-object-p object)
431     (signal 'wrong-type-argument (list 'mixi-object-p object)))
432   (let ((class (mixi-object-class object)))
433     (substring (symbol-name class) (length mixi-object-prefix))))
434
435 (defun mixi-read-object (exp)
436   "Read one Lisp expression as mixi object."
437   (if (mixi-object-p exp)
438       (let ((func (intern (concat mixi-object-prefix "make-"
439                                   (mixi-object-name exp))))
440             (args (mapcar (lambda (arg)
441                             (mixi-read-object arg))
442                           (cdr exp))))
443         (let ((object (apply func (cdr args))))
444           (when (car args)
445             (mixi-object-set-timestamp object (car args)))
446           object))
447     exp))
448
449 (defun mixi-object-timestamp (object)
450   "Return the timestamp of OJBECT."
451   (unless (mixi-object-p object)
452     (signal 'wrong-type-argument (list 'mixi-object-p object)))
453   (aref (cdr object) 0))
454 (defalias 'mixi-object-realize-p 'mixi-object-timestamp)
455
456 (defun mixi-object-owner (object)
457   "Return the owner of OBJECT."
458   (unless (mixi-object-p object)
459     (signal 'wrong-type-argument (list 'mixi-object-p object)))
460   (let ((func (intern (concat mixi-object-prefix
461                               (mixi-object-name object) "-owner"))))
462     (funcall func object)))
463
464 (defun mixi-object-id (object)
465   "Return the id of OBJECT."
466   (unless (mixi-object-p object)
467     (signal 'wrong-type-argument (list 'mixi-object-p object)))
468   (let ((func (intern (concat mixi-object-prefix
469                               (mixi-object-name object) "-id"))))
470     (funcall func object)))
471
472 (defun mixi-object-time (object)
473   "Return the time of OBJECT."
474   (unless (mixi-object-p object)
475     (signal 'wrong-type-argument (list 'mixi-object-p object)))
476   (let ((func (intern (concat mixi-object-prefix
477                               (mixi-object-name object) "-time"))))
478     (funcall func object)))
479
480 (defun mixi-object-title (object)
481   "Return the title of OBJECT."
482   (unless (mixi-object-p object)
483     (signal 'wrong-type-argument (list 'mixi-object-p object)))
484   (let ((func (intern (concat mixi-object-prefix
485                               (mixi-object-name object) "-title"))))
486     (funcall func object)))
487
488 (defun mixi-object-content (object)
489   "Return the content of OBJECT."
490   (unless (mixi-object-p object)
491     (signal 'wrong-type-argument (list 'mixi-object-p object)))
492   (let ((func (intern (concat mixi-object-prefix
493                               (mixi-object-name object) "-content"))))
494     (funcall func object)))
495
496 (defun mixi-object-set-timestamp (object timestamp)
497   "Set the timestamp of OBJECT."
498   (unless (mixi-object-p object)
499     (signal 'wrong-type-argument (list 'mixi-object-p object)))
500   (aset (cdr object) 0 timestamp))
501
502 (defmacro mixi-object-touch (object)
503   `(mixi-object-set-timestamp ,object (current-time)))
504
505 (defconst mixi-object-url-regexp
506   "/\\(show\\|view\\)_\\([a-z]+\\)\\.pl")
507
508 (defun mixi-make-object-from-url (url)
509   "Return a mixi object from URL."
510   (if (string-match mixi-object-url-regexp url)
511       (let ((name (match-string 2 url)))
512         (when (string= name "bbs")
513           (setq name "topic"))
514         (let ((func (intern (concat mixi-object-prefix "make-" name
515                                     "-from-url"))))
516           (funcall func url)))
517     (when (string-match "/home\\.pl" url)
518       (mixi-make-friend-from-url url))))
519
520 ;; Cache.
521 ;; stolen from time-date.el
522 (defun mixi-time-less-p (t1 t2)
523   "Say whether time value T1 is less than time value T2."
524   (unless (numberp (cdr t1))
525     (setq t1 (cons (car t1) (car (cdr t1)))))
526   (unless (numberp (cdr t2))
527     (setq t2 (cons (car t2) (car (cdr t2)))))
528   (or (< (car t1) (car t2))
529       (and (= (car t1) (car t2))
530            (< (cdr t1) (cdr t2)))))
531
532 (defun mixi-time-add (t1 t2)
533   "Add two time values.  One should represent a time difference."
534   (unless (numberp (cdr t1))
535     (setq t1 (cons (car t1) (car (cdr t1)))))
536   (unless (numberp (cdr t2))
537     (setq t2 (cons (car t2) (car (cdr t2)))))
538   (let ((low (+ (cdr t1) (cdr t2))))
539     (cons (+ (car t1) (car t2) (lsh low -16)) low)))
540
541 ;; stolen from time-date.el
542 (defun mixi-seconds-to-time (seconds)
543   "Convert SECONDS (a floating point number) to a time value."
544   (cons (floor seconds 65536)
545         (floor (mod seconds 65536))))
546
547 (defun mixi-cache-expired-p (object)
548   "Whether a cache of OBJECT is expired."
549   (let ((timestamp (mixi-object-timestamp object)))
550     (unless (or (null mixi-cache-expires)
551                  (null timestamp))
552       (if (numberp mixi-cache-expires)
553           (mixi-time-less-p
554            (mixi-time-add timestamp (mixi-seconds-to-time mixi-cache-expires))
555            (current-time))
556         t))))
557
558 (defun mixi-make-cache (key value table)
559   "Make a cache object and return it."
560   (let ((cache (gethash key table)))
561     (if (and cache (not (mixi-cache-expired-p cache)))
562         cache
563       (puthash key value table))))
564
565 (defconst mixi-cache-file-regexp "[a-z]+-cache$")
566 (defconst mixi-cache-regexp (concat mixi-object-prefix
567                                     mixi-cache-file-regexp))
568
569 (defun mixi-save-cache ()
570   (unless (file-directory-p mixi-directory)
571     (make-directory mixi-directory t))
572   (let ((caches (apropos-internal mixi-cache-regexp 'boundp)))
573     (mapc (lambda (symbol)
574             (with-temp-file (expand-file-name
575                              (substring (symbol-name symbol)
576                                         (length mixi-object-prefix))
577                              mixi-directory)
578               (let ((coding-system-for-write mixi-coding-system)
579                     (cache (symbol-value symbol)))
580                 (insert "#s(hash-table size "
581                         (number-to-string (hash-table-count cache))
582                         " test equal data (\n")
583                 (maphash
584                  (lambda (key value)
585                    (let (print-level print-length)
586                      (insert (prin1-to-string key) " "
587                              (prin1-to-string value) "\n")))
588                  cache))
589               (insert "))")))
590           caches)))
591
592 ;; stolen (and modified) from lsdb.el
593 (defun mixi-read-cache (&optional marker)
594   "Read one Lisp expression as text from MARKER, return as Lisp object."
595   (save-excursion
596     (goto-char marker)
597     (if (looking-at "^#s(")
598         (let ((end-marker
599                (progn
600                  (forward-char 2);skip "#s"
601                  (forward-sexp);move to the left paren
602                  (point-marker))))
603           (with-temp-buffer
604             (buffer-disable-undo)
605             (insert-buffer-substring (marker-buffer marker)
606                                      marker end-marker)
607             (goto-char (point-min))
608             (delete-char 2)
609             (let ((object (read (current-buffer)))
610                   data)
611               (if (eq 'hash-table (car object))
612                   (progn
613                     (setq data (plist-get (cdr object) 'data))
614                     (while data
615                       (pop data);throw it away
616                       (mixi-read-object (pop data))))
617                 object))))
618       (read marker))))
619
620 (defun mixi-load-cache ()
621   (when (file-directory-p mixi-directory)
622     ;; FIXME: Load friend and community first.
623     (let ((files (directory-files mixi-directory t mixi-cache-file-regexp)))
624       (mapc (lambda (file)
625               (let ((buffer (find-file-noselect file)))
626                 (unwind-protect
627                     (save-excursion
628                       (set-buffer buffer)
629                       (goto-char (point-min))
630                       (re-search-forward "^#s(")
631                       (goto-char (match-beginning 0))
632                       (mixi-read-cache (point-marker)))
633                   (kill-buffer buffer))))
634             files))))
635
636 ;; Friend object.
637 (defvar mixi-friend-cache (make-hash-table :test 'equal))
638 (defun mixi-make-friend (id &optional nick name sex address age birthday
639                             blood-type birthplace hobby job organization
640                             profile)
641   "Return a friend object."
642   (mixi-make-cache id (cons 'mixi-friend (vector nil id nick name sex address
643                                                  age birthday blood-type
644                                                  birthplace hobby job
645                                                  organization profile))
646                    mixi-friend-cache))
647
648 (defun mixi-make-me ()
649   "Return a my object."
650   (unless mixi-me
651     (with-mixi-retrieve "/home.pl"
652       (if (string-match mixi-my-id-regexp buffer)
653           (setq mixi-me (mixi-make-friend (match-string 1 buffer)))
654         (signal 'error (list 'who-am-i)))))
655   mixi-me)
656
657 (defconst mixi-friend-url-regexp
658   "/show_friend\\.pl\\?id=\\([0-9]+\\)")
659
660 (defun mixi-make-friend-from-url (url)
661   "Return a friend object from URL."
662   (if (string-match mixi-friend-url-regexp url)
663       (let ((id (match-string 1 url)))
664         (mixi-make-friend id))
665     (when (string-match "/home\\.pl" url)
666       (mixi-make-me))))
667
668 (defmacro mixi-friend-p (friend)
669   `(eq (mixi-object-class ,friend) 'mixi-friend))
670
671 (defmacro mixi-friend-page (friend)
672   `(concat "/show_friend.pl?id=" (mixi-friend-id ,friend)))
673
674 (defconst mixi-friend-nick-regexp
675   "<img alt=\"\\*\" src=\"http://img\\.mixi\\.jp/img/dot0\\.gif\" width=\"1\" height=\"5\"><br>\r?
676 \\(.*\\)¤µ¤ó([0-9]+)")
677 (defconst mixi-friend-name-regexp
678   "<td BGCOLOR=#F2DDB7 WIDTH=80 NOWRAP><font COLOR=#996600>̾\\(&nbsp;\\| \\)Á°</font></td>
679
680 ?<td WIDTH=345>\\(.+?\\)\\(</td>\\|<img\\)")
681 (defconst mixi-friend-sex-regexp
682   "<td BGCOLOR=#F2DDB7\\( WIDTH=80 NOWRAP\\)?><font COLOR=#996600>À­\\(&nbsp;\\| \\)ÊÌ</font></td>
683
684 ?<td WIDTH=345>\\([Ã˽÷]\\)À­\\(</td>\\|<img\\)")
685 (defconst mixi-friend-address-regexp
686   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>¸½½»½ê</font></td>\n<td>\\(.+\\)\\(\n.+\n\\)?</td></tr>")
687 (defconst mixi-friend-age-regexp
688   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>ǯ\\(&nbsp;\\| \\)Îð</font></td>\n<td>\\([0-9]+\\)ºÐ\\(\n.+\n\\)?</td></tr>")
689 (defconst mixi-friend-birthday-regexp
690   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>ÃÂÀ¸Æü</font></td>\n<td>\\([0-9]+\\)·î\\([0-9]+\\)Æü\\(\n.+\n\\)?</td></tr>")
691 (defconst mixi-friend-blood-type-regexp
692   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>·ì±Õ·¿</font></td>\n<td>\\([ABO]B?\\)·¿\\(\n\n\\)?</td></tr>")
693 (defconst mixi-friend-birthplace-regexp
694   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>½Ð¿ÈÃÏ</font>\n?</td>\n<td>\\(.+\\)\\(\n.+\n\\)?</td></tr>")
695 (defconst mixi-friend-hobby-regexp
696   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>¼ñ\\(&nbsp;\\| \\)Ì£</font></td>\n<td>\\(.+?\\)\\(</td>\\|<img\\)")
697 (defconst mixi-friend-job-regexp
698   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>¿¦\\(&nbsp;\\| \\)¶È</font></td>\n<td>\\(.+\\)\\(\n.+\n\\)?</td></tr>")
699 (defconst mixi-friend-organization-regexp
700   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>½ê\\(&nbsp;\\| \\)°</font></td>\n<td[^>]*>\\(.+\\)\\(\n.+\n\\)?</td></tr>")
701 (defconst mixi-friend-profile-regexp
702   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>¼«¸Ê¾Ò²ð</font></td>\n<td CLASS=h120>\\(.+\\)</td></tr>")
703
704 (defun mixi-friend-realize (friend)
705   "Realize a FRIEND."
706   ;; FIXME: Check a expiration of cache?
707   (unless (mixi-object-realize-p friend)
708     (let (buf)
709       (with-mixi-retrieve (mixi-friend-page friend)
710         (setq buf buffer))
711       (if (string-match mixi-friend-nick-regexp buf)
712           (mixi-friend-set-nick friend (match-string 1 buf))
713         (signal 'error (list 'cannot-find-nick friend)))
714       ;; For getting my profile.
715       (unless (string-match mixi-friend-name-regexp buf)
716         (with-mixi-retrieve (concat "/show_profile.pl?id="
717                                     (mixi-friend-id friend))
718           (setq buf buffer)))
719       (if (string-match mixi-friend-name-regexp buf)
720             (mixi-friend-set-name friend (match-string 2 buf))
721         (signal 'error (list 'cannot-find-name friend)))
722       (if (string-match mixi-friend-sex-regexp buf)
723           (mixi-friend-set-sex friend
724                                (if (string= (match-string 3 buf) "ÃË")
725                                    'male 'female))
726         (signal 'error (list 'cannot-find-sex friend)))
727       (when (string-match mixi-friend-address-regexp buf)
728         (mixi-friend-set-address friend (match-string 1 buf)))
729       (when (string-match mixi-friend-age-regexp buf)
730         (mixi-friend-set-age
731          friend (string-to-number (match-string 2 buf))))
732       (when (string-match mixi-friend-birthday-regexp buf)
733         (mixi-friend-set-birthday
734          friend (list (string-to-number (match-string 1 buf))
735                       (string-to-number (match-string 2 buf)))))
736       (when (string-match mixi-friend-blood-type-regexp buf)
737         (mixi-friend-set-blood-type friend (intern (match-string 1 buf))))
738       (when (string-match mixi-friend-birthplace-regexp buf)
739         (mixi-friend-set-birthplace friend (match-string 1 buf)))
740       (when (string-match mixi-friend-hobby-regexp buf)
741         (mixi-friend-set-hobby
742          friend (split-string (match-string 2 buf) ", ")))
743       (when (string-match mixi-friend-job-regexp buf)
744         (mixi-friend-set-job friend (match-string 2 buf)))
745       (when (string-match mixi-friend-organization-regexp buf)
746         (mixi-friend-set-organization friend (match-string 2 buf)))
747       (when (string-match mixi-friend-profile-regexp buf)
748         (mixi-friend-set-profile friend (match-string 1 buf))))
749     (mixi-object-touch friend)))
750
751 (defun mixi-friend-id (friend)
752   "Return the id of FRIEND."
753   (unless (mixi-friend-p friend)
754     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
755   (aref (cdr friend) 1))
756
757 (defun mixi-friend-nick (friend)
758   "Return the nick of FRIEND."
759   (unless (mixi-friend-p friend)
760     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
761   (unless (aref (cdr friend) 2)
762     (mixi-friend-realize friend))
763   (aref (cdr friend) 2))
764
765 (defun mixi-friend-name (friend)
766   "Return the name of FRIEND."
767   (unless (mixi-friend-p friend)
768     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
769   (mixi-friend-realize friend)
770   (aref (cdr friend) 3))
771
772 (defun mixi-friend-sex (friend)
773   "Return the sex of FRIEND."
774   (unless (mixi-friend-p friend)
775     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
776   (mixi-friend-realize friend)
777   (aref (cdr friend) 4))
778
779 (defun mixi-friend-address (friend)
780   "Return the address of FRIEND."
781   (unless (mixi-friend-p friend)
782     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
783   (mixi-friend-realize friend)
784   (aref (cdr friend) 5))
785
786 (defun mixi-friend-age (friend)
787   "Return the age of FRIEND."
788   (unless (mixi-friend-p friend)
789     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
790   (mixi-friend-realize friend)
791   (aref (cdr friend) 6))
792
793 (defun mixi-friend-birthday (friend)
794   "Return the birthday of FRIEND."
795   (unless (mixi-friend-p friend)
796     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
797   (mixi-friend-realize friend)
798   (aref (cdr friend) 7))
799
800 (defun mixi-friend-blood-type (friend)
801   "Return the blood type of FRIEND."
802   (unless (mixi-friend-p friend)
803     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
804   (mixi-friend-realize friend)
805   (aref (cdr friend) 8))
806
807 (defun mixi-friend-birthplace (friend)
808   "Return the birthplace of FRIEND."
809   (unless (mixi-friend-p friend)
810     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
811   (mixi-friend-realize friend)
812   (aref (cdr friend) 9))
813
814 (defun mixi-friend-hobby (friend)
815   "Return the hobby of FRIEND."
816   (unless (mixi-friend-p friend)
817     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
818   (mixi-friend-realize friend)
819   (aref (cdr friend) 10))
820
821 (defun mixi-friend-job (friend)
822   "Return the job of FRIEND."
823   (unless (mixi-friend-p friend)
824     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
825   (mixi-friend-realize friend)
826   (aref (cdr friend) 11))
827
828 (defun mixi-friend-organization (friend)
829   "Return the organization of FRIEND."
830   (unless (mixi-friend-p friend)
831     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
832   (mixi-friend-realize friend)
833   (aref (cdr friend) 12))
834
835 (defun mixi-friend-profile (friend)
836   "Return the pforile of FRIEND."
837   (unless (mixi-friend-p friend)
838     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
839   (mixi-friend-realize friend)
840   (aref (cdr friend) 13))
841
842 (defun mixi-friend-set-nick (friend nick)
843   "Set the nick of FRIEND."
844   (unless (mixi-friend-p friend)
845     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
846   (aset (cdr friend) 2 nick))
847
848 (defun mixi-friend-set-name (friend name)
849   "Set the name of FRIEND."
850   (unless (mixi-friend-p friend)
851     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
852   (aset (cdr friend) 3 name))
853
854 (defun mixi-friend-set-sex (friend sex)
855   "Set the sex of FRIEND."
856   (unless (mixi-friend-p friend)
857     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
858   (aset (cdr friend) 4 sex))
859
860 (defun mixi-friend-set-address (friend address)
861   "Set the address of FRIEND."
862   (unless (mixi-friend-p friend)
863     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
864   (aset (cdr friend) 5 address))
865
866 (defun mixi-friend-set-age (friend age)
867   "Set the age of FRIEND."
868   (unless (mixi-friend-p friend)
869     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
870   (aset (cdr friend) 6 age))
871
872 (defun mixi-friend-set-birthday (friend birthday)
873   "Set the birthday of FRIEND."
874   (unless (mixi-friend-p friend)
875     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
876   (aset (cdr friend) 7 birthday))
877
878 (defun mixi-friend-set-blood-type (friend blood-type)
879   "Set the blood type of FRIEND."
880   (unless (mixi-friend-p friend)
881     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
882   (aset (cdr friend) 8 blood-type))
883
884 (defun mixi-friend-set-birthplace (friend birthplace)
885   "Set the birthplace of FRIEND."
886   (unless (mixi-friend-p friend)
887     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
888   (aset (cdr friend) 9 birthplace))
889
890 (defun mixi-friend-set-hobby (friend hobby)
891   "Set the hobby of FRIEND."
892   (unless (mixi-friend-p friend)
893     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
894   (aset (cdr friend) 10 hobby))
895
896 (defun mixi-friend-set-job (friend job)
897   "Set the job of FRIEND."
898   (unless (mixi-friend-p friend)
899     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
900   (aset (cdr friend) 11 job))
901
902 (defun mixi-friend-set-organization (friend organization)
903   "Set the organization of FRIEND."
904   (unless (mixi-friend-p friend)
905     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
906   (aset (cdr friend) 12 organization))
907
908 (defun mixi-friend-set-profile (friend profile)
909   "Set the profile of FRIEND."
910   (unless (mixi-friend-p friend)
911     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
912   (aset (cdr friend) 13 profile))
913
914 (defmacro mixi-friend-list-page (&optional friend)
915   `(concat "/list_friend.pl?page=%d"
916            (when ,friend (concat "&id=" (mixi-friend-id ,friend)))))
917
918 (defconst mixi-friend-list-id-regexp
919   "<a href=show_friend\\.pl\\?id=\\([0-9]+\\)")
920 (defconst mixi-friend-list-nick-regexp
921   "<td valign=middle>\\(.+\\)¤µ¤ó([0-9]+)<br />")
922
923 (defun mixi-get-friends (&rest args)
924   "Get friends of FRIEND."
925   (when (> (length args) 2)
926     (signal 'wrong-number-of-arguments (list 'mixi-get-friends (length args))))
927   (let ((friend (nth 0 args))
928         (range (nth 1 args)))
929     (when (or (not (mixi-friend-p friend)) (mixi-friend-p range))
930       (setq friend (nth 1 args))
931       (setq range (nth 0 args)))
932     (unless (or (null friend) (mixi-friend-p friend))
933       (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
934     (let ((ids (mixi-get-matched-items (mixi-friend-list-page friend)
935                                        mixi-friend-list-id-regexp
936                                        range))
937           (nicks (mixi-get-matched-items (mixi-friend-list-page friend)
938                                          mixi-friend-list-nick-regexp
939                                          range)))
940       (let ((index 0)
941             ret)
942         (while (< index (length ids))
943           (setq ret (cons (mixi-make-friend (nth 0 (nth index ids))
944                                             (nth 0 (nth index nicks))) ret))
945           (incf index))
946         (reverse ret)))))
947
948 ;; Favorite.
949 (defmacro mixi-favorite-list-page ()
950   `(concat "/list_bookmark.pl?page=%d"))
951
952 (defconst mixi-favorite-list-id-regexp
953   "<td ALIGN=center BGCOLOR=#FDF9F2 width=330><a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">")
954 (defconst mixi-favorite-list-nick-regexp
955   "<td BGCOLOR=#FDF9F2><font COLOR=#996600>̾&nbsp;&nbsp;Á°</font></td>
956 <td COLSPAN=2 BGCOLOR=#FFFFFF>\\(.+\\)</td></tr>")
957
958 (defun mixi-get-favorites (&optional range)
959   "Get favorites."
960   (let ((ids (mixi-get-matched-items (mixi-favorite-list-page)
961                                      mixi-favorite-list-id-regexp
962                                      range))
963         (nicks (mixi-get-matched-items (mixi-favorite-list-page)
964                                        mixi-favorite-list-nick-regexp
965                                        range)))
966     (let ((index 0)
967           ret)
968       (while (< index (length ids))
969         (setq ret (cons (mixi-make-friend (nth 0 (nth index ids))
970                                           (nth 0 (nth index nicks))) ret))
971         (incf index))
972       (reverse ret))))
973
974 ;; Log object.
975 (defun mixi-make-log (friend time)
976   "Return a log object."
977   (cons 'mixi-log (vector friend time)))
978
979 (defmacro mixi-log-p (log)
980   `(eq (mixi-object-class ,log) 'mixi-log))
981
982 (defun mixi-log-friend (log)
983   "Return the friend of LOG."
984   (unless (mixi-log-p log)
985     (signal 'wrong-type-argument (list 'mixi-log-p log)))
986   (aref (cdr log) 0))
987
988 (defun mixi-log-time (log)
989   "Return the time of LOG."
990   (unless (mixi-log-p log)
991     (signal 'wrong-type-argument (list 'mixi-log-p log)))
992   (aref (cdr log) 1))
993
994 (defmacro mixi-log-list-page ()
995   `(concat "/show_log.pl"))
996
997 (defconst mixi-log-list-regexp
998   "\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü \\([0-9]+\\):\\([0-9]+\\) <a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.*\\)</a></li>")
999
1000 (defun mixi-get-logs (&optional range)
1001   "Get logs."
1002   (let ((items (mixi-get-matched-items (mixi-log-list-page)
1003                                        mixi-log-list-regexp
1004                                        range)))
1005     (mapcar (lambda (item)
1006               (mixi-make-log (mixi-make-friend (nth 5 item) (nth 6 item))
1007                              (encode-time 0
1008                                           (string-to-number (nth 4 item))
1009                                           (string-to-number (nth 3 item))
1010                                           (string-to-number (nth 2 item))
1011                                           (string-to-number (nth 1 item))
1012                                           (string-to-number (nth 0 item)))))
1013             items)))
1014
1015 ;; Diary object.
1016 (defvar mixi-diary-cache (make-hash-table :test 'equal))
1017 (defun mixi-make-diary (owner id &optional time title content)
1018   "Return a diary object."
1019   (let ((owner (or owner (mixi-make-me))))
1020     (mixi-make-cache (list (mixi-friend-id owner) id)
1021                      (cons 'mixi-diary (vector nil owner id time title
1022                                                content))
1023                      mixi-diary-cache)))
1024
1025 (defconst mixi-diary-url-regexp
1026   "/view_diary\\.pl\\?id=\\([0-9]+\\)&owner_id=\\([0-9]+\\)")
1027
1028 (defun mixi-make-diary-from-url (url)
1029   "Return a diary object from URL."
1030   (when (string-match mixi-diary-url-regexp url)
1031     (let ((id (match-string 1 url))
1032           (owner-id (match-string 2 url)))
1033       (mixi-make-diary (mixi-make-friend owner-id) id))))
1034
1035 (defmacro mixi-diary-p (diary)
1036   `(eq (mixi-object-class ,diary) 'mixi-diary))
1037
1038 (defmacro mixi-diary-page (diary)
1039   `(concat "/view_diary.pl?id=" (mixi-diary-id ,diary)
1040            "&owner_id=" (mixi-friend-id (mixi-diary-owner ,diary))))
1041
1042 (defconst mixi-diary-closed-regexp
1043   "<td>ͧ¿Í\\(¤Îͧ¿Í\\)?¤Þ¤Ç¸ø³«¤Î¤¿¤áÆɤळ¤È¤¬½ÐÍè¤Þ¤»¤ó¡£</td></tr>")
1044 (defconst mixi-diary-owner-nick-regexp
1045   "<td WIDTH=490 background=http://img\\.mixi\\.jp/img/bg_w\\.gif><b><font COLOR=#605048>\\(.+?\\)\\(¤µ¤ó\\)?¤ÎÆüµ­</font></b></td>")
1046 (defconst mixi-diary-time-regexp
1047   "<td \\(align\\|ALIGN\\)=\"?center\"? \\(rowspan\\|ROWSPAN\\)=\"?[23]\"? \\(nowrap=\"nowrap\"\\|NOWRAP\\) \\(width\\|WIDTH\\)=\"?95\"? bgcolor=\"?#FFD8B0\"?>\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü\\(<br />\\|<br>\\)\\([0-9]+\\):\\([0-9]+\\)</td>")
1048 (defconst mixi-diary-title-regexp
1049   "<td \\(bgcolor\\|BGCOLOR\\)=\"?#FFF4E0\"? width=\"?430\"?>&nbsp;\\([^<]+\\)</td>")
1050 (defconst mixi-diary-content-regexp
1051   "<td \\(class\\|CLASS\\)=\"?h12\"?>\\(.*\\)</td>")
1052
1053 (defun mixi-diary-realize (diary)
1054   "Realize a DIARY."
1055   ;; FIXME: Check a expiration of cache?
1056   (unless (mixi-object-realize-p diary)
1057     (with-mixi-retrieve (mixi-diary-page diary)
1058       (unless (string-match mixi-diary-closed-regexp buffer)
1059         (if (string-match mixi-diary-owner-nick-regexp buffer)
1060             (mixi-friend-set-nick (mixi-diary-owner diary)
1061                                   (match-string 1 buffer))
1062           (signal 'error (list 'cannot-find-owner-nick diary)))
1063         (if (string-match mixi-diary-time-regexp buffer)
1064             (mixi-diary-set-time
1065              diary (encode-time 0 (string-to-number (match-string 10 buffer))
1066                                 (string-to-number (match-string 9 buffer))
1067                                 (string-to-number (match-string 7 buffer))
1068                                 (string-to-number (match-string 6 buffer))
1069                                 (string-to-number (match-string 5 buffer))))
1070           (signal 'error (list 'cannot-find-time diary)))
1071         (if (string-match mixi-diary-title-regexp buffer)
1072             (mixi-diary-set-title diary (match-string 2 buffer))
1073           (signal 'error (list 'cannot-find-title diary)))
1074         (if (string-match mixi-diary-content-regexp buffer)
1075             (mixi-diary-set-content diary (match-string 2 buffer))
1076           (signal 'error (list 'cannot-find-content diary)))))
1077     (mixi-object-touch diary)))
1078
1079 (defun mixi-diary-owner (diary)
1080   "Return the owner of DIARY."
1081   (unless (mixi-diary-p diary)
1082     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1083   (aref (cdr diary) 1))
1084
1085 (defun mixi-diary-id (diary)
1086   "Return the id of DIARY."
1087   (unless (mixi-diary-p diary)
1088     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1089   (aref (cdr diary) 2))
1090
1091 (defun mixi-diary-time (diary)
1092   "Return the time of DIARY."
1093   (unless (mixi-diary-p diary)
1094     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1095   (mixi-diary-realize diary)
1096   (aref (cdr diary) 3))
1097
1098 (defun mixi-diary-title (diary)
1099   "Return the title of DIARY."
1100   (unless (mixi-diary-p diary)
1101     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1102   (mixi-diary-realize diary)
1103   (aref (cdr diary) 4))
1104
1105 (defun mixi-diary-content (diary)
1106   "Return the content of DIARY."
1107   (unless (mixi-diary-p diary)
1108     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1109   (mixi-diary-realize diary)
1110   (aref (cdr diary) 5))
1111
1112 (defun mixi-diary-set-time (diary time)
1113   "Set the time of DIARY."
1114   (unless (mixi-diary-p diary)
1115     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1116   (aset (cdr diary) 3 time))
1117
1118 (defun mixi-diary-set-title (diary title)
1119   "Set the title of DIARY."
1120   (unless (mixi-diary-p diary)
1121     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1122   (aset (cdr diary) 4 title))
1123
1124 (defun mixi-diary-set-content (diary content)
1125   "Set the content of DIARY."
1126   (unless (mixi-diary-p diary)
1127     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1128   (aset (cdr diary) 5 content))
1129
1130 (defmacro mixi-diary-list-page (&optional friend)
1131   `(concat "/list_diary.pl?page=%d"
1132            (when ,friend (concat "&id=" (mixi-friend-id ,friend)))))
1133
1134 (defconst mixi-diary-list-regexp
1135   "<a href=\"view_diary\\.pl\\?id=\\([0-9]+\\)&owner_id=[0-9]+\">")
1136
1137 (defun mixi-get-diaries (&rest args)
1138   "Get diaries of FRIEND."
1139   (when (> (length args) 2)
1140     (signal 'wrong-number-of-arguments
1141             (list 'mixi-get-diaries (length args))))
1142   (let ((friend (nth 0 args))
1143         (range (nth 1 args)))
1144     (when (or (not (mixi-friend-p friend)) (mixi-friend-p range))
1145       (setq friend (nth 1 args))
1146       (setq range (nth 0 args)))
1147     (unless (or (null friend) (mixi-friend-p friend))
1148       (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
1149     (let ((items (mixi-get-matched-items (mixi-diary-list-page friend)
1150                                          mixi-diary-list-regexp
1151                                          range)))
1152       (mapcar (lambda (item)
1153                 (mixi-make-diary friend (nth 0 item)))
1154               items))))
1155
1156 (defmacro mixi-new-diary-list-page ()
1157   `(concat "/new_friend_diary.pl?page=%d"))
1158
1159 (defconst mixi-new-diary-list-regexp
1160   "<a class=\"new_link\" href=view_diary\\.pl\\?id=\\([0-9]+\\)&owner_id=\\([0-9]+\\)>")
1161
1162 (defun mixi-get-new-diaries (&optional range)
1163   "Get new diaries."
1164   (let ((items (mixi-get-matched-items (mixi-new-diary-list-page)
1165                                        mixi-new-diary-list-regexp
1166                                        range)))
1167     (mapcar (lambda (item)
1168               (mixi-make-diary (mixi-make-friend (nth 1 item)) (nth 0 item)))
1169             items)))
1170
1171 (defmacro mixi-search-diary-list-page (keyword)
1172   `(concat "/search_diary.pl?page=%d&submit=search&keyword="
1173              (mixi-url-encode-and-quote-percent-string keyword)))
1174
1175 (defconst mixi-search-diary-list-regexp
1176   "<a href=\"view_diary\\.pl\\?id=\\([0-9]+\\)&owner_id=\\([0-9]+\\)\">")
1177
1178 (defun mixi-search-diaries (keyword &optional range)
1179   (let ((items (mixi-get-matched-items (mixi-search-diary-list-page keyword)
1180                                        mixi-search-diary-list-regexp
1181                                        range)))
1182     (mapcar (lambda (item)
1183               (mixi-make-diary (mixi-make-friend (nth 1 item)) (nth 0 item)))
1184             items)))
1185
1186 ;; Community object.
1187 (defvar mixi-community-cache (make-hash-table :test 'equal))
1188 (defun mixi-make-community (id &optional name birthday owner category members
1189                                open-level authority description)
1190   "Return a community object."
1191   (mixi-make-cache id (cons 'mixi-community (vector nil id name birthday owner
1192                                                     category members
1193                                                     open-level authority
1194                                                     description))
1195                    mixi-community-cache))
1196
1197 (defconst mixi-community-url-regexp
1198   "/view_community\\.pl\\?id=\\([0-9]+\\)")
1199
1200 (defun mixi-make-community-from-url (url)
1201   "Return a community object from URL."
1202   (when (string-match mixi-community-url-regexp url)
1203     (let ((id (match-string 1 url)))
1204       (mixi-make-community id))))
1205
1206 (defmacro mixi-community-p (community)
1207   `(eq (mixi-object-class ,community) 'mixi-community))
1208
1209 (defmacro mixi-community-page (community)
1210   `(concat "/view_community.pl?id=" (mixi-community-id ,community)))
1211
1212 ;; FIXME: Not community specific.
1213 (defconst mixi-community-nodata-regexp
1214   "^¥Ç¡¼¥¿¤¬¤¢¤ê¤Þ¤»¤ó")
1215 (defconst mixi-community-name-regexp
1216   "<td WIDTH=345>\\(.*\\)</td></tr>")
1217 (defconst mixi-community-birthday-regexp
1218   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>³«ÀßÆü</font></td>\r
1219 <td>\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü</td>")
1220 ;; FIXME: Care when the owner has seceded.
1221 (defconst mixi-community-owner-regexp
1222   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>´ÉÍý¿Í</font></td>\r
1223 <td>\r
1224 \r
1225 <a href=\"\\(home\\.pl\\|show_friend\\.pl\\?id=\\([0-9]+\\)\\)\">\r
1226 \\(.*\\)</a>")
1227 (defconst mixi-community-category-regexp
1228   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>¥«¥Æ¥´¥ê</font></td>\r
1229 <td>\\([^<]+\\)</td>")
1230 (defconst mixi-community-members-regexp
1231   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>¥á¥ó¥Ð¡¼¿ô</font></td>\r
1232 <td>\\([0-9]+\\)¿Í</td></tr>")
1233 (defconst mixi-community-open-level-regexp
1234   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>»²²Ã¾ò·ï¤È<br>¸ø³«¥ì¥Ù¥ë</font></td>\r
1235 <td>\\(.+\\)</td></tr>")
1236 (defconst mixi-community-authority-regexp
1237   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>¥È¥Ô¥Ã¥¯ºîÀ®¤Î¸¢¸Â</font></td>\r
1238 <td>\\(.+\\)</td></tr>")
1239 (defconst mixi-community-description-regexp
1240   "<td CLASS=h120>\\(.+\\)</td>")
1241
1242 (defun mixi-community-realize (community)
1243   "Realize a COMMUNITY."
1244   ;; FIXME: Check a expiration of cache?
1245   (unless (mixi-object-realize-p community)
1246     (with-mixi-retrieve (mixi-community-page community)
1247       (if (string-match mixi-community-nodata-regexp buffer)
1248           ;; FIXME: Set all members?
1249           (mixi-community-set-name community "¥Ç¡¼¥¿¤¬¤¢¤ê¤Þ¤»¤ó")
1250         (if (string-match mixi-community-name-regexp buffer)
1251             (mixi-community-set-name community (match-string 1 buffer))
1252           (signal 'error (list 'cannot-find-name community)))
1253         (if (string-match mixi-community-birthday-regexp buffer)
1254             (mixi-community-set-birthday
1255              community
1256              (encode-time 0 0 0 (string-to-number (match-string 3 buffer))
1257                           (string-to-number (match-string 2 buffer))
1258                           (string-to-number (match-string 1 buffer))))
1259           (signal 'error (list 'cannot-find-birthday community)))
1260         (if (string-match mixi-community-owner-regexp buffer)
1261             (if (string= (match-string 1 buffer) "home.pl")
1262                 (mixi-community-set-owner community (mixi-make-me))
1263               (mixi-community-set-owner
1264                community (mixi-make-friend (match-string 2 buffer)
1265                                            (match-string 3 buffer))))
1266           (signal 'error (list 'cannot-find-owner community)))
1267         (if (string-match mixi-community-category-regexp buffer)
1268             (mixi-community-set-category community (match-string 1 buffer))
1269           (signal 'error (list 'cannot-find-category community)))
1270         (if (string-match mixi-community-members-regexp buffer)
1271             (mixi-community-set-members
1272              community (string-to-number (match-string 1 buffer)))
1273           (signal 'error (list 'cannot-find-members community)))
1274         (if (string-match mixi-community-open-level-regexp buffer)
1275             (mixi-community-set-open-level community (match-string 1 buffer))
1276           (signal 'error (list 'cannot-find-open-level community)))
1277         (if (string-match mixi-community-authority-regexp buffer)
1278             (mixi-community-set-authority community (match-string 1 buffer))
1279           (signal 'error (list 'cannot-find-authority community)))
1280         (if (string-match mixi-community-description-regexp buffer)
1281             (mixi-community-set-description community (match-string 1 buffer))
1282           (signal 'error (list 'cannot-find-description community)))))
1283     (mixi-object-touch community)))
1284
1285 (defun mixi-community-id (community)
1286   "Return the id of COMMUNITY."
1287   (unless (mixi-community-p community)
1288     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1289   (aref (cdr community) 1))
1290
1291 (defun mixi-community-name (community)
1292   "Return the name of COMMUNITY."
1293   (unless (mixi-community-p community)
1294     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1295   (unless (aref (cdr community) 2)
1296     (mixi-community-realize community))
1297   (aref (cdr community) 2))
1298
1299 (defun mixi-community-birthday (community)
1300   "Return the birthday of COMMUNITY."
1301   (unless (mixi-community-p community)
1302     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1303   (mixi-community-realize community)
1304   (aref (cdr community) 3))
1305
1306 (defun mixi-community-owner (community)
1307   "Return the owner of COMMUNITY."
1308   (unless (mixi-community-p community)
1309     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1310   (mixi-community-realize community)
1311   (aref (cdr community) 4))
1312
1313 (defun mixi-community-category (community)
1314   "Return the category of COMMUNITY."
1315   (unless (mixi-community-p community)
1316     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1317   (mixi-community-realize community)
1318   (aref (cdr community) 5))
1319
1320 (defun mixi-community-members (community)
1321   "Return the members of COMMUNITY."
1322   (unless (mixi-community-p community)
1323     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1324   (mixi-community-realize community)
1325   (aref (cdr community) 6))
1326
1327 (defun mixi-community-open-level (community)
1328   "Return the open-level of COMMUNITY."
1329   (unless (mixi-community-p community)
1330     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1331   (mixi-community-realize community)
1332   (aref (cdr community) 7))
1333
1334 (defun mixi-community-authority (community)
1335   "Return the authority of COMMUNITY."
1336   (unless (mixi-community-p community)
1337     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1338   (mixi-community-realize community)
1339   (aref (cdr community) 8))
1340
1341 (defun mixi-community-description (community)
1342   "Return the description of COMMUNITY."
1343   (unless (mixi-community-p community)
1344     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1345   (mixi-community-realize community)
1346   (aref (cdr community) 9))
1347
1348 (defun mixi-community-set-name (community name)
1349   "Set the name of COMMUNITY."
1350   (unless (mixi-community-p community)
1351     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1352   (aset (cdr community) 2 name))
1353
1354 (defun mixi-community-set-birthday (community birthday)
1355   "Set the birthday of COMMUNITY."
1356   (unless (mixi-community-p community)
1357     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1358   (aset (cdr community) 3 birthday))
1359
1360 (defun mixi-community-set-owner (community owner)
1361   "Set the owner of COMMUNITY."
1362   (unless (mixi-community-p community)
1363     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1364   (unless (mixi-friend-p owner)
1365     (signal 'wrong-type-argument (list 'mixi-friend-p owner)))
1366   (aset (cdr community) 4 owner))
1367
1368 (defun mixi-community-set-category (community category)
1369   "Set the category of COMMUNITY."
1370   (unless (mixi-community-p community)
1371     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1372   (aset (cdr community) 5 category))
1373
1374 (defun mixi-community-set-members (community members)
1375   "Set the name of COMMUNITY."
1376   (unless (mixi-community-p community)
1377     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1378   (aset (cdr community) 6 members))
1379
1380 (defun mixi-community-set-open-level (community open-level)
1381   "Set the name of COMMUNITY."
1382   (unless (mixi-community-p community)
1383     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1384   (aset (cdr community) 7 open-level))
1385
1386 (defun mixi-community-set-authority (community authority)
1387   "Set the name of COMMUNITY."
1388   (unless (mixi-community-p community)
1389     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1390   (aset (cdr community) 8 authority))
1391
1392 (defun mixi-community-set-description (community description)
1393   "Set the name of COMMUNITY."
1394   (unless (mixi-community-p community)
1395     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1396   (aset (cdr community) 9 description))
1397
1398 (defmacro mixi-community-list-page (&optional friend)
1399   `(concat "/list_community.pl?page=%d"
1400            (when ,friend (concat "&id=" (mixi-friend-id ,friend)))))
1401
1402 (defconst mixi-community-list-id-regexp
1403   "<a href=view_community\\.pl\\?id=\\([0-9]+\\)")
1404 (defconst mixi-community-list-name-regexp
1405   "<td valign=middle>\\(.+\\)([0-9]+)</td>")
1406
1407 (defun mixi-get-communities (&rest args)
1408   "Get communities of FRIEND."
1409   (when (> (length args) 2)
1410     (signal 'wrong-number-of-arguments
1411             (list 'mixi-get-communities (length args))))
1412   (let ((friend (nth 0 args))
1413         (range (nth 1 args)))
1414     (when (or (not (mixi-friend-p friend)) (mixi-friend-p range))
1415       (setq friend (nth 1 args))
1416       (setq range (nth 0 args)))
1417     (unless (or (null friend) (mixi-friend-p friend))
1418       (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
1419     (let ((ids (mixi-get-matched-items (mixi-community-list-page friend)
1420                                        mixi-community-list-id-regexp
1421                                        range))
1422           (names (mixi-get-matched-items (mixi-community-list-page friend)
1423                                          mixi-community-list-name-regexp
1424                                          range)))
1425       (let ((index 0)
1426             ret)
1427         (while (< index (length ids))
1428           (setq ret (cons (mixi-make-community (nth 0 (nth index ids))
1429                                                (nth 0 (nth index names))) ret))
1430           (incf index))
1431         (reverse ret)))))
1432
1433 (defmacro mixi-search-community-list-page (keyword)
1434   `(concat "/search_community.pl?page=%d&&sort=date&type=com&submit=main"
1435            "&keyword=" (mixi-url-encode-and-quote-percent-string keyword)
1436            "&category_id=0"))
1437
1438 (defconst mixi-search-community-list-regexp
1439   "<td WIDTH=90 VALIGN=top ROWSPAN=4 ALIGN=center background=http://img\\.mixi\\.jp/img/bg_line\\.gif><a href=\"view_community\\.pl\\?id=\\([0-9]+\\)\"><img SRC=\"http://img-c[0-9]+\\.mixi\\.jp/photo/comm/[^.]+\\.jpg\" VSPACE=3 border=0></a></td>
1440 <td NOWRAP WIDTH=90 BGCOLOR=#FDF9F2><font COLOR=#996600>¥³¥ß¥å¥Ë¥Æ¥£Ì¾</font></td>
1441 <td COLSPAN=2 WIDTH=370 BGCOLOR=#FFFFFF>\\([^<]+\\)</td></tr>")
1442
1443 ;; FIXME: Support category.
1444 (defun mixi-search-communities (keyword &optional range)
1445   (let ((items (mixi-get-matched-items (mixi-search-community-list-page
1446                                         keyword)
1447                                        mixi-search-community-list-regexp
1448                                        range)))
1449     (mapcar (lambda (item)
1450               (mixi-make-community (nth 0 item) (nth 1 item)))
1451             items)))
1452
1453 ;; Topic object.
1454 (defvar mixi-topic-cache (make-hash-table :test 'equal))
1455 (defun mixi-make-topic (community id &optional time title owner content)
1456   "Return a topic object."
1457   (mixi-make-cache (list (mixi-community-id community) id)
1458                    (cons 'mixi-topic (vector nil community id time title owner
1459                                              content))
1460                    mixi-topic-cache))
1461
1462 (defconst mixi-topic-url-regexp
1463   "/view_bbs\\.pl\\?id=\\([0-9]+\\)\\(&comment_count=[0-9]+\\)?&comm_id=\\([0-9]+\\)")
1464
1465 (defun mixi-make-topic-from-url (url)
1466   "Return a topic object from URL."
1467   (when (string-match mixi-topic-url-regexp url)
1468     (let ((id (match-string 1 url))
1469           (community-id (match-string 3 url)))
1470       (mixi-make-topic (mixi-make-community community-id) id))))
1471
1472 (defmacro mixi-topic-p (topic)
1473   `(eq (mixi-object-class ,topic) 'mixi-topic))
1474
1475 (defmacro mixi-topic-page (topic)
1476   `(concat "/view_bbs.pl?id=" (mixi-topic-id ,topic)
1477            "&comm_id=" (mixi-community-id (mixi-topic-community ,topic))))
1478
1479 (defconst mixi-topic-community-regexp
1480   "<td width=\"595\" background=\"http://img\\.mixi\\.jp/img/bg_w\\.gif\"><b>\\[\\(.+\\)\\] ¥È¥Ô¥Ã¥¯</b></td>")
1481 (defconst mixi-topic-time-regexp
1482   "<td rowspan=\"3\" width=\"110\" bgcolor=\"#ffd8b0\" align=\"center\" valign=\"top\" nowrap>\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü<br>\\([0-9]+\\):\\([0-9]+\\)</td>")
1483 (defconst mixi-topic-title-regexp
1484   "<td bgcolor=\"#fff4e0\">&nbsp;\\([^<]+\\)</td>")
1485 (defconst mixi-topic-owner-regexp
1486   "<td bgcolor=\"#fdf9f2\">&nbsp;<font color=\"#dfb479\"></font>&nbsp;<a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.*?\\)\\(¤µ¤ó\\)?</a>")
1487 (defconst mixi-topic-content-regexp
1488   "<table width=\"500\" border=\"0\" cellspacing=\"0\" cellpadding=\"5\"><tr><td class=\"h120\"><table><tr>\\(<td width=\"130\" height=\"140\" align=\"center\" valign=\"middle\"><a href=\"javascript:void(0)\" onClick=\"MM_openBrWindow('show_bbs_picture\\.pl\\?id=[0-9]+&number=[0-9]+','pict','width=680,height=660,toolbar=no,scrollbars=yes,left=5,top=5')\"><img src=\"http://ic[0-9]+\\.mixi\\.jp/[^.]+\\.jpg\" border=\"0\"></a></td>\n\\)*</tr></table>\\(.+\\)</td></tr></table>")
1489
1490 (defun mixi-topic-realize (topic)
1491   "Realize a TOPIC."
1492   ;; FIXME: Check a expiration of cache?
1493   (unless (mixi-object-realize-p topic)
1494     (with-mixi-retrieve (mixi-topic-page topic)
1495       (if (string-match mixi-topic-community-regexp buffer)
1496           (mixi-community-set-name (mixi-topic-community topic)
1497                                    (match-string 1 buffer))
1498         (signal 'error (list 'cannot-find-community topic)))
1499       (if (string-match mixi-topic-time-regexp buffer)
1500           (mixi-topic-set-time
1501            topic (encode-time 0 (string-to-number (match-string 5 buffer))
1502                               (string-to-number (match-string 4 buffer))
1503                               (string-to-number (match-string 3 buffer))
1504                               (string-to-number (match-string 2 buffer))
1505                               (string-to-number (match-string 1 buffer))))
1506         (signal 'error (list 'cannot-find-time topic)))
1507       (if (string-match mixi-topic-title-regexp buffer)
1508           (mixi-topic-set-title topic (match-string 1 buffer))
1509         (signal 'error (list 'cannot-find-title topic)))
1510       (if (string-match mixi-topic-owner-regexp buffer)
1511           (mixi-topic-set-owner topic
1512                                 (mixi-make-friend (match-string 1 buffer)
1513                                                   (match-string 2 buffer)))
1514         (signal 'error (list 'cannot-find-owner topic)))
1515       (if (string-match mixi-topic-content-regexp buffer)
1516           (mixi-topic-set-content topic (match-string 2 buffer))
1517         (signal 'error (list 'cannot-find-content topic))))
1518     (mixi-object-touch topic)))
1519
1520 (defun mixi-topic-community (topic)
1521   "Return the community of TOPIC."
1522   (unless (mixi-topic-p topic)
1523     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1524   (aref (cdr topic) 1))
1525
1526 (defun mixi-topic-id (topic)
1527   "Return the id of TOPIC."
1528   (unless (mixi-topic-p topic)
1529     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1530   (aref (cdr topic) 2))
1531
1532 (defun mixi-topic-time (topic)
1533   "Return the time of TOPIC."
1534   (unless (mixi-topic-p topic)
1535     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1536   (mixi-topic-realize topic)
1537   (aref (cdr topic) 3))
1538
1539 (defun mixi-topic-title (topic)
1540   "Return the title of TOPIC."
1541   (unless (mixi-topic-p topic)
1542     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1543   (mixi-topic-realize topic)
1544   (aref (cdr topic) 4))
1545
1546 (defun mixi-topic-owner (topic)
1547   "Return the owner of TOPIC."
1548   (unless (mixi-topic-p topic)
1549     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1550   (mixi-topic-realize topic)
1551   (aref (cdr topic) 5))
1552
1553 (defun mixi-topic-content (topic)
1554   "Return the content of TOPIC."
1555   (unless (mixi-topic-p topic)
1556     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1557   (mixi-topic-realize topic)
1558   (aref (cdr topic) 6))
1559
1560 (defun mixi-topic-set-time (topic time)
1561   "Set the time of TOPIC."
1562   (unless (mixi-topic-p topic)
1563     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1564   (aset (cdr topic) 3 time))
1565
1566 (defun mixi-topic-set-title (topic title)
1567   "Set the title of TOPIC."
1568   (unless (mixi-topic-p topic)
1569     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1570   (aset (cdr topic) 4 title))
1571
1572 (defun mixi-topic-set-owner (topic owner)
1573   "Set the owner of TOPIC."
1574   (unless (mixi-topic-p topic)
1575     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1576   (unless (mixi-friend-p owner)
1577     (signal 'wrong-type-argument (list 'mixi-friend-p owner)))
1578   (aset (cdr topic) 5 owner))
1579
1580 (defun mixi-topic-set-content (topic content)
1581   "Set the content of TOPIC."
1582   (unless (mixi-topic-p topic)
1583     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1584   (aset (cdr topic) 6 content))
1585
1586 ;; Event object.
1587 (defvar mixi-event-cache (make-hash-table :test 'equal))
1588 (defun mixi-make-event (community id &optional time title owner date place
1589                                   detail limit members)
1590   "Return a event object."
1591   (mixi-make-cache (list (mixi-community-id community) id)
1592                    (cons 'mixi-event (vector nil community id time title owner
1593                                              date place detail limit members))
1594                    mixi-event-cache))
1595
1596 (defconst mixi-event-url-regexp
1597   "/view_event\\.pl\\?id=\\([0-9]+\\)\\(&comment_count=[0-9]+\\)?&comm_id=\\([0-9]+\\)")
1598
1599 (defun mixi-make-event-from-url (url)
1600   "Return a event object from URL."
1601   (when (string-match mixi-event-url-regexp url)
1602     (let ((id (match-string 1 url))
1603           (community-id (match-string 3 url)))
1604       (mixi-make-event (mixi-make-community community-id) id))))
1605
1606 (defmacro mixi-event-p (event)
1607   `(eq (mixi-object-class ,event) 'mixi-event))
1608
1609 (defmacro mixi-event-page (event)
1610   `(concat "/view_event.pl?id=" (mixi-event-id ,event)
1611            "&comm_id=" (mixi-community-id (mixi-event-community ,event))))
1612
1613 (defconst mixi-event-community-regexp
1614   "<td WIDTH=595 background=http://img\\.mixi\\.jp/img/bg_w\\.gif><b>\\[\\(.+\\)\\] ¥¤¥Ù¥ó¥È</b></td>")
1615 (defconst mixi-event-time-regexp
1616   "<td ROWSPAN=11 BGCOLOR=#FFD8B0 ALIGN=center VALIGN=top WIDTH=110>
1617 \\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü<br>
1618 \\([0-9]+\\):\\([0-9]+\\)</td>")
1619 (defconst mixi-event-title-regexp
1620   "<td bgcolor=#FFF4E0 width=410>&nbsp;\\([^<]+\\)</td>")
1621 (defconst mixi-event-owner-regexp
1622   "<td BGCOLOR=#FDF9F2>&nbsp;<a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.*\\)</a>")
1623 (defconst mixi-event-date-regexp
1624   "<td BGCOLOR=#FFFFFF ALIGN=center NOWRAP>³«ºÅÆü»þ</td>
1625 <td BGCOLOR=#FFFFFF>
1626 &nbsp;\\(.+\\)
1627 </td>")
1628 (defconst mixi-event-place-regexp
1629   "<td BGCOLOR=#FFFFFF ALIGN=center NOWRAP>³«ºÅ¾ì½ê</td>
1630 <td BGCOLOR=#FFFFFF>
1631 &nbsp;\\(.+\\)
1632 </td>")
1633 (defconst mixi-event-detail-regexp
1634   "<td BGCOLOR=#FFFFFF ALIGN=center NOWRAP>¾ÜºÙ</td>
1635 <td BGCOLOR=#FFFFFF><table BORDER=0 CELLSPACING=0 CELLPADDING=5><tr><td CLASS=h120>\\(.+\\)</td></tr></table></td>")
1636 (defconst mixi-event-limit-regexp
1637   "<td BGCOLOR=#FFFFFF ALIGN=center NOWRAP>Ê罸´ü¸Â</td>
1638 <td BGCOLOR=#FFFFFF>&nbsp;\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü</td>")
1639 (defconst mixi-event-members-regexp
1640   "<td BGCOLOR=#FFFFFF ALIGN=center NOWRAP>»²²Ã¼Ô</td>
1641 <td BGCOLOR=#FFFFFF>
1642 <table BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH=100%>
1643 <tr>
1644 <td>&nbsp;\\(.+\\)</td>")
1645
1646 (defun mixi-event-realize (event)
1647   "Realize a EVENT."
1648   ;; FIXME: Check a expiration of cache?
1649   (unless (mixi-object-realize-p event)
1650     (with-mixi-retrieve (mixi-event-page event)
1651       (if (string-match mixi-event-community-regexp buffer)
1652           (mixi-community-set-name (mixi-event-community event)
1653                                    (match-string 1 buffer))
1654         (signal 'error (list 'cannot-find-title event)))
1655       (if (string-match mixi-event-time-regexp buffer)
1656           (mixi-event-set-time
1657            event (encode-time 0 (string-to-number (match-string 5 buffer))
1658                               (string-to-number (match-string 4 buffer))
1659                               (string-to-number (match-string 3 buffer))
1660                               (string-to-number (match-string 2 buffer))
1661                               (string-to-number (match-string 1 buffer))))
1662         (signal 'error (list 'cannot-find-time event)))
1663       (if (string-match mixi-event-title-regexp buffer)
1664           (mixi-event-set-title event (match-string 1 buffer))
1665         (signal 'error (list 'cannot-find-title event)))
1666       (if (string-match mixi-event-owner-regexp buffer)
1667           (mixi-event-set-owner event
1668                                 (mixi-make-friend (match-string 1 buffer)
1669                                                   (match-string 2 buffer)))
1670         (signal 'error (list 'cannot-find-owner event)))
1671       (if (string-match mixi-event-date-regexp buffer)
1672           (mixi-event-set-date event (match-string 1 buffer))
1673         (signal 'error (list 'cannot-find-date event)))
1674       (if (string-match mixi-event-place-regexp buffer)
1675           (mixi-event-set-place event (match-string 1 buffer))
1676         (signal 'error (list 'cannot-find-place event)))
1677       (if (string-match mixi-event-detail-regexp buffer)
1678           (mixi-event-set-detail event (match-string 1 buffer))
1679         (signal 'error (list 'cannot-find-detail event)))
1680       (when (string-match mixi-event-limit-regexp buffer)
1681         (mixi-event-set-limit
1682          event (encode-time 0 0 0 (string-to-number (match-string 3 buffer))
1683                             (string-to-number (match-string 2 buffer))
1684                             (string-to-number (match-string 1 buffer)))))
1685       (if (string-match mixi-event-members-regexp buffer)
1686           (mixi-event-set-members event (match-string 1 buffer))
1687         (signal 'error (list 'cannot-find-members event))))
1688     (mixi-object-touch event)))
1689
1690 (defun mixi-event-community (event)
1691   "Return the community of EVENT."
1692   (unless (mixi-event-p event)
1693     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1694   (aref (cdr event) 1))
1695
1696 (defun mixi-event-id (event)
1697   "Return the id of EVENT."
1698   (unless (mixi-event-p event)
1699     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1700   (aref (cdr event) 2))
1701
1702 (defun mixi-event-time (event)
1703   "Return the time of EVENT."
1704   (unless (mixi-event-p event)
1705     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1706   (mixi-event-realize event)
1707   (aref (cdr event) 3))
1708
1709 (defun mixi-event-title (event)
1710   "Return the title of EVENT."
1711   (unless (mixi-event-p event)
1712     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1713   (mixi-event-realize event)
1714   (aref (cdr event) 4))
1715
1716 (defun mixi-event-owner (event)
1717   "Return the owner of EVENT."
1718   (unless (mixi-event-p event)
1719     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1720   (mixi-event-realize event)
1721   (aref (cdr event) 5))
1722
1723 (defun mixi-event-date (event)
1724   "Return the date of EVENT."
1725   (unless (mixi-event-p event)
1726     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1727   (mixi-event-realize event)
1728   (aref (cdr event) 6))
1729
1730 (defun mixi-event-place (event)
1731   "Return the place of EVENT."
1732   (unless (mixi-event-p event)
1733     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1734   (mixi-event-realize event)
1735   (aref (cdr event) 7))
1736
1737 (defun mixi-event-detail (event)
1738   "Return the detail of EVENT."
1739   (unless (mixi-event-p event)
1740     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1741   (mixi-event-realize event)
1742   (aref (cdr event) 8))
1743
1744 (defun mixi-event-limit (event)
1745   "Return the limit of EVENT."
1746   (unless (mixi-event-p event)
1747     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1748   (mixi-event-realize event)
1749   (aref (cdr event) 9))
1750
1751 (defun mixi-event-members (event)
1752   "Return the members of EVENT."
1753   (unless (mixi-event-p event)
1754     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1755   (mixi-event-realize event)
1756   (aref (cdr event) 10))
1757
1758 (defun mixi-event-set-time (event time)
1759   "Set the time of EVENT."
1760   (unless (mixi-event-p event)
1761     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1762   (aset (cdr event) 3 time))
1763
1764 (defun mixi-event-set-title (event title)
1765   "Set the title of EVENT."
1766   (unless (mixi-event-p event)
1767     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1768   (aset (cdr event) 4 title))
1769
1770 (defun mixi-event-set-owner (event owner)
1771   "Set the owner of EVENT."
1772   (unless (mixi-event-p event)
1773     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1774   (unless (mixi-friend-p owner)
1775     (signal 'wrong-type-argument (list 'mixi-friend-p owner)))
1776   (aset (cdr event) 5 owner))
1777
1778 (defun mixi-event-set-date (event date)
1779   "Set the date of EVENT."
1780   (unless (mixi-event-p event)
1781     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1782   (aset (cdr event) 6 date))
1783
1784 (defun mixi-event-set-place (event place)
1785   "Set the place of EVENT."
1786   (unless (mixi-event-p event)
1787     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1788   (aset (cdr event) 7 place))
1789
1790 (defun mixi-event-set-detail (event detail)
1791   "Set the detail of EVENT."
1792   (unless (mixi-event-p event)
1793     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1794   (aset (cdr event) 8 detail))
1795
1796 (defun mixi-event-set-limit (event limit)
1797   "Set the limit of EVENT."
1798   (unless (mixi-event-p event)
1799     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1800   (aset (cdr event) 9 limit))
1801
1802 (defun mixi-event-set-members (event members)
1803   "Set the members of EVENT."
1804   (unless (mixi-event-p event)
1805     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1806   (aset (cdr event) 10 members))
1807
1808 ;; Bbs object.
1809 (defconst mixi-bbs-list '(mixi-topic mixi-event))
1810
1811 (defmacro mixi-bbs-p (object)
1812   `(when (memq (mixi-object-class ,object) mixi-bbs-list)
1813      t))
1814
1815 (defun mixi-bbs-community (object)
1816   "Return the community of OBJECT."
1817   (unless (mixi-bbs-p object)
1818     (signal 'wrong-type-argument (list 'mixi-bbs-p object)))
1819   (let ((func (intern (concat mixi-object-prefix
1820                               (mixi-object-name object) "-community"))))
1821     (funcall func object)))
1822
1823 (defalias 'mixi-bbs-id 'mixi-object-id)
1824 (defalias 'mixi-bbs-time 'mixi-object-time)
1825 (defalias 'mixi-bbs-title 'mixi-object-title)
1826 (defalias 'mixi-bbs-owner 'mixi-object-owner)
1827 (defalias 'mixi-bbs-content 'mixi-object-content)
1828
1829 (defmacro mixi-bbs-list-page (community)
1830   `(concat "/list_bbs.pl?page=%d"
1831            "&id=" (mixi-community-id ,community)))
1832
1833 (defconst mixi-bbs-list-regexp
1834   "<a href=view_\\(bbs\\|event\\)\\.pl\\?id=\\([0-9]+\\)")
1835
1836 (defun mixi-get-bbses (community &optional range)
1837   "Get bbese of COMMUNITY."
1838   (unless (mixi-community-p community)
1839     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1840   (let ((items (mixi-get-matched-items (mixi-bbs-list-page community)
1841                                        mixi-bbs-list-regexp
1842                                        range)))
1843     (mapcar (lambda (item)
1844               (let ((name (nth 0 item)))
1845                 (when (string= name "bbs")
1846                   (setq name "topic"))
1847                 (let ((func (intern (concat "mixi-make-" name))))
1848                   (funcall func community (nth 1 item)))))
1849             items)))
1850
1851 (defmacro mixi-new-bbs-list-page ()
1852   `(concat "/new_bbs.pl?page=%d"))
1853
1854 (defconst mixi-new-bbs-list-regexp
1855   "<a href=\"view_\\(bbs\\|event\\)\\.pl\\?id=\\([0-9]+\\)&comment_count=[0-9]+&comm_id=\\([0-9]+\\)\" class=\"new_link\">")
1856
1857 (defun mixi-get-new-bbses (&optional range)
1858   "Get new topics."
1859   (let ((items (mixi-get-matched-items (mixi-new-bbs-list-page)
1860                                        mixi-new-bbs-list-regexp
1861                                        range)))
1862     (mapcar (lambda (item)
1863               (let ((name (nth 0 item)))
1864                 (when (string= name "bbs")
1865                   (setq name "topic"))
1866                 (let ((func (intern (concat "mixi-make-" name))))
1867                   (funcall func (mixi-make-community (nth 2 item))
1868                            (nth 1 item)))))
1869             items)))
1870
1871 (defmacro mixi-search-bbs-list-page (keyword)
1872   `(concat "/search_topic.pl?page=%d&type=top&submit=search"
1873            "&keyword=" (mixi-url-encode-and-quote-percent-string keyword)
1874            "&community_id=0&category_id=0"))
1875
1876 (defconst mixi-search-bbs-list-regexp
1877   "<a href=\"view_\\(bbs\\|event\\)\\.pl\\?id=\\([0-9]+\\)&comm_id=\\([0-9]+\\)\"><img src=http://img\\.mixi\\.jp/img/shbtn\\.gif ALT=¾ÜºÙ¤ò¸«¤ë BORDER=0 WIDTH=104 HEIGHT=19></a>")
1878
1879 ;; FIXME: Support community and category.
1880 (defun mixi-search-bbses (keyword &optional range)
1881   (let ((items (mixi-get-matched-items (mixi-search-bbs-list-page keyword)
1882                                        mixi-search-bbs-list-regexp
1883                                        range)))
1884     (mapcar (lambda (item)
1885               (let ((name (nth 0 item)))
1886                 (when (string= name "bbs")
1887                   (setq name "topic"))
1888                 (let ((func (intern (concat "mixi-make-" name))))
1889                   (funcall func (mixi-make-community (nth 2 item))
1890                            (nth 1 item)))))
1891             items)))
1892
1893 ;; Comment object.
1894 (defun mixi-make-comment (parent owner time content)
1895   "Return a comment object."
1896   (cons 'mixi-comment (vector parent owner time content)))
1897
1898 (defmacro mixi-comment-p (comment)
1899   `(eq (mixi-object-class ,comment) 'mixi-comment))
1900
1901 (defun mixi-comment-parent (comment)
1902   "Return the parent of COMMENT."
1903   (unless (mixi-comment-p comment)
1904     (signal 'wrong-type-argument (list 'mixi-comment-p comment)))
1905   (aref (cdr comment) 0))
1906
1907 (defun mixi-comment-owner (comment)
1908   "Return the owner of COMMENT."
1909   (unless (mixi-comment-p comment)
1910     (signal 'wrong-type-argument (list 'mixi-comment-p comment)))
1911   (aref (cdr comment) 1))
1912
1913 (defun mixi-comment-time (comment)
1914   "Return the time of COMMENT."
1915   (unless (mixi-comment-p comment)
1916     (signal 'wrong-type-argument (list 'mixi-comment-p comment)))
1917   (aref (cdr comment) 2))
1918
1919 (defun mixi-comment-content (comment)
1920   "Return the content of COMMENT."
1921   (unless (mixi-comment-p comment)
1922     (signal 'wrong-type-argument (list 'mixi-comment-p comment)))
1923   (aref (cdr comment) 3))
1924
1925 (defun mixi-diary-comment-list-page (diary)
1926   (concat "/view_diary.pl?full=1"
1927           "&id=" (mixi-diary-id diary)
1928           "&owner_id=" (mixi-friend-id (mixi-diary-owner diary))))
1929
1930 ;; FIXME: Split regexp to time, owner(id and nick) and contents.
1931 (defconst mixi-diary-comment-list-regexp
1932 "<td rowspan=\"2\" align=\"center\" width=\"95\" bgcolor=\"#f2ddb7\" nowrap>
1933 \\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü<br>\\([0-9]+\\):\\([0-9]+\\)\\(<br>
1934 <input type=checkbox name=comment_id value=\".+\">
1935 \\|\\)
1936 </td>
1937 <td ALIGN=center BGCOLOR=#FDF9F2 WIDTH=430>
1938 <table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"410\">
1939 <tr>
1940 \\(<td>\\)
1941 <a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.*\\)</a>
1942
1943 \\(<font color=\"#f2ddb7\">|</font> <a href=[^>]+>ºï½ü</a>
1944
1945 \\|\\)</td>
1946 </tr>
1947 </table>
1948 </td>
1949 </tr>
1950 <!-- ËÜʸ : start -->
1951 <tr>
1952 <td bgcolor=\"#ffffff\">
1953 <table BORDER=0 CELLSPACING=0 CELLPADDING=[35] WIDTH=410>
1954 <tr>
1955 <td CLASS=h12>
1956 \\(.+\\)
1957 </td></tr></table>")
1958
1959 (defun mixi-topic-comment-list-page (topic)
1960   (concat "/view_bbs.pl?page=all"
1961           "&id=" (mixi-topic-id topic)
1962           "&comm_id=" (mixi-community-id (mixi-topic-community topic))))
1963
1964 ;; FIXME: Split regexp to time, owner(id and nick) and contents.
1965 (defconst mixi-topic-comment-list-regexp
1966   "<tr valign=\"top\">
1967 <td rowspan=\"2\" width=\"110\" bgcolor=\"#f2ddb7\" align=\"center\" nowrap>
1968 \\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü<br>
1969 \\([0-9]+\\):\\([0-9]+\\)<br>
1970 \\(<input type=\"checkbox\" name=\"comment_id\" value=\".+\">
1971 \\|\\)</td>
1972 <td bgcolor=\"#fdf9f2\">&nbsp;<font color=\"#f8a448\">
1973 <b>[^<]+</b>:</font>&nbsp;
1974 \\(
1975 \\|\\) *<a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.*\\)</a>
1976
1977 ?\\(
1978
1979 \\|<font color=\"#f2ddb7\">|&nbsp;</font><a href=\"delete_bbs_comment\\.pl\\?id=[0-9]+&comm_id=[0-9]+&comment_id=[0-9]+\">ºï½ü</a>
1980 \\|\\)</td>
1981 </tr>
1982 <tr>
1983 <td bgcolor=\"#ffffff\" align=\"center\">
1984 <table border=\"0\" cellspacing=\"0\" cellpadding=\"5\" width=\"500\">
1985 <tr>
1986 <td class=\"h120\">
1987
1988 \\(.+\\)
1989 </td>
1990 </tr>
1991 </table>
1992 </td>
1993 </tr>")
1994
1995 (defun mixi-event-comment-list-page (event)
1996   (concat "/view_event.pl?page=all"
1997           "&id=" (mixi-event-id event)
1998           "&comm_id=" (mixi-community-id (mixi-event-community event))))
1999
2000 ;; FIXME: Split regexp to time, owner(id and nick) and contents.
2001 (defconst mixi-event-comment-list-regexp
2002   "<tr>
2003 <td ROWSPAN=2 ALIGN=center BGCOLOR=#F2DDB7 WIDTH=110>
2004 \\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü<br>
2005 \\([0-9]+\\):\\([0-9]+\\)<br>
2006 \\(</td>\\)
2007 \\(<td BGCOLOR=#FDF9F2>\\)
2008 <font COLOR=#F8A448><b>[^<]+</b> :</font>
2009 <a HREF=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.*\\)</a>
2010
2011 </td>
2012 </tr>
2013 \\(<tr>\\)
2014 <td ALIGN=center BGCOLOR=#FFFFFF>
2015 <table BORDER=0 CELLSPACING=0 CELLPADDING=5 WIDTH=500>
2016 <tr><td CLASS=h120>\\(.+\\)</td></tr>
2017 </table>
2018 </td>
2019 </tr>")
2020
2021 (defun mixi-get-comments (parent &optional range)
2022   "Get comments of PARENT."
2023   (unless (mixi-object-p parent)
2024     (signal 'wrong-type-argument (list 'mixi-object-p parent)))
2025   (let* ((name (mixi-object-name parent))
2026          (list-page (intern (concat mixi-object-prefix name
2027                                     "-comment-list-page")))
2028          (regexp (eval (intern (concat mixi-object-prefix name
2029                                        "-comment-list-regexp")))))
2030     (let ((items (mixi-get-matched-items
2031                   (funcall list-page parent) regexp)))
2032       (let (list)
2033         (catch 'stop
2034           (mapc (lambda (item)
2035                   (when (and (numberp range)
2036                              (>= (length list) range))
2037                     (throw 'stop nil))
2038                   (setq list (cons item list)))
2039                 (reverse items)))
2040         (setq items (reverse list)))
2041       (mapcar (lambda (item)
2042                 (mixi-make-comment parent (mixi-make-friend
2043                                            (nth 7 item) (nth 8 item))
2044                                    (encode-time
2045                                     0
2046                                     (string-to-number (nth 4 item))
2047                                     (string-to-number (nth 3 item))
2048                                     (string-to-number (nth 2 item))
2049                                     (string-to-number (nth 1 item))
2050                                     (string-to-number (nth 0 item)))
2051                                    (nth 10 item)))
2052               items))))
2053
2054 (defmacro mixi-new-comment-list-page ()
2055   `(concat "/new_comment.pl?page=%d"))
2056
2057 (defconst mixi-new-comment-list-regexp
2058   "<a href=\"view_diary\\.pl\\?id=\\([0-9]+\\)&owner_id=\\([0-9]+\\)&comment_count=[0-9]+\" class=\"new_link\">")
2059
2060 (defun mixi-get-new-comments (&optional range)
2061   "Get new comments."
2062   (let ((items (mixi-get-matched-items (mixi-new-comment-list-page)
2063                                        mixi-new-comment-list-regexp
2064                                        range)))
2065     (mapcar (lambda (item)
2066               (mixi-make-diary (mixi-make-friend (nth 1 item)) (nth 0 item)))
2067             items)))
2068
2069 ;; Message object.
2070 (defconst mixi-message-box-list '(inbox outbox savebox thrash)) ; thrash?
2071
2072 (defmacro mixi-message-box-p (box)
2073   `(when (memq ,box mixi-message-box-list)
2074      t))
2075
2076 (defun mixi-message-box-name (box)
2077   "Return the name of BOX."
2078   (unless (mixi-message-box-p box)
2079     (signal 'wrong-type-argument (list 'mixi-message-box-p box)))
2080   (symbol-name box))
2081
2082 (defvar mixi-message-cache (make-hash-table :test 'equal))
2083 (defun mixi-make-message (id box &optional owner title time content)
2084   "Return a message object."
2085   (mixi-make-cache (list id box)
2086                    (cons 'mixi-message (vector nil id box owner title time
2087                                                content))
2088                    mixi-message-cache))
2089
2090 (defconst mixi-message-url-regexp
2091   "/view_message\\.pl\\?id=\\([a-z0-9]+\\)&box=\\([a-z]+\\)")
2092
2093 (defun mixi-make-message-from-url (url)
2094   "Return a message object from URL."
2095   (when (string-match mixi-message-url-regexp url)
2096     (let ((id (match-string 1 url))
2097           (box (match-string 2 url)))
2098       (mixi-make-message id box))))
2099
2100 (defmacro mixi-message-p (message)
2101   `(eq (mixi-object-class ,message) 'mixi-message))
2102
2103 (defmacro mixi-message-page (message)
2104   `(concat "/view_message.pl?id=" (mixi-message-id ,message)
2105            "&box=" (mixi-message-box ,message)))
2106
2107 (defconst mixi-message-owner-regexp
2108   "<font COLOR=#996600>\\(º¹½Ð¿Í\\|°¸&nbsp;Àè\\)</font>&nbsp;:&nbsp;<a HREF=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.*\\)\\(</a>\\|</td>\\)")
2109 (defconst mixi-message-title-regexp
2110 "<font COLOR=#996600>·ï\\(¡¡\\|&nbsp;\\)̾</font>&nbsp;:&nbsp;\\(.+\\)\n?</td>")
2111 (defconst mixi-message-time-regexp
2112 "<font COLOR=#996600>Æü\\(¡¡\\|&nbsp;\\)ÉÕ</font>&nbsp;:&nbsp;\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü \\([0-9]+\\)»þ\\([0-9]+\\)ʬ&nbsp;&nbsp;")
2113 (defconst mixi-message-content-regexp
2114   "<tr><td CLASS=h120>\\(.+\\)</td></tr>")
2115
2116 (defun mixi-message-realize (message)
2117   "Realize a MESSAGE."
2118   (unless (mixi-object-realize-p message)
2119     (with-mixi-retrieve (mixi-message-page message)
2120       (if (string-match mixi-message-owner-regexp buffer)
2121           (mixi-message-set-owner message
2122                                   (mixi-make-friend (match-string 2 buffer)
2123                                                     (match-string 3 buffer)))
2124         (signal 'error (list 'cannot-find-owner message)))
2125       (if (string-match mixi-message-title-regexp buffer)
2126           (mixi-message-set-title message (match-string 2 buffer))
2127         (signal 'error (list 'cannot-find-title message)))
2128       (if (string-match mixi-message-time-regexp buffer)
2129           (mixi-message-set-time
2130            message (encode-time 0 (string-to-number (match-string 6 buffer))
2131                                 (string-to-number (match-string 5 buffer))
2132                                 (string-to-number (match-string 4 buffer))
2133                                 (string-to-number (match-string 3 buffer))
2134                                 (string-to-number (match-string 2 buffer))))
2135         (signal 'error (list 'cannot-find-time message)))
2136       (if (string-match mixi-message-content-regexp buffer)
2137           (mixi-message-set-content message (match-string 1 buffer))
2138         (signal 'error (list 'cannot-find-content message))))
2139     (mixi-object-touch message)))
2140
2141 (defun mixi-message-id (message)
2142   "Return the id of MESSAGE."
2143   (unless (mixi-message-p message)
2144     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2145   (aref (cdr message) 1))
2146
2147 (defun mixi-message-box (message)
2148   "Return the box of MESSAGE."
2149   (unless (mixi-message-p message)
2150     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2151   (aref (cdr message) 2))
2152
2153 (defun mixi-message-owner (message)
2154   "Return the owner of MESSAGE."
2155   (unless (mixi-message-p message)
2156     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2157   (mixi-message-realize message)
2158   (aref (cdr message) 3))
2159
2160 (defun mixi-message-title (message)
2161   "Return the title of MESSAGE."
2162   (unless (mixi-message-p message)
2163     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2164   (mixi-message-realize message)
2165   (aref (cdr message) 4))
2166
2167 (defun mixi-message-time (message)
2168   "Return the date of MESSAGE."
2169   (unless (mixi-message-p message)
2170     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2171   (mixi-message-realize message)
2172   (aref (cdr message) 5))
2173
2174 (defun mixi-message-content (message)
2175   "Return the content of MESSAGE."
2176   (unless (mixi-message-p message)
2177     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2178   (mixi-message-realize message)
2179   (aref (cdr message) 6))
2180
2181 (defun mixi-message-set-owner (message owner)
2182   "Set the owner of MESSAGE."
2183   (unless (mixi-message-p message)
2184     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2185   (aset (cdr message) 3 owner))
2186
2187 (defun mixi-message-set-title (message title)
2188   "Set the title of MESSAGE."
2189   (unless (mixi-message-p message)
2190     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2191   (aset (cdr message) 4 title))
2192
2193 (defun mixi-message-set-time (message time)
2194   "Set the date of MESSAGE."
2195   (unless (mixi-message-p message)
2196     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2197   (aset (cdr message) 5 time))
2198
2199 (defun mixi-message-set-content (message content)
2200   "Set the content of MESSAGE."
2201   (unless (mixi-message-p message)
2202     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2203   (aset (cdr message) 6 content))
2204
2205 (defmacro mixi-message-list-page (&optional box)
2206   `(concat "/list_message.pl?page=%d"
2207            (when ,box (concat "&box=" ,box))))
2208
2209 (defconst mixi-message-list-regexp
2210   "<td><a HREF=\"view_message\\.pl\\?id=\\(.+\\)&box=\\(.+\\)\">")
2211
2212 (defun mixi-get-messages (&rest args)
2213   "Get messages."
2214   (when (> (length args) 2)
2215     (signal 'wrong-number-of-arguments
2216             (list 'mixi-get-messages (length args))))
2217   (let ((box (nth 0 args))
2218         (range (nth 1 args)))
2219     (when (or (not (mixi-message-box-p box))
2220               (mixi-message-box-p range))
2221       (setq box (nth 1 args))
2222       (setq range (nth 0 args)))
2223     (let ((items (mixi-get-matched-items
2224                   (mixi-message-list-page
2225                    (when box (mixi-message-box-name box)))
2226                   mixi-message-list-regexp
2227                   range)))
2228       (mapcar (lambda (item)
2229                 (mixi-make-message (nth 0 item) (nth 1 item)))
2230               items))))
2231
2232 ;; Introduction object.
2233 (defun mixi-make-introduction (parent owner content)
2234   "Return a introduction object."
2235   (cons 'mixi-introduction (vector parent owner content)))
2236
2237 (defmacro mixi-introduction-p (introduction)
2238   `(eq (mixi-object-class ,introduction) 'mixi-introduction))
2239
2240 (defun mixi-introduction-parent (introduction)
2241   "Return the parent of INTRODUCTION."
2242   (unless (mixi-introduction-p introduction)
2243     (signal 'wrong-type-argument (list 'mixi-introduction-p introduction)))
2244   (aref (cdr introduction) 0))
2245
2246 (defun mixi-introduction-owner (introduction)
2247   "Return the owner of INTRODUCTION."
2248   (unless (mixi-introduction-p introduction)
2249     (signal 'wrong-type-argument (list 'mixi-introduction-p introduction)))
2250   (aref (cdr introduction) 1))
2251
2252 (defun mixi-introduction-content (introduction)
2253   "Return the content of INTRODUCTION."
2254   (unless (mixi-introduction-p introduction)
2255     (signal 'wrong-type-argument (list 'mixi-introduction-p introduction)))
2256   (aref (cdr introduction) 3))
2257
2258 (defmacro mixi-introduction-list-page (&optional friend)
2259   `(concat "/show_intro.pl?page=%d"
2260            (when ,friend (concat "&id=" (mixi-friend-id ,friend)))))
2261
2262 (defconst mixi-introduction-list-regexp
2263   "<tr bgcolor=#FFFFFF>
2264 <td WIDTH=150 background=http://img\\.mixi\\.jp/img/bg_line\\.gif align=\"center\"><a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\"><img src=\".+\" border=0><br>
2265 \\(.*\\)</td></a>
2266
2267 <td WIDTH=480>
2268 \\(´Ø·¸¡§.+<br>
2269
2270
2271 \\(\\(.\\|\n<br>\\)+\\)\\|
2272 \\(\\(.\\|\n<br>\\)+\\)\\)
2273
2274
2275
2276
2277 </td>
2278 </tr>")
2279 (defconst mixi-my-introduction-list-regexp
2280   "<tr bgcolor=#FFFFFF>
2281 <td WIDTH=150 background=http://img\\.mixi\\.jp/img/bg_line\\.gif align=\"center\"><a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\"><img src=\".+\" border=0><br>
2282 \\(.*\\)</td></a>
2283
2284
2285 <td WIDTH=480>
2286 \\(´Ø·¸¡§.+<br>
2287
2288
2289 \\(\\(.\\|\n<br>\\)+\\)\\|
2290 \\(\\(.\\|\n<br>\\)+\\)\\)
2291
2292
2293 <br>
2294 <a href=\"edit_intro\\.pl\\?id=\\1&type=edit\">¤³¤Îͧ¿Í¤ò¾Ò²ð¤¹¤ë</a>
2295
2296
2297 <BR>
2298 <a href=\"delete_intro\\.pl\\?id=\\1\">ºï½ü</a>
2299
2300 </td>
2301 </tr>")
2302
2303 (defun mixi-get-introductions (&rest args)
2304   "Get introductions."
2305   (when (> (length args) 2)
2306     (signal 'wrong-number-of-arguments
2307             (list 'mixi-get-introduction (length args))))
2308   (let ((friend (nth 0 args))
2309         (range (nth 1 args)))
2310     (when (or (not (mixi-friend-p friend)) (mixi-friend-p range))
2311       (setq friend (nth 1 args))
2312       (setq range (nth 0 args)))
2313     (unless (or (null friend) (mixi-friend-p friend))
2314       (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
2315     (let* ((regexp (if friend mixi-introduction-list-regexp
2316                      mixi-my-introduction-list-regexp))
2317            (items (mixi-get-matched-items (mixi-introduction-list-page friend)
2318                                           regexp
2319                                           range)))
2320       (mapcar (lambda (item)
2321                 (mixi-make-introduction (or friend (mixi-make-me))
2322                                         (mixi-make-friend (nth 0 item)
2323                                                           (nth 1 item))
2324                                         (nth 2 item)))
2325               items))))
2326
2327 (provide 'mixi)
2328
2329 ;;; mixi.el ends here