Merge from trunk
[emacs.git] / lisp / gnus / mm-extern.el
blobf40f798789cec0aced7100bfd7b6252fc230b95c
1 ;;; mm-extern.el --- showing message/external-body
3 ;; Copyright (C) 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
7 ;; Keywords: message external-body
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;;; Code:
28 ;; For Emacs < 22.2.
29 (eval-and-compile
30 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
32 (eval-when-compile (require 'cl))
34 (require 'mm-util)
35 (require 'mm-decode)
36 (require 'mm-url)
38 (defvar gnus-article-mime-handles)
40 (defvar mm-extern-function-alist
41 '((local-file . mm-extern-local-file)
42 (url . mm-extern-url)
43 (anon-ftp . mm-extern-anon-ftp)
44 (ftp . mm-extern-ftp)
45 ;;; (tftp . mm-extern-tftp)
46 (mail-server . mm-extern-mail-server)
47 ;;; (afs . mm-extern-afs))
50 (defvar mm-extern-anonymous "anonymous")
52 (defun mm-extern-local-file (handle)
53 (erase-buffer)
54 (let ((name (cdr (assq 'name (cdr (mm-handle-type handle)))))
55 (coding-system-for-read mm-binary-coding-system))
56 (unless name
57 (error "The filename is not specified"))
58 (mm-disable-multibyte)
59 (if (file-exists-p name)
60 (mm-insert-file-contents name nil nil nil nil t)
61 (error "File %s is gone" name))))
63 (defun mm-extern-url (handle)
64 (erase-buffer)
65 (let ((url (cdr (assq 'url (cdr (mm-handle-type handle)))))
66 (name buffer-file-name)
67 (coding-system-for-read mm-binary-coding-system))
68 (unless url
69 (error "URL is not specified"))
70 (mm-disable-multibyte)
71 (mm-url-insert-file-contents url)
72 (setq buffer-file-name name)))
74 (defun mm-extern-anon-ftp (handle)
75 (erase-buffer)
76 (let* ((params (cdr (mm-handle-type handle)))
77 (name (cdr (assq 'name params)))
78 (site (cdr (assq 'site params)))
79 (directory (cdr (assq 'directory params)))
80 (mode (cdr (assq 'mode params)))
81 (path (concat "/" (or mm-extern-anonymous
82 (read-string (format "ID for %s: " site)))
83 "@" site ":" directory "/" name))
84 (coding-system-for-read mm-binary-coding-system))
85 (unless name
86 (error "The filename is not specified"))
87 (mm-disable-multibyte)
88 (mm-insert-file-contents path nil nil nil nil t)))
90 (defun mm-extern-ftp (handle)
91 (let (mm-extern-anonymous)
92 (mm-extern-anon-ftp handle)))
94 (declare-function message-goto-body "message" (&optional interactivep))
96 (defun mm-extern-mail-server (handle)
97 (require 'message)
98 (let* ((params (cdr (mm-handle-type handle)))
99 (server (cdr (assq 'server params)))
100 (subject (or (cdr (assq 'subject params)) "none"))
101 (buf (current-buffer))
102 info)
103 (if (y-or-n-p (format "Send a request message to %s? " server))
104 (save-window-excursion
105 (message-mail server subject)
106 (message-goto-body)
107 (delete-region (point) (point-max))
108 (insert-buffer-substring buf)
109 (message "Requesting external body...")
110 (message-send-and-exit)
111 (setq info "Request is sent.")
112 (message info))
113 (setq info "Request is not sent."))
114 (goto-char (point-min))
115 (insert "[" info "]\n\n")))
117 ;;;###autoload
118 (defun mm-extern-cache-contents (handle)
119 "Put the external-body part of HANDLE into its cache."
120 (let* ((access-type (cdr (assq 'access-type
121 (cdr (mm-handle-type handle)))))
122 (func (cdr (assq (intern
123 (downcase
124 (or access-type
125 (error "Couldn't find access type"))))
126 mm-extern-function-alist)))
127 handles)
128 (unless func
129 (error "Access type (%s) is not supported" access-type))
130 (mm-with-part handle
131 (goto-char (point-max))
132 (insert "\n\n")
133 ;; It should be just a single MIME handle.
134 (setq handles (mm-dissect-buffer t)))
135 (unless (bufferp (car handles))
136 (mm-destroy-parts handles)
137 (error "Multipart external body is not supported"))
138 (with-current-buffer (mm-handle-buffer handles)
139 (let (good)
140 (unwind-protect
141 (progn
142 (funcall func handle)
143 (setq good t))
144 (unless good
145 (mm-destroy-parts handles))))
146 (mm-handle-set-cache handle handles))
147 (setq gnus-article-mime-handles
148 (mm-merge-handles gnus-article-mime-handles handles))))
150 ;;;###autoload
151 (defun mm-inline-external-body (handle &optional no-display)
152 "Show the external-body part of HANDLE.
153 This function replaces the buffer of HANDLE with a buffer contains
154 the entire message.
155 If NO-DISPLAY is nil, display it. Otherwise, do nothing after replacing."
156 (unless (mm-handle-cache handle)
157 (mm-extern-cache-contents handle))
158 (unless no-display
159 (save-excursion
160 (save-restriction
161 (narrow-to-region (point) (point))
162 (mm-display-part (mm-handle-cache handle))))
163 ;; Move undisplayer added to the cached handle to the parent.
164 (mm-handle-set-undisplayer
165 handle (mm-handle-undisplayer (mm-handle-cache handle)))
166 (mm-handle-set-undisplayer (mm-handle-cache handle) nil)))
168 (provide 'mm-extern)
170 ;; arch-tag: 9653808e-14d9-4172-86e6-adceaa05378e
171 ;;; mm-extern.el ends here