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