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