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