ebe2d5ca4a4b6b81e89ae126dd3f98509000ea17
[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   (mixi-realize-diary diary)
1170   (aref (cdr diary) 3))
1171
1172 (defun mixi-diary-title (diary)
1173   "Return the title of DIARY."
1174   (unless (mixi-diary-p diary)
1175     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1176   (mixi-realize-diary diary)
1177   (aref (cdr diary) 4))
1178
1179 (defun mixi-diary-content (diary)
1180   "Return the content of DIARY."
1181   (unless (mixi-diary-p diary)
1182     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1183   (mixi-realize-diary diary)
1184   (aref (cdr diary) 5))
1185
1186 (defun mixi-diary-set-time (diary time)
1187   "Set the time of DIARY."
1188   (unless (mixi-diary-p diary)
1189     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1190   (aset (cdr diary) 3 time))
1191
1192 (defun mixi-diary-set-title (diary title)
1193   "Set the title of DIARY."
1194   (unless (mixi-diary-p diary)
1195     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1196   (aset (cdr diary) 4 title))
1197
1198 (defun mixi-diary-set-content (diary content)
1199   "Set the content of DIARY."
1200   (unless (mixi-diary-p diary)
1201     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1202   (aset (cdr diary) 5 content))
1203
1204 (defmacro mixi-diary-list-page (&optional friend)
1205   `(concat "/list_diary.pl?page=%d"
1206            (when ,friend (concat "&id=" (mixi-friend-id ,friend)))))
1207
1208 (defconst mixi-diary-list-regexp
1209   "<a href=\"view_diary\\.pl\\?id=\\([0-9]+\\)&owner_id=[0-9]+\">")
1210
1211 (defun mixi-get-diaries (&rest friend-or-range)
1212   "Get diaries of FRIEND."
1213   (when (> (length friend-or-range) 2)
1214     (signal 'wrong-number-of-arguments
1215             (list 'mixi-get-diaries (length friend-or-range))))
1216   (let ((friend (nth 0 friend-or-range))
1217         (range (nth 1 friend-or-range)))
1218     (when (or (not (mixi-friend-p friend)) (mixi-friend-p range))
1219       (setq friend (nth 1 friend-or-range))
1220       (setq range (nth 0 friend-or-range)))
1221     (unless (or (null friend) (mixi-friend-p friend))
1222       (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
1223     (let ((items (mixi-get-matched-items (mixi-diary-list-page friend)
1224                                          mixi-diary-list-regexp
1225                                          range)))
1226       (mapcar (lambda (item)
1227                 (mixi-make-diary friend (nth 0 item)))
1228               items))))
1229
1230 (defmacro mixi-new-diary-list-page ()
1231   `(concat "/new_friend_diary.pl?page=%d"))
1232
1233 (defconst mixi-new-diary-list-regexp
1234   "<a class=\"new_link\" href=view_diary\\.pl\\?id=\\([0-9]+\\)&owner_id=\\([0-9]+\\)>")
1235
1236 (defun mixi-get-new-diaries (&optional range)
1237   "Get new diaries."
1238   (let ((items (mixi-get-matched-items (mixi-new-diary-list-page)
1239                                        mixi-new-diary-list-regexp
1240                                        range)))
1241     (mapcar (lambda (item)
1242               (mixi-make-diary (mixi-make-friend (nth 1 item)) (nth 0 item)))
1243             items)))
1244
1245 (defmacro mixi-search-diary-list-page (keyword)
1246   `(concat "/search_diary.pl?page=%d&submit=search&keyword="
1247              (mixi-url-encode-and-quote-percent-string keyword)))
1248
1249 (defconst mixi-search-diary-list-regexp
1250   "<a href=\"view_diary\\.pl\\?id=\\([0-9]+\\)&owner_id=\\([0-9]+\\)\">")
1251
1252 (defun mixi-search-diaries (keyword &optional range)
1253   (let ((items (mixi-get-matched-items (mixi-search-diary-list-page keyword)
1254                                        mixi-search-diary-list-regexp
1255                                        range)))
1256     (mapcar (lambda (item)
1257               (mixi-make-diary (mixi-make-friend (nth 1 item)) (nth 0 item)))
1258             items)))
1259
1260 (defmacro mixi-post-diary-page ()
1261   `(concat "/add_diary.pl"))
1262
1263 (defconst mixi-post-key-regexp
1264   "<input type=\"?hidden\"? name=\"?post_key\"? value=\"\\([a-z0-9]+\\)\">")
1265 (defconst mixi-post-succeed-regexp
1266   "<b>\\(ºîÀ®\\|½ñ¤­¹þ¤ß\\)¤¬´°Î»¤·¤Þ¤·¤¿¡£È¿±Ç¤Ë»þ´Ö¤¬¤«¤«¤ë¤³¤È¤¬¤¢¤ê¤Þ¤¹¤Î¤Ç¡¢É½¼¨¤µ¤ì¤Æ¤¤¤Ê¤¤¾ì¹ç¤Ï¾¯¡¹¤ªÂÔ¤Á¤¯¤À¤µ¤¤¡£</b>")
1267
1268 ;; FIXME: Support photos.
1269 (defun mixi-post-diary (title content)
1270   "Post a diary."
1271   (unless (stringp title)
1272     (signal 'wrong-type-argument (list 'stringp title)))
1273   (unless (stringp content)
1274     (signal 'wrong-type-argument (list 'stringp content)))
1275   (let ((fields `(("id" . ,(mixi-friend-id (mixi-make-me)))
1276                   ("diary_title" . ,title)
1277                   ("diary_body" . ,content)
1278                   ("submit" . "main")))
1279         post-key)
1280     (with-mixi-post-form (mixi-post-diary-page) fields
1281       (if (string-match mixi-post-key-regexp buffer)
1282           (setq post-key (match-string 1 buffer))
1283         (mixi-post-error 'cannot-find-key)))
1284     (setq fields `(("post_key" . ,post-key)
1285                    ("id" . ,(mixi-friend-id (mixi-make-me)))
1286                    ("diary_title" . ,title)
1287                    ("diary_body" . ,content)
1288                    ("submit" . "confirm")))
1289     (with-mixi-post-form (mixi-post-diary-page) fields
1290       (unless (string-match mixi-post-succeed-regexp buffer)
1291         (mixi-post-error 'cannot-find-succeed)))))
1292
1293 ;; Community object.
1294 (defvar mixi-community-cache (make-hash-table :test 'equal))
1295 (defun mixi-make-community (id &optional name birthday owner category members
1296                                open-level authority description)
1297   "Return a community object."
1298   (mixi-make-cache id (cons 'mixi-community (vector nil id name birthday owner
1299                                                     category members
1300                                                     open-level authority
1301                                                     description))
1302                    mixi-community-cache))
1303
1304 (defconst mixi-community-url-regexp
1305   "/view_community\\.pl\\?id=\\([0-9]+\\)")
1306
1307 (defun mixi-make-community-from-url (url)
1308   "Return a community object from URL."
1309   (when (string-match mixi-community-url-regexp url)
1310     (let ((id (match-string 1 url)))
1311       (mixi-make-community id))))
1312
1313 (defmacro mixi-community-p (community)
1314   `(eq (mixi-object-class ,community) 'mixi-community))
1315
1316 (defmacro mixi-community-page (community)
1317   `(concat "/view_community.pl?id=" (mixi-community-id ,community)))
1318
1319 ;; FIXME: Not community specific.
1320 (defconst mixi-community-nodata-regexp
1321   "^¥Ç¡¼¥¿¤¬¤¢¤ê¤Þ¤»¤ó")
1322 (defconst mixi-community-name-regexp
1323   "<td WIDTH=345>\\(.*\\)</td></tr>")
1324 (defconst mixi-community-birthday-regexp
1325   "<td BGCOLOR=#F2DDB7 WIDTH=80><font COLOR=#996600>³«ÀßÆü</font></td>\r
1326 <td WIDTH=345>\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü</td>")
1327 ;; FIXME: Care when the owner has seceded.
1328 (defconst mixi-community-owner-regexp
1329   "<td BGCOLOR=#F2DDB7 WIDTH=80><font COLOR=#996600>´ÉÍý¿Í</font></td>\r
1330 <td WIDTH=345>\r
1331 <table style=\"width: 100%;\"><tr><td>\r
1332 <a href=\"\\(home\\.pl\\|show_friend\\.pl\\?id=\\([0-9]+\\)\\)\">\\(.*\\)</a>")
1333 (defconst mixi-community-category-regexp
1334   "<td BGCOLOR=#F2DDB7 WIDTH=80><font COLOR=#996600>¥«¥Æ¥´¥ê</font></td>\r
1335 <td WIDTH=345>\\([^<]+\\)</td>")
1336 (defconst mixi-community-members-regexp
1337   "<td BGCOLOR=#F2DDB7 WIDTH=80><font COLOR=#996600>¥á¥ó¥Ð¡¼¿ô</font></td>\r
1338 <td WIDTH=345>\\([0-9]+\\)¿Í</td></tr>")
1339 (defconst mixi-community-open-level-regexp
1340   "<td BGCOLOR=#F2DDB7 WIDTH=80><font COLOR=#996600>»²²Ã¾ò·ï¤È<br>¸ø³«¥ì¥Ù¥ë</font></td>\r
1341 <td WIDTH=345>\\(.+\\)</td></tr>")
1342 (defconst mixi-community-authority-regexp
1343   "<td BGCOLOR=#F2DDB7 WIDTH=80><font COLOR=#996600>¥È¥Ô¥Ã¥¯ºîÀ®¤Î¸¢¸Â</font></td>\r
1344 <td WIDTH=345>\\(.+\\)</td></tr>")
1345 (defconst mixi-community-description-regexp
1346   "<td CLASS=h120 WIDTH=345>\\(.+\\)</td>")
1347
1348 (defun mixi-realize-community (community)
1349   "Realize a COMMUNITY."
1350   ;; FIXME: Check a expiration of cache?
1351   (unless (mixi-object-realized-p community)
1352     (with-mixi-retrieve (mixi-community-page community)
1353       (if (string-match mixi-community-nodata-regexp buffer)
1354           ;; FIXME: Set all members?
1355           (mixi-community-set-name community "¥Ç¡¼¥¿¤¬¤¢¤ê¤Þ¤»¤ó")
1356         (if (string-match mixi-community-name-regexp buffer)
1357             (mixi-community-set-name community (match-string 1 buffer))
1358           (mixi-realization-error 'cannot-find-name community))
1359         (if (string-match mixi-community-birthday-regexp buffer)
1360             (mixi-community-set-birthday
1361              community
1362              (encode-time 0 0 0 (string-to-number (match-string 3 buffer))
1363                           (string-to-number (match-string 2 buffer))
1364                           (string-to-number (match-string 1 buffer))))
1365           (mixi-realization-error 'cannot-find-birthday community))
1366         (if (string-match mixi-community-owner-regexp buffer)
1367             (if (string= (match-string 1 buffer) "home.pl")
1368                 (mixi-community-set-owner community (mixi-make-me))
1369               (mixi-community-set-owner
1370                community (mixi-make-friend (match-string 2 buffer)
1371                                            (match-string 3 buffer))))
1372           (mixi-realization-error 'cannot-find-owner community))
1373         (if (string-match mixi-community-category-regexp buffer)
1374             (mixi-community-set-category community (match-string 1 buffer))
1375           (mixi-realization-error 'cannot-find-category community))
1376         (if (string-match mixi-community-members-regexp buffer)
1377             (mixi-community-set-members
1378              community (string-to-number (match-string 1 buffer)))
1379           (mixi-realization-error 'cannot-find-members community))
1380         (if (string-match mixi-community-open-level-regexp buffer)
1381             (mixi-community-set-open-level community (match-string 1 buffer))
1382           (mixi-realization-error 'cannot-find-open-level community))
1383         (if (string-match mixi-community-authority-regexp buffer)
1384             (mixi-community-set-authority community (match-string 1 buffer))
1385           (mixi-realization-error 'cannot-find-authority community))
1386         (if (string-match mixi-community-description-regexp buffer)
1387             (mixi-community-set-description community (match-string 1 buffer))
1388           (mixi-realization-error 'cannot-find-description community))))
1389     (mixi-object-touch community)))
1390
1391 (defun mixi-community-id (community)
1392   "Return the id of COMMUNITY."
1393   (unless (mixi-community-p community)
1394     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1395   (aref (cdr community) 1))
1396
1397 (defun mixi-community-name (community)
1398   "Return the name of COMMUNITY."
1399   (unless (mixi-community-p community)
1400     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1401   (unless (aref (cdr community) 2)
1402     (mixi-realize-community community))
1403   (aref (cdr community) 2))
1404
1405 (defun mixi-community-birthday (community)
1406   "Return the birthday of COMMUNITY."
1407   (unless (mixi-community-p community)
1408     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1409   (mixi-realize-community community)
1410   (aref (cdr community) 3))
1411
1412 (defun mixi-community-owner (community)
1413   "Return the owner of COMMUNITY."
1414   (unless (mixi-community-p community)
1415     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1416   (mixi-realize-community community)
1417   (aref (cdr community) 4))
1418
1419 (defun mixi-community-category (community)
1420   "Return the category of COMMUNITY."
1421   (unless (mixi-community-p community)
1422     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1423   (mixi-realize-community community)
1424   (aref (cdr community) 5))
1425
1426 (defun mixi-community-members (community)
1427   "Return the members of COMMUNITY."
1428   (unless (mixi-community-p community)
1429     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1430   (mixi-realize-community community)
1431   (aref (cdr community) 6))
1432
1433 (defun mixi-community-open-level (community)
1434   "Return the open-level of COMMUNITY."
1435   (unless (mixi-community-p community)
1436     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1437   (mixi-realize-community community)
1438   (aref (cdr community) 7))
1439
1440 (defun mixi-community-authority (community)
1441   "Return the authority of COMMUNITY."
1442   (unless (mixi-community-p community)
1443     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1444   (mixi-realize-community community)
1445   (aref (cdr community) 8))
1446
1447 (defun mixi-community-description (community)
1448   "Return the description of COMMUNITY."
1449   (unless (mixi-community-p community)
1450     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1451   (mixi-realize-community community)
1452   (aref (cdr community) 9))
1453
1454 (defun mixi-community-set-name (community name)
1455   "Set the name of COMMUNITY."
1456   (unless (mixi-community-p community)
1457     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1458   (aset (cdr community) 2 name))
1459
1460 (defun mixi-community-set-birthday (community birthday)
1461   "Set the birthday of COMMUNITY."
1462   (unless (mixi-community-p community)
1463     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1464   (aset (cdr community) 3 birthday))
1465
1466 (defun mixi-community-set-owner (community owner)
1467   "Set the owner of COMMUNITY."
1468   (unless (mixi-community-p community)
1469     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1470   (unless (mixi-friend-p owner)
1471     (signal 'wrong-type-argument (list 'mixi-friend-p owner)))
1472   (aset (cdr community) 4 owner))
1473
1474 (defun mixi-community-set-category (community category)
1475   "Set the category of COMMUNITY."
1476   (unless (mixi-community-p community)
1477     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1478   (aset (cdr community) 5 category))
1479
1480 (defun mixi-community-set-members (community members)
1481   "Set the name of COMMUNITY."
1482   (unless (mixi-community-p community)
1483     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1484   (aset (cdr community) 6 members))
1485
1486 (defun mixi-community-set-open-level (community open-level)
1487   "Set the name of COMMUNITY."
1488   (unless (mixi-community-p community)
1489     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1490   (aset (cdr community) 7 open-level))
1491
1492 (defun mixi-community-set-authority (community authority)
1493   "Set the name of COMMUNITY."
1494   (unless (mixi-community-p community)
1495     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1496   (aset (cdr community) 8 authority))
1497
1498 (defun mixi-community-set-description (community description)
1499   "Set the name of COMMUNITY."
1500   (unless (mixi-community-p community)
1501     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1502   (aset (cdr community) 9 description))
1503
1504 (defmacro mixi-community-list-page (&optional friend)
1505   `(concat "/list_community.pl?page=%d"
1506            (when ,friend (concat "&id=" (mixi-friend-id ,friend)))))
1507
1508 (defconst mixi-community-list-id-regexp
1509   "<a href=view_community\\.pl\\?id=\\([0-9]+\\)")
1510 (defconst mixi-community-list-name-regexp
1511   "<td valign=middle>\\(.+\\)([0-9]+)</td>")
1512
1513 (defun mixi-get-communities (&rest friend-or-range)
1514   "Get communities of FRIEND."
1515   (when (> (length friend-or-range) 2)
1516     (signal 'wrong-number-of-arguments
1517             (list 'mixi-get-communities (length friend-or-range))))
1518   (let ((friend (nth 0 friend-or-range))
1519         (range (nth 1 friend-or-range)))
1520     (when (or (not (mixi-friend-p friend)) (mixi-friend-p range))
1521       (setq friend (nth 1 friend-or-range))
1522       (setq range (nth 0 friend-or-range)))
1523     (unless (or (null friend) (mixi-friend-p friend))
1524       (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
1525     (let ((ids (mixi-get-matched-items (mixi-community-list-page friend)
1526                                        mixi-community-list-id-regexp
1527                                        range))
1528           (names (mixi-get-matched-items (mixi-community-list-page friend)
1529                                          mixi-community-list-name-regexp
1530                                          range)))
1531       (let ((index 0)
1532             ret)
1533         (while (< index (length ids))
1534           (setq ret (cons (mixi-make-community (nth 0 (nth index ids))
1535                                                (nth 0 (nth index names))) ret))
1536           (incf index))
1537         (reverse ret)))))
1538
1539 (defmacro mixi-search-community-list-page (keyword)
1540   `(concat "/search_community.pl?page=%d&&sort=date&type=com&submit=main"
1541            "&keyword=" (mixi-url-encode-and-quote-percent-string keyword)
1542            "&category_id=0"))
1543
1544 (defconst mixi-search-community-list-regexp
1545   "<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>
1546 <td NOWRAP WIDTH=90 BGCOLOR=#FDF9F2><font COLOR=#996600>¥³¥ß¥å¥Ë¥Æ¥£Ì¾</font></td>
1547 <td COLSPAN=2 WIDTH=370 BGCOLOR=#FFFFFF>\\([^<]+\\)</td></tr>")
1548
1549 ;; FIXME: Support category.
1550 (defun mixi-search-communities (keyword &optional range)
1551   (let ((items (mixi-get-matched-items (mixi-search-community-list-page
1552                                         keyword)
1553                                        mixi-search-community-list-regexp
1554                                        range)))
1555     (mapcar (lambda (item)
1556               (mixi-make-community (nth 0 item) (nth 1 item)))
1557             items)))
1558
1559 ;; Topic object.
1560 (defvar mixi-topic-cache (make-hash-table :test 'equal))
1561 (defun mixi-make-topic (community id &optional time title owner content)
1562   "Return a topic object."
1563   (mixi-make-cache (list (mixi-community-id community) id)
1564                    (cons 'mixi-topic (vector nil community id time title owner
1565                                              content))
1566                    mixi-topic-cache))
1567
1568 (defconst mixi-topic-url-regexp
1569   "/view_bbs\\.pl\\?id=\\([0-9]+\\)\\(&comment_count=[0-9]+\\)?&comm_id=\\([0-9]+\\)")
1570
1571 (defun mixi-make-topic-from-url (url)
1572   "Return a topic object from URL."
1573   (when (string-match mixi-topic-url-regexp url)
1574     (let ((id (match-string 1 url))
1575           (community-id (match-string 3 url)))
1576       (mixi-make-topic (mixi-make-community community-id) id))))
1577
1578 (defmacro mixi-topic-p (topic)
1579   `(eq (mixi-object-class ,topic) 'mixi-topic))
1580
1581 (defmacro mixi-topic-page (topic)
1582   `(concat "/view_bbs.pl?id=" (mixi-topic-id ,topic)
1583            "&comm_id=" (mixi-community-id (mixi-topic-community ,topic))))
1584
1585 (defconst mixi-topic-community-regexp
1586   "<td width=\"595\" background=\"http://img\\.mixi\\.jp/img/bg_w\\.gif\"><b>\\[\\(.+\\)\\] ¥È¥Ô¥Ã¥¯</b></td>")
1587 (defconst mixi-topic-time-regexp
1588   "<td rowspan=\"3\" width=\"110\" bgcolor=\"#ffd8b0\" align=\"center\" valign=\"top\" nowrap>\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü<br>\\([0-9]+\\):\\([0-9]+\\)</td>")
1589 (defconst mixi-topic-title-regexp
1590   "<td bgcolor=\"#fff4e0\">&nbsp;\\([^<]+\\)</td>")
1591 (defconst mixi-topic-owner-regexp
1592   "<td bgcolor=\"#fdf9f2\">&nbsp;<font color=\"#dfb479\"></font>&nbsp;<a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.*?\\)\\(¤µ¤ó\\)?</a>")
1593 (defconst mixi-topic-content-regexp
1594   "<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>")
1595
1596 (defun mixi-realize-topic (topic)
1597   "Realize a TOPIC."
1598   ;; FIXME: Check a expiration of cache?
1599   (unless (mixi-object-realized-p topic)
1600     (with-mixi-retrieve (mixi-topic-page topic)
1601       (if (string-match mixi-topic-community-regexp buffer)
1602           (mixi-community-set-name (mixi-topic-community topic)
1603                                    (match-string 1 buffer))
1604         (mixi-realization-error 'cannot-find-community topic))
1605       (if (string-match mixi-topic-time-regexp buffer)
1606           (mixi-topic-set-time
1607            topic (encode-time 0 (string-to-number (match-string 5 buffer))
1608                               (string-to-number (match-string 4 buffer))
1609                               (string-to-number (match-string 3 buffer))
1610                               (string-to-number (match-string 2 buffer))
1611                               (string-to-number (match-string 1 buffer))))
1612         (mixi-realization-error 'cannot-find-time topic))
1613       (if (string-match mixi-topic-title-regexp buffer)
1614           (mixi-topic-set-title topic (match-string 1 buffer))
1615         (mixi-realization-error 'cannot-find-title topic))
1616       (if (string-match mixi-topic-owner-regexp buffer)
1617           (mixi-topic-set-owner topic
1618                                 (mixi-make-friend (match-string 1 buffer)
1619                                                   (match-string 2 buffer)))
1620         (mixi-realization-error 'cannot-find-owner topic))
1621       (if (string-match mixi-topic-content-regexp buffer)
1622           (mixi-topic-set-content topic (match-string 2 buffer))
1623         (mixi-realization-error 'cannot-find-content topic)))
1624     (mixi-object-touch topic)))
1625
1626 (defun mixi-topic-community (topic)
1627   "Return the community of TOPIC."
1628   (unless (mixi-topic-p topic)
1629     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1630   (aref (cdr topic) 1))
1631
1632 (defun mixi-topic-id (topic)
1633   "Return the id of TOPIC."
1634   (unless (mixi-topic-p topic)
1635     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1636   (aref (cdr topic) 2))
1637
1638 (defun mixi-topic-time (topic)
1639   "Return the time of TOPIC."
1640   (unless (mixi-topic-p topic)
1641     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1642   (mixi-realize-topic topic)
1643   (aref (cdr topic) 3))
1644
1645 (defun mixi-topic-title (topic)
1646   "Return the title of TOPIC."
1647   (unless (mixi-topic-p topic)
1648     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1649   (mixi-realize-topic topic)
1650   (aref (cdr topic) 4))
1651
1652 (defun mixi-topic-owner (topic)
1653   "Return the owner of TOPIC."
1654   (unless (mixi-topic-p topic)
1655     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1656   (mixi-realize-topic topic)
1657   (aref (cdr topic) 5))
1658
1659 (defun mixi-topic-content (topic)
1660   "Return the content of TOPIC."
1661   (unless (mixi-topic-p topic)
1662     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1663   (mixi-realize-topic topic)
1664   (aref (cdr topic) 6))
1665
1666 (defun mixi-topic-set-time (topic time)
1667   "Set the time of TOPIC."
1668   (unless (mixi-topic-p topic)
1669     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1670   (aset (cdr topic) 3 time))
1671
1672 (defun mixi-topic-set-title (topic title)
1673   "Set the title of TOPIC."
1674   (unless (mixi-topic-p topic)
1675     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1676   (aset (cdr topic) 4 title))
1677
1678 (defun mixi-topic-set-owner (topic owner)
1679   "Set the owner of TOPIC."
1680   (unless (mixi-topic-p topic)
1681     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1682   (unless (mixi-friend-p owner)
1683     (signal 'wrong-type-argument (list 'mixi-friend-p owner)))
1684   (aset (cdr topic) 5 owner))
1685
1686 (defun mixi-topic-set-content (topic content)
1687   "Set the content of TOPIC."
1688   (unless (mixi-topic-p topic)
1689     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1690   (aset (cdr topic) 6 content))
1691
1692 (defmacro mixi-post-topic-page (community)
1693   `(concat "/add_bbs.pl?id=" (mixi-community-id community)))
1694
1695 ;; FIXME: Support photos.
1696 (defun mixi-post-topic (community title content)
1697   "Post a topic to COMMUNITY."
1698   (unless (mixi-community-p community)
1699     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1700   (unless (stringp title)
1701     (signal 'wrong-type-argument (list 'stringp title)))
1702   (unless (stringp content)
1703     (signal 'wrong-type-argument (list 'stringp content)))
1704   (let ((fields `(("bbs_title" . ,title)
1705                   ("bbs_body" . ,content)
1706                   ("submit" . "main")))
1707         post-key)
1708     (with-mixi-post-form (mixi-post-topic-page community) fields
1709       (if (string-match mixi-post-key-regexp buffer)
1710           (setq post-key (match-string 1 buffer))
1711         (mixi-post-error 'cannot-find-key community)))
1712     (setq fields `(("post_key" . ,post-key)
1713                    ("bbs_title" . ,title)
1714                    ("bbs_body" . ,content)
1715                    ("submit" . "confirm")))
1716     (with-mixi-post-form (mixi-post-topic-page community) fields
1717       (unless (string-match mixi-post-succeed-regexp buffer)
1718         (mixi-post-error 'cannot-find-succeed community)))))
1719
1720 ;; Event object.
1721 (defvar mixi-event-cache (make-hash-table :test 'equal))
1722 (defun mixi-make-event (community id &optional time title owner date place
1723                                   detail limit members)
1724   "Return a event object."
1725   (mixi-make-cache (list (mixi-community-id community) id)
1726                    (cons 'mixi-event (vector nil community id time title owner
1727                                              date place detail limit members))
1728                    mixi-event-cache))
1729
1730 (defconst mixi-event-url-regexp
1731   "/view_event\\.pl\\?id=\\([0-9]+\\)\\(&comment_count=[0-9]+\\)?&comm_id=\\([0-9]+\\)")
1732
1733 (defun mixi-make-event-from-url (url)
1734   "Return a event object from URL."
1735   (when (string-match mixi-event-url-regexp url)
1736     (let ((id (match-string 1 url))
1737           (community-id (match-string 3 url)))
1738       (mixi-make-event (mixi-make-community community-id) id))))
1739
1740 (defmacro mixi-event-p (event)
1741   `(eq (mixi-object-class ,event) 'mixi-event))
1742
1743 (defmacro mixi-event-page (event)
1744   `(concat "/view_event.pl?id=" (mixi-event-id ,event)
1745            "&comm_id=" (mixi-community-id (mixi-event-community ,event))))
1746
1747 (defconst mixi-event-community-regexp
1748   "<td WIDTH=595 background=http://img\\.mixi\\.jp/img/bg_w\\.gif><b>\\[\\(.+\\)\\] ¥¤¥Ù¥ó¥È</b></td>")
1749 (defconst mixi-event-time-regexp
1750   "<td ROWSPAN=11 \\(BGCOLOR\\|bgcolor\\)=#FFD8B0 \\(ALIGN\\|align\\)=center \\(VALIGN\\|Valign\\)=top WIDTH=110>
1751 ?\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü<br>
1752 ?\\([0-9]+\\):\\([0-9]+\\)</td>")
1753 (defconst mixi-event-title-regexp
1754   "<td bgcolor=#FFF4E0\\( width=410\\)?>&nbsp;\\([^<]+\\)</td>")
1755 (defconst mixi-event-owner-regexp
1756   "<td \\(BGCOLOR\\|bgcolor\\)=#FDF9F2>&nbsp;<a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.*\\)</a>")
1757 (defconst mixi-event-owner-seceded-regexp
1758   "<td \\(BGCOLOR\\|bgcolor\\)=#FDF9F2>&nbsp;\\((mixi Âà²ñºÑ)\\)")
1759 (defconst mixi-event-date-regexp
1760   "<td \\(BGCOLOR\\|bgcolor\\)=#\\(FFFFFF\\|ffffff\\) \\(ALIGN\\|align\\)=center NOWRAP>³«ºÅÆü»þ</td>
1761 <td \\(BGCOLOR\\|bgcolor\\)=#\\(FFFFFF\\|ffffff\\)>
1762 &nbsp;\\(.+\\)
1763 </td>")
1764 (defconst mixi-event-place-regexp
1765   "<td \\(BGCOLOR\\|bgcolor\\)=#\\(FFFFFF\\|ffffff\\) \\(ALIGN\\|align\\)=center NOWRAP>³«ºÅ¾ì½ê</td>
1766 <td \\(BGCOLOR\\|bgcolor\\)=#\\(FFFFFF\\|ffffff\\)>
1767 &nbsp;\\(.+\\)
1768 </td>")
1769 (defconst mixi-event-detail-regexp
1770   "<td \\(BGCOLOR\\|bgcolor\\)=#\\(FFFFFF\\|ffffff\\) \\(ALIGN\\|align\\)=center NOWRAP>¾ÜºÙ</td>
1771 <td \\(BGCOLOR\\|bgcolor\\)=#\\(FFFFFF\\|ffffff\\)><table BORDER=0 CELLSPACING=0 CELLPADDING=5><tr><td CLASS=h120>\\(.+\\)</td></tr></table></td>")
1772 (defconst mixi-event-limit-regexp
1773   "<td \\(BGCOLOR\\|bgcolor\\)=\"?#\\(FFFFFF\\|ffffff\\)\"? \\(ALIGN\\|align\\)=\"?center\"? NOWRAP>Ê罸´ü¸Â</td>
1774 ?<td \\(BGCOLOR\\|bgcolor\\)=#\\(FFFFFF\\|ffffff\\)>&nbsp;\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü</td>")
1775 (defconst mixi-event-members-regexp
1776   "<td \\(BGCOLOR\\|bgcolor\\)=#\\(FFFFFF\\|ffffff\\) \\(ALIGN\\|align\\)=center NOWRAP>»²²Ã¼Ô</td>
1777 <td \\(BGCOLOR\\|bgcolor\\)=#\\(FFFFFF\\|ffffff\\)>
1778
1779 ?
1780 ?<table BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH=100%>
1781 <tr>
1782
1783 ?<td>&nbsp;\\(.+\\)</td>")
1784
1785 (defun mixi-realize-event (event)
1786   "Realize a EVENT."
1787   ;; FIXME: Check a expiration of cache?
1788   (unless (mixi-object-realized-p event)
1789     (with-mixi-retrieve (mixi-event-page event)
1790       (if (string-match mixi-event-community-regexp buffer)
1791           (mixi-community-set-name (mixi-event-community event)
1792                                    (match-string 1 buffer))
1793         (mixi-realization-error 'cannot-find-community event))
1794       (if (string-match mixi-event-time-regexp buffer)
1795           (mixi-event-set-time
1796            event (encode-time 0 (string-to-number (match-string 8 buffer))
1797                               (string-to-number (match-string 7 buffer))
1798                               (string-to-number (match-string 6 buffer))
1799                               (string-to-number (match-string 5 buffer))
1800                               (string-to-number (match-string 4 buffer))))
1801         (mixi-realization-error 'cannot-find-time event))
1802       (if (string-match mixi-event-title-regexp buffer)
1803           (mixi-event-set-title event (match-string 2 buffer))
1804         (mixi-realization-error 'cannot-find-title event))
1805       (if (string-match mixi-event-owner-regexp buffer)
1806           (mixi-event-set-owner event
1807                                 (mixi-make-friend (match-string 2 buffer)
1808                                                   (match-string 3 buffer)))
1809         (if (string-match mixi-event-owner-seceded-regexp buffer)
1810             (mixi-event-set-owner event
1811                                   (mixi-make-friend nil
1812                                                     (match-string 2 buffer)))
1813           (mixi-realization-error 'cannot-find-owner event)))
1814       (if (string-match mixi-event-date-regexp buffer)
1815           (mixi-event-set-date event (match-string 6 buffer))
1816         (mixi-realization-error 'cannot-find-date event))
1817       (if (string-match mixi-event-place-regexp buffer)
1818           (mixi-event-set-place event (match-string 6 buffer))
1819         (mixi-realization-error 'cannot-find-place event))
1820       (if (string-match mixi-event-detail-regexp buffer)
1821           (mixi-event-set-detail event (match-string 6 buffer))
1822         (mixi-realization-error 'cannot-find-detail event))
1823       (when (string-match mixi-event-limit-regexp buffer)
1824         (mixi-event-set-limit
1825          event (encode-time 0 0 0 (string-to-number (match-string 8 buffer))
1826                             (string-to-number (match-string 7 buffer))
1827                             (string-to-number (match-string 6 buffer)))))
1828       (if (string-match mixi-event-members-regexp buffer)
1829           (mixi-event-set-members event (match-string 6 buffer))
1830         (mixi-realization-error 'cannot-find-members event)))
1831     (mixi-object-touch event)))
1832
1833 (defun mixi-event-community (event)
1834   "Return the community of EVENT."
1835   (unless (mixi-event-p event)
1836     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1837   (aref (cdr event) 1))
1838
1839 (defun mixi-event-id (event)
1840   "Return the id of EVENT."
1841   (unless (mixi-event-p event)
1842     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1843   (aref (cdr event) 2))
1844
1845 (defun mixi-event-time (event)
1846   "Return the time of EVENT."
1847   (unless (mixi-event-p event)
1848     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1849   (mixi-realize-event event)
1850   (aref (cdr event) 3))
1851
1852 (defun mixi-event-title (event)
1853   "Return the title of EVENT."
1854   (unless (mixi-event-p event)
1855     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1856   (mixi-realize-event event)
1857   (aref (cdr event) 4))
1858
1859 (defun mixi-event-owner (event)
1860   "Return the owner of EVENT."
1861   (unless (mixi-event-p event)
1862     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1863   (mixi-realize-event event)
1864   (aref (cdr event) 5))
1865
1866 (defun mixi-event-date (event)
1867   "Return the date of EVENT."
1868   (unless (mixi-event-p event)
1869     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1870   (mixi-realize-event event)
1871   (aref (cdr event) 6))
1872
1873 (defun mixi-event-place (event)
1874   "Return the place of EVENT."
1875   (unless (mixi-event-p event)
1876     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1877   (mixi-realize-event event)
1878   (aref (cdr event) 7))
1879
1880 (defun mixi-event-detail (event)
1881   "Return the detail of EVENT."
1882   (unless (mixi-event-p event)
1883     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1884   (mixi-realize-event event)
1885   (aref (cdr event) 8))
1886
1887 (defun mixi-event-limit (event)
1888   "Return the limit of EVENT."
1889   (unless (mixi-event-p event)
1890     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1891   (mixi-realize-event event)
1892   (aref (cdr event) 9))
1893
1894 (defun mixi-event-members (event)
1895   "Return the members of EVENT."
1896   (unless (mixi-event-p event)
1897     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1898   (mixi-realize-event event)
1899   (aref (cdr event) 10))
1900
1901 (defun mixi-event-set-time (event time)
1902   "Set the time of EVENT."
1903   (unless (mixi-event-p event)
1904     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1905   (aset (cdr event) 3 time))
1906
1907 (defun mixi-event-set-title (event title)
1908   "Set the title of EVENT."
1909   (unless (mixi-event-p event)
1910     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1911   (aset (cdr event) 4 title))
1912
1913 (defun mixi-event-set-owner (event owner)
1914   "Set the owner of EVENT."
1915   (unless (mixi-event-p event)
1916     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1917   (unless (mixi-friend-p owner)
1918     (signal 'wrong-type-argument (list 'mixi-friend-p owner)))
1919   (aset (cdr event) 5 owner))
1920
1921 (defun mixi-event-set-date (event date)
1922   "Set the date of EVENT."
1923   (unless (mixi-event-p event)
1924     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1925   (aset (cdr event) 6 date))
1926
1927 (defun mixi-event-set-place (event place)
1928   "Set the place of EVENT."
1929   (unless (mixi-event-p event)
1930     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1931   (aset (cdr event) 7 place))
1932
1933 (defun mixi-event-set-detail (event detail)
1934   "Set the detail of EVENT."
1935   (unless (mixi-event-p event)
1936     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1937   (aset (cdr event) 8 detail))
1938
1939 (defun mixi-event-set-limit (event limit)
1940   "Set the limit of EVENT."
1941   (unless (mixi-event-p event)
1942     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1943   (aset (cdr event) 9 limit))
1944
1945 (defun mixi-event-set-members (event members)
1946   "Set the members of EVENT."
1947   (unless (mixi-event-p event)
1948     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1949   (aset (cdr event) 10 members))
1950
1951 ;; Bbs object.
1952 (defconst mixi-bbs-list '(mixi-topic mixi-event))
1953
1954 (defmacro mixi-bbs-p (object)
1955   `(when (memq (mixi-object-class ,object) mixi-bbs-list)
1956      t))
1957
1958 (defun mixi-bbs-community (object)
1959   "Return the community of OBJECT."
1960   (unless (mixi-bbs-p object)
1961     (signal 'wrong-type-argument (list 'mixi-bbs-p object)))
1962   (let ((func (intern (concat mixi-object-prefix
1963                               (mixi-object-name object) "-community"))))
1964     (funcall func object)))
1965
1966 (defalias 'mixi-bbs-id 'mixi-object-id)
1967 (defalias 'mixi-bbs-time 'mixi-object-time)
1968 (defalias 'mixi-bbs-title 'mixi-object-title)
1969 (defalias 'mixi-bbs-owner 'mixi-object-owner)
1970 (defalias 'mixi-bbs-content 'mixi-object-content)
1971
1972 (defmacro mixi-bbs-list-page (community)
1973   `(concat "/list_bbs.pl?page=%d"
1974            "&id=" (mixi-community-id ,community)))
1975
1976 (defconst mixi-bbs-list-regexp
1977   "<a href=view_\\(bbs\\|event\\)\\.pl\\?id=\\([0-9]+\\)")
1978
1979 (defun mixi-get-bbses (community &optional range)
1980   "Get bbese of COMMUNITY."
1981   (unless (mixi-community-p community)
1982     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1983   (let ((items (mixi-get-matched-items (mixi-bbs-list-page community)
1984                                        mixi-bbs-list-regexp
1985                                        range)))
1986     (mapcar (lambda (item)
1987               (let ((name (nth 0 item)))
1988                 (when (string= name "bbs")
1989                   (setq name "topic"))
1990                 (let ((func (intern (concat "mixi-make-" name))))
1991                   (funcall func community (nth 1 item)))))
1992             items)))
1993
1994 (defmacro mixi-new-bbs-list-page ()
1995   `(concat "/new_bbs.pl?page=%d"))
1996
1997 (defconst mixi-new-bbs-list-regexp
1998   "<a href=\"view_\\(bbs\\|event\\)\\.pl\\?id=\\([0-9]+\\)&comment_count=[0-9]+&comm_id=\\([0-9]+\\)\" class=\"new_link\">")
1999
2000 (defun mixi-get-new-bbses (&optional range)
2001   "Get new topics."
2002   (let ((items (mixi-get-matched-items (mixi-new-bbs-list-page)
2003                                        mixi-new-bbs-list-regexp
2004                                        range)))
2005     (mapcar (lambda (item)
2006               (let ((name (nth 0 item)))
2007                 (when (string= name "bbs")
2008                   (setq name "topic"))
2009                 (let ((func (intern (concat "mixi-make-" name))))
2010                   (funcall func (mixi-make-community (nth 2 item))
2011                            (nth 1 item)))))
2012             items)))
2013
2014 (defmacro mixi-search-bbs-list-page (keyword)
2015   `(concat "/search_topic.pl?page=%d&type=top&submit=search"
2016            "&keyword=" (mixi-url-encode-and-quote-percent-string keyword)
2017            "&community_id=0&category_id=0"))
2018
2019 (defconst mixi-search-bbs-list-regexp
2020   "<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>")
2021
2022 ;; FIXME: Support community and category.
2023 (defun mixi-search-bbses (keyword &optional range)
2024   (let ((items (mixi-get-matched-items (mixi-search-bbs-list-page keyword)
2025                                        mixi-search-bbs-list-regexp
2026                                        range)))
2027     (mapcar (lambda (item)
2028               (let ((name (nth 0 item)))
2029                 (when (string= name "bbs")
2030                   (setq name "topic"))
2031                 (let ((func (intern (concat "mixi-make-" name))))
2032                   (funcall func (mixi-make-community (nth 2 item))
2033                            (nth 1 item)))))
2034             items)))
2035
2036 ;; Comment object.
2037 (defun mixi-make-comment (parent owner time content)
2038   "Return a comment object."
2039   (cons 'mixi-comment (vector parent owner time content)))
2040
2041 (defmacro mixi-comment-p (comment)
2042   `(eq (mixi-object-class ,comment) 'mixi-comment))
2043
2044 (defun mixi-comment-parent (comment)
2045   "Return the parent of COMMENT."
2046   (unless (mixi-comment-p comment)
2047     (signal 'wrong-type-argument (list 'mixi-comment-p comment)))
2048   (aref (cdr comment) 0))
2049
2050 (defun mixi-comment-owner (comment)
2051   "Return the owner of COMMENT."
2052   (unless (mixi-comment-p comment)
2053     (signal 'wrong-type-argument (list 'mixi-comment-p comment)))
2054   (aref (cdr comment) 1))
2055
2056 (defun mixi-comment-time (comment)
2057   "Return the time of COMMENT."
2058   (unless (mixi-comment-p comment)
2059     (signal 'wrong-type-argument (list 'mixi-comment-p comment)))
2060   (aref (cdr comment) 2))
2061
2062 (defun mixi-comment-content (comment)
2063   "Return the content of COMMENT."
2064   (unless (mixi-comment-p comment)
2065     (signal 'wrong-type-argument (list 'mixi-comment-p comment)))
2066   (aref (cdr comment) 3))
2067
2068 (defun mixi-diary-comment-list-page (diary)
2069   (concat "/view_diary.pl?full=1"
2070           "&id=" (mixi-diary-id diary)
2071           "&owner_id=" (mixi-friend-id (mixi-diary-owner diary))))
2072
2073 ;; FIXME: Split regexp to time, owner(id and nick) and contents.
2074 (defconst mixi-diary-comment-list-regexp
2075 "<td rowspan=\"2\" align=\"center\" width=\"95\" bgcolor=\"#f2ddb7\" nowrap>
2076 \\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü<br>\\([0-9]+\\):\\([0-9]+\\)\\(<br>
2077 <input type=checkbox name=comment_id value=\".+\">
2078 \\|\\)
2079 </td>
2080 <td ALIGN=center BGCOLOR=#FDF9F2 WIDTH=430>
2081 <table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"410\">
2082 <tr>
2083 \\(<td>\\)
2084 <a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.*\\)</a>
2085
2086 \\(<font color=\"#f2ddb7\">|</font> <a href=[^>]+>ºï½ü</a>
2087
2088 \\|\\)</td>
2089 </tr>
2090 </table>
2091 </td>
2092 </tr>
2093 <!-- [^ ]+ : start -->
2094 <tr>
2095 <td bgcolor=\"#ffffff\">
2096 <table BORDER=0 CELLSPACING=0 CELLPADDING=[35] WIDTH=410>
2097 <tr>
2098 <td CLASS=h12>
2099 \\(.+\\)
2100 </td></tr></table>")
2101
2102 (defun mixi-topic-comment-list-page (topic)
2103   (concat "/view_bbs.pl?page=all"
2104           "&id=" (mixi-topic-id topic)
2105           "&comm_id=" (mixi-community-id (mixi-topic-community topic))))
2106
2107 ;; FIXME: Split regexp to time, owner(id and nick) and contents.
2108 (defconst mixi-topic-comment-list-regexp
2109   "<tr valign=\"top\">
2110 <td rowspan=\"2\" width=\"110\" bgcolor=\"#f2ddb7\" align=\"center\" nowrap>
2111 \\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü<br>
2112 \\([0-9]+\\):\\([0-9]+\\)<br>
2113 \\(<input type=\"checkbox\" name=\"comment_id\" value=\".+\">
2114 \\|\\)</td>
2115 <td bgcolor=\"#fdf9f2\">&nbsp;<font color=\"#f8a448\">
2116 <b>[^<]+</b>:</font>&nbsp;
2117 \\(
2118 \\|\\) *<a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.*\\)</a>
2119
2120 ?\\(
2121
2122 \\|<font color=\"#f2ddb7\">|&nbsp;</font><a href=\"delete_bbs_comment\\.pl\\?id=[0-9]+&comm_id=[0-9]+&comment_id=[0-9]+\">ºï½ü</a>
2123 \\|\\)</td>
2124 </tr>
2125 <tr>
2126 <td bgcolor=\"#ffffff\" align=\"center\">
2127 <table border=\"0\" cellspacing=\"0\" cellpadding=\"5\" width=\"500\">
2128 <tr>
2129 <td class=\"h120\">
2130
2131 \\(.+\\)
2132 </td>
2133 </tr>
2134 </table>
2135 </td>
2136 </tr>")
2137
2138 (defun mixi-event-comment-list-page (event)
2139   (concat "/view_event.pl?page=all"
2140           "&id=" (mixi-event-id event)
2141           "&comm_id=" (mixi-community-id (mixi-event-community event))))
2142
2143 ;; FIXME: Split regexp to time, owner(id and nick) and contents.
2144 (defconst mixi-event-comment-list-regexp
2145   "<tr>
2146 <td ROWSPAN=2 ALIGN=center BGCOLOR=#F2DDB7 WIDTH=110>
2147 \\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü<br>
2148 \\([0-9]+\\):\\([0-9]+\\)<br>
2149 \\(</td>\\)
2150 \\(<td BGCOLOR=#FDF9F2>\\)
2151 <font COLOR=#F8A448><b>[^<]+</b> :</font>
2152 <a HREF=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.*\\)</a>
2153
2154 \\(<font COLOR=#F2DDB7>|</font>
2155 <a href=\"delete_bbs_comment\\.pl\\?id=[0-9]+&comm_id=[0-9]+&comment_id=[0-9]+&type=event\">ºï½ü</a>
2156
2157 \\|\\)</td>
2158 </tr>
2159 <tr>
2160 <td ALIGN=center BGCOLOR=#FFFFFF>
2161 <table BORDER=0 CELLSPACING=0 CELLPADDING=5 WIDTH=500>
2162 <tr><td CLASS=h120>\\(.+\\)</td></tr>
2163 </table>
2164 </td>
2165 </tr>")
2166
2167 (defun mixi-get-comments (parent &optional range)
2168   "Get comments of PARENT."
2169   (unless (mixi-object-p parent)
2170     (signal 'wrong-type-argument (list 'mixi-object-p parent)))
2171   (let* ((name (mixi-object-name parent))
2172          (list-page (intern (concat mixi-object-prefix name
2173                                     "-comment-list-page")))
2174          (regexp (eval (intern (concat mixi-object-prefix name
2175                                        "-comment-list-regexp")))))
2176     (let ((items (mixi-get-matched-items
2177                   (funcall list-page parent) regexp)))
2178       (let (list)
2179         (catch 'stop
2180           (mapc (lambda (item)
2181                   (when (and (numberp range)
2182                              (>= (length list) range))
2183                     (throw 'stop nil))
2184                   (setq list (cons item list)))
2185                 (reverse items)))
2186         (setq items (reverse list)))
2187       (mapcar (lambda (item)
2188                 (mixi-make-comment parent (mixi-make-friend
2189                                            (nth 7 item) (nth 8 item))
2190                                    (encode-time
2191                                     0
2192                                     (string-to-number (nth 4 item))
2193                                     (string-to-number (nth 3 item))
2194                                     (string-to-number (nth 2 item))
2195                                     (string-to-number (nth 1 item))
2196                                     (string-to-number (nth 0 item)))
2197                                    (nth 10 item)))
2198               items))))
2199
2200 (defmacro mixi-new-comment-list-page ()
2201   `(concat "/new_comment.pl?page=%d"))
2202
2203 (defconst mixi-new-comment-list-regexp
2204   "<a href=\"view_diary\\.pl\\?id=\\([0-9]+\\)&owner_id=\\([0-9]+\\)&comment_count=[0-9]+\" class=\"new_link\">")
2205
2206 (defun mixi-get-new-comments (&optional range)
2207   "Get new comments."
2208   (let ((items (mixi-get-matched-items (mixi-new-comment-list-page)
2209                                        mixi-new-comment-list-regexp
2210                                        range)))
2211     (mapcar (lambda (item)
2212               (mixi-make-diary (mixi-make-friend (nth 1 item)) (nth 0 item)))
2213             items)))
2214
2215 (defun mixi-post-diary-comment-page (diary)
2216   (concat "/add_comment.pl?&diary_id=" (mixi-diary-id diary)))
2217
2218 (defun mixi-post-topic-comment-page (topic)
2219   (concat "/add_bbs_comment.pl?id=" (mixi-topic-id topic)
2220           "&comm_id=" (mixi-community-id (mixi-topic-community topic))))
2221
2222 (defun mixi-post-event-comment-page (event)
2223   (concat "/add_event_comment.pl?id=" (mixi-event-id event)
2224           "&comm_id=" (mixi-community-id (mixi-event-community event))))
2225
2226 ;; FIXME: Support photos.
2227 (defun mixi-post-comment (parent content)
2228   "Post a comment to PARENT."
2229   (unless (mixi-object-p parent)
2230     (signal 'wrong-type-argument (list 'mixi-object-p parent)))
2231   (unless (stringp content)
2232     (signal 'wrong-type-argument (list 'stringp content)))
2233   (let* ((name (mixi-object-name parent))
2234          (page (intern (concat mixi-object-prefix "post-" name
2235                                "-comment-page")))
2236          fields post-key)
2237     (if (mixi-diary-p parent)
2238         (setq fields
2239               `(("owner_id" . ,(mixi-friend-id (mixi-diary-owner parent)))
2240                 ("comment_body" . ,content)))
2241       (setq fields `(("comment" . ,content))))
2242     (with-mixi-post-form (funcall page parent) fields
2243       (if (string-match mixi-post-key-regexp buffer)
2244           (setq post-key (match-string 1 buffer))
2245         (mixi-post-error 'cannot-find-key parent)))
2246     (if (mixi-diary-p parent)
2247         (setq fields
2248               `(("post_key" . ,post-key)
2249                 ("owner_id" . ,(mixi-friend-id (mixi-diary-owner parent)))
2250                 ("comment_body" . ,content)
2251                 ("submit" . "confirm")))
2252       (setq fields `(("post_key" . ,post-key)
2253                      ("comment" . ,content)
2254                      ("submit" . "confirm"))))
2255     (with-mixi-post-form (funcall page parent) fields
2256       (unless (string-match mixi-post-succeed-regexp buffer)
2257         (mixi-post-error 'cannot-find-succeed parent)))))
2258
2259 ;; Message object.
2260 (defconst mixi-message-box-list '(inbox outbox savebox thrash)) ; thrash?
2261
2262 (defmacro mixi-message-box-p (box)
2263   `(when (memq ,box mixi-message-box-list)
2264      t))
2265
2266 (defun mixi-message-box-name (box)
2267   "Return the name of BOX."
2268   (unless (mixi-message-box-p box)
2269     (signal 'wrong-type-argument (list 'mixi-message-box-p box)))
2270   (symbol-name box))
2271
2272 (defvar mixi-message-cache (make-hash-table :test 'equal))
2273 (defun mixi-make-message (id box &optional owner title time content)
2274   "Return a message object."
2275   (mixi-make-cache (list id box)
2276                    (cons 'mixi-message (vector nil id box owner title time
2277                                                content))
2278                    mixi-message-cache))
2279
2280 (defconst mixi-message-url-regexp
2281   "/view_message\\.pl\\?id=\\([a-z0-9]+\\)&box=\\([a-z]+\\)")
2282
2283 (defun mixi-make-message-from-url (url)
2284   "Return a message object from URL."
2285   (when (string-match mixi-message-url-regexp url)
2286     (let ((id (match-string 1 url))
2287           (box (match-string 2 url)))
2288       (mixi-make-message id box))))
2289
2290 (defmacro mixi-message-p (message)
2291   `(eq (mixi-object-class ,message) 'mixi-message))
2292
2293 (defmacro mixi-message-page (message)
2294   `(concat "/view_message.pl?id=" (mixi-message-id ,message)
2295            "&box=" (mixi-message-box ,message)))
2296
2297 (defconst mixi-message-owner-regexp
2298   "<font COLOR=#996600>\\(º¹½Ð¿Í\\|°¸&nbsp;Àè\\)</font>&nbsp;:&nbsp;<a HREF=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.*\\)\\(</a>\\|</td>\\)")
2299 (defconst mixi-message-title-regexp
2300 "<font COLOR=#996600>·ï\\(¡¡\\|&nbsp;\\)̾</font>&nbsp;:&nbsp;\\(.+\\)\n?</td>")
2301 (defconst mixi-message-time-regexp
2302 "<font COLOR=#996600>Æü\\(¡¡\\|&nbsp;\\)ÉÕ</font>&nbsp;:&nbsp;\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü \\([0-9]+\\)»þ\\([0-9]+\\)ʬ&nbsp;&nbsp;")
2303 (defconst mixi-message-content-regexp
2304   "<tr><td CLASS=h120>\\(.+\\)</td></tr>")
2305
2306 (defun mixi-realize-message (message)
2307   "Realize a MESSAGE."
2308   (unless (mixi-object-realized-p message)
2309     (with-mixi-retrieve (mixi-message-page message)
2310       (if (string-match mixi-message-owner-regexp buffer)
2311           (mixi-message-set-owner message
2312                                   (mixi-make-friend (match-string 2 buffer)
2313                                                     (match-string 3 buffer)))
2314         (mixi-realization-error 'cannot-find-owner message))
2315       (if (string-match mixi-message-title-regexp buffer)
2316           (mixi-message-set-title message (match-string 2 buffer))
2317         (mixi-realization-error 'cannot-find-title message))
2318       (if (string-match mixi-message-time-regexp buffer)
2319           (mixi-message-set-time
2320            message (encode-time 0 (string-to-number (match-string 6 buffer))
2321                                 (string-to-number (match-string 5 buffer))
2322                                 (string-to-number (match-string 4 buffer))
2323                                 (string-to-number (match-string 3 buffer))
2324                                 (string-to-number (match-string 2 buffer))))
2325         (mixi-realization-error 'cannot-find-time message))
2326       (if (string-match mixi-message-content-regexp buffer)
2327           (mixi-message-set-content message (match-string 1 buffer))
2328         (mixi-realization-error 'cannot-find-content message)))
2329     (mixi-object-touch message)))
2330
2331 (defun mixi-message-id (message)
2332   "Return the id of MESSAGE."
2333   (unless (mixi-message-p message)
2334     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2335   (aref (cdr message) 1))
2336
2337 (defun mixi-message-box (message)
2338   "Return the box of MESSAGE."
2339   (unless (mixi-message-p message)
2340     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2341   (aref (cdr message) 2))
2342
2343 (defun mixi-message-owner (message)
2344   "Return the owner of MESSAGE."
2345   (unless (mixi-message-p message)
2346     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2347   (mixi-realize-message message)
2348   (aref (cdr message) 3))
2349
2350 (defun mixi-message-title (message)
2351   "Return the title of MESSAGE."
2352   (unless (mixi-message-p message)
2353     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2354   (mixi-realize-message message)
2355   (aref (cdr message) 4))
2356
2357 (defun mixi-message-time (message)
2358   "Return the date of MESSAGE."
2359   (unless (mixi-message-p message)
2360     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2361   (mixi-realize-message message)
2362   (aref (cdr message) 5))
2363
2364 (defun mixi-message-content (message)
2365   "Return the content of MESSAGE."
2366   (unless (mixi-message-p message)
2367     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2368   (mixi-realize-message message)
2369   (aref (cdr message) 6))
2370
2371 (defun mixi-message-set-owner (message owner)
2372   "Set the owner of MESSAGE."
2373   (unless (mixi-message-p message)
2374     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2375   (aset (cdr message) 3 owner))
2376
2377 (defun mixi-message-set-title (message title)
2378   "Set the title of MESSAGE."
2379   (unless (mixi-message-p message)
2380     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2381   (aset (cdr message) 4 title))
2382
2383 (defun mixi-message-set-time (message time)
2384   "Set the date of MESSAGE."
2385   (unless (mixi-message-p message)
2386     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2387   (aset (cdr message) 5 time))
2388
2389 (defun mixi-message-set-content (message content)
2390   "Set the content of MESSAGE."
2391   (unless (mixi-message-p message)
2392     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2393   (aset (cdr message) 6 content))
2394
2395 (defmacro mixi-message-list-page (&optional box)
2396   `(concat "/list_message.pl?page=%d"
2397            (when ,box (concat "&box=" ,box))))
2398
2399 (defconst mixi-message-list-regexp
2400   "<td><a HREF=\"view_message\\.pl\\?id=\\(.+\\)&box=\\(.+\\)\">")
2401
2402 (defun mixi-get-messages (&rest box-or-range)
2403   "Get messages of BOX."
2404   (when (> (length box-or-range) 2)
2405     (signal 'wrong-number-of-arguments
2406             (list 'mixi-get-messages (length box-or-range))))
2407   (let ((box (nth 0 box-or-range))
2408         (range (nth 1 box-or-range)))
2409     (when (or (not (mixi-message-box-p box))
2410               (mixi-message-box-p range))
2411       (setq box (nth 1 box-or-range))
2412       (setq range (nth 0 box-or-range)))
2413     (let ((items (mixi-get-matched-items
2414                   (mixi-message-list-page
2415                    (when box (mixi-message-box-name box)))
2416                   mixi-message-list-regexp
2417                   range)))
2418       (mapcar (lambda (item)
2419                 (mixi-make-message (nth 0 item) (nth 1 item)))
2420               items))))
2421
2422 (defmacro mixi-post-message-page (friend)
2423   `(concat "/send_message.pl?id=" (mixi-friend-id friend)))
2424
2425 (defconst mixi-post-message-key-regexp
2426   "<input name=post_key type=hidden value=\\([a-z0-9]+\\)>")
2427
2428 (defconst mixi-post-message-succeed-regexp
2429   "<b>Á÷¿®´°Î»</b>¤·¤Þ¤·¤¿¡£")
2430
2431 (defun mixi-post-message (friend title content)
2432   "Post a message to FRIEND."
2433   (unless (mixi-friend-p friend)
2434     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
2435   (unless (stringp title)
2436     (signal 'wrong-type-argument (list 'stringp title)))
2437   (unless (stringp content)
2438     (signal 'wrong-type-argument (list 'stringp content)))
2439   (let ((fields `(("subject" . ,title)
2440                   ("body" . ,content)
2441                   ("submit" . "main")))
2442         post-key)
2443     (with-mixi-post-form (mixi-post-message-page friend) fields
2444       (if (string-match mixi-post-message-key-regexp buffer)
2445           (setq post-key (match-string 1 buffer))
2446         (mixi-post-error 'cannot-find-key friend)))
2447     (setq fields `(("post_key" . ,post-key)
2448                    ("subject" . ,title)
2449                    ("body" . ,content)
2450                    ("yes" . "¡¡Á÷¡¡¿®¡¡")
2451                    ("submit" . "confirm")))
2452     (with-mixi-post-form (mixi-post-message-page friend) fields
2453       (unless (string-match mixi-post-message-succeed-regexp buffer)
2454         (mixi-post-error 'cannot-find-succeed friend)))))
2455
2456 ;; Introduction object.
2457 (defun mixi-make-introduction (parent owner content)
2458   "Return a introduction object."
2459   (cons 'mixi-introduction (vector parent owner content)))
2460
2461 (defmacro mixi-introduction-p (introduction)
2462   `(eq (mixi-object-class ,introduction) 'mixi-introduction))
2463
2464 (defun mixi-introduction-parent (introduction)
2465   "Return the parent of INTRODUCTION."
2466   (unless (mixi-introduction-p introduction)
2467     (signal 'wrong-type-argument (list 'mixi-introduction-p introduction)))
2468   (aref (cdr introduction) 0))
2469
2470 (defun mixi-introduction-owner (introduction)
2471   "Return the owner of INTRODUCTION."
2472   (unless (mixi-introduction-p introduction)
2473     (signal 'wrong-type-argument (list 'mixi-introduction-p introduction)))
2474   (aref (cdr introduction) 1))
2475
2476 (defun mixi-introduction-content (introduction)
2477   "Return the content of INTRODUCTION."
2478   (unless (mixi-introduction-p introduction)
2479     (signal 'wrong-type-argument (list 'mixi-introduction-p introduction)))
2480   (aref (cdr introduction) 3))
2481
2482 (defmacro mixi-introduction-list-page (&optional friend)
2483   `(concat "/show_intro.pl?page=%d"
2484            (when ,friend (concat "&id=" (mixi-friend-id ,friend)))))
2485
2486 (defconst mixi-introduction-list-regexp
2487   "<tr bgcolor=#FFFFFF>
2488 <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>
2489 \\(.*\\)</td></a>
2490
2491 <td WIDTH=480>
2492 \\(´Ø·¸¡§.+<br>
2493
2494
2495 \\(\\(.\\|\n<br>\\)+\\)\\|
2496 \\(\\(.\\|\n<br>\\)+\\)\\)
2497
2498
2499
2500
2501 </td>
2502 </tr>")
2503 (defconst mixi-my-introduction-list-regexp
2504   "<tr bgcolor=#FFFFFF>
2505 <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>
2506 \\(.*\\)</td></a>
2507
2508
2509 <td WIDTH=480>
2510 \\(´Ø·¸¡§.+<br>
2511
2512
2513 \\(\\(.\\|\n<br>\\)+\\)\\|
2514 \\(\\(.\\|\n<br>\\)+\\)\\)
2515
2516
2517 <br>
2518 <a href=\"edit_intro\\.pl\\?id=\\1&type=edit\">¤³¤Îͧ¿Í¤ò¾Ò²ð¤¹¤ë</a>
2519
2520
2521 <BR>
2522 <a href=\"delete_intro\\.pl\\?id=\\1\">ºï½ü</a>
2523
2524 </td>
2525 </tr>")
2526
2527 (defun mixi-get-introductions (&rest friend-or-range)
2528   "Get introductions of FRIEND."
2529   (when (> (length friend-or-range) 2)
2530     (signal 'wrong-number-of-arguments
2531             (list 'mixi-get-introduction (length friend-or-range))))
2532   (let ((friend (nth 0 friend-or-range))
2533         (range (nth 1 friend-or-range)))
2534     (when (or (not (mixi-friend-p friend)) (mixi-friend-p range))
2535       (setq friend (nth 1 friend-or-range))
2536       (setq range (nth 0 friend-or-range)))
2537     (unless (or (null friend) (mixi-friend-p friend))
2538       (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
2539     (let* ((regexp (if friend mixi-introduction-list-regexp
2540                      mixi-my-introduction-list-regexp))
2541            (items (mixi-get-matched-items (mixi-introduction-list-page friend)
2542                                           regexp
2543                                           range)))
2544       (mapcar (lambda (item)
2545                 (mixi-make-introduction (or friend (mixi-make-me))
2546                                         (mixi-make-friend (nth 0 item)
2547                                                           (nth 1 item))
2548                                         (nth 2 item)))
2549               items))))
2550
2551 (provide 'mixi)
2552
2553 ;;; mixi.el ends here