7d188681de71e675021a252464f0781dfacdfe48
[gnus] / lisp / mm-extern.el
1 ;;; mm-extern.el --- showing message/external-body
2
3 ;; Copyright (C) 2000, 2001, 2002, 2003, 2004,
4 ;;   2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5
6 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
7 ;; Keywords: message external-body
8
9 ;; This file is part of GNU Emacs.
10
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.
15
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.
20
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/>.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 ;; For Emacs < 22.2.
29 (eval-and-compile
30   (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
31
32 (eval-when-compile (require 'cl))
33
34 (require 'mm-util)
35 (require 'mm-decode)
36 (require 'mm-url)
37
38 (defvar gnus-article-mime-handles)
39
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))
48     ))
49
50 (defvar mm-extern-anonymous "anonymous")
51
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))))
62
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-with-unibyte-current-buffer
71       (mm-url-insert-file-contents url))
72     (mm-disable-multibyte)
73     (setq buffer-file-name name)))
74
75 (defun mm-extern-anon-ftp (handle)
76   (erase-buffer)
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))
86     (unless name
87       (error "The filename is not specified"))
88     (mm-disable-multibyte)
89     (mm-insert-file-contents path nil nil nil nil t)))
90
91 (defun mm-extern-ftp (handle)
92   (let (mm-extern-anonymous)
93     (mm-extern-anon-ftp handle)))
94
95 (declare-function message-goto-body "message" (&optional interactivep))
96
97 (defun mm-extern-mail-server (handle)
98   (require 'message)
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))
103          info)
104     (if (y-or-n-p (format "Send a request message to %s? " server))
105         (save-window-excursion
106           (message-mail server subject)
107           (message-goto-body)
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.")
113           (message info))
114       (setq info "Request is not sent."))
115     (goto-char (point-min))
116     (insert "[" info "]\n\n")))
117
118 ;;;###autoload
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
124                            (downcase
125                             (or access-type
126                                 (error "Couldn't find access type"))))
127                           mm-extern-function-alist)))
128          buf handles)
129     (unless func
130       (error "Access type (%s) is not supported" access-type))
131     (mm-with-part handle
132       (goto-char (point-max))
133       (insert "\n\n")
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"))
139     (save-excursion
140       (set-buffer (setq buf (mm-handle-buffer handles)))
141       (let (good)
142         (unwind-protect
143             (progn
144               (funcall func handle)
145               (setq good t))
146           (unless good
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))))
151
152 ;;;###autoload
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
156 the entire message.
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))
160   (unless no-display
161     (save-excursion
162       (save-restriction
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)))
169
170 (provide 'mm-extern)
171
172 ;; arch-tag: 9653808e-14d9-4172-86e6-adceaa05378e
173 ;;; mm-extern.el ends here