1 ;;; mm-extern.el --- showing message/external-body
3 ;; Copyright (C) 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009 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/>.
30 (unless (fboundp 'declare-function
) (defmacro declare-function
(&rest r
))))
32 (eval-when-compile (require 'cl
))
38 (defvar gnus-article-mime-handles
)
40 (defvar mm-extern-function-alist
41 '((local-file . mm-extern-local-file
)
43 (anon-ftp . mm-extern-anon-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)
54 (let ((name (cdr (assq 'name
(cdr (mm-handle-type handle
)))))
55 (coding-system-for-read mm-binary-coding-system
))
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)
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
))
69 (error "URL is not specified"))
70 (mm-with-unibyte-current-buffer
71 (mm-url-insert-file-contents url
))
72 (mm-disable-multibyte)
73 (setq buffer-file-name name
)))
75 (defun mm-extern-anon-ftp (handle)
77 (let* ((params (cdr (mm-handle-type handle
)))
78 (name (cdr (assq 'name params
)))
79 (site (cdr (assq 'site params
)))
80 (directory (cdr (assq 'directory params
)))
81 (mode (cdr (assq 'mode params
)))
82 (path (concat "/" (or mm-extern-anonymous
83 (read-string (format "ID for %s: " site
)))
84 "@" site
":" directory
"/" name
))
85 (coding-system-for-read mm-binary-coding-system
))
87 (error "The filename is not specified"))
88 (mm-disable-multibyte)
89 (mm-insert-file-contents path nil nil nil nil t
)))
91 (defun mm-extern-ftp (handle)
92 (let (mm-extern-anonymous)
93 (mm-extern-anon-ftp handle
)))
95 (declare-function message-goto-body
"message" (&optional interactivep
))
97 (defun mm-extern-mail-server (handle)
99 (let* ((params (cdr (mm-handle-type handle
)))
100 (server (cdr (assq 'server params
)))
101 (subject (or (cdr (assq 'subject params
)) "none"))
102 (buf (current-buffer))
104 (if (y-or-n-p (format "Send a request message to %s? " server
))
105 (save-window-excursion
106 (message-mail server subject
)
108 (delete-region (point) (point-max))
109 (insert-buffer-substring buf
)
110 (message "Requesting external body...")
111 (message-send-and-exit)
112 (setq info
"Request is sent.")
114 (setq info
"Request is not sent."))
115 (goto-char (point-min))
116 (insert "[" info
"]\n\n")))
119 (defun mm-extern-cache-contents (handle)
120 "Put the external-body part of HANDLE into its cache."
121 (let* ((access-type (cdr (assq 'access-type
122 (cdr (mm-handle-type handle
)))))
123 (func (cdr (assq (intern
126 (error "Couldn't find access type"))))
127 mm-extern-function-alist
)))
130 (error "Access type (%s) is not supported" access-type
))
132 (goto-char (point-max))
134 ;; It should be just a single MIME handle.
135 (setq handles
(mm-dissect-buffer t
)))
136 (unless (bufferp (car handles
))
137 (mm-destroy-parts handles
)
138 (error "Multipart external body is not supported"))
140 (set-buffer (setq buf
(mm-handle-buffer handles
)))
144 (funcall func handle
)
147 (mm-destroy-parts handles
))))
148 (mm-handle-set-cache handle handles
))
149 (setq gnus-article-mime-handles
150 (mm-merge-handles gnus-article-mime-handles handles
))))
153 (defun mm-inline-external-body (handle &optional no-display
)
154 "Show the external-body part of HANDLE.
155 This function replaces the buffer of HANDLE with a buffer contains
157 If NO-DISPLAY is nil, display it. Otherwise, do nothing after replacing."
158 (unless (mm-handle-cache handle
)
159 (mm-extern-cache-contents handle
))
163 (narrow-to-region (point) (point))
164 (mm-display-part (mm-handle-cache handle
))))
165 ;; Move undisplayer added to the cached handle to the parent.
166 (mm-handle-set-undisplayer
167 handle
(mm-handle-undisplayer (mm-handle-cache handle
)))
168 (mm-handle-set-undisplayer (mm-handle-cache handle
) nil
)))
172 ;; arch-tag: 9653808e-14d9-4172-86e6-adceaa05378e
173 ;;; mm-extern.el ends here