* mixi.el (mixi-object-p): Check whether class is symbol.
[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 ((class (mixi-object-class object))
484         (func (intern (concat mixi-object-prefix
485                               (mixi-object-name object) "-title")))
486         prefix)
487     (cond ((eq class 'mixi-event)
488            (setq prefix "[¥¤¥Ù¥ó¥È]")))
489     (concat prefix (funcall func object))))
490
491 (defun mixi-object-content (object)
492   "Return the content of OBJECT."
493   (unless (mixi-object-p object)
494     (signal 'wrong-type-argument (list 'mixi-object-p object)))
495   (let ((class (mixi-object-class object)))
496     (cond ((eq class 'mixi-event)
497            (let ((limit (mixi-event-limit object)))
498              (setq limit (if limit
499                              (format-time-string "%Yǯ%m·î%dÆü" limit)
500                            "»ØÄê¤Ê¤·"))
501              (concat "<dl><dt>³«ºÅÆü»þ¡§</dt>"
502                      "<dd>" (mixi-event-date object) "</dd>"
503                      "<dt>³«ºÅ¾ì½ê¡§</dt>"
504                      "<dd>" (mixi-event-place object) "</dd>"
505                      "<dt>¾ÜºÙ¡§</dt>"
506                      "<dd>" (mixi-event-detail object) "</dd>"
507                      "<dt>Ê罸´ü¸Â¡§</dt>"
508                      "<dd>" limit "</dd>"
509                      "<dt>»²²Ã¼Ô¡§</dt>"
510                      "<dd>" (mixi-event-members object) "</dd></dl>")))
511           (t
512            (let ((func (intern (concat mixi-object-prefix
513                                        (mixi-object-name object) "-content"))))
514              (funcall func object))))))
515
516 (defun mixi-object-set-timestamp (object timestamp)
517   "Set the timestamp of OBJECT."
518   (unless (mixi-object-p object)
519     (signal 'wrong-type-argument (list 'mixi-object-p object)))
520   (aset (cdr object) 0 timestamp))
521
522 (defmacro mixi-object-touch (object)
523   `(mixi-object-set-timestamp ,object (current-time)))
524
525 (defconst mixi-object-url-regexp
526   "/\\(show\\|view\\)_\\([a-z]+\\)\\.pl")
527
528 (defun mixi-make-object-from-url (url)
529   "Return a mixi object from URL."
530   (if (string-match mixi-object-url-regexp url)
531       (let ((name (match-string 2 url)))
532         (when (string= name "bbs")
533           (setq name "topic"))
534         (let ((func (intern (concat mixi-object-prefix "make-" name
535                                     "-from-url"))))
536           (funcall func url)))
537     (when (string-match "/home\\.pl" url)
538       (mixi-make-friend-from-url url))))
539
540 ;; Cache.
541 ;; stolen from time-date.el
542 (defun mixi-time-less-p (t1 t2)
543   "Say whether time value T1 is less than time value T2."
544   (unless (numberp (cdr t1))
545     (setq t1 (cons (car t1) (car (cdr t1)))))
546   (unless (numberp (cdr t2))
547     (setq t2 (cons (car t2) (car (cdr t2)))))
548   (or (< (car t1) (car t2))
549       (and (= (car t1) (car t2))
550            (< (cdr t1) (cdr t2)))))
551
552 (defun mixi-time-add (t1 t2)
553   "Add two time values.  One should represent a time difference."
554   (unless (numberp (cdr t1))
555     (setq t1 (cons (car t1) (car (cdr t1)))))
556   (unless (numberp (cdr t2))
557     (setq t2 (cons (car t2) (car (cdr t2)))))
558   (let ((low (+ (cdr t1) (cdr t2))))
559     (cons (+ (car t1) (car t2) (lsh low -16)) low)))
560
561 ;; stolen from time-date.el
562 (defun mixi-seconds-to-time (seconds)
563   "Convert SECONDS (a floating point number) to a time value."
564   (cons (floor seconds 65536)
565         (floor (mod seconds 65536))))
566
567 (defun mixi-cache-expired-p (object)
568   "Whether a cache of OBJECT is expired."
569   (let ((timestamp (mixi-object-timestamp object)))
570     (unless (or (null mixi-cache-expires)
571                  (null timestamp))
572       (if (numberp mixi-cache-expires)
573           (mixi-time-less-p
574            (mixi-time-add timestamp (mixi-seconds-to-time mixi-cache-expires))
575            (current-time))
576         t))))
577
578 (defun mixi-make-cache (key value table)
579   "Make a cache object and return it."
580   (let ((cache (gethash key table)))
581     (if (and cache (not (mixi-cache-expired-p cache)))
582         cache
583       (puthash key value table))))
584
585 (defconst mixi-cache-file-regexp "[a-z]+-cache$")
586 (defconst mixi-cache-regexp (concat mixi-object-prefix
587                                     mixi-cache-file-regexp))
588
589 (defun mixi-save-cache ()
590   (unless (file-directory-p mixi-directory)
591     (make-directory mixi-directory t))
592   (let ((caches (apropos-internal mixi-cache-regexp 'boundp)))
593     (mapc (lambda (symbol)
594             (with-temp-file (expand-file-name
595                              (substring (symbol-name symbol)
596                                         (length mixi-object-prefix))
597                              mixi-directory)
598               (let ((coding-system-for-write mixi-coding-system)
599                     (cache (symbol-value symbol)))
600                 (insert "#s(hash-table size "
601                         (number-to-string (hash-table-count cache))
602                         " test equal data (\n")
603                 (maphash
604                  (lambda (key value)
605                    (let (print-level print-length)
606                      (insert (prin1-to-string key) " "
607                              (prin1-to-string value) "\n")))
608                  cache))
609               (insert "))")))
610           caches)))
611
612 ;; stolen (and modified) from lsdb.el
613 (defun mixi-read-cache (&optional marker)
614   "Read one Lisp expression as text from MARKER, return as Lisp object."
615   (save-excursion
616     (goto-char marker)
617     (if (looking-at "^#s(")
618         (let ((end-marker
619                (progn
620                  (forward-char 2);skip "#s"
621                  (forward-sexp);move to the left paren
622                  (point-marker))))
623           (with-temp-buffer
624             (buffer-disable-undo)
625             (insert-buffer-substring (marker-buffer marker)
626                                      marker end-marker)
627             (goto-char (point-min))
628             (delete-char 2)
629             (let ((object (read (current-buffer)))
630                   data)
631               (if (eq 'hash-table (car object))
632                   (progn
633                     (setq data (plist-get (cdr object) 'data))
634                     (while data
635                       (pop data);throw it away
636                       (mixi-read-object (pop data))))
637                 object))))
638       (read marker))))
639
640 (defun mixi-load-cache ()
641   (when (file-directory-p mixi-directory)
642     ;; FIXME: Load friend and community first.
643     (let ((files (directory-files mixi-directory t mixi-cache-file-regexp)))
644       (mapc (lambda (file)
645               (let ((buffer (find-file-noselect file)))
646                 (unwind-protect
647                     (save-excursion
648                       (set-buffer buffer)
649                       (goto-char (point-min))
650                       (re-search-forward "^#s(")
651                       (goto-char (match-beginning 0))
652                       (mixi-read-cache (point-marker)))
653                   (kill-buffer buffer))))
654             files))))
655
656 ;; Friend object.
657 (defvar mixi-friend-cache (make-hash-table :test 'equal))
658 (defun mixi-make-friend (id &optional nick name sex address age birthday
659                             blood-type birthplace hobby job organization
660                             profile)
661   "Return a friend object."
662   (mixi-make-cache id (cons 'mixi-friend (vector nil id nick name sex address
663                                                  age birthday blood-type
664                                                  birthplace hobby job
665                                                  organization profile))
666                    mixi-friend-cache))
667
668 (defun mixi-make-me ()
669   "Return a my object."
670   (unless mixi-me
671     (with-mixi-retrieve "/home.pl"
672       (if (string-match mixi-my-id-regexp buffer)
673           (setq mixi-me (mixi-make-friend (match-string 1 buffer)))
674         (signal 'error (list 'who-am-i)))))
675   mixi-me)
676
677 (defconst mixi-friend-url-regexp
678   "/show_friend\\.pl\\?id=\\([0-9]+\\)")
679
680 (defun mixi-make-friend-from-url (url)
681   "Return a friend object from URL."
682   (if (string-match mixi-friend-url-regexp url)
683       (let ((id (match-string 1 url)))
684         (mixi-make-friend id))
685     (when (string-match "/home\\.pl" url)
686       (mixi-make-me))))
687
688 (defmacro mixi-friend-p (friend)
689   `(eq (mixi-object-class ,friend) 'mixi-friend))
690
691 (defmacro mixi-friend-page (friend)
692   `(concat "/show_friend.pl?id=" (mixi-friend-id ,friend)))
693
694 (defconst mixi-friend-nick-regexp
695   "<img alt=\"\\*\" src=\"http://img\\.mixi\\.jp/img/dot0\\.gif\" width=\"1\" height=\"5\"><br>\n\\(.*\\)¤µ¤ó([0-9]+)")
696 (defconst mixi-friend-name-sex-regexp
697   "<td BGCOLOR=#F2DDB7 WIDTH=80 NOWRAP><font COLOR=#996600>̾\\(&nbsp;\\| \\)Á°</font></td>\n+<td WIDTH=345>\\([^<]+\\) (\\([Ã˽÷]\\)À­)</td>")
698 (defconst mixi-friend-address-regexp
699   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>¸½½»½ê</font></td>\n<td>\\(.+\\)\\(\n.+\n\\)?</td></tr>")
700 (defconst mixi-friend-age-regexp
701   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>ǯ\\(&nbsp;\\| \\)Îð</font></td>\n<td>\\([0-9]+\\)ºÐ\\(\n.+\n\\)?</td></tr>")
702 (defconst mixi-friend-birthday-regexp
703   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>ÃÂÀ¸Æü</font></td>\n<td>\\([0-9]+\\)·î\\([0-9]+\\)Æü\\(\n.+\n\\)?</td></tr>")
704 (defconst mixi-friend-blood-type-regexp
705   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>·ì±Õ·¿</font></td>\n<td>\\([ABO]B?\\)·¿\\(\n\n\\)?</td></tr>")
706 (defconst mixi-friend-birthplace-regexp
707   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>½Ð¿ÈÃÏ</font>\n?</td>\n<td>\\(.+\\)\\(\n.+\n\\)?</td></tr>")
708 (defconst mixi-friend-hobby-regexp
709   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>¼ñ\\(&nbsp;\\| \\)Ì£</font></td>\n<td>\\(.+\\)</td></tr>")
710 (defconst mixi-friend-job-regexp
711   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>¿¦\\(&nbsp;\\| \\)¶È</font></td>\n<td>\\(.+\\)\\(\n.+\n\\)?</td></tr>")
712 (defconst mixi-friend-organization-regexp
713   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>½ê\\(&nbsp;\\| \\)°</font></td>\n<td[^>]*>\\(.+\\)\\(\n.+\n\\)?</td></tr>")
714 (defconst mixi-friend-profile-regexp
715   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>¼«¸Ê¾Ò²ð</font></td>\n<td CLASS=h120>\\(.+\\)</td></tr>")
716
717 (defun mixi-friend-realize (friend)
718   "Realize a FRIEND."
719   ;; FIXME: Check a expiration of cache?
720   (unless (mixi-object-realize-p friend)
721     (let (buf)
722       (with-mixi-retrieve (mixi-friend-page friend)
723         (setq buf buffer))
724       (if (string-match mixi-friend-nick-regexp buf)
725           (mixi-friend-set-nick friend (match-string 1 buf))
726         (signal 'error (list 'cannot-find-nick friend)))
727       ;; For getting my profile.
728       (unless (string-match mixi-friend-name-sex-regexp buf)
729         (with-mixi-retrieve "/show_profile.pl"
730           (setq buf buffer)))
731       (if (string-match mixi-friend-name-sex-regexp buf)
732           (progn
733             (mixi-friend-set-name friend (match-string 2 buf))
734             (mixi-friend-set-sex friend
735                                  (if (string= (match-string 3 buf) "ÃË")
736                                      'male 'female)))
737         (signal 'error (list 'cannot-find-name-or-sex friend)))
738       (when (string-match mixi-friend-address-regexp buf)
739         (mixi-friend-set-address friend (match-string 1 buf)))
740       (when (string-match mixi-friend-age-regexp buf)
741         (mixi-friend-set-age
742          friend (string-to-number (match-string 2 buf))))
743       (when (string-match mixi-friend-birthday-regexp buf)
744         (mixi-friend-set-birthday
745          friend (list (string-to-number (match-string 1 buf))
746                       (string-to-number (match-string 2 buf)))))
747       (when (string-match mixi-friend-blood-type-regexp buf)
748         (mixi-friend-set-blood-type friend (intern (match-string 1 buf))))
749       (when (string-match mixi-friend-birthplace-regexp buf)
750         (mixi-friend-set-birthplace friend (match-string 1 buf)))
751       (when (string-match mixi-friend-hobby-regexp buf)
752         (mixi-friend-set-hobby
753          friend (split-string (match-string 2 buf) ", ")))
754       (when (string-match mixi-friend-job-regexp buf)
755         (mixi-friend-set-job friend (match-string 2 buf)))
756       (when (string-match mixi-friend-organization-regexp buf)
757         (mixi-friend-set-organization friend (match-string 2 buf)))
758       (when (string-match mixi-friend-profile-regexp buf)
759         (mixi-friend-set-profile friend (match-string 1 buf))))
760     (mixi-object-touch friend)))
761
762 (defun mixi-friend-id (friend)
763   "Return the id of FRIEND."
764   (unless (mixi-friend-p friend)
765     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
766   (aref (cdr friend) 1))
767
768 (defun mixi-friend-nick (friend)
769   "Return the nick of FRIEND."
770   (unless (mixi-friend-p friend)
771     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
772   (unless (aref (cdr friend) 2)
773     (mixi-friend-realize friend))
774   (aref (cdr friend) 2))
775
776 (defun mixi-friend-name (friend)
777   "Return the name of FRIEND."
778   (unless (mixi-friend-p friend)
779     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
780   (mixi-friend-realize friend)
781   (aref (cdr friend) 3))
782
783 (defun mixi-friend-sex (friend)
784   "Return the sex of FRIEND."
785   (unless (mixi-friend-p friend)
786     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
787   (mixi-friend-realize friend)
788   (aref (cdr friend) 4))
789
790 (defun mixi-friend-address (friend)
791   "Return the address of FRIEND."
792   (unless (mixi-friend-p friend)
793     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
794   (mixi-friend-realize friend)
795   (aref (cdr friend) 5))
796
797 (defun mixi-friend-age (friend)
798   "Return the age of FRIEND."
799   (unless (mixi-friend-p friend)
800     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
801   (mixi-friend-realize friend)
802   (aref (cdr friend) 6))
803
804 (defun mixi-friend-birthday (friend)
805   "Return the birthday of FRIEND."
806   (unless (mixi-friend-p friend)
807     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
808   (mixi-friend-realize friend)
809   (aref (cdr friend) 7))
810
811 (defun mixi-friend-blood-type (friend)
812   "Return the blood type of FRIEND."
813   (unless (mixi-friend-p friend)
814     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
815   (mixi-friend-realize friend)
816   (aref (cdr friend) 8))
817
818 (defun mixi-friend-birthplace (friend)
819   "Return the birthplace of FRIEND."
820   (unless (mixi-friend-p friend)
821     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
822   (mixi-friend-realize friend)
823   (aref (cdr friend) 9))
824
825 (defun mixi-friend-hobby (friend)
826   "Return the hobby of FRIEND."
827   (unless (mixi-friend-p friend)
828     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
829   (mixi-friend-realize friend)
830   (aref (cdr friend) 10))
831
832 (defun mixi-friend-job (friend)
833   "Return the job of FRIEND."
834   (unless (mixi-friend-p friend)
835     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
836   (mixi-friend-realize friend)
837   (aref (cdr friend) 11))
838
839 (defun mixi-friend-organization (friend)
840   "Return the organization of FRIEND."
841   (unless (mixi-friend-p friend)
842     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
843   (mixi-friend-realize friend)
844   (aref (cdr friend) 12))
845
846 (defun mixi-friend-profile (friend)
847   "Return the pforile of FRIEND."
848   (unless (mixi-friend-p friend)
849     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
850   (mixi-friend-realize friend)
851   (aref (cdr friend) 13))
852
853 (defun mixi-friend-set-nick (friend nick)
854   "Set the nick of FRIEND."
855   (unless (mixi-friend-p friend)
856     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
857   (aset (cdr friend) 2 nick))
858
859 (defun mixi-friend-set-name (friend name)
860   "Set the name of FRIEND."
861   (unless (mixi-friend-p friend)
862     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
863   (aset (cdr friend) 3 name))
864
865 (defun mixi-friend-set-sex (friend sex)
866   "Set the sex of FRIEND."
867   (unless (mixi-friend-p friend)
868     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
869   (aset (cdr friend) 4 sex))
870
871 (defun mixi-friend-set-address (friend address)
872   "Set the address of FRIEND."
873   (unless (mixi-friend-p friend)
874     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
875   (aset (cdr friend) 5 address))
876
877 (defun mixi-friend-set-age (friend age)
878   "Set the age of FRIEND."
879   (unless (mixi-friend-p friend)
880     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
881   (aset (cdr friend) 6 age))
882
883 (defun mixi-friend-set-birthday (friend birthday)
884   "Set the birthday of FRIEND."
885   (unless (mixi-friend-p friend)
886     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
887   (aset (cdr friend) 7 birthday))
888
889 (defun mixi-friend-set-blood-type (friend blood-type)
890   "Set the blood type of FRIEND."
891   (unless (mixi-friend-p friend)
892     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
893   (aset (cdr friend) 8 blood-type))
894
895 (defun mixi-friend-set-birthplace (friend birthplace)
896   "Set the birthplace of FRIEND."
897   (unless (mixi-friend-p friend)
898     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
899   (aset (cdr friend) 9 birthplace))
900
901 (defun mixi-friend-set-hobby (friend hobby)
902   "Set the hobby of FRIEND."
903   (unless (mixi-friend-p friend)
904     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
905   (aset (cdr friend) 10 hobby))
906
907 (defun mixi-friend-set-job (friend job)
908   "Set the job of FRIEND."
909   (unless (mixi-friend-p friend)
910     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
911   (aset (cdr friend) 11 job))
912
913 (defun mixi-friend-set-organization (friend organization)
914   "Set the organization of FRIEND."
915   (unless (mixi-friend-p friend)
916     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
917   (aset (cdr friend) 12 organization))
918
919 (defun mixi-friend-set-profile (friend profile)
920   "Set the profile of FRIEND."
921   (unless (mixi-friend-p friend)
922     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
923   (aset (cdr friend) 13 profile))
924
925 (defmacro mixi-friend-list-page (&optional friend)
926   `(concat "/list_friend.pl?page=%d"
927            (when ,friend (concat "&id=" (mixi-friend-id ,friend)))))
928
929 (defconst mixi-friend-list-id-regexp
930   "<a href=show_friend\\.pl\\?id=\\([0-9]+\\)")
931 (defconst mixi-friend-list-nick-regexp
932   "<td valign=middle>\\(.+\\)¤µ¤ó([0-9]+)<br />")
933
934 (defun mixi-get-friends (&rest args)
935   "Get friends of FRIEND."
936   (when (> (length args) 2)
937     (signal 'wrong-number-of-arguments (list 'mixi-get-friends (length args))))
938   (let ((friend (nth 0 args))
939         (range (nth 1 args)))
940     (when (or (not (mixi-friend-p friend)) (mixi-friend-p range))
941       (setq friend (nth 1 args))
942       (setq range (nth 0 args)))
943     (unless (or (null friend) (mixi-friend-p friend))
944       (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
945     (let ((ids (mixi-get-matched-items (mixi-friend-list-page friend)
946                                        mixi-friend-list-id-regexp
947                                        range))
948           (nicks (mixi-get-matched-items (mixi-friend-list-page friend)
949                                          mixi-friend-list-nick-regexp
950                                          range)))
951       (let ((index 0)
952             ret)
953         (while (< index (length ids))
954           (setq ret (cons (mixi-make-friend (nth 0 (nth index ids))
955                                             (nth 0 (nth index nicks))) ret))
956           (incf index))
957         (reverse ret)))))
958
959 ;; Favorite.
960 (defmacro mixi-favorite-list-page ()
961   `(concat "/list_bookmark.pl?page=%d"))
962
963 (defconst mixi-favorite-list-id-regexp
964   "<td ALIGN=center BGCOLOR=#FDF9F2 width=330><a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">")
965 (defconst mixi-favorite-list-nick-regexp
966   "<td BGCOLOR=#FDF9F2><font COLOR=#996600>̾&nbsp;&nbsp;Á°</font></td>
967 <td COLSPAN=2 BGCOLOR=#FFFFFF>\\(.+\\)</td></tr>")
968
969 (defun mixi-get-favorites (&optional range)
970   "Get favorites."
971   (let ((ids (mixi-get-matched-items (mixi-favorite-list-page)
972                                      mixi-favorite-list-id-regexp
973                                      range))
974         (nicks (mixi-get-matched-items (mixi-favorite-list-page)
975                                        mixi-favorite-list-nick-regexp
976                                        range)))
977     (let ((index 0)
978           ret)
979       (while (< index (length ids))
980         (setq ret (cons (mixi-make-friend (nth 0 (nth index ids))
981                                           (nth 0 (nth index nicks))) ret))
982         (incf index))
983       (reverse ret))))
984
985 ;; Log object.
986 (defun mixi-make-log (friend time)
987   "Return a log object."
988   (cons 'mixi-log (vector friend time)))
989
990 (defmacro mixi-log-p (log)
991   `(eq (mixi-object-class ,log) 'mixi-log))
992
993 (defun mixi-log-friend (log)
994   "Return the friend of LOG."
995   (unless (mixi-log-p log)
996     (signal 'wrong-type-argument (list 'mixi-log-p log)))
997   (aref (cdr log) 0))
998
999 (defun mixi-log-time (log)
1000   "Return the time of LOG."
1001   (unless (mixi-log-p log)
1002     (signal 'wrong-type-argument (list 'mixi-log-p log)))
1003   (aref (cdr log) 1))
1004
1005 (defmacro mixi-log-list-page ()
1006   `(concat "/show_log.pl"))
1007
1008 (defconst mixi-log-list-regexp
1009   "\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü \\([0-9]+\\):\\([0-9]+\\) <a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.*\\)</a></li>")
1010
1011 (defun mixi-get-logs (&optional range)
1012   "Get logs."
1013   (let ((items (mixi-get-matched-items (mixi-log-list-page)
1014                                        mixi-log-list-regexp
1015                                        range)))
1016     (mapcar (lambda (item)
1017               (mixi-make-log (mixi-make-friend (nth 5 item) (nth 6 item))
1018                              (encode-time 0
1019                                           (string-to-number (nth 4 item))
1020                                           (string-to-number (nth 3 item))
1021                                           (string-to-number (nth 2 item))
1022                                           (string-to-number (nth 1 item))
1023                                           (string-to-number (nth 0 item)))))
1024             items)))
1025
1026 ;; Diary object.
1027 (defvar mixi-diary-cache (make-hash-table :test 'equal))
1028 (defun mixi-make-diary (owner id &optional time title content)
1029   "Return a diary object."
1030   (let ((owner (or owner (mixi-make-me))))
1031     (mixi-make-cache (list (mixi-friend-id owner) id)
1032                      (cons 'mixi-diary (vector nil owner id time title
1033                                                content))
1034                      mixi-diary-cache)))
1035
1036 (defconst mixi-diary-url-regexp
1037   "/view_diary\\.pl\\?id=\\([0-9]+\\)&owner_id=\\([0-9]+\\)")
1038
1039 (defun mixi-make-diary-from-url (url)
1040   "Return a diary object from URL."
1041   (when (string-match mixi-diary-url-regexp url)
1042     (let ((id (match-string 1 url))
1043           (owner-id (match-string 2 url)))
1044       (mixi-make-diary (mixi-make-friend owner-id) id))))
1045
1046 (defmacro mixi-diary-p (diary)
1047   `(eq (mixi-object-class ,diary) 'mixi-diary))
1048
1049 (defmacro mixi-diary-page (diary)
1050   `(concat "/view_diary.pl?id=" (mixi-diary-id ,diary)
1051            "&owner_id=" (mixi-friend-id (mixi-diary-owner ,diary))))
1052
1053 (defconst mixi-diary-closed-regexp
1054   "<td>ͧ¿Í\\(¤Îͧ¿Í\\)?¤Þ¤Ç¸ø³«¤Î¤¿¤áÆɤळ¤È¤¬½ÐÍè¤Þ¤»¤ó¡£</td></tr>")
1055 (defconst mixi-diary-owner-nick-regexp
1056   "<td WIDTH=490 background=http://img\\.mixi\\.jp/img/bg_w\\.gif><b><font COLOR=#605048>\\(.+?\\)\\(¤µ¤ó\\)?¤ÎÆüµ­</font></b></td>")
1057 (defconst mixi-diary-time-regexp
1058   "<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>")
1059 (defconst mixi-diary-title-regexp
1060   "<td \\(bgcolor\\|BGCOLOR\\)=\"?#FFF4E0\"? width=\"?430\"?>&nbsp;\\([^<]+\\)</td>")
1061 (defconst mixi-diary-content-regexp
1062   "<td \\(class\\|CLASS\\)=\"?h12\"?>\\(.*\\)</td>")
1063
1064 (defun mixi-diary-realize (diary)
1065   "Realize a DIARY."
1066   ;; FIXME: Check a expiration of cache?
1067   (unless (mixi-object-realize-p diary)
1068     (with-mixi-retrieve (mixi-diary-page diary)
1069       (unless (string-match mixi-diary-closed-regexp buffer)
1070         (if (string-match mixi-diary-owner-nick-regexp buffer)
1071             (mixi-friend-set-nick (mixi-diary-owner diary)
1072                                   (match-string 1 buffer))
1073           (signal 'error (list 'cannot-find-owner-nick diary)))
1074         (if (string-match mixi-diary-time-regexp buffer)
1075             (mixi-diary-set-time
1076              diary (encode-time 0 (string-to-number (match-string 10 buffer))
1077                                 (string-to-number (match-string 9 buffer))
1078                                 (string-to-number (match-string 7 buffer))
1079                                 (string-to-number (match-string 6 buffer))
1080                                 (string-to-number (match-string 5 buffer))))
1081           (signal 'error (list 'cannot-find-time diary)))
1082         (if (string-match mixi-diary-title-regexp buffer)
1083             (mixi-diary-set-title diary (match-string 2 buffer))
1084           (signal 'error (list 'cannot-find-title diary)))
1085         (if (string-match mixi-diary-content-regexp buffer)
1086             (mixi-diary-set-content diary (match-string 2 buffer))
1087           (signal 'error (list 'cannot-find-content diary)))))
1088     (mixi-object-touch diary)))
1089
1090 (defun mixi-diary-owner (diary)
1091   "Return the owner of DIARY."
1092   (unless (mixi-diary-p diary)
1093     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1094   (aref (cdr diary) 1))
1095
1096 (defun mixi-diary-id (diary)
1097   "Return the id of DIARY."
1098   (unless (mixi-diary-p diary)
1099     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1100   (aref (cdr diary) 2))
1101
1102 (defun mixi-diary-time (diary)
1103   "Return the time of DIARY."
1104   (unless (mixi-diary-p diary)
1105     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1106   (mixi-diary-realize diary)
1107   (aref (cdr diary) 3))
1108
1109 (defun mixi-diary-title (diary)
1110   "Return the title of DIARY."
1111   (unless (mixi-diary-p diary)
1112     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1113   (mixi-diary-realize diary)
1114   (aref (cdr diary) 4))
1115
1116 (defun mixi-diary-content (diary)
1117   "Return the content of DIARY."
1118   (unless (mixi-diary-p diary)
1119     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1120   (mixi-diary-realize diary)
1121   (aref (cdr diary) 5))
1122
1123 (defun mixi-diary-set-time (diary time)
1124   "Set the time of DIARY."
1125   (unless (mixi-diary-p diary)
1126     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1127   (aset (cdr diary) 3 time))
1128
1129 (defun mixi-diary-set-title (diary title)
1130   "Set the title of DIARY."
1131   (unless (mixi-diary-p diary)
1132     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1133   (aset (cdr diary) 4 title))
1134
1135 (defun mixi-diary-set-content (diary content)
1136   "Set the content of DIARY."
1137   (unless (mixi-diary-p diary)
1138     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1139   (aset (cdr diary) 5 content))
1140
1141 (defmacro mixi-diary-list-page (&optional friend)
1142   `(concat "/list_diary.pl?page=%d"
1143            (when ,friend (concat "&id=" (mixi-friend-id ,friend)))))
1144
1145 (defconst mixi-diary-list-regexp
1146   "<a href=\"view_diary\\.pl\\?id=\\([0-9]+\\)&owner_id=[0-9]+\">")
1147
1148 (defun mixi-get-diaries (&rest args)
1149   "Get diaries of FRIEND."
1150   (when (> (length args) 2)
1151     (signal 'wrong-number-of-arguments
1152             (list 'mixi-get-diaries (length args))))
1153   (let ((friend (nth 0 args))
1154         (range (nth 1 args)))
1155     (when (or (not (mixi-friend-p friend)) (mixi-friend-p range))
1156       (setq friend (nth 1 args))
1157       (setq range (nth 0 args)))
1158     (unless (or (null friend) (mixi-friend-p friend))
1159       (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
1160     (let ((items (mixi-get-matched-items (mixi-diary-list-page friend)
1161                                          mixi-diary-list-regexp
1162                                          range)))
1163       (mapcar (lambda (item)
1164                 (mixi-make-diary friend (nth 0 item)))
1165               items))))
1166
1167 (defmacro mixi-new-diary-list-page ()
1168   `(concat "/new_friend_diary.pl?page=%d"))
1169
1170 (defconst mixi-new-diary-list-regexp
1171   "<a class=\"new_link\" href=view_diary\\.pl\\?id=\\([0-9]+\\)&owner_id=\\([0-9]+\\)>")
1172
1173 (defun mixi-get-new-diaries (&optional range)
1174   "Get new diaries."
1175   (let ((items (mixi-get-matched-items (mixi-new-diary-list-page)
1176                                        mixi-new-diary-list-regexp
1177                                        range)))
1178     (mapcar (lambda (item)
1179               (mixi-make-diary (mixi-make-friend (nth 1 item)) (nth 0 item)))
1180             items)))
1181
1182 (defmacro mixi-search-diary-list-page (keyword)
1183   `(concat "/search_diary.pl?page=%d&submit=search&keyword="
1184              (mixi-url-encode-and-quote-percent-string keyword)))
1185
1186 (defconst mixi-search-diary-list-regexp
1187   "<a href=\"view_diary\\.pl\\?id=\\([0-9]+\\)&owner_id=\\([0-9]+\\)\">")
1188
1189 (defun mixi-search-diaries (keyword &optional range)
1190   (let ((items (mixi-get-matched-items (mixi-search-diary-list-page keyword)
1191                                        mixi-search-diary-list-regexp
1192                                        range)))
1193     (mapcar (lambda (item)
1194               (mixi-make-diary (mixi-make-friend (nth 1 item)) (nth 0 item)))
1195             items)))
1196
1197 ;; Community object.
1198 (defvar mixi-community-cache (make-hash-table :test 'equal))
1199 (defun mixi-make-community (id &optional name birthday owner category members
1200                                open-level authority description)
1201   "Return a community object."
1202   (mixi-make-cache id (cons 'mixi-community (vector nil id name birthday owner
1203                                                     category members
1204                                                     open-level authority
1205                                                     description))
1206                    mixi-community-cache))
1207
1208 (defconst mixi-community-url-regexp
1209   "/view_community\\.pl\\?id=\\([0-9]+\\)")
1210
1211 (defun mixi-make-community-from-url (url)
1212   "Return a community object from URL."
1213   (when (string-match mixi-community-url-regexp url)
1214     (let ((id (match-string 1 url)))
1215       (mixi-make-community id))))
1216
1217 (defmacro mixi-community-p (community)
1218   `(eq (mixi-object-class ,community) 'mixi-community))
1219
1220 (defmacro mixi-community-page (community)
1221   `(concat "/view_community.pl?id=" (mixi-community-id ,community)))
1222
1223 ;; FIXME: Not community specific.
1224 (defconst mixi-community-nodata-regexp
1225   "^¥Ç¡¼¥¿¤¬¤¢¤ê¤Þ¤»¤ó")
1226 (defconst mixi-community-name-regexp
1227   "<td WIDTH=345>\\(.*\\)</td></tr>")
1228 (defconst mixi-community-birthday-regexp
1229   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>³«ÀßÆü</font></td>\n<td>\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü</td>")
1230 ;; FIXME: Care when the owner has seceded.
1231 (defconst mixi-community-owner-regexp
1232   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>´ÉÍý¿Í</font></td>\n<td>\n\n<a href=\"\\(home\\.pl\\|show_friend\\.pl\\?id=\\([0-9]+\\)\\)\">\n\\(.*\\)</a>")
1233 (defconst mixi-community-category-regexp
1234   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>¥«¥Æ¥´¥ê</font></td>\n<td>\\([^<]+\\)</td>")
1235 (defconst mixi-community-members-regexp
1236   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>¥á¥ó¥Ð¡¼¿ô</font></td>\n<td>\\([0-9]+\\)¿Í</td></tr>")
1237 (defconst mixi-community-open-level-regexp
1238   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>»²²Ã¾ò·ï¤È<br>¸ø³«¥ì¥Ù¥ë</font></td>
1239 <td>\\(.+\\)</td></tr>")
1240 (defconst mixi-community-authority-regexp
1241   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>¥È¥Ô¥Ã¥¯ºîÀ®¤Î¸¢¸Â</font></td>\n<td>\\(.+\\)</td></tr>")
1242 (defconst mixi-community-description-regexp
1243   "<td CLASS=h120>\\(.+\\)</td>")
1244
1245 (defun mixi-community-realize (community)
1246   "Realize a COMMUNITY."
1247   ;; FIXME: Check a expiration of cache?
1248   (unless (mixi-object-realize-p community)
1249     (with-mixi-retrieve (mixi-community-page community)
1250       (if (string-match mixi-community-nodata-regexp buffer)
1251           ;; FIXME: Set all members?
1252           (mixi-community-set-name community "¥Ç¡¼¥¿¤¬¤¢¤ê¤Þ¤»¤ó")
1253         (if (string-match mixi-community-name-regexp buffer)
1254             (mixi-community-set-name community (match-string 1 buffer))
1255           (signal 'error (list 'cannot-find-name community)))
1256         (if (string-match mixi-community-birthday-regexp buffer)
1257             (mixi-community-set-birthday
1258              community
1259              (encode-time 0 0 0 (string-to-number (match-string 3 buffer))
1260                           (string-to-number (match-string 2 buffer))
1261                           (string-to-number (match-string 1 buffer))))
1262           (signal 'error (list 'cannot-find-birthday community)))
1263         (if (string-match mixi-community-owner-regexp buffer)
1264             (if (string= (match-string 1 buffer) "home.pl")
1265                 (mixi-community-set-owner community (mixi-make-me))
1266               (mixi-community-set-owner
1267                community (mixi-make-friend (match-string 2 buffer)
1268                                            (match-string 3 buffer))))
1269           (signal 'error (list 'cannot-find-owner community)))
1270         (if (string-match mixi-community-category-regexp buffer)
1271             (mixi-community-set-category community (match-string 1 buffer))
1272           (signal 'error (list 'cannot-find-category community)))
1273         (if (string-match mixi-community-members-regexp buffer)
1274             (mixi-community-set-members
1275              community (string-to-number (match-string 1 buffer)))
1276           (signal 'error (list 'cannot-find-members community)))
1277         (if (string-match mixi-community-open-level-regexp buffer)
1278             (mixi-community-set-open-level community (match-string 1 buffer))
1279           (signal 'error (list 'cannot-find-open-level community)))
1280         (if (string-match mixi-community-authority-regexp buffer)
1281             (mixi-community-set-authority community (match-string 1 buffer))
1282           (signal 'error (list 'cannot-find-authority community)))
1283         (if (string-match mixi-community-description-regexp buffer)
1284             (mixi-community-set-description community (match-string 1 buffer))
1285           (signal 'error (list 'cannot-find-description community)))))
1286     (mixi-object-touch community)))
1287
1288 (defun mixi-community-id (community)
1289   "Return the id of COMMUNITY."
1290   (unless (mixi-community-p community)
1291     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1292   (aref (cdr community) 1))
1293
1294 (defun mixi-community-name (community)
1295   "Return the name of COMMUNITY."
1296   (unless (mixi-community-p community)
1297     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1298   (unless (aref (cdr community) 2)
1299     (mixi-community-realize community))
1300   (aref (cdr community) 2))
1301
1302 (defun mixi-community-birthday (community)
1303   "Return the birthday of COMMUNITY."
1304   (unless (mixi-community-p community)
1305     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1306   (mixi-community-realize community)
1307   (aref (cdr community) 3))
1308
1309 (defun mixi-community-owner (community)
1310   "Return the owner of COMMUNITY."
1311   (unless (mixi-community-p community)
1312     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1313   (mixi-community-realize community)
1314   (aref (cdr community) 4))
1315
1316 (defun mixi-community-category (community)
1317   "Return the category of COMMUNITY."
1318   (unless (mixi-community-p community)
1319     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1320   (mixi-community-realize community)
1321   (aref (cdr community) 5))
1322
1323 (defun mixi-community-members (community)
1324   "Return the members of COMMUNITY."
1325   (unless (mixi-community-p community)
1326     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1327   (mixi-community-realize community)
1328   (aref (cdr community) 6))
1329
1330 (defun mixi-community-open-level (community)
1331   "Return the open-level of COMMUNITY."
1332   (unless (mixi-community-p community)
1333     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1334   (mixi-community-realize community)
1335   (aref (cdr community) 7))
1336
1337 (defun mixi-community-authority (community)
1338   "Return the authority of COMMUNITY."
1339   (unless (mixi-community-p community)
1340     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1341   (mixi-community-realize community)
1342   (aref (cdr community) 8))
1343
1344 (defun mixi-community-description (community)
1345   "Return the description of COMMUNITY."
1346   (unless (mixi-community-p community)
1347     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1348   (mixi-community-realize community)
1349   (aref (cdr community) 9))
1350
1351 (defun mixi-community-set-name (community name)
1352   "Set the name of COMMUNITY."
1353   (unless (mixi-community-p community)
1354     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1355   (aset (cdr community) 2 name))
1356
1357 (defun mixi-community-set-birthday (community birthday)
1358   "Set the birthday of COMMUNITY."
1359   (unless (mixi-community-p community)
1360     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1361   (aset (cdr community) 3 birthday))
1362
1363 (defun mixi-community-set-owner (community owner)
1364   "Set the owner of COMMUNITY."
1365   (unless (mixi-community-p community)
1366     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1367   (unless (mixi-friend-p owner)
1368     (signal 'wrong-type-argument (list 'mixi-friend-p owner)))
1369   (aset (cdr community) 4 owner))
1370
1371 (defun mixi-community-set-category (community category)
1372   "Set the category of COMMUNITY."
1373   (unless (mixi-community-p community)
1374     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1375   (aset (cdr community) 5 category))
1376
1377 (defun mixi-community-set-members (community members)
1378   "Set the name of COMMUNITY."
1379   (unless (mixi-community-p community)
1380     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1381   (aset (cdr community) 6 members))
1382
1383 (defun mixi-community-set-open-level (community open-level)
1384   "Set the name of COMMUNITY."
1385   (unless (mixi-community-p community)
1386     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1387   (aset (cdr community) 7 open-level))
1388
1389 (defun mixi-community-set-authority (community authority)
1390   "Set the name of COMMUNITY."
1391   (unless (mixi-community-p community)
1392     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1393   (aset (cdr community) 8 authority))
1394
1395 (defun mixi-community-set-description (community description)
1396   "Set the name of COMMUNITY."
1397   (unless (mixi-community-p community)
1398     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1399   (aset (cdr community) 9 description))
1400
1401 (defmacro mixi-community-list-page (&optional friend)
1402   `(concat "/list_community.pl?page=%d"
1403            (when ,friend (concat "&id=" (mixi-friend-id ,friend)))))
1404
1405 (defconst mixi-community-list-id-regexp
1406   "<a href=view_community\\.pl\\?id=\\([0-9]+\\)")
1407 (defconst mixi-community-list-name-regexp
1408   "<td valign=middle>\\(.+\\)([0-9]+)</td>")
1409
1410 (defun mixi-get-communities (&rest args)
1411   "Get communities of FRIEND."
1412   (when (> (length args) 2)
1413     (signal 'wrong-number-of-arguments
1414             (list 'mixi-get-communities (length args))))
1415   (let ((friend (nth 0 args))
1416         (range (nth 1 args)))
1417     (when (or (not (mixi-friend-p friend)) (mixi-friend-p range))
1418       (setq friend (nth 1 args))
1419       (setq range (nth 0 args)))
1420     (unless (or (null friend) (mixi-friend-p friend))
1421       (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
1422     (let ((ids (mixi-get-matched-items (mixi-community-list-page friend)
1423                                        mixi-community-list-id-regexp
1424                                        range))
1425           (names (mixi-get-matched-items (mixi-community-list-page friend)
1426                                          mixi-community-list-name-regexp
1427                                          range)))
1428       (let ((index 0)
1429             ret)
1430         (while (< index (length ids))
1431           (setq ret (cons (mixi-make-community (nth 0 (nth index ids))
1432                                                (nth 0 (nth index names))) ret))
1433           (incf index))
1434         (reverse ret)))))
1435
1436 (defmacro mixi-search-community-list-page (keyword)
1437   `(concat "/search_community.pl?page=%d&&sort=date&type=com&submit=main"
1438            "&keyword=" (mixi-url-encode-and-quote-percent-string keyword)
1439            "&category_id=0"))
1440
1441 (defconst mixi-search-community-list-regexp
1442   "<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>
1443 <td NOWRAP WIDTH=90 BGCOLOR=#FDF9F2><font COLOR=#996600>¥³¥ß¥å¥Ë¥Æ¥£Ì¾</font></td>
1444 <td COLSPAN=2 WIDTH=370 BGCOLOR=#FFFFFF>\\([^<]+\\)</td></tr>")
1445
1446 ;; FIXME: Support category.
1447 (defun mixi-search-communities (keyword &optional range)
1448   (let ((items (mixi-get-matched-items (mixi-search-community-list-page
1449                                         keyword)
1450                                        mixi-search-community-list-regexp
1451                                        range)))
1452     (mapcar (lambda (item)
1453               (mixi-make-community (nth 0 item) (nth 1 item)))
1454             items)))
1455
1456 ;; Topic object.
1457 (defvar mixi-topic-cache (make-hash-table :test 'equal))
1458 (defun mixi-make-topic (community id &optional time title owner content)
1459   "Return a topic object."
1460   (mixi-make-cache (list (mixi-community-id community) id)
1461                    (cons 'mixi-topic (vector nil community id time title owner
1462                                              content))
1463                    mixi-topic-cache))
1464
1465 (defconst mixi-topic-url-regexp
1466   "/view_bbs\\.pl\\?id=\\([0-9]+\\)\\(&comment_count=[0-9]+\\)?&comm_id=\\([0-9]+\\)")
1467
1468 (defun mixi-make-topic-from-url (url)
1469   "Return a topic object from URL."
1470   (when (string-match mixi-topic-url-regexp url)
1471     (let ((id (match-string 1 url))
1472           (community-id (match-string 3 url)))
1473       (mixi-make-topic (mixi-make-community community-id) id))))
1474
1475 (defmacro mixi-topic-p (topic)
1476   `(eq (mixi-object-class ,topic) 'mixi-topic))
1477
1478 (defmacro mixi-topic-page (topic)
1479   `(concat "/view_bbs.pl?id=" (mixi-topic-id ,topic)
1480            "&comm_id=" (mixi-community-id (mixi-topic-community ,topic))))
1481
1482 (defconst mixi-topic-time-regexp
1483   "<td rowspan=\"3\" width=\"110\" bgcolor=\"#ffd8b0\" align=\"center\" valign=\"top\" nowrap>\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü<br>\\([0-9]+\\):\\([0-9]+\\)</td>")
1484 (defconst mixi-topic-title-regexp
1485   "<td bgcolor=\"#fff4e0\">&nbsp;\\([^<]+\\)</td>")
1486 (defconst mixi-topic-owner-regexp
1487   "<td bgcolor=\"#fdf9f2\">&nbsp;<font color=\"#dfb479\"></font>&nbsp;<a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.*?\\)\\(¤µ¤ó\\)?</a>")
1488 (defconst mixi-topic-content-regexp
1489   "<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>")
1490
1491 (defun mixi-topic-realize (topic)
1492   "Realize a TOPIC."
1493   ;; FIXME: Check a expiration of cache?
1494   (unless (mixi-object-realize-p topic)
1495     (with-mixi-retrieve (mixi-topic-page topic)
1496       (if (string-match mixi-topic-time-regexp buffer)
1497           (mixi-topic-set-time
1498            topic (encode-time 0 (string-to-number (match-string 5 buffer))
1499                               (string-to-number (match-string 4 buffer))
1500                               (string-to-number (match-string 3 buffer))
1501                               (string-to-number (match-string 2 buffer))
1502                               (string-to-number (match-string 1 buffer))))
1503         (signal 'error (list 'cannot-find-time topic)))
1504       (if (string-match mixi-topic-title-regexp buffer)
1505           (mixi-topic-set-title topic (match-string 1 buffer))
1506         (signal 'error (list 'cannot-find-title topic)))
1507       (if (string-match mixi-topic-owner-regexp buffer)
1508           (mixi-topic-set-owner topic
1509                                 (mixi-make-friend (match-string 1 buffer)
1510                                                   (match-string 2 buffer)))
1511         (signal 'error (list 'cannot-find-owner topic)))
1512       (if (string-match mixi-topic-content-regexp buffer)
1513           (mixi-topic-set-content topic (match-string 2 buffer))
1514         (signal 'error (list 'cannot-find-content topic))))
1515     (mixi-object-touch topic)))
1516
1517 (defun mixi-topic-community (topic)
1518   "Return the community of TOPIC."
1519   (unless (mixi-topic-p topic)
1520     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1521   (aref (cdr topic) 1))
1522
1523 (defun mixi-topic-id (topic)
1524   "Return the id of TOPIC."
1525   (unless (mixi-topic-p topic)
1526     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1527   (aref (cdr topic) 2))
1528
1529 (defun mixi-topic-time (topic)
1530   "Return the time of TOPIC."
1531   (unless (mixi-topic-p topic)
1532     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1533   (mixi-topic-realize topic)
1534   (aref (cdr topic) 3))
1535
1536 (defun mixi-topic-title (topic)
1537   "Return the title of TOPIC."
1538   (unless (mixi-topic-p topic)
1539     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1540   (mixi-topic-realize topic)
1541   (aref (cdr topic) 4))
1542
1543 (defun mixi-topic-owner (topic)
1544   "Return the owner of TOPIC."
1545   (unless (mixi-topic-p topic)
1546     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1547   (mixi-topic-realize topic)
1548   (aref (cdr topic) 5))
1549
1550 (defun mixi-topic-content (topic)
1551   "Return the content of TOPIC."
1552   (unless (mixi-topic-p topic)
1553     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1554   (mixi-topic-realize topic)
1555   (aref (cdr topic) 6))
1556
1557 (defun mixi-topic-set-time (topic time)
1558   "Set the time of TOPIC."
1559   (unless (mixi-topic-p topic)
1560     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1561   (aset (cdr topic) 3 time))
1562
1563 (defun mixi-topic-set-title (topic title)
1564   "Set the title of TOPIC."
1565   (unless (mixi-topic-p topic)
1566     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1567   (aset (cdr topic) 4 title))
1568
1569 (defun mixi-topic-set-owner (topic owner)
1570   "Set the owner of TOPIC."
1571   (unless (mixi-topic-p topic)
1572     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1573   (unless (mixi-friend-p owner)
1574     (signal 'wrong-type-argument (list 'mixi-friend-p owner)))
1575   (aset (cdr topic) 5 owner))
1576
1577 (defun mixi-topic-set-content (topic content)
1578   "Set the content of TOPIC."
1579   (unless (mixi-topic-p topic)
1580     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1581   (aset (cdr topic) 6 content))
1582
1583 ;; Event object.
1584 (defvar mixi-event-cache (make-hash-table :test 'equal))
1585 (defun mixi-make-event (community id &optional time title owner date place
1586                                   detail limit members)
1587   "Return a event object."
1588   (mixi-make-cache (list (mixi-community-id community) id)
1589                    (cons 'mixi-event (vector nil community id time title owner
1590                                              date place detail limit members))
1591                    mixi-event-cache))
1592
1593 (defconst mixi-event-url-regexp
1594   "/view_event\\.pl\\?id=\\([0-9]+\\)\\(&comment_count=[0-9]+\\)?&comm_id=\\([0-9]+\\)")
1595
1596 (defun mixi-make-event-from-url (url)
1597   "Return a event object from URL."
1598   (when (string-match mixi-event-url-regexp url)
1599     (let ((id (match-string 1 url))
1600           (community-id (match-string 3 url)))
1601       (mixi-make-event (mixi-make-community community-id) id))))
1602
1603 (defmacro mixi-event-p (event)
1604   `(eq (mixi-object-class ,event) 'mixi-event))
1605
1606 (defmacro mixi-event-page (event)
1607   `(concat "/view_event.pl?id=" (mixi-event-id ,event)
1608            "&comm_id=" (mixi-community-id (mixi-event-community ,event))))
1609
1610 (defconst mixi-event-time-regexp
1611   "<td ROWSPAN=11 BGCOLOR=#FFD8B0 ALIGN=center VALIGN=top WIDTH=110>
1612 \\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü<br>
1613 \\([0-9]+\\):\\([0-9]+\\)</td>")
1614 (defconst mixi-event-title-regexp
1615   "<td bgcolor=#FFF4E0 width=410>&nbsp;\\([^<]+\\)</td>")
1616 (defconst mixi-event-owner-regexp
1617   "<td BGCOLOR=#FDF9F2>&nbsp;<a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.*\\)</a>")
1618 (defconst mixi-event-date-regexp
1619   "<td BGCOLOR=#FFFFFF ALIGN=center NOWRAP>³«ºÅÆü»þ</td>
1620 <td BGCOLOR=#FFFFFF>
1621 &nbsp;\\(.+\\)
1622 </td>")
1623 (defconst mixi-event-place-regexp
1624   "<td BGCOLOR=#FFFFFF ALIGN=center NOWRAP>³«ºÅ¾ì½ê</td>
1625 <td BGCOLOR=#FFFFFF>
1626 &nbsp;\\(.+\\)
1627 </td>")
1628 (defconst mixi-event-detail-regexp
1629   "<td BGCOLOR=#FFFFFF ALIGN=center NOWRAP>¾ÜºÙ</td>
1630 <td BGCOLOR=#FFFFFF><table BORDER=0 CELLSPACING=0 CELLPADDING=5><tr><td CLASS=h120>\\(.+\\)</td></tr></table></td>")
1631 (defconst mixi-event-limit-regexp
1632   "<td BGCOLOR=#FFFFFF ALIGN=center NOWRAP>Ê罸´ü¸Â</td>
1633 <td BGCOLOR=#FFFFFF>&nbsp;\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü</td>")
1634 (defconst mixi-event-members-regexp
1635   "<td BGCOLOR=#FFFFFF ALIGN=center NOWRAP>»²²Ã¼Ô</td>
1636 <td BGCOLOR=#FFFFFF>
1637 <table BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH=100%>
1638 <tr>
1639 <td>&nbsp;\\(.+\\)</td>")
1640
1641 (defun mixi-event-realize (event)
1642   "Realize a EVENT."
1643   ;; FIXME: Check a expiration of cache?
1644   (unless (mixi-object-realize-p event)
1645     (with-mixi-retrieve (mixi-event-page event)
1646       (if (string-match mixi-event-time-regexp buffer)
1647           (mixi-event-set-time
1648            event (encode-time 0 (string-to-number (match-string 5 buffer))
1649                               (string-to-number (match-string 4 buffer))
1650                               (string-to-number (match-string 3 buffer))
1651                               (string-to-number (match-string 2 buffer))
1652                               (string-to-number (match-string 1 buffer))))
1653         (signal 'error (list 'cannot-find-time event)))
1654       (if (string-match mixi-event-title-regexp buffer)
1655           (mixi-event-set-title event (match-string 1 buffer))
1656         (signal 'error (list 'cannot-find-title event)))
1657       (if (string-match mixi-event-owner-regexp buffer)
1658           (mixi-event-set-owner event
1659                                 (mixi-make-friend (match-string 1 buffer)
1660                                                   (match-string 2 buffer)))
1661         (signal 'error (list 'cannot-find-owner event)))
1662       (if (string-match mixi-event-date-regexp buffer)
1663           (mixi-event-set-date event (match-string 1 buffer))
1664         (signal 'error (list 'cannot-find-date event)))
1665       (if (string-match mixi-event-place-regexp buffer)
1666           (mixi-event-set-place event (match-string 1 buffer))
1667         (signal 'error (list 'cannot-find-place event)))
1668       (if (string-match mixi-event-detail-regexp buffer)
1669           (mixi-event-set-detail event (match-string 1 buffer))
1670         (signal 'error (list 'cannot-find-detail event)))
1671       (when (string-match mixi-event-limit-regexp buffer)
1672         (mixi-event-set-limit
1673          event (encode-time 0 0 0 (string-to-number (match-string 3 buffer))
1674                             (string-to-number (match-string 2 buffer))
1675                             (string-to-number (match-string 1 buffer)))))
1676       (if (string-match mixi-event-members-regexp buffer)
1677           (mixi-event-set-members event (match-string 1 buffer))
1678         (signal 'error (list 'cannot-find-members event))))
1679     (mixi-object-touch event)))
1680
1681 (defun mixi-event-community (event)
1682   "Return the community of EVENT."
1683   (unless (mixi-event-p event)
1684     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1685   (aref (cdr event) 1))
1686
1687 (defun mixi-event-id (event)
1688   "Return the id of EVENT."
1689   (unless (mixi-event-p event)
1690     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1691   (aref (cdr event) 2))
1692
1693 (defun mixi-event-time (event)
1694   "Return the time of EVENT."
1695   (unless (mixi-event-p event)
1696     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1697   (mixi-event-realize event)
1698   (aref (cdr event) 3))
1699
1700 (defun mixi-event-title (event)
1701   "Return the title of EVENT."
1702   (unless (mixi-event-p event)
1703     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1704   (mixi-event-realize event)
1705   (aref (cdr event) 4))
1706
1707 (defun mixi-event-owner (event)
1708   "Return the owner of EVENT."
1709   (unless (mixi-event-p event)
1710     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1711   (mixi-event-realize event)
1712   (aref (cdr event) 5))
1713
1714 (defun mixi-event-date (event)
1715   "Return the date of EVENT."
1716   (unless (mixi-event-p event)
1717     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1718   (mixi-event-realize event)
1719   (aref (cdr event) 6))
1720
1721 (defun mixi-event-place (event)
1722   "Return the place of EVENT."
1723   (unless (mixi-event-p event)
1724     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1725   (mixi-event-realize event)
1726   (aref (cdr event) 7))
1727
1728 (defun mixi-event-detail (event)
1729   "Return the detail of EVENT."
1730   (unless (mixi-event-p event)
1731     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1732   (mixi-event-realize event)
1733   (aref (cdr event) 8))
1734
1735 (defun mixi-event-limit (event)
1736   "Return the limit of EVENT."
1737   (unless (mixi-event-p event)
1738     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1739   (mixi-event-realize event)
1740   (aref (cdr event) 9))
1741
1742 (defun mixi-event-members (event)
1743   "Return the members of EVENT."
1744   (unless (mixi-event-p event)
1745     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1746   (mixi-event-realize event)
1747   (aref (cdr event) 10))
1748
1749 (defun mixi-event-set-time (event time)
1750   "Set the time of EVENT."
1751   (unless (mixi-event-p event)
1752     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1753   (aset (cdr event) 3 time))
1754
1755 (defun mixi-event-set-title (event title)
1756   "Set the title of EVENT."
1757   (unless (mixi-event-p event)
1758     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1759   (aset (cdr event) 4 title))
1760
1761 (defun mixi-event-set-owner (event owner)
1762   "Set the owner of EVENT."
1763   (unless (mixi-event-p event)
1764     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1765   (unless (mixi-friend-p owner)
1766     (signal 'wrong-type-argument (list 'mixi-friend-p owner)))
1767   (aset (cdr event) 5 owner))
1768
1769 (defun mixi-event-set-date (event date)
1770   "Set the date of EVENT."
1771   (unless (mixi-event-p event)
1772     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1773   (aset (cdr event) 6 date))
1774
1775 (defun mixi-event-set-place (event place)
1776   "Set the place of EVENT."
1777   (unless (mixi-event-p event)
1778     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1779   (aset (cdr event) 7 place))
1780
1781 (defun mixi-event-set-detail (event detail)
1782   "Set the detail of EVENT."
1783   (unless (mixi-event-p event)
1784     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1785   (aset (cdr event) 8 detail))
1786
1787 (defun mixi-event-set-limit (event limit)
1788   "Set the limit of EVENT."
1789   (unless (mixi-event-p event)
1790     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1791   (aset (cdr event) 9 limit))
1792
1793 (defun mixi-event-set-members (event members)
1794   "Set the members of EVENT."
1795   (unless (mixi-event-p event)
1796     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1797   (aset (cdr event) 10 members))
1798
1799 ;; Bbs object.
1800 (defalias 'mixi-bbs-owner 'mixi-object-owner)
1801 (defalias 'mixi-bbs-id 'mixi-object-id)
1802 (defalias 'mixi-bbs-time 'mixi-object-time)
1803 (defalias 'mixi-bbs-title 'mixi-object-title)
1804 (defalias 'mixi-bbs-content 'mixi-object-content)
1805
1806 (defmacro mixi-bbs-list-page (community)
1807   `(concat "/list_bbs.pl?page=%d"
1808            "&id=" (mixi-community-id ,community)))
1809
1810 (defconst mixi-bbs-list-regexp
1811   "<a href=view_\\(bbs\\|event\\)\\.pl\\?id=\\([0-9]+\\)")
1812
1813 (defun mixi-get-bbses (community &optional range)
1814   "Get bbese of COMMUNITY."
1815   (unless (mixi-community-p community)
1816     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1817   (let ((items (mixi-get-matched-items (mixi-bbs-list-page community)
1818                                        mixi-bbs-list-regexp
1819                                        range)))
1820     (mapcar (lambda (item)
1821               (let ((name (nth 0 item)))
1822                 (when (string= name "bbs")
1823                   (setq name "topic"))
1824                 (let ((func (intern (concat "mixi-make-" name))))
1825                   (funcall func community (nth 1 item)))))
1826             items)))
1827
1828 (defmacro mixi-new-bbs-list-page ()
1829   `(concat "/new_bbs.pl?page=%d"))
1830
1831 (defconst mixi-new-bbs-list-regexp
1832   "<a href=\"view_\\(bbs\\|event\\)\\.pl\\?id=\\([0-9]+\\)&comment_count=[0-9]+&comm_id=\\([0-9]+\\)\" class=\"new_link\">")
1833
1834 (defun mixi-get-new-bbses (&optional range)
1835   "Get new topics."
1836   (let ((items (mixi-get-matched-items (mixi-new-bbs-list-page)
1837                                        mixi-new-bbs-list-regexp
1838                                        range)))
1839     (mapcar (lambda (item)
1840               (let ((name (nth 0 item)))
1841                 (when (string= name "bbs")
1842                   (setq name "topic"))
1843                 (let ((func (intern (concat "mixi-make-" name))))
1844                   (funcall func (mixi-make-community (nth 2 item))
1845                            (nth 1 item)))))
1846             items)))
1847
1848 (defmacro mixi-search-bbs-list-page (keyword)
1849   `(concat "/search_topic.pl?page=%d&type=top&submit=search"
1850            "&keyword=" (mixi-url-encode-and-quote-percent-string keyword)
1851            "&community_id=0&category_id=0"))
1852
1853 (defconst mixi-search-bbs-list-regexp
1854   "<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>")
1855
1856 ;; FIXME: Support community and category.
1857 (defun mixi-search-bbses (keyword &optional range)
1858   (let ((items (mixi-get-matched-items (mixi-search-bbs-list-page keyword)
1859                                        mixi-search-bbs-list-regexp
1860                                        range)))
1861     (mapcar (lambda (item)
1862               (let ((name (nth 0 item)))
1863                 (when (string= name "bbs")
1864                   (setq name "topic"))
1865                 (let ((func (intern (concat "mixi-make-" name))))
1866                   (funcall func (mixi-make-community (nth 2 item))
1867                            (nth 1 item)))))
1868             items)))
1869
1870 ;; Comment object.
1871 (defun mixi-make-comment (parent owner time content)
1872   "Return a comment object."
1873   (cons 'mixi-comment (vector parent owner time content)))
1874
1875 (defmacro mixi-comment-p (comment)
1876   `(eq (mixi-object-class ,comment) 'mixi-comment))
1877
1878 (defun mixi-comment-parent (comment)
1879   "Return the parent of COMMENT."
1880   (unless (mixi-comment-p comment)
1881     (signal 'wrong-type-argument (list 'mixi-comment-p comment)))
1882   (aref (cdr comment) 0))
1883
1884 (defun mixi-comment-owner (comment)
1885   "Return the owner of COMMENT."
1886   (unless (mixi-comment-p comment)
1887     (signal 'wrong-type-argument (list 'mixi-comment-p comment)))
1888   (aref (cdr comment) 1))
1889
1890 (defun mixi-comment-time (comment)
1891   "Return the time of COMMENT."
1892   (unless (mixi-comment-p comment)
1893     (signal 'wrong-type-argument (list 'mixi-comment-p comment)))
1894   (aref (cdr comment) 2))
1895
1896 (defun mixi-comment-content (comment)
1897   "Return the content of COMMENT."
1898   (unless (mixi-comment-p comment)
1899     (signal 'wrong-type-argument (list 'mixi-comment-p comment)))
1900   (aref (cdr comment) 3))
1901
1902 (defun mixi-diary-comment-list-page (diary)
1903   (concat "/view_diary.pl?full=1"
1904           "&id=" (mixi-diary-id diary)
1905           "&owner_id=" (mixi-friend-id (mixi-diary-owner diary))))
1906
1907 ;; FIXME: Split regexp to time, owner(id and nick) and contents.
1908 (defconst mixi-diary-comment-list-regexp
1909 "<td rowspan=\"2\" align=\"center\" width=\"95\" bgcolor=\"#f2ddb7\" nowrap>
1910 \\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü<br>\\([0-9]+\\):\\([0-9]+\\)\\(<br>
1911 <input type=checkbox name=comment_id value=\".+\">
1912 \\|\\)
1913 </td>
1914 <td ALIGN=center BGCOLOR=#FDF9F2 WIDTH=430>
1915 <table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"410\">
1916 <tr>
1917 \\(<td>\\)
1918 <a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.*\\)</a>
1919
1920 \\(<font color=\"#f2ddb7\">|</font> <a href=[^>]+>ºï½ü</a>
1921
1922 \\|\\)</td>
1923 </tr>
1924 </table>
1925 </td>
1926 </tr>
1927 <!-- ËÜʸ : start -->
1928 <tr>
1929 <td bgcolor=\"#ffffff\">
1930 <table BORDER=0 CELLSPACING=0 CELLPADDING=[35] WIDTH=410>
1931 <tr>
1932 <td CLASS=h12>
1933 \\(.+\\)
1934 </td></tr></table>")
1935
1936 (defun mixi-topic-comment-list-page (topic)
1937   (concat "/view_bbs.pl?page=all"
1938           "&id=" (mixi-topic-id topic)
1939           "&comm_id=" (mixi-community-id (mixi-topic-community topic))))
1940
1941 ;; FIXME: Split regexp to time, owner(id and nick) and contents.
1942 (defconst mixi-topic-comment-list-regexp
1943   "<tr valign=\"top\">
1944 <td rowspan=\"2\" width=\"110\" bgcolor=\"#f2ddb7\" align=\"center\" nowrap>
1945 \\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü<br>
1946 \\([0-9]+\\):\\([0-9]+\\)<br>
1947 \\(<input type=\"checkbox\" name=\"comment_id\" value=\".+\">
1948 \\|\\)</td>
1949 <td bgcolor=\"#fdf9f2\">&nbsp;<font color=\"#f8a448\">
1950 <b>[^<]+</b>:</font>&nbsp;
1951 \\(
1952 \\|\\) *<a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.*\\)</a>
1953
1954 ?\\(
1955
1956 \\|<font color=\"#f2ddb7\">|&nbsp;</font><a href=\"delete_bbs_comment\\.pl\\?id=[0-9]+&comm_id=[0-9]+&comment_id=[0-9]+\">ºï½ü</a>
1957 \\|\\)</td>
1958 </tr>
1959 <tr>
1960 <td bgcolor=\"#ffffff\" align=\"center\">
1961 <table border=\"0\" cellspacing=\"0\" cellpadding=\"5\" width=\"500\">
1962 <tr>
1963 <td class=\"h120\">
1964
1965 \\(.+\\)
1966 </td>
1967 </tr>
1968 </table>
1969 </td>
1970 </tr>")
1971
1972 (defun mixi-event-comment-list-page (event)
1973   (concat "/view_event.pl?page=all"
1974           "&id=" (mixi-event-id event)
1975           "&comm_id=" (mixi-community-id (mixi-event-community event))))
1976
1977 ;; FIXME: Split regexp to time, owner(id and nick) and contents.
1978 (defconst mixi-event-comment-list-regexp
1979   "<tr>
1980 <td ROWSPAN=2 ALIGN=center BGCOLOR=#F2DDB7 WIDTH=110>
1981 \\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü<br>
1982 \\([0-9]+\\):\\([0-9]+\\)<br>
1983 \\(</td>\\)
1984 \\(<td BGCOLOR=#FDF9F2>\\)
1985 <font COLOR=#F8A448><b>[^<]+</b> :</font>
1986 <a HREF=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.*\\)</a>
1987
1988 </td>
1989 </tr>
1990 \\(<tr>\\)
1991 <td ALIGN=center BGCOLOR=#FFFFFF>
1992 <table BORDER=0 CELLSPACING=0 CELLPADDING=5 WIDTH=500>
1993 <tr><td CLASS=h120>\\(.+\\)</td></tr>
1994 </table>
1995 </td>
1996 </tr>")
1997
1998 (defun mixi-get-comments (parent &optional range)
1999   "Get comments of PARENT."
2000   (unless (mixi-object-p parent)
2001     (signal 'wrong-type-argument (list 'mixi-object-p parent)))
2002   (let* ((name (mixi-object-name parent))
2003          (list-page (intern (concat mixi-object-prefix name
2004                                     "-comment-list-page")))
2005          (regexp (eval (intern (concat mixi-object-prefix name
2006                                        "-comment-list-regexp")))))
2007     (let ((items (mixi-get-matched-items
2008                   (funcall list-page parent) regexp)))
2009       (let (list)
2010         (catch 'stop
2011           (mapc (lambda (item)
2012                   (when (and (numberp range)
2013                              (>= (length list) range))
2014                     (throw 'stop nil))
2015                   (setq list (cons item list)))
2016                 (reverse items)))
2017         (setq items (reverse list)))
2018       (mapcar (lambda (item)
2019                 (mixi-make-comment parent (mixi-make-friend
2020                                            (nth 7 item) (nth 8 item))
2021                                    (encode-time
2022                                     0
2023                                     (string-to-number (nth 4 item))
2024                                     (string-to-number (nth 3 item))
2025                                     (string-to-number (nth 2 item))
2026                                     (string-to-number (nth 1 item))
2027                                     (string-to-number (nth 0 item)))
2028                                    (nth 10 item)))
2029               items))))
2030
2031 (defmacro mixi-new-comment-list-page ()
2032   `(concat "/new_comment.pl?page=%d"))
2033
2034 (defconst mixi-new-comment-list-regexp
2035   "<a href=\"view_diary\\.pl\\?id=\\([0-9]+\\)&owner_id=\\([0-9]+\\)&comment_count=[0-9]+\" class=\"new_link\">")
2036
2037 (defun mixi-get-new-comments (&optional range)
2038   "Get new comments."
2039   (let ((items (mixi-get-matched-items (mixi-new-comment-list-page)
2040                                        mixi-new-comment-list-regexp
2041                                        range)))
2042     (mapcar (lambda (item)
2043               (mixi-make-diary (mixi-make-friend (nth 1 item)) (nth 0 item)))
2044             items)))
2045
2046 ;; Message object.
2047 (defconst mixi-message-box-list '(inbox outbox savebox thrash)) ; thrash?
2048
2049 (defmacro mixi-message-box-p (box)
2050   `(when (memq ,box mixi-message-box-list)
2051      t))
2052
2053 (defun mixi-message-box-name (box)
2054   "Return the name of BOX."
2055   (unless (mixi-message-box-p box)
2056     (signal 'wrong-type-argument (list 'mixi-message-box-p box)))
2057   (symbol-name box))
2058
2059 (defvar mixi-message-cache (make-hash-table :test 'equal))
2060 (defun mixi-make-message (id box &optional owner title time content)
2061   "Return a message object."
2062   (mixi-make-cache (list id box)
2063                    (cons 'mixi-message (vector nil id box owner title time
2064                                                content))
2065                    mixi-message-cache))
2066
2067 (defconst mixi-message-url-regexp
2068   "/view_message\\.pl\\?id=\\([a-z0-9]+\\)&box=\\([a-z]+\\)")
2069
2070 (defun mixi-make-message-from-url (url)
2071   "Return a message object from URL."
2072   (when (string-match mixi-message-url-regexp url)
2073     (let ((id (match-string 1 url))
2074           (box (match-string 2 url)))
2075       (mixi-make-message id box))))
2076
2077 (defmacro mixi-message-p (message)
2078   `(eq (mixi-object-class ,message) 'mixi-message))
2079
2080 (defmacro mixi-message-page (message)
2081   `(concat "/view_message.pl?id=" (mixi-message-id ,message)
2082            "&box=" (mixi-message-box ,message)))
2083
2084 (defconst mixi-message-owner-regexp
2085   "<font COLOR=#996600>\\(º¹½Ð¿Í\\|°¸&nbsp;Àè\\)</font>&nbsp;:&nbsp;<a HREF=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.*\\)\\(</a>\\|</td>\\)")
2086 (defconst mixi-message-title-regexp
2087 "<font COLOR=#996600>·ï\\(¡¡\\|&nbsp;\\)̾</font>&nbsp;:&nbsp;\\(.+\\)\n?</td>")
2088 (defconst mixi-message-time-regexp
2089 "<font COLOR=#996600>Æü\\(¡¡\\|&nbsp;\\)ÉÕ</font>&nbsp;:&nbsp;\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü \\([0-9]+\\)»þ\\([0-9]+\\)ʬ&nbsp;&nbsp;")
2090 (defconst mixi-message-content-regexp
2091   "<tr><td CLASS=h120>\\(.+\\)</td></tr>")
2092
2093 (defun mixi-message-realize (message)
2094   "Realize a MESSAGE."
2095   (unless (mixi-object-realize-p message)
2096     (with-mixi-retrieve (mixi-message-page message)
2097       (if (string-match mixi-message-owner-regexp buffer)
2098           (mixi-message-set-owner message
2099                                   (mixi-make-friend (match-string 2 buffer)
2100                                                     (match-string 3 buffer)))
2101         (signal 'error (list 'cannot-find-owner message)))
2102       (if (string-match mixi-message-title-regexp buffer)
2103           (mixi-message-set-title message (match-string 2 buffer))
2104         (signal 'error (list 'cannot-find-title message)))
2105       (if (string-match mixi-message-time-regexp buffer)
2106           (mixi-message-set-time
2107            message (encode-time 0 (string-to-number (match-string 6 buffer))
2108                                 (string-to-number (match-string 5 buffer))
2109                                 (string-to-number (match-string 4 buffer))
2110                                 (string-to-number (match-string 3 buffer))
2111                                 (string-to-number (match-string 2 buffer))))
2112         (signal 'error (list 'cannot-find-time message)))
2113       (if (string-match mixi-message-content-regexp buffer)
2114           (mixi-message-set-content message (match-string 1 buffer))
2115         (signal 'error (list 'cannot-find-content message))))
2116     (mixi-object-touch message)))
2117
2118 (defun mixi-message-id (message)
2119   "Return the id of MESSAGE."
2120   (unless (mixi-message-p message)
2121     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2122   (aref (cdr message) 1))
2123
2124 (defun mixi-message-box (message)
2125   "Return the box of MESSAGE."
2126   (unless (mixi-message-p message)
2127     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2128   (aref (cdr message) 2))
2129
2130 (defun mixi-message-owner (message)
2131   "Return the owner of MESSAGE."
2132   (unless (mixi-message-p message)
2133     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2134   (mixi-message-realize message)
2135   (aref (cdr message) 3))
2136
2137 (defun mixi-message-title (message)
2138   "Return the title of MESSAGE."
2139   (unless (mixi-message-p message)
2140     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2141   (mixi-message-realize message)
2142   (aref (cdr message) 4))
2143
2144 (defun mixi-message-time (message)
2145   "Return the date of MESSAGE."
2146   (unless (mixi-message-p message)
2147     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2148   (mixi-message-realize message)
2149   (aref (cdr message) 5))
2150
2151 (defun mixi-message-content (message)
2152   "Return the content of MESSAGE."
2153   (unless (mixi-message-p message)
2154     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2155   (mixi-message-realize message)
2156   (aref (cdr message) 6))
2157
2158 (defun mixi-message-set-owner (message owner)
2159   "Set the owner of MESSAGE."
2160   (unless (mixi-message-p message)
2161     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2162   (aset (cdr message) 3 owner))
2163
2164 (defun mixi-message-set-title (message title)
2165   "Set the title of MESSAGE."
2166   (unless (mixi-message-p message)
2167     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2168   (aset (cdr message) 4 title))
2169
2170 (defun mixi-message-set-time (message time)
2171   "Set the date of MESSAGE."
2172   (unless (mixi-message-p message)
2173     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2174   (aset (cdr message) 5 time))
2175
2176 (defun mixi-message-set-content (message content)
2177   "Set the content of MESSAGE."
2178   (unless (mixi-message-p message)
2179     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2180   (aset (cdr message) 6 content))
2181
2182 (defmacro mixi-message-list-page (&optional box)
2183   `(concat "/list_message.pl?page=%d"
2184            (when ,box (concat "&box=" ,box))))
2185
2186 (defconst mixi-message-list-regexp
2187   "<td><a HREF=\"view_message\\.pl\\?id=\\(.+\\)&box=\\(.+\\)\">")
2188
2189 (defun mixi-get-messages (&rest args)
2190   "Get messages."
2191   (when (> (length args) 2)
2192     (signal 'wrong-number-of-arguments
2193             (list 'mixi-get-messages (length args))))
2194   (let ((box (nth 0 args))
2195         (range (nth 1 args)))
2196     (when (or (not (mixi-message-box-p box))
2197               (mixi-message-box-p range))
2198       (setq box (nth 1 args))
2199       (setq range (nth 0 args)))
2200     (let ((items (mixi-get-matched-items
2201                   (mixi-message-list-page
2202                    (when box (mixi-message-box-name box)))
2203                   mixi-message-list-regexp
2204                   range)))
2205       (mapcar (lambda (item)
2206                 (mixi-make-message (nth 0 item) (nth 1 item)))
2207               items))))
2208
2209 ;; Introduction object.
2210 (defun mixi-make-introduction (parent owner content)
2211   "Return a introduction object."
2212   (cons 'mixi-introduction (vector parent owner content)))
2213
2214 (defmacro mixi-introduction-p (introduction)
2215   `(eq (mixi-object-class ,introduction) 'mixi-introduction))
2216
2217 (defun mixi-introduction-parent (introduction)
2218   "Return the parent of INTRODUCTION."
2219   (unless (mixi-introduction-p introduction)
2220     (signal 'wrong-type-argument (list 'mixi-introduction-p introduction)))
2221   (aref (cdr introduction) 0))
2222
2223 (defun mixi-introduction-owner (introduction)
2224   "Return the owner of INTRODUCTION."
2225   (unless (mixi-introduction-p introduction)
2226     (signal 'wrong-type-argument (list 'mixi-introduction-p introduction)))
2227   (aref (cdr introduction) 1))
2228
2229 (defun mixi-introduction-content (introduction)
2230   "Return the content of INTRODUCTION."
2231   (unless (mixi-introduction-p introduction)
2232     (signal 'wrong-type-argument (list 'mixi-introduction-p introduction)))
2233   (aref (cdr introduction) 3))
2234
2235 (defmacro mixi-introduction-list-page (&optional friend)
2236   `(concat "/show_intro.pl?page=%d"
2237            (when ,friend (concat "&id=" (mixi-friend-id ,friend)))))
2238
2239 (defconst mixi-introduction-list-regexp
2240   "<tr bgcolor=#FFFFFF>
2241 <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>
2242 \\(.*\\)</td></a>
2243
2244 <td WIDTH=480>
2245 \\(´Ø·¸¡§.+<br>
2246
2247
2248 \\(\\(.\\|\n<br>\\)+\\)\\|
2249 \\(\\(.\\|\n<br>\\)+\\)\\)
2250
2251
2252
2253
2254 </td>
2255 </tr>")
2256 (defconst mixi-my-introduction-list-regexp
2257   "<tr bgcolor=#FFFFFF>
2258 <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>
2259 \\(.*\\)</td></a>
2260
2261
2262 <td WIDTH=480>
2263 \\(´Ø·¸¡§.+<br>
2264
2265
2266 \\(\\(.\\|\n<br>\\)+\\)\\|
2267 \\(\\(.\\|\n<br>\\)+\\)\\)
2268
2269
2270 <br>
2271 <a href=\"edit_intro\\.pl\\?id=\\1&type=edit\">¤³¤Îͧ¿Í¤ò¾Ò²ð¤¹¤ë</a>
2272
2273
2274 <BR>
2275 <a href=\"delete_intro\\.pl\\?id=\\1\">ºï½ü</a>
2276
2277 </td>
2278 </tr>")
2279
2280 (defun mixi-get-introductions (&rest args)
2281   "Get introductions."
2282   (when (> (length args) 2)
2283     (signal 'wrong-number-of-arguments
2284             (list 'mixi-get-introduction (length args))))
2285   (let ((friend (nth 0 args))
2286         (range (nth 1 args)))
2287     (when (or (not (mixi-friend-p friend)) (mixi-friend-p range))
2288       (setq friend (nth 1 args))
2289       (setq range (nth 0 args)))
2290     (unless (or (null friend) (mixi-friend-p friend))
2291       (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
2292     (let* ((regexp (if friend mixi-introduction-list-regexp
2293                      mixi-my-introduction-list-regexp))
2294            (items (mixi-get-matched-items (mixi-introduction-list-page friend)
2295                                           regexp
2296                                           range)))
2297       (mapcar (lambda (item)
2298                 (mixi-make-introduction (or friend (mixi-make-me))
2299                                         (mixi-make-friend (nth 0 item)
2300                                                           (nth 1 item))
2301                                         (nth 2 item)))
2302               items))))
2303
2304 (provide 'mixi)
2305
2306 ;;; mixi.el ends here