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