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