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