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