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