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