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