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