646b9963b602c83006e4e8a039d1ecab423da424
[elisp/mixi.git] / mixi.el
1 ;; mixi.el --- API libraries 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 ;; APIs for getting contents:
28 ;;
29 ;;  * mixi-get-friends
30 ;;  * mixi-get-favorites
31 ;;  * mixi-get-logs
32 ;;  * mixi-get-diaries
33 ;;  * mixi-get-new-diaries
34 ;;  * mixi-search-diaries
35 ;;  * mixi-get-communities
36 ;;  * mixi-search-communities
37 ;;  * mixi-get-bbses
38 ;;  * mixi-get-new-bbses
39 ;;  * mixi-search-bbses
40 ;;  * mixi-get-comments
41 ;;  * mixi-get-new-comments
42 ;;  * mixi-get-messages
43 ;;  * mixi-get-introductions
44 ;;  * mixi-get-news
45 ;;
46 ;; APIs for posting:
47 ;;
48 ;;  * mixi-post-diary
49 ;;  * mixi-post-topic
50 ;;  * mixi-post-comment
51 ;;  * mixi-post-message
52 ;; 
53 ;; Utilities:
54 ;;
55 ;;  * mixi-remove-markup
56
57 ;; Examples:
58 ;;
59 ;; Display newest 3 diaries like a mail format.
60 ;;
61 ;; (let ((range 3)
62 ;;       (buffer (get-buffer-create "*temp*"))
63 ;;       (format "%Y/%m/%d %H:%M"))
64 ;;   (pop-to-buffer buffer)
65 ;;   (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 files.
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         (cond ((string= name "bbs")
613                (setq name "topic"))
614               ((string= name "profile")
615                (setq name "friend")))
616         (let ((func (intern (concat mixi-object-prefix "make-" name
617                                     "-from-url"))))
618           (funcall func url)))
619     (when (string-match "/home\\.pl" url)
620       (mixi-make-friend-from-url url))))
621
622 ;; Cache.
623 ;; stolen from time-date.el
624 (defun mixi-time-less-p (t1 t2)
625   "Say whether time value T1 is less than time value T2."
626   (unless (numberp (cdr t1))
627     (setq t1 (cons (car t1) (car (cdr t1)))))
628   (unless (numberp (cdr t2))
629     (setq t2 (cons (car t2) (car (cdr t2)))))
630   (or (< (car t1) (car t2))
631       (and (= (car t1) (car t2))
632            (< (cdr t1) (cdr t2)))))
633
634 (defun mixi-time-add (t1 t2)
635   "Add two time values.  One should represent a time difference."
636   (unless (numberp (cdr t1))
637     (setq t1 (cons (car t1) (car (cdr t1)))))
638   (unless (numberp (cdr t2))
639     (setq t2 (cons (car t2) (car (cdr t2)))))
640   (let ((low (+ (cdr t1) (cdr t2))))
641     (cons (+ (car t1) (car t2) (lsh low -16)) low)))
642
643 ;; stolen from time-date.el
644 (defun mixi-seconds-to-time (seconds)
645   "Convert SECONDS (a floating point number) to a time value."
646   (cons (floor seconds 65536)
647         (floor (mod seconds 65536))))
648
649 (defun mixi-cache-expired-p (object)
650   "Whether a cache of OBJECT is expired."
651   (let ((timestamp (mixi-object-timestamp object)))
652     (unless (or (null mixi-cache-expires)
653                  (null timestamp))
654       (if (numberp mixi-cache-expires)
655           (mixi-time-less-p
656            (mixi-time-add timestamp (mixi-seconds-to-time mixi-cache-expires))
657            (current-time))
658         t))))
659
660 (defun mixi-make-cache (key value table)
661   "Make a cache object and return it."
662   (let ((cache (gethash key table)))
663     (if (and cache (not (mixi-cache-expired-p cache)))
664         cache
665       (puthash key value table))))
666
667 (defconst mixi-cache-file-regexp "[a-z]+-cache$")
668 (defconst mixi-cache-regexp (concat mixi-object-prefix
669                                     mixi-cache-file-regexp))
670
671 (defun mixi-save-cache ()
672   (let ((cache-directory (expand-file-name "cache" mixi-directory)))
673     (unless (file-directory-p cache-directory)
674       (make-directory cache-directory t))
675     (let ((caches (apropos-internal mixi-cache-regexp 'boundp)))
676       (mapc (lambda (symbol)
677               (with-temp-file (expand-file-name
678                                (substring (symbol-name symbol)
679                                           (length mixi-object-prefix))
680                                cache-directory)
681                 (let ((coding-system-for-write mixi-coding-system)
682                       (cache (symbol-value symbol)))
683                   (insert "#s(hash-table size "
684                           (number-to-string (hash-table-count cache))
685                           " test equal data (\n")
686                   (maphash
687                    (lambda (key value)
688                      (let (print-level print-length)
689                        (insert (prin1-to-string key) " "
690                                (prin1-to-string value) "\n")))
691                    cache))
692                 (insert "))")))
693             caches))))
694
695 ;; stolen (and modified) from lsdb.el
696 (defun mixi-read-cache (&optional marker)
697   "Read one Lisp expression as text from MARKER, return as Lisp object."
698   (save-excursion
699     (goto-char marker)
700     (if (looking-at "^#s(")
701         (let ((end-marker
702                (progn
703                  (forward-char 2);skip "#s"
704                  (forward-sexp);move to the left paren
705                  (point-marker))))
706           (with-temp-buffer
707             (buffer-disable-undo)
708             (insert-buffer-substring (marker-buffer marker)
709                                      marker end-marker)
710             (goto-char (point-min))
711             (delete-char 2)
712             (let ((object (read (current-buffer)))
713                   data)
714               (if (eq 'hash-table (car object))
715                   (progn
716                     (setq data (plist-get (cdr object) 'data))
717                     (while data
718                       (pop data);throw it away
719                       (mixi-read-object (pop data))))
720                 object))))
721       (read marker))))
722
723 (defun mixi-load-cache ()
724   (let ((cache-directory (expand-file-name "cache" mixi-directory)))
725     (when (file-directory-p cache-directory)
726       ;; FIXME: Load friend and community first.
727       (let ((files (directory-files cache-directory t
728                                     mixi-cache-file-regexp)))
729         (mapc (lambda (file)
730                 (let ((buffer (find-file-noselect file)))
731                   (unwind-protect
732                       (save-excursion
733                         (set-buffer buffer)
734                         (goto-char (point-min))
735                         (re-search-forward "^#s(")
736                         (goto-char (match-beginning 0))
737                         (mixi-read-cache (point-marker)))
738                     (kill-buffer buffer))))
739               files)))))
740
741 ;; Friend object.
742 (defvar mixi-friend-cache (make-hash-table :test 'equal))
743 (defun mixi-make-friend (id &optional nick name sex address age birthday
744                             blood-type birthplace hobby job organization
745                             profile)
746   "Return a friend object."
747   (mixi-make-cache id (cons 'mixi-friend (vector nil id nick name sex address
748                                                  age birthday blood-type
749                                                  birthplace hobby job
750                                                  organization profile))
751                    mixi-friend-cache))
752
753 (defun mixi-make-me ()
754   "Return a my object."
755   (unless mixi-me
756     (with-mixi-retrieve "/home.pl"
757       (if (re-search-forward mixi-my-id-regexp)
758           (setq mixi-me (mixi-make-friend (match-string 1)))
759         (signal 'error (list 'who-am-i)))))
760   mixi-me)
761
762 (defconst mixi-friend-url-regexp
763   "/show_\\(friend\\|profile\\)\\.pl\\?id=\\([0-9]+\\)")
764
765 (defun mixi-make-friend-from-url (url)
766   "Return a friend object from URL."
767   (if (string-match mixi-friend-url-regexp url)
768       (let ((id (match-string 2 url)))
769         (mixi-make-friend id))
770     (when (string-match "/home\\.pl" url)
771       (mixi-make-me))))
772
773 (defmacro mixi-friend-p (friend)
774   `(eq (mixi-object-class ,friend) 'mixi-friend))
775
776 (defmacro mixi-friend-page (friend)
777   `(concat "/show_profile.pl?id=" (mixi-friend-id ,friend)))
778
779 (defconst mixi-friend-nick-regexp
780   "<img \\(alt=\"\\*\" \\)?src=\"?http://img\\.mixi\\.jp/img/dot0\\.gif\"? width=\"?1\"? height=\"?5\"?\\( /\\)?><br\\( /\\)?>\r?
781 \\(.*\\)¤µ¤ó([0-9]+)")
782 (defconst mixi-friend-name-regexp
783   "̾\\(&nbsp;\\| \\)Á°</font></td>
784
785 ?<td width=\"?345\"?>\\(.+?\\)\\(</td>\\| <img\\)")
786 (defconst mixi-friend-sex-regexp
787   "À­\\(&nbsp;\\| \\)ÊÌ</font></td>
788
789 ?<td width=\"?345\"?>\\([Ã˽÷]\\)À­\\(</td>\\| <img\\)")
790 (defconst mixi-friend-address-regexp
791   "¸½½»½ê</font></td>
792 <td>\\(.+?\\)\\(</td>\\| <img\\)")
793 (defconst mixi-friend-age-regexp
794   "ǯ\\(&nbsp;\\| \\)Îð</font></td>\n<td>\\([0-9]+\\)ºÐ\\(</td>\\| <img\\)")
795 (defconst mixi-friend-birthday-regexp
796   "ÃÂÀ¸Æü</font></td>\n<td>\\([0-9]+\\)·î\\([0-9]+\\)Æü\\(</td>\\| <img\\)")
797 (defconst mixi-friend-blood-type-regexp
798   "·ì±Õ·¿</font></td>\n<td>\\([ABO]B?\\)·¿\\(</td>\\| <img\\)")
799 (defconst mixi-friend-birthplace-regexp
800   "½Ð¿ÈÃÏ</font>\n?</td>\n<td>\\(.+?\\)\\(</td>\\| <img\\)")
801 (defconst mixi-friend-hobby-regexp
802   "¼ñ\\(&nbsp;\\| \\)Ì£</font></td>\n<td>\\(.+?\\)\\(</td>\\| <img\\)")
803 (defconst mixi-friend-job-regexp
804   "¿¦\\(&nbsp;\\| \\)¶È</font></td>\n<td>\\(.+?\\)\\(</td>\\| <img\\)")
805 (defconst mixi-friend-organization-regexp
806   "½ê\\(&nbsp;\\| \\)°</font></td>\n<td[^>]*>\\(.+?\\)\\(</td>\\| <img\\)")
807 (defconst mixi-friend-profile-regexp
808   "¼«¸Ê¾Ò²ð</font></td>
809 <td class=\"?h120\"?>\\(.+\\)</td></tr>")
810
811 (defun mixi-realize-friend (friend)
812   "Realize a FRIEND."
813   ;; FIXME: Check a expiration of cache?
814   (unless (mixi-object-realized-p friend)
815     (with-mixi-retrieve (mixi-friend-page friend)
816       (let ((case-fold-search t))
817         (if (re-search-forward mixi-friend-nick-regexp nil t)
818             (mixi-friend-set-nick friend (match-string 4))
819           (mixi-realization-error 'cannot-find-nick friend))
820         (when (re-search-forward mixi-friend-name-regexp nil t)
821           (mixi-friend-set-name friend (match-string 2)))
822         (when (re-search-forward mixi-friend-sex-regexp nil t)
823           (mixi-friend-set-sex friend (if (string= (match-string 2) "ÃË")
824                                           'male 'female)))
825         (when (re-search-forward mixi-friend-address-regexp nil t)
826           (mixi-friend-set-address friend (match-string 1)))
827         (when (re-search-forward mixi-friend-age-regexp nil t)
828           (mixi-friend-set-age friend (string-to-number (match-string 2))))
829         (when (re-search-forward mixi-friend-birthday-regexp nil t)
830           (mixi-friend-set-birthday
831            friend (list (string-to-number (match-string 1))
832                         (string-to-number (match-string 2)))))
833         (when (re-search-forward mixi-friend-blood-type-regexp nil t)
834           (mixi-friend-set-blood-type friend (intern (match-string 1))))
835         (when (re-search-forward mixi-friend-birthplace-regexp nil t)
836           (mixi-friend-set-birthplace friend (match-string 1)))
837         (when (re-search-forward mixi-friend-hobby-regexp nil t)
838           (mixi-friend-set-hobby friend (split-string (match-string 2) ", ")))
839         (when (re-search-forward mixi-friend-job-regexp nil t)
840           (mixi-friend-set-job friend (match-string 2)))
841         (when (re-search-forward mixi-friend-organization-regexp nil t)
842           (mixi-friend-set-organization friend (match-string 2)))
843         (when (re-search-forward mixi-friend-profile-regexp nil t)
844           (mixi-friend-set-profile friend (match-string 1)))))
845     (mixi-object-touch friend)))
846
847 (defun mixi-friend-id (friend)
848   "Return the id of FRIEND."
849   (unless (mixi-friend-p friend)
850     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
851   (aref (cdr friend) 1))
852
853 (defun mixi-friend-nick (friend)
854   "Return the nick of FRIEND."
855   (unless (mixi-friend-p friend)
856     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
857   (unless (aref (cdr friend) 2)
858     (mixi-realize-friend friend))
859   (aref (cdr friend) 2))
860
861 (defun mixi-friend-name (friend)
862   "Return the name of FRIEND."
863   (unless (mixi-friend-p friend)
864     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
865   (mixi-realize-friend friend)
866   (aref (cdr friend) 3))
867
868 (defun mixi-friend-sex (friend)
869   "Return the sex of FRIEND."
870   (unless (mixi-friend-p friend)
871     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
872   (mixi-realize-friend friend)
873   (aref (cdr friend) 4))
874
875 (defun mixi-friend-address (friend)
876   "Return the address of FRIEND."
877   (unless (mixi-friend-p friend)
878     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
879   (mixi-realize-friend friend)
880   (aref (cdr friend) 5))
881
882 (defun mixi-friend-age (friend)
883   "Return the age of FRIEND."
884   (unless (mixi-friend-p friend)
885     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
886   (mixi-realize-friend friend)
887   (aref (cdr friend) 6))
888
889 (defun mixi-friend-birthday (friend)
890   "Return the birthday of FRIEND."
891   (unless (mixi-friend-p friend)
892     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
893   (mixi-realize-friend friend)
894   (aref (cdr friend) 7))
895
896 (defun mixi-friend-blood-type (friend)
897   "Return the blood type of FRIEND."
898   (unless (mixi-friend-p friend)
899     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
900   (mixi-realize-friend friend)
901   (aref (cdr friend) 8))
902
903 (defun mixi-friend-birthplace (friend)
904   "Return the birthplace of FRIEND."
905   (unless (mixi-friend-p friend)
906     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
907   (mixi-realize-friend friend)
908   (aref (cdr friend) 9))
909
910 (defun mixi-friend-hobby (friend)
911   "Return the hobby of FRIEND."
912   (unless (mixi-friend-p friend)
913     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
914   (mixi-realize-friend friend)
915   (aref (cdr friend) 10))
916
917 (defun mixi-friend-job (friend)
918   "Return the job of FRIEND."
919   (unless (mixi-friend-p friend)
920     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
921   (mixi-realize-friend friend)
922   (aref (cdr friend) 11))
923
924 (defun mixi-friend-organization (friend)
925   "Return the organization of FRIEND."
926   (unless (mixi-friend-p friend)
927     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
928   (mixi-realize-friend friend)
929   (aref (cdr friend) 12))
930
931 (defun mixi-friend-profile (friend)
932   "Return the pforile of FRIEND."
933   (unless (mixi-friend-p friend)
934     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
935   (mixi-realize-friend friend)
936   (aref (cdr friend) 13))
937
938 (defun mixi-friend-set-nick (friend nick)
939   "Set the nick of FRIEND."
940   (unless (mixi-friend-p friend)
941     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
942   (aset (cdr friend) 2 nick))
943
944 (defun mixi-friend-set-name (friend name)
945   "Set the name of FRIEND."
946   (unless (mixi-friend-p friend)
947     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
948   (aset (cdr friend) 3 name))
949
950 (defun mixi-friend-set-sex (friend sex)
951   "Set the sex of FRIEND."
952   (unless (mixi-friend-p friend)
953     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
954   (aset (cdr friend) 4 sex))
955
956 (defun mixi-friend-set-address (friend address)
957   "Set the address of FRIEND."
958   (unless (mixi-friend-p friend)
959     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
960   (aset (cdr friend) 5 address))
961
962 (defun mixi-friend-set-age (friend age)
963   "Set the age of FRIEND."
964   (unless (mixi-friend-p friend)
965     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
966   (aset (cdr friend) 6 age))
967
968 (defun mixi-friend-set-birthday (friend birthday)
969   "Set the birthday of FRIEND."
970   (unless (mixi-friend-p friend)
971     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
972   (aset (cdr friend) 7 birthday))
973
974 (defun mixi-friend-set-blood-type (friend blood-type)
975   "Set the blood type of FRIEND."
976   (unless (mixi-friend-p friend)
977     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
978   (aset (cdr friend) 8 blood-type))
979
980 (defun mixi-friend-set-birthplace (friend birthplace)
981   "Set the birthplace of FRIEND."
982   (unless (mixi-friend-p friend)
983     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
984   (aset (cdr friend) 9 birthplace))
985
986 (defun mixi-friend-set-hobby (friend hobby)
987   "Set the hobby of FRIEND."
988   (unless (mixi-friend-p friend)
989     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
990   (aset (cdr friend) 10 hobby))
991
992 (defun mixi-friend-set-job (friend job)
993   "Set the job of FRIEND."
994   (unless (mixi-friend-p friend)
995     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
996   (aset (cdr friend) 11 job))
997
998 (defun mixi-friend-set-organization (friend organization)
999   "Set the organization of FRIEND."
1000   (unless (mixi-friend-p friend)
1001     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
1002   (aset (cdr friend) 12 organization))
1003
1004 (defun mixi-friend-set-profile (friend profile)
1005   "Set the profile of FRIEND."
1006   (unless (mixi-friend-p friend)
1007     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
1008   (aset (cdr friend) 13 profile))
1009
1010 (defmacro mixi-friend-list-page (&optional friend)
1011   `(concat "/list_friend.pl?page=%d"
1012            (when ,friend (concat "&id=" (mixi-friend-id ,friend)))))
1013
1014 (defconst mixi-friend-list-id-regexp
1015   "<a href=show_friend\\.pl\\?id=\\([0-9]+\\)")
1016 (defconst mixi-friend-list-nick-regexp
1017   "<td valign=middle>\\(.+\\)¤µ¤ó([0-9]+)<br />")
1018
1019 (defun mixi-get-friends (&rest friend-or-range)
1020   "Get friends of FRIEND."
1021   (when (> (length friend-or-range) 2)
1022     (signal 'wrong-number-of-arguments (list 'mixi-get-friends
1023                                              (length friend-or-range))))
1024   (let ((friend (nth 0 friend-or-range))
1025         (range (nth 1 friend-or-range)))
1026     (when (or (not (mixi-friend-p friend)) (mixi-friend-p range))
1027       (setq friend (nth 1 friend-or-range))
1028       (setq range (nth 0 friend-or-range)))
1029     (unless (or (null friend) (mixi-friend-p friend))
1030       (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
1031     (let ((ids (mixi-get-matched-items (mixi-friend-list-page friend)
1032                                        mixi-friend-list-id-regexp
1033                                        range))
1034           (nicks (mixi-get-matched-items (mixi-friend-list-page friend)
1035                                          mixi-friend-list-nick-regexp
1036                                          range)))
1037       (let ((index 0)
1038             ret)
1039         (while (< index (length ids))
1040           (setq ret (cons (mixi-make-friend (nth 0 (nth index ids))
1041                                             (nth 0 (nth index nicks))) ret))
1042           (incf index))
1043         (reverse ret)))))
1044
1045 ;; Favorite.
1046 (defmacro mixi-favorite-list-page ()
1047   `(concat "/list_bookmark.pl?page=%d"))
1048
1049 (defconst mixi-favorite-list-id-regexp
1050   "<td ALIGN=center BGCOLOR=#FDF9F2 width=330><a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">")
1051 (defconst mixi-favorite-list-nick-regexp
1052   "<td BGCOLOR=#FDF9F2><font COLOR=#996600>̾&nbsp;&nbsp;Á°</font></td>
1053 <td COLSPAN=2 BGCOLOR=#FFFFFF>\\(.+\\)</td></tr>")
1054
1055 (defun mixi-get-favorites (&optional range)
1056   "Get favorites."
1057   (let ((ids (mixi-get-matched-items (mixi-favorite-list-page)
1058                                      mixi-favorite-list-id-regexp
1059                                      range))
1060         (nicks (mixi-get-matched-items (mixi-favorite-list-page)
1061                                        mixi-favorite-list-nick-regexp
1062                                        range)))
1063     (let ((index 0)
1064           ret)
1065       (while (< index (length ids))
1066         (setq ret (cons (mixi-make-friend (nth 0 (nth index ids))
1067                                           (nth 0 (nth index nicks))) ret))
1068         (incf index))
1069       (reverse ret))))
1070
1071 ;; Log object.
1072 (defun mixi-make-log (friend time)
1073   "Return a log object."
1074   (cons 'mixi-log (vector friend time)))
1075
1076 (defmacro mixi-log-p (log)
1077   `(eq (mixi-object-class ,log) 'mixi-log))
1078
1079 (defun mixi-log-friend (log)
1080   "Return the friend of LOG."
1081   (unless (mixi-log-p log)
1082     (signal 'wrong-type-argument (list 'mixi-log-p log)))
1083   (aref (cdr log) 0))
1084
1085 (defun mixi-log-time (log)
1086   "Return the time of LOG."
1087   (unless (mixi-log-p log)
1088     (signal 'wrong-type-argument (list 'mixi-log-p log)))
1089   (aref (cdr log) 1))
1090
1091 (defmacro mixi-log-list-page ()
1092   `(concat "/show_log.pl"))
1093
1094 (defconst mixi-log-list-regexp
1095   "\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü \\([0-9]+\\):\\([0-9]+\\) <a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.*\\)</a></li>")
1096
1097 (defun mixi-get-logs (&optional range)
1098   "Get logs."
1099   (let ((items (mixi-get-matched-items (mixi-log-list-page)
1100                                        mixi-log-list-regexp
1101                                        range)))
1102     (mapcar (lambda (item)
1103               (mixi-make-log (mixi-make-friend (nth 5 item) (nth 6 item))
1104                              (encode-time 0
1105                                           (string-to-number (nth 4 item))
1106                                           (string-to-number (nth 3 item))
1107                                           (string-to-number (nth 2 item))
1108                                           (string-to-number (nth 1 item))
1109                                           (string-to-number (nth 0 item)))))
1110             items)))
1111
1112 ;; Diary object.
1113 (defvar mixi-diary-cache (make-hash-table :test 'equal))
1114 (defun mixi-make-diary (owner id &optional comment-count time title content)
1115   "Return a diary object."
1116   (let ((owner (or owner (mixi-make-me))))
1117     (mixi-make-cache (list (mixi-friend-id owner) id)
1118                      (cons 'mixi-diary (vector nil owner id comment-count time
1119                                                title content))
1120                      mixi-diary-cache)))
1121
1122 (defconst mixi-diary-url-regexp
1123   "/view_diary\\.pl\\?id=\\([0-9]+\\)&owner_id=\\([0-9]+\\)\\(&comment_count=\\([0-9]+\\)\\)?")
1124
1125 (defun mixi-make-diary-from-url (url)
1126   "Return a diary object from URL."
1127   (when (string-match mixi-diary-url-regexp url)
1128     (let ((id (match-string 1 url))
1129           (owner-id (match-string 2 url))
1130           (comment-count (match-string 4 url)))
1131       (mixi-make-diary (mixi-make-friend owner-id) id comment-count))))
1132
1133 (defmacro mixi-diary-p (diary)
1134   `(eq (mixi-object-class ,diary) 'mixi-diary))
1135
1136 (defmacro mixi-diary-page (diary)
1137   `(concat "/view_diary.pl?id=" (mixi-diary-id ,diary)
1138            "&owner_id=" (mixi-friend-id (mixi-diary-owner ,diary))))
1139
1140 (defconst mixi-diary-closed-regexp
1141   "<td>ͧ¿Í\\(¤Îͧ¿Í\\)?¤Þ¤Ç¸ø³«¤Î¤¿¤áÆɤळ¤È¤¬½ÐÍè¤Þ¤»¤ó¡£</td></tr>")
1142 (defconst mixi-diary-owner-nick-regexp
1143   "<td WIDTH=490 background=http://img\\.mixi\\.jp/img/bg_w\\.gif><b><font COLOR=#605048>\\(.+?\\)\\(¤µ¤ó\\)?¤ÎÆüµ­</font></b></td>")
1144 (defconst mixi-diary-time-regexp
1145   "<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>")
1146 (defconst mixi-diary-title-regexp
1147   "<td \\(bgcolor\\|BGCOLOR\\)=\"?#FFF4E0\"? width=\"?430\"?>&nbsp;\\([^<]+\\)</td>")
1148 (defconst mixi-diary-content-regexp
1149   "<td \\(class\\|CLASS\\)=\"?h12\"?>\\(.*\\)</td>")
1150
1151 (defun mixi-realize-diary (diary &optional page)
1152   "Realize a DIARY."
1153   ;; FIXME: Check a expiration of cache?
1154   (unless (mixi-object-realized-p diary)
1155     (with-mixi-retrieve (or page (mixi-diary-page diary))
1156       (unless (re-search-forward mixi-diary-closed-regexp nil t)
1157         (if (re-search-forward mixi-diary-owner-nick-regexp nil t)
1158             (mixi-friend-set-nick (mixi-diary-owner diary) (match-string 1))
1159           (mixi-realization-error 'cannot-find-owner-nick diary))
1160         (if (re-search-forward mixi-diary-time-regexp nil t)
1161             (mixi-diary-set-time
1162              diary (encode-time 0 (string-to-number (match-string 10))
1163                                 (string-to-number (match-string 9))
1164                                 (string-to-number (match-string 7))
1165                                 (string-to-number (match-string 6))
1166                                 (string-to-number (match-string 5))))
1167           (mixi-realization-error 'cannot-find-time diary))
1168         (if (re-search-forward mixi-diary-title-regexp nil t)
1169             (mixi-diary-set-title diary (match-string 2))
1170           (mixi-realization-error 'cannot-find-title diary))
1171         (if (re-search-forward mixi-diary-content-regexp nil t)
1172             (mixi-diary-set-content diary (match-string 2))
1173           (mixi-realization-error 'cannot-find-content diary))))
1174     (mixi-object-touch diary)))
1175
1176 (defun mixi-diary-owner (diary)
1177   "Return the owner of DIARY."
1178   (unless (mixi-diary-p diary)
1179     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1180   (aref (cdr diary) 1))
1181
1182 (defun mixi-diary-id (diary)
1183   "Return the id of DIARY."
1184   (unless (mixi-diary-p diary)
1185     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1186   (aref (cdr diary) 2))
1187
1188 (defun mixi-diary-comment-count (diary)
1189   "Return the comment-count of DIARY."
1190   (unless (mixi-diary-p diary)
1191     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1192   (aref (cdr diary) 3))
1193
1194 (defun mixi-diary-time (diary)
1195   "Return the time of DIARY."
1196   (unless (mixi-diary-p diary)
1197     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1198   (unless (aref (cdr diary) 3)
1199     (mixi-realize-diary diary))
1200   (aref (cdr diary) 4))
1201
1202 (defun mixi-diary-title (diary)
1203   "Return the title of DIARY."
1204   (unless (mixi-diary-p diary)
1205     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1206   (unless (aref (cdr diary) 4)
1207     (mixi-realize-diary diary))
1208   (aref (cdr diary) 5))
1209
1210 (defun mixi-diary-content (diary)
1211   "Return the content of DIARY."
1212   (unless (mixi-diary-p diary)
1213     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1214   (mixi-realize-diary diary)
1215   (aref (cdr diary) 6))
1216
1217 (defun mixi-diary-set-comment-count (diary comment-count)
1218   "Set the comment-count of DIARY."
1219   (unless (mixi-diary-p diary)
1220     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1221   (aset (cdr diary) 3 comment-count))
1222
1223 (defun mixi-diary-set-time (diary time)
1224   "Set the time of DIARY."
1225   (unless (mixi-diary-p diary)
1226     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1227   (aset (cdr diary) 4 time))
1228
1229 (defun mixi-diary-set-title (diary title)
1230   "Set the title of DIARY."
1231   (unless (mixi-diary-p diary)
1232     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1233   (aset (cdr diary) 5 title))
1234
1235 (defun mixi-diary-set-content (diary content)
1236   "Set the content of DIARY."
1237   (unless (mixi-diary-p diary)
1238     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1239   (aset (cdr diary) 6 content))
1240
1241 (defmacro mixi-diary-list-page (&optional friend)
1242   `(concat "/list_diary.pl?page=%d"
1243            (when ,friend (concat "&id=" (mixi-friend-id ,friend)))))
1244
1245 (defconst mixi-diary-list-regexp
1246   "<tr VALIGN=top>
1247 <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>
1248 <td bgcolor=\"#FFF4E0\">&nbsp;<a href=\"view_diary\\.pl\\?id=\\([0-9]+\\)&owner_id=[0-9]+\">\\(.*\\)</a></td>")
1249
1250 (defun mixi-get-diaries (&rest friend-or-range)
1251   "Get diaries of FRIEND."
1252   (when (> (length friend-or-range) 2)
1253     (signal 'wrong-number-of-arguments
1254             (list 'mixi-get-diaries (length friend-or-range))))
1255   (let ((friend (nth 0 friend-or-range))
1256         (range (nth 1 friend-or-range)))
1257     (when (or (not (mixi-friend-p friend)) (mixi-friend-p range))
1258       (setq friend (nth 1 friend-or-range))
1259       (setq range (nth 0 friend-or-range)))
1260     (unless (or (null friend) (mixi-friend-p friend))
1261       (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
1262     (let ((items (mixi-get-matched-items (mixi-diary-list-page friend)
1263                                          mixi-diary-list-regexp
1264                                          range))
1265           (year (nth 5 (decode-time (current-time))))
1266           (month (nth 4 (decode-time (current-time)))))
1267       (mapcar (lambda (item)
1268                 (let ((month-of-item (string-to-number (nth 0 item))))
1269                   (when (> month-of-item month)
1270                     (decf year))
1271                   (setq month month-of-item)
1272                   (mixi-make-diary friend (nth 5 item) nil
1273                                    (encode-time
1274                                     0 (string-to-number (nth 3 item))
1275                                     (string-to-number (nth 2 item))
1276                                     (string-to-number (nth 1 item))
1277                                     month year)
1278                                    (nth 6 item))))
1279               items))))
1280
1281 (defmacro mixi-new-diary-list-page ()
1282   `(concat "/new_friend_diary.pl?page=%d"))
1283
1284 (defconst mixi-new-diary-list-regexp
1285   "<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>
1286 <td WIDTH=450><a class=\"new_link\" href=view_diary\\.pl\\?id=\\([0-9]+\\)&owner_id=\\([0-9]+\\)>\\(.+\\)</a> (\\(.*\\)) ")
1287
1288 (defun mixi-get-new-diaries (&optional range)
1289   "Get new diaries."
1290   (let ((items (mixi-get-matched-items (mixi-new-diary-list-page)
1291                                        mixi-new-diary-list-regexp
1292                                        range)))
1293     (mapcar (lambda (item)
1294               (mixi-make-diary (mixi-make-friend (nth 6 item) (nth 8 item))
1295                                (nth 5 item)
1296                                nil
1297                                (encode-time
1298                                 0 (string-to-number (nth 4 item))
1299                                 (string-to-number (nth 3 item))
1300                                 (string-to-number (nth 2 item))
1301                                 (string-to-number (nth 1 item))
1302                                 (string-to-number (nth 0 item)))
1303                                (nth 7 item)))
1304             items)))
1305
1306 (defmacro mixi-search-diary-list-page (keyword)
1307   `(concat "/search_diary.pl?page=%d&submit=search&keyword="
1308              (mixi-url-encode-and-quote-percent-string ,keyword)))
1309
1310 (defconst mixi-search-diary-list-regexp
1311   "<td BGCOLOR=#FDF9F2><font COLOR=#996600>̾&nbsp;&nbsp;Á°</font></td>
1312 <td COLSPAN=2 BGCOLOR=#FFFFFF>\\(.*\\)
1313
1314 </td></tr>
1315
1316 <tr>
1317 <td BGCOLOR=#FDF9F2><font COLOR=#996600>¥¿¥¤¥È¥ë</font></td>
1318 <td COLSPAN=2 BGCOLOR=#FFFFFF>\\(.+\\)</td></tr>
1319
1320 <tr>
1321 <td BGCOLOR=#FDF9F2><font COLOR=#996600>ËÜ&nbsp;&nbsp;ʸ</font></td>
1322 <td COLSPAN=2 BGCOLOR=#FFFFFF>\\(.*\\)</td></tr>
1323
1324
1325 <tr>
1326 <td NOWRAP BGCOLOR=#FDF9F2 WIDTH=80><font COLOR=#996600>ºîÀ®Æü»þ</font></td>
1327 <td BGCOLOR=#FFFFFF WIDTH=220>\\([0-9]+\\)·î\\([0-9]+\\)Æü \\([0-9]+\\):\\([0-9]+\\)</td>
1328 <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>
1329 </table>
1330 </td></tr></table>")
1331
1332 (defun mixi-search-diaries (keyword &optional range)
1333   (let ((items (mixi-get-matched-items (mixi-search-diary-list-page keyword)
1334                                        mixi-search-diary-list-regexp
1335                                        range))
1336         (year (nth 5 (decode-time (current-time))))
1337         (month (nth 4 (decode-time (current-time)))))
1338     (mapcar (lambda (item)
1339                 (let ((month-of-item (string-to-number (nth 3 item))))
1340                   (when (> month-of-item month)
1341                     (decf year))
1342                   (setq month month-of-item)
1343                   (mixi-make-diary (mixi-make-friend (nth 8 item) (nth 0 item))
1344                                    (nth 7 item)
1345                                    nil
1346                                    (encode-time
1347                                     0 (string-to-number (nth 6 item))
1348                                     (string-to-number (nth 5 item))
1349                                     (string-to-number (nth 4 item))
1350                                     month year)
1351                                    (nth 1 item)
1352                                    (nth 2 item))))
1353             items)))
1354
1355 (defmacro mixi-post-diary-page ()
1356   `(concat "/add_diary.pl"))
1357
1358 (defconst mixi-post-key-regexp
1359   "<input type=\"?hidden\"? name=\"?post_key\"? value=\"\\([a-z0-9]+\\)\">")
1360 (defconst mixi-post-succeed-regexp
1361   "<b>\\(ºîÀ®\\|½ñ¤­¹þ¤ß\\)¤¬´°Î»¤·¤Þ¤·¤¿¡£È¿±Ç¤Ë»þ´Ö¤¬¤«¤«¤ë¤³¤È¤¬¤¢¤ê¤Þ¤¹¤Î¤Ç¡¢É½¼¨¤µ¤ì¤Æ¤¤¤Ê¤¤¾ì¹ç¤Ï¾¯¡¹¤ªÂÔ¤Á¤¯¤À¤µ¤¤¡£</b>")
1362
1363 ;; FIXME: Support photos.
1364 (defun mixi-post-diary (title content)
1365   "Post a diary."
1366   (unless (stringp title)
1367     (signal 'wrong-type-argument (list 'stringp title)))
1368   (unless (stringp content)
1369     (signal 'wrong-type-argument (list 'stringp content)))
1370   (let ((fields `(("id" . ,(mixi-friend-id (mixi-make-me)))
1371                   ("diary_title" . ,title)
1372                   ("diary_body" . ,content)
1373                   ("submit" . "main")))
1374         post-key)
1375     (with-mixi-post-form (mixi-post-diary-page) fields
1376       (if (re-search-forward mixi-post-key-regexp nil t)
1377           (setq post-key (match-string 1))
1378         (mixi-post-error 'cannot-find-key)))
1379     (setq fields `(("post_key" . ,post-key)
1380                    ("id" . ,(mixi-friend-id (mixi-make-me)))
1381                    ("diary_title" . ,title)
1382                    ("diary_body" . ,content)
1383                    ("submit" . "confirm")))
1384     (with-mixi-post-form (mixi-post-diary-page) fields
1385       (unless (re-search-forward mixi-post-succeed-regexp nil t)
1386         (mixi-post-error 'cannot-find-succeed)))))
1387
1388 ;; Community object.
1389 (defvar mixi-community-cache (make-hash-table :test 'equal))
1390 (defun mixi-make-community (id &optional name birthday owner category members
1391                                open-level authority description)
1392   "Return a community object."
1393   (mixi-make-cache id (cons 'mixi-community (vector nil id name birthday owner
1394                                                     category members
1395                                                     open-level authority
1396                                                     description))
1397                    mixi-community-cache))
1398
1399 (defconst mixi-community-url-regexp
1400   "/view_community\\.pl\\?id=\\([0-9]+\\)")
1401
1402 (defun mixi-make-community-from-url (url)
1403   "Return a community object from URL."
1404   (when (string-match mixi-community-url-regexp url)
1405     (let ((id (match-string 1 url)))
1406       (mixi-make-community id))))
1407
1408 (defmacro mixi-community-p (community)
1409   `(eq (mixi-object-class ,community) 'mixi-community))
1410
1411 (defmacro mixi-community-page (community)
1412   `(concat "/view_community.pl?id=" (mixi-community-id ,community)))
1413
1414 ;; FIXME: Not community specific.
1415 (defconst mixi-community-nodata-regexp
1416   "^¥Ç¡¼¥¿¤¬¤¢¤ê¤Þ¤»¤ó")
1417 (defconst mixi-community-name-regexp
1418   "<td WIDTH=345>\\(.*\\)</td></tr>")
1419 (defconst mixi-community-birthday-regexp
1420   "<td BGCOLOR=#F2DDB7 WIDTH=80><font COLOR=#996600>³«ÀßÆü</font></td>\r
1421 <td WIDTH=345>\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü")
1422 ;; FIXME: Care when the owner has seceded.
1423 (defconst mixi-community-owner-regexp
1424   "<td BGCOLOR=#F2DDB7 WIDTH=80><font COLOR=#996600>´ÉÍý¿Í</font></td>\r
1425 <td WIDTH=345>\r
1426 <table style=\"width: 100%;\"><tr><td>\r
1427 <a href=\"\\(home\\.pl\\|show_friend\\.pl\\?id=\\([0-9]+\\)\\)\">\\(.*\\)</a>")
1428 (defconst mixi-community-category-regexp
1429   "<td BGCOLOR=#F2DDB7 WIDTH=80><font COLOR=#996600>¥«¥Æ¥´¥ê</font></td>\r
1430 <td WIDTH=345>\\([^<]+\\)</td>")
1431 (defconst mixi-community-members-regexp
1432   "<td BGCOLOR=#F2DDB7 WIDTH=80><font COLOR=#996600>¥á¥ó¥Ð¡¼¿ô</font></td>\r
1433 <td WIDTH=345>\\([0-9]+\\)¿Í</td></tr>")
1434 (defconst mixi-community-open-level-regexp
1435   "<td BGCOLOR=#F2DDB7 WIDTH=80><font COLOR=#996600>»²²Ã¾ò·ï¤È<br>¸ø³«¥ì¥Ù¥ë</font></td>\r
1436 <td WIDTH=345>\\(.+\\)</td></tr>")
1437 (defconst mixi-community-authority-regexp
1438   "<td BGCOLOR=#F2DDB7 WIDTH=80><font COLOR=#996600>¥È¥Ô¥Ã¥¯ºîÀ®¤Î¸¢¸Â</font></td>\r
1439 <td WIDTH=345>\\(.+\\)</td></tr>")
1440 (defconst mixi-community-description-regexp
1441   "<td CLASS=h120 WIDTH=345>\\(.+\\)</td>")
1442
1443 (defun mixi-realize-community (community)
1444   "Realize a COMMUNITY."
1445   ;; FIXME: Check a expiration of cache?
1446   (unless (mixi-object-realized-p community)
1447     (with-mixi-retrieve (mixi-community-page community)
1448       (if (re-search-forward mixi-community-nodata-regexp nil t)
1449           ;; FIXME: Set all members?
1450           (mixi-community-set-name community "¥Ç¡¼¥¿¤¬¤¢¤ê¤Þ¤»¤ó")
1451         (if (re-search-forward mixi-community-name-regexp nil t)
1452             (mixi-community-set-name community (match-string 1))
1453           (mixi-realization-error 'cannot-find-name community))
1454         (if (re-search-forward mixi-community-birthday-regexp nil t)
1455             (mixi-community-set-birthday
1456              community (encode-time 0 0 0 (string-to-number (match-string 3))
1457                                     (string-to-number (match-string 2))
1458                                     (string-to-number (match-string 1))))
1459           (mixi-realization-error 'cannot-find-birthday community))
1460         (if (re-search-forward mixi-community-owner-regexp nil t)
1461             (if (string= (match-string 1) "home.pl")
1462                 (mixi-community-set-owner community (mixi-make-me))
1463               (mixi-community-set-owner community
1464                                         (mixi-make-friend (match-string 2)
1465                                                           (match-string 3))))
1466           (mixi-realization-error 'cannot-find-owner community))
1467         (if (re-search-forward mixi-community-category-regexp nil t)
1468             (mixi-community-set-category community (match-string 1))
1469           (mixi-realization-error 'cannot-find-category community))
1470         (if (re-search-forward mixi-community-members-regexp nil t)
1471             (mixi-community-set-members community
1472                                         (string-to-number (match-string 1)))
1473           (mixi-realization-error 'cannot-find-members community))
1474         (if (re-search-forward mixi-community-open-level-regexp nil t)
1475             (mixi-community-set-open-level community (match-string 1))
1476           (mixi-realization-error 'cannot-find-open-level community))
1477         (if (re-search-forward mixi-community-authority-regexp nil t)
1478             (mixi-community-set-authority community (match-string 1))
1479           (mixi-realization-error 'cannot-find-authority community))
1480         (if (re-search-forward mixi-community-description-regexp nil t)
1481             (mixi-community-set-description community (match-string 1))
1482           (mixi-realization-error 'cannot-find-description community))))
1483     (mixi-object-touch community)))
1484
1485 (defun mixi-community-id (community)
1486   "Return the id of COMMUNITY."
1487   (unless (mixi-community-p community)
1488     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1489   (aref (cdr community) 1))
1490
1491 (defun mixi-community-name (community)
1492   "Return the name of COMMUNITY."
1493   (unless (mixi-community-p community)
1494     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1495   (unless (aref (cdr community) 2)
1496     (mixi-realize-community community))
1497   (aref (cdr community) 2))
1498
1499 (defun mixi-community-birthday (community)
1500   "Return the birthday of COMMUNITY."
1501   (unless (mixi-community-p community)
1502     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1503   (mixi-realize-community community)
1504   (aref (cdr community) 3))
1505
1506 (defun mixi-community-owner (community)
1507   "Return the owner of COMMUNITY."
1508   (unless (mixi-community-p community)
1509     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1510   (mixi-realize-community community)
1511   (aref (cdr community) 4))
1512
1513 (defun mixi-community-category (community)
1514   "Return the category of COMMUNITY."
1515   (unless (mixi-community-p community)
1516     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1517   (mixi-realize-community community)
1518   (aref (cdr community) 5))
1519
1520 (defun mixi-community-members (community)
1521   "Return the members of COMMUNITY."
1522   (unless (mixi-community-p community)
1523     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1524   (mixi-realize-community community)
1525   (aref (cdr community) 6))
1526
1527 (defun mixi-community-open-level (community)
1528   "Return the open-level of COMMUNITY."
1529   (unless (mixi-community-p community)
1530     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1531   (mixi-realize-community community)
1532   (aref (cdr community) 7))
1533
1534 (defun mixi-community-authority (community)
1535   "Return the authority of COMMUNITY."
1536   (unless (mixi-community-p community)
1537     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1538   (mixi-realize-community community)
1539   (aref (cdr community) 8))
1540
1541 (defun mixi-community-description (community)
1542   "Return the description of COMMUNITY."
1543   (unless (mixi-community-p community)
1544     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1545   (mixi-realize-community community)
1546   (aref (cdr community) 9))
1547
1548 (defun mixi-community-set-name (community name)
1549   "Set the name of COMMUNITY."
1550   (unless (mixi-community-p community)
1551     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1552   (aset (cdr community) 2 name))
1553
1554 (defun mixi-community-set-birthday (community birthday)
1555   "Set the birthday of COMMUNITY."
1556   (unless (mixi-community-p community)
1557     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1558   (aset (cdr community) 3 birthday))
1559
1560 (defun mixi-community-set-owner (community owner)
1561   "Set the owner of COMMUNITY."
1562   (unless (mixi-community-p community)
1563     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1564   (unless (mixi-friend-p owner)
1565     (signal 'wrong-type-argument (list 'mixi-friend-p owner)))
1566   (aset (cdr community) 4 owner))
1567
1568 (defun mixi-community-set-category (community category)
1569   "Set the category of COMMUNITY."
1570   (unless (mixi-community-p community)
1571     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1572   (aset (cdr community) 5 category))
1573
1574 (defun mixi-community-set-members (community members)
1575   "Set the name of COMMUNITY."
1576   (unless (mixi-community-p community)
1577     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1578   (aset (cdr community) 6 members))
1579
1580 (defun mixi-community-set-open-level (community open-level)
1581   "Set the name of COMMUNITY."
1582   (unless (mixi-community-p community)
1583     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1584   (aset (cdr community) 7 open-level))
1585
1586 (defun mixi-community-set-authority (community authority)
1587   "Set the name of COMMUNITY."
1588   (unless (mixi-community-p community)
1589     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1590   (aset (cdr community) 8 authority))
1591
1592 (defun mixi-community-set-description (community description)
1593   "Set the name of COMMUNITY."
1594   (unless (mixi-community-p community)
1595     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1596   (aset (cdr community) 9 description))
1597
1598 (defmacro mixi-community-list-page (&optional friend)
1599   `(concat "/list_community.pl?page=%d"
1600            (when ,friend (concat "&id=" (mixi-friend-id ,friend)))))
1601
1602 (defconst mixi-community-list-id-regexp
1603   "<a href=view_community\\.pl\\?id=\\([0-9]+\\)")
1604 (defconst mixi-community-list-name-regexp
1605   "<td valign=middle>\\(.+\\)([0-9]+)</td>")
1606
1607 (defun mixi-get-communities (&rest friend-or-range)
1608   "Get communities of FRIEND."
1609   (when (> (length friend-or-range) 2)
1610     (signal 'wrong-number-of-arguments
1611             (list 'mixi-get-communities (length friend-or-range))))
1612   (let ((friend (nth 0 friend-or-range))
1613         (range (nth 1 friend-or-range)))
1614     (when (or (not (mixi-friend-p friend)) (mixi-friend-p range))
1615       (setq friend (nth 1 friend-or-range))
1616       (setq range (nth 0 friend-or-range)))
1617     (unless (or (null friend) (mixi-friend-p friend))
1618       (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
1619     (let ((ids (mixi-get-matched-items (mixi-community-list-page friend)
1620                                        mixi-community-list-id-regexp
1621                                        range))
1622           (names (mixi-get-matched-items (mixi-community-list-page friend)
1623                                          mixi-community-list-name-regexp
1624                                          range)))
1625       (let ((index 0)
1626             ret)
1627         (while (< index (length ids))
1628           (setq ret (cons (mixi-make-community (nth 0 (nth index ids))
1629                                                (nth 0 (nth index names))) ret))
1630           (incf index))
1631         (reverse ret)))))
1632
1633 (defmacro mixi-search-community-list-page (keyword)
1634   `(concat "/search_community.pl?page=%d&&sort=date&type=com&submit=main"
1635            "&keyword=" (mixi-url-encode-and-quote-percent-string ,keyword)
1636            "&category_id=0"))
1637
1638 (defconst mixi-search-community-list-regexp
1639   "<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>
1640 <td NOWRAP WIDTH=90 BGCOLOR=#FDF9F2><font COLOR=#996600>¥³¥ß¥å¥Ë¥Æ¥£Ì¾</font></td>
1641 <td COLSPAN=2 WIDTH=370 BGCOLOR=#FFFFFF>\\([^<]+\\)</td></tr>")
1642
1643 ;; FIXME: Support category.
1644 (defun mixi-search-communities (keyword &optional range)
1645   (let ((items (mixi-get-matched-items (mixi-search-community-list-page
1646                                         keyword)
1647                                        mixi-search-community-list-regexp
1648                                        range)))
1649     (mapcar (lambda (item)
1650               (mixi-make-community (nth 0 item) (nth 1 item)))
1651             items)))
1652
1653 ;; Topic object.
1654 (defvar mixi-topic-cache (make-hash-table :test 'equal))
1655 (defun mixi-make-topic (community id &optional comment-count time title owner
1656                                   content)
1657   "Return a topic object."
1658   (mixi-make-cache (list (mixi-community-id community) id)
1659                    (cons 'mixi-topic (vector nil community id comment-count
1660                                              time title owner content))
1661                    mixi-topic-cache))
1662
1663 (defconst mixi-topic-url-regexp
1664   "/view_bbs\\.pl\\?id=\\([0-9]+\\)\\(&comment_count=\\([0-9]+\\)\\)?&comm_id=\\([0-9]+\\)")
1665
1666 (defun mixi-make-topic-from-url (url)
1667   "Return a topic object from URL."
1668   (when (string-match mixi-topic-url-regexp url)
1669     (let ((id (match-string 1 url))
1670           (comment-count (match-string 3 url))
1671           (community-id (match-string 4 url)))
1672       (mixi-make-topic (mixi-make-community community-id) id comment-count))))
1673
1674 (defmacro mixi-topic-p (topic)
1675   `(eq (mixi-object-class ,topic) 'mixi-topic))
1676
1677 (defmacro mixi-topic-page (topic)
1678   `(concat "/view_bbs.pl?id=" (mixi-topic-id ,topic)
1679            "&comm_id=" (mixi-community-id (mixi-topic-community ,topic))))
1680
1681 (defconst mixi-topic-community-regexp
1682   "<td width=\"595\" background=\"http://img\\.mixi\\.jp/img/bg_w\\.gif\"><b>\\[\\(.+\\)\\] ¥È¥Ô¥Ã¥¯</b></td>")
1683 (defconst mixi-topic-time-regexp
1684   "<td rowspan=\"3\" width=\"110\" bgcolor=\"#ffd8b0\" align=\"center\" valign=\"top\" nowrap>\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü<br>\\([0-9]+\\):\\([0-9]+\\)</td>")
1685 (defconst mixi-topic-title-regexp
1686   "<td bgcolor=\"#fff4e0\">&nbsp;\\([^<]+\\)</td>")
1687 (defconst mixi-topic-owner-regexp
1688   "<td bgcolor=\"#fdf9f2\">&nbsp;<font color=\"#dfb479\"></font>&nbsp;<a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.*?\\)\\(¤µ¤ó\\)?</a>")
1689 (defconst mixi-topic-content-regexp
1690   "<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>")
1691
1692 (defun mixi-realize-topic (topic &optional page)
1693   "Realize a TOPIC."
1694   ;; FIXME: Check a expiration of cache?
1695   (unless (mixi-object-realized-p topic)
1696     (with-mixi-retrieve (or page (mixi-topic-page topic))
1697       (if (re-search-forward mixi-topic-community-regexp nil t)
1698           (mixi-community-set-name (mixi-topic-community topic)
1699                                    (match-string 1))
1700         (mixi-realization-error 'cannot-find-community topic))
1701       (if (re-search-forward mixi-topic-time-regexp nil t)
1702           (mixi-topic-set-time
1703            topic (encode-time 0 (string-to-number (match-string 5))
1704                               (string-to-number (match-string 4))
1705                               (string-to-number (match-string 3))
1706                               (string-to-number (match-string 2))
1707                               (string-to-number (match-string 1))))
1708         (mixi-realization-error 'cannot-find-time topic))
1709       (if (re-search-forward mixi-topic-title-regexp nil t)
1710           (mixi-topic-set-title topic (match-string 1))
1711         (mixi-realization-error 'cannot-find-title topic))
1712       (if (re-search-forward mixi-topic-owner-regexp nil t)
1713           (mixi-topic-set-owner topic (mixi-make-friend (match-string 1)
1714                                                         (match-string 2)))
1715         (mixi-realization-error 'cannot-find-owner topic))
1716       (if (re-search-forward mixi-topic-content-regexp nil t)
1717           (mixi-topic-set-content topic (match-string 2))
1718         (mixi-realization-error 'cannot-find-content topic)))
1719     (mixi-object-touch topic)))
1720
1721 (defun mixi-topic-community (topic)
1722   "Return the community of TOPIC."
1723   (unless (mixi-topic-p topic)
1724     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1725   (aref (cdr topic) 1))
1726
1727 (defun mixi-topic-id (topic)
1728   "Return the id of TOPIC."
1729   (unless (mixi-topic-p topic)
1730     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1731   (aref (cdr topic) 2))
1732
1733 (defun mixi-topic-comment-count (topic)
1734   "Return the comment-count of TOPIC."
1735   (unless (mixi-topic-p topic)
1736     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1737   (aref (cdr topic) 3))
1738
1739 (defun mixi-topic-time (topic)
1740   "Return the time of TOPIC."
1741   (unless (mixi-topic-p topic)
1742     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1743   (mixi-realize-topic topic)
1744   (aref (cdr topic) 4))
1745
1746 (defun mixi-topic-title (topic)
1747   "Return the title of TOPIC."
1748   (unless (mixi-topic-p topic)
1749     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1750   (mixi-realize-topic topic)
1751   (aref (cdr topic) 5))
1752
1753 (defun mixi-topic-owner (topic)
1754   "Return the owner of TOPIC."
1755   (unless (mixi-topic-p topic)
1756     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1757   (mixi-realize-topic topic)
1758   (aref (cdr topic) 6))
1759
1760 (defun mixi-topic-content (topic)
1761   "Return the content of TOPIC."
1762   (unless (mixi-topic-p topic)
1763     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1764   (mixi-realize-topic topic)
1765   (aref (cdr topic) 7))
1766
1767 (defun mixi-topic-set-comment-count (topic comment-count)
1768   "Set the comment-count of TOPIC."
1769   (unless (mixi-topic-p topic)
1770     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1771   (aset (cdr topic) 3 comment-count))
1772
1773 (defun mixi-topic-set-time (topic time)
1774   "Set the time of TOPIC."
1775   (unless (mixi-topic-p topic)
1776     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1777   (aset (cdr topic) 4 time))
1778
1779 (defun mixi-topic-set-title (topic title)
1780   "Set the title of TOPIC."
1781   (unless (mixi-topic-p topic)
1782     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1783   (aset (cdr topic) 5 title))
1784
1785 (defun mixi-topic-set-owner (topic owner)
1786   "Set the owner of TOPIC."
1787   (unless (mixi-topic-p topic)
1788     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1789   (unless (mixi-friend-p owner)
1790     (signal 'wrong-type-argument (list 'mixi-friend-p owner)))
1791   (aset (cdr topic) 6 owner))
1792
1793 (defun mixi-topic-set-content (topic content)
1794   "Set the content of TOPIC."
1795   (unless (mixi-topic-p topic)
1796     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1797   (aset (cdr topic) 7 content))
1798
1799 (defmacro mixi-post-topic-page (community)
1800   `(concat "/add_bbs.pl?id=" (mixi-community-id community)))
1801
1802 ;; FIXME: Support photos.
1803 (defun mixi-post-topic (community title content)
1804   "Post a topic to COMMUNITY."
1805   (unless (mixi-community-p community)
1806     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1807   (unless (stringp title)
1808     (signal 'wrong-type-argument (list 'stringp title)))
1809   (unless (stringp content)
1810     (signal 'wrong-type-argument (list 'stringp content)))
1811   (let ((fields `(("bbs_title" . ,title)
1812                   ("bbs_body" . ,content)
1813                   ("submit" . "main")))
1814         post-key)
1815     (with-mixi-post-form (mixi-post-topic-page community) fields
1816       (if (re-search-forward mixi-post-key-regexp nil t)
1817           (setq post-key (match-string 1))
1818         (mixi-post-error 'cannot-find-key community)))
1819     (setq fields `(("post_key" . ,post-key)
1820                    ("bbs_title" . ,title)
1821                    ("bbs_body" . ,content)
1822                    ("submit" . "confirm")))
1823     (with-mixi-post-form (mixi-post-topic-page community) fields
1824       (unless (re-search-forward mixi-post-succeed-regexp nil t)
1825         (mixi-post-error 'cannot-find-succeed community)))))
1826
1827 ;; Event object.
1828 (defvar mixi-event-cache (make-hash-table :test 'equal))
1829 (defun mixi-make-event (community id &optional comment-count time title owner
1830                                   date place detail limit members)
1831   "Return a event object."
1832   (mixi-make-cache (list (mixi-community-id community) id)
1833                    (cons 'mixi-event (vector nil community id comment-count
1834                                              time title owner date place
1835                                              detail limit members))
1836                    mixi-event-cache))
1837
1838 (defconst mixi-event-url-regexp
1839   "/view_event\\.pl\\?id=\\([0-9]+\\)\\(&comment_count=\\([0-9]+\\)\\)?&comm_id=\\([0-9]+\\)")
1840
1841 (defun mixi-make-event-from-url (url)
1842   "Return a event object from URL."
1843   (when (string-match mixi-event-url-regexp url)
1844     (let ((id (match-string 1 url))
1845           (comment-count (match-string 3 url))
1846           (community-id (match-string 4 url)))
1847       (mixi-make-event (mixi-make-community community-id) id comment-count))))
1848
1849 (defmacro mixi-event-p (event)
1850   `(eq (mixi-object-class ,event) 'mixi-event))
1851
1852 (defmacro mixi-event-page (event)
1853   `(concat "/view_event.pl?id=" (mixi-event-id ,event)
1854            "&comm_id=" (mixi-community-id (mixi-event-community ,event))))
1855
1856 (defconst mixi-event-community-regexp
1857   "<td WIDTH=595 background=http://img\\.mixi\\.jp/img/bg_w\\.gif><b>\\[\\(.+\\)\\] ¥¤¥Ù¥ó¥È</b></td>")
1858 (defconst mixi-event-time-regexp
1859   "<td ROWSPAN=11 \\(BGCOLOR\\|bgcolor\\)=#FFD8B0 \\(ALIGN\\|align\\)=center \\(VALIGN\\|Valign\\)=top WIDTH=110>
1860 ?\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü<br>
1861 ?\\([0-9]+\\):\\([0-9]+\\)</td>")
1862 (defconst mixi-event-title-regexp
1863   "<td bgcolor=#FFF4E0\\( width=410\\)?>&nbsp;\\([^<]+\\)</td>")
1864 (defconst mixi-event-owner-regexp
1865   "<td \\(BGCOLOR\\|bgcolor\\)=#FDF9F2>&nbsp;<a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.*\\)</a>")
1866 (defconst mixi-event-owner-seceded-regexp
1867   "<td \\(BGCOLOR\\|bgcolor\\)=#FDF9F2>&nbsp;\\((mixi Âà²ñºÑ)\\)")
1868 (defconst mixi-event-date-regexp
1869   "<td \\(BGCOLOR\\|bgcolor\\)=#\\(FFFFFF\\|ffffff\\) \\(ALIGN\\|align\\)=center NOWRAP>³«ºÅÆü»þ</td>
1870 <td \\(BGCOLOR\\|bgcolor\\)=#\\(FFFFFF\\|ffffff\\)>
1871 &nbsp;\\(.+\\)
1872 </td>")
1873 (defconst mixi-event-place-regexp
1874   "<td \\(BGCOLOR\\|bgcolor\\)=#\\(FFFFFF\\|ffffff\\) \\(ALIGN\\|align\\)=center NOWRAP>³«ºÅ¾ì½ê</td>
1875 <td \\(BGCOLOR\\|bgcolor\\)=#\\(FFFFFF\\|ffffff\\)>
1876 &nbsp;\\(.+\\)
1877 </td>")
1878 (defconst mixi-event-detail-regexp
1879   "<td \\(BGCOLOR\\|bgcolor\\)=#\\(FFFFFF\\|ffffff\\) \\(ALIGN\\|align\\)=center NOWRAP>¾ÜºÙ</td>
1880 <td \\(BGCOLOR\\|bgcolor\\)=#\\(FFFFFF\\|ffffff\\)><table BORDER=0 CELLSPACING=0 CELLPADDING=5><tr><td CLASS=h120>\\(.+\\)</td></tr></table></td>")
1881 (defconst mixi-event-limit-regexp
1882   "<td \\(BGCOLOR\\|bgcolor\\)=\"?#\\(FFFFFF\\|ffffff\\)\"? \\(ALIGN\\|align\\)=\"?center\"? NOWRAP>Ê罸´ü¸Â</td>
1883 ?<td \\(BGCOLOR\\|bgcolor\\)=#\\(FFFFFF\\|ffffff\\)>&nbsp;\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü</td>")
1884 (defconst mixi-event-members-regexp
1885   "<td \\(BGCOLOR\\|bgcolor\\)=#\\(FFFFFF\\|ffffff\\) \\(ALIGN\\|align\\)=center NOWRAP>»²²Ã¼Ô</td>
1886 <td \\(BGCOLOR\\|bgcolor\\)=#\\(FFFFFF\\|ffffff\\)>
1887
1888 ?
1889 ?<table BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH=100%>
1890 <tr>
1891
1892 ?<td>&nbsp;\\(.+\\)</td>")
1893
1894 (defun mixi-realize-event (event &optional page)
1895   "Realize a EVENT."
1896   ;; FIXME: Check a expiration of cache?
1897   (unless (mixi-object-realized-p event)
1898     (with-mixi-retrieve (or page (mixi-event-page event))
1899       (if (re-search-forward mixi-event-community-regexp nil t)
1900           (mixi-community-set-name (mixi-event-community event)
1901                                    (match-string 1))
1902         (mixi-realization-error 'cannot-find-community event))
1903       (if (re-search-forward mixi-event-time-regexp nil t)
1904           (mixi-event-set-time
1905            event (encode-time 0 (string-to-number (match-string 8))
1906                               (string-to-number (match-string 7))
1907                               (string-to-number (match-string 6))
1908                               (string-to-number (match-string 5))
1909                               (string-to-number (match-string 4))))
1910         (mixi-realization-error 'cannot-find-time event))
1911       (if (re-search-forward mixi-event-title-regexp nil t)
1912           (mixi-event-set-title event (match-string 2))
1913         (mixi-realization-error 'cannot-find-title event))
1914       (if (re-search-forward mixi-event-owner-regexp nil t)
1915           (mixi-event-set-owner event (mixi-make-friend (match-string 2)
1916                                                         (match-string 3)))
1917         (if (re-search-forward mixi-event-owner-seceded-regexp nil t)
1918             (mixi-event-set-owner event
1919                                   (mixi-make-friend nil (match-string 2)))
1920           (mixi-realization-error 'cannot-find-owner event)))
1921       (if (re-search-forward mixi-event-date-regexp nil t)
1922           (mixi-event-set-date event (match-string 6))
1923         (mixi-realization-error 'cannot-find-date event))
1924       (if (re-search-forward mixi-event-place-regexp nil t)
1925           (mixi-event-set-place event (match-string 6))
1926         (mixi-realization-error 'cannot-find-place event))
1927       (if (re-search-forward mixi-event-detail-regexp nil t)
1928           (mixi-event-set-detail event (match-string 6))
1929         (mixi-realization-error 'cannot-find-detail event))
1930       (when (re-search-forward mixi-event-limit-regexp nil t)
1931         (mixi-event-set-limit
1932          event (encode-time 0 0 0 (string-to-number (match-string 8))
1933                             (string-to-number (match-string 7))
1934                             (string-to-number (match-string 6)))))
1935       (if (re-search-forward mixi-event-members-regexp nil t)
1936           (mixi-event-set-members event (match-string 6))
1937         (mixi-realization-error 'cannot-find-members event)))
1938     (mixi-object-touch event)))
1939
1940 (defun mixi-event-community (event)
1941   "Return the community of EVENT."
1942   (unless (mixi-event-p event)
1943     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1944   (aref (cdr event) 1))
1945
1946 (defun mixi-event-id (event)
1947   "Return the id of EVENT."
1948   (unless (mixi-event-p event)
1949     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1950   (aref (cdr event) 2))
1951
1952 (defun mixi-event-comment-count (event)
1953   "Return the comment-count of EVENT."
1954   (unless (mixi-event-p event)
1955     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1956   (aref (cdr event) 3))
1957
1958 (defun mixi-event-time (event)
1959   "Return the time 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) 4))
1964
1965 (defun mixi-event-title (event)
1966   "Return the title 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) 5))
1971
1972 (defun mixi-event-owner (event)
1973   "Return the owner of EVENT."
1974   (unless (mixi-event-p event)
1975     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1976   (mixi-realize-event event)
1977   (aref (cdr event) 6))
1978
1979 (defun mixi-event-date (event)
1980   "Return the date of EVENT."
1981   (unless (mixi-event-p event)
1982     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1983   (mixi-realize-event event)
1984   (aref (cdr event) 7))
1985
1986 (defun mixi-event-place (event)
1987   "Return the place of EVENT."
1988   (unless (mixi-event-p event)
1989     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1990   (mixi-realize-event event)
1991   (aref (cdr event) 8))
1992
1993 (defun mixi-event-detail (event)
1994   "Return the detail of EVENT."
1995   (unless (mixi-event-p event)
1996     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1997   (mixi-realize-event event)
1998   (aref (cdr event) 9))
1999
2000 (defun mixi-event-limit (event)
2001   "Return the limit of EVENT."
2002   (unless (mixi-event-p event)
2003     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2004   (mixi-realize-event event)
2005   (aref (cdr event) 10))
2006
2007 (defun mixi-event-members (event)
2008   "Return the members of EVENT."
2009   (unless (mixi-event-p event)
2010     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2011   (mixi-realize-event event)
2012   (aref (cdr event) 11))
2013
2014 (defun mixi-event-set-comment-count (event comment-count)
2015   "Set the comment-count of EVENT."
2016   (unless (mixi-event-p event)
2017     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2018   (aset (cdr event) 3 comment-count))
2019
2020 (defun mixi-event-set-time (event time)
2021   "Set the time of EVENT."
2022   (unless (mixi-event-p event)
2023     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2024   (aset (cdr event) 4 time))
2025
2026 (defun mixi-event-set-title (event title)
2027   "Set the title of EVENT."
2028   (unless (mixi-event-p event)
2029     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2030   (aset (cdr event) 5 title))
2031
2032 (defun mixi-event-set-owner (event owner)
2033   "Set the owner of EVENT."
2034   (unless (mixi-event-p event)
2035     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2036   (unless (mixi-friend-p owner)
2037     (signal 'wrong-type-argument (list 'mixi-friend-p owner)))
2038   (aset (cdr event) 6 owner))
2039
2040 (defun mixi-event-set-date (event date)
2041   "Set the date of EVENT."
2042   (unless (mixi-event-p event)
2043     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2044   (aset (cdr event) 7 date))
2045
2046 (defun mixi-event-set-place (event place)
2047   "Set the place of EVENT."
2048   (unless (mixi-event-p event)
2049     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2050   (aset (cdr event) 8 place))
2051
2052 (defun mixi-event-set-detail (event detail)
2053   "Set the detail of EVENT."
2054   (unless (mixi-event-p event)
2055     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2056   (aset (cdr event) 9 detail))
2057
2058 (defun mixi-event-set-limit (event limit)
2059   "Set the limit of EVENT."
2060   (unless (mixi-event-p event)
2061     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2062   (aset (cdr event) 10 limit))
2063
2064 (defun mixi-event-set-members (event members)
2065   "Set the members of EVENT."
2066   (unless (mixi-event-p event)
2067     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2068   (aset (cdr event) 11 members))
2069
2070 ;; BBS object.
2071 (defconst mixi-bbs-list '(mixi-topic mixi-event))
2072
2073 (defmacro mixi-bbs-p (bbs)
2074   `(memq (mixi-object-class ,bbs) mixi-bbs-list))
2075
2076 (defun mixi-bbs-community (bbs)
2077   "Return the community of BBS."
2078   (unless (mixi-bbs-p bbs)
2079     (signal 'wrong-type-argument (list 'mixi-bbs-p bbs)))
2080   (let ((func (intern (concat mixi-object-prefix
2081                               (mixi-object-name bbs) "-community"))))
2082     (funcall func bbs)))
2083
2084 (defun mixi-bbs-comment-count (bbs)
2085   "Return the comment-count of BBS."
2086   (unless (mixi-bbs-p bbs)
2087     (signal 'wrong-type-argument (list 'mixi-bbs-p bbs)))
2088   (let ((func (intern (concat mixi-object-prefix
2089                               (mixi-object-name bbs) "-comment-count"))))
2090     (funcall func bbs)))
2091
2092 (defun mixi-bbs-set-comment-count (bbs count)
2093   "Set the comment-count of BBS."
2094   (unless (mixi-bbs-p bbs)
2095     (signal 'wrong-type-argument (list 'mixi-bbs-p bbs)))
2096   (let ((func (intern (concat mixi-object-prefix
2097                               (mixi-object-name bbs) "-set-comment-count"))))
2098     (funcall func bbs count)))
2099
2100 (defalias 'mixi-bbs-id 'mixi-object-id)
2101 (defalias 'mixi-bbs-time 'mixi-object-time)
2102 (defalias 'mixi-bbs-title 'mixi-object-title)
2103 (defalias 'mixi-bbs-owner 'mixi-object-owner)
2104 (defalias 'mixi-bbs-content 'mixi-object-content)
2105
2106 (defmacro mixi-bbs-list-page (community)
2107   `(concat "/list_bbs.pl?page=%d"
2108            "&id=" (mixi-community-id ,community)))
2109
2110 (defconst mixi-bbs-list-regexp
2111   "<a href=view_\\(bbs\\|event\\)\\.pl\\?id=\\([0-9]+\\)")
2112
2113 (defun mixi-get-bbses (community &optional range)
2114   "Get bbese of COMMUNITY."
2115   (unless (mixi-community-p community)
2116     (signal 'wrong-type-argument (list 'mixi-community-p community)))
2117   (let ((items (mixi-get-matched-items (mixi-bbs-list-page community)
2118                                        mixi-bbs-list-regexp
2119                                        range)))
2120     (mapcar (lambda (item)
2121               (let ((name (nth 0 item)))
2122                 (when (string= name "bbs")
2123                   (setq name "topic"))
2124                 (let ((func (intern (concat "mixi-make-" name))))
2125                   (funcall func community (nth 1 item)))))
2126             items)))
2127
2128 (defmacro mixi-new-bbs-list-page ()
2129   `(concat "/new_bbs.pl?page=%d"))
2130
2131 (defconst mixi-new-bbs-list-regexp
2132   "<a href=\"view_\\(bbs\\|event\\)\\.pl\\?id=\\([0-9]+\\)&comment_count=\\([0-9]+\\)&comm_id=\\([0-9]+\\)\" class=\"new_link\">")
2133
2134 (defun mixi-get-new-bbses (&optional range)
2135   "Get new topics."
2136   (let ((items (mixi-get-matched-items (mixi-new-bbs-list-page)
2137                                        mixi-new-bbs-list-regexp
2138                                        range)))
2139     (delq nil
2140           (mapcar (lambda (item)
2141                     (let ((name (nth 0 item)))
2142                       (when (string= name "bbs")
2143                         (setq name "topic"))
2144                       (let* ((func (intern (concat "mixi-make-" name)))
2145                              (bbs (funcall func
2146                                            (mixi-make-community (nth 3 item))
2147                                            (nth 1 item)))
2148                              (comment-count (mixi-bbs-comment-count bbs))
2149                              (count (string-to-number (nth 2 item))))
2150                         (when (or (null comment-count)
2151                                   (< comment-count count))
2152                           (mixi-bbs-set-comment-count bbs count)
2153                           bbs))))
2154                   items))))
2155  
2156 (defmacro mixi-search-bbs-list-page (keyword)
2157   `(concat "/search_topic.pl?page=%d&type=top&submit=search"
2158            "&keyword=" (mixi-url-encode-and-quote-percent-string ,keyword)
2159            "&community_id=0&category_id=0"))
2160
2161 (defconst mixi-search-bbs-list-regexp
2162   "<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>")
2163
2164 ;; FIXME: Support community and category.
2165 (defun mixi-search-bbses (keyword &optional range)
2166   (let ((items (mixi-get-matched-items (mixi-search-bbs-list-page keyword)
2167                                        mixi-search-bbs-list-regexp
2168                                        range)))
2169     (mapcar (lambda (item)
2170               (let ((name (nth 0 item)))
2171                 (when (string= name "bbs")
2172                   (setq name "topic"))
2173                 (let ((func (intern (concat "mixi-make-" name))))
2174                   (funcall func (mixi-make-community (nth 2 item))
2175                            (nth 1 item)))))
2176             items)))
2177
2178 ;; Parent object.
2179 (defmacro mixi-parent-p (object)
2180   `(or (eq (mixi-object-class ,object) 'mixi-diary)
2181        (mixi-bbs-p object)))
2182
2183 (defun mixi-realize-parent (parent &optional page)
2184   "Realize a PARENT."
2185   (unless (mixi-parent-p parent)
2186     (signal 'wrong-type-argument (list 'mixi-parent-p parent)))
2187   (let ((func (intern (concat mixi-object-prefix "realize-"
2188                               (mixi-object-name parent)))))
2189     (funcall func parent page)))
2190
2191 ;; Comment object.
2192 (defun mixi-make-comment (parent owner time content)
2193   "Return a comment object."
2194   (cons 'mixi-comment (vector parent owner time content)))
2195
2196 (defmacro mixi-comment-p (comment)
2197   `(eq (mixi-object-class ,comment) 'mixi-comment))
2198
2199 (defun mixi-comment-parent (comment)
2200   "Return the parent of COMMENT."
2201   (unless (mixi-comment-p comment)
2202     (signal 'wrong-type-argument (list 'mixi-comment-p comment)))
2203   (aref (cdr comment) 0))
2204
2205 (defun mixi-comment-owner (comment)
2206   "Return the owner of COMMENT."
2207   (unless (mixi-comment-p comment)
2208     (signal 'wrong-type-argument (list 'mixi-comment-p comment)))
2209   (aref (cdr comment) 1))
2210
2211 (defun mixi-comment-time (comment)
2212   "Return the time of COMMENT."
2213   (unless (mixi-comment-p comment)
2214     (signal 'wrong-type-argument (list 'mixi-comment-p comment)))
2215   (aref (cdr comment) 2))
2216
2217 (defun mixi-comment-content (comment)
2218   "Return the content of COMMENT."
2219   (unless (mixi-comment-p comment)
2220     (signal 'wrong-type-argument (list 'mixi-comment-p comment)))
2221   (aref (cdr comment) 3))
2222
2223 (defun mixi-diary-comment-list-page (diary)
2224   (concat "/view_diary.pl?full=1"
2225           "&id=" (mixi-diary-id diary)
2226           "&owner_id=" (mixi-friend-id (mixi-diary-owner diary))))
2227
2228 ;; FIXME: Split regexp to time, owner(id and nick) and contents.
2229 (defconst mixi-diary-comment-list-regexp
2230 "<td rowspan=\"2\" align=\"center\" width=\"95\" bgcolor=\"#f2ddb7\" nowrap>
2231 \\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü<br>\\([0-9]+\\):\\([0-9]+\\)\\(<br>
2232 <input type=checkbox name=comment_id value=\".+\">
2233 \\|\\)
2234 </td>
2235 <td ALIGN=center BGCOLOR=#FDF9F2 WIDTH=430>
2236 <table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"410\">
2237 <tr>
2238 \\(<td>\\)
2239 <a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.*\\)</a>
2240
2241 \\(<font color=\"#f2ddb7\">|</font> <a href=[^>]+>ºï½ü</a>
2242
2243 \\|\\)</td>
2244 </tr>
2245 </table>
2246 </td>
2247 </tr>
2248 <!-- [^ ]+ : start -->
2249 <tr>
2250 <td bgcolor=\"#ffffff\">
2251 <table BORDER=0 CELLSPACING=0 CELLPADDING=[35] WIDTH=410>
2252 <tr>
2253 <td CLASS=h12>
2254 \\(.+\\)
2255 </td></tr></table>")
2256
2257 (defun mixi-topic-comment-list-page (topic)
2258   (concat "/view_bbs.pl?page=all"
2259           "&id=" (mixi-topic-id topic)
2260           "&comm_id=" (mixi-community-id (mixi-topic-community topic))))
2261
2262 ;; FIXME: Split regexp to time, owner(id and nick) and contents.
2263 (defconst mixi-topic-comment-list-regexp
2264   "<tr valign=\"top\">
2265 <td rowspan=\"2\" width=\"110\" bgcolor=\"#f2ddb7\" align=\"center\" nowrap>
2266 \\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü<br>
2267 \\([0-9]+\\):\\([0-9]+\\)<br>
2268 \\(<input type=\"checkbox\" name=\"comment_id\" value=\".+\">
2269 \\|\\)</td>
2270 <td bgcolor=\"#fdf9f2\">&nbsp;<font color=\"#f8a448\">
2271 <b>[^<]+</b>:</font>&nbsp;
2272 \\(
2273 \\|\\) *<a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.*\\)</a>
2274
2275 ?\\(
2276
2277 \\|<font color=\"#f2ddb7\">|&nbsp;</font><a href=\"delete_bbs_comment\\.pl\\?id=[0-9]+&comm_id=[0-9]+&comment_id=[0-9]+\">ºï½ü</a>
2278 \\|\\)</td>
2279 </tr>
2280 <tr>
2281 <td bgcolor=\"#ffffff\" align=\"center\">
2282 <table border=\"0\" cellspacing=\"0\" cellpadding=\"5\" width=\"500\">
2283 <tr>
2284 <td class=\"h120\">
2285
2286 \\(.+\\)
2287 </td>
2288 </tr>
2289 </table>
2290 </td>
2291 </tr>")
2292
2293 (defun mixi-event-comment-list-page (event)
2294   (concat "/view_event.pl?page=all"
2295           "&id=" (mixi-event-id event)
2296           "&comm_id=" (mixi-community-id (mixi-event-community event))))
2297
2298 ;; FIXME: Split regexp to time, owner(id and nick) and contents.
2299 (defconst mixi-event-comment-list-regexp
2300   "<tr>
2301 <td ROWSPAN=2 ALIGN=center BGCOLOR=#F2DDB7 WIDTH=110>
2302 \\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü<br>
2303 \\([0-9]+\\):\\([0-9]+\\)<br>
2304 \\(</td>\\)
2305 \\(<td BGCOLOR=#FDF9F2>\\)
2306 <font COLOR=#F8A448><b>[^<]+</b> :</font>
2307 <a HREF=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.*\\)</a>
2308
2309 \\(<font COLOR=#F2DDB7>|</font>
2310 <a href=\"delete_bbs_comment\\.pl\\?id=[0-9]+&comm_id=[0-9]+&comment_id=[0-9]+&type=event\">ºï½ü</a>
2311
2312 \\|\\)</td>
2313 </tr>
2314 <tr>
2315 <td ALIGN=center BGCOLOR=#FFFFFF>
2316 <table BORDER=0 CELLSPACING=0 CELLPADDING=5 WIDTH=500>
2317 <tr><td CLASS=h120>\\(.+\\)</td></tr>
2318 </table>
2319 </td>
2320 </tr>")
2321
2322 (defun mixi-get-comments (parent &optional range)
2323   "Get comments of PARENT."
2324   (unless (mixi-parent-p parent)
2325     (signal 'wrong-type-argument (list 'mixi-parent-p parent)))
2326   (let* ((name (mixi-object-name parent))
2327          (list-page (intern (concat mixi-object-prefix name
2328                                     "-comment-list-page")))
2329          (regexp (eval (intern (concat mixi-object-prefix name
2330                                        "-comment-list-regexp"))))
2331          (page (funcall list-page parent)))
2332     (unless (mixi-object-realized-p parent)
2333       (mixi-realize-parent parent page)
2334       (setq page nil))
2335     (let ((items (mixi-get-matched-items page regexp range t)))
2336       (mapcar (lambda (item)
2337                 (mixi-make-comment parent (mixi-make-friend
2338                                            (nth 7 item) (nth 8 item))
2339                                    (encode-time
2340                                     0
2341                                     (string-to-number (nth 4 item))
2342                                     (string-to-number (nth 3 item))
2343                                     (string-to-number (nth 2 item))
2344                                     (string-to-number (nth 1 item))
2345                                     (string-to-number (nth 0 item)))
2346                                    (nth 10 item)))
2347               items))))
2348
2349 (defmacro mixi-new-comment-list-page ()
2350   `(concat "/new_comment.pl?page=%d"))
2351
2352 (defconst mixi-new-comment-list-regexp
2353   "<a href=\"view_diary\\.pl\\?id=\\([0-9]+\\)&owner_id=\\([0-9]+\\)&comment_count=\\([0-9]+\\)\" class=\"new_link\">")
2354
2355 (defun mixi-get-new-comments (&optional range)
2356   "Get new comments."
2357   (let ((items (mixi-get-matched-items (mixi-new-comment-list-page)
2358                                        mixi-new-comment-list-regexp
2359                                        range)))
2360     (delq nil
2361           (mapcar (lambda (item)
2362                     (let* ((diary (mixi-make-diary
2363                                    (mixi-make-friend (nth 1 item))
2364                                    (nth 0 item)))
2365                            (comment-count (mixi-diary-comment-count diary))
2366                            (count (string-to-number (nth 2 item))))
2367                       (when (or (null comment-count)
2368                                 (< comment-count count))
2369                         (mixi-diary-set-comment-count diary count)
2370                         diary)))
2371                   items))))
2372
2373 (defun mixi-post-diary-comment-page (diary)
2374   (concat "/add_comment.pl?&diary_id=" (mixi-diary-id diary)))
2375
2376 (defun mixi-post-topic-comment-page (topic)
2377   (concat "/add_bbs_comment.pl?id=" (mixi-topic-id topic)
2378           "&comm_id=" (mixi-community-id (mixi-topic-community topic))))
2379
2380 (defun mixi-post-event-comment-page (event)
2381   (concat "/add_event_comment.pl?id=" (mixi-event-id event)
2382           "&comm_id=" (mixi-community-id (mixi-event-community event))))
2383
2384 ;; FIXME: Support photos.
2385 (defun mixi-post-comment (parent content)
2386   "Post a comment to PARENT."
2387   (unless (mixi-object-p parent)
2388     (signal 'wrong-type-argument (list 'mixi-object-p parent)))
2389   (unless (stringp content)
2390     (signal 'wrong-type-argument (list 'stringp content)))
2391   (let* ((name (mixi-object-name parent))
2392          (page (intern (concat mixi-object-prefix "post-" name
2393                                "-comment-page")))
2394          fields post-key)
2395     (if (mixi-diary-p parent)
2396         (setq fields
2397               `(("owner_id" . ,(mixi-friend-id (mixi-diary-owner parent)))
2398                 ("comment_body" . ,content)))
2399       (setq fields `(("comment" . ,content))))
2400     (with-mixi-post-form (funcall page parent) fields
2401       (if (re-search-forward mixi-post-key-regexp nil t)
2402           (setq post-key (match-string 1))
2403         (mixi-post-error 'cannot-find-key parent)))
2404     (if (mixi-diary-p parent)
2405         (setq fields
2406               `(("post_key" . ,post-key)
2407                 ("owner_id" . ,(mixi-friend-id (mixi-diary-owner parent)))
2408                 ("comment_body" . ,content)
2409                 ("submit" . "confirm")))
2410       (setq fields `(("post_key" . ,post-key)
2411                      ("comment" . ,content)
2412                      ("submit" . "confirm"))))
2413     (with-mixi-post-form (funcall page parent) fields
2414       (unless (re-search-forward mixi-post-succeed-regexp nil t)
2415         (mixi-post-error 'cannot-find-succeed parent)))))
2416
2417 ;; Message object.
2418 (defconst mixi-message-box-list '(inbox outbox savebox thrash)) ; thrash?
2419
2420 (defmacro mixi-message-box-p (box)
2421   `(memq ,box mixi-message-box-list))
2422
2423 (defun mixi-message-box-name (box)
2424   "Return the name of BOX."
2425   (unless (mixi-message-box-p box)
2426     (signal 'wrong-type-argument (list 'mixi-message-box-p box)))
2427   (symbol-name box))
2428
2429 (defvar mixi-message-cache (make-hash-table :test 'equal))
2430 (defun mixi-make-message (id box &optional owner title time content)
2431   "Return a message object."
2432   (mixi-make-cache (list id box)
2433                    (cons 'mixi-message (vector nil id box owner title time
2434                                                content))
2435                    mixi-message-cache))
2436
2437 (defconst mixi-message-url-regexp
2438   "/view_message\\.pl\\?id=\\([a-z0-9]+\\)&box=\\([a-z]+\\)")
2439
2440 (defun mixi-make-message-from-url (url)
2441   "Return a message object from URL."
2442   (when (string-match mixi-message-url-regexp url)
2443     (let ((id (match-string 1 url))
2444           (box (match-string 2 url)))
2445       (mixi-make-message id box))))
2446
2447 (defmacro mixi-message-p (message)
2448   `(eq (mixi-object-class ,message) 'mixi-message))
2449
2450 (defmacro mixi-message-page (message)
2451   `(concat "/view_message.pl?id=" (mixi-message-id ,message)
2452            "&box=" (mixi-message-box ,message)))
2453
2454 (defconst mixi-message-owner-regexp
2455   "<font COLOR=#996600>\\(º¹½Ð¿Í\\|°¸&nbsp;Àè\\)</font>&nbsp;:&nbsp;<a HREF=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.*\\)\\(</a>\\|</td>\\)")
2456 (defconst mixi-message-time-regexp
2457 "<font COLOR=#996600>Æü\\(¡¡\\|&nbsp;\\)ÉÕ</font>&nbsp;:&nbsp;\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü \\([0-9]+\\)»þ\\([0-9]+\\)ʬ&nbsp;&nbsp;")
2458 (defconst mixi-message-title-regexp
2459 "<font COLOR=#996600>·ï\\(¡¡\\|&nbsp;\\)̾</font>&nbsp;:&nbsp;\\(.+\\)\n?</td>")
2460 (defconst mixi-message-content-regexp
2461   "<tr><td CLASS=h120>\\(.+\\)</td></tr>")
2462
2463 (defun mixi-realize-message (message)
2464   "Realize a MESSAGE."
2465   (unless (mixi-object-realized-p message)
2466     (with-mixi-retrieve (mixi-message-page message)
2467       (if (re-search-forward mixi-message-owner-regexp nil t)
2468           (mixi-message-set-owner message
2469                                   (mixi-make-friend (match-string 2)
2470                                                     (match-string 3)))
2471         (mixi-realization-error 'cannot-find-owner message))
2472       (if (re-search-forward mixi-message-time-regexp nil t)
2473           (mixi-message-set-time
2474            message (encode-time 0 (string-to-number (match-string 6))
2475                                 (string-to-number (match-string 5))
2476                                 (string-to-number (match-string 4))
2477                                 (string-to-number (match-string 3))
2478                                 (string-to-number (match-string 2))))
2479         (mixi-realization-error 'cannot-find-time message))
2480       (if (re-search-forward mixi-message-title-regexp nil t)
2481           (mixi-message-set-title message (match-string 2))
2482         (mixi-realization-error 'cannot-find-title message))
2483       (if (re-search-forward mixi-message-content-regexp nil t)
2484           (mixi-message-set-content message (match-string 1))
2485         (mixi-realization-error 'cannot-find-content message)))
2486     (mixi-object-touch message)))
2487
2488 (defun mixi-message-id (message)
2489   "Return the id of MESSAGE."
2490   (unless (mixi-message-p message)
2491     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2492   (aref (cdr message) 1))
2493
2494 (defun mixi-message-box (message)
2495   "Return the box of MESSAGE."
2496   (unless (mixi-message-p message)
2497     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2498   (aref (cdr message) 2))
2499
2500 (defun mixi-message-owner (message)
2501   "Return the owner of MESSAGE."
2502   (unless (mixi-message-p message)
2503     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2504   (mixi-realize-message message)
2505   (aref (cdr message) 3))
2506
2507 (defun mixi-message-title (message)
2508   "Return the title of MESSAGE."
2509   (unless (mixi-message-p message)
2510     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2511   (mixi-realize-message message)
2512   (aref (cdr message) 4))
2513
2514 (defun mixi-message-time (message)
2515   "Return the date of MESSAGE."
2516   (unless (mixi-message-p message)
2517     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2518   (mixi-realize-message message)
2519   (aref (cdr message) 5))
2520
2521 (defun mixi-message-content (message)
2522   "Return the content of MESSAGE."
2523   (unless (mixi-message-p message)
2524     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2525   (mixi-realize-message message)
2526   (aref (cdr message) 6))
2527
2528 (defun mixi-message-set-owner (message owner)
2529   "Set the owner of MESSAGE."
2530   (unless (mixi-message-p message)
2531     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2532   (aset (cdr message) 3 owner))
2533
2534 (defun mixi-message-set-title (message title)
2535   "Set the title of MESSAGE."
2536   (unless (mixi-message-p message)
2537     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2538   (aset (cdr message) 4 title))
2539
2540 (defun mixi-message-set-time (message time)
2541   "Set the date of MESSAGE."
2542   (unless (mixi-message-p message)
2543     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2544   (aset (cdr message) 5 time))
2545
2546 (defun mixi-message-set-content (message content)
2547   "Set the content of MESSAGE."
2548   (unless (mixi-message-p message)
2549     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2550   (aset (cdr message) 6 content))
2551
2552 (defmacro mixi-message-list-page (&optional box)
2553   `(concat "/list_message.pl?page=%d"
2554            (when ,box (concat "&box=" ,box))))
2555
2556 (defconst mixi-message-list-regexp
2557   "<td><a HREF=\"view_message\\.pl\\?id=\\(.+\\)&box=\\(.+\\)\">")
2558
2559 (defun mixi-get-messages (&rest box-or-range)
2560   "Get messages of BOX."
2561   (when (> (length box-or-range) 2)
2562     (signal 'wrong-number-of-arguments
2563             (list 'mixi-get-messages (length box-or-range))))
2564   (let ((box (nth 0 box-or-range))
2565         (range (nth 1 box-or-range)))
2566     (when (or (not (mixi-message-box-p box))
2567               (mixi-message-box-p range))
2568       (setq box (nth 1 box-or-range))
2569       (setq range (nth 0 box-or-range)))
2570     (let ((items (mixi-get-matched-items
2571                   (mixi-message-list-page
2572                    (when box (mixi-message-box-name box)))
2573                   mixi-message-list-regexp
2574                   range)))
2575       (mapcar (lambda (item)
2576                 (mixi-make-message (nth 0 item) (nth 1 item)))
2577               items))))
2578
2579 (defmacro mixi-post-message-page (friend)
2580   `(concat "/send_message.pl?id=" (mixi-friend-id friend)))
2581
2582 (defconst mixi-post-message-key-regexp
2583   "<input name=post_key type=hidden value=\\([a-z0-9]+\\)>")
2584
2585 (defconst mixi-post-message-succeed-regexp
2586   "<b>Á÷¿®´°Î»</b>¤·¤Þ¤·¤¿¡£")
2587
2588 (defun mixi-post-message (friend title content)
2589   "Post a message to FRIEND."
2590   (unless (mixi-friend-p friend)
2591     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
2592   (unless (stringp title)
2593     (signal 'wrong-type-argument (list 'stringp title)))
2594   (unless (stringp content)
2595     (signal 'wrong-type-argument (list 'stringp content)))
2596   (let ((fields `(("subject" . ,title)
2597                   ("body" . ,content)
2598                   ("submit" . "main")))
2599         post-key)
2600     (with-mixi-post-form (mixi-post-message-page friend) fields
2601       (if (re-search-forward mixi-post-message-key-regexp nil t)
2602           (setq post-key (match-string 1))
2603         (mixi-post-error 'cannot-find-key friend)))
2604     (setq fields `(("post_key" . ,post-key)
2605                    ("subject" . ,title)
2606                    ("body" . ,content)
2607                    ("yes" . "¡¡Á÷¡¡¿®¡¡")
2608                    ("submit" . "confirm")))
2609     (with-mixi-post-form (mixi-post-message-page friend) fields
2610       (unless (re-search-forward mixi-post-message-succeed-regexp nil t)
2611         (mixi-post-error 'cannot-find-succeed friend)))))
2612
2613 ;; Introduction object.
2614 (defun mixi-make-introduction (parent owner content)
2615   "Return a introduction object."
2616   (cons 'mixi-introduction (vector parent owner content)))
2617
2618 (defmacro mixi-introduction-p (introduction)
2619   `(eq (mixi-object-class ,introduction) 'mixi-introduction))
2620
2621 (defun mixi-introduction-parent (introduction)
2622   "Return the parent of INTRODUCTION."
2623   (unless (mixi-introduction-p introduction)
2624     (signal 'wrong-type-argument (list 'mixi-introduction-p introduction)))
2625   (aref (cdr introduction) 0))
2626
2627 (defun mixi-introduction-owner (introduction)
2628   "Return the owner of INTRODUCTION."
2629   (unless (mixi-introduction-p introduction)
2630     (signal 'wrong-type-argument (list 'mixi-introduction-p introduction)))
2631   (aref (cdr introduction) 1))
2632
2633 (defun mixi-introduction-content (introduction)
2634   "Return the content of INTRODUCTION."
2635   (unless (mixi-introduction-p introduction)
2636     (signal 'wrong-type-argument (list 'mixi-introduction-p introduction)))
2637   (aref (cdr introduction) 3))
2638
2639 (defmacro mixi-introduction-list-page (&optional friend)
2640   `(concat "/show_intro.pl?page=%d"
2641            (when ,friend (concat "&id=" (mixi-friend-id ,friend)))))
2642
2643 (defconst mixi-introduction-list-regexp
2644   "<tr bgcolor=#FFFFFF>
2645 <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>
2646 \\(.*\\)</td></a>
2647
2648 <td WIDTH=480>
2649 \\(´Ø·¸¡§.+<br>
2650
2651
2652 \\(\\(.\\|\n<br>\\)+\\)\\|
2653 \\(\\(.\\|\n<br>\\)+\\)\\)
2654
2655
2656
2657
2658 </td>
2659 </tr>")
2660 (defconst mixi-my-introduction-list-regexp
2661   "<tr bgcolor=#FFFFFF>
2662 <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>
2663 \\(.*\\)</td></a>
2664
2665
2666 <td WIDTH=480>
2667 \\(´Ø·¸¡§.+<br>
2668
2669
2670 \\(\\(.\\|\n<br>\\)+\\)\\|
2671 \\(\\(.\\|\n<br>\\)+\\)\\)
2672
2673
2674 <br>
2675 <a href=\"edit_intro\\.pl\\?id=\\1&type=edit\">¤³¤Îͧ¿Í¤ò¾Ò²ð¤¹¤ë</a>
2676
2677
2678 <BR>
2679 <a href=\"delete_intro\\.pl\\?id=\\1\">ºï½ü</a>
2680
2681 </td>
2682 </tr>")
2683
2684 (defun mixi-get-introductions (&rest friend-or-range)
2685   "Get introductions of FRIEND."
2686   (when (> (length friend-or-range) 2)
2687     (signal 'wrong-number-of-arguments
2688             (list 'mixi-get-introduction (length friend-or-range))))
2689   (let ((friend (nth 0 friend-or-range))
2690         (range (nth 1 friend-or-range)))
2691     (when (or (not (mixi-friend-p friend)) (mixi-friend-p range))
2692       (setq friend (nth 1 friend-or-range))
2693       (setq range (nth 0 friend-or-range)))
2694     (unless (or (null friend) (mixi-friend-p friend))
2695       (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
2696     (let* ((regexp (if friend mixi-introduction-list-regexp
2697                      mixi-my-introduction-list-regexp))
2698            (items (mixi-get-matched-items (mixi-introduction-list-page friend)
2699                                           regexp
2700                                           range)))
2701       (mapcar (lambda (item)
2702                 (mixi-make-introduction (or friend (mixi-make-me))
2703                                         (mixi-make-friend (nth 0 item)
2704                                                           (nth 1 item))
2705                                         (nth 2 item)))
2706               items))))
2707
2708 ;; News object.
2709 (defvar mixi-news-cache (make-hash-table :test 'equal))
2710 (defun mixi-make-news (media-id id &optional media time title content)
2711   "Return a news object."
2712   (mixi-make-cache (list media-id id)
2713                    (cons 'mixi-news (vector nil media-id id media time title
2714                                             content))
2715                    mixi-news-cache))
2716
2717 (defconst mixi-news-url-regexp
2718   "/view_news\\.pl\\?id=\\([0-9]+\\)&media_id=\\([0-9]+\\)")
2719
2720 (defun mixi-make-news-from-url (url)
2721   "Return a news object from URL."
2722   (when (string-match mixi-news-url-regexp url)
2723     (let ((id (match-string 1 url))
2724           (media-id (match-string 2 url)))
2725       (mixi-make-news media-id id))))
2726
2727 (defmacro mixi-news-p (news)
2728   `(eq (mixi-object-class ,news) 'mixi-news))
2729
2730 (defmacro mixi-news-page (news)
2731   `(concat "http://news.mixi.jp/view_news.pl?id=" (mixi-news-id ,news)
2732            "&media_id=" (mixi-news-media-id ,news)))
2733
2734 (defconst mixi-news-title-regexp
2735   "<td HEIGHT=\"46\" STYLE=\"font-weight: bold;font-size: 14px;\" CLASS=\"h130\">\\(.+\\)\\(\r
2736 \\)?</td>")
2737 (defconst mixi-news-media-time-regexp
2738   "<td COLSPAN=\"2\" ALIGN=\"right\">(\\(.+\\)&nbsp;-&nbsp;\\([0-9]+\\)·î\\([0-9]+\\)Æü \\([0-9]+\\):\\([0-9]+\\))</td></tr>")
2739 (defconst mixi-news-content-regexp
2740   "<td CLASS=\"h150\">
2741
2742 \\(.+\\)
2743 <br>
2744 \\(.*
2745 \\)?
2746
2747 \\(</td>\\|<br>\\)")
2748
2749 (defun mixi-realize-news (news)
2750   "Realize a NEWS."
2751   ;; FIXME: Check a expiration of cache?
2752   (unless (mixi-object-realized-p news)
2753     (with-mixi-retrieve (mixi-news-page news)
2754       (if (re-search-forward mixi-news-title-regexp nil t)
2755           (mixi-news-set-title news (match-string 1))
2756         (mixi-realization-error 'cannot-find-title news))
2757       (if (re-search-forward mixi-news-media-time-regexp nil t)
2758           (progn
2759             (mixi-news-set-media news (match-string 1))
2760             (let ((year (nth 5 (decode-time (current-time))))
2761                   (month (nth 4 (decode-time (current-time))))
2762                   (month-of-item (string-to-number (match-string 2))))
2763               (when (> month-of-item month)
2764                 (decf year))
2765               (mixi-news-set-time
2766                news (encode-time 0 (string-to-number (match-string 5))
2767                                  (string-to-number (match-string 4))
2768                                  (string-to-number (match-string 3))
2769                                  month year))))
2770         (mixi-realization-error 'cannot-find-media-time news))
2771       (if (re-search-forward mixi-news-content-regexp nil t)
2772           (mixi-news-set-content news (match-string 1))
2773         (mixi-realization-error 'cannot-find-content news)))
2774     (mixi-object-touch news)))
2775
2776 (defun mixi-news-media-id (news)
2777   "Return the media-id of NEWS."
2778   (unless (mixi-news-p news)
2779     (signal 'wrong-type-argument (list 'mixi-news-p news)))
2780   (aref (cdr news) 1))
2781
2782 (defun mixi-news-id (news)
2783   "Return the id of NEWS."
2784   (unless (mixi-news-p news)
2785     (signal 'wrong-type-argument (list 'mixi-news-p news)))
2786   (aref (cdr news) 2))
2787
2788 (defun mixi-news-media (news)
2789   "Return the media of NEWS."
2790   (unless (mixi-news-p news)
2791     (signal 'wrong-type-argument (list 'mixi-news-p news)))
2792   (unless (aref (cdr news) 3)
2793     (mixi-realize-news news))
2794   (aref (cdr news) 3))
2795
2796 (defun mixi-news-time (news)
2797   "Return the time of NEWS."
2798   (unless (mixi-news-p news)
2799     (signal 'wrong-type-argument (list 'mixi-news-p news)))
2800   (unless (aref (cdr news) 4)
2801     (mixi-realize-news news))
2802   (aref (cdr news) 4))
2803
2804 (defun mixi-news-title (news)
2805   "Return the title of NEWS."
2806   (unless (mixi-news-p news)
2807     (signal 'wrong-type-argument (list 'mixi-news-p news)))
2808   (unless (aref (cdr news) 5)
2809     (mixi-realize-news news))
2810   (aref (cdr news) 5))
2811
2812 (defun mixi-news-content (news)
2813   "Return the content of NEWS."
2814   (unless (mixi-news-p news)
2815     (signal 'wrong-type-argument (list 'mixi-news-p news)))
2816   (mixi-realize-news news)
2817   (aref (cdr news) 6))
2818
2819 (defun mixi-news-set-media (news media)
2820   "Set the media of NEWS."
2821   (unless (mixi-news-p news)
2822     (signal 'wrong-type-argument (list 'mixi-news-p news)))
2823   (aset (cdr news) 3 media))
2824
2825 (defun mixi-news-set-time (news time)
2826   "Set the time of NEWS."
2827   (unless (mixi-news-p news)
2828     (signal 'wrong-type-argument (list 'mixi-news-p news)))
2829   (aset (cdr news) 4 time))
2830
2831 (defun mixi-news-set-title (news title)
2832   "Set the title of NEWS."
2833   (unless (mixi-news-p news)
2834     (signal 'wrong-type-argument (list 'mixi-news-p news)))
2835   (aset (cdr news) 5 title))
2836
2837 (defun mixi-news-set-content (news content)
2838   "Set the content of NEWS."
2839   (unless (mixi-news-p news)
2840     (signal 'wrong-type-argument (list 'mixi-news-p news)))
2841   (aset (cdr news) 6 content))
2842
2843 (defconst mixi-news-category-list '(domestic politics economy area abroad
2844                                              sports entertainment IT))
2845
2846 (defmacro mixi-news-category-p (category)
2847   `(memq ,category mixi-news-category-list))
2848
2849 (defun mixi-news-category-id (category)
2850   "Return the id of CATEGORY."
2851   (unless (mixi-news-category-p category)
2852     (signal 'wrong-type-argument (list 'mixi-news-category-p category)))
2853   (number-to-string
2854    (1+ (- (length mixi-news-category-list)
2855           (length (memq category mixi-news-category-list))))))
2856
2857 (defconst mixi-news-sort-list '(newest pickup))
2858
2859 (defmacro mixi-news-sort-p (sort)
2860   `(memq ,sort mixi-news-sort-list))
2861
2862 (defun mixi-news-sort-id (sort)
2863   "Return the id of SORT."
2864   (unless (mixi-news-sort-p sort)
2865     (signal 'wrong-type-argument (list 'mixi-news-sort-p sort)))
2866   (number-to-string
2867    (- (length mixi-news-sort-list)
2868       (length (memq sort mixi-news-sort-list)))))
2869
2870 (defmacro mixi-news-list-page (category sort)
2871   `(concat "http://news.mixi.jp/list_news_category.pl?page=%d"
2872            "&sort=" (mixi-news-sort-id ,sort)
2873            "&id=" (mixi-news-category-id ,category)
2874            "&type=bn"))
2875
2876 (defconst mixi-news-list-regexp
2877   "<tr bgcolor=\"\\(#FCF5EB\\|#FFFFFF\\)\">
2878 <td WIDTH=\"1%\" valign=top CLASS=\"h120\">¡¦</td>
2879 <td WIDTH=\"97%\" CLASS=\"h120\"><A HREF=\"view_news\\.pl\\?id=\\([0-9]+\\)&media_id=\\([0-9]+\\)\"class=\"new_link\">\\(.+\\)</A>
2880 \\(<IMG SRC=\"http://img\\.mixi\\.jp/img/news_camera3\\.gif\" WIDTH=\"11\" HEIGHT=\"12\">\\|\\)
2881
2882 </td>
2883 <td WIDTH=\"1%\" nowrap CLASS=\"f08\"><A HREF=\"list_news_media\\.pl\\?id=[0-9]+\">\\(.+\\)</A></td>
2884 <td WIDTH=\"1%\" nowrap CLASS=\"f08\">\\([0-9]+\\)·î\\([0-9]+\\)Æü \\([0-9]+\\):\\([0-9]+\\)</td></tr>")
2885
2886 (defun mixi-get-news (category sort &optional range)
2887   "Get news of CATEGORY and SORT."
2888   (unless (mixi-news-category-p category)
2889     (signal 'wrong-type-argument (list 'mixi-news-category-p category)))
2890   (unless (mixi-news-sort-p sort)
2891     (signal 'wrong-type-argument (list 'mixi-news-sort-p sort)))
2892   (let ((items (mixi-get-matched-items (mixi-news-list-page category sort)
2893                                        mixi-news-list-regexp
2894                                        range))
2895         (year (nth 5 (decode-time (current-time))))
2896         (month (nth 4 (decode-time (current-time)))))
2897     (mapcar (lambda (item)
2898               (let ((month-of-item (string-to-number (nth 6 item))))
2899                 (when (> month-of-item month)
2900                   (decf year))
2901                 (setq month month-of-item)
2902                 (mixi-make-news (nth 2 item) (nth 1 item) (nth 5 item)
2903                                 (encode-time
2904                                  0 (string-to-number (nth 9 item))
2905                                  (string-to-number (nth 8 item))
2906                                  (string-to-number (nth 7 item))
2907                                  month year)
2908                                 (nth 3 item))))
2909             items)))
2910
2911 (provide 'mixi)
2912
2913 ;;; mixi.el ends here