1 ;;; mm-extern.el --- showing message/external-body
3 ;; Copyright (C) 2000-2012 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/>.
27 ;; For Emacs <22.2 and XEmacs.
29 (unless (fboundp 'declare-function
) (defmacro declare-function
(&rest r
))))
31 (eval-when-compile (require 'cl
))
37 (defvar gnus-article-mime-handles
)
39 (defvar mm-extern-function-alist
40 '((local-file . mm-extern-local-file
)
42 (anon-ftp . mm-extern-anon-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)
53 (let ((name (cdr (assq 'name
(cdr (mm-handle-type handle
)))))
54 (coding-system-for-read mm-binary-coding-system
))
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)
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
))
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)
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
))
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)
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))
102 (if (y-or-n-p (format "Send a request message to %s? " server
))
103 (save-window-excursion
104 (message-mail server subject
)
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.")
112 (setq info
"Request is not sent."))
113 (goto-char (point-min))
114 (insert "[" info
"]\n\n")))
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
124 (error "Couldn't find access type"))))
125 mm-extern-function-alist
)))
128 (error "Access type (%s) is not supported" access-type
))
130 (goto-char (point-max))
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
)
141 (funcall func handle
)
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
))))
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
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
))
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
)))
169 ;;; mm-extern.el ends here