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