* mixi.el (mixi-friend-nick-regexp): Follow the change of mixi.
[elisp/mixi.git] / mixi-utils.el
1 ;; mixi-utils.el --- Utilities for mixi object -*- coding: euc-jp -*-
2
3 ;; Copyright (C) 2007, 2008 OHASHI Akira
4
5 ;; Author: OHASHI Akira <bg66@koka-in.org>
6 ;; Keywords: hypermedia
7
8 ;; This file is *NOT* a part of Emacs.
9
10 ;; This program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
24
25 ;;; Commentary:
26
27 ;; Bug reports:
28 ;;
29 ;; If you have bug reports and/or suggestions for improvement, please
30 ;; send them via <URL:http://mixi.jp/view_community.pl?id=1596390>.
31
32 ;;; Code:
33
34 (require 'mixi)
35
36 (defvar mixi-reply-to nil)
37
38 (defmacro with-mixi-class (object &rest body)
39   `(let ((class (mixi-object-class ,object)))
40      ,@body))
41 (put 'with-mixi-class 'lisp-indent-function 'defun)
42 (put 'with-mixi-class 'edebug-form-spec '(body))
43
44 (defun mixi-make-objects (url-or-function &optional range)
45   (if (stringp url-or-function)
46       (let ((object (mixi-make-object-from-url url-or-function)))
47         (with-mixi-class object
48           (cond ((eq class 'mixi-friend)
49                  (mixi-get-diaries object range))
50                 ((eq class 'mixi-community)
51                  (mixi-get-bbses object range))
52                 ((mixi-parent-p object)
53                  (mixi-get-comments object range))
54                 (t (error (concat (symbol-name class)
55                                   " is not supported yet."))))))
56     (funcall url-or-function range)))
57
58 (defun mixi-make-title (object &optional add-parent)
59   (with-mixi-class object
60     (cond ((eq class 'mixi-comment)
61            (concat "Re: " (mixi-make-title
62                            (mixi-comment-parent object) add-parent)))
63           ((eq class 'mixi-log)
64            (mixi-friend-nick (mixi-log-friend object)))
65           ((eq class 'mixi-echo)
66            (concat (mixi-friend-nick (mixi-echo-owner object)) " ("
67                    (mixi-echo-post-time object) ")"))
68           (t
69            (let ((prefix (when (eq class 'mixi-event) "[¥¤¥Ù¥ó¥È]"))
70                  (subject (mixi-object-title object))
71                  (suffix (when add-parent
72                            (concat " ("
73                                    (cond ((eq class 'mixi-diary)
74                                           (mixi-friend-nick
75                                            (mixi-diary-owner object)))
76                                          ((eq class 'mixi-news)
77                                           (mixi-news-media object))
78                                          (t
79                                           (mixi-community-name
80                                            (mixi-bbs-community object))))
81                                    ")"))))
82              (concat prefix subject suffix))))))
83
84 (defun mixi-make-author (object &optional add-comment-count)
85   (with-mixi-class object
86     (cond ((eq class 'mixi-news)
87            (mixi-news-media object))
88           ((and add-comment-count
89                 (eq class 'mixi-comment)
90                 (mixi-bbs-p (mixi-comment-parent object)))
91            (concat (mixi-comment-count object) " "
92                    (mixi-friend-nick (mixi-comment-owner object))))
93           ((eq class 'mixi-release)
94            "mixi±¿±Ä»ö̳¶É")
95           ((eq class 'mixi-message)
96            (let ((owner (mixi-message-owner object)))
97              (if (null owner) "mixi" (mixi-friend-nick owner))))
98           (t
99            (let ((owner (if (eq class 'mixi-log)
100                             (mixi-log-friend object)
101                           (mixi-object-owner object))))
102              (mixi-friend-nick owner))))))
103
104 (defun mixi-make-time (object)
105   (with-mixi-class object
106     (if (eq class 'mixi-echo)
107         (let ((post-time (mixi-echo-post-time object)))
108           (encode-time
109            (string-to-number (substring post-time 12 14))
110            (string-to-number (substring post-time 10 12))
111            (string-to-number (substring post-time 8 10))
112            (string-to-number (substring post-time 6 8))
113            (string-to-number (substring post-time 4 6))
114            (string-to-number (substring post-time 0 4))))
115       (mixi-object-time object))))
116
117 (defun mixi-make-date (object)
118   (let* ((time (mixi-make-time object))
119          (cts (current-time-string time))
120          (day-of-week (substring cts 0 3))
121          (month (substring cts 4 7)))
122     (concat day-of-week ", "
123             (format-time-string "%d" time) " "
124             month " "
125             (format-time-string "%Y %H:%M:%S %z" time))))
126
127 (defun mixi-make-id-1 (object)
128   (with-mixi-class object
129     (concat
130      (format-time-string "%Y%m%d%H%M" (mixi-make-time object)) "."
131      (cond ((eq class 'mixi-comment)
132             (concat (mixi-friend-id (mixi-comment-owner object)) "@"
133                     (mixi-object-id (mixi-comment-parent object)) "."
134                     (mixi-friend-id (mixi-object-owner
135                                      (mixi-comment-parent object))) "."))
136            ((eq class 'mixi-log)
137             (concat (mixi-friend-id (mixi-log-friend object)) "@"))
138            ((eq class 'mixi-release)
139             (concat (md5 (mixi-release-title object)) "@"))
140            ((eq class 'mixi-echo)
141             (concat (mixi-friend-id (mixi-echo-owner object)) "@"))
142            ((and (eq class 'mixi-message) (null (mixi-message-owner object)))
143             (concat (mixi-object-id object) "@"))
144            (t
145             (concat (mixi-object-id object) "@"
146                     (if (eq class 'mixi-news)
147                         (mixi-news-media-id object)
148                       (mixi-object-id (mixi-object-owner object))) ".")))
149      (mixi-object-name object))))
150
151 (defun mixi-make-message-id (object)
152   (format "<%s.mixi.jp>" (mixi-make-id-1 object)))
153
154 (defun mixi-make-tag-uri (object)
155   (format "tag:mixi.jp,%s:%s"
156           (format-time-string "%Y-%m-%d" (mixi-make-time object))
157           (mixi-make-id-1 object)))
158
159 (defun mixi-make-url (object)
160   (with-mixi-class object
161     (cond ((eq class 'mixi-diary)
162            (mixi-expand-url (mixi-diary-page object)))
163           ((eq class 'mixi-topic)
164            (mixi-expand-url (mixi-topic-page object)))
165           ((eq class 'mixi-event)
166            (mixi-expand-url (mixi-event-page object)))
167           ((eq class 'mixi-comment)
168            (concat (mixi-make-url (mixi-comment-parent object))
169                    "#comment"))
170           ((eq class 'mixi-message)
171            (mixi-expand-url (mixi-message-page object)))
172           ((eq class 'mixi-news)
173            (mixi-news-page object))
174           ((eq class 'mixi-release)
175            (let ((url (mixi-release-list-page)))
176              (mixi-expand-url (substring url 0
177                                          (string-match "?" url)))))
178           ((eq class 'mixi-log)
179            (mixi-expand-url (mixi-friend-page (mixi-log-friend object))))
180           ((eq class 'mixi-friend)
181            (mixi-expand-url (mixi-friend-page object)))
182           ((eq class 'mixi-echo)
183            (mixi-expand-url (mixi-echo-page object))))))
184
185 (defun mixi-make-encoded-url (object)
186   (mixi-url-encode-string (mixi-make-url object)))
187
188 (defun mixi-make-content (object)
189   (with-mixi-class object
190     (cond ((eq class 'mixi-event)
191            (concat "<dl>"
192                    "<dt>³«ºÅÆü»þ¡§</dt>"
193                    "<dd>" (mixi-event-date object) "</dd>\n"
194                    "<dt>³«ºÅ¾ì½ê¡§</dt>"
195                    "<dd>" (mixi-event-place object) "</dd>\n"
196                    "<dt>¾ÜºÙ¡§</dt>"
197                    "<dd>" (mixi-event-detail object) "</dd>\n"
198                    "<dt>Ê罸´ü¸Â¡§</dt>"
199                    "<dd>" (mixi-event-limit object) "</dd>\n"
200                    "<dt>»²²Ã¼Ô¡§</dt>"
201                    "<dd>" (mixi-event-members object) "</dd>\n"
202                    "</dl>"))
203           ((eq class 'mixi-friend)
204            (if (mixi-object-realized-p object)
205                (let ((sex (if (eq (mixi-friend-sex object) 'male) "ÃË" "½÷"))
206                      (age (if (numberp (mixi-friend-age object))
207                               (number-to-string (mixi-friend-age object))
208                             "??"))
209                      (birthday (if (mixi-friend-birthday object)
210                                    (concat
211                                     (mapconcat (lambda (number)
212                                                  (number-to-string number))
213                                                (mixi-friend-birthday object)
214                                                "·î") "Æü")
215                                  "??·î??Æü"))
216                      (blood-type (if (mixi-friend-blood-type object)
217                                      (symbol-name
218                                       (mixi-friend-blood-type object))
219                                    "?"))
220                      (hobby (mapconcat 'identity
221                                        (mixi-friend-hobby object) ", ")))
222                  (concat "<dl>"
223                          "<dt>̾Á°¡§</dt>"
224                          "<dd>" (mixi-friend-name object) "</dd>\n"
225                          "<dt>À­ÊÌ¡§</dt>"
226                          "<dd>" sex "À­</dd>\n"
227                          "<dt>¸½½»½ê¡§</dt>"
228                          "<dd>" (mixi-friend-address object) "</dd>\n"
229                          "<dt>ǯÎð¡§</dt>"
230                          "<dd>" age "ºÐ</dd>\n"
231                          "<dt>ÃÂÀ¸Æü¡§</dt>"
232                          "<dd>" birthday "</dd>\n"
233                          "<dt>·ì±Õ·¿¡§</dt>"
234                          "<dd>" blood-type "·¿</dd>\n"
235                          "<dt>½Ð¿ÈÃÏ¡§</dt>"
236                          "<dd>" (mixi-friend-birthplace object) "</dd>\n"
237                          "<dt>¼ñÌ£¡§</dt>"
238                          "<dd>" hobby "</dd>\n"
239                          "<dt>¿¦¶È¡§</dt>"
240                          "<dd>" (mixi-friend-job object) "</dd>\n"
241                          "<dt>½ê°¡§</dt>"
242                          "<dd>" (mixi-friend-organization object) "</dd>\n"
243                          "<dt>¼«¸Ê¾Ò²ð¡§</dt>"
244                          "<dd>" (mixi-friend-profile object) "</dd>\n"
245                          "</dl>"))
246              (concat "<a href=\"" (mixi-make-url object)
247                      "\">¥×¥í¥Õ¥£¡¼¥ë¤òɽ¼¨¤¹¤ë</a>")))
248           ((eq class 'mixi-log)
249            (mixi-make-content (mixi-log-friend object)))
250           (t (mixi-object-content object)))))
251
252 (defun mixi-make-reply-to (object)
253   (setq mixi-reply-to "mixi;")
254   (with-mixi-class object
255     (setq mixi-reply-to
256           (concat
257            (cond ((eq class 'mixi-diary)
258                   (concat mixi-reply-to "comment;diary;"
259                           (mixi-friend-id (mixi-diary-owner object)) ";"
260                           (mixi-diary-id object)))
261                  ((mixi-bbs-p object)
262                   (concat mixi-reply-to "comment;"
263                           (mixi-object-name object) ";"
264                           (mixi-community-id (mixi-bbs-community object)) ";"
265                           (mixi-bbs-id object)))
266                  ((eq class 'mixi-community)
267                   (concat mixi-reply-to "topic;"
268                           (mixi-community-id object)))
269                  ((or (eq class 'mixi-news) (eq object (mixi-make-me)))
270                   (concat mixi-reply-to "diary"))
271                  ((and (eq class 'mixi-message)
272                        (not (null (mixi-message-owner object))))
273                   (concat mixi-reply-to "message;"
274                           (mixi-friend-id (mixi-message-owner object))))
275                  ((or (eq class 'mixi-friend) (eq class 'mixi-log))
276                   (concat mixi-reply-to "message;"
277                           (mixi-friend-id object)))
278                  ((eq class 'mixi-echo)
279                   (concat mixi-reply-to "echo;"
280                           (mixi-friend-id (mixi-echo-owner object)) ";"
281                           (mixi-echo-post-time object)))
282                  (t
283                   (concat mixi-reply-to "diary")))))))
284
285 (defconst mixi-to-regexp
286   "^mixi;\\([a-z]+\\);?\\([a-z0-9]+\\)?;?\\([0-9]+\\)?;?\\([0-9]+\\)?")
287
288 (defun mixi-send-mail (to title content)
289   (when (string-match mixi-to-regexp to)
290     (let ((method (match-string 1 to)))
291       (cond ((string= method "comment")
292              (let ((parent (match-string 2 to))
293                    (owner-id (match-string 3 to))
294                    (id (match-string 4 to)))
295                (if (string= parent "diary")
296                    (mixi-post-comment
297                     (mixi-make-diary (mixi-make-friend owner-id) id) content)
298                  (let ((func (intern
299                               (concat mixi-object-prefix "make-" parent))))
300                    (mixi-post-comment
301                     (funcall func (mixi-make-community owner-id) id)
302                     content)))))
303             ((string= method "topic")
304              (mixi-post-topic (mixi-make-community (match-string 2 to))
305                               title content))
306             ((string= method "diary")
307              (mixi-post-diary title content))
308             ((string= method "message")
309              (mixi-post-message (mixi-make-friend (match-string 2 to))
310                                 title content))
311             ((string= method "echo")
312              (let ((owner-id (match-string 2 to))
313                    (post-time (match-string 3 to)))
314                (mixi-post-echo content
315                                (mixi-make-echo (mixi-make-friend owner-id)
316                                                post-time))))))))
317
318 (provide 'mixi-utils)
319
320 ;;; mixi-utils.el ends here