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