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