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