a17394741c06eed352ea93aa07ab9eaef435bb0b
[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 ;; FIXME: Hmm.
513 (defun mixi-url-encode-and-quote-percent-string (string)
514   (apply (function concat)
515          (mapcar
516           (lambda (char)
517             (cond
518              ((eq char ?\n)             ; newline
519               "%%0D%%0A")
520              ((string-match "[-a-zA-Z0-9_:/.]" (char-to-string char)) ; xxx?
521               (char-to-string char))    ; printable
522              ((char-equal char ?\x20)   ; space
523               "+")
524              (t
525               (format "%%%%%02x" char))))       ; escape
526           ;; Coerce a string into a list of chars.
527           (append (encode-coding-string (or string "") mixi-coding-system)
528                   nil))))
529
530 ;; Object.
531 (defconst mixi-object-prefix "mixi-")
532
533 (defmacro mixi-object-class (object)
534   `(car-safe ,object))
535
536 (defmacro mixi-object-p (object)
537   `(let ((class (mixi-object-class ,object)))
538      (when (symbolp class)
539        (eq (string-match (concat "^" mixi-object-prefix)
540                          (symbol-name class)) 0))))
541
542 (defun mixi-object-name (object)
543   "Return the name of OBJECT."
544   (unless (mixi-object-p object)
545     (signal 'wrong-type-argument (list 'mixi-object-p object)))
546   (let ((class (mixi-object-class object)))
547     (substring (symbol-name class) (length mixi-object-prefix))))
548
549 (defun mixi-read-object (exp)
550   "Read one Lisp expression as mixi object."
551   (if (mixi-object-p exp)
552       (let ((func (intern (concat mixi-object-prefix "make-"
553                                   (mixi-object-name exp))))
554             (args (mapcar (lambda (arg)
555                             (mixi-read-object arg))
556                           (cdr exp))))
557         (let ((object (apply func (cdr args))))
558           (when (car args)
559             (mixi-object-set-timestamp object (car args)))
560           object))
561     exp))
562
563 (defun mixi-object-timestamp (object)
564   "Return the timestamp of OJBECT."
565   (unless (mixi-object-p object)
566     (signal 'wrong-type-argument (list 'mixi-object-p object)))
567   (aref (cdr object) 0))
568 (defalias 'mixi-object-realized-p 'mixi-object-timestamp)
569
570 (defun mixi-object-owner (object)
571   "Return the owner of OBJECT."
572   (unless (mixi-object-p object)
573     (signal 'wrong-type-argument (list 'mixi-object-p object)))
574   (let ((func (intern (concat mixi-object-prefix
575                               (mixi-object-name object) "-owner"))))
576     (funcall func object)))
577
578 (defun mixi-object-id (object)
579   "Return the id of OBJECT."
580   (unless (mixi-object-p object)
581     (signal 'wrong-type-argument (list 'mixi-object-p object)))
582   (let ((func (intern (concat mixi-object-prefix
583                               (mixi-object-name object) "-id"))))
584     (funcall func object)))
585
586 (defun mixi-object-time (object)
587   "Return the time of OBJECT."
588   (unless (mixi-object-p object)
589     (signal 'wrong-type-argument (list 'mixi-object-p object)))
590   (let ((func (intern (concat mixi-object-prefix
591                               (mixi-object-name object) "-time"))))
592     (funcall func object)))
593
594 (defun mixi-object-title (object)
595   "Return the title of OBJECT."
596   (unless (mixi-object-p object)
597     (signal 'wrong-type-argument (list 'mixi-object-p object)))
598   (let ((func (intern (concat mixi-object-prefix
599                               (mixi-object-name object) "-title"))))
600     (funcall func object)))
601
602 (defun mixi-object-content (object)
603   "Return the content of OBJECT."
604   (unless (mixi-object-p object)
605     (signal 'wrong-type-argument (list 'mixi-object-p object)))
606   (let ((func (intern (concat mixi-object-prefix
607                               (mixi-object-name object) "-content"))))
608     (funcall func object)))
609
610 (defun mixi-object-set-timestamp (object timestamp)
611   "Set the timestamp of OBJECT."
612   (unless (mixi-object-p object)
613     (signal 'wrong-type-argument (list 'mixi-object-p object)))
614   (aset (cdr object) 0 timestamp))
615
616 (defmacro mixi-object-touch (object)
617   `(mixi-object-set-timestamp ,object (current-time)))
618
619 (defconst mixi-object-url-regexp
620   "/\\(show\\|view\\)_\\([a-z]+\\)\\.pl")
621
622 (defun mixi-make-object-from-url (url)
623   "Return a mixi object from URL."
624   (if (string-match mixi-object-url-regexp url)
625       (let ((name (match-string 2 url)))
626         (cond ((string= name "bbs")
627                (setq name "topic"))
628               ((string= name "profile")
629                (setq name "friend")))
630         (let ((func (intern (concat mixi-object-prefix "make-" name
631                                     "-from-url"))))
632           (funcall func url)))
633     (when (string-match "/home\\.pl" url)
634       (mixi-make-friend-from-url url))))
635
636 ;; Cache.
637 ;; stolen from time-date.el
638 (defun mixi-time-less-p (t1 t2)
639   "Say whether time value T1 is less than time value T2."
640   (unless (numberp (cdr t1))
641     (setq t1 (cons (car t1) (car (cdr t1)))))
642   (unless (numberp (cdr t2))
643     (setq t2 (cons (car t2) (car (cdr t2)))))
644   (or (< (car t1) (car t2))
645       (and (= (car t1) (car t2))
646            (< (cdr t1) (cdr t2)))))
647
648 (defun mixi-time-add (t1 t2)
649   "Add two time values.  One should represent a time difference."
650   (unless (numberp (cdr t1))
651     (setq t1 (cons (car t1) (car (cdr t1)))))
652   (unless (numberp (cdr t2))
653     (setq t2 (cons (car t2) (car (cdr t2)))))
654   (let ((low (+ (cdr t1) (cdr t2))))
655     (cons (+ (car t1) (car t2) (lsh low -16)) low)))
656
657 ;; stolen from time-date.el
658 (defun mixi-seconds-to-time (seconds)
659   "Convert SECONDS (a floating point number) to a time value."
660   (cons (floor seconds 65536)
661         (floor (mod seconds 65536))))
662
663 (defun mixi-cache-expired-p (object)
664   "Whether a cache of OBJECT is expired."
665   (let ((timestamp (mixi-object-timestamp object)))
666     (unless (or (null mixi-cache-expires)
667                  (null timestamp))
668       (if (numberp mixi-cache-expires)
669           (mixi-time-less-p
670            (mixi-time-add timestamp (mixi-seconds-to-time mixi-cache-expires))
671            (current-time))
672         t))))
673
674 (defun mixi-make-cache (key value table)
675   "Make a cache object and return it."
676   (let ((cache (gethash key table)))
677     (if (and cache (not (mixi-cache-expired-p cache)))
678         cache
679       (puthash key value table))))
680
681 (defconst mixi-cache-file-regexp "[a-z]+-cache$")
682 (defconst mixi-cache-regexp (concat mixi-object-prefix
683                                     mixi-cache-file-regexp))
684
685 (defun mixi-save-cache ()
686   (let ((cache-directory (expand-file-name "cache" mixi-directory)))
687     (unless (file-directory-p cache-directory)
688       (make-directory cache-directory t))
689     (let ((caches (apropos-internal mixi-cache-regexp 'boundp)))
690       (mapc (lambda (symbol)
691               (with-temp-file (expand-file-name
692                                (substring (symbol-name symbol)
693                                           (length mixi-object-prefix))
694                                cache-directory)
695                 (let ((coding-system-for-write mixi-coding-system)
696                       (cache (symbol-value symbol)))
697                   (insert "#s(hash-table size "
698                           (number-to-string (hash-table-count cache))
699                           " test equal data (\n")
700                   (maphash
701                    (lambda (key value)
702                      (let (print-level print-length)
703                        (insert (prin1-to-string key) " "
704                                (prin1-to-string value) "\n")))
705                    cache))
706                 (insert "))")))
707             caches))))
708
709 ;; stolen (and modified) from lsdb.el
710 (defun mixi-read-cache (&optional marker)
711   "Read one Lisp expression as text from MARKER, return as Lisp object."
712   (save-excursion
713     (goto-char marker)
714     (if (looking-at "^#s(")
715         (let ((end-marker
716                (progn
717                  (forward-char 2);skip "#s"
718                  (forward-sexp);move to the left paren
719                  (point-marker))))
720           (with-temp-buffer
721             (buffer-disable-undo)
722             (insert-buffer-substring (marker-buffer marker)
723                                      marker end-marker)
724             (goto-char (point-min))
725             (delete-char 2)
726             (let ((object (read (current-buffer)))
727                   data)
728               (if (eq 'hash-table (car object))
729                   (progn
730                     (setq data (plist-get (cdr object) 'data))
731                     (while data
732                       (pop data);throw it away
733                       (mixi-read-object (pop data))))
734                 object))))
735       (read marker))))
736
737 (defun mixi-load-cache ()
738   (let ((cache-directory (expand-file-name "cache" mixi-directory)))
739     (when (file-directory-p cache-directory)
740       ;; FIXME: Load friend and community first.
741       (let ((files (directory-files cache-directory t
742                                     mixi-cache-file-regexp)))
743         (mapc (lambda (file)
744                 (let ((buffer (find-file-noselect file)))
745                   (unwind-protect
746                       (save-excursion
747                         (set-buffer buffer)
748                         (goto-char (point-min))
749                         (re-search-forward "^#s(")
750                         (goto-char (match-beginning 0))
751                         (mixi-read-cache (point-marker)))
752                     (kill-buffer buffer))))
753               files)))))
754
755 ;; Friend object.
756 (defvar mixi-friend-cache (make-hash-table :test 'equal))
757 (defun mixi-make-friend (id &optional nick name sex address age birthday
758                             blood-type birthplace hobby job organization
759                             profile)
760   "Return a friend object."
761   (mixi-make-cache id (cons 'mixi-friend (vector nil id nick name sex address
762                                                  age birthday blood-type
763                                                  birthplace hobby job
764                                                  organization profile))
765                    mixi-friend-cache))
766
767 (defun mixi-make-me ()
768   "Return a my object."
769   (unless mixi-me
770     (with-mixi-retrieve "/home.pl"
771       (if (re-search-forward mixi-my-id-regexp)
772           (setq mixi-me (mixi-make-friend (match-string 1)))
773         (signal 'error (list 'who-am-i)))))
774   mixi-me)
775
776 (defconst mixi-friend-url-regexp
777   "/show_\\(friend\\|profile\\)\\.pl\\?id=\\([0-9]+\\)")
778
779 (defun mixi-make-friend-from-url (url)
780   "Return a friend object from URL."
781   (if (string-match mixi-friend-url-regexp url)
782       (let ((id (match-string 2 url)))
783         (mixi-make-friend id))
784     (when (string-match "/home\\.pl" url)
785       (mixi-make-me))))
786
787 (defmacro mixi-friend-p (friend)
788   `(eq (mixi-object-class ,friend) 'mixi-friend))
789
790 (defmacro mixi-friend-page (friend)
791   `(concat "/show_profile.pl?id=" (mixi-friend-id ,friend)))
792
793 (defconst mixi-friend-nick-regexp
794   "<img \\(alt=\"\\*\" \\)?src=\"?http://img\\.mixi\\.jp/img/dot0\\.gif\"? width=\"?1\"? height=\"?5\"?\\( /\\)?><br\\( /\\)?>\r?
795 \\(.*\\)¤µ¤ó([0-9]+)")
796 (defconst mixi-friend-name-regexp
797   "̾\\(&nbsp;\\| \\)Á°</font></td>
798
799 ?<td width=\"?345\"?>\\(.+?\\)\\(</td>\\| <img\\)")
800 (defconst mixi-friend-sex-regexp
801   "À­\\(&nbsp;\\| \\)ÊÌ</font></td>
802
803 ?<td width=\"?345\"?>\\([Ã˽÷]\\)À­\\(</td>\\| <img\\)")
804 (defconst mixi-friend-address-regexp
805   "¸½½»½ê</font></td>
806 <td>\\(.+?\\)\\(</td>\\| <img\\)")
807 (defconst mixi-friend-age-regexp
808   "ǯ\\(&nbsp;\\| \\)Îð</font></td>\n<td>\\([0-9]+\\)ºÐ\\(</td>\\| <img\\)")
809 (defconst mixi-friend-birthday-regexp
810   "ÃÂÀ¸Æü</font></td>\n<td>\\([0-9]+\\)·î\\([0-9]+\\)Æü\\(</td>\\| <img\\)")
811 (defconst mixi-friend-blood-type-regexp
812   "·ì±Õ·¿</font></td>\n<td>\\([ABO]B?\\)·¿\\(</td>\\| <img\\)")
813 (defconst mixi-friend-birthplace-regexp
814   "½Ð¿ÈÃÏ</font>\n?</td>\n<td>\\(.+?\\)\\(</td>\\| <img\\)")
815 (defconst mixi-friend-hobby-regexp
816   "¼ñ\\(&nbsp;\\| \\)Ì£</font></td>\n<td>\\(.+?\\)\\(</td>\\| <img\\)")
817 (defconst mixi-friend-job-regexp
818   "¿¦\\(&nbsp;\\| \\)¶È</font></td>\n<td>\\(.+?\\)\\(</td>\\| <img\\)")
819 (defconst mixi-friend-organization-regexp
820   "½ê\\(&nbsp;\\| \\)°</font></td>\n<td[^>]*>\\(.+?\\)\\(</td>\\| <img\\)")
821 (defconst mixi-friend-profile-regexp
822   "¼«¸Ê¾Ò²ð</font></td>
823 <td class=\"?h120\"?>\\(.+\\)</td></tr>")
824
825 (defun mixi-realize-friend (friend)
826   "Realize a FRIEND."
827   ;; FIXME: Check a expiration of cache?
828   (unless (mixi-object-realized-p friend)
829     (with-mixi-retrieve (mixi-friend-page friend)
830       (let ((case-fold-search t))
831         (if (re-search-forward mixi-friend-nick-regexp nil t)
832             (mixi-friend-set-nick friend (match-string 4))
833           (mixi-realization-error 'cannot-find-nick friend))
834         (when (re-search-forward mixi-friend-name-regexp nil t)
835           (mixi-friend-set-name friend (match-string 2)))
836         (when (re-search-forward mixi-friend-sex-regexp nil t)
837           (mixi-friend-set-sex friend (if (string= (match-string 2) "ÃË")
838                                           'male 'female)))
839         (when (re-search-forward mixi-friend-address-regexp nil t)
840           (mixi-friend-set-address friend (match-string 1)))
841         (when (re-search-forward mixi-friend-age-regexp nil t)
842           (mixi-friend-set-age friend (string-to-number (match-string 2))))
843         (when (re-search-forward mixi-friend-birthday-regexp nil t)
844           (mixi-friend-set-birthday
845            friend (list (string-to-number (match-string 1))
846                         (string-to-number (match-string 2)))))
847         (when (re-search-forward mixi-friend-blood-type-regexp nil t)
848           (mixi-friend-set-blood-type friend (intern (match-string 1))))
849         (when (re-search-forward mixi-friend-birthplace-regexp nil t)
850           (mixi-friend-set-birthplace friend (match-string 1)))
851         (when (re-search-forward mixi-friend-hobby-regexp nil t)
852           (mixi-friend-set-hobby friend (split-string (match-string 2) ", ")))
853         (when (re-search-forward mixi-friend-job-regexp nil t)
854           (mixi-friend-set-job friend (match-string 2)))
855         (when (re-search-forward mixi-friend-organization-regexp nil t)
856           (mixi-friend-set-organization friend (match-string 2)))
857         (when (re-search-forward mixi-friend-profile-regexp nil t)
858           (mixi-friend-set-profile friend (match-string 1)))))
859     (mixi-object-touch friend)))
860
861 (defun mixi-friend-id (friend)
862   "Return the id of FRIEND."
863   (unless (mixi-friend-p friend)
864     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
865   (aref (cdr friend) 1))
866
867 (defun mixi-friend-nick (friend)
868   "Return the nick of FRIEND."
869   (unless (mixi-friend-p friend)
870     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
871   (unless (aref (cdr friend) 2)
872     (mixi-realize-friend friend))
873   (aref (cdr friend) 2))
874
875 (defun mixi-friend-name (friend)
876   "Return the name of FRIEND."
877   (unless (mixi-friend-p friend)
878     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
879   (mixi-realize-friend friend)
880   (aref (cdr friend) 3))
881
882 (defun mixi-friend-sex (friend)
883   "Return the sex 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) 4))
888
889 (defun mixi-friend-address (friend)
890   "Return the address 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) 5))
895
896 (defun mixi-friend-age (friend)
897   "Return the age 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) 6))
902
903 (defun mixi-friend-birthday (friend)
904   "Return the birthday 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) 7))
909
910 (defun mixi-friend-blood-type (friend)
911   "Return the blood type 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) 8))
916
917 (defun mixi-friend-birthplace (friend)
918   "Return the birthplace 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) 9))
923
924 (defun mixi-friend-hobby (friend)
925   "Return the hobby 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) 10))
930
931 (defun mixi-friend-job (friend)
932   "Return the job 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) 11))
937
938 (defun mixi-friend-organization (friend)
939   "Return the organization 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) 12))
944
945 (defun mixi-friend-profile (friend)
946   "Return the pforile 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) 13))
951
952 (defun mixi-friend-set-nick (friend nick)
953   "Set the nick of FRIEND."
954   (unless (mixi-friend-p friend)
955     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
956   (aset (cdr friend) 2 nick))
957
958 (defun mixi-friend-set-name (friend name)
959   "Set the name of FRIEND."
960   (unless (mixi-friend-p friend)
961     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
962   (aset (cdr friend) 3 name))
963
964 (defun mixi-friend-set-sex (friend sex)
965   "Set the sex of FRIEND."
966   (unless (mixi-friend-p friend)
967     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
968   (aset (cdr friend) 4 sex))
969
970 (defun mixi-friend-set-address (friend address)
971   "Set the address of FRIEND."
972   (unless (mixi-friend-p friend)
973     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
974   (aset (cdr friend) 5 address))
975
976 (defun mixi-friend-set-age (friend age)
977   "Set the age of FRIEND."
978   (unless (mixi-friend-p friend)
979     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
980   (aset (cdr friend) 6 age))
981
982 (defun mixi-friend-set-birthday (friend birthday)
983   "Set the birthday of FRIEND."
984   (unless (mixi-friend-p friend)
985     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
986   (aset (cdr friend) 7 birthday))
987
988 (defun mixi-friend-set-blood-type (friend blood-type)
989   "Set the blood type of FRIEND."
990   (unless (mixi-friend-p friend)
991     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
992   (aset (cdr friend) 8 blood-type))
993
994 (defun mixi-friend-set-birthplace (friend birthplace)
995   "Set the birthplace of FRIEND."
996   (unless (mixi-friend-p friend)
997     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
998   (aset (cdr friend) 9 birthplace))
999
1000 (defun mixi-friend-set-hobby (friend hobby)
1001   "Set the hobby of FRIEND."
1002   (unless (mixi-friend-p friend)
1003     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
1004   (aset (cdr friend) 10 hobby))
1005
1006 (defun mixi-friend-set-job (friend job)
1007   "Set the job of FRIEND."
1008   (unless (mixi-friend-p friend)
1009     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
1010   (aset (cdr friend) 11 job))
1011
1012 (defun mixi-friend-set-organization (friend organization)
1013   "Set the organization of FRIEND."
1014   (unless (mixi-friend-p friend)
1015     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
1016   (aset (cdr friend) 12 organization))
1017
1018 (defun mixi-friend-set-profile (friend profile)
1019   "Set the profile of FRIEND."
1020   (unless (mixi-friend-p friend)
1021     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
1022   (aset (cdr friend) 13 profile))
1023
1024 (defmacro mixi-friend-list-page (&optional friend)
1025   `(concat "/list_friend.pl?page=%d"
1026            (when ,friend (concat "&id=" (mixi-friend-id ,friend)))))
1027
1028 (defconst mixi-friend-list-id-regexp
1029   "<a href=show_friend\\.pl\\?id=\\([0-9]+\\)")
1030 (defconst mixi-friend-list-nick-regexp
1031   "<td valign=middle>\\(.+\\)¤µ¤ó([0-9]+)<br />")
1032
1033 ;;;###autoload
1034 (defun mixi-get-friends (&rest friend-or-range)
1035   "Get friends of FRIEND."
1036   (when (> (length friend-or-range) 2)
1037     (signal 'wrong-number-of-arguments (list 'mixi-get-friends
1038                                              (length friend-or-range))))
1039   (let ((friend (nth 0 friend-or-range))
1040         (range (nth 1 friend-or-range)))
1041     (when (or (not (mixi-friend-p friend)) (mixi-friend-p range))
1042       (setq friend (nth 1 friend-or-range))
1043       (setq range (nth 0 friend-or-range)))
1044     (unless (or (null friend) (mixi-friend-p friend))
1045       (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
1046     (let ((ids (mixi-get-matched-items (mixi-friend-list-page friend)
1047                                        mixi-friend-list-id-regexp
1048                                        range))
1049           (nicks (mixi-get-matched-items (mixi-friend-list-page friend)
1050                                          mixi-friend-list-nick-regexp
1051                                          range)))
1052       (let ((index 0)
1053             ret)
1054         (while (< index (length ids))
1055           (setq ret (cons (mixi-make-friend (nth 0 (nth index ids))
1056                                             (nth 0 (nth index nicks))) ret))
1057           (incf index))
1058         (reverse ret)))))
1059
1060 ;; Favorite.
1061 (defmacro mixi-favorite-list-page ()
1062   `(concat "/list_bookmark.pl?page=%d"))
1063
1064 (defconst mixi-favorite-list-id-regexp
1065   "<td ALIGN=center BGCOLOR=#FDF9F2 width=330><a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">")
1066 (defconst mixi-favorite-list-nick-regexp
1067   "<td BGCOLOR=#FDF9F2><font COLOR=#996600>̾&nbsp;&nbsp;Á°</font></td>
1068 <td COLSPAN=2 BGCOLOR=#FFFFFF>\\(.+\\)</td></tr>")
1069
1070 ;;;###autoload
1071 (defun mixi-get-favorites (&optional range)
1072   "Get favorites."
1073   (let ((ids (mixi-get-matched-items (mixi-favorite-list-page)
1074                                      mixi-favorite-list-id-regexp
1075                                      range))
1076         (nicks (mixi-get-matched-items (mixi-favorite-list-page)
1077                                        mixi-favorite-list-nick-regexp
1078                                        range)))
1079     (let ((index 0)
1080           ret)
1081       (while (< index (length ids))
1082         (setq ret (cons (mixi-make-friend (nth 0 (nth index ids))
1083                                           (nth 0 (nth index nicks))) ret))
1084         (incf index))
1085       (reverse ret))))
1086
1087 ;; Log object.
1088 (defun mixi-make-log (friend time)
1089   "Return a log object."
1090   (cons 'mixi-log (vector friend time)))
1091
1092 (defmacro mixi-log-p (log)
1093   `(eq (mixi-object-class ,log) 'mixi-log))
1094
1095 (defun mixi-log-friend (log)
1096   "Return the friend of LOG."
1097   (unless (mixi-log-p log)
1098     (signal 'wrong-type-argument (list 'mixi-log-p log)))
1099   (aref (cdr log) 0))
1100
1101 (defun mixi-log-time (log)
1102   "Return the time of LOG."
1103   (unless (mixi-log-p log)
1104     (signal 'wrong-type-argument (list 'mixi-log-p log)))
1105   (aref (cdr log) 1))
1106
1107 (defmacro mixi-log-list-page ()
1108   `(concat "/show_log.pl"))
1109
1110 (defconst mixi-log-list-regexp
1111   "\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü \\([0-9]+\\):\\([0-9]+\\) <a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.*\\)</a>")
1112
1113 ;;;###autoload
1114 (defun mixi-get-logs (&optional range)
1115   "Get logs."
1116   (let ((items (mixi-get-matched-items (mixi-log-list-page)
1117                                        mixi-log-list-regexp
1118                                        range)))
1119     (mapcar (lambda (item)
1120               (mixi-make-log (mixi-make-friend (nth 5 item) (nth 6 item))
1121                              (encode-time 0
1122                                           (string-to-number (nth 4 item))
1123                                           (string-to-number (nth 3 item))
1124                                           (string-to-number (nth 2 item))
1125                                           (string-to-number (nth 1 item))
1126                                           (string-to-number (nth 0 item)))))
1127             items)))
1128
1129 ;; Diary object.
1130 (defvar mixi-diary-cache (make-hash-table :test 'equal))
1131 (defun mixi-make-diary (owner id &optional comment-count time title content)
1132   "Return a diary object."
1133   (let ((owner (or owner (mixi-make-me))))
1134     (mixi-make-cache (list (mixi-friend-id owner) id)
1135                      (cons 'mixi-diary (vector nil owner id comment-count time
1136                                                title content))
1137                      mixi-diary-cache)))
1138
1139 (defconst mixi-diary-url-regexp
1140   "/view_diary\\.pl\\?id=\\([0-9]+\\)&owner_id=\\([0-9]+\\)\\(&comment_count=\\([0-9]+\\)\\)?")
1141
1142 (defun mixi-make-diary-from-url (url)
1143   "Return a diary object from URL."
1144   (when (string-match mixi-diary-url-regexp url)
1145     (let ((id (match-string 1 url))
1146           (owner-id (match-string 2 url))
1147           (comment-count (match-string 4 url)))
1148       (mixi-make-diary (mixi-make-friend owner-id) id comment-count))))
1149
1150 (defmacro mixi-diary-p (diary)
1151   `(eq (mixi-object-class ,diary) 'mixi-diary))
1152
1153 (defmacro mixi-diary-page (diary)
1154   `(concat "/view_diary.pl?id=" (mixi-diary-id ,diary)
1155            "&owner_id=" (mixi-friend-id (mixi-diary-owner ,diary))))
1156
1157 (defconst mixi-diary-closed-regexp
1158   "<td>ͧ¿Í\\(¤Îͧ¿Í\\)?¤Þ¤Ç¸ø³«¤Î¤¿¤áÆɤळ¤È¤¬½ÐÍè¤Þ¤»¤ó¡£</td></tr>")
1159 (defconst mixi-diary-owner-nick-regexp
1160   "<td WIDTH=490 background=http://img\\.mixi\\.jp/img/bg_w\\.gif><b><font COLOR=#605048>\\(.+?\\)\\(¤µ¤ó\\)?¤ÎÆüµ­</font></b></td>")
1161 (defconst mixi-diary-time-regexp
1162   "<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>")
1163 (defconst mixi-diary-title-regexp
1164   "<td \\(bgcolor\\|BGCOLOR\\)=\"?#FFF4E0\"? width=\"?430\"?>&nbsp;\\([^<]+\\)</td>")
1165 (defconst mixi-diary-content-regexp
1166   "<td \\(class\\|CLASS\\)=\"?h12\"?>\\(.*\\)</td>")
1167
1168 (defun mixi-realize-diary (diary &optional page)
1169   "Realize a DIARY."
1170   ;; FIXME: Check a expiration of cache?
1171   (unless (mixi-object-realized-p diary)
1172     (with-mixi-retrieve (or page (mixi-diary-page diary))
1173       (unless (re-search-forward mixi-diary-closed-regexp nil t)
1174         (if (re-search-forward mixi-diary-owner-nick-regexp nil t)
1175             (mixi-friend-set-nick (mixi-diary-owner diary) (match-string 1))
1176           (mixi-realization-error 'cannot-find-owner-nick diary))
1177         (if (re-search-forward mixi-diary-time-regexp nil t)
1178             (mixi-diary-set-time
1179              diary (encode-time 0 (string-to-number (match-string 10))
1180                                 (string-to-number (match-string 9))
1181                                 (string-to-number (match-string 7))
1182                                 (string-to-number (match-string 6))
1183                                 (string-to-number (match-string 5))))
1184           (mixi-realization-error 'cannot-find-time diary))
1185         (if (re-search-forward mixi-diary-title-regexp nil t)
1186             (mixi-diary-set-title diary (match-string 2))
1187           (mixi-realization-error 'cannot-find-title diary))
1188         (if (re-search-forward mixi-diary-content-regexp nil t)
1189             (mixi-diary-set-content diary (match-string 2))
1190           (mixi-realization-error 'cannot-find-content diary))))
1191     (mixi-object-touch diary)))
1192
1193 (defun mixi-diary-owner (diary)
1194   "Return the owner of DIARY."
1195   (unless (mixi-diary-p diary)
1196     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1197   (aref (cdr diary) 1))
1198
1199 (defun mixi-diary-id (diary)
1200   "Return the id of DIARY."
1201   (unless (mixi-diary-p diary)
1202     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1203   (aref (cdr diary) 2))
1204
1205 (defun mixi-diary-comment-count (diary)
1206   "Return the comment-count of DIARY."
1207   (unless (mixi-diary-p diary)
1208     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1209   (aref (cdr diary) 3))
1210
1211 (defun mixi-diary-time (diary)
1212   "Return the time of DIARY."
1213   (unless (mixi-diary-p diary)
1214     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1215   (unless (aref (cdr diary) 3)
1216     (mixi-realize-diary diary))
1217   (aref (cdr diary) 4))
1218
1219 (defun mixi-diary-title (diary)
1220   "Return the title of DIARY."
1221   (unless (mixi-diary-p diary)
1222     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1223   (unless (aref (cdr diary) 4)
1224     (mixi-realize-diary diary))
1225   (aref (cdr diary) 5))
1226
1227 (defun mixi-diary-content (diary)
1228   "Return the content of DIARY."
1229   (unless (mixi-diary-p diary)
1230     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1231   (mixi-realize-diary diary)
1232   (aref (cdr diary) 6))
1233
1234 (defun mixi-diary-set-comment-count (diary comment-count)
1235   "Set the comment-count of DIARY."
1236   (unless (mixi-diary-p diary)
1237     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1238   (aset (cdr diary) 3 comment-count))
1239
1240 (defun mixi-diary-set-time (diary time)
1241   "Set the time of DIARY."
1242   (unless (mixi-diary-p diary)
1243     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1244   (aset (cdr diary) 4 time))
1245
1246 (defun mixi-diary-set-title (diary title)
1247   "Set the title of DIARY."
1248   (unless (mixi-diary-p diary)
1249     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1250   (aset (cdr diary) 5 title))
1251
1252 (defun mixi-diary-set-content (diary content)
1253   "Set the content of DIARY."
1254   (unless (mixi-diary-p diary)
1255     (signal 'wrong-type-argument (list 'mixi-diary-p diary)))
1256   (aset (cdr diary) 6 content))
1257
1258 (defmacro mixi-diary-list-page (&optional friend)
1259   `(concat "/list_diary.pl?page=%d"
1260            (when ,friend (concat "&id=" (mixi-friend-id ,friend)))))
1261
1262 (defconst mixi-diary-list-regexp
1263   "<tr VALIGN=top>
1264 <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>
1265 <td bgcolor=\"#FFF4E0\">&nbsp;<a href=\"view_diary\\.pl\\?id=\\([0-9]+\\)&owner_id=[0-9]+\">\\(.*\\)</a></td>")
1266
1267 ;;;###autoload
1268 (defun mixi-get-diaries (&rest friend-or-range)
1269   "Get diaries of FRIEND."
1270   (when (> (length friend-or-range) 2)
1271     (signal 'wrong-number-of-arguments
1272             (list 'mixi-get-diaries (length friend-or-range))))
1273   (let ((friend (nth 0 friend-or-range))
1274         (range (nth 1 friend-or-range)))
1275     (when (or (not (mixi-friend-p friend)) (mixi-friend-p range))
1276       (setq friend (nth 1 friend-or-range))
1277       (setq range (nth 0 friend-or-range)))
1278     (unless (or (null friend) (mixi-friend-p friend))
1279       (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
1280     (let ((items (mixi-get-matched-items (mixi-diary-list-page friend)
1281                                          mixi-diary-list-regexp
1282                                          range))
1283           (year (nth 5 (decode-time (current-time))))
1284           (month (nth 4 (decode-time (current-time)))))
1285       (mapcar (lambda (item)
1286                 (let ((month-of-item (string-to-number (nth 0 item))))
1287                   (when (> month-of-item month)
1288                     (decf year))
1289                   (setq month month-of-item)
1290                   (mixi-make-diary friend (nth 5 item) nil
1291                                    (encode-time
1292                                     0 (string-to-number (nth 3 item))
1293                                     (string-to-number (nth 2 item))
1294                                     (string-to-number (nth 1 item))
1295                                     month year)
1296                                    (nth 6 item))))
1297               items))))
1298
1299 (defmacro mixi-new-diary-list-page ()
1300   `(concat "/new_friend_diary.pl?page=%d"))
1301
1302 (defconst mixi-new-diary-list-regexp
1303   "<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>
1304 <td WIDTH=450><a class=\"new_link\" href=view_diary\\.pl\\?id=\\([0-9]+\\)&owner_id=\\([0-9]+\\)>\\(.+\\)</a> (\\(.*\\)) ")
1305
1306 ;;;###autoload
1307 (defun mixi-get-new-diaries (&optional range)
1308   "Get new diaries."
1309   (let ((items (mixi-get-matched-items (mixi-new-diary-list-page)
1310                                        mixi-new-diary-list-regexp
1311                                        range)))
1312     (mapcar (lambda (item)
1313               (mixi-make-diary (mixi-make-friend (nth 6 item) (nth 8 item))
1314                                (nth 5 item)
1315                                nil
1316                                (encode-time
1317                                 0 (string-to-number (nth 4 item))
1318                                 (string-to-number (nth 3 item))
1319                                 (string-to-number (nth 2 item))
1320                                 (string-to-number (nth 1 item))
1321                                 (string-to-number (nth 0 item)))
1322                                (nth 7 item)))
1323             items)))
1324
1325 (defmacro mixi-search-diary-list-page (keyword)
1326   `(concat "/search_diary.pl?page=%d&submit=search&keyword="
1327              (mixi-url-encode-and-quote-percent-string ,keyword)))
1328
1329 (defconst mixi-search-diary-list-regexp
1330   "<td BGCOLOR=#FDF9F2><font COLOR=#996600>̾&nbsp;&nbsp;Á°</font></td>
1331 <td COLSPAN=2 BGCOLOR=#FFFFFF>\\(.*\\)
1332
1333 </td></tr>
1334
1335 <tr>
1336 <td BGCOLOR=#FDF9F2><font COLOR=#996600>¥¿¥¤¥È¥ë</font></td>
1337 <td COLSPAN=2 BGCOLOR=#FFFFFF>\\(.+\\)</td></tr>
1338
1339 <tr>
1340 <td BGCOLOR=#FDF9F2><font COLOR=#996600>ËÜ&nbsp;&nbsp;ʸ</font></td>
1341 <td COLSPAN=2 BGCOLOR=#FFFFFF>\\(.*\\)</td></tr>
1342
1343
1344 <tr>
1345 <td NOWRAP BGCOLOR=#FDF9F2 WIDTH=80><font COLOR=#996600>ºîÀ®Æü»þ</font></td>
1346 <td BGCOLOR=#FFFFFF WIDTH=220>\\([0-9]+\\)·î\\([0-9]+\\)Æü \\([0-9]+\\):\\([0-9]+\\)</td>
1347 <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>
1348 </table>
1349 </td></tr></table>")
1350
1351 ;;;###autoload
1352 (defun mixi-search-diaries (keyword &optional range)
1353   (let ((items (mixi-get-matched-items (mixi-search-diary-list-page keyword)
1354                                        mixi-search-diary-list-regexp
1355                                        range))
1356         (year (nth 5 (decode-time (current-time))))
1357         (month (nth 4 (decode-time (current-time)))))
1358     (mapcar (lambda (item)
1359                 (let ((month-of-item (string-to-number (nth 3 item))))
1360                   (when (> month-of-item month)
1361                     (decf year))
1362                   (setq month month-of-item)
1363                   (mixi-make-diary (mixi-make-friend (nth 8 item) (nth 0 item))
1364                                    (nth 7 item)
1365                                    nil
1366                                    (encode-time
1367                                     0 (string-to-number (nth 6 item))
1368                                     (string-to-number (nth 5 item))
1369                                     (string-to-number (nth 4 item))
1370                                     month year)
1371                                    (nth 1 item)
1372                                    (nth 2 item))))
1373             items)))
1374
1375 (defmacro mixi-post-diary-page ()
1376   `(concat "/add_diary.pl"))
1377
1378 (defconst mixi-post-key-regexp
1379   "<input type=\"?hidden\"? name=\"?post_key\"? value=\"\\([a-z0-9]+\\)\">")
1380 (defconst mixi-post-succeed-regexp
1381   "<b>\\(ºîÀ®\\|½ñ¤­¹þ¤ß\\)¤¬´°Î»¤·¤Þ¤·¤¿¡£È¿±Ç¤Ë»þ´Ö¤¬¤«¤«¤ë¤³¤È¤¬¤¢¤ê¤Þ¤¹¤Î¤Ç¡¢É½¼¨¤µ¤ì¤Æ¤¤¤Ê¤¤¾ì¹ç¤Ï¾¯¡¹¤ªÂÔ¤Á¤¯¤À¤µ¤¤¡£</b>")
1382
1383 ;; FIXME: Support photos.
1384 ;;;###autoload
1385 (defun mixi-post-diary (title content)
1386   "Post a diary."
1387   (unless (stringp title)
1388     (signal 'wrong-type-argument (list 'stringp title)))
1389   (unless (stringp content)
1390     (signal 'wrong-type-argument (list 'stringp content)))
1391   (let ((fields `(("id" . ,(mixi-friend-id (mixi-make-me)))
1392                   ("diary_title" . ,title)
1393                   ("diary_body" . ,content)
1394                   ("submit" . "main")))
1395         post-key)
1396     (with-mixi-post-form (mixi-post-diary-page) fields
1397       (if (re-search-forward mixi-post-key-regexp nil t)
1398           (setq post-key (match-string 1))
1399         (mixi-post-error 'cannot-find-key)))
1400     (setq fields `(("post_key" . ,post-key)
1401                    ("id" . ,(mixi-friend-id (mixi-make-me)))
1402                    ("diary_title" . ,title)
1403                    ("diary_body" . ,content)
1404                    ("submit" . "confirm")))
1405     (with-mixi-post-form (mixi-post-diary-page) fields
1406       (unless (re-search-forward mixi-post-succeed-regexp nil t)
1407         (mixi-post-error 'cannot-find-succeed)))))
1408
1409 ;; Community object.
1410 (defvar mixi-community-cache (make-hash-table :test 'equal))
1411 (defun mixi-make-community (id &optional name birthday owner category members
1412                                open-level authority description)
1413   "Return a community object."
1414   (mixi-make-cache id (cons 'mixi-community (vector nil id name birthday owner
1415                                                     category members
1416                                                     open-level authority
1417                                                     description))
1418                    mixi-community-cache))
1419
1420 (defconst mixi-community-url-regexp
1421   "/view_community\\.pl\\?id=\\([0-9]+\\)")
1422
1423 (defun mixi-make-community-from-url (url)
1424   "Return a community object from URL."
1425   (when (string-match mixi-community-url-regexp url)
1426     (let ((id (match-string 1 url)))
1427       (mixi-make-community id))))
1428
1429 (defmacro mixi-community-p (community)
1430   `(eq (mixi-object-class ,community) 'mixi-community))
1431
1432 (defmacro mixi-community-page (community)
1433   `(concat "/view_community.pl?id=" (mixi-community-id ,community)))
1434
1435 ;; FIXME: Not community specific.
1436 (defconst mixi-community-nodata-regexp
1437   "^¥Ç¡¼¥¿¤¬¤¢¤ê¤Þ¤»¤ó")
1438 (defconst mixi-community-name-regexp
1439   "<td WIDTH=345>\\(.*\\)</td></tr>")
1440 (defconst mixi-community-birthday-regexp
1441   "<td BGCOLOR=#F2DDB7 WIDTH=80><font COLOR=#996600>³«ÀßÆü</font></td>\r
1442 <td WIDTH=345>\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü")
1443 ;; FIXME: Care when the owner has seceded.
1444 (defconst mixi-community-owner-regexp
1445   "<td BGCOLOR=#F2DDB7 WIDTH=80><font COLOR=#996600>´ÉÍý¿Í</font></td>\r
1446 <td WIDTH=345>\r
1447 <table style=\"width: 100%;\"><tr><td>\r
1448 <a href=\"\\(home\\.pl\\|show_friend\\.pl\\?id=\\([0-9]+\\)\\)\">\\(.*\\)</a>")
1449 (defconst mixi-community-category-regexp
1450   "<td BGCOLOR=#F2DDB7 WIDTH=80><font COLOR=#996600>¥«¥Æ¥´¥ê</font></td>\r
1451 <td WIDTH=345>\\([^<]+\\)</td>")
1452 (defconst mixi-community-members-regexp
1453   "<td BGCOLOR=#F2DDB7 WIDTH=80><font COLOR=#996600>¥á¥ó¥Ð¡¼¿ô</font></td>\r
1454 <td WIDTH=345>\\([0-9]+\\)¿Í</td></tr>")
1455 (defconst mixi-community-open-level-regexp
1456   "<td BGCOLOR=#F2DDB7 WIDTH=80><font COLOR=#996600>»²²Ã¾ò·ï¤È<br>¸ø³«¥ì¥Ù¥ë</font></td>\r
1457 <td WIDTH=345>\\(.+\\)</td></tr>")
1458 (defconst mixi-community-authority-regexp
1459   "<td BGCOLOR=#F2DDB7 WIDTH=80><font COLOR=#996600>¥È¥Ô¥Ã¥¯ºîÀ®¤Î¸¢¸Â</font></td>\r
1460 <td WIDTH=345>\\(.+\\)</td></tr>")
1461 (defconst mixi-community-description-regexp
1462   "<td CLASS=h120 WIDTH=345>\\(.+\\)</td>")
1463
1464 (defun mixi-realize-community (community)
1465   "Realize a COMMUNITY."
1466   ;; FIXME: Check a expiration of cache?
1467   (unless (mixi-object-realized-p community)
1468     (with-mixi-retrieve (mixi-community-page community)
1469       (if (re-search-forward mixi-community-nodata-regexp nil t)
1470           ;; FIXME: Set all members?
1471           (mixi-community-set-name community "¥Ç¡¼¥¿¤¬¤¢¤ê¤Þ¤»¤ó")
1472         (if (re-search-forward mixi-community-name-regexp nil t)
1473             (mixi-community-set-name community (match-string 1))
1474           (mixi-realization-error 'cannot-find-name community))
1475         (if (re-search-forward mixi-community-birthday-regexp nil t)
1476             (mixi-community-set-birthday
1477              community (encode-time 0 0 0 (string-to-number (match-string 3))
1478                                     (string-to-number (match-string 2))
1479                                     (string-to-number (match-string 1))))
1480           (mixi-realization-error 'cannot-find-birthday community))
1481         (if (re-search-forward mixi-community-owner-regexp nil t)
1482             (if (string= (match-string 1) "home.pl")
1483                 (mixi-community-set-owner community (mixi-make-me))
1484               (mixi-community-set-owner community
1485                                         (mixi-make-friend (match-string 2)
1486                                                           (match-string 3))))
1487           (mixi-realization-error 'cannot-find-owner community))
1488         (if (re-search-forward mixi-community-category-regexp nil t)
1489             (mixi-community-set-category community (match-string 1))
1490           (mixi-realization-error 'cannot-find-category community))
1491         (if (re-search-forward mixi-community-members-regexp nil t)
1492             (mixi-community-set-members community
1493                                         (string-to-number (match-string 1)))
1494           (mixi-realization-error 'cannot-find-members community))
1495         (if (re-search-forward mixi-community-open-level-regexp nil t)
1496             (mixi-community-set-open-level community (match-string 1))
1497           (mixi-realization-error 'cannot-find-open-level community))
1498         (if (re-search-forward mixi-community-authority-regexp nil t)
1499             (mixi-community-set-authority community (match-string 1))
1500           (mixi-realization-error 'cannot-find-authority community))
1501         (if (re-search-forward mixi-community-description-regexp nil t)
1502             (mixi-community-set-description community (match-string 1))
1503           (mixi-realization-error 'cannot-find-description community))))
1504     (mixi-object-touch community)))
1505
1506 (defun mixi-community-id (community)
1507   "Return the id of COMMUNITY."
1508   (unless (mixi-community-p community)
1509     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1510   (aref (cdr community) 1))
1511
1512 (defun mixi-community-name (community)
1513   "Return the name of COMMUNITY."
1514   (unless (mixi-community-p community)
1515     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1516   (unless (aref (cdr community) 2)
1517     (mixi-realize-community community))
1518   (aref (cdr community) 2))
1519
1520 (defun mixi-community-birthday (community)
1521   "Return the birthday of COMMUNITY."
1522   (unless (mixi-community-p community)
1523     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1524   (mixi-realize-community community)
1525   (aref (cdr community) 3))
1526
1527 (defun mixi-community-owner (community)
1528   "Return the owner of COMMUNITY."
1529   (unless (mixi-community-p community)
1530     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1531   (mixi-realize-community community)
1532   (aref (cdr community) 4))
1533
1534 (defun mixi-community-category (community)
1535   "Return the category of COMMUNITY."
1536   (unless (mixi-community-p community)
1537     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1538   (mixi-realize-community community)
1539   (aref (cdr community) 5))
1540
1541 (defun mixi-community-members (community)
1542   "Return the members of COMMUNITY."
1543   (unless (mixi-community-p community)
1544     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1545   (mixi-realize-community community)
1546   (aref (cdr community) 6))
1547
1548 (defun mixi-community-open-level (community)
1549   "Return the open-level of COMMUNITY."
1550   (unless (mixi-community-p community)
1551     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1552   (mixi-realize-community community)
1553   (aref (cdr community) 7))
1554
1555 (defun mixi-community-authority (community)
1556   "Return the authority of COMMUNITY."
1557   (unless (mixi-community-p community)
1558     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1559   (mixi-realize-community community)
1560   (aref (cdr community) 8))
1561
1562 (defun mixi-community-description (community)
1563   "Return the description of COMMUNITY."
1564   (unless (mixi-community-p community)
1565     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1566   (mixi-realize-community community)
1567   (aref (cdr community) 9))
1568
1569 (defun mixi-community-set-name (community name)
1570   "Set the name of COMMUNITY."
1571   (unless (mixi-community-p community)
1572     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1573   (aset (cdr community) 2 name))
1574
1575 (defun mixi-community-set-birthday (community birthday)
1576   "Set the birthday of COMMUNITY."
1577   (unless (mixi-community-p community)
1578     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1579   (aset (cdr community) 3 birthday))
1580
1581 (defun mixi-community-set-owner (community owner)
1582   "Set the owner of COMMUNITY."
1583   (unless (mixi-community-p community)
1584     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1585   (unless (mixi-friend-p owner)
1586     (signal 'wrong-type-argument (list 'mixi-friend-p owner)))
1587   (aset (cdr community) 4 owner))
1588
1589 (defun mixi-community-set-category (community category)
1590   "Set the category of COMMUNITY."
1591   (unless (mixi-community-p community)
1592     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1593   (aset (cdr community) 5 category))
1594
1595 (defun mixi-community-set-members (community members)
1596   "Set the name of COMMUNITY."
1597   (unless (mixi-community-p community)
1598     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1599   (aset (cdr community) 6 members))
1600
1601 (defun mixi-community-set-open-level (community open-level)
1602   "Set the name of COMMUNITY."
1603   (unless (mixi-community-p community)
1604     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1605   (aset (cdr community) 7 open-level))
1606
1607 (defun mixi-community-set-authority (community authority)
1608   "Set the name of COMMUNITY."
1609   (unless (mixi-community-p community)
1610     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1611   (aset (cdr community) 8 authority))
1612
1613 (defun mixi-community-set-description (community description)
1614   "Set the name of COMMUNITY."
1615   (unless (mixi-community-p community)
1616     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1617   (aset (cdr community) 9 description))
1618
1619 (defmacro mixi-community-list-page (&optional friend)
1620   `(concat "/list_community.pl?page=%d"
1621            (when ,friend (concat "&id=" (mixi-friend-id ,friend)))))
1622
1623 (defconst mixi-community-list-id-regexp
1624   "<a href=view_community\\.pl\\?id=\\([0-9]+\\)")
1625 (defconst mixi-community-list-name-regexp
1626   "<td valign=middle>\\(.+\\)([0-9]+)</td>")
1627
1628 ;;;###autoload
1629 (defun mixi-get-communities (&rest friend-or-range)
1630   "Get communities of FRIEND."
1631   (when (> (length friend-or-range) 2)
1632     (signal 'wrong-number-of-arguments
1633             (list 'mixi-get-communities (length friend-or-range))))
1634   (let ((friend (nth 0 friend-or-range))
1635         (range (nth 1 friend-or-range)))
1636     (when (or (not (mixi-friend-p friend)) (mixi-friend-p range))
1637       (setq friend (nth 1 friend-or-range))
1638       (setq range (nth 0 friend-or-range)))
1639     (unless (or (null friend) (mixi-friend-p friend))
1640       (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
1641     (let ((ids (mixi-get-matched-items (mixi-community-list-page friend)
1642                                        mixi-community-list-id-regexp
1643                                        range))
1644           (names (mixi-get-matched-items (mixi-community-list-page friend)
1645                                          mixi-community-list-name-regexp
1646                                          range)))
1647       (let ((index 0)
1648             ret)
1649         (while (< index (length ids))
1650           (setq ret (cons (mixi-make-community (nth 0 (nth index ids))
1651                                                (nth 0 (nth index names))) ret))
1652           (incf index))
1653         (reverse ret)))))
1654
1655 (defmacro mixi-search-community-list-page (keyword)
1656   `(concat "/search_community.pl?page=%d&&sort=date&type=com&submit=main"
1657            "&keyword=" (mixi-url-encode-and-quote-percent-string ,keyword)
1658            "&category_id=0"))
1659
1660 (defconst mixi-search-community-list-regexp
1661   "<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>
1662 <td NOWRAP WIDTH=90 BGCOLOR=#FDF9F2><font COLOR=#996600>¥³¥ß¥å¥Ë¥Æ¥£Ì¾</font></td>
1663 <td COLSPAN=2 WIDTH=370 BGCOLOR=#FFFFFF>\\([^<]+\\)</td></tr>")
1664
1665 ;; FIXME: Support category.
1666 ;;;###autoload
1667 (defun mixi-search-communities (keyword &optional range)
1668   (let ((items (mixi-get-matched-items (mixi-search-community-list-page
1669                                         keyword)
1670                                        mixi-search-community-list-regexp
1671                                        range)))
1672     (mapcar (lambda (item)
1673               (mixi-make-community (nth 0 item) (nth 1 item)))
1674             items)))
1675
1676 ;; Topic object.
1677 (defvar mixi-topic-cache (make-hash-table :test 'equal))
1678 (defun mixi-make-topic (community id &optional comment-count time title owner
1679                                   content)
1680   "Return a topic object."
1681   (mixi-make-cache (list (mixi-community-id community) id)
1682                    (cons 'mixi-topic (vector nil community id comment-count
1683                                              time title owner content))
1684                    mixi-topic-cache))
1685
1686 (defconst mixi-topic-url-regexp
1687   "/view_bbs\\.pl\\?id=\\([0-9]+\\)\\(&comment_count=\\([0-9]+\\)\\)?&comm_id=\\([0-9]+\\)")
1688
1689 (defun mixi-make-topic-from-url (url)
1690   "Return a topic object from URL."
1691   (when (string-match mixi-topic-url-regexp url)
1692     (let ((id (match-string 1 url))
1693           (comment-count (match-string 3 url))
1694           (community-id (match-string 4 url)))
1695       (mixi-make-topic (mixi-make-community community-id) id comment-count))))
1696
1697 (defmacro mixi-topic-p (topic)
1698   `(eq (mixi-object-class ,topic) 'mixi-topic))
1699
1700 (defmacro mixi-topic-page (topic)
1701   `(concat "/view_bbs.pl?id=" (mixi-topic-id ,topic)
1702            "&comm_id=" (mixi-community-id (mixi-topic-community ,topic))))
1703
1704 (defconst mixi-topic-community-regexp
1705   "<td width=\"595\" background=\"http://img\\.mixi\\.jp/img/bg_w\\.gif\"><b>\\[\\(.+\\)\\] ¥È¥Ô¥Ã¥¯</b></td>")
1706 (defconst mixi-topic-time-regexp
1707   "<td rowspan=\"3\" width=\"110\" bgcolor=\"#ffd8b0\" align=\"center\" valign=\"top\" nowrap>\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü<br>\\([0-9]+\\):\\([0-9]+\\)</td>")
1708 (defconst mixi-topic-title-regexp
1709   "<td bgcolor=\"#fff4e0\">&nbsp;\\([^<]+\\)</td>")
1710 (defconst mixi-topic-owner-regexp
1711   "<td bgcolor=\"#fdf9f2\">&nbsp;<font color=\"#dfb479\"></font>&nbsp;<a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.*?\\)\\(¤µ¤ó\\)?</a>")
1712 (defconst mixi-topic-content-regexp
1713   "<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>")
1714
1715 (defun mixi-realize-topic (topic &optional page)
1716   "Realize a TOPIC."
1717   ;; FIXME: Check a expiration of cache?
1718   (unless (mixi-object-realized-p topic)
1719     (with-mixi-retrieve (or page (mixi-topic-page topic))
1720       (if (re-search-forward mixi-topic-community-regexp nil t)
1721           (mixi-community-set-name (mixi-topic-community topic)
1722                                    (match-string 1))
1723         (mixi-realization-error 'cannot-find-community topic))
1724       (if (re-search-forward mixi-topic-time-regexp nil t)
1725           (mixi-topic-set-time
1726            topic (encode-time 0 (string-to-number (match-string 5))
1727                               (string-to-number (match-string 4))
1728                               (string-to-number (match-string 3))
1729                               (string-to-number (match-string 2))
1730                               (string-to-number (match-string 1))))
1731         (mixi-realization-error 'cannot-find-time topic))
1732       (if (re-search-forward mixi-topic-title-regexp nil t)
1733           (mixi-topic-set-title topic (match-string 1))
1734         (mixi-realization-error 'cannot-find-title topic))
1735       (if (re-search-forward mixi-topic-owner-regexp nil t)
1736           (mixi-topic-set-owner topic (mixi-make-friend (match-string 1)
1737                                                         (match-string 2)))
1738         (mixi-realization-error 'cannot-find-owner topic))
1739       (if (re-search-forward mixi-topic-content-regexp nil t)
1740           (mixi-topic-set-content topic (match-string 2))
1741         (mixi-realization-error 'cannot-find-content topic)))
1742     (mixi-object-touch topic)))
1743
1744 (defun mixi-topic-community (topic)
1745   "Return the community of TOPIC."
1746   (unless (mixi-topic-p topic)
1747     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1748   (aref (cdr topic) 1))
1749
1750 (defun mixi-topic-id (topic)
1751   "Return the id of TOPIC."
1752   (unless (mixi-topic-p topic)
1753     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1754   (aref (cdr topic) 2))
1755
1756 (defun mixi-topic-comment-count (topic)
1757   "Return the comment-count of TOPIC."
1758   (unless (mixi-topic-p topic)
1759     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1760   (aref (cdr topic) 3))
1761
1762 (defun mixi-topic-time (topic)
1763   "Return the time of TOPIC."
1764   (unless (mixi-topic-p topic)
1765     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1766   (mixi-realize-topic topic)
1767   (aref (cdr topic) 4))
1768
1769 (defun mixi-topic-title (topic)
1770   "Return the title of TOPIC."
1771   (unless (mixi-topic-p topic)
1772     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1773   (mixi-realize-topic topic)
1774   (aref (cdr topic) 5))
1775
1776 (defun mixi-topic-owner (topic)
1777   "Return the owner of TOPIC."
1778   (unless (mixi-topic-p topic)
1779     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1780   (mixi-realize-topic topic)
1781   (aref (cdr topic) 6))
1782
1783 (defun mixi-topic-content (topic)
1784   "Return the content of TOPIC."
1785   (unless (mixi-topic-p topic)
1786     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1787   (mixi-realize-topic topic)
1788   (aref (cdr topic) 7))
1789
1790 (defun mixi-topic-set-comment-count (topic comment-count)
1791   "Set the comment-count of TOPIC."
1792   (unless (mixi-topic-p topic)
1793     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1794   (aset (cdr topic) 3 comment-count))
1795
1796 (defun mixi-topic-set-time (topic time)
1797   "Set the time of TOPIC."
1798   (unless (mixi-topic-p topic)
1799     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1800   (aset (cdr topic) 4 time))
1801
1802 (defun mixi-topic-set-title (topic title)
1803   "Set the title of TOPIC."
1804   (unless (mixi-topic-p topic)
1805     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1806   (aset (cdr topic) 5 title))
1807
1808 (defun mixi-topic-set-owner (topic owner)
1809   "Set the owner of TOPIC."
1810   (unless (mixi-topic-p topic)
1811     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1812   (unless (mixi-friend-p owner)
1813     (signal 'wrong-type-argument (list 'mixi-friend-p owner)))
1814   (aset (cdr topic) 6 owner))
1815
1816 (defun mixi-topic-set-content (topic content)
1817   "Set the content of TOPIC."
1818   (unless (mixi-topic-p topic)
1819     (signal 'wrong-type-argument (list 'mixi-topic-p topic)))
1820   (aset (cdr topic) 7 content))
1821
1822 (defmacro mixi-post-topic-page (community)
1823   `(concat "/add_bbs.pl?id=" (mixi-community-id community)))
1824
1825 ;; FIXME: Support photos.
1826 ;;;###autoload
1827 (defun mixi-post-topic (community title content)
1828   "Post a topic to COMMUNITY."
1829   (unless (mixi-community-p community)
1830     (signal 'wrong-type-argument (list 'mixi-community-p community)))
1831   (unless (stringp title)
1832     (signal 'wrong-type-argument (list 'stringp title)))
1833   (unless (stringp content)
1834     (signal 'wrong-type-argument (list 'stringp content)))
1835   (let ((fields `(("bbs_title" . ,title)
1836                   ("bbs_body" . ,content)
1837                   ("submit" . "main")))
1838         post-key)
1839     (with-mixi-post-form (mixi-post-topic-page community) fields
1840       (if (re-search-forward mixi-post-key-regexp nil t)
1841           (setq post-key (match-string 1))
1842         (mixi-post-error 'cannot-find-key community)))
1843     (setq fields `(("post_key" . ,post-key)
1844                    ("bbs_title" . ,title)
1845                    ("bbs_body" . ,content)
1846                    ("submit" . "confirm")))
1847     (with-mixi-post-form (mixi-post-topic-page community) fields
1848       (unless (re-search-forward mixi-post-succeed-regexp nil t)
1849         (mixi-post-error 'cannot-find-succeed community)))))
1850
1851 ;; Event object.
1852 (defvar mixi-event-cache (make-hash-table :test 'equal))
1853 (defun mixi-make-event (community id &optional comment-count time title owner
1854                                   date place detail limit members)
1855   "Return a event object."
1856   (mixi-make-cache (list (mixi-community-id community) id)
1857                    (cons 'mixi-event (vector nil community id comment-count
1858                                              time title owner date place
1859                                              detail limit members))
1860                    mixi-event-cache))
1861
1862 (defconst mixi-event-url-regexp
1863   "/view_event\\.pl\\?id=\\([0-9]+\\)\\(&comment_count=\\([0-9]+\\)\\)?&comm_id=\\([0-9]+\\)")
1864
1865 (defun mixi-make-event-from-url (url)
1866   "Return a event object from URL."
1867   (when (string-match mixi-event-url-regexp url)
1868     (let ((id (match-string 1 url))
1869           (comment-count (match-string 3 url))
1870           (community-id (match-string 4 url)))
1871       (mixi-make-event (mixi-make-community community-id) id comment-count))))
1872
1873 (defmacro mixi-event-p (event)
1874   `(eq (mixi-object-class ,event) 'mixi-event))
1875
1876 (defmacro mixi-event-page (event)
1877   `(concat "/view_event.pl?id=" (mixi-event-id ,event)
1878            "&comm_id=" (mixi-community-id (mixi-event-community ,event))))
1879
1880 (defconst mixi-event-community-regexp
1881   "<td WIDTH=595 background=http://img\\.mixi\\.jp/img/bg_w\\.gif><b>\\[\\(.+\\)\\] ¥¤¥Ù¥ó¥È</b></td>")
1882 (defconst mixi-event-time-regexp
1883   "<td ROWSPAN=11 \\(BGCOLOR\\|bgcolor\\)=#FFD8B0 \\(ALIGN\\|align\\)=center \\(VALIGN\\|Valign\\)=top WIDTH=110>
1884 ?\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü<br>
1885 ?\\([0-9]+\\):\\([0-9]+\\)</td>")
1886 (defconst mixi-event-title-regexp
1887   "<td bgcolor=#FFF4E0\\( width=410\\)?>&nbsp;\\([^<]+\\)</td>")
1888 (defconst mixi-event-owner-regexp
1889   "<td \\(BGCOLOR\\|bgcolor\\)=#FDF9F2>&nbsp;<a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.*\\)</a>")
1890 (defconst mixi-event-owner-seceded-regexp
1891   "<td \\(BGCOLOR\\|bgcolor\\)=#FDF9F2>&nbsp;\\((mixi Âà²ñºÑ)\\)")
1892 (defconst mixi-event-date-regexp
1893   "<td \\(BGCOLOR\\|bgcolor\\)=#\\(FFFFFF\\|ffffff\\) \\(ALIGN\\|align\\)=center NOWRAP>³«ºÅÆü»þ</td>
1894 <td \\(BGCOLOR\\|bgcolor\\)=#\\(FFFFFF\\|ffffff\\)>
1895 &nbsp;\\(.+\\)
1896 </td>")
1897 (defconst mixi-event-place-regexp
1898   "<td \\(BGCOLOR\\|bgcolor\\)=#\\(FFFFFF\\|ffffff\\) \\(ALIGN\\|align\\)=center NOWRAP>³«ºÅ¾ì½ê</td>
1899 <td \\(BGCOLOR\\|bgcolor\\)=#\\(FFFFFF\\|ffffff\\)>
1900 &nbsp;\\(.+\\)
1901 </td>")
1902 (defconst mixi-event-detail-regexp
1903   "<td \\(BGCOLOR\\|bgcolor\\)=#\\(FFFFFF\\|ffffff\\) \\(ALIGN\\|align\\)=center NOWRAP>¾ÜºÙ</td>
1904 <td \\(BGCOLOR\\|bgcolor\\)=#\\(FFFFFF\\|ffffff\\)><table BORDER=0 CELLSPACING=0 CELLPADDING=5><tr><td CLASS=h120>\\(.+\\)</td></tr></table></td>")
1905 (defconst mixi-event-limit-regexp
1906   "<td \\(BGCOLOR\\|bgcolor\\)=\"?#\\(FFFFFF\\|ffffff\\)\"? \\(ALIGN\\|align\\)=\"?center\"? NOWRAP>Ê罸´ü¸Â</td>
1907 ?<td \\(BGCOLOR\\|bgcolor\\)=#\\(FFFFFF\\|ffffff\\)>&nbsp;\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü</td>")
1908 (defconst mixi-event-members-regexp
1909   "<td \\(BGCOLOR\\|bgcolor\\)=#\\(FFFFFF\\|ffffff\\) \\(ALIGN\\|align\\)=center NOWRAP>»²²Ã¼Ô</td>
1910 <td \\(BGCOLOR\\|bgcolor\\)=#\\(FFFFFF\\|ffffff\\)>
1911
1912 ?
1913 ?<table BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH=100%>
1914 <tr>
1915
1916 ?<td>&nbsp;\\(.+\\)</td>")
1917
1918 (defun mixi-realize-event (event &optional page)
1919   "Realize a EVENT."
1920   ;; FIXME: Check a expiration of cache?
1921   (unless (mixi-object-realized-p event)
1922     (with-mixi-retrieve (or page (mixi-event-page event))
1923       (if (re-search-forward mixi-event-community-regexp nil t)
1924           (mixi-community-set-name (mixi-event-community event)
1925                                    (match-string 1))
1926         (mixi-realization-error 'cannot-find-community event))
1927       (if (re-search-forward mixi-event-time-regexp nil t)
1928           (mixi-event-set-time
1929            event (encode-time 0 (string-to-number (match-string 8))
1930                               (string-to-number (match-string 7))
1931                               (string-to-number (match-string 6))
1932                               (string-to-number (match-string 5))
1933                               (string-to-number (match-string 4))))
1934         (mixi-realization-error 'cannot-find-time event))
1935       (if (re-search-forward mixi-event-title-regexp nil t)
1936           (mixi-event-set-title event (match-string 2))
1937         (mixi-realization-error 'cannot-find-title event))
1938       (if (re-search-forward mixi-event-owner-regexp nil t)
1939           (mixi-event-set-owner event (mixi-make-friend (match-string 2)
1940                                                         (match-string 3)))
1941         (if (re-search-forward mixi-event-owner-seceded-regexp nil t)
1942             (mixi-event-set-owner event
1943                                   (mixi-make-friend nil (match-string 2)))
1944           (mixi-realization-error 'cannot-find-owner event)))
1945       (if (re-search-forward mixi-event-date-regexp nil t)
1946           (mixi-event-set-date event (match-string 6))
1947         (mixi-realization-error 'cannot-find-date event))
1948       (if (re-search-forward mixi-event-place-regexp nil t)
1949           (mixi-event-set-place event (match-string 6))
1950         (mixi-realization-error 'cannot-find-place event))
1951       (if (re-search-forward mixi-event-detail-regexp nil t)
1952           (mixi-event-set-detail event (match-string 6))
1953         (mixi-realization-error 'cannot-find-detail event))
1954       (when (re-search-forward mixi-event-limit-regexp nil t)
1955         (mixi-event-set-limit
1956          event (encode-time 0 0 0 (string-to-number (match-string 8))
1957                             (string-to-number (match-string 7))
1958                             (string-to-number (match-string 6)))))
1959       (if (re-search-forward mixi-event-members-regexp nil t)
1960           (mixi-event-set-members event (match-string 6))
1961         (mixi-realization-error 'cannot-find-members event)))
1962     (mixi-object-touch event)))
1963
1964 (defun mixi-event-community (event)
1965   "Return the community of EVENT."
1966   (unless (mixi-event-p event)
1967     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1968   (aref (cdr event) 1))
1969
1970 (defun mixi-event-id (event)
1971   "Return the id of EVENT."
1972   (unless (mixi-event-p event)
1973     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1974   (aref (cdr event) 2))
1975
1976 (defun mixi-event-comment-count (event)
1977   "Return the comment-count of EVENT."
1978   (unless (mixi-event-p event)
1979     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1980   (aref (cdr event) 3))
1981
1982 (defun mixi-event-time (event)
1983   "Return the time of EVENT."
1984   (unless (mixi-event-p event)
1985     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1986   (mixi-realize-event event)
1987   (aref (cdr event) 4))
1988
1989 (defun mixi-event-title (event)
1990   "Return the title of EVENT."
1991   (unless (mixi-event-p event)
1992     (signal 'wrong-type-argument (list 'mixi-event-p event)))
1993   (mixi-realize-event event)
1994   (aref (cdr event) 5))
1995
1996 (defun mixi-event-owner (event)
1997   "Return the owner of EVENT."
1998   (unless (mixi-event-p event)
1999     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2000   (mixi-realize-event event)
2001   (aref (cdr event) 6))
2002
2003 (defun mixi-event-date (event)
2004   "Return the date of EVENT."
2005   (unless (mixi-event-p event)
2006     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2007   (mixi-realize-event event)
2008   (aref (cdr event) 7))
2009
2010 (defun mixi-event-place (event)
2011   "Return the place of EVENT."
2012   (unless (mixi-event-p event)
2013     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2014   (mixi-realize-event event)
2015   (aref (cdr event) 8))
2016
2017 (defun mixi-event-detail (event)
2018   "Return the detail of EVENT."
2019   (unless (mixi-event-p event)
2020     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2021   (mixi-realize-event event)
2022   (aref (cdr event) 9))
2023
2024 (defun mixi-event-limit (event)
2025   "Return the limit of EVENT."
2026   (unless (mixi-event-p event)
2027     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2028   (mixi-realize-event event)
2029   (aref (cdr event) 10))
2030
2031 (defun mixi-event-members (event)
2032   "Return the members of EVENT."
2033   (unless (mixi-event-p event)
2034     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2035   (mixi-realize-event event)
2036   (aref (cdr event) 11))
2037
2038 (defun mixi-event-set-comment-count (event comment-count)
2039   "Set the comment-count of EVENT."
2040   (unless (mixi-event-p event)
2041     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2042   (aset (cdr event) 3 comment-count))
2043
2044 (defun mixi-event-set-time (event time)
2045   "Set the time of EVENT."
2046   (unless (mixi-event-p event)
2047     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2048   (aset (cdr event) 4 time))
2049
2050 (defun mixi-event-set-title (event title)
2051   "Set the title of EVENT."
2052   (unless (mixi-event-p event)
2053     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2054   (aset (cdr event) 5 title))
2055
2056 (defun mixi-event-set-owner (event owner)
2057   "Set the owner of EVENT."
2058   (unless (mixi-event-p event)
2059     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2060   (unless (mixi-friend-p owner)
2061     (signal 'wrong-type-argument (list 'mixi-friend-p owner)))
2062   (aset (cdr event) 6 owner))
2063
2064 (defun mixi-event-set-date (event date)
2065   "Set the date of EVENT."
2066   (unless (mixi-event-p event)
2067     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2068   (aset (cdr event) 7 date))
2069
2070 (defun mixi-event-set-place (event place)
2071   "Set the place of EVENT."
2072   (unless (mixi-event-p event)
2073     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2074   (aset (cdr event) 8 place))
2075
2076 (defun mixi-event-set-detail (event detail)
2077   "Set the detail of EVENT."
2078   (unless (mixi-event-p event)
2079     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2080   (aset (cdr event) 9 detail))
2081
2082 (defun mixi-event-set-limit (event limit)
2083   "Set the limit of EVENT."
2084   (unless (mixi-event-p event)
2085     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2086   (aset (cdr event) 10 limit))
2087
2088 (defun mixi-event-set-members (event members)
2089   "Set the members of EVENT."
2090   (unless (mixi-event-p event)
2091     (signal 'wrong-type-argument (list 'mixi-event-p event)))
2092   (aset (cdr event) 11 members))
2093
2094 ;; BBS object.
2095 (defconst mixi-bbs-list '(mixi-topic mixi-event))
2096
2097 (defmacro mixi-bbs-p (bbs)
2098   `(memq (mixi-object-class ,bbs) mixi-bbs-list))
2099
2100 (defun mixi-bbs-community (bbs)
2101   "Return the community of BBS."
2102   (unless (mixi-bbs-p bbs)
2103     (signal 'wrong-type-argument (list 'mixi-bbs-p bbs)))
2104   (let ((func (intern (concat mixi-object-prefix
2105                               (mixi-object-name bbs) "-community"))))
2106     (funcall func bbs)))
2107
2108 (defun mixi-bbs-comment-count (bbs)
2109   "Return the comment-count of BBS."
2110   (unless (mixi-bbs-p bbs)
2111     (signal 'wrong-type-argument (list 'mixi-bbs-p bbs)))
2112   (let ((func (intern (concat mixi-object-prefix
2113                               (mixi-object-name bbs) "-comment-count"))))
2114     (funcall func bbs)))
2115
2116 (defun mixi-bbs-set-comment-count (bbs count)
2117   "Set the comment-count of BBS."
2118   (unless (mixi-bbs-p bbs)
2119     (signal 'wrong-type-argument (list 'mixi-bbs-p bbs)))
2120   (let ((func (intern (concat mixi-object-prefix
2121                               (mixi-object-name bbs) "-set-comment-count"))))
2122     (funcall func bbs count)))
2123
2124 (defalias 'mixi-bbs-id 'mixi-object-id)
2125 (defalias 'mixi-bbs-time 'mixi-object-time)
2126 (defalias 'mixi-bbs-title 'mixi-object-title)
2127 (defalias 'mixi-bbs-owner 'mixi-object-owner)
2128 (defalias 'mixi-bbs-content 'mixi-object-content)
2129
2130 (defmacro mixi-bbs-list-page (community)
2131   `(concat "/list_bbs.pl?page=%d"
2132            "&id=" (mixi-community-id ,community)))
2133
2134 (defconst mixi-bbs-list-regexp
2135   "<a href=view_\\(bbs\\|event\\)\\.pl\\?id=\\([0-9]+\\)")
2136
2137 ;;;###autoload
2138 (defun mixi-get-bbses (community &optional range)
2139   "Get bbese of COMMUNITY."
2140   (unless (mixi-community-p community)
2141     (signal 'wrong-type-argument (list 'mixi-community-p community)))
2142   (let ((items (mixi-get-matched-items (mixi-bbs-list-page community)
2143                                        mixi-bbs-list-regexp
2144                                        range)))
2145     (mapcar (lambda (item)
2146               (let ((name (nth 0 item)))
2147                 (when (string= name "bbs")
2148                   (setq name "topic"))
2149                 (let ((func (intern (concat "mixi-make-" name))))
2150                   (funcall func community (nth 1 item)))))
2151             items)))
2152
2153 (defmacro mixi-new-bbs-list-page ()
2154   `(concat "/new_bbs.pl?page=%d"))
2155
2156 (defconst mixi-new-bbs-list-regexp
2157   "<a href=\"view_\\(bbs\\|event\\)\\.pl\\?id=\\([0-9]+\\)&comment_count=\\([0-9]+\\)&comm_id=\\([0-9]+\\)\" class=\"new_link\">")
2158
2159 ;;;###autoload
2160 (defun mixi-get-new-bbses (&optional range)
2161   "Get new topics."
2162   (let ((items (mixi-get-matched-items (mixi-new-bbs-list-page)
2163                                        mixi-new-bbs-list-regexp
2164                                        range)))
2165     (delq nil
2166           (mapcar (lambda (item)
2167                     (let ((name (nth 0 item)))
2168                       (when (string= name "bbs")
2169                         (setq name "topic"))
2170                       (let* ((func (intern (concat "mixi-make-" name)))
2171                              (bbs (funcall func
2172                                            (mixi-make-community (nth 3 item))
2173                                            (nth 1 item)))
2174                              (comment-count (mixi-bbs-comment-count bbs))
2175                              (count (string-to-number (nth 2 item))))
2176                         (when (or (null comment-count)
2177                                   (< comment-count count))
2178                           (mixi-bbs-set-comment-count bbs count)
2179                           bbs))))
2180                   items))))
2181  
2182 (defmacro mixi-search-bbs-list-page (keyword)
2183   `(concat "/search_topic.pl?page=%d&type=top&submit=search"
2184            "&keyword=" (mixi-url-encode-and-quote-percent-string ,keyword)
2185            "&community_id=0&category_id=0"))
2186
2187 (defconst mixi-search-bbs-list-regexp
2188   "<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>")
2189
2190 ;; FIXME: Support community and category.
2191 ;;;###autoload
2192 (defun mixi-search-bbses (keyword &optional range)
2193   (let ((items (mixi-get-matched-items (mixi-search-bbs-list-page keyword)
2194                                        mixi-search-bbs-list-regexp
2195                                        range)))
2196     (mapcar (lambda (item)
2197               (let ((name (nth 0 item)))
2198                 (when (string= name "bbs")
2199                   (setq name "topic"))
2200                 (let ((func (intern (concat "mixi-make-" name))))
2201                   (funcall func (mixi-make-community (nth 2 item))
2202                            (nth 1 item)))))
2203             items)))
2204
2205 ;; Parent object.
2206 (defmacro mixi-parent-p (object)
2207   `(or (eq (mixi-object-class ,object) 'mixi-diary)
2208        (mixi-bbs-p ,object)))
2209
2210 (defun mixi-realize-parent (parent &optional page)
2211   "Realize a PARENT."
2212   (unless (mixi-parent-p parent)
2213     (signal 'wrong-type-argument (list 'mixi-parent-p parent)))
2214   (let ((func (intern (concat mixi-object-prefix "realize-"
2215                               (mixi-object-name parent)))))
2216     (funcall func parent page)))
2217
2218 ;; Comment object.
2219 (defun mixi-make-comment (parent owner time content)
2220   "Return a comment object."
2221   (cons 'mixi-comment (vector parent owner time content)))
2222
2223 (defmacro mixi-comment-p (comment)
2224   `(eq (mixi-object-class ,comment) 'mixi-comment))
2225
2226 (defun mixi-comment-parent (comment)
2227   "Return the parent of COMMENT."
2228   (unless (mixi-comment-p comment)
2229     (signal 'wrong-type-argument (list 'mixi-comment-p comment)))
2230   (aref (cdr comment) 0))
2231
2232 (defun mixi-comment-owner (comment)
2233   "Return the owner of COMMENT."
2234   (unless (mixi-comment-p comment)
2235     (signal 'wrong-type-argument (list 'mixi-comment-p comment)))
2236   (aref (cdr comment) 1))
2237
2238 (defun mixi-comment-time (comment)
2239   "Return the time of COMMENT."
2240   (unless (mixi-comment-p comment)
2241     (signal 'wrong-type-argument (list 'mixi-comment-p comment)))
2242   (aref (cdr comment) 2))
2243
2244 (defun mixi-comment-content (comment)
2245   "Return the content of COMMENT."
2246   (unless (mixi-comment-p comment)
2247     (signal 'wrong-type-argument (list 'mixi-comment-p comment)))
2248   (aref (cdr comment) 3))
2249
2250 (defun mixi-diary-comment-list-page (diary)
2251   (concat "/view_diary.pl?full=1"
2252           "&id=" (mixi-diary-id diary)
2253           "&owner_id=" (mixi-friend-id (mixi-diary-owner diary))))
2254
2255 ;; FIXME: Split regexp to time, owner(id and nick) and contents.
2256 (defconst mixi-diary-comment-list-regexp
2257 "<td rowspan=\"2\" align=\"center\" width=\"95\" bgcolor=\"#f2ddb7\" nowrap>
2258 \\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü<br>\\([0-9]+\\):\\([0-9]+\\)\\(<br>
2259 <input type=checkbox name=comment_id value=\".+\">
2260 \\|\\)
2261 </td>
2262 <td ALIGN=center BGCOLOR=#FDF9F2 WIDTH=430>
2263 <table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"410\">
2264 <tr>
2265 \\(<td>\\)
2266 <a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.*\\)</a>
2267
2268 \\(<font color=\"#f2ddb7\">|</font> <a href=[^>]+>ºï½ü</a>
2269
2270 \\|\\)</td>
2271 </tr>
2272 </table>
2273 </td>
2274 </tr>
2275 <!-- [^ ]+ : start -->
2276 <tr>
2277 <td bgcolor=\"#ffffff\">
2278 <table BORDER=0 CELLSPACING=0 CELLPADDING=[35] WIDTH=410>
2279 <tr>
2280 <td CLASS=h12>
2281 \\(.+\\)
2282 </td></tr></table>")
2283
2284 (defun mixi-topic-comment-list-page (topic)
2285   (concat "/view_bbs.pl?page=all"
2286           "&id=" (mixi-topic-id topic)
2287           "&comm_id=" (mixi-community-id (mixi-topic-community topic))))
2288
2289 ;; FIXME: Split regexp to time, owner(id and nick) and contents.
2290 (defconst mixi-topic-comment-list-regexp
2291   "<tr valign=\"top\">
2292 <td rowspan=\"2\" width=\"110\" bgcolor=\"#f2ddb7\" align=\"center\" nowrap>
2293 \\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü<br>
2294 \\([0-9]+\\):\\([0-9]+\\)<br>
2295 \\(<input type=\"checkbox\" name=\"comment_id\" value=\".+\">
2296 \\|\\)</td>
2297 <td bgcolor=\"#fdf9f2\">&nbsp;<font color=\"#f8a448\">
2298 <b>[^<]+</b>:</font>&nbsp;
2299 \\(
2300 \\|\\) *<a href=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.*\\)</a>
2301
2302 ?\\(
2303
2304 \\|<font color=\"#f2ddb7\">|&nbsp;</font><a href=\"delete_bbs_comment\\.pl\\?id=[0-9]+&comm_id=[0-9]+&comment_id=[0-9]+\">ºï½ü</a>
2305 \\|\\)</td>
2306 </tr>
2307 <tr>
2308 <td bgcolor=\"#ffffff\" align=\"center\">
2309 <table border=\"0\" cellspacing=\"0\" cellpadding=\"5\" width=\"500\">
2310 <tr>
2311 <td class=\"h120\">
2312
2313 \\(.+\\)
2314 </td>
2315 </tr>
2316 </table>
2317 </td>
2318 </tr>")
2319
2320 (defun mixi-event-comment-list-page (event)
2321   (concat "/view_event.pl?page=all"
2322           "&id=" (mixi-event-id event)
2323           "&comm_id=" (mixi-community-id (mixi-event-community event))))
2324
2325 ;; FIXME: Split regexp to time, owner(id and nick) and contents.
2326 (defconst mixi-event-comment-list-regexp
2327   "<tr>
2328 <td ROWSPAN=2 ALIGN=center BGCOLOR=#F2DDB7 WIDTH=110>
2329 \\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü<br>
2330 \\([0-9]+\\):\\([0-9]+\\)<br>
2331 \\(</td>\\)
2332 \\(<td BGCOLOR=#FDF9F2>\\)
2333 <font COLOR=#F8A448><b>[^<]+</b> :</font>
2334 <a HREF=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.*\\)</a>
2335
2336 \\(<font COLOR=#F2DDB7>|</font>
2337 <a href=\"delete_bbs_comment\\.pl\\?id=[0-9]+&comm_id=[0-9]+&comment_id=[0-9]+&type=event\">ºï½ü</a>
2338
2339 \\|\\)</td>
2340 </tr>
2341 <tr>
2342 <td ALIGN=center BGCOLOR=#FFFFFF>
2343 <table BORDER=0 CELLSPACING=0 CELLPADDING=5 WIDTH=500>
2344 <tr><td CLASS=h120>\\(.+\\)</td></tr>
2345 </table>
2346 </td>
2347 </tr>")
2348
2349 ;;;###autoload
2350 (defun mixi-get-comments (parent &optional range)
2351   "Get comments of PARENT."
2352   (unless (mixi-parent-p parent)
2353     (signal 'wrong-type-argument (list 'mixi-parent-p parent)))
2354   (let* ((name (mixi-object-name parent))
2355          (list-page (intern (concat mixi-object-prefix name
2356                                     "-comment-list-page")))
2357          (regexp (eval (intern (concat mixi-object-prefix name
2358                                        "-comment-list-regexp"))))
2359          (page (funcall list-page parent)))
2360     (unless (mixi-object-realized-p parent)
2361       (mixi-realize-parent parent page)
2362       (setq page nil))
2363     (let ((items (mixi-get-matched-items page regexp range t)))
2364       (mapcar (lambda (item)
2365                 (mixi-make-comment parent (mixi-make-friend
2366                                            (nth 7 item) (nth 8 item))
2367                                    (encode-time
2368                                     0
2369                                     (string-to-number (nth 4 item))
2370                                     (string-to-number (nth 3 item))
2371                                     (string-to-number (nth 2 item))
2372                                     (string-to-number (nth 1 item))
2373                                     (string-to-number (nth 0 item)))
2374                                    (nth 10 item)))
2375               items))))
2376
2377 (defmacro mixi-new-comment-list-page ()
2378   `(concat "/new_comment.pl?page=%d"))
2379
2380 (defconst mixi-new-comment-list-regexp
2381   "<a href=\"view_diary\\.pl\\?id=\\([0-9]+\\)&owner_id=\\([0-9]+\\)&comment_count=\\([0-9]+\\)\" class=\"new_link\">")
2382
2383 ;;;###autoload
2384 (defun mixi-get-new-comments (&optional range)
2385   "Get new comments."
2386   (let ((items (mixi-get-matched-items (mixi-new-comment-list-page)
2387                                        mixi-new-comment-list-regexp
2388                                        range)))
2389     (delq nil
2390           (mapcar (lambda (item)
2391                     (let* ((diary (mixi-make-diary
2392                                    (mixi-make-friend (nth 1 item))
2393                                    (nth 0 item)))
2394                            (comment-count (mixi-diary-comment-count diary))
2395                            (count (string-to-number (nth 2 item))))
2396                       (when (or (null comment-count)
2397                                 (< comment-count count))
2398                         (mixi-diary-set-comment-count diary count)
2399                         diary)))
2400                   items))))
2401
2402 (defun mixi-post-diary-comment-page (diary)
2403   (concat "/add_comment.pl?&diary_id=" (mixi-diary-id diary)))
2404
2405 (defun mixi-post-topic-comment-page (topic)
2406   (concat "/add_bbs_comment.pl?id=" (mixi-topic-id topic)
2407           "&comm_id=" (mixi-community-id (mixi-topic-community topic))))
2408
2409 (defun mixi-post-event-comment-page (event)
2410   (concat "/add_event_comment.pl?id=" (mixi-event-id event)
2411           "&comm_id=" (mixi-community-id (mixi-event-community event))))
2412
2413 ;; FIXME: Support photos.
2414 ;;;###autoload
2415 (defun mixi-post-comment (parent content)
2416   "Post a comment to PARENT."
2417   (unless (mixi-object-p parent)
2418     (signal 'wrong-type-argument (list 'mixi-object-p parent)))
2419   (unless (stringp content)
2420     (signal 'wrong-type-argument (list 'stringp content)))
2421   (let* ((name (mixi-object-name parent))
2422          (page (intern (concat mixi-object-prefix "post-" name
2423                                "-comment-page")))
2424          fields post-key)
2425     (if (mixi-diary-p parent)
2426         (setq fields
2427               `(("owner_id" . ,(mixi-friend-id (mixi-diary-owner parent)))
2428                 ("comment_body" . ,content)))
2429       (setq fields `(("comment" . ,content))))
2430     (with-mixi-post-form (funcall page parent) fields
2431       (if (re-search-forward mixi-post-key-regexp nil t)
2432           (setq post-key (match-string 1))
2433         (mixi-post-error 'cannot-find-key parent)))
2434     (if (mixi-diary-p parent)
2435         (setq fields
2436               `(("post_key" . ,post-key)
2437                 ("owner_id" . ,(mixi-friend-id (mixi-diary-owner parent)))
2438                 ("comment_body" . ,content)
2439                 ("submit" . "confirm")))
2440       (setq fields `(("post_key" . ,post-key)
2441                      ("comment" . ,content)
2442                      ("submit" . "confirm"))))
2443     (with-mixi-post-form (funcall page parent) fields
2444       (unless (re-search-forward mixi-post-succeed-regexp nil t)
2445         (mixi-post-error 'cannot-find-succeed parent)))))
2446
2447 ;; Message object.
2448 (defconst mixi-message-box-list '(inbox outbox savebox thrash)) ; thrash?
2449
2450 (defmacro mixi-message-box-p (box)
2451   `(memq ,box mixi-message-box-list))
2452
2453 (defun mixi-message-box-name (box)
2454   "Return the name of BOX."
2455   (unless (mixi-message-box-p box)
2456     (signal 'wrong-type-argument (list 'mixi-message-box-p box)))
2457   (symbol-name box))
2458
2459 (defvar mixi-message-cache (make-hash-table :test 'equal))
2460 (defun mixi-make-message (id box &optional owner title time content)
2461   "Return a message object."
2462   (mixi-make-cache (list id box)
2463                    (cons 'mixi-message (vector nil id box owner title time
2464                                                content))
2465                    mixi-message-cache))
2466
2467 (defconst mixi-message-url-regexp
2468   "/view_message\\.pl\\?id=\\([a-z0-9]+\\)&box=\\([a-z]+\\)")
2469
2470 (defun mixi-make-message-from-url (url)
2471   "Return a message object from URL."
2472   (when (string-match mixi-message-url-regexp url)
2473     (let ((id (match-string 1 url))
2474           (box (match-string 2 url)))
2475       (mixi-make-message id box))))
2476
2477 (defmacro mixi-message-p (message)
2478   `(eq (mixi-object-class ,message) 'mixi-message))
2479
2480 (defmacro mixi-message-page (message)
2481   `(concat "/view_message.pl?id=" (mixi-message-id ,message)
2482            "&box=" (mixi-message-box ,message)))
2483
2484 (defconst mixi-message-owner-regexp
2485   "<font COLOR=#996600>\\(º¹½Ð¿Í\\|°¸&nbsp;Àè\\)</font>&nbsp;:&nbsp;<a HREF=\"show_friend\\.pl\\?id=\\([0-9]+\\)\">\\(.*\\)\\(</a>\\|</td>\\)")
2486 (defconst mixi-message-time-regexp
2487 "<font COLOR=#996600>Æü\\(¡¡\\|&nbsp;\\)ÉÕ</font>&nbsp;:&nbsp;\\([0-9]+\\)ǯ\\([0-9]+\\)·î\\([0-9]+\\)Æü \\([0-9]+\\)»þ\\([0-9]+\\)ʬ&nbsp;&nbsp;")
2488 (defconst mixi-message-title-regexp
2489 "<font COLOR=#996600>·ï\\(¡¡\\|&nbsp;\\)̾</font>&nbsp;:&nbsp;\\(.*\\)\n?</td>")
2490 (defconst mixi-message-content-regexp
2491   "<tr><td CLASS=h120>\\(.*\\)</td></tr>")
2492
2493 (defun mixi-realize-message (message)
2494   "Realize a MESSAGE."
2495   (unless (mixi-object-realized-p message)
2496     (with-mixi-retrieve (mixi-message-page message)
2497       (if (re-search-forward mixi-message-owner-regexp nil t)
2498           (mixi-message-set-owner message
2499                                   (mixi-make-friend (match-string 2)
2500                                                     (match-string 3)))
2501         (mixi-realization-error 'cannot-find-owner message))
2502       (if (re-search-forward mixi-message-time-regexp nil t)
2503           (mixi-message-set-time
2504            message (encode-time 0 (string-to-number (match-string 6))
2505                                 (string-to-number (match-string 5))
2506                                 (string-to-number (match-string 4))
2507                                 (string-to-number (match-string 3))
2508                                 (string-to-number (match-string 2))))
2509         (mixi-realization-error 'cannot-find-time message))
2510       (if (re-search-forward mixi-message-title-regexp nil t)
2511           (mixi-message-set-title message (match-string 2))
2512         (mixi-realization-error 'cannot-find-title message))
2513       (if (re-search-forward mixi-message-content-regexp nil t)
2514           (mixi-message-set-content message (match-string 1))
2515         (mixi-realization-error 'cannot-find-content message)))
2516     (mixi-object-touch message)))
2517
2518 (defun mixi-message-id (message)
2519   "Return the id of MESSAGE."
2520   (unless (mixi-message-p message)
2521     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2522   (aref (cdr message) 1))
2523
2524 (defun mixi-message-box (message)
2525   "Return the box of MESSAGE."
2526   (unless (mixi-message-p message)
2527     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2528   (aref (cdr message) 2))
2529
2530 (defun mixi-message-owner (message)
2531   "Return the owner of MESSAGE."
2532   (unless (mixi-message-p message)
2533     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2534   (mixi-realize-message message)
2535   (aref (cdr message) 3))
2536
2537 (defun mixi-message-title (message)
2538   "Return the title of MESSAGE."
2539   (unless (mixi-message-p message)
2540     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2541   (mixi-realize-message message)
2542   (aref (cdr message) 4))
2543
2544 (defun mixi-message-time (message)
2545   "Return the date of MESSAGE."
2546   (unless (mixi-message-p message)
2547     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2548   (mixi-realize-message message)
2549   (aref (cdr message) 5))
2550
2551 (defun mixi-message-content (message)
2552   "Return the content of MESSAGE."
2553   (unless (mixi-message-p message)
2554     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2555   (mixi-realize-message message)
2556   (aref (cdr message) 6))
2557
2558 (defun mixi-message-set-owner (message owner)
2559   "Set the owner of MESSAGE."
2560   (unless (mixi-message-p message)
2561     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2562   (aset (cdr message) 3 owner))
2563
2564 (defun mixi-message-set-title (message title)
2565   "Set the title of MESSAGE."
2566   (unless (mixi-message-p message)
2567     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2568   (aset (cdr message) 4 title))
2569
2570 (defun mixi-message-set-time (message time)
2571   "Set the date of MESSAGE."
2572   (unless (mixi-message-p message)
2573     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2574   (aset (cdr message) 5 time))
2575
2576 (defun mixi-message-set-content (message content)
2577   "Set the content of MESSAGE."
2578   (unless (mixi-message-p message)
2579     (signal 'wrong-type-argument (list 'mixi-message-p message)))
2580   (aset (cdr message) 6 content))
2581
2582 (defmacro mixi-message-list-page (&optional box)
2583   `(concat "/list_message.pl?page=%d"
2584            (when ,box (concat "&box=" ,box))))
2585
2586 (defconst mixi-message-list-regexp
2587   "<td><a HREF=\"view_message\\.pl\\?id=\\(.+\\)&box=\\(.+\\)\">")
2588
2589 ;;;###autoload
2590 (defun mixi-get-messages (&rest box-or-range)
2591   "Get messages of BOX."
2592   (when (> (length box-or-range) 2)
2593     (signal 'wrong-number-of-arguments
2594             (list 'mixi-get-messages (length box-or-range))))
2595   (let ((box (nth 0 box-or-range))
2596         (range (nth 1 box-or-range)))
2597     (when (or (not (mixi-message-box-p box))
2598               (mixi-message-box-p range))
2599       (setq box (nth 1 box-or-range))
2600       (setq range (nth 0 box-or-range)))
2601     (let ((items (mixi-get-matched-items
2602                   (mixi-message-list-page
2603                    (when box (mixi-message-box-name box)))
2604                   mixi-message-list-regexp
2605                   range)))
2606       (mapcar (lambda (item)
2607                 (mixi-make-message (nth 0 item) (nth 1 item)))
2608               items))))
2609
2610 (defmacro mixi-post-message-page (friend)
2611   `(concat "/send_message.pl?id=" (mixi-friend-id friend)))
2612
2613 (defconst mixi-post-message-key-regexp
2614   "<input name=post_key type=hidden value=\\([a-z0-9]+\\)>")
2615
2616 (defconst mixi-post-message-succeed-regexp
2617   "<b>Á÷¿®´°Î»</b>¤·¤Þ¤·¤¿¡£")
2618
2619 ;;;###autoload
2620 (defun mixi-post-message (friend title content)
2621   "Post a message to FRIEND."
2622   (unless (mixi-friend-p friend)
2623     (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
2624   (unless (stringp title)
2625     (signal 'wrong-type-argument (list 'stringp title)))
2626   (unless (stringp content)
2627     (signal 'wrong-type-argument (list 'stringp content)))
2628   (let ((fields `(("subject" . ,title)
2629                   ("body" . ,content)
2630                   ("submit" . "main")))
2631         post-key)
2632     (with-mixi-post-form (mixi-post-message-page friend) fields
2633       (if (re-search-forward mixi-post-message-key-regexp nil t)
2634           (setq post-key (match-string 1))
2635         (mixi-post-error 'cannot-find-key friend)))
2636     (setq fields `(("post_key" . ,post-key)
2637                    ("subject" . ,title)
2638                    ("body" . ,content)
2639                    ("yes" . "¡¡Á÷¡¡¿®¡¡")
2640                    ("submit" . "confirm")))
2641     (with-mixi-post-form (mixi-post-message-page friend) fields
2642       (unless (re-search-forward mixi-post-message-succeed-regexp nil t)
2643         (mixi-post-error 'cannot-find-succeed friend)))))
2644
2645 ;; Introduction object.
2646 (defun mixi-make-introduction (parent owner content)
2647   "Return a introduction object."
2648   (cons 'mixi-introduction (vector parent owner content)))
2649
2650 (defmacro mixi-introduction-p (introduction)
2651   `(eq (mixi-object-class ,introduction) 'mixi-introduction))
2652
2653 (defun mixi-introduction-parent (introduction)
2654   "Return the parent of INTRODUCTION."
2655   (unless (mixi-introduction-p introduction)
2656     (signal 'wrong-type-argument (list 'mixi-introduction-p introduction)))
2657   (aref (cdr introduction) 0))
2658
2659 (defun mixi-introduction-owner (introduction)
2660   "Return the owner of INTRODUCTION."
2661   (unless (mixi-introduction-p introduction)
2662     (signal 'wrong-type-argument (list 'mixi-introduction-p introduction)))
2663   (aref (cdr introduction) 1))
2664
2665 (defun mixi-introduction-content (introduction)
2666   "Return the content of INTRODUCTION."
2667   (unless (mixi-introduction-p introduction)
2668     (signal 'wrong-type-argument (list 'mixi-introduction-p introduction)))
2669   (aref (cdr introduction) 3))
2670
2671 (defmacro mixi-introduction-list-page (&optional friend)
2672   `(concat "/show_intro.pl?page=%d"
2673            (when ,friend (concat "&id=" (mixi-friend-id ,friend)))))
2674
2675 (defconst mixi-introduction-list-regexp
2676   "<tr bgcolor=#FFFFFF>
2677 <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>
2678 \\(.*\\)</td></a>
2679
2680 <td WIDTH=480>
2681 \\(´Ø·¸¡§.+<br>
2682
2683
2684 \\(\\(.\\|\n<br>\\)+\\)\\|
2685 \\(\\(.\\|\n<br>\\)+\\)\\)
2686
2687
2688
2689
2690 </td>
2691 </tr>")
2692 (defconst mixi-my-introduction-list-regexp
2693   "<tr bgcolor=#FFFFFF>
2694 <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>
2695 \\(.*\\)</td></a>
2696
2697
2698 <td WIDTH=480>
2699 \\(´Ø·¸¡§.+<br>
2700
2701
2702 \\(\\(.\\|\n<br>\\)+\\)\\|
2703 \\(\\(.\\|\n<br>\\)+\\)\\)
2704
2705
2706 <br>
2707 <a href=\"edit_intro\\.pl\\?id=\\1&type=edit\">¤³¤Îͧ¿Í¤ò¾Ò²ð¤¹¤ë</a>
2708
2709
2710 <BR>
2711 <a href=\"delete_intro\\.pl\\?id=\\1\">ºï½ü</a>
2712
2713 </td>
2714 </tr>")
2715
2716 ;;;###autoload
2717 (defun mixi-get-introductions (&rest friend-or-range)
2718   "Get introductions of FRIEND."
2719   (when (> (length friend-or-range) 2)
2720     (signal 'wrong-number-of-arguments
2721             (list 'mixi-get-introduction (length friend-or-range))))
2722   (let ((friend (nth 0 friend-or-range))
2723         (range (nth 1 friend-or-range)))
2724     (when (or (not (mixi-friend-p friend)) (mixi-friend-p range))
2725       (setq friend (nth 1 friend-or-range))
2726       (setq range (nth 0 friend-or-range)))
2727     (unless (or (null friend) (mixi-friend-p friend))
2728       (signal 'wrong-type-argument (list 'mixi-friend-p friend)))
2729     (let* ((regexp (if friend mixi-introduction-list-regexp
2730                      mixi-my-introduction-list-regexp))
2731            (items (mixi-get-matched-items (mixi-introduction-list-page friend)
2732                                           regexp
2733                                           range)))
2734       (mapcar (lambda (item)
2735                 (mixi-make-introduction (or friend (mixi-make-me))
2736                                         (mixi-make-friend (nth 0 item)
2737                                                           (nth 1 item))
2738                                         (nth 2 item)))
2739               items))))
2740
2741 ;; News object.
2742 (defvar mixi-news-cache (make-hash-table :test 'equal))
2743 (defun mixi-make-news (media-id id &optional media time title content)
2744   "Return a news object."
2745   (mixi-make-cache (list media-id id)
2746                    (cons 'mixi-news (vector nil media-id id media time title
2747                                             content))
2748                    mixi-news-cache))
2749
2750 (defconst mixi-news-url-regexp
2751   "/view_news\\.pl\\?id=\\([0-9]+\\)&media_id=\\([0-9]+\\)")
2752
2753 (defun mixi-make-news-from-url (url)
2754   "Return a news object from URL."
2755   (when (string-match mixi-news-url-regexp url)
2756     (let ((id (match-string 1 url))
2757           (media-id (match-string 2 url)))
2758       (mixi-make-news media-id id))))
2759
2760 (defmacro mixi-news-p (news)
2761   `(eq (mixi-object-class ,news) 'mixi-news))
2762
2763 (defmacro mixi-news-page (news)
2764   `(concat "http://news.mixi.jp/view_news.pl?id=" (mixi-news-id ,news)
2765            "&media_id=" (mixi-news-media-id ,news)))
2766
2767
2768 (defconst mixi-news-finished-regexp
2769   "<td ALIGN=center background=http://img\\.mixi\\.jp/img/bg_line\\.gif> ¿½¤·Ìõ¤´¤¶¤¤¤Þ¤»¤ó¤¬¡¢¤³¤Î¥Ë¥å¡¼¥¹¤Ï·ÇºÜ½ªÎ»¤·¤Þ¤·¤¿¡£</td>")
2770 (defconst mixi-news-title-regexp
2771   "<td HEIGHT=\"46\" STYLE=\"font-weight: bold;font-size: 14px;\" CLASS=\"h130\">\\(.+\\)\\(\r
2772 \\)?</td>")
2773 (defconst mixi-news-media-time-regexp
2774   "<td COLSPAN=\"2\" ALIGN=\"right\">(\\(.+\\)&nbsp;-&nbsp;\\([0-9]+\\)·î\\([0-9]+\\)Æü \\([0-9]+\\):\\([0-9]+\\))</td></tr>")
2775 (defconst mixi-news-content-regexp
2776   "<td CLASS=\"h150\">
2777
2778 \\(.+\\)
2779 <br>
2780 \\(.*
2781 \\)?
2782
2783 \\(</td>\\|<br>\\)")
2784
2785 (defun mixi-realize-news (news)
2786   "Realize a NEWS."
2787   ;; FIXME: Check a expiration of cache?
2788   (unless (mixi-object-realized-p news)
2789     (with-mixi-retrieve (mixi-news-page news)
2790       (if (re-search-forward mixi-news-finished-regexp nil t)
2791           (mixi-news-set-content news (match-string 0))
2792         (if (re-search-forward mixi-news-title-regexp nil t)
2793             (mixi-news-set-title news (match-string 1))
2794           (mixi-realization-error 'cannot-find-title news))
2795         (if (re-search-forward mixi-news-media-time-regexp nil t)
2796             (progn
2797               (mixi-news-set-media news (match-string 1))
2798               (let ((year (nth 5 (decode-time (current-time))))
2799                     (month (nth 4 (decode-time (current-time))))
2800                     (month-of-item (string-to-number (match-string 2))))
2801                 (when (> month-of-item month)
2802                   (decf year))
2803                 (mixi-news-set-time
2804                  news (encode-time 0 (string-to-number (match-string 5))
2805                                    (string-to-number (match-string 4))
2806                                    (string-to-number (match-string 3))
2807                                    month year))))
2808           (mixi-realization-error 'cannot-find-media-time news))
2809         (if (re-search-forward mixi-news-content-regexp nil t)
2810             (mixi-news-set-content news (match-string 1))
2811           (mixi-realization-error 'cannot-find-content news))))
2812     (mixi-object-touch news)))
2813
2814 (defun mixi-news-media-id (news)
2815   "Return the media-id of NEWS."
2816   (unless (mixi-news-p news)
2817     (signal 'wrong-type-argument (list 'mixi-news-p news)))
2818   (aref (cdr news) 1))
2819
2820 (defun mixi-news-id (news)
2821   "Return the id of NEWS."
2822   (unless (mixi-news-p news)
2823     (signal 'wrong-type-argument (list 'mixi-news-p news)))
2824   (aref (cdr news) 2))
2825
2826 (defun mixi-news-media (news)
2827   "Return the media of NEWS."
2828   (unless (mixi-news-p news)
2829     (signal 'wrong-type-argument (list 'mixi-news-p news)))
2830   (unless (aref (cdr news) 3)
2831     (mixi-realize-news news))
2832   (aref (cdr news) 3))
2833
2834 (defun mixi-news-time (news)
2835   "Return the time of NEWS."
2836   (unless (mixi-news-p news)
2837     (signal 'wrong-type-argument (list 'mixi-news-p news)))
2838   (unless (aref (cdr news) 4)
2839     (mixi-realize-news news))
2840   (aref (cdr news) 4))
2841
2842 (defun mixi-news-title (news)
2843   "Return the title of NEWS."
2844   (unless (mixi-news-p news)
2845     (signal 'wrong-type-argument (list 'mixi-news-p news)))
2846   (unless (aref (cdr news) 5)
2847     (mixi-realize-news news))
2848   (aref (cdr news) 5))
2849
2850 (defun mixi-news-content (news)
2851   "Return the content of NEWS."
2852   (unless (mixi-news-p news)
2853     (signal 'wrong-type-argument (list 'mixi-news-p news)))
2854   (mixi-realize-news news)
2855   (aref (cdr news) 6))
2856
2857 (defun mixi-news-set-media (news media)
2858   "Set the media of NEWS."
2859   (unless (mixi-news-p news)
2860     (signal 'wrong-type-argument (list 'mixi-news-p news)))
2861   (aset (cdr news) 3 media))
2862
2863 (defun mixi-news-set-time (news time)
2864   "Set the time of NEWS."
2865   (unless (mixi-news-p news)
2866     (signal 'wrong-type-argument (list 'mixi-news-p news)))
2867   (aset (cdr news) 4 time))
2868
2869 (defun mixi-news-set-title (news title)
2870   "Set the title of NEWS."
2871   (unless (mixi-news-p news)
2872     (signal 'wrong-type-argument (list 'mixi-news-p news)))
2873   (aset (cdr news) 5 title))
2874
2875 (defun mixi-news-set-content (news content)
2876   "Set the content of NEWS."
2877   (unless (mixi-news-p news)
2878     (signal 'wrong-type-argument (list 'mixi-news-p news)))
2879   (aset (cdr news) 6 content))
2880
2881 (defconst mixi-news-category-list '(domestic politics economy area abroad
2882                                              sports entertainment IT))
2883
2884 (defmacro mixi-news-category-p (category)
2885   `(memq ,category mixi-news-category-list))
2886
2887 (defun mixi-news-category-id (category)
2888   "Return the id of CATEGORY."
2889   (unless (mixi-news-category-p category)
2890     (signal 'wrong-type-argument (list 'mixi-news-category-p category)))
2891   (number-to-string
2892    (1+ (- (length mixi-news-category-list)
2893           (length (memq category mixi-news-category-list))))))
2894
2895 (defconst mixi-news-sort-list '(newest pickup))
2896
2897 (defmacro mixi-news-sort-p (sort)
2898   `(memq ,sort mixi-news-sort-list))
2899
2900 (defun mixi-news-sort-id (sort)
2901   "Return the id of SORT."
2902   (unless (mixi-news-sort-p sort)
2903     (signal 'wrong-type-argument (list 'mixi-news-sort-p sort)))
2904   (number-to-string
2905    (- (length mixi-news-sort-list)
2906       (length (memq sort mixi-news-sort-list)))))
2907
2908 (defmacro mixi-news-list-page (category sort)
2909   `(concat "http://news.mixi.jp/list_news_category.pl?page=%d"
2910            "&sort=" (mixi-news-sort-id ,sort)
2911            "&id=" (mixi-news-category-id ,category)
2912            "&type=bn"))
2913
2914 (defconst mixi-news-list-regexp
2915   "<tr bgcolor=\"\\(#FCF5EB\\|#FFFFFF\\)\">
2916 <td WIDTH=\"1%\" valign=top CLASS=\"h120\">¡¦</td>
2917 <td WIDTH=\"97%\" CLASS=\"h120\"><A HREF=\"view_news\\.pl\\?id=\\([0-9]+\\)&media_id=\\([0-9]+\\)\"class=\"new_link\">\\(.+\\)</A>
2918 \\(<IMG SRC=\"http://img\\.mixi\\.jp/img/news_camera3\\.gif\" WIDTH=\"11\" HEIGHT=\"12\">\\|\\)
2919
2920 </td>
2921 <td WIDTH=\"1%\" nowrap CLASS=\"f08\"><A HREF=\"list_news_media\\.pl\\?id=[0-9]+\">\\(.+\\)</A></td>
2922 <td WIDTH=\"1%\" nowrap CLASS=\"f08\">\\([0-9]+\\)·î\\([0-9]+\\)Æü \\([0-9]+\\):\\([0-9]+\\)</td></tr>")
2923
2924 ;;;###autoload
2925 (defun mixi-get-news (category sort &optional range)
2926   "Get news of CATEGORY and SORT."
2927   (unless (mixi-news-category-p category)
2928     (signal 'wrong-type-argument (list 'mixi-news-category-p category)))
2929   (unless (mixi-news-sort-p sort)
2930     (signal 'wrong-type-argument (list 'mixi-news-sort-p sort)))
2931   (let ((items (mixi-get-matched-items (mixi-news-list-page category sort)
2932                                        mixi-news-list-regexp
2933                                        range))
2934         (year (nth 5 (decode-time (current-time))))
2935         (month (nth 4 (decode-time (current-time)))))
2936     (mapcar (lambda (item)
2937               (let ((month-of-item (string-to-number (nth 6 item))))
2938                 (when (> month-of-item month)
2939                   (decf year))
2940                 (setq month month-of-item)
2941                 (mixi-make-news (nth 2 item) (nth 1 item) (nth 5 item)
2942                                 (encode-time
2943                                  0 (string-to-number (nth 9 item))
2944                                  (string-to-number (nth 8 item))
2945                                  (string-to-number (nth 7 item))
2946                                  month year)
2947                                 (nth 3 item))))
2948             items)))
2949
2950 (provide 'mixi)
2951
2952 ;;; mixi.el ends here