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