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