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