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