* mixi.el (mixi-object-timestamp): New accessor method.
[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-get-communities
35 ;;  * mixi-get-topics
36 ;;  * mixi-get-new-topics
37 ;;  * mixi-get-comments
38 ;;  * mixi-get-new-comments
39
40 ;; Example:
41 ;;
42 ;; Display newest 3 diaries like a mail format.
43 ;;
44 ;; (let ((max-numbers 3)
45 ;;       (buffer (generate-new-buffer "*temp*"))
46 ;;       (format "%Y/%m/%d %H:%M"))
47 ;;   (pop-to-buffer buffer)
48 ;;   (mapc (lambda (diary)
49 ;;        (let ((subject (mixi-diary-title diary))
50 ;;              ;; Don't get owner's nick at first for omitting a useless
51 ;;              ;; retrieval.
52 ;;              (from (mixi-friend-nick (mixi-diary-owner diary)))
53 ;;              (date (format-time-string format (mixi-diary-time diary)))
54 ;;              (body (mixi-diary-content diary)))
55 ;;          (insert "From: " from "\n"
56 ;;                  "Subject: " subject "\n"
57 ;;                  "Date: " date "\n\n"
58 ;;                  body "\n\n")))
59 ;;      (mixi-get-new-diaries max-numbers))
60 ;;   (set-buffer-modified-p nil)
61 ;;   (setq buffer-read-only t)
62 ;;   (goto-char (point-min)))
63 ;;
64 ;; Display newest 3 diaries including newest 3 comments like a mail format.
65 ;; Comments are displayed like a reply mail.
66 ;;
67 ;; (let ((max-numbers 3)
68 ;;       (buffer (generate-new-buffer "*temp*"))
69 ;;       (format "%Y/%m/%d %H:%M"))
70 ;;   (pop-to-buffer buffer)
71 ;;   (mapc (lambda (diary)
72 ;;        (let ((subject (mixi-diary-title diary))
73 ;;              ;; Don't get owner's nick at first for omitting a useless
74 ;;              ;; retrieval.
75 ;;              (from (mixi-friend-nick (mixi-diary-owner diary)))
76 ;;              (date (format-time-string format (mixi-diary-time diary)))
77 ;;              (body (mixi-diary-content diary)))
78 ;;          (insert "From: " from "\n"
79 ;;                  "Subject: " subject "\n"
80 ;;                  "Date: " date "\n\n"
81 ;;                  body "\n\n")
82 ;;          (mapc (lambda (comment)
83 ;;                  (let ((from (mixi-friend-nick
84 ;;                               (mixi-comment-owner comment)))
85 ;;                        (subject (concat "Re: " subject))
86 ;;                        (date (format-time-string
87 ;;                               format (mixi-comment-time comment)))
88 ;;                        (body (mixi-comment-content comment)))
89 ;;                    (insert "From: " from "\n"
90 ;;                            "Subject: " subject "\n"
91 ;;                            "Date: " date "\n\n"
92 ;;                            body "\n\n")))
93 ;;                (mixi-get-comments diary max-numbers))))
94 ;;      (mixi-get-new-diaries max-numbers))
95 ;;   (set-buffer-modified-p nil)
96 ;;   (setq buffer-read-only t)
97 ;;   (goto-char (point-min)))
98
99 ;;; Code:
100
101 (condition-case nil
102     (require 'url)
103   (error))
104
105 (condition-case nil
106     (require 'w3m)
107   (error))
108
109 (eval-when-compile (require 'cl))
110
111 (defgroup mixi nil
112   "API library for accessing to mixi."
113   :group 'hypermedia)
114
115 (defcustom mixi-url "http://mixi.jp"
116   "*The URL of mixi."
117   :type 'string
118   :group 'mixi)
119
120 (defcustom mixi-coding-system 'euc-jp
121   "*Coding system for mixi."
122   :type 'coding-system
123   :group 'mixi)
124
125 (defcustom mixi-retrieve-function
126   (if (fboundp 'url-retrieve-synchronously)
127       'mixi-w3-retrieve 'mixi-w3m-retrieve)
128   "*The function for retrieving."
129   :type '(choice (const :tag "Using w3" mixi-w3-retrieve)
130                  (const :tag "Using w3m" mixi-w3m-retrieve)
131                  (const :tag "Using curl" mixi-curl-retrieve)
132                  (function :format "Other function: %v\n" :size 0))
133   :group 'mixi)
134
135 (defcustom mixi-curl-program "curl"
136   "*The program name of `curl'."
137   :type 'file
138   :group 'mixi)
139
140 (defcustom mixi-curl-cookie-file (expand-file-name "~/.mixi-cookies.txt")
141   "*The location of cookie file created by `curl'."
142   :type 'file
143   :group 'mixi)
144
145 (defcustom mixi-default-email nil
146   "*Default E-mail address that is used to login automatically."
147   :type '(choice (string :tag "E-mail address")
148                  (const :tag "Asked when it is necessary" nil))
149   :group 'mixi)
150
151 (defcustom mixi-default-password nil
152   "*Default password that is used to login automatically."
153   :type '(choice (string :tag "Password")
154                  (const :tag "Asked when it is necessary" nil))
155   :group 'mixi)
156
157 (defcustom mixi-accept-adult-contents t
158   "*If non-nil, accept to access to the adult contents."
159   :type 'boolean
160   :group 'mixi)
161
162 (defcustom mixi-continuously-access-interval 4.0
163   "*Time interval between each mixi access.
164 Increase this value when unexpected error frequently occurs."
165   :type 'number
166   :group 'mixi)
167
168 (defcustom mixi-cache-expires 3600
169   "*Seconds for expiration of a cached object."
170   :type '(choice (integer :tag "Expired seconds")
171                  (const :tag "Don't expire" nil))
172   :group 'mixi)
173
174 ;; FIXME: Not implemented.
175 (defcustom mixi-cache-use-file t
176   "*If non-nil, caches are saved to files."
177   :type 'boolean
178   :group 'mixi)
179
180 (defcustom mixi-cache-directory (expand-file-name "~/.mixi")
181   "*Where to look for cache files."
182   :type 'directory
183   :group 'mixi)
184
185 (defvar mixi-me nil)
186
187 ;; Utilities.
188 (defmacro mixi-message (string)
189   `(concat "[mixi] " ,string))
190
191 (defconst mixi-message-adult-contents
192   "¤³¤Î¥Ú¡¼¥¸¤«¤éÀè¤Ï¥¢¥À¥ë¥È¡ÊÀ®¿Í¸þ¤±¡Ë¥³¥ó¥Æ¥ó¥Ä¤¬´Þ¤Þ¤ì¤Æ¤¤¤Þ¤¹¡£<br>
193 ±ÜÍ÷¤ËƱ°Õ¤µ¤ì¤¿Êý¤Î¤ß¡¢Àè¤Ø¤ª¿Ê¤ß¤¯¤À¤µ¤¤¡£")
194 (defconst mixi-message-continuously-accessing
195   "°ÂÄꤷ¤Æ¥µ¥¤¥È¤Î±¿±Ä¤ò¤ª¤³¤Ê¤¦°Ù¡¢´Ö³Ö¤ò¶õ¤±¤Ê¤¤Ï¢Â³Åª¤Ê¥Ú¡¼¥¸¤ÎÁ«°Ü¡¦¹¹<br>
196 ¿·¤ÏÀ©¸Â¤µ¤»¤Æ¤¤¤¿¤À¤¤¤Æ¤ª¤ê¤Þ¤¹¡£¤´ÌÂÏǤò¤ª¤«¤±¤¤¤¿¤·¤Þ¤¹¤¬¡¢¤·¤Ð¤é¤¯¤ª<br>
197 ÂÔ¤Á¤¤¤¿¤À¤¤¤Æ¤«¤éÁàºî¤ò¤ª¤³¤Ê¤Ã¤Æ¤¯¤À¤µ¤¤¡£")
198 (defconst mixi-warning-continuously-accessing
199   "´Ö³Ö¤ò¶õ¤±¤Ê¤¤Ï¢Â³Åª¤Ê¥Ú¡¼¥¸¤ÎÁ«°Ü¡¦¹¹¿·¤òÉÑÈˤˤª¤³¤Ê¤ï¤ì¤Æ¤¤¤ë¤³¤È¤¬¸«<br>
200 ¼õ¤±¤é¤ì¤Þ¤·¤¿¤Î¤Ç¡¢°ì»þŪ¤ËÁàºî¤òÄä»ß¤µ¤»¤Æ¤¤¤¿¤À¤­¤Þ¤¹¡£¿½¤·Ìõ¤´¤¶¤¤¤Þ<br>
201 ¤»¤ó¤¬¡¢¤·¤Ð¤é¤¯¤Î´Ö¤ªÂÔ¤Á¤¯¤À¤µ¤¤¡£")
202
203 (defun mixi-retrieve-1 (buffer url &optional post-data)
204   (when (string-match mixi-message-adult-contents buffer)
205     (if mixi-accept-adult-contents
206         (setq buffer (funcall mixi-retrieve-function url "submit=agree"))
207       (setq buffer (funcall mixi-retrieve-function (concat url "?")))))
208   (when (string-match mixi-warning-continuously-accessing buffer)
209     (error (mixi-message "Continuously accessing")))
210   (if (not (string-match mixi-message-continuously-accessing buffer))
211       buffer
212     (message (mixi-message "Waiting for continuously accessing..."))
213     (sit-for mixi-continuously-access-interval)
214     (funcall mixi-retrieve-function url post-data)))
215
216 (defun mixi-w3-retrieve (url &optional post-data)
217   "Retrieve the URL and return gotten strings."
218   (if post-data
219       (progn
220         (setq url-request-method "POST")
221         (setq url-request-data post-data))
222     (setq url-request-method "GET")
223     (setq url-request-data nil))
224   (let* ((url (url-expand-file-name url mixi-url))
225          (buffer (url-retrieve-synchronously url))
226          ret)
227     (unless (bufferp buffer)
228       (error (mixi-message "Cannot retrieve")))
229     (with-current-buffer buffer
230       (goto-char (point-min))
231       (if (re-search-forward "HTTP/[0-9.]+ 302 Moved" nil t)
232           (if (re-search-forward
233                (concat "Location: " mixi-url "\\(.+\\)") nil t)
234               (setq ret (mixi-w3-retrieve (match-string 1) post-data))
235             (setq ret (mixi-w3-retrieve "/home.pl" post-data)))
236         (unless (re-search-forward "HTTP/[0-9.]+ 200 OK" nil t)
237           (error (mixi-message "Cannot retrieve")))
238         (search-forward "\n\n")
239         (setq ret (mm-decode-coding-string
240                    (buffer-substring-no-properties (point) (point-max))
241                    mixi-coding-system))
242         (kill-buffer buffer)
243         (setq ret (mixi-retrieve-1 ret url post-data))))
244     ret))
245
246 (defun mixi-w3m-retrieve (url &optional post-data)
247   "Retrieve the URL and return gotten strings."
248   (let ((url (w3m-expand-url url mixi-url)))
249     (with-temp-buffer
250       (if (not (string= (w3m-retrieve url nil nil post-data) "text/html"))
251           (error (mixi-message "Cannot retrieve"))
252         (w3m-decode-buffer url)
253         (let ((ret (buffer-substring-no-properties (point-min) (point-max))))
254           (mixi-retrieve-1 ret url post-data))))))
255
256 (defun mixi-curl-retrieve (url &optional post-data)
257   "Retrieve the URL and return gotten strings."
258   (with-temp-buffer
259     (if (fboundp 'set-buffer-multibyte)
260         (set-buffer-multibyte nil))
261     (let ((orig-mode (default-file-modes))
262           (coding-system-for-read 'binary)
263           (coding-system-for-write 'binary)
264           process ret)
265       (unwind-protect
266           (progn
267             (set-default-file-modes 448)
268             (setq process
269                   (apply #'start-process "curl" (current-buffer)
270                          mixi-curl-program
271                          (append (if post-data '("-d" "@-"))
272                                  (list "-i" "-L" "-s"
273                                        "-b" mixi-curl-cookie-file
274                                        "-c" mixi-curl-cookie-file
275                                        (concat mixi-url url)))))
276             (set-process-sentinel process #'ignore))
277         (set-default-file-modes orig-mode))
278       (when post-data
279         (process-send-string process (concat post-data "\n"))
280         (process-send-eof process))
281       (while (eq (process-status process) 'run)
282         (accept-process-output process 1))
283       (goto-char (point-min))
284       (while (looking-at "HTTP/[0-9]+\\.[0-9]+ [13][0-9][0-9]")
285         (delete-region (point) (re-search-forward "\r?\n\r?\n")))
286       (unless (looking-at "HTTP/[0-9]+\\.[0-9]+ 200")
287         (error (mixi-message "Cannot retrieve")))
288       (delete-region (point) (re-search-forward "\r?\n\r?\n"))
289       (setq ret (decode-coding-string (buffer-string) mixi-coding-system))
290       (mixi-retrieve-1 ret url post-data))))
291
292 (defconst mixi-my-id-regexp
293   "<a href=\"add_diary\\.pl\\?id=\\([0-9]+\\)")
294
295 (defun mixi-login (&optional email password)
296   "Login to mixi."
297   (when (and (eq mixi-retrieve-function 'mixi-w3m-retrieve)
298              (not w3m-use-cookies))
299     (error
300      (mixi-message
301       "Require to accept cookies.  Please set `w3m-use-cookies' to t.")))
302   (let ((email (or email mixi-default-email
303                    (read-from-minibuffer (mixi-message "Login Email: "))))
304         (password (or password mixi-default-password
305                       (read-passwd (mixi-message "Login Password: ")))))
306     (let ((buffer (funcall mixi-retrieve-function "/login.pl"
307                            (concat "email=" email
308                                    "&password=" password
309                                    "&next_url=/home.pl"
310                                    "&sticky=on"))))
311       (unless (string-match "url=/check\\.pl\\?n=" buffer)
312         (error (mixi-message "Cannot login")))
313       (setq buffer (funcall mixi-retrieve-function "/check.pl?n=home.pl"))
314       (if (string-match mixi-my-id-regexp buffer)
315           (setq mixi-me (mixi-make-friend (match-string 1 buffer)))
316         (error (mixi-message "Cannot login"))))))
317
318 (defun mixi-logout ()
319   (funcall mixi-retrieve-function "/logout.pl"))
320
321 (defmacro with-mixi-retrieve (url &rest body)
322   `(let (buffer)
323      (when ,url
324        (setq buffer (funcall mixi-retrieve-function ,url))
325        (when (string-match "login.pl" buffer)
326          (mixi-login)
327          (setq buffer (funcall mixi-retrieve-function ,url))))
328      ,@body))
329 (put 'with-mixi-retrieve 'lisp-indent-function 'defun)
330 (put 'with-mixi-retrieve 'edebug-form-spec '(form body))
331
332 (defun mixi-get-matched-items (url max-numbers regexp)
333   "Get matched items to REGEXP in URL."
334   (let ((page 1)
335         ids)
336     (catch 'end
337       (while (or (null max-numbers) (< (length ids) max-numbers))
338         (with-mixi-retrieve (format url page)
339           (let ((pos 0))
340             (while (and (string-match regexp buffer pos)
341                         (or (null max-numbers) (< (length ids) max-numbers)))
342               (let ((num 1)
343                     list)
344                 (while (match-string num buffer)
345                   (setq list (cons (match-string num buffer) list))
346                   (incf num))
347                 (when (member (reverse list) ids)
348                   (throw 'end ids))
349                 (setq ids (cons (reverse list) ids))
350                 (setq pos (match-end (1- num)))))
351             (when (eq pos 0)
352               (throw 'end ids))))
353         (incf page)))
354     ;; FIXME: Sort? Now order by newest.
355     (reverse ids)))
356
357 ;; stolen (and modified) from shimbun.el
358 (defun mixi-remove-markup (string)
359   "Remove markups from STRING."
360   (with-temp-buffer
361     (insert string)
362     (save-excursion
363       (goto-char (point-min))
364       (while (search-forward "<!--" nil t)
365         (delete-region (match-beginning 0)
366                        (or (search-forward "-->" nil t)
367                            (point-max))))
368       (goto-char (point-min))
369       (while (re-search-forward "<[^>]+>" nil t)
370         (replace-match "" t t))
371       (goto-char (point-min))
372       (while (re-search-forward "\r" nil t)
373         (replace-match "\n" t t)))
374     (buffer-string)))
375
376 ;; Cache.
377 ;; stolen from time-date.el
378 (defun mixi-time-less-p (t1 t2)
379   "Say whether time value T1 is less than time value T2."
380   (or (< (car t1) (car t2))
381       (and (= (car t1) (car t2))
382            (< (nth 1 t1) (nth 1 t2)))))
383
384 (defun mixi-time-add (t1 t2)
385   "Add two time values.  One should represent a time difference."
386   (let ((low (+ (cdr t1) (cdr t2))))
387     (cons (+ (car t1) (car t2) (lsh low -16)) low)))
388
389 ;; stolen from time-date.el
390 (defun mixi-seconds-to-time (seconds)
391   "Convert SECONDS (a floating point number) to a time value."
392   (cons (floor seconds 65536)
393         (floor (mod seconds 65536))))
394
395 (defun mixi-cache-expired-p (object)
396   "Whether a cache of OBJECT is expired."
397   (let ((timestamp (mixi-object-timestamp object)))
398     (unless (or (null mixi-cache-expires)
399                  (null timestamp))
400       (mixi-time-less-p
401        (mixi-time-add timestamp (mixi-seconds-to-time mixi-cache-expires))
402        (current-time)))))
403
404 (defun mixi-make-cache (key value table)
405   "Make a cache object and return it."
406   (let ((cache (gethash key table)))
407     (if (and cache (not (mixi-cache-expired-p cache)))
408         cache
409       (puthash key value table))))
410
411 ;; Object.
412 (defconst mixi-object-prefix "mixi-")
413
414 (defmacro mixi-object-class (object)
415   `(car-safe ,object))
416
417 (defmacro mixi-object-p (object)
418   `(eq (string-match (concat "^" mixi-object-prefix)
419                      (symbol-name (mixi-object-class ,object))) 0))
420
421 (defun mixi-object-name (object)
422   "Return the name of OBJECT."
423   (unless (mixi-object-p object)
424     (signal 'wrong-type-argument (list 'mixi-object-p object)))
425   (let ((class (mixi-object-class object)))
426     (substring (symbol-name class) (length mixi-object-prefix))))
427
428 (defun mixi-object-timestamp (object)
429   "Return the timestamp of OJBECT."
430   (unless (mixi-object-p object)
431     (signal 'wrong-type-argument (list 'mixi-object-p object)))
432   (aref (cdr object) 0))
433 (defalias 'mixi-object-realize-p 'mixi-object-timestamp)
434
435 (defun mixi-object-id (object)
436   "Return the id of OBJECT."
437   (unless (mixi-object-p object)
438     (signal 'wrong-type-argument (list 'mixi-object-p object)))
439   (let ((func (intern (concat mixi-object-prefix
440                               (mixi-object-name object) "-id"))))
441     (funcall func object)))
442
443 (defun mixi-object-set-timestamp (object timestamp)
444   "Set the timestamp of OBJECT."
445   (unless (mixi-object-p object)
446     (signal 'wrong-type-argument (list 'mixi-object-p object)))
447   (aset (cdr object) 0 timestamp))
448
449 (defmacro mixi-object-touch (object)
450   `(mixi-object-set-timestamp ,object (current-time)))
451
452 ;; Friend object.
453 (defvar mixi-friend-cache (make-hash-table :test 'equal))
454 (defun mixi-make-friend (id &optional nick)
455   "Return a friend object."
456   (mixi-make-cache id (cons 'mixi-friend (vector nil id nick nil nil nil nil
457                                                  nil nil nil nil nil nil nil))
458                    mixi-friend-cache))
459
460 (defun mixi-make-me ()
461   (unless mixi-me
462     (with-mixi-retrieve "/home.pl"
463       (if (string-match mixi-my-id-regexp buffer)
464           (setq mixi-me (mixi-make-friend (match-string 1 buffer)))
465         (signal 'error (list 'who-am-i)))))
466   mixi-me)
467
468 (defmacro mixi-friend-p (friend)
469   `(eq (mixi-object-class ,friend) 'mixi-friend))
470
471 (defmacro mixi-friend-page (friend)
472   `(concat "/show_friend.pl?id=" (mixi-friend-id ,friend)))
473
474 (defconst mixi-friend-nick-regexp
475   "<img alt=\"\\*\" src=\"http://img\\.mixi\\.jp/img/dot0\\.gif\" width=\"1\" height=\"5\"><br>\n\\(.*\\)¤µ¤ó([0-9]+)")
476 (defconst mixi-friend-name-sex-regexp
477   "<td BGCOLOR=#F2DDB7 WIDTH=80 NOWRAP><font COLOR=#996600>̾\\(&nbsp;\\| \\)Á°</font></td>\n+<td WIDTH=345>\\([^<]+\\) (\\([Ã˽÷]\\)À­)</td>")
478 (defconst mixi-friend-address-regexp
479   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>¸½½»½ê</font></td>\n<td>\\(.+\\)\\(\n.+\n\\)?</td></tr>")
480 (defconst mixi-friend-age-regexp
481   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>ǯ\\(&nbsp;\\| \\)Îð</font></td>\n<td>\\([0-9]+\\)ºÐ\\(\n.+\n\\)?</td></tr>")
482 (defconst mixi-friend-birthday-regexp
483   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>ÃÂÀ¸Æü</font></td>\n<td>\\([0-9]+\\)·î\\([0-9]+\\)Æü\\(\n.+\n\\)?</td></tr>")
484 (defconst mixi-friend-blood-type-regexp
485   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>·ì±Õ·¿</font></td>\n<td>\\([ABO]B?\\)·¿\\(\n\n\\)?</td></tr>")
486 (defconst mixi-friend-birthplace-regexp
487   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>½Ð¿ÈÃÏ</font>\n?</td>\n<td>\\(.+\\)\\(\n.+\n\\)?</td></tr>")
488 (defconst mixi-friend-hobby-regexp
489   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>¼ñ\\(&nbsp;\\| \\)Ì£</font></td>\n<td>\\(.+\\)</td></tr>")
490 (defconst mixi-friend-job-regexp
491   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>¿¦\\(&nbsp;\\| \\)¶È</font></td>\n<td>\\(.+\\)\\(\n.+\n\\)?</td></tr>")
492 (defconst mixi-friend-organization-regexp
493   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>½ê\\(&nbsp;\\| \\)°</font></td>\n<td[^>]*>\\(.+\\)\\(\n.+\n\\)?</td></tr>")
494 (defconst mixi-friend-profile-regexp
495   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>¼«¸Ê¾Ò²ð</font></td>\n<td CLASS=h120>\\(.+\\)</td></tr>")
496
497 (defun mixi-friend-realize (friend)
498   "Realize a FRIEND."
499   ;; FIXME: Check a expiration of cache?
500   (unless (mixi-object-realize-p friend)
501     (let (buf)
502       (with-mixi-retrieve (mixi-friend-page friend)
503         (setq buf buffer))
504       (if (string-match mixi-friend-nick-regexp buf)
505           (mixi-friend-set-nick friend (match-string 1 buf))
506         (signal 'error (list 'cannot-find-nick friend)))
507       ;; For getting my profile.
508       (unless (string-match mixi-friend-name-sex-regexp buf)
509         (with-mixi-retrieve "/show_profile.pl"
510           (setq buf buffer)))
511       (if (string-match mixi-friend-name-sex-regexp buf)
512           (progn
513             (mixi-friend-set-name friend (match-string 2 buf))
514             (mixi-friend-set-sex friend
515                                  (if (string= (match-string 3 buf) "ÃË")
516                                      'male 'female)))
517         (signal 'error (list 'cannot-find-name-or-sex friend)))
518       (when (string-match mixi-friend-address-regexp buf)
519         (mixi-friend-set-address friend (match-string 1 buf)))
520       (when (string-match mixi-friend-age-regexp buf)
521         (mixi-friend-set-age
522          friend (string-to-number (match-string 2 buf))))
523       (when (string-match mixi-friend-birthday-regexp buf)
524         (mixi-friend-set-birthday
525          friend (list (string-to-number (match-string 1 buf))
526                       (string-to-number (match-string 2 buf)))))
527       (when (string-match mixi-friend-blood-type-regexp buf)
528         (mixi-friend-set-blood-type friend (intern (match-string 1 buf))))
529       (when (string-match mixi-friend-birthplace-regexp buf)
530         (mixi-friend-set-birthplace friend (match-string 1 buf)))
531       (when (string-match mixi-friend-hobby-regexp buf)
532         (mixi-friend-set-hobby
533          friend (split-string (match-string 2 buf) ", ")))
534       (when (string-match mixi-friend-job-regexp buf)
535         (mixi-friend-set-job friend (match-string 2 buf)))
536       (when (string-match mixi-friend-organization-regexp buf)
537         (mixi-friend-set-organization friend (match-string 2 buf)))
538       (when (string-match mixi-friend-profile-regexp buf)
539         (mixi-friend-set-profile
540          friend (mixi-remove-markup (match-string 1 buf)))))
541     (mixi-object-touch friend)))
542
543 (defun mixi-friend-id (friend)
544   "Return the id of FRIEND."
545   (unless (mixi-friend-p friend)
546     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
547   (aref (cdr friend) 1))
548
549 (defun mixi-friend-nick (friend)
550   "Return the nick of FRIEND."
551   (unless (mixi-friend-p friend)
552     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
553   (unless (aref (cdr friend) 2)
554     (mixi-friend-realize friend))
555   (aref (cdr friend) 2))
556
557 (defun mixi-friend-name (friend)
558   "Return the name of FRIEND."
559   (unless (mixi-friend-p friend)
560     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
561   (mixi-friend-realize friend)
562   (aref (cdr friend) 3))
563
564 (defun mixi-friend-sex (friend)
565   "Return the sex of FRIEND."
566   (unless (mixi-friend-p friend)
567     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
568   (mixi-friend-realize friend)
569   (aref (cdr friend) 4))
570
571 (defun mixi-friend-address (friend)
572   "Return the address of FRIEND."
573   (unless (mixi-friend-p friend)
574     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
575   (mixi-friend-realize friend)
576   (aref (cdr friend) 5))
577
578 (defun mixi-friend-age (friend)
579   "Return the age of FRIEND."
580   (unless (mixi-friend-p friend)
581     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
582   (mixi-friend-realize friend)
583   (aref (cdr friend) 6))
584
585 (defun mixi-friend-birthday (friend)
586   "Return the birthday of FRIEND."
587   (unless (mixi-friend-p friend)
588     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
589   (mixi-friend-realize friend)
590   (aref (cdr friend) 7))
591
592 (defun mixi-friend-blood-type (friend)
593   "Return the blood type of FRIEND."
594   (unless (mixi-friend-p friend)
595     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
596   (mixi-friend-realize friend)
597   (aref (cdr friend) 8))
598
599 (defun mixi-friend-birthplace (friend)
600   "Return the birthplace of FRIEND."
601   (unless (mixi-friend-p friend)
602     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
603   (mixi-friend-realize friend)
604   (aref (cdr friend) 9))
605
606 (defun mixi-friend-hobby (friend)
607   "Return the hobby of FRIEND."
608   (unless (mixi-friend-p friend)
609     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
610   (mixi-friend-realize friend)
611   (aref (cdr friend) 10))
612
613 (defun mixi-friend-job (friend)
614   "Return the job of FRIEND."
615   (unless (mixi-friend-p friend)
616     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
617   (mixi-friend-realize friend)
618   (aref (cdr friend) 11))
619
620 (defun mixi-friend-organization (friend)
621   "Return the organization of FRIEND."
622   (unless (mixi-friend-p friend)
623     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
624   (mixi-friend-realize friend)
625   (aref (cdr friend) 12))
626
627 (defun mixi-friend-profile (friend)
628   "Return the pforile of FRIEND."
629   (unless (mixi-friend-p friend)
630     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
631   (mixi-friend-realize friend)
632   (aref (cdr friend) 13))
633
634 (defun mixi-friend-set-nick (friend nick)
635   "Set the nick of FRIEND."
636   (unless (mixi-friend-p friend)
637     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
638   (aset (cdr friend) 2 nick))
639
640 (defun mixi-friend-set-name (friend name)
641   "Set the name of FRIEND."
642   (unless (mixi-friend-p friend)
643     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
644   (aset (cdr friend) 3 name))
645
646 (defun mixi-friend-set-sex (friend sex)
647   "Set the sex of FRIEND."
648   (unless (mixi-friend-p friend)
649     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
650   (aset (cdr friend) 4 sex))
651
652 (defun mixi-friend-set-address (friend address)
653   "Set the address of FRIEND."
654   (unless (mixi-friend-p friend)
655     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
656   (aset (cdr friend) 5 address))
657
658 (defun mixi-friend-set-age (friend age)
659   "Set the age of FRIEND."
660   (unless (mixi-friend-p friend)
661     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
662   (aset (cdr friend) 6 age))
663
664 (defun mixi-friend-set-birthday (friend birthday)
665   "Set the birthday of FRIEND."
666   (unless (mixi-friend-p friend)
667     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
668   (aset (cdr friend) 7 birthday))
669
670 (defun mixi-friend-set-blood-type (friend blood-type)
671   "Set the blood type of FRIEND."
672   (unless (mixi-friend-p friend)
673     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
674   (aset (cdr friend) 8 blood-type))
675
676 (defun mixi-friend-set-birthplace (friend birthplace)
677   "Set the birthplace of FRIEND."
678   (unless (mixi-friend-p friend)
679     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
680   (aset (cdr friend) 9 birthplace))
681
682 (defun mixi-friend-set-hobby (friend hobby)
683   "Set the hobby of FRIEND."
684   (unless (mixi-friend-p friend)
685     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
686   (aset (cdr friend) 10 hobby))
687
688 (defun mixi-friend-set-job (friend job)
689   "Set the job of FRIEND."
690   (unless (mixi-friend-p friend)
691     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
692   (aset (cdr friend) 11 job))
693
694 (defun mixi-friend-set-organization (friend organization)
695   "Set the organization of FRIEND."
696   (unless (mixi-friend-p friend)
697     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
698   (aset (cdr friend) 12 organization))
699
700 (defun mixi-friend-set-profile (friend profile)
701   "Set the profile of FRIEND."
702   (unless (mixi-friend-p friend)
703     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
704   (aset (cdr friend) 13 profile))
705
706 (defmacro mixi-friend-list-page (&optional friend)
707   `(concat "/list_friend.pl?page=%d"
708            (when ,friend (concat "&id=" (mixi-friend-id ,friend)))))
709
710 (defconst mixi-friend-list-id-regexp
711   "<a href=show_friend\\.pl\\?id=\\([0-9]+\\)")
712 (defconst mixi-friend-list-nick-regexp
713   "<td valign=middle>\\(.+\\)¤µ¤ó([0-9]+)<br />")
714
715 (defun mixi-get-friends (&rest args)
716   "Get friends of FRIEND."
717   (when (> (length args) 2)
718     (signal 'wrong-number-of-arguments (list 'mixi-get-friends (length args))))
719   (let ((friend (nth 0 args))
720         (max-numbers (nth 1 args)))
721     (when (or (not (mixi-friend-p friend)) (mixi-friend-p max-numbers))
722       (setq friend (nth 1 args))
723       (setq max-numbers (nth 0 args)))
724     (unless (or (null friend) (mixi-friend-p friend))
725       (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
726     (let ((ids (mixi-get-matched-items (mixi-friend-list-page friend)
727                                        max-numbers
728                                        mixi-friend-list-id-regexp))
729           (nicks (mixi-get-matched-items (mixi-friend-list-page friend)
730                                          max-numbers
731                                          mixi-friend-list-nick-regexp)))
732       (let ((index 0)
733             ret)
734         (while (< index (length ids))
735           (setq ret (cons (mixi-make-friend (nth 0 (nth index ids))
736                                             (nth 0 (nth index nicks))) ret))
737           (incf index))
738         (reverse ret)))))
739
740 ;; Favorite.
741 (defmacro mixi-favorite-list-page ()
742   `(concat "/list_bookmark.pl?page=%d"))
743
744 (defconst mixi-favorite-list-id-regexp
745   "<td ALIGN=center BGCOLOR=#FDF9F2 width=330><a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">")
746 (defconst mixi-favorite-list-nick-regexp
747   "<td BGCOLOR=#FDF9F2><font COLOR=#996600>̾&nbsp;&nbsp;Á°</font></td>
748 <td COLSPAN=2 BGCOLOR=#FFFFFF>\\(.+\\)</td></tr>")
749
750 (defun mixi-get-favorites (&optional max-numbers)
751   "Get favorites."
752   (let ((ids (mixi-get-matched-items (mixi-favorite-list-page)
753                                      max-numbers
754                                      mixi-favorite-list-id-regexp))
755         (nicks (mixi-get-matched-items (mixi-favorite-list-page)
756                                        max-numbers
757                                        mixi-favorite-list-nick-regexp)))
758     (let ((index 0)
759           ret)
760       (while (< index (length ids))
761         (setq ret (cons (mixi-make-friend (nth 0 (nth index ids))
762                                           (nth 0 (nth index nicks))) ret))
763         (incf index))
764       (reverse ret))))
765
766 ;; Log object.
767 (defun mixi-make-log (friend time)
768   "Return a log object."
769   (cons 'mixi-log (vector friend time)))
770
771 (defmacro mixi-log-p (log)
772   `(eq (mixi-object-class ,log) 'mixi-log))
773
774 (defun mixi-log-friend (log)
775   "Return the friend of LOG."
776   (unless (mixi-log-p log)
777     (signal 'wrong-type-argument (list 'mixi-log-p log)))
778   (aref (cdr log) 0))
779
780 (defun mixi-log-time (log)
781   "Return the time of LOG."
782   (unless (mixi-log-p log)
783     (signal 'wrong-type-argument (list 'mixi-log-p log)))
784   (aref (cdr log) 1))
785
786 (defmacro mixi-log-list-page ()
787   `(concat "/show_log.pl"))
788
789 (defconst mixi-log-list-regexp
790   "\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü \\([0-9]+\\):\\([0-9]+\\) <a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.+\\)</a><br>")
791
792 (defun mixi-get-logs (&optional max-numbers)
793   "Get logs."
794   (let ((items (mixi-get-matched-items (mixi-log-list-page)
795                                        max-numbers
796                                        mixi-log-list-regexp)))
797     (mapcar (lambda (item)
798               (mixi-make-log (mixi-make-friend (nth 5 item) (nth 6 item))
799                              (encode-time 0
800                                           (string-to-number (nth 4 item))
801                                           (string-to-number (nth 3 item))
802                                           (string-to-number (nth 2 item))
803                                           (string-to-number (nth 1 item))
804                                           (string-to-number (nth 0 item)))))
805             items)))
806
807 ;; Diary object.
808 (defvar mixi-diary-cache (make-hash-table :test 'equal))
809 (defun mixi-make-diary (owner id)
810   "Return a diary object."
811   (let ((owner (or owner (mixi-make-me))))
812     (mixi-make-cache (list (mixi-friend-id owner) id)
813                      (cons 'mixi-diary (vector nil owner id nil nil nil))
814                      mixi-diary-cache)))
815
816 (defmacro mixi-diary-p (diary)
817   `(eq (mixi-object-class ,diary) 'mixi-diary))
818
819 (defmacro mixi-diary-page (diary)
820   `(concat "/view_diary.pl?id=" (mixi-diary-id ,diary)
821            "&owner_id=" (mixi-friend-id (mixi-diary-owner ,diary))))
822
823 ;; FIXME: Remove `¤µ¤ó'.
824 (defconst mixi-diary-owner-nick-regexp
825   "<td WIDTH=490 background=http://img\\.mixi\\.jp/img/bg_w\\.gif><b><font COLOR=#605048>\\(.+\\)\\(¤µ¤ó\\)?¤ÎÆüµ­</font></b></td>")
826 (defconst mixi-diary-time-regexp
827   "<td ALIGN=center ROWSPAN=2 NOWRAP WIDTH=95 bgcolor=#FFD8B0>\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü<br>\\([0-9]+\\):\\([0-9]+\\)</td>")
828 (defconst mixi-diary-title-regexp
829   "<td BGCOLOR=#FFF4E0 WIDTH=430>&nbsp;\\([^<]+\\)</td></tr>")
830 (defconst mixi-diary-content-regexp
831   "<td CLASS=h12>\\(.+\\)</td></tr>")
832
833 (defun mixi-diary-realize (diary)
834   "Realize a DIARY."
835   ;; FIXME: Check a expiration of cache?
836   (unless (mixi-object-realize-p diary)
837     (with-mixi-retrieve (mixi-diary-page diary)
838       (if (string-match mixi-diary-owner-nick-regexp buffer)
839           (mixi-friend-set-nick (mixi-diary-owner diary)
840                                 (match-string 1 buffer))
841         (signal 'error (list 'cannot-find-owner-nick diary)))
842       (if (string-match mixi-diary-time-regexp buffer)
843           (mixi-diary-set-time
844            diary (encode-time 0 (string-to-number (match-string 5 buffer))
845                               (string-to-number (match-string 4 buffer))
846                               (string-to-number (match-string 3 buffer))
847                               (string-to-number (match-string 2 buffer))
848                               (string-to-number (match-string 1 buffer))))
849         (signal 'error (list 'cannot-find-time diary)))
850       (if (string-match mixi-diary-title-regexp buffer)
851           (mixi-diary-set-title diary (match-string 1 buffer))
852         (signal 'error (list 'cannot-find-title diary)))
853       (if (string-match mixi-diary-content-regexp buffer)
854           (mixi-diary-set-content diary (mixi-remove-markup
855                                          (match-string 1 buffer)))
856         (signal 'error (list 'cannot-find-content diary))))
857     (mixi-object-touch diary)))
858
859 (defun mixi-diary-owner (diary)
860   "Return the owner of DIARY."
861   (unless (mixi-diary-p diary)
862     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
863   (aref (cdr diary) 1))
864
865 (defun mixi-diary-id (diary)
866   "Return the id of DIARY."
867   (unless (mixi-diary-p diary)
868     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
869   (aref (cdr diary) 2))
870
871 (defun mixi-diary-time (diary)
872   "Return the time of DIARY."
873   (unless (mixi-diary-p diary)
874     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
875   (mixi-diary-realize diary)
876   (aref (cdr diary) 3))
877
878 (defun mixi-diary-title (diary)
879   "Return the title of DIARY."
880   (unless (mixi-diary-p diary)
881     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
882   (mixi-diary-realize diary)
883   (aref (cdr diary) 4))
884
885 (defun mixi-diary-content (diary)
886   "Return the content of DIARY."
887   (unless (mixi-diary-p diary)
888     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
889   (mixi-diary-realize diary)
890   (aref (cdr diary) 5))
891
892 (defun mixi-diary-set-time (diary time)
893   "Set the time of DIARY."
894   (unless (mixi-diary-p diary)
895     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
896   (aset (cdr diary) 3 time))
897
898 (defun mixi-diary-set-title (diary title)
899   "Set the title of DIARY."
900   (unless (mixi-diary-p diary)
901     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
902   (aset (cdr diary) 4 title))
903
904 (defun mixi-diary-set-content (diary content)
905   "Set the content of DIARY."
906   (unless (mixi-diary-p diary)
907     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
908   (aset (cdr diary) 5 content))
909
910 (defmacro mixi-diary-list-page (&optional friend)
911   `(concat "/list_diary.pl?page=%d"
912            (when ,friend (concat "&id=" (mixi-friend-id ,friend)))))
913
914 (defconst mixi-diary-list-regexp
915   "<a href=\"view_diary\\.pl\\?id=\\([0-9]+\\)&owner_id=[0-9]+\">")
916
917 (defun mixi-get-diaries (&rest args)
918   "Get diaries of FRIEND."
919   (when (> (length args) 2)
920     (signal 'wrong-number-of-arguments (list 'mixi-get-friends (length args))))
921   (let ((friend (nth 0 args))
922         (max-numbers (nth 1 args)))
923     (when (or (not (mixi-friend-p friend)) (mixi-friend-p max-numbers))
924       (setq friend (nth 1 args))
925       (setq max-numbers (nth 0 args)))
926     (unless (or (null friend) (mixi-friend-p friend))
927       (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
928     (let ((items (mixi-get-matched-items (mixi-diary-list-page friend)
929                                          max-numbers
930                                          mixi-diary-list-regexp)))
931       (mapcar (lambda (item)
932                 (mixi-make-diary friend (nth 0 item)))
933               items))))
934
935 (defmacro mixi-new-diary-list-page ()
936   `(concat "/new_friend_diary.pl?page=%d"))
937
938 (defconst mixi-new-diary-list-regexp
939   "<a class=\"new_link\" href=view_diary\\.pl\\?id=\\([0-9]+\\)&owner_id=\\([0-9]+\\)>")
940
941 (defun mixi-get-new-diaries (&optional max-numbers)
942   "Get new diaries."
943   (let ((items (mixi-get-matched-items (mixi-new-diary-list-page)
944                                        max-numbers
945                                        mixi-new-diary-list-regexp)))
946     (mapcar (lambda (item)
947               (mixi-make-diary (mixi-make-friend (nth 1 item)) (nth 0 item)))
948             items)))
949
950 ;; Community object.
951 (defvar mixi-community-cache (make-hash-table :test 'equal))
952 (defun mixi-make-community (id &optional name)
953   "Return a community object."
954   (mixi-make-cache id (cons 'mixi-community (vector nil id name nil nil nil
955                                                     nil nil nil nil))
956                    mixi-community-cache))
957
958 (defmacro mixi-community-p (community)
959   `(eq (mixi-object-class ,community) 'mixi-community))
960
961 (defmacro mixi-community-page (community)
962   `(concat "/view_community.pl?id=" (mixi-community-id ,community)))
963
964 (defconst mixi-community-nodata-regexp
965   "^¥Ç¡¼¥¿¤¬¤¢¤ê¤Þ¤»¤ó")
966 (defconst mixi-community-name-regexp
967   "<td WIDTH=345>\\(.*\\)</td></tr>")
968 (defconst mixi-community-birthday-regexp
969   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>³«ÀßÆü</font></td>\n<td>\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü</td>")
970 ;; FIXME: Care when the owner has seceded.
971 (defconst mixi-community-owner-regexp
972   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>´ÉÍý¿Í</font></td>\n<td>\n\n<a href=\"\\(home\\.pl\\|show_friend\\.pl\\?id=\\([0-9]+\\)\\)\">\n\\(.+\\)</a>")
973 (defconst mixi-community-category-regexp
974   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>¥«¥Æ¥´¥ê</font></td>\n<td>\\([^<]+\\)</td>")
975 (defconst mixi-community-members-regexp
976   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>¥á¥ó¥Ð¡¼¿ô</font></td>\n<td>\\([0-9]+\\)¿Í</td></tr>")
977 (defconst mixi-community-open-level-regexp
978   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>»²²Ã¾ò·ï¤È<br>¸ø³«¥ì¥Ù¥ë</font></td>
979 <td>\\(.+\\)</td></tr>")
980 (defconst mixi-community-authority-regexp
981   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>¥È¥Ô¥Ã¥¯ºîÀ®¤Î¸¢¸Â</font></td>\n<td>\\(.+\\)</td></tr>")
982 (defconst mixi-community-description-regexp
983   "<td CLASS=h120>\\(.+\\)</td>")
984
985 (defun mixi-community-realize (community)
986   "Realize a COMMUNITY."
987   ;; FIXME: Check a expiration of cache?
988   (unless (mixi-object-realize-p community)
989     (with-mixi-retrieve (mixi-community-page community)
990       (if (string-match mixi-community-nodata-regexp buffer)
991           ;; FIXME: Set all members?
992           (mixi-community-set-name community "¥Ç¡¼¥¿¤¬¤¢¤ê¤Þ¤»¤ó")
993         (if (string-match mixi-community-name-regexp buffer)
994             (mixi-community-set-name community (match-string 1 buffer))
995           (signal 'error (list 'cannot-find-name community)))
996         (if (string-match mixi-community-birthday-regexp buffer)
997             (mixi-community-set-birthday
998              community
999              (encode-time 0 0 0 (string-to-number (match-string 3 buffer))
1000                           (string-to-number (match-string 2 buffer))
1001                           (string-to-number (match-string 1 buffer))))
1002           (signal 'error (list 'cannot-find-birthday community)))
1003         (if (string-match mixi-community-owner-regexp buffer)
1004             (if (string= (match-string 1 buffer) "home.pl")
1005                 (mixi-community-set-owner community (mixi-make-me))
1006               (mixi-community-set-owner
1007                community (mixi-make-friend (match-string 2 buffer)
1008                                            (match-string 3 buffer))))
1009           (signal 'error (list 'cannot-find-owner community)))
1010         (if (string-match mixi-community-category-regexp buffer)
1011             (mixi-community-set-category community (match-string 1 buffer))
1012           (signal 'error (list 'cannot-find-category community)))
1013         (if (string-match mixi-community-members-regexp buffer)
1014             (mixi-community-set-members
1015              community (string-to-number (match-string 1 buffer)))
1016           (signal 'error (list 'cannot-find-members community)))
1017         (if (string-match mixi-community-open-level-regexp buffer)
1018             (mixi-community-set-open-level community (match-string 1 buffer))
1019           (signal 'error (list 'cannot-find-open-level community)))
1020         (if (string-match mixi-community-authority-regexp buffer)
1021             (mixi-community-set-authority community (match-string 1 buffer))
1022           (signal 'error (list 'cannot-find-authority community)))
1023         (if (string-match mixi-community-description-regexp buffer)
1024             (mixi-community-set-description
1025              community (mixi-remove-markup (match-string 1 buffer)))
1026           (signal 'error (list 'cannot-find-description community)))))
1027     (mixi-object-touch community)))
1028
1029 (defun mixi-community-id (community)
1030   "Return the id of COMMUNITY."
1031   (unless (mixi-community-p community)
1032     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1033   (aref (cdr community) 1))
1034
1035 (defun mixi-community-name (community)
1036   "Return the name of COMMUNITY."
1037   (unless (mixi-community-p community)
1038     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1039   (unless (aref (cdr community) 2)
1040     (mixi-community-realize community))
1041   (aref (cdr community) 2))
1042
1043 (defun mixi-community-birthday (community)
1044   "Return the birthday of COMMUNITY."
1045   (unless (mixi-community-p community)
1046     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1047   (mixi-community-realize community)
1048   (aref (cdr community) 3))
1049
1050 (defun mixi-community-owner (community)
1051   "Return the owner of COMMUNITY."
1052   (unless (mixi-community-p community)
1053     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1054   (mixi-community-realize community)
1055   (aref (cdr community) 4))
1056
1057 (defun mixi-community-category (community)
1058   "Return the category of COMMUNITY."
1059   (unless (mixi-community-p community)
1060     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1061   (mixi-community-realize community)
1062   (aref (cdr community) 5))
1063
1064 (defun mixi-community-members (community)
1065   "Return the members of COMMUNITY."
1066   (unless (mixi-community-p community)
1067     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1068   (mixi-community-realize community)
1069   (aref (cdr community) 6))
1070
1071 (defun mixi-community-open-level (community)
1072   "Return the open-level of COMMUNITY."
1073   (unless (mixi-community-p community)
1074     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1075   (mixi-community-realize community)
1076   (aref (cdr community) 7))
1077
1078 (defun mixi-community-authority (community)
1079   "Return the authority of COMMUNITY."
1080   (unless (mixi-community-p community)
1081     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1082   (mixi-community-realize community)
1083   (aref (cdr community) 8))
1084
1085 (defun mixi-community-description (community)
1086   "Return the description of COMMUNITY."
1087   (unless (mixi-community-p community)
1088     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1089   (mixi-community-realize community)
1090   (aref (cdr community) 9))
1091
1092 (defun mixi-community-set-name (community name)
1093   "Set the name of COMMUNITY."
1094   (unless (mixi-community-p community)
1095     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1096   (aset (cdr community) 2 name))
1097
1098 (defun mixi-community-set-birthday (community birthday)
1099   "Set the birthday of COMMUNITY."
1100   (unless (mixi-community-p community)
1101     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1102   (aset (cdr community) 3 birthday))
1103
1104 (defun mixi-community-set-owner (community owner)
1105   "Set the owner of COMMUNITY."
1106   (unless (mixi-community-p community)
1107     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1108   (unless (mixi-friend-p owner)
1109     (signal 'wrong-type-argument (list 'mixi-friend-p owner)))
1110   (aset (cdr community) 4 owner))
1111
1112 (defun mixi-community-set-category (community category)
1113   "Set the category of COMMUNITY."
1114   (unless (mixi-community-p community)
1115     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1116   (aset (cdr community) 5 category))
1117
1118 (defun mixi-community-set-members (community members)
1119   "Set the name of COMMUNITY."
1120   (unless (mixi-community-p community)
1121     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1122   (aset (cdr community) 6 members))
1123
1124 (defun mixi-community-set-open-level (community open-level)
1125   "Set the name of COMMUNITY."
1126   (unless (mixi-community-p community)
1127     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1128   (aset (cdr community) 7 open-level))
1129
1130 (defun mixi-community-set-authority (community authority)
1131   "Set the name of COMMUNITY."
1132   (unless (mixi-community-p community)
1133     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1134   (aset (cdr community) 8 authority))
1135
1136 (defun mixi-community-set-description (community description)
1137   "Set the name of COMMUNITY."
1138   (unless (mixi-community-p community)
1139     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1140   (aset (cdr community) 9 description))
1141
1142 (defmacro mixi-community-list-page (&optional friend)
1143   `(concat "/list_community.pl?page=%d"
1144            (when ,friend (concat "&id=" (mixi-friend-id ,friend)))))
1145
1146 (defconst mixi-community-list-id-regexp
1147   "<a href=view_community\\.pl\\?id=\\([0-9]+\\)")
1148 (defconst mixi-community-list-name-regexp
1149   "<td valign=middle>\\(.+\\)([0-9]+)</td>")
1150
1151 (defun mixi-get-communities (&rest args)
1152   "Get communities of FRIEND."
1153   (when (> (length args) 2)
1154     (signal 'wrong-number-of-arguments (list 'mixi-get-friends (length args))))
1155   (let ((friend (nth 0 args))
1156         (max-numbers (nth 1 args)))
1157     (when (or (not (mixi-friend-p friend)) (mixi-friend-p max-numbers))
1158       (setq friend (nth 1 args))
1159       (setq max-numbers (nth 0 args)))
1160     (unless (or (null friend) (mixi-friend-p friend))
1161       (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
1162     (let ((ids (mixi-get-matched-items (mixi-community-list-page friend)
1163                                        max-numbers
1164                                        mixi-community-list-id-regexp))
1165           (names (mixi-get-matched-items (mixi-community-list-page friend)
1166                                          max-numbers
1167                                          mixi-community-list-name-regexp)))
1168       (let ((index 0)
1169             ret)
1170         (while (< index (length ids))
1171           (setq ret (cons (mixi-make-community (nth 0 (nth index ids))
1172                                                (nth 0 (nth index names))) ret))
1173           (incf index))
1174         (reverse ret)))))
1175
1176 ;; Topic object.
1177 (defvar mixi-topic-cache (make-hash-table :test 'equal))
1178 (defun mixi-make-topic (community id)
1179   "Return a topic object."
1180   (mixi-make-cache (list (mixi-community-id community) id)
1181                    (cons 'mixi-topic (vector nil community id nil nil nil nil))
1182                    mixi-topic-cache))
1183
1184 (defmacro mixi-topic-p (topic)
1185   `(eq (mixi-object-class ,topic) 'mixi-topic))
1186
1187 (defmacro mixi-topic-page (topic)
1188   `(concat "/view_bbs.pl?id=" (mixi-topic-id ,topic)
1189            "&comm_id=" (mixi-community-id (mixi-topic-community ,topic))))
1190
1191 (defconst mixi-topic-time-regexp
1192   "<td rowspan=\"3\" width=\"110\" bgcolor=\"#ffd8b0\" align=\"center\" valign=\"top\" nowrap>\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü<br>\\([0-9]+\\):\\([0-9]+\\)</td>")
1193 (defconst mixi-topic-title-regexp
1194   "<td bgcolor=\"#fff4e0\">&nbsp;\\([^<]+\\)</td>")
1195 ;; FIXME: Remove `¤µ¤ó'.
1196 (defconst mixi-topic-owner-regexp
1197   "<td bgcolor=\"#fdf9f2\">&nbsp;<font color=\"#dfb479\"></font>&nbsp;<a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.+\\)\\(¤µ¤ó\\)?</a>")
1198 (defconst mixi-topic-content-regexp
1199   "<td class=\"h120\"><table><tr>\\(.+\\)?</tr></table>\\(.+\\)</td>")
1200
1201 (defun mixi-topic-realize (topic)
1202   "Realize a TOPIC."
1203   ;; FIXME: Check a expiration of cache?
1204   (unless (mixi-object-realize-p topic)
1205     (with-mixi-retrieve (mixi-topic-page topic)
1206       (if (string-match mixi-topic-time-regexp buffer)
1207           (mixi-topic-set-time
1208            topic (encode-time 0 (string-to-number (match-string 5 buffer))
1209                               (string-to-number (match-string 4 buffer))
1210                               (string-to-number (match-string 3 buffer))
1211                               (string-to-number (match-string 2 buffer))
1212                               (string-to-number (match-string 1 buffer))))
1213         (signal 'error (list 'cannot-find-time topic)))
1214       (if (string-match mixi-topic-title-regexp buffer)
1215           (mixi-topic-set-title topic (match-string 1 buffer))
1216         (signal 'error (list 'cannot-find-title topic)))
1217       (if (string-match mixi-topic-owner-regexp buffer)
1218           (mixi-topic-set-owner topic
1219                                 (mixi-make-friend (match-string 1 buffer)
1220                                                   (match-string 2 buffer)))
1221         (signal 'error (list 'cannot-find-owner topic)))
1222       (if (string-match mixi-topic-content-regexp buffer)
1223           (mixi-topic-set-content topic (mixi-remove-markup
1224                                          (match-string 2 buffer)))
1225         (signal 'error (list 'cannot-find-content topic))))
1226     (mixi-object-touch topic)))
1227
1228 (defun mixi-topic-community (topic)
1229   "Return the community of TOPIC."
1230   (unless (mixi-topic-p topic)
1231     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1232   (aref (cdr topic) 1))
1233
1234 (defun mixi-topic-id (topic)
1235   "Return the id of TOPIC."
1236   (unless (mixi-topic-p topic)
1237     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1238   (aref (cdr topic) 2))
1239
1240 (defun mixi-topic-time (topic)
1241   "Return the time of TOPIC."
1242   (unless (mixi-topic-p topic)
1243     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1244   (mixi-topic-realize topic)
1245   (aref (cdr topic) 3))
1246
1247 (defun mixi-topic-title (topic)
1248   "Return the title of TOPIC."
1249   (unless (mixi-topic-p topic)
1250     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1251   (mixi-topic-realize topic)
1252   (aref (cdr topic) 4))
1253
1254 (defun mixi-topic-owner (topic)
1255   "Return the owner of TOPIC."
1256   (unless (mixi-topic-p topic)
1257     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1258   (mixi-topic-realize topic)
1259   (aref (cdr topic) 5))
1260
1261 (defun mixi-topic-content (topic)
1262   "Return the content of TOPIC."
1263   (unless (mixi-topic-p topic)
1264     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1265   (mixi-topic-realize topic)
1266   (aref (cdr topic) 6))
1267
1268 (defun mixi-topic-set-time (topic time)
1269   "Set the time of TOPIC."
1270   (unless (mixi-topic-p topic)
1271     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1272   (aset (cdr topic) 3 time))
1273
1274 (defun mixi-topic-set-title (topic title)
1275   "Set the title of TOPIC."
1276   (unless (mixi-topic-p topic)
1277     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1278   (aset (cdr topic) 4 title))
1279
1280 (defun mixi-topic-set-owner (topic owner)
1281   "Set the owner of TOPIC."
1282   (unless (mixi-topic-p topic)
1283     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1284   (unless (mixi-friend-p owner)
1285     (signal 'wrong-type-argument (list 'mixi-friend-p owner)))
1286   (aset (cdr topic) 5 owner))
1287
1288 (defun mixi-topic-set-content (topic content)
1289   "Set the content of TOPIC."
1290   (unless (mixi-topic-p topic)
1291     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1292   (aset (cdr topic) 6 content))
1293
1294 (defmacro mixi-topic-list-page (community)
1295   `(concat "/list_bbs.pl?page=%d"
1296            "&id=" (mixi-community-id ,community)))
1297
1298 (defconst mixi-topic-list-regexp
1299   "<a href=view_bbs\\.pl\\?id=\\([0-9]+\\)")
1300
1301 (defun mixi-get-topics (community &optional max-numbers)
1302   "Get topics of COMMUNITY."
1303   (unless (mixi-community-p community)
1304     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1305   (let ((items (mixi-get-matched-items (mixi-topic-list-page community)
1306                                        max-numbers
1307                                        mixi-topic-list-regexp)))
1308     (mapcar (lambda (item)
1309               (mixi-make-topic community (nth 0 item)))
1310             items)))
1311
1312 (defmacro mixi-new-topic-list-page ()
1313   `(concat "/new_bbs.pl?page=%d"))
1314
1315 (defconst mixi-new-topic-list-regexp
1316   "<a href=\"view_bbs\\.pl\\?id=\\([0-9]+\\)&comment_count=[0-9]+&comm_id=\\([0-9]+\\)\" class=\"new_link\">")
1317
1318 (defun mixi-get-new-topics (&optional max-numbers)
1319   "Get new topics."
1320   (let ((items (mixi-get-matched-items (mixi-new-topic-list-page)
1321                                        max-numbers
1322                                        mixi-new-topic-list-regexp)))
1323     (mapcar (lambda (item)
1324               (mixi-make-topic (mixi-make-community (nth 1 item))
1325                                (nth 0 item)))
1326             items)))
1327
1328 ;; Comment object.
1329 (defun mixi-make-comment (parent owner time content)
1330   "Return a comment object."
1331   (cons 'mixi-comment (vector parent owner time content)))
1332
1333 (defmacro mixi-comment-p (comment)
1334   `(eq (mixi-object-class ,comment) 'mixi-comment))
1335
1336 (defun mixi-comment-parent (comment)
1337   "Return the parent of COMMENT."
1338   (unless (mixi-comment-p comment)
1339     (signal 'wrong-type-argument (list 'mixi-comment-p comment)))
1340   (aref (cdr comment) 0))
1341
1342 (defun mixi-comment-owner (comment)
1343   "Return the owner of COMMENT."
1344   (unless (mixi-comment-p comment)
1345     (signal 'wrong-type-argument (list 'mixi-comment-p comment)))
1346   (aref (cdr comment) 1))
1347
1348 (defun mixi-comment-time (comment)
1349   "Return the time of COMMENT."
1350   (unless (mixi-comment-p comment)
1351     (signal 'wrong-type-argument (list 'mixi-comment-p comment)))
1352   (aref (cdr comment) 2))
1353
1354 (defun mixi-comment-content (comment)
1355   "Return the content of COMMENT."
1356   (unless (mixi-comment-p comment)
1357     (signal 'wrong-type-argument (list 'mixi-comment-p comment)))
1358   (aref (cdr comment) 3))
1359
1360 (defun mixi-diary-comment-list-page (diary)
1361   (concat "/view_diary.pl?page=%d"
1362           "&id=" (mixi-diary-id diary)
1363           "&owner_id=" (mixi-friend-id (mixi-diary-owner diary))))
1364
1365 ;; FIXME: Split regexp to time, owner(id and nick) and contents.
1366 (defconst mixi-diary-comment-list-regexp
1367 "<td rowspan=\"2\" align=\"center\" width=\"95\" bgcolor=\"#f2ddb7\" nowrap>
1368 \\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü<br>\\([0-9]+\\):\\([0-9]+\\)\\(<br>
1369 <input type=checkbox name=comment_id value=\".+\">
1370 \\|\\)
1371 </td>
1372 <td ALIGN=center BGCOLOR=#FDF9F2 WIDTH=430>
1373 <table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"410\">
1374 <tr>
1375 <td>
1376 <a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.+\\)</a>
1377
1378 </td>
1379 </tr>
1380 </table>
1381 </td>
1382 </tr>
1383 <!-- ËÜʸ : start -->
1384 <tr>
1385 <td bgcolor=\"#ffffff\">
1386 <table BORDER=0 CELLSPACING=0 CELLPADDING=[35] WIDTH=410>
1387 <tr>
1388 <td CLASS=h12>
1389 \\(.+\\)
1390 </td></tr></table>")
1391
1392 (defun mixi-topic-comment-list-page (topic)
1393   (concat "/view_bbs.pl?page=%d"
1394           "&id=" (mixi-topic-id topic)
1395           "&comm_id=" (mixi-community-id (mixi-topic-community topic))))
1396
1397 ;; FIXME: Split regexp to time, owner(id and nick) and contents.
1398 (defconst mixi-topic-comment-list-regexp
1399   "<tr valign=\"top\">
1400 <td rowspan=\"2\" width=\"110\" bgcolor=\"#f2ddb7\" align=\"center\" nowrap>
1401 \\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü<br>
1402 \\([0-9]+\\):\\([0-9]+\\)<br>
1403 \\(</td>\\)
1404 <td bgcolor=\"#fdf9f2\">&nbsp;<font color=\"#f8a448\">
1405 <b>&nbsp;&nbsp;[0-9]+</b>:</font>&nbsp;
1406
1407 <a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.+\\)</a>
1408
1409
1410
1411 </td>
1412 </tr>
1413 <tr>
1414 <td bgcolor=\"#ffffff\" align=\"center\">
1415 <table border=\"0\" cellspacing=\"0\" cellpadding=\"5\" width=\"500\">
1416 <tr>
1417 <td class=\"h120\">
1418
1419 \\(.+\\)
1420 </td>
1421 </tr>
1422 </table>
1423 </td>
1424 </tr>")
1425
1426 (defun mixi-get-comments (parent &optional max-numbers)
1427   "Get comments of PARENT."
1428   (unless (mixi-object-p parent)
1429     (signal 'wrong-type-argument (list 'mixi-object-p parent)))
1430   (let* ((name (mixi-object-name parent))
1431          (list-page (intern (concat mixi-object-prefix name
1432                                     "-comment-list-page")))
1433          (regexp (eval (intern (concat mixi-object-prefix name
1434                                        "-comment-list-regexp")))))
1435     (let ((items (mixi-get-matched-items
1436                   (funcall list-page parent) max-numbers regexp)))
1437       (mapcar (lambda (item)
1438                 (mixi-make-comment parent (mixi-make-friend
1439                                            (nth 6 item) (nth 7 item))
1440                                    (encode-time
1441                                     0
1442                                     (string-to-number (nth 4 item))
1443                                     (string-to-number (nth 3 item))
1444                                     (string-to-number (nth 2 item))
1445                                     (string-to-number (nth 1 item))
1446                                     (string-to-number (nth 0 item)))
1447                                    (mixi-remove-markup (nth 8 item))))
1448               items))))
1449
1450 (defmacro mixi-new-comment-list-page ()
1451   `(concat "/new_comment.pl?page=%d"))
1452
1453 (defconst mixi-new-comment-list-regexp
1454   "<a href=\"view_diary\\.pl\\?id=\\([0-9]+\\)&owner_id=\\([0-9]+\\)&comment_count=[0-9]+\" class=\"new_link\">")
1455
1456 (defun mixi-get-new-comments (&optional max-numbers)
1457   "Get new comments."
1458   (let ((items (mixi-get-matched-items (mixi-new-comment-list-page)
1459                                        max-numbers
1460                                        mixi-new-comment-list-regexp)))
1461     (mapcar (lambda (item)
1462               (let ((diary (mixi-make-diary
1463                             (mixi-make-friend (nth 1 item))
1464                             (nth 0 item))))
1465                 (mixi-get-comments diary)))
1466             items)))
1467
1468 ;; Message object.
1469 (defvar mixi-message-cache (make-hash-table :test 'equal))
1470 (defun mixi-make-message (id box)
1471   "Return a message object."
1472   (mixi-make-cache (list id box)
1473                    (cons 'mixi-message (vector nil id box nil nil nil nil))
1474                    mixi-message-cache))
1475
1476 (defmacro mixi-message-p (message)
1477   `(eq (mixi-object-class ,message) 'mixi-message))
1478
1479 (defmacro mixi-message-page (message)
1480   `(concat "/view_message.pl?id=" (mixi-message-id ,message)
1481            "&box=" (mixi-message-box ,message)))
1482
1483 (defconst mixi-message-owner-regexp
1484   "<font COLOR=#996600>º¹½Ð¿Í</font>&nbsp;:&nbsp;<a HREF=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.+\\)</a>")
1485 (defconst mixi-message-title-regexp
1486   "<font COLOR=#996600>·ï¡¡Ì¾</font>&nbsp;:&nbsp;\\(.+\\)
1487 </td>")
1488 (defconst mixi-message-time-regexp
1489   "<font COLOR=#996600>Æü¡¡ÉÕ</font>&nbsp;:&nbsp;\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü \\([0-9]+\\)»þ\\([0-9]+\\)ʬ&nbsp;&nbsp;")
1490 (defconst mixi-message-content-regexp
1491   "<tr><td CLASS=h120>\\(.+\\)</td></tr>")
1492
1493 (defun mixi-message-realize (message)
1494   "Realize a MESSAGE."
1495   (unless (mixi-object-realize-p message)
1496     (with-mixi-retrieve (mixi-message-page message)
1497       (if (string-match mixi-message-owner-regexp buffer)
1498           (mixi-message-set-owner message
1499                                   (mixi-make-friend (match-string 1 buffer)
1500                                                     (match-string 2 buffer)))
1501         (signal 'error (list 'cannot-find-owner message)))
1502       (if (string-match mixi-message-title-regexp buffer)
1503           (mixi-message-set-title message (match-string 1 buffer))
1504         (signal 'error (list 'cannot-find-title message)))
1505       (if (string-match mixi-message-time-regexp buffer)
1506           (mixi-message-set-time
1507            message (encode-time 0 (string-to-number (match-string 5 buffer))
1508                                 (string-to-number (match-string 4 buffer))
1509                                 (string-to-number (match-string 3 buffer))
1510                                 (string-to-number (match-string 2 buffer))
1511                                 (string-to-number (match-string 1 buffer))))
1512         (signal 'error (list 'cannot-find-time message)))
1513       (if (string-match mixi-message-content-regexp buffer)
1514           (mixi-message-set-content message (mixi-remove-markup
1515                                              (match-string 1 buffer)))
1516         (signal 'error (list 'cannot-find-content message))))
1517     (mixi-object-touch message)))
1518
1519 (defun mixi-message-id (message)
1520   "Return the id of MESSAGE."
1521   (unless (mixi-message-p message)
1522     (signal 'wrong-type-argument (list 'mixi-message-p message)))
1523   (aref (cdr message) 1))
1524
1525 (defun mixi-message-box (message)
1526   "Return the box of MESSAGE."
1527   (unless (mixi-message-p message)
1528     (signal 'wrong-type-argument (list 'mixi-message-p message)))
1529   (aref (cdr message) 2))
1530
1531 (defun mixi-message-owner (message)
1532   "Return the owner of MESSAGE."
1533   (unless (mixi-message-p message)
1534     (signal 'wrong-type-argument (list 'mixi-message-p message)))
1535   (mixi-message-realize message)
1536   (aref (cdr message) 3))
1537
1538 (defun mixi-message-title (message)
1539   "Return the title of MESSAGE."
1540   (unless (mixi-message-p message)
1541     (signal 'wrong-type-argument (list 'mixi-message-p message)))
1542   (mixi-message-realize message)
1543   (aref (cdr message) 4))
1544
1545 (defun mixi-message-time (message)
1546   "Return the date of MESSAGE."
1547   (unless (mixi-message-p message)
1548     (signal 'wrong-type-argument (list 'mixi-message-p message)))
1549   (mixi-message-realize message)
1550   (aref (cdr message) 5))
1551
1552 (defun mixi-message-content (message)
1553   "Return the content of MESSAGE."
1554   (unless (mixi-message-p message)
1555     (signal 'wrong-type-argument (list 'mixi-message-p message)))
1556   (mixi-message-realize message)
1557   (aref (cdr message) 6))
1558
1559 (defun mixi-message-set-owner (message owner)
1560   "Set the owner of MESSAGE."
1561   (unless (mixi-message-p message)
1562     (signal 'wrong-type-argument (list 'mixi-message-p message)))
1563   (aset (cdr message) 3 owner))
1564
1565 (defun mixi-message-set-title (message title)
1566   "Set the title of MESSAGE."
1567   (unless (mixi-message-p message)
1568     (signal 'wrong-type-argument (list 'mixi-message-p message)))
1569   (aset (cdr message) 4 title))
1570
1571 (defun mixi-message-set-time (message time)
1572   "Set the date of MESSAGE."
1573   (unless (mixi-message-p message)
1574     (signal 'wrong-type-argument (list 'mixi-message-p message)))
1575   (aset (cdr message) 5 time))
1576
1577 (defun mixi-message-set-content (message content)
1578   "Set the content of MESSAGE."
1579   (unless (mixi-message-p message)
1580     (signal 'wrong-type-argument (list 'mixi-message-p message)))
1581   (aset (cdr message) 6 content))
1582
1583 (defmacro mixi-message-list-page (&optional box)
1584   `(concat "/list_message.pl?page=%d"
1585            (when ,box (concat "&box=" ,box))))
1586
1587 (defconst mixi-message-list-regexp
1588   "<td><a HREF=\"view_message\\.pl\\?id=\\(.+\\)&box=\\(.+\\)\">")
1589
1590 (defun mixi-get-messages (&rest args)
1591   "Get messages."
1592   (when (> (length args) 2)
1593     (signal 'wrong-number-of-arguments (list 'mixi-get-messages
1594                                              (length args))))
1595   (let ((box (nth 0 args))
1596         (max-numbers (nth 1 args)))
1597     (when (or (not (stringp box)) (stringp max-numbers))
1598       (setq box (nth 1 args))
1599       (setq max-numbers (nth 0 args)))
1600     (let ((items (mixi-get-matched-items (mixi-message-list-page box)
1601                                          max-numbers
1602                                          mixi-message-list-regexp)))
1603       (mapcar (lambda (item)
1604                 (mixi-make-message (nth 0 item) (nth 1 item)))
1605               items))))
1606
1607 (provide 'mixi)
1608
1609 ;;; mixi.el ends here