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