(url-request-method): Abolish useless variables.
[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           (t
96            (let ((owner (if (eq class 'mixi-log)
97                             (mixi-log-friend object)
98                           (mixi-object-owner object))))
99              (mixi-friend-nick owner))))))
100
101 (defun mixi-make-time (object)
102   (with-mixi-class object
103     (if (eq class 'mixi-echo)
104         (let ((post-time (mixi-echo-post-time object)))
105           (encode-time
106            (string-to-number (substring post-time 12 14))
107            (string-to-number (substring post-time 10 12))
108            (string-to-number (substring post-time 8 10))
109            (string-to-number (substring post-time 6 8))
110            (string-to-number (substring post-time 4 6))
111            (string-to-number (substring post-time 0 4))))
112       (mixi-object-time object))))
113
114 (defun mixi-make-date (object)
115   (let* ((time (mixi-make-time object))
116          (cts (current-time-string time))
117          (day-of-week (substring cts 0 3))
118          (month (substring cts 4 7)))
119     (concat day-of-week ", "
120             (format-time-string "%d" time) " "
121             month " "
122             (format-time-string "%Y %H:%M:%S %z" time))))
123
124 (defun mixi-make-id-1 (object)
125   (with-mixi-class object
126     (concat
127      (format-time-string "%Y%m%d%H%M" (mixi-make-time object)) "."
128      (cond ((eq class 'mixi-comment)
129             (concat (mixi-friend-id (mixi-comment-owner object)) "@"
130                     (mixi-object-id (mixi-comment-parent object)) "."
131                     (mixi-friend-id (mixi-object-owner
132                                      (mixi-comment-parent object))) "."))
133            ((eq class 'mixi-log)
134             (concat (mixi-friend-id (mixi-log-friend object)) "@"))
135            ((eq class 'mixi-release)
136             (concat (md5 (mixi-release-title object)) "@"))
137            ((eq class 'mixi-echo)
138             (concat (mixi-friend-id (mixi-echo-owner object)) "@"))
139            (t
140             (concat (mixi-object-id object) "@"
141                     (if (eq class 'mixi-news)
142                         (mixi-news-media-id object)
143                       (mixi-object-id (mixi-object-owner object))) ".")))
144      (mixi-object-name object))))
145
146 (defun mixi-make-message-id (object)
147   (format "<%s.mixi.jp>" (mixi-make-id-1 object)))
148
149 (defun mixi-make-tag-uri (object)
150   (format "tag:mixi.jp,%s:%s"
151           (format-time-string "%Y-%m-%d" (mixi-make-time object))
152           (mixi-make-id-1 object)))
153
154 (defun mixi-make-url (object)
155   (with-mixi-class object
156     (cond ((eq class 'mixi-diary)
157            (mixi-expand-url (mixi-diary-page object)))
158           ((eq class 'mixi-topic)
159            (mixi-expand-url (mixi-topic-page object)))
160           ((eq class 'mixi-event)
161            (mixi-expand-url (mixi-event-page object)))
162           ((eq class 'mixi-comment)
163            (concat (mixi-make-url (mixi-comment-parent object))
164                    "#comment"))
165           ((eq class 'mixi-message)
166            (mixi-expand-url (mixi-message-page object)))
167           ((eq class 'mixi-news)
168            (mixi-news-page object))
169           ((eq class 'mixi-release)
170            (let ((url (mixi-release-list-page)))
171              (mixi-expand-url (substring url 0
172                                          (string-match "?" url)))))
173           ((eq class 'mixi-log)
174            (mixi-expand-url (mixi-friend-page (mixi-log-friend object))))
175           ((eq class 'mixi-friend)
176            (mixi-expand-url (mixi-friend-page object)))
177           ((eq class 'mixi-echo)
178            (mixi-expand-url (mixi-echo-page object))))))
179
180 (defun mixi-make-encoded-url (object)
181   (mixi-url-encode-string (mixi-make-url object)))
182
183 (defun mixi-make-content (object)
184   (with-mixi-class object
185     (cond ((eq class 'mixi-event)
186            (concat "<dl>"
187                    "<dt>³«ºÅÆü»þ¡§</dt>"
188                    "<dd>" (mixi-event-date object) "</dd>\n"
189                    "<dt>³«ºÅ¾ì½ê¡§</dt>"
190                    "<dd>" (mixi-event-place object) "</dd>\n"
191                    "<dt>¾ÜºÙ¡§</dt>"
192                    "<dd>" (mixi-event-detail object) "</dd>\n"
193                    "<dt>Ê罸´ü¸Â¡§</dt>"
194                    "<dd>" (mixi-event-limit object) "</dd>\n"
195                    "<dt>»²²Ã¼Ô¡§</dt>"
196                    "<dd>" (mixi-event-members object) "</dd>\n"
197                    "</dl>"))
198           ((eq class 'mixi-friend)
199            (if (mixi-object-realized-p object)
200                (let ((sex (if (eq (mixi-friend-sex object) 'male) "ÃË" "½÷"))
201                      (age (if (numberp (mixi-friend-age object))
202                               (number-to-string (mixi-friend-age object))
203                             "??"))
204                      (birthday (if (mixi-friend-birthday object)
205                                    (concat
206                                     (mapconcat (lambda (number)
207                                                  (number-to-string number))
208                                                (mixi-friend-birthday object)
209                                                "·î") "Æü")
210                                  "??·î??Æü"))
211                      (blood-type (if (mixi-friend-blood-type object)
212                                      (symbol-name
213                                       (mixi-friend-blood-type object))
214                                    "?"))
215                      (hobby (mapconcat 'identity
216                                        (mixi-friend-hobby object) ", ")))
217                  (concat "<dl>"
218                          "<dt>̾Á°¡§</dt>"
219                          "<dd>" (mixi-friend-name object) "</dd>\n"
220                          "<dt>À­ÊÌ¡§</dt>"
221                          "<dd>" sex "À­</dd>\n"
222                          "<dt>¸½½»½ê¡§</dt>"
223                          "<dd>" (mixi-friend-address object) "</dd>\n"
224                          "<dt>ǯÎð¡§</dt>"
225                          "<dd>" age "ºÐ</dd>\n"
226                          "<dt>ÃÂÀ¸Æü¡§</dt>"
227                          "<dd>" birthday "</dd>\n"
228                          "<dt>·ì±Õ·¿¡§</dt>"
229                          "<dd>" blood-type "·¿</dd>\n"
230                          "<dt>½Ð¿ÈÃÏ¡§</dt>"
231                          "<dd>" (mixi-friend-birthplace object) "</dd>\n"
232                          "<dt>¼ñÌ£¡§</dt>"
233                          "<dd>" hobby "</dd>\n"
234                          "<dt>¿¦¶È¡§</dt>"
235                          "<dd>" (mixi-friend-job object) "</dd>\n"
236                          "<dt>½ê°¡§</dt>"
237                          "<dd>" (mixi-friend-organization object) "</dd>\n"
238                          "<dt>¼«¸Ê¾Ò²ð¡§</dt>"
239                          "<dd>" (mixi-friend-profile object) "</dd>\n"
240                          "</dl>"))
241              (concat "<a href=\"" (mixi-make-url object)
242                      "\">¥×¥í¥Õ¥£¡¼¥ë¤òɽ¼¨¤¹¤ë</a>")))
243           ((eq class 'mixi-log)
244            (mixi-make-content (mixi-log-friend object)))
245           (t (mixi-object-content object)))))
246
247 (defun mixi-make-reply-to (object)
248   (setq mixi-reply-to "mixi;")
249   (with-mixi-class object
250     (setq mixi-reply-to
251           (concat
252            (cond ((eq class 'mixi-diary)
253                   (concat mixi-reply-to "comment;diary;"
254                           (mixi-friend-id (mixi-diary-owner object)) ";"
255                           (mixi-diary-id object)))
256                  ((mixi-bbs-p object)
257                   (concat mixi-reply-to "comment;"
258                           (mixi-object-name object) ";"
259                           (mixi-community-id (mixi-bbs-community object)) ";"
260                           (mixi-bbs-id object)))
261                  ((eq class 'mixi-community)
262                   (concat mixi-reply-to "topic;"
263                           (mixi-community-id object)))
264                  ((or (eq class 'mixi-news) (eq object (mixi-make-me)))
265                   (concat mixi-reply-to "diary"))
266                  ((eq class 'mixi-message)
267                   (concat mixi-reply-to "message;"
268                           (mixi-friend-id (mixi-message-owner object))))
269                  ((or (eq class 'mixi-friend) (eq class 'mixi-log))
270                   (concat mixi-reply-to "message;"
271                           (mixi-friend-id object)))
272                  ((eq class 'mixi-echo)
273                   (concat mixi-reply-to "echo;"
274                           (mixi-friend-id (mixi-echo-owner object)) ";"
275                           (mixi-echo-post-time object)))
276                  (t
277                   (concat mixi-reply-to "diary")))))))
278
279 (defconst mixi-to-regexp
280   "^mixi;\\([a-z]+\\);?\\([a-z0-9]+\\)?;?\\([0-9]+\\)?;?\\([0-9]+\\)?")
281
282 (defun mixi-send-mail (to title content)
283   (when (string-match mixi-to-regexp to)
284     (let ((method (match-string 1 to)))
285       (cond ((string= method "comment")
286              (let ((parent (match-string 2 to))
287                    (owner-id (match-string 3 to))
288                    (id (match-string 4 to)))
289                (if (string= parent "diary")
290                    (mixi-post-comment
291                     (mixi-make-diary (mixi-make-friend owner-id) id) content)
292                  (let ((func (intern
293                               (concat mixi-object-prefix "make-" parent))))
294                    (mixi-post-comment
295                     (funcall func (mixi-make-community owner-id) id)
296                     content)))))
297             ((string= method "topic")
298              (mixi-post-topic (mixi-make-community (match-string 2 to))
299                               title content))
300             ((string= method "diary")
301              (mixi-post-diary title content))
302             ((string= method "message")
303              (mixi-post-message (mixi-make-friend (match-string 2 to))
304                                 title content))
305             ((string= method "echo")
306              (let ((owner-id (match-string 2 to))
307                    (post-time (match-string 3 to)))
308                (mixi-post-echo content
309                                (mixi-make-echo (mixi-make-friend owner-id)
310                                                post-time))))))))
311
312 (provide 'mixi-utils)
313
314 ;;; mixi-utils.el ends here