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