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