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