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