(mixi-get-friends): Follow the change of mixi.
[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.192 $")
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 (defun mixi-my-friend-list-page (&optional dummy)
1062   (concat "/list_friend_simple.pl?page=%d"))
1063
1064 (defun mixi-friend-list-page (friend)
1065   (concat "/list_friend.pl?page=%d&id=" (mixi-friend-id friend)))
1066
1067 (defconst mixi-friend-list-id-regexp
1068   "<a href=\"?show_friend\\.pl\\?id=\\([0-9]+\\)\"?")
1069 (defconst mixi-my-friend-list-nick-regexp
1070   "<p>\\(.+\\)([0-9]+)</p>")
1071 (defconst mixi-friend-list-nick-regexp
1072   "<span>\\(.+\\)¤µ¤ó([0-9]+)</span>")
1073
1074 ;;;###autoload
1075 (defun mixi-get-friends (&rest friend-or-range)
1076   "Get friends of FRIEND."
1077   (when (> (length friend-or-range) 2)
1078     (signal 'wrong-number-of-arguments (list 'mixi-get-friends
1079                                              (length friend-or-range))))
1080   (let ((friend (nth 0 friend-or-range))
1081         (range (nth 1 friend-or-range)))
1082     (when (or (not (mixi-friend-p friend)) (mixi-friend-p range))
1083       (setq friend (nth 1 friend-or-range))
1084       (setq range (nth 0 friend-or-range)))
1085     (unless (or (null friend) (mixi-friend-p friend))
1086       (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
1087     (let (list-page list-nick-regexp)
1088       (if (or (null friend) (equal friend mixi-me))
1089           (setq list-page 'mixi-my-friend-list-page
1090                 list-nick-regexp mixi-my-friend-list-nick-regexp)
1091           (setq list-page 'mixi-friend-list-page
1092                 list-nick-regexp mixi-friend-list-nick-regexp))
1093       (let ((ids (mixi-get-matched-items (funcall list-page friend)
1094                                          mixi-friend-list-id-regexp
1095                                          range))
1096             (nicks (mixi-get-matched-items (funcall list-page friend)
1097                                            list-nick-regexp
1098                                            range)))
1099         (let ((index 0)
1100               ret)
1101           (while (< index (length ids))
1102             (setq ret (cons (mixi-make-friend (nth 0 (nth index ids))
1103                                               (nth 0 (nth index nicks))) ret))
1104             (incf index))
1105           (reverse ret))))))
1106
1107 ;; Favorite.
1108 (defmacro mixi-favorite-list-page ()
1109   `(concat "/list_bookmark.pl?page=%d"))
1110
1111 (defconst mixi-favorite-list-regexp
1112   "<td bgcolor=\"#FDF9F2\"><font color=\"#996600\">̾Á°</font></td>
1113 <td colspan=\"2\" bgcolor=\"#FFFFFF\"><a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.*\\)</a></td>")
1114
1115 ;;;###autoload
1116 (defun mixi-get-favorites (&optional range)
1117   "Get favorites."
1118   (let ((items (mixi-get-matched-items (mixi-favorite-list-page)
1119                                        mixi-favorite-list-regexp
1120                                        range)))
1121     (mapcar (lambda (item)
1122               (mixi-make-friend (nth 0 item) (nth 1 item)))
1123             items)))
1124
1125 ;; Log object.
1126 (defun mixi-make-log (friend time)
1127   "Return a log object."
1128   (cons 'mixi-log (vector friend time)))
1129
1130 (defmacro mixi-log-p (log)
1131   `(eq (mixi-object-class ,log) 'mixi-log))
1132
1133 (defun mixi-log-friend (log)
1134   "Return the friend of LOG."
1135   (unless (mixi-log-p log)
1136     (signal 'wrong-type-argument (list 'mixi-log-p log)))
1137   (aref (cdr log) 0))
1138
1139 (defun mixi-log-time (log)
1140   "Return the time of LOG."
1141   (unless (mixi-log-p log)
1142     (signal 'wrong-type-argument (list 'mixi-log-p log)))
1143   (aref (cdr log) 1))
1144
1145 (defmacro mixi-log-list-page ()
1146   `(concat "/show_log.pl"))
1147
1148 (defconst mixi-log-list-regexp
1149   "\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü \\([0-9]+\\):\\([0-9]+\\) <a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.*?\\)</a>")
1150
1151 ;;;###autoload
1152 (defun mixi-get-logs (&optional range)
1153   "Get logs."
1154   (let ((items (mixi-get-matched-items (mixi-log-list-page)
1155                                        mixi-log-list-regexp
1156                                        range)))
1157     (mapcar (lambda (item)
1158               (mixi-make-log (mixi-make-friend (nth 5 item) (nth 6 item))
1159                              (encode-time 0
1160                                           (string-to-number (nth 4 item))
1161                                           (string-to-number (nth 3 item))
1162                                           (string-to-number (nth 2 item))
1163                                           (string-to-number (nth 1 item))
1164                                           (string-to-number (nth 0 item)))))
1165             items)))
1166
1167 ;; Recommended friend.
1168 (defmacro mixi-recommended-friend-list-page ()
1169   `(concat "http://indies.mixi.jp/recommend.pl"))
1170
1171 (defconst mixi-recommended-friend-list-regexp
1172   "<div class=\"iconListImage\"><a href=\"http://mixi\\.jp/show_friend\\.pl\\?id=\\([0-9]+\\)\" [^>]+>.+</a></div><span>\\(.+?\\)¤µ¤ó([0-9]+)</span>")
1173
1174 ;;;###autoload
1175 (defun mixi-get-recommended-friends (&optional range)
1176   "Get recommended friends."
1177   (let ((items (mixi-get-matched-items (mixi-recommended-friend-list-page)
1178                                        mixi-recommended-friend-list-regexp
1179                                        range)))
1180     (mapcar (lambda (item)
1181               (mixi-make-friend (nth 0 item) (nth 1 item)))
1182             items)))
1183
1184 ;; Diary object.
1185 (defvar mixi-diary-cache (make-hash-table :test 'equal))
1186 (defun mixi-make-diary (owner id &optional comment-count time title content)
1187   "Return a diary object."
1188   (let ((owner (or owner (mixi-make-me))))
1189     (mixi-make-cache (list (mixi-friend-id owner) id)
1190                      (cons 'mixi-diary (vector nil owner id comment-count time
1191                                                title content))
1192                      mixi-diary-cache)))
1193
1194 (defconst mixi-diary-url-regexp
1195   "/view_diary\\.pl\\?id=\\([0-9]+\\)&owner_id=\\([0-9]+\\)\\(&comment_count=\\([0-9]+\\)\\)?")
1196
1197 (defun mixi-make-diary-from-url (url)
1198   "Return a diary object from URL."
1199   (when (string-match mixi-diary-url-regexp url)
1200     (let ((id (match-string 1 url))
1201           (owner-id (match-string 2 url))
1202           (comment-count (match-string 4 url)))
1203       (mixi-make-diary (mixi-make-friend owner-id) id comment-count))))
1204
1205 (defmacro mixi-diary-p (diary)
1206   `(eq (mixi-object-class ,diary) 'mixi-diary))
1207
1208 (defmacro mixi-diary-page (diary)
1209   `(concat "/view_diary.pl?id=" (mixi-diary-id ,diary)
1210            "&owner_id=" (mixi-friend-id (mixi-diary-owner ,diary))))
1211
1212 (defconst mixi-diary-closed-regexp
1213   "<td>ͧ¿Í\\(¤Îͧ¿Í\\)?¤Þ¤Ç¸ø³«¤Î¤¿¤áÆɤळ¤È¤¬½ÐÍè¤Þ¤»¤ó¡£</td>")
1214 (defconst mixi-diary-owner-nick-regexp
1215   "<div class=\"diaryTitle\\(Friend\\)? clearfix\">
1216 <h2>\\(.+?\\)\\(¤µ¤ó\\)?¤ÎÆüµ­</h2>")
1217 (defconst mixi-diary-title-regexp
1218   "<div class=\"listDiaryTitle\">
1219 <dl class=\"clearfix\">
1220 <dt>\\([^<\n]+\\)\\(<span>\\)?")
1221 (defconst mixi-diary-time-regexp
1222   "<dd>\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü\\([0-9]+\\):\\([0-9]+\\)</dd>")
1223 (defconst mixi-diary-content-regexp
1224   "<div class=\"txtconfirmArea\">
1225 \\(\\(.\\|\r?\n\\)*?\\)
1226 <!--/viewDiaryBox--></div>")
1227
1228 (defun mixi-realize-diary (diary &optional page)
1229   "Realize a DIARY."
1230   ;; FIXME: Check an expiration of cache?
1231   (unless (mixi-object-realized-p diary)
1232     (with-mixi-retrieve (or page (mixi-diary-page diary))
1233       (let ((case-fold-search t))
1234         (unless (re-search-forward mixi-diary-closed-regexp nil t)
1235           (if (re-search-forward mixi-diary-owner-nick-regexp nil t)
1236               (mixi-friend-set-nick (mixi-diary-owner diary)
1237                                     (match-string 2))
1238             (mixi-realization-error 'cannot-find-owner-nick diary))
1239           (if (re-search-forward mixi-diary-title-regexp nil t)
1240               (mixi-diary-set-title diary (match-string 1))
1241             (mixi-realization-error 'cannot-find-title diary))
1242           (if (re-search-forward mixi-diary-time-regexp nil t)
1243               (mixi-diary-set-time
1244                diary (encode-time 0 (string-to-number (match-string 5))
1245                                   (string-to-number (match-string 4))
1246                                   (string-to-number (match-string 3))
1247                                   (string-to-number (match-string 2))
1248                                   (string-to-number (match-string 1))))
1249             (mixi-realization-error 'cannot-find-time diary))
1250           (if (re-search-forward mixi-diary-content-regexp nil t)
1251               (mixi-diary-set-content diary (match-string 1))
1252             (mixi-realization-error 'cannot-find-content diary)))))
1253     (mixi-object-touch diary)))
1254
1255 (defun mixi-diary-owner (diary)
1256   "Return the owner of DIARY."
1257   (unless (mixi-diary-p diary)
1258     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1259   (aref (cdr diary) 1))
1260
1261 (defun mixi-diary-id (diary)
1262   "Return the id of DIARY."
1263   (unless (mixi-diary-p diary)
1264     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1265   (aref (cdr diary) 2))
1266
1267 (defun mixi-diary-comment-count (diary)
1268   "Return the comment-count of DIARY."
1269   (unless (mixi-diary-p diary)
1270     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1271   (aref (cdr diary) 3))
1272
1273 (defun mixi-diary-time (diary)
1274   "Return the time of DIARY."
1275   (unless (mixi-diary-p diary)
1276     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1277   (unless (aref (cdr diary) 3)
1278     (mixi-realize-diary diary))
1279   (aref (cdr diary) 4))
1280
1281 (defun mixi-diary-title (diary)
1282   "Return the title of DIARY."
1283   (unless (mixi-diary-p diary)
1284     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1285   (unless (aref (cdr diary) 4)
1286     (mixi-realize-diary diary))
1287   (aref (cdr diary) 5))
1288
1289 (defun mixi-diary-content (diary)
1290   "Return the content of DIARY."
1291   (unless (mixi-diary-p diary)
1292     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1293   (mixi-realize-diary diary)
1294   (aref (cdr diary) 6))
1295
1296 (defun mixi-diary-set-comment-count (diary comment-count)
1297   "Set the comment-count of DIARY."
1298   (unless (mixi-diary-p diary)
1299     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1300   (aset (cdr diary) 3 comment-count))
1301
1302 (defun mixi-diary-set-time (diary time)
1303   "Set the time of DIARY."
1304   (unless (mixi-diary-p diary)
1305     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1306   (aset (cdr diary) 4 time))
1307
1308 (defun mixi-diary-set-title (diary title)
1309   "Set the title of DIARY."
1310   (unless (mixi-diary-p diary)
1311     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1312   (aset (cdr diary) 5 title))
1313
1314 (defun mixi-diary-set-content (diary content)
1315   "Set the content of DIARY."
1316   (unless (mixi-diary-p diary)
1317     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1318   (aset (cdr diary) 6 content))
1319
1320 (defmacro mixi-diary-list-page (&optional friend)
1321   `(concat "/list_diary.pl?page=%d"
1322            (when ,friend (concat "&id=" (mixi-friend-id ,friend)))))
1323
1324 (defconst mixi-diary-list-regexp
1325   "<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>
1326 <dd>\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü\n?\\([0-9]+\\):\\([0-9]+\\)</dd>
1327 </dl>")
1328
1329 ;;;###autoload
1330 (defun mixi-get-diaries (&rest friend-or-range)
1331   "Get diaries of FRIEND."
1332   (when (> (length friend-or-range) 2)
1333     (signal 'wrong-number-of-arguments
1334             (list 'mixi-get-diaries (length friend-or-range))))
1335   (let ((friend (nth 0 friend-or-range))
1336         (range (nth 1 friend-or-range)))
1337     (when (or (not (mixi-friend-p friend)) (mixi-friend-p range))
1338       (setq friend (nth 1 friend-or-range))
1339       (setq range (nth 0 friend-or-range)))
1340     (unless (or (null friend) (mixi-friend-p friend))
1341       (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
1342     (let ((items (mixi-get-matched-items (mixi-diary-list-page friend)
1343                                          mixi-diary-list-regexp
1344                                          range)))
1345       (mapcar (lambda (item)
1346                 (mixi-make-diary friend (nth 1 item) nil
1347                                  (encode-time
1348                                   0 (string-to-number (nth 8 item))
1349                                   (string-to-number (nth 7 item))
1350                                   (string-to-number (nth 6 item))
1351                                   (string-to-number (nth 5 item))
1352                                   (string-to-number (nth 4 item)))
1353                                   (nth 2 item)))
1354               items))))
1355
1356 (defmacro mixi-new-diary-list-page ()
1357   `(concat "/new_friend_diary.pl?page=%d"))
1358
1359 (defconst mixi-new-diary-list-regexp
1360   "<dt>\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü&nbsp;\\([0-9]+\\):\\([0-9]+\\)</dt>
1361 <dd><a href=\"view_diary\\.pl\\?id=\\([0-9]+\\)&owner_id=\\([0-9]+\\)\">\\(.+\\)</a> (\\(.*\\))<div ")
1362 (defconst mixi-new-diary-list-title-regexp
1363   "\\(.+\\) (\\([0-9]+\\))")
1364
1365 ;;;###autoload
1366 (defun mixi-get-new-diaries (&optional range)
1367   "Get new diaries."
1368   (let ((items (mixi-get-matched-items (mixi-new-diary-list-page)
1369                                        mixi-new-diary-list-regexp
1370                                        range)))
1371     (delq nil
1372           (mapcar (lambda (item)
1373                     (let ((title (nth 7 item))
1374                           count)
1375                       (when (string-match mixi-new-diary-list-title-regexp
1376                                           title)
1377                         (setq count (string-to-number (match-string 2 title)))
1378                         (setq title (match-string 1 title)))
1379                       (let* ((diary (mixi-make-diary
1380                                      (mixi-make-friend (nth 6 item)
1381                                                        (nth 8 item))
1382                                      (nth 5 item)))
1383                              (comment-count (mixi-diary-comment-count diary)))
1384                         (mixi-diary-set-time diary
1385                                              (encode-time
1386                                               0 (string-to-number (nth 4 item))
1387                                               (string-to-number (nth 3 item))
1388                                               (string-to-number (nth 2 item))
1389                                               (string-to-number (nth 1 item))
1390                                               (string-to-number (nth 0 item))))
1391                         (mixi-diary-set-title diary title)
1392                         (when (or (null comment-count)
1393                                   (< comment-count count))
1394                           (mixi-diary-set-comment-count diary count)
1395                           diary))))
1396                   items))))
1397
1398 (defmacro mixi-search-diary-list-page (keyword)
1399   `(concat "/search_diary.pl?page=%d&submit=search&keyword="
1400              (mixi-url-encode-and-quote-percent-string ,keyword)))
1401
1402 (defconst mixi-search-diary-list-regexp
1403  "<li>
1404 <div class=\"listIcon\"><a href=\"view_diary\\.pl\\?id=\\([0-9]+\\)&owner_id=\\([0-9]+\\)\"><img src=\".*\" alt=\".*\" /></a>
1405 <span class=\"name\"><a href=\"view_diary\\.pl\\?id=[0-9]+&owner_id=[0-9]+\">\\(.*\\)</a></span></div>
1406 <div class=\"listContents\">
1407 <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>
1408 <p class=\"description\">\\(.*\\)
1409 </div>
1410 </li>")
1411
1412 ;;;###autoload
1413 (defun mixi-search-diaries (keyword &optional range)
1414   (let ((items (mixi-get-matched-items (mixi-search-diary-list-page keyword)
1415                                        mixi-search-diary-list-regexp
1416                                        range))
1417         (year (nth 5 (decode-time (current-time))))
1418         (month (nth 4 (decode-time (current-time)))))
1419     (mapcar (lambda (item)
1420                 (let ((month-of-item (string-to-number (nth 4 item))))
1421                   (when (> month-of-item month)
1422                     (decf year))
1423                   (setq month month-of-item)
1424                   (mixi-make-diary (mixi-make-friend (nth 1 item) (nth 2 item))
1425                                    (nth 0 item)
1426                                    nil
1427                                    (encode-time
1428                                     0 (string-to-number (nth 7 item))
1429                                     (string-to-number (nth 6 item))
1430                                     (string-to-number (nth 5 item))
1431                                     month year)
1432                                    (nth 3 item)
1433                                    (nth 8 item))))
1434             items)))
1435
1436 (defmacro mixi-post-diary-page ()
1437   `(concat "/add_diary.pl"))
1438
1439 (defconst mixi-post-key-regexp
1440   "<input type=\"?hidden\"? name=\"?post_key\"? +value=\"\\([a-z0-9]+\\)\"")
1441 (defconst mixi-post-succeed-regexp
1442   "<p\\(\\| class=\"messageAlert\"\\)>\\(ºîÀ®\\|½ñ¤­¹þ¤ß\\)¤¬´°Î»¤·¤Þ¤·¤¿¡£È¿±Ç¤Ë»þ´Ö¤¬¤«¤«¤ë¤³¤È¤¬¤¢¤ê¤Þ¤¹¡£</p>")
1443
1444 ;; FIXME: Support photos.
1445 ;;;###autoload
1446 (defun mixi-post-diary (title content)
1447   "Post a diary."
1448   (unless (stringp title)
1449     (signal 'wrong-type-argument (list 'stringp title)))
1450   (unless (stringp content)
1451     (signal 'wrong-type-argument (list 'stringp content)))
1452   (let ((fields `(("id" . ,(mixi-friend-id (mixi-make-me)))
1453                   ("diary_title" . ,title)
1454                   ("diary_body" . ,content)
1455                   ("submit" . "main")))
1456         post-key)
1457     (with-mixi-post-form (mixi-post-diary-page) fields
1458       (if (re-search-forward mixi-post-key-regexp nil t)
1459           (setq post-key (match-string 1))
1460         (mixi-post-error 'cannot-find-key)))
1461     (setq fields `(("post_key" . ,post-key)
1462                    ("id" . ,(mixi-friend-id (mixi-make-me)))
1463                    ("diary_title" . ,title)
1464                    ("diary_body" . ,content)
1465                    ("submit" . "confirm")))
1466     (with-mixi-post-form (mixi-post-diary-page) fields
1467       (unless (re-search-forward mixi-post-succeed-regexp nil t)
1468         (mixi-post-error 'cannot-find-succeed)))))
1469
1470 ;; Community object.
1471 (defvar mixi-community-cache (make-hash-table :test 'equal))
1472 (defun mixi-make-community (id &optional name birthday owner category members
1473                                open-level authority description)
1474   "Return a community object."
1475   (mixi-make-cache id (cons 'mixi-community (vector nil id name birthday owner
1476                                                     category members
1477                                                     open-level authority
1478                                                     description))
1479                    mixi-community-cache))
1480
1481 (defconst mixi-community-url-regexp
1482   "/view_community\\.pl\\?id=\\([0-9]+\\)")
1483
1484 (defun mixi-make-community-from-url (url)
1485   "Return a community object from URL."
1486   (when (string-match mixi-community-url-regexp url)
1487     (let ((id (match-string 1 url)))
1488       (mixi-make-community id))))
1489
1490 (defmacro mixi-community-p (community)
1491   `(eq (mixi-object-class ,community) 'mixi-community))
1492
1493 (defmacro mixi-community-page (community)
1494   `(concat "/view_community.pl?id=" (mixi-community-id ,community)))
1495
1496 ;; FIXME: Not community specific.
1497 (defconst mixi-community-nodata-regexp
1498   "^¥Ç¡¼¥¿¤¬¤¢¤ê¤Þ¤»¤ó")
1499 (defconst mixi-community-name-regexp
1500   "<div class=\"pageTitle communityTitle002 communityTop\">
1501 <h2>\\(.*\\)</h2>")
1502 (defconst mixi-community-birthday-regexp
1503   "<dt>³«ÀßÆü</dt>
1504 <dd>\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü<br />")
1505 ;; FIXME: Care when the owner has seceded.
1506 (defconst mixi-community-owner-regexp
1507   "<dt>´ÉÍý¿Í</dt>
1508 <dd>
1509 <a href=\"\\(home\\.pl\\|show_friend\\.pl\\?id=\\([0-9]+\\)\\)\">\\(.*\\)</a>")
1510 (defconst mixi-community-category-regexp
1511   "<dt>¥«¥Æ¥´¥ê</dt>
1512 <dd>\\(.+\\)</dd>")
1513 (defconst mixi-community-members-regexp
1514   "<dt>¥á¥ó¥Ð¡¼¿ô</dt>
1515 <dd>\\([0-9]+\\)¿Í</dd>")
1516 (defconst mixi-community-open-level-regexp
1517   "<dt>»²²Ã¾ò·ï¤È<br />¸ø³«¥ì¥Ù¥ë</dt>
1518 <dd>\\(.+\\)</dd>")
1519 (defconst mixi-community-authority-regexp
1520   "<dt>¥È¥Ô¥Ã¥¯¤Î<br />ºîÀ®¸¢¸Â</dt>
1521 <dd>
1522 \\(.+\\)
1523
1524
1525 </dd>")
1526 (defconst mixi-community-description-regexp
1527   "<div id=\"communityIntro\">
1528 <p>\\(.+\\)</p>")
1529
1530 (defun mixi-realize-community (community)
1531   "Realize a COMMUNITY."
1532   ;; FIXME: Check an expiration of cache?
1533   (unless (mixi-object-realized-p community)
1534     (with-mixi-retrieve (mixi-community-page community)
1535       (let ((case-fold-search t))
1536         (if (re-search-forward mixi-community-nodata-regexp nil t)
1537             ;; FIXME: Set all members?
1538             (mixi-community-set-name community "¥Ç¡¼¥¿¤¬¤¢¤ê¤Þ¤»¤ó")
1539           (if (re-search-forward mixi-community-name-regexp nil t)
1540               (mixi-community-set-name community (match-string 1))
1541             (mixi-realization-error 'cannot-find-name community))
1542           (if (re-search-forward mixi-community-birthday-regexp nil t)
1543               (mixi-community-set-birthday
1544                community (encode-time 0 0 0
1545                                       (string-to-number (match-string 3))
1546                                       (string-to-number (match-string 2))
1547                                       (string-to-number (match-string 1))))
1548             (mixi-realization-error 'cannot-find-birthday community))
1549           (if (re-search-forward mixi-community-owner-regexp nil t)
1550               (if (string= (match-string 1) "home.pl")
1551                   (mixi-community-set-owner community (mixi-make-me))
1552                 (mixi-community-set-owner community (mixi-make-friend
1553                                                      (match-string 2)
1554                                                      (match-string 3))))
1555             (mixi-realization-error 'cannot-find-owner community))
1556           (if (re-search-forward mixi-community-category-regexp nil t)
1557               (mixi-community-set-category community (match-string 1))
1558             (mixi-realization-error 'cannot-find-category community))
1559           (if (re-search-forward mixi-community-members-regexp nil t)
1560               (mixi-community-set-members community
1561                                           (string-to-number (match-string 1)))
1562             (mixi-realization-error 'cannot-find-members community))
1563           (if (re-search-forward mixi-community-open-level-regexp nil t)
1564               (mixi-community-set-open-level community (match-string 1))
1565             (mixi-realization-error 'cannot-find-open-level community))
1566           (if (re-search-forward mixi-community-authority-regexp nil t)
1567               (mixi-community-set-authority community (match-string 1))
1568             (mixi-realization-error 'cannot-find-authority community))
1569           (if (re-search-forward mixi-community-description-regexp nil t)
1570               (mixi-community-set-description community (match-string 1))
1571             (mixi-realization-error 'cannot-find-description community)))))
1572     (mixi-object-touch community)))
1573
1574 (defun mixi-community-id (community)
1575   "Return the id of COMMUNITY."
1576   (unless (mixi-community-p community)
1577     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1578   (aref (cdr community) 1))
1579
1580 (defun mixi-community-name (community)
1581   "Return the name of COMMUNITY."
1582   (unless (mixi-community-p community)
1583     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1584   (unless (aref (cdr community) 2)
1585     (mixi-realize-community community))
1586   (aref (cdr community) 2))
1587
1588 (defun mixi-community-birthday (community)
1589   "Return the birthday of COMMUNITY."
1590   (unless (mixi-community-p community)
1591     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1592   (mixi-realize-community community)
1593   (aref (cdr community) 3))
1594
1595 (defun mixi-community-owner (community)
1596   "Return the owner of COMMUNITY."
1597   (unless (mixi-community-p community)
1598     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1599   (mixi-realize-community community)
1600   (aref (cdr community) 4))
1601
1602 (defun mixi-community-category (community)
1603   "Return the category of COMMUNITY."
1604   (unless (mixi-community-p community)
1605     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1606   (mixi-realize-community community)
1607   (aref (cdr community) 5))
1608
1609 (defun mixi-community-members (community)
1610   "Return the members of COMMUNITY."
1611   (unless (mixi-community-p community)
1612     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1613   (mixi-realize-community community)
1614   (aref (cdr community) 6))
1615
1616 (defun mixi-community-open-level (community)
1617   "Return the open-level of COMMUNITY."
1618   (unless (mixi-community-p community)
1619     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1620   (mixi-realize-community community)
1621   (aref (cdr community) 7))
1622
1623 (defun mixi-community-authority (community)
1624   "Return the authority of COMMUNITY."
1625   (unless (mixi-community-p community)
1626     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1627   (mixi-realize-community community)
1628   (aref (cdr community) 8))
1629
1630 (defun mixi-community-description (community)
1631   "Return the description of COMMUNITY."
1632   (unless (mixi-community-p community)
1633     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1634   (mixi-realize-community community)
1635   (aref (cdr community) 9))
1636
1637 (defun mixi-community-set-name (community name)
1638   "Set the name of COMMUNITY."
1639   (unless (mixi-community-p community)
1640     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1641   (aset (cdr community) 2 name))
1642
1643 (defun mixi-community-set-birthday (community birthday)
1644   "Set the birthday of COMMUNITY."
1645   (unless (mixi-community-p community)
1646     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1647   (aset (cdr community) 3 birthday))
1648
1649 (defun mixi-community-set-owner (community owner)
1650   "Set the owner of COMMUNITY."
1651   (unless (mixi-community-p community)
1652     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1653   (unless (mixi-friend-p owner)
1654     (signal 'wrong-type-argument (list 'mixi-friend-p owner)))
1655   (aset (cdr community) 4 owner))
1656
1657 (defun mixi-community-set-category (community category)
1658   "Set the category of COMMUNITY."
1659   (unless (mixi-community-p community)
1660     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1661   (aset (cdr community) 5 category))
1662
1663 (defun mixi-community-set-members (community members)
1664   "Set the name of COMMUNITY."
1665   (unless (mixi-community-p community)
1666     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1667   (aset (cdr community) 6 members))
1668
1669 (defun mixi-community-set-open-level (community open-level)
1670   "Set the name of COMMUNITY."
1671   (unless (mixi-community-p community)
1672     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1673   (aset (cdr community) 7 open-level))
1674
1675 (defun mixi-community-set-authority (community authority)
1676   "Set the name of COMMUNITY."
1677   (unless (mixi-community-p community)
1678     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1679   (aset (cdr community) 8 authority))
1680
1681 (defun mixi-community-set-description (community description)
1682   "Set the name of COMMUNITY."
1683   (unless (mixi-community-p community)
1684     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1685   (aset (cdr community) 9 description))
1686
1687 (defmacro mixi-community-list-page (&optional friend)
1688   `(concat "/list_community.pl?page=%d"
1689            (when ,friend (concat "&id=" (mixi-friend-id ,friend)))))
1690
1691 (defconst mixi-community-list-id-regexp
1692   "<a href=\"?view_community\\.pl\\?id=\\([0-9]+\\)\"?")
1693 (defconst mixi-community-list-name-regexp
1694   "<span>\\(.+\\)([0-9]+)</span>")
1695
1696 ;;;###autoload
1697 (defun mixi-get-communities (&rest friend-or-range)
1698   "Get communities of FRIEND."
1699   (when (> (length friend-or-range) 2)
1700     (signal 'wrong-number-of-arguments
1701             (list 'mixi-get-communities (length friend-or-range))))
1702   (let ((friend (nth 0 friend-or-range))
1703         (range (nth 1 friend-or-range)))
1704     (when (or (not (mixi-friend-p friend)) (mixi-friend-p range))
1705       (setq friend (nth 1 friend-or-range))
1706       (setq range (nth 0 friend-or-range)))
1707     (unless (or (null friend) (mixi-friend-p friend))
1708       (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
1709     (let ((ids (mixi-get-matched-items (mixi-community-list-page friend)
1710                                        mixi-community-list-id-regexp
1711                                        range))
1712           (names (mixi-get-matched-items (mixi-community-list-page friend)
1713                                          mixi-community-list-name-regexp
1714                                          range)))
1715       (let ((index 0)
1716             ret)
1717         (while (< index (length ids))
1718           (setq ret (cons (mixi-make-community (nth 0 (nth index ids))
1719                                                (nth 0 (nth index names))) ret))
1720           (incf index))
1721         (reverse ret)))))
1722
1723 (defmacro mixi-search-community-list-page (keyword)
1724   `(concat "/search_community.pl?page=%d&&sort=date&type=com&submit=main"
1725            "&keyword=" (mixi-url-encode-and-quote-percent-string ,keyword)
1726            "&category_id=0"))
1727
1728 (defconst mixi-search-community-list-regexp
1729   "<dt class=\"communityTitle\"><a href=\"view_community\\.pl\\?id=\\([0-9]+\\)\">\\(.+\\) ([0-9]+)</a>")
1730
1731 ;; FIXME: Support category.
1732 ;;;###autoload
1733 (defun mixi-search-communities (keyword &optional range)
1734   (let ((items (mixi-get-matched-items (mixi-search-community-list-page
1735                                         keyword)
1736                                        mixi-search-community-list-regexp
1737                                        range)))
1738     (mapcar (lambda (item)
1739               (mixi-make-community (nth 0 item) (nth 1 item)))
1740             items)))
1741
1742 ;; Recommended community.
1743 (defalias 'mixi-recommended-community-list-page
1744   'mixi-recommended-friend-list-page)
1745
1746 (defconst mixi-recommended-community-list-regexp
1747   "<div class=\"iconListImage\"><a href=\"http://mixi\\.jp/view_community\\.pl\\?id=\\([0-9]+\\)\" [^>]+>.+</a></div><span>\\(.+\\)([0-9]+)</span>")
1748
1749 ;;;###autoload
1750 (defun mixi-get-recommended-communities (&optional range)
1751   "Get recommended communities."
1752   (let ((items (mixi-get-matched-items (mixi-recommended-community-list-page)
1753                                        mixi-recommended-community-list-regexp
1754                                        range)))
1755     (mapcar (lambda (item)
1756               (mixi-make-community (nth 0 item) (nth 1 item)))
1757             items)))
1758
1759 ;; Topic object.
1760 (defvar mixi-topic-cache (make-hash-table :test 'equal))
1761 (defun mixi-make-topic (community id &optional comment-count time title owner
1762                                   content)
1763   "Return a topic object."
1764   (mixi-make-cache (list (mixi-community-id community) id)
1765                    (cons 'mixi-topic (vector nil community id comment-count
1766                                              time title owner content))
1767                    mixi-topic-cache))
1768
1769 (defconst mixi-topic-url-regexp
1770   "/view_bbs\\.pl\\?id=\\([0-9]+\\)\\(&comment_count=\\([0-9]+\\)\\)?&comm_id=\\([0-9]+\\)")
1771
1772 (defun mixi-make-topic-from-url (url)
1773   "Return a topic object from URL."
1774   (when (string-match mixi-topic-url-regexp url)
1775     (let ((id (match-string 1 url))
1776           (comment-count (match-string 3 url))
1777           (community-id (match-string 4 url)))
1778       (mixi-make-topic (mixi-make-community community-id) id comment-count))))
1779
1780 (defmacro mixi-topic-p (topic)
1781   `(eq (mixi-object-class ,topic) 'mixi-topic))
1782
1783 (defmacro mixi-topic-page (topic)
1784   `(concat "/view_bbs.pl?id=" (mixi-topic-id ,topic)
1785            "&comm_id=" (mixi-community-id (mixi-topic-community ,topic))))
1786
1787 (defconst mixi-topic-community-regexp
1788   "<div class=\"pageTitle communityTitle002\">
1789 <h2>\\(.+\\) ¥È¥Ô¥Ã¥¯</h2>")
1790 (defconst mixi-topic-title-regexp
1791   "<span class=\"title\">\\([^<]+\\)</span>")
1792 (defconst mixi-topic-time-regexp
1793   "<span class=\"date\">\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü \\([0-9]+\\):\\([0-9]+\\)</span>")
1794 (defconst mixi-topic-owner-regexp
1795   "<dt><a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.*?\\)\\(¤µ¤ó\\)?</a>")
1796 (defconst mixi-topic-content-regexp
1797   "<dd>\\(\\(.\\|\r?\n\\)*?\\)</dd>")
1798
1799 (defun mixi-realize-topic (topic &optional page)
1800   "Realize a TOPIC."
1801   ;; FIXME: Check an expiration of cache?
1802   (unless (mixi-object-realized-p topic)
1803     (with-mixi-retrieve (or page (mixi-topic-page topic))
1804       (if (re-search-forward mixi-topic-community-regexp nil t)
1805           (mixi-community-set-name (mixi-topic-community topic)
1806                                    (match-string 1))
1807         (mixi-realization-error 'cannot-find-community topic))
1808       (if (re-search-forward mixi-topic-title-regexp nil t)
1809           (mixi-topic-set-title topic (match-string 1))
1810         (mixi-realization-error 'cannot-find-title topic))
1811       (if (re-search-forward mixi-topic-time-regexp nil t)
1812           (mixi-topic-set-time
1813            topic (encode-time 0 (string-to-number (match-string 5))
1814                               (string-to-number (match-string 4))
1815                               (string-to-number (match-string 3))
1816                               (string-to-number (match-string 2))
1817                               (string-to-number (match-string 1))))
1818         (mixi-realization-error 'cannot-find-time topic))
1819       (if (re-search-forward mixi-topic-owner-regexp nil t)
1820           (mixi-topic-set-owner topic (mixi-make-friend (match-string 1)
1821                                                         (match-string 2)))
1822         (mixi-realization-error 'cannot-find-owner topic))
1823       (if (re-search-forward mixi-topic-content-regexp nil t)
1824           (mixi-topic-set-content topic (match-string 1))
1825         (mixi-realization-error 'cannot-find-content topic)))
1826     (mixi-object-touch topic)))
1827
1828 (defun mixi-topic-community (topic)
1829   "Return the community of TOPIC."
1830   (unless (mixi-topic-p topic)
1831     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1832   (aref (cdr topic) 1))
1833
1834 (defun mixi-topic-id (topic)
1835   "Return the id of TOPIC."
1836   (unless (mixi-topic-p topic)
1837     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1838   (aref (cdr topic) 2))
1839
1840 (defun mixi-topic-comment-count (topic)
1841   "Return the comment-count of TOPIC."
1842   (unless (mixi-topic-p topic)
1843     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1844   (aref (cdr topic) 3))
1845
1846 (defun mixi-topic-time (topic)
1847   "Return the time of TOPIC."
1848   (unless (mixi-topic-p topic)
1849     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1850   (mixi-realize-topic topic)
1851   (aref (cdr topic) 4))
1852
1853 (defun mixi-topic-title (topic)
1854   "Return the title of TOPIC."
1855   (unless (mixi-topic-p topic)
1856     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1857   (mixi-realize-topic topic)
1858   (aref (cdr topic) 5))
1859
1860 (defun mixi-topic-owner (topic)
1861   "Return the owner of TOPIC."
1862   (unless (mixi-topic-p topic)
1863     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1864   (mixi-realize-topic topic)
1865   (aref (cdr topic) 6))
1866
1867 (defun mixi-topic-content (topic)
1868   "Return the content of TOPIC."
1869   (unless (mixi-topic-p topic)
1870     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1871   (mixi-realize-topic topic)
1872   (aref (cdr topic) 7))
1873
1874 (defun mixi-topic-set-comment-count (topic comment-count)
1875   "Set the comment-count of TOPIC."
1876   (unless (mixi-topic-p topic)
1877     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1878   (aset (cdr topic) 3 comment-count))
1879
1880 (defun mixi-topic-set-time (topic time)
1881   "Set the time of TOPIC."
1882   (unless (mixi-topic-p topic)
1883     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1884   (aset (cdr topic) 4 time))
1885
1886 (defun mixi-topic-set-title (topic title)
1887   "Set the title of TOPIC."
1888   (unless (mixi-topic-p topic)
1889     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1890   (aset (cdr topic) 5 title))
1891
1892 (defun mixi-topic-set-owner (topic owner)
1893   "Set the owner of TOPIC."
1894   (unless (mixi-topic-p topic)
1895     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1896   (unless (mixi-friend-p owner)
1897     (signal 'wrong-type-argument (list 'mixi-friend-p owner)))
1898   (aset (cdr topic) 6 owner))
1899
1900 (defun mixi-topic-set-content (topic content)
1901   "Set the content of TOPIC."
1902   (unless (mixi-topic-p topic)
1903     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1904   (aset (cdr topic) 7 content))
1905
1906 (defmacro mixi-post-topic-page (community)
1907   `(concat "/add_bbs.pl?id=" (mixi-community-id community)))
1908
1909 ;; FIXME: Support photos.
1910 ;;;###autoload
1911 (defun mixi-post-topic (community title content)
1912   "Post a topic to COMMUNITY."
1913   (unless (mixi-community-p community)
1914     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1915   (unless (stringp title)
1916     (signal 'wrong-type-argument (list 'stringp title)))
1917   (unless (stringp content)
1918     (signal 'wrong-type-argument (list 'stringp content)))
1919   (let ((fields `(("bbs_title" . ,title)
1920                   ("bbs_body" . ,content)
1921                   ("submit" . "main")))
1922         post-key)
1923     (with-mixi-post-form (mixi-post-topic-page community) fields
1924       (if (re-search-forward mixi-post-key-regexp nil t)
1925           (setq post-key (match-string 1))
1926         (mixi-post-error 'cannot-find-key community)))
1927     (setq fields `(("post_key" . ,post-key)
1928                    ("bbs_title" . ,title)
1929                    ("bbs_body" . ,content)
1930                    ("submit" . "confirm")))
1931     (with-mixi-post-form (mixi-post-topic-page community) fields
1932       (unless (re-search-forward mixi-post-succeed-regexp nil t)
1933         (mixi-post-error 'cannot-find-succeed community)))))
1934
1935 ;; Event object.
1936 (defvar mixi-event-cache (make-hash-table :test 'equal))
1937 (defun mixi-make-event (community id &optional comment-count time title owner
1938                                   date place detail limit members)
1939   "Return a event object."
1940   (mixi-make-cache (list (mixi-community-id community) id)
1941                    (cons 'mixi-event (vector nil community id comment-count
1942                                              time title owner date place
1943                                              detail limit members))
1944                    mixi-event-cache))
1945
1946 (defconst mixi-event-url-regexp
1947   "/view_event\\.pl\\?id=\\([0-9]+\\)\\(&comment_count=\\([0-9]+\\)\\)?&comm_id=\\([0-9]+\\)")
1948
1949 (defun mixi-make-event-from-url (url)
1950   "Return a event object from URL."
1951   (when (string-match mixi-event-url-regexp url)
1952     (let ((id (match-string 1 url))
1953           (comment-count (match-string 3 url))
1954           (community-id (match-string 4 url)))
1955       (mixi-make-event (mixi-make-community community-id) id comment-count))))
1956
1957 (defmacro mixi-event-p (event)
1958   `(eq (mixi-object-class ,event) 'mixi-event))
1959
1960 (defmacro mixi-event-page (event)
1961   `(concat "/view_event.pl?id=" (mixi-event-id ,event)
1962            "&comm_id=" (mixi-community-id (mixi-event-community ,event))))
1963
1964 (defconst mixi-event-community-regexp
1965   "<div class=\"pageTitle communityTitle002\">
1966 <h2>\\(.+\\) ¥¤¥Ù¥ó¥È</h2>")
1967 (defconst mixi-event-title-regexp
1968   "<span class=\"title\">\\([^<]+\\)</span>")
1969 (defconst mixi-event-time-regexp
1970   "<span class=\"date\">\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü \\([0-9]+\\):\\([0-9]+\\)</span>")
1971 (defconst mixi-event-date-regexp
1972   "<dt>³«ºÅÆü»þ</dt>
1973 <dd>\\(.+\\)</dd>")
1974 (defconst mixi-event-place-regexp
1975   "<dt>³«ºÅ¾ì½ê</dt>
1976 <dd>\\(.+\\)</dd>")
1977 (defconst mixi-event-owner-regexp
1978   "<dt>\\((mixi Âà²ñºÑ)\\|<a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.*\\)</a>\\)</dt>")
1979 (defconst mixi-event-owner-seceded-regexp
1980   "(mixi Âà²ñºÑ)")
1981 (defconst mixi-event-detail-regexp
1982   "<dd>\\(\\(.\\|\r?\n\\)*?\\)</dd>
1983 </dl>")
1984 (defconst mixi-event-limit-regexp
1985   "<dt>Ê罸´ü¸Â</dt>
1986 <dd>\\(.+\\)</dd>")
1987 (defconst mixi-event-members-regexp
1988   "<dt>»²²Ã¼Ô</dt>
1989 <dd>\\(.+\\)</dd>")
1990
1991 (defun mixi-realize-event (event &optional page)
1992   "Realize a EVENT."
1993   ;; FIXME: Check an expiration of cache?
1994   (unless (mixi-object-realized-p event)
1995     (with-mixi-retrieve (or page (mixi-event-page event))
1996       (let ((case-fold-search t))
1997         (if (re-search-forward mixi-event-community-regexp nil t)
1998             (mixi-community-set-name (mixi-event-community event)
1999                                      (match-string 1))
2000           (mixi-realization-error 'cannot-find-community event))
2001         (if (re-search-forward mixi-event-title-regexp nil t)
2002             (mixi-event-set-title event (match-string 1))
2003           (mixi-realization-error 'cannot-find-title event))
2004         (if (re-search-forward mixi-event-time-regexp nil t)
2005             (mixi-event-set-time
2006              event (encode-time 0 (string-to-number (match-string 5))
2007                                 (string-to-number (match-string 4))
2008                                 (string-to-number (match-string 3))
2009                                 (string-to-number (match-string 2))
2010                                 (string-to-number (match-string 1))))
2011           (mixi-realization-error 'cannot-find-time event))
2012         (if (re-search-forward mixi-event-date-regexp nil t)
2013             (mixi-event-set-date event (match-string 1))
2014           (mixi-realization-error 'cannot-find-date event))
2015         (if (re-search-forward mixi-event-place-regexp nil t)
2016             (mixi-event-set-place event (match-string 1))
2017           (mixi-realization-error 'cannot-find-place event))
2018         (if (re-search-forward mixi-event-owner-regexp nil t)
2019             (let ((seceded-or-not (match-string 1))
2020                   (id (match-string 2))
2021                   (nick (match-string 3)))
2022               (if (string= mixi-event-owner-seceded-regexp seceded-or-not)
2023                   (mixi-event-set-owner event
2024                                         (mixi-make-friend nil seceded-or-not))
2025                 (mixi-event-set-owner event (mixi-make-friend id nick))))
2026           (mixi-realization-error 'cannot-find-owner event))
2027         (if (re-search-forward mixi-event-detail-regexp nil t)
2028             (mixi-event-set-detail event (match-string 1))
2029           (mixi-realization-error 'cannot-find-detail event))
2030         (if (re-search-forward mixi-event-limit-regexp nil t)
2031           (mixi-event-set-limit event (match-string 1))
2032           (mixi-realization-error 'cannot-find-limit event))
2033         (if (re-search-forward mixi-event-members-regexp nil t)
2034             (mixi-event-set-members event (match-string 1))
2035           (mixi-realization-error 'cannot-find-members event))))
2036     (mixi-object-touch event)))
2037
2038 (defun mixi-event-community (event)
2039   "Return the community of EVENT."
2040   (unless (mixi-event-p event)
2041     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2042   (aref (cdr event) 1))
2043
2044 (defun mixi-event-id (event)
2045   "Return the id of EVENT."
2046   (unless (mixi-event-p event)
2047     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2048   (aref (cdr event) 2))
2049
2050 (defun mixi-event-comment-count (event)
2051   "Return the comment-count of EVENT."
2052   (unless (mixi-event-p event)
2053     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2054   (aref (cdr event) 3))
2055
2056 (defun mixi-event-time (event)
2057   "Return the time of EVENT."
2058   (unless (mixi-event-p event)
2059     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2060   (mixi-realize-event event)
2061   (aref (cdr event) 4))
2062
2063 (defun mixi-event-title (event)
2064   "Return the title of EVENT."
2065   (unless (mixi-event-p event)
2066     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2067   (mixi-realize-event event)
2068   (aref (cdr event) 5))
2069
2070 (defun mixi-event-owner (event)
2071   "Return the owner of EVENT."
2072   (unless (mixi-event-p event)
2073     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2074   (mixi-realize-event event)
2075   (aref (cdr event) 6))
2076
2077 (defun mixi-event-date (event)
2078   "Return the date of EVENT."
2079   (unless (mixi-event-p event)
2080     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2081   (mixi-realize-event event)
2082   (aref (cdr event) 7))
2083
2084 (defun mixi-event-place (event)
2085   "Return the place of EVENT."
2086   (unless (mixi-event-p event)
2087     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2088   (mixi-realize-event event)
2089   (aref (cdr event) 8))
2090
2091 (defun mixi-event-detail (event)
2092   "Return the detail of EVENT."
2093   (unless (mixi-event-p event)
2094     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2095   (mixi-realize-event event)
2096   (aref (cdr event) 9))
2097
2098 (defun mixi-event-limit (event)
2099   "Return the limit of EVENT."
2100   (unless (mixi-event-p event)
2101     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2102   (mixi-realize-event event)
2103   (aref (cdr event) 10))
2104
2105 (defun mixi-event-members (event)
2106   "Return the members of EVENT."
2107   (unless (mixi-event-p event)
2108     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2109   (mixi-realize-event event)
2110   (aref (cdr event) 11))
2111
2112 (defun mixi-event-set-comment-count (event comment-count)
2113   "Set the comment-count of EVENT."
2114   (unless (mixi-event-p event)
2115     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2116   (aset (cdr event) 3 comment-count))
2117
2118 (defun mixi-event-set-time (event time)
2119   "Set the time of EVENT."
2120   (unless (mixi-event-p event)
2121     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2122   (aset (cdr event) 4 time))
2123
2124 (defun mixi-event-set-title (event title)
2125   "Set the title of EVENT."
2126   (unless (mixi-event-p event)
2127     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2128   (aset (cdr event) 5 title))
2129
2130 (defun mixi-event-set-owner (event owner)
2131   "Set the owner of EVENT."
2132   (unless (mixi-event-p event)
2133     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2134   (unless (mixi-friend-p owner)
2135     (signal 'wrong-type-argument (list 'mixi-friend-p owner)))
2136   (aset (cdr event) 6 owner))
2137
2138 (defun mixi-event-set-date (event date)
2139   "Set the date of EVENT."
2140   (unless (mixi-event-p event)
2141     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2142   (aset (cdr event) 7 date))
2143
2144 (defun mixi-event-set-place (event place)
2145   "Set the place of EVENT."
2146   (unless (mixi-event-p event)
2147     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2148   (aset (cdr event) 8 place))
2149
2150 (defun mixi-event-set-detail (event detail)
2151   "Set the detail of EVENT."
2152   (unless (mixi-event-p event)
2153     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2154   (aset (cdr event) 9 detail))
2155
2156 (defun mixi-event-set-limit (event limit)
2157   "Set the limit of EVENT."
2158   (unless (mixi-event-p event)
2159     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2160   (aset (cdr event) 10 limit))
2161
2162 (defun mixi-event-set-members (event members)
2163   "Set the members of EVENT."
2164   (unless (mixi-event-p event)
2165     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2166   (aset (cdr event) 11 members))
2167
2168 ;; BBS object.
2169 (defconst mixi-bbs-list '(mixi-topic mixi-event))
2170
2171 (defmacro mixi-bbs-p (bbs)
2172   `(memq (mixi-object-class ,bbs) mixi-bbs-list))
2173
2174 (defun mixi-bbs-community (bbs)
2175   "Return the community of BBS."
2176   (unless (mixi-bbs-p bbs)
2177     (signal 'wrong-type-argument (list 'mixi-bbs-p bbs)))
2178   (let ((func (intern (concat mixi-object-prefix
2179                               (mixi-object-name bbs) "-community"))))
2180     (funcall func bbs)))
2181
2182 (defun mixi-bbs-comment-count (bbs)
2183   "Return the comment-count of BBS."
2184   (unless (mixi-bbs-p bbs)
2185     (signal 'wrong-type-argument (list 'mixi-bbs-p bbs)))
2186   (let ((func (intern (concat mixi-object-prefix
2187                               (mixi-object-name bbs) "-comment-count"))))
2188     (funcall func bbs)))
2189
2190 (defun mixi-bbs-set-comment-count (bbs count)
2191   "Set the comment-count of BBS."
2192   (unless (mixi-bbs-p bbs)
2193     (signal 'wrong-type-argument (list 'mixi-bbs-p bbs)))
2194   (let ((func (intern (concat mixi-object-prefix
2195                               (mixi-object-name bbs) "-set-comment-count"))))
2196     (funcall func bbs count)))
2197
2198 (defalias 'mixi-bbs-id 'mixi-object-id)
2199 (defalias 'mixi-bbs-time 'mixi-object-time)
2200 (defalias 'mixi-bbs-title 'mixi-object-title)
2201 (defalias 'mixi-bbs-owner 'mixi-object-owner)
2202 (defalias 'mixi-bbs-content 'mixi-object-content)
2203
2204 (defmacro mixi-bbs-list-page (community)
2205   `(concat "/list_bbs.pl?page=%d"
2206            "&id=" (mixi-community-id ,community)))
2207
2208 (defconst mixi-bbs-list-regexp
2209   "<a href=\"?view_\\(bbs\\|event\\)\\.pl\\?id=\\([0-9]+\\)")
2210
2211 ;;;###autoload
2212 (defun mixi-get-bbses (community &optional range)
2213   "Get bbses of COMMUNITY."
2214   (unless (mixi-community-p community)
2215     (signal 'wrong-type-argument (list 'mixi-community-p community)))
2216   (let ((items (mixi-get-matched-items (mixi-bbs-list-page community)
2217                                        mixi-bbs-list-regexp
2218                                        range)))
2219     (mapcar (lambda (item)
2220               (let ((name (nth 0 item)))
2221                 (when (string= name "bbs")
2222                   (setq name "topic"))
2223                 (let ((func (intern (concat "mixi-make-" name))))
2224                   (funcall func community (nth 1 item)))))
2225             items)))
2226
2227 (defmacro mixi-new-bbs-list-page ()
2228   `(concat "/new_bbs.pl?page=%d"))
2229
2230 (defconst mixi-new-bbs-list-regexp
2231   "<dd><a href=\"view_\\(bbs\\|event\\)\\.pl\\?id=\\([0-9]+\\)&comment_count=\\([0-9]+\\)&comm_id=\\([0-9]+\\)\">")
2232
2233 ;;;###autoload
2234 (defun mixi-get-new-bbses (&optional range)
2235   "Get new topics."
2236   (let ((items (mixi-get-matched-items (mixi-new-bbs-list-page)
2237                                        mixi-new-bbs-list-regexp
2238                                        range)))
2239     (delq nil
2240           (mapcar (lambda (item)
2241                     (let ((name (nth 0 item)))
2242                       (when (string= name "bbs")
2243                         (setq name "topic"))
2244                       (let* ((func (intern (concat "mixi-make-" name)))
2245                              (bbs (funcall func
2246                                            (mixi-make-community (nth 3 item))
2247                                            (nth 1 item)))
2248                              (comment-count (mixi-bbs-comment-count bbs))
2249                              (count (string-to-number (nth 2 item))))
2250                         (when (or (null comment-count)
2251                                   (< comment-count count))
2252                           (mixi-bbs-set-comment-count bbs count)
2253                           bbs))))
2254                   items))))
2255  
2256 (defmacro mixi-search-bbs-list-page (keyword)
2257   `(concat "/search_topic.pl?page=%d&type=top&submit=search"
2258            "&keyword=" (mixi-url-encode-and-quote-percent-string ,keyword)
2259            "&community_id=0&category_id=0"))
2260
2261 (defconst mixi-search-bbs-list-regexp
2262   "<a href=\"view_\\(bbs\\|event\\)\\.pl\\?id=\\([0-9]+\\)&comm_id=\\([0-9]+\\)\" class=\"title\">")
2263
2264 ;; FIXME: Support community and category.
2265 ;;;###autoload
2266 (defun mixi-search-bbses (keyword &optional range)
2267   (let ((items (mixi-get-matched-items (mixi-search-bbs-list-page keyword)
2268                                        mixi-search-bbs-list-regexp
2269                                        range)))
2270     (mapcar (lambda (item)
2271               (let ((name (nth 0 item)))
2272                 (when (string= name "bbs")
2273                   (setq name "topic"))
2274                 (let ((func (intern (concat "mixi-make-" name))))
2275                   (funcall func (mixi-make-community (nth 2 item))
2276                            (nth 1 item)))))
2277             items)))
2278
2279 ;; Parent object.
2280 (defmacro mixi-parent-p (object)
2281   `(or (eq (mixi-object-class ,object) 'mixi-diary)
2282        (mixi-bbs-p ,object)))
2283
2284 (defun mixi-realize-parent (parent &optional page)
2285   "Realize a PARENT."
2286   (unless (mixi-parent-p parent)
2287     (signal 'wrong-type-argument (list 'mixi-parent-p parent)))
2288   (let ((func (intern (concat mixi-object-prefix "realize-"
2289                               (mixi-object-name parent)))))
2290     (funcall func parent page)))
2291
2292 ;; Comment object.
2293 (defun mixi-make-comment (parent owner time content &optional count)
2294   "Return a comment object."
2295   (cons 'mixi-comment (vector parent owner time content count)))
2296
2297 (defmacro mixi-comment-p (comment)
2298   `(eq (mixi-object-class ,comment) 'mixi-comment))
2299
2300 (defun mixi-comment-parent (comment)
2301   "Return the parent of COMMENT."
2302   (unless (mixi-comment-p comment)
2303     (signal 'wrong-type-argument (list 'mixi-comment-p comment)))
2304   (aref (cdr comment) 0))
2305
2306 (defun mixi-comment-owner (comment)
2307   "Return the owner of COMMENT."
2308   (unless (mixi-comment-p comment)
2309     (signal 'wrong-type-argument (list 'mixi-comment-p comment)))
2310   (aref (cdr comment) 1))
2311
2312 (defun mixi-comment-time (comment)
2313   "Return the time of COMMENT."
2314   (unless (mixi-comment-p comment)
2315     (signal 'wrong-type-argument (list 'mixi-comment-p comment)))
2316   (aref (cdr comment) 2))
2317
2318 (defun mixi-comment-content (comment)
2319   "Return the content of COMMENT."
2320   (unless (mixi-comment-p comment)
2321     (signal 'wrong-type-argument (list 'mixi-comment-p comment)))
2322   (aref (cdr comment) 3))
2323
2324 (defun mixi-comment-count (comment)
2325   "Return the count of COMMENT."
2326   (unless (mixi-comment-p comment)
2327     (signal 'wrong-type-argument (list 'mixi-comment-p comment)))
2328   (aref (cdr comment) 4))
2329
2330 (defun mixi-diary-comment-list-page (diary)
2331   (concat "/view_diary.pl?full=1"
2332           "&id=" (mixi-diary-id diary)
2333           "&owner_id=" (mixi-friend-id (mixi-diary-owner diary))))
2334
2335 ;; FIXME: Split regexp to time, owner(id and nick) and contents.
2336 (defconst mixi-diary-comment-list-regexp
2337   "<span class=\"commentTitleName\">\\(<input type=\"checkbox\" name=\"comment_id\" value=\".+\" />
2338 \\|\\)<a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.*\\)</a>
2339 \\(
2340 | <a href=\"delete_comment\\.pl\\?diary_id=[0-9]+&owner_id=[0-9]+&comment_id=.+&type=comment\">¼«Ê¬¤Î¥³¥á¥ó¥È¤òºï½ü¤¹¤ë</a>
2341 \\|\\)</span>
2342
2343 <span class=\"commentTitleDate\">\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü&nbsp;\\([0-9]+\\):\\([0-9]+\\)</span>
2344 </dt>
2345 +
2346 <dd>
2347 \\(\\(.\\|\r?\n\\)*?\\)
2348 </dd>
2349 </dl>
2350 </div>")
2351
2352 (defun mixi-topic-comment-list-page (topic)
2353   (concat "/view_bbs.pl?page=all"
2354           "&id=" (mixi-topic-id topic)
2355           "&comm_id=" (mixi-community-id (mixi-topic-community topic))))
2356
2357 ;; FIXME: Split regexp to time, owner(id and nick) and contents.
2358 (defconst mixi-topic-comment-list-regexp
2359   "<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>
2360 <span class=\"date\">\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü \\([0-9]+\\):\\([0-9]+\\)</span></dt>
2361 <dd>
2362 <dl class=\"commentContent01\">
2363 <dt><a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.*\\)</a></dt>
2364 <dd>
2365 \\(\\(.\\|\r?\n\\)*?\\)
2366 </dd>")
2367
2368 (defun mixi-event-comment-list-page (event)
2369   (concat "/view_event.pl?page=all"
2370           "&id=" (mixi-event-id event)
2371           "&comm_id=" (mixi-community-id (mixi-event-community event))))
2372
2373 ;; FIXME: Split regexp to time, owner(id and nick) and contents.
2374 (defconst mixi-event-comment-list-regexp
2375   "<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>
2376 <span class=\"date\">\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü \\([0-9]+\\):\\([0-9]+\\)</span></dt>
2377 <dd>
2378 <dl class=\"commentContent01\">
2379 <dt><a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.*\\)</a></dt>
2380 <dd>
2381 \\(\\(.\\|\r?\n\\)*?\\)
2382 </dd>")
2383
2384 ;;;###autoload
2385 (defun mixi-get-comments (parent &optional range)
2386   "Get comments of PARENT."
2387   (unless (mixi-parent-p parent)
2388     (signal 'wrong-type-argument (list 'mixi-parent-p parent)))
2389   (let* ((name (mixi-object-name parent))
2390          (list-page (intern (concat mixi-object-prefix name
2391                                     "-comment-list-page")))
2392          (regexp (eval (intern (concat mixi-object-prefix name
2393                                        "-comment-list-regexp"))))
2394          (page (funcall list-page parent)))
2395     (unless (mixi-object-realized-p parent)
2396       (mixi-realize-parent parent page)
2397       (setq page nil))
2398     (let ((items (mixi-get-matched-items page regexp range t)))
2399       (mapcar (lambda (item)
2400                 (let (owner-id owner-nick year month day hour minute content
2401                                count)
2402                   (if (eq (mixi-object-class parent) 'mixi-diary)
2403                       (progn
2404                         (setq owner-id (nth 1 item))
2405                         (setq owner-nick (nth 2 item))
2406                         (setq year (nth 4 item))
2407                         (setq month (nth 5 item))
2408                         (setq day (nth 6 item))
2409                         (setq hour (nth 7 item))
2410                         (setq minute (nth 8 item))
2411                         (setq content (nth 9 item)))
2412                     (setq owner-id (nth 8 item))
2413                     (setq owner-nick (nth 9 item))
2414                     (setq year (nth 3 item))
2415                     (setq month (nth 4 item))
2416                     (setq day (nth 5 item))
2417                     (setq hour (nth 6 item))
2418                     (setq minute (nth 7 item))
2419                     (setq content (nth 10 item))
2420                     (setq count (nth 1 item)))
2421                   (mixi-make-comment parent (mixi-make-friend owner-id
2422                                                               owner-nick)
2423                                      (encode-time
2424                                       0
2425                                       (string-to-number minute)
2426                                       (string-to-number hour)
2427                                       (string-to-number day)
2428                                       (string-to-number month)
2429                                       (string-to-number year))
2430                                      content count)))
2431               items))))
2432
2433 (defmacro mixi-new-comment-list-page ()
2434   `(concat "/new_comment.pl?page=%d"))
2435
2436 (defconst mixi-new-comment-list-regexp
2437   "<a href=\"view_diary\\.pl\\?id=\\([0-9]+\\)&owner_id=\\([0-9]+\\)&comment_count=\\([0-9]+\\)\">")
2438
2439 ;;;###autoload
2440 (defun mixi-get-new-comments (&optional range)
2441   "Get new comments."
2442   (let ((items (mixi-get-matched-items (mixi-new-comment-list-page)
2443                                        mixi-new-comment-list-regexp
2444                                        range)))
2445     (delq nil
2446           (mapcar (lambda (item)
2447                     (let* ((diary (mixi-make-diary
2448                                    (mixi-make-friend (nth 1 item))
2449                                    (nth 0 item)))
2450                            (comment-count (mixi-diary-comment-count diary))
2451                            (count (string-to-number (nth 2 item))))
2452                       (when (or (null comment-count)
2453                                 (< comment-count count))
2454                         (mixi-diary-set-comment-count diary count)
2455                         diary)))
2456                   items))))
2457
2458 (defmacro mixi-new-bbs-comment-list-page ()
2459   `(concat "/new_bbs_comment.pl?page=%d"))
2460
2461 (defconst mixi-new-bbs-comment-list-regexp
2462   "<a href=\"?view_\\(bbs\\|event\\)\\.pl\\?id=\\([0-9]+\\)&comment_count=\\([0-9]+\\)&comm_id=\\([0-9]+\\)\"?>")
2463
2464 ;;;###autoload
2465 (defun mixi-get-new-bbs-comments (&optional range)
2466   "Get new BBS comments."
2467   (let ((items (mixi-get-matched-items (mixi-new-bbs-comment-list-page)
2468                                        mixi-new-bbs-comment-list-regexp
2469                                        range)))
2470     (delq nil
2471           (mapcar (lambda (item)
2472                     (let ((name (nth 0 item)))
2473                       (when (string= name "bbs")
2474                         (setq name "topic"))
2475                       (let ((make-func (intern (concat "mixi-make-" name)))
2476                             (comment-count-func
2477                              (intern (concat "mixi-" name "-comment-count")))
2478                             (set-comment-count-func
2479                              (intern (concat "mixi-" name
2480                                              "-set-comment-count"))))
2481                         (let* ((bbs (funcall make-func
2482                                      (mixi-make-community (nth 3 item))
2483                                      (nth 1 item)))
2484                                (comment-count (funcall comment-count-func bbs))
2485                                (count (string-to-number (nth 2 item))))
2486                           (when (or (null comment-count)
2487                                     (< comment-count count))
2488                             (funcall set-comment-count-func bbs count)
2489                             bbs)))))
2490                   items))))
2491
2492 (defun mixi-post-diary-comment-page (diary)
2493   (concat "/add_comment.pl?&diary_id=" (mixi-diary-id diary)))
2494
2495 (defun mixi-post-topic-comment-page (topic)
2496   (concat "/add_bbs_comment.pl?id=" (mixi-topic-id topic)
2497           "&comm_id=" (mixi-community-id (mixi-topic-community topic))))
2498
2499 (defun mixi-post-event-comment-page (event)
2500   (concat "/add_event_comment.pl?id=" (mixi-event-id event)
2501           "&comm_id=" (mixi-community-id (mixi-event-community event))))
2502
2503 ;; FIXME: Support photos.
2504 ;;;###autoload
2505 (defun mixi-post-comment (parent content)
2506   "Post a comment to PARENT."
2507   (unless (mixi-object-p parent)
2508     (signal 'wrong-type-argument (list 'mixi-object-p parent)))
2509   (unless (stringp content)
2510     (signal 'wrong-type-argument (list 'stringp content)))
2511   (let* ((name (mixi-object-name parent))
2512          (page (intern (concat mixi-object-prefix "post-" name
2513                                "-comment-page")))
2514          fields post-key)
2515     (if (mixi-diary-p parent)
2516         (setq fields
2517               `(("owner_id" . ,(mixi-friend-id (mixi-diary-owner parent)))
2518                 ("comment_body" . ,content)))
2519       (setq fields `(("comment" . ,content))))
2520     (with-mixi-post-form (funcall page parent) fields
2521       (if (re-search-forward mixi-post-key-regexp nil t)
2522           (setq post-key (match-string 1))
2523         (mixi-post-error 'cannot-find-key parent)))
2524     (if (mixi-diary-p parent)
2525         (setq fields
2526               `(("post_key" . ,post-key)
2527                 ("owner_id" . ,(mixi-friend-id (mixi-diary-owner parent)))
2528                 ("comment_body" . ,content)
2529                 ("submit" . "confirm")))
2530       (setq fields `(("post_key" . ,post-key)
2531                      ("comment" . ,content)
2532                      ("submit" . "confirm"))))
2533     (with-mixi-post-form (funcall page parent) fields
2534       (unless (re-search-forward mixi-post-succeed-regexp nil t)
2535         (mixi-post-error 'cannot-find-succeed parent)))))
2536
2537 ;; Message object.
2538 (defconst mixi-message-box-list '(inbox outbox savebox thrash)) ; thrash?
2539
2540 (defmacro mixi-message-box-p (box)
2541   `(memq ,box mixi-message-box-list))
2542
2543 (defun mixi-message-box-name (box)
2544   "Return the name of BOX."
2545   (unless (mixi-message-box-p box)
2546     (signal 'wrong-type-argument (list 'mixi-message-box-p box)))
2547   (symbol-name box))
2548
2549 (defvar mixi-message-cache (make-hash-table :test 'equal))
2550 (defun mixi-make-message (id box &optional owner title time content)
2551   "Return a message object."
2552   (mixi-make-cache (list id box)
2553                    (cons 'mixi-message (vector nil id box owner title time
2554                                                content))
2555                    mixi-message-cache))
2556
2557 (defconst mixi-message-url-regexp
2558   "/view_message\\.pl\\?id=\\([a-z0-9]+\\)&box=\\([a-z]+\\)")
2559
2560 (defun mixi-make-message-from-url (url)
2561   "Return a message object from URL."
2562   (when (string-match mixi-message-url-regexp url)
2563     (let ((id (match-string 1 url))
2564           (box (match-string 2 url)))
2565       (mixi-make-message id box))))
2566
2567 (defmacro mixi-message-p (message)
2568   `(eq (mixi-object-class ,message) 'mixi-message))
2569
2570 (defmacro mixi-message-page (message)
2571   `(concat "/view_message.pl?id=" (mixi-message-id ,message)
2572            "&box=" (mixi-message-box ,message)))
2573
2574 (defconst mixi-message-owner-regexp
2575   "<font COLOR=#996600>\\(º¹½Ð¿Í\\|°¸&nbsp;Àè\\)</font>&nbsp;:&nbsp;<a HREF=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.*\\)\\(</a>\\|</td>\\)")
2576 (defconst mixi-message-time-regexp
2577 "<font COLOR=#996600>Æü\\(¡¡\\|&nbsp;\\)ÉÕ</font>&nbsp;:&nbsp;\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü \\([0-9]+\\)»þ\\([0-9]+\\)ʬ&nbsp;&nbsp;")
2578 (defconst mixi-message-title-regexp
2579 "<font COLOR=#996600>·ï\\(¡¡\\|&nbsp;\\)̾</font>&nbsp;:&nbsp;\\(.*\\)\n?</td>")
2580 (defconst mixi-message-content-regexp
2581   "<tr><td CLASS=h120 width=\"500\">\\(.*\\)</td></tr>")
2582
2583 (defun mixi-realize-message (message)
2584   "Realize a MESSAGE."
2585   (unless (mixi-object-realized-p message)
2586     (with-mixi-retrieve (mixi-message-page message)
2587       (if (re-search-forward mixi-message-owner-regexp nil t)
2588           (mixi-message-set-owner message
2589                                   (mixi-make-friend (match-string 2)
2590                                                     (match-string 3)))
2591         (mixi-realization-error 'cannot-find-owner message))
2592       (if (re-search-forward mixi-message-time-regexp nil t)
2593           (mixi-message-set-time
2594            message (encode-time 0 (string-to-number (match-string 6))
2595                                 (string-to-number (match-string 5))
2596                                 (string-to-number (match-string 4))
2597                                 (string-to-number (match-string 3))
2598                                 (string-to-number (match-string 2))))
2599         (mixi-realization-error 'cannot-find-time message))
2600       (if (re-search-forward mixi-message-title-regexp nil t)
2601           (mixi-message-set-title message (match-string 2))
2602         (mixi-realization-error 'cannot-find-title message))
2603       (if (re-search-forward mixi-message-content-regexp nil t)
2604           (mixi-message-set-content message (match-string 1))
2605         (mixi-realization-error 'cannot-find-content message)))
2606     (mixi-object-touch message)))
2607
2608 (defun mixi-message-id (message)
2609   "Return the id of MESSAGE."
2610   (unless (mixi-message-p message)
2611     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2612   (aref (cdr message) 1))
2613
2614 (defun mixi-message-box (message)
2615   "Return the box of MESSAGE."
2616   (unless (mixi-message-p message)
2617     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2618   (aref (cdr message) 2))
2619
2620 (defun mixi-message-owner (message)
2621   "Return the owner of MESSAGE."
2622   (unless (mixi-message-p message)
2623     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2624   (mixi-realize-message message)
2625   (aref (cdr message) 3))
2626
2627 (defun mixi-message-title (message)
2628   "Return the title of MESSAGE."
2629   (unless (mixi-message-p message)
2630     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2631   (mixi-realize-message message)
2632   (aref (cdr message) 4))
2633
2634 (defun mixi-message-time (message)
2635   "Return the date of MESSAGE."
2636   (unless (mixi-message-p message)
2637     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2638   (mixi-realize-message message)
2639   (aref (cdr message) 5))
2640
2641 (defun mixi-message-content (message)
2642   "Return the content of MESSAGE."
2643   (unless (mixi-message-p message)
2644     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2645   (mixi-realize-message message)
2646   (aref (cdr message) 6))
2647
2648 (defun mixi-message-set-owner (message owner)
2649   "Set the owner of MESSAGE."
2650   (unless (mixi-message-p message)
2651     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2652   (aset (cdr message) 3 owner))
2653
2654 (defun mixi-message-set-title (message title)
2655   "Set the title of MESSAGE."
2656   (unless (mixi-message-p message)
2657     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2658   (aset (cdr message) 4 title))
2659
2660 (defun mixi-message-set-time (message time)
2661   "Set the date of MESSAGE."
2662   (unless (mixi-message-p message)
2663     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2664   (aset (cdr message) 5 time))
2665
2666 (defun mixi-message-set-content (message content)
2667   "Set the content of MESSAGE."
2668   (unless (mixi-message-p message)
2669     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2670   (aset (cdr message) 6 content))
2671
2672 (defmacro mixi-message-list-page (&optional box)
2673   `(concat "/list_message.pl?page=%d"
2674            (when ,box (concat "&box=" ,box))))
2675
2676 (defconst mixi-message-list-regexp
2677   "<td><a HREF=\"view_message\\.pl\\?id=\\(.+\\)&box=\\(.+\\)\">")
2678
2679 ;;;###autoload
2680 (defun mixi-get-messages (&rest box-or-range)
2681   "Get messages of BOX."
2682   (when (> (length box-or-range) 2)
2683     (signal 'wrong-number-of-arguments
2684             (list 'mixi-get-messages (length box-or-range))))
2685   (let ((box (nth 0 box-or-range))
2686         (range (nth 1 box-or-range)))
2687     (when (or (not (mixi-message-box-p box))
2688               (mixi-message-box-p range))
2689       (setq box (nth 1 box-or-range))
2690       (setq range (nth 0 box-or-range)))
2691     (let ((items (mixi-get-matched-items
2692                   (mixi-message-list-page
2693                    (when box (mixi-message-box-name box)))
2694                   mixi-message-list-regexp
2695                   range)))
2696       (mapcar (lambda (item)
2697                 (mixi-make-message (nth 0 item) (nth 1 item)))
2698               items))))
2699
2700 (defmacro mixi-post-message-page (friend)
2701   `(concat "/send_message.pl?id=" (mixi-friend-id friend)))
2702
2703 (defconst mixi-post-message-key-regexp
2704   "<input name=post_key type=hidden value=\\([a-z0-9]+\\)>")
2705
2706 (defconst mixi-post-message-succeed-regexp
2707   "<b>Á÷¿®´°Î»</b>¤·¤Þ¤·¤¿¡£")
2708
2709 ;;;###autoload
2710 (defun mixi-post-message (friend title content)
2711   "Post a message to FRIEND."
2712   (unless (mixi-friend-p friend)
2713     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
2714   (unless (stringp title)
2715     (signal 'wrong-type-argument (list 'stringp title)))
2716   (unless (stringp content)
2717     (signal 'wrong-type-argument (list 'stringp content)))
2718   (let ((fields `(("subject" . ,title)
2719                   ("body" . ,content)
2720                   ("submit" . "main")))
2721         post-key)
2722     (with-mixi-post-form (mixi-post-message-page friend) fields
2723       (if (re-search-forward mixi-post-message-key-regexp nil t)
2724           (setq post-key (match-string 1))
2725         (mixi-post-error 'cannot-find-key friend)))
2726     (setq fields `(("post_key" . ,post-key)
2727                    ("subject" . ,title)
2728                    ("body" . ,content)
2729                    ("yes" . "¡¡Á÷¡¡¿®¡¡")
2730                    ("submit" . "confirm")))
2731     (with-mixi-post-form (mixi-post-message-page friend) fields
2732       (unless (re-search-forward mixi-post-message-succeed-regexp nil t)
2733         (mixi-post-error 'cannot-find-succeed friend)))))
2734
2735 ;; Introduction object.
2736 (defun mixi-make-introduction (parent owner content)
2737   "Return a introduction object."
2738   (cons 'mixi-introduction (vector parent owner content)))
2739
2740 (defmacro mixi-introduction-p (introduction)
2741   `(eq (mixi-object-class ,introduction) 'mixi-introduction))
2742
2743 (defun mixi-introduction-parent (introduction)
2744   "Return the parent of INTRODUCTION."
2745   (unless (mixi-introduction-p introduction)
2746     (signal 'wrong-type-argument (list 'mixi-introduction-p introduction)))
2747   (aref (cdr introduction) 0))
2748
2749 (defun mixi-introduction-owner (introduction)
2750   "Return the owner of INTRODUCTION."
2751   (unless (mixi-introduction-p introduction)
2752     (signal 'wrong-type-argument (list 'mixi-introduction-p introduction)))
2753   (aref (cdr introduction) 1))
2754
2755 (defun mixi-introduction-content (introduction)
2756   "Return the content of INTRODUCTION."
2757   (unless (mixi-introduction-p introduction)
2758     (signal 'wrong-type-argument (list 'mixi-introduction-p introduction)))
2759   (aref (cdr introduction) 3))
2760
2761 (defmacro mixi-introduction-list-page (&optional friend)
2762   `(concat "/show_intro.pl?page=%d"
2763            (when ,friend (concat "&id=" (mixi-friend-id ,friend)))))
2764
2765 (defconst mixi-introduction-list-regexp
2766   "<tr bgcolor=#FFFFFF>
2767 <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>
2768 \\(.*\\)</td></a>
2769
2770 <td WIDTH=480>
2771 \\(´Ø·¸¡§.+<br>
2772
2773
2774 \\(\\(.\\|\n<br>\\)+\\)\\|
2775 \\(\\(.\\|\n<br>\\)+\\)\\)
2776
2777
2778
2779
2780 </td>
2781 </tr>")
2782 (defconst mixi-my-introduction-list-regexp
2783   "<tr bgcolor=#FFFFFF>
2784 <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>
2785 \\(.*\\)</td></a>
2786
2787
2788 <td WIDTH=480>
2789 \\(´Ø·¸¡§.+<br>
2790
2791
2792 \\(\\(.\\|\n<br>\\)+\\)\\|
2793 \\(\\(.\\|\n<br>\\)+\\)\\)
2794
2795
2796 <br>
2797 <a href=\"edit_intro\\.pl\\?id=\\1&type=edit\">¤³¤Îͧ¿Í¤ò¾Ò²ð¤¹¤ë</a>
2798
2799
2800 <BR>
2801 <a href=\"delete_intro\\.pl\\?id=\\1\">ºï½ü</a>
2802
2803 </td>
2804 </tr>")
2805
2806 ;;;###autoload
2807 (defun mixi-get-introductions (&rest friend-or-range)
2808   "Get introductions of FRIEND."
2809   (when (> (length friend-or-range) 2)
2810     (signal 'wrong-number-of-arguments
2811             (list 'mixi-get-introduction (length friend-or-range))))
2812   (let ((friend (nth 0 friend-or-range))
2813         (range (nth 1 friend-or-range)))
2814     (when (or (not (mixi-friend-p friend)) (mixi-friend-p range))
2815       (setq friend (nth 1 friend-or-range))
2816       (setq range (nth 0 friend-or-range)))
2817     (unless (or (null friend) (mixi-friend-p friend))
2818       (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
2819     (let* ((regexp (if friend mixi-introduction-list-regexp
2820                      mixi-my-introduction-list-regexp))
2821            (items (mixi-get-matched-items (mixi-introduction-list-page friend)
2822                                           regexp
2823                                           range)))
2824       (mapcar (lambda (item)
2825                 (mixi-make-introduction (or friend (mixi-make-me))
2826                                         (mixi-make-friend (nth 0 item)
2827                                                           (nth 1 item))
2828                                         (nth 2 item)))
2829               items))))
2830
2831 ;; News object.
2832 (defvar mixi-news-cache (make-hash-table :test 'equal))
2833 (defun mixi-make-news (media-id id &optional media time title content)
2834   "Return a news object."
2835   (mixi-make-cache (list media-id id)
2836                    (cons 'mixi-news (vector nil media-id id media time title
2837                                             content))
2838                    mixi-news-cache))
2839
2840 (defconst mixi-news-url-regexp
2841   "/view_news\\.pl\\?id=\\([0-9]+\\)&media_id=\\([0-9]+\\)")
2842
2843 (defun mixi-make-news-from-url (url)
2844   "Return a news object from URL."
2845   (when (string-match mixi-news-url-regexp url)
2846     (let ((id (match-string 1 url))
2847           (media-id (match-string 2 url)))
2848       (mixi-make-news media-id id))))
2849
2850 (defmacro mixi-news-p (news)
2851   `(eq (mixi-object-class ,news) 'mixi-news))
2852
2853 (defmacro mixi-news-page (news)
2854   `(concat "http://news.mixi.jp/view_news.pl?id=" (mixi-news-id ,news)
2855            "&media_id=" (mixi-news-media-id ,news)))
2856
2857 (defconst mixi-news-finished-regexp
2858   "<p class=\"supplement01\">¢¨¿½¤·Ìõ¤¢¤ê¤Þ¤»¤ó¤¬¡¢¤³¤Î¥Ë¥å¡¼¥¹¤Ï·ÇºÜ´ü´Ö¤¬½ªÎ»¤·¤¿¤«¡¢URL¤¬´Ö°ã¤Ã¤Æ¤¤¤ë¤¿¤á¤´Í÷¤¤¤¿¤À¤±¤Þ¤»¤ó¡£¾Ü¤·¤¯¤Ï<a href=\"http://mixi.jp/help.pl#16h\">¤³¤Á¤é</a>¤ò¤´Í÷¤¯¤À¤µ¤¤¡£</p>")
2859 (defconst mixi-news-title-regexp
2860   "<div class=\"articleH[ae][ae]ding\">
2861 <h2>\\(.+\\)</h2>")
2862 (defconst mixi-news-media-time-regexp
2863   "<p class=\"date\">¡Ê\\(.+\\) - \\([0-9]+\\)·î\\([0-9]+\\)Æü \\([0-9]+\\):\\([0-9]+\\)¡Ë</p>")
2864 (defconst mixi-news-content-regexp
2865   "<div class=\"article\">
2866 \\(.+\\)
2867 +
2868 \\(</div>
2869 </div>\\|<div class=\"additional\">\\)")
2870
2871 (defun mixi-realize-news (news)
2872   "Realize a NEWS."
2873   ;; FIXME: Check an expiration of cache?
2874   (unless (mixi-object-realized-p news)
2875     (with-mixi-retrieve (mixi-news-page news)
2876       (if (re-search-forward mixi-news-finished-regexp nil t)
2877           (mixi-news-set-content news (match-string 0))
2878         (if (re-search-forward mixi-news-title-regexp nil t)
2879             (mixi-news-set-title news (match-string 1))
2880           (mixi-realization-error 'cannot-find-title news))
2881         (if (re-search-forward mixi-news-media-time-regexp nil t)
2882             (progn
2883               (mixi-news-set-media news (match-string 1))
2884               (let ((year (nth 5 (decode-time (current-time))))
2885                     (month (nth 4 (decode-time (current-time))))
2886                     (month-of-item (string-to-number (match-string 2))))
2887                 (when (> month-of-item month)
2888                   (decf year))
2889                 (mixi-news-set-time
2890                  news (encode-time 0 (string-to-number (match-string 5))
2891                                    (string-to-number (match-string 4))
2892                                    (string-to-number (match-string 3))
2893                                    month year))))
2894           (mixi-realization-error 'cannot-find-media-time news))
2895         (if (re-search-forward mixi-news-content-regexp nil t)
2896             (mixi-news-set-content news (match-string 1))
2897           (mixi-realization-error 'cannot-find-content news))))
2898     (mixi-object-touch news)))
2899
2900 (defun mixi-news-media-id (news)
2901   "Return the media-id of NEWS."
2902   (unless (mixi-news-p news)
2903     (signal 'wrong-type-argument (list 'mixi-news-p news)))
2904   (aref (cdr news) 1))
2905
2906 (defun mixi-news-id (news)
2907   "Return the id of NEWS."
2908   (unless (mixi-news-p news)
2909     (signal 'wrong-type-argument (list 'mixi-news-p news)))
2910   (aref (cdr news) 2))
2911
2912 (defun mixi-news-media (news)
2913   "Return the media of NEWS."
2914   (unless (mixi-news-p news)
2915     (signal 'wrong-type-argument (list 'mixi-news-p news)))
2916   (unless (aref (cdr news) 3)
2917     (mixi-realize-news news))
2918   (aref (cdr news) 3))
2919
2920 (defun mixi-news-time (news)
2921   "Return the time of NEWS."
2922   (unless (mixi-news-p news)
2923     (signal 'wrong-type-argument (list 'mixi-news-p news)))
2924   (unless (aref (cdr news) 4)
2925     (mixi-realize-news news))
2926   (aref (cdr news) 4))
2927
2928 (defun mixi-news-title (news)
2929   "Return the title of NEWS."
2930   (unless (mixi-news-p news)
2931     (signal 'wrong-type-argument (list 'mixi-news-p news)))
2932   (unless (aref (cdr news) 5)
2933     (mixi-realize-news news))
2934   (aref (cdr news) 5))
2935
2936 (defun mixi-news-content (news)
2937   "Return the content of NEWS."
2938   (unless (mixi-news-p news)
2939     (signal 'wrong-type-argument (list 'mixi-news-p news)))
2940   (mixi-realize-news news)
2941   (aref (cdr news) 6))
2942
2943 (defun mixi-news-set-media (news media)
2944   "Set the media of NEWS."
2945   (unless (mixi-news-p news)
2946     (signal 'wrong-type-argument (list 'mixi-news-p news)))
2947   (aset (cdr news) 3 media))
2948
2949 (defun mixi-news-set-time (news time)
2950   "Set the time of NEWS."
2951   (unless (mixi-news-p news)
2952     (signal 'wrong-type-argument (list 'mixi-news-p news)))
2953   (aset (cdr news) 4 time))
2954
2955 (defun mixi-news-set-title (news title)
2956   "Set the title of NEWS."
2957   (unless (mixi-news-p news)
2958     (signal 'wrong-type-argument (list 'mixi-news-p news)))
2959   (aset (cdr news) 5 title))
2960
2961 (defun mixi-news-set-content (news content)
2962   "Set the content of NEWS."
2963   (unless (mixi-news-p news)
2964     (signal 'wrong-type-argument (list 'mixi-news-p news)))
2965   (aset (cdr news) 6 content))
2966
2967 (defconst mixi-news-category-list '(domestic politics economy area abroad
2968                                              sports entertainment IT game-anime
2969                                              column))
2970
2971 (defmacro mixi-news-category-p (category)
2972   `(memq ,category mixi-news-category-list))
2973
2974 (defun mixi-news-category-id (category)
2975   "Return the id of CATEGORY."
2976   (unless (mixi-news-category-p category)
2977     (signal 'wrong-type-argument (list 'mixi-news-category-p category)))
2978   (number-to-string
2979    (1+ (- (length mixi-news-category-list)
2980           (length (memq category mixi-news-category-list))))))
2981
2982 (defconst mixi-news-sort-list '(newest pickup))
2983
2984 (defmacro mixi-news-sort-p (sort)
2985   `(memq ,sort mixi-news-sort-list))
2986
2987 (defun mixi-news-sort-id (sort)
2988   "Return the id of SORT."
2989   (unless (mixi-news-sort-p sort)
2990     (signal 'wrong-type-argument (list 'mixi-news-sort-p sort)))
2991   (number-to-string
2992    (- (length mixi-news-sort-list)
2993       (length (memq sort mixi-news-sort-list)))))
2994
2995 (defmacro mixi-news-list-page (category sort)
2996   `(concat "http://news.mixi.jp/list_news_category.pl?page=%d"
2997            "&sort=" (mixi-news-sort-id ,sort)
2998            "&id=" (mixi-news-category-id ,category)
2999            "&type=bn"))
3000
3001 (defconst mixi-news-list-regexp
3002   "<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>
3003 <td class=\"media\"><a href=\"list_news_media\\.pl\\?id=[0-9]+\">\\(.+\\)</a></td>
3004 <td class=\"date\">\\([0-9]+\\)·î\\([0-9]+\\)Æü \\([0-9]+\\):\\([0-9]+\\)</td>")
3005
3006 ;;;###autoload
3007 (defun mixi-get-news (category sort &optional range)
3008   "Get news of CATEGORY and SORT."
3009   (unless (mixi-news-category-p category)
3010     (signal 'wrong-type-argument (list 'mixi-news-category-p category)))
3011   (unless (mixi-news-sort-p sort)
3012     (signal 'wrong-type-argument (list 'mixi-news-sort-p sort)))
3013   (let ((items (mixi-get-matched-items (mixi-news-list-page category sort)
3014                                        mixi-news-list-regexp
3015                                        range))
3016         (year (nth 5 (decode-time (current-time))))
3017         (month (nth 4 (decode-time (current-time)))))
3018     (mapcar (lambda (item)
3019               (let ((month-of-item (string-to-number (nth 4 item))))
3020                 (when (> month-of-item month)
3021                   (decf year))
3022                 (setq month month-of-item)
3023                 (mixi-make-news (nth 1 item) (nth 0 item) (nth 3 item)
3024                                 (encode-time
3025                                  0 (string-to-number (nth 7 item))
3026                                  (string-to-number (nth 6 item))
3027                                  (string-to-number (nth 5 item))
3028                                  month year)
3029                                 (nth 2 item))))
3030             items)))
3031
3032 ;; Release object.
3033 (defun mixi-make-release (title time content)
3034   "Return a release object."
3035   (cons 'mixi-release (vector title time content)))
3036
3037 (defmacro mixi-release-p (release)
3038   `(eq (mixi-object-class ,release) 'mixi-release))
3039
3040 (defun mixi-release-title (release)
3041   "Return the title of RELEASE."
3042   (unless (mixi-release-p release)
3043     (signal 'wrong-type-argument (list 'mixi-release-p release)))
3044   (aref (cdr release) 0))
3045
3046 (defun mixi-release-time (release)
3047   "Return the time of RELEASE."
3048   (unless (mixi-release-p release)
3049     (signal 'wrong-type-argument (list 'mixi-release-p release)))
3050   (aref (cdr release) 1))
3051
3052 (defun mixi-release-content (release)
3053   "Return the content of RELEASE."
3054   (unless (mixi-release-p release)
3055     (signal 'wrong-type-argument (list 'mixi-release-p release)))
3056   (aref (cdr release) 2))
3057
3058 (defmacro mixi-release-list-page ()
3059   `(concat "/release_info.pl?page=%d"))
3060
3061 (defconst mixi-release-list-regexp
3062   "<td>&nbsp;<span STYLE=\"font-size:8pt;color:#DFB479\">¢£</span>&nbsp;<b><font COLOR=#605048>\\(.+\\)</font></b></td>
3063 <td ALIGN=right><font COLOR=#605048>\\([1-9][0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)</font></td>
3064 </tr>
3065 </table>
3066 </td>
3067 </tr>
3068 </table>
3069
3070 <br>
3071
3072 <table BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH=450>
3073 <tr> 
3074 <td CLASS=h130>
3075 \\(.+\\)
3076 </td>
3077 </tr>
3078 </table>")
3079
3080 ;;;###autoload
3081 (defun mixi-get-releases (&optional range)
3082   "Get releases."
3083   (let ((items (mixi-get-matched-items (mixi-release-list-page)
3084                                        mixi-release-list-regexp
3085                                        range)))
3086     (mapcar (lambda (item)
3087               (mixi-make-release (nth 0 item)
3088                                  (encode-time 0 0 0
3089                                               (string-to-number (nth 3 item))
3090                                               (string-to-number (nth 2 item))
3091                                               (string-to-number (nth 1 item)))
3092                                  (nth 4 item)))
3093             items)))
3094
3095 ;; Echo object.
3096 (defvar mixi-echo-cache (make-hash-table :test 'equal))
3097 ;; FIXME: Support parent.
3098 (defun mixi-make-echo (owner post-time &optional content)
3099   "Return an echo object."
3100   (let ((owner (or owner (mixi-make-me))))
3101     (mixi-make-cache (list (mixi-friend-id owner) post-time)
3102                      (cons 'mixi-echo (vector nil owner post-time content))
3103                      mixi-echo-cache)))
3104
3105 (defconst mixi-echo-url-regexp
3106   "view_echo\\.pl\\?id=\\([0-9]+\\)&post_time=\\([0-9]+\\)")
3107
3108 (defun mixi-make-echo-from-url (url)
3109   "Return an echo object from URL."
3110   (when (string-match mixi-echo-url-regexp url)
3111     (let ((owner-id (match-string 1 url))
3112           (post-time (match-string 2 url)))
3113       (mixi-make-echo (mixi-make-friend owner-id) post-time))))
3114
3115 (defmacro mixi-echo-p (echo)
3116   `(eq (mixi-object-class ,echo) 'mixi-echo))
3117
3118 (defmacro mixi-echo-page (echo)
3119   `(concat "/view_echo.pl?id=" (mixi-friend-id (mixi-echo-owner ,echo))
3120            "&post_time=" (mixi-echo-post-time ,echo)))
3121
3122 (defconst mixi-echo-owner-nick-regexp
3123   "<div id=\"echo_nickname_[0-9]+\"  style=\"display: none;\">\\(.*\\)</div>")
3124 (defconst mixi-echo-content-regexp
3125   "<div id=\"echo_body_[0-9]+\"      style=\"display: none;\">\\(.+\\)</div>")
3126
3127 (defun mixi-realize-echo (echo)
3128   "Realize an ECHO."
3129   ;; FIXME: Check an expiration of cache?
3130   (unless (mixi-object-realized-p echo)
3131     (with-mixi-retrieve (mixi-echo-page echo)
3132       (let ((case-fold-search t))
3133         (if (re-search-forward mixi-echo-owner-nick-regexp nil t)
3134             (mixi-friend-set-nick (mixi-echo-owner echo)
3135                                   (match-string 1))
3136           (mixi-realization-error 'cannot-find-owner-nick echo))
3137         (if (re-search-forward mixi-echo-content-regexp nil t)
3138             (mixi-echo-set-content echo (match-string 1))
3139           (mixi-realization-error 'cannot-find-owner-nick echo))))
3140     (mixi-object-touch echo)))
3141
3142 (defun mixi-echo-owner (echo)
3143   "Return the owner of ECHO."
3144   (unless (mixi-echo-p echo)
3145     (signal 'wrong-type-argument (list 'mixi-echo-p echo)))
3146   (aref (cdr echo) 1))
3147
3148 (defun mixi-echo-post-time (echo)
3149   "Return the post-time of ECHO."
3150   (unless (mixi-echo-p echo)
3151     (signal 'wrong-type-argument (list 'mixi-echo-p echo)))
3152   (aref (cdr echo) 2))
3153
3154 (defun mixi-echo-content (echo)
3155   "Return the content of ECHO."
3156   (unless (mixi-echo-p echo)
3157     (signal 'wrong-type-argument (list 'mixi-echo-p echo)))
3158   (mixi-realize-echo echo)
3159   (aref (cdr echo) 3))
3160
3161 (defun mixi-echo-set-content (echo content)
3162   "Set the content of ECHO."
3163   (unless (mixi-echo-p echo)
3164     (signal 'wrong-type-argument (list 'mixi-echo-p echo)))
3165   (aset (cdr echo) 3 content))
3166
3167 (defmacro mixi-echo-list-page (&optional friend)
3168   `(concat "/list_echo.pl?page=%d"
3169            (when ,friend (concat "&id=" (mixi-friend-id ,friend)))))
3170
3171 (defconst mixi-echo-list-regexp
3172   "<a href=\"view_echo\\.pl\\?id=\\([0-9]+\\)&post_time=\\([0-9]+\\)\">")
3173
3174 ;;;###autoload
3175 (defun mixi-get-echoes (&rest friend-or-range)
3176   "Get echoes of FRIEND."
3177   (when (> (length friend-or-range) 2)
3178     (signal 'wrong-number-of-arguments
3179             (list 'mixi-get-echoes (length friend-or-range))))
3180   (let ((friend (nth 0 friend-or-range))
3181         (range (nth 1 friend-or-range)))
3182     (when (or (not (mixi-friend-p friend)) (mixi-friend-p range))
3183       (setq friend (nth 1 friend-or-range))
3184       (setq range (nth 0 friend-or-range)))
3185     (unless (or (null friend) (mixi-friend-p friend))
3186       (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
3187     (let ((items (mixi-get-matched-items (mixi-echo-list-page friend)
3188                                          mixi-echo-list-regexp
3189                                          range)))
3190       (mapcar (lambda (item)
3191                 (mixi-make-echo friend (nth 1 item)))
3192               items))))
3193
3194 (defmacro mixi-new-echo-list-page ()
3195   `(concat "/recent_echo.pl?page=%d"))
3196
3197 (defconst mixi-new-echo-list-regexp
3198   "<a href=\"view_echo\\.pl\\?id=\\([0-9]+\\)&post_time=\\([0-9]+\\)\">")
3199
3200 ;;;###autoload
3201 (defun mixi-get-new-echoes (&optional range)
3202   "Get new echoes."
3203   (let ((items (mixi-get-matched-items (mixi-new-echo-list-page)
3204                                        mixi-new-echo-list-regexp
3205                                        range)))
3206     (mapcar (lambda (item)
3207               (mixi-make-echo (mixi-make-friend (nth 0 item)) (nth 1 item)))
3208             items)))
3209
3210 (defmacro mixi-post-echo-page ()
3211   `(concat "/add_echo.pl"))
3212
3213 (defconst mixi-echo-post-succeed-regexp mixi-my-id-regexp)
3214
3215 ;;;###autoload
3216 (defun mixi-post-echo (content &optional parent)
3217   "Post an echo."
3218   (unless (stringp content)
3219     (signal 'wrong-type-argument (list 'stringp content)))
3220   (unless (or (null parent) (mixi-echo-p parent))
3221     (signal 'wrong-type-argument (list 'mixi-echo-p parent)))
3222   (let (fields post-key)
3223     (with-mixi-retrieve (mixi-post-echo-page)
3224       (if (re-search-forward mixi-post-key-regexp nil t)
3225           (setq post-key (match-string 1))
3226         (mixi-post-error 'cannot-find-key)))
3227     (setq fields `(("post_key" . ,post-key)
3228                    ("redirect" . "home")
3229                    ("body" . ,content)))
3230     (when (mixi-echo-p parent)
3231       (setq fields (cons `("parent_member_id" . ,(mixi-friend-id
3232                                                   (mixi-echo-owner parent)))
3233                          fields))
3234       (setq fields (cons `("parent_post_time" . ,(mixi-echo-post-time parent))
3235                          fields)))
3236     (with-mixi-post-form (mixi-post-echo-page) fields
3237       (unless (re-search-forward mixi-echo-post-succeed-regexp nil t)
3238         (mixi-post-error 'cannot-find-succeed)))))
3239
3240 (provide 'mixi)
3241
3242 ;;; mixi.el ends here