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