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