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