Refill some copyright headers.
[bpt/emacs.git] / lisp / gnus / mm-extern.el
CommitLineData
23f87bed 1;;; mm-extern.el --- showing message/external-body
e84b4b86 2
e9bffc61
GM
3;; Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
4;; 2009, 2010, 2011 Free Software Foundation, Inc.
23f87bed
MB
5
6;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
7;; Keywords: message external-body
8
9;; This file is part of GNU Emacs.
10
5e809f55
GM
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.
23f87bed 15
5e809f55
GM
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.
23f87bed
MB
20
21;; You should have received a copy of the GNU General Public License
5e809f55 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23f87bed
MB
23
24;;; Commentary:
25
26;;; Code:
27
f0b7f5a8 28;; For Emacs <22.2 and XEmacs.
f72f1503
GM
29(eval-and-compile
30 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
31
23f87bed
MB
32(eval-when-compile (require 'cl))
33
34(require 'mm-util)
35(require 'mm-decode)
36(require 'mm-url)
37
0654efe0
JB
38(defvar gnus-article-mime-handles)
39
23f87bed
MB
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)
d7cd3710 61 (error "File %s is gone" name))))
23f87bed
MB
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"))
23f87bed 70 (mm-disable-multibyte)
3d73e841 71 (mm-url-insert-file-contents url)
23f87bed
MB
72 (setq buffer-file-name name)))
73
74(defun mm-extern-anon-ftp (handle)
75 (erase-buffer)
76 (let* ((params (cdr (mm-handle-type handle)))
77 (name (cdr (assq 'name params)))
78 (site (cdr (assq 'site params)))
79 (directory (cdr (assq 'directory params)))
80 (mode (cdr (assq 'mode params)))
81 (path (concat "/" (or mm-extern-anonymous
82 (read-string (format "ID for %s: " site)))
83 "@" site ":" directory "/" name))
84 (coding-system-for-read mm-binary-coding-system))
85 (unless name
86 (error "The filename is not specified"))
87 (mm-disable-multibyte)
88 (mm-insert-file-contents path nil nil nil nil t)))
89
90(defun mm-extern-ftp (handle)
91 (let (mm-extern-anonymous)
92 (mm-extern-anon-ftp handle)))
93
acab4d4b 94(declare-function message-goto-body "message" ())
f72f1503 95
23f87bed
MB
96(defun mm-extern-mail-server (handle)
97 (require 'message)
98 (let* ((params (cdr (mm-handle-type handle)))
99 (server (cdr (assq 'server params)))
100 (subject (or (cdr (assq 'subject params)) "none"))
101 (buf (current-buffer))
102 info)
5dab7628 103 (if (y-or-n-p (format "Send a request message to %s? " server))
23f87bed
MB
104 (save-window-excursion
105 (message-mail server subject)
106 (message-goto-body)
107 (delete-region (point) (point-max))
108 (insert-buffer-substring buf)
109 (message "Requesting external body...")
110 (message-send-and-exit)
111 (setq info "Request is sent.")
112 (message info))
113 (setq info "Request is not sent."))
114 (goto-char (point-min))
115 (insert "[" info "]\n\n")))
116
117;;;###autoload
531bedc3
MB
118(defun mm-extern-cache-contents (handle)
119 "Put the external-body part of HANDLE into its cache."
23f87bed
MB
120 (let* ((access-type (cdr (assq 'access-type
121 (cdr (mm-handle-type handle)))))
122 (func (cdr (assq (intern
123 (downcase
124 (or access-type
125 (error "Couldn't find access type"))))
126 mm-extern-function-alist)))
3d73e841 127 handles)
531bedc3
MB
128 (unless func
129 (error "Access type (%s) is not supported" access-type))
130 (mm-with-part handle
131 (goto-char (point-max))
132 (insert "\n\n")
133 ;; It should be just a single MIME handle.
134 (setq handles (mm-dissect-buffer t)))
135 (unless (bufferp (car handles))
136 (mm-destroy-parts handles)
137 (error "Multipart external body is not supported"))
3d73e841 138 (with-current-buffer (mm-handle-buffer handles)
531bedc3
MB
139 (let (good)
140 (unwind-protect
141 (progn
142 (funcall func handle)
143 (setq good t))
144 (unless good
145 (mm-destroy-parts handles))))
146 (mm-handle-set-cache handle handles))
147 (setq gnus-article-mime-handles
148 (mm-merge-handles gnus-article-mime-handles handles))))
149
150;;;###autoload
151(defun mm-inline-external-body (handle &optional no-display)
152 "Show the external-body part of HANDLE.
153This function replaces the buffer of HANDLE with a buffer contains
154the entire message.
155If NO-DISPLAY is nil, display it. Otherwise, do nothing after replacing."
156 (unless (mm-handle-cache handle)
157 (mm-extern-cache-contents handle))
158 (unless no-display
159 (save-excursion
160 (save-restriction
161 (narrow-to-region (point) (point))
163cb72d
MB
162 (mm-display-part (mm-handle-cache handle))))
163 ;; Move undisplayer added to the cached handle to the parent.
164 (mm-handle-set-undisplayer
165 handle (mm-handle-undisplayer (mm-handle-cache handle)))
166 (mm-handle-set-undisplayer (mm-handle-cache handle) nil)))
23f87bed
MB
167
168(provide 'mm-extern)
169
23f87bed 170;;; mm-extern.el ends here