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