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