* mixi.el (mixi-entity-alist): New constant.
[elisp/mixi.git] / mixi-atom.el
1 ;; mixi-atom.el --- Atom Syndication Format
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 (require 'mixi-utils)
36
37 (defcustom mixi-atom-coding-system 'utf-8
38   "*Coding system for Atom Syndication Format."
39   :type 'coding-system
40   :group 'mixi)
41
42 (defcustom mixi-atom-namespace "http://www.w3.org/2005/Atom"
43   "*Namespace for Atom Syndication Format."
44   :type 'string
45   :group 'mixi)
46
47 (defcustom mixi-atom-self ""
48   "*URI for retrieving Atom Feed Documents representing this Atom feed."
49   :type 'string
50   :group 'mixi)
51
52 (defcustom mixi-atom-title "Mixi Feed"
53   "*Title for feed."
54   :type 'string
55   :group 'mixi)
56
57 (defcustom mixi-atom-file "~/atom.xml"
58   "*File name for `mixi-make-atom-file'."
59   :group 'mixi
60   :type 'string)
61
62 (defcustom mixi-atom-syndication-list
63   '((mixi-get-diaries . 10))
64   "*A list of atom syndication definition.
65 Each element looks like (URL . RANGE) or (FUNCTION . RANGE).
66 URL is the URL for mixi access point.  If URL is friend's, get his/her diaries
67 as article.  If community's, get its BBSes.  If diary's or BBS's, get its
68 comments.
69 FUNCTION is the function which has one `range' argument and returns the list
70 of mixi object.
71 RANGE is the range for getting articles.  If RANGE is nil, get all articles."
72   :group 'mixi
73   :type '(repeat (cons
74                   (radio (string :tag "URL")
75                          (const :tag "New diaries" mixi-get-new-diaries)
76                          (const :tag "New comments" mixi-get-new-comments)
77                          (const :tag "New BBSes" mixi-get-new-bbses)
78                          (const :tag "Messages" mixi-get-messages)
79                          (const :tag "Logs" mixi-get-logs)
80                          (function :tag "Other function"))
81                   (radio (integer :tag "Range")
82                          (const :tag "All" nil)))))
83
84 (defmacro mixi-atom-make-date (time)
85   `(let ((date (format-time-string "%Y-%m-%dT%T%z" ,time)))
86      (if (string-match "[+-][0-9][0-9][0-9][0-9]$" date)
87          (let ((length (length date)))
88            (format "%s:%s"
89                    (substring date 0 (- length 2))
90                    (substring date (- length 2) length)))
91        date)))
92
93 (defmacro mixi-atom-now ()
94   `(mixi-atom-make-date (current-time)))
95
96 (defun mixi-make-atom-entry (object)
97   "Make Atom entry."
98   (concat "<entry>\n"
99           " <title>" (mixi-make-title object) "</title>\n"
100           " <link href=\"" (mixi-make-encoded-url object) "\"/>\n"
101           " <id>" (mixi-make-tag-uri object) "</id>\n"
102           " <updated>" (mixi-atom-make-date (mixi-object-time object))
103           "</updated>\n"
104           " <summary>" (mixi-encode-specials-string
105                         (mixi-remove-markup (mixi-make-content object)))
106           "</summary>\n"
107           "</entry>\n"))
108
109 (defun mixi-make-atom-entries (objects &optional range)
110   "Make Atom entries."
111   (let (entries)
112     (mapcar (lambda (object)
113               (setq entries
114                     (concat entries (mixi-make-atom-entry object)))
115               (when (mixi-parent-p object)
116                 (let ((comments (mixi-get-comments object range)))
117                   (while comments
118                     (setq entries
119                           (concat entries
120                                   (mixi-make-atom-entry (car comments))))
121                     (setq comments (cdr comments))))))
122             objects)
123     entries))
124
125 (defun mixi-make-atom ()
126   "Make Atom Syndication Format"
127   (concat "<?xml version=\"1.0\" encoding=\""
128           (symbol-name mixi-atom-coding-system) "\"?>\n"
129           "<feed xmlns=\"" mixi-atom-namespace "\">\n"
130           "<link rel=\"self\" href=\"" mixi-atom-self "\"/>\n"
131           "\n"
132           "<title>" mixi-atom-title "</title>\n"
133           "<link href=\"" mixi-url "\"/>\n"
134           "<updated>" (mixi-atom-now) "</updated>\n"
135           "<author>\n"
136           " <name>" (mixi-friend-nick (mixi-make-me)) "</name>\n"
137           "</author>\n"
138           "<id>tag:mixi.jp," (format-time-string "%Y-%m-%d"
139                                                  (current-time))
140           ":" (mixi-friend-nick (mixi-make-me)) "</id>\n"
141           "\n"
142           (mapconcat 'identity
143                      (mapcar (lambda (list)
144                                (let ((url-or-function (car list))
145                                      (range (cdr list)))
146                                  (mixi-make-atom-entries
147                                   (mixi-make-objects url-or-function
148                                                      range) range)))
149                              mixi-atom-syndication-list)
150                      "\n")
151           "\n"
152           "</feed>\n"))
153
154 ;;;###autoload
155 (defun mixi-atom-cgi ()
156   (princ (concat "Content-Type: application/atom+xml; charset="
157                  (symbol-name mixi-atom-coding-system) "\n"
158                  "\n"
159                  (encode-coding-string (mixi-make-atom)
160                                        mixi-atom-coding-system))))
161
162 ;;;###autoload
163 (defun mixi-atom-file ()
164   (with-temp-buffer
165     (insert (mixi-make-atom))
166     (let ((coding-system-for-write mixi-atom-coding-system)
167           (file (expand-file-name mixi-atom-file)))
168       (write-region (point-min) (point-max) file))))
169
170 (provide 'mixi-atom)
171
172 ;;; mixi-atom.el ends here