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