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