* Makefile.am (compile-individually): New target.
[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 profile 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>")
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=\"?center\"? rowspan=\"?[23]\"? nowrap\\(=\"?nowrap\"?\\)? width=\"?95\"? bgcolor=\"?#FFD8B0\"?>\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü<br\\( /\\)?>\\([0-9]+\\):\\([0-9]+\\)</td>")
1165 (defconst mixi-diary-title-regexp
1166   "<td bgcolor=\"?#FFF4E0\"? width=\"?430\"?>&nbsp;\\([^<]+\\)</td>")
1167 (defconst mixi-diary-content-regexp
1168   "<td class=\"?h12\"?>\\(\\(.\\|\r?\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       (let ((case-fold-search t))
1176         (unless (re-search-forward mixi-diary-closed-regexp nil t)
1177           (if (re-search-forward mixi-diary-owner-nick-regexp nil t)
1178               (mixi-friend-set-nick (mixi-diary-owner diary)
1179                                     (match-string 1))
1180             (mixi-realization-error 'cannot-find-owner-nick diary))
1181           (if (re-search-forward mixi-diary-time-regexp nil t)
1182               (mixi-diary-set-time
1183                diary (encode-time 0 (string-to-number (match-string 7))
1184                                   (string-to-number (match-string 6))
1185                                   (string-to-number (match-string 4))
1186                                   (string-to-number (match-string 3))
1187                                   (string-to-number (match-string 2))))
1188             (mixi-realization-error 'cannot-find-time diary))
1189           (if (re-search-forward mixi-diary-title-regexp nil t)
1190               (mixi-diary-set-title diary (match-string 1))
1191             (mixi-realization-error 'cannot-find-title diary))
1192           (if (re-search-forward mixi-diary-content-regexp nil t)
1193               (mixi-diary-set-content diary (match-string 1))
1194             (mixi-realization-error 'cannot-find-content diary)))))
1195     (mixi-object-touch diary)))
1196
1197 (defun mixi-diary-owner (diary)
1198   "Return the owner of DIARY."
1199   (unless (mixi-diary-p diary)
1200     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1201   (aref (cdr diary) 1))
1202
1203 (defun mixi-diary-id (diary)
1204   "Return the id of DIARY."
1205   (unless (mixi-diary-p diary)
1206     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1207   (aref (cdr diary) 2))
1208
1209 (defun mixi-diary-comment-count (diary)
1210   "Return the comment-count of DIARY."
1211   (unless (mixi-diary-p diary)
1212     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1213   (aref (cdr diary) 3))
1214
1215 (defun mixi-diary-time (diary)
1216   "Return the time of DIARY."
1217   (unless (mixi-diary-p diary)
1218     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1219   (unless (aref (cdr diary) 3)
1220     (mixi-realize-diary diary))
1221   (aref (cdr diary) 4))
1222
1223 (defun mixi-diary-title (diary)
1224   "Return the title of DIARY."
1225   (unless (mixi-diary-p diary)
1226     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1227   (unless (aref (cdr diary) 4)
1228     (mixi-realize-diary diary))
1229   (aref (cdr diary) 5))
1230
1231 (defun mixi-diary-content (diary)
1232   "Return the content of DIARY."
1233   (unless (mixi-diary-p diary)
1234     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1235   (mixi-realize-diary diary)
1236   (aref (cdr diary) 6))
1237
1238 (defun mixi-diary-set-comment-count (diary comment-count)
1239   "Set the comment-count of DIARY."
1240   (unless (mixi-diary-p diary)
1241     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1242   (aset (cdr diary) 3 comment-count))
1243
1244 (defun mixi-diary-set-time (diary time)
1245   "Set the time of DIARY."
1246   (unless (mixi-diary-p diary)
1247     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1248   (aset (cdr diary) 4 time))
1249
1250 (defun mixi-diary-set-title (diary title)
1251   "Set the title of DIARY."
1252   (unless (mixi-diary-p diary)
1253     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1254   (aset (cdr diary) 5 title))
1255
1256 (defun mixi-diary-set-content (diary content)
1257   "Set the content of DIARY."
1258   (unless (mixi-diary-p diary)
1259     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1260   (aset (cdr diary) 6 content))
1261
1262 (defmacro mixi-diary-list-page (&optional friend)
1263   `(concat "/list_diary.pl?page=%d"
1264            (when ,friend (concat "&id=" (mixi-friend-id ,friend)))))
1265
1266 (defconst mixi-diary-list-regexp
1267   "<tr VALIGN=top>
1268 <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>
1269 <td bgcolor=\"#FFF4E0\">&nbsp;<a href=\"view_diary\\.pl\\?id=\\([0-9]+\\)&owner_id=[0-9]+\">\\(.*\\)</a></td>")
1270
1271 ;;;###autoload
1272 (defun mixi-get-diaries (&rest friend-or-range)
1273   "Get diaries of FRIEND."
1274   (when (> (length friend-or-range) 2)
1275     (signal 'wrong-number-of-arguments
1276             (list 'mixi-get-diaries (length friend-or-range))))
1277   (let ((friend (nth 0 friend-or-range))
1278         (range (nth 1 friend-or-range)))
1279     (when (or (not (mixi-friend-p friend)) (mixi-friend-p range))
1280       (setq friend (nth 1 friend-or-range))
1281       (setq range (nth 0 friend-or-range)))
1282     (unless (or (null friend) (mixi-friend-p friend))
1283       (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
1284     (let ((items (mixi-get-matched-items (mixi-diary-list-page friend)
1285                                          mixi-diary-list-regexp
1286                                          range))
1287           (year (nth 5 (decode-time (current-time))))
1288           (month (nth 4 (decode-time (current-time)))))
1289       (mapcar (lambda (item)
1290                 (let ((month-of-item (string-to-number (nth 0 item))))
1291                   (when (> month-of-item month)
1292                     (decf year))
1293                   (setq month month-of-item)
1294                   (mixi-make-diary friend (nth 5 item) nil
1295                                    (encode-time
1296                                     0 (string-to-number (nth 3 item))
1297                                     (string-to-number (nth 2 item))
1298                                     (string-to-number (nth 1 item))
1299                                     month year)
1300                                    (nth 6 item))))
1301               items))))
1302
1303 (defmacro mixi-new-diary-list-page ()
1304   `(concat "/new_friend_diary.pl?page=%d"))
1305
1306 (defconst mixi-new-diary-list-regexp
1307   "<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>
1308 <td WIDTH=450><a class=\"new_link\" href=view_diary\\.pl\\?id=\\([0-9]+\\)&owner_id=\\([0-9]+\\)>\\(.+\\)</a> (\\(.*\\)) ")
1309
1310 ;;;###autoload
1311 (defun mixi-get-new-diaries (&optional range)
1312   "Get new diaries."
1313   (let ((items (mixi-get-matched-items (mixi-new-diary-list-page)
1314                                        mixi-new-diary-list-regexp
1315                                        range)))
1316     (mapcar (lambda (item)
1317               (mixi-make-diary (mixi-make-friend (nth 6 item) (nth 8 item))
1318                                (nth 5 item)
1319                                nil
1320                                (encode-time
1321                                 0 (string-to-number (nth 4 item))
1322                                 (string-to-number (nth 3 item))
1323                                 (string-to-number (nth 2 item))
1324                                 (string-to-number (nth 1 item))
1325                                 (string-to-number (nth 0 item)))
1326                                (nth 7 item)))
1327             items)))
1328
1329 (defmacro mixi-search-diary-list-page (keyword)
1330   `(concat "/search_diary.pl?page=%d&submit=search&keyword="
1331              (mixi-url-encode-and-quote-percent-string ,keyword)))
1332
1333 (defconst mixi-search-diary-list-regexp
1334   "<td BGCOLOR=#FDF9F2><font COLOR=#996600>̾&nbsp;&nbsp;Á°</font></td>
1335 <td COLSPAN=2 BGCOLOR=#FFFFFF>\\(.*\\)
1336
1337 </td></tr>
1338
1339 <tr>
1340 <td BGCOLOR=#FDF9F2><font COLOR=#996600>¥¿¥¤¥È¥ë</font></td>
1341 <td COLSPAN=2 BGCOLOR=#FFFFFF>\\(.+\\)</td></tr>
1342
1343 <tr>
1344 <td BGCOLOR=#FDF9F2><font COLOR=#996600>ËÜ&nbsp;&nbsp;ʸ</font></td>
1345 <td COLSPAN=2 BGCOLOR=#FFFFFF>\\(.*\\)</td></tr>
1346
1347
1348 <tr>
1349 <td NOWRAP BGCOLOR=#FDF9F2 WIDTH=80><font COLOR=#996600>ºîÀ®Æü»þ</font></td>
1350 <td BGCOLOR=#FFFFFF WIDTH=220>\\([0-9]+\\)·î\\([0-9]+\\)Æü \\([0-9]+\\):\\([0-9]+\\)</td>
1351 <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>
1352 </table>
1353 </td></tr></table>")
1354
1355 ;;;###autoload
1356 (defun mixi-search-diaries (keyword &optional range)
1357   (let ((items (mixi-get-matched-items (mixi-search-diary-list-page keyword)
1358                                        mixi-search-diary-list-regexp
1359                                        range))
1360         (year (nth 5 (decode-time (current-time))))
1361         (month (nth 4 (decode-time (current-time)))))
1362     (mapcar (lambda (item)
1363                 (let ((month-of-item (string-to-number (nth 3 item))))
1364                   (when (> month-of-item month)
1365                     (decf year))
1366                   (setq month month-of-item)
1367                   (mixi-make-diary (mixi-make-friend (nth 8 item) (nth 0 item))
1368                                    (nth 7 item)
1369                                    nil
1370                                    (encode-time
1371                                     0 (string-to-number (nth 6 item))
1372                                     (string-to-number (nth 5 item))
1373                                     (string-to-number (nth 4 item))
1374                                     month year)
1375                                    (nth 1 item)
1376                                    (nth 2 item))))
1377             items)))
1378
1379 (defmacro mixi-post-diary-page ()
1380   `(concat "/add_diary.pl"))
1381
1382 (defconst mixi-post-key-regexp
1383   "<input type=\"?hidden\"? name=\"?post_key\"? value=\"\\([a-z0-9]+\\)\">")
1384 (defconst mixi-post-succeed-regexp
1385   "<b>\\(ºîÀ®\\|½ñ¤­¹þ¤ß\\)¤¬´°Î»¤·¤Þ¤·¤¿¡£È¿±Ç¤Ë»þ´Ö¤¬¤«¤«¤ë¤³¤È¤¬¤¢¤ê¤Þ¤¹¤Î¤Ç¡¢É½¼¨¤µ¤ì¤Æ¤¤¤Ê¤¤¾ì¹ç¤Ï¾¯¡¹¤ªÂÔ¤Á¤¯¤À¤µ¤¤¡£</b>")
1386
1387 ;; FIXME: Support photos.
1388 ;;;###autoload
1389 (defun mixi-post-diary (title content)
1390   "Post a diary."
1391   (unless (stringp title)
1392     (signal 'wrong-type-argument (list 'stringp title)))
1393   (unless (stringp content)
1394     (signal 'wrong-type-argument (list 'stringp content)))
1395   (let ((fields `(("id" . ,(mixi-friend-id (mixi-make-me)))
1396                   ("diary_title" . ,title)
1397                   ("diary_body" . ,content)
1398                   ("submit" . "main")))
1399         post-key)
1400     (with-mixi-post-form (mixi-post-diary-page) fields
1401       (if (re-search-forward mixi-post-key-regexp nil t)
1402           (setq post-key (match-string 1))
1403         (mixi-post-error 'cannot-find-key)))
1404     (setq fields `(("post_key" . ,post-key)
1405                    ("id" . ,(mixi-friend-id (mixi-make-me)))
1406                    ("diary_title" . ,title)
1407                    ("diary_body" . ,content)
1408                    ("submit" . "confirm")))
1409     (with-mixi-post-form (mixi-post-diary-page) fields
1410       (unless (re-search-forward mixi-post-succeed-regexp nil t)
1411         (mixi-post-error 'cannot-find-succeed)))))
1412
1413 ;; Community object.
1414 (defvar mixi-community-cache (make-hash-table :test 'equal))
1415 (defun mixi-make-community (id &optional name birthday owner category members
1416                                open-level authority description)
1417   "Return a community object."
1418   (mixi-make-cache id (cons 'mixi-community (vector nil id name birthday owner
1419                                                     category members
1420                                                     open-level authority
1421                                                     description))
1422                    mixi-community-cache))
1423
1424 (defconst mixi-community-url-regexp
1425   "/view_community\\.pl\\?id=\\([0-9]+\\)")
1426
1427 (defun mixi-make-community-from-url (url)
1428   "Return a community object from URL."
1429   (when (string-match mixi-community-url-regexp url)
1430     (let ((id (match-string 1 url)))
1431       (mixi-make-community id))))
1432
1433 (defmacro mixi-community-p (community)
1434   `(eq (mixi-object-class ,community) 'mixi-community))
1435
1436 (defmacro mixi-community-page (community)
1437   `(concat "/view_community.pl?id=" (mixi-community-id ,community)))
1438
1439 ;; FIXME: Not community specific.
1440 (defconst mixi-community-nodata-regexp
1441   "^¥Ç¡¼¥¿¤¬¤¢¤ê¤Þ¤»¤ó")
1442 (defconst mixi-community-name-regexp
1443   "<td width=\"?345\"?>\\(.*\\)</td></tr>")
1444 (defconst mixi-community-birthday-regexp
1445   "<td bgcolor=\"?#F2DDB7\"? width=\"?80\"?><font color=\"?#996600\"?>³«ÀßÆü</font></td>
1446 <td width=\"?345\"?>\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü")
1447 ;; FIXME: Care when the owner has seceded.
1448 (defconst mixi-community-owner-regexp
1449   "<td bgcolor=\"?#F2DDB7\"? width=\"?80\"?><font color=\"?#996600\"?>´ÉÍý¿Í</font></td>
1450 <td width=\"?345\"?>
1451 <table style=\"width: 100%;\"><tr><td>
1452 <a href=\"\\(home\\.pl\\|show_friend\\.pl\\?id=\\([0-9]+\\)\\)\">\\(.*\\)</a>")
1453 (defconst mixi-community-category-regexp
1454   "<td bgcolor=\"?#F2DDB7\"? width=\"?80\"?><font color=\"?#996600\"?>¥«¥Æ¥´¥ê</font></td>
1455 <td width=\"?345\"?>\\([^<]+\\)</td>")
1456 (defconst mixi-community-members-regexp
1457   "<td bgcolor=\"?#F2DDB7\"? width=\"?80\"?><font color=\"?#996600\"?>¥á¥ó¥Ð¡¼¿ô</font></td>
1458 <td width=\"?345\"?>\\([0-9]+\\)¿Í</td></tr>")
1459 (defconst mixi-community-open-level-regexp
1460   "<td bgcolor=\"?#F2DDB7\"? width=\"?80\"?><font color=\"?#996600\"?>»²²Ã¾ò·ï¤È<br />¸ø³«¥ì¥Ù¥ë</font></td>
1461 <td width=\"?345\"?>\\(.+\\)</td></tr>")
1462 (defconst mixi-community-authority-regexp
1463   "<td bgcolor=\"?#F2DDB7\"? width=\"?80\"?><font color=\"?#996600\"?>¥È¥Ô¥Ã¥¯ºîÀ®¤Î¸¢¸Â</font></td>
1464 <td width=\"?345\"?>\\(.+\\)</td></tr>")
1465 (defconst mixi-community-description-regexp
1466   "<td class=\"?h120\"? width=\"?345\"?>\\(.+\\)</td>")
1467
1468 (defun mixi-realize-community (community)
1469   "Realize a COMMUNITY."
1470   ;; FIXME: Check a expiration of cache?
1471   (unless (mixi-object-realized-p community)
1472     (with-mixi-retrieve (mixi-community-page community)
1473       (let ((case-fold-search t))
1474         (if (re-search-forward mixi-community-nodata-regexp nil t)
1475             ;; FIXME: Set all members?
1476             (mixi-community-set-name community "¥Ç¡¼¥¿¤¬¤¢¤ê¤Þ¤»¤ó")
1477           (if (re-search-forward mixi-community-name-regexp nil t)
1478               (mixi-community-set-name community (match-string 1))
1479             (mixi-realization-error 'cannot-find-name community))
1480           (if (re-search-forward mixi-community-birthday-regexp nil t)
1481               (mixi-community-set-birthday
1482                community (encode-time 0 0 0
1483                                       (string-to-number (match-string 3))
1484                                       (string-to-number (match-string 2))
1485                                       (string-to-number (match-string 1))))
1486             (mixi-realization-error 'cannot-find-birthday community))
1487           (if (re-search-forward mixi-community-owner-regexp nil t)
1488               (if (string= (match-string 1) "home.pl")
1489                   (mixi-community-set-owner community (mixi-make-me))
1490                 (mixi-community-set-owner community (mixi-make-friend
1491                                                      (match-string 2)
1492                                                      (match-string 3))))
1493             (mixi-realization-error 'cannot-find-owner community))
1494           (if (re-search-forward mixi-community-category-regexp nil t)
1495               (mixi-community-set-category community (match-string 1))
1496             (mixi-realization-error 'cannot-find-category community))
1497           (if (re-search-forward mixi-community-members-regexp nil t)
1498               (mixi-community-set-members community
1499                                           (string-to-number (match-string 1)))
1500             (mixi-realization-error 'cannot-find-members community))
1501           (if (re-search-forward mixi-community-open-level-regexp nil t)
1502               (mixi-community-set-open-level community (match-string 1))
1503             (mixi-realization-error 'cannot-find-open-level community))
1504           (if (re-search-forward mixi-community-authority-regexp nil t)
1505               (mixi-community-set-authority community (match-string 1))
1506             (mixi-realization-error 'cannot-find-authority community))
1507           (if (re-search-forward mixi-community-description-regexp nil t)
1508               (mixi-community-set-description community (match-string 1))
1509             (mixi-realization-error 'cannot-find-description community)))))
1510     (mixi-object-touch community)))
1511
1512 (defun mixi-community-id (community)
1513   "Return the id of COMMUNITY."
1514   (unless (mixi-community-p community)
1515     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1516   (aref (cdr community) 1))
1517
1518 (defun mixi-community-name (community)
1519   "Return the name of COMMUNITY."
1520   (unless (mixi-community-p community)
1521     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1522   (unless (aref (cdr community) 2)
1523     (mixi-realize-community community))
1524   (aref (cdr community) 2))
1525
1526 (defun mixi-community-birthday (community)
1527   "Return the birthday of COMMUNITY."
1528   (unless (mixi-community-p community)
1529     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1530   (mixi-realize-community community)
1531   (aref (cdr community) 3))
1532
1533 (defun mixi-community-owner (community)
1534   "Return the owner of COMMUNITY."
1535   (unless (mixi-community-p community)
1536     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1537   (mixi-realize-community community)
1538   (aref (cdr community) 4))
1539
1540 (defun mixi-community-category (community)
1541   "Return the category of COMMUNITY."
1542   (unless (mixi-community-p community)
1543     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1544   (mixi-realize-community community)
1545   (aref (cdr community) 5))
1546
1547 (defun mixi-community-members (community)
1548   "Return the members of COMMUNITY."
1549   (unless (mixi-community-p community)
1550     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1551   (mixi-realize-community community)
1552   (aref (cdr community) 6))
1553
1554 (defun mixi-community-open-level (community)
1555   "Return the open-level of COMMUNITY."
1556   (unless (mixi-community-p community)
1557     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1558   (mixi-realize-community community)
1559   (aref (cdr community) 7))
1560
1561 (defun mixi-community-authority (community)
1562   "Return the authority of COMMUNITY."
1563   (unless (mixi-community-p community)
1564     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1565   (mixi-realize-community community)
1566   (aref (cdr community) 8))
1567
1568 (defun mixi-community-description (community)
1569   "Return the description of COMMUNITY."
1570   (unless (mixi-community-p community)
1571     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1572   (mixi-realize-community community)
1573   (aref (cdr community) 9))
1574
1575 (defun mixi-community-set-name (community name)
1576   "Set the name of COMMUNITY."
1577   (unless (mixi-community-p community)
1578     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1579   (aset (cdr community) 2 name))
1580
1581 (defun mixi-community-set-birthday (community birthday)
1582   "Set the birthday of COMMUNITY."
1583   (unless (mixi-community-p community)
1584     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1585   (aset (cdr community) 3 birthday))
1586
1587 (defun mixi-community-set-owner (community owner)
1588   "Set the owner of COMMUNITY."
1589   (unless (mixi-community-p community)
1590     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1591   (unless (mixi-friend-p owner)
1592     (signal 'wrong-type-argument (list 'mixi-friend-p owner)))
1593   (aset (cdr community) 4 owner))
1594
1595 (defun mixi-community-set-category (community category)
1596   "Set the category of COMMUNITY."
1597   (unless (mixi-community-p community)
1598     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1599   (aset (cdr community) 5 category))
1600
1601 (defun mixi-community-set-members (community members)
1602   "Set the name of COMMUNITY."
1603   (unless (mixi-community-p community)
1604     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1605   (aset (cdr community) 6 members))
1606
1607 (defun mixi-community-set-open-level (community open-level)
1608   "Set the name of COMMUNITY."
1609   (unless (mixi-community-p community)
1610     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1611   (aset (cdr community) 7 open-level))
1612
1613 (defun mixi-community-set-authority (community authority)
1614   "Set the name of COMMUNITY."
1615   (unless (mixi-community-p community)
1616     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1617   (aset (cdr community) 8 authority))
1618
1619 (defun mixi-community-set-description (community description)
1620   "Set the name of COMMUNITY."
1621   (unless (mixi-community-p community)
1622     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1623   (aset (cdr community) 9 description))
1624
1625 (defmacro mixi-community-list-page (&optional friend)
1626   `(concat "/list_community.pl?page=%d"
1627            (when ,friend (concat "&id=" (mixi-friend-id ,friend)))))
1628
1629 (defconst mixi-community-list-id-regexp
1630   "<a href=view_community\\.pl\\?id=\\([0-9]+\\)")
1631 (defconst mixi-community-list-name-regexp
1632   "<td valign=middle>\\(.+\\)([0-9]+)</td>")
1633
1634 ;;;###autoload
1635 (defun mixi-get-communities (&rest friend-or-range)
1636   "Get communities of FRIEND."
1637   (when (> (length friend-or-range) 2)
1638     (signal 'wrong-number-of-arguments
1639             (list 'mixi-get-communities (length friend-or-range))))
1640   (let ((friend (nth 0 friend-or-range))
1641         (range (nth 1 friend-or-range)))
1642     (when (or (not (mixi-friend-p friend)) (mixi-friend-p range))
1643       (setq friend (nth 1 friend-or-range))
1644       (setq range (nth 0 friend-or-range)))
1645     (unless (or (null friend) (mixi-friend-p friend))
1646       (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
1647     (let ((ids (mixi-get-matched-items (mixi-community-list-page friend)
1648                                        mixi-community-list-id-regexp
1649                                        range))
1650           (names (mixi-get-matched-items (mixi-community-list-page friend)
1651                                          mixi-community-list-name-regexp
1652                                          range)))
1653       (let ((index 0)
1654             ret)
1655         (while (< index (length ids))
1656           (setq ret (cons (mixi-make-community (nth 0 (nth index ids))
1657                                                (nth 0 (nth index names))) ret))
1658           (incf index))
1659         (reverse ret)))))
1660
1661 (defmacro mixi-search-community-list-page (keyword)
1662   `(concat "/search_community.pl?page=%d&&sort=date&type=com&submit=main"
1663            "&keyword=" (mixi-url-encode-and-quote-percent-string ,keyword)
1664            "&category_id=0"))
1665
1666 (defconst mixi-search-community-list-regexp
1667   "<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>
1668 <td NOWRAP WIDTH=90 BGCOLOR=#FDF9F2><font COLOR=#996600>¥³¥ß¥å¥Ë¥Æ¥£Ì¾</font></td>
1669 <td COLSPAN=2 WIDTH=370 BGCOLOR=#FFFFFF>\\([^<]+\\)</td></tr>")
1670
1671 ;; FIXME: Support category.
1672 ;;;###autoload
1673 (defun mixi-search-communities (keyword &optional range)
1674   (let ((items (mixi-get-matched-items (mixi-search-community-list-page
1675                                         keyword)
1676                                        mixi-search-community-list-regexp
1677                                        range)))
1678     (mapcar (lambda (item)
1679               (mixi-make-community (nth 0 item) (nth 1 item)))
1680             items)))
1681
1682 ;; Topic object.
1683 (defvar mixi-topic-cache (make-hash-table :test 'equal))
1684 (defun mixi-make-topic (community id &optional comment-count time title owner
1685                                   content)
1686   "Return a topic object."
1687   (mixi-make-cache (list (mixi-community-id community) id)
1688                    (cons 'mixi-topic (vector nil community id comment-count
1689                                              time title owner content))
1690                    mixi-topic-cache))
1691
1692 (defconst mixi-topic-url-regexp
1693   "/view_bbs\\.pl\\?id=\\([0-9]+\\)\\(&comment_count=\\([0-9]+\\)\\)?&comm_id=\\([0-9]+\\)")
1694
1695 (defun mixi-make-topic-from-url (url)
1696   "Return a topic object from URL."
1697   (when (string-match mixi-topic-url-regexp url)
1698     (let ((id (match-string 1 url))
1699           (comment-count (match-string 3 url))
1700           (community-id (match-string 4 url)))
1701       (mixi-make-topic (mixi-make-community community-id) id comment-count))))
1702
1703 (defmacro mixi-topic-p (topic)
1704   `(eq (mixi-object-class ,topic) 'mixi-topic))
1705
1706 (defmacro mixi-topic-page (topic)
1707   `(concat "/view_bbs.pl?id=" (mixi-topic-id ,topic)
1708            "&comm_id=" (mixi-community-id (mixi-topic-community ,topic))))
1709
1710 (defconst mixi-topic-community-regexp
1711   "<td width=\"595\" background=\"http://img\\.mixi\\.jp/img/bg_w\\.gif\"><b>\\[\\(.+\\)\\] ¥È¥Ô¥Ã¥¯</b></td>")
1712 (defconst mixi-topic-time-regexp
1713   "<td rowspan=\"3\" width=\"110\" bgcolor=\"#ffd8b0\" align=\"center\" valign=\"top\" nowrap>\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü<br>\\([0-9]+\\):\\([0-9]+\\)</td>")
1714 (defconst mixi-topic-title-regexp
1715   "<td bgcolor=\"#fff4e0\">&nbsp;\\([^<]+\\)</td>")
1716 (defconst mixi-topic-owner-regexp
1717   "<td bgcolor=\"#fdf9f2\">&nbsp;<font color=\"#dfb479\"></font>&nbsp;<a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.*?\\)\\(¤µ¤ó\\)?</a>")
1718 (defconst mixi-topic-content-regexp
1719   "<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>")
1720
1721 (defun mixi-realize-topic (topic &optional page)
1722   "Realize a TOPIC."
1723   ;; FIXME: Check a expiration of cache?
1724   (unless (mixi-object-realized-p topic)
1725     (with-mixi-retrieve (or page (mixi-topic-page topic))
1726       (if (re-search-forward mixi-topic-community-regexp nil t)
1727           (mixi-community-set-name (mixi-topic-community topic)
1728                                    (match-string 1))
1729         (mixi-realization-error 'cannot-find-community topic))
1730       (if (re-search-forward mixi-topic-time-regexp nil t)
1731           (mixi-topic-set-time
1732            topic (encode-time 0 (string-to-number (match-string 5))
1733                               (string-to-number (match-string 4))
1734                               (string-to-number (match-string 3))
1735                               (string-to-number (match-string 2))
1736                               (string-to-number (match-string 1))))
1737         (mixi-realization-error 'cannot-find-time topic))
1738       (if (re-search-forward mixi-topic-title-regexp nil t)
1739           (mixi-topic-set-title topic (match-string 1))
1740         (mixi-realization-error 'cannot-find-title topic))
1741       (if (re-search-forward mixi-topic-owner-regexp nil t)
1742           (mixi-topic-set-owner topic (mixi-make-friend (match-string 1)
1743                                                         (match-string 2)))
1744         (mixi-realization-error 'cannot-find-owner topic))
1745       (if (re-search-forward mixi-topic-content-regexp nil t)
1746           (mixi-topic-set-content topic (match-string 2))
1747         (mixi-realization-error 'cannot-find-content topic)))
1748     (mixi-object-touch topic)))
1749
1750 (defun mixi-topic-community (topic)
1751   "Return the community of TOPIC."
1752   (unless (mixi-topic-p topic)
1753     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1754   (aref (cdr topic) 1))
1755
1756 (defun mixi-topic-id (topic)
1757   "Return the id of TOPIC."
1758   (unless (mixi-topic-p topic)
1759     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1760   (aref (cdr topic) 2))
1761
1762 (defun mixi-topic-comment-count (topic)
1763   "Return the comment-count of TOPIC."
1764   (unless (mixi-topic-p topic)
1765     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1766   (aref (cdr topic) 3))
1767
1768 (defun mixi-topic-time (topic)
1769   "Return the time of TOPIC."
1770   (unless (mixi-topic-p topic)
1771     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1772   (mixi-realize-topic topic)
1773   (aref (cdr topic) 4))
1774
1775 (defun mixi-topic-title (topic)
1776   "Return the title of TOPIC."
1777   (unless (mixi-topic-p topic)
1778     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1779   (mixi-realize-topic topic)
1780   (aref (cdr topic) 5))
1781
1782 (defun mixi-topic-owner (topic)
1783   "Return the owner of TOPIC."
1784   (unless (mixi-topic-p topic)
1785     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1786   (mixi-realize-topic topic)
1787   (aref (cdr topic) 6))
1788
1789 (defun mixi-topic-content (topic)
1790   "Return the content of TOPIC."
1791   (unless (mixi-topic-p topic)
1792     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1793   (mixi-realize-topic topic)
1794   (aref (cdr topic) 7))
1795
1796 (defun mixi-topic-set-comment-count (topic comment-count)
1797   "Set the comment-count of TOPIC."
1798   (unless (mixi-topic-p topic)
1799     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1800   (aset (cdr topic) 3 comment-count))
1801
1802 (defun mixi-topic-set-time (topic time)
1803   "Set the time of TOPIC."
1804   (unless (mixi-topic-p topic)
1805     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1806   (aset (cdr topic) 4 time))
1807
1808 (defun mixi-topic-set-title (topic title)
1809   "Set the title of TOPIC."
1810   (unless (mixi-topic-p topic)
1811     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1812   (aset (cdr topic) 5 title))
1813
1814 (defun mixi-topic-set-owner (topic owner)
1815   "Set the owner of TOPIC."
1816   (unless (mixi-topic-p topic)
1817     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1818   (unless (mixi-friend-p owner)
1819     (signal 'wrong-type-argument (list 'mixi-friend-p owner)))
1820   (aset (cdr topic) 6 owner))
1821
1822 (defun mixi-topic-set-content (topic content)
1823   "Set the content of TOPIC."
1824   (unless (mixi-topic-p topic)
1825     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1826   (aset (cdr topic) 7 content))
1827
1828 (defmacro mixi-post-topic-page (community)
1829   `(concat "/add_bbs.pl?id=" (mixi-community-id community)))
1830
1831 ;; FIXME: Support photos.
1832 ;;;###autoload
1833 (defun mixi-post-topic (community title content)
1834   "Post a topic to COMMUNITY."
1835   (unless (mixi-community-p community)
1836     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1837   (unless (stringp title)
1838     (signal 'wrong-type-argument (list 'stringp title)))
1839   (unless (stringp content)
1840     (signal 'wrong-type-argument (list 'stringp content)))
1841   (let ((fields `(("bbs_title" . ,title)
1842                   ("bbs_body" . ,content)
1843                   ("submit" . "main")))
1844         post-key)
1845     (with-mixi-post-form (mixi-post-topic-page community) fields
1846       (if (re-search-forward mixi-post-key-regexp nil t)
1847           (setq post-key (match-string 1))
1848         (mixi-post-error 'cannot-find-key community)))
1849     (setq fields `(("post_key" . ,post-key)
1850                    ("bbs_title" . ,title)
1851                    ("bbs_body" . ,content)
1852                    ("submit" . "confirm")))
1853     (with-mixi-post-form (mixi-post-topic-page community) fields
1854       (unless (re-search-forward mixi-post-succeed-regexp nil t)
1855         (mixi-post-error 'cannot-find-succeed community)))))
1856
1857 ;; Event object.
1858 (defvar mixi-event-cache (make-hash-table :test 'equal))
1859 (defun mixi-make-event (community id &optional comment-count time title owner
1860                                   date place detail limit members)
1861   "Return a event object."
1862   (mixi-make-cache (list (mixi-community-id community) id)
1863                    (cons 'mixi-event (vector nil community id comment-count
1864                                              time title owner date place
1865                                              detail limit members))
1866                    mixi-event-cache))
1867
1868 (defconst mixi-event-url-regexp
1869   "/view_event\\.pl\\?id=\\([0-9]+\\)\\(&comment_count=\\([0-9]+\\)\\)?&comm_id=\\([0-9]+\\)")
1870
1871 (defun mixi-make-event-from-url (url)
1872   "Return a event object from URL."
1873   (when (string-match mixi-event-url-regexp url)
1874     (let ((id (match-string 1 url))
1875           (comment-count (match-string 3 url))
1876           (community-id (match-string 4 url)))
1877       (mixi-make-event (mixi-make-community community-id) id comment-count))))
1878
1879 (defmacro mixi-event-p (event)
1880   `(eq (mixi-object-class ,event) 'mixi-event))
1881
1882 (defmacro mixi-event-page (event)
1883   `(concat "/view_event.pl?id=" (mixi-event-id ,event)
1884            "&comm_id=" (mixi-community-id (mixi-event-community ,event))))
1885
1886 (defconst mixi-event-community-regexp
1887   "<td width=\"?595\"? background=\"?http://img\\.mixi\\.jp/img/bg_w\\.gif\"?><b>\\[\\(.+\\)\\] ¥¤¥Ù¥ó¥È</b></td>")
1888 (defconst mixi-event-time-regexp
1889   "<td rowspan=\"?11\"? bgcolor=\"?#FFD8B0\"? align=\"?center\"? valign=\"?top\"? width=\"?110\"?>
1890 ?\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü<br>
1891 ?\\([0-9]+\\):\\([0-9]+\\)</td>")
1892 (defconst mixi-event-title-regexp
1893   "<td bgcolor=\"?#FFF4E0\"?\\( width=\"?410\"?\\)?>&nbsp;\\([^<]+\\)</td>")
1894 (defconst mixi-event-owner-regexp
1895   "<td bgcolor=\"?#FDF9F2\"?>&nbsp;<a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.*\\)</a>")
1896 (defconst mixi-event-owner-seceded-regexp
1897   "<td bgcolor=\"?#FDF9F2\"?>&nbsp;\\((mixi Âà²ñºÑ)\\)")
1898 (defconst mixi-event-date-regexp
1899   "<td bgcolor=\"?#FFFFFF\"? align=\"?center\"? nowrap>³«ºÅÆü»þ</td>
1900 <td bgcolor=\"?#FFFFFF\"?>
1901 &nbsp;\\(.+\\)
1902 </td>")
1903 (defconst mixi-event-place-regexp
1904   "<td bgcolor=\"?#FFFFFF\"? align=\"?center\"? nowrap>³«ºÅ¾ì½ê</td>
1905 <td bgcolor=\"?#FFFFFF\"?>
1906 &nbsp;\\(.+\\)
1907 </td>")
1908 (defconst mixi-event-detail-regexp
1909   "<td bgcolor=\"?#FFFFFF\"? align=\"?center\"? nowrap>¾ÜºÙ</td>
1910 <td bgcolor=\"?#FFFFFF\"?><table border=\"?0\"? cellspacing=\"?0\"? cellpadding=\"?5\"?><tr><td class=\"?h120\"?>\\(.+\\)</td></tr></table></td>")
1911 (defconst mixi-event-limit-regexp
1912   "<td bgcolor=\"?#FFFFFF\"? align=\"?center\"? nowrap>Ê罸´ü¸Â</td>
1913 ?<td bgcolor=\"?#FFFFFF\"?>&nbsp;\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü</td>")
1914 (defconst mixi-event-members-regexp
1915   "<td bgcolor=\"?#FFFFFF\"? align=\"?center\"? nowrap>»²²Ã¼Ô</td>
1916 <td bgcolor=\"?#FFFFFF\"?>
1917
1918 ?
1919 ?<table border=\"?0\"? cellspacing=\"?0\"? cellpadding=\"?0\"? width=\"?100%\"?>
1920 <tr>
1921
1922 ?<td>&nbsp;\\(.+\\)</td>")
1923
1924 (defun mixi-realize-event (event &optional page)
1925   "Realize a EVENT."
1926   ;; FIXME: Check a expiration of cache?
1927   (unless (mixi-object-realized-p event)
1928     (with-mixi-retrieve (or page (mixi-event-page event))
1929       (let ((case-fold-search t))
1930         (if (re-search-forward mixi-event-community-regexp nil t)
1931             (mixi-community-set-name (mixi-event-community event)
1932                                      (match-string 1))
1933           (mixi-realization-error 'cannot-find-community event))
1934         (if (re-search-forward mixi-event-time-regexp nil t)
1935             (mixi-event-set-time
1936              event (encode-time 0 (string-to-number (match-string 5))
1937                                 (string-to-number (match-string 4))
1938                                 (string-to-number (match-string 3))
1939                                 (string-to-number (match-string 2))
1940                                 (string-to-number (match-string 1))))
1941           (mixi-realization-error 'cannot-find-time event))
1942         (if (re-search-forward mixi-event-title-regexp nil t)
1943             (mixi-event-set-title event (match-string 2))
1944           (mixi-realization-error 'cannot-find-title event))
1945         (if (re-search-forward mixi-event-owner-regexp nil t)
1946             (mixi-event-set-owner event (mixi-make-friend (match-string 1)
1947                                                           (match-string 2)))
1948           (if (re-search-forward mixi-event-owner-seceded-regexp nil t)
1949               (mixi-event-set-owner event
1950                                     (mixi-make-friend nil (match-string 1)))
1951             (mixi-realization-error 'cannot-find-owner event)))
1952         (if (re-search-forward mixi-event-date-regexp nil t)
1953             (mixi-event-set-date event (match-string 1))
1954           (mixi-realization-error 'cannot-find-date event))
1955         (if (re-search-forward mixi-event-place-regexp nil t)
1956             (mixi-event-set-place event (match-string 1))
1957           (mixi-realization-error 'cannot-find-place event))
1958         (if (re-search-forward mixi-event-detail-regexp nil t)
1959             (mixi-event-set-detail event (match-string 1))
1960           (mixi-realization-error 'cannot-find-detail event))
1961         (when (re-search-forward mixi-event-limit-regexp nil t)
1962           (mixi-event-set-limit
1963            event (encode-time 0 0 0 (string-to-number (match-string 3))
1964                               (string-to-number (match-string 2))
1965                               (string-to-number (match-string 1)))))
1966         (if (re-search-forward mixi-event-members-regexp nil t)
1967             (mixi-event-set-members event (match-string 1))
1968           (mixi-realization-error 'cannot-find-members event))))
1969     (mixi-object-touch event)))
1970
1971 (defun mixi-event-community (event)
1972   "Return the community of EVENT."
1973   (unless (mixi-event-p event)
1974     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1975   (aref (cdr event) 1))
1976
1977 (defun mixi-event-id (event)
1978   "Return the id of EVENT."
1979   (unless (mixi-event-p event)
1980     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1981   (aref (cdr event) 2))
1982
1983 (defun mixi-event-comment-count (event)
1984   "Return the comment-count of EVENT."
1985   (unless (mixi-event-p event)
1986     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1987   (aref (cdr event) 3))
1988
1989 (defun mixi-event-time (event)
1990   "Return the time of EVENT."
1991   (unless (mixi-event-p event)
1992     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1993   (mixi-realize-event event)
1994   (aref (cdr event) 4))
1995
1996 (defun mixi-event-title (event)
1997   "Return the title of EVENT."
1998   (unless (mixi-event-p event)
1999     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2000   (mixi-realize-event event)
2001   (aref (cdr event) 5))
2002
2003 (defun mixi-event-owner (event)
2004   "Return the owner of EVENT."
2005   (unless (mixi-event-p event)
2006     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2007   (mixi-realize-event event)
2008   (aref (cdr event) 6))
2009
2010 (defun mixi-event-date (event)
2011   "Return the date of EVENT."
2012   (unless (mixi-event-p event)
2013     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2014   (mixi-realize-event event)
2015   (aref (cdr event) 7))
2016
2017 (defun mixi-event-place (event)
2018   "Return the place of EVENT."
2019   (unless (mixi-event-p event)
2020     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2021   (mixi-realize-event event)
2022   (aref (cdr event) 8))
2023
2024 (defun mixi-event-detail (event)
2025   "Return the detail of EVENT."
2026   (unless (mixi-event-p event)
2027     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2028   (mixi-realize-event event)
2029   (aref (cdr event) 9))
2030
2031 (defun mixi-event-limit (event)
2032   "Return the limit of EVENT."
2033   (unless (mixi-event-p event)
2034     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2035   (mixi-realize-event event)
2036   (aref (cdr event) 10))
2037
2038 (defun mixi-event-members (event)
2039   "Return the members of EVENT."
2040   (unless (mixi-event-p event)
2041     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2042   (mixi-realize-event event)
2043   (aref (cdr event) 11))
2044
2045 (defun mixi-event-set-comment-count (event comment-count)
2046   "Set the comment-count of EVENT."
2047   (unless (mixi-event-p event)
2048     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2049   (aset (cdr event) 3 comment-count))
2050
2051 (defun mixi-event-set-time (event time)
2052   "Set the time of EVENT."
2053   (unless (mixi-event-p event)
2054     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2055   (aset (cdr event) 4 time))
2056
2057 (defun mixi-event-set-title (event title)
2058   "Set the title of EVENT."
2059   (unless (mixi-event-p event)
2060     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2061   (aset (cdr event) 5 title))
2062
2063 (defun mixi-event-set-owner (event owner)
2064   "Set the owner of EVENT."
2065   (unless (mixi-event-p event)
2066     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2067   (unless (mixi-friend-p owner)
2068     (signal 'wrong-type-argument (list 'mixi-friend-p owner)))
2069   (aset (cdr event) 6 owner))
2070
2071 (defun mixi-event-set-date (event date)
2072   "Set the date of EVENT."
2073   (unless (mixi-event-p event)
2074     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2075   (aset (cdr event) 7 date))
2076
2077 (defun mixi-event-set-place (event place)
2078   "Set the place of EVENT."
2079   (unless (mixi-event-p event)
2080     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2081   (aset (cdr event) 8 place))
2082
2083 (defun mixi-event-set-detail (event detail)
2084   "Set the detail of EVENT."
2085   (unless (mixi-event-p event)
2086     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2087   (aset (cdr event) 9 detail))
2088
2089 (defun mixi-event-set-limit (event limit)
2090   "Set the limit of EVENT."
2091   (unless (mixi-event-p event)
2092     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2093   (aset (cdr event) 10 limit))
2094
2095 (defun mixi-event-set-members (event members)
2096   "Set the members of EVENT."
2097   (unless (mixi-event-p event)
2098     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2099   (aset (cdr event) 11 members))
2100
2101 ;; BBS object.
2102 (defconst mixi-bbs-list '(mixi-topic mixi-event))
2103
2104 (defmacro mixi-bbs-p (bbs)
2105   `(memq (mixi-object-class ,bbs) mixi-bbs-list))
2106
2107 (defun mixi-bbs-community (bbs)
2108   "Return the community of BBS."
2109   (unless (mixi-bbs-p bbs)
2110     (signal 'wrong-type-argument (list 'mixi-bbs-p bbs)))
2111   (let ((func (intern (concat mixi-object-prefix
2112                               (mixi-object-name bbs) "-community"))))
2113     (funcall func bbs)))
2114
2115 (defun mixi-bbs-comment-count (bbs)
2116   "Return the comment-count of BBS."
2117   (unless (mixi-bbs-p bbs)
2118     (signal 'wrong-type-argument (list 'mixi-bbs-p bbs)))
2119   (let ((func (intern (concat mixi-object-prefix
2120                               (mixi-object-name bbs) "-comment-count"))))
2121     (funcall func bbs)))
2122
2123 (defun mixi-bbs-set-comment-count (bbs count)
2124   "Set the comment-count of BBS."
2125   (unless (mixi-bbs-p bbs)
2126     (signal 'wrong-type-argument (list 'mixi-bbs-p bbs)))
2127   (let ((func (intern (concat mixi-object-prefix
2128                               (mixi-object-name bbs) "-set-comment-count"))))
2129     (funcall func bbs count)))
2130
2131 (defalias 'mixi-bbs-id 'mixi-object-id)
2132 (defalias 'mixi-bbs-time 'mixi-object-time)
2133 (defalias 'mixi-bbs-title 'mixi-object-title)
2134 (defalias 'mixi-bbs-owner 'mixi-object-owner)
2135 (defalias 'mixi-bbs-content 'mixi-object-content)
2136
2137 (defmacro mixi-bbs-list-page (community)
2138   `(concat "/list_bbs.pl?page=%d"
2139            "&id=" (mixi-community-id ,community)))
2140
2141 (defconst mixi-bbs-list-regexp
2142   "<a href=view_\\(bbs\\|event\\)\\.pl\\?id=\\([0-9]+\\)")
2143
2144 ;;;###autoload
2145 (defun mixi-get-bbses (community &optional range)
2146   "Get bbese of COMMUNITY."
2147   (unless (mixi-community-p community)
2148     (signal 'wrong-type-argument (list 'mixi-community-p community)))
2149   (let ((items (mixi-get-matched-items (mixi-bbs-list-page community)
2150                                        mixi-bbs-list-regexp
2151                                        range)))
2152     (mapcar (lambda (item)
2153               (let ((name (nth 0 item)))
2154                 (when (string= name "bbs")
2155                   (setq name "topic"))
2156                 (let ((func (intern (concat "mixi-make-" name))))
2157                   (funcall func community (nth 1 item)))))
2158             items)))
2159
2160 (defmacro mixi-new-bbs-list-page ()
2161   `(concat "/new_bbs.pl?page=%d"))
2162
2163 (defconst mixi-new-bbs-list-regexp
2164   "<a href=\"view_\\(bbs\\|event\\)\\.pl\\?id=\\([0-9]+\\)&comment_count=\\([0-9]+\\)&comm_id=\\([0-9]+\\)\" class=\"new_link\">")
2165
2166 ;;;###autoload
2167 (defun mixi-get-new-bbses (&optional range)
2168   "Get new topics."
2169   (let ((items (mixi-get-matched-items (mixi-new-bbs-list-page)
2170                                        mixi-new-bbs-list-regexp
2171                                        range)))
2172     (delq nil
2173           (mapcar (lambda (item)
2174                     (let ((name (nth 0 item)))
2175                       (when (string= name "bbs")
2176                         (setq name "topic"))
2177                       (let* ((func (intern (concat "mixi-make-" name)))
2178                              (bbs (funcall func
2179                                            (mixi-make-community (nth 3 item))
2180                                            (nth 1 item)))
2181                              (comment-count (mixi-bbs-comment-count bbs))
2182                              (count (string-to-number (nth 2 item))))
2183                         (when (or (null comment-count)
2184                                   (< comment-count count))
2185                           (mixi-bbs-set-comment-count bbs count)
2186                           bbs))))
2187                   items))))
2188  
2189 (defmacro mixi-search-bbs-list-page (keyword)
2190   `(concat "/search_topic.pl?page=%d&type=top&submit=search"
2191            "&keyword=" (mixi-url-encode-and-quote-percent-string ,keyword)
2192            "&community_id=0&category_id=0"))
2193
2194 (defconst mixi-search-bbs-list-regexp
2195   "<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>")
2196
2197 ;; FIXME: Support community and category.
2198 ;;;###autoload
2199 (defun mixi-search-bbses (keyword &optional range)
2200   (let ((items (mixi-get-matched-items (mixi-search-bbs-list-page keyword)
2201                                        mixi-search-bbs-list-regexp
2202                                        range)))
2203     (mapcar (lambda (item)
2204               (let ((name (nth 0 item)))
2205                 (when (string= name "bbs")
2206                   (setq name "topic"))
2207                 (let ((func (intern (concat "mixi-make-" name))))
2208                   (funcall func (mixi-make-community (nth 2 item))
2209                            (nth 1 item)))))
2210             items)))
2211
2212 ;; Parent object.
2213 (defmacro mixi-parent-p (object)
2214   `(or (eq (mixi-object-class ,object) 'mixi-diary)
2215        (mixi-bbs-p ,object)))
2216
2217 (defun mixi-realize-parent (parent &optional page)
2218   "Realize a PARENT."
2219   (unless (mixi-parent-p parent)
2220     (signal 'wrong-type-argument (list 'mixi-parent-p parent)))
2221   (let ((func (intern (concat mixi-object-prefix "realize-"
2222                               (mixi-object-name parent)))))
2223     (funcall func parent page)))
2224
2225 ;; Comment object.
2226 (defun mixi-make-comment (parent owner time content)
2227   "Return a comment object."
2228   (cons 'mixi-comment (vector parent owner time content)))
2229
2230 (defmacro mixi-comment-p (comment)
2231   `(eq (mixi-object-class ,comment) 'mixi-comment))
2232
2233 (defun mixi-comment-parent (comment)
2234   "Return the parent of COMMENT."
2235   (unless (mixi-comment-p comment)
2236     (signal 'wrong-type-argument (list 'mixi-comment-p comment)))
2237   (aref (cdr comment) 0))
2238
2239 (defun mixi-comment-owner (comment)
2240   "Return the owner of COMMENT."
2241   (unless (mixi-comment-p comment)
2242     (signal 'wrong-type-argument (list 'mixi-comment-p comment)))
2243   (aref (cdr comment) 1))
2244
2245 (defun mixi-comment-time (comment)
2246   "Return the time of COMMENT."
2247   (unless (mixi-comment-p comment)
2248     (signal 'wrong-type-argument (list 'mixi-comment-p comment)))
2249   (aref (cdr comment) 2))
2250
2251 (defun mixi-comment-content (comment)
2252   "Return the content of COMMENT."
2253   (unless (mixi-comment-p comment)
2254     (signal 'wrong-type-argument (list 'mixi-comment-p comment)))
2255   (aref (cdr comment) 3))
2256
2257 (defun mixi-diary-comment-list-page (diary)
2258   (concat "/view_diary.pl?full=1"
2259           "&id=" (mixi-diary-id diary)
2260           "&owner_id=" (mixi-friend-id (mixi-diary-owner diary))))
2261
2262 ;; FIXME: Split regexp to time, owner(id and nick) and contents.
2263 (defconst mixi-diary-comment-list-regexp
2264 "<td rowspan=\"2\" align=\"center\" width=\"95\" bgcolor=\"#f2ddb7\" nowrap>
2265 \\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü<br>\\([0-9]+\\):\\([0-9]+\\)\\(<br>
2266 <input type=checkbox name=comment_id value=\".+\">
2267 \\|\\)
2268 </td>
2269 <td ALIGN=center BGCOLOR=#FDF9F2 WIDTH=430>
2270 <table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"410\">
2271 <tr>
2272 \\(<td>\\)
2273 <a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.*\\)</a>
2274
2275 \\(<font color=\"#f2ddb7\">|</font> <a href=[^>]+>ºï½ü</a>
2276
2277 \\|\\)</td>
2278 </tr>
2279 </table>
2280 </td>
2281 </tr>
2282 <!-- [^ ]+ : start -->
2283 <tr>
2284 <td bgcolor=\"#ffffff\">
2285 <table BORDER=0 CELLSPACING=0 CELLPADDING=[35] WIDTH=410>
2286 <tr>
2287 <td CLASS=h12>
2288 \\(.+\\)
2289 </td></tr></table>")
2290
2291 (defun mixi-topic-comment-list-page (topic)
2292   (concat "/view_bbs.pl?page=all"
2293           "&id=" (mixi-topic-id topic)
2294           "&comm_id=" (mixi-community-id (mixi-topic-community topic))))
2295
2296 ;; FIXME: Split regexp to time, owner(id and nick) and contents.
2297 (defconst mixi-topic-comment-list-regexp
2298   "<tr valign=\"top\">
2299 <td rowspan=\"2\" width=\"110\" bgcolor=\"#f2ddb7\" align=\"center\" nowrap>
2300 \\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü<br>
2301 \\([0-9]+\\):\\([0-9]+\\)<br>
2302 \\(<input type=\"checkbox\" name=\"comment_id\" value=\".+\">
2303 \\|\\)</td>
2304 <td bgcolor=\"#fdf9f2\">&nbsp;<font color=\"#f8a448\">
2305 <b>[^<]+</b>:</font>&nbsp;
2306 \\(
2307 \\|\\) *<a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.*\\)</a>
2308
2309 ?\\(
2310
2311 \\|<font color=\"#f2ddb7\">|&nbsp;</font><a href=\"delete_bbs_comment\\.pl\\?id=[0-9]+&comm_id=[0-9]+&comment_id=[0-9]+\">ºï½ü</a>
2312 \\|\\)</td>
2313 </tr>
2314 <tr>
2315 <td bgcolor=\"#ffffff\" align=\"center\">
2316 <table border=\"0\" cellspacing=\"0\" cellpadding=\"5\" width=\"500\">
2317 <tr>
2318 <td class=\"h120\">
2319
2320 \\(.+\\)
2321 </td>
2322 </tr>
2323 </table>
2324 </td>
2325 </tr>")
2326
2327 (defun mixi-event-comment-list-page (event)
2328   (concat "/view_event.pl?page=all"
2329           "&id=" (mixi-event-id event)
2330           "&comm_id=" (mixi-community-id (mixi-event-community event))))
2331
2332 ;; FIXME: Split regexp to time, owner(id and nick) and contents.
2333 (defconst mixi-event-comment-list-regexp
2334   "<tr>
2335 <td ROWSPAN=2 ALIGN=center BGCOLOR=#F2DDB7 WIDTH=110>
2336 \\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü<br>
2337 \\([0-9]+\\):\\([0-9]+\\)<br>
2338 \\(</td>\\)
2339 \\(<td BGCOLOR=#FDF9F2>\\)
2340 <font COLOR=#F8A448><b>[^<]+</b> :</font>
2341 <a HREF=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.*\\)</a>
2342
2343 \\(<font COLOR=#F2DDB7>|</font>
2344 <a href=\"delete_bbs_comment\\.pl\\?id=[0-9]+&comm_id=[0-9]+&comment_id=[0-9]+&type=event\">ºï½ü</a>
2345
2346 \\|\\)</td>
2347 </tr>
2348 <tr>
2349 <td ALIGN=center BGCOLOR=#FFFFFF>
2350 <table BORDER=0 CELLSPACING=0 CELLPADDING=5 WIDTH=500>
2351 <tr><td CLASS=h120>\\(.+\\)</td></tr>
2352 </table>
2353 </td>
2354 </tr>")
2355
2356 ;;;###autoload
2357 (defun mixi-get-comments (parent &optional range)
2358   "Get comments of PARENT."
2359   (unless (mixi-parent-p parent)
2360     (signal 'wrong-type-argument (list 'mixi-parent-p parent)))
2361   (let* ((name (mixi-object-name parent))
2362          (list-page (intern (concat mixi-object-prefix name
2363                                     "-comment-list-page")))
2364          (regexp (eval (intern (concat mixi-object-prefix name
2365                                        "-comment-list-regexp"))))
2366          (page (funcall list-page parent)))
2367     (unless (mixi-object-realized-p parent)
2368       (mixi-realize-parent parent page)
2369       (setq page nil))
2370     (let ((items (mixi-get-matched-items page regexp range t)))
2371       (mapcar (lambda (item)
2372                 (mixi-make-comment parent (mixi-make-friend
2373                                            (nth 7 item) (nth 8 item))
2374                                    (encode-time
2375                                     0
2376                                     (string-to-number (nth 4 item))
2377                                     (string-to-number (nth 3 item))
2378                                     (string-to-number (nth 2 item))
2379                                     (string-to-number (nth 1 item))
2380                                     (string-to-number (nth 0 item)))
2381                                    (nth 10 item)))
2382               items))))
2383
2384 (defmacro mixi-new-comment-list-page ()
2385   `(concat "/new_comment.pl?page=%d"))
2386
2387 (defconst mixi-new-comment-list-regexp
2388   "<a href=\"view_diary\\.pl\\?id=\\([0-9]+\\)&owner_id=\\([0-9]+\\)&comment_count=\\([0-9]+\\)\" class=\"new_link\">")
2389
2390 ;;;###autoload
2391 (defun mixi-get-new-comments (&optional range)
2392   "Get new comments."
2393   (let ((items (mixi-get-matched-items (mixi-new-comment-list-page)
2394                                        mixi-new-comment-list-regexp
2395                                        range)))
2396     (delq nil
2397           (mapcar (lambda (item)
2398                     (let* ((diary (mixi-make-diary
2399                                    (mixi-make-friend (nth 1 item))
2400                                    (nth 0 item)))
2401                            (comment-count (mixi-diary-comment-count diary))
2402                            (count (string-to-number (nth 2 item))))
2403                       (when (or (null comment-count)
2404                                 (< comment-count count))
2405                         (mixi-diary-set-comment-count diary count)
2406                         diary)))
2407                   items))))
2408
2409 (defun mixi-post-diary-comment-page (diary)
2410   (concat "/add_comment.pl?&diary_id=" (mixi-diary-id diary)))
2411
2412 (defun mixi-post-topic-comment-page (topic)
2413   (concat "/add_bbs_comment.pl?id=" (mixi-topic-id topic)
2414           "&comm_id=" (mixi-community-id (mixi-topic-community topic))))
2415
2416 (defun mixi-post-event-comment-page (event)
2417   (concat "/add_event_comment.pl?id=" (mixi-event-id event)
2418           "&comm_id=" (mixi-community-id (mixi-event-community event))))
2419
2420 ;; FIXME: Support photos.
2421 ;;;###autoload
2422 (defun mixi-post-comment (parent content)
2423   "Post a comment to PARENT."
2424   (unless (mixi-object-p parent)
2425     (signal 'wrong-type-argument (list 'mixi-object-p parent)))
2426   (unless (stringp content)
2427     (signal 'wrong-type-argument (list 'stringp content)))
2428   (let* ((name (mixi-object-name parent))
2429          (page (intern (concat mixi-object-prefix "post-" name
2430                                "-comment-page")))
2431          fields post-key)
2432     (if (mixi-diary-p parent)
2433         (setq fields
2434               `(("owner_id" . ,(mixi-friend-id (mixi-diary-owner parent)))
2435                 ("comment_body" . ,content)))
2436       (setq fields `(("comment" . ,content))))
2437     (with-mixi-post-form (funcall page parent) fields
2438       (if (re-search-forward mixi-post-key-regexp nil t)
2439           (setq post-key (match-string 1))
2440         (mixi-post-error 'cannot-find-key parent)))
2441     (if (mixi-diary-p parent)
2442         (setq fields
2443               `(("post_key" . ,post-key)
2444                 ("owner_id" . ,(mixi-friend-id (mixi-diary-owner parent)))
2445                 ("comment_body" . ,content)
2446                 ("submit" . "confirm")))
2447       (setq fields `(("post_key" . ,post-key)
2448                      ("comment" . ,content)
2449                      ("submit" . "confirm"))))
2450     (with-mixi-post-form (funcall page parent) fields
2451       (unless (re-search-forward mixi-post-succeed-regexp nil t)
2452         (mixi-post-error 'cannot-find-succeed parent)))))
2453
2454 ;; Message object.
2455 (defconst mixi-message-box-list '(inbox outbox savebox thrash)) ; thrash?
2456
2457 (defmacro mixi-message-box-p (box)
2458   `(memq ,box mixi-message-box-list))
2459
2460 (defun mixi-message-box-name (box)
2461   "Return the name of BOX."
2462   (unless (mixi-message-box-p box)
2463     (signal 'wrong-type-argument (list 'mixi-message-box-p box)))
2464   (symbol-name box))
2465
2466 (defvar mixi-message-cache (make-hash-table :test 'equal))
2467 (defun mixi-make-message (id box &optional owner title time content)
2468   "Return a message object."
2469   (mixi-make-cache (list id box)
2470                    (cons 'mixi-message (vector nil id box owner title time
2471                                                content))
2472                    mixi-message-cache))
2473
2474 (defconst mixi-message-url-regexp
2475   "/view_message\\.pl\\?id=\\([a-z0-9]+\\)&box=\\([a-z]+\\)")
2476
2477 (defun mixi-make-message-from-url (url)
2478   "Return a message object from URL."
2479   (when (string-match mixi-message-url-regexp url)
2480     (let ((id (match-string 1 url))
2481           (box (match-string 2 url)))
2482       (mixi-make-message id box))))
2483
2484 (defmacro mixi-message-p (message)
2485   `(eq (mixi-object-class ,message) 'mixi-message))
2486
2487 (defmacro mixi-message-page (message)
2488   `(concat "/view_message.pl?id=" (mixi-message-id ,message)
2489            "&box=" (mixi-message-box ,message)))
2490
2491 (defconst mixi-message-owner-regexp
2492   "<font COLOR=#996600>\\(º¹½Ð¿Í\\|°¸&nbsp;Àè\\)</font>&nbsp;:&nbsp;<a HREF=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.*\\)\\(</a>\\|</td>\\)")
2493 (defconst mixi-message-time-regexp
2494 "<font COLOR=#996600>Æü\\(¡¡\\|&nbsp;\\)ÉÕ</font>&nbsp;:&nbsp;\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü \\([0-9]+\\)»þ\\([0-9]+\\)ʬ&nbsp;&nbsp;")
2495 (defconst mixi-message-title-regexp
2496 "<font COLOR=#996600>·ï\\(¡¡\\|&nbsp;\\)̾</font>&nbsp;:&nbsp;\\(.*\\)\n?</td>")
2497 (defconst mixi-message-content-regexp
2498   "<tr><td CLASS=h120>\\(.*\\)</td></tr>")
2499
2500 (defun mixi-realize-message (message)
2501   "Realize a MESSAGE."
2502   (unless (mixi-object-realized-p message)
2503     (with-mixi-retrieve (mixi-message-page message)
2504       (if (re-search-forward mixi-message-owner-regexp nil t)
2505           (mixi-message-set-owner message
2506                                   (mixi-make-friend (match-string 2)
2507                                                     (match-string 3)))
2508         (mixi-realization-error 'cannot-find-owner message))
2509       (if (re-search-forward mixi-message-time-regexp nil t)
2510           (mixi-message-set-time
2511            message (encode-time 0 (string-to-number (match-string 6))
2512                                 (string-to-number (match-string 5))
2513                                 (string-to-number (match-string 4))
2514                                 (string-to-number (match-string 3))
2515                                 (string-to-number (match-string 2))))
2516         (mixi-realization-error 'cannot-find-time message))
2517       (if (re-search-forward mixi-message-title-regexp nil t)
2518           (mixi-message-set-title message (match-string 2))
2519         (mixi-realization-error 'cannot-find-title message))
2520       (if (re-search-forward mixi-message-content-regexp nil t)
2521           (mixi-message-set-content message (match-string 1))
2522         (mixi-realization-error 'cannot-find-content message)))
2523     (mixi-object-touch message)))
2524
2525 (defun mixi-message-id (message)
2526   "Return the id of MESSAGE."
2527   (unless (mixi-message-p message)
2528     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2529   (aref (cdr message) 1))
2530
2531 (defun mixi-message-box (message)
2532   "Return the box of MESSAGE."
2533   (unless (mixi-message-p message)
2534     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2535   (aref (cdr message) 2))
2536
2537 (defun mixi-message-owner (message)
2538   "Return the owner of MESSAGE."
2539   (unless (mixi-message-p message)
2540     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2541   (mixi-realize-message message)
2542   (aref (cdr message) 3))
2543
2544 (defun mixi-message-title (message)
2545   "Return the title of MESSAGE."
2546   (unless (mixi-message-p message)
2547     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2548   (mixi-realize-message message)
2549   (aref (cdr message) 4))
2550
2551 (defun mixi-message-time (message)
2552   "Return the date of MESSAGE."
2553   (unless (mixi-message-p message)
2554     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2555   (mixi-realize-message message)
2556   (aref (cdr message) 5))
2557
2558 (defun mixi-message-content (message)
2559   "Return the content of MESSAGE."
2560   (unless (mixi-message-p message)
2561     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2562   (mixi-realize-message message)
2563   (aref (cdr message) 6))
2564
2565 (defun mixi-message-set-owner (message owner)
2566   "Set the owner of MESSAGE."
2567   (unless (mixi-message-p message)
2568     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2569   (aset (cdr message) 3 owner))
2570
2571 (defun mixi-message-set-title (message title)
2572   "Set the title of MESSAGE."
2573   (unless (mixi-message-p message)
2574     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2575   (aset (cdr message) 4 title))
2576
2577 (defun mixi-message-set-time (message time)
2578   "Set the date of MESSAGE."
2579   (unless (mixi-message-p message)
2580     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2581   (aset (cdr message) 5 time))
2582
2583 (defun mixi-message-set-content (message content)
2584   "Set the content of MESSAGE."
2585   (unless (mixi-message-p message)
2586     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2587   (aset (cdr message) 6 content))
2588
2589 (defmacro mixi-message-list-page (&optional box)
2590   `(concat "/list_message.pl?page=%d"
2591            (when ,box (concat "&box=" ,box))))
2592
2593 (defconst mixi-message-list-regexp
2594   "<td><a HREF=\"view_message\\.pl\\?id=\\(.+\\)&box=\\(.+\\)\">")
2595
2596 ;;;###autoload
2597 (defun mixi-get-messages (&rest box-or-range)
2598   "Get messages of BOX."
2599   (when (> (length box-or-range) 2)
2600     (signal 'wrong-number-of-arguments
2601             (list 'mixi-get-messages (length box-or-range))))
2602   (let ((box (nth 0 box-or-range))
2603         (range (nth 1 box-or-range)))
2604     (when (or (not (mixi-message-box-p box))
2605               (mixi-message-box-p range))
2606       (setq box (nth 1 box-or-range))
2607       (setq range (nth 0 box-or-range)))
2608     (let ((items (mixi-get-matched-items
2609                   (mixi-message-list-page
2610                    (when box (mixi-message-box-name box)))
2611                   mixi-message-list-regexp
2612                   range)))
2613       (mapcar (lambda (item)
2614                 (mixi-make-message (nth 0 item) (nth 1 item)))
2615               items))))
2616
2617 (defmacro mixi-post-message-page (friend)
2618   `(concat "/send_message.pl?id=" (mixi-friend-id friend)))
2619
2620 (defconst mixi-post-message-key-regexp
2621   "<input name=post_key type=hidden value=\\([a-z0-9]+\\)>")
2622
2623 (defconst mixi-post-message-succeed-regexp
2624   "<b>Á÷¿®´°Î»</b>¤·¤Þ¤·¤¿¡£")
2625
2626 ;;;###autoload
2627 (defun mixi-post-message (friend title content)
2628   "Post a message to FRIEND."
2629   (unless (mixi-friend-p friend)
2630     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
2631   (unless (stringp title)
2632     (signal 'wrong-type-argument (list 'stringp title)))
2633   (unless (stringp content)
2634     (signal 'wrong-type-argument (list 'stringp content)))
2635   (let ((fields `(("subject" . ,title)
2636                   ("body" . ,content)
2637                   ("submit" . "main")))
2638         post-key)
2639     (with-mixi-post-form (mixi-post-message-page friend) fields
2640       (if (re-search-forward mixi-post-message-key-regexp nil t)
2641           (setq post-key (match-string 1))
2642         (mixi-post-error 'cannot-find-key friend)))
2643     (setq fields `(("post_key" . ,post-key)
2644                    ("subject" . ,title)
2645                    ("body" . ,content)
2646                    ("yes" . "¡¡Á÷¡¡¿®¡¡")
2647                    ("submit" . "confirm")))
2648     (with-mixi-post-form (mixi-post-message-page friend) fields
2649       (unless (re-search-forward mixi-post-message-succeed-regexp nil t)
2650         (mixi-post-error 'cannot-find-succeed friend)))))
2651
2652 ;; Introduction object.
2653 (defun mixi-make-introduction (parent owner content)
2654   "Return a introduction object."
2655   (cons 'mixi-introduction (vector parent owner content)))
2656
2657 (defmacro mixi-introduction-p (introduction)
2658   `(eq (mixi-object-class ,introduction) 'mixi-introduction))
2659
2660 (defun mixi-introduction-parent (introduction)
2661   "Return the parent of INTRODUCTION."
2662   (unless (mixi-introduction-p introduction)
2663     (signal 'wrong-type-argument (list 'mixi-introduction-p introduction)))
2664   (aref (cdr introduction) 0))
2665
2666 (defun mixi-introduction-owner (introduction)
2667   "Return the owner of INTRODUCTION."
2668   (unless (mixi-introduction-p introduction)
2669     (signal 'wrong-type-argument (list 'mixi-introduction-p introduction)))
2670   (aref (cdr introduction) 1))
2671
2672 (defun mixi-introduction-content (introduction)
2673   "Return the content of INTRODUCTION."
2674   (unless (mixi-introduction-p introduction)
2675     (signal 'wrong-type-argument (list 'mixi-introduction-p introduction)))
2676   (aref (cdr introduction) 3))
2677
2678 (defmacro mixi-introduction-list-page (&optional friend)
2679   `(concat "/show_intro.pl?page=%d"
2680            (when ,friend (concat "&id=" (mixi-friend-id ,friend)))))
2681
2682 (defconst mixi-introduction-list-regexp
2683   "<tr bgcolor=#FFFFFF>
2684 <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>
2685 \\(.*\\)</td></a>
2686
2687 <td WIDTH=480>
2688 \\(´Ø·¸¡§.+<br>
2689
2690
2691 \\(\\(.\\|\n<br>\\)+\\)\\|
2692 \\(\\(.\\|\n<br>\\)+\\)\\)
2693
2694
2695
2696
2697 </td>
2698 </tr>")
2699 (defconst mixi-my-introduction-list-regexp
2700   "<tr bgcolor=#FFFFFF>
2701 <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>
2702 \\(.*\\)</td></a>
2703
2704
2705 <td WIDTH=480>
2706 \\(´Ø·¸¡§.+<br>
2707
2708
2709 \\(\\(.\\|\n<br>\\)+\\)\\|
2710 \\(\\(.\\|\n<br>\\)+\\)\\)
2711
2712
2713 <br>
2714 <a href=\"edit_intro\\.pl\\?id=\\1&type=edit\">¤³¤Îͧ¿Í¤ò¾Ò²ð¤¹¤ë</a>
2715
2716
2717 <BR>
2718 <a href=\"delete_intro\\.pl\\?id=\\1\">ºï½ü</a>
2719
2720 </td>
2721 </tr>")
2722
2723 ;;;###autoload
2724 (defun mixi-get-introductions (&rest friend-or-range)
2725   "Get introductions of FRIEND."
2726   (when (> (length friend-or-range) 2)
2727     (signal 'wrong-number-of-arguments
2728             (list 'mixi-get-introduction (length friend-or-range))))
2729   (let ((friend (nth 0 friend-or-range))
2730         (range (nth 1 friend-or-range)))
2731     (when (or (not (mixi-friend-p friend)) (mixi-friend-p range))
2732       (setq friend (nth 1 friend-or-range))
2733       (setq range (nth 0 friend-or-range)))
2734     (unless (or (null friend) (mixi-friend-p friend))
2735       (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
2736     (let* ((regexp (if friend mixi-introduction-list-regexp
2737                      mixi-my-introduction-list-regexp))
2738            (items (mixi-get-matched-items (mixi-introduction-list-page friend)
2739                                           regexp
2740                                           range)))
2741       (mapcar (lambda (item)
2742                 (mixi-make-introduction (or friend (mixi-make-me))
2743                                         (mixi-make-friend (nth 0 item)
2744                                                           (nth 1 item))
2745                                         (nth 2 item)))
2746               items))))
2747
2748 ;; News object.
2749 (defvar mixi-news-cache (make-hash-table :test 'equal))
2750 (defun mixi-make-news (media-id id &optional media time title content)
2751   "Return a news object."
2752   (mixi-make-cache (list media-id id)
2753                    (cons 'mixi-news (vector nil media-id id media time title
2754                                             content))
2755                    mixi-news-cache))
2756
2757 (defconst mixi-news-url-regexp
2758   "/view_news\\.pl\\?id=\\([0-9]+\\)&media_id=\\([0-9]+\\)")
2759
2760 (defun mixi-make-news-from-url (url)
2761   "Return a news object from URL."
2762   (when (string-match mixi-news-url-regexp url)
2763     (let ((id (match-string 1 url))
2764           (media-id (match-string 2 url)))
2765       (mixi-make-news media-id id))))
2766
2767 (defmacro mixi-news-p (news)
2768   `(eq (mixi-object-class ,news) 'mixi-news))
2769
2770 (defmacro mixi-news-page (news)
2771   `(concat "http://news.mixi.jp/view_news.pl?id=" (mixi-news-id ,news)
2772            "&media_id=" (mixi-news-media-id ,news)))
2773
2774
2775 (defconst mixi-news-finished-regexp
2776   "<td ALIGN=center background=http://img\\.mixi\\.jp/img/bg_line\\.gif> ¿½¤·Ìõ¤´¤¶¤¤¤Þ¤»¤ó¤¬¡¢¤³¤Î¥Ë¥å¡¼¥¹¤Ï·ÇºÜ½ªÎ»¤·¤Þ¤·¤¿¡£</td>")
2777 (defconst mixi-news-title-regexp
2778   "<td HEIGHT=\"46\" STYLE=\"font-weight: bold;font-size: 14px;\" CLASS=\"h130\">\\(.+\\)\\(
2779 \\)?</td>")
2780 (defconst mixi-news-media-time-regexp
2781   "<td COLSPAN=\"2\" ALIGN=\"right\">(\\(.+\\)&nbsp;-&nbsp;\\([0-9]+\\)·î\\([0-9]+\\)Æü \\([0-9]+\\):\\([0-9]+\\))</td></tr>")
2782 (defconst mixi-news-content-regexp
2783   "<td CLASS=\"h150\">
2784
2785 \\(.+\\)
2786 <br>
2787 \\(.*
2788 \\)?
2789
2790 \\(</td>\\|<br>\\)")
2791
2792 (defun mixi-realize-news (news)
2793   "Realize a NEWS."
2794   ;; FIXME: Check a expiration of cache?
2795   (unless (mixi-object-realized-p news)
2796     (with-mixi-retrieve (mixi-news-page news)
2797       (if (re-search-forward mixi-news-finished-regexp nil t)
2798           (mixi-news-set-content news (match-string 0))
2799         (if (re-search-forward mixi-news-title-regexp nil t)
2800             (mixi-news-set-title news (match-string 1))
2801           (mixi-realization-error 'cannot-find-title news))
2802         (if (re-search-forward mixi-news-media-time-regexp nil t)
2803             (progn
2804               (mixi-news-set-media news (match-string 1))
2805               (let ((year (nth 5 (decode-time (current-time))))
2806                     (month (nth 4 (decode-time (current-time))))
2807                     (month-of-item (string-to-number (match-string 2))))
2808                 (when (> month-of-item month)
2809                   (decf year))
2810                 (mixi-news-set-time
2811                  news (encode-time 0 (string-to-number (match-string 5))
2812                                    (string-to-number (match-string 4))
2813                                    (string-to-number (match-string 3))
2814                                    month year))))
2815           (mixi-realization-error 'cannot-find-media-time news))
2816         (if (re-search-forward mixi-news-content-regexp nil t)
2817             (mixi-news-set-content news (match-string 1))
2818           (mixi-realization-error 'cannot-find-content news))))
2819     (mixi-object-touch news)))
2820
2821 (defun mixi-news-media-id (news)
2822   "Return the media-id of NEWS."
2823   (unless (mixi-news-p news)
2824     (signal 'wrong-type-argument (list 'mixi-news-p news)))
2825   (aref (cdr news) 1))
2826
2827 (defun mixi-news-id (news)
2828   "Return the id of NEWS."
2829   (unless (mixi-news-p news)
2830     (signal 'wrong-type-argument (list 'mixi-news-p news)))
2831   (aref (cdr news) 2))
2832
2833 (defun mixi-news-media (news)
2834   "Return the media of NEWS."
2835   (unless (mixi-news-p news)
2836     (signal 'wrong-type-argument (list 'mixi-news-p news)))
2837   (unless (aref (cdr news) 3)
2838     (mixi-realize-news news))
2839   (aref (cdr news) 3))
2840
2841 (defun mixi-news-time (news)
2842   "Return the time of NEWS."
2843   (unless (mixi-news-p news)
2844     (signal 'wrong-type-argument (list 'mixi-news-p news)))
2845   (unless (aref (cdr news) 4)
2846     (mixi-realize-news news))
2847   (aref (cdr news) 4))
2848
2849 (defun mixi-news-title (news)
2850   "Return the title of NEWS."
2851   (unless (mixi-news-p news)
2852     (signal 'wrong-type-argument (list 'mixi-news-p news)))
2853   (unless (aref (cdr news) 5)
2854     (mixi-realize-news news))
2855   (aref (cdr news) 5))
2856
2857 (defun mixi-news-content (news)
2858   "Return the content of NEWS."
2859   (unless (mixi-news-p news)
2860     (signal 'wrong-type-argument (list 'mixi-news-p news)))
2861   (mixi-realize-news news)
2862   (aref (cdr news) 6))
2863
2864 (defun mixi-news-set-media (news media)
2865   "Set the media of NEWS."
2866   (unless (mixi-news-p news)
2867     (signal 'wrong-type-argument (list 'mixi-news-p news)))
2868   (aset (cdr news) 3 media))
2869
2870 (defun mixi-news-set-time (news time)
2871   "Set the time of NEWS."
2872   (unless (mixi-news-p news)
2873     (signal 'wrong-type-argument (list 'mixi-news-p news)))
2874   (aset (cdr news) 4 time))
2875
2876 (defun mixi-news-set-title (news title)
2877   "Set the title of NEWS."
2878   (unless (mixi-news-p news)
2879     (signal 'wrong-type-argument (list 'mixi-news-p news)))
2880   (aset (cdr news) 5 title))
2881
2882 (defun mixi-news-set-content (news content)
2883   "Set the content of NEWS."
2884   (unless (mixi-news-p news)
2885     (signal 'wrong-type-argument (list 'mixi-news-p news)))
2886   (aset (cdr news) 6 content))
2887
2888 (defconst mixi-news-category-list '(domestic politics economy area abroad
2889                                              sports entertainment IT))
2890
2891 (defmacro mixi-news-category-p (category)
2892   `(memq ,category mixi-news-category-list))
2893
2894 (defun mixi-news-category-id (category)
2895   "Return the id of CATEGORY."
2896   (unless (mixi-news-category-p category)
2897     (signal 'wrong-type-argument (list 'mixi-news-category-p category)))
2898   (number-to-string
2899    (1+ (- (length mixi-news-category-list)
2900           (length (memq category mixi-news-category-list))))))
2901
2902 (defconst mixi-news-sort-list '(newest pickup))
2903
2904 (defmacro mixi-news-sort-p (sort)
2905   `(memq ,sort mixi-news-sort-list))
2906
2907 (defun mixi-news-sort-id (sort)
2908   "Return the id of SORT."
2909   (unless (mixi-news-sort-p sort)
2910     (signal 'wrong-type-argument (list 'mixi-news-sort-p sort)))
2911   (number-to-string
2912    (- (length mixi-news-sort-list)
2913       (length (memq sort mixi-news-sort-list)))))
2914
2915 (defmacro mixi-news-list-page (category sort)
2916   `(concat "http://news.mixi.jp/list_news_category.pl?page=%d"
2917            "&sort=" (mixi-news-sort-id ,sort)
2918            "&id=" (mixi-news-category-id ,category)
2919            "&type=bn"))
2920
2921 (defconst mixi-news-list-regexp
2922   "<tr bgcolor=\"\\(#FCF5EB\\|#FFFFFF\\)\">
2923 <td WIDTH=\"1%\" valign=top CLASS=\"h120\">¡¦</td>
2924 <td WIDTH=\"97%\" CLASS=\"h120\"><A HREF=\"view_news\\.pl\\?id=\\([0-9]+\\)&media_id=\\([0-9]+\\)\"class=\"new_link\">\\(.+\\)</A>
2925 \\(<IMG SRC=\"http://img\\.mixi\\.jp/img/news_camera3\\.gif\" WIDTH=\"11\" HEIGHT=\"12\">\\|\\)
2926
2927 </td>
2928 <td WIDTH=\"1%\" nowrap CLASS=\"f08\"><A HREF=\"list_news_media\\.pl\\?id=[0-9]+\">\\(.+\\)</A></td>
2929 <td WIDTH=\"1%\" nowrap CLASS=\"f08\">\\([0-9]+\\)·î\\([0-9]+\\)Æü \\([0-9]+\\):\\([0-9]+\\)</td></tr>")
2930
2931 ;;;###autoload
2932 (defun mixi-get-news (category sort &optional range)
2933   "Get news of CATEGORY and SORT."
2934   (unless (mixi-news-category-p category)
2935     (signal 'wrong-type-argument (list 'mixi-news-category-p category)))
2936   (unless (mixi-news-sort-p sort)
2937     (signal 'wrong-type-argument (list 'mixi-news-sort-p sort)))
2938   (let ((items (mixi-get-matched-items (mixi-news-list-page category sort)
2939                                        mixi-news-list-regexp
2940                                        range))
2941         (year (nth 5 (decode-time (current-time))))
2942         (month (nth 4 (decode-time (current-time)))))
2943     (mapcar (lambda (item)
2944               (let ((month-of-item (string-to-number (nth 6 item))))
2945                 (when (> month-of-item month)
2946                   (decf year))
2947                 (setq month month-of-item)
2948                 (mixi-make-news (nth 2 item) (nth 1 item) (nth 5 item)
2949                                 (encode-time
2950                                  0 (string-to-number (nth 9 item))
2951                                  (string-to-number (nth 8 item))
2952                                  (string-to-number (nth 7 item))
2953                                  month year)
2954                                 (nth 3 item))))
2955             items)))
2956
2957 (provide 'mixi)
2958
2959 ;;; mixi.el ends here