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