Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / lisp / gnus / mm-extern.el
blob882c8545e595aaad7376eaa212777b600fd204c5
1 ;;; mm-extern.el --- showing message/external-body
3 ;; Copyright (C) 2000-2014 Free Software Foundation, Inc.
5 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
6 ;; Keywords: message external-body
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs 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 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs 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.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;;; Code:
27 ;; For Emacs <22.2 and XEmacs.
28 (eval-and-compile
29 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
31 (eval-when-compile (require 'cl))
33 (require 'mm-util)
34 (require 'mm-decode)
35 (require 'mm-url)
37 (defvar gnus-article-mime-handles)
39 (defvar mm-extern-function-alist
40 '((local-file . mm-extern-local-file)
41 (url . mm-extern-url)
42 (anon-ftp . mm-extern-anon-ftp)
43 (ftp . mm-extern-ftp)
44 ;;; (tftp . mm-extern-tftp)
45 (mail-server . mm-extern-mail-server)
46 ;;; (afs . mm-extern-afs))
49 (defvar mm-extern-anonymous "anonymous")
51 (defun mm-extern-local-file (handle)
52 (erase-buffer)
53 (let ((name (cdr (assq 'name (cdr (mm-handle-type handle)))))
54 (coding-system-for-read mm-binary-coding-system))
55 (unless name
56 (error "The filename is not specified"))
57 (mm-disable-multibyte)
58 (if (file-exists-p name)
59 (mm-insert-file-contents name nil nil nil nil t)
60 (error "File %s is gone" name))))
62 (defun mm-extern-url (handle)
63 (erase-buffer)
64 (let ((url (cdr (assq 'url (cdr (mm-handle-type handle)))))
65 (name buffer-file-name)
66 (coding-system-for-read mm-binary-coding-system))
67 (unless url
68 (error "URL is not specified"))
69 (mm-disable-multibyte)
70 (mm-url-insert-file-contents url)
71 (setq buffer-file-name name)))
73 (defun mm-extern-anon-ftp (handle)
74 (erase-buffer)
75 (let* ((params (cdr (mm-handle-type handle)))
76 (name (cdr (assq 'name params)))
77 (site (cdr (assq 'site params)))
78 (directory (cdr (assq 'directory params)))
79 (mode (cdr (assq 'mode params)))
80 (path (concat "/" (or mm-extern-anonymous
81 (read-string (format "ID for %s: " site)))
82 "@" site ":" directory "/" name))
83 (coding-system-for-read mm-binary-coding-system))
84 (unless name
85 (error "The filename is not specified"))
86 (mm-disable-multibyte)
87 (mm-insert-file-contents path nil nil nil nil t)))
89 (defun mm-extern-ftp (handle)
90 (let (mm-extern-anonymous)
91 (mm-extern-anon-ftp handle)))
93 (declare-function message-goto-body "message" ())
95 (defun mm-extern-mail-server (handle)
96 (require 'message)
97 (let* ((params (cdr (mm-handle-type handle)))
98 (server (cdr (assq 'server params)))
99 (subject (or (cdr (assq 'subject params)) "none"))
100 (buf (current-buffer))
101 info)
102 (if (y-or-n-p (format "Send a request message to %s? " server))
103 (save-window-excursion
104 (message-mail server subject)
105 (message-goto-body)
106 (delete-region (point) (point-max))
107 (insert-buffer-substring buf)
108 (message "Requesting external body...")
109 (message-send-and-exit)
110 (setq info "Request is sent.")
111 (message info))
112 (setq info "Request is not sent."))
113 (goto-char (point-min))
114 (insert "[" info "]\n\n")))
116 ;;;###autoload
117 (defun mm-extern-cache-contents (handle)
118 "Put the external-body part of HANDLE into its cache."
119 (let* ((access-type (cdr (assq 'access-type
120 (cdr (mm-handle-type handle)))))
121 (func (cdr (assq (intern
122 (downcase
123 (or access-type
124 (error "Couldn't find access type"))))
125 mm-extern-function-alist)))
126 handles)
127 (unless func
128 (error "Access type (%s) is not supported" access-type))
129 (mm-with-part handle
130 (goto-char (point-max))
131 (insert "\n\n")
132 ;; It should be just a single MIME handle.
133 (setq handles (mm-dissect-buffer t)))
134 (unless (bufferp (car handles))
135 (mm-destroy-parts handles)
136 (error "Multipart external body is not supported"))
137 (with-current-buffer (mm-handle-buffer handles)
138 (let (good)
139 (unwind-protect
140 (progn
141 (funcall func handle)
142 (setq good t))
143 (unless good
144 (mm-destroy-parts handles))))
145 (mm-handle-set-cache handle handles))
146 (setq gnus-article-mime-handles
147 (mm-merge-handles gnus-article-mime-handles handles))))
149 ;;;###autoload
150 (defun mm-inline-external-body (handle &optional no-display)
151 "Show the external-body part of HANDLE.
152 This function replaces the buffer of HANDLE with a buffer contains
153 the entire message.
154 If NO-DISPLAY is nil, display it. Otherwise, do nothing after replacing."
155 (unless (mm-handle-cache handle)
156 (mm-extern-cache-contents handle))
157 (unless no-display
158 (save-excursion
159 (save-restriction
160 (narrow-to-region (point) (point))
161 (mm-display-part (mm-handle-cache handle))))
162 ;; Move undisplayer added to the cached handle to the parent.
163 (mm-handle-set-undisplayer
164 handle (mm-handle-undisplayer (mm-handle-cache handle)))
165 (mm-handle-set-undisplayer (mm-handle-cache handle) nil)))
167 (provide 'mm-extern)
169 ;;; mm-extern.el ends here