(riece-mixi-send-object-with-url): Don't send a title.
[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            (let ((limit (mixi-event-limit object)))
153              (setq limit (if limit
154                              (format-time-string "%Yǯ%m·î%dÆü" limit)
155                            "»ØÄê¤Ê¤·"))
156              (concat "<dl>"
157                      "<dt>³«ºÅÆü»þ¡§</dt>"
158                      "<dd>" (mixi-event-date object) "</dd>\n"
159                      "<dt>³«ºÅ¾ì½ê¡§</dt>"
160                      "<dd>" (mixi-event-place object) "</dd>\n"
161                      "<dt>¾ÜºÙ¡§</dt>"
162                      "<dd>" (mixi-event-detail object) "</dd>\n"
163                      "<dt>Ê罸´ü¸Â¡§</dt>"
164                      "<dd>" limit "</dd>\n"
165                      "<dt>»²²Ã¼Ô¡§</dt>"
166                      "<dd>" (mixi-event-members object) "</dd>\n"
167                      "</dl>")))
168           ((eq class 'mixi-friend)
169            (if (mixi-object-realized-p object)
170                (let ((sex (if (eq (mixi-friend-sex object) 'male) "ÃË" "½÷"))
171                      (age (number-to-string (mixi-friend-age object)))
172                      (birthday
173                       (concat (mapconcat (lambda (number)
174                                            (number-to-string number))
175                                          (mixi-friend-birthday object) "·î")
176                               "Æü"))
177                      (blood-type (symbol-name
178                                   (mixi-friend-blood-type object)))
179                      (hobby (mapconcat 'identity
180                                        (mixi-friend-hobby object) ", ")))
181                  (concat "<dl>"
182                          "<dt>̾Á°¡§</dt>"
183                          "<dd>" (mixi-friend-name object) "</dd>\n"
184                          "<dt>À­ÊÌ¡§</dt>"
185                          "<dd>" sex "À­</dd>\n"
186                          "<dt>¸½½»½ê¡§</dt>"
187                          "<dd>" (mixi-friend-address object) "</dd>\n"
188                          "<dt>ǯÎð¡§</dt>"
189                          "<dd>" age "ºÐ</dd>\n"
190                          "<dt>ÃÂÀ¸Æü¡§</dt>"
191                          "<dd>" birthday "</dd>\n"
192                          "<dt>·ì±Õ·¿¡§</dt>"
193                          "<dd>" blood-type "·¿</dd>\n"
194                          "<dt>½Ð¿ÈÃÏ¡§</dt>"
195                          "<dd>" (mixi-friend-birthplace object) "</dd>\n"
196                          "<dt>¼ñÌ£¡§</dt>"
197                          "<dd>" hobby "</dd>\n"
198                          "<dt>¿¦¶È¡§</dt>"
199                          "<dd>" (mixi-friend-job object) "</dd>\n"
200                          "<dt>½ê°¡§</dt>"
201                          "<dd>" (mixi-friend-organization object) "</dd>\n"
202                          "<dt>¼«¸Ê¾Ò²ð¡§</dt>"
203                          "<dd>" (mixi-friend-profile object) "</dd>\n"
204                          "</dl>"))
205              (concat "<a href=\"" (mixi-make-url object)
206                      "\">¥×¥í¥Õ¥£¡¼¥ë¤òɽ¼¨¤¹¤ë</a>")))
207           ((eq class 'mixi-log)
208            (mixi-make-content (mixi-log-friend object)))
209           (t (mixi-object-content object)))))
210
211 (defun mixi-make-reply-to (object)
212   (setq mixi-reply-to "mixi;")
213   (with-mixi-class object
214     (setq mixi-reply-to
215           (concat
216            (cond ((eq class 'mixi-diary)
217                   (concat mixi-reply-to "comment;diary;"
218                           (mixi-friend-id (mixi-diary-owner object)) ";"
219                           (mixi-diary-id object)))
220                  ((mixi-bbs-p object)
221                   (concat mixi-reply-to "comment;"
222                           (mixi-object-name object) ";"
223                           (mixi-community-id (mixi-bbs-community object)) ";"
224                           (mixi-bbs-id object)))
225                  ((eq class 'mixi-community)
226                   (concat mixi-reply-to "topic;"
227                           (mixi-community-id object)))
228                  ((or (eq class 'mixi-news) (eq object (mixi-make-me)))
229                   (concat mixi-reply-to "diary"))
230                  ((eq class 'mixi-message)
231                   (concat mixi-reply-to "message;"
232                           (mixi-friend-id (mixi-message-owner object))))
233                  ((or (eq class 'mixi-friend) (eq class 'mixi-log))
234                   (concat mixi-reply-to "message;"
235                           (mixi-friend-id object))))))))
236
237 (defconst mixi-to-regexp
238   "^mixi;\\([a-z]+\\);?\\([a-z0-9]+\\)?;?\\([0-9]+\\)?;?\\([0-9]+\\)?")
239
240 (defun mixi-send-mail (to title content)
241   (when (string-match mixi-to-regexp to)
242     (let ((method (match-string 1 to)))
243       (cond ((string= method "comment")
244              (let ((parent (match-string 2 to))
245                    (owner-id (match-string 3 to))
246                    (id (match-string 4 to)))
247                (if (string= parent "diary")
248                    (mixi-post-comment
249                     (mixi-make-diary (mixi-make-friend owner-id) id) content)
250                  (let ((func (intern
251                               (concat mixi-object-prefix "make-" parent))))
252                    (mixi-post-comment
253                     (funcall func (mixi-make-community owner-id) id)
254                     content)))))
255             ((string= method "topic")
256              (mixi-post-topic (mixi-make-community (match-string 2 to))
257                               title content))
258             ((string= method "diary")
259              (mixi-post-diary title content))
260             ((string= method "message")
261              (mixi-post-message (mixi-make-friend (match-string 2 to))
262                                 title content))))))
263
264 (provide 'mixi-utils)
265
266 ;;; mixi-utils.el ends here