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