4f708cff39ad95100e8e94d5873ace092ed5bde5
[elisp/mixi.git] / mixi.el
1 ;; mixi.el --- API libraries for accessing to mixi -*- coding: euc-jp -*-
2
3 ;; Copyright (C) 2005, 2006, 2007 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 GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
24
25 ;;; Commentary:
26
27 ;; APIs for getting contents:
28 ;;
29 ;;  * mixi-get-friends
30 ;;  * mixi-get-favorites
31 ;;  * mixi-get-logs
32 ;;  * mixi-get-recommended-friends (indies)
33 ;;  * mixi-get-diaries
34 ;;  * mixi-get-new-diaries
35 ;;  * mixi-search-diaries
36 ;;  * mixi-get-communities
37 ;;  * mixi-search-communities
38 ;;  * mixi-get-recommended-communities (indies)
39 ;;  * mixi-get-bbses
40 ;;  * mixi-get-new-bbses
41 ;;  * mixi-search-bbses
42 ;;  * mixi-get-comments
43 ;;  * mixi-get-new-comments
44 ;;  * mixi-get-new-bbs-comments
45 ;;  * mixi-get-messages
46 ;;  * mixi-get-introductions (broken)
47 ;;  * mixi-get-news
48 ;;
49 ;; APIs for posting:
50 ;;
51 ;;  * mixi-post-diary
52 ;;  * mixi-post-topic
53 ;;  * mixi-post-comment
54 ;;  * mixi-post-message
55 ;; 
56 ;; Utilities:
57 ;;
58 ;;  * mixi-remove-markup (half broken)
59
60 ;; Examples:
61 ;;
62 ;; Display newest 3 diaries like a mail format.
63 ;;
64 ;; (let ((range 3)
65 ;;       (buffer (get-buffer-create "*temp*"))
66 ;;       (format "%Y/%m/%d %H:%M"))
67 ;;   (pop-to-buffer buffer)
68 ;;   (let ((diaries (mixi-get-new-diaries range)))
69 ;;     (while diaries
70 ;;       (let* ((diary (car diaries))
71 ;;           (subject (mixi-diary-title diary))
72 ;;           (from (mixi-friend-nick (mixi-diary-owner diary)))
73 ;;           (date (format-time-string format (mixi-diary-time diary)))
74 ;;           (body (mixi-remove-markup (mixi-diary-content diary))))
75 ;;      (insert "From: " from "\n"
76 ;;              "Subject: " subject "\n"
77 ;;              "Date: " date "\n\n"
78 ;;              body "\n\n"))
79 ;;       (setq diaries (cdr diaries))))
80 ;;   (set-buffer-modified-p nil)
81 ;;   (setq buffer-read-only t)
82 ;;   (goto-char (point-min)))
83 ;;
84 ;; Display newest 3 diaries including newest 3 comments like a mail format.
85 ;; Comments are displayed like a reply mail.
86 ;;
87 ;; (let ((range 3)
88 ;;       (buffer (get-buffer-create "*temp*"))
89 ;;       (format "%Y/%m/%d %H:%M"))
90 ;;   (pop-to-buffer buffer)
91 ;;   (let ((diaries (mixi-get-new-diaries range)))
92 ;;     (while diaries
93 ;;       (let* ((diary (car diaries))
94 ;;           (subject (mixi-diary-title diary))
95 ;;           (from (mixi-friend-nick (mixi-diary-owner diary)))
96 ;;           (date (format-time-string format (mixi-diary-time diary)))
97 ;;           (body (mixi-remove-markup (mixi-diary-content diary))))
98 ;;      (insert "From: " from "\n"
99 ;;              "Subject: " subject "\n"
100 ;;              "Date: " date "\n\n"
101 ;;              body "\n\n")
102 ;;      (let ((comments (mixi-get-comments diary range)))
103 ;;        (while comments
104 ;;          (let* ((comment (car comments))
105 ;;                 (from (mixi-friend-nick (mixi-comment-owner comment)))
106 ;;                 (subject (concat "Re: " subject))
107 ;;                 (date (format-time-string format
108 ;;                                           (mixi-comment-time comment)))
109 ;;                 (body (mixi-remove-markup (mixi-comment-content comment))))
110 ;;            (insert "From: " from "\n"
111 ;;                    "Subject: " subject "\n"
112 ;;                    "Date: " date "\n\n"
113 ;;                    body "\n\n"))
114 ;;          (setq comments (cdr comments)))))
115 ;;       (setq diaries (cdr diaries))))
116 ;;   (set-buffer-modified-p nil)
117 ;;   (setq buffer-read-only t)
118 ;;   (goto-char (point-min)))
119
120 ;; Bug reports:
121 ;;
122 ;; If you have bug reports and/or suggestions for improvement, please
123 ;; send them via <URL:http://mixi.jp/view_community.pl?id=1596390>.
124
125 ;;; Code:
126
127 (eval-when-compile (require 'cl))
128
129 ;; Functions and variables which should be defined in the other module
130 ;; at run-time.
131 (eval-when-compile
132   (defvar w3m-use-cookies)
133   (defvar url-request-method)
134   (defvar url-request-data)
135   (defvar url-request-extra-headers)
136   (defvar url-show-status)
137   (autoload 'w3m-decode-buffer "w3m")
138   (autoload 'w3m-retrieve "w3m")
139   (autoload 'url-retrieve-synchronously "url"))
140
141 (defconst mixi-revision "$Revision: 1.178 $")
142
143 (defgroup mixi nil
144   "API library for accessing to mixi."
145   :group 'hypermedia)
146
147 (defcustom mixi-url "http://mixi.jp"
148   "*The URL of mixi."
149   :type 'string
150   :group 'mixi)
151
152 (defcustom mixi-directory (expand-file-name "~/.mixi")
153   "*Where to look for mixi files."
154   :type 'directory
155   :group 'mixi)
156
157 (defcustom mixi-coding-system 'euc-jp
158   "*Coding system for mixi."
159   :type 'coding-system
160   :group 'mixi)
161
162 (defcustom mixi-curl-program "curl"
163   "*The program name of `curl'."
164   :type 'file
165   :group 'mixi)
166
167 (defcustom mixi-curl-cookie-file (expand-file-name "cookies.txt"
168                                                    mixi-directory)
169   "*The location of cookie file created by `curl'."
170   :type 'file
171   :group 'mixi)
172
173 (defcustom mixi-backend
174   (or (condition-case nil
175           (progn
176             (require 'w3m)
177             'w3m)
178         (error))
179       (condition-case nil
180           (progn
181             (require 'url)
182             (if (fboundp 'url-retrieve-synchronously)
183                 'url))
184         (error))
185       (if (and (fboundp 'executable-find)
186                (executable-find mixi-curl-program))
187           'curl)
188       (error "Cannot set `mixi-backend'."))
189   "*The backend for accessing to mixi."
190   :type '(radio (const :tag "Use emacs-w3m" w3m)
191                 (const :tag "Use url.el" url)
192                 (const :tag "Use curl" curl)
193                 (symbol :tag "The other backend"))
194   :group 'mixi)
195
196 (defcustom mixi-login-use-ssl nil
197   "*If non-ni, login using SSL."
198   :type 'boolean
199   :group 'mixi)
200
201 (defcustom mixi-default-email nil
202   "*Default E-mail address that is used to login automatically."
203   :type '(radio (string :tag "E-mail address")
204                 (const :tag "Asked when it is necessary" nil))
205   :group 'mixi)
206
207 (defcustom mixi-default-password nil
208   "*Default password that is used to login automatically."
209   :type '(radio (string :tag "Password")
210                 (const :tag "Asked when it is necessary" nil))
211   :group 'mixi)
212
213 (defcustom mixi-accept-adult-contents t
214   "*If non-nil, accept to access to the adult contents."
215   :type 'boolean
216   :group 'mixi)
217
218 (defcustom mixi-continuously-access-interval 4.0
219   "*Time interval between each mixi access.
220 Increase this value when unexpected error frequently occurs."
221   :type 'number
222   :group 'mixi)
223
224 (defcustom mixi-cache-expires nil
225   "*Seconds for expiration of a cached object."
226   :type '(radio (integer :tag "Expired seconds")
227                 (const :tag "Don't expire" nil)
228                 (const :tag "Don't cache" t))
229   :group 'mixi)
230
231 ;; FIXME: Not implemented.
232 (defcustom mixi-cache-use-file t
233   "*If non-nil, caches are saved to files."
234   :type 'boolean
235   :group 'mixi)
236
237 (defvar mixi-temp-buffer-name " *mixi temp*")
238 (defvar mixi-me nil)
239
240 ;; Utilities.
241 (defmacro mixi-message (&rest strings)
242   `(concat "[mixi] " ,@strings))
243
244 (put 'mixi-realization-error
245      'error-message (mixi-message "Cannot realize object"))
246 (put 'mixi-realization-error
247      'error-conditions '(mixi-realization-error error))
248
249 (put 'mixi-post-error
250      'error-message (mixi-message "Cannot post"))
251 (put 'mixi-post-error
252      'error-conditions '(mixi-post-error error))
253
254 (defmacro mixi-realization-error (type object)
255   `(let ((data (if debug-on-error
256                    (list ,type ,object (buffer-string))
257                  (list ,type ,object))))
258      (signal 'mixi-realization-error data)))
259
260 (defmacro mixi-post-error (type &optional object)
261   `(let ((data (when debug-on-error (list (buffer-string)))))
262      (if ,object
263          (setq data (cons ,type (cons ,object data)))
264        (setq data (cons ,type data)))
265      (signal 'mixi-post-error data)))
266
267 (defconst mixi-message-adult-contents
268   "¤³¤Î¥Ú¡¼¥¸¤«¤éÀè¤Ï¥¢¥À¥ë¥È¡ÊÀ®¿Í¸þ¤±¡Ë¥³¥ó¥Æ¥ó¥Ä¤¬´Þ¤Þ¤ì¤Æ¤¤¤Þ¤¹¡£<br>
269 ±ÜÍ÷¤ËƱ°Õ¤µ¤ì¤¿Êý¤Î¤ß¡¢Àè¤Ø¤ª¿Ê¤ß¤¯¤À¤µ¤¤¡£")
270 (defconst mixi-message-continuously-accessing
271   "°ÂÄꤷ¤Æ¥µ¥¤¥È¤Î±¿±Ä¤ò¤ª¤³¤Ê¤¦°Ù¡¢´Ö³Ö¤ò¶õ¤±¤Ê¤¤Ï¢Â³Åª¤Ê¥Ú¡¼¥¸¤ÎÁ«°Ü¡¦¹¹<br>
272 ¿·¤ÏÀ©¸Â¤µ¤»¤Æ¤¤¤¿¤À¤¤¤Æ¤ª¤ê¤Þ¤¹¡£¤´ÌÂÏǤò¤ª¤«¤±¤¤¤¿¤·¤Þ¤¹¤¬¡¢¤·¤Ð¤é¤¯¤ª<br>
273 ÂÔ¤Á¤¤¤¿¤À¤¤¤Æ¤«¤éÁàºî¤ò¤ª¤³¤Ê¤Ã¤Æ¤¯¤À¤µ¤¤¡£")
274 (defconst mixi-warning-continuously-accessing
275   "´Ö³Ö¤ò¶õ¤±¤Ê¤¤Ï¢Â³Åª¤Ê¥Ú¡¼¥¸¤ÎÁ«°Ü¡¦¹¹¿·¤òÉÑÈˤˤª¤³¤Ê¤ï¤ì¤Æ¤¤¤ë¤³¤È¤¬¸«<br>
276 ¼õ¤±¤é¤ì¤Þ¤·¤¿¤Î¤Ç¡¢°ì»þŪ¤ËÁàºî¤òÄä»ß¤µ¤»¤Æ¤¤¤¿¤À¤­¤Þ¤¹¡£¿½¤·Ìõ¤´¤¶¤¤¤Þ<br>
277 ¤»¤ó¤¬¡¢¤·¤Ð¤é¤¯¤Î´Ö¤ªÂÔ¤Á¤¯¤À¤µ¤¤¡£")
278
279 (defmacro mixi-retrieve (url &optional post-data)
280   `(funcall (intern (concat "mixi-" (symbol-name mixi-backend) "-retrieve"))
281             ,url ,post-data))
282
283 (defmacro mixi-post-form (url fields)
284   `(funcall (intern (concat "mixi-" (symbol-name mixi-backend) "-post-form"))
285             ,url ,fields))
286
287 (defun mixi-parse-buffer (url buffer &optional post-data)
288   (when (string-match mixi-message-adult-contents buffer)
289     (if mixi-accept-adult-contents
290         (setq buffer (mixi-retrieve url "submit=agree"))
291       (setq buffer (mixi-retrieve (concat url "?")))))
292   (when (string-match mixi-warning-continuously-accessing buffer)
293     (error (mixi-message "Access denied.  Please wait a while and increase "
294                          "the value of `mixi-continuously-access-interval'.")))
295   (if (not (string-match mixi-message-continuously-accessing buffer))
296       buffer
297     (message (mixi-message "Waiting for continuously accessing..."))
298     (sleep-for mixi-continuously-access-interval)
299     (mixi-retrieve url post-data)))
300
301 (defmacro mixi-expand-url (url)
302   `(if (string-match "^http" ,url)
303        ,url
304      (concat mixi-url ,url)))
305
306 ;; FIXME: Support files.
307 (defun mixi-make-form-data (fields)
308   "Make form data and return (CONTENT-TYPE . FORM-DATA)."
309   (let* ((boundary (apply 'format "--_%d_%d_%d" (current-time)))
310          (content-type (concat "multipart/form-data; boundary=" boundary))
311          (form-data
312           (mapconcat
313            (lambda (field)
314              (concat "--" boundary "\r\n"
315                      "Content-Disposition: form-data; name=\""
316                      (car field) "\"\r\n"
317                      "\r\n"
318                      (encode-coding-string (cdr field) mixi-coding-system)))
319            fields "\r\n")))
320     (cons content-type (concat form-data "\r\n--" boundary "--"))))
321
322 (defun mixi-url-retrieve (url &optional post-data extra-headers)
323   "Retrieve the URL and return gotten strings."
324   (let* ((url-request-method (if post-data "POST" "GET"))
325          (url-request-data post-data)
326          (url-request-extra-headers extra-headers)
327          (url (mixi-expand-url url))
328          url-show-status
329          (buffer (url-retrieve-synchronously url))
330          ret)
331     (unless (bufferp buffer)
332       (error (mixi-message "Cannot retrieve: " url)))
333     (with-current-buffer buffer
334       (goto-char (point-min))
335       (while (looking-at "HTTP/[0-9]+\\.[0-9]+ [13][0-9][0-9]")
336         (delete-region (point) (re-search-forward "\r?\n\r?\n")))
337       (unless (looking-at "HTTP/[0-9]+\\.[0-9]+ 200")
338         (error (mixi-message "Cannot retrieve: " url)))
339       (delete-region (point) (re-search-forward "\r?\n\r?\n"))
340       (setq ret (decode-coding-string (buffer-string) mixi-coding-system))
341       (kill-buffer buffer)
342       (mixi-parse-buffer url ret post-data))))
343
344 (defun mixi-url-post-form (url fields)
345   (let* ((form-data (mixi-make-form-data fields))
346          (extra-headers `(("Content-Type" . ,(car form-data)))))
347     (mixi-url-retrieve url (cdr form-data) extra-headers)))
348
349 (defun mixi-w3m-retrieve (url &optional post-data)
350   "Retrieve the URL and return gotten strings."
351   (let ((url (mixi-expand-url url)))
352     (with-temp-buffer
353       (if (not (string= (w3m-retrieve url nil nil post-data) "text/html"))
354           (error (mixi-message "Cannot retrieve: " url))
355         (w3m-decode-buffer url)
356         (let ((ret (buffer-substring-no-properties (point-min) (point-max))))
357           (mixi-parse-buffer url ret post-data))))))
358
359 (defun mixi-w3m-post-form (url fields)
360   (let ((form-data (mixi-make-form-data fields)))
361     (mixi-w3m-retrieve url form-data)))
362
363 (defun mixi-curl-retrieve (url &optional post-data form-data)
364   "Retrieve the URL and return gotten strings."
365   (with-temp-buffer
366     (if (fboundp 'set-buffer-multibyte)
367         (set-buffer-multibyte nil))
368     (let ((orig-mode (default-file-modes))
369           (coding-system-for-read 'binary)
370           (coding-system-for-write 'binary)
371           process ret)
372       (unwind-protect
373           (progn
374             (set-default-file-modes 448)
375             (setq process
376                   (apply #'start-process "curl" (current-buffer)
377                          mixi-curl-program
378                          (append (if post-data '("-d" "@-"))
379                                  form-data
380                                  (list "-i" "-L" "-s"
381                                        "-b" mixi-curl-cookie-file
382                                        "-c" mixi-curl-cookie-file
383                                        (mixi-expand-url url)))))
384             (set-process-sentinel process #'ignore))
385         (set-default-file-modes orig-mode))
386       (when post-data
387         (process-send-string process (concat post-data "\n"))
388         (process-send-eof process))
389       (while (eq (process-status process) 'run)
390         (accept-process-output process 1))
391       (goto-char (point-min))
392       (while (looking-at "HTTP/[0-9]+\\.[0-9]+ [13][0-9][0-9]")
393         (delete-region (point) (re-search-forward "\r?\n\r?\n")))
394       (unless (looking-at "HTTP/[0-9]+\\.[0-9]+ 200")
395         (error (mixi-message "Cannot retrieve: " url)))
396       (delete-region (point) (re-search-forward "\r?\n\r?\n"))
397       (setq ret (decode-coding-string (buffer-string) mixi-coding-system))
398       (mixi-parse-buffer url ret post-data))))
399
400 (defun mixi-curl-post-form (url fields)
401   (let (form-data)
402     (mapcar (lambda (field)
403               (push "-F" form-data)
404               (push (concat (car field) "="
405                             (encode-coding-string (cdr field)
406                                                   mixi-coding-system))
407                     form-data))
408             fields)
409     (mixi-curl-retrieve url nil (reverse form-data))))
410
411 (defconst mixi-my-id-regexp
412   "<a href=\"show_profile\\.pl\\?id=\\([0-9]+\\)")
413
414 (defun mixi-login (&optional email password)
415   "Login to mixi."
416   (when (and (eq mixi-backend 'w3m) (not w3m-use-cookies))
417     (error (mixi-message "Require to accept cookies.  Please set "
418                          "`w3m-use-cookies' to t.")))
419   (let ((email (or email mixi-default-email
420                    (read-from-minibuffer (mixi-message "Login Email: "))))
421         (password (or password mixi-default-password
422                       (read-passwd (mixi-message "Login Password: ")))))
423     (let ((url (mixi-expand-url "/login.pl")))
424       (when (and mixi-login-use-ssl (string-match "^http:" url))
425         (setq url (replace-match "https:" nil nil url)))
426       (let ((buffer (mixi-retrieve url
427                                    (concat "email=" email
428                                            "&password=" password
429                                            "&next_url=/home.pl"
430                                            "&sticky=on"))))
431         (unless (string-match "url=/check\\.pl\\?n=" buffer)
432           (error (mixi-message "Cannot login")))
433         (setq buffer (mixi-retrieve "/check.pl?n=home.pl"))
434         (if (string-match mixi-my-id-regexp buffer)
435             (setq mixi-me (mixi-make-friend (match-string 1 buffer)))
436           (error (mixi-message "Cannot login")))))))
437
438 (defun mixi-logout ()
439   (mixi-retrieve "/logout.pl"))
440
441 (defconst mixi-login-form "<form action=\"/login.pl\" method=\"post\">")
442
443 (defmacro with-mixi-retrieve (url &rest body)
444   `(with-current-buffer (get-buffer-create mixi-temp-buffer-name)
445      (when ,url
446        (erase-buffer)
447        (insert (mixi-retrieve ,url))
448        (goto-char (point-min))
449        (when (search-forward mixi-login-form nil t)
450          (mixi-login)
451          (erase-buffer)
452          (insert (mixi-retrieve ,url))))
453      (goto-char (point-min))
454      ,@body))
455 (put 'with-mixi-retrieve 'lisp-indent-function 'defun)
456 (put 'with-mixi-retrieve 'edebug-form-spec '(body))
457
458 (defmacro with-mixi-post-form (url fields &rest body)
459   `(with-current-buffer (get-buffer-create mixi-temp-buffer-name)
460      (when ,url
461        (erase-buffer)
462        (insert (mixi-post-form ,url ,fields))
463        (goto-char (point-min))
464        (when (search-forward mixi-login-form nil t)
465          (mixi-login)
466          (erase-buffer)
467          (insert (mixi-post-form ,url ,fields))))
468      (goto-char (point-min))
469      ,@body))
470 (put 'with-mixi-post-form 'lisp-indent-function 'defun)
471 (put 'with-mixi-post-form 'edebug-form-spec '(body))
472
473 (defun mixi-get-matched-items (url regexp &optional range reverse)
474   "Get matched items to REGEXP in URL."
475   (let ((page 1)
476         ids)
477     (catch 'end
478       (while (or (null range) (< (length ids) range))
479         (with-mixi-retrieve (when url (format url page))
480           (let ((func (if reverse (progn
481                                     (goto-char (point-max))
482                                     're-search-backward)
483                         're-search-forward))
484                 found)
485             (while (and (funcall func regexp nil t)
486                         (or (null range) (< (length ids) range)))
487               (let ((num 1)
488                     list)
489                 (while (match-string num)
490                   (setq list (cons (match-string num) list))
491                   (incf num))
492                 (when (not (member (reverse list) ids))
493                   (setq found t)
494                   (setq ids (cons (reverse list) ids)))))
495             (when (not found)
496               (throw 'end ids))))
497         (incf page)))
498     (reverse ids)))
499
500 ;; stolen (and modified) from shimbun.el
501 (defun mixi-remove-markup (string)
502   "Remove markups from STRING."
503   (with-temp-buffer
504     (insert (or string ""))
505     (save-excursion
506       (goto-char (point-min))
507       (while (search-forward "<!--" nil t)
508         (delete-region (match-beginning 0)
509                        (or (search-forward "-->" nil t)
510                            (point-max))))
511       (goto-char (point-min))
512       (while (re-search-forward "<[^>]+>" nil t)
513         (replace-match "" t t))
514       (goto-char (point-min))
515       (while (re-search-forward "\r" nil t)
516         (replace-match "\n" t t)))
517     ;; FIXME: Decode entities.
518     (buffer-string)))
519
520 ;; stolen (and modified) from w3m.el
521 (defconst mixi-entity-alist '(("gt" . ">")
522                               ("lt" . "<")
523                               ("amp" . "&"))
524   "Alist of html character entities and values.")
525
526 ;; stolen (and modified) from w3m.el
527 (defun mixi-encode-specials-string (str)
528   "Encode special characters in the string STR."
529   (let ((pos 0)
530         (buf))
531     (while (string-match "[<>&]" str pos)
532       (setq buf
533             (cons ";"
534                   (cons (car (rassoc (match-string 0 str) mixi-entity-alist))
535                         (cons "&"
536                               (cons (substring str pos (match-beginning 0))
537                                     buf))))
538             pos (match-end 0)))
539     (if buf
540         (apply 'concat (nreverse (cons (substring str pos) buf)))
541       str)))
542
543 ;; stolen (and modified) from w3m.el
544 (defun mixi-url-encode-string (string)
545   (apply (function concat)
546          (mapcar
547           (lambda (char)
548             (cond
549              ((eq char ?\n)             ; newline
550               "%0D%0A")
551              ((string-match "[-a-zA-Z0-9_:/.]" (char-to-string char)) ; xxx?
552               (char-to-string char))    ; printable
553              ((char-equal char ?\x20)   ; space
554               "+")
555              (t
556               (format "%%%02x" char)))) ; escape
557           ;; Coerce a string into a list of chars.
558           (append (encode-coding-string (or string "") mixi-coding-system)
559                   nil))))
560
561 (defun mixi-url-encode-and-quote-percent-string (string)
562   (let ((string (mixi-url-encode-string string))
563         (pos 0))
564     (while (string-match "%" string pos)
565       (setq string (replace-match "%%" nil nil string))
566       (setq pos (+ (match-end 0) 1)))
567     string))
568
569 ;; Object.
570 (defconst mixi-object-prefix "mixi-")
571
572 (defmacro mixi-object-class (object)
573   `(car-safe ,object))
574
575 (defmacro mixi-object-p (object)
576   `(let ((class (mixi-object-class ,object)))
577      (when (symbolp class)
578        (eq (string-match (concat "^" mixi-object-prefix)
579                          (symbol-name class)) 0))))
580
581 (defun mixi-object-name (object)
582   "Return the name of OBJECT."
583   (unless (mixi-object-p object)
584     (signal 'wrong-type-argument (list 'mixi-object-p object)))
585   (let ((class (mixi-object-class object)))
586     (substring (symbol-name class) (length mixi-object-prefix))))
587
588 (defun mixi-read-object (exp)
589   "Read one Lisp expression as mixi object."
590   (if (mixi-object-p exp)
591       (let ((func (intern (concat mixi-object-prefix "make-"
592                                   (mixi-object-name exp))))
593             (args (mapcar (lambda (arg)
594                             (mixi-read-object arg))
595                           (cdr exp))))
596         (let ((object (apply func (cdr args))))
597           (when (car args)
598             (mixi-object-set-timestamp object (car args)))
599           object))
600     exp))
601
602 (defun mixi-object-timestamp (object)
603   "Return the timestamp of OJBECT."
604   (unless (mixi-object-p object)
605     (signal 'wrong-type-argument (list 'mixi-object-p object)))
606   (aref (cdr object) 0))
607 (defalias 'mixi-object-realized-p 'mixi-object-timestamp)
608
609 (defun mixi-object-owner (object)
610   "Return the owner of OBJECT."
611   (unless (mixi-object-p object)
612     (signal 'wrong-type-argument (list 'mixi-object-p object)))
613   (let ((func (intern (concat mixi-object-prefix
614                               (mixi-object-name object) "-owner"))))
615     (funcall func object)))
616
617 (defun mixi-object-id (object)
618   "Return the id of OBJECT."
619   (unless (mixi-object-p object)
620     (signal 'wrong-type-argument (list 'mixi-object-p object)))
621   (let ((func (intern (concat mixi-object-prefix
622                               (mixi-object-name object) "-id"))))
623     (funcall func object)))
624
625 (defun mixi-object-time (object)
626   "Return the time of OBJECT."
627   (unless (mixi-object-p object)
628     (signal 'wrong-type-argument (list 'mixi-object-p object)))
629   (let ((func (intern (concat mixi-object-prefix
630                               (mixi-object-name object) "-time"))))
631     (funcall func object)))
632
633 (defun mixi-object-title (object)
634   "Return the title of OBJECT."
635   (unless (mixi-object-p object)
636     (signal 'wrong-type-argument (list 'mixi-object-p object)))
637   (let ((func (intern (concat mixi-object-prefix
638                               (mixi-object-name object) "-title"))))
639     (funcall func object)))
640
641 (defun mixi-object-content (object)
642   "Return the content of OBJECT."
643   (unless (mixi-object-p object)
644     (signal 'wrong-type-argument (list 'mixi-object-p object)))
645   (let ((func (intern (concat mixi-object-prefix
646                               (mixi-object-name object) "-content"))))
647     (funcall func object)))
648
649 (defun mixi-object-set-timestamp (object timestamp)
650   "Set the timestamp of OBJECT."
651   (unless (mixi-object-p object)
652     (signal 'wrong-type-argument (list 'mixi-object-p object)))
653   (aset (cdr object) 0 timestamp))
654
655 (defmacro mixi-object-touch (object)
656   `(mixi-object-set-timestamp ,object (current-time)))
657
658 (defconst mixi-object-url-regexp
659   "/\\(show\\|view\\)_\\([a-z]+\\)\\.pl")
660
661 (defun mixi-make-object-from-url (url)
662   "Return a mixi object from URL."
663   (if (string-match mixi-object-url-regexp url)
664       (let ((name (match-string 2 url)))
665         (cond ((string= name "bbs")
666                (setq name "topic"))
667               ((string= name "profile")
668                (setq name "friend")))
669         (let ((func (intern (concat mixi-object-prefix "make-" name
670                                     "-from-url"))))
671           (funcall func url)))
672     (when (string-match "/home\\.pl" url)
673       (mixi-make-friend-from-url url))))
674
675 ;; Cache.
676 ;; stolen from time-date.el
677 (defun mixi-time-less-p (t1 t2)
678   "Say whether time value T1 is less than time value T2."
679   (unless (numberp (cdr t1))
680     (setq t1 (cons (car t1) (car (cdr t1)))))
681   (unless (numberp (cdr t2))
682     (setq t2 (cons (car t2) (car (cdr t2)))))
683   (or (< (car t1) (car t2))
684       (and (= (car t1) (car t2))
685            (< (cdr t1) (cdr t2)))))
686
687 (defun mixi-time-add (t1 t2)
688   "Add two time values.  One should represent a time difference."
689   (unless (numberp (cdr t1))
690     (setq t1 (cons (car t1) (car (cdr t1)))))
691   (unless (numberp (cdr t2))
692     (setq t2 (cons (car t2) (car (cdr t2)))))
693   (let ((low (+ (cdr t1) (cdr t2))))
694     (cons (+ (car t1) (car t2) (lsh low -16)) low)))
695
696 ;; stolen from time-date.el
697 (defun mixi-seconds-to-time (seconds)
698   "Convert SECONDS (a floating point number) to a time value."
699   (cons (floor seconds 65536)
700         (floor (mod seconds 65536))))
701
702 (defun mixi-cache-expired-p (object)
703   "Whether a cache of OBJECT is expired."
704   (let ((timestamp (mixi-object-timestamp object)))
705     (unless (or (null mixi-cache-expires)
706                  (null timestamp))
707       (if (numberp mixi-cache-expires)
708           (mixi-time-less-p
709            (mixi-time-add timestamp (mixi-seconds-to-time mixi-cache-expires))
710            (current-time))
711         t))))
712
713 (defun mixi-make-cache (key value table)
714   "Make a cache object and return it."
715   (let ((cache (gethash key table)))
716     (if (and cache (not (mixi-cache-expired-p cache)))
717         cache
718       (puthash key value table))))
719
720 (defconst mixi-cache-file-regexp "[a-z]+-cache$")
721 (defconst mixi-cache-regexp (concat mixi-object-prefix
722                                     mixi-cache-file-regexp))
723
724 (defun mixi-save-cache ()
725   (let ((cache-directory (expand-file-name "cache" mixi-directory)))
726     (unless (file-directory-p cache-directory)
727       (make-directory cache-directory t))
728     (let ((caches (apropos-internal mixi-cache-regexp 'boundp)))
729       (while caches
730         (with-temp-file (expand-file-name
731                          (substring (symbol-name (car caches))
732                                     (length mixi-object-prefix))
733                          cache-directory)
734           (let ((coding-system-for-write mixi-coding-system)
735                 (cache (symbol-value (car caches))))
736             (insert "#s(hash-table size "
737                     (number-to-string (hash-table-count cache))
738                     " test equal data (\n")
739             (maphash
740              (lambda (key value)
741                (let (print-level print-length)
742                  (insert (prin1-to-string key) " "
743                          (prin1-to-string value) "\n")))
744              cache))
745           (insert "))"))
746         (setq caches (cdr caches))))))
747
748 ;; stolen (and modified) from lsdb.el
749 (defun mixi-read-cache (&optional marker)
750   "Read one Lisp expression as text from MARKER, return as Lisp object."
751   (save-excursion
752     (goto-char marker)
753     (if (looking-at "^#s(")
754         (let ((end-marker
755                (progn
756                  (forward-char 2);skip "#s"
757                  (forward-sexp);move to the left paren
758                  (point-marker))))
759           (with-temp-buffer
760             (buffer-disable-undo)
761             (insert-buffer-substring (marker-buffer marker)
762                                      marker end-marker)
763             (goto-char (point-min))
764             (delete-char 2)
765             (let ((object (read (current-buffer)))
766                   data)
767               (if (eq 'hash-table (car object))
768                   (progn
769                     (setq data (plist-get (cdr object) 'data))
770                     (while data
771                       (pop data);throw it away
772                       (mixi-read-object (pop data))))
773                 object))))
774       (read marker))))
775
776 (defun mixi-load-cache ()
777   (let ((cache-directory (expand-file-name "cache" mixi-directory)))
778     (when (file-directory-p cache-directory)
779       ;; FIXME: Load friend and community first.
780       (let ((files (directory-files cache-directory t
781                                     mixi-cache-file-regexp)))
782         (while files
783           (let ((buffer (find-file-noselect (car files))))
784             (unwind-protect
785                 (save-excursion
786                   (set-buffer buffer)
787                   (goto-char (point-min))
788                   (re-search-forward "^#s(")
789                   (goto-char (match-beginning 0))
790                   (mixi-read-cache (point-marker)))
791               (kill-buffer buffer)))
792           (setq files (cdr files)))))))
793
794 ;; Friend object.
795 (defvar mixi-friend-cache (make-hash-table :test 'equal))
796 (defun mixi-make-friend (id &optional nick name sex address age birthday
797                             blood-type birthplace hobby job organization
798                             profile)
799   "Return a friend object."
800   (mixi-make-cache id (cons 'mixi-friend (vector nil id nick name sex address
801                                                  age birthday blood-type
802                                                  birthplace hobby job
803                                                  organization profile))
804                    mixi-friend-cache))
805
806 (defun mixi-make-me ()
807   "Return a my object."
808   (unless mixi-me
809     (with-mixi-retrieve "/home.pl"
810       (if (re-search-forward mixi-my-id-regexp nil t)
811           (setq mixi-me (mixi-make-friend (match-string 1)))
812         (signal 'error (list 'who-am-i)))))
813   mixi-me)
814
815 (defconst mixi-friend-url-regexp
816   "/show_\\(friend\\|profile\\)\\.pl\\?id=\\([0-9]+\\)")
817
818 (defun mixi-make-friend-from-url (url)
819   "Return a friend object from URL."
820   (if (string-match mixi-friend-url-regexp url)
821       (let ((id (match-string 2 url)))
822         (mixi-make-friend id))
823     (when (string-match "/home\\.pl" url)
824       (mixi-make-me))))
825
826 (defmacro mixi-friend-p (friend)
827   `(eq (mixi-object-class ,friend) 'mixi-friend))
828
829 (defmacro mixi-friend-page (friend)
830   `(concat "/show_profile.pl?id=" (mixi-friend-id ,friend)))
831
832 (defconst mixi-friend-nick-regexp
833   "<h3>\\(.*\\)¤µ¤ó([0-9]+)</h3>")
834 (defconst mixi-friend-name-regexp
835   "<dt>̾Á°</dt>\n?<dd>\\(.+?\\)\\(<img\\|</dd>\\)")
836 (defconst mixi-friend-sex-regexp
837   "<dt>À­ÊÌ</dt>\n?<dd>\\([Ã˽÷]\\)À­\\(<img\\|</dd>\\)")
838 (defconst mixi-friend-address-regexp
839   "<dt>¸½½»½ê</dt>\n?<dd>\\(.+?\\)\\(<img \\|</dd>\\)")
840 (defconst mixi-friend-age-regexp
841   "<dt>ǯÎð</dt>\n?<dd>\\([0-9]+\\)ºÐ\\( <img\\|</dd>\\)")
842 (defconst mixi-friend-birthday-regexp
843   "<dt>ÃÂÀ¸Æü</dt>\n?<dd>\\([0-9]+\\)·î\\([0-9]+\\)Æü\\(<img \\|</dd>\\)")
844 (defconst mixi-friend-blood-type-regexp
845   "<dt>·ì±Õ·¿</dt>\n?<dd>\\([ABO]B?\\)·¿\\(<img \\|</dd>\\)")
846 (defconst mixi-friend-birthplace-regexp
847   "<dt>½Ð¿ÈÃÏ</dt>\n?<dd>\\(.+?\\)\\(<img \\|</dd>\\)")
848 (defconst mixi-friend-hobby-regexp
849   "<dt>¼ñÌ£</dt>\n?<dd>\\(.+?\\)\\(<img \\|</dd>\\)")
850 (defconst mixi-friend-job-regexp
851   "<dt>¿¦¶È</dt>\n?<dd>\\(.+?\\)\\(<img \\|</dd>\\)")
852 (defconst mixi-friend-organization-regexp
853   "<dt>½ê°</dt>\n?<dd>\\(.+?\\)\\(<img \\|</dd>\\)")
854 (defconst mixi-friend-profile-regexp
855   "<dt>¼«¸Ê¾Ò²ð</dt>\n?<dd class=\"userInput\">\\(.+?\\)</dd>")
856
857 (defun mixi-realize-friend (friend)
858   "Realize a FRIEND."
859   ;; FIXME: Check a expiration of cache?
860   (unless (mixi-object-realized-p friend)
861     (with-mixi-retrieve (mixi-friend-page friend)
862       (let ((case-fold-search t))
863         (if (re-search-forward mixi-friend-nick-regexp nil t)
864             (mixi-friend-set-nick friend (match-string 1))
865           (mixi-realization-error 'cannot-find-nick friend))
866         (when (re-search-forward mixi-friend-name-regexp nil t)
867           (mixi-friend-set-name friend (match-string 1)))
868         (when (re-search-forward mixi-friend-sex-regexp nil t)
869           (mixi-friend-set-sex friend (if (string= (match-string 1) "ÃË")
870                                           'male 'female)))
871         (when (re-search-forward mixi-friend-address-regexp nil t)
872           (mixi-friend-set-address friend (match-string 1)))
873         (when (re-search-forward mixi-friend-age-regexp nil t)
874           (mixi-friend-set-age friend (string-to-number (match-string 1))))
875         (when (re-search-forward mixi-friend-birthday-regexp nil t)
876           (mixi-friend-set-birthday
877            friend (list (string-to-number (match-string 1))
878                         (string-to-number (match-string 2)))))
879         (when (re-search-forward mixi-friend-blood-type-regexp nil t)
880           (mixi-friend-set-blood-type friend (intern (match-string 1))))
881         (when (re-search-forward mixi-friend-birthplace-regexp nil t)
882           (mixi-friend-set-birthplace friend (match-string 1)))
883         (when (re-search-forward mixi-friend-hobby-regexp nil t)
884           (mixi-friend-set-hobby friend (split-string (match-string 1) ", ")))
885         (when (re-search-forward mixi-friend-job-regexp nil t)
886           (mixi-friend-set-job friend (match-string 1)))
887         (when (re-search-forward mixi-friend-organization-regexp nil t)
888           (mixi-friend-set-organization friend (match-string 1)))
889         (when (re-search-forward mixi-friend-profile-regexp nil t)
890           (mixi-friend-set-profile friend (match-string 1)))))
891     (mixi-object-touch friend)))
892
893 (defun mixi-friend-id (friend)
894   "Return the id of FRIEND."
895   (unless (mixi-friend-p friend)
896     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
897   (aref (cdr friend) 1))
898
899 (defun mixi-friend-nick (friend)
900   "Return the nick of FRIEND."
901   (unless (mixi-friend-p friend)
902     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
903   (unless (aref (cdr friend) 2)
904     (mixi-realize-friend friend))
905   (aref (cdr friend) 2))
906
907 (defun mixi-friend-name (friend)
908   "Return the name of FRIEND."
909   (unless (mixi-friend-p friend)
910     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
911   (mixi-realize-friend friend)
912   (aref (cdr friend) 3))
913
914 (defun mixi-friend-sex (friend)
915   "Return the sex of FRIEND."
916   (unless (mixi-friend-p friend)
917     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
918   (mixi-realize-friend friend)
919   (aref (cdr friend) 4))
920
921 (defun mixi-friend-address (friend)
922   "Return the address of FRIEND."
923   (unless (mixi-friend-p friend)
924     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
925   (mixi-realize-friend friend)
926   (aref (cdr friend) 5))
927
928 (defun mixi-friend-age (friend)
929   "Return the age of FRIEND."
930   (unless (mixi-friend-p friend)
931     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
932   (mixi-realize-friend friend)
933   (aref (cdr friend) 6))
934
935 (defun mixi-friend-birthday (friend)
936   "Return the birthday of FRIEND."
937   (unless (mixi-friend-p friend)
938     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
939   (mixi-realize-friend friend)
940   (aref (cdr friend) 7))
941
942 (defun mixi-friend-blood-type (friend)
943   "Return the blood type of FRIEND."
944   (unless (mixi-friend-p friend)
945     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
946   (mixi-realize-friend friend)
947   (aref (cdr friend) 8))
948
949 (defun mixi-friend-birthplace (friend)
950   "Return the birthplace of FRIEND."
951   (unless (mixi-friend-p friend)
952     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
953   (mixi-realize-friend friend)
954   (aref (cdr friend) 9))
955
956 (defun mixi-friend-hobby (friend)
957   "Return the hobby of FRIEND."
958   (unless (mixi-friend-p friend)
959     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
960   (mixi-realize-friend friend)
961   (aref (cdr friend) 10))
962
963 (defun mixi-friend-job (friend)
964   "Return the job of FRIEND."
965   (unless (mixi-friend-p friend)
966     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
967   (mixi-realize-friend friend)
968   (aref (cdr friend) 11))
969
970 (defun mixi-friend-organization (friend)
971   "Return the organization of FRIEND."
972   (unless (mixi-friend-p friend)
973     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
974   (mixi-realize-friend friend)
975   (aref (cdr friend) 12))
976
977 (defun mixi-friend-profile (friend)
978   "Return the profile of FRIEND."
979   (unless (mixi-friend-p friend)
980     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
981   (mixi-realize-friend friend)
982   (aref (cdr friend) 13))
983
984 (defun mixi-friend-set-nick (friend nick)
985   "Set the nick of FRIEND."
986   (unless (mixi-friend-p friend)
987     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
988   (aset (cdr friend) 2 nick))
989
990 (defun mixi-friend-set-name (friend name)
991   "Set the name of FRIEND."
992   (unless (mixi-friend-p friend)
993     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
994   (aset (cdr friend) 3 name))
995
996 (defun mixi-friend-set-sex (friend sex)
997   "Set the sex of FRIEND."
998   (unless (mixi-friend-p friend)
999     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
1000   (aset (cdr friend) 4 sex))
1001
1002 (defun mixi-friend-set-address (friend address)
1003   "Set the address of FRIEND."
1004   (unless (mixi-friend-p friend)
1005     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
1006   (aset (cdr friend) 5 address))
1007
1008 (defun mixi-friend-set-age (friend age)
1009   "Set the age of FRIEND."
1010   (unless (mixi-friend-p friend)
1011     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
1012   (aset (cdr friend) 6 age))
1013
1014 (defun mixi-friend-set-birthday (friend birthday)
1015   "Set the birthday of FRIEND."
1016   (unless (mixi-friend-p friend)
1017     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
1018   (aset (cdr friend) 7 birthday))
1019
1020 (defun mixi-friend-set-blood-type (friend blood-type)
1021   "Set the blood type of FRIEND."
1022   (unless (mixi-friend-p friend)
1023     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
1024   (aset (cdr friend) 8 blood-type))
1025
1026 (defun mixi-friend-set-birthplace (friend birthplace)
1027   "Set the birthplace of FRIEND."
1028   (unless (mixi-friend-p friend)
1029     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
1030   (aset (cdr friend) 9 birthplace))
1031
1032 (defun mixi-friend-set-hobby (friend hobby)
1033   "Set the hobby of FRIEND."
1034   (unless (mixi-friend-p friend)
1035     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
1036   (aset (cdr friend) 10 hobby))
1037
1038 (defun mixi-friend-set-job (friend job)
1039   "Set the job of FRIEND."
1040   (unless (mixi-friend-p friend)
1041     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
1042   (aset (cdr friend) 11 job))
1043
1044 (defun mixi-friend-set-organization (friend organization)
1045   "Set the organization of FRIEND."
1046   (unless (mixi-friend-p friend)
1047     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
1048   (aset (cdr friend) 12 organization))
1049
1050 (defun mixi-friend-set-profile (friend profile)
1051   "Set the profile of FRIEND."
1052   (unless (mixi-friend-p friend)
1053     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
1054   (aset (cdr friend) 13 profile))
1055
1056 (defmacro mixi-friend-list-page (&optional friend)
1057   `(concat "/list_friend.pl?page=%d"
1058            (when ,friend (concat "&id=" (mixi-friend-id ,friend)))))
1059
1060 (defconst mixi-friend-list-id-regexp
1061   "<a href=\"?show_friend\\.pl\\?id=\\([0-9]+\\)\"?")
1062 (defconst mixi-friend-list-nick-regexp
1063   "<span>\\(.+\\)¤µ¤ó([0-9]+)</span>")
1064
1065 ;;;###autoload
1066 (defun mixi-get-friends (&rest friend-or-range)
1067   "Get friends of FRIEND."
1068   (when (> (length friend-or-range) 2)
1069     (signal 'wrong-number-of-arguments (list 'mixi-get-friends
1070                                              (length friend-or-range))))
1071   (let ((friend (nth 0 friend-or-range))
1072         (range (nth 1 friend-or-range)))
1073     (when (or (not (mixi-friend-p friend)) (mixi-friend-p range))
1074       (setq friend (nth 1 friend-or-range))
1075       (setq range (nth 0 friend-or-range)))
1076     (unless (or (null friend) (mixi-friend-p friend))
1077       (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
1078     (let ((ids (mixi-get-matched-items (mixi-friend-list-page friend)
1079                                        mixi-friend-list-id-regexp
1080                                        range))
1081           (nicks (mixi-get-matched-items (mixi-friend-list-page friend)
1082                                          mixi-friend-list-nick-regexp
1083                                          range)))
1084       (let ((index 0)
1085             ret)
1086         (while (< index (length ids))
1087           (setq ret (cons (mixi-make-friend (nth 0 (nth index ids))
1088                                             (nth 0 (nth index nicks))) ret))
1089           (incf index))
1090         (reverse ret)))))
1091
1092 ;; Favorite.
1093 (defmacro mixi-favorite-list-page ()
1094   `(concat "/list_bookmark.pl?page=%d"))
1095
1096 (defconst mixi-favorite-list-regexp
1097   "<td bgcolor=\"#FDF9F2\"><font color=\"#996600\">̾Á°</font></td>
1098 <td colspan=\"2\" bgcolor=\"#FFFFFF\"><a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.*\\)</a></td>")
1099
1100 ;;;###autoload
1101 (defun mixi-get-favorites (&optional range)
1102   "Get favorites."
1103   (let ((items (mixi-get-matched-items (mixi-favorite-list-page)
1104                                        mixi-favorite-list-regexp
1105                                        range)))
1106     (mapcar (lambda (item)
1107               (mixi-make-friend (nth 0 item) (nth 1 item)))
1108             items)))
1109
1110 ;; Log object.
1111 (defun mixi-make-log (friend time)
1112   "Return a log object."
1113   (cons 'mixi-log (vector friend time)))
1114
1115 (defmacro mixi-log-p (log)
1116   `(eq (mixi-object-class ,log) 'mixi-log))
1117
1118 (defun mixi-log-friend (log)
1119   "Return the friend of LOG."
1120   (unless (mixi-log-p log)
1121     (signal 'wrong-type-argument (list 'mixi-log-p log)))
1122   (aref (cdr log) 0))
1123
1124 (defun mixi-log-time (log)
1125   "Return the time of LOG."
1126   (unless (mixi-log-p log)
1127     (signal 'wrong-type-argument (list 'mixi-log-p log)))
1128   (aref (cdr log) 1))
1129
1130 (defmacro mixi-log-list-page ()
1131   `(concat "/show_log.pl"))
1132
1133 (defconst mixi-log-list-regexp
1134   "\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü \\([0-9]+\\):\\([0-9]+\\) <a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.*?\\)</a>")
1135
1136 ;;;###autoload
1137 (defun mixi-get-logs (&optional range)
1138   "Get logs."
1139   (let ((items (mixi-get-matched-items (mixi-log-list-page)
1140                                        mixi-log-list-regexp
1141                                        range)))
1142     (mapcar (lambda (item)
1143               (mixi-make-log (mixi-make-friend (nth 5 item) (nth 6 item))
1144                              (encode-time 0
1145                                           (string-to-number (nth 4 item))
1146                                           (string-to-number (nth 3 item))
1147                                           (string-to-number (nth 2 item))
1148                                           (string-to-number (nth 1 item))
1149                                           (string-to-number (nth 0 item)))))
1150             items)))
1151
1152 ;; Recommended friend.
1153 (defmacro mixi-recommended-friend-list-page ()
1154   `(concat "http://indies.mixi.jp/recommend.pl"))
1155
1156 (defconst mixi-recommended-friend-list-regexp
1157   "<div class=\"iconListImage\"><a href=\"http://mixi\\.jp/show_friend\\.pl\\?id=\\([0-9]+\\)\" [^>]+>.+</a></div><span>\\(.+?\\)¤µ¤ó([0-9]+)</span>")
1158
1159 ;;;###autoload
1160 (defun mixi-get-recommended-friends (&optional range)
1161   "Get recommended friends."
1162   (let ((items (mixi-get-matched-items (mixi-recommended-friend-list-page)
1163                                        mixi-recommended-friend-list-regexp
1164                                        range)))
1165     (mapcar (lambda (item)
1166               (mixi-make-friend (nth 0 item) (nth 1 item)))
1167             items)))
1168
1169 ;; Diary object.
1170 (defvar mixi-diary-cache (make-hash-table :test 'equal))
1171 (defun mixi-make-diary (owner id &optional comment-count time title content)
1172   "Return a diary object."
1173   (let ((owner (or owner (mixi-make-me))))
1174     (mixi-make-cache (list (mixi-friend-id owner) id)
1175                      (cons 'mixi-diary (vector nil owner id comment-count time
1176                                                title content))
1177                      mixi-diary-cache)))
1178
1179 (defconst mixi-diary-url-regexp
1180   "/view_diary\\.pl\\?id=\\([0-9]+\\)&owner_id=\\([0-9]+\\)\\(&comment_count=\\([0-9]+\\)\\)?")
1181
1182 (defun mixi-make-diary-from-url (url)
1183   "Return a diary object from URL."
1184   (when (string-match mixi-diary-url-regexp url)
1185     (let ((id (match-string 1 url))
1186           (owner-id (match-string 2 url))
1187           (comment-count (match-string 4 url)))
1188       (mixi-make-diary (mixi-make-friend owner-id) id comment-count))))
1189
1190 (defmacro mixi-diary-p (diary)
1191   `(eq (mixi-object-class ,diary) 'mixi-diary))
1192
1193 (defmacro mixi-diary-page (diary)
1194   `(concat "/view_diary.pl?id=" (mixi-diary-id ,diary)
1195            "&owner_id=" (mixi-friend-id (mixi-diary-owner ,diary))))
1196
1197 (defconst mixi-diary-closed-regexp
1198   "<td>ͧ¿Í\\(¤Îͧ¿Í\\)?¤Þ¤Ç¸ø³«¤Î¤¿¤áÆɤळ¤È¤¬½ÐÍè¤Þ¤»¤ó¡£</td>")
1199 (defconst mixi-diary-owner-nick-regexp
1200   "<div class=\"diaryTitle\\(Friend\\)? clearfix\">
1201 <h2>\\(.+?\\)\\(¤µ¤ó\\)?¤ÎÆüµ­</h2>")
1202 (defconst mixi-diary-title-regexp
1203   "<div class=\"listDiaryTitle\">
1204 <dl class=\"clearfix\">
1205 <dt>\\([^<\n]+\\)\\(<span>\\)?")
1206 (defconst mixi-diary-time-regexp
1207   "<dd>\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü\\([0-9]+\\):\\([0-9]+\\)</dd>")
1208 (defconst mixi-diary-content-regexp
1209   "<div class=\"txtconfirmArea\">
1210 \\(\\(.\\|\r?\n\\)*?\\)
1211 <!--/viewDiaryBox--></div>")
1212
1213 (defun mixi-realize-diary (diary &optional page)
1214   "Realize a DIARY."
1215   ;; FIXME: Check a expiration of cache?
1216   (unless (mixi-object-realized-p diary)
1217     (with-mixi-retrieve (or page (mixi-diary-page diary))
1218       (let ((case-fold-search t))
1219         (unless (re-search-forward mixi-diary-closed-regexp nil t)
1220           (if (re-search-forward mixi-diary-owner-nick-regexp nil t)
1221               (mixi-friend-set-nick (mixi-diary-owner diary)
1222                                     (match-string 2))
1223             (mixi-realization-error 'cannot-find-owner-nick diary))
1224           (if (re-search-forward mixi-diary-title-regexp nil t)
1225               (mixi-diary-set-title diary (match-string 1))
1226             (mixi-realization-error 'cannot-find-title diary))
1227           (if (re-search-forward mixi-diary-time-regexp nil t)
1228               (mixi-diary-set-time
1229                diary (encode-time 0 (string-to-number (match-string 5))
1230                                   (string-to-number (match-string 4))
1231                                   (string-to-number (match-string 3))
1232                                   (string-to-number (match-string 2))
1233                                   (string-to-number (match-string 1))))
1234             (mixi-realization-error 'cannot-find-time diary))
1235           (if (re-search-forward mixi-diary-content-regexp nil t)
1236               (mixi-diary-set-content diary (match-string 1))
1237             (mixi-realization-error 'cannot-find-content diary)))))
1238     (mixi-object-touch diary)))
1239
1240 (defun mixi-diary-owner (diary)
1241   "Return the owner of DIARY."
1242   (unless (mixi-diary-p diary)
1243     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1244   (aref (cdr diary) 1))
1245
1246 (defun mixi-diary-id (diary)
1247   "Return the id of DIARY."
1248   (unless (mixi-diary-p diary)
1249     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1250   (aref (cdr diary) 2))
1251
1252 (defun mixi-diary-comment-count (diary)
1253   "Return the comment-count of DIARY."
1254   (unless (mixi-diary-p diary)
1255     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1256   (aref (cdr diary) 3))
1257
1258 (defun mixi-diary-time (diary)
1259   "Return the time of DIARY."
1260   (unless (mixi-diary-p diary)
1261     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1262   (unless (aref (cdr diary) 3)
1263     (mixi-realize-diary diary))
1264   (aref (cdr diary) 4))
1265
1266 (defun mixi-diary-title (diary)
1267   "Return the title of DIARY."
1268   (unless (mixi-diary-p diary)
1269     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1270   (unless (aref (cdr diary) 4)
1271     (mixi-realize-diary diary))
1272   (aref (cdr diary) 5))
1273
1274 (defun mixi-diary-content (diary)
1275   "Return the content of DIARY."
1276   (unless (mixi-diary-p diary)
1277     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1278   (mixi-realize-diary diary)
1279   (aref (cdr diary) 6))
1280
1281 (defun mixi-diary-set-comment-count (diary comment-count)
1282   "Set the comment-count of DIARY."
1283   (unless (mixi-diary-p diary)
1284     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1285   (aset (cdr diary) 3 comment-count))
1286
1287 (defun mixi-diary-set-time (diary time)
1288   "Set the time of DIARY."
1289   (unless (mixi-diary-p diary)
1290     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1291   (aset (cdr diary) 4 time))
1292
1293 (defun mixi-diary-set-title (diary title)
1294   "Set the title of DIARY."
1295   (unless (mixi-diary-p diary)
1296     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1297   (aset (cdr diary) 5 title))
1298
1299 (defun mixi-diary-set-content (diary content)
1300   "Set the content of DIARY."
1301   (unless (mixi-diary-p diary)
1302     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1303   (aset (cdr diary) 6 content))
1304
1305 (defmacro mixi-diary-list-page (&optional friend)
1306   `(concat "/list_diary.pl?page=%d"
1307            (when ,friend (concat "&id=" (mixi-friend-id ,friend)))))
1308
1309 (defconst mixi-diary-list-regexp
1310   "<dt>\\(<input name=\"diary_id\" type=\"checkbox\" value=\"[0-9]+\"  />\\|\\)<a href=\"view_diary\\.pl\\?id=\\([0-9]+\\)&owner_id=[0-9]+\">\\(.*\\)</a>\\(<span><a href=\"edit_diary\\.pl\\?id=[0-9]+\">ÊÔ½¸¤¹¤ë</a></span>\\|\\)</dt>
1311 <dd>\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü\n?\\([0-9]+\\):\\([0-9]+\\)</dd>
1312 </dl>")
1313
1314 ;;;###autoload
1315 (defun mixi-get-diaries (&rest friend-or-range)
1316   "Get diaries of FRIEND."
1317   (when (> (length friend-or-range) 2)
1318     (signal 'wrong-number-of-arguments
1319             (list 'mixi-get-diaries (length friend-or-range))))
1320   (let ((friend (nth 0 friend-or-range))
1321         (range (nth 1 friend-or-range)))
1322     (when (or (not (mixi-friend-p friend)) (mixi-friend-p range))
1323       (setq friend (nth 1 friend-or-range))
1324       (setq range (nth 0 friend-or-range)))
1325     (unless (or (null friend) (mixi-friend-p friend))
1326       (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
1327     (let ((items (mixi-get-matched-items (mixi-diary-list-page friend)
1328                                          mixi-diary-list-regexp
1329                                          range)))
1330       (mapcar (lambda (item)
1331                 (mixi-make-diary friend (nth 1 item) nil
1332                                  (encode-time
1333                                   0 (string-to-number (nth 8 item))
1334                                   (string-to-number (nth 7 item))
1335                                   (string-to-number (nth 6 item))
1336                                   (string-to-number (nth 5 item))
1337                                   (string-to-number (nth 4 item)))
1338                                   (nth 2 item)))
1339               items))))
1340
1341 (defmacro mixi-new-diary-list-page ()
1342   `(concat "/new_friend_diary.pl?page=%d"))
1343
1344 (defconst mixi-new-diary-list-regexp
1345   "<dt>\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü&nbsp;\\([0-9]+\\):\\([0-9]+\\)</dt>
1346 <dd><a href=\"view_diary\\.pl\\?id=\\([0-9]+\\)&owner_id=\\([0-9]+\\)\">\\(.+\\)</a> (\\(.*\\))<div ")
1347
1348 ;;;###autoload
1349 (defun mixi-get-new-diaries (&optional range)
1350   "Get new diaries."
1351   (let ((items (mixi-get-matched-items (mixi-new-diary-list-page)
1352                                        mixi-new-diary-list-regexp
1353                                        range)))
1354     (mapcar (lambda (item)
1355               (mixi-make-diary (mixi-make-friend (nth 6 item) (nth 8 item))
1356                                (nth 5 item)
1357                                nil
1358                                (encode-time
1359                                 0 (string-to-number (nth 4 item))
1360                                 (string-to-number (nth 3 item))
1361                                 (string-to-number (nth 2 item))
1362                                 (string-to-number (nth 1 item))
1363                                 (string-to-number (nth 0 item)))
1364                                (nth 7 item)))
1365             items)))
1366
1367 (defmacro mixi-search-diary-list-page (keyword)
1368   `(concat "/search_diary.pl?page=%d&submit=search&keyword="
1369              (mixi-url-encode-and-quote-percent-string ,keyword)))
1370
1371 (defconst mixi-search-diary-list-regexp
1372   "<td BGCOLOR=#FDF9F2><font COLOR=#996600>̾&nbsp;&nbsp;Á°</font></td>
1373 <td COLSPAN=2 BGCOLOR=#FFFFFF>\\(.*\\)
1374
1375 </td></tr>
1376
1377 <tr>
1378 <td BGCOLOR=#FDF9F2><font COLOR=#996600>¥¿¥¤¥È¥ë</font></td>
1379 <td COLSPAN=2 BGCOLOR=#FFFFFF>\\(.+\\)</td></tr>
1380
1381 <tr>
1382 <td BGCOLOR=#FDF9F2><font COLOR=#996600>ËÜ&nbsp;&nbsp;ʸ</font></td>
1383 <td COLSPAN=2 BGCOLOR=#FFFFFF width=\"380\">\\(.*\\)</td></tr>
1384
1385
1386 <tr>
1387 <td NOWRAP BGCOLOR=#FDF9F2 WIDTH=80><font COLOR=#996600>ºîÀ®Æü»þ</font></td>
1388 <td BGCOLOR=#FFFFFF WIDTH=220>\\([0-9]+\\)·î\\([0-9]+\\)Æü \\([0-9]+\\):\\([0-9]+\\)</td>
1389 <td ALIGN=center BGCOLOR=#FDF9F2 width=250><a href=\"view_diary\\.pl\\?id=\\([0-9]+\\)&owner_id=\\([0-9]+\\)\"><img src=http://img\\.mixi\\.jp/img/shbtn\\.gif ALT=¾ÜºÙ¤ò¸«¤ë BORDER=0 WIDTH=104 HEIGHT=19></a></td></tr>
1390 </table>
1391 </td></tr></table>")
1392
1393 ;;;###autoload
1394 (defun mixi-search-diaries (keyword &optional range)
1395   (let ((items (mixi-get-matched-items (mixi-search-diary-list-page keyword)
1396                                        mixi-search-diary-list-regexp
1397                                        range))
1398         (year (nth 5 (decode-time (current-time))))
1399         (month (nth 4 (decode-time (current-time)))))
1400     (mapcar (lambda (item)
1401                 (let ((month-of-item (string-to-number (nth 3 item))))
1402                   (when (> month-of-item month)
1403                     (decf year))
1404                   (setq month month-of-item)
1405                   (mixi-make-diary (mixi-make-friend (nth 8 item) (nth 0 item))
1406                                    (nth 7 item)
1407                                    nil
1408                                    (encode-time
1409                                     0 (string-to-number (nth 6 item))
1410                                     (string-to-number (nth 5 item))
1411                                     (string-to-number (nth 4 item))
1412                                     month year)
1413                                    (nth 1 item)
1414                                    (nth 2 item))))
1415             items)))
1416
1417 (defmacro mixi-post-diary-page ()
1418   `(concat "/add_diary.pl"))
1419
1420 (defconst mixi-post-key-regexp
1421   "<input type=\"?hidden\"? name=\"?post_key\"? +value=\"\\([a-z0-9]+\\)\"")
1422 (defconst mixi-post-succeed-regexp
1423   "<p\\(\\| class=\"messageAlert\"\\)>\\(ºîÀ®\\|½ñ¤­¹þ¤ß\\)¤¬´°Î»¤·¤Þ¤·¤¿¡£È¿±Ç¤Ë»þ´Ö¤¬¤«¤«¤ë¤³¤È¤¬¤¢¤ê¤Þ¤¹¤Î¤Ç¡¢É½¼¨¤µ¤ì¤Æ¤¤¤Ê¤¤¾ì¹ç¤Ï¾¯¡¹¤ªÂÔ¤Á¤¯¤À¤µ¤¤¡£</p>")
1424
1425 ;; FIXME: Support photos.
1426 ;;;###autoload
1427 (defun mixi-post-diary (title content)
1428   "Post a diary."
1429   (unless (stringp title)
1430     (signal 'wrong-type-argument (list 'stringp title)))
1431   (unless (stringp content)
1432     (signal 'wrong-type-argument (list 'stringp content)))
1433   (let ((fields `(("id" . ,(mixi-friend-id (mixi-make-me)))
1434                   ("diary_title" . ,title)
1435                   ("diary_body" . ,content)
1436                   ("submit" . "main")))
1437         post-key)
1438     (with-mixi-post-form (mixi-post-diary-page) fields
1439       (if (re-search-forward mixi-post-key-regexp nil t)
1440           (setq post-key (match-string 1))
1441         (mixi-post-error 'cannot-find-key)))
1442     (setq fields `(("post_key" . ,post-key)
1443                    ("id" . ,(mixi-friend-id (mixi-make-me)))
1444                    ("diary_title" . ,title)
1445                    ("diary_body" . ,content)
1446                    ("submit" . "confirm")))
1447     (with-mixi-post-form (mixi-post-diary-page) fields
1448       (unless (re-search-forward mixi-post-succeed-regexp nil t)
1449         (mixi-post-error 'cannot-find-succeed)))))
1450
1451 ;; Community object.
1452 (defvar mixi-community-cache (make-hash-table :test 'equal))
1453 (defun mixi-make-community (id &optional name birthday owner category members
1454                                open-level authority description)
1455   "Return a community object."
1456   (mixi-make-cache id (cons 'mixi-community (vector nil id name birthday owner
1457                                                     category members
1458                                                     open-level authority
1459                                                     description))
1460                    mixi-community-cache))
1461
1462 (defconst mixi-community-url-regexp
1463   "/view_community\\.pl\\?id=\\([0-9]+\\)")
1464
1465 (defun mixi-make-community-from-url (url)
1466   "Return a community object from URL."
1467   (when (string-match mixi-community-url-regexp url)
1468     (let ((id (match-string 1 url)))
1469       (mixi-make-community id))))
1470
1471 (defmacro mixi-community-p (community)
1472   `(eq (mixi-object-class ,community) 'mixi-community))
1473
1474 (defmacro mixi-community-page (community)
1475   `(concat "/view_community.pl?id=" (mixi-community-id ,community)))
1476
1477 ;; FIXME: Not community specific.
1478 (defconst mixi-community-nodata-regexp
1479   "^¥Ç¡¼¥¿¤¬¤¢¤ê¤Þ¤»¤ó")
1480 (defconst mixi-community-name-regexp
1481   "<div class=\"pageTitle communityTitle002 communityTop\">
1482 <h2>\\(.*\\)</h2>")
1483 (defconst mixi-community-birthday-regexp
1484   "<dt>³«ÀßÆü</dt>
1485 <dd>\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü<br />")
1486 ;; FIXME: Care when the owner has seceded.
1487 (defconst mixi-community-owner-regexp
1488   "<dt>´ÉÍý¿Í</dt>
1489 <dd>
1490 <a href=\"\\(home\\.pl\\|show_friend\\.pl\\?id=\\([0-9]+\\)\\)\">\\(.*\\)</a>")
1491 (defconst mixi-community-category-regexp
1492   "<dt>¥«¥Æ¥´¥ê</dt>
1493 <dd>\\(.+\\)</dd>")
1494 (defconst mixi-community-members-regexp
1495   "<dt>¥á¥ó¥Ð¡¼¿ô</dt>
1496 <dd>\\([0-9]+\\)¿Í</dd>")
1497 (defconst mixi-community-open-level-regexp
1498   "<dt>»²²Ã¾ò·ï¤È<br />¸ø³«¥ì¥Ù¥ë</dt>
1499 <dd>\\(.+\\)</dd>")
1500 (defconst mixi-community-authority-regexp
1501   "<dt>¥È¥Ô¥Ã¥¯¤Î<br />ºîÀ®¸¢¸Â</dt>
1502 <dd>
1503 \\(.+\\)
1504
1505
1506 </dd>")
1507 (defconst mixi-community-description-regexp
1508   "<div id=\"communityIntro\">
1509 <p>\\(.+\\)</p>")
1510
1511 (defun mixi-realize-community (community)
1512   "Realize a COMMUNITY."
1513   ;; FIXME: Check a expiration of cache?
1514   (unless (mixi-object-realized-p community)
1515     (with-mixi-retrieve (mixi-community-page community)
1516       (let ((case-fold-search t))
1517         (if (re-search-forward mixi-community-nodata-regexp nil t)
1518             ;; FIXME: Set all members?
1519             (mixi-community-set-name community "¥Ç¡¼¥¿¤¬¤¢¤ê¤Þ¤»¤ó")
1520           (if (re-search-forward mixi-community-name-regexp nil t)
1521               (mixi-community-set-name community (match-string 1))
1522             (mixi-realization-error 'cannot-find-name community))
1523           (if (re-search-forward mixi-community-birthday-regexp nil t)
1524               (mixi-community-set-birthday
1525                community (encode-time 0 0 0
1526                                       (string-to-number (match-string 3))
1527                                       (string-to-number (match-string 2))
1528                                       (string-to-number (match-string 1))))
1529             (mixi-realization-error 'cannot-find-birthday community))
1530           (if (re-search-forward mixi-community-owner-regexp nil t)
1531               (if (string= (match-string 1) "home.pl")
1532                   (mixi-community-set-owner community (mixi-make-me))
1533                 (mixi-community-set-owner community (mixi-make-friend
1534                                                      (match-string 2)
1535                                                      (match-string 3))))
1536             (mixi-realization-error 'cannot-find-owner community))
1537           (if (re-search-forward mixi-community-category-regexp nil t)
1538               (mixi-community-set-category community (match-string 1))
1539             (mixi-realization-error 'cannot-find-category community))
1540           (if (re-search-forward mixi-community-members-regexp nil t)
1541               (mixi-community-set-members community
1542                                           (string-to-number (match-string 1)))
1543             (mixi-realization-error 'cannot-find-members community))
1544           (if (re-search-forward mixi-community-open-level-regexp nil t)
1545               (mixi-community-set-open-level community (match-string 1))
1546             (mixi-realization-error 'cannot-find-open-level community))
1547           (if (re-search-forward mixi-community-authority-regexp nil t)
1548               (mixi-community-set-authority community (match-string 1))
1549             (mixi-realization-error 'cannot-find-authority community))
1550           (if (re-search-forward mixi-community-description-regexp nil t)
1551               (mixi-community-set-description community (match-string 1))
1552             (mixi-realization-error 'cannot-find-description community)))))
1553     (mixi-object-touch community)))
1554
1555 (defun mixi-community-id (community)
1556   "Return the id of COMMUNITY."
1557   (unless (mixi-community-p community)
1558     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1559   (aref (cdr community) 1))
1560
1561 (defun mixi-community-name (community)
1562   "Return the name of COMMUNITY."
1563   (unless (mixi-community-p community)
1564     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1565   (unless (aref (cdr community) 2)
1566     (mixi-realize-community community))
1567   (aref (cdr community) 2))
1568
1569 (defun mixi-community-birthday (community)
1570   "Return the birthday of COMMUNITY."
1571   (unless (mixi-community-p community)
1572     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1573   (mixi-realize-community community)
1574   (aref (cdr community) 3))
1575
1576 (defun mixi-community-owner (community)
1577   "Return the owner of COMMUNITY."
1578   (unless (mixi-community-p community)
1579     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1580   (mixi-realize-community community)
1581   (aref (cdr community) 4))
1582
1583 (defun mixi-community-category (community)
1584   "Return the category of COMMUNITY."
1585   (unless (mixi-community-p community)
1586     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1587   (mixi-realize-community community)
1588   (aref (cdr community) 5))
1589
1590 (defun mixi-community-members (community)
1591   "Return the members of COMMUNITY."
1592   (unless (mixi-community-p community)
1593     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1594   (mixi-realize-community community)
1595   (aref (cdr community) 6))
1596
1597 (defun mixi-community-open-level (community)
1598   "Return the open-level of COMMUNITY."
1599   (unless (mixi-community-p community)
1600     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1601   (mixi-realize-community community)
1602   (aref (cdr community) 7))
1603
1604 (defun mixi-community-authority (community)
1605   "Return the authority of COMMUNITY."
1606   (unless (mixi-community-p community)
1607     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1608   (mixi-realize-community community)
1609   (aref (cdr community) 8))
1610
1611 (defun mixi-community-description (community)
1612   "Return the description of COMMUNITY."
1613   (unless (mixi-community-p community)
1614     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1615   (mixi-realize-community community)
1616   (aref (cdr community) 9))
1617
1618 (defun mixi-community-set-name (community name)
1619   "Set the name of COMMUNITY."
1620   (unless (mixi-community-p community)
1621     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1622   (aset (cdr community) 2 name))
1623
1624 (defun mixi-community-set-birthday (community birthday)
1625   "Set the birthday of COMMUNITY."
1626   (unless (mixi-community-p community)
1627     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1628   (aset (cdr community) 3 birthday))
1629
1630 (defun mixi-community-set-owner (community owner)
1631   "Set the owner of COMMUNITY."
1632   (unless (mixi-community-p community)
1633     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1634   (unless (mixi-friend-p owner)
1635     (signal 'wrong-type-argument (list 'mixi-friend-p owner)))
1636   (aset (cdr community) 4 owner))
1637
1638 (defun mixi-community-set-category (community category)
1639   "Set the category of COMMUNITY."
1640   (unless (mixi-community-p community)
1641     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1642   (aset (cdr community) 5 category))
1643
1644 (defun mixi-community-set-members (community members)
1645   "Set the name of COMMUNITY."
1646   (unless (mixi-community-p community)
1647     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1648   (aset (cdr community) 6 members))
1649
1650 (defun mixi-community-set-open-level (community open-level)
1651   "Set the name of COMMUNITY."
1652   (unless (mixi-community-p community)
1653     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1654   (aset (cdr community) 7 open-level))
1655
1656 (defun mixi-community-set-authority (community authority)
1657   "Set the name of COMMUNITY."
1658   (unless (mixi-community-p community)
1659     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1660   (aset (cdr community) 8 authority))
1661
1662 (defun mixi-community-set-description (community description)
1663   "Set the name of COMMUNITY."
1664   (unless (mixi-community-p community)
1665     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1666   (aset (cdr community) 9 description))
1667
1668 (defmacro mixi-community-list-page (&optional friend)
1669   `(concat "/list_community.pl?page=%d"
1670            (when ,friend (concat "&id=" (mixi-friend-id ,friend)))))
1671
1672 (defconst mixi-community-list-id-regexp
1673   "<a href=\"?view_community\\.pl\\?id=\\([0-9]+\\)\"?")
1674 (defconst mixi-community-list-name-regexp
1675   "<span>\\(.+\\)([0-9]+)</span>")
1676
1677 ;;;###autoload
1678 (defun mixi-get-communities (&rest friend-or-range)
1679   "Get communities of FRIEND."
1680   (when (> (length friend-or-range) 2)
1681     (signal 'wrong-number-of-arguments
1682             (list 'mixi-get-communities (length friend-or-range))))
1683   (let ((friend (nth 0 friend-or-range))
1684         (range (nth 1 friend-or-range)))
1685     (when (or (not (mixi-friend-p friend)) (mixi-friend-p range))
1686       (setq friend (nth 1 friend-or-range))
1687       (setq range (nth 0 friend-or-range)))
1688     (unless (or (null friend) (mixi-friend-p friend))
1689       (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
1690     (let ((ids (mixi-get-matched-items (mixi-community-list-page friend)
1691                                        mixi-community-list-id-regexp
1692                                        range))
1693           (names (mixi-get-matched-items (mixi-community-list-page friend)
1694                                          mixi-community-list-name-regexp
1695                                          range)))
1696       (let ((index 0)
1697             ret)
1698         (while (< index (length ids))
1699           (setq ret (cons (mixi-make-community (nth 0 (nth index ids))
1700                                                (nth 0 (nth index names))) ret))
1701           (incf index))
1702         (reverse ret)))))
1703
1704 (defmacro mixi-search-community-list-page (keyword)
1705   `(concat "/search_community.pl?page=%d&&sort=date&type=com&submit=main"
1706            "&keyword=" (mixi-url-encode-and-quote-percent-string ,keyword)
1707            "&category_id=0"))
1708
1709 (defconst mixi-search-community-list-regexp
1710   "<dt class=\"communityTitle\"><a href=\"view_community\\.pl\\?id=\\([0-9]+\\)\">\\(.+\\) ([0-9]+)</a>")
1711
1712 ;; FIXME: Support category.
1713 ;;;###autoload
1714 (defun mixi-search-communities (keyword &optional range)
1715   (let ((items (mixi-get-matched-items (mixi-search-community-list-page
1716                                         keyword)
1717                                        mixi-search-community-list-regexp
1718                                        range)))
1719     (mapcar (lambda (item)
1720               (mixi-make-community (nth 0 item) (nth 1 item)))
1721             items)))
1722
1723 ;; Recommended community.
1724 (defalias 'mixi-recommended-community-list-page
1725   'mixi-recommended-friend-list-page)
1726
1727 (defconst mixi-recommended-community-list-regexp
1728   "<div class=\"iconListImage\"><a href=\"http://mixi\\.jp/view_community\\.pl\\?id=\\([0-9]+\\)\" [^>]+>.+</a></div><span>\\(.+\\)([0-9]+)</span>")
1729
1730 ;;;###autoload
1731 (defun mixi-get-recommended-communities (&optional range)
1732   "Get recommended communities."
1733   (let ((items (mixi-get-matched-items (mixi-recommended-community-list-page)
1734                                        mixi-recommended-community-list-regexp
1735                                        range)))
1736     (mapcar (lambda (item)
1737               (mixi-make-community (nth 0 item) (nth 1 item)))
1738             items)))
1739
1740 ;; Topic object.
1741 (defvar mixi-topic-cache (make-hash-table :test 'equal))
1742 (defun mixi-make-topic (community id &optional comment-count time title owner
1743                                   content)
1744   "Return a topic object."
1745   (mixi-make-cache (list (mixi-community-id community) id)
1746                    (cons 'mixi-topic (vector nil community id comment-count
1747                                              time title owner content))
1748                    mixi-topic-cache))
1749
1750 (defconst mixi-topic-url-regexp
1751   "/view_bbs\\.pl\\?id=\\([0-9]+\\)\\(&comment_count=\\([0-9]+\\)\\)?&comm_id=\\([0-9]+\\)")
1752
1753 (defun mixi-make-topic-from-url (url)
1754   "Return a topic object from URL."
1755   (when (string-match mixi-topic-url-regexp url)
1756     (let ((id (match-string 1 url))
1757           (comment-count (match-string 3 url))
1758           (community-id (match-string 4 url)))
1759       (mixi-make-topic (mixi-make-community community-id) id comment-count))))
1760
1761 (defmacro mixi-topic-p (topic)
1762   `(eq (mixi-object-class ,topic) 'mixi-topic))
1763
1764 (defmacro mixi-topic-page (topic)
1765   `(concat "/view_bbs.pl?id=" (mixi-topic-id ,topic)
1766            "&comm_id=" (mixi-community-id (mixi-topic-community ,topic))))
1767
1768 (defconst mixi-topic-community-regexp
1769   "<div class=\"pageTitle communityTitle002\">
1770 <h2>\\(.+\\) ¥È¥Ô¥Ã¥¯</h2>")
1771 (defconst mixi-topic-title-regexp
1772   "<span class=\"title\">\\([^<]+\\)</span>")
1773 (defconst mixi-topic-time-regexp
1774   "<span class=\"date\">\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü \\([0-9]+\\):\\([0-9]+\\)</span>")
1775 (defconst mixi-topic-owner-regexp
1776   "<dt><a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.*?\\)\\(¤µ¤ó\\)?</a>")
1777 (defconst mixi-topic-content-regexp
1778   "<dd>\\(\\(.\\|\r?\n\\)*?\\)</dd>")
1779
1780 (defun mixi-realize-topic (topic &optional page)
1781   "Realize a TOPIC."
1782   ;; FIXME: Check a expiration of cache?
1783   (unless (mixi-object-realized-p topic)
1784     (with-mixi-retrieve (or page (mixi-topic-page topic))
1785       (if (re-search-forward mixi-topic-community-regexp nil t)
1786           (mixi-community-set-name (mixi-topic-community topic)
1787                                    (match-string 1))
1788         (mixi-realization-error 'cannot-find-community topic))
1789       (if (re-search-forward mixi-topic-title-regexp nil t)
1790           (mixi-topic-set-title topic (match-string 1))
1791         (mixi-realization-error 'cannot-find-title topic))
1792       (if (re-search-forward mixi-topic-time-regexp nil t)
1793           (mixi-topic-set-time
1794            topic (encode-time 0 (string-to-number (match-string 5))
1795                               (string-to-number (match-string 4))
1796                               (string-to-number (match-string 3))
1797                               (string-to-number (match-string 2))
1798                               (string-to-number (match-string 1))))
1799         (mixi-realization-error 'cannot-find-time topic))
1800       (if (re-search-forward mixi-topic-owner-regexp nil t)
1801           (mixi-topic-set-owner topic (mixi-make-friend (match-string 1)
1802                                                         (match-string 2)))
1803         (mixi-realization-error 'cannot-find-owner topic))
1804       (if (re-search-forward mixi-topic-content-regexp nil t)
1805           (mixi-topic-set-content topic (match-string 1))
1806         (mixi-realization-error 'cannot-find-content topic)))
1807     (mixi-object-touch topic)))
1808
1809 (defun mixi-topic-community (topic)
1810   "Return the community of TOPIC."
1811   (unless (mixi-topic-p topic)
1812     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1813   (aref (cdr topic) 1))
1814
1815 (defun mixi-topic-id (topic)
1816   "Return the id of TOPIC."
1817   (unless (mixi-topic-p topic)
1818     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1819   (aref (cdr topic) 2))
1820
1821 (defun mixi-topic-comment-count (topic)
1822   "Return the comment-count of TOPIC."
1823   (unless (mixi-topic-p topic)
1824     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1825   (aref (cdr topic) 3))
1826
1827 (defun mixi-topic-time (topic)
1828   "Return the time of TOPIC."
1829   (unless (mixi-topic-p topic)
1830     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1831   (mixi-realize-topic topic)
1832   (aref (cdr topic) 4))
1833
1834 (defun mixi-topic-title (topic)
1835   "Return the title of TOPIC."
1836   (unless (mixi-topic-p topic)
1837     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1838   (mixi-realize-topic topic)
1839   (aref (cdr topic) 5))
1840
1841 (defun mixi-topic-owner (topic)
1842   "Return the owner of TOPIC."
1843   (unless (mixi-topic-p topic)
1844     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1845   (mixi-realize-topic topic)
1846   (aref (cdr topic) 6))
1847
1848 (defun mixi-topic-content (topic)
1849   "Return the content of TOPIC."
1850   (unless (mixi-topic-p topic)
1851     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1852   (mixi-realize-topic topic)
1853   (aref (cdr topic) 7))
1854
1855 (defun mixi-topic-set-comment-count (topic comment-count)
1856   "Set the comment-count of TOPIC."
1857   (unless (mixi-topic-p topic)
1858     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1859   (aset (cdr topic) 3 comment-count))
1860
1861 (defun mixi-topic-set-time (topic time)
1862   "Set the time of TOPIC."
1863   (unless (mixi-topic-p topic)
1864     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1865   (aset (cdr topic) 4 time))
1866
1867 (defun mixi-topic-set-title (topic title)
1868   "Set the title of TOPIC."
1869   (unless (mixi-topic-p topic)
1870     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1871   (aset (cdr topic) 5 title))
1872
1873 (defun mixi-topic-set-owner (topic owner)
1874   "Set the owner of TOPIC."
1875   (unless (mixi-topic-p topic)
1876     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1877   (unless (mixi-friend-p owner)
1878     (signal 'wrong-type-argument (list 'mixi-friend-p owner)))
1879   (aset (cdr topic) 6 owner))
1880
1881 (defun mixi-topic-set-content (topic content)
1882   "Set the content of TOPIC."
1883   (unless (mixi-topic-p topic)
1884     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1885   (aset (cdr topic) 7 content))
1886
1887 (defmacro mixi-post-topic-page (community)
1888   `(concat "/add_bbs.pl?id=" (mixi-community-id community)))
1889
1890 ;; FIXME: Support photos.
1891 ;;;###autoload
1892 (defun mixi-post-topic (community title content)
1893   "Post a topic to COMMUNITY."
1894   (unless (mixi-community-p community)
1895     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1896   (unless (stringp title)
1897     (signal 'wrong-type-argument (list 'stringp title)))
1898   (unless (stringp content)
1899     (signal 'wrong-type-argument (list 'stringp content)))
1900   (let ((fields `(("bbs_title" . ,title)
1901                   ("bbs_body" . ,content)
1902                   ("submit" . "main")))
1903         post-key)
1904     (with-mixi-post-form (mixi-post-topic-page community) fields
1905       (if (re-search-forward mixi-post-key-regexp nil t)
1906           (setq post-key (match-string 1))
1907         (mixi-post-error 'cannot-find-key community)))
1908     (setq fields `(("post_key" . ,post-key)
1909                    ("bbs_title" . ,title)
1910                    ("bbs_body" . ,content)
1911                    ("submit" . "confirm")))
1912     (with-mixi-post-form (mixi-post-topic-page community) fields
1913       (unless (re-search-forward mixi-post-succeed-regexp nil t)
1914         (mixi-post-error 'cannot-find-succeed community)))))
1915
1916 ;; Event object.
1917 (defvar mixi-event-cache (make-hash-table :test 'equal))
1918 (defun mixi-make-event (community id &optional comment-count time title owner
1919                                   date place detail limit members)
1920   "Return a event object."
1921   (mixi-make-cache (list (mixi-community-id community) id)
1922                    (cons 'mixi-event (vector nil community id comment-count
1923                                              time title owner date place
1924                                              detail limit members))
1925                    mixi-event-cache))
1926
1927 (defconst mixi-event-url-regexp
1928   "/view_event\\.pl\\?id=\\([0-9]+\\)\\(&comment_count=\\([0-9]+\\)\\)?&comm_id=\\([0-9]+\\)")
1929
1930 (defun mixi-make-event-from-url (url)
1931   "Return a event object from URL."
1932   (when (string-match mixi-event-url-regexp url)
1933     (let ((id (match-string 1 url))
1934           (comment-count (match-string 3 url))
1935           (community-id (match-string 4 url)))
1936       (mixi-make-event (mixi-make-community community-id) id comment-count))))
1937
1938 (defmacro mixi-event-p (event)
1939   `(eq (mixi-object-class ,event) 'mixi-event))
1940
1941 (defmacro mixi-event-page (event)
1942   `(concat "/view_event.pl?id=" (mixi-event-id ,event)
1943            "&comm_id=" (mixi-community-id (mixi-event-community ,event))))
1944
1945 (defconst mixi-event-community-regexp
1946   "<div class=\"pageTitle communityTitle002\">
1947 <h2>\\(.+\\) ¥¤¥Ù¥ó¥È</h2>")
1948 (defconst mixi-event-title-regexp
1949   "<span class=\"title\">\\([^<]+\\)</span>")
1950 (defconst mixi-event-time-regexp
1951   "<span class=\"date\">\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü \\([0-9]+\\):\\([0-9]+\\)</span>")
1952 (defconst mixi-event-date-regexp
1953   "<dt>³«ºÅÆü»þ</dt>
1954 <dd>\\(.+\\)</dd>")
1955 (defconst mixi-event-place-regexp
1956   "<dt>³«ºÅ¾ì½ê</dt>
1957 <dd>\\(.+\\)</dd>")
1958 (defconst mixi-event-owner-regexp
1959   "<dt>\\((mixi Âà²ñºÑ)\\|<a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.*\\)</a>\\)</dt>")
1960 (defconst mixi-event-owner-seceded-regexp
1961   "(mixi Âà²ñºÑ)")
1962 (defconst mixi-event-detail-regexp
1963   "<dd>\\(\\(.\\|\r?\n\\)*?\\)</dd>
1964 </dl>")
1965 (defconst mixi-event-limit-regexp
1966   "<dt>Ê罸´ü¸Â</dt>
1967 <dd>\\(.+\\)</dd>")
1968 (defconst mixi-event-members-regexp
1969   "<dt>»²²Ã¼Ô</dt>
1970 <dd>\\(.+\\)</dd>")
1971
1972 (defun mixi-realize-event (event &optional page)
1973   "Realize a EVENT."
1974   ;; FIXME: Check a expiration of cache?
1975   (unless (mixi-object-realized-p event)
1976     (with-mixi-retrieve (or page (mixi-event-page event))
1977       (let ((case-fold-search t))
1978         (if (re-search-forward mixi-event-community-regexp nil t)
1979             (mixi-community-set-name (mixi-event-community event)
1980                                      (match-string 1))
1981           (mixi-realization-error 'cannot-find-community event))
1982         (if (re-search-forward mixi-event-title-regexp nil t)
1983             (mixi-event-set-title event (match-string 1))
1984           (mixi-realization-error 'cannot-find-title event))
1985         (if (re-search-forward mixi-event-time-regexp nil t)
1986             (mixi-event-set-time
1987              event (encode-time 0 (string-to-number (match-string 5))
1988                                 (string-to-number (match-string 4))
1989                                 (string-to-number (match-string 3))
1990                                 (string-to-number (match-string 2))
1991                                 (string-to-number (match-string 1))))
1992           (mixi-realization-error 'cannot-find-time event))
1993         (if (re-search-forward mixi-event-date-regexp nil t)
1994             (mixi-event-set-date event (match-string 1))
1995           (mixi-realization-error 'cannot-find-date event))
1996         (if (re-search-forward mixi-event-place-regexp nil t)
1997             (mixi-event-set-place event (match-string 1))
1998           (mixi-realization-error 'cannot-find-place event))
1999         (if (re-search-forward mixi-event-owner-regexp nil t)
2000             (let ((seceded-or-not (match-string 1))
2001                   (id (match-string 2))
2002                   (nick (match-string 3)))
2003               (if (string= mixi-event-owner-seceded-regexp seceded-or-not)
2004                   (mixi-event-set-owner event
2005                                         (mixi-make-friend nil seceded-or-not))
2006                 (mixi-event-set-owner event (mixi-make-friend id nick))))
2007           (mixi-realization-error 'cannot-find-owner event))
2008         (if (re-search-forward mixi-event-detail-regexp nil t)
2009             (mixi-event-set-detail event (match-string 1))
2010           (mixi-realization-error 'cannot-find-detail event))
2011         (if (re-search-forward mixi-event-limit-regexp nil t)
2012           (mixi-event-set-limit event (match-string 1))
2013           (mixi-realization-error 'cannot-find-limit event))
2014         (if (re-search-forward mixi-event-members-regexp nil t)
2015             (mixi-event-set-members event (match-string 1))
2016           (mixi-realization-error 'cannot-find-members event))))
2017     (mixi-object-touch event)))
2018
2019 (defun mixi-event-community (event)
2020   "Return the community of EVENT."
2021   (unless (mixi-event-p event)
2022     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2023   (aref (cdr event) 1))
2024
2025 (defun mixi-event-id (event)
2026   "Return the id of EVENT."
2027   (unless (mixi-event-p event)
2028     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2029   (aref (cdr event) 2))
2030
2031 (defun mixi-event-comment-count (event)
2032   "Return the comment-count of EVENT."
2033   (unless (mixi-event-p event)
2034     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2035   (aref (cdr event) 3))
2036
2037 (defun mixi-event-time (event)
2038   "Return the time of EVENT."
2039   (unless (mixi-event-p event)
2040     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2041   (mixi-realize-event event)
2042   (aref (cdr event) 4))
2043
2044 (defun mixi-event-title (event)
2045   "Return the title of EVENT."
2046   (unless (mixi-event-p event)
2047     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2048   (mixi-realize-event event)
2049   (aref (cdr event) 5))
2050
2051 (defun mixi-event-owner (event)
2052   "Return the owner of EVENT."
2053   (unless (mixi-event-p event)
2054     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2055   (mixi-realize-event event)
2056   (aref (cdr event) 6))
2057
2058 (defun mixi-event-date (event)
2059   "Return the date of EVENT."
2060   (unless (mixi-event-p event)
2061     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2062   (mixi-realize-event event)
2063   (aref (cdr event) 7))
2064
2065 (defun mixi-event-place (event)
2066   "Return the place of EVENT."
2067   (unless (mixi-event-p event)
2068     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2069   (mixi-realize-event event)
2070   (aref (cdr event) 8))
2071
2072 (defun mixi-event-detail (event)
2073   "Return the detail of EVENT."
2074   (unless (mixi-event-p event)
2075     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2076   (mixi-realize-event event)
2077   (aref (cdr event) 9))
2078
2079 (defun mixi-event-limit (event)
2080   "Return the limit of EVENT."
2081   (unless (mixi-event-p event)
2082     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2083   (mixi-realize-event event)
2084   (aref (cdr event) 10))
2085
2086 (defun mixi-event-members (event)
2087   "Return the members of EVENT."
2088   (unless (mixi-event-p event)
2089     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2090   (mixi-realize-event event)
2091   (aref (cdr event) 11))
2092
2093 (defun mixi-event-set-comment-count (event comment-count)
2094   "Set the comment-count of EVENT."
2095   (unless (mixi-event-p event)
2096     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2097   (aset (cdr event) 3 comment-count))
2098
2099 (defun mixi-event-set-time (event time)
2100   "Set the time of EVENT."
2101   (unless (mixi-event-p event)
2102     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2103   (aset (cdr event) 4 time))
2104
2105 (defun mixi-event-set-title (event title)
2106   "Set the title of EVENT."
2107   (unless (mixi-event-p event)
2108     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2109   (aset (cdr event) 5 title))
2110
2111 (defun mixi-event-set-owner (event owner)
2112   "Set the owner of EVENT."
2113   (unless (mixi-event-p event)
2114     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2115   (unless (mixi-friend-p owner)
2116     (signal 'wrong-type-argument (list 'mixi-friend-p owner)))
2117   (aset (cdr event) 6 owner))
2118
2119 (defun mixi-event-set-date (event date)
2120   "Set the date of EVENT."
2121   (unless (mixi-event-p event)
2122     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2123   (aset (cdr event) 7 date))
2124
2125 (defun mixi-event-set-place (event place)
2126   "Set the place of EVENT."
2127   (unless (mixi-event-p event)
2128     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2129   (aset (cdr event) 8 place))
2130
2131 (defun mixi-event-set-detail (event detail)
2132   "Set the detail of EVENT."
2133   (unless (mixi-event-p event)
2134     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2135   (aset (cdr event) 9 detail))
2136
2137 (defun mixi-event-set-limit (event limit)
2138   "Set the limit of EVENT."
2139   (unless (mixi-event-p event)
2140     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2141   (aset (cdr event) 10 limit))
2142
2143 (defun mixi-event-set-members (event members)
2144   "Set the members of EVENT."
2145   (unless (mixi-event-p event)
2146     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2147   (aset (cdr event) 11 members))
2148
2149 ;; BBS object.
2150 (defconst mixi-bbs-list '(mixi-topic mixi-event))
2151
2152 (defmacro mixi-bbs-p (bbs)
2153   `(memq (mixi-object-class ,bbs) mixi-bbs-list))
2154
2155 (defun mixi-bbs-community (bbs)
2156   "Return the community of BBS."
2157   (unless (mixi-bbs-p bbs)
2158     (signal 'wrong-type-argument (list 'mixi-bbs-p bbs)))
2159   (let ((func (intern (concat mixi-object-prefix
2160                               (mixi-object-name bbs) "-community"))))
2161     (funcall func bbs)))
2162
2163 (defun mixi-bbs-comment-count (bbs)
2164   "Return the comment-count of BBS."
2165   (unless (mixi-bbs-p bbs)
2166     (signal 'wrong-type-argument (list 'mixi-bbs-p bbs)))
2167   (let ((func (intern (concat mixi-object-prefix
2168                               (mixi-object-name bbs) "-comment-count"))))
2169     (funcall func bbs)))
2170
2171 (defun mixi-bbs-set-comment-count (bbs count)
2172   "Set the comment-count of BBS."
2173   (unless (mixi-bbs-p bbs)
2174     (signal 'wrong-type-argument (list 'mixi-bbs-p bbs)))
2175   (let ((func (intern (concat mixi-object-prefix
2176                               (mixi-object-name bbs) "-set-comment-count"))))
2177     (funcall func bbs count)))
2178
2179 (defalias 'mixi-bbs-id 'mixi-object-id)
2180 (defalias 'mixi-bbs-time 'mixi-object-time)
2181 (defalias 'mixi-bbs-title 'mixi-object-title)
2182 (defalias 'mixi-bbs-owner 'mixi-object-owner)
2183 (defalias 'mixi-bbs-content 'mixi-object-content)
2184
2185 (defmacro mixi-bbs-list-page (community)
2186   `(concat "/list_bbs.pl?page=%d"
2187            "&id=" (mixi-community-id ,community)))
2188
2189 (defconst mixi-bbs-list-regexp
2190   "<a href=\"?view_\\(bbs\\|event\\)\\.pl\\?id=\\([0-9]+\\)")
2191
2192 ;;;###autoload
2193 (defun mixi-get-bbses (community &optional range)
2194   "Get bbese of COMMUNITY."
2195   (unless (mixi-community-p community)
2196     (signal 'wrong-type-argument (list 'mixi-community-p community)))
2197   (let ((items (mixi-get-matched-items (mixi-bbs-list-page community)
2198                                        mixi-bbs-list-regexp
2199                                        range)))
2200     (mapcar (lambda (item)
2201               (let ((name (nth 0 item)))
2202                 (when (string= name "bbs")
2203                   (setq name "topic"))
2204                 (let ((func (intern (concat "mixi-make-" name))))
2205                   (funcall func community (nth 1 item)))))
2206             items)))
2207
2208 (defmacro mixi-new-bbs-list-page ()
2209   `(concat "/new_bbs.pl?page=%d"))
2210
2211 (defconst mixi-new-bbs-list-regexp
2212   "<dd><a href=\"view_\\(bbs\\|event\\)\\.pl\\?id=\\([0-9]+\\)&comment_count=\\([0-9]+\\)&comm_id=\\([0-9]+\\)\">")
2213
2214 ;;;###autoload
2215 (defun mixi-get-new-bbses (&optional range)
2216   "Get new topics."
2217   (let ((items (mixi-get-matched-items (mixi-new-bbs-list-page)
2218                                        mixi-new-bbs-list-regexp
2219                                        range)))
2220     (delq nil
2221           (mapcar (lambda (item)
2222                     (let ((name (nth 0 item)))
2223                       (when (string= name "bbs")
2224                         (setq name "topic"))
2225                       (let* ((func (intern (concat "mixi-make-" name)))
2226                              (bbs (funcall func
2227                                            (mixi-make-community (nth 3 item))
2228                                            (nth 1 item)))
2229                              (comment-count (mixi-bbs-comment-count bbs))
2230                              (count (string-to-number (nth 2 item))))
2231                         (when (or (null comment-count)
2232                                   (< comment-count count))
2233                           (mixi-bbs-set-comment-count bbs count)
2234                           bbs))))
2235                   items))))
2236  
2237 (defmacro mixi-search-bbs-list-page (keyword)
2238   `(concat "/search_topic.pl?page=%d&type=top&submit=search"
2239            "&keyword=" (mixi-url-encode-and-quote-percent-string ,keyword)
2240            "&community_id=0&category_id=0"))
2241
2242 (defconst mixi-search-bbs-list-regexp
2243   "<a href=\"view_\\(bbs\\|event\\)\\.pl\\?id=\\([0-9]+\\)&comm_id=\\([0-9]+\\)\" class=\"title\">")
2244
2245 ;; FIXME: Support community and category.
2246 ;;;###autoload
2247 (defun mixi-search-bbses (keyword &optional range)
2248   (let ((items (mixi-get-matched-items (mixi-search-bbs-list-page keyword)
2249                                        mixi-search-bbs-list-regexp
2250                                        range)))
2251     (mapcar (lambda (item)
2252               (let ((name (nth 0 item)))
2253                 (when (string= name "bbs")
2254                   (setq name "topic"))
2255                 (let ((func (intern (concat "mixi-make-" name))))
2256                   (funcall func (mixi-make-community (nth 2 item))
2257                            (nth 1 item)))))
2258             items)))
2259
2260 ;; Parent object.
2261 (defmacro mixi-parent-p (object)
2262   `(or (eq (mixi-object-class ,object) 'mixi-diary)
2263        (mixi-bbs-p ,object)))
2264
2265 (defun mixi-realize-parent (parent &optional page)
2266   "Realize a PARENT."
2267   (unless (mixi-parent-p parent)
2268     (signal 'wrong-type-argument (list 'mixi-parent-p parent)))
2269   (let ((func (intern (concat mixi-object-prefix "realize-"
2270                               (mixi-object-name parent)))))
2271     (funcall func parent page)))
2272
2273 ;; Comment object.
2274 (defun mixi-make-comment (parent owner time content &optional count)
2275   "Return a comment object."
2276   (cons 'mixi-comment (vector parent owner time content count)))
2277
2278 (defmacro mixi-comment-p (comment)
2279   `(eq (mixi-object-class ,comment) 'mixi-comment))
2280
2281 (defun mixi-comment-parent (comment)
2282   "Return the parent of COMMENT."
2283   (unless (mixi-comment-p comment)
2284     (signal 'wrong-type-argument (list 'mixi-comment-p comment)))
2285   (aref (cdr comment) 0))
2286
2287 (defun mixi-comment-owner (comment)
2288   "Return the owner of COMMENT."
2289   (unless (mixi-comment-p comment)
2290     (signal 'wrong-type-argument (list 'mixi-comment-p comment)))
2291   (aref (cdr comment) 1))
2292
2293 (defun mixi-comment-time (comment)
2294   "Return the time of COMMENT."
2295   (unless (mixi-comment-p comment)
2296     (signal 'wrong-type-argument (list 'mixi-comment-p comment)))
2297   (aref (cdr comment) 2))
2298
2299 (defun mixi-comment-content (comment)
2300   "Return the content of COMMENT."
2301   (unless (mixi-comment-p comment)
2302     (signal 'wrong-type-argument (list 'mixi-comment-p comment)))
2303   (aref (cdr comment) 3))
2304
2305 (defun mixi-comment-count (comment)
2306   "Return the count of COMMENT."
2307   (unless (mixi-comment-p comment)
2308     (signal 'wrong-type-argument (list 'mixi-comment-p comment)))
2309   (aref (cdr comment) 4))
2310
2311 (defun mixi-diary-comment-list-page (diary)
2312   (concat "/view_diary.pl?full=1"
2313           "&id=" (mixi-diary-id diary)
2314           "&owner_id=" (mixi-friend-id (mixi-diary-owner diary))))
2315
2316 ;; FIXME: Split regexp to time, owner(id and nick) and contents.
2317 (defconst mixi-diary-comment-list-regexp
2318   "<span class=\"commentTitleName\">\\(<input type=\"checkbox\" name=\"comment_id\" value=\".+\" />
2319 \\|\\)<a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.*\\)</a>
2320 \\(
2321 | <a href=\"delete_comment\\.pl\\?diary_id=[0-9]+&owner_id=[0-9]+&comment_id=.+&type=comment\">¼«Ê¬¤Î¥³¥á¥ó¥È¤òºï½ü¤¹¤ë</a>
2322 \\|\\)</span>
2323
2324 <span class=\"commentTitleDate\">\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü&nbsp;\\([0-9]+\\):\\([0-9]+\\)</span>
2325 </dt>
2326 +
2327 <dd>
2328 \\(\\(.\\|\r?\n\\)*?\\)
2329 </dd>
2330 </dl>
2331 </div>")
2332
2333 (defun mixi-topic-comment-list-page (topic)
2334   (concat "/view_bbs.pl?page=all"
2335           "&id=" (mixi-topic-id topic)
2336           "&comm_id=" (mixi-community-id (mixi-topic-community topic))))
2337
2338 ;; FIXME: Split regexp to time, owner(id and nick) and contents.
2339 (defconst mixi-topic-comment-list-regexp
2340   "<dt class=\"commentDate clearfix\"><span class=\"senderId\">\\(<input id=\"commentCheck01\" name=\"comment_id\" type=\"checkbox\" value=\"[0-9]+\" /><label for=\"commentCheck01\">\\|\\)\\([0-9]+\\)\\(<span class=\"deleteTextArea\"><a href=\"delete_bbs_comment\\.pl\\?id=[0-9]+&comm_id=[0-9]+&comment_id=[0-9]+\">¼«Ê¬¤Î¥³¥á¥ó¥È¤òºï½ü¤¹¤ë</a></span>\\|</label>\\|\\)</span>
2341 <span class=\"date\">\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü \\([0-9]+\\):\\([0-9]+\\)</span></dt>
2342 <dd>
2343 <dl class=\"commentContent01\">
2344 <dt><a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.*\\)</a></dt>
2345 <dd>
2346 \\(\\(.\\|\r?\n\\)*?\\)
2347 </dd>")
2348
2349 (defun mixi-event-comment-list-page (event)
2350   (concat "/view_event.pl?page=all"
2351           "&id=" (mixi-event-id event)
2352           "&comm_id=" (mixi-community-id (mixi-event-community event))))
2353
2354 ;; FIXME: Split regexp to time, owner(id and nick) and contents.
2355 (defconst mixi-event-comment-list-regexp
2356   "<dt class=\"commentDate clearfix\"><span class=\"senderId\">\\(<input id=\"commentCheck01\" name=\"comment_id\" type=\"checkbox\" value=\"[0-9]+\" /><label for=\"commentCheck01\">\\|\\)\\([0-9]+\\)\\(<span class=\"deleteTextArea\"><a href=\"delete_bbs_comment\\.pl\\?id=[0-9]+&comm_id=[0-9]+&comment_id=[0-9]+\">¼«Ê¬¤Î¥³¥á¥ó¥È¤òºï½ü¤¹¤ë</a></span>\\|</label>\\|\\)</span>
2357 <span class=\"date\">\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü \\([0-9]+\\):\\([0-9]+\\)</span></dt>
2358 <dd>
2359 <dl class=\"commentContent01\">
2360 <dt><a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.*\\)</a></dt>
2361 <dd>
2362 \\(\\(.\\|\r?\n\\)*?\\)
2363 </dd>")
2364
2365 ;;;###autoload
2366 (defun mixi-get-comments (parent &optional range)
2367   "Get comments of PARENT."
2368   (unless (mixi-parent-p parent)
2369     (signal 'wrong-type-argument (list 'mixi-parent-p parent)))
2370   (let* ((name (mixi-object-name parent))
2371          (list-page (intern (concat mixi-object-prefix name
2372                                     "-comment-list-page")))
2373          (regexp (eval (intern (concat mixi-object-prefix name
2374                                        "-comment-list-regexp"))))
2375          (page (funcall list-page parent)))
2376     (unless (mixi-object-realized-p parent)
2377       (mixi-realize-parent parent page)
2378       (setq page nil))
2379     (let ((items (mixi-get-matched-items page regexp range t)))
2380       (mapcar (lambda (item)
2381                 (let (owner-id owner-nick year month day hour minute content
2382                                count)
2383                   (if (eq (mixi-object-class parent) 'mixi-diary)
2384                       (progn
2385                         (setq owner-id (nth 1 item))
2386                         (setq owner-nick (nth 2 item))
2387                         (setq year (nth 4 item))
2388                         (setq month (nth 5 item))
2389                         (setq day (nth 6 item))
2390                         (setq hour (nth 7 item))
2391                         (setq minute (nth 8 item))
2392                         (setq content (nth 9 item)))
2393                     (setq owner-id (nth 8 item))
2394                     (setq owner-nick (nth 9 item))
2395                     (setq year (nth 3 item))
2396                     (setq month (nth 4 item))
2397                     (setq day (nth 5 item))
2398                     (setq hour (nth 6 item))
2399                     (setq minute (nth 7 item))
2400                     (setq content (nth 10 item))
2401                     (setq count (nth 1 item)))
2402                   (mixi-make-comment parent (mixi-make-friend owner-id
2403                                                               owner-nick)
2404                                      (encode-time
2405                                       0
2406                                       (string-to-number minute)
2407                                       (string-to-number hour)
2408                                       (string-to-number day)
2409                                       (string-to-number month)
2410                                       (string-to-number year))
2411                                      content count)))
2412               items))))
2413
2414 (defmacro mixi-new-comment-list-page ()
2415   `(concat "/new_comment.pl?page=%d"))
2416
2417 (defconst mixi-new-comment-list-regexp
2418   "<a href=\"view_diary\\.pl\\?id=\\([0-9]+\\)&owner_id=\\([0-9]+\\)&comment_count=\\([0-9]+\\)\">")
2419
2420 ;;;###autoload
2421 (defun mixi-get-new-comments (&optional range)
2422   "Get new comments."
2423   (let ((items (mixi-get-matched-items (mixi-new-comment-list-page)
2424                                        mixi-new-comment-list-regexp
2425                                        range)))
2426     (delq nil
2427           (mapcar (lambda (item)
2428                     (let* ((diary (mixi-make-diary
2429                                    (mixi-make-friend (nth 1 item))
2430                                    (nth 0 item)))
2431                            (comment-count (mixi-diary-comment-count diary))
2432                            (count (string-to-number (nth 2 item))))
2433                       (when (or (null comment-count)
2434                                 (< comment-count count))
2435                         (mixi-diary-set-comment-count diary count)
2436                         diary)))
2437                   items))))
2438
2439 (defmacro mixi-new-bbs-comment-list-page ()
2440   `(concat "/new_bbs_comment.pl?page=%d"))
2441
2442 (defconst mixi-new-bbs-comment-list-regexp
2443   "<a href=\"?view_\\(bbs\\|event\\)\\.pl\\?id=\\([0-9]+\\)&comment_count=\\([0-9]+\\)&comm_id=\\([0-9]+\\)\"?>")
2444
2445 ;;;###autoload
2446 (defun mixi-get-new-bbs-comments (&optional range)
2447   "Get new BBS comments."
2448   (let ((items (mixi-get-matched-items (mixi-new-bbs-comment-list-page)
2449                                        mixi-new-bbs-comment-list-regexp
2450                                        range)))
2451     (delq nil
2452           (mapcar (lambda (item)
2453                     (let ((name (nth 0 item)))
2454                       (when (string= name "bbs")
2455                         (setq name "topic"))
2456                       (let ((make-func (intern (concat "mixi-make-" name)))
2457                             (comment-count-func
2458                              (intern (concat "mixi-" name "-comment-count")))
2459                             (set-comment-count-func
2460                              (intern (concat "mixi-" name
2461                                              "-set-comment-count"))))
2462                         (let* ((bbs (funcall make-func
2463                                      (mixi-make-community (nth 3 item))
2464                                      (nth 1 item)))
2465                                (comment-count (funcall comment-count-func bbs))
2466                                (count (string-to-number (nth 2 item))))
2467                           (when (or (null comment-count)
2468                                     (< comment-count count))
2469                             (funcall set-comment-count-func bbs count)
2470                             bbs)))))
2471                   items))))
2472
2473 (defun mixi-post-diary-comment-page (diary)
2474   (concat "/add_comment.pl?&diary_id=" (mixi-diary-id diary)))
2475
2476 (defun mixi-post-topic-comment-page (topic)
2477   (concat "/add_bbs_comment.pl?id=" (mixi-topic-id topic)
2478           "&comm_id=" (mixi-community-id (mixi-topic-community topic))))
2479
2480 (defun mixi-post-event-comment-page (event)
2481   (concat "/add_event_comment.pl?id=" (mixi-event-id event)
2482           "&comm_id=" (mixi-community-id (mixi-event-community event))))
2483
2484 ;; FIXME: Support photos.
2485 ;;;###autoload
2486 (defun mixi-post-comment (parent content)
2487   "Post a comment to PARENT."
2488   (unless (mixi-object-p parent)
2489     (signal 'wrong-type-argument (list 'mixi-object-p parent)))
2490   (unless (stringp content)
2491     (signal 'wrong-type-argument (list 'stringp content)))
2492   (let* ((name (mixi-object-name parent))
2493          (page (intern (concat mixi-object-prefix "post-" name
2494                                "-comment-page")))
2495          fields post-key)
2496     (if (mixi-diary-p parent)
2497         (setq fields
2498               `(("owner_id" . ,(mixi-friend-id (mixi-diary-owner parent)))
2499                 ("comment_body" . ,content)))
2500       (setq fields `(("comment" . ,content))))
2501     (with-mixi-post-form (funcall page parent) fields
2502       (if (re-search-forward mixi-post-key-regexp nil t)
2503           (setq post-key (match-string 1))
2504         (mixi-post-error 'cannot-find-key parent)))
2505     (if (mixi-diary-p parent)
2506         (setq fields
2507               `(("post_key" . ,post-key)
2508                 ("owner_id" . ,(mixi-friend-id (mixi-diary-owner parent)))
2509                 ("comment_body" . ,content)
2510                 ("submit" . "confirm")))
2511       (setq fields `(("post_key" . ,post-key)
2512                      ("comment" . ,content)
2513                      ("submit" . "confirm"))))
2514     (with-mixi-post-form (funcall page parent) fields
2515       (unless (re-search-forward mixi-post-succeed-regexp nil t)
2516         (mixi-post-error 'cannot-find-succeed parent)))))
2517
2518 ;; Message object.
2519 (defconst mixi-message-box-list '(inbox outbox savebox thrash)) ; thrash?
2520
2521 (defmacro mixi-message-box-p (box)
2522   `(memq ,box mixi-message-box-list))
2523
2524 (defun mixi-message-box-name (box)
2525   "Return the name of BOX."
2526   (unless (mixi-message-box-p box)
2527     (signal 'wrong-type-argument (list 'mixi-message-box-p box)))
2528   (symbol-name box))
2529
2530 (defvar mixi-message-cache (make-hash-table :test 'equal))
2531 (defun mixi-make-message (id box &optional owner title time content)
2532   "Return a message object."
2533   (mixi-make-cache (list id box)
2534                    (cons 'mixi-message (vector nil id box owner title time
2535                                                content))
2536                    mixi-message-cache))
2537
2538 (defconst mixi-message-url-regexp
2539   "/view_message\\.pl\\?id=\\([a-z0-9]+\\)&box=\\([a-z]+\\)")
2540
2541 (defun mixi-make-message-from-url (url)
2542   "Return a message object from URL."
2543   (when (string-match mixi-message-url-regexp url)
2544     (let ((id (match-string 1 url))
2545           (box (match-string 2 url)))
2546       (mixi-make-message id box))))
2547
2548 (defmacro mixi-message-p (message)
2549   `(eq (mixi-object-class ,message) 'mixi-message))
2550
2551 (defmacro mixi-message-page (message)
2552   `(concat "/view_message.pl?id=" (mixi-message-id ,message)
2553            "&box=" (mixi-message-box ,message)))
2554
2555 (defconst mixi-message-owner-regexp
2556   "<font COLOR=#996600>\\(º¹½Ð¿Í\\|°¸&nbsp;Àè\\)</font>&nbsp;:&nbsp;<a HREF=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.*\\)\\(</a>\\|</td>\\)")
2557 (defconst mixi-message-time-regexp
2558 "<font COLOR=#996600>Æü\\(¡¡\\|&nbsp;\\)ÉÕ</font>&nbsp;:&nbsp;\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü \\([0-9]+\\)»þ\\([0-9]+\\)ʬ&nbsp;&nbsp;")
2559 (defconst mixi-message-title-regexp
2560 "<font COLOR=#996600>·ï\\(¡¡\\|&nbsp;\\)̾</font>&nbsp;:&nbsp;\\(.*\\)\n?</td>")
2561 (defconst mixi-message-content-regexp
2562   "<tr><td CLASS=h120 width=\"500\">\\(.*\\)</td></tr>")
2563
2564 (defun mixi-realize-message (message)
2565   "Realize a MESSAGE."
2566   (unless (mixi-object-realized-p message)
2567     (with-mixi-retrieve (mixi-message-page message)
2568       (if (re-search-forward mixi-message-owner-regexp nil t)
2569           (mixi-message-set-owner message
2570                                   (mixi-make-friend (match-string 2)
2571                                                     (match-string 3)))
2572         (mixi-realization-error 'cannot-find-owner message))
2573       (if (re-search-forward mixi-message-time-regexp nil t)
2574           (mixi-message-set-time
2575            message (encode-time 0 (string-to-number (match-string 6))
2576                                 (string-to-number (match-string 5))
2577                                 (string-to-number (match-string 4))
2578                                 (string-to-number (match-string 3))
2579                                 (string-to-number (match-string 2))))
2580         (mixi-realization-error 'cannot-find-time message))
2581       (if (re-search-forward mixi-message-title-regexp nil t)
2582           (mixi-message-set-title message (match-string 2))
2583         (mixi-realization-error 'cannot-find-title message))
2584       (if (re-search-forward mixi-message-content-regexp nil t)
2585           (mixi-message-set-content message (match-string 1))
2586         (mixi-realization-error 'cannot-find-content message)))
2587     (mixi-object-touch message)))
2588
2589 (defun mixi-message-id (message)
2590   "Return the id of MESSAGE."
2591   (unless (mixi-message-p message)
2592     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2593   (aref (cdr message) 1))
2594
2595 (defun mixi-message-box (message)
2596   "Return the box of MESSAGE."
2597   (unless (mixi-message-p message)
2598     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2599   (aref (cdr message) 2))
2600
2601 (defun mixi-message-owner (message)
2602   "Return the owner of MESSAGE."
2603   (unless (mixi-message-p message)
2604     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2605   (mixi-realize-message message)
2606   (aref (cdr message) 3))
2607
2608 (defun mixi-message-title (message)
2609   "Return the title of MESSAGE."
2610   (unless (mixi-message-p message)
2611     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2612   (mixi-realize-message message)
2613   (aref (cdr message) 4))
2614
2615 (defun mixi-message-time (message)
2616   "Return the date of MESSAGE."
2617   (unless (mixi-message-p message)
2618     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2619   (mixi-realize-message message)
2620   (aref (cdr message) 5))
2621
2622 (defun mixi-message-content (message)
2623   "Return the content of MESSAGE."
2624   (unless (mixi-message-p message)
2625     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2626   (mixi-realize-message message)
2627   (aref (cdr message) 6))
2628
2629 (defun mixi-message-set-owner (message owner)
2630   "Set the owner of MESSAGE."
2631   (unless (mixi-message-p message)
2632     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2633   (aset (cdr message) 3 owner))
2634
2635 (defun mixi-message-set-title (message title)
2636   "Set the title of MESSAGE."
2637   (unless (mixi-message-p message)
2638     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2639   (aset (cdr message) 4 title))
2640
2641 (defun mixi-message-set-time (message time)
2642   "Set the date of MESSAGE."
2643   (unless (mixi-message-p message)
2644     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2645   (aset (cdr message) 5 time))
2646
2647 (defun mixi-message-set-content (message content)
2648   "Set the content of MESSAGE."
2649   (unless (mixi-message-p message)
2650     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2651   (aset (cdr message) 6 content))
2652
2653 (defmacro mixi-message-list-page (&optional box)
2654   `(concat "/list_message.pl?page=%d"
2655            (when ,box (concat "&box=" ,box))))
2656
2657 (defconst mixi-message-list-regexp
2658   "<td><a HREF=\"view_message\\.pl\\?id=\\(.+\\)&box=\\(.+\\)\">")
2659
2660 ;;;###autoload
2661 (defun mixi-get-messages (&rest box-or-range)
2662   "Get messages of BOX."
2663   (when (> (length box-or-range) 2)
2664     (signal 'wrong-number-of-arguments
2665             (list 'mixi-get-messages (length box-or-range))))
2666   (let ((box (nth 0 box-or-range))
2667         (range (nth 1 box-or-range)))
2668     (when (or (not (mixi-message-box-p box))
2669               (mixi-message-box-p range))
2670       (setq box (nth 1 box-or-range))
2671       (setq range (nth 0 box-or-range)))
2672     (let ((items (mixi-get-matched-items
2673                   (mixi-message-list-page
2674                    (when box (mixi-message-box-name box)))
2675                   mixi-message-list-regexp
2676                   range)))
2677       (mapcar (lambda (item)
2678                 (mixi-make-message (nth 0 item) (nth 1 item)))
2679               items))))
2680
2681 (defmacro mixi-post-message-page (friend)
2682   `(concat "/send_message.pl?id=" (mixi-friend-id friend)))
2683
2684 (defconst mixi-post-message-key-regexp
2685   "<input name=post_key type=hidden value=\\([a-z0-9]+\\)>")
2686
2687 (defconst mixi-post-message-succeed-regexp
2688   "<b>Á÷¿®´°Î»</b>¤·¤Þ¤·¤¿¡£")
2689
2690 ;;;###autoload
2691 (defun mixi-post-message (friend title content)
2692   "Post a message to FRIEND."
2693   (unless (mixi-friend-p friend)
2694     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
2695   (unless (stringp title)
2696     (signal 'wrong-type-argument (list 'stringp title)))
2697   (unless (stringp content)
2698     (signal 'wrong-type-argument (list 'stringp content)))
2699   (let ((fields `(("subject" . ,title)
2700                   ("body" . ,content)
2701                   ("submit" . "main")))
2702         post-key)
2703     (with-mixi-post-form (mixi-post-message-page friend) fields
2704       (if (re-search-forward mixi-post-message-key-regexp nil t)
2705           (setq post-key (match-string 1))
2706         (mixi-post-error 'cannot-find-key friend)))
2707     (setq fields `(("post_key" . ,post-key)
2708                    ("subject" . ,title)
2709                    ("body" . ,content)
2710                    ("yes" . "¡¡Á÷¡¡¿®¡¡")
2711                    ("submit" . "confirm")))
2712     (with-mixi-post-form (mixi-post-message-page friend) fields
2713       (unless (re-search-forward mixi-post-message-succeed-regexp nil t)
2714         (mixi-post-error 'cannot-find-succeed friend)))))
2715
2716 ;; Introduction object.
2717 (defun mixi-make-introduction (parent owner content)
2718   "Return a introduction object."
2719   (cons 'mixi-introduction (vector parent owner content)))
2720
2721 (defmacro mixi-introduction-p (introduction)
2722   `(eq (mixi-object-class ,introduction) 'mixi-introduction))
2723
2724 (defun mixi-introduction-parent (introduction)
2725   "Return the parent of INTRODUCTION."
2726   (unless (mixi-introduction-p introduction)
2727     (signal 'wrong-type-argument (list 'mixi-introduction-p introduction)))
2728   (aref (cdr introduction) 0))
2729
2730 (defun mixi-introduction-owner (introduction)
2731   "Return the owner of INTRODUCTION."
2732   (unless (mixi-introduction-p introduction)
2733     (signal 'wrong-type-argument (list 'mixi-introduction-p introduction)))
2734   (aref (cdr introduction) 1))
2735
2736 (defun mixi-introduction-content (introduction)
2737   "Return the content of INTRODUCTION."
2738   (unless (mixi-introduction-p introduction)
2739     (signal 'wrong-type-argument (list 'mixi-introduction-p introduction)))
2740   (aref (cdr introduction) 3))
2741
2742 (defmacro mixi-introduction-list-page (&optional friend)
2743   `(concat "/show_intro.pl?page=%d"
2744            (when ,friend (concat "&id=" (mixi-friend-id ,friend)))))
2745
2746 (defconst mixi-introduction-list-regexp
2747   "<tr bgcolor=#FFFFFF>
2748 <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>
2749 \\(.*\\)</td></a>
2750
2751 <td WIDTH=480>
2752 \\(´Ø·¸¡§.+<br>
2753
2754
2755 \\(\\(.\\|\n<br>\\)+\\)\\|
2756 \\(\\(.\\|\n<br>\\)+\\)\\)
2757
2758
2759
2760
2761 </td>
2762 </tr>")
2763 (defconst mixi-my-introduction-list-regexp
2764   "<tr bgcolor=#FFFFFF>
2765 <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>
2766 \\(.*\\)</td></a>
2767
2768
2769 <td WIDTH=480>
2770 \\(´Ø·¸¡§.+<br>
2771
2772
2773 \\(\\(.\\|\n<br>\\)+\\)\\|
2774 \\(\\(.\\|\n<br>\\)+\\)\\)
2775
2776
2777 <br>
2778 <a href=\"edit_intro\\.pl\\?id=\\1&type=edit\">¤³¤Îͧ¿Í¤ò¾Ò²ð¤¹¤ë</a>
2779
2780
2781 <BR>
2782 <a href=\"delete_intro\\.pl\\?id=\\1\">ºï½ü</a>
2783
2784 </td>
2785 </tr>")
2786
2787 ;;;###autoload
2788 (defun mixi-get-introductions (&rest friend-or-range)
2789   "Get introductions of FRIEND."
2790   (when (> (length friend-or-range) 2)
2791     (signal 'wrong-number-of-arguments
2792             (list 'mixi-get-introduction (length friend-or-range))))
2793   (let ((friend (nth 0 friend-or-range))
2794         (range (nth 1 friend-or-range)))
2795     (when (or (not (mixi-friend-p friend)) (mixi-friend-p range))
2796       (setq friend (nth 1 friend-or-range))
2797       (setq range (nth 0 friend-or-range)))
2798     (unless (or (null friend) (mixi-friend-p friend))
2799       (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
2800     (let* ((regexp (if friend mixi-introduction-list-regexp
2801                      mixi-my-introduction-list-regexp))
2802            (items (mixi-get-matched-items (mixi-introduction-list-page friend)
2803                                           regexp
2804                                           range)))
2805       (mapcar (lambda (item)
2806                 (mixi-make-introduction (or friend (mixi-make-me))
2807                                         (mixi-make-friend (nth 0 item)
2808                                                           (nth 1 item))
2809                                         (nth 2 item)))
2810               items))))
2811
2812 ;; News object.
2813 (defvar mixi-news-cache (make-hash-table :test 'equal))
2814 (defun mixi-make-news (media-id id &optional media time title content)
2815   "Return a news object."
2816   (mixi-make-cache (list media-id id)
2817                    (cons 'mixi-news (vector nil media-id id media time title
2818                                             content))
2819                    mixi-news-cache))
2820
2821 (defconst mixi-news-url-regexp
2822   "/view_news\\.pl\\?id=\\([0-9]+\\)&media_id=\\([0-9]+\\)")
2823
2824 (defun mixi-make-news-from-url (url)
2825   "Return a news object from URL."
2826   (when (string-match mixi-news-url-regexp url)
2827     (let ((id (match-string 1 url))
2828           (media-id (match-string 2 url)))
2829       (mixi-make-news media-id id))))
2830
2831 (defmacro mixi-news-p (news)
2832   `(eq (mixi-object-class ,news) 'mixi-news))
2833
2834 (defmacro mixi-news-page (news)
2835   `(concat "http://news.mixi.jp/view_news.pl?id=" (mixi-news-id ,news)
2836            "&media_id=" (mixi-news-media-id ,news)))
2837
2838 (defconst mixi-news-finished-regexp
2839   "¿½¤·Ìõ¤´¤¶¤¤¤Þ¤»¤ó¤¬¡¢¤³¤Î¥Ë¥å¡¼¥¹¤Ï·ÇºÜ¤¬½ªÎ»¤·¤¿¤«¡¢URL¤¬´Ö°ã¤Ã¤Æ¤¤¤¤¤ë¤¿¤á¤´Í÷¤¤¤¿¤À¤±¤Þ¤»¤ó¡£</td>")
2840 (defconst mixi-news-title-regexp
2841   "<td HEIGHT=\"46\" STYLE=\"font-weight: bold;font-size: 14px;\" CLASS=\"h130\">\\(.+\\)\\(
2842 \\)?</td>")
2843 (defconst mixi-news-media-time-regexp
2844   "<td COLSPAN=\"2\" ALIGN=\"right\">(\\(.+\\)&nbsp;-&nbsp;\\([0-9]+\\)·î\\([0-9]+\\)Æü \\([0-9]+\\):\\([0-9]+\\))</td></tr>")
2845 (defconst mixi-news-content-regexp
2846   "<td CLASS=\"h150\">
2847
2848 \\(.+\\)
2849 <br>
2850 \\(.*
2851 \\)?
2852
2853 \\(</td>\\|<br>\\)")
2854
2855 (defun mixi-realize-news (news)
2856   "Realize a NEWS."
2857   ;; FIXME: Check a expiration of cache?
2858   (unless (mixi-object-realized-p news)
2859     (with-mixi-retrieve (mixi-news-page news)
2860       (if (re-search-forward mixi-news-finished-regexp nil t)
2861           (mixi-news-set-content news (match-string 0))
2862         (if (re-search-forward mixi-news-title-regexp nil t)
2863             (mixi-news-set-title news (match-string 1))
2864           (mixi-realization-error 'cannot-find-title news))
2865         (if (re-search-forward mixi-news-media-time-regexp nil t)
2866             (progn
2867               (mixi-news-set-media news (match-string 1))
2868               (let ((year (nth 5 (decode-time (current-time))))
2869                     (month (nth 4 (decode-time (current-time))))
2870                     (month-of-item (string-to-number (match-string 2))))
2871                 (when (> month-of-item month)
2872                   (decf year))
2873                 (mixi-news-set-time
2874                  news (encode-time 0 (string-to-number (match-string 5))
2875                                    (string-to-number (match-string 4))
2876                                    (string-to-number (match-string 3))
2877                                    month year))))
2878           (mixi-realization-error 'cannot-find-media-time news))
2879         (if (re-search-forward mixi-news-content-regexp nil t)
2880             (mixi-news-set-content news (match-string 1))
2881           (mixi-realization-error 'cannot-find-content news))))
2882     (mixi-object-touch news)))
2883
2884 (defun mixi-news-media-id (news)
2885   "Return the media-id of NEWS."
2886   (unless (mixi-news-p news)
2887     (signal 'wrong-type-argument (list 'mixi-news-p news)))
2888   (aref (cdr news) 1))
2889
2890 (defun mixi-news-id (news)
2891   "Return the id of NEWS."
2892   (unless (mixi-news-p news)
2893     (signal 'wrong-type-argument (list 'mixi-news-p news)))
2894   (aref (cdr news) 2))
2895
2896 (defun mixi-news-media (news)
2897   "Return the media of NEWS."
2898   (unless (mixi-news-p news)
2899     (signal 'wrong-type-argument (list 'mixi-news-p news)))
2900   (unless (aref (cdr news) 3)
2901     (mixi-realize-news news))
2902   (aref (cdr news) 3))
2903
2904 (defun mixi-news-time (news)
2905   "Return the time of NEWS."
2906   (unless (mixi-news-p news)
2907     (signal 'wrong-type-argument (list 'mixi-news-p news)))
2908   (unless (aref (cdr news) 4)
2909     (mixi-realize-news news))
2910   (aref (cdr news) 4))
2911
2912 (defun mixi-news-title (news)
2913   "Return the title of NEWS."
2914   (unless (mixi-news-p news)
2915     (signal 'wrong-type-argument (list 'mixi-news-p news)))
2916   (unless (aref (cdr news) 5)
2917     (mixi-realize-news news))
2918   (aref (cdr news) 5))
2919
2920 (defun mixi-news-content (news)
2921   "Return the content of NEWS."
2922   (unless (mixi-news-p news)
2923     (signal 'wrong-type-argument (list 'mixi-news-p news)))
2924   (mixi-realize-news news)
2925   (aref (cdr news) 6))
2926
2927 (defun mixi-news-set-media (news media)
2928   "Set the media of NEWS."
2929   (unless (mixi-news-p news)
2930     (signal 'wrong-type-argument (list 'mixi-news-p news)))
2931   (aset (cdr news) 3 media))
2932
2933 (defun mixi-news-set-time (news time)
2934   "Set the time of NEWS."
2935   (unless (mixi-news-p news)
2936     (signal 'wrong-type-argument (list 'mixi-news-p news)))
2937   (aset (cdr news) 4 time))
2938
2939 (defun mixi-news-set-title (news title)
2940   "Set the title of NEWS."
2941   (unless (mixi-news-p news)
2942     (signal 'wrong-type-argument (list 'mixi-news-p news)))
2943   (aset (cdr news) 5 title))
2944
2945 (defun mixi-news-set-content (news content)
2946   "Set the content of NEWS."
2947   (unless (mixi-news-p news)
2948     (signal 'wrong-type-argument (list 'mixi-news-p news)))
2949   (aset (cdr news) 6 content))
2950
2951 (defconst mixi-news-category-list '(domestic politics economy area abroad
2952                                              sports entertainment IT))
2953
2954 (defmacro mixi-news-category-p (category)
2955   `(memq ,category mixi-news-category-list))
2956
2957 (defun mixi-news-category-id (category)
2958   "Return the id of CATEGORY."
2959   (unless (mixi-news-category-p category)
2960     (signal 'wrong-type-argument (list 'mixi-news-category-p category)))
2961   (number-to-string
2962    (1+ (- (length mixi-news-category-list)
2963           (length (memq category mixi-news-category-list))))))
2964
2965 (defconst mixi-news-sort-list '(newest pickup))
2966
2967 (defmacro mixi-news-sort-p (sort)
2968   `(memq ,sort mixi-news-sort-list))
2969
2970 (defun mixi-news-sort-id (sort)
2971   "Return the id of SORT."
2972   (unless (mixi-news-sort-p sort)
2973     (signal 'wrong-type-argument (list 'mixi-news-sort-p sort)))
2974   (number-to-string
2975    (- (length mixi-news-sort-list)
2976       (length (memq sort mixi-news-sort-list)))))
2977
2978 (defmacro mixi-news-list-page (category sort)
2979   `(concat "http://news.mixi.jp/list_news_category.pl?page=%d"
2980            "&sort=" (mixi-news-sort-id ,sort)
2981            "&id=" (mixi-news-category-id ,category)
2982            "&type=bn"))
2983
2984 (defconst mixi-news-list-regexp
2985   "<tr bgcolor=\"\\(#FCF5EB\\|#FFFFFF\\)\">
2986 <td WIDTH=\"1%\" valign=top CLASS=\"h120\">¡¦</td>
2987 <td WIDTH=\"97%\" CLASS=\"h120\"><A HREF=\"view_news\\.pl\\?id=\\([0-9]+\\)&media_id=\\([0-9]+\\)\"class=\"new_link\">\\(.+\\)</A>
2988 \\(<IMG SRC=\"http://img\\.mixi\\.jp/img/news_camera3\\.gif\" WIDTH=\"11\" HEIGHT=\"12\">\\|\\)
2989
2990 </td>
2991 <td WIDTH=\"1%\" nowrap CLASS=\"f08\"><A HREF=\"list_news_media\\.pl\\?id=[0-9]+\">\\(.+\\)</A></td>
2992 <td WIDTH=\"1%\" nowrap CLASS=\"f08\">\\([0-9]+\\)·î\\([0-9]+\\)Æü \\([0-9]+\\):\\([0-9]+\\)</td></tr>")
2993
2994 ;;;###autoload
2995 (defun mixi-get-news (category sort &optional range)
2996   "Get news of CATEGORY and SORT."
2997   (unless (mixi-news-category-p category)
2998     (signal 'wrong-type-argument (list 'mixi-news-category-p category)))
2999   (unless (mixi-news-sort-p sort)
3000     (signal 'wrong-type-argument (list 'mixi-news-sort-p sort)))
3001   (let ((items (mixi-get-matched-items (mixi-news-list-page category sort)
3002                                        mixi-news-list-regexp
3003                                        range))
3004         (year (nth 5 (decode-time (current-time))))
3005         (month (nth 4 (decode-time (current-time)))))
3006     (mapcar (lambda (item)
3007               (let ((month-of-item (string-to-number (nth 6 item))))
3008                 (when (> month-of-item month)
3009                   (decf year))
3010                 (setq month month-of-item)
3011                 (mixi-make-news (nth 2 item) (nth 1 item) (nth 5 item)
3012                                 (encode-time
3013                                  0 (string-to-number (nth 9 item))
3014                                  (string-to-number (nth 8 item))
3015                                  (string-to-number (nth 7 item))
3016                                  month year)
3017                                 (nth 3 item))))
3018             items)))
3019
3020 (provide 'mixi)
3021
3022 ;;; mixi.el ends here