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