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