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