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