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