* mixi.el (mixi-expand-url): New macro.
[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-get-communities
35 ;;  * mixi-get-topics
36 ;;  * mixi-get-new-topics
37 ;;  * mixi-get-comments
38 ;;  * mixi-get-new-comments
39 ;;  * mixi-get-messages
40 ;;  * mixi-get-introductions
41
42 ;; Example:
43 ;;
44 ;; Display newest 3 diaries like a mail format.
45 ;;
46 ;; (let ((max-numbers 3)
47 ;;       (buffer (generate-new-buffer "*temp*"))
48 ;;       (format "%Y/%m/%d %H:%M"))
49 ;;   (pop-to-buffer buffer)
50 ;;   (mapc (lambda (diary)
51 ;;        (let ((subject (mixi-diary-title diary))
52 ;;              ;; Don't get owner's nick at first for omitting a useless
53 ;;              ;; retrieval.
54 ;;              (from (mixi-friend-nick (mixi-diary-owner diary)))
55 ;;              (date (format-time-string format (mixi-diary-time diary)))
56 ;;              (body (mixi-diary-content diary)))
57 ;;          (insert "From: " from "\n"
58 ;;                  "Subject: " subject "\n"
59 ;;                  "Date: " date "\n\n"
60 ;;                  body "\n\n")))
61 ;;      (mixi-get-new-diaries max-numbers))
62 ;;   (set-buffer-modified-p nil)
63 ;;   (setq buffer-read-only t)
64 ;;   (goto-char (point-min)))
65 ;;
66 ;; Display newest 3 diaries including newest 3 comments like a mail format.
67 ;; Comments are displayed like a reply mail.
68 ;;
69 ;; (let ((max-numbers 3)
70 ;;       (buffer (generate-new-buffer "*temp*"))
71 ;;       (format "%Y/%m/%d %H:%M"))
72 ;;   (pop-to-buffer buffer)
73 ;;   (mapc (lambda (diary)
74 ;;        (let ((subject (mixi-diary-title diary))
75 ;;              ;; Don't get owner's nick at first for omitting a useless
76 ;;              ;; retrieval.
77 ;;              (from (mixi-friend-nick (mixi-diary-owner diary)))
78 ;;              (date (format-time-string format (mixi-diary-time diary)))
79 ;;              (body (mixi-diary-content diary)))
80 ;;          (insert "From: " from "\n"
81 ;;                  "Subject: " subject "\n"
82 ;;                  "Date: " date "\n\n"
83 ;;                  body "\n\n")
84 ;;          (mapc (lambda (comment)
85 ;;                  (let ((from (mixi-friend-nick
86 ;;                               (mixi-comment-owner comment)))
87 ;;                        (subject (concat "Re: " subject))
88 ;;                        (date (format-time-string
89 ;;                               format (mixi-comment-time comment)))
90 ;;                        (body (mixi-comment-content comment)))
91 ;;                    (insert "From: " from "\n"
92 ;;                            "Subject: " subject "\n"
93 ;;                            "Date: " date "\n\n"
94 ;;                            body "\n\n")))
95 ;;                (mixi-get-comments diary max-numbers))))
96 ;;      (mixi-get-new-diaries max-numbers))
97 ;;   (set-buffer-modified-p nil)
98 ;;   (setq buffer-read-only t)
99 ;;   (goto-char (point-min)))
100
101 ;;; Code:
102
103 (eval-when-compile (require 'cl))
104
105 (defgroup mixi nil
106   "API library for accessing to mixi."
107   :group 'hypermedia)
108
109 (defcustom mixi-url "http://mixi.jp"
110   "*The URL of mixi."
111   :type 'string
112   :group 'mixi)
113
114 (defcustom mixi-coding-system 'euc-jp
115   "*Coding system for mixi."
116   :type 'coding-system
117   :group 'mixi)
118
119 (defcustom mixi-curl-program "curl"
120   "*The program name of `curl'."
121   :type 'file
122   :group 'mixi)
123
124 (defcustom mixi-curl-cookie-file (expand-file-name "~/.mixi-cookies.txt")
125   "*The location of cookie file created by `curl'."
126   :type 'file
127   :group 'mixi)
128
129 (defcustom mixi-retrieve-function
130   (or (condition-case nil
131           (progn
132             (require 'url)
133             (if (fboundp 'url-retrieve-synchronously)
134                 'mixi-w3-retrieve))
135         (error))
136       (condition-case nil
137           (progn
138             (require 'w3m)
139             'mixi-w3m-retrieve)
140         (error))
141       (if (and (fboundp 'executable-find)
142                (executable-find mixi-curl-program))
143           'mixi-curl-retrieve)
144       (error "Can't set `mixi-retrieve-function'"))
145   "*The function for retrieving."
146   :type '(radio (const :tag "Using w3" mixi-w3-retrieve)
147                 (const :tag "Using w3m" mixi-w3m-retrieve)
148                 (const :tag "Using curl" mixi-curl-retrieve)
149                 (function :format "Other function: %v\n" :size 0))
150   :group 'mixi)
151
152 (defcustom mixi-default-email nil
153   "*Default E-mail address that is used to login automatically."
154   :type '(radio (string :tag "E-mail address")
155                 (const :tag "Asked when it is necessary" nil))
156   :group 'mixi)
157
158 (defcustom mixi-default-password nil
159   "*Default password that is used to login automatically."
160   :type '(radio (string :tag "Password")
161                 (const :tag "Asked when it is necessary" nil))
162   :group 'mixi)
163
164 (defcustom mixi-accept-adult-contents t
165   "*If non-nil, accept to access to the adult contents."
166   :type 'boolean
167   :group 'mixi)
168
169 (defcustom mixi-continuously-access-interval 4.0
170   "*Time interval between each mixi access.
171 Increase this value when unexpected error frequently occurs."
172   :type 'number
173   :group 'mixi)
174
175 (defcustom mixi-cache-expires 3600
176   "*Seconds for expiration of a cached object."
177   :type '(radio (integer :tag "Expired seconds")
178                 (const :tag "Don't expire" nil)
179                 (const :tag "Don't cache" t))
180   :group 'mixi)
181
182 ;; FIXME: Not implemented.
183 (defcustom mixi-cache-use-file t
184   "*If non-nil, caches are saved to files."
185   :type 'boolean
186   :group 'mixi)
187
188 (defcustom mixi-cache-directory (expand-file-name "~/.mixi")
189   "*Where to look for cache files."
190   :type 'directory
191   :group 'mixi)
192
193 (defvar mixi-me nil)
194
195 ;; Utilities.
196 (defmacro mixi-message (string)
197   `(concat "[mixi] " ,string))
198
199 (defconst mixi-message-adult-contents
200   "¤³¤Î¥Ú¡¼¥¸¤«¤éÀè¤Ï¥¢¥À¥ë¥È¡ÊÀ®¿Í¸þ¤±¡Ë¥³¥ó¥Æ¥ó¥Ä¤¬´Þ¤Þ¤ì¤Æ¤¤¤Þ¤¹¡£<br>
201 ±ÜÍ÷¤ËƱ°Õ¤µ¤ì¤¿Êý¤Î¤ß¡¢Àè¤Ø¤ª¿Ê¤ß¤¯¤À¤µ¤¤¡£")
202 (defconst mixi-message-continuously-accessing
203   "°ÂÄꤷ¤Æ¥µ¥¤¥È¤Î±¿±Ä¤ò¤ª¤³¤Ê¤¦°Ù¡¢´Ö³Ö¤ò¶õ¤±¤Ê¤¤Ï¢Â³Åª¤Ê¥Ú¡¼¥¸¤ÎÁ«°Ü¡¦¹¹<br>
204 ¿·¤ÏÀ©¸Â¤µ¤»¤Æ¤¤¤¿¤À¤¤¤Æ¤ª¤ê¤Þ¤¹¡£¤´ÌÂÏǤò¤ª¤«¤±¤¤¤¿¤·¤Þ¤¹¤¬¡¢¤·¤Ð¤é¤¯¤ª<br>
205 ÂÔ¤Á¤¤¤¿¤À¤¤¤Æ¤«¤éÁàºî¤ò¤ª¤³¤Ê¤Ã¤Æ¤¯¤À¤µ¤¤¡£")
206 (defconst mixi-warning-continuously-accessing
207   "´Ö³Ö¤ò¶õ¤±¤Ê¤¤Ï¢Â³Åª¤Ê¥Ú¡¼¥¸¤ÎÁ«°Ü¡¦¹¹¿·¤òÉÑÈˤˤª¤³¤Ê¤ï¤ì¤Æ¤¤¤ë¤³¤È¤¬¸«<br>
208 ¼õ¤±¤é¤ì¤Þ¤·¤¿¤Î¤Ç¡¢°ì»þŪ¤ËÁàºî¤òÄä»ß¤µ¤»¤Æ¤¤¤¿¤À¤­¤Þ¤¹¡£¿½¤·Ìõ¤´¤¶¤¤¤Þ<br>
209 ¤»¤ó¤¬¡¢¤·¤Ð¤é¤¯¤Î´Ö¤ªÂÔ¤Á¤¯¤À¤µ¤¤¡£")
210
211 (defun mixi-retrieve-1 (buffer url &optional post-data)
212   (when (string-match mixi-message-adult-contents buffer)
213     (if mixi-accept-adult-contents
214         (setq buffer (funcall mixi-retrieve-function url "submit=agree"))
215       (setq buffer (funcall mixi-retrieve-function (concat url "?")))))
216   (when (string-match mixi-warning-continuously-accessing buffer)
217     (error (mixi-message "Continuously accessing")))
218   (if (not (string-match mixi-message-continuously-accessing buffer))
219       buffer
220     (message (mixi-message "Waiting for continuously accessing..."))
221     (sit-for mixi-continuously-access-interval)
222     (funcall mixi-retrieve-function url post-data)))
223
224 (defmacro mixi-expand-url (url)
225   `(concat mixi-url ,url))
226
227 (defun mixi-w3-retrieve (url &optional post-data)
228   "Retrieve the URL and return gotten strings."
229   (if post-data
230       (progn
231         (setq url-request-method "POST")
232         (setq url-request-data post-data))
233     (setq url-request-method "GET")
234     (setq url-request-data nil))
235   (let* ((url (mixi-expand-url url))
236          (buffer (url-retrieve-synchronously url))
237          ret)
238     (unless (bufferp buffer)
239       (error (mixi-message "Cannot retrieve")))
240     (with-current-buffer buffer
241       (goto-char (point-min))
242       (if (re-search-forward "HTTP/[0-9.]+ 302 Moved" nil t)
243           (if (re-search-forward
244                (concat "Location: " mixi-url "\\(.+\\)") nil t)
245               (setq ret (mixi-w3-retrieve (match-string 1) post-data))
246             (setq ret (mixi-w3-retrieve "/home.pl" post-data)))
247         (unless (re-search-forward "HTTP/[0-9.]+ 200 OK" nil t)
248           (error (mixi-message "Cannot retrieve")))
249         (search-forward "\n\n")
250         (setq ret (mm-decode-coding-string
251                    (buffer-substring-no-properties (point) (point-max))
252                    mixi-coding-system))
253         (kill-buffer buffer)
254         (setq ret (mixi-retrieve-1 ret url post-data))))
255     ret))
256
257 (defun mixi-w3m-retrieve (url &optional post-data)
258   "Retrieve the URL and return gotten strings."
259   (let ((url (mixi-expand-url url)))
260     (with-temp-buffer
261       (if (not (string= (w3m-retrieve url nil nil post-data) "text/html"))
262           (error (mixi-message "Cannot retrieve"))
263         (w3m-decode-buffer url)
264         (let ((ret (buffer-substring-no-properties (point-min) (point-max))))
265           (mixi-retrieve-1 ret url post-data))))))
266
267 (defun mixi-curl-retrieve (url &optional post-data)
268   "Retrieve the URL and return gotten strings."
269   (with-temp-buffer
270     (if (fboundp 'set-buffer-multibyte)
271         (set-buffer-multibyte nil))
272     (let ((orig-mode (default-file-modes))
273           (coding-system-for-read 'binary)
274           (coding-system-for-write 'binary)
275           process ret)
276       (unwind-protect
277           (progn
278             (set-default-file-modes 448)
279             (setq process
280                   (apply #'start-process "curl" (current-buffer)
281                          mixi-curl-program
282                          (append (if post-data '("-d" "@-"))
283                                  (list "-i" "-L" "-s"
284                                        "-b" mixi-curl-cookie-file
285                                        "-c" mixi-curl-cookie-file
286                                        (mixi-expand-url url)))))
287             (set-process-sentinel process #'ignore))
288         (set-default-file-modes orig-mode))
289       (when post-data
290         (process-send-string process (concat post-data "\n"))
291         (process-send-eof process))
292       (while (eq (process-status process) 'run)
293         (accept-process-output process 1))
294       (goto-char (point-min))
295       (while (looking-at "HTTP/[0-9]+\\.[0-9]+ [13][0-9][0-9]")
296         (delete-region (point) (re-search-forward "\r?\n\r?\n")))
297       (unless (looking-at "HTTP/[0-9]+\\.[0-9]+ 200")
298         (error (mixi-message "Cannot retrieve")))
299       (delete-region (point) (re-search-forward "\r?\n\r?\n"))
300       (setq ret (decode-coding-string (buffer-string) mixi-coding-system))
301       (mixi-retrieve-1 ret url post-data))))
302
303 (defconst mixi-my-id-regexp
304   "<a href=\"add_diary\\.pl\\?id=\\([0-9]+\\)")
305
306 (defun mixi-login (&optional email password)
307   "Login to mixi."
308   (when (and (eq mixi-retrieve-function 'mixi-w3m-retrieve)
309              (not w3m-use-cookies))
310     (error
311      (mixi-message
312       "Require to accept cookies.  Please set `w3m-use-cookies' to t.")))
313   (let ((email (or email mixi-default-email
314                    (read-from-minibuffer (mixi-message "Login Email: "))))
315         (password (or password mixi-default-password
316                       (read-passwd (mixi-message "Login Password: ")))))
317     (let ((buffer (funcall mixi-retrieve-function "/login.pl"
318                            (concat "email=" email
319                                    "&password=" password
320                                    "&next_url=/home.pl"
321                                    "&sticky=on"))))
322       (unless (string-match "url=/check\\.pl\\?n=" buffer)
323         (error (mixi-message "Cannot login")))
324       (setq buffer (funcall mixi-retrieve-function "/check.pl?n=home.pl"))
325       (if (string-match mixi-my-id-regexp buffer)
326           (setq mixi-me (mixi-make-friend (match-string 1 buffer)))
327         (error (mixi-message "Cannot login"))))))
328
329 (defun mixi-logout ()
330   (funcall mixi-retrieve-function "/logout.pl"))
331
332 (defmacro with-mixi-retrieve (url &rest body)
333   `(let (buffer)
334      (when ,url
335        (setq buffer (funcall mixi-retrieve-function ,url))
336        (when (string-match "login.pl" buffer)
337          (mixi-login)
338          (setq buffer (funcall mixi-retrieve-function ,url))))
339      ,@body))
340 (put 'with-mixi-retrieve 'lisp-indent-function 'defun)
341 (put 'with-mixi-retrieve 'edebug-form-spec '(form body))
342
343 (defun mixi-get-matched-items (url max-numbers regexp)
344   "Get matched items to REGEXP in URL."
345   (let ((page 1)
346         ids)
347     (catch 'end
348       (while (or (null max-numbers) (< (length ids) max-numbers))
349         (with-mixi-retrieve (format url page)
350           (let ((pos 0))
351             (while (and (string-match regexp buffer pos)
352                         (or (null max-numbers) (< (length ids) max-numbers)))
353               (let ((num 1)
354                     list)
355                 (while (match-string num buffer)
356                   (setq list (cons (match-string num buffer) list))
357                   (incf num))
358                 (when (member (reverse list) ids)
359                   (throw 'end ids))
360                 (setq ids (cons (reverse list) ids))
361                 (setq pos (match-end (1- num)))))
362             (when (eq pos 0)
363               (throw 'end ids))))
364         (incf page)))
365     ;; FIXME: Sort? Now order by newest.
366     (reverse ids)))
367
368 ;; stolen (and modified) from shimbun.el
369 (defun mixi-remove-markup (string)
370   "Remove markups from STRING."
371   (with-temp-buffer
372     (insert (or string ""))
373     (save-excursion
374       (goto-char (point-min))
375       (while (search-forward "<!--" nil t)
376         (delete-region (match-beginning 0)
377                        (or (search-forward "-->" nil t)
378                            (point-max))))
379       (goto-char (point-min))
380       (while (re-search-forward "<[^>]+>" nil t)
381         (replace-match "" t t))
382       (goto-char (point-min))
383       (while (re-search-forward "\r" nil t)
384         (replace-match "\n" t t)))
385     (buffer-string)))
386
387 ;; Cache.
388 ;; stolen from time-date.el
389 (defun mixi-time-less-p (t1 t2)
390   "Say whether time value T1 is less than time value T2."
391   (unless (numberp (cdr t1))
392     (setq t1 (cons (car t1) (car (cdr t1)))))
393   (unless (numberp (cdr t2))
394     (setq t2 (cons (car t2) (car (cdr t2)))))
395   (or (< (car t1) (car t2))
396       (and (= (car t1) (car t2))
397            (< (cdr t1) (cdr t2)))))
398
399 (defun mixi-time-add (t1 t2)
400   "Add two time values.  One should represent a time difference."
401   (unless (numberp (cdr t1))
402     (setq t1 (cons (car t1) (car (cdr t1)))))
403   (unless (numberp (cdr t2))
404     (setq t2 (cons (car t2) (car (cdr t2)))))
405   (let ((low (+ (cdr t1) (cdr t2))))
406     (cons (+ (car t1) (car t2) (lsh low -16)) low)))
407
408 ;; stolen from time-date.el
409 (defun mixi-seconds-to-time (seconds)
410   "Convert SECONDS (a floating point number) to a time value."
411   (cons (floor seconds 65536)
412         (floor (mod seconds 65536))))
413
414 (defun mixi-cache-expired-p (object)
415   "Whether a cache of OBJECT is expired."
416   (let ((timestamp (mixi-object-timestamp object)))
417     (unless (or (null mixi-cache-expires)
418                  (null timestamp))
419       (if (numberp mixi-cache-expires)
420           (mixi-time-less-p
421            (mixi-time-add timestamp (mixi-seconds-to-time mixi-cache-expires))
422            (current-time))
423         t))))
424
425 (defun mixi-make-cache (key value table)
426   "Make a cache object and return it."
427   (let ((cache (gethash key table)))
428     (if (and cache (not (mixi-cache-expired-p cache)))
429         cache
430       (puthash key value table))))
431
432 ;; Object.
433 (defconst mixi-object-prefix "mixi-")
434
435 (defmacro mixi-object-class (object)
436   `(car-safe ,object))
437
438 (defmacro mixi-object-p (object)
439   `(eq (string-match (concat "^" mixi-object-prefix)
440                      (symbol-name (mixi-object-class ,object))) 0))
441
442 (defun mixi-object-name (object)
443   "Return the name of OBJECT."
444   (unless (mixi-object-p object)
445     (signal 'wrong-type-argument (list 'mixi-object-p object)))
446   (let ((class (mixi-object-class object)))
447     (substring (symbol-name class) (length mixi-object-prefix))))
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-id (object)
457   "Return the id 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) "-id"))))
462     (funcall func object)))
463
464 (defun mixi-object-set-timestamp (object timestamp)
465   "Set the timestamp of OBJECT."
466   (unless (mixi-object-p object)
467     (signal 'wrong-type-argument (list 'mixi-object-p object)))
468   (aset (cdr object) 0 timestamp))
469
470 (defmacro mixi-object-touch (object)
471   `(mixi-object-set-timestamp ,object (current-time)))
472
473 ;; Friend object.
474 (defvar mixi-friend-cache (make-hash-table :test 'equal))
475 (defun mixi-make-friend (id &optional nick)
476   "Return a friend object."
477   (mixi-make-cache id (cons 'mixi-friend (vector nil id nick nil nil nil nil
478                                                  nil nil nil nil nil nil nil))
479                    mixi-friend-cache))
480
481 (defun mixi-make-me ()
482   (unless mixi-me
483     (with-mixi-retrieve "/home.pl"
484       (if (string-match mixi-my-id-regexp buffer)
485           (setq mixi-me (mixi-make-friend (match-string 1 buffer)))
486         (signal 'error (list 'who-am-i)))))
487   mixi-me)
488
489 (defmacro mixi-friend-p (friend)
490   `(eq (mixi-object-class ,friend) 'mixi-friend))
491
492 (defmacro mixi-friend-page (friend)
493   `(concat "/show_friend.pl?id=" (mixi-friend-id ,friend)))
494
495 (defconst mixi-friend-nick-regexp
496   "<img alt=\"\\*\" src=\"http://img\\.mixi\\.jp/img/dot0\\.gif\" width=\"1\" height=\"5\"><br>\n\\(.*\\)¤µ¤ó([0-9]+)")
497 (defconst mixi-friend-name-sex-regexp
498   "<td BGCOLOR=#F2DDB7 WIDTH=80 NOWRAP><font COLOR=#996600>̾\\(&nbsp;\\| \\)Á°</font></td>\n+<td WIDTH=345>\\([^<]+\\) (\\([Ã˽÷]\\)À­)</td>")
499 (defconst mixi-friend-address-regexp
500   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>¸½½»½ê</font></td>\n<td>\\(.+\\)\\(\n.+\n\\)?</td></tr>")
501 (defconst mixi-friend-age-regexp
502   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>ǯ\\(&nbsp;\\| \\)Îð</font></td>\n<td>\\([0-9]+\\)ºÐ\\(\n.+\n\\)?</td></tr>")
503 (defconst mixi-friend-birthday-regexp
504   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>ÃÂÀ¸Æü</font></td>\n<td>\\([0-9]+\\)·î\\([0-9]+\\)Æü\\(\n.+\n\\)?</td></tr>")
505 (defconst mixi-friend-blood-type-regexp
506   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>·ì±Õ·¿</font></td>\n<td>\\([ABO]B?\\)·¿\\(\n\n\\)?</td></tr>")
507 (defconst mixi-friend-birthplace-regexp
508   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>½Ð¿ÈÃÏ</font>\n?</td>\n<td>\\(.+\\)\\(\n.+\n\\)?</td></tr>")
509 (defconst mixi-friend-hobby-regexp
510   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>¼ñ\\(&nbsp;\\| \\)Ì£</font></td>\n<td>\\(.+\\)</td></tr>")
511 (defconst mixi-friend-job-regexp
512   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>¿¦\\(&nbsp;\\| \\)¶È</font></td>\n<td>\\(.+\\)\\(\n.+\n\\)?</td></tr>")
513 (defconst mixi-friend-organization-regexp
514   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>½ê\\(&nbsp;\\| \\)°</font></td>\n<td[^>]*>\\(.+\\)\\(\n.+\n\\)?</td></tr>")
515 (defconst mixi-friend-profile-regexp
516   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>¼«¸Ê¾Ò²ð</font></td>\n<td CLASS=h120>\\(.+\\)</td></tr>")
517
518 (defun mixi-friend-realize (friend)
519   "Realize a FRIEND."
520   ;; FIXME: Check a expiration of cache?
521   (unless (mixi-object-realize-p friend)
522     (let (buf)
523       (with-mixi-retrieve (mixi-friend-page friend)
524         (setq buf buffer))
525       (if (string-match mixi-friend-nick-regexp buf)
526           (mixi-friend-set-nick friend (match-string 1 buf))
527         (signal 'error (list 'cannot-find-nick friend)))
528       ;; For getting my profile.
529       (unless (string-match mixi-friend-name-sex-regexp buf)
530         (with-mixi-retrieve "/show_profile.pl"
531           (setq buf buffer)))
532       (if (string-match mixi-friend-name-sex-regexp buf)
533           (progn
534             (mixi-friend-set-name friend (match-string 2 buf))
535             (mixi-friend-set-sex friend
536                                  (if (string= (match-string 3 buf) "ÃË")
537                                      'male 'female)))
538         (signal 'error (list 'cannot-find-name-or-sex friend)))
539       (when (string-match mixi-friend-address-regexp buf)
540         (mixi-friend-set-address friend (match-string 1 buf)))
541       (when (string-match mixi-friend-age-regexp buf)
542         (mixi-friend-set-age
543          friend (string-to-number (match-string 2 buf))))
544       (when (string-match mixi-friend-birthday-regexp buf)
545         (mixi-friend-set-birthday
546          friend (list (string-to-number (match-string 1 buf))
547                       (string-to-number (match-string 2 buf)))))
548       (when (string-match mixi-friend-blood-type-regexp buf)
549         (mixi-friend-set-blood-type friend (intern (match-string 1 buf))))
550       (when (string-match mixi-friend-birthplace-regexp buf)
551         (mixi-friend-set-birthplace friend (match-string 1 buf)))
552       (when (string-match mixi-friend-hobby-regexp buf)
553         (mixi-friend-set-hobby
554          friend (split-string (match-string 2 buf) ", ")))
555       (when (string-match mixi-friend-job-regexp buf)
556         (mixi-friend-set-job friend (match-string 2 buf)))
557       (when (string-match mixi-friend-organization-regexp buf)
558         (mixi-friend-set-organization friend (match-string 2 buf)))
559       (when (string-match mixi-friend-profile-regexp buf)
560         (mixi-friend-set-profile
561          friend (mixi-remove-markup (match-string 1 buf)))))
562     (mixi-object-touch friend)))
563
564 (defun mixi-friend-id (friend)
565   "Return the id of FRIEND."
566   (unless (mixi-friend-p friend)
567     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
568   (aref (cdr friend) 1))
569
570 (defun mixi-friend-nick (friend)
571   "Return the nick of FRIEND."
572   (unless (mixi-friend-p friend)
573     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
574   (unless (aref (cdr friend) 2)
575     (mixi-friend-realize friend))
576   (aref (cdr friend) 2))
577
578 (defun mixi-friend-name (friend)
579   "Return the name of FRIEND."
580   (unless (mixi-friend-p friend)
581     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
582   (mixi-friend-realize friend)
583   (aref (cdr friend) 3))
584
585 (defun mixi-friend-sex (friend)
586   "Return the sex of FRIEND."
587   (unless (mixi-friend-p friend)
588     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
589   (mixi-friend-realize friend)
590   (aref (cdr friend) 4))
591
592 (defun mixi-friend-address (friend)
593   "Return the address of FRIEND."
594   (unless (mixi-friend-p friend)
595     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
596   (mixi-friend-realize friend)
597   (aref (cdr friend) 5))
598
599 (defun mixi-friend-age (friend)
600   "Return the age of FRIEND."
601   (unless (mixi-friend-p friend)
602     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
603   (mixi-friend-realize friend)
604   (aref (cdr friend) 6))
605
606 (defun mixi-friend-birthday (friend)
607   "Return the birthday of FRIEND."
608   (unless (mixi-friend-p friend)
609     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
610   (mixi-friend-realize friend)
611   (aref (cdr friend) 7))
612
613 (defun mixi-friend-blood-type (friend)
614   "Return the blood type of FRIEND."
615   (unless (mixi-friend-p friend)
616     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
617   (mixi-friend-realize friend)
618   (aref (cdr friend) 8))
619
620 (defun mixi-friend-birthplace (friend)
621   "Return the birthplace of FRIEND."
622   (unless (mixi-friend-p friend)
623     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
624   (mixi-friend-realize friend)
625   (aref (cdr friend) 9))
626
627 (defun mixi-friend-hobby (friend)
628   "Return the hobby of FRIEND."
629   (unless (mixi-friend-p friend)
630     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
631   (mixi-friend-realize friend)
632   (aref (cdr friend) 10))
633
634 (defun mixi-friend-job (friend)
635   "Return the job of FRIEND."
636   (unless (mixi-friend-p friend)
637     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
638   (mixi-friend-realize friend)
639   (aref (cdr friend) 11))
640
641 (defun mixi-friend-organization (friend)
642   "Return the organization of FRIEND."
643   (unless (mixi-friend-p friend)
644     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
645   (mixi-friend-realize friend)
646   (aref (cdr friend) 12))
647
648 (defun mixi-friend-profile (friend)
649   "Return the pforile of FRIEND."
650   (unless (mixi-friend-p friend)
651     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
652   (mixi-friend-realize friend)
653   (aref (cdr friend) 13))
654
655 (defun mixi-friend-set-nick (friend nick)
656   "Set the nick of FRIEND."
657   (unless (mixi-friend-p friend)
658     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
659   (aset (cdr friend) 2 nick))
660
661 (defun mixi-friend-set-name (friend name)
662   "Set the name of FRIEND."
663   (unless (mixi-friend-p friend)
664     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
665   (aset (cdr friend) 3 name))
666
667 (defun mixi-friend-set-sex (friend sex)
668   "Set the sex of FRIEND."
669   (unless (mixi-friend-p friend)
670     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
671   (aset (cdr friend) 4 sex))
672
673 (defun mixi-friend-set-address (friend address)
674   "Set the address of FRIEND."
675   (unless (mixi-friend-p friend)
676     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
677   (aset (cdr friend) 5 address))
678
679 (defun mixi-friend-set-age (friend age)
680   "Set the age of FRIEND."
681   (unless (mixi-friend-p friend)
682     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
683   (aset (cdr friend) 6 age))
684
685 (defun mixi-friend-set-birthday (friend birthday)
686   "Set the birthday of FRIEND."
687   (unless (mixi-friend-p friend)
688     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
689   (aset (cdr friend) 7 birthday))
690
691 (defun mixi-friend-set-blood-type (friend blood-type)
692   "Set the blood type of FRIEND."
693   (unless (mixi-friend-p friend)
694     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
695   (aset (cdr friend) 8 blood-type))
696
697 (defun mixi-friend-set-birthplace (friend birthplace)
698   "Set the birthplace of FRIEND."
699   (unless (mixi-friend-p friend)
700     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
701   (aset (cdr friend) 9 birthplace))
702
703 (defun mixi-friend-set-hobby (friend hobby)
704   "Set the hobby of FRIEND."
705   (unless (mixi-friend-p friend)
706     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
707   (aset (cdr friend) 10 hobby))
708
709 (defun mixi-friend-set-job (friend job)
710   "Set the job of FRIEND."
711   (unless (mixi-friend-p friend)
712     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
713   (aset (cdr friend) 11 job))
714
715 (defun mixi-friend-set-organization (friend organization)
716   "Set the organization of FRIEND."
717   (unless (mixi-friend-p friend)
718     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
719   (aset (cdr friend) 12 organization))
720
721 (defun mixi-friend-set-profile (friend profile)
722   "Set the profile of FRIEND."
723   (unless (mixi-friend-p friend)
724     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
725   (aset (cdr friend) 13 profile))
726
727 (defmacro mixi-friend-list-page (&optional friend)
728   `(concat "/list_friend.pl?page=%d"
729            (when ,friend (concat "&id=" (mixi-friend-id ,friend)))))
730
731 (defconst mixi-friend-list-id-regexp
732   "<a href=show_friend\\.pl\\?id=\\([0-9]+\\)")
733 (defconst mixi-friend-list-nick-regexp
734   "<td valign=middle>\\(.+\\)¤µ¤ó([0-9]+)<br />")
735
736 (defun mixi-get-friends (&rest args)
737   "Get friends of FRIEND."
738   (when (> (length args) 2)
739     (signal 'wrong-number-of-arguments (list 'mixi-get-friends (length args))))
740   (let ((friend (nth 0 args))
741         (max-numbers (nth 1 args)))
742     (when (or (not (mixi-friend-p friend)) (mixi-friend-p max-numbers))
743       (setq friend (nth 1 args))
744       (setq max-numbers (nth 0 args)))
745     (unless (or (null friend) (mixi-friend-p friend))
746       (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
747     (let ((ids (mixi-get-matched-items (mixi-friend-list-page friend)
748                                        max-numbers
749                                        mixi-friend-list-id-regexp))
750           (nicks (mixi-get-matched-items (mixi-friend-list-page friend)
751                                          max-numbers
752                                          mixi-friend-list-nick-regexp)))
753       (let ((index 0)
754             ret)
755         (while (< index (length ids))
756           (setq ret (cons (mixi-make-friend (nth 0 (nth index ids))
757                                             (nth 0 (nth index nicks))) ret))
758           (incf index))
759         (reverse ret)))))
760
761 ;; Favorite.
762 (defmacro mixi-favorite-list-page ()
763   `(concat "/list_bookmark.pl?page=%d"))
764
765 (defconst mixi-favorite-list-id-regexp
766   "<td ALIGN=center BGCOLOR=#FDF9F2 width=330><a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">")
767 (defconst mixi-favorite-list-nick-regexp
768   "<td BGCOLOR=#FDF9F2><font COLOR=#996600>̾&nbsp;&nbsp;Á°</font></td>
769 <td COLSPAN=2 BGCOLOR=#FFFFFF>\\(.+\\)</td></tr>")
770
771 (defun mixi-get-favorites (&optional max-numbers)
772   "Get favorites."
773   (let ((ids (mixi-get-matched-items (mixi-favorite-list-page)
774                                      max-numbers
775                                      mixi-favorite-list-id-regexp))
776         (nicks (mixi-get-matched-items (mixi-favorite-list-page)
777                                        max-numbers
778                                        mixi-favorite-list-nick-regexp)))
779     (let ((index 0)
780           ret)
781       (while (< index (length ids))
782         (setq ret (cons (mixi-make-friend (nth 0 (nth index ids))
783                                           (nth 0 (nth index nicks))) ret))
784         (incf index))
785       (reverse ret))))
786
787 ;; Log object.
788 (defun mixi-make-log (friend time)
789   "Return a log object."
790   (cons 'mixi-log (vector friend time)))
791
792 (defmacro mixi-log-p (log)
793   `(eq (mixi-object-class ,log) 'mixi-log))
794
795 (defun mixi-log-friend (log)
796   "Return the friend of LOG."
797   (unless (mixi-log-p log)
798     (signal 'wrong-type-argument (list 'mixi-log-p log)))
799   (aref (cdr log) 0))
800
801 (defun mixi-log-time (log)
802   "Return the time of LOG."
803   (unless (mixi-log-p log)
804     (signal 'wrong-type-argument (list 'mixi-log-p log)))
805   (aref (cdr log) 1))
806
807 (defmacro mixi-log-list-page ()
808   `(concat "/show_log.pl"))
809
810 (defconst mixi-log-list-regexp
811   "\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü \\([0-9]+\\):\\([0-9]+\\) <a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.+\\)</a><br>")
812
813 (defun mixi-get-logs (&optional max-numbers)
814   "Get logs."
815   (let ((items (mixi-get-matched-items (mixi-log-list-page)
816                                        max-numbers
817                                        mixi-log-list-regexp)))
818     (mapcar (lambda (item)
819               (mixi-make-log (mixi-make-friend (nth 5 item) (nth 6 item))
820                              (encode-time 0
821                                           (string-to-number (nth 4 item))
822                                           (string-to-number (nth 3 item))
823                                           (string-to-number (nth 2 item))
824                                           (string-to-number (nth 1 item))
825                                           (string-to-number (nth 0 item)))))
826             items)))
827
828 ;; Diary object.
829 (defvar mixi-diary-cache (make-hash-table :test 'equal))
830 (defun mixi-make-diary (owner id)
831   "Return a diary object."
832   (let ((owner (or owner (mixi-make-me))))
833     (mixi-make-cache (list (mixi-friend-id owner) id)
834                      (cons 'mixi-diary (vector nil owner id nil nil nil))
835                      mixi-diary-cache)))
836
837 (defmacro mixi-diary-p (diary)
838   `(eq (mixi-object-class ,diary) 'mixi-diary))
839
840 (defmacro mixi-diary-page (diary)
841   `(concat "/view_diary.pl?id=" (mixi-diary-id ,diary)
842            "&owner_id=" (mixi-friend-id (mixi-diary-owner ,diary))))
843
844 ;; FIXME: Remove `¤µ¤ó'.
845 (defconst mixi-diary-owner-nick-regexp
846   "<td WIDTH=490 background=http://img\\.mixi\\.jp/img/bg_w\\.gif><b><font COLOR=#605048>\\(.+\\)\\(¤µ¤ó\\)?¤ÎÆüµ­</font></b></td>")
847 (defconst mixi-diary-time-regexp
848   "<td ALIGN=center ROWSPAN=2 NOWRAP WIDTH=95 bgcolor=#FFD8B0>\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü<br>\\([0-9]+\\):\\([0-9]+\\)</td>")
849 (defconst mixi-diary-title-regexp
850   "<td BGCOLOR=#FFF4E0 WIDTH=430>&nbsp;\\([^<]+\\)</td></tr>")
851 (defconst mixi-diary-content-regexp
852   "<td CLASS=h12>\\(.+\\)</td></tr>")
853
854 (defun mixi-diary-realize (diary)
855   "Realize a DIARY."
856   ;; FIXME: Check a expiration of cache?
857   (unless (mixi-object-realize-p diary)
858     (with-mixi-retrieve (mixi-diary-page diary)
859       (if (string-match mixi-diary-owner-nick-regexp buffer)
860           (mixi-friend-set-nick (mixi-diary-owner diary)
861                                 (match-string 1 buffer))
862         (signal 'error (list 'cannot-find-owner-nick diary)))
863       (if (string-match mixi-diary-time-regexp buffer)
864           (mixi-diary-set-time
865            diary (encode-time 0 (string-to-number (match-string 5 buffer))
866                               (string-to-number (match-string 4 buffer))
867                               (string-to-number (match-string 3 buffer))
868                               (string-to-number (match-string 2 buffer))
869                               (string-to-number (match-string 1 buffer))))
870         (signal 'error (list 'cannot-find-time diary)))
871       (if (string-match mixi-diary-title-regexp buffer)
872           (mixi-diary-set-title diary (match-string 1 buffer))
873         (signal 'error (list 'cannot-find-title diary)))
874       (if (string-match mixi-diary-content-regexp buffer)
875           (mixi-diary-set-content diary (mixi-remove-markup
876                                          (match-string 1 buffer)))
877         (signal 'error (list 'cannot-find-content diary))))
878     (mixi-object-touch diary)))
879
880 (defun mixi-diary-owner (diary)
881   "Return the owner of DIARY."
882   (unless (mixi-diary-p diary)
883     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
884   (aref (cdr diary) 1))
885
886 (defun mixi-diary-id (diary)
887   "Return the id of DIARY."
888   (unless (mixi-diary-p diary)
889     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
890   (aref (cdr diary) 2))
891
892 (defun mixi-diary-time (diary)
893   "Return the time of DIARY."
894   (unless (mixi-diary-p diary)
895     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
896   (mixi-diary-realize diary)
897   (aref (cdr diary) 3))
898
899 (defun mixi-diary-title (diary)
900   "Return the title of DIARY."
901   (unless (mixi-diary-p diary)
902     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
903   (mixi-diary-realize diary)
904   (aref (cdr diary) 4))
905
906 (defun mixi-diary-content (diary)
907   "Return the content of DIARY."
908   (unless (mixi-diary-p diary)
909     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
910   (mixi-diary-realize diary)
911   (aref (cdr diary) 5))
912
913 (defun mixi-diary-set-time (diary time)
914   "Set the time of DIARY."
915   (unless (mixi-diary-p diary)
916     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
917   (aset (cdr diary) 3 time))
918
919 (defun mixi-diary-set-title (diary title)
920   "Set the title of DIARY."
921   (unless (mixi-diary-p diary)
922     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
923   (aset (cdr diary) 4 title))
924
925 (defun mixi-diary-set-content (diary content)
926   "Set the content of DIARY."
927   (unless (mixi-diary-p diary)
928     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
929   (aset (cdr diary) 5 content))
930
931 (defmacro mixi-diary-list-page (&optional friend)
932   `(concat "/list_diary.pl?page=%d"
933            (when ,friend (concat "&id=" (mixi-friend-id ,friend)))))
934
935 (defconst mixi-diary-list-regexp
936   "<a href=\"view_diary\\.pl\\?id=\\([0-9]+\\)&owner_id=[0-9]+\">")
937
938 (defun mixi-get-diaries (&rest args)
939   "Get diaries of FRIEND."
940   (when (> (length args) 2)
941     (signal 'wrong-number-of-arguments
942             (list 'mixi-get-diaries (length args))))
943   (let ((friend (nth 0 args))
944         (max-numbers (nth 1 args)))
945     (when (or (not (mixi-friend-p friend)) (mixi-friend-p max-numbers))
946       (setq friend (nth 1 args))
947       (setq max-numbers (nth 0 args)))
948     (unless (or (null friend) (mixi-friend-p friend))
949       (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
950     (let ((items (mixi-get-matched-items (mixi-diary-list-page friend)
951                                          max-numbers
952                                          mixi-diary-list-regexp)))
953       (mapcar (lambda (item)
954                 (mixi-make-diary friend (nth 0 item)))
955               items))))
956
957 (defmacro mixi-new-diary-list-page ()
958   `(concat "/new_friend_diary.pl?page=%d"))
959
960 (defconst mixi-new-diary-list-regexp
961   "<a class=\"new_link\" href=view_diary\\.pl\\?id=\\([0-9]+\\)&owner_id=\\([0-9]+\\)>")
962
963 (defun mixi-get-new-diaries (&optional max-numbers)
964   "Get new diaries."
965   (let ((items (mixi-get-matched-items (mixi-new-diary-list-page)
966                                        max-numbers
967                                        mixi-new-diary-list-regexp)))
968     (mapcar (lambda (item)
969               (mixi-make-diary (mixi-make-friend (nth 1 item)) (nth 0 item)))
970             items)))
971
972 ;; Community object.
973 (defvar mixi-community-cache (make-hash-table :test 'equal))
974 (defun mixi-make-community (id &optional name)
975   "Return a community object."
976   (mixi-make-cache id (cons 'mixi-community (vector nil id name nil nil nil
977                                                     nil nil nil nil))
978                    mixi-community-cache))
979
980 (defmacro mixi-community-p (community)
981   `(eq (mixi-object-class ,community) 'mixi-community))
982
983 (defmacro mixi-community-page (community)
984   `(concat "/view_community.pl?id=" (mixi-community-id ,community)))
985
986 (defconst mixi-community-nodata-regexp
987   "^¥Ç¡¼¥¿¤¬¤¢¤ê¤Þ¤»¤ó")
988 (defconst mixi-community-name-regexp
989   "<td WIDTH=345>\\(.*\\)</td></tr>")
990 (defconst mixi-community-birthday-regexp
991   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>³«ÀßÆü</font></td>\n<td>\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü</td>")
992 ;; FIXME: Care when the owner has seceded.
993 (defconst mixi-community-owner-regexp
994   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>´ÉÍý¿Í</font></td>\n<td>\n\n<a href=\"\\(home\\.pl\\|show_friend\\.pl\\?id=\\([0-9]+\\)\\)\">\n\\(.+\\)</a>")
995 (defconst mixi-community-category-regexp
996   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>¥«¥Æ¥´¥ê</font></td>\n<td>\\([^<]+\\)</td>")
997 (defconst mixi-community-members-regexp
998   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>¥á¥ó¥Ð¡¼¿ô</font></td>\n<td>\\([0-9]+\\)¿Í</td></tr>")
999 (defconst mixi-community-open-level-regexp
1000   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>»²²Ã¾ò·ï¤È<br>¸ø³«¥ì¥Ù¥ë</font></td>
1001 <td>\\(.+\\)</td></tr>")
1002 (defconst mixi-community-authority-regexp
1003   "<td BGCOLOR=#F2DDB7><font COLOR=#996600>¥È¥Ô¥Ã¥¯ºîÀ®¤Î¸¢¸Â</font></td>\n<td>\\(.+\\)</td></tr>")
1004 (defconst mixi-community-description-regexp
1005   "<td CLASS=h120>\\(.+\\)</td>")
1006
1007 (defun mixi-community-realize (community)
1008   "Realize a COMMUNITY."
1009   ;; FIXME: Check a expiration of cache?
1010   (unless (mixi-object-realize-p community)
1011     (with-mixi-retrieve (mixi-community-page community)
1012       (if (string-match mixi-community-nodata-regexp buffer)
1013           ;; FIXME: Set all members?
1014           (mixi-community-set-name community "¥Ç¡¼¥¿¤¬¤¢¤ê¤Þ¤»¤ó")
1015         (if (string-match mixi-community-name-regexp buffer)
1016             (mixi-community-set-name community (match-string 1 buffer))
1017           (signal 'error (list 'cannot-find-name community)))
1018         (if (string-match mixi-community-birthday-regexp buffer)
1019             (mixi-community-set-birthday
1020              community
1021              (encode-time 0 0 0 (string-to-number (match-string 3 buffer))
1022                           (string-to-number (match-string 2 buffer))
1023                           (string-to-number (match-string 1 buffer))))
1024           (signal 'error (list 'cannot-find-birthday community)))
1025         (if (string-match mixi-community-owner-regexp buffer)
1026             (if (string= (match-string 1 buffer) "home.pl")
1027                 (mixi-community-set-owner community (mixi-make-me))
1028               (mixi-community-set-owner
1029                community (mixi-make-friend (match-string 2 buffer)
1030                                            (match-string 3 buffer))))
1031           (signal 'error (list 'cannot-find-owner community)))
1032         (if (string-match mixi-community-category-regexp buffer)
1033             (mixi-community-set-category community (match-string 1 buffer))
1034           (signal 'error (list 'cannot-find-category community)))
1035         (if (string-match mixi-community-members-regexp buffer)
1036             (mixi-community-set-members
1037              community (string-to-number (match-string 1 buffer)))
1038           (signal 'error (list 'cannot-find-members community)))
1039         (if (string-match mixi-community-open-level-regexp buffer)
1040             (mixi-community-set-open-level community (match-string 1 buffer))
1041           (signal 'error (list 'cannot-find-open-level community)))
1042         (if (string-match mixi-community-authority-regexp buffer)
1043             (mixi-community-set-authority community (match-string 1 buffer))
1044           (signal 'error (list 'cannot-find-authority community)))
1045         (if (string-match mixi-community-description-regexp buffer)
1046             (mixi-community-set-description
1047              community (mixi-remove-markup (match-string 1 buffer)))
1048           (signal 'error (list 'cannot-find-description community)))))
1049     (mixi-object-touch community)))
1050
1051 (defun mixi-community-id (community)
1052   "Return the id of COMMUNITY."
1053   (unless (mixi-community-p community)
1054     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1055   (aref (cdr community) 1))
1056
1057 (defun mixi-community-name (community)
1058   "Return the name of COMMUNITY."
1059   (unless (mixi-community-p community)
1060     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1061   (unless (aref (cdr community) 2)
1062     (mixi-community-realize community))
1063   (aref (cdr community) 2))
1064
1065 (defun mixi-community-birthday (community)
1066   "Return the birthday of COMMUNITY."
1067   (unless (mixi-community-p community)
1068     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1069   (mixi-community-realize community)
1070   (aref (cdr community) 3))
1071
1072 (defun mixi-community-owner (community)
1073   "Return the owner of COMMUNITY."
1074   (unless (mixi-community-p community)
1075     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1076   (mixi-community-realize community)
1077   (aref (cdr community) 4))
1078
1079 (defun mixi-community-category (community)
1080   "Return the category of COMMUNITY."
1081   (unless (mixi-community-p community)
1082     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1083   (mixi-community-realize community)
1084   (aref (cdr community) 5))
1085
1086 (defun mixi-community-members (community)
1087   "Return the members of COMMUNITY."
1088   (unless (mixi-community-p community)
1089     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1090   (mixi-community-realize community)
1091   (aref (cdr community) 6))
1092
1093 (defun mixi-community-open-level (community)
1094   "Return the open-level of COMMUNITY."
1095   (unless (mixi-community-p community)
1096     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1097   (mixi-community-realize community)
1098   (aref (cdr community) 7))
1099
1100 (defun mixi-community-authority (community)
1101   "Return the authority of COMMUNITY."
1102   (unless (mixi-community-p community)
1103     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1104   (mixi-community-realize community)
1105   (aref (cdr community) 8))
1106
1107 (defun mixi-community-description (community)
1108   "Return the description of COMMUNITY."
1109   (unless (mixi-community-p community)
1110     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1111   (mixi-community-realize community)
1112   (aref (cdr community) 9))
1113
1114 (defun mixi-community-set-name (community name)
1115   "Set the name of COMMUNITY."
1116   (unless (mixi-community-p community)
1117     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1118   (aset (cdr community) 2 name))
1119
1120 (defun mixi-community-set-birthday (community birthday)
1121   "Set the birthday of COMMUNITY."
1122   (unless (mixi-community-p community)
1123     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1124   (aset (cdr community) 3 birthday))
1125
1126 (defun mixi-community-set-owner (community owner)
1127   "Set the owner of COMMUNITY."
1128   (unless (mixi-community-p community)
1129     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1130   (unless (mixi-friend-p owner)
1131     (signal 'wrong-type-argument (list 'mixi-friend-p owner)))
1132   (aset (cdr community) 4 owner))
1133
1134 (defun mixi-community-set-category (community category)
1135   "Set the category of COMMUNITY."
1136   (unless (mixi-community-p community)
1137     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1138   (aset (cdr community) 5 category))
1139
1140 (defun mixi-community-set-members (community members)
1141   "Set the name of COMMUNITY."
1142   (unless (mixi-community-p community)
1143     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1144   (aset (cdr community) 6 members))
1145
1146 (defun mixi-community-set-open-level (community open-level)
1147   "Set the name of COMMUNITY."
1148   (unless (mixi-community-p community)
1149     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1150   (aset (cdr community) 7 open-level))
1151
1152 (defun mixi-community-set-authority (community authority)
1153   "Set the name of COMMUNITY."
1154   (unless (mixi-community-p community)
1155     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1156   (aset (cdr community) 8 authority))
1157
1158 (defun mixi-community-set-description (community description)
1159   "Set the name of COMMUNITY."
1160   (unless (mixi-community-p community)
1161     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1162   (aset (cdr community) 9 description))
1163
1164 (defmacro mixi-community-list-page (&optional friend)
1165   `(concat "/list_community.pl?page=%d"
1166            (when ,friend (concat "&id=" (mixi-friend-id ,friend)))))
1167
1168 (defconst mixi-community-list-id-regexp
1169   "<a href=view_community\\.pl\\?id=\\([0-9]+\\)")
1170 (defconst mixi-community-list-name-regexp
1171   "<td valign=middle>\\(.+\\)([0-9]+)</td>")
1172
1173 (defun mixi-get-communities (&rest args)
1174   "Get communities of FRIEND."
1175   (when (> (length args) 2)
1176     (signal 'wrong-number-of-arguments
1177             (list 'mixi-get-communities (length args))))
1178   (let ((friend (nth 0 args))
1179         (max-numbers (nth 1 args)))
1180     (when (or (not (mixi-friend-p friend)) (mixi-friend-p max-numbers))
1181       (setq friend (nth 1 args))
1182       (setq max-numbers (nth 0 args)))
1183     (unless (or (null friend) (mixi-friend-p friend))
1184       (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
1185     (let ((ids (mixi-get-matched-items (mixi-community-list-page friend)
1186                                        max-numbers
1187                                        mixi-community-list-id-regexp))
1188           (names (mixi-get-matched-items (mixi-community-list-page friend)
1189                                          max-numbers
1190                                          mixi-community-list-name-regexp)))
1191       (let ((index 0)
1192             ret)
1193         (while (< index (length ids))
1194           (setq ret (cons (mixi-make-community (nth 0 (nth index ids))
1195                                                (nth 0 (nth index names))) ret))
1196           (incf index))
1197         (reverse ret)))))
1198
1199 ;; Topic object.
1200 (defvar mixi-topic-cache (make-hash-table :test 'equal))
1201 (defun mixi-make-topic (community id)
1202   "Return a topic object."
1203   (mixi-make-cache (list (mixi-community-id community) id)
1204                    (cons 'mixi-topic (vector nil community id nil nil nil nil))
1205                    mixi-topic-cache))
1206
1207 (defmacro mixi-topic-p (topic)
1208   `(eq (mixi-object-class ,topic) 'mixi-topic))
1209
1210 (defmacro mixi-topic-page (topic)
1211   `(concat "/view_bbs.pl?id=" (mixi-topic-id ,topic)
1212            "&comm_id=" (mixi-community-id (mixi-topic-community ,topic))))
1213
1214 (defconst mixi-topic-time-regexp
1215   "<td rowspan=\"3\" width=\"110\" bgcolor=\"#ffd8b0\" align=\"center\" valign=\"top\" nowrap>\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü<br>\\([0-9]+\\):\\([0-9]+\\)</td>")
1216 (defconst mixi-topic-title-regexp
1217   "<td bgcolor=\"#fff4e0\">&nbsp;\\([^<]+\\)</td>")
1218 ;; FIXME: Remove `¤µ¤ó'.
1219 (defconst mixi-topic-owner-regexp
1220   "<td bgcolor=\"#fdf9f2\">&nbsp;<font color=\"#dfb479\"></font>&nbsp;<a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.+\\)\\(¤µ¤ó\\)?</a>")
1221 (defconst mixi-topic-content-regexp
1222   "<td class=\"h120\"><table><tr>\\(.+\\)?</tr></table>\\(.+\\)</td>")
1223
1224 (defun mixi-topic-realize (topic)
1225   "Realize a TOPIC."
1226   ;; FIXME: Check a expiration of cache?
1227   (unless (mixi-object-realize-p topic)
1228     (with-mixi-retrieve (mixi-topic-page topic)
1229       (if (string-match mixi-topic-time-regexp buffer)
1230           (mixi-topic-set-time
1231            topic (encode-time 0 (string-to-number (match-string 5 buffer))
1232                               (string-to-number (match-string 4 buffer))
1233                               (string-to-number (match-string 3 buffer))
1234                               (string-to-number (match-string 2 buffer))
1235                               (string-to-number (match-string 1 buffer))))
1236         (signal 'error (list 'cannot-find-time topic)))
1237       (if (string-match mixi-topic-title-regexp buffer)
1238           (mixi-topic-set-title topic (match-string 1 buffer))
1239         (signal 'error (list 'cannot-find-title topic)))
1240       (if (string-match mixi-topic-owner-regexp buffer)
1241           (mixi-topic-set-owner topic
1242                                 (mixi-make-friend (match-string 1 buffer)
1243                                                   (match-string 2 buffer)))
1244         (signal 'error (list 'cannot-find-owner topic)))
1245       (if (string-match mixi-topic-content-regexp buffer)
1246           (mixi-topic-set-content topic (mixi-remove-markup
1247                                          (match-string 2 buffer)))
1248         (signal 'error (list 'cannot-find-content topic))))
1249     (mixi-object-touch topic)))
1250
1251 (defun mixi-topic-community (topic)
1252   "Return the community of TOPIC."
1253   (unless (mixi-topic-p topic)
1254     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1255   (aref (cdr topic) 1))
1256
1257 (defun mixi-topic-id (topic)
1258   "Return the id of TOPIC."
1259   (unless (mixi-topic-p topic)
1260     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1261   (aref (cdr topic) 2))
1262
1263 (defun mixi-topic-time (topic)
1264   "Return the time of TOPIC."
1265   (unless (mixi-topic-p topic)
1266     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1267   (mixi-topic-realize topic)
1268   (aref (cdr topic) 3))
1269
1270 (defun mixi-topic-title (topic)
1271   "Return the title of TOPIC."
1272   (unless (mixi-topic-p topic)
1273     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1274   (mixi-topic-realize topic)
1275   (aref (cdr topic) 4))
1276
1277 (defun mixi-topic-owner (topic)
1278   "Return the owner of TOPIC."
1279   (unless (mixi-topic-p topic)
1280     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1281   (mixi-topic-realize topic)
1282   (aref (cdr topic) 5))
1283
1284 (defun mixi-topic-content (topic)
1285   "Return the content of TOPIC."
1286   (unless (mixi-topic-p topic)
1287     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1288   (mixi-topic-realize topic)
1289   (aref (cdr topic) 6))
1290
1291 (defun mixi-topic-set-time (topic time)
1292   "Set the time of TOPIC."
1293   (unless (mixi-topic-p topic)
1294     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1295   (aset (cdr topic) 3 time))
1296
1297 (defun mixi-topic-set-title (topic title)
1298   "Set the title of TOPIC."
1299   (unless (mixi-topic-p topic)
1300     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1301   (aset (cdr topic) 4 title))
1302
1303 (defun mixi-topic-set-owner (topic owner)
1304   "Set the owner of TOPIC."
1305   (unless (mixi-topic-p topic)
1306     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1307   (unless (mixi-friend-p owner)
1308     (signal 'wrong-type-argument (list 'mixi-friend-p owner)))
1309   (aset (cdr topic) 5 owner))
1310
1311 (defun mixi-topic-set-content (topic content)
1312   "Set the content of TOPIC."
1313   (unless (mixi-topic-p topic)
1314     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1315   (aset (cdr topic) 6 content))
1316
1317 (defmacro mixi-topic-list-page (community)
1318   `(concat "/list_bbs.pl?page=%d"
1319            "&id=" (mixi-community-id ,community)))
1320
1321 (defconst mixi-topic-list-regexp
1322   "<a href=view_bbs\\.pl\\?id=\\([0-9]+\\)")
1323
1324 (defun mixi-get-topics (community &optional max-numbers)
1325   "Get topics of COMMUNITY."
1326   (unless (mixi-community-p community)
1327     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1328   (let ((items (mixi-get-matched-items (mixi-topic-list-page community)
1329                                        max-numbers
1330                                        mixi-topic-list-regexp)))
1331     (mapcar (lambda (item)
1332               (mixi-make-topic community (nth 0 item)))
1333             items)))
1334
1335 (defmacro mixi-new-topic-list-page ()
1336   `(concat "/new_bbs.pl?page=%d"))
1337
1338 (defconst mixi-new-topic-list-regexp
1339   "<a href=\"view_bbs\\.pl\\?id=\\([0-9]+\\)&comment_count=[0-9]+&comm_id=\\([0-9]+\\)\" class=\"new_link\">")
1340
1341 (defun mixi-get-new-topics (&optional max-numbers)
1342   "Get new topics."
1343   (let ((items (mixi-get-matched-items (mixi-new-topic-list-page)
1344                                        max-numbers
1345                                        mixi-new-topic-list-regexp)))
1346     (mapcar (lambda (item)
1347               (mixi-make-topic (mixi-make-community (nth 1 item))
1348                                (nth 0 item)))
1349             items)))
1350
1351 ;; Comment object.
1352 (defun mixi-make-comment (parent owner time content)
1353   "Return a comment object."
1354   (cons 'mixi-comment (vector parent owner time content)))
1355
1356 (defmacro mixi-comment-p (comment)
1357   `(eq (mixi-object-class ,comment) 'mixi-comment))
1358
1359 (defun mixi-comment-parent (comment)
1360   "Return the parent of COMMENT."
1361   (unless (mixi-comment-p comment)
1362     (signal 'wrong-type-argument (list 'mixi-comment-p comment)))
1363   (aref (cdr comment) 0))
1364
1365 (defun mixi-comment-owner (comment)
1366   "Return the owner of COMMENT."
1367   (unless (mixi-comment-p comment)
1368     (signal 'wrong-type-argument (list 'mixi-comment-p comment)))
1369   (aref (cdr comment) 1))
1370
1371 (defun mixi-comment-time (comment)
1372   "Return the time of COMMENT."
1373   (unless (mixi-comment-p comment)
1374     (signal 'wrong-type-argument (list 'mixi-comment-p comment)))
1375   (aref (cdr comment) 2))
1376
1377 (defun mixi-comment-content (comment)
1378   "Return the content of COMMENT."
1379   (unless (mixi-comment-p comment)
1380     (signal 'wrong-type-argument (list 'mixi-comment-p comment)))
1381   (aref (cdr comment) 3))
1382
1383 (defun mixi-diary-comment-list-page (diary)
1384   (concat "/view_diary.pl?page=%d"
1385           "&id=" (mixi-diary-id diary)
1386           "&owner_id=" (mixi-friend-id (mixi-diary-owner diary))))
1387
1388 ;; FIXME: Split regexp to time, owner(id and nick) and contents.
1389 (defconst mixi-diary-comment-list-regexp
1390 "<td rowspan=\"2\" align=\"center\" width=\"95\" bgcolor=\"#f2ddb7\" nowrap>
1391 \\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü<br>\\([0-9]+\\):\\([0-9]+\\)\\(<br>
1392 <input type=checkbox name=comment_id value=\".+\">
1393 \\|\\)
1394 </td>
1395 <td ALIGN=center BGCOLOR=#FDF9F2 WIDTH=430>
1396 <table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"410\">
1397 <tr>
1398 <td>
1399 <a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.+\\)</a>
1400
1401 \\(<font color=\"#f2ddb7\">|</font> <a href=[^>]+>ºï½ü</a>
1402
1403 \\|\\)</td>
1404 </tr>
1405 </table>
1406 </td>
1407 </tr>
1408 <!-- ËÜʸ : start -->
1409 <tr>
1410 <td bgcolor=\"#ffffff\">
1411 <table BORDER=0 CELLSPACING=0 CELLPADDING=[35] WIDTH=410>
1412 <tr>
1413 <td CLASS=h12>
1414 \\(.+\\)
1415 </td></tr></table>")
1416
1417 (defun mixi-topic-comment-list-page (topic)
1418   (concat "/view_bbs.pl?page=%d"
1419           "&id=" (mixi-topic-id topic)
1420           "&comm_id=" (mixi-community-id (mixi-topic-community topic))))
1421
1422 ;; FIXME: Split regexp to time, owner(id and nick) and contents.
1423 (defconst mixi-topic-comment-list-regexp
1424   "<tr valign=\"top\">
1425 <td rowspan=\"2\" width=\"110\" bgcolor=\"#f2ddb7\" align=\"center\" nowrap>
1426 \\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü<br>
1427 \\([0-9]+\\):\\([0-9]+\\)<br>
1428 </td>
1429 <td bgcolor=\"#fdf9f2\">&nbsp;<font color=\"#f8a448\">
1430 <b>[^<]+</b>:</font>&nbsp;
1431 \\(
1432 \\|\\)<a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.+\\)</a>
1433
1434 \\(
1435
1436 \\|\\)</td>
1437 </tr>
1438 <tr>
1439 <td bgcolor=\"#ffffff\" align=\"center\">
1440 <table border=\"0\" cellspacing=\"0\" cellpadding=\"5\" width=\"500\">
1441 <tr>
1442 <td class=\"h120\">
1443
1444 \\(.+\\)
1445 </td>
1446 </tr>
1447 </table>
1448 </td>
1449 </tr>")
1450
1451 (defun mixi-get-comments (parent &optional max-numbers)
1452   "Get comments of PARENT."
1453   (unless (mixi-object-p parent)
1454     (signal 'wrong-type-argument (list 'mixi-object-p parent)))
1455   (let* ((name (mixi-object-name parent))
1456          (list-page (intern (concat mixi-object-prefix name
1457                                     "-comment-list-page")))
1458          (regexp (eval (intern (concat mixi-object-prefix name
1459                                        "-comment-list-regexp")))))
1460     (let ((items (mixi-get-matched-items
1461                   (funcall list-page parent) max-numbers regexp)))
1462       (mapcar (lambda (item)
1463                 (mixi-make-comment parent (mixi-make-friend
1464                                            (nth 6 item) (nth 7 item))
1465                                    (encode-time
1466                                     0
1467                                     (string-to-number (nth 4 item))
1468                                     (string-to-number (nth 3 item))
1469                                     (string-to-number (nth 2 item))
1470                                     (string-to-number (nth 1 item))
1471                                     (string-to-number (nth 0 item)))
1472                                    (mixi-remove-markup (nth 9 item))))
1473               items))))
1474
1475 (defmacro mixi-new-comment-list-page ()
1476   `(concat "/new_comment.pl?page=%d"))
1477
1478 (defconst mixi-new-comment-list-regexp
1479   "<a href=\"view_diary\\.pl\\?id=\\([0-9]+\\)&owner_id=\\([0-9]+\\)&comment_count=[0-9]+\" class=\"new_link\">")
1480
1481 (defun mixi-get-new-comments (&optional max-numbers)
1482   "Get new comments."
1483   (let ((items (mixi-get-matched-items (mixi-new-comment-list-page)
1484                                        max-numbers
1485                                        mixi-new-comment-list-regexp)))
1486     (mapcar (lambda (item)
1487               (let ((diary (mixi-make-diary
1488                             (mixi-make-friend (nth 1 item))
1489                             (nth 0 item))))
1490                 (mixi-get-comments diary)))
1491             items)))
1492
1493 ;; Message object.
1494 (defconst mixi-message-box-list '(inbox outbox savebox thrash)) ; thrash?
1495
1496 (defmacro mixi-message-box-p (box)
1497   `(when (memq ,box mixi-message-box-list)
1498      t))
1499
1500 (defun mixi-message-box-name (box)
1501   "Return the name of BOX."
1502   (unless (mixi-message-box-p box)
1503     (signal 'wrong-type-argument (list 'mixi-message-box-p box)))
1504   (symbol-name box))
1505
1506 (defvar mixi-message-cache (make-hash-table :test 'equal))
1507 (defun mixi-make-message (id box)
1508   "Return a message object."
1509   (mixi-make-cache (list id box)
1510                    (cons 'mixi-message (vector nil id box nil nil nil nil))
1511                    mixi-message-cache))
1512
1513 (defmacro mixi-message-p (message)
1514   `(eq (mixi-object-class ,message) 'mixi-message))
1515
1516 (defmacro mixi-message-page (message)
1517   `(concat "/view_message.pl?id=" (mixi-message-id ,message)
1518            "&box=" (mixi-message-box ,message)))
1519
1520 (defconst mixi-message-owner-regexp
1521   "<font COLOR=#996600>º¹½Ð¿Í</font>&nbsp;:&nbsp;<a HREF=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.+\\)</a>")
1522 (defconst mixi-message-title-regexp
1523   "<font COLOR=#996600>·ï¡¡Ì¾</font>&nbsp;:&nbsp;\\(.+\\)
1524 </td>")
1525 (defconst mixi-message-time-regexp
1526   "<font COLOR=#996600>Æü¡¡ÉÕ</font>&nbsp;:&nbsp;\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü \\([0-9]+\\)»þ\\([0-9]+\\)ʬ&nbsp;&nbsp;")
1527 (defconst mixi-message-content-regexp
1528   "<tr><td CLASS=h120>\\(.+\\)</td></tr>")
1529
1530 (defun mixi-message-realize (message)
1531   "Realize a MESSAGE."
1532   (unless (mixi-object-realize-p message)
1533     (with-mixi-retrieve (mixi-message-page message)
1534       (if (string-match mixi-message-owner-regexp buffer)
1535           (mixi-message-set-owner message
1536                                   (mixi-make-friend (match-string 1 buffer)
1537                                                     (match-string 2 buffer)))
1538         (signal 'error (list 'cannot-find-owner message)))
1539       (if (string-match mixi-message-title-regexp buffer)
1540           (mixi-message-set-title message (match-string 1 buffer))
1541         (signal 'error (list 'cannot-find-title message)))
1542       (if (string-match mixi-message-time-regexp buffer)
1543           (mixi-message-set-time
1544            message (encode-time 0 (string-to-number (match-string 5 buffer))
1545                                 (string-to-number (match-string 4 buffer))
1546                                 (string-to-number (match-string 3 buffer))
1547                                 (string-to-number (match-string 2 buffer))
1548                                 (string-to-number (match-string 1 buffer))))
1549         (signal 'error (list 'cannot-find-time message)))
1550       (if (string-match mixi-message-content-regexp buffer)
1551           (mixi-message-set-content message (mixi-remove-markup
1552                                              (match-string 1 buffer)))
1553         (signal 'error (list 'cannot-find-content message))))
1554     (mixi-object-touch message)))
1555
1556 (defun mixi-message-id (message)
1557   "Return the id of MESSAGE."
1558   (unless (mixi-message-p message)
1559     (signal 'wrong-type-argument (list 'mixi-message-p message)))
1560   (aref (cdr message) 1))
1561
1562 (defun mixi-message-box (message)
1563   "Return the box of MESSAGE."
1564   (unless (mixi-message-p message)
1565     (signal 'wrong-type-argument (list 'mixi-message-p message)))
1566   (aref (cdr message) 2))
1567
1568 (defun mixi-message-owner (message)
1569   "Return the owner of MESSAGE."
1570   (unless (mixi-message-p message)
1571     (signal 'wrong-type-argument (list 'mixi-message-p message)))
1572   (mixi-message-realize message)
1573   (aref (cdr message) 3))
1574
1575 (defun mixi-message-title (message)
1576   "Return the title of MESSAGE."
1577   (unless (mixi-message-p message)
1578     (signal 'wrong-type-argument (list 'mixi-message-p message)))
1579   (mixi-message-realize message)
1580   (aref (cdr message) 4))
1581
1582 (defun mixi-message-time (message)
1583   "Return the date of MESSAGE."
1584   (unless (mixi-message-p message)
1585     (signal 'wrong-type-argument (list 'mixi-message-p message)))
1586   (mixi-message-realize message)
1587   (aref (cdr message) 5))
1588
1589 (defun mixi-message-content (message)
1590   "Return the content of MESSAGE."
1591   (unless (mixi-message-p message)
1592     (signal 'wrong-type-argument (list 'mixi-message-p message)))
1593   (mixi-message-realize message)
1594   (aref (cdr message) 6))
1595
1596 (defun mixi-message-set-owner (message owner)
1597   "Set the owner of MESSAGE."
1598   (unless (mixi-message-p message)
1599     (signal 'wrong-type-argument (list 'mixi-message-p message)))
1600   (aset (cdr message) 3 owner))
1601
1602 (defun mixi-message-set-title (message title)
1603   "Set the title of MESSAGE."
1604   (unless (mixi-message-p message)
1605     (signal 'wrong-type-argument (list 'mixi-message-p message)))
1606   (aset (cdr message) 4 title))
1607
1608 (defun mixi-message-set-time (message time)
1609   "Set the date of MESSAGE."
1610   (unless (mixi-message-p message)
1611     (signal 'wrong-type-argument (list 'mixi-message-p message)))
1612   (aset (cdr message) 5 time))
1613
1614 (defun mixi-message-set-content (message content)
1615   "Set the content of MESSAGE."
1616   (unless (mixi-message-p message)
1617     (signal 'wrong-type-argument (list 'mixi-message-p message)))
1618   (aset (cdr message) 6 content))
1619
1620 (defmacro mixi-message-list-page (&optional box)
1621   `(concat "/list_message.pl?page=%d"
1622            (when ,box (concat "&box=" ,box))))
1623
1624 (defconst mixi-message-list-regexp
1625   "<td><a HREF=\"view_message\\.pl\\?id=\\(.+\\)&box=\\(.+\\)\">")
1626
1627 (defun mixi-get-messages (&rest args)
1628   "Get messages."
1629   (when (> (length args) 2)
1630     (signal 'wrong-number-of-arguments
1631             (list 'mixi-get-messages (length args))))
1632   (let ((box (nth 0 args))
1633         (max-numbers (nth 1 args)))
1634     (when (or (not (mixi-message-box-p box))
1635               (mixi-message-box-p max-numbers))
1636       (setq box (nth 1 args))
1637       (setq max-numbers (nth 0 args)))
1638     (let ((items (mixi-get-matched-items
1639                   (mixi-message-list-page
1640                    (when box (mixi-message-box-name box)))
1641                   max-numbers
1642                   mixi-message-list-regexp)))
1643       (mapcar (lambda (item)
1644                 (mixi-make-message (nth 0 item) (nth 1 item)))
1645               items))))
1646
1647 ;; Introduction object.
1648 (defun mixi-make-introduction (parent owner content)
1649   "Return a introduction object."
1650   (cons 'mixi-introduction (vector parent owner content)))
1651
1652 (defmacro mixi-introduction-p (introduction)
1653   `(eq (mixi-object-class ,introduction) 'mixi-introduction))
1654
1655 (defun mixi-introduction-parent (introduction)
1656   "Return the parent of INTRODUCTION."
1657   (unless (mixi-introduction-p introduction)
1658     (signal 'wrong-type-argument (list 'mixi-introduction-p introduction)))
1659   (aref (cdr introduction) 0))
1660
1661 (defun mixi-introduction-owner (introduction)
1662   "Return the owner of INTRODUCTION."
1663   (unless (mixi-introduction-p introduction)
1664     (signal 'wrong-type-argument (list 'mixi-introduction-p introduction)))
1665   (aref (cdr introduction) 1))
1666
1667 (defun mixi-introduction-content (introduction)
1668   "Return the content of INTRODUCTION."
1669   (unless (mixi-introduction-p introduction)
1670     (signal 'wrong-type-argument (list 'mixi-introduction-p introduction)))
1671   (aref (cdr introduction) 3))
1672
1673 (defmacro mixi-introduction-list-page (&optional friend)
1674   `(concat "/show_intro.pl?page=%d"
1675            (when ,friend (concat "&id=" (mixi-friend-id ,friend)))))
1676
1677 (defconst mixi-introduction-list-regexp
1678   "<tr bgcolor=#FFFFFF>
1679 <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>
1680 \\(.+\\)</td></a>
1681
1682 <td WIDTH=480>
1683 \\(´Ø·¸¡§.+<br>
1684
1685
1686 \\(\\(.\\|\n<br>\\)+\\)\\|
1687 \\(\\(.\\|\n<br>\\)+\\)\\)
1688
1689
1690
1691
1692 </td>
1693 </tr>")
1694 (defconst mixi-my-introduction-list-regexp
1695   "<tr bgcolor=#FFFFFF>
1696 <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>
1697 \\(.+\\)</td></a>
1698
1699
1700 <td WIDTH=480>
1701 \\(´Ø·¸¡§.+<br>
1702
1703
1704 \\(\\(.\\|\n<br>\\)+\\)\\|
1705 \\(\\(.\\|\n<br>\\)+\\)\\)
1706
1707
1708 <br>
1709 <a href=\"edit_intro\\.pl\\?id=\\1&type=edit\">¤³¤Îͧ¿Í¤ò¾Ò²ð¤¹¤ë</a>
1710
1711
1712 <BR>
1713 <a href=\"delete_intro\\.pl\\?id=\\1\">ºï½ü</a>
1714
1715 </td>
1716 </tr>")
1717
1718 (defun mixi-get-introductions (&rest args)
1719   "Get introductions."
1720   (when (> (length args) 2)
1721     (signal 'wrong-number-of-arguments
1722             (list 'mixi-get-introduction (length args))))
1723   (let ((friend (nth 0 args))
1724         (max-numbers (nth 1 args)))
1725     (when (or (not (mixi-friend-p friend)) (mixi-friend-p max-numbers))
1726       (setq friend (nth 1 args))
1727       (setq max-numbers (nth 0 args)))
1728     (unless (or (null friend) (mixi-friend-p friend))
1729       (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
1730     (let* ((regexp (if friend mixi-introduction-list-regexp
1731                      mixi-my-introduction-list-regexp))
1732            (items (mixi-get-matched-items (mixi-introduction-list-page friend)
1733                                           max-numbers
1734                                           regexp)))
1735       (mapcar (lambda (item)
1736                 (mixi-make-introduction (or friend (mixi-make-me))
1737                                         (mixi-make-friend (nth 0 item)
1738                                                           (nth 1 item))
1739                                         (mixi-remove-markup (nth 2 item))))
1740               items))))
1741
1742 (provide 'mixi)
1743
1744 ;;; mixi.el ends here